Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: chrome/browser/android/vr_shell/ui_scene.cc

Issue 2814443004: Refactor VR math off of GVR types, onto gfx types where possible. (Closed)
Patch Set: Fix tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/vr_shell/ui_scene.h" 5 #include "chrome/browser/android/vr_shell/ui_scene.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/android/vr_shell/animation.h" 13 #include "chrome/browser/android/vr_shell/animation.h"
14 #include "chrome/browser/android/vr_shell/easing.h" 14 #include "chrome/browser/android/vr_shell/easing.h"
15 #include "chrome/browser/android/vr_shell/ui_elements.h" 15 #include "chrome/browser/android/vr_shell/ui_elements.h"
16 #include "device/vr/vr_math.h"
16 17
17 namespace vr_shell { 18 namespace vr_shell {
18 19
19 namespace { 20 namespace {
20 21
21 // Parse an integer to an int or enum value. 22 // Parse an integer to an int or enum value.
22 template <typename T> 23 template <typename T>
23 bool ParseInt(const base::DictionaryValue& dict, 24 bool ParseInt(const base::DictionaryValue& dict,
24 const std::string& key, 25 const std::string& key,
25 T* output) { 26 T* output) {
(...skipping 13 matching lines...) Expand all
39 double value; 40 double value;
40 if (!dict.GetDouble(key, &value)) { 41 if (!dict.GetDouble(key, &value)) {
41 return false; 42 return false;
42 } 43 }
43 *output = value; 44 *output = value;
44 return true; 45 return true;
45 } 46 }
46 47
47 bool ParseColorf(const base::DictionaryValue& dict, 48 bool ParseColorf(const base::DictionaryValue& dict,
48 const std::string& key, 49 const std::string& key,
49 Colorf* output) { 50 vr::Colorf* output) {
50 const base::DictionaryValue* item_dict; 51 const base::DictionaryValue* item_dict;
51 if (dict.GetDictionary(key, &item_dict)) { 52 if (dict.GetDictionary(key, &item_dict)) {
52 double value; 53 double value;
53 CHECK(item_dict->GetDouble("r", &value)); 54 CHECK(item_dict->GetDouble("r", &value));
54 output->r = value; 55 output->r = value;
55 CHECK(item_dict->GetDouble("g", &value)); 56 CHECK(item_dict->GetDouble("g", &value));
56 output->g = value; 57 output->g = value;
57 CHECK(item_dict->GetDouble("b", &value)); 58 CHECK(item_dict->GetDouble("b", &value));
58 output->b = value; 59 output->b = value;
59 CHECK(item_dict->GetDouble("a", &value)); 60 CHECK(item_dict->GetDouble("a", &value));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 143 }
143 144
144 void ApplyAnchoring(const ContentRectangle& parent, 145 void ApplyAnchoring(const ContentRectangle& parent,
145 XAnchoring x_anchoring, 146 XAnchoring x_anchoring,
146 YAnchoring y_anchoring, 147 YAnchoring y_anchoring,
147 Transform* transform) { 148 Transform* transform) {
148 // To anchor a child, use the parent's size to find its edge. 149 // To anchor a child, use the parent's size to find its edge.
149 float x_offset; 150 float x_offset;
150 switch (x_anchoring) { 151 switch (x_anchoring) {
151 case XLEFT: 152 case XLEFT:
152 x_offset = -0.5f * parent.size.x; 153 x_offset = -0.5f * parent.size.x();
153 break; 154 break;
154 case XRIGHT: 155 case XRIGHT:
155 x_offset = 0.5f * parent.size.x; 156 x_offset = 0.5f * parent.size.x();
156 break; 157 break;
157 case XNONE: 158 case XNONE:
158 x_offset = 0.0f; 159 x_offset = 0.0f;
159 break; 160 break;
160 } 161 }
161 float y_offset; 162 float y_offset;
162 switch (y_anchoring) { 163 switch (y_anchoring) {
163 case YTOP: 164 case YTOP:
164 y_offset = 0.5f * parent.size.y; 165 y_offset = 0.5f * parent.size.y();
165 break; 166 break;
166 case YBOTTOM: 167 case YBOTTOM:
167 y_offset = -0.5f * parent.size.y; 168 y_offset = -0.5f * parent.size.y();
168 break; 169 break;
169 case YNONE: 170 case YNONE:
170 y_offset = 0.0f; 171 y_offset = 0.0f;
171 break; 172 break;
172 } 173 }
173 transform->Translate(x_offset, y_offset, 0); 174 transform->Translate(gfx::Vector3dF(x_offset, y_offset, 0));
174 } 175 }
175 176
176 } // namespace 177 } // namespace
177 178
178 void UiScene::AddUiElement(std::unique_ptr<ContentRectangle> element) { 179 void UiScene::AddUiElement(std::unique_ptr<ContentRectangle> element) {
179 CHECK_GE(element->id, 0); 180 CHECK_GE(element->id, 0);
180 CHECK_EQ(GetUiElementById(element->id), nullptr); 181 CHECK_EQ(GetUiElementById(element->id), nullptr);
181 if (element->parent_id >= 0) { 182 if (element->parent_id >= 0) {
182 CHECK_NE(GetUiElementById(element->parent_id), nullptr); 183 CHECK_NE(GetUiElementById(element->parent_id), nullptr);
183 } else { 184 } else {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 elements.push_back(element.get()); 367 elements.push_back(element.get());
367 } 368 }
368 } 369 }
369 return elements; 370 return elements;
370 } 371 }
371 372
372 bool UiScene::HasVisibleHeadLockedElements() const { 373 bool UiScene::HasVisibleHeadLockedElements() const {
373 return !GetHeadLockedElements().empty(); 374 return !GetHeadLockedElements().empty();
374 } 375 }
375 376
376 const Colorf& UiScene::GetBackgroundColor() const { 377 const vr::Colorf& UiScene::GetBackgroundColor() const {
377 return background_color_; 378 return background_color_;
378 } 379 }
379 380
380 float UiScene::GetBackgroundDistance() const { 381 float UiScene::GetBackgroundDistance() const {
381 return background_distance_; 382 return background_distance_;
382 } 383 }
383 384
384 bool UiScene::GetWebVrRenderingEnabled() const { 385 bool UiScene::GetWebVrRenderingEnabled() const {
385 return webvr_rendering_enabled_; 386 return webvr_rendering_enabled_;
386 } 387 }
(...skipping 12 matching lines...) Expand all
399 return; 400 return;
400 401
401 ContentRectangle* parent = nullptr; 402 ContentRectangle* parent = nullptr;
402 if (element->parent_id >= 0) { 403 if (element->parent_id >= 0) {
403 parent = GetUiElementById(element->parent_id); 404 parent = GetUiElementById(element->parent_id);
404 CHECK(parent != nullptr); 405 CHECK(parent != nullptr);
405 } 406 }
406 407
407 Transform* transform = element->mutable_transform(); 408 Transform* transform = element->mutable_transform();
408 transform->MakeIdentity(); 409 transform->MakeIdentity();
409 transform->Scale(element->size.x, element->size.y, element->size.z); 410 transform->Scale(element->size);
410 element->computed_opacity = element->opacity; 411 element->computed_opacity = element->opacity;
411 element->computed_lock_to_fov = element->lock_to_fov; 412 element->computed_lock_to_fov = element->lock_to_fov;
412 413
413 // Compute an inheritable transformation that can be applied to this element, 414 // Compute an inheritable transformation that can be applied to this element,
414 // and it's children, if applicable. 415 // and it's children, if applicable.
415 Transform* inheritable = &element->inheritable_transform; 416 Transform* inheritable = &element->inheritable_transform;
416 inheritable->MakeIdentity(); 417 inheritable->MakeIdentity();
417 inheritable->Scale(element->scale.x, element->scale.y, element->scale.z); 418 inheritable->Scale(element->scale);
418 inheritable->Rotate(element->rotation.x, element->rotation.y, 419 inheritable->Rotate(element->rotation);
419 element->rotation.z, element->rotation.angle); 420 inheritable->Translate(element->translation);
420 inheritable->Translate(element->translation.x, element->translation.y,
421 element->translation.z);
422 if (parent) { 421 if (parent) {
423 ApplyAnchoring(*parent, element->x_anchoring, element->y_anchoring, 422 ApplyAnchoring(*parent, element->x_anchoring, element->y_anchoring,
424 inheritable); 423 inheritable);
425 ApplyRecursiveTransforms(parent); 424 ApplyRecursiveTransforms(parent);
426 inheritable->to_world = MatrixMul(parent->inheritable_transform.to_world, 425 vr::MatrixMul(parent->inheritable_transform.to_world, inheritable->to_world,
427 inheritable->to_world); 426 &inheritable->to_world);
428 427
429 element->computed_opacity *= parent->opacity; 428 element->computed_opacity *= parent->opacity;
430 element->computed_lock_to_fov = parent->lock_to_fov; 429 element->computed_lock_to_fov = parent->lock_to_fov;
431 } 430 }
432 431
433 transform->to_world = MatrixMul(inheritable->to_world, transform->to_world); 432 vr::MatrixMul(inheritable->to_world, transform->to_world,
433 &transform->to_world);
434 element->dirty = false; 434 element->dirty = false;
435 } 435 }
436 436
437 void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, 437 void UiScene::ApplyDictToElement(const base::DictionaryValue& dict,
438 ContentRectangle* element) { 438 ContentRectangle* element) {
439 int parent_id; 439 int parent_id;
440 440
441 if (ParseInt(dict, "parentId", &parent_id)) { 441 if (ParseInt(dict, "parentId", &parent_id)) {
442 CHECK_GE(parent_id, 0); 442 CHECK_GE(parent_id, 0);
443 CHECK_NE(GetUiElementById(parent_id), nullptr); 443 CHECK_NE(GetUiElementById(parent_id), nullptr);
444 element->parent_id = parent_id; 444 element->parent_id = parent_id;
445 } 445 }
446 446
447 dict.GetString("name", &element->name); 447 dict.GetString("name", &element->name);
448 dict.GetBoolean("visible", &element->visible); 448 dict.GetBoolean("visible", &element->visible);
449 dict.GetBoolean("hitTestable", &element->hit_testable); 449 dict.GetBoolean("hitTestable", &element->hit_testable);
450 dict.GetBoolean("lockToFov", &element->lock_to_fov); 450 dict.GetBoolean("lockToFov", &element->lock_to_fov);
451 ParseInt(dict, "drawPhase", &element->draw_phase); 451 ParseInt(dict, "drawPhase", &element->draw_phase);
452 ParseFloat(dict, "opacity", &element->opacity); 452 ParseFloat(dict, "opacity", &element->opacity);
453 453
454 DCHECK(!(element->lock_to_fov && element->parent_id != -1)); 454 DCHECK(!(element->lock_to_fov && element->parent_id != -1));
455 455 float val;
456 ParseFloat(dict, "sizeX", &element->size.x); 456 ParseFloat(dict, "sizeX", &val);
457 ParseFloat(dict, "sizeY", &element->size.y); 457 element->size.set_x(val);
458 ParseFloat(dict, "scaleX", &element->scale.x); 458 ParseFloat(dict, "sizeY", &val);
459 ParseFloat(dict, "scaleY", &element->scale.y); 459 element->size.set_y(val);
460 ParseFloat(dict, "scaleZ", &element->scale.z); 460 ParseFloat(dict, "scaleX", &val);
461 element->scale.set_x(val);
462 ParseFloat(dict, "scaleY", &val);
463 element->scale.set_y(val);
464 ParseFloat(dict, "scaleZ", &val);
465 element->scale.set_z(val);
466 ParseFloat(dict, "translationX", &val);
467 element->translation.set_x(val);
468 ParseFloat(dict, "translationY", &val);
469 element->translation.set_y(val);
470 ParseFloat(dict, "translationZ", &val);
471 element->translation.set_z(val);
461 ParseFloat(dict, "rotationX", &element->rotation.x); 472 ParseFloat(dict, "rotationX", &element->rotation.x);
462 ParseFloat(dict, "rotationY", &element->rotation.y); 473 ParseFloat(dict, "rotationY", &element->rotation.y);
463 ParseFloat(dict, "rotationZ", &element->rotation.z); 474 ParseFloat(dict, "rotationZ", &element->rotation.z);
464 ParseFloat(dict, "rotationAngle", &element->rotation.angle); 475 ParseFloat(dict, "rotationAngle", &element->rotation.angle);
465 ParseFloat(dict, "translationX", &element->translation.x);
466 ParseFloat(dict, "translationY", &element->translation.y);
467 ParseFloat(dict, "translationZ", &element->translation.z);
468 476
469 if (ParseInt(dict, "xAnchoring", &element->x_anchoring)) { 477 if (ParseInt(dict, "xAnchoring", &element->x_anchoring)) {
470 CHECK_GE(element->parent_id, 0); 478 CHECK_GE(element->parent_id, 0);
471 } 479 }
472 if (ParseInt(dict, "yAnchoring", &element->y_anchoring)) { 480 if (ParseInt(dict, "yAnchoring", &element->y_anchoring)) {
473 CHECK_GE(element->parent_id, 0); 481 CHECK_GE(element->parent_id, 0);
474 } 482 }
475 483
476 // Parse the element fill. 484 // Parse the element fill.
477 if (ParseInt(dict, "fillType", &element->fill)) { 485 if (ParseInt(dict, "fillType", &element->fill)) {
478 // If the previous content element has a new filling now make sure this is 486 // If the previous content element has a new filling now make sure this is
479 // tracked correctly. 487 // tracked correctly.
480 if (content_element_ == element && element->fill != Fill::CONTENT) { 488 if (content_element_ == element && element->fill != Fill::CONTENT) {
481 content_element_ = nullptr; 489 content_element_ = nullptr;
482 } 490 }
483 491
492 int val;
484 switch (element->fill) { 493 switch (element->fill) {
485 case Fill::SPRITE: 494 case Fill::SPRITE:
486 CHECK(ParseInt(dict, "copyRectX", &element->copy_rect.x)); 495 CHECK(ParseInt(dict, "copyRectX", &val));
487 CHECK(ParseInt(dict, "copyRectY", &element->copy_rect.y)); 496 element->copy_rect.set_x(val);
488 CHECK(ParseInt(dict, "copyRectWidth", &element->copy_rect.width)); 497 CHECK(ParseInt(dict, "copyRectY", &val));
489 CHECK(ParseInt(dict, "copyRectHeight", &element->copy_rect.height)); 498 element->copy_rect.set_y(val);
499 CHECK(ParseInt(dict, "copyRectWidth", &val));
500 element->copy_rect.set_width(val);
501 CHECK(ParseInt(dict, "copyRectHeight", &val));
502 element->copy_rect.set_height(val);
490 break; 503 break;
491 case Fill::OPAQUE_GRADIENT: 504 case Fill::OPAQUE_GRADIENT:
492 CHECK(ParseColorf(dict, "edgeColor", &element->edge_color)); 505 CHECK(ParseColorf(dict, "edgeColor", &element->edge_color));
493 CHECK(ParseColorf(dict, "centerColor", &element->center_color)); 506 CHECK(ParseColorf(dict, "centerColor", &element->center_color));
494 break; 507 break;
495 case Fill::GRID_GRADIENT: 508 case Fill::GRID_GRADIENT:
496 CHECK(ParseColorf(dict, "edgeColor", &element->edge_color)); 509 CHECK(ParseColorf(dict, "edgeColor", &element->edge_color));
497 CHECK(ParseColorf(dict, "centerColor", &element->center_color)); 510 CHECK(ParseColorf(dict, "centerColor", &element->center_color));
498 CHECK(ParseInt(dict, "gridlineCount", &element->gridline_count)); 511 CHECK(ParseInt(dict, "gridlineCount", &element->gridline_count));
499 CHECK_GE(element->gridline_count, 0); 512 CHECK_GE(element->gridline_count, 0);
500 break; 513 break;
501 case Fill::CONTENT: 514 case Fill::CONTENT:
502 CHECK_EQ(content_element_, nullptr); 515 CHECK_EQ(content_element_, nullptr);
503 content_element_ = element; 516 content_element_ = element;
504 break; 517 break;
505 default: 518 default:
506 element->fill = Fill::NONE; 519 element->fill = Fill::NONE;
507 break; 520 break;
508 } 521 }
509 } 522 }
510 } 523 }
511 524
512 } // namespace vr_shell 525 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/ui_scene.h ('k') | chrome/browser/android/vr_shell/ui_scene_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698