| 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 "chrome/browser/android/vr_shell/ui_elements.h" | 8 #include "chrome/browser/android/vr_shell/ui_elements.h" |
| 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_controller.h" | 11 #include "chrome/browser/android/vr_shell/vr_controller.h" |
| 12 #include "chrome/browser/android/vr_shell/vr_gesture.h" | 12 #include "chrome/browser/android/vr_shell/vr_gesture.h" |
| 13 #include "chrome/browser/android/vr_shell/vr_gl_util.h" | 13 #include "chrome/browser/android/vr_shell/vr_gl_util.h" |
| 14 #include "chrome/browser/android/vr_shell/vr_input_manager.h" | 14 #include "chrome/browser/android/vr_shell/vr_input_manager.h" |
| 15 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 15 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
| 16 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 16 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/render_widget_host.h" | 18 #include "content/public/browser/render_widget_host.h" |
| 19 #include "content/public/browser/render_widget_host_view.h" | 19 #include "content/public/browser/render_widget_host_view.h" |
| 20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/referrer.h" | 21 #include "content/public/common/referrer.h" |
| 22 #include "content/public/common/screen_info.h" | 22 #include "content/public/common/screen_info.h" |
| 23 #include "jni/VrShellImpl_jni.h" | 23 #include "jni/VrShellImpl_jni.h" |
| 24 #include "third_party/WebKit/public/web/WebInputEvent.h" | 24 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 25 #include "ui/android/view_android.h" | 25 #include "ui/android/view_android.h" |
| 26 #include "ui/android/window_android.h" | 26 #include "ui/android/window_android.h" |
| 27 #include "ui/base/page_transition_types.h" | 27 #include "ui/base/page_transition_types.h" |
| 28 #include "ui/display/android/screen_android.h" |
| 29 #include "ui/display/display.h" |
| 30 #include "ui/gfx/android/device_display_info.h" |
| 28 #include "ui/gl/gl_bindings.h" | 31 #include "ui/gl/gl_bindings.h" |
| 29 #include "ui/gl/init/gl_factory.h" | 32 #include "ui/gl/init/gl_factory.h" |
| 30 | 33 |
| 31 using base::android::JavaParamRef; | 34 using base::android::JavaParamRef; |
| 35 using base::android::ScopedJavaLocalRef; |
| 32 | 36 |
| 33 namespace { | 37 namespace { |
| 34 // Constant taken from treasure_hunt demo. | 38 // Constant taken from treasure_hunt demo. |
| 35 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; | 39 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 36 | 40 |
| 37 static constexpr float kZNear = 0.1f; | 41 static constexpr float kZNear = 0.1f; |
| 38 static constexpr float kZFar = 1000.0f; | 42 static constexpr float kZFar = 1000.0f; |
| 39 | 43 |
| 40 static constexpr gvr::Vec3f kDesktopPositionDefault = {0.0f, 0.0f, -2.0f}; | 44 static constexpr gvr::Vec3f kDesktopPositionDefault = {0.0f, 0.0f, -2.0f}; |
| 41 static constexpr float kDesktopHeightDefault = 1.6f; | 45 static constexpr float kDesktopHeightDefault = 1.6f; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // into geometry (they ignore the Z buffer), leading to odd effects | 86 // into geometry (they ignore the Z buffer), leading to odd effects |
| 83 // if they are far away. | 87 // if they are far away. |
| 84 static constexpr vr_shell::Recti kWebVrWarningTransientRect = { | 88 static constexpr vr_shell::Recti kWebVrWarningTransientRect = { |
| 85 0, 128, 512, 256}; | 89 0, 128, 512, 256}; |
| 86 static constexpr vr_shell::Recti kWebVrWarningPermanentRect = {0, 0, 512, 128}; | 90 static constexpr vr_shell::Recti kWebVrWarningPermanentRect = {0, 0, 512, 128}; |
| 87 static constexpr float kWebVrWarningDistance = 0.7f; // meters | 91 static constexpr float kWebVrWarningDistance = 0.7f; // meters |
| 88 static constexpr float kWebVrWarningPermanentAngle = 16.3f; // degrees up | 92 static constexpr float kWebVrWarningPermanentAngle = 16.3f; // degrees up |
| 89 // How long the transient warning needs to be displayed. | 93 // How long the transient warning needs to be displayed. |
| 90 static constexpr int64_t kWebVrWarningSeconds = 30; | 94 static constexpr int64_t kWebVrWarningSeconds = 30; |
| 91 | 95 |
| 96 static constexpr int kContentDisplayId = 1; |
| 97 static constexpr int kUiDisplayId = 2; |
| 98 |
| 92 vr_shell::VrShell* g_instance; | 99 vr_shell::VrShell* g_instance; |
| 93 | 100 |
| 94 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; | 101 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
| 95 | 102 |
| 96 float Distance(const gvr::Vec3f& vec1, const gvr::Vec3f& vec2) { | 103 float Distance(const gvr::Vec3f& vec1, const gvr::Vec3f& vec2) { |
| 97 float xdiff = (vec1.x - vec2.x); | 104 float xdiff = (vec1.x - vec2.x); |
| 98 float ydiff = (vec1.y - vec2.y); | 105 float ydiff = (vec1.y - vec2.y); |
| 99 float zdiff = (vec1.z - vec2.z); | 106 float zdiff = (vec1.z - vec2.z); |
| 100 float scale = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff; | 107 float scale = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff; |
| 101 return std::sqrt(scale); | 108 return std::sqrt(scale); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 117 quat.qw = 0.0f; | 124 quat.qw = 0.0f; |
| 118 } else { | 125 } else { |
| 119 quat.qx = vec.y; | 126 quat.qx = vec.y; |
| 120 quat.qy = -vec.x; | 127 quat.qy = -vec.x; |
| 121 quat.qz = 0.0f; | 128 quat.qz = 0.0f; |
| 122 vr_shell::NormalizeQuat(quat); | 129 vr_shell::NormalizeQuat(quat); |
| 123 } | 130 } |
| 124 return quat; | 131 return quat; |
| 125 } | 132 } |
| 126 | 133 |
| 134 void populateDisplayWithDefaults(display::Display& display, float dpr, |
| 135 gfx::Rect bounds) { |
| 136 gfx::DeviceDisplayInfo device_info; |
| 137 display.SetScaleAndBounds(dpr, bounds); |
| 138 display.SetRotationAsDegree(device_info.GetRotationDegrees()); |
| 139 display.set_color_depth(device_info.GetBitsPerPixel()); |
| 140 display.set_depth_per_component(device_info.GetBitsPerComponent()); |
| 141 display.set_is_monochrome(device_info.GetBitsPerComponent() == 0); |
| 142 } |
| 143 |
| 127 } // namespace | 144 } // namespace |
| 128 | 145 |
| 129 namespace vr_shell { | 146 namespace vr_shell { |
| 130 | 147 |
| 131 VrShell::VrShell(JNIEnv* env, jobject obj, | 148 VrShell::VrShell(JNIEnv* env, jobject obj, |
| 132 content::WebContents* main_contents, | 149 content::WebContents* main_contents, |
| 133 ui::WindowAndroid* content_window, | 150 ui::WindowAndroid* content_window, |
| 134 content::WebContents* ui_contents, | 151 content::WebContents* ui_contents, |
| 135 ui::WindowAndroid* ui_window) | 152 ui::WindowAndroid* ui_window) |
| 136 : desktop_screen_tilt_(kDesktopScreenTiltDefault), | 153 : desktop_screen_tilt_(kDesktopScreenTiltDefault), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 157 | 174 |
| 158 gvr::Mat4f identity; | 175 gvr::Mat4f identity; |
| 159 SetIdentityM(identity); | 176 SetIdentityM(identity); |
| 160 webvr_head_pose_.resize(kPoseRingBufferSize, identity); | 177 webvr_head_pose_.resize(kPoseRingBufferSize, identity); |
| 161 } | 178 } |
| 162 | 179 |
| 163 void VrShell::UpdateCompositorLayers(JNIEnv* env, | 180 void VrShell::UpdateCompositorLayers(JNIEnv* env, |
| 164 const JavaParamRef<jobject>& obj) { | 181 const JavaParamRef<jobject>& obj) { |
| 165 content_compositor_->SetLayer(main_contents_); | 182 content_compositor_->SetLayer(main_contents_); |
| 166 ui_compositor_->SetLayer(ui_contents_); | 183 ui_compositor_->SetLayer(ui_contents_); |
| 184 |
| 185 // TODO(mthiesse): These are temporary placeholders until the UI js is |
| 186 // updated to call these functions. |
| 187 SetUiCssSize(1920, 1080, 1.0); |
| 188 SetContentCssSize(1280, 720, 1.0); |
| 167 } | 189 } |
| 168 | 190 |
| 169 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 191 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 170 delete this; | 192 delete this; |
| 171 } | 193 } |
| 172 | 194 |
| 173 void VrShell::LoadUIContent() { | 195 void VrShell::LoadUIContent() { |
| 174 GURL url(kVrShellUIURL); | 196 GURL url(kVrShellUIURL); |
| 175 ui_contents_->GetController().LoadURL( | 197 ui_contents_->GetController().LoadURL( |
| 176 url, content::Referrer(), | 198 url, content::Referrer(), |
| 177 ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string("")); | 199 ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string("")); |
| 178 } | 200 } |
| 179 | 201 |
| 180 bool RegisterVrShell(JNIEnv* env) { | 202 bool RegisterVrShell(JNIEnv* env) { |
| 181 return RegisterNativesImpl(env); | 203 return RegisterNativesImpl(env); |
| 182 } | 204 } |
| 183 | 205 |
| 184 VrShell::~VrShell() { | 206 VrShell::~VrShell() { |
| 207 display::ScreenAndroid* screen = reinterpret_cast<display::ScreenAndroid*>( |
| 208 display::Screen::GetScreen()); |
| 209 screen->ClearDisplayForWindow(ui_contents_->GetNativeView()); |
| 210 screen->ClearDisplayForWindow(main_contents_->GetNativeView()); |
| 185 g_instance = nullptr; | 211 g_instance = nullptr; |
| 186 gl::init::ClearGLBindings(); | 212 gl::init::ClearGLBindings(); |
| 187 } | 213 } |
| 188 | 214 |
| 189 void VrShell::SetDelegate(JNIEnv* env, | 215 void VrShell::SetDelegate(JNIEnv* env, |
| 190 const base::android::JavaParamRef<jobject>& obj, | 216 const base::android::JavaParamRef<jobject>& obj, |
| 191 const base::android::JavaParamRef<jobject>& delegate) { | 217 const base::android::JavaParamRef<jobject>& delegate) { |
| 192 delegate_ = VrShellDelegate::getNativeDelegate(env, delegate); | 218 delegate_ = VrShellDelegate::getNativeDelegate(env, delegate); |
| 193 } | 219 } |
| 194 | 220 |
| (...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 void VrShell::UpdateWebVRTextureBounds( | 800 void VrShell::UpdateWebVRTextureBounds( |
| 775 int eye, float left, float top, float width, float height) { | 801 int eye, float left, float top, float width, float height) { |
| 776 gvr::Rectf bounds = { left, top, width, height }; | 802 gvr::Rectf bounds = { left, top, width, height }; |
| 777 vr_shell_renderer_->GetWebVrRenderer()->UpdateTextureBounds(eye, bounds); | 803 vr_shell_renderer_->GetWebVrRenderer()->UpdateTextureBounds(eye, bounds); |
| 778 } | 804 } |
| 779 | 805 |
| 780 gvr::GvrApi* VrShell::gvr_api() { | 806 gvr::GvrApi* VrShell::gvr_api() { |
| 781 return gvr_api_.get(); | 807 return gvr_api_.get(); |
| 782 } | 808 } |
| 783 | 809 |
| 784 void VrShell::ContentSurfaceChanged(JNIEnv* env, | 810 void VrShell::SurfacesChanged(JNIEnv* env, |
| 785 const JavaParamRef<jobject>& object, | 811 const JavaParamRef<jobject>& object, |
| 786 jint width, | 812 const JavaParamRef<jobject>& content_surface, |
| 787 jint height, | 813 const JavaParamRef<jobject>& ui_surface) { |
| 788 const JavaParamRef<jobject>& surface) { | 814 content_compositor_->SurfaceChanged(content_surface); |
| 789 content_compositor_->SurfaceChanged((int)width, (int)height, surface); | 815 ui_compositor_->SurfaceChanged(ui_surface); |
| 790 content::ScreenInfo result; | |
| 791 main_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost()-> | |
| 792 GetScreenInfo(&result); | |
| 793 float dpr = result.device_scale_factor; | |
| 794 scene_->GetUiElementById(kBrowserUiElementId)->copy_rect = | |
| 795 { 0, 0, width / dpr, height / dpr }; | |
| 796 } | 816 } |
| 797 | 817 |
| 798 void VrShell::UiSurfaceChanged(JNIEnv* env, | 818 void VrShell::ContentBoundsChanged(JNIEnv* env, |
| 799 const JavaParamRef<jobject>& object, | 819 const JavaParamRef<jobject>& object, |
| 800 jint width, | 820 jint width, jint height) { |
| 801 jint height, | 821 content_compositor_->SetWindowBounds(width, height); |
| 802 const JavaParamRef<jobject>& surface) { | 822 main_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost() |
| 803 ui_compositor_->SurfaceChanged((int)width, (int)height, surface); | 823 ->WasResized(); |
| 804 content::ScreenInfo result; | 824 } |
| 805 ui_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost()->GetScreenInfo( | 825 |
| 806 &result); | 826 void VrShell::UIBoundsChanged(JNIEnv* env, |
| 807 ui_tex_width_ = width / result.device_scale_factor; | 827 const JavaParamRef<jobject>& object, |
| 808 ui_tex_height_ = height / result.device_scale_factor; | 828 jint width, |
| 829 jint height) { |
| 830 ui_compositor_->SetWindowBounds(width, height); |
| 831 ui_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost()->WasResized(); |
| 809 } | 832 } |
| 810 | 833 |
| 811 UiScene* VrShell::GetScene() { | 834 UiScene* VrShell::GetScene() { |
| 812 return scene_.get(); | 835 return scene_.get(); |
| 813 } | 836 } |
| 814 | 837 |
| 815 void VrShell::QueueTask(base::Callback<void()>& callback) { | 838 void VrShell::QueueTask(base::Callback<void()>& callback) { |
| 816 base::AutoLock lock(task_queue_lock_); | 839 base::AutoLock lock(task_queue_lock_); |
| 817 task_queue_.push(callback); | 840 task_queue_.push(callback); |
| 818 } | 841 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 849 controller.Reload(false); | 872 controller.Reload(false); |
| 850 break; | 873 break; |
| 851 case ZOOM_OUT: // Not handled yet. | 874 case ZOOM_OUT: // Not handled yet. |
| 852 case ZOOM_IN: // Not handled yet. | 875 case ZOOM_IN: // Not handled yet. |
| 853 break; | 876 break; |
| 854 default: | 877 default: |
| 855 NOTREACHED(); | 878 NOTREACHED(); |
| 856 } | 879 } |
| 857 } | 880 } |
| 858 | 881 |
| 882 void VrShell::SetContentCssSize(float width, float height, float dpr) { |
| 883 const gfx::Rect bounds_in_pixels = gfx::Rect(gfx::ScaleToCeiledSize( |
| 884 gfx::Size(width, height), dpr)); |
| 885 display::Display display(kContentDisplayId); |
| 886 |
| 887 populateDisplayWithDefaults(display, dpr, bounds_in_pixels); |
| 888 |
| 889 display::ScreenAndroid* screen = reinterpret_cast<display::ScreenAndroid*>( |
| 890 display::Screen::GetScreen()); |
| 891 screen->SetDisplayForWindow(main_contents_->GetNativeView(), display); |
| 892 |
| 893 JNIEnv* env = base::android::AttachCurrentThread(); |
| 894 Java_VrShellImpl_setContentCssSize(env, j_vr_shell_.obj(), width, height, |
| 895 dpr); |
| 896 |
| 897 scene_->GetUiElementById(kBrowserUiElementId)->copy_rect = |
| 898 { 0, 0, width, height }; |
| 899 } |
| 900 |
| 901 void VrShell::SetUiCssSize(float width, float height, float dpr) { |
| 902 const gfx::Rect bounds_in_pixels = gfx::Rect(gfx::ScaleToCeiledSize( |
| 903 gfx::Size(width, height), dpr)); |
| 904 display::Display display(kUiDisplayId); |
| 905 |
| 906 populateDisplayWithDefaults(display, dpr, bounds_in_pixels); |
| 907 |
| 908 display::ScreenAndroid* screen = reinterpret_cast<display::ScreenAndroid*>( |
| 909 display::Screen::GetScreen()); |
| 910 screen->SetDisplayForWindow(ui_contents_->GetNativeView(), display); |
| 911 |
| 912 JNIEnv* env = base::android::AttachCurrentThread(); |
| 913 Java_VrShellImpl_setUiCssSize(env, j_vr_shell_.obj(), width, height, dpr); |
| 914 |
| 915 ui_tex_width_ = width; |
| 916 ui_tex_height_ = height; |
| 917 } |
| 918 |
| 859 // ---------------------------------------------------------------------------- | 919 // ---------------------------------------------------------------------------- |
| 860 // Native JNI methods | 920 // Native JNI methods |
| 861 // ---------------------------------------------------------------------------- | 921 // ---------------------------------------------------------------------------- |
| 862 | 922 |
| 863 jlong Init(JNIEnv* env, | 923 jlong Init(JNIEnv* env, |
| 864 const JavaParamRef<jobject>& obj, | 924 const JavaParamRef<jobject>& obj, |
| 865 const JavaParamRef<jobject>& content_web_contents, | 925 const JavaParamRef<jobject>& content_web_contents, |
| 866 jlong content_window_android, | 926 jlong content_window_android, |
| 867 const JavaParamRef<jobject>& ui_web_contents, | 927 const JavaParamRef<jobject>& ui_web_contents, |
| 868 jlong ui_window_android) { | 928 jlong ui_window_android) { |
| 869 return reinterpret_cast<intptr_t>(new VrShell( | 929 return reinterpret_cast<intptr_t>(new VrShell( |
| 870 env, obj, content::WebContents::FromJavaWebContents(content_web_contents), | 930 env, obj, content::WebContents::FromJavaWebContents(content_web_contents), |
| 871 reinterpret_cast<ui::WindowAndroid*>(content_window_android), | 931 reinterpret_cast<ui::WindowAndroid*>(content_window_android), |
| 872 content::WebContents::FromJavaWebContents(ui_web_contents), | 932 content::WebContents::FromJavaWebContents(ui_web_contents), |
| 873 reinterpret_cast<ui::WindowAndroid*>(ui_window_android))); | 933 reinterpret_cast<ui::WindowAndroid*>(ui_window_android))); |
| 874 } | 934 } |
| 875 | 935 |
| 876 } // namespace vr_shell | 936 } // namespace vr_shell |
| OLD | NEW |