| 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 <thread> | 7 #include <thread> |
| 8 | 8 |
| 9 #include "chrome/browser/android/vr_shell/ui_scene.h" | 9 #include "chrome/browser/android/vr_shell/ui_scene.h" |
| 10 #include "chrome/browser/android/vr_shell/vr_compositor.h" | 10 #include "chrome/browser/android/vr_shell/vr_compositor.h" |
| 11 #include "chrome/browser/android/vr_shell/vr_gl_util.h" | 11 #include "chrome/browser/android/vr_shell/vr_gl_util.h" |
| 12 #include "chrome/browser/android/vr_shell/vr_math.h" | 12 #include "chrome/browser/android/vr_shell/vr_math.h" |
| 13 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 13 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
| 14 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 14 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 15 #include "content/public/browser/android/content_view_core.h" | 15 #include "content/public/browser/android/content_view_core.h" |
| 16 #include "content/public/browser/navigation_controller.h" |
| 16 #include "content/public/browser/render_widget_host.h" | 17 #include "content/public/browser/render_widget_host.h" |
| 17 #include "content/public/browser/render_widget_host_view.h" | 18 #include "content/public/browser/render_widget_host_view.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/referrer.h" |
| 19 #include "jni/VrShell_jni.h" | 21 #include "jni/VrShell_jni.h" |
| 20 #include "ui/android/view_android.h" | 22 #include "ui/android/view_android.h" |
| 21 #include "ui/android/window_android.h" | 23 #include "ui/android/window_android.h" |
| 24 #include "ui/base/page_transition_types.h" |
| 22 #include "ui/gl/gl_bindings.h" | 25 #include "ui/gl/gl_bindings.h" |
| 23 #include "ui/gl/init/gl_factory.h" | 26 #include "ui/gl/init/gl_factory.h" |
| 24 | 27 |
| 25 using base::android::JavaParamRef; | 28 using base::android::JavaParamRef; |
| 26 | 29 |
| 27 namespace { | 30 namespace { |
| 28 // Constant taken from treasure_hunt demo. | 31 // Constant taken from treasure_hunt demo. |
| 29 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; | 32 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 30 | 33 |
| 31 static constexpr float kZNear = 0.1f; | 34 static constexpr float kZNear = 0.1f; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 59 | 62 |
| 60 // Fraction of the z-distance to the object the cursor is drawn at to avoid | 63 // Fraction of the z-distance to the object the cursor is drawn at to avoid |
| 61 // rounding errors drawing the cursor behind the object. | 64 // rounding errors drawing the cursor behind the object. |
| 62 static constexpr float kReticleZOffset = 0.99f; | 65 static constexpr float kReticleZOffset = 0.99f; |
| 63 | 66 |
| 64 // UI element 0 is the browser content rectangle. | 67 // UI element 0 is the browser content rectangle. |
| 65 static constexpr int kBrowserUiElementId = 0; | 68 static constexpr int kBrowserUiElementId = 0; |
| 66 | 69 |
| 67 vr_shell::VrShell* g_instance; | 70 vr_shell::VrShell* g_instance; |
| 68 | 71 |
| 72 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
| 73 |
| 69 } // namespace | 74 } // namespace |
| 70 | 75 |
| 71 namespace vr_shell { | 76 namespace vr_shell { |
| 72 | 77 |
| 73 VrShell::VrShell(JNIEnv* env, jobject obj, | 78 VrShell::VrShell(JNIEnv* env, |
| 74 content::ContentViewCore* content_core, | 79 jobject obj, |
| 75 ui::WindowAndroid* content_window) | 80 content::ContentViewCore* content_cvc, |
| 81 ui::WindowAndroid* content_window, |
| 82 content::ContentViewCore* ui_cvc, |
| 83 ui::WindowAndroid* ui_window) |
| 76 : desktop_screen_tilt_(kDesktopScreenTiltDefault), | 84 : desktop_screen_tilt_(kDesktopScreenTiltDefault), |
| 77 desktop_height_(kDesktopHeightDefault), | 85 desktop_height_(kDesktopHeightDefault), |
| 78 desktop_position_(kDesktopPositionDefault), | 86 desktop_position_(kDesktopPositionDefault), |
| 79 cursor_distance_(-kDesktopPositionDefault.z), | 87 cursor_distance_(-kDesktopPositionDefault.z), |
| 80 content_cvc_(content_core), | 88 content_cvc_(content_cvc), |
| 89 ui_cvc_(ui_cvc), |
| 81 delegate_(nullptr), | 90 delegate_(nullptr), |
| 82 weak_ptr_factory_(this) { | 91 weak_ptr_factory_(this) { |
| 83 g_instance = this; | 92 g_instance = this; |
| 84 j_vr_shell_.Reset(env, obj); | 93 j_vr_shell_.Reset(env, obj); |
| 85 content_compositor_view_.reset(new VrCompositor(content_window)); | 94 content_compositor_.reset(new VrCompositor(content_window)); |
| 95 ui_compositor_.reset(new VrCompositor(ui_window)); |
| 86 | 96 |
| 87 float screen_width = kScreenWidthRatio * desktop_height_; | 97 float screen_width = kScreenWidthRatio * desktop_height_; |
| 88 float screen_height = kScreenHeightRatio * desktop_height_; | 98 float screen_height = kScreenHeightRatio * desktop_height_; |
| 89 std::unique_ptr<ContentRectangle> rect(new ContentRectangle()); | 99 std::unique_ptr<ContentRectangle> rect(new ContentRectangle()); |
| 90 rect->id = kBrowserUiElementId; | 100 rect->id = kBrowserUiElementId; |
| 91 rect->size = {screen_width, screen_height, 1.0f}; | 101 rect->size = {screen_width, screen_height, 1.0f}; |
| 92 scene_.AddUiElement(rect); | 102 scene_.AddUiElement(rect); |
| 93 | 103 |
| 94 desktop_plane_ = scene_.GetUiElementById(kBrowserUiElementId); | 104 desktop_plane_ = scene_.GetUiElementById(kBrowserUiElementId); |
| 95 | 105 |
| 96 content_cvc_->GetWebContents()->GetRenderWidgetHostView() | 106 LoadUIContent(); |
| 97 ->GetRenderWidgetHost()->WasResized(); | |
| 98 } | 107 } |
| 99 | 108 |
| 100 void VrShell::UpdateCompositorLayers(JNIEnv* env, | 109 void VrShell::UpdateCompositorLayers(JNIEnv* env, |
| 101 const JavaParamRef<jobject>& obj) { | 110 const JavaParamRef<jobject>& obj) { |
| 102 content_compositor_view_->SetLayer(content_cvc_); | 111 content_compositor_->SetLayer(content_cvc_); |
| 112 ui_compositor_->SetLayer(ui_cvc_); |
| 103 } | 113 } |
| 104 | 114 |
| 105 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 115 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 106 delete this; | 116 delete this; |
| 107 } | 117 } |
| 108 | 118 |
| 119 void VrShell::LoadUIContent() { |
| 120 GURL url(kVrShellUIURL); |
| 121 ui_cvc_->GetWebContents()->GetController().LoadURL( |
| 122 url, content::Referrer(), |
| 123 ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string("")); |
| 124 } |
| 125 |
| 109 bool RegisterVrShell(JNIEnv* env) { | 126 bool RegisterVrShell(JNIEnv* env) { |
| 110 return RegisterNativesImpl(env); | 127 return RegisterNativesImpl(env); |
| 111 } | 128 } |
| 112 | 129 |
| 113 VrShell::~VrShell() { | 130 VrShell::~VrShell() { |
| 114 g_instance = nullptr; | 131 g_instance = nullptr; |
| 115 gl::init::ClearGLBindings(); | 132 gl::init::ClearGLBindings(); |
| 116 } | 133 } |
| 117 | 134 |
| 118 void VrShell::SetDelegate(JNIEnv* env, | 135 void VrShell::SetDelegate(JNIEnv* env, |
| 119 const base::android::JavaParamRef<jobject>& obj, | 136 const base::android::JavaParamRef<jobject>& obj, |
| 120 const base::android::JavaParamRef<jobject>& delegate) { | 137 const base::android::JavaParamRef<jobject>& delegate) { |
| 121 delegate_ = VrShellDelegate::getNativeDelegate(env, delegate); | 138 delegate_ = VrShellDelegate::getNativeDelegate(env, delegate); |
| 122 } | 139 } |
| 123 | 140 |
| 124 void VrShell::GvrInit(JNIEnv* env, | 141 void VrShell::GvrInit(JNIEnv* env, |
| 125 const JavaParamRef<jobject>& obj, | 142 const JavaParamRef<jobject>& obj, |
| 126 jlong native_gvr_api) { | 143 jlong native_gvr_api) { |
| 127 gvr_api_ = | 144 gvr_api_ = |
| 128 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); | 145 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); |
| 129 | 146 |
| 130 if (delegate_) | 147 if (delegate_) |
| 131 delegate_->OnVrShellReady(this); | 148 delegate_->OnVrShellReady(this); |
| 132 } | 149 } |
| 133 | 150 |
| 134 void VrShell::InitializeGl(JNIEnv* env, | 151 void VrShell::InitializeGl(JNIEnv* env, |
| 135 const JavaParamRef<jobject>& obj, | 152 const JavaParamRef<jobject>& obj, |
| 136 jint texture_data_handle) { | 153 jint content_texture_handle, |
| 154 jint ui_texture_handle) { |
| 137 CHECK(gl::GetGLImplementation() != gl::kGLImplementationNone || | 155 CHECK(gl::GetGLImplementation() != gl::kGLImplementationNone || |
| 138 gl::init::InitializeGLOneOff()); | 156 gl::init::InitializeGLOneOff()); |
| 139 | 157 |
| 140 content_texture_id_ = texture_data_handle; | 158 content_texture_id_ = content_texture_handle; |
| 159 ui_texture_id_ = ui_texture_handle; |
| 160 |
| 141 gvr_api_->InitializeGl(); | 161 gvr_api_->InitializeGl(); |
| 142 std::vector<gvr::BufferSpec> specs; | 162 std::vector<gvr::BufferSpec> specs; |
| 143 specs.push_back(gvr_api_->CreateBufferSpec()); | 163 specs.push_back(gvr_api_->CreateBufferSpec()); |
| 144 render_size_ = specs[0].GetSize(); | 164 render_size_ = specs[0].GetSize(); |
| 145 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); | 165 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); |
| 146 | 166 |
| 147 vr_shell_renderer_.reset(new VrShellRenderer()); | 167 vr_shell_renderer_.reset(new VrShellRenderer()); |
| 148 buffer_viewport_list_.reset( | 168 buffer_viewport_list_.reset( |
| 149 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); | 169 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); |
| 150 buffer_viewport_.reset( | 170 buffer_viewport_.reset( |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 void VrShell::UpdateWebVRTextureBounds( | 491 void VrShell::UpdateWebVRTextureBounds( |
| 472 int eye, float left, float top, float width, float height) { | 492 int eye, float left, float top, float width, float height) { |
| 473 gvr::Rectf bounds = { left, top, width, height }; | 493 gvr::Rectf bounds = { left, top, width, height }; |
| 474 vr_shell_renderer_->GetWebVrRenderer()->UpdateTextureBounds(eye, bounds); | 494 vr_shell_renderer_->GetWebVrRenderer()->UpdateTextureBounds(eye, bounds); |
| 475 } | 495 } |
| 476 | 496 |
| 477 gvr::GvrApi* VrShell::gvr_api() { | 497 gvr::GvrApi* VrShell::gvr_api() { |
| 478 return gvr_api_.get(); | 498 return gvr_api_.get(); |
| 479 } | 499 } |
| 480 | 500 |
| 481 void VrShell::ContentSurfaceDestroyed(JNIEnv* env, | |
| 482 const JavaParamRef<jobject>& object) { | |
| 483 content_compositor_view_->SurfaceDestroyed(); | |
| 484 } | |
| 485 | |
| 486 void VrShell::ContentSurfaceChanged(JNIEnv* env, | 501 void VrShell::ContentSurfaceChanged(JNIEnv* env, |
| 487 const JavaParamRef<jobject>& object, | 502 const JavaParamRef<jobject>& object, |
| 488 jint width, | 503 jint width, |
| 489 jint height, | 504 jint height, |
| 490 const JavaParamRef<jobject>& surface) { | 505 const JavaParamRef<jobject>& surface) { |
| 491 content_compositor_view_->SurfaceChanged((int)width, (int)height, surface); | 506 content_compositor_->SurfaceChanged((int)width, (int)height, surface); |
| 507 } |
| 508 |
| 509 void VrShell::UiSurfaceChanged(JNIEnv* env, |
| 510 const JavaParamRef<jobject>& object, |
| 511 jint width, |
| 512 jint height, |
| 513 const JavaParamRef<jobject>& surface) { |
| 514 ui_compositor_->SurfaceChanged((int)width, (int)height, surface); |
| 492 } | 515 } |
| 493 | 516 |
| 494 // ---------------------------------------------------------------------------- | 517 // ---------------------------------------------------------------------------- |
| 495 // Native JNI methods | 518 // Native JNI methods |
| 496 // ---------------------------------------------------------------------------- | 519 // ---------------------------------------------------------------------------- |
| 497 | 520 |
| 498 jlong Init(JNIEnv* env, | 521 jlong Init(JNIEnv* env, |
| 499 const JavaParamRef<jobject>& obj, | 522 const JavaParamRef<jobject>& obj, |
| 500 const JavaParamRef<jobject>& content_web_contents, | 523 const JavaParamRef<jobject>& content_web_contents, |
| 501 jlong content_window_android) { | 524 jlong content_window_android, |
| 525 const JavaParamRef<jobject>& ui_web_contents, |
| 526 jlong ui_window_android) { |
| 502 content::ContentViewCore* c_core = content::ContentViewCore::FromWebContents( | 527 content::ContentViewCore* c_core = content::ContentViewCore::FromWebContents( |
| 503 content::WebContents::FromJavaWebContents(content_web_contents)); | 528 content::WebContents::FromJavaWebContents(content_web_contents)); |
| 529 content::ContentViewCore* ui_core = content::ContentViewCore::FromWebContents( |
| 530 content::WebContents::FromJavaWebContents(ui_web_contents)); |
| 504 return reinterpret_cast<intptr_t>(new VrShell( | 531 return reinterpret_cast<intptr_t>(new VrShell( |
| 505 env, obj, c_core, | 532 env, obj, c_core, |
| 506 reinterpret_cast<ui::WindowAndroid*>(content_window_android))); | 533 reinterpret_cast<ui::WindowAndroid*>(content_window_android), ui_core, |
| 534 reinterpret_cast<ui::WindowAndroid*>(ui_window_android))); |
| 507 } | 535 } |
| 508 | 536 |
| 509 } // namespace vr_shell | 537 } // namespace vr_shell |
| OLD | NEW |