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

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

Issue 2570553004: Clean up some VrShell threading issues and remove unnecessary WeakPtr types. (Closed)
Patch Set: Created 4 years 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/vr_shell_gl.h" 5 #include "chrome/browser/android/vr_shell/vr_shell_gl.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 #include "chrome/browser/android/vr_shell/ui_elements.h" 10 #include "chrome/browser/android/vr_shell/ui_elements.h"
11 #include "chrome/browser/android/vr_shell/ui_interface.h"
12 #include "chrome/browser/android/vr_shell/ui_scene.h" 11 #include "chrome/browser/android/vr_shell/ui_scene.h"
13 #include "chrome/browser/android/vr_shell/vr_controller.h" 12 #include "chrome/browser/android/vr_shell/vr_controller.h"
14 #include "chrome/browser/android/vr_shell/vr_gl_util.h" 13 #include "chrome/browser/android/vr_shell/vr_gl_util.h"
15 #include "chrome/browser/android/vr_shell/vr_input_manager.h" 14 #include "chrome/browser/android/vr_shell/vr_input_manager.h"
16 #include "chrome/browser/android/vr_shell/vr_math.h" 15 #include "chrome/browser/android/vr_shell/vr_math.h"
17 #include "chrome/browser/android/vr_shell/vr_shell.h" 16 #include "chrome/browser/android/vr_shell/vr_shell.h"
18 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" 17 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
19 #include "third_party/WebKit/public/platform/WebInputEvent.h" 18 #include "third_party/WebKit/public/platform/WebInputEvent.h"
20 #include "ui/gfx/vsync_provider.h" 19 #include "ui/gfx/vsync_provider.h"
21 #include "ui/gl/android/scoped_java_surface.h" 20 #include "ui/gl/android/scoped_java_surface.h"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 return pixels[0]; 164 return pixels[0];
166 } 165 }
167 VLOG(1) << "WebVR: reject decoded pose index " << (int)pixels[0] << 166 VLOG(1) << "WebVR: reject decoded pose index " << (int)pixels[0] <<
168 ", bad magic number " << (int)pixels[1] << ", " << (int)pixels[2]; 167 ", bad magic number " << (int)pixels[1] << ", " << (int)pixels[2];
169 return -1; 168 return -1;
170 } 169 }
171 170
172 } // namespace 171 } // namespace
173 172
174 VrShellGl::VrShellGl( 173 VrShellGl::VrShellGl(
175 VrShell* vr_shell,
176 const base::WeakPtr<VrShell>& weak_vr_shell, 174 const base::WeakPtr<VrShell>& weak_vr_shell,
177 const base::WeakPtr<VrInputManager>& content_input_manager, 175 const base::WeakPtr<VrInputManager>& content_input_manager,
178 const base::WeakPtr<VrInputManager>& ui_input_manager, 176 const base::WeakPtr<VrInputManager>& ui_input_manager,
179 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, 177 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
180 gvr_context* gvr_api) 178 gvr_context* gvr_api,
181 : task_runner_(base::ThreadTaskRunnerHandle::Get()), 179 bool initially_web_vr)
182 vr_shell_(vr_shell), 180 : web_vr_mode_(initially_web_vr),
181 task_runner_(base::ThreadTaskRunnerHandle::Get()),
183 weak_vr_shell_(weak_vr_shell), 182 weak_vr_shell_(weak_vr_shell),
184 content_input_manager_(content_input_manager), 183 content_input_manager_(content_input_manager),
185 ui_input_manager_(ui_input_manager), 184 ui_input_manager_(ui_input_manager),
186 main_thread_task_runner_(std::move(main_thread_task_runner)), 185 main_thread_task_runner_(std::move(main_thread_task_runner)),
187 weak_ptr_factory_(this) { 186 weak_ptr_factory_(this) {
188 GvrInit(gvr_api); 187 GvrInit(gvr_api);
189 } 188 }
190 189
191 VrShellGl::~VrShellGl() { 190 VrShellGl::~VrShellGl() {
192 draw_task_.Cancel(); 191 draw_task_.Cancel();
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 webvr_right_viewport_.get()); 375 webvr_right_viewport_.get());
377 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); 376 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer);
378 377
379 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind( 378 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
380 &VrShell::GvrDelegateReady, weak_vr_shell_)); 379 &VrShell::GvrDelegateReady, weak_vr_shell_));
381 } 380 }
382 381
383 void VrShellGl::UpdateController(const gvr::Vec3f& forward_vector) { 382 void VrShellGl::UpdateController(const gvr::Vec3f& forward_vector) {
384 controller_->UpdateState(); 383 controller_->UpdateState();
385 384
386 #if defined(ENABLE_VR_SHELL)
387 // TODO(mthiesse): Fix menu button handling, which should be posted to the UI
388 // thread instead of handled here.
389 385
390 // Note that button up/down state is transient, so ButtonUpHappened only 386 // Note that button up/down state is transient, so ButtonUpHappened only
391 // returns true for a single frame (and we're guaranteed not to miss it). 387 // returns true for a single frame (and we're guaranteed not to miss it).
392 if (controller_->ButtonUpHappened( 388 if (controller_->ButtonUpHappened(
393 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) { 389 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) {
394 // html_interface_->SetMenuMode(!html_interface_->GetMenuMode()); 390 main_thread_task_runner_->PostTask(
391 FROM_HERE, base::Bind(&VrShell::AppButtonPressed, weak_vr_shell_));
392 }
395 393
396 // TODO(mthiesse): The page is no longer visible when in menu mode. We 394 if (web_vr_mode_) {
397 // should unfocus or otherwise let it know it's hidden.
398 // if (html_interface_->GetMode() == UiInterface::Mode::WEB_VR) {
399 // const auto&& task = html_interface_->GetMenuMode() ?
400 // &device::GvrDeviceProvider::OnDisplayBlur :
401 // &device::GvrDeviceProvider::OnDisplayFocus;
402 // main_thread_task_runner_->PostTask(
403 // FROM_HERE, base::Bind(task, delegate_->GetDeviceProvider()));
404 // }
405 }
406 #endif
407 if (vr_shell_->GetUiInterface()->GetMode() == UiInterface::Mode::WEB_VR) {
408 // Process screen touch events for Cardboard button compatibility. 395 // Process screen touch events for Cardboard button compatibility.
409 // Also send tap events for controller "touchpad click" events. 396 // Also send tap events for controller "touchpad click" events.
410 if (touch_pending_ || controller_->ButtonUpHappened( 397 if (touch_pending_ || controller_->ButtonUpHappened(
411 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) { 398 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) {
412 touch_pending_ = false; 399 touch_pending_ = false;
413 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent()); 400 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent());
414 gesture->sourceDevice = blink::WebGestureDeviceTouchpad; 401 gesture->sourceDevice = blink::WebGestureDeviceTouchpad;
415 gesture->timeStampSeconds = 402 gesture->timeStampSeconds =
416 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); 403 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
417 gesture->type = WebInputEvent::GestureTapDown; 404 gesture->type = WebInputEvent::GestureTapDown;
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 592
606 void VrShellGl::DrawFrame() { 593 void VrShellGl::DrawFrame() {
607 TRACE_EVENT0("gpu", "VrShellGl::DrawFrame"); 594 TRACE_EVENT0("gpu", "VrShellGl::DrawFrame");
608 // Reset the viewport list to just the pair of viewports for the 595 // Reset the viewport list to just the pair of viewports for the
609 // primary buffer each frame. Head-locked viewports get added by 596 // primary buffer each frame. Head-locked viewports get added by
610 // DrawVrShell if needed. 597 // DrawVrShell if needed.
611 buffer_viewport_list_->SetToRecommendedBufferViewports(); 598 buffer_viewport_list_->SetToRecommendedBufferViewports();
612 599
613 // TODO(klausw): Fix this. Resizing buffers here leads to webVR mode showing 600 // TODO(klausw): Fix this. Resizing buffers here leads to webVR mode showing
614 // nothing but a black screen. 601 // nothing but a black screen.
615 // if (vr_shell_->GetUiInterface()->GetMode() == UiInterface::Mode::WEB_VR) { 602 // if (web_vr_mode_) {
616 // // If needed, resize the primary buffer for use with WebVR. 603 // // If needed, resize the primary buffer for use with WebVR.
617 // if (render_size_primary_ != render_size_primary_webvr_) { 604 // if (render_size_primary_ != render_size_primary_webvr_) {
618 // if (!render_size_primary_webvr_.width) { 605 // if (!render_size_primary_webvr_.width) {
619 // VLOG(2) << "WebVR rendering size not known yet, dropping frame"; 606 // VLOG(2) << "WebVR rendering size not known yet, dropping frame";
620 // return; 607 // return;
621 // } 608 // }
622 // render_size_primary_ = render_size_primary_webvr_; 609 // render_size_primary_ = render_size_primary_webvr_;
623 // swap_chain_->ResizeBuffer(kFramePrimaryBuffer, render_size_primary_); 610 // swap_chain_->ResizeBuffer(kFramePrimaryBuffer, render_size_primary_);
624 // } 611 // }
625 // } else { 612 // } else {
(...skipping 21 matching lines...) Expand all
647 } 634 }
648 635
649 frame.BindBuffer(kFramePrimaryBuffer); 636 frame.BindBuffer(kFramePrimaryBuffer);
650 637
651 // Update the render position of all UI elements (including desktop). 638 // Update the render position of all UI elements (including desktop).
652 const float screen_tilt = kDesktopScreenTiltDefault * M_PI / 180.0f; 639 const float screen_tilt = kDesktopScreenTiltDefault * M_PI / 180.0f;
653 scene_->UpdateTransforms(screen_tilt, UiScene::TimeInMicroseconds()); 640 scene_->UpdateTransforms(screen_tilt, UiScene::TimeInMicroseconds());
654 641
655 UpdateController(GetForwardVector(head_pose)); 642 UpdateController(GetForwardVector(head_pose));
656 643
657 if (vr_shell_->GetUiInterface()->GetMode() == UiInterface::Mode::WEB_VR) { 644 if (web_vr_mode_) {
658 DrawWebVr(); 645 DrawWebVr();
659 646
660 // When using async reprojection, we need to know which pose was used in 647 // When using async reprojection, we need to know which pose was used in
661 // the WebVR app for drawing this frame. Due to unknown amounts of 648 // the WebVR app for drawing this frame. Due to unknown amounts of
662 // buffering in the compositor and SurfaceTexture, we read the pose number 649 // buffering in the compositor and SurfaceTexture, we read the pose number
663 // from a corner pixel. There's no point in doing this for legacy 650 // from a corner pixel. There's no point in doing this for legacy
664 // distortion rendering since that doesn't need a pose, and reading back 651 // distortion rendering since that doesn't need a pose, and reading back
665 // pixels is an expensive operation. TODO(klausw,crbug.com/655722): stop 652 // pixels is an expensive operation. TODO(klausw,crbug.com/655722): stop
666 // doing this once we have working no-compositor rendering for WebVR. 653 // doing this once we have working no-compositor rendering for WebVR.
667 if (gvr_api_->GetAsyncReprojectionEnabled()) { 654 if (gvr_api_->GetAsyncReprojectionEnabled()) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 if (!rect->visible) { 692 if (!rect->visible) {
706 continue; 693 continue;
707 } 694 }
708 if (rect->lock_to_fov) { 695 if (rect->lock_to_fov) {
709 head_locked_elements.push_back(rect.get()); 696 head_locked_elements.push_back(rect.get());
710 } else { 697 } else {
711 world_elements.push_back(rect.get()); 698 world_elements.push_back(rect.get());
712 } 699 }
713 } 700 }
714 701
715 if (vr_shell_->GetUiInterface()->GetMode() == UiInterface::Mode::WEB_VR) { 702 if (web_vr_mode_) {
716 // WebVR is incompatible with 3D world compositing since the 703 // WebVR is incompatible with 3D world compositing since the
717 // depth buffer was already populated with unknown scaling - the 704 // depth buffer was already populated with unknown scaling - the
718 // WebVR app has full control over zNear/zFar. Just leave the 705 // WebVR app has full control over zNear/zFar. Just leave the
719 // existing content in place in the primary buffer without 706 // existing content in place in the primary buffer without
720 // clearing. Currently, there aren't any world elements in WebVR 707 // clearing. Currently, there aren't any world elements in WebVR
721 // mode, this will need further testing if those get added 708 // mode, this will need further testing if those get added
722 // later. 709 // later.
723 } else { 710 } else {
724 // Non-WebVR mode, enable depth testing and clear the primary buffers. 711 // Non-WebVR mode, enable depth testing and clear the primary buffers.
725 glEnable(GL_CULL_FACE); 712 glEnable(GL_CULL_FACE);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 glViewport(pixel_rect.left, pixel_rect.bottom, 775 glViewport(pixel_rect.left, pixel_rect.bottom,
789 pixel_rect.right - pixel_rect.left, 776 pixel_rect.right - pixel_rect.left,
790 pixel_rect.top - pixel_rect.bottom); 777 pixel_rect.top - pixel_rect.bottom);
791 778
792 const gvr::Mat4f render_matrix = MatrixMul( 779 const gvr::Mat4f render_matrix = MatrixMul(
793 PerspectiveMatrixFromView( 780 PerspectiveMatrixFromView(
794 buffer_viewport_->GetSourceFov(), kZNear, kZFar), 781 buffer_viewport_->GetSourceFov(), kZNear, kZFar),
795 view_matrix); 782 view_matrix);
796 783
797 DrawElements(render_matrix, elements); 784 DrawElements(render_matrix, elements);
798 if (head_pose != nullptr && 785 if (head_pose != nullptr && !web_vr_mode_) {
799 vr_shell_->GetUiInterface()->GetMode() != UiInterface::Mode::WEB_VR) {
800 DrawCursor(render_matrix); 786 DrawCursor(render_matrix);
801 } 787 }
802 } 788 }
803 } 789 }
804 790
805 void VrShellGl::DrawElements( 791 void VrShellGl::DrawElements(
806 const gvr::Mat4f& render_matrix, 792 const gvr::Mat4f& render_matrix,
807 const std::vector<const ContentRectangle*>& elements) { 793 const std::vector<const ContentRectangle*>& elements) {
808 for (const auto& rect : elements) { 794 for (const auto& rect : elements) {
809 Rectf copy_rect; 795 Rectf copy_rect;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 } 916 }
931 917
932 void VrShellGl::OnResume() { 918 void VrShellGl::OnResume() {
933 gvr_api_->RefreshViewerProfile(); 919 gvr_api_->RefreshViewerProfile();
934 gvr_api_->ResumeTracking(); 920 gvr_api_->ResumeTracking();
935 controller_->OnResume(); 921 controller_->OnResume();
936 draw_task_.Reset(base::Bind(&VrShellGl::DrawFrame, base::Unretained(this))); 922 draw_task_.Reset(base::Bind(&VrShellGl::DrawFrame, base::Unretained(this)));
937 ScheduleNextDrawFrame(); 923 ScheduleNextDrawFrame();
938 } 924 }
939 925
940 void VrShellGl::SetWebVrMode(bool enabled) { 926 void VrShellGl::SetWebVrMode(bool enabled) {
bshe 2016/12/14 16:06:46 Is this function ever being called?
mthiesse 2016/12/14 16:20:43 Yes, VrShell::SetWebVrMode posts to it.
bshe 2016/12/14 17:36:51 dooh. Missed that...
941 if (enabled) { 927 web_vr_mode_ = enabled;
942 vr_shell_->GetUiInterface()->SetMode(UiInterface::Mode::WEB_VR);
943 } else {
944 vr_shell_->GetUiInterface()->SetMode(UiInterface::Mode::STANDARD);
945 }
946 } 928 }
947 929
948 void VrShellGl::UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, 930 void VrShellGl::UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds,
949 const gvr::Rectf& right_bounds) { 931 const gvr::Rectf& right_bounds) {
950 webvr_left_viewport_->SetSourceUv(left_bounds); 932 webvr_left_viewport_->SetSourceUv(left_bounds);
951 webvr_right_viewport_->SetSourceUv(right_bounds); 933 webvr_right_viewport_->SetSourceUv(right_bounds);
952 } 934 }
953 935
954 gvr::GvrApi* VrShellGl::gvr_api() { 936 gvr::GvrApi* VrShellGl::gvr_api() {
955 return gvr_api_.get(); 937 return gvr_api_.get();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 986
1005 task_runner_->PostDelayedTask(FROM_HERE, draw_task_.callback(), target - now); 987 task_runner_->PostDelayedTask(FROM_HERE, draw_task_.callback(), target - now);
1006 } 988 }
1007 989
1008 void VrShellGl::ForceExitVR() { 990 void VrShellGl::ForceExitVR() {
1009 main_thread_task_runner_->PostTask( 991 main_thread_task_runner_->PostTask(
1010 FROM_HERE, base::Bind(&VrShell::ForceExitVR, weak_vr_shell_)); 992 FROM_HERE, base::Bind(&VrShell::ForceExitVR, weak_vr_shell_));
1011 } 993 }
1012 994
1013 } // namespace vr_shell 995 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698