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

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

Powered by Google App Engine
This is Rietveld 408576698