| 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/vr_shell_renderer.h" | 9 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 10 #include "chrome/browser/android/vr_shell/vr_util.h" | 10 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 11 #include "content/public/browser/android/content_view_core.h" |
| 12 #include "content/public/browser/render_widget_host.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 11 #include "jni/VrShell_jni.h" | 15 #include "jni/VrShell_jni.h" |
| 16 #include "ui/android/view_android.h" |
| 17 #include "ui/android/window_android.h" |
| 12 #include "ui/gl/gl_bindings.h" | 18 #include "ui/gl/gl_bindings.h" |
| 13 #include "ui/gl/init/gl_factory.h" | 19 #include "ui/gl/init/gl_factory.h" |
| 14 | 20 |
| 15 using base::android::JavaParamRef; | 21 using base::android::JavaParamRef; |
| 16 | 22 |
| 17 namespace { | 23 namespace { |
| 18 // Constant taken from treasure_hunt demo. | 24 // Constant taken from treasure_hunt demo. |
| 19 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; | 25 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 20 | 26 |
| 21 static constexpr float kZNear = 0.1f; | 27 static constexpr float kZNear = 0.1f; |
| 22 static constexpr float kZFar = 1000.0f; | 28 static constexpr float kZFar = 1000.0f; |
| 23 | 29 |
| 24 static constexpr gvr::Vec3f kDesktopPositionDefault = {0.0f, 0.0f, -2.0f}; | 30 static constexpr gvr::Vec3f kDesktopPositionDefault = {0.0f, 0.0f, -2.0f}; |
| 25 static constexpr float kDesktopHeightDefault = 1.6f; | 31 static constexpr float kDesktopHeightDefault = 1.6f; |
| 26 | 32 |
| 27 // Screen angle in degrees. 0 = vertical, positive = top closer. | 33 // Screen angle in degrees. 0 = vertical, positive = top closer. |
| 28 static constexpr float kDesktopScreenTiltDefault = 0; | 34 static constexpr float kDesktopScreenTiltDefault = 0; |
| 29 | 35 |
| 30 static constexpr float kScreenHeightMeters = 2.0f; | 36 static constexpr float kScreenHeightRatio = 1.0f; |
| 31 static constexpr float kScreenWidthMeters = 2.0f; | 37 static constexpr float kScreenWidthRatio = 16.0f / 9.0f; |
| 32 } | 38 } // namespace |
| 33 | 39 |
| 34 namespace vr_shell { | 40 namespace vr_shell { |
| 35 | 41 |
| 36 VrShell::VrShell(JNIEnv* env, jobject obj) | 42 VrShell::VrShell(JNIEnv* env, jobject obj, |
| 43 content::ContentViewCore* content_core, |
| 44 ui::WindowAndroid* content_window) |
| 37 : desktop_screen_tilt_(kDesktopScreenTiltDefault), | 45 : desktop_screen_tilt_(kDesktopScreenTiltDefault), |
| 38 desktop_height_(kDesktopHeightDefault), | 46 desktop_height_(kDesktopHeightDefault), |
| 39 desktop_position_(kDesktopPositionDefault) { | 47 desktop_position_(kDesktopPositionDefault), |
| 48 content_core_(content_core) { |
| 40 j_vr_shell_.Reset(env, obj); | 49 j_vr_shell_.Reset(env, obj); |
| 50 content_compositor_view_.reset(new SimpleCompositorView(content_window)); |
| 41 ui_rects_.emplace_back(new ContentRectangle()); | 51 ui_rects_.emplace_back(new ContentRectangle()); |
| 42 desktop_plane_ = ui_rects_.back().get(); | 52 desktop_plane_ = ui_rects_.back().get(); |
| 43 desktop_plane_->id = 0; | 53 desktop_plane_->id = 0; |
| 44 desktop_plane_->copy_rect = {0.0f, 0.0f, 1.0f, 1.0f}; | 54 desktop_plane_->copy_rect = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 45 // TODO(cjgrant): If we use the native path for content clicks, fix this. | 55 // TODO(cjgrant): If we use the native path for content clicks, fix this. |
| 46 desktop_plane_->window_rect = {0, 0, 0, 0}; | 56 desktop_plane_->window_rect = {0, 0, 0, 0}; |
| 47 desktop_plane_->translation = {0.0f, 0.0f, 0.0f}; | 57 desktop_plane_->translation = {0.0f, 0.0f, 0.0f}; |
| 48 desktop_plane_->x_anchoring = XNONE; | 58 desktop_plane_->x_anchoring = XNONE; |
| 49 desktop_plane_->y_anchoring = YNONE; | 59 desktop_plane_->y_anchoring = YNONE; |
| 50 desktop_plane_->anchor_z = false; | 60 desktop_plane_->anchor_z = false; |
| 51 desktop_plane_->orientation_axis_angle = {{1.0f, 0.0f, 0.0f, 0.0f}}; | 61 desktop_plane_->orientation_axis_angle = {{1.0f, 0.0f, 0.0f, 0.0f}}; |
| 52 desktop_plane_->rotation_axis_angle = {{0.0f, 0.0f, 0.0f, 0.0f}}; | 62 desktop_plane_->rotation_axis_angle = {{0.0f, 0.0f, 0.0f, 0.0f}}; |
| 63 content_core_->GetWebContents()->GetRenderWidgetHostView() |
| 64 ->GetRenderWidgetHost()->WasResized(); |
| 65 } |
| 66 |
| 67 void VrShell::UpdateCompositorLayers(JNIEnv* env, |
| 68 const JavaParamRef<jobject>& obj) { |
| 69 content_compositor_view_->SetLayer(content_core_); |
| 53 } | 70 } |
| 54 | 71 |
| 55 void VrShell::Destroy(JNIEnv* env, | 72 void VrShell::Destroy(JNIEnv* env, |
| 56 const base::android::JavaParamRef<jobject>& obj) { | 73 const base::android::JavaParamRef<jobject>& obj) { |
| 57 delete this; | 74 delete this; |
| 58 gl::init::ClearGLBindings(); | 75 gl::init::ClearGLBindings(); |
| 59 } | 76 } |
| 60 | 77 |
| 61 bool RegisterVrShell(JNIEnv* env) { | 78 bool RegisterVrShell(JNIEnv* env) { |
| 62 return RegisterNativesImpl(env); | 79 return RegisterNativesImpl(env); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // applying a neck model should not elevate the camera. | 130 // applying a neck model should not elevate the camera. |
| 114 offset[1] -= kNeckVerticalOffset; | 131 offset[1] -= kNeckVerticalOffset; |
| 115 | 132 |
| 116 // Right-multiply the inverse translation onto the | 133 // Right-multiply the inverse translation onto the |
| 117 // object_from_reference_matrix. | 134 // object_from_reference_matrix. |
| 118 TranslateMRight(mat_forward, mat_forward, -offset[0], -offset[1], -offset[2]); | 135 TranslateMRight(mat_forward, mat_forward, -offset[0], -offset[1], -offset[2]); |
| 119 } | 136 } |
| 120 | 137 |
| 121 void VrShell::DrawFrame(JNIEnv* env, | 138 void VrShell::DrawFrame(JNIEnv* env, |
| 122 const JavaParamRef<jobject>& obj) { | 139 const JavaParamRef<jobject>& obj) { |
| 123 float screen_width = kScreenWidthMeters * desktop_height_; | 140 float screen_width = kScreenWidthRatio * desktop_height_; |
| 124 float screen_height = kScreenHeightMeters * desktop_height_; | 141 float screen_height = kScreenHeightRatio * desktop_height_; |
| 125 | 142 |
| 126 float screen_tilt = desktop_screen_tilt_ * M_PI / 180.0f; | 143 float screen_tilt = desktop_screen_tilt_ * M_PI / 180.0f; |
| 127 | 144 |
| 128 buffer_viewport_list_->SetToRecommendedBufferViewports(); | 145 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
| 129 | 146 |
| 130 gvr::Frame frame = swap_chain_->AcquireFrame(); | 147 gvr::Frame frame = swap_chain_->AcquireFrame(); |
| 131 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 148 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 132 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 149 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 133 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | 150 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); |
| 134 | 151 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 234 } |
| 218 | 235 |
| 219 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 236 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 220 if (gvr_api_ == nullptr) | 237 if (gvr_api_ == nullptr) |
| 221 return; | 238 return; |
| 222 | 239 |
| 223 gvr_api_->RefreshViewerProfile(); | 240 gvr_api_->RefreshViewerProfile(); |
| 224 gvr_api_->ResumeTracking(); | 241 gvr_api_->ResumeTracking(); |
| 225 } | 242 } |
| 226 | 243 |
| 244 void VrShell::ContentSurfaceDestroyed(JNIEnv* env, |
| 245 const JavaParamRef<jobject>& object) { |
| 246 content_compositor_view_->SurfaceDestroyed(); |
| 247 } |
| 248 |
| 249 void VrShell::ContentSurfaceChanged(JNIEnv* env, |
| 250 const JavaParamRef<jobject>& object, |
| 251 jint width, |
| 252 jint height, |
| 253 const JavaParamRef<jobject>& surface) { |
| 254 content_compositor_view_->SurfaceChanged((int)width, (int)height, surface); |
| 255 } |
| 256 |
| 227 void VrShell::UpdateTransforms(float screen_width_meters, | 257 void VrShell::UpdateTransforms(float screen_width_meters, |
| 228 float screen_height_meters, | 258 float screen_height_meters, |
| 229 float screen_tilt) { | 259 float screen_tilt) { |
| 230 for (std::unique_ptr<ContentRectangle>& rect : ui_rects_) { | 260 for (std::unique_ptr<ContentRectangle>& rect : ui_rects_) { |
| 231 rect->transform_.MakeIdentity(); | 261 rect->transform_.MakeIdentity(); |
| 232 rect->transform_.Scale(rect->size.x, rect->size.y, rect->size.z); | 262 rect->transform_.Scale(rect->size.x, rect->size.y, rect->size.z); |
| 233 float x_anchor_translate; | 263 float x_anchor_translate; |
| 234 switch (rect->x_anchoring) { | 264 switch (rect->x_anchoring) { |
| 235 case XLEFT: | 265 case XLEFT: |
| 236 x_anchor_translate = desktop_position_.x - screen_width_meters * 0.5; | 266 x_anchor_translate = desktop_position_.x - screen_width_meters * 0.5; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 267 // TODO(cjgrant): Establish which exact rotations we'll provide. | 297 // TODO(cjgrant): Establish which exact rotations we'll provide. |
| 268 // Adjust for screen tilt. | 298 // Adjust for screen tilt. |
| 269 rect->transform_.Rotate(1.0f, 0.0f, 0.0f, screen_tilt); | 299 rect->transform_.Rotate(1.0f, 0.0f, 0.0f, screen_tilt); |
| 270 } | 300 } |
| 271 } | 301 } |
| 272 | 302 |
| 273 // ---------------------------------------------------------------------------- | 303 // ---------------------------------------------------------------------------- |
| 274 // Native JNI methods | 304 // Native JNI methods |
| 275 // ---------------------------------------------------------------------------- | 305 // ---------------------------------------------------------------------------- |
| 276 | 306 |
| 277 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 307 jlong Init(JNIEnv* env, |
| 278 return reinterpret_cast<intptr_t>(new VrShell(env, obj)); | 308 const JavaParamRef<jobject>& obj, |
| 309 const JavaParamRef<jobject>& content_web_contents, |
| 310 jlong content_window_android) { |
| 311 content::ContentViewCore* c_core = content::ContentViewCore::FromWebContents( |
| 312 content::WebContents::FromJavaWebContents(content_web_contents)); |
| 313 return reinterpret_cast<intptr_t>(new VrShell( |
| 314 env, obj, c_core, |
| 315 reinterpret_cast<ui::WindowAndroid*>(content_window_android))); |
| 279 } | 316 } |
| 280 | 317 |
| 281 } // namespace vr_shell | 318 } // namespace vr_shell |
| OLD | NEW |