OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ | 5 #ifndef ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ |
6 #define ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ | 6 #define ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/cancelable_callback.h" |
| 11 #include "content/public/browser/android/synchronous_compositor_client.h" |
10 #include "skia/ext/refptr.h" | 12 #include "skia/ext/refptr.h" |
11 #include "ui/gfx/point.h" | 13 #include "ui/gfx/point.h" |
12 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
13 #include "ui/gfx/vector2d_f.h" | 15 #include "ui/gfx/vector2d_f.h" |
14 | 16 |
15 class SkCanvas; | 17 class SkCanvas; |
16 class SkPicture; | 18 class SkPicture; |
17 struct AwDrawGLInfo; | 19 struct AwDrawGLInfo; |
18 struct AwDrawSWFunctionTable; | 20 struct AwDrawSWFunctionTable; |
19 | 21 |
20 namespace content { | 22 namespace content { |
21 class ContentViewCore; | 23 class ContentViewCore; |
| 24 class SynchronousCompositor; |
| 25 class WebContents; |
22 } | 26 } |
23 | 27 |
24 namespace android_webview { | 28 namespace android_webview { |
25 | 29 |
| 30 class HardwareRenderer; |
| 31 |
| 32 class BrowserViewRendererClient { |
| 33 public: |
| 34 // Request DrawGL be called. Passing null |canvas| implies the request |
| 35 // will be of AwDrawGLInfo::kModeProcess type. The callback |
| 36 // may never be made, and the mode may be promoted to kModeDraw. |
| 37 virtual bool RequestDrawGL(jobject canvas) = 0; |
| 38 |
| 39 // Called when a new Picture is available. Needs to be enabled |
| 40 // via the EnableOnNewPicture method. |
| 41 virtual void OnNewPicture() = 0; |
| 42 |
| 43 // Called to trigger view invalidations. |
| 44 virtual void PostInvalidate() = 0; |
| 45 |
| 46 // Synchronously call back to SetGlobalVisibleRect with current value. |
| 47 virtual void UpdateGlobalVisibleRect() = 0; |
| 48 |
| 49 // Called to get view's absolute location on the screen. |
| 50 virtual gfx::Point GetLocationOnScreen() = 0; |
| 51 |
| 52 // Try to set the view's scroll offset to |new_value|. |
| 53 virtual void ScrollContainerViewTo(gfx::Vector2d new_value) = 0; |
| 54 |
| 55 // Set the view's scroll offset cap to |new_value|. |
| 56 virtual void SetMaxContainerViewScrollOffset(gfx::Vector2d new_value) = 0; |
| 57 |
| 58 // Is a Android view system managed fling in progress? |
| 59 virtual bool IsFlingActive() const = 0; |
| 60 |
| 61 // Set the current page scale to |page_scale_factor| and page scale limits |
| 62 // to |min_page_scale_factor|..|max_page_scale_factor|. |
| 63 virtual void SetPageScaleFactorAndLimits(float page_scale_factor, |
| 64 float min_page_scale_factor, |
| 65 float max_page_scale_factor) = 0; |
| 66 |
| 67 // Set the current contents_size to |contents_size_dip|. |
| 68 virtual void SetContentsSize(gfx::SizeF contents_size_dip) = 0; |
| 69 |
| 70 // Handle overscroll. |
| 71 virtual void DidOverscroll(gfx::Vector2d overscroll_delta) = 0; |
| 72 |
| 73 protected: |
| 74 virtual ~BrowserViewRendererClient() {} |
| 75 }; |
| 76 |
| 77 // Delegate to perform rendering actions involving Java objects. |
| 78 class BrowserViewRendererJavaHelper { |
| 79 public: |
| 80 static BrowserViewRendererJavaHelper* GetInstance(); |
| 81 |
| 82 typedef base::Callback<bool(SkCanvas*)> RenderMethod; |
| 83 |
| 84 // Try obtaining the native SkCanvas from |java_canvas| and call |
| 85 // |render_source| with it. If that fails, allocate an auxilary bitmap |
| 86 // for |render_source| to render into, then copy the bitmap into |
| 87 // |java_canvas|. |
| 88 virtual bool RenderViaAuxilaryBitmapIfNeeded( |
| 89 jobject java_canvas, |
| 90 const gfx::Vector2d& scroll_correction, |
| 91 const gfx::Rect& clip, |
| 92 RenderMethod render_source) = 0; |
| 93 |
| 94 protected: |
| 95 virtual ~BrowserViewRendererJavaHelper() {} |
| 96 }; |
| 97 |
26 // Interface for all the WebView-specific content rendering operations. | 98 // Interface for all the WebView-specific content rendering operations. |
27 // Provides software and hardware rendering and the Capture Picture API. | 99 // Provides software and hardware rendering and the Capture Picture API. |
28 class BrowserViewRenderer { | 100 class BrowserViewRenderer : public content::SynchronousCompositorClient { |
29 public: | 101 public: |
30 class Client { | 102 BrowserViewRenderer(BrowserViewRendererClient* client, |
31 public: | 103 content::WebContents* web_contents); |
32 // Request DrawGL be called. Passing null canvas implies the request | 104 |
33 // will be of AwDrawGLInfo::kModeProcess type. The callback | 105 virtual ~BrowserViewRenderer(); |
34 // may never be made, and the mode may be promoted to kModeDraw. | |
35 virtual bool RequestDrawGL(jobject canvas) = 0; | |
36 | |
37 // Called when a new Picture is available. Needs to be enabled | |
38 // via the EnableOnNewPicture method. | |
39 virtual void OnNewPicture() = 0; | |
40 | |
41 // Called to trigger view invalidations. | |
42 virtual void PostInvalidate() = 0; | |
43 | |
44 // Synchronously call back to SetGlobalVisibleRect with current value. | |
45 virtual void UpdateGlobalVisibleRect() = 0; | |
46 | |
47 // Called to get view's absolute location on the screen. | |
48 virtual gfx::Point GetLocationOnScreen() = 0; | |
49 | |
50 // Try to set the view's scroll offset to |new_value|. | |
51 virtual void ScrollContainerViewTo(gfx::Vector2d new_value) = 0; | |
52 | |
53 // Set the view's scroll offset cap to |new_value|. | |
54 virtual void SetMaxContainerViewScrollOffset(gfx::Vector2d new_value) = 0; | |
55 | |
56 // Is a WebView-managed fling in progress? | |
57 virtual bool IsFlingActive() const = 0; | |
58 | |
59 // Set the current page scale to |page_scale_factor| and page scale limits | |
60 // to |min_page_scale_factor|..|max_page_scale_factor|. | |
61 virtual void SetPageScaleFactorAndLimits(float page_scale_factor, | |
62 float min_page_scale_factor, | |
63 float max_page_scale_factor) = 0; | |
64 | |
65 // Set the current contents_size to |contents_size_dip|. | |
66 virtual void SetContentsSize(gfx::SizeF contents_size_dip) = 0; | |
67 | |
68 // Handle overscroll. | |
69 virtual void DidOverscroll(gfx::Vector2d overscroll_delta) = 0; | |
70 | |
71 protected: | |
72 virtual ~Client() {} | |
73 }; | |
74 | |
75 // Delegate to perform rendering actions involving Java objects. | |
76 class JavaHelper { | |
77 public: | |
78 static JavaHelper* GetInstance(); | |
79 | |
80 typedef base::Callback<bool(SkCanvas*)> RenderMethod; | |
81 virtual bool RenderViaAuxilaryBitmapIfNeeded( | |
82 jobject java_canvas, | |
83 const gfx::Vector2d& scroll_correction, | |
84 const gfx::Rect& clip, | |
85 RenderMethod render_source) = 0; | |
86 | |
87 protected: | |
88 virtual ~JavaHelper() {} | |
89 }; | |
90 | |
91 // Global hookup methods. | |
92 static void SetAwDrawSWFunctionTable(AwDrawSWFunctionTable* table); | |
93 | |
94 // Rendering methods. | |
95 | 106 |
96 // Main handler for view drawing: performs a SW draw immediately, or sets up | 107 // Main handler for view drawing: performs a SW draw immediately, or sets up |
97 // a subsequent GL Draw (via Client::RequestDrawGL) and returns true. A false | 108 // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and |
98 // return value indicates nothing was or will be drawn. | 109 // returns true. A false return value indicates nothing was or will be drawn. |
99 // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates | 110 // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates |
100 // a GL Draw maybe possible on this canvas. |scroll| if the view's current | 111 // a GL Draw maybe possible on this canvas. |scroll| if the view's current |
101 // scroll offset. |clip| is the canvas's clip bounds. |visible_rect| is the | 112 // scroll offset. |clip| is the canvas's clip bounds. |visible_rect| is the |
102 // intersection of the view size and the window in window coordinates. | 113 // intersection of the view size and the window in window coordinates. |
103 virtual bool OnDraw(jobject java_canvas, | 114 bool OnDraw(jobject java_canvas, |
104 bool is_hardware_canvas, | 115 bool is_hardware_canvas, |
105 const gfx::Vector2d& scroll, | 116 const gfx::Vector2d& scroll, |
106 const gfx::Rect& clip) = 0; | 117 const gfx::Rect& clip); |
107 | 118 |
108 // Called in response to a prior Client::RequestDrawGL() call. See | 119 // Called in response to a prior BrowserViewRendererClient::RequestDrawGL() |
109 // AwDrawGLInfo documentation for more details of the contract. | 120 // call. See AwDrawGLInfo documentation for more details of the contract. |
110 virtual void DrawGL(AwDrawGLInfo* draw_info) = 0; | 121 void DrawGL(AwDrawGLInfo* draw_info); |
111 | 122 |
112 // The global visible rect changed and this is the new value. | 123 // The global visible rect changed and this is the new value. |
113 virtual void SetGlobalVisibleRect(const gfx::Rect& visible_rect) = 0; | 124 void SetGlobalVisibleRect(const gfx::Rect& visible_rect); |
114 | 125 |
115 // CapturePicture API methods. | 126 // CapturePicture API methods. |
116 virtual skia::RefPtr<SkPicture> CapturePicture(int width, int height) = 0; | 127 skia::RefPtr<SkPicture> CapturePicture(int width, int height); |
117 virtual void EnableOnNewPicture(bool enabled) = 0; | 128 void EnableOnNewPicture(bool enabled); |
118 | 129 |
119 virtual void ClearView() = 0; | 130 void ClearView(); |
120 | 131 |
121 // View update notifications. | 132 // View update notifications. |
122 virtual void SetIsPaused(bool paused) = 0; | 133 void SetIsPaused(bool paused); |
123 virtual void SetViewVisibility(bool visible) = 0; | 134 void SetViewVisibility(bool visible); |
124 virtual void SetWindowVisibility(bool visible) = 0; | 135 void SetWindowVisibility(bool visible); |
125 virtual void OnSizeChanged(int width, int height) = 0; | 136 void OnSizeChanged(int width, int height); |
126 virtual void OnAttachedToWindow(int width, int height) = 0; | 137 void OnAttachedToWindow(int width, int height); |
127 virtual void OnDetachedFromWindow() = 0; | 138 void OnDetachedFromWindow(); |
128 | 139 |
129 // Sets the scale for logical<->physical pixel conversions. | 140 // Sets the scale for logical<->physical pixel conversions. |
130 virtual void SetDipScale(float dip_scale) = 0; | 141 void SetDipScale(float dip_scale); |
131 | 142 |
132 // Set the root layer scroll offset to |new_value|. | 143 // Set the root layer scroll offset to |new_value|. |
133 virtual void ScrollTo(gfx::Vector2d new_value) = 0; | 144 void ScrollTo(gfx::Vector2d new_value); |
134 | 145 |
135 // Android views hierarchy gluing. | 146 // Android views hierarchy gluing. |
136 virtual bool IsAttachedToWindow() = 0; | 147 bool IsAttachedToWindow() const; |
137 virtual bool IsVisible() = 0; | 148 bool IsVisible() const; |
138 virtual gfx::Rect GetScreenRect() = 0; | 149 gfx::Rect GetScreenRect() const; |
139 | 150 |
140 // ComponentCallbacks2.onTrimMemory callback. | 151 // ComponentCallbacks2.onTrimMemory callback. |
141 virtual void TrimMemory(int level) = 0; | 152 void TrimMemory(int level); |
142 | 153 |
143 virtual ~BrowserViewRenderer() {} | 154 // SynchronousCompositorClient overrides |
| 155 virtual void DidInitializeCompositor( |
| 156 content::SynchronousCompositor* compositor) OVERRIDE; |
| 157 virtual void DidDestroyCompositor(content::SynchronousCompositor* compositor) |
| 158 OVERRIDE; |
| 159 virtual void SetContinuousInvalidate(bool invalidate) OVERRIDE; |
| 160 virtual void SetMaxRootLayerScrollOffset(gfx::Vector2dF new_value) OVERRIDE; |
| 161 virtual void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_css) |
| 162 OVERRIDE; |
| 163 virtual void DidUpdateContent() OVERRIDE; |
| 164 virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() OVERRIDE; |
| 165 virtual bool IsExternalFlingActive() const OVERRIDE; |
| 166 virtual void SetRootLayerPageScaleFactorAndLimits(float page_scale_factor, |
| 167 float min_page_scale_factor, |
| 168 float max_page_scale_factor) |
| 169 OVERRIDE; |
| 170 virtual void SetRootLayerScrollableSize(gfx::SizeF scrollable_size) OVERRIDE; |
| 171 virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll, |
| 172 gfx::Vector2dF latest_overscroll_delta, |
| 173 gfx::Vector2dF current_fling_velocity) OVERRIDE; |
| 174 |
| 175 private: |
| 176 // Checks the continuous invalidate and block invalidate state, and schedule |
| 177 // invalidates appropriately. If |invalidate_ignore_compositor| is true, |
| 178 // then send a view invalidate regardless of compositor expectation. |
| 179 void EnsureContinuousInvalidation(AwDrawGLInfo* draw_info, |
| 180 bool invalidate_ignore_compositor); |
| 181 bool DrawSWInternal(jobject java_canvas, const gfx::Rect& clip_bounds); |
| 182 bool CompositeSW(SkCanvas* canvas); |
| 183 |
| 184 // If we call up view invalidate and OnDraw is not called before a deadline, |
| 185 // then we keep ticking the SynchronousCompositor so it can make progress. |
| 186 void FallbackTickFired(); |
| 187 void ForceFakeCompositeSW(); |
| 188 |
| 189 gfx::Vector2d max_scroll_offset() const; |
| 190 |
| 191 // For debug tracing or logging. Return the string representation of this |
| 192 // view renderer's state and the |draw_info| if provided. |
| 193 std::string ToString(AwDrawGLInfo* draw_info) const; |
| 194 |
| 195 BrowserViewRendererClient* client_; |
| 196 content::WebContents* web_contents_; |
| 197 content::SynchronousCompositor* compositor_; |
| 198 |
| 199 scoped_ptr<HardwareRenderer> hardware_renderer_; |
| 200 |
| 201 bool is_paused_; |
| 202 bool view_visible_; |
| 203 bool window_visible_; // Only applicable if |attached_to_window_| is true. |
| 204 bool attached_to_window_; |
| 205 float dip_scale_; |
| 206 float page_scale_factor_; |
| 207 bool on_new_picture_enable_; |
| 208 bool clear_view_; |
| 209 |
| 210 // When true, we should continuously invalidate and keep drawing, for example |
| 211 // to drive animation. This value is set by the compositor and should always |
| 212 // reflect the expectation of the compositor and not be reused for other |
| 213 // states. |
| 214 bool compositor_needs_continuous_invalidate_; |
| 215 |
| 216 // Used to block additional invalidates while one is already pending or before |
| 217 // compositor draw which may switch continuous_invalidate on and off in the |
| 218 // process. |
| 219 bool block_invalidates_; |
| 220 |
| 221 // Holds a callback to FallbackTickFired while it is pending. |
| 222 base::CancelableClosure fallback_tick_; |
| 223 |
| 224 int width_; |
| 225 int height_; |
| 226 |
| 227 // Should always call UpdateGlobalVisibleRect before using this. |
| 228 gfx::Rect cached_global_visible_rect_; |
| 229 |
| 230 // Last View scroll when View.onDraw() was called. |
| 231 gfx::Vector2d scroll_at_start_of_frame_; |
| 232 |
| 233 // Current scroll offset in CSS pixels. |
| 234 gfx::Vector2dF scroll_offset_dip_; |
| 235 |
| 236 // Max scroll offset in CSS pixels. |
| 237 gfx::Vector2dF max_scroll_offset_dip_; |
| 238 |
| 239 // Used to prevent rounding errors from accumulating enough to generate |
| 240 // visible skew (especially noticeable when scrolling up and down in the same |
| 241 // spot over a period of time). |
| 242 gfx::Vector2dF overscroll_rounding_error_; |
| 243 |
| 244 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer); |
144 }; | 245 }; |
145 | 246 |
146 } // namespace android_webview | 247 } // namespace android_webview |
147 | 248 |
148 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ | 249 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_ |
OLD | NEW |