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/debug/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"
21 namespace android_webview
{
23 class BrowserViewRendererClient
;
25 // Interface for all the WebView-specific content rendering operations.
26 // Provides software and hardware rendering and the Capture Picture API.
27 class BrowserViewRenderer
: public content::SynchronousCompositorClient
{
29 static void CalculateTileMemoryPolicy();
32 BrowserViewRendererClient
* client
,
33 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
);
35 virtual ~BrowserViewRenderer();
37 SharedRendererState
* GetAwDrawGLViewContext();
38 bool RequestDrawGL(bool wait_for_completion
);
40 // Called before either OnDrawHardware or OnDrawSoftware to set the view
41 // state of this frame. |scroll| is the view's current scroll offset.
42 // |global_visible_rect| is the intersection of the view size and the window
43 // in window coordinates.
44 void PrepareToDraw(const gfx::Vector2d
& scroll
,
45 const gfx::Rect
& global_visible_rect
);
47 // Main handlers for view drawing. A false return value indicates no new
49 bool OnDrawHardware();
50 bool OnDrawSoftware(SkCanvas
* canvas
);
52 // CapturePicture API methods.
53 skia::RefPtr
<SkPicture
> CapturePicture(int width
, int height
);
54 void EnableOnNewPicture(bool enabled
);
58 // View update notifications.
59 void SetIsPaused(bool paused
);
60 void SetViewVisibility(bool visible
);
61 void SetWindowVisibility(bool visible
);
62 void OnSizeChanged(int width
, int height
);
63 void OnAttachedToWindow(int width
, int height
);
64 void OnDetachedFromWindow();
66 // Sets the scale for logical<->physical pixel conversions.
67 void SetDipScale(float dip_scale
);
69 // Set the root layer scroll offset to |new_value|.
70 void ScrollTo(gfx::Vector2d new_value
);
72 // Android views hierarchy gluing.
73 bool IsVisible() const;
74 gfx::Rect
GetScreenRect() const;
75 bool attached_to_window() const { return attached_to_window_
; }
76 bool hardware_enabled() const { return hardware_enabled_
; }
77 gfx::Size
size() const { return size_
; }
78 void ReleaseHardware();
80 void TrimMemory(const int level
, const bool visible
);
82 // SynchronousCompositorClient overrides.
83 virtual void DidInitializeCompositor(
84 content::SynchronousCompositor
* compositor
) override
;
85 virtual void DidDestroyCompositor(content::SynchronousCompositor
* compositor
)
87 virtual void SetContinuousInvalidate(bool invalidate
) override
;
88 virtual void DidUpdateContent() override
;
89 virtual gfx::Vector2dF
GetTotalRootLayerScrollOffset() override
;
90 virtual void UpdateRootLayerState(
91 const gfx::Vector2dF
& total_scroll_offset_dip
,
92 const gfx::Vector2dF
& max_scroll_offset_dip
,
93 const gfx::SizeF
& scrollable_size_dip
,
94 float page_scale_factor
,
95 float min_page_scale_factor
,
96 float max_page_scale_factor
) override
;
97 virtual bool IsExternalFlingActive() const override
;
98 virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll
,
99 gfx::Vector2dF latest_overscroll_delta
,
100 gfx::Vector2dF current_fling_velocity
) override
;
102 void UpdateParentDrawConstraints();
103 void DidSkipCommitFrame();
104 void InvalidateOnFunctorDestroy();
107 void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_dip
);
109 // Checks the continuous invalidate and block invalidate state, and schedule
110 // invalidates appropriately. If |force_invalidate| is true, then send a view
111 // invalidate regardless of compositor expectation. If |skip_reschedule_tick|
112 // is true and if there is already a pending fallback tick, don't reschedule
114 void EnsureContinuousInvalidation(bool force_invalidate
,
115 bool skip_reschedule_tick
);
116 bool CompositeSW(SkCanvas
* canvas
);
118 void DidSkipCompositeInDraw();
119 scoped_refptr
<base::debug::ConvertableToTraceFormat
> RootLayerStateAsValue(
120 const gfx::Vector2dF
& total_scroll_offset_dip
,
121 const gfx::SizeF
& scrollable_size_dip
);
123 scoped_ptr
<cc::CompositorFrame
> CompositeHw();
124 void ReturnUnusedResource(scoped_ptr
<cc::CompositorFrame
> frame
);
125 void ReturnResourceFromParent();
127 // If we call up view invalidate and OnDraw is not called before a deadline,
128 // then we keep ticking the SynchronousCompositor so it can make progress.
129 // Do this in a two stage tick due to native MessageLoop favors delayed task,
130 // so ensure delayed task is inserted only after the draw task returns.
131 void PostFallbackTick();
132 void FallbackTickFired();
134 // Force invoke the compositor to run produce a 1x1 software frame that is
135 // immediately discarded. This is a hack to force invoke parts of the
136 // compositor that are not directly exposed here.
137 void ForceFakeCompositeSW();
139 gfx::Vector2d
max_scroll_offset() const;
141 size_t CalculateDesiredMemoryPolicy();
142 // For debug tracing or logging. Return the string representation of this
143 // view renderer's state.
144 std::string
ToString() const;
146 BrowserViewRendererClient
* client_
;
147 SharedRendererState shared_renderer_state_
;
148 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
150 content::SynchronousCompositor
* compositor_
;
154 bool window_visible_
; // Only applicable if |attached_to_window_| is true.
155 bool attached_to_window_
;
156 bool hardware_enabled_
;
158 float page_scale_factor_
;
159 bool on_new_picture_enable_
;
162 gfx::Vector2d last_on_draw_scroll_offset_
;
163 gfx::Rect last_on_draw_global_visible_rect_
;
165 // The draw constraints from the parent compositor. These are only used for
167 ParentCompositorDrawConstraints parent_draw_constraints_
;
169 // When true, we should continuously invalidate and keep drawing, for example
170 // to drive animation. This value is set by the compositor and should always
171 // reflect the expectation of the compositor and not be reused for other
173 bool compositor_needs_continuous_invalidate_
;
175 bool invalidate_after_composite_
;
177 // Used to block additional invalidates while one is already pending.
178 bool block_invalidates_
;
180 base::CancelableClosure post_fallback_tick_
;
181 base::CancelableClosure fallback_tick_fired_
;
182 bool fallback_tick_pending_
;
186 // Current scroll offset in CSS pixels.
187 gfx::Vector2dF scroll_offset_dip_
;
189 // Max scroll offset in CSS pixels.
190 gfx::Vector2dF max_scroll_offset_dip_
;
192 // Used to prevent rounding errors from accumulating enough to generate
193 // visible skew (especially noticeable when scrolling up and down in the same
194 // spot over a period of time).
195 gfx::Vector2dF overscroll_rounding_error_
;
197 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer
);
200 } // namespace android_webview
202 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_