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

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

Issue 2879973002: Expose Gamepad API instance for Cardboard button (Closed)
Patch Set: move to a new cardboard gamepad data fetcher 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.h" 5 #include "chrome/browser/android/vr_shell/vr_shell.h"
6 6
7 #include <android/native_window_jni.h> 7 #include <android/native_window_jni.h>
8 8
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 24 matching lines...) Expand all
35 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h" 35 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
36 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h" 36 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
37 #include "content/public/browser/browser_thread.h" 37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/navigation_controller.h" 38 #include "content/public/browser/navigation_controller.h"
39 #include "content/public/browser/render_view_host.h" 39 #include "content/public/browser/render_view_host.h"
40 #include "content/public/browser/render_widget_host.h" 40 #include "content/public/browser/render_widget_host.h"
41 #include "content/public/browser/render_widget_host_view.h" 41 #include "content/public/browser/render_widget_host_view.h"
42 #include "content/public/browser/web_contents.h" 42 #include "content/public/browser/web_contents.h"
43 #include "content/public/common/content_features.h" 43 #include "content/public/common/content_features.h"
44 #include "content/public/common/referrer.h" 44 #include "content/public/common/referrer.h"
45 #include "device/vr/android/gvr/cardboard_gamepad_data_fetcher.h"
45 #include "device/vr/android/gvr/gvr_device.h" 46 #include "device/vr/android/gvr/gvr_device.h"
46 #include "device/vr/android/gvr/gvr_device_provider.h" 47 #include "device/vr/android/gvr/gvr_device_provider.h"
47 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" 48 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h"
48 #include "gpu/command_buffer/common/mailbox.h" 49 #include "gpu/command_buffer/common/mailbox.h"
49 #include "jni/VrShellImpl_jni.h" 50 #include "jni/VrShellImpl_jni.h"
50 #include "third_party/WebKit/public/platform/WebInputEvent.h" 51 #include "third_party/WebKit/public/platform/WebInputEvent.h"
51 #include "ui/android/view_android.h" 52 #include "ui/android/view_android.h"
52 #include "ui/android/window_android.h" 53 #include "ui/android/window_android.h"
53 #include "ui/base/page_transition_types.h" 54 #include "ui/base/page_transition_types.h"
54 #include "ui/display/display.h" 55 #include "ui/display/display.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 119
119 gl_thread_ = base::MakeUnique<VrGLThread>( 120 gl_thread_ = base::MakeUnique<VrGLThread>(
120 weak_ptr_factory_.GetWeakPtr(), main_thread_task_runner_, gvr_api, 121 weak_ptr_factory_.GetWeakPtr(), main_thread_task_runner_, gvr_api,
121 for_web_vr, in_cct, reprojected_rendering_); 122 for_web_vr, in_cct, reprojected_rendering_);
122 ui_ = gl_thread_.get(); 123 ui_ = gl_thread_.get();
123 124
124 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); 125 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0);
125 options.priority = base::ThreadPriority::DISPLAY; 126 options.priority = base::ThreadPriority::DISPLAY;
126 gl_thread_->StartWithOptions(options); 127 gl_thread_->StartWithOptions(options);
127 128
128
129 content::BrowserThread::PostTask( 129 content::BrowserThread::PostTask(
130 content::BrowserThread::FILE, FROM_HERE, 130 content::BrowserThread::FILE, FROM_HERE,
131 base::Bind(LoadControllerModelTask, weak_ptr_factory_.GetWeakPtr(), 131 base::Bind(LoadControllerModelTask, weak_ptr_factory_.GetWeakPtr(),
132 main_thread_task_runner_)); 132 main_thread_task_runner_));
133 } 133 }
134 134
135 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { 135 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
136 delete this; 136 delete this;
137 } 137 }
138 138
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 188 }
189 } 189 }
190 190
191 bool RegisterVrShell(JNIEnv* env) { 191 bool RegisterVrShell(JNIEnv* env) {
192 return RegisterNativesImpl(env); 192 return RegisterNativesImpl(env);
193 } 193 }
194 194
195 VrShell::~VrShell() { 195 VrShell::~VrShell() {
196 DVLOG(1) << __FUNCTION__ << "=" << this; 196 DVLOG(1) << __FUNCTION__ << "=" << this;
197 poll_capturing_media_task_.Cancel(); 197 poll_capturing_media_task_.Cancel();
198 if (gamepad_source_active_) { 198 if (gvr_gamepad_source_active_) {
199 device::GamepadDataFetcherManager::GetInstance()->RemoveSourceFactory( 199 device::GamepadDataFetcherManager::GetInstance()->RemoveSourceFactory(
200 device::GAMEPAD_SOURCE_GVR); 200 device::GAMEPAD_SOURCE_GVR);
201 } 201 }
202 202
203 if (cardboard_gamepad_source_active_) {
204 device::GamepadDataFetcherManager::GetInstance()->RemoveSourceFactory(
205 device::GAMEPAD_SOURCE_CARDBOARD);
206 }
207
203 delegate_provider_->RemoveDelegate(); 208 delegate_provider_->RemoveDelegate();
204 { 209 {
205 // The GvrLayout is, and must always be, used only on the UI thread, and the 210 // The GvrLayout is, and must always be, used only on the UI thread, and the
206 // GvrApi used for rendering should only be used from the GL thread as it's 211 // GvrApi used for rendering should only be used from the GL thread as it's
207 // not thread safe. However, the GvrLayout owns the GvrApi instance, and 212 // not thread safe. However, the GvrLayout owns the GvrApi instance, and
208 // when it gets shut down it deletes the GvrApi instance with it. Therefore, 213 // when it gets shut down it deletes the GvrApi instance with it. Therefore,
209 // we need to block shutting down the GvrLayout on stopping our GL thread 214 // we need to block shutting down the GvrLayout on stopping our GL thread
210 // from using the GvrApi instance. 215 // from using the GvrApi instance.
211 // base::Thread::Stop, which is called when destroying the thread, asserts 216 // base::Thread::Stop, which is called when destroying the thread, asserts
212 // that IO is allowed to prevent jank, but there shouldn't be any concerns 217 // that IO is allowed to prevent jank, but there shouldn't be any concerns
(...skipping 26 matching lines...) Expand all
239 delegate_provider_->device_provider()->Device()->OnBlur(); 244 delegate_provider_->device_provider()->Device()->OnBlur();
240 else 245 else
241 delegate_provider_->device_provider()->Device()->OnFocus(); 246 delegate_provider_->device_provider()->Device()->OnFocus();
242 } 247 }
243 248
244 void VrShell::NavigateBack() { 249 void VrShell::NavigateBack() {
245 JNIEnv* env = base::android::AttachCurrentThread(); 250 JNIEnv* env = base::android::AttachCurrentThread();
246 Java_VrShellImpl_navigateBack(env, j_vr_shell_.obj()); 251 Java_VrShellImpl_navigateBack(env, j_vr_shell_.obj());
247 } 252 }
248 253
249 void VrShell::OnTriggerEvent(JNIEnv* env, const JavaParamRef<jobject>& obj) { 254 void VrShell::ToggleCardboardGamepad(bool enabled) {
250 gl_thread_->task_runner()->PostTask( 255 // enable/disable updating gamepad state
251 FROM_HERE, 256 if (cardboard_gamepad_source_active_ && !enabled) {
252 base::Bind(&VrShellGl::OnTriggerEvent, gl_thread_->GetVrShellGl())); 257 device::GamepadDataFetcherManager::GetInstance()->RemoveSourceFactory(
258 device::GAMEPAD_SOURCE_CARDBOARD);
259 cardboard_gamepad_data_fetcher_ = nullptr;
260 cardboard_gamepad_source_active_ = false;
261 }
262
263 if (!cardboard_gamepad_source_active_ && enabled) {
264 // enable the gamepad
mthiesse 2017/05/18 18:31:35 DCHECK that the gvr fetcher isn't active at the sa
billorr 2017/05/22 20:11:09 We do want them both active. For Oculus or Vive y
265 if (!delegate_provider_->device_provider())
266 return;
267
268 unsigned int device_id =
269 delegate_provider_->device_provider()->Device()->id();
270
271 device::GamepadDataFetcherManager::GetInstance()->AddFactory(
272 new device::CardboardGamepadDataFetcher::Factory(this, device_id));
273 cardboard_gamepad_source_active_ = true;
274 }
275 }
276
277 void VrShell::OnTriggerEvent(JNIEnv* env,
278 const JavaParamRef<jobject>& obj,
279 bool touched) {
280 // Send screen taps over to VrShellGl to be turned into simulated clicks for
281 // cardboard.
282 if (touched)
283 gl_thread_->task_runner()->PostTask(
284 FROM_HERE,
285 base::Bind(&VrShellGl::OnTriggerEvent, gl_thread_->GetVrShellGl()));
286
287 // If we are running cardboard, update gamepad state.
288 if (cardboard_gamepad_source_active_) {
289 device::CardboardGamepadData pad;
290 pad.timestamp = cardboard_gamepad_timer_++;
mthiesse 2017/05/18 18:31:35 Why are we using a counter for the timestamp? Why
billorr 2017/05/22 20:11:09 The timestamp is just a monotonically increasing v
291 pad.is_screen_touching = touched;
292 if (cardboard_gamepad_data_fetcher_) {
293 cardboard_gamepad_data_fetcher_->SetGamepadData(pad);
294 }
295 }
253 } 296 }
254 297
255 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { 298 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) {
256 gl_thread_->task_runner()->PostTask( 299 gl_thread_->task_runner()->PostTask(
257 FROM_HERE, base::Bind(&VrShellGl::OnPause, gl_thread_->GetVrShellGl())); 300 FROM_HERE, base::Bind(&VrShellGl::OnPause, gl_thread_->GetVrShellGl()));
258 301
259 // exit vr session 302 // exit vr session
260 if (metrics_helper_) 303 if (metrics_helper_)
261 metrics_helper_->SetVRActive(false); 304 metrics_helper_->SetVRActive(false);
262 SetIsInVR(web_contents_, false); 305 SetIsInVR(web_contents_, false);
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 void VrShell::ProcessContentGesture( 623 void VrShell::ProcessContentGesture(
581 std::unique_ptr<blink::WebInputEvent> event) { 624 std::unique_ptr<blink::WebInputEvent> event) {
582 if (input_manager_) { 625 if (input_manager_) {
583 input_manager_->ProcessUpdatedGesture(std::move(event)); 626 input_manager_->ProcessUpdatedGesture(std::move(event));
584 } else if (android_ui_gesture_target_) { 627 } else if (android_ui_gesture_target_) {
585 android_ui_gesture_target_->DispatchWebInputEvent(std::move(event)); 628 android_ui_gesture_target_->DispatchWebInputEvent(std::move(event));
586 } 629 }
587 } 630 }
588 631
589 void VrShell::UpdateGamepadData(device::GvrGamepadData pad) { 632 void VrShell::UpdateGamepadData(device::GvrGamepadData pad) {
590 if (!gamepad_source_active_) { 633 if (!gvr_gamepad_source_active_) {
591 if (!delegate_provider_->device_provider()) 634 if (!delegate_provider_->device_provider())
592 return; 635 return;
593 636
594 unsigned int device_id = 637 unsigned int device_id =
595 delegate_provider_->device_provider()->Device()->id(); 638 delegate_provider_->device_provider()->Device()->id();
639
596 device::GamepadDataFetcherManager::GetInstance()->AddFactory( 640 device::GamepadDataFetcherManager::GetInstance()->AddFactory(
597 new device::GvrGamepadDataFetcher::Factory(this, device_id)); 641 new device::GvrGamepadDataFetcher::Factory(this, device_id));
598 gamepad_source_active_ = true; 642 gvr_gamepad_source_active_ = true;
599 } 643 }
600 if (gamepad_data_fetcher_) { 644 gvr_gamepad_data_fetcher_->SetGamepadData(pad);
601 gamepad_data_fetcher_->SetGamepadData(pad);
602 }
603 } 645 }
604 646
605 void VrShell::RegisterGamepadDataFetcher( 647 void VrShell::RegisterGvrGamepadDataFetcher(
606 device::GvrGamepadDataFetcher* fetcher) { 648 device::GvrGamepadDataFetcher* fetcher) {
607 DVLOG(1) << __FUNCTION__ << "(" << fetcher << ")"; 649 DVLOG(1) << __FUNCTION__ << "(" << fetcher << ")";
608 gamepad_data_fetcher_ = fetcher; 650 gvr_gamepad_data_fetcher_ = fetcher;
651 }
652
653 void VrShell::RegisterCardboardGamepadDataFetcher(
654 device::CardboardGamepadDataFetcher* fetcher) {
655 DVLOG(1) << __FUNCTION__ << "(" << fetcher << ")";
656 cardboard_gamepad_data_fetcher_ = fetcher;
609 } 657 }
610 658
611 // ---------------------------------------------------------------------------- 659 // ----------------------------------------------------------------------------
612 // Native JNI methods 660 // Native JNI methods
613 // ---------------------------------------------------------------------------- 661 // ----------------------------------------------------------------------------
614 662
615 jlong Init(JNIEnv* env, 663 jlong Init(JNIEnv* env,
616 const JavaParamRef<jobject>& obj, 664 const JavaParamRef<jobject>& obj,
617 const JavaParamRef<jobject>& delegate, 665 const JavaParamRef<jobject>& delegate,
618 jlong window_android, 666 jlong window_android,
619 jboolean for_web_vr, 667 jboolean for_web_vr,
620 jboolean in_cct, 668 jboolean in_cct,
621 jlong gvr_api, 669 jlong gvr_api,
622 jboolean reprojected_rendering) { 670 jboolean reprojected_rendering) {
623 return reinterpret_cast<intptr_t>(new VrShell( 671 return reinterpret_cast<intptr_t>(new VrShell(
624 env, obj, reinterpret_cast<ui::WindowAndroid*>(window_android), 672 env, obj, reinterpret_cast<ui::WindowAndroid*>(window_android),
625 for_web_vr, in_cct, 673 for_web_vr, in_cct,
626 VrShellDelegate::GetNativeVrShellDelegate(env, delegate), 674 VrShellDelegate::GetNativeVrShellDelegate(env, delegate),
627 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); 675 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering));
628 } 676 }
629 677
630 } // namespace vr_shell 678 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698