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

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

Issue 2833773005: Pause drawing webvr when the App button is pressed (Closed)
Patch Set: refactor Created 3 years, 7 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/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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 return mouse_event; 150 return mouse_event;
150 } 151 }
151 152
152 enum class ViewerType { 153 enum class ViewerType {
153 UNKNOWN_TYPE = 0, 154 UNKNOWN_TYPE = 0,
154 CARDBOARD = 1, 155 CARDBOARD = 1,
155 DAYDREAM = 2, 156 DAYDREAM = 2,
156 VIEWER_TYPE_MAX, 157 VIEWER_TYPE_MAX,
157 }; 158 };
158 159
159 void RunVRDisplayInfoCallback(
160 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback,
161 device::mojom::VRDisplayInfoPtr info) {
162 callback.Run(std::move(info));
163 }
164
165 void MatfToGvrMat(const vr::Mat4f& in, gvr::Mat4f* out) { 160 void MatfToGvrMat(const vr::Mat4f& in, gvr::Mat4f* out) {
166 // If our std::array implementation doesn't have any non-data members, we can 161 // If our std::array implementation doesn't have any non-data members, we can
167 // just cast the gvr matrix to an std::array. 162 // just cast the gvr matrix to an std::array.
168 static_assert(sizeof(in) == sizeof(*out), 163 static_assert(sizeof(in) == sizeof(*out),
169 "Cannot reinterpret gvr::Mat4f as vr::Matf"); 164 "Cannot reinterpret gvr::Mat4f as vr::Matf");
170 *out = *reinterpret_cast<gvr::Mat4f*>(const_cast<vr::Mat4f*>(&in)); 165 *out = *reinterpret_cast<gvr::Mat4f*>(const_cast<vr::Mat4f*>(&in));
171 } 166 }
172 167
173 void GvrMatToMatf(const gvr::Mat4f& in, vr::Mat4f* out) { 168 void GvrMatToMatf(const gvr::Mat4f& in, vr::Mat4f* out) {
174 // If our std::array implementation doesn't have any non-data members, we can 169 // If our std::array implementation doesn't have any non-data members, we can
175 // just cast the gvr matrix to an std::array. 170 // just cast the gvr matrix to an std::array.
176 static_assert(sizeof(in) == sizeof(*out), 171 static_assert(sizeof(in) == sizeof(*out),
177 "Cannot reinterpret gvr::Mat4f as vr::Matf"); 172 "Cannot reinterpret gvr::Mat4f as vr::Matf");
178 *out = *reinterpret_cast<vr::Mat4f*>(const_cast<gvr::Mat4f*>(&in)); 173 *out = *reinterpret_cast<vr::Mat4f*>(const_cast<gvr::Mat4f*>(&in));
179 } 174 }
180 175
181 gvr::Rectf UVFromGfxRect(gfx::RectF rect) { 176 gvr::Rectf UVFromGfxRect(gfx::RectF rect) {
182 return {rect.x(), rect.x() + rect.width(), 1.0f - rect.bottom(), 177 return {rect.x(), rect.x() + rect.width(), 1.0f - rect.bottom(),
183 1.0f - rect.y()}; 178 1.0f - rect.y()};
184 } 179 }
185 180
186 gfx::RectF GfxRectFromUV(gvr::Rectf rect) { 181 gfx::RectF GfxRectFromUV(gvr::Rectf rect) {
187 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,
188 rect.top - rect.bottom); 183 rect.top - rect.bottom);
189 } 184 }
190 185
191 } // namespace 186 } // namespace
192 187
193 VrShellGl::VrShellGl( 188 VrShellGl::VrShellGl(
194 const base::WeakPtr<VrShell>& weak_vr_shell, 189 const base::WeakPtr<VrBrowserInterface>& weak_browser_interface,
195 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
196 gvr_context* gvr_api, 190 gvr_context* gvr_api,
197 bool initially_web_vr, 191 bool initially_web_vr,
198 bool reprojected_rendering, 192 bool reprojected_rendering,
199 UiScene* scene) 193 UiScene* scene)
200 : web_vr_mode_(initially_web_vr), 194 : web_vr_mode_(initially_web_vr),
201 surfaceless_rendering_(reprojected_rendering), 195 surfaceless_rendering_(reprojected_rendering),
202 task_runner_(base::ThreadTaskRunnerHandle::Get()), 196 task_runner_(base::ThreadTaskRunnerHandle::Get()),
203 binding_(this), 197 binding_(this),
204 weak_vr_shell_(weak_vr_shell), 198 weak_browser_interface_(weak_browser_interface),
205 main_thread_task_runner_(std::move(main_thread_task_runner)),
206 scene_(scene), 199 scene_(scene),
207 #if DCHECK_IS_ON() 200 #if DCHECK_IS_ON()
208 fps_meter_(new FPSMeter()), 201 fps_meter_(new FPSMeter()),
209 #endif 202 #endif
210 weak_ptr_factory_(this) { 203 weak_ptr_factory_(this) {
211 GvrInit(gvr_api); 204 GvrInit(gvr_api);
212 } 205 }
213 206
214 VrShellGl::~VrShellGl() { 207 VrShellGl::~VrShellGl() {
215 vsync_task_.Cancel(); 208 vsync_task_.Cancel();
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 291
299 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); 292 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this)));
300 OnVSync(); 293 OnVSync();
301 294
302 ready_to_draw_ = true; 295 ready_to_draw_ = true;
303 } 296 }
304 297
305 void VrShellGl::CreateContentSurface() { 298 void VrShellGl::CreateContentSurface() {
306 content_surface_ = 299 content_surface_ =
307 base::MakeUnique<gl::ScopedJavaSurface>(content_surface_texture_.get()); 300 base::MakeUnique<gl::ScopedJavaSurface>(content_surface_texture_.get());
308 main_thread_task_runner_->PostTask( 301 weak_browser_interface_->ContentSurfaceChanged(
309 FROM_HERE, base::Bind(&VrShell::ContentSurfaceChanged, weak_vr_shell_, 302 content_surface_->j_surface().obj());
310 content_surface_->j_surface().obj()));
311 } 303 }
312 304
313 void VrShellGl::CreateOrResizeWebVRSurface(const gfx::Size& size) { 305 void VrShellGl::CreateOrResizeWebVRSurface(const gfx::Size& size) {
314 if (!webvr_surface_texture_) { 306 if (!webvr_surface_texture_) {
315 DLOG(ERROR) << "No WebVR surface texture available"; 307 DLOG(ERROR) << "No WebVR surface texture available";
316 return; 308 return;
317 } 309 }
318 310
319 // ContentPhysicalBoundsChanged is getting called twice with 311 // ContentPhysicalBoundsChanged is getting called twice with
320 // identical sizes? Avoid thrashing the existing context. 312 // identical sizes? Avoid thrashing the existing context.
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, 488 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE,
497 webvr_left_viewport_.get()); 489 webvr_left_viewport_.get());
498 webvr_left_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); 490 webvr_left_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer);
499 491
500 webvr_right_viewport_.reset( 492 webvr_right_viewport_.reset(
501 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); 493 new gvr::BufferViewport(gvr_api_->CreateBufferViewport()));
502 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, 494 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE,
503 webvr_right_viewport_.get()); 495 webvr_right_viewport_.get());
504 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer); 496 webvr_right_viewport_->SetSourceBufferIndex(kFramePrimaryBuffer);
505 497
506 main_thread_task_runner_->PostTask( 498 weak_browser_interface_->GvrDelegateReady();
cjgrant 2017/05/02 18:07:06 I can't handle this - the code looks too clean now
ymalik 2017/05/02 20:28:40 :D thanks. Just to be clear, this comment doesn't
cjgrant 2017/05/03 14:40:38 Correct. Sorry. :)
507 FROM_HERE, base::Bind(&VrShell::GvrDelegateReady, weak_vr_shell_));
508 } 499 }
509 500
510 void VrShellGl::UpdateController(const gfx::Vector3dF& head_direction) { 501 void VrShellGl::UpdateController(const gfx::Vector3dF& head_direction) {
511 controller_->UpdateState(head_direction); 502 controller_->UpdateState(head_direction);
512 pointer_start_ = controller_->GetPointerStart(); 503 pointer_start_ = controller_->GetPointerStart();
513 504
514 device::GvrGamepadData pad = controller_->GetGamepadData(); 505 weak_browser_interface_->UpdateGamepadData(controller_->GetGamepadData());
515 main_thread_task_runner_->PostTask(
516 FROM_HERE, base::Bind(&VrShell::UpdateGamepadData, weak_vr_shell_, pad));
517 } 506 }
518 507
519 void VrShellGl::HandleControllerInput(const gfx::Vector3dF& head_direction) { 508 void VrShellGl::HandleControllerInput(const gfx::Vector3dF& head_direction) {
520 if (ShouldDrawWebVr()) { 509 if (ShouldDrawWebVr()) {
521 // Process screen touch events for Cardboard button compatibility. 510 // Process screen touch events for Cardboard button compatibility.
522 // Also send tap events for controller "touchpad click" events. 511 // Also send tap events for controller "touchpad click" events.
523 if (touch_pending_ || 512 if (touch_pending_ ||
524 controller_->ButtonUpHappened( 513 controller_->ButtonUpHappened(
525 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) { 514 gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) {
526 touch_pending_ = false; 515 touch_pending_ = false;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 // considered a regular click 635 // considered a regular click
647 // TODO(asimjour1): We need to refactor the gesture recognition outside of 636 // TODO(asimjour1): We need to refactor the gesture recognition outside of
648 // VrShellGl. 637 // VrShellGl.
649 UiInterface::Direction direction = UiInterface::NONE; 638 UiInterface::Direction direction = UiInterface::NONE;
650 float gesture_xz_angle; 639 float gesture_xz_angle;
651 if (vr::XZAngle(controller_start_direction_, controller_direction, 640 if (vr::XZAngle(controller_start_direction_, controller_direction,
652 &gesture_xz_angle)) { 641 &gesture_xz_angle)) {
653 if (fabs(gesture_xz_angle) > kMinAppButtonGestureAngleRad) { 642 if (fabs(gesture_xz_angle) > kMinAppButtonGestureAngleRad) {
654 direction = 643 direction =
655 gesture_xz_angle < 0 ? UiInterface::LEFT : UiInterface::RIGHT; 644 gesture_xz_angle < 0 ? UiInterface::LEFT : UiInterface::RIGHT;
656 main_thread_task_runner_->PostTask( 645 weak_browser_interface_->AppButtonGesturePerformed(direction);
657 FROM_HERE, base::Bind(&VrShell::AppButtonGesturePerformed,
658 weak_vr_shell_, direction));
659 } 646 }
660 } 647 }
661 if (direction == UiInterface::NONE) { 648 if (direction == UiInterface::NONE)
662 main_thread_task_runner_->PostTask( 649 weak_browser_interface_->AppButtonPressed();
663 FROM_HERE, base::Bind(&VrShell::AppButtonPressed, weak_vr_shell_));
664 }
665 } 650 }
666 } 651 }
667 652
668 void VrShellGl::SendInputToContent(InputTarget input_target, 653 void VrShellGl::SendInputToContent(InputTarget input_target,
669 int pixel_x, 654 int pixel_x,
670 int pixel_y) { 655 int pixel_y) {
671 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list = 656 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list =
672 controller_->DetectGestures(); 657 controller_->DetectGestures();
673 double timestamp = gesture_list.front()->TimeStampSeconds(); 658 double timestamp = gesture_list.front()->TimeStampSeconds();
674 659
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 base::Bind(&UiElement::OnButtonUp, 764 base::Bind(&UiElement::OnButtonUp,
780 base::Unretained(target_element))); 765 base::Unretained(target_element)));
781 } 766 }
782 click_target_element_ = nullptr; 767 click_target_element_ = nullptr;
783 } 768 }
784 } 769 }
785 } 770 }
786 771
787 void VrShellGl::SendGestureToContent( 772 void VrShellGl::SendGestureToContent(
788 std::unique_ptr<blink::WebInputEvent> event) { 773 std::unique_ptr<blink::WebInputEvent> event) {
789 main_thread_task_runner_->PostTask( 774 weak_browser_interface_->ProcessContentGesture(std::move(event));
790 FROM_HERE, base::Bind(&VrShell::ProcessContentGesture, weak_vr_shell_,
791 base::Passed(std::move(event))));
792 } 775 }
793 776
794 void VrShellGl::DrawFrame(int16_t frame_index) { 777 void VrShellGl::DrawFrame(int16_t frame_index) {
795 TRACE_EVENT1("gpu", "VrShellGl::DrawFrame", "frame", frame_index); 778 TRACE_EVENT1("gpu", "VrShellGl::DrawFrame", "frame", frame_index);
796 779
797 base::TimeTicks current_time = base::TimeTicks::Now(); 780 base::TimeTicks current_time = base::TimeTicks::Now();
798 781
799 // Reset the viewport list to just the pair of viewports for the 782 // Reset the viewport list to just the pair of viewports for the
800 // primary buffer each frame. Head-locked viewports get added by 783 // primary buffer each frame. Head-locked viewports get added by
801 // DrawVrShell if needed. 784 // DrawVrShell if needed.
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 void VrShellGl::UpdateVSyncInterval(int64_t timebase_nanos, 1306 void VrShellGl::UpdateVSyncInterval(int64_t timebase_nanos,
1324 double interval_seconds) { 1307 double interval_seconds) {
1325 vsync_timebase_ = base::TimeTicks(); 1308 vsync_timebase_ = base::TimeTicks();
1326 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); 1309 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000);
1327 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); 1310 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds);
1328 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this))); 1311 vsync_task_.Reset(base::Bind(&VrShellGl::OnVSync, base::Unretained(this)));
1329 OnVSync(); 1312 OnVSync();
1330 } 1313 }
1331 1314
1332 void VrShellGl::ForceExitVr() { 1315 void VrShellGl::ForceExitVr() {
1333 main_thread_task_runner_->PostTask( 1316 weak_browser_interface_->ForceExitVr();
1334 FROM_HERE, base::Bind(&VrShell::ForceExitVr, weak_vr_shell_));
1335 } 1317 }
1336 1318
1337 void VrShellGl::SendVSync(base::TimeDelta time, 1319 void VrShellGl::SendVSync(base::TimeDelta time,
1338 const GetVSyncCallback& callback) { 1320 const GetVSyncCallback& callback) {
1339 uint8_t frame_index = frame_index_++; 1321 uint8_t frame_index = frame_index_++;
1340 1322
1341 TRACE_EVENT1("input", "VrShellGl::SendVSync", "frame", frame_index); 1323 TRACE_EVENT1("input", "VrShellGl::SendVSync", "frame", frame_index);
1342 1324
1343 vr::Mat4f head_mat; 1325 vr::Mat4f head_mat;
1344 device::mojom::VRPosePtr pose = 1326 device::mojom::VRPosePtr pose =
1345 device::GvrDelegate::GetVRPosePtrWithNeckModel(gvr_api_.get(), &head_mat); 1327 device::GvrDelegate::GetVRPosePtrWithNeckModel(gvr_api_.get(), &head_mat);
1346 1328
1347 webvr_head_pose_[frame_index % kPoseRingBufferSize] = head_mat; 1329 webvr_head_pose_[frame_index % kPoseRingBufferSize] = head_mat;
1348 1330
1349 callback.Run(std::move(pose), time, frame_index, 1331 callback.Run(std::move(pose), time, frame_index,
1350 device::mojom::VRVSyncProvider::Status::SUCCESS); 1332 device::mojom::VRVSyncProvider::Status::SUCCESS);
1351 } 1333 }
1352 1334
1353 void VrShellGl::CreateVRDisplayInfo( 1335 void VrShellGl::CreateVRDisplayInfo(
1354 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback, 1336 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback,
1355 uint32_t device_id) { 1337 uint32_t device_id) {
1356 // This assumes that the initial webvr_surface_size_ was set to the 1338 // This assumes that the initial webvr_surface_size_ was set to the
1357 // appropriate recommended render resolution as the default size during 1339 // appropriate recommended render resolution as the default size during
1358 // InitializeGl. Revisit if the initialization order changes. 1340 // InitializeGl. Revisit if the initialization order changes.
1359 device::mojom::VRDisplayInfoPtr info = 1341 device::mojom::VRDisplayInfoPtr info =
1360 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), 1342 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(),
1361 webvr_surface_size_, device_id); 1343 webvr_surface_size_, device_id);
1362 main_thread_task_runner_->PostTask( 1344 weak_browser_interface_->RunVRDisplayInfoCallback(callback, &info);
1363 FROM_HERE,
1364 base::Bind(&RunVRDisplayInfoCallback, callback, base::Passed(&info)));
1365 } 1345 }
1366 1346
1367 } // namespace vr_shell 1347 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698