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/shared_renderer_state.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/callback.h"
11 #include "base/cancelable_callback.h"
12 #include "content/public/browser/android/synchronous_compositor_client.h"
13 #include "skia/ext/refptr.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/vector2d_f.h"
20 struct AwDrawSWFunctionTable
;
23 class ContentViewCore
;
24 class SynchronousCompositor
;
28 namespace android_webview
{
30 class BrowserViewRendererClient
;
32 // Delegate to perform rendering actions involving Java objects.
33 class BrowserViewRendererJavaHelper
{
35 static BrowserViewRendererJavaHelper
* GetInstance();
37 typedef base::Callback
<bool(SkCanvas
*)> RenderMethod
;
39 // Try obtaining the native SkCanvas from |java_canvas| and call
40 // |render_source| with it. If that fails, allocate an auxilary bitmap
41 // for |render_source| to render into, then copy the bitmap into
43 virtual bool RenderViaAuxilaryBitmapIfNeeded(
45 const gfx::Vector2d
& scroll_correction
,
46 const gfx::Rect
& clip
,
47 RenderMethod render_source
) = 0;
50 virtual ~BrowserViewRendererJavaHelper() {}
53 // Interface for all the WebView-specific content rendering operations.
54 // Provides software and hardware rendering and the Capture Picture API.
55 class BrowserViewRenderer
: public content::SynchronousCompositorClient
{
58 BrowserViewRendererClient
* client
,
59 SharedRendererState
* shared_renderer_state
,
60 content::WebContents
* web_contents
,
61 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
);
63 virtual ~BrowserViewRenderer();
65 // Main handler for view drawing: performs a SW draw immediately, or sets up
66 // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and
67 // returns true. A false return value indicates nothing was or will be drawn.
68 // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates
69 // a GL Draw maybe possible on this canvas. |scroll| if the view's current
70 // scroll offset. |clip| is the canvas's clip bounds. |global_visible_rect|
71 // is the intersection of the view size and the window in window coordinates.
72 bool OnDraw(jobject java_canvas
,
73 bool is_hardware_canvas
,
74 const gfx::Vector2d
& scroll
,
75 const gfx::Rect
& global_visible_rect
,
76 const gfx::Rect
& clip
);
77 void DidDrawGL(const DrawGLResult
& result
);
79 // CapturePicture API methods.
80 skia::RefPtr
<SkPicture
> CapturePicture(int width
, int height
);
81 void EnableOnNewPicture(bool enabled
);
85 // View update notifications.
86 void SetIsPaused(bool paused
);
87 void SetViewVisibility(bool visible
);
88 void SetWindowVisibility(bool visible
);
89 void OnSizeChanged(int width
, int height
);
90 void OnAttachedToWindow(int width
, int height
);
91 void OnDetachedFromWindow();
93 // Sets the scale for logical<->physical pixel conversions.
94 void SetDipScale(float dip_scale
);
96 // Set the root layer scroll offset to |new_value|.
97 void ScrollTo(gfx::Vector2d new_value
);
99 // Android views hierarchy gluing.
100 bool IsAttachedToWindow() const;
101 bool IsVisible() const;
102 gfx::Rect
GetScreenRect() const;
104 // Force invoke the compositor to run produce a 1x1 software frame that is
105 // immediately discarded. This is a hack to force invoke parts of the
106 // compositor that are not directly exposed here.
107 void ForceFakeCompositeSW();
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 SetMaxRootLayerScrollOffset(gfx::Vector2dF new_value
) OVERRIDE
;
116 virtual void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_css
)
118 virtual void DidUpdateContent() OVERRIDE
;
119 virtual gfx::Vector2dF
GetTotalRootLayerScrollOffset() OVERRIDE
;
120 virtual bool IsExternalFlingActive() const OVERRIDE
;
121 virtual void SetRootLayerPageScaleFactorAndLimits(float page_scale_factor
,
122 float min_page_scale_factor
,
123 float max_page_scale_factor
)
125 virtual void SetRootLayerScrollableSize(gfx::SizeF scrollable_size
) OVERRIDE
;
126 virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll
,
127 gfx::Vector2dF latest_overscroll_delta
,
128 gfx::Vector2dF current_fling_velocity
) OVERRIDE
;
131 // Checks the continuous invalidate and block invalidate state, and schedule
132 // invalidates appropriately. If |force_invalidate| is true, then send a view
133 // invalidate regardless of compositor expectation.
134 void EnsureContinuousInvalidation(bool force_invalidate
);
135 bool DrawSWInternal(jobject java_canvas
, const gfx::Rect
& clip_bounds
);
136 bool CompositeSW(SkCanvas
* canvas
);
137 void DidComposite(bool force_invalidate
);
139 // If we call up view invalidate and OnDraw is not called before a deadline,
140 // then we keep ticking the SynchronousCompositor so it can make progress.
141 void FallbackTickFired();
143 gfx::Vector2d
max_scroll_offset() const;
145 // For debug tracing or logging. Return the string representation of this
146 // view renderer's state and the |draw_info| if provided.
147 std::string
ToString(AwDrawGLInfo
* draw_info
) const;
149 BrowserViewRendererClient
* client_
;
150 SharedRendererState
* shared_renderer_state_
;
151 content::WebContents
* web_contents_
;
152 // TODO(boliu): This class should only be used on the UI thread. However in
153 // short term to supporting HardwareRenderer, some callbacks on
154 // SynchronousCompositorClient may be called on non-UI thread. These are
155 // used to detect this and post them back to UI thread.
156 base::WeakPtrFactory
<BrowserViewRenderer
> weak_factory_on_ui_thread_
;
157 base::WeakPtr
<BrowserViewRenderer
> ui_thread_weak_ptr_
;
158 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
160 bool has_compositor_
;
164 bool window_visible_
; // Only applicable if |attached_to_window_| is true.
165 bool attached_to_window_
;
167 float page_scale_factor_
;
168 bool on_new_picture_enable_
;
171 // When true, we should continuously invalidate and keep drawing, for example
172 // to drive animation. This value is set by the compositor and should always
173 // reflect the expectation of the compositor and not be reused for other
175 bool compositor_needs_continuous_invalidate_
;
177 // Used to block additional invalidates while one is already pending.
178 bool block_invalidates_
;
180 // Holds a callback to FallbackTickFired while it is pending.
181 base::CancelableClosure fallback_tick_
;
186 DrawGLInput draw_gl_input_
;
188 // TODO(boliu): This is a short term solution to support
189 // SynchronousCompositorClient methods called on non-UI thread.
190 base::Lock scroll_offset_dip_lock_
;
191 // Current scroll offset in CSS pixels.
192 gfx::Vector2dF scroll_offset_dip_
;
194 // Max scroll offset in CSS pixels.
195 gfx::Vector2dF max_scroll_offset_dip_
;
197 // Used to prevent rounding errors from accumulating enough to generate
198 // visible skew (especially noticeable when scrolling up and down in the same
199 // spot over a period of time).
200 gfx::Vector2dF overscroll_rounding_error_
;
202 DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer
);
205 } // namespace android_webview
207 #endif // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_