| 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.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 6 | 6 |
| 7 #include <android/native_window_jni.h> |
| 8 |
| 7 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 9 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 10 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/values.h" | 14 #include "base/values.h" |
| 13 #include "chrome/browser/android/vr_shell/ui_interface.h" | 15 #include "chrome/browser/android/vr_shell/ui_interface.h" |
| 14 #include "chrome/browser/android/vr_shell/vr_compositor.h" | 16 #include "chrome/browser/android/vr_shell/vr_compositor.h" |
| 15 #include "chrome/browser/android/vr_shell/vr_input_manager.h" | 17 #include "chrome/browser/android/vr_shell/vr_input_manager.h" |
| 16 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 18 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 41 | 43 |
| 42 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; | 44 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
| 43 | 45 |
| 44 class GLThread : public base::Thread { | 46 class GLThread : public base::Thread { |
| 45 public: | 47 public: |
| 46 GLThread(const base::WeakPtr<VrShell>& weak_vr_shell, | 48 GLThread(const base::WeakPtr<VrShell>& weak_vr_shell, |
| 47 const base::WeakPtr<VrInputManager>& content_input_manager, | 49 const base::WeakPtr<VrInputManager>& content_input_manager, |
| 48 const base::WeakPtr<VrInputManager>& ui_input_manager, | 50 const base::WeakPtr<VrInputManager>& ui_input_manager, |
| 49 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
| 50 gvr_context* gvr_api, | 52 gvr_context* gvr_api, |
| 51 bool initially_web_vr) | 53 bool initially_web_vr, |
| 54 bool reprojected_rendering) |
| 52 : base::Thread("VrShellGL"), | 55 : base::Thread("VrShellGL"), |
| 53 weak_vr_shell_(weak_vr_shell), | 56 weak_vr_shell_(weak_vr_shell), |
| 54 content_input_manager_(content_input_manager), | 57 content_input_manager_(content_input_manager), |
| 55 ui_input_manager_(ui_input_manager), | 58 ui_input_manager_(ui_input_manager), |
| 56 main_thread_task_runner_(std::move(main_thread_task_runner)), | 59 main_thread_task_runner_(std::move(main_thread_task_runner)), |
| 57 gvr_api_(gvr_api), | 60 gvr_api_(gvr_api), |
| 58 initially_web_vr_(initially_web_vr) {} | 61 initially_web_vr_(initially_web_vr), |
| 62 reprojected_rendering_(reprojected_rendering) {} |
| 59 | 63 |
| 60 ~GLThread() override { | 64 ~GLThread() override { |
| 61 Stop(); | 65 Stop(); |
| 62 } | 66 } |
| 63 base::WeakPtr<VrShellGl> GetVrShellGl() { return weak_vr_shell_gl_; } | 67 base::WeakPtr<VrShellGl> GetVrShellGl() { return weak_vr_shell_gl_; } |
| 64 VrShellGl* GetVrShellGlUnsafe() { return vr_shell_gl_.get(); } | 68 VrShellGl* GetVrShellGlUnsafe() { return vr_shell_gl_.get(); } |
| 65 | 69 |
| 66 protected: | 70 protected: |
| 67 void Init() override { | 71 void Init() override { |
| 68 vr_shell_gl_.reset(new VrShellGl(std::move(weak_vr_shell_), | 72 vr_shell_gl_.reset(new VrShellGl(std::move(weak_vr_shell_), |
| 69 std::move(content_input_manager_), | 73 std::move(content_input_manager_), |
| 70 std::move(ui_input_manager_), | 74 std::move(ui_input_manager_), |
| 71 std::move(main_thread_task_runner_), | 75 std::move(main_thread_task_runner_), |
| 72 gvr_api_, | 76 gvr_api_, |
| 73 initially_web_vr_)); | 77 initially_web_vr_, |
| 78 reprojected_rendering_)); |
| 74 weak_vr_shell_gl_ = vr_shell_gl_->GetWeakPtr(); | 79 weak_vr_shell_gl_ = vr_shell_gl_->GetWeakPtr(); |
| 75 if (!vr_shell_gl_->Initialize()) { | 80 vr_shell_gl_->Initialize(); |
| 76 vr_shell_gl_.reset(); | |
| 77 } | |
| 78 } | 81 } |
| 79 void CleanUp() override { | 82 void CleanUp() override { |
| 80 vr_shell_gl_.reset(); | 83 vr_shell_gl_.reset(); |
| 81 } | 84 } |
| 82 | 85 |
| 83 private: | 86 private: |
| 84 // Created on GL thread. | 87 // Created on GL thread. |
| 85 std::unique_ptr<VrShellGl> vr_shell_gl_; | 88 std::unique_ptr<VrShellGl> vr_shell_gl_; |
| 86 base::WeakPtr<VrShellGl> weak_vr_shell_gl_; | 89 base::WeakPtr<VrShellGl> weak_vr_shell_gl_; |
| 87 | 90 |
| 88 base::WeakPtr<VrShell> weak_vr_shell_; | 91 base::WeakPtr<VrShell> weak_vr_shell_; |
| 89 base::WeakPtr<VrInputManager> content_input_manager_; | 92 base::WeakPtr<VrInputManager> content_input_manager_; |
| 90 base::WeakPtr<VrInputManager> ui_input_manager_; | 93 base::WeakPtr<VrInputManager> ui_input_manager_; |
| 91 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 94 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 92 gvr_context* gvr_api_; | 95 gvr_context* gvr_api_; |
| 93 bool initially_web_vr_; | 96 bool initially_web_vr_; |
| 97 bool reprojected_rendering_; |
| 94 }; | 98 }; |
| 95 | 99 |
| 96 } // namespace | 100 } // namespace |
| 97 | 101 |
| 98 VrShell::VrShell(JNIEnv* env, | 102 VrShell::VrShell(JNIEnv* env, |
| 99 jobject obj, | 103 jobject obj, |
| 100 content::WebContents* main_contents, | 104 content::WebContents* main_contents, |
| 101 ui::WindowAndroid* content_window, | 105 ui::WindowAndroid* content_window, |
| 102 content::WebContents* ui_contents, | 106 content::WebContents* ui_contents, |
| 103 ui::WindowAndroid* ui_window, | 107 ui::WindowAndroid* ui_window, |
| 104 bool for_web_vr, | 108 bool for_web_vr, |
| 105 VrShellDelegate* delegate, | 109 VrShellDelegate* delegate, |
| 106 gvr_context* gvr_api) | 110 gvr_context* gvr_api, |
| 111 bool reprojected_rendering) |
| 107 : WebContentsObserver(ui_contents), | 112 : WebContentsObserver(ui_contents), |
| 108 main_contents_(main_contents), | 113 main_contents_(main_contents), |
| 109 content_compositor_(new VrCompositor(content_window, false)), | 114 content_compositor_(new VrCompositor(content_window, false)), |
| 110 ui_contents_(ui_contents), | 115 ui_contents_(ui_contents), |
| 111 ui_compositor_(new VrCompositor(ui_window, true)), | 116 ui_compositor_(new VrCompositor(ui_window, true)), |
| 112 delegate_(delegate), | 117 delegate_(delegate), |
| 113 metrics_helper_(new VrMetricsHelper(main_contents_)), | 118 metrics_helper_(new VrMetricsHelper(main_contents_)), |
| 114 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 119 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 120 reprojected_rendering_(reprojected_rendering), |
| 115 weak_ptr_factory_(this) { | 121 weak_ptr_factory_(this) { |
| 116 DCHECK(g_instance == nullptr); | 122 DCHECK(g_instance == nullptr); |
| 117 g_instance = this; | 123 g_instance = this; |
| 118 j_vr_shell_.Reset(env, obj); | 124 j_vr_shell_.Reset(env, obj); |
| 119 | 125 |
| 120 content_input_manager_.reset(new VrInputManager(main_contents_)); | 126 content_input_manager_.reset(new VrInputManager(main_contents_)); |
| 121 ui_input_manager_.reset(new VrInputManager(ui_contents_)); | 127 ui_input_manager_.reset(new VrInputManager(ui_contents_)); |
| 122 | 128 |
| 123 content_compositor_->SetLayer(main_contents_); | 129 content_compositor_->SetLayer(main_contents_); |
| 124 ui_compositor_->SetLayer(ui_contents_); | 130 ui_compositor_->SetLayer(ui_contents_); |
| 125 | 131 |
| 126 gl_thread_.reset(new GLThread(weak_ptr_factory_.GetWeakPtr(), | 132 gl_thread_.reset(new GLThread(weak_ptr_factory_.GetWeakPtr(), |
| 127 content_input_manager_->GetWeakPtr(), | 133 content_input_manager_->GetWeakPtr(), |
| 128 ui_input_manager_->GetWeakPtr(), | 134 ui_input_manager_->GetWeakPtr(), |
| 129 main_thread_task_runner_, | 135 main_thread_task_runner_, |
| 130 gvr_api, | 136 gvr_api, |
| 131 for_web_vr)); | 137 for_web_vr, |
| 138 reprojected_rendering_)); |
| 132 | 139 |
| 133 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); | 140 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); |
| 134 options.priority = base::ThreadPriority::DISPLAY; | 141 options.priority = base::ThreadPriority::DISPLAY; |
| 135 gl_thread_->StartWithOptions(options); | 142 gl_thread_->StartWithOptions(options); |
| 136 | 143 |
| 137 if (for_web_vr) | 144 if (for_web_vr) |
| 138 metrics_helper_->SetWebVREnabled(true); | 145 metrics_helper_->SetWebVREnabled(true); |
| 139 html_interface_.reset(new UiInterface( | 146 html_interface_.reset(new UiInterface( |
| 140 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD, | 147 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD, |
| 141 main_contents_->IsFullscreen())); | 148 main_contents_->IsFullscreen())); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 215 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 209 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | 216 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
| 210 thread->task_runner()->PostTask( | 217 thread->task_runner()->PostTask( |
| 211 FROM_HERE, base::Bind(&VrShellGl::OnResume, thread->GetVrShellGl())); | 218 FROM_HERE, base::Bind(&VrShellGl::OnResume, thread->GetVrShellGl())); |
| 212 | 219 |
| 213 // exit vr session | 220 // exit vr session |
| 214 metrics_helper_->SetVRActive(true); | 221 metrics_helper_->SetVRActive(true); |
| 215 SetShowingOverscrollGlow(false); | 222 SetShowingOverscrollGlow(false); |
| 216 } | 223 } |
| 217 | 224 |
| 225 void VrShell::SetSurface(JNIEnv* env, |
| 226 const JavaParamRef<jobject>& obj, |
| 227 const JavaParamRef<jobject>& surface) { |
| 228 CHECK(!reprojected_rendering_); |
| 229 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
| 230 gfx::AcceleratedWidget window = |
| 231 ANativeWindow_fromSurface(base::android::AttachCurrentThread(), surface); |
| 232 PostToGlThreadWhenReady(base::Bind(&VrShellGl::InitializeGl, |
| 233 thread->GetVrShellGl(), |
| 234 base::Unretained(window))); |
| 235 } |
| 236 |
| 218 void VrShell::SetShowingOverscrollGlow(bool showing_glow) { | 237 void VrShell::SetShowingOverscrollGlow(bool showing_glow) { |
| 219 main_contents_->GetRenderWidgetHostView()->SetShowingOverscrollGlow( | 238 main_contents_->GetRenderWidgetHostView()->SetShowingOverscrollGlow( |
| 220 showing_glow); | 239 showing_glow); |
| 221 } | 240 } |
| 222 | 241 |
| 223 base::WeakPtr<VrShell> VrShell::GetWeakPtr( | 242 base::WeakPtr<VrShell> VrShell::GetWeakPtr( |
| 224 const content::WebContents* web_contents) { | 243 const content::WebContents* web_contents) { |
| 225 // Ensure that the WebContents requesting the VrShell instance is the one | 244 // Ensure that the WebContents requesting the VrShell instance is the one |
| 226 // we created. | 245 // we created. |
| 227 if (g_instance != nullptr && g_instance->ui_contents_ == web_contents) | 246 if (g_instance != nullptr && g_instance->ui_contents_ == web_contents) |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 // ---------------------------------------------------------------------------- | 465 // ---------------------------------------------------------------------------- |
| 447 // Native JNI methods | 466 // Native JNI methods |
| 448 // ---------------------------------------------------------------------------- | 467 // ---------------------------------------------------------------------------- |
| 449 | 468 |
| 450 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj, | 469 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj, |
| 451 const JavaParamRef<jobject>& content_web_contents, | 470 const JavaParamRef<jobject>& content_web_contents, |
| 452 jlong content_window_android, | 471 jlong content_window_android, |
| 453 const JavaParamRef<jobject>& ui_web_contents, | 472 const JavaParamRef<jobject>& ui_web_contents, |
| 454 jlong ui_window_android, jboolean for_web_vr, | 473 jlong ui_window_android, jboolean for_web_vr, |
| 455 const base::android::JavaParamRef<jobject>& delegate, | 474 const base::android::JavaParamRef<jobject>& delegate, |
| 456 jlong gvr_api) { | 475 jlong gvr_api, jboolean reprojected_rendering) { |
| 457 return reinterpret_cast<intptr_t>(new VrShell( | 476 return reinterpret_cast<intptr_t>(new VrShell( |
| 458 env, obj, content::WebContents::FromJavaWebContents(content_web_contents), | 477 env, obj, content::WebContents::FromJavaWebContents(content_web_contents), |
| 459 reinterpret_cast<ui::WindowAndroid*>(content_window_android), | 478 reinterpret_cast<ui::WindowAndroid*>(content_window_android), |
| 460 content::WebContents::FromJavaWebContents(ui_web_contents), | 479 content::WebContents::FromJavaWebContents(ui_web_contents), |
| 461 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), | 480 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), |
| 462 for_web_vr, VrShellDelegate::GetNativeDelegate(env, delegate), | 481 for_web_vr, VrShellDelegate::GetNativeDelegate(env, delegate), |
| 463 reinterpret_cast<gvr_context*>(gvr_api))); | 482 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); |
| 464 } | 483 } |
| 465 | 484 |
| 466 } // namespace vr_shell | 485 } // namespace vr_shell |
| OLD | NEW |