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/global_tile_manager.h"
9 #include "android_webview/browser/global_tile_manager_client.h"
10 #include "android_webview/browser/shared_renderer_state.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/callback.h"
13 #include "base/cancelable_callback.h"
14 #include "content/public/browser/android/synchronous_compositor_client.h"
15 #include "skia/ext/refptr.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/vector2d_f.h"
22 struct AwDrawSWFunctionTable
;
25 class ContentViewCore
;
26 class SynchronousCompositor
;
27 struct SynchronousCompositorMemoryPolicy
;
31 namespace android_webview
{
33 class BrowserViewRendererClient
;
35 // Delegate to perform rendering actions involving Java objects.
36 class BrowserViewRendererJavaHelper
{
38 static BrowserViewRendererJavaHelper
* GetInstance();
40 typedef base::Callback
<bool(SkCanvas
*)> RenderMethod
;
42 // Try obtaining the native SkCanvas from |java_canvas| and call
43 // |render_source| with it. If that fails, allocate an auxilary bitmap
44 // for |render_source| to render into, then copy the bitmap into
46 virtual bool RenderViaAuxilaryBitmapIfNeeded(
48 const gfx::Vector2d
& scroll_correction
,
49 const gfx::Rect
& clip
,
50 RenderMethod render_source
) = 0;
53 virtual ~BrowserViewRendererJavaHelper() {}
56 // Interface for all the WebView-specific content rendering operations.
57 // Provides software and hardware rendering and the Capture Picture API.
58 class BrowserViewRenderer
: public content::SynchronousCompositorClient
,
59 public GlobalTileManagerClient
{
61 static void CalculateTileMemoryPolicy();
64 BrowserViewRendererClient
* client
,
65 SharedRendererState
* shared_renderer_state
,
66 content::WebContents
* web_contents
,
67 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
);
69 virtual ~BrowserViewRenderer();
71 // Main handler for view drawing: performs a SW draw immediately, or sets up
72 // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and
73 // returns true. A false return value indicates nothing was or will be drawn.
74 // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates
75 // a GL Draw maybe possible on this canvas. |scroll| if the view's current
76 // scroll offset. |clip| is the canvas's clip bounds. |global_visible_rect|
77 // is the intersection of the view size and the window in window coordinates.
78 bool OnDraw(jobject java_canvas
,
79 bool is_hardware_canvas
,
80 const gfx::Vector2d
& scroll
,
81 const gfx::Rect
& global_visible_rect
,
82 const gfx::Rect
& clip
);
83 void DidDrawGL(const DrawGLResult
& result
);
85 // CapturePicture API methods.
86 skia::RefPtr
<SkPicture
> CapturePicture(int width
, int height
);
87 void EnableOnNewPicture(bool enabled
);
91 // View update notifications.
92 void SetIsPaused(bool paused
);
93 void SetViewVisibility(bool visible
);
94 void SetWindowVisibility(bool visible
);
95 void OnSizeChanged(int width
, int height
);
96 void OnAttachedToWindow(int width
, int height
);
97 void OnDetachedFromWindow();
99 // Sets the scale for logical<->physical pixel conversions.
100 void SetDipScale(float dip_scale
);
102 // Set the root layer scroll offset to |new_value|.
103 void ScrollTo(gfx::Vector2d new_value
);
105 // Android views hierarchy gluing.
106 bool IsAttachedToWindow() const;
107 bool IsVisible() const;
108 gfx::Rect
GetScreenRect() const;
110 // Set the memory policy in shared renderer state and request the tiles from
111 // GlobalTileManager. The actually amount of memory allowed by
112 // GlobalTileManager may not be equal to what's requested in |policy|.
113 void RequestMemoryPolicy(content::SynchronousCompositorMemoryPolicy
& policy
);
115 void TrimMemory(const int level
, const bool visible
);
117 // SynchronousCompositorClient overrides
118 virtual void DidInitializeCompositor(
119 content::SynchronousCompositor
* compositor
) OVERRIDE
;
120 virtual void DidDestroyCompositor(content::SynchronousCompositor
* compositor
)
122 virtual void SetContinuousInvalidate(bool invalidate
) OVERRIDE
;
123 virtual void SetMaxRootLayerScrollOffset(gfx::Vector2dF new_value
) OVERRIDE
;
124 virtual void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_css
)
126 virtual void DidUpdateContent() OVERRIDE
;
127 virtual gfx::Vector2dF
GetTotalRootLayerScrollOffset() OVERRIDE
;
128 virtual bool IsExternalFlingActive() const OVERRIDE
;
129 virtual void SetRootLayerPageScaleFactorAndLimits(float page_scale_factor
,
130 float min_page_scale_factor
,
131 float max_page_scale_factor
)
133 virtual void SetRootLayerScrollableSize(gfx::SizeF scrollable_size
) OVERRIDE
;
134 virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll
,
135 gfx::Vector2dF latest_overscroll_delta
,
136 gfx::Vector2dF current_fling_velocity
) OVERRIDE
;
138 // GlobalTileManagerClient overrides
139 virtual size_t GetNumTiles() const OVERRIDE
;
140 virtual void SetNumTiles(size_t num_tiles
,
141 bool effective_immediately
) OVERRIDE
;
144 // Checks the continuous invalidate and block invalidate state, and schedule
145 // invalidates appropriately. If |force_invalidate| is true, then send a view
146 // invalidate regardless of compositor expectation.
147 void EnsureContinuousInvalidation(bool force_invalidate
);
148 bool DrawSWInternal(jobject java_canvas
, const gfx::Rect
& clip_bounds
);
149 bool CompositeSW(SkCanvas
* canvas
);
150 void DidComposite(bool force_invalidate
);
152 // If we call up view invalidate and OnDraw is not called before a deadline,
153 // then we keep ticking the SynchronousCompositor so it can make progress.
154 void FallbackTickFired();
156 // Force invoke the compositor to run produce a 1x1 software frame that is
157 // immediately discarded. This is a hack to force invoke parts of the
158 // compositor that are not directly exposed here.
159 void ForceFakeCompositeSW();
161 void EnforceMemoryPolicyImmediately(
162 content::SynchronousCompositorMemoryPolicy policy
);
164 gfx::Vector2d
max_scroll_offset() const;
166 content::SynchronousCompositorMemoryPolicy
CalculateDesiredMemoryPolicy();
167 // For debug tracing or logging. Return the string representation of this
168 // view renderer's state and the |draw_info| if provided.
169 std::string
ToString(AwDrawGLInfo
* draw_info
) const;
171 BrowserViewRendererClient
* client_
;
172 SharedRendererState
* shared_renderer_state_
;
173 content::WebContents
* web_contents_
;
174 // TODO(boliu): This class should only be used on the UI thread. However in
175 // short term to supporting HardwareRenderer, some callbacks on
176 // SynchronousCompositorClient may be called on non-UI thread. These are
177 // used to detect this and post them back to UI thread.
178 base::WeakPtrFactory
<BrowserViewRenderer
> weak_factory_on_ui_thread_
;
179 base::WeakPtr
<BrowserViewRenderer
> ui_thread_weak_ptr_
;
180 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
182 bool has_compositor_
;
186 bool window_visible_
; // Only applicable if |attached_to_window_| is true.
187 bool attached_to_window_
;
189 float page_scale_factor_
;
190 bool on_new_picture_enable_
;
193 // When true, we should continuously invalidate and keep drawing, for example
194 // to drive animation. This value is set by the compositor and should always
195 // reflect the expectation of the compositor and not be reused for other
197 bool compositor_needs_continuous_invalidate_
;
199 // Used to block additional invalidates while one is already pending.
200 bool block_invalidates_
;
202 // Holds a callback to FallbackTickFired while it is pending.
203 base::CancelableClosure fallback_tick_
;
208 DrawGLInput draw_gl_input_
;
210 // Current scroll offset in CSS pixels.
211 gfx::Vector2dF scroll_offset_dip_
;
213 // Max scroll offset in CSS pixels.
214 gfx::Vector2dF max_scroll_offset_dip_
;
216 // Used to prevent rounding errors from accumulating enough to generate
217 // visible skew (especially noticeable when scrolling up and down in the same
218 // spot over a period of time).
219 gfx::Vector2dF overscroll_rounding_error_
;
221 GlobalTileManager::Key tile_manager_key_
;
223 // The following 2 are used to construct a memory policy and set the memory
224 // policy on the shared_renderer_state_ atomically.
228 // TODO(boliu): This is a short term solution to support
229 // SynchronousCompositorClient methods called on RenderThread. This is only
230 // used on data that must be modified immediately instead of being posted
232 base::Lock render_thread_lock_
;
234 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer
);
237 } // namespace android_webview
239 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_