| 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); |
| 167 ~ScopedSetSurfaceFactory(); |
| 168 }; |
| 169 |
| 170 ScopedSetSurfaceFactory::ScopedSetSurfaceFactory( |
| 171 InProcessViewRenderer* factory) { |
| 172 DCHECK(factory); |
| 173 DCHECK(!gfx::SurfaceFactoryAndroid::GetInstance()); |
| 174 gfx::SurfaceFactoryAndroid::SetInstance(factory); |
| 175 } |
| 176 |
| 177 ScopedSetSurfaceFactory::~ScopedSetSurfaceFactory() { |
| 178 gfx::SurfaceFactoryAndroid::SetInstance(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()) { |
| 173 LOG(ERROR) << "Failed to request DrawGL. Probably going to deadlock."; | 191 LOG(ERROR) << "Failed to request DrawGL. Probably going to deadlock."; |
| 174 } | 192 } |
| 175 } | 193 } |
| 176 } | 194 } |
| 177 | 195 |
| 178 // static | 196 // static |
| 179 void BrowserViewRenderer::SetAwDrawSWFunctionTable( | 197 void BrowserViewRenderer::SetAwDrawSWFunctionTable( |
| 180 AwDrawSWFunctionTable* table) { | 198 AwDrawSWFunctionTable* table) { |
| 181 g_sw_draw_functions = table; | 199 g_sw_draw_functions = table; |
| 182 g_is_skia_version_compatible = | 200 g_is_skia_version_compatible = |
| 183 g_sw_draw_functions->is_skia_version_compatible(&SkGraphics::GetVersion); | 201 g_sw_draw_functions->is_skia_version_compatible(&SkGraphics::GetVersion); |
| 184 LOG_IF(WARNING, !g_is_skia_version_compatible) | 202 LOG_IF(WARNING, !g_is_skia_version_compatible) |
| 185 << "Skia versions are not compatible, rendering performance will suffer."; | 203 << "Skia versions are not compatible, rendering performance will suffer."; |
| 186 | 204 |
| 187 gpu::InProcessCommandBuffer::SetScheduleCallback( | 205 gpu::InProcessCommandBuffer::SetScheduleCallback( |
| 188 base::Bind(&ScheduleGpuWork)); | 206 base::Bind(&ScheduleGpuWork)); |
| 207 |
| 208 DCHECK(!gfx::SurfaceFactoryAndroid::GetInstance()); |
| 189 } | 209 } |
| 190 | 210 |
| 191 // static | 211 // static |
| 192 AwDrawSWFunctionTable* BrowserViewRenderer::GetAwDrawSWFunctionTable() { | 212 AwDrawSWFunctionTable* BrowserViewRenderer::GetAwDrawSWFunctionTable() { |
| 193 return g_sw_draw_functions; | 213 return g_sw_draw_functions; |
| 194 } | 214 } |
| 195 | 215 |
| 196 // static | 216 // static |
| 197 bool BrowserViewRenderer::IsSkiaVersionCompatible() { | 217 bool BrowserViewRenderer::IsSkiaVersionCompatible() { |
| 198 DCHECK(g_sw_draw_functions); | 218 DCHECK(g_sw_draw_functions); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 return compositor_ && client_->RequestDrawGL(java_canvas); | 302 return compositor_ && client_->RequestDrawGL(java_canvas); |
| 283 } | 303 } |
| 284 // Perform a software draw | 304 // Perform a software draw |
| 285 block_invalidates_ = true; | 305 block_invalidates_ = true; |
| 286 bool result = DrawSWInternal(java_canvas, clip); | 306 bool result = DrawSWInternal(java_canvas, clip); |
| 287 block_invalidates_ = false; | 307 block_invalidates_ = false; |
| 288 EnsureContinuousInvalidation(NULL, false); | 308 EnsureContinuousInvalidation(NULL, false); |
| 289 return result; | 309 return result; |
| 290 } | 310 } |
| 291 | 311 |
| 312 scoped_refptr<gfx::GLSurface> |
| 313 InProcessViewRenderer::CreateNonOwnedViewSurface() { |
| 314 DCHECK(!hardware_initialized_); |
| 315 DCHECK(!gl_surface_); |
| 316 gl_surface_ = new AwGLSurface; |
| 317 return gl_surface_; |
| 318 } |
| 319 |
| 320 bool InProcessViewRenderer::InitializeHwDraw() { |
| 321 TRACE_EVENT0("android_webview", "InitializeHwDraw"); |
| 322 |
| 323 { |
| 324 ScopedSetSurfaceFactory setter(this); |
| 325 hardware_failed_ = !compositor_->InitializeHwDraw(); |
| 326 } |
| 327 |
| 328 hardware_initialized_ = true; |
| 329 if (!hardware_failed_) { |
| 330 DCHECK(gl_surface_); |
| 331 } |
| 332 |
| 333 return !hardware_failed_; |
| 334 } |
| 335 |
| 292 void InProcessViewRenderer::DrawGL(AwDrawGLInfo* draw_info) { | 336 void InProcessViewRenderer::DrawGL(AwDrawGLInfo* draw_info) { |
| 293 TRACE_EVENT0("android_webview", "InProcessViewRenderer::DrawGL"); | 337 TRACE_EVENT0("android_webview", "InProcessViewRenderer::DrawGL"); |
| 294 DCHECK(visible_); | 338 DCHECK(visible_); |
| 295 | 339 |
| 296 manager_key_ = g_view_renderer_manager.Get().DidDrawGL(manager_key_, this); | 340 manager_key_ = g_view_renderer_manager.Get().DidDrawGL(manager_key_, this); |
| 297 | 341 |
| 298 // We need to watch if the current Android context has changed and enforce | 342 // We need to watch if the current Android context has changed and enforce |
| 299 // a clean-up in the compositor. | 343 // a clean-up in the compositor. |
| 300 EGLContext current_context = eglGetCurrentContext(); | 344 EGLContext current_context = eglGetCurrentContext(); |
| 301 if (!current_context) { | 345 if (!current_context) { |
| 302 TRACE_EVENT_INSTANT0( | 346 TRACE_EVENT_INSTANT0( |
| 303 "android_webview", "EarlyOut_NullEGLContext", TRACE_EVENT_SCOPE_THREAD); | 347 "android_webview", "EarlyOut_NullEGLContext", TRACE_EVENT_SCOPE_THREAD); |
| 304 return; | 348 return; |
| 305 } | 349 } |
| 306 | 350 |
| 307 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); | 351 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); |
| 308 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); | 352 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); |
| 309 ScopedAllowGL allow_gl; | 353 ScopedAllowGL allow_gl; |
| 310 | 354 |
| 311 if (attached_to_window_ && compositor_ && !hardware_initialized_) { | 355 if (attached_to_window_ && compositor_ && !hardware_initialized_) { |
| 312 TRACE_EVENT0("android_webview", "InitializeHwDraw"); | 356 if (InitializeHwDraw()) |
| 313 hardware_failed_ = !compositor_->InitializeHwDraw(); | 357 last_egl_context_ = current_context; |
| 314 hardware_initialized_ = true; | 358 else |
| 315 last_egl_context_ = current_context; | |
| 316 | |
| 317 if (hardware_failed_) | |
| 318 return; | 359 return; |
| 319 } | 360 } |
| 320 | 361 |
| 321 if (draw_info->mode == AwDrawGLInfo::kModeProcess) | 362 if (draw_info->mode == AwDrawGLInfo::kModeProcess) |
| 322 return; | 363 return; |
| 323 | 364 |
| 324 // DrawGL may be called without OnDraw, so cancel |fallback_tick_| here as | 365 // DrawGL may be called without OnDraw, so cancel |fallback_tick_| here as |
| 325 // well just to be safe. | 366 // well just to be safe. |
| 326 fallback_tick_.Cancel(); | 367 fallback_tick_.Cancel(); |
| 327 | 368 |
| 328 if (last_egl_context_ != current_context) { | 369 if (last_egl_context_ != current_context) { |
| 329 // TODO(boliu): Handle context lost | 370 // TODO(boliu): Handle context lost |
| 330 TRACE_EVENT_INSTANT0( | 371 TRACE_EVENT_INSTANT0( |
| 331 "android_webview", "EGLContextChanged", TRACE_EVENT_SCOPE_THREAD); | 372 "android_webview", "EGLContextChanged", TRACE_EVENT_SCOPE_THREAD); |
| 332 } | 373 } |
| 333 last_egl_context_ = current_context; | |
| 334 | 374 |
| 335 if (!compositor_) { | 375 if (!compositor_) { |
| 336 TRACE_EVENT_INSTANT0( | 376 TRACE_EVENT_INSTANT0( |
| 337 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); | 377 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); |
| 338 return; | 378 return; |
| 339 } | 379 } |
| 340 | 380 |
| 381 DCHECK(gl_surface_); |
| 382 gl_surface_->SetBackingFrameBufferObject( |
| 383 state_restore.framebuffer_binding_ext()); |
| 384 |
| 341 gfx::Transform transform; | 385 gfx::Transform transform; |
| 342 transform.matrix().setColMajorf(draw_info->transform); | 386 transform.matrix().setColMajorf(draw_info->transform); |
| 343 transform.Translate(scroll_at_start_of_frame_.x(), | 387 transform.Translate(scroll_at_start_of_frame_.x(), |
| 344 scroll_at_start_of_frame_.y()); | 388 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, | 389 gfx::Rect clip_rect(draw_info->clip_left, |
| 348 draw_info->clip_top, | 390 draw_info->clip_top, |
| 349 draw_info->clip_right - draw_info->clip_left, | 391 draw_info->clip_right - draw_info->clip_left, |
| 350 draw_info->clip_bottom - draw_info->clip_top); | 392 draw_info->clip_bottom - draw_info->clip_top); |
| 393 block_invalidates_ = true; |
| 394 // TODO(joth): Check return value. |
| 351 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), | 395 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), |
| 352 transform, | 396 transform, |
| 353 clip_rect, | 397 clip_rect, |
| 354 state_restore.stencil_enabled()); | 398 state_restore.stencil_enabled()); |
| 355 block_invalidates_ = false; | 399 block_invalidates_ = false; |
| 400 gl_surface_->ResetBackingFrameBufferObject(); |
| 356 | 401 |
| 357 UpdateCachedGlobalVisibleRect(); | 402 UpdateCachedGlobalVisibleRect(); |
| 358 bool drew_full_visible_rect = clip_rect.Contains(cached_global_visible_rect_); | 403 bool drew_full_visible_rect = clip_rect.Contains(cached_global_visible_rect_); |
| 359 EnsureContinuousInvalidation(draw_info, !drew_full_visible_rect); | 404 EnsureContinuousInvalidation(draw_info, !drew_full_visible_rect); |
| 360 } | 405 } |
| 361 | 406 |
| 362 void InProcessViewRenderer::SetGlobalVisibleRect( | 407 void InProcessViewRenderer::SetGlobalVisibleRect( |
| 363 const gfx::Rect& visible_rect) { | 408 const gfx::Rect& visible_rect) { |
| 364 cached_global_visible_rect_ = visible_rect; | 409 cached_global_visible_rect_ = visible_rect; |
| 365 } | 410 } |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 NoLongerExpectsDrawGL(); | 590 NoLongerExpectsDrawGL(); |
| 546 if (hardware_initialized_) { | 591 if (hardware_initialized_) { |
| 547 DCHECK(compositor_); | 592 DCHECK(compositor_); |
| 548 | 593 |
| 549 ScopedAppGLStateRestore state_restore( | 594 ScopedAppGLStateRestore state_restore( |
| 550 ScopedAppGLStateRestore::MODE_DETACH_FROM_WINDOW); | 595 ScopedAppGLStateRestore::MODE_DETACH_FROM_WINDOW); |
| 551 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); | 596 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); |
| 552 ScopedAllowGL allow_gl; | 597 ScopedAllowGL allow_gl; |
| 553 compositor_->ReleaseHwDraw(); | 598 compositor_->ReleaseHwDraw(); |
| 554 hardware_initialized_ = false; | 599 hardware_initialized_ = false; |
| 600 gl_surface_ = NULL; |
| 555 } | 601 } |
| 556 | 602 |
| 557 attached_to_window_ = false; | 603 attached_to_window_ = false; |
| 558 } | 604 } |
| 559 | 605 |
| 560 bool InProcessViewRenderer::IsAttachedToWindow() { | 606 bool InProcessViewRenderer::IsAttachedToWindow() { |
| 561 return attached_to_window_; | 607 return attached_to_window_; |
| 562 } | 608 } |
| 563 | 609 |
| 564 bool InProcessViewRenderer::IsViewVisible() { | 610 bool InProcessViewRenderer::IsViewVisible() { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 base::StringAppendF(&str, | 822 base::StringAppendF(&str, |
| 777 "surface width height: [%d %d] ", | 823 "surface width height: [%d %d] ", |
| 778 draw_info->width, | 824 draw_info->width, |
| 779 draw_info->height); | 825 draw_info->height); |
| 780 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | 826 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); |
| 781 } | 827 } |
| 782 return str; | 828 return str; |
| 783 } | 829 } |
| 784 | 830 |
| 785 } // namespace android_webview | 831 } // namespace android_webview |
| OLD | NEW |