| 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 |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/android/vr_shell/fps_meter.h" | 16 #include "chrome/browser/android/vr_shell/fps_meter.h" |
| 17 #include "chrome/browser/android/vr_shell/mailbox_to_surface_bridge.h" | 17 #include "chrome/browser/android/vr_shell/mailbox_to_surface_bridge.h" |
| 18 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" | 18 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" |
| 19 #include "chrome/browser/android/vr_shell/ui_interface.h" | 19 #include "chrome/browser/android/vr_shell/ui_interface.h" |
| 20 #include "chrome/browser/android/vr_shell/ui_scene.h" | 20 #include "chrome/browser/android/vr_shell/ui_scene.h" |
| 21 #include "chrome/browser/android/vr_shell/ui_scene_manager.h" | 21 #include "chrome/browser/android/vr_shell/ui_scene_manager.h" |
| 22 #include "chrome/browser/android/vr_shell/vr_controller.h" | 22 #include "chrome/browser/android/vr_shell/vr_controller.h" |
| 23 #include "chrome/browser/android/vr_shell/vr_gl_thread.h" |
| 23 #include "chrome/browser/android/vr_shell/vr_gl_util.h" | 24 #include "chrome/browser/android/vr_shell/vr_gl_util.h" |
| 24 #include "chrome/browser/android/vr_shell/vr_shell.h" | 25 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 25 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 26 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 26 #include "device/vr/android/gvr/gvr_delegate.h" | 27 #include "device/vr/android/gvr/gvr_delegate.h" |
| 27 #include "device/vr/android/gvr/gvr_device.h" | 28 #include "device/vr/android/gvr/gvr_device.h" |
| 28 #include "device/vr/android/gvr/gvr_gamepad_data_provider.h" | 29 #include "device/vr/android/gvr/gvr_gamepad_data_provider.h" |
| 29 #include "device/vr/vr_math.h" | 30 #include "device/vr/vr_math.h" |
| 30 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 31 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 31 #include "third_party/WebKit/public/platform/WebMouseEvent.h" | 32 #include "third_party/WebKit/public/platform/WebMouseEvent.h" |
| 32 #include "ui/gl/android/scoped_java_surface.h" | 33 #include "ui/gl/android/scoped_java_surface.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 1.0f - rect.y()}; | 178 1.0f - rect.y()}; |
| 178 } | 179 } |
| 179 | 180 |
| 180 gfx::RectF GfxRectFromUV(gvr::Rectf rect) { | 181 gfx::RectF GfxRectFromUV(gvr::Rectf rect) { |
| 181 return gfx::RectF(rect.left, 1.0 - rect.top, rect.right - rect.left, | 182 return gfx::RectF(rect.left, 1.0 - rect.top, rect.right - rect.left, |
| 182 rect.top - rect.bottom); | 183 rect.top - rect.bottom); |
| 183 } | 184 } |
| 184 | 185 |
| 185 } // namespace | 186 } // namespace |
| 186 | 187 |
| 187 VrShellGl::VrShellGl( | 188 VrShellGl::VrShellGl(VrBrowserInterface* browser, |
| 188 const base::WeakPtr<VrShell>& weak_vr_shell, | 189 gvr_context* gvr_api, |
| 189 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 190 bool initially_web_vr, |
| 190 gvr_context* gvr_api, | 191 bool reprojected_rendering, |
| 191 bool initially_web_vr, | 192 UiScene* scene) |
| 192 bool reprojected_rendering, | |
| 193 UiScene* scene) | |
| 194 : web_vr_mode_(initially_web_vr), | 193 : web_vr_mode_(initially_web_vr), |
| 195 surfaceless_rendering_(reprojected_rendering), | 194 surfaceless_rendering_(reprojected_rendering), |
| 196 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 195 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 197 binding_(this), | 196 binding_(this), |
| 198 weak_vr_shell_(weak_vr_shell), | 197 browser_(browser), |
| 199 main_thread_task_runner_(std::move(main_thread_task_runner)), | |
| 200 scene_(scene), | 198 scene_(scene), |
| 201 #if DCHECK_IS_ON() | 199 #if DCHECK_IS_ON() |
| 202 fps_meter_(new FPSMeter()), | 200 fps_meter_(new FPSMeter()), |
| 203 #endif | 201 #endif |
| 204 weak_ptr_factory_(this) { | 202 weak_ptr_factory_(this) { |
| 205 GvrInit(gvr_api); | 203 GvrInit(gvr_api); |
| 206 } | 204 } |
| 207 | 205 |
| 208 VrShellGl::~VrShellGl() { | 206 VrShellGl::~VrShellGl() { |
| 209 vsync_task_.Cancel(); | 207 vsync_task_.Cancel(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 290 |
| 293 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); | 291 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); |
| 294 OnVSync(); | 292 OnVSync(); |
| 295 | 293 |
| 296 ready_to_draw_ = true; | 294 ready_to_draw_ = true; |
| 297 } | 295 } |
| 298 | 296 |
| 299 void VrShellGl::CreateContentSurface() { | 297 void VrShellGl::CreateContentSurface() { |
| 300 content_surface_ = | 298 content_surface_ = |
| 301 base::MakeUnique<gl::ScopedJavaSurface>(content_surface_texture_.get()); | 299 base::MakeUnique<gl::ScopedJavaSurface>(content_surface_texture_.get()); |
| 302 main_thread_task_runner_->PostTask( | 300 browser_->ContentSurfaceChanged(content_surface_->j_surface().obj()); |
| 303 FROM_HERE, base::Bind(&VrShell::ContentSurfaceChanged, weak_vr_shell_, | |
| 304 content_surface_->j_surface().obj())); | |
| 305 } | 301 } |
| 306 | 302 |
| 307 void VrShellGl::CreateOrResizeWebVRSurface(const gfx::Size& size) { | 303 void VrShellGl::CreateOrResizeWebVRSurface(const gfx::Size& size) { |
| 308 if (!webvr_surface_texture_) { | 304 if (!webvr_surface_texture_) { |
| 309 DLOG(ERROR) << "No WebVR surface texture available"; | 305 DLOG(ERROR) << "No WebVR surface texture available"; |
| 310 return; | 306 return; |
| 311 } | 307 } |
| 312 | 308 |
| 313 // ContentPhysicalBoundsChanged is getting called twice with | 309 // ContentPhysicalBoundsChanged is getting called twice with |
| 314 // identical sizes? Avoid thrashing the existing context. | 310 // identical sizes? Avoid thrashing the existing context. |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, | 486 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, |
| 491 webvr_left_viewport_.get()); | 487 webvr_left_viewport_.get()); |
| 492 webvr_left_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); | 488 webvr_left_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); |
| 493 | 489 |
| 494 webvr_right_viewport_.reset( | 490 webvr_right_viewport_.reset( |
| 495 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); | 491 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); |
| 496 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | 492 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, |
| 497 webvr_right_viewport_.get()); | 493 webvr_right_viewport_.get()); |
| 498 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); | 494 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); |
| 499 | 495 |
| 500 main_thread_task_runner_->PostTask( | 496 browser_->GvrDelegateReady(); |
| 501 FROM_HERE, base::Bind(&VrShell::GvrDelegateReady, weak_vr_shell_)); | |
| 502 } | 497 } |
| 503 | 498 |
| 504 void VrShellGl::UpdateController(const gfx::Vector3dF& head_direction) { | 499 void VrShellGl::UpdateController(const gfx::Vector3dF& head_direction) { |
| 505 controller_->UpdateState(head_direction); | 500 controller_->UpdateState(head_direction); |
| 506 pointer_start_ = controller_->GetPointerStart(); | 501 pointer_start_ = controller_->GetPointerStart(); |
| 507 | 502 |
| 508 device::GvrGamepadData pad = controller_->GetGamepadData(); | 503 browser_->UpdateGamepadData(controller_->GetGamepadData()); |
| 509 main_thread_task_runner_->PostTask( | |
| 510 FROM_HERE, base::Bind(&VrShell::UpdateGamepadData, weak_vr_shell_, pad)); | |
| 511 } | 504 } |
| 512 | 505 |
| 513 void VrShellGl::HandleControllerInput(const gfx::Vector3dF& head_direction) { | 506 void VrShellGl::HandleControllerInput(const gfx::Vector3dF& head_direction) { |
| 514 if (ShouldDrawWebVr()) { | 507 if (ShouldDrawWebVr()) { |
| 515 // Process screen touch events for Cardboard button compatibility. | 508 // Process screen touch events for Cardboard button compatibility. |
| 516 // Also send tap events for controller "touchpad click" events. | 509 // Also send tap events for controller "touchpad click" events. |
| 517 if (touch_pending_ || | 510 if (touch_pending_ || |
| 518 controller_->ButtonUpHappened( | 511 controller_->ButtonUpHappened( |
| 519 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) { | 512 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) { |
| 520 touch_pending_ = false; | 513 touch_pending_ = false; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 // considered a regular click | 633 // considered a regular click |
| 641 // TODO(asimjour1): We need to refactor the gesture recognition outside of | 634 // TODO(asimjour1): We need to refactor the gesture recognition outside of |
| 642 // VrShellGl. | 635 // VrShellGl. |
| 643 UiInterface::Direction direction = UiInterface::NONE; | 636 UiInterface::Direction direction = UiInterface::NONE; |
| 644 float gesture_xz_angle; | 637 float gesture_xz_angle; |
| 645 if (vr::XZAngle(controller_start_direction_, controller_direction, | 638 if (vr::XZAngle(controller_start_direction_, controller_direction, |
| 646 &gesture_xz_angle)) { | 639 &gesture_xz_angle)) { |
| 647 if (fabs(gesture_xz_angle) > kMinAppButtonGestureAngleRad) { | 640 if (fabs(gesture_xz_angle) > kMinAppButtonGestureAngleRad) { |
| 648 direction = | 641 direction = |
| 649 gesture_xz_angle < 0 ? UiInterface::LEFT : UiInterface::RIGHT; | 642 gesture_xz_angle < 0 ? UiInterface::LEFT : UiInterface::RIGHT; |
| 650 main_thread_task_runner_->PostTask( | 643 browser_->AppButtonGesturePerformed(direction); |
| 651 FROM_HERE, base::Bind(&VrShell::AppButtonGesturePerformed, | |
| 652 weak_vr_shell_, direction)); | |
| 653 } | 644 } |
| 654 } | 645 } |
| 655 if (direction == UiInterface::NONE) { | 646 if (direction == UiInterface::NONE) |
| 656 main_thread_task_runner_->PostTask( | 647 browser_->OnAppButtonClicked(); |
| 657 FROM_HERE, base::Bind(&VrShell::AppButtonPressed, weak_vr_shell_)); | |
| 658 } | |
| 659 } | 648 } |
| 660 } | 649 } |
| 661 | 650 |
| 662 void VrShellGl::SendInputToContent(InputTarget input_target, | 651 void VrShellGl::SendInputToContent(InputTarget input_target, |
| 663 int pixel_x, | 652 int pixel_x, |
| 664 int pixel_y) { | 653 int pixel_y) { |
| 665 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list = | 654 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list = |
| 666 controller_->DetectGestures(); | 655 controller_->DetectGestures(); |
| 667 double timestamp = gesture_list.front()->TimeStampSeconds(); | 656 double timestamp = gesture_list.front()->TimeStampSeconds(); |
| 668 | 657 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 base::Bind(&UiElement::OnButtonUp, | 762 base::Bind(&UiElement::OnButtonUp, |
| 774 base::Unretained(target_element))); | 763 base::Unretained(target_element))); |
| 775 } | 764 } |
| 776 click_target_element_ = nullptr; | 765 click_target_element_ = nullptr; |
| 777 } | 766 } |
| 778 } | 767 } |
| 779 } | 768 } |
| 780 | 769 |
| 781 void VrShellGl::SendGestureToContent( | 770 void VrShellGl::SendGestureToContent( |
| 782 std::unique_ptr<blink::WebInputEvent> event) { | 771 std::unique_ptr<blink::WebInputEvent> event) { |
| 783 main_thread_task_runner_->PostTask( | 772 browser_->ProcessContentGesture(std::move(event)); |
| 784 FROM_HERE, base::Bind(&VrShell::ProcessContentGesture, weak_vr_shell_, | |
| 785 base::Passed(std::move(event)))); | |
| 786 } | 773 } |
| 787 | 774 |
| 788 void VrShellGl::DrawFrame(int16_t frame_index) { | 775 void VrShellGl::DrawFrame(int16_t frame_index) { |
| 789 TRACE_EVENT1("gpu", "VrShellGl::DrawFrame", "frame", frame_index); | 776 TRACE_EVENT1("gpu", "VrShellGl::DrawFrame", "frame", frame_index); |
| 790 | 777 |
| 791 base::TimeTicks current_time = base::TimeTicks::Now(); | 778 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 792 | 779 |
| 793 // Reset the viewport list to just the pair of viewports for the | 780 // Reset the viewport list to just the pair of viewports for the |
| 794 // primary buffer each frame. Head-locked viewports get added by | 781 // primary buffer each frame. Head-locked viewports get added by |
| 795 // DrawVrShell if needed. | 782 // DrawVrShell if needed. |
| (...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1317 void VrShellGl::UpdateVSyncInterval(int64_t timebase_nanos, | 1304 void VrShellGl::UpdateVSyncInterval(int64_t timebase_nanos, |
| 1318 double interval_seconds) { | 1305 double interval_seconds) { |
| 1319 vsync_timebase_ = base::TimeTicks(); | 1306 vsync_timebase_ = base::TimeTicks(); |
| 1320 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); | 1307 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); |
| 1321 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); | 1308 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); |
| 1322 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); | 1309 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); |
| 1323 OnVSync(); | 1310 OnVSync(); |
| 1324 } | 1311 } |
| 1325 | 1312 |
| 1326 void VrShellGl::ForceExitVr() { | 1313 void VrShellGl::ForceExitVr() { |
| 1327 main_thread_task_runner_->PostTask( | 1314 browser_->ForceExitVr(); |
| 1328 FROM_HERE, base::Bind(&VrShell::ForceExitVr, weak_vr_shell_)); | |
| 1329 } | 1315 } |
| 1330 | 1316 |
| 1331 void VrShellGl::SendVSync(base::TimeDelta time, | 1317 void VrShellGl::SendVSync(base::TimeDelta time, |
| 1332 const GetVSyncCallback& callback) { | 1318 const GetVSyncCallback& callback) { |
| 1333 uint8_t frame_index = frame_index_++; | 1319 uint8_t frame_index = frame_index_++; |
| 1334 | 1320 |
| 1335 TRACE_EVENT1("input", "VrShellGl::SendVSync", "frame", frame_index); | 1321 TRACE_EVENT1("input", "VrShellGl::SendVSync", "frame", frame_index); |
| 1336 | 1322 |
| 1337 vr::Mat4f head_mat; | 1323 vr::Mat4f head_mat; |
| 1338 device::mojom::VRPosePtr pose = | 1324 device::mojom::VRPosePtr pose = |
| 1339 device::GvrDelegate::GetVRPosePtrWithNeckModel(gvr_api_.get(), &head_mat); | 1325 device::GvrDelegate::GetVRPosePtrWithNeckModel(gvr_api_.get(), &head_mat); |
| 1340 | 1326 |
| 1341 webvr_head_pose_[frame_index % kPoseRingBufferSize] = head_mat; | 1327 webvr_head_pose_[frame_index % kPoseRingBufferSize] = head_mat; |
| 1342 | 1328 |
| 1343 callback.Run(std::move(pose), time, frame_index, | 1329 callback.Run(std::move(pose), time, frame_index, |
| 1344 device::mojom::VRVSyncProvider::Status::SUCCESS); | 1330 device::mojom::VRVSyncProvider::Status::SUCCESS); |
| 1345 } | 1331 } |
| 1346 | 1332 |
| 1347 void VrShellGl::CreateVRDisplayInfo( | 1333 void VrShellGl::CreateVRDisplayInfo( |
| 1348 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback, | 1334 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback, |
| 1349 uint32_t device_id) { | 1335 uint32_t device_id) { |
| 1350 // This assumes that the initial webvr_surface_size_ was set to the | 1336 // This assumes that the initial webvr_surface_size_ was set to the |
| 1351 // appropriate recommended render resolution as the default size during | 1337 // appropriate recommended render resolution as the default size during |
| 1352 // InitializeGl. Revisit if the initialization order changes. | 1338 // InitializeGl. Revisit if the initialization order changes. |
| 1353 device::mojom::VRDisplayInfoPtr info = | 1339 device::mojom::VRDisplayInfoPtr info = |
| 1354 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), | 1340 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), |
| 1355 webvr_surface_size_, device_id); | 1341 webvr_surface_size_, device_id); |
| 1356 main_thread_task_runner_->PostTask(FROM_HERE, | 1342 browser_->RunVRDisplayInfoCallback(callback, &info); |
| 1357 base::Bind(callback, base::Passed(&info))); | |
| 1358 } | 1343 } |
| 1359 | 1344 |
| 1360 } // namespace vr_shell | 1345 } // namespace vr_shell |
| OLD | NEW |