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 "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 7 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
8 #include "chrome/browser/android/vr_shell/vr_util.h" | 8 #include "chrome/browser/android/vr_shell/vr_util.h" |
9 #include "jni/VrShell_jni.h" | 9 #include "jni/VrShell_jni.h" |
10 #include "ui/gl/gl_bindings.h" | 10 #include "ui/gl/gl_bindings.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 transfrom_to_world.m[3][2] = 0; | 49 transfrom_to_world.m[3][2] = 0; |
50 transfrom_to_world.m[3][3] = 1; | 50 transfrom_to_world.m[3][3] = 1; |
51 } | 51 } |
52 | 52 |
53 void ContentRect::Translate(float x, float y, float z) { | 53 void ContentRect::Translate(float x, float y, float z) { |
54 transfrom_to_world.m[0][3] += x; | 54 transfrom_to_world.m[0][3] += x; |
55 transfrom_to_world.m[1][3] += y; | 55 transfrom_to_world.m[1][3] += y; |
56 transfrom_to_world.m[2][3] += z; | 56 transfrom_to_world.m[2][3] += z; |
57 } | 57 } |
58 | 58 |
59 VrShell::VrShell(JNIEnv* env, jobject obj) { | 59 VrShell::VrShell(JNIEnv* env, jobject obj) : |
60 webvr_mode_(false) { | |
60 j_vr_shell_.Reset(env, obj); | 61 j_vr_shell_.Reset(env, obj); |
61 } | 62 } |
62 | 63 |
63 void VrShell::Destroy(JNIEnv* env, | 64 void VrShell::Destroy(JNIEnv* env, |
64 const base::android::JavaParamRef<jobject>& obj) { | 65 const base::android::JavaParamRef<jobject>& obj) { |
65 delete this; | 66 delete this; |
66 } | 67 } |
67 | 68 |
68 bool RegisterVrShell(JNIEnv* env) { | 69 bool RegisterVrShell(JNIEnv* env) { |
69 return RegisterNativesImpl(env); | 70 return RegisterNativesImpl(env); |
70 } | 71 } |
71 | 72 |
72 VrShell::~VrShell() {} | 73 VrShell::~VrShell() { |
74 device::GvrDelegateManager::GetInstance()->Shutdown(); | |
75 } | |
73 | 76 |
74 void VrShell::GvrInit(JNIEnv* env, | 77 void VrShell::GvrInit(JNIEnv* env, |
75 const base::android::JavaParamRef<jobject>& obj, | 78 const base::android::JavaParamRef<jobject>& obj, |
76 jlong native_gvr_api) { | 79 jlong native_gvr_api) { |
77 gvr_api_ = | 80 gvr_api_ = |
78 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); | 81 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); |
82 | |
83 device::GvrDelegateManager::GetInstance()->Initialize(this); | |
79 } | 84 } |
80 | 85 |
81 void VrShell::InitializeGl(JNIEnv* env, | 86 void VrShell::InitializeGl(JNIEnv* env, |
82 const base::android::JavaParamRef<jobject>& obj, | 87 const base::android::JavaParamRef<jobject>& obj, |
83 jint texture_data_handle) { | 88 jint texture_data_handle) { |
84 gl::init::InitializeGLOneOff(); | 89 gl::init::InitializeGLOneOff(); |
85 gvr_api_->InitializeGl(); | 90 gvr_api_->InitializeGl(); |
86 std::vector<gvr::BufferSpec> specs; | 91 std::vector<gvr::BufferSpec> specs; |
87 specs.push_back(gvr_api_->CreateBufferSpec()); | 92 specs.push_back(gvr_api_->CreateBufferSpec()); |
88 render_size_ = specs[0].GetSize(); | 93 render_size_ = specs[0].GetSize(); |
89 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); | 94 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); |
90 content_rect_.reset(new ContentRect()); | 95 content_rect_.reset(new ContentRect()); |
91 content_rect_->content_texture_handle = | 96 content_rect_->content_texture_handle = |
92 reinterpret_cast<int>(texture_data_handle); | 97 reinterpret_cast<int>(texture_data_handle); |
93 vr_shell_renderer_.reset(new VrShellRenderer()); | 98 vr_shell_renderer_.reset(new VrShellRenderer()); |
94 buffer_viewport_list_.reset( | 99 buffer_viewport_list_.reset( |
95 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); | 100 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); |
96 buffer_viewport_.reset( | 101 buffer_viewport_.reset( |
97 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); | 102 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); |
98 } | 103 } |
99 | 104 |
100 void VrShell::DrawFrame(JNIEnv* env, | 105 void VrShell::DrawFrame(JNIEnv* env, |
101 const base::android::JavaParamRef<jobject>& obj) { | 106 const base::android::JavaParamRef<jobject>& obj) { |
102 buffer_viewport_list_->SetToRecommendedBufferViewports(); | 107 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
103 gvr::Frame frame = swap_chain_->AcquireFrame(); | 108 gvr::Frame frame = swap_chain_->AcquireFrame(); |
104 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | |
105 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | |
106 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | |
107 | |
108 // Content area positioning. | |
109 content_rect_->SetIdentity(); | |
110 content_rect_->Translate(kContentRectPositionDefault.x, | |
111 kContentRectPositionDefault.y, | |
112 kContentRectPositionDefault.z); | |
113 | |
114 gvr::Mat4f left_eye_view_matrix = | |
115 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); | |
116 gvr::Mat4f right_eye_view_matrix = | |
117 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); | |
118 | 109 |
119 // Bind back to the default framebuffer. | 110 // Bind back to the default framebuffer. |
120 frame.BindBuffer(0); | 111 frame.BindBuffer(0); |
121 | 112 |
122 // Use culling to remove back faces. | 113 if (webvr_mode_) { |
123 glEnable(GL_CULL_FACE); | 114 // Don't need face culling, depth testing, blending, etc. Turn it all off. |
115 glDisable(GL_CULL_FACE); | |
116 glDepthMask(GL_FALSE); | |
117 glDisable(GL_DEPTH_TEST); | |
118 glDisable(GL_SCISSOR_TEST); | |
119 glDisable(GL_BLEND); | |
120 glDisable(GL_POLYGON_OFFSET_FILL); | |
124 | 121 |
125 // Enable depth testing. | 122 // Don't need to clear, since we're drawing over the entire render target. |
126 glEnable(GL_DEPTH_TEST); | |
127 glEnable(GL_SCISSOR_TEST); | |
128 | 123 |
129 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | 124 DrawWebVR(); |
125 } else { | |
126 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | |
mthiesse
2016/09/04 16:04:03
Can you move this branch out to a DrawVRShell() fu
| |
127 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | |
128 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | |
130 | 129 |
131 // Enable transparency. | 130 // Content area positioning. |
132 glEnable(GL_BLEND); | 131 content_rect_->SetIdentity(); |
133 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 132 content_rect_->Translate(kContentRectPositionDefault.x, |
133 kContentRectPositionDefault.y, | |
134 kContentRectPositionDefault.z); | |
134 | 135 |
135 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, | 136 gvr::Mat4f left_eye_view_matrix = |
136 buffer_viewport_.get()); | 137 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); |
137 DrawEye(left_eye_view_matrix, *buffer_viewport_); | 138 gvr::Mat4f right_eye_view_matrix = |
138 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | 139 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); |
139 buffer_viewport_.get()); | 140 |
140 DrawEye(right_eye_view_matrix, *buffer_viewport_); | 141 // Use culling to remove back faces. |
142 glEnable(GL_CULL_FACE); | |
143 | |
144 // Enable depth testing. | |
145 glEnable(GL_DEPTH_TEST); | |
146 glEnable(GL_SCISSOR_TEST); | |
147 | |
148 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); | |
149 | |
150 // Enable transparency. | |
151 glEnable(GL_BLEND); | |
152 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
153 | |
154 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, | |
155 buffer_viewport_.get()); | |
156 DrawEye(left_eye_view_matrix, *buffer_viewport_); | |
157 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | |
158 buffer_viewport_.get()); | |
159 DrawEye(right_eye_view_matrix, *buffer_viewport_); | |
160 } | |
141 | 161 |
142 frame.Unbind(); | 162 frame.Unbind(); |
143 frame.Submit(*buffer_viewport_list_, head_pose_); | 163 frame.Submit(*buffer_viewport_list_, head_pose_); |
144 } | 164 } |
145 | 165 |
146 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, | 166 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, |
147 const gvr::BufferViewport& params) { | 167 const gvr::BufferViewport& params) { |
148 gvr::Recti pixel_rect = | 168 gvr::Recti pixel_rect = |
149 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); | 169 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); |
150 glViewport(pixel_rect.left, pixel_rect.bottom, | 170 glViewport(pixel_rect.left, pixel_rect.bottom, |
(...skipping 14 matching lines...) Expand all Loading... | |
165 | 185 |
166 void VrShell::DrawContentRect() { | 186 void VrShell::DrawContentRect() { |
167 gvr::Mat4f content_rect_combined_matrix = | 187 gvr::Mat4f content_rect_combined_matrix = |
168 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); | 188 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); |
169 content_rect_combined_matrix = | 189 content_rect_combined_matrix = |
170 MatrixMul(projection_matrix_, content_rect_combined_matrix); | 190 MatrixMul(projection_matrix_, content_rect_combined_matrix); |
171 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | 191 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( |
172 content_rect_->content_texture_handle, content_rect_combined_matrix); | 192 content_rect_->content_texture_handle, content_rect_combined_matrix); |
173 } | 193 } |
174 | 194 |
195 void VrShell::DrawWebVR() { | |
196 glViewport(0, 0, render_size_.width, render_size_.height); | |
197 vr_shell_renderer_->GetWebVrRenderer()->Draw( | |
198 reinterpret_cast<int>(content_rect_->content_texture_handle)); | |
199 } | |
200 | |
175 void VrShell::OnPause(JNIEnv* env, | 201 void VrShell::OnPause(JNIEnv* env, |
176 const base::android::JavaParamRef<jobject>& obj) { | 202 const base::android::JavaParamRef<jobject>& obj) { |
177 if (gvr_api_ == nullptr) | 203 if (gvr_api_ == nullptr) |
178 return; | 204 return; |
179 gvr_api_->PauseTracking(); | 205 gvr_api_->PauseTracking(); |
180 } | 206 } |
181 | 207 |
182 void VrShell::OnResume(JNIEnv* env, | 208 void VrShell::OnResume(JNIEnv* env, |
183 const base::android::JavaParamRef<jobject>& obj) { | 209 const base::android::JavaParamRef<jobject>& obj) { |
184 if (gvr_api_ == nullptr) | 210 if (gvr_api_ == nullptr) |
185 return; | 211 return; |
186 gvr_api_->RefreshViewerProfile(); | 212 gvr_api_->RefreshViewerProfile(); |
187 gvr_api_->ResumeTracking(); | 213 gvr_api_->ResumeTracking(); |
188 } | 214 } |
189 | 215 |
216 void VrShell::RequestWebVRPresent() { | |
217 webvr_mode_ = true; | |
218 } | |
219 | |
220 void VrShell::ExitWebVRPresent() { | |
221 webvr_mode_ = false; | |
222 } | |
223 | |
224 void VrShell::SubmitWebVRFrame() { | |
225 } | |
226 | |
227 void VrShell::UpdateWebVRTextureBounds( | |
228 int eye, float left, float top, float width, float height) { | |
229 gvr::Rectf bounds = { left, top, width, height }; | |
230 vr_shell_renderer_->GetWebVrRenderer()->UpdateTextureBounds(eye, bounds); | |
231 } | |
232 | |
233 gvr::GvrApi* VrShell::gvr_api() { | |
234 return gvr_api_.get(); | |
235 } | |
236 | |
190 // ---------------------------------------------------------------------------- | 237 // ---------------------------------------------------------------------------- |
191 // Native JNI methods | 238 // Native JNI methods |
192 // ---------------------------------------------------------------------------- | 239 // ---------------------------------------------------------------------------- |
193 | 240 |
194 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { | 241 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { |
195 VrShell* vrShell = new VrShell(env, obj); | 242 VrShell* vrShell = new VrShell(env, obj); |
196 return reinterpret_cast<intptr_t>(vrShell); | 243 return reinterpret_cast<intptr_t>(vrShell); |
197 } | 244 } |
198 | 245 |
199 } // namespace vr_shell | 246 } // namespace vr_shell |
OLD | NEW |