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/android/scoped_java_ref.h"
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/debug/trace_event.h"
14 #include "content/public/browser/android/synchronous_compositor.h"
15 #include "content/public/browser/android/synchronous_compositor_client.h"
16 #include "skia/ext/refptr.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/vector2d_f.h"
27 namespace android_webview {
29 class BrowserViewRendererClient;
31 // Delegate to perform rendering actions involving Java objects.
32 class BrowserViewRendererJavaHelper {
34 static BrowserViewRendererJavaHelper* GetInstance();
36 typedef base::Callback<bool(SkCanvas*)> RenderMethod;
38 // Try obtaining the native SkCanvas from |java_canvas| and call
39 // |render_source| with it. If that fails, allocate an auxilary bitmap
40 // for |render_source| to render into, then copy the bitmap into
42 virtual bool RenderViaAuxilaryBitmapIfNeeded(
44 const gfx::Vector2d& scroll_correction,
45 const gfx::Size& auxiliary_bitmap_size,
46 RenderMethod render_source) = 0;
49 virtual ~BrowserViewRendererJavaHelper() {}
52 // Interface for all the WebView-specific content rendering operations.
53 // Provides software and hardware rendering and the Capture Picture API.
54 class BrowserViewRenderer : public content::SynchronousCompositorClient {
56 static void CalculateTileMemoryPolicy();
59 BrowserViewRendererClient* client,
60 content::WebContents* web_contents,
61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
63 virtual ~BrowserViewRenderer();
65 intptr_t GetAwDrawGLViewContext();
66 bool RequestDrawGL(bool wait_for_completion);
68 // Main handler for view drawing: performs a SW draw immediately, or sets up
69 // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and
70 // returns true. A false return value indicates nothing was or will be drawn.
71 // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates
72 // a GL Draw maybe possible on this canvas. |scroll| if the view's current
73 // scroll offset. |clip| is the canvas's clip bounds. |global_visible_rect|
74 // is the intersection of the view size and the window in window coordinates.
75 bool OnDraw(jobject java_canvas,
76 bool is_hardware_canvas,
77 const gfx::Vector2d& scroll,
78 const gfx::Rect& global_visible_rect);
80 // CapturePicture API methods.
81 skia::RefPtr<SkPicture> CapturePicture(int width, int height);
82 void EnableOnNewPicture(bool enabled);
86 // View update notifications.
87 void SetIsPaused(bool paused);
88 void SetViewVisibility(bool visible);
89 void SetWindowVisibility(bool visible);
90 void OnSizeChanged(int width, int height);
91 void OnAttachedToWindow(int width, int height);
92 void OnDetachedFromWindow();
94 // Sets the scale for logical<->physical pixel conversions.
95 void SetDipScale(float dip_scale);
97 // Set the root layer scroll offset to |new_value|.
98 void ScrollTo(gfx::Vector2d new_value);
100 // Android views hierarchy gluing.
101 bool IsVisible() const;
102 gfx::Rect GetScreenRect() const;
103 bool attached_to_window() const { return attached_to_window_; }
104 bool hardware_enabled() const { return hardware_enabled_; }
105 void ReleaseHardware();
107 void TrimMemory(const int level, const bool visible);
109 // SynchronousCompositorClient overrides.
110 virtual void DidInitializeCompositor(
111 content::SynchronousCompositor* compositor) override;
112 virtual void DidDestroyCompositor(content::SynchronousCompositor* compositor)
114 virtual void SetContinuousInvalidate(bool invalidate) override;
115 virtual void DidUpdateContent() override;
116 virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() override;
117 virtual void UpdateRootLayerState(
118 const gfx::Vector2dF& total_scroll_offset_dip,
119 const gfx::Vector2dF& max_scroll_offset_dip,
120 const gfx::SizeF& scrollable_size_dip,
121 float page_scale_factor,
122 float min_page_scale_factor,
123 float max_page_scale_factor) override;
124 virtual bool IsExternalFlingActive() const override;
125 virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll,
126 gfx::Vector2dF latest_overscroll_delta,
127 gfx::Vector2dF current_fling_velocity) override;
129 void UpdateParentDrawConstraints();
130 void DidSkipCommitFrame();
131 void InvalidateOnFunctorDestroy();
134 void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_dip);
135 // Checks the continuous invalidate and block invalidate state, and schedule
136 // invalidates appropriately. If |force_invalidate| is true, then send a view
137 // invalidate regardless of compositor expectation. If |skip_reschedule_tick|
138 // is true and if there is already a pending fallback tick, don't reschedule
140 void EnsureContinuousInvalidation(bool force_invalidate,
141 bool skip_reschedule_tick);
142 bool OnDrawSoftware(jobject java_canvas);
143 bool CompositeSW(SkCanvas* canvas);
145 void DidSkipCompositeInDraw();
146 scoped_refptr<base::debug::ConvertableToTraceFormat> RootLayerStateAsValue(
147 const gfx::Vector2dF& total_scroll_offset_dip,
148 const gfx::SizeF& scrollable_size_dip);
150 bool OnDrawHardware();
151 scoped_ptr<cc::CompositorFrame> CompositeHw();
152 void ReturnUnusedResource(scoped_ptr<cc::CompositorFrame> frame);
153 void ReturnResourceFromParent();
155 // If we call up view invalidate and OnDraw is not called before a deadline,
156 // then we keep ticking the SynchronousCompositor so it can make progress.
157 // Do this in a two stage tick due to native MessageLoop favors delayed task,
158 // so ensure delayed task is inserted only after the draw task returns.
159 void PostFallbackTick();
160 void FallbackTickFired();
162 // Force invoke the compositor to run produce a 1x1 software frame that is
163 // immediately discarded. This is a hack to force invoke parts of the
164 // compositor that are not directly exposed here.
165 void ForceFakeCompositeSW();
167 gfx::Vector2d max_scroll_offset() const;
169 size_t CalculateDesiredMemoryPolicy();
170 // For debug tracing or logging. Return the string representation of this
171 // view renderer's state.
172 std::string ToString() const;
174 BrowserViewRendererClient* client_;
175 SharedRendererState shared_renderer_state_;
176 content::WebContents* web_contents_;
177 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
179 content::SynchronousCompositor* compositor_;
183 bool window_visible_; // Only applicable if |attached_to_window_| is true.
184 bool attached_to_window_;
185 bool hardware_enabled_;
187 float page_scale_factor_;
188 bool on_new_picture_enable_;
191 gfx::Vector2d last_on_draw_scroll_offset_;
192 gfx::Rect last_on_draw_global_visible_rect_;
194 // The draw constraints from the parent compositor. These are only used for
196 ParentCompositorDrawConstraints parent_draw_constraints_;
198 // When true, we should continuously invalidate and keep drawing, for example
199 // to drive animation. This value is set by the compositor and should always
200 // reflect the expectation of the compositor and not be reused for other
202 bool compositor_needs_continuous_invalidate_;
204 bool invalidate_after_composite_;
206 // Used to block additional invalidates while one is already pending.
207 bool block_invalidates_;
209 base::CancelableClosure post_fallback_tick_;
210 base::CancelableClosure fallback_tick_fired_;
211 bool fallback_tick_pending_;
216 // Current scroll offset in CSS pixels.
217 gfx::Vector2dF scroll_offset_dip_;
219 // Max scroll offset in CSS pixels.
220 gfx::Vector2dF max_scroll_offset_dip_;
222 // Used to prevent rounding errors from accumulating enough to generate
223 // visible skew (especially noticeable when scrolling up and down in the same
224 // spot over a period of time).
225 gfx::Vector2dF overscroll_rounding_error_;
227 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer);
230 } // namespace android_webview
232 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_