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/rect.h"
16 #include "ui/gfx/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 intptr_t 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
);
108 // Checks the continuous invalidate and block invalidate state, and schedule
109 // invalidates appropriately. If |force_invalidate| is true, then send a view
110 // invalidate regardless of compositor expectation. If |skip_reschedule_tick|
111 // is true and if there is already a pending fallback tick, don't reschedule
113 void EnsureContinuousInvalidation(bool force_invalidate
,
114 bool skip_reschedule_tick
);
115 bool CompositeSW(SkCanvas
* canvas
);
117 void DidSkipCompositeInDraw();
118 scoped_refptr
<base::debug::ConvertableToTraceFormat
> RootLayerStateAsValue(
119 const gfx::Vector2dF
& total_scroll_offset_dip
,
120 const gfx::SizeF
& scrollable_size_dip
);
122 scoped_ptr
<cc::CompositorFrame
> CompositeHw();
123 void ReturnUnusedResource(scoped_ptr
<cc::CompositorFrame
> frame
);
124 void ReturnResourceFromParent();
126 // If we call up view invalidate and OnDraw is not called before a deadline,
127 // then we keep ticking the SynchronousCompositor so it can make progress.
128 // Do this in a two stage tick due to native MessageLoop favors delayed task,
129 // so ensure delayed task is inserted only after the draw task returns.
130 void PostFallbackTick();
131 void FallbackTickFired();
133 // Force invoke the compositor to run produce a 1x1 software frame that is
134 // immediately discarded. This is a hack to force invoke parts of the
135 // compositor that are not directly exposed here.
136 void ForceFakeCompositeSW();
138 gfx::Vector2d
max_scroll_offset() const;
140 size_t CalculateDesiredMemoryPolicy();
141 // For debug tracing or logging. Return the string representation of this
142 // view renderer's state.
143 std::string
ToString() const;
145 BrowserViewRendererClient
* client_
;
146 SharedRendererState shared_renderer_state_
;
147 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
149 content::SynchronousCompositor
* compositor_
;
153 bool window_visible_
; // Only applicable if |attached_to_window_| is true.
154 bool attached_to_window_
;
155 bool hardware_enabled_
;
157 float page_scale_factor_
;
158 bool on_new_picture_enable_
;
161 gfx::Vector2d last_on_draw_scroll_offset_
;
162 gfx::Rect last_on_draw_global_visible_rect_
;
164 // The draw constraints from the parent compositor. These are only used for
166 ParentCompositorDrawConstraints parent_draw_constraints_
;
168 // When true, we should continuously invalidate and keep drawing, for example
169 // to drive animation. This value is set by the compositor and should always
170 // reflect the expectation of the compositor and not be reused for other
172 bool compositor_needs_continuous_invalidate_
;
174 bool invalidate_after_composite_
;
176 // Used to block additional invalidates while one is already pending.
177 bool block_invalidates_
;
179 base::CancelableClosure post_fallback_tick_
;
180 base::CancelableClosure fallback_tick_fired_
;
181 bool fallback_tick_pending_
;
185 // Current scroll offset in CSS pixels.
186 gfx::Vector2dF scroll_offset_dip_
;
188 // Max scroll offset in CSS pixels.
189 gfx::Vector2dF max_scroll_offset_dip_
;
191 // Used to prevent rounding errors from accumulating enough to generate
192 // visible skew (especially noticeable when scrolling up and down in the same
193 // spot over a period of time).
194 gfx::Vector2dF overscroll_rounding_error_
;
196 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer
);
199 } // namespace android_webview
201 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_