Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 9 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 8 #include "chrome/browser/android/vr_shell/vr_util.h" | 10 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 9 #include "jni/VrShell_jni.h" | 11 #include "jni/VrShell_jni.h" |
| 10 #include "ui/gl/gl_bindings.h" | 12 #include "ui/gl/gl_bindings.h" |
| 11 #include "ui/gl/init/gl_factory.h" | 13 #include "ui/gl/init/gl_factory.h" |
| 12 | 14 |
| 15 using base::android::JavaParamRef; | |
| 16 | |
| 13 namespace vr_shell { | 17 namespace vr_shell { |
| 14 | 18 |
| 15 namespace { | 19 constexpr gvr::Vec3f VrShell::kDesktopPositionDefault; |
| 16 // Constant taken from treasure_hunt demo. | |
| 17 const long kPredictionTimeWithoutVsyncNanos = 50000000; | |
| 18 | |
| 19 const float kZNear = 0.1f; | |
| 20 const float kZFar = 1000.0f; | |
| 21 | |
| 22 // Content rect in world coordinates. Height and width are currently supplied | |
| 23 // as DrawFrame arguments. | |
| 24 const gvr::Vec3f kContentRectPositionDefault = {0.0f, 0.0f, -1.0f}; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 ContentRect::ContentRect() { | |
| 29 SetIdentity(); | |
| 30 } | |
| 31 | |
| 32 ContentRect::~ContentRect() {} | |
| 33 | |
| 34 void ContentRect::SetIdentity() { | |
| 35 transfrom_to_world.m[0][0] = 1; | |
| 36 transfrom_to_world.m[0][1] = 0; | |
| 37 transfrom_to_world.m[0][2] = 0; | |
| 38 transfrom_to_world.m[0][3] = 0; | |
| 39 transfrom_to_world.m[1][0] = 0; | |
| 40 transfrom_to_world.m[1][1] = 1; | |
| 41 transfrom_to_world.m[1][2] = 0; | |
| 42 transfrom_to_world.m[1][3] = 0; | |
| 43 transfrom_to_world.m[2][0] = 0; | |
| 44 transfrom_to_world.m[2][1] = 0; | |
| 45 transfrom_to_world.m[2][2] = 1; | |
| 46 transfrom_to_world.m[2][3] = 0; | |
| 47 transfrom_to_world.m[3][0] = 0; | |
| 48 transfrom_to_world.m[3][1] = 0; | |
| 49 transfrom_to_world.m[3][2] = 0; | |
| 50 transfrom_to_world.m[3][3] = 1; | |
| 51 } | |
| 52 | |
| 53 void ContentRect::Translate(float x, float y, float z) { | |
| 54 transfrom_to_world.m[0][3] += x; | |
| 55 transfrom_to_world.m[1][3] += y; | |
| 56 transfrom_to_world.m[2][3] += z; | |
| 57 } | |
| 58 | 20 |
| 59 VrShell::VrShell(JNIEnv* env, jobject obj) { | 21 VrShell::VrShell(JNIEnv* env, jobject obj) { |
| 60 j_vr_shell_.Reset(env, obj); | 22 j_vr_shell_.Reset(env, obj); |
| 23 ui_rects_.emplace_back(new ContentRectangle()); | |
| 24 desktop_plane_ = ui_rects_.back().get(); | |
| 25 desktop_plane_->id = 0; | |
| 26 desktop_plane_->copyRect = {0.0f, 0.0f, 1.0f, 1.0f}; | |
| 27 // TODO(cjgrant): If we use the native path for content clicks, fix this. | |
| 28 desktop_plane_->windowRect = {0, 0, 0, 0}; | |
| 29 desktop_plane_->translation = {0.0f, 0.0f, 0.0f}; | |
| 30 desktop_plane_->xAnchoring = XNONE; | |
| 31 desktop_plane_->yAnchoring = YNONE; | |
| 32 desktop_plane_->anchorZ = false; | |
| 33 desktop_plane_->orientationAxisAngle = {{1.0f, 0.0f, 0.0f, 0.0f}}; | |
| 34 desktop_plane_->rotationAxisAngle = {{0.0f, 0.0f, 0.0f, 0.0f}}; | |
| 61 } | 35 } |
| 62 | 36 |
| 63 void VrShell::Destroy(JNIEnv* env, | 37 void VrShell::Destroy(JNIEnv* env, |
| 64 const base::android::JavaParamRef<jobject>& obj) { | 38 const base::android::JavaParamRef<jobject>& obj) { |
| 65 delete this; | 39 delete this; |
| 40 gl::init::ClearGLBindings(); | |
| 66 } | 41 } |
| 67 | 42 |
| 68 bool RegisterVrShell(JNIEnv* env) { | 43 bool RegisterVrShell(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); | 44 return RegisterNativesImpl(env); |
| 70 } | 45 } |
| 71 | 46 |
| 72 VrShell::~VrShell() {} | 47 VrShell::~VrShell() {} |
| 73 | 48 |
| 74 void VrShell::GvrInit(JNIEnv* env, | 49 void VrShell::GvrInit(JNIEnv* env, |
| 75 const base::android::JavaParamRef<jobject>& obj, | 50 const JavaParamRef<jobject>& obj, |
| 76 jlong native_gvr_api) { | 51 jlong native_gvr_api) { |
| 77 gvr_api_ = | 52 gvr_api_ = |
| 78 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); | 53 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); |
| 79 } | 54 } |
| 80 | 55 |
| 81 void VrShell::InitializeGl(JNIEnv* env, | 56 void VrShell::InitializeGl(JNIEnv* env, |
| 82 const base::android::JavaParamRef<jobject>& obj, | 57 const JavaParamRef<jobject>& obj, |
| 83 jint texture_data_handle) { | 58 jint textureDataHandle) { |
| 84 gl::init::InitializeGLOneOff(); | 59 CHECK(gl::GetGLImplementation() != gl::kGLImplementationNone || |
| 60 gl::init::InitializeGLOneOff()); | |
| 61 | |
| 62 content_texture_id_ = textureDataHandle; | |
| 85 gvr_api_->InitializeGl(); | 63 gvr_api_->InitializeGl(); |
| 86 std::vector<gvr::BufferSpec> specs; | 64 std::vector<gvr::BufferSpec> specs; |
| 87 specs.push_back(gvr_api_->CreateBufferSpec()); | 65 specs.push_back(gvr_api_->CreateBufferSpec()); |
| 88 render_size_ = specs[0].GetSize(); | 66 render_size_ = specs[0].GetSize(); |
| 89 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); | 67 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); |
| 90 content_rect_.reset(new ContentRect()); | 68 |
| 91 content_rect_->content_texture_handle = | 69 desktop_plane_->contentTextureHandle = content_texture_id_; |
| 92 reinterpret_cast<int>(texture_data_handle); | 70 |
| 93 vr_shell_renderer_.reset(new VrShellRenderer()); | 71 vr_shell_renderer_.reset(new VrShellRenderer()); |
| 94 buffer_viewport_list_.reset( | 72 buffer_viewport_list_.reset( |
| 95 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); | 73 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); |
| 96 buffer_viewport_.reset( | 74 buffer_viewport_.reset( |
| 97 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); | 75 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); |
| 98 } | 76 } |
| 99 | 77 |
| 78 void ApplyNeckModel(gvr::Mat4f& mat_forward) { | |
| 79 // This assumes that the input matrix is a pure rotation matrix. The | |
| 80 // input object_from_reference matrix has the inverse rotation of | |
| 81 // the head rotation. Invert it (this is just a transpose). | |
| 82 gvr::Mat4f mat = MatrixTranspose(mat_forward); | |
| 83 | |
| 84 // Position of the point between the eyes, relative to the neck pivot: | |
| 85 const float kNeckHorizontalOffset = -0.080f; // meters in Z | |
| 86 const float kNeckVerticalOffset = 0.075f; // meters in Y | |
| 87 | |
| 88 std::array<float, 4> neckOffset = { | |
| 89 {0.0f, kNeckVerticalOffset, kNeckHorizontalOffset, 1.0f}}; | |
| 90 | |
| 91 // Rotate eyes around neck pivot point. | |
| 92 auto offset = MatrixVectorMul(mat, neckOffset); | |
| 93 | |
| 94 // Measure new position relative to original center of head, because | |
| 95 // applying a neck model should not elevate the camera. | |
| 96 offset[1] -= kNeckVerticalOffset; | |
| 97 | |
| 98 // Right-multiply the inverse translation onto the | |
| 99 // object_from_reference_matrix. | |
| 100 translateMRight(mat_forward, mat_forward, -offset[0], -offset[1], -offset[2]); | |
| 101 } | |
| 102 | |
| 100 void VrShell::DrawFrame(JNIEnv* env, | 103 void VrShell::DrawFrame(JNIEnv* env, |
| 101 const base::android::JavaParamRef<jobject>& obj) { | 104 const JavaParamRef<jobject>& obj) { |
| 105 float screen_width = kScreenWidthMeters * desktop_height_; | |
| 106 float screen_height = kScreenHeightMeters * desktop_height_; | |
| 107 | |
| 108 float screenTilt = desktop_screen_tilt_ * M_PI / 180.0f; | |
| 109 | |
| 102 buffer_viewport_list_->SetToRecommendedBufferViewports(); | 110 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
| 111 | |
| 103 gvr::Frame frame = swap_chain_->AcquireFrame(); | 112 gvr::Frame frame = swap_chain_->AcquireFrame(); |
| 104 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 113 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 105 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 114 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 106 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | 115 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); |
| 107 | 116 |
| 108 // Content area positioning. | 117 gvr::Vec3f headPos = getTranslation(head_pose_); |
| 109 content_rect_->SetIdentity(); | 118 if (headPos.x == 0.0f && headPos.y == 0.0f && headPos.z == 0.0f) { |
| 110 content_rect_->Translate(kContentRectPositionDefault.x, | 119 // This appears to be a 3DOF pose without a neck model. Add one. |
| 111 kContentRectPositionDefault.y, | 120 // The head pose has redundant data. Assume we're only using the |
| 112 kContentRectPositionDefault.z); | 121 // object_from_reference_matrix, we're not updating position_external. |
| 122 // TODO: Not sure what object_from_reference_matrix is. The new api removed | |
| 123 // it. For now, removing it seems working fine. | |
| 124 ApplyNeckModel(head_pose_); | |
| 125 } | |
| 113 | 126 |
| 127 desktop_plane_->size = {screen_width, screen_height, 1.0f}; | |
| 128 desktop_plane_->translation.x = desktop_position_.x; | |
| 129 desktop_plane_->translation.y = desktop_position_.y; | |
| 130 desktop_plane_->translation.z = desktop_position_.z; | |
| 131 | |
| 132 // Update position of all UI elements (including desktop) | |
| 133 UpdateTransforms(screen_width, screen_height, screenTilt); | |
| 134 | |
| 135 // Everything should be positioned now, ready for drawing. | |
| 114 gvr::Mat4f left_eye_view_matrix = | 136 gvr::Mat4f left_eye_view_matrix = |
| 115 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); | 137 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); |
| 116 gvr::Mat4f right_eye_view_matrix = | 138 gvr::Mat4f right_eye_view_matrix = |
| 117 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); | 139 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); |
| 118 | 140 |
| 119 // Bind back to the default framebuffer. | 141 // Bind back to the default framebuffer. |
| 120 frame.BindBuffer(0); | 142 frame.BindBuffer(0); |
| 121 | 143 |
| 122 // Use culling to remove back faces. | 144 // Use culling to remove back faces. |
| 123 glEnable(GL_CULL_FACE); | 145 glEnable(GL_CULL_FACE); |
| 124 | 146 |
| 125 // Enable depth testing. | 147 // Enable depth testing. |
| 126 glEnable(GL_DEPTH_TEST); | 148 glEnable(GL_DEPTH_TEST); |
| 127 glEnable(GL_SCISSOR_TEST); | 149 glEnable(GL_SCISSOR_TEST); |
| 128 | 150 |
| 129 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | 151 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); |
| 130 | 152 |
| 131 // Enable transparency. | |
| 132 glEnable(GL_BLEND); | |
| 133 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 134 | |
| 135 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, | 153 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, |
| 136 buffer_viewport_.get()); | 154 buffer_viewport_.get()); |
| 137 DrawEye(left_eye_view_matrix, *buffer_viewport_); | 155 DrawEye(left_eye_view_matrix, *buffer_viewport_); |
| 138 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | 156 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, |
| 139 buffer_viewport_.get()); | 157 buffer_viewport_.get()); |
| 140 DrawEye(right_eye_view_matrix, *buffer_viewport_); | 158 DrawEye(right_eye_view_matrix, *buffer_viewport_); |
| 141 | 159 |
| 142 frame.Unbind(); | 160 frame.Unbind(); |
| 143 frame.Submit(*buffer_viewport_list_, head_pose_); | 161 frame.Submit(*buffer_viewport_list_, head_pose_); |
| 144 } | 162 } |
| 145 | 163 |
| 146 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, | 164 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, |
| 147 const gvr::BufferViewport& params) { | 165 const gvr::BufferViewport& params) { |
| 148 gvr::Recti pixel_rect = | 166 gvr::Recti pixel_rect = |
| 149 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); | 167 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); |
| 150 glViewport(pixel_rect.left, pixel_rect.bottom, | 168 glViewport(pixel_rect.left, pixel_rect.bottom, |
| 151 pixel_rect.right - pixel_rect.left, | 169 pixel_rect.right - pixel_rect.left, |
| 152 pixel_rect.top - pixel_rect.bottom); | 170 pixel_rect.top - pixel_rect.bottom); |
| 153 glScissor(pixel_rect.left, pixel_rect.bottom, | 171 glScissor(pixel_rect.left, pixel_rect.bottom, |
| 154 pixel_rect.right - pixel_rect.left, | 172 pixel_rect.right - pixel_rect.left, |
| 155 pixel_rect.top - pixel_rect.bottom); | 173 pixel_rect.top - pixel_rect.bottom); |
| 156 | 174 |
| 157 view_matrix_ = view_matrix; | 175 view_matrix_ = view_matrix; |
| 158 | 176 |
| 159 projection_matrix_ = | 177 projection_matrix_ = |
| 160 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); | 178 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); |
| 161 | 179 |
| 162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 180 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 163 DrawContentRect(); | 181 DrawUI(); |
| 164 } | 182 } |
| 165 | 183 |
| 166 void VrShell::DrawContentRect() { | 184 void VrShell::DrawUI() { |
| 167 gvr::Mat4f content_rect_combined_matrix = | 185 for (std::size_t i = 0; i < ui_rects_.size(); ++i) { |
| 168 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); | 186 gvr::Mat4f combinedMatrix = |
| 169 content_rect_combined_matrix = | 187 MatrixMul(view_matrix_, ui_rects_[i].get()->mTransform.mToWorld); |
| 170 MatrixMul(projection_matrix_, content_rect_combined_matrix); | 188 combinedMatrix = MatrixMul(projection_matrix_, combinedMatrix); |
| 171 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | 189 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( |
| 172 content_rect_->content_texture_handle, content_rect_combined_matrix); | 190 ui_rects_[i].get()->contentTextureHandle, combinedMatrix, |
| 191 ui_rects_[i].get()->copyRect); | |
| 192 } | |
| 173 } | 193 } |
| 174 | 194 |
| 175 void VrShell::OnPause(JNIEnv* env, | 195 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 176 const base::android::JavaParamRef<jobject>& obj) { | |
| 177 if (gvr_api_ == nullptr) | 196 if (gvr_api_ == nullptr) |
| 178 return; | 197 return; |
| 179 gvr_api_->PauseTracking(); | 198 gvr_api_->PauseTracking(); |
| 180 } | 199 } |
| 181 | 200 |
| 182 void VrShell::OnResume(JNIEnv* env, | 201 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 183 const base::android::JavaParamRef<jobject>& obj) { | |
| 184 if (gvr_api_ == nullptr) | 202 if (gvr_api_ == nullptr) |
| 185 return; | 203 return; |
| 186 gvr_api_->RefreshViewerProfile(); | 204 // Refreshing the viewer profile accesses disk, so we do it off main thread |
| 205 // to avoid strict mode errors. | |
| 206 std::thread t([this] { gvr_api_->RefreshViewerProfile(); }); | |
|
David Trainor- moved to gerrit
2016/09/08 05:49:30
Do we use std::thread anywhere else in content/ or
mthiesse
2016/09/08 17:44:41
I've just added a strict mode exception for this (
| |
| 207 t.join(); | |
| 187 gvr_api_->ResumeTracking(); | 208 gvr_api_->ResumeTracking(); |
| 188 } | 209 } |
| 189 | 210 |
| 211 void VrShell::UpdateTransforms(float screenWidthMeters, | |
| 212 float screenHeightMeters, | |
| 213 float screenTilt) { | |
| 214 // TODO(mthiesse): Thread safety. This is not thread safe. | |
| 215 for (std::unique_ptr<ContentRectangle>& rect : ui_rects_) { | |
| 216 rect->mTransform.SetIdentity(); | |
| 217 rect->mTransform.Scale(rect->size.x, rect->size.y, rect->size.z); | |
| 218 float xAnchorTranslate; | |
| 219 switch (rect->xAnchoring) { | |
| 220 case XLEFT: | |
| 221 xAnchorTranslate = desktop_position_.x - screenWidthMeters * 0.5; | |
| 222 break; | |
| 223 case XRIGHT: | |
| 224 xAnchorTranslate = desktop_position_.x + screenWidthMeters * 0.5; | |
| 225 break; | |
| 226 case XCENTER: | |
| 227 xAnchorTranslate = desktop_position_.x; | |
| 228 break; | |
| 229 case XNONE: | |
| 230 xAnchorTranslate = 0; | |
| 231 break; | |
| 232 } | |
| 233 float yAnchorTranslate; | |
| 234 switch (rect->yAnchoring) { | |
| 235 case YTOP: | |
| 236 yAnchorTranslate = desktop_position_.y + screenHeightMeters * 0.5; | |
| 237 break; | |
| 238 case YBOTTOM: | |
| 239 yAnchorTranslate = desktop_position_.y - screenHeightMeters * 0.5; | |
| 240 break; | |
| 241 case YCENTER: | |
| 242 yAnchorTranslate = desktop_position_.y; | |
| 243 break; | |
| 244 case YNONE: | |
| 245 yAnchorTranslate = 0; | |
| 246 break; | |
| 247 } | |
| 248 float zAnchorTranslate = rect->anchorZ ? desktop_position_.z : 0; | |
| 249 rect->mTransform.Translate(xAnchorTranslate + rect->translation.x, | |
| 250 yAnchorTranslate + rect->translation.y, | |
| 251 zAnchorTranslate + rect->translation.z); | |
| 252 // TODO(cjgrant): Establish which exact rotations we'll provide. | |
| 253 // Adjust for screen tilt. | |
| 254 rect->mTransform.Rotate(1.0f, 0.0f, 0.0f, screenTilt); | |
| 255 } | |
| 256 } | |
| 257 | |
| 190 // ---------------------------------------------------------------------------- | 258 // ---------------------------------------------------------------------------- |
| 191 // Native JNI methods | 259 // Native JNI methods |
| 192 // ---------------------------------------------------------------------------- | 260 // ---------------------------------------------------------------------------- |
| 193 | 261 |
| 194 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { | 262 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 195 VrShell* vrShell = new VrShell(env, obj); | 263 return reinterpret_cast<intptr_t>(new VrShell(env, obj)); |
| 196 return reinterpret_cast<intptr_t>(vrShell); | |
| 197 } | 264 } |
| 198 | 265 |
| 199 } // namespace vr_shell | 266 } // namespace vr_shell |
| OLD | NEW |