Roll src/third_party/skia dd4e568:8c9b6f1
[chromium-blink-merge.git] / android_webview / browser / browser_view_renderer.cc
blobb9336d6119a230f9ccef7ac87c39bad71015c35d
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
3 // found in the LICENSE file.
5 #include "android_webview/browser/browser_view_renderer.h"
7 #include "android_webview/browser/browser_view_renderer_client.h"
8 #include "android_webview/browser/child_frame.h"
9 #include "base/auto_reset.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/supports_user_data.h"
15 #include "base/trace_event/trace_event_argument.h"
16 #include "cc/output/compositor_frame.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h"
19 #include "gpu/command_buffer/service/gpu_switches.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "third_party/skia/include/core/SkCanvas.h"
22 #include "third_party/skia/include/core/SkPicture.h"
23 #include "third_party/skia/include/core/SkPictureRecorder.h"
24 #include "ui/gfx/geometry/vector2d_conversions.h"
26 namespace android_webview {
28 namespace {
30 const double kEpsilon = 1e-8;
32 const int64 kFallbackTickTimeoutInMilliseconds = 100;
34 // Used to calculate memory allocation. Determined experimentally.
35 const size_t kMemoryMultiplier = 20;
36 const size_t kBytesPerPixel = 4;
37 const size_t kMemoryAllocationStep = 5 * 1024 * 1024;
38 uint64 g_memory_override_in_bytes = 0u;
40 const void* kBrowserViewRendererUserDataKey = &kBrowserViewRendererUserDataKey;
42 class BrowserViewRendererUserData : public base::SupportsUserData::Data {
43 public:
44 BrowserViewRendererUserData(BrowserViewRenderer* ptr) : bvr_(ptr) {}
46 static BrowserViewRenderer* GetBrowserViewRenderer(
47 content::WebContents* web_contents) {
48 if (!web_contents)
49 return NULL;
50 BrowserViewRendererUserData* data =
51 static_cast<BrowserViewRendererUserData*>(
52 web_contents->GetUserData(kBrowserViewRendererUserDataKey));
53 return data ? data->bvr_ : NULL;
56 private:
57 BrowserViewRenderer* bvr_;
60 } // namespace
62 // static
63 void BrowserViewRenderer::CalculateTileMemoryPolicy() {
64 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
66 // If the value was overridden on the command line, use the specified value.
67 bool client_hard_limit_bytes_overridden =
68 cl->HasSwitch(switches::kForceGpuMemAvailableMb);
69 if (client_hard_limit_bytes_overridden) {
70 base::StringToUint64(
71 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
72 switches::kForceGpuMemAvailableMb),
73 &g_memory_override_in_bytes);
74 g_memory_override_in_bytes *= 1024 * 1024;
78 // static
79 BrowserViewRenderer* BrowserViewRenderer::FromWebContents(
80 content::WebContents* web_contents) {
81 return BrowserViewRendererUserData::GetBrowserViewRenderer(web_contents);
84 BrowserViewRenderer::BrowserViewRenderer(
85 BrowserViewRendererClient* client,
86 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
87 : client_(client),
88 shared_renderer_state_(ui_task_runner, this),
89 ui_task_runner_(ui_task_runner),
90 compositor_(NULL),
91 is_paused_(false),
92 view_visible_(false),
93 window_visible_(false),
94 attached_to_window_(false),
95 hardware_enabled_(false),
96 dip_scale_(0.0),
97 page_scale_factor_(1.0),
98 on_new_picture_enable_(false),
99 clear_view_(false),
100 offscreen_pre_raster_(false),
101 fallback_tick_pending_(false) {
104 BrowserViewRenderer::~BrowserViewRenderer() {
107 void BrowserViewRenderer::RegisterWithWebContents(
108 content::WebContents* web_contents) {
109 web_contents->SetUserData(kBrowserViewRendererUserDataKey,
110 new BrowserViewRendererUserData(this));
113 SharedRendererState* BrowserViewRenderer::GetAwDrawGLViewContext() {
114 return &shared_renderer_state_;
117 bool BrowserViewRenderer::RequestDrawGL(bool wait_for_completion) {
118 return client_->RequestDrawGL(wait_for_completion);
121 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) {
122 DCHECK(ui_task_runner_->BelongsToCurrentThread());
123 // Constants from Android ComponentCallbacks2.
124 enum {
125 TRIM_MEMORY_RUNNING_LOW = 10,
126 TRIM_MEMORY_UI_HIDDEN = 20,
127 TRIM_MEMORY_BACKGROUND = 40,
128 TRIM_MEMORY_MODERATE = 60,
131 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because
132 // it does not indicate memory pressure, but merely that the app is
133 // backgrounded.
134 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN)
135 return;
137 // Do not release resources on view we expect to get DrawGL soon.
138 if (level < TRIM_MEMORY_BACKGROUND && visible)
139 return;
141 // Nothing to drop.
142 if (!compositor_ || !hardware_enabled_)
143 return;
145 TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory");
147 // If offscreen pre-raster is disabled, drop everything in hardware. Otherwise
148 // keep the tiles and just delete the HardwareRenderer.
149 if (level >= TRIM_MEMORY_MODERATE) {
150 if (offscreen_pre_raster_)
151 shared_renderer_state_.DeleteHardwareRendererOnUI();
152 else
153 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI();
154 return;
157 // Just set the memory limit to 0 and drop all tiles. This will be reset to
158 // normal levels in the next DrawGL call.
159 if (!offscreen_pre_raster_)
160 compositor_->SetMemoryPolicy(0u);
163 void BrowserViewRenderer::UpdateMemoryPolicy() {
164 if (!hardware_enabled_) {
165 compositor_->SetMemoryPolicy(0u);
166 return;
169 size_t bytes_limit = 0u;
170 if (g_memory_override_in_bytes) {
171 bytes_limit = static_cast<size_t>(g_memory_override_in_bytes);
172 } else {
173 gfx::Rect interest_rect = offscreen_pre_raster_
174 ? gfx::Rect(size_)
175 : last_on_draw_global_visible_rect_;
176 size_t width = interest_rect.width();
177 size_t height = interest_rect.height();
178 bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height;
179 // Round up to a multiple of kMemoryAllocationStep.
180 bytes_limit =
181 (bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
184 compositor_->SetMemoryPolicy(bytes_limit);
187 void BrowserViewRenderer::PrepareToDraw(const gfx::Vector2d& scroll,
188 const gfx::Rect& global_visible_rect) {
189 last_on_draw_scroll_offset_ = scroll;
190 last_on_draw_global_visible_rect_ = global_visible_rect;
193 bool BrowserViewRenderer::CanOnDraw() {
194 if (!compositor_) {
195 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_NoCompositor",
196 TRACE_EVENT_SCOPE_THREAD);
197 return false;
199 if (clear_view_) {
200 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_ClearView",
201 TRACE_EVENT_SCOPE_THREAD);
202 return false;
205 return true;
208 bool BrowserViewRenderer::OnDrawHardware() {
209 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware");
211 shared_renderer_state_.InitializeHardwareDrawIfNeededOnUI();
213 if (!CanOnDraw()) {
214 return false;
217 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_);
218 hardware_enabled_ = true;
220 return CompositeHw();
223 bool BrowserViewRenderer::CompositeHw() {
224 CancelFallbackTick();
226 ReturnResourceFromParent();
227 UpdateMemoryPolicy();
229 ParentCompositorDrawConstraints parent_draw_constraints =
230 shared_renderer_state_.GetParentDrawConstraintsOnUI();
231 gfx::Size surface_size(size_);
232 gfx::Rect viewport(surface_size);
233 gfx::Rect clip = viewport;
234 gfx::Transform transform_for_tile_priority =
235 parent_draw_constraints.transform;
237 // If the WebView is on a layer, WebView does not know what transform is
238 // applied onto the layer so global visible rect does not make sense here.
239 // In this case, just use the surface rect for tiling.
240 gfx::Rect viewport_rect_for_tile_priority;
242 // Leave viewport_rect_for_tile_priority empty if offscreen_pre_raster_ is on.
243 if (!offscreen_pre_raster_) {
244 if (parent_draw_constraints.is_layer) {
245 viewport_rect_for_tile_priority = parent_draw_constraints.surface_rect;
246 } else {
247 viewport_rect_for_tile_priority = last_on_draw_global_visible_rect_;
251 scoped_ptr<cc::CompositorFrame> frame =
252 compositor_->DemandDrawHw(surface_size,
253 gfx::Transform(),
254 viewport,
255 clip,
256 viewport_rect_for_tile_priority,
257 transform_for_tile_priority);
258 if (!frame.get()) {
259 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame",
260 TRACE_EVENT_SCOPE_THREAD);
261 return false;
264 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(
265 new ChildFrame(frame.Pass(), viewport_rect_for_tile_priority,
266 transform_for_tile_priority, offscreen_pre_raster_,
267 parent_draw_constraints.is_layer));
269 // Uncommitted frame can happen with consecutive fallback ticks.
270 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI());
271 shared_renderer_state_.SetCompositorFrameOnUI(child_frame.Pass());
272 return true;
275 void BrowserViewRenderer::UpdateParentDrawConstraints() {
276 PostInvalidateWithFallback();
277 ParentCompositorDrawConstraints parent_draw_constraints =
278 shared_renderer_state_.GetParentDrawConstraintsOnUI();
279 client_->ParentDrawConstraintsUpdated(parent_draw_constraints);
282 void BrowserViewRenderer::ReturnUnusedResource(
283 scoped_ptr<ChildFrame> child_frame) {
284 if (!child_frame.get() || !child_frame->frame.get())
285 return;
287 cc::CompositorFrameAck frame_ack;
288 cc::TransferableResource::ReturnResources(
289 child_frame->frame->delegated_frame_data->resource_list,
290 &frame_ack.resources);
291 if (compositor_ && !frame_ack.resources.empty())
292 compositor_->ReturnResources(frame_ack);
295 void BrowserViewRenderer::ReturnResourceFromParent() {
296 cc::CompositorFrameAck frame_ack;
297 shared_renderer_state_.SwapReturnedResourcesOnUI(&frame_ack.resources);
298 if (compositor_ && !frame_ack.resources.empty()) {
299 compositor_->ReturnResources(frame_ack);
303 void BrowserViewRenderer::DetachFunctorFromView() {
304 client_->DetachFunctorFromView();
307 bool BrowserViewRenderer::OnDrawSoftware(SkCanvas* canvas) {
308 return CanOnDraw() && CompositeSW(canvas);
311 skia::RefPtr<SkPicture> BrowserViewRenderer::CapturePicture(int width,
312 int height) {
313 TRACE_EVENT0("android_webview", "BrowserViewRenderer::CapturePicture");
315 // Return empty Picture objects for empty SkPictures.
316 if (width <= 0 || height <= 0) {
317 SkPictureRecorder emptyRecorder;
318 emptyRecorder.beginRecording(0, 0);
319 return skia::AdoptRef(emptyRecorder.endRecording());
322 // Reset scroll back to the origin, will go back to the old
323 // value when scroll_reset is out of scope.
324 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_dip_,
325 gfx::Vector2dF());
327 SkPictureRecorder recorder;
328 SkCanvas* rec_canvas = recorder.beginRecording(width, height, NULL, 0);
329 if (compositor_)
330 CompositeSW(rec_canvas);
331 return skia::AdoptRef(recorder.endRecording());
334 void BrowserViewRenderer::EnableOnNewPicture(bool enabled) {
335 on_new_picture_enable_ = enabled;
338 void BrowserViewRenderer::ClearView() {
339 TRACE_EVENT_INSTANT0("android_webview",
340 "BrowserViewRenderer::ClearView",
341 TRACE_EVENT_SCOPE_THREAD);
342 if (clear_view_)
343 return;
345 clear_view_ = true;
346 // Always invalidate ignoring the compositor to actually clear the webview.
347 PostInvalidateWithFallback();
350 void BrowserViewRenderer::SetOffscreenPreRaster(bool enable) {
351 if (offscreen_pre_raster_ != enable && compositor_)
352 UpdateMemoryPolicy();
354 offscreen_pre_raster_ = enable;
357 void BrowserViewRenderer::SetIsPaused(bool paused) {
358 TRACE_EVENT_INSTANT1("android_webview",
359 "BrowserViewRenderer::SetIsPaused",
360 TRACE_EVENT_SCOPE_THREAD,
361 "paused",
362 paused);
363 is_paused_ = paused;
364 UpdateCompositorIsActive();
367 void BrowserViewRenderer::SetViewVisibility(bool view_visible) {
368 TRACE_EVENT_INSTANT1("android_webview",
369 "BrowserViewRenderer::SetViewVisibility",
370 TRACE_EVENT_SCOPE_THREAD,
371 "view_visible",
372 view_visible);
373 view_visible_ = view_visible;
376 void BrowserViewRenderer::SetWindowVisibility(bool window_visible) {
377 TRACE_EVENT_INSTANT1("android_webview",
378 "BrowserViewRenderer::SetWindowVisibility",
379 TRACE_EVENT_SCOPE_THREAD,
380 "window_visible",
381 window_visible);
382 window_visible_ = window_visible;
383 UpdateCompositorIsActive();
386 void BrowserViewRenderer::OnSizeChanged(int width, int height) {
387 TRACE_EVENT_INSTANT2("android_webview",
388 "BrowserViewRenderer::OnSizeChanged",
389 TRACE_EVENT_SCOPE_THREAD,
390 "width",
391 width,
392 "height",
393 height);
394 size_.SetSize(width, height);
397 void BrowserViewRenderer::OnAttachedToWindow(int width, int height) {
398 TRACE_EVENT2("android_webview",
399 "BrowserViewRenderer::OnAttachedToWindow",
400 "width",
401 width,
402 "height",
403 height);
404 attached_to_window_ = true;
405 size_.SetSize(width, height);
406 UpdateCompositorIsActive();
409 void BrowserViewRenderer::OnDetachedFromWindow() {
410 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
411 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI();
412 attached_to_window_ = false;
413 DCHECK(!hardware_enabled_);
414 UpdateCompositorIsActive();
417 void BrowserViewRenderer::ReleaseHardware() {
418 DCHECK(hardware_enabled_);
419 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI());
420 ReturnResourceFromParent();
421 DCHECK(shared_renderer_state_.ReturnedResourcesEmptyOnUI());
423 if (compositor_) {
424 compositor_->SetMemoryPolicy(0u);
427 hardware_enabled_ = false;
430 bool BrowserViewRenderer::IsVisible() const {
431 // Ignore |window_visible_| if |attached_to_window_| is false.
432 return view_visible_ && (!attached_to_window_ || window_visible_);
435 gfx::Rect BrowserViewRenderer::GetScreenRect() const {
436 return gfx::Rect(client_->GetLocationOnScreen(), size_);
439 void BrowserViewRenderer::DidInitializeCompositor(
440 content::SynchronousCompositor* compositor) {
441 TRACE_EVENT0("android_webview",
442 "BrowserViewRenderer::DidInitializeCompositor");
443 DCHECK(compositor);
444 DCHECK(!compositor_);
445 compositor_ = compositor;
446 UpdateCompositorIsActive();
449 void BrowserViewRenderer::DidDestroyCompositor(
450 content::SynchronousCompositor* compositor) {
451 TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor");
452 DCHECK(compositor_);
453 compositor_ = NULL;
456 void BrowserViewRenderer::SetDipScale(float dip_scale) {
457 dip_scale_ = dip_scale;
458 CHECK_GT(dip_scale_, 0.f);
461 gfx::Vector2d BrowserViewRenderer::max_scroll_offset() const {
462 DCHECK_GT(dip_scale_, 0.f);
463 return gfx::ToCeiledVector2d(gfx::ScaleVector2d(
464 max_scroll_offset_dip_, dip_scale_ * page_scale_factor_));
467 void BrowserViewRenderer::ScrollTo(gfx::Vector2d scroll_offset) {
468 gfx::Vector2d max_offset = max_scroll_offset();
469 gfx::Vector2dF scroll_offset_dip;
470 // To preserve the invariant that scrolling to the maximum physical pixel
471 // value also scrolls to the maximum dip pixel value we transform the physical
472 // offset into the dip offset by using a proportion (instead of dividing by
473 // dip_scale * page_scale_factor).
474 if (max_offset.x()) {
475 scroll_offset_dip.set_x((scroll_offset.x() * max_scroll_offset_dip_.x()) /
476 max_offset.x());
478 if (max_offset.y()) {
479 scroll_offset_dip.set_y((scroll_offset.y() * max_scroll_offset_dip_.y()) /
480 max_offset.y());
483 DCHECK_LE(0.f, scroll_offset_dip.x());
484 DCHECK_LE(0.f, scroll_offset_dip.y());
485 DCHECK(scroll_offset_dip.x() < max_scroll_offset_dip_.x() ||
486 scroll_offset_dip.x() - max_scroll_offset_dip_.x() < kEpsilon)
487 << scroll_offset_dip.x() << " " << max_scroll_offset_dip_.x();
488 DCHECK(scroll_offset_dip.y() < max_scroll_offset_dip_.y() ||
489 scroll_offset_dip.y() - max_scroll_offset_dip_.y() < kEpsilon)
490 << scroll_offset_dip.y() << " " << max_scroll_offset_dip_.y();
492 if (scroll_offset_dip_ == scroll_offset_dip)
493 return;
495 scroll_offset_dip_ = scroll_offset_dip;
497 TRACE_EVENT_INSTANT2("android_webview",
498 "BrowserViewRenderer::ScrollTo",
499 TRACE_EVENT_SCOPE_THREAD,
500 "x",
501 scroll_offset_dip.x(),
502 "y",
503 scroll_offset_dip.y());
505 if (compositor_)
506 compositor_->DidChangeRootLayerScrollOffset();
509 void BrowserViewRenderer::DidUpdateContent() {
510 TRACE_EVENT_INSTANT0("android_webview",
511 "BrowserViewRenderer::DidUpdateContent",
512 TRACE_EVENT_SCOPE_THREAD);
513 clear_view_ = false;
514 if (on_new_picture_enable_)
515 client_->OnNewPicture();
518 void BrowserViewRenderer::SetTotalRootLayerScrollOffset(
519 gfx::Vector2dF scroll_offset_dip) {
520 // TOOD(mkosiba): Add a DCHECK to say that this does _not_ get called during
521 // DrawGl when http://crbug.com/249972 is fixed.
522 if (scroll_offset_dip_ == scroll_offset_dip)
523 return;
525 scroll_offset_dip_ = scroll_offset_dip;
527 gfx::Vector2d max_offset = max_scroll_offset();
528 gfx::Vector2d scroll_offset;
529 // For an explanation as to why this is done this way see the comment in
530 // BrowserViewRenderer::ScrollTo.
531 if (max_scroll_offset_dip_.x()) {
532 scroll_offset.set_x((scroll_offset_dip.x() * max_offset.x()) /
533 max_scroll_offset_dip_.x());
536 if (max_scroll_offset_dip_.y()) {
537 scroll_offset.set_y((scroll_offset_dip.y() * max_offset.y()) /
538 max_scroll_offset_dip_.y());
541 DCHECK_LE(0, scroll_offset.x());
542 DCHECK_LE(0, scroll_offset.y());
543 DCHECK_LE(scroll_offset.x(), max_offset.x());
544 DCHECK_LE(scroll_offset.y(), max_offset.y());
546 client_->ScrollContainerViewTo(scroll_offset);
549 gfx::Vector2dF BrowserViewRenderer::GetTotalRootLayerScrollOffset() {
550 return scroll_offset_dip_;
553 bool BrowserViewRenderer::IsExternalFlingActive() const {
554 return client_->IsFlingActive();
557 void BrowserViewRenderer::UpdateRootLayerState(
558 const gfx::Vector2dF& total_scroll_offset_dip,
559 const gfx::Vector2dF& max_scroll_offset_dip,
560 const gfx::SizeF& scrollable_size_dip,
561 float page_scale_factor,
562 float min_page_scale_factor,
563 float max_page_scale_factor) {
564 TRACE_EVENT_INSTANT1(
565 "android_webview",
566 "BrowserViewRenderer::UpdateRootLayerState",
567 TRACE_EVENT_SCOPE_THREAD,
568 "state",
569 RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip));
571 DCHECK_GT(dip_scale_, 0.f);
573 max_scroll_offset_dip_ = max_scroll_offset_dip;
574 DCHECK_LE(0.f, max_scroll_offset_dip_.x());
575 DCHECK_LE(0.f, max_scroll_offset_dip_.y());
577 page_scale_factor_ = page_scale_factor;
578 DCHECK_GT(page_scale_factor_, 0.f);
580 client_->UpdateScrollState(max_scroll_offset(),
581 scrollable_size_dip,
582 page_scale_factor,
583 min_page_scale_factor,
584 max_page_scale_factor);
585 SetTotalRootLayerScrollOffset(total_scroll_offset_dip);
588 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
589 BrowserViewRenderer::RootLayerStateAsValue(
590 const gfx::Vector2dF& total_scroll_offset_dip,
591 const gfx::SizeF& scrollable_size_dip) {
592 scoped_refptr<base::trace_event::TracedValue> state =
593 new base::trace_event::TracedValue();
595 state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x());
596 state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y());
598 state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x());
599 state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y());
601 state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width());
602 state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height());
604 state->SetDouble("page_scale_factor", page_scale_factor_);
605 return state;
608 void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll,
609 gfx::Vector2dF latest_overscroll_delta,
610 gfx::Vector2dF current_fling_velocity) {
611 const float physical_pixel_scale = dip_scale_ * page_scale_factor_;
612 if (accumulated_overscroll == latest_overscroll_delta)
613 overscroll_rounding_error_ = gfx::Vector2dF();
614 gfx::Vector2dF scaled_overscroll_delta =
615 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale);
616 gfx::Vector2d rounded_overscroll_delta = gfx::ToRoundedVector2d(
617 scaled_overscroll_delta + overscroll_rounding_error_);
618 overscroll_rounding_error_ =
619 scaled_overscroll_delta - rounded_overscroll_delta;
620 client_->DidOverscroll(rounded_overscroll_delta);
623 void BrowserViewRenderer::PostInvalidate() {
624 TRACE_EVENT_INSTANT0("android_webview", "BrowserViewRenderer::PostInvalidate",
625 TRACE_EVENT_SCOPE_THREAD);
626 PostInvalidateWithFallback();
629 void BrowserViewRenderer::PostInvalidateWithFallback() {
630 // Always call view invalidate. We rely the Android framework to ignore the
631 // invalidate when it's not needed such as when view is not visible.
632 client_->PostInvalidate();
634 // Stop fallback ticks when one of these is true.
635 // 1) Webview is paused. Also need to check we are not in clear view since
636 // paused, offscreen still expect clear view to recover.
637 // 2) If we are attached to window and the window is not visible (eg when
638 // app is in the background). We are sure in this case the webview is used
639 // "on-screen" but that updates are not needed when in the background.
640 bool throttle_fallback_tick =
641 (is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_);
643 if (throttle_fallback_tick || fallback_tick_pending_)
644 return;
646 DCHECK(post_fallback_tick_.IsCancelled());
647 DCHECK(fallback_tick_fired_.IsCancelled());
649 post_fallback_tick_.Reset(base::Bind(&BrowserViewRenderer::PostFallbackTick,
650 base::Unretained(this)));
651 ui_task_runner_->PostTask(FROM_HERE, post_fallback_tick_.callback());
652 fallback_tick_pending_ = true;
655 void BrowserViewRenderer::CancelFallbackTick() {
656 post_fallback_tick_.Cancel();
657 fallback_tick_fired_.Cancel();
658 fallback_tick_pending_ = false;
661 void BrowserViewRenderer::PostFallbackTick() {
662 DCHECK(fallback_tick_fired_.IsCancelled());
663 TRACE_EVENT0("android_webview", "BrowserViewRenderer::PostFallbackTick");
664 post_fallback_tick_.Cancel();
665 fallback_tick_fired_.Reset(base::Bind(&BrowserViewRenderer::FallbackTickFired,
666 base::Unretained(this)));
667 ui_task_runner_->PostDelayedTask(
668 FROM_HERE, fallback_tick_fired_.callback(),
669 base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds));
672 void BrowserViewRenderer::FallbackTickFired() {
673 TRACE_EVENT0("android_webview", "BrowserViewRenderer::FallbackTickFired");
674 // This should only be called if OnDraw or DrawGL did not come in time, which
675 // means fallback_tick_pending_ must still be true.
676 DCHECK(fallback_tick_pending_);
677 fallback_tick_fired_.Cancel();
678 fallback_tick_pending_ = false;
679 if (compositor_) {
680 if (hardware_enabled_) {
681 CompositeHw();
682 } else {
683 ForceFakeCompositeSW();
688 void BrowserViewRenderer::ForceFakeCompositeSW() {
689 DCHECK(compositor_);
690 SkBitmap bitmap;
691 bitmap.allocN32Pixels(1, 1);
692 bitmap.eraseColor(0);
693 SkCanvas canvas(bitmap);
694 CompositeSW(&canvas);
697 bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) {
698 DCHECK(compositor_);
699 CancelFallbackTick();
700 ReturnResourceFromParent();
701 return compositor_->DemandDrawSw(canvas);
704 void BrowserViewRenderer::UpdateCompositorIsActive() {
705 if (compositor_)
706 compositor_->SetIsActive(!is_paused_ &&
707 (!attached_to_window_ || window_visible_));
710 std::string BrowserViewRenderer::ToString() const {
711 std::string str;
712 base::StringAppendF(&str, "is_paused: %d ", is_paused_);
713 base::StringAppendF(&str, "view_visible: %d ", view_visible_);
714 base::StringAppendF(&str, "window_visible: %d ", window_visible_);
715 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_);
716 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_);
717 base::StringAppendF(&str, "fallback_tick_pending: %d ",
718 fallback_tick_pending_);
719 base::StringAppendF(&str, "view size: %s ", size_.ToString().c_str());
720 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_);
721 base::StringAppendF(&str,
722 "global visible rect: %s ",
723 last_on_draw_global_visible_rect_.ToString().c_str());
724 base::StringAppendF(
725 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str());
726 base::StringAppendF(&str,
727 "overscroll_rounding_error_: %s ",
728 overscroll_rounding_error_.ToString().c_str());
729 base::StringAppendF(
730 &str, "on_new_picture_enable: %d ", on_new_picture_enable_);
731 base::StringAppendF(&str, "clear_view: %d ", clear_view_);
732 return str;
735 } // namespace android_webview