| 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" |
| 11 #include "ui/gl/init/gl_factory.h" | 11 #include "ui/gl/init/gl_factory.h" |
| 12 | 12 |
| 13 #include "base/android/build_info.h" |
| 14 #include "base/android/jni_android.h" |
| 15 #include "base/bind.h" |
| 16 #include "base/command_line.h" |
| 17 #include "base/id_map.h" |
| 18 #include "base/rand_util.h" |
| 19 #include "base/trace_event/trace_event.h" |
| 20 #include "cc/layers/layer.h" |
| 21 #include "cc/layers/layer_collections.h" |
| 22 #include "cc/layers/solid_color_layer.h" |
| 23 #include "cc/layers/texture_layer.h" |
| 24 #include "chrome/browser/android/compositor/layer/toolbar_layer.h" |
| 25 #include "chrome/browser/android/compositor/layer_title_cache.h" |
| 26 #include "chrome/browser/android/compositor/scene_layer/scene_layer.h" |
| 27 #include "content/public/browser/android/compositor.h" |
| 28 #include "content/public/browser/android/content_view_core.h" |
| 29 #include "content/public/browser/child_process_data.h" |
| 30 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/common/process_type.h" |
| 32 #include "third_party/skia/include/core/SkBitmap.h" |
| 33 #include "ui/android/resources/resource_manager.h" |
| 34 #include "ui/android/resources/ui_resource_provider.h" |
| 35 #include "ui/android/window_android.h" |
| 36 |
| 37 #include "ui/gfx/android/java_bitmap.h" |
| 38 |
| 39 #include "ui/android/delegated_frame_host_android.h" |
| 40 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
| 41 #include "cc/layers/surface_layer.h" |
| 42 |
| 43 #include "content/public/browser/browser_thread.h" |
| 44 #include "content/public/browser/render_widget_host.h" |
| 45 |
| 13 namespace vr_shell { | 46 namespace vr_shell { |
| 14 | 47 |
| 15 namespace { | 48 namespace { |
| 16 // Constant taken from treasure_hunt demo. | 49 // Constant taken from treasure_hunt demo. |
| 17 const long kPredictionTimeWithoutVsyncNanos = 50000000; | 50 const long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 18 | 51 |
| 19 const float kZNear = 0.1f; | 52 const float kZNear = 0.1f; |
| 20 const float kZFar = 1000.0f; | 53 const float kZFar = 1000.0f; |
| 21 | 54 |
| 22 // Content rect in world coordinates. Height and width are currently supplied | 55 // Content rect in world coordinates. Height and width are currently supplied |
| (...skipping 26 matching lines...) Expand all Loading... |
| 49 transfrom_to_world.m[3][2] = 0; | 82 transfrom_to_world.m[3][2] = 0; |
| 50 transfrom_to_world.m[3][3] = 1; | 83 transfrom_to_world.m[3][3] = 1; |
| 51 } | 84 } |
| 52 | 85 |
| 53 void ContentRect::Translate(float x, float y, float z) { | 86 void ContentRect::Translate(float x, float y, float z) { |
| 54 transfrom_to_world.m[0][3] += x; | 87 transfrom_to_world.m[0][3] += x; |
| 55 transfrom_to_world.m[1][3] += y; | 88 transfrom_to_world.m[1][3] += y; |
| 56 transfrom_to_world.m[2][3] += z; | 89 transfrom_to_world.m[2][3] += z; |
| 57 } | 90 } |
| 58 | 91 |
| 59 VrShell::VrShell(JNIEnv* env, jobject obj) { | 92 VrShell::VrShell(JNIEnv* env, |
| 93 jobject obj, |
| 94 jboolean low_mem_device, |
| 95 ui::WindowAndroid* window_android) |
| 96 : root_layer_(cc::SolidColorLayer::Create()), |
| 97 current_surface_format_(0), |
| 98 content_width_(0), |
| 99 content_height_(0), |
| 100 weak_factory_(this) { |
| 101 content::BrowserChildProcessObserver::Add(this); |
| 60 j_vr_shell_.Reset(env, obj); | 102 j_vr_shell_.Reset(env, obj); |
| 103 compositor_.reset(content::Compositor::Create(this, window_android)); |
| 104 compositor_->setDeviceScaleFactor(1.0f); |
| 105 |
| 106 root_layer_->SetIsDrawable(true); |
| 107 root_layer_->SetBackgroundColor(SK_ColorRED); |
| 61 } | 108 } |
| 62 | 109 |
| 63 void VrShell::Destroy(JNIEnv* env, | 110 void VrShell::Destroy(JNIEnv* env, |
| 64 const base::android::JavaParamRef<jobject>& obj) { | 111 const base::android::JavaParamRef<jobject>& obj) { |
| 65 delete this; | 112 delete this; |
| 66 } | 113 } |
| 67 | 114 |
| 68 bool RegisterVrShell(JNIEnv* env) { | 115 bool RegisterVrShell(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); | 116 return RegisterNativesImpl(env); |
| 70 } | 117 } |
| 71 | 118 |
| 72 VrShell::~VrShell() {} | 119 VrShell::~VrShell() { |
| 120 content::BrowserChildProcessObserver::Remove(this); |
| 121 |
| 122 // Explicitly reset these scoped_ptrs here because otherwise we callbacks will |
| 123 // try to access member variables during destruction. |
| 124 compositor_.reset(NULL); |
| 125 } |
| 73 | 126 |
| 74 void VrShell::GvrInit(JNIEnv* env, | 127 void VrShell::GvrInit(JNIEnv* env, |
| 75 const base::android::JavaParamRef<jobject>& obj, | 128 const base::android::JavaParamRef<jobject>& obj, |
| 76 jlong native_gvr_api) { | 129 jlong native_gvr_api) { |
| 77 gvr_api_ = | 130 gvr_api_ = |
| 78 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); | 131 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); |
| 79 } | 132 } |
| 80 | 133 |
| 81 void VrShell::InitializeGl(JNIEnv* env, | 134 void VrShell::InitializeGl(JNIEnv* env, |
| 82 const base::android::JavaParamRef<jobject>& obj, | 135 const base::android::JavaParamRef<jobject>& obj, |
| 83 jint texture_data_handle) { | 136 jint texture_data_handle) { |
| 84 gl::init::InitializeGLOneOff(); | 137 gl::init::InitializeGLOneOff(); |
| 85 gvr_api_->InitializeGl(); | 138 gvr_api_->InitializeGl(); |
| 86 std::vector<gvr::BufferSpec> specs; | 139 std::vector<gvr::BufferSpec> specs; |
| 87 specs.push_back(gvr_api_->CreateBufferSpec()); | 140 specs.push_back(gvr_api_->CreateBufferSpec()); |
| 88 render_size_ = specs[0].GetSize(); | 141 render_size_ = specs[0].GetSize(); |
| 89 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); | 142 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); |
| 90 content_rect_.reset(new ContentRect()); | 143 content_rect_.reset(new ContentRect()); |
| 91 content_rect_->content_texture_handle = | 144 content_rect_->content_texture_handle = |
| 92 reinterpret_cast<int>(texture_data_handle); | 145 reinterpret_cast<int>(texture_data_handle); |
| 93 vr_shell_renderer_.reset(new VrShellRenderer()); | 146 vr_shell_renderer_.reset(new VrShellRenderer()); |
| 94 buffer_viewport_list_.reset( | 147 buffer_viewport_list_.reset( |
| 95 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); | 148 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); |
| 96 buffer_viewport_.reset( | 149 buffer_viewport_.reset( |
| 97 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); | 150 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); |
| 98 } | 151 } |
| 99 | 152 |
| 153 void VrShell::AddContentLayer() { |
| 154 LOG(ERROR) << "bshe add content layer"; |
| 155 // root_layer_->AddChild(rwhva->delegated_frame_host()->CopySurfaceLayer()); |
| 156 } |
| 157 |
| 100 void VrShell::DrawFrame(JNIEnv* env, | 158 void VrShell::DrawFrame(JNIEnv* env, |
| 101 const base::android::JavaParamRef<jobject>& obj) { | 159 const base::android::JavaParamRef<jobject>& obj) { |
| 102 buffer_viewport_list_->SetToRecommendedBufferViewports(); | 160 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
| 103 gvr::Frame frame = swap_chain_->AcquireFrame(); | 161 gvr::Frame frame = swap_chain_->AcquireFrame(); |
| 104 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 162 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 105 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 163 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 106 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); | 164 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); |
| 107 | 165 |
| 166 // Quick hack to get valid SurfaceLayer. |
| 167 // if (root_layer_->children().size() == 0 && |
| 168 // rwhva && |
| 169 // rwhva->HasValidFrame()) { |
| 170 // content::BrowserThread::PostTask( |
| 171 // content::BrowserThread::UI, |
| 172 // FROM_HERE, |
| 173 // base::Bind(&VrShell::AddContentLayer, base::Unretained(this))); |
| 174 // } |
| 175 // |
| 108 // Content area positioning. | 176 // Content area positioning. |
| 109 content_rect_->SetIdentity(); | 177 content_rect_->SetIdentity(); |
| 110 content_rect_->Translate(kContentRectPositionDefault.x, | 178 content_rect_->Translate(kContentRectPositionDefault.x, |
| 111 kContentRectPositionDefault.y, | 179 kContentRectPositionDefault.y, |
| 112 kContentRectPositionDefault.z); | 180 kContentRectPositionDefault.z); |
| 113 | 181 |
| 114 gvr::Mat4f left_eye_view_matrix = | 182 gvr::Mat4f left_eye_view_matrix = |
| 115 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); | 183 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); |
| 116 gvr::Mat4f right_eye_view_matrix = | 184 gvr::Mat4f right_eye_view_matrix = |
| 117 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); | 185 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 buffer_viewport_.get()); | 204 buffer_viewport_.get()); |
| 137 DrawEye(left_eye_view_matrix, *buffer_viewport_); | 205 DrawEye(left_eye_view_matrix, *buffer_viewport_); |
| 138 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | 206 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, |
| 139 buffer_viewport_.get()); | 207 buffer_viewport_.get()); |
| 140 DrawEye(right_eye_view_matrix, *buffer_viewport_); | 208 DrawEye(right_eye_view_matrix, *buffer_viewport_); |
| 141 | 209 |
| 142 frame.Unbind(); | 210 frame.Unbind(); |
| 143 frame.Submit(*buffer_viewport_list_, head_pose_); | 211 frame.Submit(*buffer_viewport_list_, head_pose_); |
| 144 } | 212 } |
| 145 | 213 |
| 214 void VrShell::UpdateLayerTreeHost() {} |
| 215 |
| 216 void VrShell::OnSwapBuffersCompleted(int pending_swap_buffers) {} |
| 217 |
| 218 void VrShell::OnWebContentsReady( |
| 219 JNIEnv* env, |
| 220 const base::android::JavaParamRef<jobject>& object, |
| 221 const base::android::JavaParamRef<jobject>& jweb_contents) { |
| 222 content::WebContents* content = |
| 223 content::WebContents::FromJavaWebContents(jweb_contents); |
| 224 LOG(ERROR) << "bshe " |
| 225 << "====on Web Contents loading===" << content->IsLoading(); |
| 226 ui::ViewAndroid* view_android = content->GetNativeView(); |
| 227 view_android->GetLayer()->SetBackgroundColor(SK_ColorTRANSPARENT); |
| 228 view_android->GetLayer()->RemoveFromParent(); |
| 229 view_android->GetLayer()->SetOpacity(1.0); |
| 230 view_android->GetLayer()->SetIsDrawable(true); |
| 231 view_android->GetLayer()->SetHideLayerAndSubtree(false); |
| 232 LOG(ERROR) << "bshe " |
| 233 << "====width===" << content_width_; |
| 234 view_android->GetLayer()->SetBounds( |
| 235 gfx::Size(content_width_, content_height_)); |
| 236 content->GetRenderWidgetHostView()->Show(); |
| 237 root_layer_->AddChild(view_android->GetLayer()); |
| 238 content::RenderWidgetHost* rwh2 = |
| 239 content->GetRenderWidgetHostView()->GetRenderWidgetHost(); |
| 240 rwh2->WasResized(); |
| 241 compositor_->SetNeedsComposite(); |
| 242 } |
| 243 |
| 244 void VrShell::SurfaceCreated( |
| 245 JNIEnv* env, |
| 246 const base::android::JavaParamRef<jobject>& object) { |
| 247 compositor_->SetRootLayer(root_layer_); |
| 248 current_surface_format_ = 0; |
| 249 } |
| 250 |
| 251 void VrShell::SurfaceDestroyed( |
| 252 JNIEnv* env, |
| 253 const base::android::JavaParamRef<jobject>& object) { |
| 254 compositor_->SetSurface(NULL); |
| 255 current_surface_format_ = 0; |
| 256 } |
| 257 |
| 258 void VrShell::SurfaceChanged( |
| 259 JNIEnv* env, |
| 260 const base::android::JavaParamRef<jobject>& object, |
| 261 jint format, |
| 262 jint width, |
| 263 jint height, |
| 264 const base::android::JavaParamRef<jobject>& surface) { |
| 265 DCHECK(surface); |
| 266 if (current_surface_format_ != format) { |
| 267 current_surface_format_ = format; |
| 268 compositor_->SetSurface(surface); |
| 269 } |
| 270 gfx::Size size = gfx::Size(width, height); |
| 271 compositor_->SetWindowBounds(size); |
| 272 content_width_ = size.width(); |
| 273 content_height_ = size.height(); |
| 274 LOG(ERROR) << "bshe" << "======size=======" << size.ToString(); |
| 275 root_layer_->SetBounds(gfx::Size(content_width_, content_height_)); |
| 276 } |
| 277 |
| 278 ui::UIResourceProvider* VrShell::GetUIResourceProvider() { |
| 279 if (!compositor_) |
| 280 return NULL; |
| 281 return &compositor_->GetUIResourceProvider(); |
| 282 } |
| 283 |
| 284 void VrShell::SetNeedsComposite( |
| 285 JNIEnv* env, |
| 286 const base::android::JavaParamRef<jobject>& object) { |
| 287 compositor_->SetNeedsComposite(); |
| 288 } |
| 289 |
| 146 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, | 290 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, |
| 147 const gvr::BufferViewport& params) { | 291 const gvr::BufferViewport& params) { |
| 148 gvr::Recti pixel_rect = | 292 gvr::Recti pixel_rect = |
| 149 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); | 293 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); |
| 150 glViewport(pixel_rect.left, pixel_rect.bottom, | 294 glViewport(pixel_rect.left, pixel_rect.bottom, |
| 151 pixel_rect.right - pixel_rect.left, | 295 pixel_rect.right - pixel_rect.left, |
| 152 pixel_rect.top - pixel_rect.bottom); | 296 pixel_rect.top - pixel_rect.bottom); |
| 153 glScissor(pixel_rect.left, pixel_rect.bottom, | 297 glScissor(pixel_rect.left, pixel_rect.bottom, |
| 154 pixel_rect.right - pixel_rect.left, | 298 pixel_rect.right - pixel_rect.left, |
| 155 pixel_rect.top - pixel_rect.bottom); | 299 pixel_rect.top - pixel_rect.bottom); |
| 156 | 300 |
| 157 view_matrix_ = view_matrix; | 301 view_matrix_ = view_matrix; |
| 158 | 302 |
| 159 projection_matrix_ = | 303 projection_matrix_ = |
| 160 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); | 304 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); |
| 161 | 305 |
| 162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 306 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 163 DrawContentRect(); | 307 DrawContentRect(); |
| 164 } | 308 } |
| 165 | 309 |
| 166 void VrShell::DrawContentRect() { | 310 void VrShell::DrawContentRect() { |
| 167 gvr::Mat4f content_rect_combined_matrix = | 311 gvr::Mat4f content_rect_combined_matrix = |
| 168 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); | 312 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); |
| 169 content_rect_combined_matrix = | 313 content_rect_combined_matrix = |
| 170 MatrixMul(projection_matrix_, content_rect_combined_matrix); | 314 MatrixMul(projection_matrix_, content_rect_combined_matrix); |
| 171 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | 315 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( |
| 172 content_rect_->content_texture_handle, content_rect_combined_matrix); | 316 content_rect_->content_texture_handle, content_rect_combined_matrix); |
| 173 } | 317 } |
| 174 | 318 |
| 319 void VrShell::BrowserChildProcessHostDisconnected( |
| 320 const content::ChildProcessData& data) { |
| 321 LOG(WARNING) << "Child process disconnected (type=" << data.process_type |
| 322 << ") pid=" << data.handle << ")"; |
| 323 if (base::android::BuildInfo::GetInstance()->sdk_int() <= |
| 324 base::android::SDK_VERSION_JELLY_BEAN_MR2 && |
| 325 data.process_type == content::PROCESS_TYPE_GPU) { |
| 326 compositor_->SetSurface(nullptr); |
| 327 } |
| 328 } |
| 329 |
| 330 void VrShell::BrowserChildProcessCrashed(const content::ChildProcessData& data, |
| 331 int exit_code) { |
| 332 // The Android TERMINATION_STATUS_OOM_PROTECTED hack causes us to never go |
| 333 // through here but through BrowserChildProcessHostDisconnected() instead. |
| 334 } |
| 335 |
| 175 void VrShell::OnPause(JNIEnv* env, | 336 void VrShell::OnPause(JNIEnv* env, |
| 176 const base::android::JavaParamRef<jobject>& obj) { | 337 const base::android::JavaParamRef<jobject>& obj) { |
| 177 if (gvr_api_ == nullptr) | 338 if (gvr_api_ == nullptr) |
| 178 return; | 339 return; |
| 179 gvr_api_->PauseTracking(); | 340 gvr_api_->PauseTracking(); |
| 180 } | 341 } |
| 181 | 342 |
| 182 void VrShell::OnResume(JNIEnv* env, | 343 void VrShell::OnResume(JNIEnv* env, |
| 183 const base::android::JavaParamRef<jobject>& obj) { | 344 const base::android::JavaParamRef<jobject>& obj) { |
| 184 if (gvr_api_ == nullptr) | 345 if (gvr_api_ == nullptr) |
| 185 return; | 346 return; |
| 186 gvr_api_->RefreshViewerProfile(); | 347 gvr_api_->RefreshViewerProfile(); |
| 187 gvr_api_->ResumeTracking(); | 348 gvr_api_->ResumeTracking(); |
| 188 } | 349 } |
| 189 | 350 |
| 190 // ---------------------------------------------------------------------------- | 351 // ---------------------------------------------------------------------------- |
| 191 // Native JNI methods | 352 // Native JNI methods |
| 192 // ---------------------------------------------------------------------------- | 353 // ---------------------------------------------------------------------------- |
| 354 jlong Init(JNIEnv* env, |
| 355 const base::android::JavaParamRef<jobject>& obj, |
| 356 jboolean low_mem_device, |
| 357 jlong native_window_android) { |
| 358 ui::WindowAndroid* window_android = |
| 359 // ui::WindowAndroid::createForTesting(); |
| 360 reinterpret_cast<ui::WindowAndroid*>(native_window_android); |
| 193 | 361 |
| 194 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { | 362 VrShell* vrShell = new VrShell(env, obj, low_mem_device, window_android); |
| 195 VrShell* vrShell = new VrShell(env, obj); | 363 |
| 196 return reinterpret_cast<intptr_t>(vrShell); | 364 return reinterpret_cast<intptr_t>(vrShell); |
| 197 } | 365 } |
| 198 | 366 |
| 199 } // namespace vr_shell | 367 } // namespace vr_shell |
| OLD | NEW |