| OLD | NEW |
| 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/vr_shell_gl.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" |
| 6 | 6 |
| 7 #include <chrono> | 7 #include <chrono> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 static constexpr int kViewportListHeadlockedOffset = 2; | 79 static constexpr int kViewportListHeadlockedOffset = 2; |
| 80 | 80 |
| 81 // Buffer size large enough to handle the current backlog of poses which is | 81 // Buffer size large enough to handle the current backlog of poses which is |
| 82 // 2-3 frames. | 82 // 2-3 frames. |
| 83 static constexpr unsigned kPoseRingBufferSize = 8; | 83 static constexpr unsigned kPoseRingBufferSize = 8; |
| 84 | 84 |
| 85 // Criteria for considering holding the app button in combination with | 85 // Criteria for considering holding the app button in combination with |
| 86 // controller movement as a gesture. | 86 // controller movement as a gesture. |
| 87 static constexpr float kMinAppButtonGestureAngleRad = 0.25; | 87 static constexpr float kMinAppButtonGestureAngleRad = 0.25; |
| 88 | 88 |
| 89 static constexpr gfx::PointF kInvalidTargetPoint = |
| 90 gfx::PointF(std::numeric_limits<float>::max(), |
| 91 std::numeric_limits<float>::max()); |
| 92 |
| 89 // Generate a quaternion representing the rotation from the negative Z axis | 93 // Generate a quaternion representing the rotation from the negative Z axis |
| 90 // (0, 0, -1) to a specified vector. This is an optimized version of a more | 94 // (0, 0, -1) to a specified vector. This is an optimized version of a more |
| 91 // general vector-to-vector calculation. | 95 // general vector-to-vector calculation. |
| 92 vr::Quatf GetRotationFromZAxis(gfx::Vector3dF vec) { | 96 vr::Quatf GetRotationFromZAxis(gfx::Vector3dF vec) { |
| 93 vr::NormalizeVector(&vec); | 97 vr::NormalizeVector(&vec); |
| 94 vr::Quatf quat; | 98 vr::Quatf quat; |
| 95 quat.qw = 1.0f - vec.z(); | 99 quat.qw = 1.0f - vec.z(); |
| 96 if (quat.qw < 1e-6f) { | 100 if (quat.qw < 1e-6f) { |
| 97 // Degenerate case: vectors are exactly opposite. Replace by an | 101 // Degenerate case: vectors are exactly opposite. Replace by an |
| 98 // arbitrary 180 degree rotation to avoid invalid normalization. | 102 // arbitrary 180 degree rotation to avoid invalid normalization. |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 | 536 |
| 533 vr::Mat4f mat; | 537 vr::Mat4f mat; |
| 534 QuatToMatrix(controller_quat_, &mat); | 538 QuatToMatrix(controller_quat_, &mat); |
| 535 gfx::Vector3dF controller_direction = | 539 gfx::Vector3dF controller_direction = |
| 536 vr::MatrixVectorMul(mat, ergo_neutral_pose); | 540 vr::MatrixVectorMul(mat, ergo_neutral_pose); |
| 537 | 541 |
| 538 HandleControllerAppButtonActivity(controller_direction); | 542 HandleControllerAppButtonActivity(controller_direction); |
| 539 | 543 |
| 540 if (ShouldDrawWebVr()) | 544 if (ShouldDrawWebVr()) |
| 541 return; | 545 return; |
| 542 gfx::PointF target_local_point; | 546 gfx::PointF target_local_point(kInvalidTargetPoint); |
| 543 gfx::Vector3dF eye_to_target; | 547 gfx::Vector3dF eye_to_target; |
| 544 reticle_render_target_ = nullptr; | 548 reticle_render_target_ = nullptr; |
| 545 GetVisualTargetElement(controller_direction, eye_to_target, target_point_, | 549 GetVisualTargetElement(controller_direction, eye_to_target, target_point_, |
| 546 &reticle_render_target_, target_local_point); | 550 &reticle_render_target_, target_local_point); |
| 547 | 551 |
| 548 UiElement* target_element = nullptr; | 552 UiElement* target_element = nullptr; |
| 549 if (input_locked_element_) { | 553 if (input_locked_element_) { |
| 550 gfx::Point3F plane_intersection_point; | 554 gfx::Point3F plane_intersection_point; |
| 551 float distance_to_plane; | 555 float distance_to_plane; |
| 552 GetTargetLocalPoint(eye_to_target, *input_locked_element_, | 556 if (!GetTargetLocalPoint(eye_to_target, *input_locked_element_, |
| 553 2 * scene_->GetBackgroundDistance(), target_local_point, | 557 2 * scene_->GetBackgroundDistance(), |
| 554 plane_intersection_point, distance_to_plane); | 558 target_local_point, plane_intersection_point, |
| 559 distance_to_plane)) { |
| 560 target_local_point = kInvalidTargetPoint; |
| 561 } |
| 555 target_element = input_locked_element_; | 562 target_element = input_locked_element_; |
| 556 } else if (!in_scroll_ && !in_click_) { | 563 } else if (!in_scroll_ && !in_click_) { |
| 557 target_element = reticle_render_target_; | 564 target_element = reticle_render_target_; |
| 558 } | 565 } |
| 559 | 566 |
| 560 // Handle input targeting on the content quad, ignoring any other elements. | 567 // Handle input targeting on the content quad, ignoring any other elements. |
| 561 // Content is treated specially to accomodate scrolling, flings, etc. | 568 // Content is treated specially to accomodate scrolling, flings, etc. |
| 562 gfx::Point local_point_pixels; | 569 gfx::Point local_point_pixels; |
| 563 if (target_element && (target_element->fill() == Fill::CONTENT)) { | 570 if (target_element && (target_element->fill() == Fill::CONTENT)) { |
| 564 gfx::RectF pixel_rect(0, 0, content_tex_css_width_, | 571 local_point_pixels.set_x(content_tex_css_width_ * target_local_point.x()); |
| 565 content_tex_css_height_); | 572 local_point_pixels.set_y(content_tex_css_height_ * target_local_point.y()); |
| 566 local_point_pixels.set_x(pixel_rect.x() + | |
| 567 pixel_rect.width() * target_local_point.x()); | |
| 568 local_point_pixels.set_y(pixel_rect.y() + | |
| 569 pixel_rect.height() * target_local_point.y()); | |
| 570 } | 573 } |
| 571 std::unique_ptr<GestureList> gesture_list_ptr = controller_->DetectGestures(); | 574 std::unique_ptr<GestureList> gesture_list_ptr = controller_->DetectGestures(); |
| 572 GestureList& gesture_list = *gesture_list_ptr; | 575 GestureList& gesture_list = *gesture_list_ptr; |
| 573 for (const std::unique_ptr<blink::WebGestureEvent>& gesture : gesture_list) { | 576 for (const std::unique_ptr<blink::WebGestureEvent>& gesture : gesture_list) { |
| 574 gesture->x = local_point_pixels.x(); | 577 gesture->x = local_point_pixels.x(); |
| 575 gesture->y = local_point_pixels.y(); | 578 gesture->y = local_point_pixels.y(); |
| 576 } | 579 } |
| 577 SendFlingCancel(gesture_list); | 580 SendFlingCancel(gesture_list); |
| 578 // For simplicity, don't allow scrolling while clicking until we need to. | 581 // For simplicity, don't allow scrolling while clicking until we need to. |
| 579 if (!in_click_) { | 582 if (!in_click_) { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 return; | 740 return; |
| 738 target->OnButtonDown(target_point); | 741 target->OnButtonDown(target_point); |
| 739 } | 742 } |
| 740 | 743 |
| 741 bool VrShellGl::SendButtonUp(UiElement* target, | 744 bool VrShellGl::SendButtonUp(UiElement* target, |
| 742 const gfx::PointF& target_point) { | 745 const gfx::PointF& target_point) { |
| 743 if (!in_click_) | 746 if (!in_click_) |
| 744 return false; | 747 return false; |
| 745 if (!controller_->ButtonUpHappened(gvr::kControllerButtonClick)) | 748 if (!controller_->ButtonUpHappened(gvr::kControllerButtonClick)) |
| 746 return false; | 749 return false; |
| 750 in_click_ = false; |
| 751 if (!input_locked_element_) |
| 752 return true; |
| 747 DCHECK(input_locked_element_ == target); | 753 DCHECK(input_locked_element_ == target); |
| 748 input_locked_element_ = nullptr; | 754 input_locked_element_ = nullptr; |
| 749 in_click_ = false; | |
| 750 // We don't support down/up for content yet. | 755 // We don't support down/up for content yet. |
| 751 if (target->fill() == Fill::CONTENT) | 756 if (target->fill() == Fill::CONTENT) |
| 752 return false; | 757 return false; |
| 753 target->OnButtonUp(target_point); | 758 target->OnButtonUp(target_point); |
| 754 return true; | 759 return true; |
| 755 } | 760 } |
| 756 | 761 |
| 757 void VrShellGl::SendTap(UiElement* target, | 762 void VrShellGl::SendTap(UiElement* target, |
| 758 const gfx::PointF& target_point, | 763 const gfx::PointF& target_point, |
| 759 const gfx::Point& local_point_pixels) { | 764 const gfx::Point& local_point_pixels) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 for (auto& element : scene_->GetUiElements()) { | 817 for (auto& element : scene_->GetUiElements()) { |
| 813 if (!element->IsHitTestable()) | 818 if (!element->IsHitTestable()) |
| 814 continue; | 819 continue; |
| 815 gfx::PointF local_point; | 820 gfx::PointF local_point; |
| 816 gfx::Point3F plane_intersection_point; | 821 gfx::Point3F plane_intersection_point; |
| 817 float distance_to_plane; | 822 float distance_to_plane; |
| 818 if (!GetTargetLocalPoint(eye_to_target, *element.get(), | 823 if (!GetTargetLocalPoint(eye_to_target, *element.get(), |
| 819 closest_element_distance, local_point, | 824 closest_element_distance, local_point, |
| 820 plane_intersection_point, distance_to_plane)) | 825 plane_intersection_point, distance_to_plane)) |
| 821 continue; | 826 continue; |
| 822 | 827 if (!element->HitTest(local_point)) |
| 823 if (local_point.x() < 0.0f || local_point.x() >= 1.0f || | |
| 824 local_point.y() < 0.0f || local_point.y() >= 1.0f) | |
| 825 continue; | 828 continue; |
| 826 | 829 |
| 827 closest_element_distance = distance_to_plane; | 830 closest_element_distance = distance_to_plane; |
| 828 target_point = plane_intersection_point; | 831 target_point = plane_intersection_point; |
| 829 *target_element = element.get(); | 832 *target_element = element.get(); |
| 830 target_local_point = local_point; | 833 target_local_point = local_point; |
| 831 } | 834 } |
| 832 } | 835 } |
| 833 | 836 |
| 834 bool VrShellGl::GetTargetLocalPoint(const gfx::Vector3dF& eye_to_target, | 837 bool VrShellGl::GetTargetLocalPoint(const gfx::Vector3dF& eye_to_target, |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1476 // This assumes that the initial webvr_surface_size_ was set to the | 1479 // This assumes that the initial webvr_surface_size_ was set to the |
| 1477 // appropriate recommended render resolution as the default size during | 1480 // appropriate recommended render resolution as the default size during |
| 1478 // InitializeGl. Revisit if the initialization order changes. | 1481 // InitializeGl. Revisit if the initialization order changes. |
| 1479 device::mojom::VRDisplayInfoPtr info = | 1482 device::mojom::VRDisplayInfoPtr info = |
| 1480 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), | 1483 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), |
| 1481 webvr_surface_size_, device_id); | 1484 webvr_surface_size_, device_id); |
| 1482 browser_->RunVRDisplayInfoCallback(callback, &info); | 1485 browser_->RunVRDisplayInfoCallback(callback, &info); |
| 1483 } | 1486 } |
| 1484 | 1487 |
| 1485 } // namespace vr_shell | 1488 } // namespace vr_shell |
| OLD | NEW |