Update V8 to version 4.5.107.
[chromium-blink-merge.git] / android_webview / browser / browser_view_renderer.cc
blob7d15b89830be568129146fc0569c94a0479765cd
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 ParentCompositorDrawConstraints parent_draw_constraints =
174 shared_renderer_state_.GetParentDrawConstraintsOnUI();
175 gfx::Rect interest_rect =
176 offscreen_pre_raster_ || parent_draw_constraints.is_layer
177 ? gfx::Rect(size_)
178 : last_on_draw_global_visible_rect_;
179 size_t width = interest_rect.width();
180 size_t height = interest_rect.height();
181 bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height;
182 // Round up to a multiple of kMemoryAllocationStep.
183 bytes_limit =
184 (bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
187 compositor_->SetMemoryPolicy(bytes_limit);
190 void BrowserViewRenderer::PrepareToDraw(const gfx::Vector2d& scroll,
191 const gfx::Rect& global_visible_rect) {
192 last_on_draw_scroll_offset_ = scroll;
193 last_on_draw_global_visible_rect_ = global_visible_rect;
196 bool BrowserViewRenderer::CanOnDraw() {
197 if (!compositor_) {
198 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_NoCompositor",
199 TRACE_EVENT_SCOPE_THREAD);
200 return false;
202 if (clear_view_) {
203 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_ClearView",
204 TRACE_EVENT_SCOPE_THREAD);
205 return false;
208 return true;
211 bool BrowserViewRenderer::OnDrawHardware() {
212 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware");
214 shared_renderer_state_.InitializeHardwareDrawIfNeededOnUI();
216 if (!CanOnDraw()) {
217 return false;
220 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_);
221 hardware_enabled_ = true;
223 return CompositeHw();
226 bool BrowserViewRenderer::CompositeHw() {
227 CancelFallbackTick();
229 ReturnResourceFromParent();
230 UpdateMemoryPolicy();
232 ParentCompositorDrawConstraints parent_draw_constraints =
233 shared_renderer_state_.GetParentDrawConstraintsOnUI();
234 gfx::Size surface_size(size_);
235 gfx::Rect viewport(surface_size);
236 gfx::Rect clip = viewport;
237 gfx::Transform transform_for_tile_priority =
238 parent_draw_constraints.transform;
240 // If the WebView is on a layer, WebView does not know what transform is
241 // applied onto the layer so global visible rect does not make sense here.
242 // In this case, just use the surface rect for tiling.
243 gfx::Rect viewport_rect_for_tile_priority;
245 // Leave viewport_rect_for_tile_priority empty if offscreen_pre_raster_ is on.
246 if (!offscreen_pre_raster_ && !parent_draw_constraints.is_layer) {
247 viewport_rect_for_tile_priority = last_on_draw_global_visible_rect_;
250 scoped_ptr<cc::CompositorFrame> frame =
251 compositor_->DemandDrawHw(surface_size,
252 gfx::Transform(),
253 viewport,
254 clip,
255 viewport_rect_for_tile_priority,
256 transform_for_tile_priority);
257 if (!frame.get()) {
258 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame",
259 TRACE_EVENT_SCOPE_THREAD);
260 return false;
263 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(
264 new ChildFrame(frame.Pass(), viewport_rect_for_tile_priority.IsEmpty(),
265 transform_for_tile_priority, offscreen_pre_raster_,
266 parent_draw_constraints.is_layer));
268 // Uncommitted frame can happen with consecutive fallback ticks.
269 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI());
270 shared_renderer_state_.SetCompositorFrameOnUI(child_frame.Pass());
271 return true;
274 void BrowserViewRenderer::UpdateParentDrawConstraints() {
275 PostInvalidateWithFallback();
276 ParentCompositorDrawConstraints parent_draw_constraints =
277 shared_renderer_state_.GetParentDrawConstraintsOnUI();
278 client_->ParentDrawConstraintsUpdated(parent_draw_constraints);
281 void BrowserViewRenderer::ReturnUnusedResource(
282 scoped_ptr<ChildFrame> child_frame) {
283 if (!child_frame.get() || !child_frame->frame.get())
284 return;
286 cc::CompositorFrameAck frame_ack;
287 cc::TransferableResource::ReturnResources(
288 child_frame->frame->delegated_frame_data->resource_list,
289 &frame_ack.resources);
290 if (compositor_ && !frame_ack.resources.empty())
291 compositor_->ReturnResources(frame_ack);
294 void BrowserViewRenderer::ReturnResourceFromParent() {
295 cc::CompositorFrameAck frame_ack;
296 shared_renderer_state_.SwapReturnedResourcesOnUI(&frame_ack.resources);
297 if (compositor_ && !frame_ack.resources.empty()) {
298 compositor_->ReturnResources(frame_ack);
302 void BrowserViewRenderer::DetachFunctorFromView() {
303 client_->DetachFunctorFromView();
306 bool BrowserViewRenderer::OnDrawSoftware(SkCanvas* canvas) {
307 return CanOnDraw() && CompositeSW(canvas);
310 skia::RefPtr<SkPicture> BrowserViewRenderer::CapturePicture(int width,
311 int height) {
312 TRACE_EVENT0("android_webview", "BrowserViewRenderer::CapturePicture");
314 // Return empty Picture objects for empty SkPictures.
315 if (width <= 0 || height <= 0) {
316 SkPictureRecorder emptyRecorder;
317 emptyRecorder.beginRecording(0, 0);
318 return skia::AdoptRef(emptyRecorder.endRecording());
321 // Reset scroll back to the origin, will go back to the old
322 // value when scroll_reset is out of scope.
323 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_dip_,
324 gfx::Vector2dF());
326 SkPictureRecorder recorder;
327 SkCanvas* rec_canvas = recorder.beginRecording(width, height, NULL, 0);
328 if (compositor_)
329 CompositeSW(rec_canvas);
330 return skia::AdoptRef(recorder.endRecording());
333 void BrowserViewRenderer::EnableOnNewPicture(bool enabled) {
334 on_new_picture_enable_ = enabled;
337 void BrowserViewRenderer::ClearView() {
338 TRACE_EVENT_INSTANT0("android_webview",
339 "BrowserViewRenderer::ClearView",
340 TRACE_EVENT_SCOPE_THREAD);
341 if (clear_view_)
342 return;
344 clear_view_ = true;
345 // Always invalidate ignoring the compositor to actually clear the webview.
346 PostInvalidateWithFallback();
349 void BrowserViewRenderer::SetOffscreenPreRaster(bool enable) {
350 if (offscreen_pre_raster_ != enable && compositor_)
351 UpdateMemoryPolicy();
353 offscreen_pre_raster_ = enable;
356 void BrowserViewRenderer::SetIsPaused(bool paused) {
357 TRACE_EVENT_INSTANT1("android_webview",
358 "BrowserViewRenderer::SetIsPaused",
359 TRACE_EVENT_SCOPE_THREAD,
360 "paused",
361 paused);
362 is_paused_ = paused;
363 UpdateCompositorIsActive();
366 void BrowserViewRenderer::SetViewVisibility(bool view_visible) {
367 TRACE_EVENT_INSTANT1("android_webview",
368 "BrowserViewRenderer::SetViewVisibility",
369 TRACE_EVENT_SCOPE_THREAD,
370 "view_visible",
371 view_visible);
372 view_visible_ = view_visible;
375 void BrowserViewRenderer::SetWindowVisibility(bool window_visible) {
376 TRACE_EVENT_INSTANT1("android_webview",
377 "BrowserViewRenderer::SetWindowVisibility",
378 TRACE_EVENT_SCOPE_THREAD,
379 "window_visible",
380 window_visible);
381 window_visible_ = window_visible;
382 UpdateCompositorIsActive();
385 void BrowserViewRenderer::OnSizeChanged(int width, int height) {
386 TRACE_EVENT_INSTANT2("android_webview",
387 "BrowserViewRenderer::OnSizeChanged",
388 TRACE_EVENT_SCOPE_THREAD,
389 "width",
390 width,
391 "height",
392 height);
393 size_.SetSize(width, height);
396 void BrowserViewRenderer::OnAttachedToWindow(int width, int height) {
397 TRACE_EVENT2("android_webview",
398 "BrowserViewRenderer::OnAttachedToWindow",
399 "width",
400 width,
401 "height",
402 height);
403 attached_to_window_ = true;
404 size_.SetSize(width, height);
405 UpdateCompositorIsActive();
408 void BrowserViewRenderer::OnDetachedFromWindow() {
409 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
410 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI();
411 attached_to_window_ = false;
412 DCHECK(!hardware_enabled_);
413 UpdateCompositorIsActive();
416 void BrowserViewRenderer::OnComputeScroll(base::TimeTicks animation_time) {
417 if (pending_fling_animation_.is_null())
418 return;
419 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnComputeScroll");
420 DCHECK(!pending_fling_animation_.is_null());
421 AnimationCallback animation = pending_fling_animation_;
422 pending_fling_animation_.Reset();
423 animation.Run(animation_time);
426 void BrowserViewRenderer::ReleaseHardware() {
427 DCHECK(hardware_enabled_);
428 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI());
429 ReturnResourceFromParent();
430 DCHECK(shared_renderer_state_.ReturnedResourcesEmptyOnUI());
432 if (compositor_) {
433 compositor_->SetMemoryPolicy(0u);
436 hardware_enabled_ = false;
439 bool BrowserViewRenderer::IsVisible() const {
440 // Ignore |window_visible_| if |attached_to_window_| is false.
441 return view_visible_ && (!attached_to_window_ || window_visible_);
444 gfx::Rect BrowserViewRenderer::GetScreenRect() const {
445 return gfx::Rect(client_->GetLocationOnScreen(), size_);
448 void BrowserViewRenderer::DidInitializeCompositor(
449 content::SynchronousCompositor* compositor) {
450 TRACE_EVENT0("android_webview",
451 "BrowserViewRenderer::DidInitializeCompositor");
452 DCHECK(compositor);
453 DCHECK(!compositor_);
454 compositor_ = compositor;
455 UpdateCompositorIsActive();
458 void BrowserViewRenderer::DidDestroyCompositor(
459 content::SynchronousCompositor* compositor) {
460 TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor");
461 DCHECK(compositor_);
462 compositor_->SetIsActive(false);
463 compositor_ = NULL;
466 void BrowserViewRenderer::SetDipScale(float dip_scale) {
467 dip_scale_ = dip_scale;
468 CHECK_GT(dip_scale_, 0.f);
471 gfx::Vector2d BrowserViewRenderer::max_scroll_offset() const {
472 DCHECK_GT(dip_scale_, 0.f);
473 return gfx::ToCeiledVector2d(gfx::ScaleVector2d(
474 max_scroll_offset_dip_, dip_scale_ * page_scale_factor_));
477 void BrowserViewRenderer::ScrollTo(gfx::Vector2d scroll_offset) {
478 gfx::Vector2d max_offset = max_scroll_offset();
479 gfx::Vector2dF scroll_offset_dip;
480 // To preserve the invariant that scrolling to the maximum physical pixel
481 // value also scrolls to the maximum dip pixel value we transform the physical
482 // offset into the dip offset by using a proportion (instead of dividing by
483 // dip_scale * page_scale_factor).
484 if (max_offset.x()) {
485 scroll_offset_dip.set_x((scroll_offset.x() * max_scroll_offset_dip_.x()) /
486 max_offset.x());
488 if (max_offset.y()) {
489 scroll_offset_dip.set_y((scroll_offset.y() * max_scroll_offset_dip_.y()) /
490 max_offset.y());
493 DCHECK_LE(0.f, scroll_offset_dip.x());
494 DCHECK_LE(0.f, scroll_offset_dip.y());
495 DCHECK(scroll_offset_dip.x() < max_scroll_offset_dip_.x() ||
496 scroll_offset_dip.x() - max_scroll_offset_dip_.x() < kEpsilon)
497 << scroll_offset_dip.x() << " " << max_scroll_offset_dip_.x();
498 DCHECK(scroll_offset_dip.y() < max_scroll_offset_dip_.y() ||
499 scroll_offset_dip.y() - max_scroll_offset_dip_.y() < kEpsilon)
500 << scroll_offset_dip.y() << " " << max_scroll_offset_dip_.y();
502 if (scroll_offset_dip_ == scroll_offset_dip)
503 return;
505 scroll_offset_dip_ = scroll_offset_dip;
507 TRACE_EVENT_INSTANT2("android_webview",
508 "BrowserViewRenderer::ScrollTo",
509 TRACE_EVENT_SCOPE_THREAD,
510 "x",
511 scroll_offset_dip.x(),
512 "y",
513 scroll_offset_dip.y());
515 if (compositor_)
516 compositor_->DidChangeRootLayerScrollOffset();
519 void BrowserViewRenderer::DidUpdateContent() {
520 TRACE_EVENT_INSTANT0("android_webview",
521 "BrowserViewRenderer::DidUpdateContent",
522 TRACE_EVENT_SCOPE_THREAD);
523 clear_view_ = false;
524 if (on_new_picture_enable_)
525 client_->OnNewPicture();
528 void BrowserViewRenderer::SetTotalRootLayerScrollOffset(
529 gfx::Vector2dF scroll_offset_dip) {
530 // TOOD(mkosiba): Add a DCHECK to say that this does _not_ get called during
531 // DrawGl when http://crbug.com/249972 is fixed.
532 if (scroll_offset_dip_ == scroll_offset_dip)
533 return;
535 scroll_offset_dip_ = scroll_offset_dip;
537 gfx::Vector2d max_offset = max_scroll_offset();
538 gfx::Vector2d scroll_offset;
539 // For an explanation as to why this is done this way see the comment in
540 // BrowserViewRenderer::ScrollTo.
541 if (max_scroll_offset_dip_.x()) {
542 scroll_offset.set_x((scroll_offset_dip.x() * max_offset.x()) /
543 max_scroll_offset_dip_.x());
546 if (max_scroll_offset_dip_.y()) {
547 scroll_offset.set_y((scroll_offset_dip.y() * max_offset.y()) /
548 max_scroll_offset_dip_.y());
551 DCHECK_LE(0, scroll_offset.x());
552 DCHECK_LE(0, scroll_offset.y());
553 DCHECK_LE(scroll_offset.x(), max_offset.x());
554 DCHECK_LE(scroll_offset.y(), max_offset.y());
556 client_->ScrollContainerViewTo(scroll_offset);
559 gfx::Vector2dF BrowserViewRenderer::GetTotalRootLayerScrollOffset() {
560 return scroll_offset_dip_;
563 bool BrowserViewRenderer::IsExternalScrollActive() const {
564 return client_->IsSmoothScrollingActive();
567 void BrowserViewRenderer::UpdateRootLayerState(
568 const gfx::Vector2dF& total_scroll_offset_dip,
569 const gfx::Vector2dF& max_scroll_offset_dip,
570 const gfx::SizeF& scrollable_size_dip,
571 float page_scale_factor,
572 float min_page_scale_factor,
573 float max_page_scale_factor) {
574 TRACE_EVENT_INSTANT1(
575 "android_webview",
576 "BrowserViewRenderer::UpdateRootLayerState",
577 TRACE_EVENT_SCOPE_THREAD,
578 "state",
579 RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip));
581 DCHECK_GT(dip_scale_, 0.f);
583 max_scroll_offset_dip_ = max_scroll_offset_dip;
584 DCHECK_LE(0.f, max_scroll_offset_dip_.x());
585 DCHECK_LE(0.f, max_scroll_offset_dip_.y());
587 page_scale_factor_ = page_scale_factor;
588 DCHECK_GT(page_scale_factor_, 0.f);
590 client_->UpdateScrollState(max_scroll_offset(),
591 scrollable_size_dip,
592 page_scale_factor,
593 min_page_scale_factor,
594 max_page_scale_factor);
595 SetTotalRootLayerScrollOffset(total_scroll_offset_dip);
598 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
599 BrowserViewRenderer::RootLayerStateAsValue(
600 const gfx::Vector2dF& total_scroll_offset_dip,
601 const gfx::SizeF& scrollable_size_dip) {
602 scoped_refptr<base::trace_event::TracedValue> state =
603 new base::trace_event::TracedValue();
605 state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x());
606 state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y());
608 state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x());
609 state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y());
611 state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width());
612 state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height());
614 state->SetDouble("page_scale_factor", page_scale_factor_);
615 return state;
618 void BrowserViewRenderer::SetNeedsAnimateScroll(
619 const AnimationCallback& scroll_animation) {
620 pending_fling_animation_ = scroll_animation;
621 // No need to reschedule the fallback tick here because the compositor is
622 // fine with the animation not being ticked. The invalidate could happen some
623 // time later, or not at all.
624 client_->PostInvalidate();
627 void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll,
628 gfx::Vector2dF latest_overscroll_delta,
629 gfx::Vector2dF current_fling_velocity) {
630 const float physical_pixel_scale = dip_scale_ * page_scale_factor_;
631 if (accumulated_overscroll == latest_overscroll_delta)
632 overscroll_rounding_error_ = gfx::Vector2dF();
633 gfx::Vector2dF scaled_overscroll_delta =
634 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale);
635 gfx::Vector2d rounded_overscroll_delta = gfx::ToRoundedVector2d(
636 scaled_overscroll_delta + overscroll_rounding_error_);
637 overscroll_rounding_error_ =
638 scaled_overscroll_delta - rounded_overscroll_delta;
639 gfx::Vector2dF fling_velocity_pixels =
640 gfx::ScaleVector2d(current_fling_velocity, physical_pixel_scale);
642 client_->DidOverscroll(rounded_overscroll_delta, fling_velocity_pixels);
645 void BrowserViewRenderer::PostInvalidate() {
646 TRACE_EVENT_INSTANT0("android_webview", "BrowserViewRenderer::PostInvalidate",
647 TRACE_EVENT_SCOPE_THREAD);
648 PostInvalidateWithFallback();
651 void BrowserViewRenderer::PostInvalidateWithFallback() {
652 // Always call view invalidate. We rely the Android framework to ignore the
653 // invalidate when it's not needed such as when view is not visible.
654 client_->PostInvalidate();
656 // Stop fallback ticks when one of these is true.
657 // 1) Webview is paused. Also need to check we are not in clear view since
658 // paused, offscreen still expect clear view to recover.
659 // 2) If we are attached to window and the window is not visible (eg when
660 // app is in the background). We are sure in this case the webview is used
661 // "on-screen" but that updates are not needed when in the background.
662 bool throttle_fallback_tick =
663 (is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_);
665 if (throttle_fallback_tick || fallback_tick_pending_)
666 return;
668 DCHECK(post_fallback_tick_.IsCancelled());
669 DCHECK(fallback_tick_fired_.IsCancelled());
671 post_fallback_tick_.Reset(base::Bind(&BrowserViewRenderer::PostFallbackTick,
672 base::Unretained(this)));
673 ui_task_runner_->PostTask(FROM_HERE, post_fallback_tick_.callback());
674 fallback_tick_pending_ = true;
677 void BrowserViewRenderer::CancelFallbackTick() {
678 post_fallback_tick_.Cancel();
679 fallback_tick_fired_.Cancel();
680 fallback_tick_pending_ = false;
683 void BrowserViewRenderer::PostFallbackTick() {
684 DCHECK(fallback_tick_fired_.IsCancelled());
685 TRACE_EVENT0("android_webview", "BrowserViewRenderer::PostFallbackTick");
686 post_fallback_tick_.Cancel();
687 fallback_tick_fired_.Reset(base::Bind(&BrowserViewRenderer::FallbackTickFired,
688 base::Unretained(this)));
689 ui_task_runner_->PostDelayedTask(
690 FROM_HERE, fallback_tick_fired_.callback(),
691 base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds));
694 void BrowserViewRenderer::FallbackTickFired() {
695 TRACE_EVENT0("android_webview", "BrowserViewRenderer::FallbackTickFired");
696 // This should only be called if OnDraw or DrawGL did not come in time, which
697 // means fallback_tick_pending_ must still be true.
698 DCHECK(fallback_tick_pending_);
699 fallback_tick_fired_.Cancel();
700 fallback_tick_pending_ = false;
701 if (compositor_) {
702 if (hardware_enabled_) {
703 CompositeHw();
704 } else {
705 ForceFakeCompositeSW();
710 void BrowserViewRenderer::ForceFakeCompositeSW() {
711 DCHECK(compositor_);
712 SkBitmap bitmap;
713 bitmap.allocN32Pixels(1, 1);
714 bitmap.eraseColor(0);
715 SkCanvas canvas(bitmap);
716 CompositeSW(&canvas);
719 bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) {
720 DCHECK(compositor_);
721 CancelFallbackTick();
722 ReturnResourceFromParent();
723 return compositor_->DemandDrawSw(canvas);
726 void BrowserViewRenderer::UpdateCompositorIsActive() {
727 if (compositor_)
728 compositor_->SetIsActive(!is_paused_ &&
729 (!attached_to_window_ || window_visible_));
732 std::string BrowserViewRenderer::ToString() const {
733 std::string str;
734 base::StringAppendF(&str, "is_paused: %d ", is_paused_);
735 base::StringAppendF(&str, "view_visible: %d ", view_visible_);
736 base::StringAppendF(&str, "window_visible: %d ", window_visible_);
737 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_);
738 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_);
739 base::StringAppendF(&str, "fallback_tick_pending: %d ",
740 fallback_tick_pending_);
741 base::StringAppendF(&str, "view size: %s ", size_.ToString().c_str());
742 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_);
743 base::StringAppendF(&str,
744 "global visible rect: %s ",
745 last_on_draw_global_visible_rect_.ToString().c_str());
746 base::StringAppendF(
747 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str());
748 base::StringAppendF(&str,
749 "overscroll_rounding_error_: %s ",
750 overscroll_rounding_error_.ToString().c_str());
751 base::StringAppendF(
752 &str, "on_new_picture_enable: %d ", on_new_picture_enable_);
753 base::StringAppendF(&str, "clear_view: %d ", clear_view_);
754 return str;
757 } // namespace android_webview