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 // Java passes in a screen height of 1.0 with a width based on the configured | |
| 106 // aspect ratio. The overall size is handled here, scale to match the user's | |
| 107 // preferred overall screen height while maintaining the aspect ratio. | |
|
amp
2016/09/01 20:50:41
Could be just me, but I don't understand this comm
mthiesse
2016/09/02 01:05:33
More stale comments, sorry.
| |
| 108 float screen_width = kScreenWidthMeters * desktop_height_; | |
| 109 float screen_height = kScreenHeightMeters * desktop_height_; | |
| 110 | |
| 111 float screenTilt = desktop_screen_tilt_ * M_PI / 180.0f; | |
| 112 | |
| 102 buffer_viewport_list_->SetToRecommendedBufferViewports(); | 113 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
| 114 | |
| 103 gvr::Frame frame = swap_chain_->AcquireFrame(); | 115 gvr::Frame frame = swap_chain_->AcquireFrame(); |
| 104 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 116 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 105 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 117 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 106 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | 118 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); |
| 107 | 119 |
| 108 // Content area positioning. | 120 gvr::Vec3f headPos = getTranslation(head_pose_); |
| 109 content_rect_->SetIdentity(); | 121 if (headPos.x == 0.0f && headPos.y == 0.0f && headPos.z == 0.0f) { |
| 110 content_rect_->Translate(kContentRectPositionDefault.x, | 122 // This appears to be a 3DOF pose without a neck model. Add one. |
| 111 kContentRectPositionDefault.y, | 123 // The head pose has redundant data. Assume we're only using the |
| 112 kContentRectPositionDefault.z); | 124 // object_from_reference_matrix, we're not updating position_external. |
| 125 // TODO: Not sure what object_from_reference_matrix is. The new api removed | |
| 126 // it. For now, removing it seems working fine. | |
| 127 ApplyNeckModel(head_pose_); | |
| 128 } | |
| 113 | 129 |
| 130 desktop_plane_->size = {screen_width, screen_height, 1.0f}; | |
| 131 desktop_plane_->translation.x = desktop_position_.x; | |
| 132 desktop_plane_->translation.y = desktop_position_.y; | |
| 133 desktop_plane_->translation.z = desktop_position_.z; | |
| 134 | |
| 135 // Update position of all UI elements (including desktop) | |
| 136 UpdateTransforms(screen_width, screen_width, screenTilt); | |
|
amp
2016/09/01 20:50:40
Are the first two params both supposed to be width
mthiesse
2016/09/02 01:05:33
Good catch, that was a typo.
| |
| 137 | |
| 138 // Everything should be positioned now, ready for drawing. | |
| 114 gvr::Mat4f left_eye_view_matrix = | 139 gvr::Mat4f left_eye_view_matrix = |
| 115 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); | 140 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); |
| 116 gvr::Mat4f right_eye_view_matrix = | 141 gvr::Mat4f right_eye_view_matrix = |
| 117 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); | 142 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); |
| 118 | 143 |
| 119 // Bind back to the default framebuffer. | 144 // Bind back to the default framebuffer. |
| 120 frame.BindBuffer(0); | 145 frame.BindBuffer(0); |
| 121 | 146 |
| 122 // Use culling to remove back faces. | 147 // Use culling to remove back faces. |
| 123 glEnable(GL_CULL_FACE); | 148 glEnable(GL_CULL_FACE); |
| 124 | 149 |
| 125 // Enable depth testing. | 150 // Enable depth testing. |
| 126 glEnable(GL_DEPTH_TEST); | 151 glEnable(GL_DEPTH_TEST); |
| 127 glEnable(GL_SCISSOR_TEST); | 152 glEnable(GL_SCISSOR_TEST); |
| 128 | 153 |
| 129 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | 154 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); |
| 130 | 155 |
| 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, | 156 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, |
| 136 buffer_viewport_.get()); | 157 buffer_viewport_.get()); |
| 137 DrawEye(left_eye_view_matrix, *buffer_viewport_); | 158 DrawEye(left_eye_view_matrix, *buffer_viewport_); |
| 138 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | 159 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, |
| 139 buffer_viewport_.get()); | 160 buffer_viewport_.get()); |
| 140 DrawEye(right_eye_view_matrix, *buffer_viewport_); | 161 DrawEye(right_eye_view_matrix, *buffer_viewport_); |
| 141 | 162 |
| 142 frame.Unbind(); | 163 frame.Unbind(); |
| 143 frame.Submit(*buffer_viewport_list_, head_pose_); | 164 frame.Submit(*buffer_viewport_list_, head_pose_); |
| 144 } | 165 } |
| 145 | 166 |
| 146 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, | 167 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, |
| 147 const gvr::BufferViewport& params) { | 168 const gvr::BufferViewport& params) { |
| 148 gvr::Recti pixel_rect = | 169 gvr::Recti pixel_rect = |
| 149 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); | 170 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); |
| 150 glViewport(pixel_rect.left, pixel_rect.bottom, | 171 glViewport(pixel_rect.left, pixel_rect.bottom, |
| 151 pixel_rect.right - pixel_rect.left, | 172 pixel_rect.right - pixel_rect.left, |
| 152 pixel_rect.top - pixel_rect.bottom); | 173 pixel_rect.top - pixel_rect.bottom); |
| 153 glScissor(pixel_rect.left, pixel_rect.bottom, | 174 glScissor(pixel_rect.left, pixel_rect.bottom, |
| 154 pixel_rect.right - pixel_rect.left, | 175 pixel_rect.right - pixel_rect.left, |
| 155 pixel_rect.top - pixel_rect.bottom); | 176 pixel_rect.top - pixel_rect.bottom); |
| 156 | 177 |
| 157 view_matrix_ = view_matrix; | 178 view_matrix_ = view_matrix; |
| 158 | 179 |
| 159 projection_matrix_ = | 180 projection_matrix_ = |
| 160 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); | 181 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); |
| 161 | 182 |
| 162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 163 DrawContentRect(); | 184 DrawUI(); |
| 164 } | 185 } |
| 165 | 186 |
| 166 void VrShell::DrawContentRect() { | 187 void VrShell::DrawUI() { |
| 167 gvr::Mat4f content_rect_combined_matrix = | 188 for (std::size_t i = 0; i < ui_rects_.size(); ++i) { |
| 168 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); | 189 gvr::Mat4f combinedMatrix = |
| 169 content_rect_combined_matrix = | 190 MatrixMul(view_matrix_, ui_rects_[i].get()->mTransform.mToWorld); |
| 170 MatrixMul(projection_matrix_, content_rect_combined_matrix); | 191 combinedMatrix = MatrixMul(projection_matrix_, combinedMatrix); |
| 171 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | 192 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( |
| 172 content_rect_->content_texture_handle, content_rect_combined_matrix); | 193 ui_rects_[i].get()->contentTextureHandle, combinedMatrix, |
| 194 ui_rects_[i].get()->copyRect); | |
| 195 } | |
| 173 } | 196 } |
| 174 | 197 |
| 175 void VrShell::OnPause(JNIEnv* env, | 198 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 176 const base::android::JavaParamRef<jobject>& obj) { | |
| 177 if (gvr_api_ == nullptr) | 199 if (gvr_api_ == nullptr) |
| 178 return; | 200 return; |
| 179 gvr_api_->PauseTracking(); | 201 gvr_api_->PauseTracking(); |
| 180 } | 202 } |
| 181 | 203 |
| 182 void VrShell::OnResume(JNIEnv* env, | 204 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 183 const base::android::JavaParamRef<jobject>& obj) { | |
| 184 if (gvr_api_ == nullptr) | 205 if (gvr_api_ == nullptr) |
| 185 return; | 206 return; |
| 186 gvr_api_->RefreshViewerProfile(); | 207 // Refreshing the viewer profile accesses disk, so we do it off main thread |
| 208 // to avoid strict mode errors. | |
| 209 std::thread t([this] { gvr_api_->RefreshViewerProfile(); }); | |
| 210 t.join(); | |
| 187 gvr_api_->ResumeTracking(); | 211 gvr_api_->ResumeTracking(); |
| 188 } | 212 } |
| 189 | 213 |
| 214 void VrShell::UpdateTransforms(float screenWidthMeters, | |
| 215 float screenHeightMeters, | |
| 216 float screenTilt) { | |
| 217 // TODO(mthiesse): Thread safety. This is not thread safe. | |
| 218 for (std::unique_ptr<ContentRectangle>& rect : ui_rects_) { | |
| 219 rect->mTransform.SetIdentity(); | |
| 220 rect->mTransform.Scale(rect->size.x, rect->size.y, rect->size.z); | |
| 221 float xAnchorTranslate; | |
| 222 switch (rect->xAnchoring) { | |
| 223 case XLEFT: | |
| 224 xAnchorTranslate = desktop_position_.x - screenWidthMeters * 0.5; | |
| 225 break; | |
| 226 case XRIGHT: | |
| 227 xAnchorTranslate = desktop_position_.x + screenWidthMeters * 0.5; | |
| 228 break; | |
| 229 case XCENTER: | |
| 230 xAnchorTranslate = desktop_position_.x; | |
| 231 break; | |
| 232 case XNONE: | |
| 233 xAnchorTranslate = 0; | |
| 234 break; | |
| 235 } | |
| 236 float yAnchorTranslate; | |
| 237 switch (rect->yAnchoring) { | |
| 238 case YTOP: | |
| 239 yAnchorTranslate = desktop_position_.y + screenHeightMeters * 0.5; | |
| 240 break; | |
| 241 case YBOTTOM: | |
| 242 yAnchorTranslate = desktop_position_.y - screenHeightMeters * 0.5; | |
| 243 break; | |
| 244 case YCENTER: | |
| 245 yAnchorTranslate = desktop_position_.y; | |
| 246 break; | |
| 247 case YNONE: | |
| 248 yAnchorTranslate = 0; | |
| 249 break; | |
| 250 } | |
| 251 float zAnchorTranslate = rect->anchorZ ? desktop_position_.z : 0; | |
| 252 rect->mTransform.Translate(xAnchorTranslate + rect->translation.x, | |
| 253 yAnchorTranslate + rect->translation.y, | |
| 254 zAnchorTranslate + rect->translation.z); | |
| 255 // TODO(cjgrant): Establish which exact rotations we'll provide. | |
| 256 // Adjust for screen tilt. | |
| 257 rect->mTransform.Rotate(1.0f, 0.0f, 0.0f, screenTilt); | |
| 258 } | |
| 259 } | |
| 260 | |
| 190 // ---------------------------------------------------------------------------- | 261 // ---------------------------------------------------------------------------- |
| 191 // Native JNI methods | 262 // Native JNI methods |
| 192 // ---------------------------------------------------------------------------- | 263 // ---------------------------------------------------------------------------- |
| 193 | 264 |
| 194 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { | 265 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 195 VrShell* vrShell = new VrShell(env, obj); | 266 return reinterpret_cast<intptr_t>(new VrShell(env, obj)); |
| 196 return reinterpret_cast<intptr_t>(vrShell); | |
| 197 } | 267 } |
| 198 | 268 |
| 199 } // namespace vr_shell | 269 } // namespace vr_shell |
| OLD | NEW |