1 // Copyright (c) 2013 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 #ifndef ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
6 #define ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
8 #include "android_webview/browser/parent_compositor_draw_constraints.h"
9 #include "android_webview/browser/shared_renderer_state.h"
10 #include "base/callback.h"
11 #include "base/cancelable_callback.h"
12 #include "base/trace_event/trace_event.h"
13 #include "content/public/browser/android/synchronous_compositor.h"
14 #include "content/public/browser/android/synchronous_compositor_client.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/vector2d_f.h"
25 namespace android_webview
{
27 class BrowserViewRendererClient
;
29 // Interface for all the WebView-specific content rendering operations.
30 // Provides software and hardware rendering and the Capture Picture API.
31 class BrowserViewRenderer
: public content::SynchronousCompositorClient
{
33 static void CalculateTileMemoryPolicy();
34 static BrowserViewRenderer
* FromWebContents(
35 content::WebContents
* web_contents
);
38 BrowserViewRendererClient
* client
,
39 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
);
41 ~BrowserViewRenderer() override
;
43 void RegisterWithWebContents(content::WebContents
* web_contents
);
45 SharedRendererState
* GetAwDrawGLViewContext();
46 bool RequestDrawGL(bool wait_for_completion
);
48 // Called before either OnDrawHardware or OnDrawSoftware to set the view
49 // state of this frame. |scroll| is the view's current scroll offset.
50 // |global_visible_rect| is the intersection of the view size and the window
51 // in window coordinates.
52 void PrepareToDraw(const gfx::Vector2d
& scroll
,
53 const gfx::Rect
& global_visible_rect
);
55 // Main handlers for view drawing. A false return value indicates no new
57 bool OnDrawHardware();
58 bool OnDrawSoftware(SkCanvas
* canvas
);
60 // CapturePicture API methods.
61 skia::RefPtr
<SkPicture
> CapturePicture(int width
, int height
);
62 void EnableOnNewPicture(bool enabled
);
66 void SetOffscreenPreRaster(bool enabled
);
68 // View update notifications.
69 void SetIsPaused(bool paused
);
70 void SetViewVisibility(bool visible
);
71 void SetWindowVisibility(bool visible
);
72 void OnSizeChanged(int width
, int height
);
73 void OnAttachedToWindow(int width
, int height
);
74 void OnDetachedFromWindow();
76 // Sets the scale for logical<->physical pixel conversions.
77 void SetDipScale(float dip_scale
);
79 // Set the root layer scroll offset to |new_value|.
80 void ScrollTo(gfx::Vector2d new_value
);
82 // Android views hierarchy gluing.
83 bool IsVisible() const;
84 gfx::Rect
GetScreenRect() const;
85 bool attached_to_window() const { return attached_to_window_
; }
86 bool hardware_enabled() const { return hardware_enabled_
; }
87 gfx::Size
size() const { return size_
; }
88 void ReleaseHardware();
90 void TrimMemory(const int level
, const bool visible
);
92 // SynchronousCompositorClient overrides.
93 void DidInitializeCompositor(
94 content::SynchronousCompositor
* compositor
) override
;
95 void DidDestroyCompositor(
96 content::SynchronousCompositor
* compositor
) override
;
97 void SetContinuousInvalidate(bool invalidate
) override
;
98 void DidUpdateContent() override
;
99 gfx::Vector2dF
GetTotalRootLayerScrollOffset() override
;
100 void UpdateRootLayerState(const gfx::Vector2dF
& total_scroll_offset_dip
,
101 const gfx::Vector2dF
& max_scroll_offset_dip
,
102 const gfx::SizeF
& scrollable_size_dip
,
103 float page_scale_factor
,
104 float min_page_scale_factor
,
105 float max_page_scale_factor
) override
;
106 bool IsExternalFlingActive() const override
;
107 void DidOverscroll(gfx::Vector2dF accumulated_overscroll
,
108 gfx::Vector2dF latest_overscroll_delta
,
109 gfx::Vector2dF current_fling_velocity
) override
;
111 void UpdateParentDrawConstraints();
112 void DidSkipCommitFrame();
113 void DetachFunctorFromView();
116 void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_dip
);
118 // Checks the continuous invalidate and block invalidate state, and schedule
119 // invalidates appropriately. If |force_invalidate| is true, then send a view
120 // invalidate regardless of compositor expectation. If |skip_reschedule_tick|
121 // is true and if there is already a pending fallback tick, don't reschedule
123 void EnsureContinuousInvalidation(bool force_invalidate
,
124 bool skip_reschedule_tick
);
125 bool CompositeSW(SkCanvas
* canvas
);
127 void DidSkipCompositeInDraw();
128 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
129 RootLayerStateAsValue(const gfx::Vector2dF
& total_scroll_offset_dip
,
130 const gfx::SizeF
& scrollable_size_dip
);
132 scoped_ptr
<cc::CompositorFrame
> CompositeHw();
133 void ReturnUnusedResource(scoped_ptr
<cc::CompositorFrame
> frame
);
134 void ReturnResourceFromParent();
136 // If we call up view invalidate and OnDraw is not called before a deadline,
137 // then we keep ticking the SynchronousCompositor so it can make progress.
138 // Do this in a two stage tick due to native MessageLoop favors delayed task,
139 // so ensure delayed task is inserted only after the draw task returns.
140 void PostFallbackTick();
141 void FallbackTickFired();
143 // Force invoke the compositor to run produce a 1x1 software frame that is
144 // immediately discarded. This is a hack to force invoke parts of the
145 // compositor that are not directly exposed here.
146 void ForceFakeCompositeSW();
148 gfx::Vector2d
max_scroll_offset() const;
150 size_t CalculateDesiredMemoryPolicy();
151 // For debug tracing or logging. Return the string representation of this
152 // view renderer's state.
153 std::string
ToString() const;
155 BrowserViewRendererClient
* client_
;
156 SharedRendererState shared_renderer_state_
;
157 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
159 content::SynchronousCompositor
* compositor_
;
163 bool window_visible_
; // Only applicable if |attached_to_window_| is true.
164 bool attached_to_window_
;
165 bool hardware_enabled_
;
167 float page_scale_factor_
;
168 bool on_new_picture_enable_
;
171 bool offscreen_pre_raster_
;
173 gfx::Vector2d last_on_draw_scroll_offset_
;
174 gfx::Rect last_on_draw_global_visible_rect_
;
176 // The draw constraints from the parent compositor. These are only used for
178 ParentCompositorDrawConstraints parent_draw_constraints_
;
180 // When true, we should continuously invalidate and keep drawing, for example
181 // to drive animation. This value is set by the compositor and should always
182 // reflect the expectation of the compositor and not be reused for other
184 bool compositor_needs_continuous_invalidate_
;
186 bool invalidate_after_composite_
;
188 // Used to block additional invalidates while one is already pending.
189 bool block_invalidates_
;
191 base::CancelableClosure post_fallback_tick_
;
192 base::CancelableClosure fallback_tick_fired_
;
193 bool fallback_tick_pending_
;
197 // Current scroll offset in CSS pixels.
198 gfx::Vector2dF scroll_offset_dip_
;
200 // Max scroll offset in CSS pixels.
201 gfx::Vector2dF max_scroll_offset_dip_
;
203 // Used to prevent rounding errors from accumulating enough to generate
204 // visible skew (especially noticeable when scrolling up and down in the same
205 // spot over a period of time).
206 gfx::Vector2dF overscroll_rounding_error_
;
208 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer
);
211 } // namespace android_webview
213 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_