Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser_view_renderer.h" | 5 #include "android_webview/browser/browser_view_renderer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "android_webview/browser/browser_view_renderer_client.h" | 9 #include "android_webview/browser/browser_view_renderer_client.h" |
| 10 #include "android_webview/browser/child_frame.h" | 10 #include "android_webview/browser/child_frame.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "third_party/skia/include/core/SkPictureRecorder.h" | 25 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 26 #include "ui/gfx/geometry/scroll_offset.h" | 26 #include "ui/gfx/geometry/scroll_offset.h" |
| 27 #include "ui/gfx/geometry/vector2d_conversions.h" | 27 #include "ui/gfx/geometry/vector2d_conversions.h" |
| 28 | 28 |
| 29 namespace android_webview { | 29 namespace android_webview { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const double kEpsilon = 1e-8; | 33 const double kEpsilon = 1e-8; |
| 34 | 34 |
| 35 const int64_t kFallbackTickTimeoutInMilliseconds = 100; | |
| 36 | |
| 37 // Used to calculate memory allocation. Determined experimentally. | 35 // Used to calculate memory allocation. Determined experimentally. |
| 38 const size_t kMemoryMultiplier = 20; | 36 const size_t kMemoryMultiplier = 20; |
| 39 const size_t kBytesPerPixel = 4; | 37 const size_t kBytesPerPixel = 4; |
| 40 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; | 38 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; |
| 41 uint64_t g_memory_override_in_bytes = 0u; | 39 uint64_t g_memory_override_in_bytes = 0u; |
| 42 | 40 |
| 43 const void* const kBrowserViewRendererUserDataKey = | 41 const void* const kBrowserViewRendererUserDataKey = |
| 44 &kBrowserViewRendererUserDataKey; | 42 &kBrowserViewRendererUserDataKey; |
| 45 | 43 |
| 46 class BrowserViewRendererUserData : public base::SupportsUserData::Data { | 44 class BrowserViewRendererUserData : public base::SupportsUserData::Data { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 window_visible_(false), | 97 window_visible_(false), |
| 100 attached_to_window_(false), | 98 attached_to_window_(false), |
| 101 hardware_enabled_(false), | 99 hardware_enabled_(false), |
| 102 dip_scale_(0.f), | 100 dip_scale_(0.f), |
| 103 page_scale_factor_(1.f), | 101 page_scale_factor_(1.f), |
| 104 min_page_scale_factor_(0.f), | 102 min_page_scale_factor_(0.f), |
| 105 max_page_scale_factor_(0.f), | 103 max_page_scale_factor_(0.f), |
| 106 on_new_picture_enable_(false), | 104 on_new_picture_enable_(false), |
| 107 clear_view_(false), | 105 clear_view_(false), |
| 108 offscreen_pre_raster_(false), | 106 offscreen_pre_raster_(false), |
| 109 fallback_tick_pending_(false), | |
| 110 next_compositor_id_(1) {} | 107 next_compositor_id_(1) {} |
| 111 | 108 |
| 112 BrowserViewRenderer::~BrowserViewRenderer() { | 109 BrowserViewRenderer::~BrowserViewRenderer() { |
| 113 DCHECK(compositor_map_.empty()); | 110 DCHECK(compositor_map_.empty()); |
| 114 } | 111 } |
| 115 | 112 |
| 116 void BrowserViewRenderer::RegisterWithWebContents( | 113 void BrowserViewRenderer::RegisterWithWebContents( |
| 117 content::WebContents* web_contents) { | 114 content::WebContents* web_contents) { |
| 118 web_contents->SetUserData(kBrowserViewRendererUserDataKey, | 115 web_contents->SetUserData(kBrowserViewRendererUserDataKey, |
| 119 new BrowserViewRendererUserData(this)); | 116 new BrowserViewRendererUserData(this)); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 | 222 |
| 226 shared_renderer_state_.InitializeHardwareDrawIfNeededOnUI(); | 223 shared_renderer_state_.InitializeHardwareDrawIfNeededOnUI(); |
| 227 | 224 |
| 228 if (!CanOnDraw()) { | 225 if (!CanOnDraw()) { |
| 229 return false; | 226 return false; |
| 230 } | 227 } |
| 231 | 228 |
| 232 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_); | 229 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_); |
| 233 hardware_enabled_ = true; | 230 hardware_enabled_ = true; |
| 234 | 231 |
| 235 return CompositeHw(); | |
| 236 } | |
| 237 | |
| 238 bool BrowserViewRenderer::CompositeHw() { | |
| 239 CancelFallbackTick(); | |
| 240 | |
| 241 ReturnResourceFromParent(); | 232 ReturnResourceFromParent(); |
| 242 UpdateMemoryPolicy(); | 233 UpdateMemoryPolicy(); |
| 243 | 234 |
| 244 ParentCompositorDrawConstraints parent_draw_constraints = | 235 ParentCompositorDrawConstraints parent_draw_constraints = |
| 245 shared_renderer_state_.GetParentDrawConstraintsOnUI(); | 236 shared_renderer_state_.GetParentDrawConstraintsOnUI(); |
| 246 gfx::Size surface_size(size_); | 237 gfx::Size surface_size(size_); |
| 247 gfx::Rect viewport(surface_size); | 238 gfx::Rect viewport(surface_size); |
| 248 gfx::Rect clip = viewport; | 239 gfx::Rect clip = viewport; |
| 249 gfx::Transform transform_for_tile_priority = | 240 gfx::Transform transform_for_tile_priority = |
| 250 parent_draw_constraints.transform; | 241 parent_draw_constraints.transform; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 270 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame", | 261 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame", |
| 271 TRACE_EVENT_SCOPE_THREAD); | 262 TRACE_EVENT_SCOPE_THREAD); |
| 272 return shared_renderer_state_.HasFrameOnUI(); | 263 return shared_renderer_state_.HasFrameOnUI(); |
| 273 } | 264 } |
| 274 | 265 |
| 275 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(new ChildFrame( | 266 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(new ChildFrame( |
| 276 std::move(frame), GetCompositorID(compositor_), | 267 std::move(frame), GetCompositorID(compositor_), |
| 277 viewport_rect_for_tile_priority.IsEmpty(), transform_for_tile_priority, | 268 viewport_rect_for_tile_priority.IsEmpty(), transform_for_tile_priority, |
| 278 offscreen_pre_raster_, parent_draw_constraints.is_layer)); | 269 offscreen_pre_raster_, parent_draw_constraints.is_layer)); |
| 279 | 270 |
| 280 // Uncommitted frame can happen with consecutive fallback ticks. | |
| 281 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI()); | 271 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI()); |
| 282 shared_renderer_state_.SetCompositorFrameOnUI(std::move(child_frame)); | 272 shared_renderer_state_.SetCompositorFrameOnUI(std::move(child_frame)); |
| 283 return true; | 273 return true; |
| 284 } | 274 } |
| 285 | 275 |
| 286 void BrowserViewRenderer::UpdateParentDrawConstraints() { | 276 void BrowserViewRenderer::UpdateParentDrawConstraints() { |
| 287 PostInvalidateWithFallback(); | 277 PostInvalidate(); |
| 288 ParentCompositorDrawConstraints parent_draw_constraints = | 278 ParentCompositorDrawConstraints parent_draw_constraints = |
| 289 shared_renderer_state_.GetParentDrawConstraintsOnUI(); | 279 shared_renderer_state_.GetParentDrawConstraintsOnUI(); |
| 290 client_->ParentDrawConstraintsUpdated(parent_draw_constraints); | 280 client_->ParentDrawConstraintsUpdated(parent_draw_constraints); |
| 291 } | 281 } |
| 292 | 282 |
| 293 void BrowserViewRenderer::ReturnUnusedResource( | 283 void BrowserViewRenderer::ReturnUnusedResource( |
| 294 scoped_ptr<ChildFrame> child_frame) { | 284 scoped_ptr<ChildFrame> child_frame) { |
| 295 if (!child_frame.get() || !child_frame->frame.get()) | 285 if (!child_frame.get() || !child_frame->frame.get()) |
| 296 return; | 286 return; |
| 297 | 287 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 | 350 |
| 361 void BrowserViewRenderer::ClearView() { | 351 void BrowserViewRenderer::ClearView() { |
| 362 TRACE_EVENT_INSTANT0("android_webview", | 352 TRACE_EVENT_INSTANT0("android_webview", |
| 363 "BrowserViewRenderer::ClearView", | 353 "BrowserViewRenderer::ClearView", |
| 364 TRACE_EVENT_SCOPE_THREAD); | 354 TRACE_EVENT_SCOPE_THREAD); |
| 365 if (clear_view_) | 355 if (clear_view_) |
| 366 return; | 356 return; |
| 367 | 357 |
| 368 clear_view_ = true; | 358 clear_view_ = true; |
| 369 // Always invalidate ignoring the compositor to actually clear the webview. | 359 // Always invalidate ignoring the compositor to actually clear the webview. |
| 370 PostInvalidateWithFallback(); | 360 PostInvalidate(); |
| 371 } | 361 } |
| 372 | 362 |
| 373 void BrowserViewRenderer::SetOffscreenPreRaster(bool enable) { | 363 void BrowserViewRenderer::SetOffscreenPreRaster(bool enable) { |
| 374 if (offscreen_pre_raster_ != enable && compositor_) | 364 if (offscreen_pre_raster_ != enable && compositor_) |
| 375 UpdateMemoryPolicy(); | 365 UpdateMemoryPolicy(); |
| 376 | 366 |
| 377 offscreen_pre_raster_ = enable; | 367 offscreen_pre_raster_ = enable; |
| 378 } | 368 } |
| 379 | 369 |
| 380 void BrowserViewRenderer::SetIsPaused(bool paused) { | 370 void BrowserViewRenderer::SetIsPaused(bool paused) { |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 693 scaled_overscroll_delta - rounded_overscroll_delta; | 683 scaled_overscroll_delta - rounded_overscroll_delta; |
| 694 gfx::Vector2dF fling_velocity_pixels = | 684 gfx::Vector2dF fling_velocity_pixels = |
| 695 gfx::ScaleVector2d(current_fling_velocity, physical_pixel_scale); | 685 gfx::ScaleVector2d(current_fling_velocity, physical_pixel_scale); |
| 696 | 686 |
| 697 client_->DidOverscroll(rounded_overscroll_delta, fling_velocity_pixels); | 687 client_->DidOverscroll(rounded_overscroll_delta, fling_velocity_pixels); |
| 698 } | 688 } |
| 699 | 689 |
| 700 void BrowserViewRenderer::PostInvalidate() { | 690 void BrowserViewRenderer::PostInvalidate() { |
| 701 TRACE_EVENT_INSTANT0("android_webview", "BrowserViewRenderer::PostInvalidate", | 691 TRACE_EVENT_INSTANT0("android_webview", "BrowserViewRenderer::PostInvalidate", |
| 702 TRACE_EVENT_SCOPE_THREAD); | 692 TRACE_EVENT_SCOPE_THREAD); |
| 703 PostInvalidateWithFallback(); | |
| 704 } | |
| 705 | |
| 706 void BrowserViewRenderer::PostInvalidateWithFallback() { | |
| 707 // Always call view invalidate. We rely the Android framework to ignore the | |
| 708 // invalidate when it's not needed such as when view is not visible. | |
| 709 client_->PostInvalidate(); | 693 client_->PostInvalidate(); |
| 710 | |
| 711 // Stop fallback ticks when one of these is true. | |
| 712 // 1) Webview is paused. Also need to check we are not in clear view since | |
| 713 // paused, offscreen still expect clear view to recover. | |
| 714 // 2) If we are attached to window and the window is not visible (eg when | |
| 715 // app is in the background). We are sure in this case the webview is used | |
| 716 // "on-screen" but that updates are not needed when in the background. | |
| 717 bool throttle_fallback_tick = | |
| 718 (is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_); | |
|
boliu
2016/03/11 17:43:09
So, according to IsClientVisible, visible = !is_pa
| |
| 719 | |
| 720 if (throttle_fallback_tick || fallback_tick_pending_) | |
| 721 return; | |
| 722 | |
| 723 DCHECK(post_fallback_tick_.IsCancelled()); | |
| 724 DCHECK(fallback_tick_fired_.IsCancelled()); | |
| 725 | |
| 726 post_fallback_tick_.Reset(base::Bind(&BrowserViewRenderer::PostFallbackTick, | |
| 727 base::Unretained(this))); | |
| 728 ui_task_runner_->PostTask(FROM_HERE, post_fallback_tick_.callback()); | |
| 729 fallback_tick_pending_ = true; | |
| 730 } | |
| 731 | |
| 732 void BrowserViewRenderer::CancelFallbackTick() { | |
| 733 post_fallback_tick_.Cancel(); | |
| 734 fallback_tick_fired_.Cancel(); | |
| 735 fallback_tick_pending_ = false; | |
| 736 } | |
| 737 | |
| 738 void BrowserViewRenderer::PostFallbackTick() { | |
| 739 DCHECK(fallback_tick_fired_.IsCancelled()); | |
| 740 TRACE_EVENT0("android_webview", "BrowserViewRenderer::PostFallbackTick"); | |
| 741 post_fallback_tick_.Cancel(); | |
| 742 fallback_tick_fired_.Reset(base::Bind(&BrowserViewRenderer::FallbackTickFired, | |
| 743 base::Unretained(this))); | |
| 744 ui_task_runner_->PostDelayedTask( | |
| 745 FROM_HERE, fallback_tick_fired_.callback(), | |
| 746 base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds)); | |
| 747 } | |
| 748 | |
| 749 void BrowserViewRenderer::FallbackTickFired() { | |
| 750 TRACE_EVENT0("android_webview", "BrowserViewRenderer::FallbackTickFired"); | |
| 751 // This should only be called if OnDraw or DrawGL did not come in time, which | |
| 752 // means fallback_tick_pending_ must still be true. | |
| 753 DCHECK(fallback_tick_pending_); | |
| 754 fallback_tick_fired_.Cancel(); | |
| 755 fallback_tick_pending_ = false; | |
| 756 if (compositor_) { | |
| 757 if (hardware_enabled_ && !size_.IsEmpty()) { | |
| 758 CompositeHw(); | |
| 759 } else { | |
| 760 ForceFakeCompositeSW(); | |
| 761 } | |
| 762 } | |
| 763 } | |
| 764 | |
| 765 void BrowserViewRenderer::ForceFakeCompositeSW() { | |
| 766 DCHECK(compositor_); | |
| 767 SkBitmap bitmap; | |
| 768 bitmap.allocN32Pixels(1, 1); | |
| 769 bitmap.eraseColor(0); | |
| 770 SkCanvas canvas(bitmap); | |
| 771 CompositeSW(&canvas); | |
| 772 } | 694 } |
| 773 | 695 |
| 774 bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) { | 696 bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) { |
| 775 DCHECK(compositor_); | 697 DCHECK(compositor_); |
| 776 CancelFallbackTick(); | |
| 777 ReturnResourceFromParent(); | 698 ReturnResourceFromParent(); |
| 778 return compositor_->DemandDrawSw(canvas); | 699 return compositor_->DemandDrawSw(canvas); |
| 779 } | 700 } |
| 780 | 701 |
| 781 void BrowserViewRenderer::UpdateCompositorIsActive() { | 702 void BrowserViewRenderer::UpdateCompositorIsActive() { |
| 782 if (compositor_) { | 703 if (compositor_) { |
| 783 compositor_->SetIsActive(!is_paused_ && | 704 compositor_->SetIsActive(!is_paused_ && |
| 784 (!attached_to_window_ || window_visible_)); | 705 (!attached_to_window_ || window_visible_)); |
| 785 } | 706 } |
| 786 } | 707 } |
| 787 | 708 |
| 788 std::string BrowserViewRenderer::ToString() const { | 709 std::string BrowserViewRenderer::ToString() const { |
| 789 std::string str; | 710 std::string str; |
| 790 base::StringAppendF(&str, "is_paused: %d ", is_paused_); | 711 base::StringAppendF(&str, "is_paused: %d ", is_paused_); |
| 791 base::StringAppendF(&str, "view_visible: %d ", view_visible_); | 712 base::StringAppendF(&str, "view_visible: %d ", view_visible_); |
| 792 base::StringAppendF(&str, "window_visible: %d ", window_visible_); | 713 base::StringAppendF(&str, "window_visible: %d ", window_visible_); |
| 793 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_); | 714 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_); |
| 794 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_); | 715 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_); |
| 795 base::StringAppendF(&str, "fallback_tick_pending: %d ", | |
| 796 fallback_tick_pending_); | |
| 797 base::StringAppendF(&str, "view size: %s ", size_.ToString().c_str()); | 716 base::StringAppendF(&str, "view size: %s ", size_.ToString().c_str()); |
| 798 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_); | 717 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_); |
| 799 base::StringAppendF(&str, | 718 base::StringAppendF(&str, |
| 800 "global visible rect: %s ", | 719 "global visible rect: %s ", |
| 801 last_on_draw_global_visible_rect_.ToString().c_str()); | 720 last_on_draw_global_visible_rect_.ToString().c_str()); |
| 802 base::StringAppendF( | 721 base::StringAppendF( |
| 803 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str()); | 722 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str()); |
| 804 base::StringAppendF(&str, | 723 base::StringAppendF(&str, |
| 805 "overscroll_rounding_error_: %s ", | 724 "overscroll_rounding_error_: %s ", |
| 806 overscroll_rounding_error_.ToString().c_str()); | 725 overscroll_rounding_error_.ToString().c_str()); |
| 807 base::StringAppendF( | 726 base::StringAppendF( |
| 808 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); | 727 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); |
| 809 base::StringAppendF(&str, "clear_view: %d ", clear_view_); | 728 base::StringAppendF(&str, "clear_view: %d ", clear_view_); |
| 810 return str; | 729 return str; |
| 811 } | 730 } |
| 812 | 731 |
| 813 } // namespace android_webview | 732 } // namespace android_webview |
| OLD | NEW |