Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "android_webview/browser/in_process_view_renderer.h" | 5 #include "android_webview/browser/in_process_view_renderer.h" |
| 6 | 6 |
| 7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
| 8 | 8 |
| 9 #include "android_webview/browser/aw_gl_surface.h" | |
| 9 #include "android_webview/browser/scoped_app_gl_state_restore.h" | 10 #include "android_webview/browser/scoped_app_gl_state_restore.h" |
| 10 #include "android_webview/common/aw_switches.h" | 11 #include "android_webview/common/aw_switches.h" |
| 11 #include "android_webview/public/browser/draw_gl.h" | 12 #include "android_webview/public/browser/draw_gl.h" |
| 12 #include "android_webview/public/browser/draw_sw.h" | 13 #include "android_webview/public/browser/draw_sw.h" |
| 13 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
| 14 #include "base/auto_reset.h" | 15 #include "base/auto_reset.h" |
| 15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 16 #include "base/debug/trace_event.h" | 17 #include "base/debug/trace_event.h" |
| 17 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 154 } |
| 154 | 155 |
| 155 ScopedAllowGL::~ScopedAllowGL() { | 156 ScopedAllowGL::~ScopedAllowGL() { |
| 156 allow_gl = false; | 157 allow_gl = false; |
| 157 } | 158 } |
| 158 | 159 |
| 159 bool ScopedAllowGL::allow_gl = false; | 160 bool ScopedAllowGL::allow_gl = false; |
| 160 | 161 |
| 161 base::LazyInstance<GLViewRendererManager>::Leaky g_view_renderer_manager; | 162 base::LazyInstance<GLViewRendererManager>::Leaky g_view_renderer_manager; |
| 162 | 163 |
| 164 class ScopedSetSurfaceFactory { | |
| 165 public: | |
| 166 ScopedSetSurfaceFactory(InProcessViewRenderer* factory); | |
|
piman
2013/08/06 21:41:55
nit: explicit
| |
| 167 ~ScopedSetSurfaceFactory(); | |
| 168 }; | |
| 169 | |
| 170 ScopedSetSurfaceFactory::ScopedSetSurfaceFactory( | |
| 171 InProcessViewRenderer* factory) { | |
| 172 DCHECK(factory); | |
| 173 DCHECK(!gfx::GLFactoryAndroid::GetCurrent()); | |
| 174 gfx::GLFactoryAndroid::SetCurrent(factory); | |
| 175 } | |
| 176 | |
| 177 ScopedSetSurfaceFactory::~ScopedSetSurfaceFactory() { | |
| 178 gfx::GLFactoryAndroid::SetCurrent(NULL); | |
| 179 } | |
| 180 | |
| 163 } // namespace | 181 } // namespace |
| 164 | 182 |
| 165 // Called from different threads! | 183 // Called from different threads! |
| 166 static void ScheduleGpuWork() { | 184 static void ScheduleGpuWork() { |
| 167 if (ScopedAllowGL::IsAllowed()) { | 185 if (ScopedAllowGL::IsAllowed()) { |
| 168 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); | 186 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); |
| 169 } else { | 187 } else { |
| 170 InProcessViewRenderer* renderer = static_cast<InProcessViewRenderer*>( | 188 InProcessViewRenderer* renderer = static_cast<InProcessViewRenderer*>( |
| 171 g_view_renderer_manager.Get().GetMostRecentlyDrawn()); | 189 g_view_renderer_manager.Get().GetMostRecentlyDrawn()); |
| 172 if (!renderer || !renderer->RequestProcessGL()) { | 190 if (!renderer || !renderer->RequestProcessGL()) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 return compositor_ && client_->RequestDrawGL(java_canvas); | 300 return compositor_ && client_->RequestDrawGL(java_canvas); |
| 283 } | 301 } |
| 284 // Perform a software draw | 302 // Perform a software draw |
| 285 block_invalidates_ = true; | 303 block_invalidates_ = true; |
| 286 bool result = DrawSWInternal(java_canvas, clip); | 304 bool result = DrawSWInternal(java_canvas, clip); |
| 287 block_invalidates_ = false; | 305 block_invalidates_ = false; |
| 288 EnsureContinuousInvalidation(NULL, false); | 306 EnsureContinuousInvalidation(NULL, false); |
| 289 return result; | 307 return result; |
| 290 } | 308 } |
| 291 | 309 |
| 310 scoped_refptr<gfx::GLSurface> | |
| 311 InProcessViewRenderer::CreateNonOwnedViewSurface() { | |
| 312 DCHECK(!hardware_initialized_); | |
| 313 DCHECK(!gl_surface_); | |
| 314 gl_surface_ = new AwGLSurface; | |
| 315 return gl_surface_; | |
| 316 } | |
| 317 | |
| 318 bool InProcessViewRenderer::InitializeHwDraw() { | |
| 319 TRACE_EVENT0("android_webview", "InitializeHwDraw"); | |
| 320 | |
| 321 { | |
| 322 ScopedSetSurfaceFactory setter(this); | |
|
piman
2013/08/06 21:41:55
So, essentially, you're passing |this| all the way
no sievers
2013/08/06 21:44:50
Just chatted with Bo and realized (a little too la
boliu
2013/08/06 21:51:16
That's actually attempt 1: https://codereview.chro
| |
| 323 hardware_failed_ = !compositor_->InitializeHwDraw(); | |
| 324 } | |
| 325 | |
| 326 hardware_initialized_ = true; | |
| 327 if (!hardware_failed_) { | |
| 328 DCHECK(gl_surface_); | |
| 329 } | |
| 330 | |
| 331 return !hardware_failed_; | |
| 332 } | |
| 333 | |
| 292 void InProcessViewRenderer::DrawGL(AwDrawGLInfo* draw_info) { | 334 void InProcessViewRenderer::DrawGL(AwDrawGLInfo* draw_info) { |
| 293 TRACE_EVENT0("android_webview", "InProcessViewRenderer::DrawGL"); | 335 TRACE_EVENT0("android_webview", "InProcessViewRenderer::DrawGL"); |
| 294 DCHECK(visible_); | 336 DCHECK(visible_); |
| 295 | 337 |
| 296 manager_key_ = g_view_renderer_manager.Get().DidDrawGL(manager_key_, this); | 338 manager_key_ = g_view_renderer_manager.Get().DidDrawGL(manager_key_, this); |
| 297 | 339 |
| 298 // We need to watch if the current Android context has changed and enforce | 340 // We need to watch if the current Android context has changed and enforce |
| 299 // a clean-up in the compositor. | 341 // a clean-up in the compositor. |
| 300 EGLContext current_context = eglGetCurrentContext(); | 342 EGLContext current_context = eglGetCurrentContext(); |
| 301 if (!current_context) { | 343 if (!current_context) { |
| 302 TRACE_EVENT_INSTANT0( | 344 TRACE_EVENT_INSTANT0( |
| 303 "android_webview", "EarlyOut_NullEGLContext", TRACE_EVENT_SCOPE_THREAD); | 345 "android_webview", "EarlyOut_NullEGLContext", TRACE_EVENT_SCOPE_THREAD); |
| 304 return; | 346 return; |
| 305 } | 347 } |
| 306 | 348 |
| 307 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); | 349 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); |
| 308 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); | 350 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); |
| 309 ScopedAllowGL allow_gl; | 351 ScopedAllowGL allow_gl; |
| 310 | 352 |
| 311 if (attached_to_window_ && compositor_ && !hardware_initialized_) { | 353 if (attached_to_window_ && compositor_ && !hardware_initialized_) { |
| 312 TRACE_EVENT0("android_webview", "InitializeHwDraw"); | 354 if (InitializeHwDraw()) |
| 313 hardware_failed_ = !compositor_->InitializeHwDraw(); | 355 last_egl_context_ = current_context; |
| 314 hardware_initialized_ = true; | 356 else |
| 315 last_egl_context_ = current_context; | |
| 316 | |
| 317 if (hardware_failed_) | |
| 318 return; | 357 return; |
| 319 } | 358 } |
| 320 | 359 |
| 321 if (draw_info->mode == AwDrawGLInfo::kModeProcess) | 360 if (draw_info->mode == AwDrawGLInfo::kModeProcess) |
| 322 return; | 361 return; |
| 323 | 362 |
| 324 // DrawGL may be called without OnDraw, so cancel |fallback_tick_| here as | 363 // DrawGL may be called without OnDraw, so cancel |fallback_tick_| here as |
| 325 // well just to be safe. | 364 // well just to be safe. |
| 326 fallback_tick_.Cancel(); | 365 fallback_tick_.Cancel(); |
| 327 | 366 |
| 328 if (last_egl_context_ != current_context) { | 367 if (last_egl_context_ != current_context) { |
| 329 // TODO(boliu): Handle context lost | 368 // TODO(boliu): Handle context lost |
| 330 TRACE_EVENT_INSTANT0( | 369 TRACE_EVENT_INSTANT0( |
| 331 "android_webview", "EGLContextChanged", TRACE_EVENT_SCOPE_THREAD); | 370 "android_webview", "EGLContextChanged", TRACE_EVENT_SCOPE_THREAD); |
| 332 } | 371 } |
| 333 last_egl_context_ = current_context; | |
| 334 | 372 |
| 335 if (!compositor_) { | 373 if (!compositor_) { |
| 336 TRACE_EVENT_INSTANT0( | 374 TRACE_EVENT_INSTANT0( |
| 337 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); | 375 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); |
| 338 return; | 376 return; |
| 339 } | 377 } |
| 340 | 378 |
| 379 DCHECK(gl_surface_); | |
| 380 gl_surface_->SetBackingFrameBufferObject( | |
| 381 state_restore.framebuffer_binding_ext()); | |
| 382 | |
| 341 gfx::Transform transform; | 383 gfx::Transform transform; |
| 342 transform.matrix().setColMajorf(draw_info->transform); | 384 transform.matrix().setColMajorf(draw_info->transform); |
| 343 transform.Translate(scroll_at_start_of_frame_.x(), | 385 transform.Translate(scroll_at_start_of_frame_.x(), |
| 344 scroll_at_start_of_frame_.y()); | 386 scroll_at_start_of_frame_.y()); |
| 345 // TODO(joth): Check return value. | |
| 346 block_invalidates_ = true; | |
| 347 gfx::Rect clip_rect(draw_info->clip_left, | 387 gfx::Rect clip_rect(draw_info->clip_left, |
| 348 draw_info->clip_top, | 388 draw_info->clip_top, |
| 349 draw_info->clip_right - draw_info->clip_left, | 389 draw_info->clip_right - draw_info->clip_left, |
| 350 draw_info->clip_bottom - draw_info->clip_top); | 390 draw_info->clip_bottom - draw_info->clip_top); |
| 391 block_invalidates_ = true; | |
| 392 // TODO(joth): Check return value. | |
| 351 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), | 393 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), |
| 352 transform, | 394 transform, |
| 353 clip_rect, | 395 clip_rect, |
| 354 state_restore.stencil_enabled()); | 396 state_restore.stencil_enabled()); |
| 355 block_invalidates_ = false; | 397 block_invalidates_ = false; |
| 398 gl_surface_->ResetBackingFrameBufferObject(); | |
| 356 | 399 |
| 357 UpdateCachedGlobalVisibleRect(); | 400 UpdateCachedGlobalVisibleRect(); |
| 358 bool drew_full_visible_rect = clip_rect.Contains(cached_global_visible_rect_); | 401 bool drew_full_visible_rect = clip_rect.Contains(cached_global_visible_rect_); |
| 359 EnsureContinuousInvalidation(draw_info, !drew_full_visible_rect); | 402 EnsureContinuousInvalidation(draw_info, !drew_full_visible_rect); |
| 360 } | 403 } |
| 361 | 404 |
| 362 void InProcessViewRenderer::SetGlobalVisibleRect( | 405 void InProcessViewRenderer::SetGlobalVisibleRect( |
| 363 const gfx::Rect& visible_rect) { | 406 const gfx::Rect& visible_rect) { |
| 364 cached_global_visible_rect_ = visible_rect; | 407 cached_global_visible_rect_ = visible_rect; |
| 365 } | 408 } |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 NoLongerExpectsDrawGL(); | 588 NoLongerExpectsDrawGL(); |
| 546 if (hardware_initialized_) { | 589 if (hardware_initialized_) { |
| 547 DCHECK(compositor_); | 590 DCHECK(compositor_); |
| 548 | 591 |
| 549 ScopedAppGLStateRestore state_restore( | 592 ScopedAppGLStateRestore state_restore( |
| 550 ScopedAppGLStateRestore::MODE_DETACH_FROM_WINDOW); | 593 ScopedAppGLStateRestore::MODE_DETACH_FROM_WINDOW); |
| 551 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); | 594 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); |
| 552 ScopedAllowGL allow_gl; | 595 ScopedAllowGL allow_gl; |
| 553 compositor_->ReleaseHwDraw(); | 596 compositor_->ReleaseHwDraw(); |
| 554 hardware_initialized_ = false; | 597 hardware_initialized_ = false; |
| 598 gl_surface_ = NULL; | |
| 555 } | 599 } |
| 556 | 600 |
| 557 attached_to_window_ = false; | 601 attached_to_window_ = false; |
| 558 } | 602 } |
| 559 | 603 |
| 560 bool InProcessViewRenderer::IsAttachedToWindow() { | 604 bool InProcessViewRenderer::IsAttachedToWindow() { |
| 561 return attached_to_window_; | 605 return attached_to_window_; |
| 562 } | 606 } |
| 563 | 607 |
| 564 bool InProcessViewRenderer::IsViewVisible() { | 608 bool InProcessViewRenderer::IsViewVisible() { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 776 base::StringAppendF(&str, | 820 base::StringAppendF(&str, |
| 777 "surface width height: [%d %d] ", | 821 "surface width height: [%d %d] ", |
| 778 draw_info->width, | 822 draw_info->width, |
| 779 draw_info->height); | 823 draw_info->height); |
| 780 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | 824 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); |
| 781 } | 825 } |
| 782 return str; | 826 return str; |
| 783 } | 827 } |
| 784 | 828 |
| 785 } // namespace android_webview | 829 } // namespace android_webview |
| OLD | NEW |