Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / android_webview / browser / browser_view_renderer.cc
blobec7dc0802eee05544367405e52064016932e57b0
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 SkPictureRecorder recorder;
322 SkCanvas* rec_canvas = recorder.beginRecording(width, height, NULL, 0);
323 if (compositor_) {
325 // Reset scroll back to the origin, will go back to the old
326 // value when scroll_reset is out of scope.
327 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_dip_,
328 gfx::Vector2dF());
329 compositor_->DidChangeRootLayerScrollOffset();
330 CompositeSW(rec_canvas);
332 compositor_->DidChangeRootLayerScrollOffset();
334 return skia::AdoptRef(recorder.endRecording());
337 void BrowserViewRenderer::EnableOnNewPicture(bool enabled) {
338 on_new_picture_enable_ = enabled;
341 void BrowserViewRenderer::ClearView() {
342 TRACE_EVENT_INSTANT0("android_webview",
343 "BrowserViewRenderer::ClearView",
344 TRACE_EVENT_SCOPE_THREAD);
345 if (clear_view_)
346 return;
348 clear_view_ = true;
349 // Always invalidate ignoring the compositor to actually clear the webview.
350 PostInvalidateWithFallback();
353 void BrowserViewRenderer::SetOffscreenPreRaster(bool enable) {
354 if (offscreen_pre_raster_ != enable && compositor_)
355 UpdateMemoryPolicy();
357 offscreen_pre_raster_ = enable;
360 void BrowserViewRenderer::SetIsPaused(bool paused) {
361 TRACE_EVENT_INSTANT1("android_webview",
362 "BrowserViewRenderer::SetIsPaused",
363 TRACE_EVENT_SCOPE_THREAD,
364 "paused",
365 paused);
366 is_paused_ = paused;
367 UpdateCompositorIsActive();
370 void BrowserViewRenderer::SetViewVisibility(bool view_visible) {
371 TRACE_EVENT_INSTANT1("android_webview",
372 "BrowserViewRenderer::SetViewVisibility",
373 TRACE_EVENT_SCOPE_THREAD,
374 "view_visible",
375 view_visible);
376 view_visible_ = view_visible;
379 void BrowserViewRenderer::SetWindowVisibility(bool window_visible) {
380 TRACE_EVENT_INSTANT1("android_webview",
381 "BrowserViewRenderer::SetWindowVisibility",
382 TRACE_EVENT_SCOPE_THREAD,
383 "window_visible",
384 window_visible);
385 window_visible_ = window_visible;
386 UpdateCompositorIsActive();
389 void BrowserViewRenderer::OnSizeChanged(int width, int height) {
390 TRACE_EVENT_INSTANT2("android_webview",
391 "BrowserViewRenderer::OnSizeChanged",
392 TRACE_EVENT_SCOPE_THREAD,
393 "width",
394 width,
395 "height",
396 height);
397 size_.SetSize(width, height);
400 void BrowserViewRenderer::OnAttachedToWindow(int width, int height) {
401 TRACE_EVENT2("android_webview",
402 "BrowserViewRenderer::OnAttachedToWindow",
403 "width",
404 width,
405 "height",
406 height);
407 attached_to_window_ = true;
408 size_.SetSize(width, height);
409 UpdateCompositorIsActive();
412 void BrowserViewRenderer::OnDetachedFromWindow() {
413 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
414 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI();
415 attached_to_window_ = false;
416 DCHECK(!hardware_enabled_);
417 UpdateCompositorIsActive();
420 void BrowserViewRenderer::OnComputeScroll(base::TimeTicks animation_time) {
421 if (!compositor_)
422 return;
423 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnComputeScroll");
424 compositor_->OnComputeScroll(animation_time);
427 void BrowserViewRenderer::ReleaseHardware() {
428 DCHECK(hardware_enabled_);
429 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI());
430 ReturnResourceFromParent();
431 DCHECK(shared_renderer_state_.ReturnedResourcesEmptyOnUI());
433 if (compositor_) {
434 compositor_->SetMemoryPolicy(0u);
437 hardware_enabled_ = false;
440 bool BrowserViewRenderer::IsVisible() const {
441 // Ignore |window_visible_| if |attached_to_window_| is false.
442 return view_visible_ && (!attached_to_window_ || window_visible_);
445 gfx::Rect BrowserViewRenderer::GetScreenRect() const {
446 return gfx::Rect(client_->GetLocationOnScreen(), size_);
449 void BrowserViewRenderer::DidInitializeCompositor(
450 content::SynchronousCompositor* compositor) {
451 TRACE_EVENT0("android_webview",
452 "BrowserViewRenderer::DidInitializeCompositor");
453 DCHECK(compositor);
454 DCHECK(!compositor_);
455 compositor_ = compositor;
456 UpdateCompositorIsActive();
459 void BrowserViewRenderer::DidDestroyCompositor(
460 content::SynchronousCompositor* compositor) {
461 TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor");
462 DCHECK(compositor_);
463 compositor_->SetIsActive(false);
464 compositor_ = NULL;
467 void BrowserViewRenderer::SetDipScale(float dip_scale) {
468 dip_scale_ = dip_scale;
469 CHECK_GT(dip_scale_, 0.f);
472 gfx::Vector2d BrowserViewRenderer::max_scroll_offset() const {
473 DCHECK_GT(dip_scale_, 0.f);
474 return gfx::ToCeiledVector2d(gfx::ScaleVector2d(
475 max_scroll_offset_dip_, dip_scale_ * page_scale_factor_));
478 void BrowserViewRenderer::ScrollTo(gfx::Vector2d scroll_offset) {
479 gfx::Vector2d max_offset = max_scroll_offset();
480 gfx::Vector2dF scroll_offset_dip;
481 // To preserve the invariant that scrolling to the maximum physical pixel
482 // value also scrolls to the maximum dip pixel value we transform the physical
483 // offset into the dip offset by using a proportion (instead of dividing by
484 // dip_scale * page_scale_factor).
485 if (max_offset.x()) {
486 scroll_offset_dip.set_x((scroll_offset.x() * max_scroll_offset_dip_.x()) /
487 max_offset.x());
489 if (max_offset.y()) {
490 scroll_offset_dip.set_y((scroll_offset.y() * max_scroll_offset_dip_.y()) /
491 max_offset.y());
494 DCHECK_LE(0.f, scroll_offset_dip.x());
495 DCHECK_LE(0.f, scroll_offset_dip.y());
496 DCHECK(scroll_offset_dip.x() < max_scroll_offset_dip_.x() ||
497 scroll_offset_dip.x() - max_scroll_offset_dip_.x() < kEpsilon)
498 << scroll_offset_dip.x() << " " << max_scroll_offset_dip_.x();
499 DCHECK(scroll_offset_dip.y() < max_scroll_offset_dip_.y() ||
500 scroll_offset_dip.y() - max_scroll_offset_dip_.y() < kEpsilon)
501 << scroll_offset_dip.y() << " " << max_scroll_offset_dip_.y();
503 if (scroll_offset_dip_ == scroll_offset_dip)
504 return;
506 scroll_offset_dip_ = scroll_offset_dip;
508 TRACE_EVENT_INSTANT2("android_webview",
509 "BrowserViewRenderer::ScrollTo",
510 TRACE_EVENT_SCOPE_THREAD,
511 "x",
512 scroll_offset_dip.x(),
513 "y",
514 scroll_offset_dip.y());
516 if (compositor_)
517 compositor_->DidChangeRootLayerScrollOffset();
520 void BrowserViewRenderer::DidUpdateContent() {
521 TRACE_EVENT_INSTANT0("android_webview",
522 "BrowserViewRenderer::DidUpdateContent",
523 TRACE_EVENT_SCOPE_THREAD);
524 clear_view_ = false;
525 if (on_new_picture_enable_)
526 client_->OnNewPicture();
529 void BrowserViewRenderer::SetTotalRootLayerScrollOffset(
530 gfx::Vector2dF scroll_offset_dip) {
531 // TOOD(mkosiba): Add a DCHECK to say that this does _not_ get called during
532 // DrawGl when http://crbug.com/249972 is fixed.
533 if (scroll_offset_dip_ == scroll_offset_dip)
534 return;
536 scroll_offset_dip_ = scroll_offset_dip;
538 gfx::Vector2d max_offset = max_scroll_offset();
539 gfx::Vector2d scroll_offset;
540 // For an explanation as to why this is done this way see the comment in
541 // BrowserViewRenderer::ScrollTo.
542 if (max_scroll_offset_dip_.x()) {
543 scroll_offset.set_x((scroll_offset_dip.x() * max_offset.x()) /
544 max_scroll_offset_dip_.x());
547 if (max_scroll_offset_dip_.y()) {
548 scroll_offset.set_y((scroll_offset_dip.y() * max_offset.y()) /
549 max_scroll_offset_dip_.y());
552 DCHECK_LE(0, scroll_offset.x());
553 DCHECK_LE(0, scroll_offset.y());
554 DCHECK_LE(scroll_offset.x(), max_offset.x());
555 DCHECK_LE(scroll_offset.y(), max_offset.y());
557 client_->ScrollContainerViewTo(scroll_offset);
560 gfx::Vector2dF BrowserViewRenderer::GetTotalRootLayerScrollOffset() {
561 return scroll_offset_dip_;
564 void BrowserViewRenderer::UpdateRootLayerState(
565 const gfx::Vector2dF& total_scroll_offset_dip,
566 const gfx::Vector2dF& max_scroll_offset_dip,
567 const gfx::SizeF& scrollable_size_dip,
568 float page_scale_factor,
569 float min_page_scale_factor,
570 float max_page_scale_factor) {
571 TRACE_EVENT_INSTANT1(
572 "android_webview",
573 "BrowserViewRenderer::UpdateRootLayerState",
574 TRACE_EVENT_SCOPE_THREAD,
575 "state",
576 RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip));
578 DCHECK_GT(dip_scale_, 0.f);
580 max_scroll_offset_dip_ = max_scroll_offset_dip;
581 DCHECK_LE(0.f, max_scroll_offset_dip_.x());
582 DCHECK_LE(0.f, max_scroll_offset_dip_.y());
584 page_scale_factor_ = page_scale_factor;
585 DCHECK_GT(page_scale_factor_, 0.f);
587 client_->UpdateScrollState(max_scroll_offset(),
588 scrollable_size_dip,
589 page_scale_factor,
590 min_page_scale_factor,
591 max_page_scale_factor);
592 SetTotalRootLayerScrollOffset(total_scroll_offset_dip);
595 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
596 BrowserViewRenderer::RootLayerStateAsValue(
597 const gfx::Vector2dF& total_scroll_offset_dip,
598 const gfx::SizeF& scrollable_size_dip) {
599 scoped_refptr<base::trace_event::TracedValue> state =
600 new base::trace_event::TracedValue();
602 state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x());
603 state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y());
605 state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x());
606 state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y());
608 state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width());
609 state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height());
611 state->SetDouble("page_scale_factor", page_scale_factor_);
612 return state;
615 void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll,
616 gfx::Vector2dF latest_overscroll_delta,
617 gfx::Vector2dF current_fling_velocity) {
618 const float physical_pixel_scale = dip_scale_ * page_scale_factor_;
619 if (accumulated_overscroll == latest_overscroll_delta)
620 overscroll_rounding_error_ = gfx::Vector2dF();
621 gfx::Vector2dF scaled_overscroll_delta =
622 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale);
623 gfx::Vector2d rounded_overscroll_delta = gfx::ToRoundedVector2d(
624 scaled_overscroll_delta + overscroll_rounding_error_);
625 overscroll_rounding_error_ =
626 scaled_overscroll_delta - rounded_overscroll_delta;
627 gfx::Vector2dF fling_velocity_pixels =
628 gfx::ScaleVector2d(current_fling_velocity, physical_pixel_scale);
630 client_->DidOverscroll(rounded_overscroll_delta, fling_velocity_pixels);
633 void BrowserViewRenderer::PostInvalidate() {
634 TRACE_EVENT_INSTANT0("android_webview", "BrowserViewRenderer::PostInvalidate",
635 TRACE_EVENT_SCOPE_THREAD);
636 PostInvalidateWithFallback();
639 void BrowserViewRenderer::PostInvalidateWithFallback() {
640 // Always call view invalidate. We rely the Android framework to ignore the
641 // invalidate when it's not needed such as when view is not visible.
642 client_->PostInvalidate();
644 // Stop fallback ticks when one of these is true.
645 // 1) Webview is paused. Also need to check we are not in clear view since
646 // paused, offscreen still expect clear view to recover.
647 // 2) If we are attached to window and the window is not visible (eg when
648 // app is in the background). We are sure in this case the webview is used
649 // "on-screen" but that updates are not needed when in the background.
650 bool throttle_fallback_tick =
651 (is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_);
653 if (throttle_fallback_tick || fallback_tick_pending_)
654 return;
656 DCHECK(post_fallback_tick_.IsCancelled());
657 DCHECK(fallback_tick_fired_.IsCancelled());
659 post_fallback_tick_.Reset(base::Bind(&BrowserViewRenderer::PostFallbackTick,
660 base::Unretained(this)));
661 ui_task_runner_->PostTask(FROM_HERE, post_fallback_tick_.callback());
662 fallback_tick_pending_ = true;
665 void BrowserViewRenderer::CancelFallbackTick() {
666 post_fallback_tick_.Cancel();
667 fallback_tick_fired_.Cancel();
668 fallback_tick_pending_ = false;
671 void BrowserViewRenderer::PostFallbackTick() {
672 DCHECK(fallback_tick_fired_.IsCancelled());
673 TRACE_EVENT0("android_webview", "BrowserViewRenderer::PostFallbackTick");
674 post_fallback_tick_.Cancel();
675 fallback_tick_fired_.Reset(base::Bind(&BrowserViewRenderer::FallbackTickFired,
676 base::Unretained(this)));
677 ui_task_runner_->PostDelayedTask(
678 FROM_HERE, fallback_tick_fired_.callback(),
679 base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds));
682 void BrowserViewRenderer::FallbackTickFired() {
683 TRACE_EVENT0("android_webview", "BrowserViewRenderer::FallbackTickFired");
684 // This should only be called if OnDraw or DrawGL did not come in time, which
685 // means fallback_tick_pending_ must still be true.
686 DCHECK(fallback_tick_pending_);
687 fallback_tick_fired_.Cancel();
688 fallback_tick_pending_ = false;
689 if (compositor_) {
690 if (hardware_enabled_) {
691 CompositeHw();
692 } else {
693 ForceFakeCompositeSW();
698 void BrowserViewRenderer::ForceFakeCompositeSW() {
699 DCHECK(compositor_);
700 SkBitmap bitmap;
701 bitmap.allocN32Pixels(1, 1);
702 bitmap.eraseColor(0);
703 SkCanvas canvas(bitmap);
704 CompositeSW(&canvas);
707 bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) {
708 DCHECK(compositor_);
709 CancelFallbackTick();
710 ReturnResourceFromParent();
711 return compositor_->DemandDrawSw(canvas);
714 void BrowserViewRenderer::UpdateCompositorIsActive() {
715 if (compositor_)
716 compositor_->SetIsActive(!is_paused_ &&
717 (!attached_to_window_ || window_visible_));
720 std::string BrowserViewRenderer::ToString() const {
721 std::string str;
722 base::StringAppendF(&str, "is_paused: %d ", is_paused_);
723 base::StringAppendF(&str, "view_visible: %d ", view_visible_);
724 base::StringAppendF(&str, "window_visible: %d ", window_visible_);
725 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_);
726 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_);
727 base::StringAppendF(&str, "fallback_tick_pending: %d ",
728 fallback_tick_pending_);
729 base::StringAppendF(&str, "view size: %s ", size_.ToString().c_str());
730 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_);
731 base::StringAppendF(&str,
732 "global visible rect: %s ",
733 last_on_draw_global_visible_rect_.ToString().c_str());
734 base::StringAppendF(
735 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str());
736 base::StringAppendF(&str,
737 "overscroll_rounding_error_: %s ",
738 overscroll_rounding_error_.ToString().c_str());
739 base::StringAppendF(
740 &str, "on_new_picture_enable: %d ", on_new_picture_enable_);
741 base::StringAppendF(&str, "clear_view: %d ", clear_view_);
742 return str;
745 } // namespace android_webview