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 "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/android/vr_shell/ui_interface.h" | 13 #include "chrome/browser/android/vr_shell/ui_interface.h" |
14 #include "chrome/browser/android/vr_shell/vr_compositor.h" | 14 #include "chrome/browser/android/vr_shell/vr_compositor.h" |
15 #include "chrome/browser/android/vr_shell/vr_input_manager.h" | 15 #include "chrome/browser/android/vr_shell/vr_input_manager.h" |
16 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 16 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
17 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" | 17 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" |
18 #include "chrome/browser/android/vr_shell/vr_usage_monitor.h" | 18 #include "chrome/browser/android/vr_shell/vr_usage_monitor.h" |
19 #include "chrome/browser/android/vr_shell/vr_web_contents_observer.h" | 19 #include "chrome/browser/android/vr_shell/vr_web_contents_observer.h" |
20 #include "content/public/browser/navigation_controller.h" | 20 #include "content/public/browser/navigation_controller.h" |
21 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/render_widget_host.h" | 22 #include "content/public/browser/render_widget_host.h" |
23 #include "content/public/browser/render_widget_host_view.h" | 23 #include "content/public/browser/render_widget_host_view.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/referrer.h" | 25 #include "content/public/common/referrer.h" |
26 #include "device/vr/android/gvr/gvr_device_provider.h" | 26 #include "device/vr/android/gvr/gvr_device_provider.h" |
27 #include "gpu/ipc/common/gpu_surface_tracker.h" | |
28 #include "gpu/ipc/common/surface_handle.h" | |
27 #include "jni/VrShellImpl_jni.h" | 29 #include "jni/VrShellImpl_jni.h" |
28 #include "ui/android/view_android.h" | 30 #include "ui/android/view_android.h" |
29 #include "ui/android/window_android.h" | 31 #include "ui/android/window_android.h" |
30 #include "ui/base/page_transition_types.h" | 32 #include "ui/base/page_transition_types.h" |
31 #include "ui/display/display.h" | 33 #include "ui/display/display.h" |
32 #include "ui/display/screen.h" | 34 #include "ui/display/screen.h" |
33 | 35 |
36 #include <android/native_window.h> | |
37 #include <android/native_window_jni.h> | |
38 | |
34 using base::android::JavaParamRef; | 39 using base::android::JavaParamRef; |
35 using base::android::JavaRef; | 40 using base::android::JavaRef; |
36 | 41 |
37 namespace vr_shell { | 42 namespace vr_shell { |
38 | 43 |
39 namespace { | 44 namespace { |
40 vr_shell::VrShell* g_instance; | 45 vr_shell::VrShell* g_instance; |
41 | 46 |
42 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; | 47 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
43 | 48 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 ui_contents_->GetController().LoadURL( | 159 ui_contents_->GetController().LoadURL( |
155 url, content::Referrer(), | 160 url, content::Referrer(), |
156 ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string("")); | 161 ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string("")); |
157 } | 162 } |
158 | 163 |
159 bool RegisterVrShell(JNIEnv* env) { | 164 bool RegisterVrShell(JNIEnv* env) { |
160 return RegisterNativesImpl(env); | 165 return RegisterNativesImpl(env); |
161 } | 166 } |
162 | 167 |
163 VrShell::~VrShell() { | 168 VrShell::~VrShell() { |
169 VLOG(1) << __FUNCTION__ << ": Destructor for presenting delegate and its gvr_a pi"; | |
164 { | 170 { |
165 // The GvrLayout is, and must always be, used only on the UI thread, and the | 171 // The GvrLayout is, and must always be, used only on the UI thread, and the |
166 // GvrApi used for rendering should only be used from the GL thread as it's | 172 // GvrApi used for rendering should only be used from the GL thread as it's |
167 // not thread safe. However, the GvrLayout owns the GvrApi instance, and | 173 // not thread safe. However, the GvrLayout owns the GvrApi instance, and |
168 // when it gets shut down it deletes the GvrApi instance with it. Therefore, | 174 // when it gets shut down it deletes the GvrApi instance with it. Therefore, |
169 // we need to block shutting down the GvrLayout on stopping our GL thread | 175 // we need to block shutting down the GvrLayout on stopping our GL thread |
170 // from using the GvrApi instance. | 176 // from using the GvrApi instance. |
171 // base::Thread::Stop, which is called when destroying the thread, asserts | 177 // base::Thread::Stop, which is called when destroying the thread, asserts |
172 // that IO is allowed to prevent jank, but there shouldn't be any concerns | 178 // that IO is allowed to prevent jank, but there shouldn't be any concerns |
173 // regarding jank in this case, because we're switching from 3D to 2D, | 179 // regarding jank in this case, because we're switching from 3D to 2D, |
174 // adding/removing a bunch of Java views, and probably changing device | 180 // adding/removing a bunch of Java views, and probably changing device |
175 // orientation here. | 181 // orientation here. |
176 base::ThreadRestrictions::ScopedAllowIO allow_io; | 182 base::ThreadRestrictions::ScopedAllowIO allow_io; |
177 gl_thread_.reset(); | 183 gl_thread_.reset(); |
artem.bolgar
2017/02/28 21:40:22
gl_thread_.reset() kills VrShellGl instance along
| |
178 } | 184 } |
179 delegate_->RemoveDelegate(); | 185 delegate_->RemoveDelegate(); |
180 g_instance = nullptr; | 186 g_instance = nullptr; |
181 } | 187 } |
182 | 188 |
183 void VrShell::PostToGlThreadWhenReady(const base::Closure& task) { | 189 void VrShell::PostToGlThreadWhenReady(const base::Closure& task) { |
184 // TODO(mthiesse): Remove this blocking wait. Queue up events if thread isn't | 190 // TODO(mthiesse): Remove this blocking wait. Queue up events if thread isn't |
185 // finished starting? | 191 // finished starting? |
186 gl_thread_->WaitUntilThreadStarted(); | 192 gl_thread_->WaitUntilThreadStarted(); |
187 gl_thread_->task_runner()->PostTask(FROM_HERE, task); | 193 gl_thread_->task_runner()->PostTask(FROM_HERE, task); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 | 237 |
232 void VrShell::OnDomContentsLoaded() { | 238 void VrShell::OnDomContentsLoaded() { |
233 html_interface_->SetURL(main_contents_->GetVisibleURL()); | 239 html_interface_->SetURL(main_contents_->GetVisibleURL()); |
234 html_interface_->SetLoading(main_contents_->IsLoading()); | 240 html_interface_->SetLoading(main_contents_->IsLoading()); |
235 html_interface_->OnDomContentsLoaded(); | 241 html_interface_->OnDomContentsLoaded(); |
236 } | 242 } |
237 | 243 |
238 void VrShell::SetWebVrMode(JNIEnv* env, | 244 void VrShell::SetWebVrMode(JNIEnv* env, |
239 const base::android::JavaParamRef<jobject>& obj, | 245 const base::android::JavaParamRef<jobject>& obj, |
240 bool enabled) { | 246 bool enabled) { |
247 VLOG(1) << __FUNCTION__ << ": enabled=" << enabled; | |
241 metrics_helper_->SetWebVREnabled(enabled); | 248 metrics_helper_->SetWebVREnabled(enabled); |
242 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | 249 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
243 PostToGlThreadWhenReady( | 250 PostToGlThreadWhenReady( |
244 base::Bind(&VrShellGl::SetWebVrMode, thread->GetVrShellGl(), enabled)); | 251 base::Bind(&VrShellGl::SetWebVrMode, thread->GetVrShellGl(), enabled)); |
245 if (enabled) { | 252 if (enabled) { |
246 html_interface_->SetMode(UiInterface::Mode::WEB_VR); | 253 html_interface_->SetMode(UiInterface::Mode::WEB_VR); |
247 } else { | 254 } else { |
248 html_interface_->SetMode(UiInterface::Mode::STANDARD); | 255 html_interface_->SetMode(UiInterface::Mode::STANDARD); |
249 } | 256 } |
250 } | 257 } |
251 | 258 |
252 void VrShell::SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) { | 259 void VrShell::SetWebVRGvrPose(const gvr::Mat4f& pose, uint32_t pose_num, int64_t pose_time_nanos) { |
253 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | 260 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
254 if (thread->IsRunning()) { | 261 if (thread->IsRunning()) { |
255 thread->task_runner()->PostTask( | 262 thread->task_runner()->PostTask( |
256 FROM_HERE, base::Bind(&VrShellGl::SetGvrPoseForWebVr, | 263 FROM_HERE, base::Bind(&VrShellGl::SetWebVRGvrPose, |
257 thread->GetVrShellGl(), pose, pose_num)); | 264 thread->GetVrShellGl(), pose, pose_num, pose_time_ nanos)); |
258 } | 265 } |
259 } | 266 } |
260 | 267 |
261 void VrShell::SetWebVRRenderSurfaceSize(int width, int height) { | |
262 // TODO(klausw,crbug.com/655722): Change the GVR render size and set the WebVR | |
263 // render surface size. | |
264 } | |
265 | |
266 gvr::Sizei VrShell::GetWebVRCompositorSurfaceSize() { | |
267 const gfx::Size& size = content_compositor_->GetWindowBounds(); | |
268 return {size.width(), size.height()}; | |
269 } | |
270 | |
271 void VrShell::SetWebVRSecureOrigin(bool secure_origin) { | 268 void VrShell::SetWebVRSecureOrigin(bool secure_origin) { |
272 // TODO(cjgrant): Align this state with the logic that drives the omnibox. | 269 // TODO(cjgrant): Align this state with the logic that drives the omnibox. |
273 html_interface_->SetWebVRSecureOrigin(secure_origin); | 270 html_interface_->SetWebVRSecureOrigin(secure_origin); |
274 } | 271 } |
275 | 272 |
276 void VrShell::SubmitWebVRFrame() {} | 273 void VrShell::SubmitWebVRFrame(int32_t surface_handle, device::mojom::VRPosePtr pose) { |
274 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | |
275 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SubmitWebVRFrame, | |
276 thread->GetVrShellGl(), | |
277 surface_handle, | |
278 std::move(pose))); | |
279 } | |
277 | 280 |
278 void VrShell::UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | 281 void VrShell::UpdateWebVRTextureBounds(uint32_t for_pose_index, |
282 const gvr::Rectf& left_bounds, | |
279 const gvr::Rectf& right_bounds) { | 283 const gvr::Rectf& right_bounds) { |
280 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | 284 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
281 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds, | 285 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds, |
282 thread->GetVrShellGl(), left_bounds, | 286 thread->GetVrShellGl(), |
287 for_pose_index, | |
288 left_bounds, | |
283 right_bounds)); | 289 right_bounds)); |
284 } | 290 } |
285 | 291 |
292 void VrShell::GetWebVRSurfaceHandle(int width, int height, const device::mojom:: VRDisplay::GetSurfaceHandleCallback& callback) { | |
293 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | |
294 PostToGlThreadWhenReady(base::Bind(&VrShellGl::GetWebVRSurfaceHandle, | |
295 thread->GetVrShellGl(), | |
296 width, | |
297 height, | |
298 std::move(callback))); | |
299 } | |
300 | |
286 // TODO(mthiesse): Do not expose GVR API outside of GL thread. | 301 // TODO(mthiesse): Do not expose GVR API outside of GL thread. |
287 // It's not thread-safe. | 302 // It's not thread-safe. |
288 gvr::GvrApi* VrShell::gvr_api() { | 303 gvr::GvrApi* VrShell::gvr_api() { |
289 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); | 304 GLThread* thread = static_cast<GLThread*>(gl_thread_.get()); |
290 if (thread->GetVrShellGlUnsafe()) { | 305 if (thread->GetVrShellGlUnsafe()) { |
291 return thread->GetVrShellGlUnsafe()->gvr_api(); | 306 return thread->GetVrShellGlUnsafe()->gvr_api(); |
292 } | 307 } |
293 CHECK(false); | 308 CHECK(false); |
294 return nullptr; | 309 return nullptr; |
295 } | 310 } |
296 | 311 |
297 void VrShell::SurfacesChanged(jobject content_surface, jobject ui_surface) { | 312 void VrShell::SurfacesChanged(jobject content_surface, jobject ui_surface) { |
298 content_compositor_->SurfaceChanged(content_surface); | 313 content_compositor_->SurfaceChanged(content_surface); |
299 ui_compositor_->SurfaceChanged(ui_surface); | 314 ui_compositor_->SurfaceChanged(ui_surface); |
300 } | 315 } |
301 | 316 |
302 void VrShell::GvrDelegateReady() { | 317 void VrShell::GvrDelegateReady() { |
303 delegate_->SetDelegate(this); | 318 delegate_->SetDelegate(this); |
304 } | 319 } |
305 | 320 |
321 void VrShell::OnWebVRFrameSubmitted(int32_t surface_handle, uint32_t frame_index , double elapsed) { | |
322 delegate_->device_provider()->OnFrameSubmitted(surface_handle, frame_index, el apsed); | |
323 } | |
324 | |
306 void VrShell::AppButtonPressed() { | 325 void VrShell::AppButtonPressed() { |
307 #if defined(ENABLE_VR_SHELL) | 326 #if defined(ENABLE_VR_SHELL) |
308 html_interface_->SetMenuMode(!html_interface_->GetMenuMode()); | 327 html_interface_->SetMenuMode(!html_interface_->GetMenuMode()); |
309 | 328 |
310 // TODO(mthiesse): The page is no longer visible when in menu mode. We | 329 // TODO(mthiesse): The page is no longer visible when in menu mode. We |
311 // should unfocus or otherwise let it know it's hidden. | 330 // should unfocus or otherwise let it know it's hidden. |
312 if (html_interface_->GetMode() == UiInterface::Mode::WEB_VR) { | 331 if (html_interface_->GetMode() == UiInterface::Mode::WEB_VR) { |
313 if (delegate_->device_provider()) { | 332 if (delegate_->device_provider()) { |
314 if (html_interface_->GetMenuMode()) { | 333 if (html_interface_->GetMenuMode()) { |
315 delegate_->device_provider()->OnDisplayBlur(); | 334 delegate_->device_provider()->OnDisplayBlur(); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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))); |
464 } | 483 } |
465 | 484 |
466 } // namespace vr_shell | 485 } // namespace vr_shell |
OLD | NEW |