Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / browser_view_renderer.h
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.
4
5 #ifndef ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
6 #define ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
7
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 "base/values.h"
15 #include "content/public/browser/android/synchronous_compositor.h"
16 #include "content/public/browser/android/synchronous_compositor_client.h"
17 #include "skia/ext/refptr.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/vector2d_f.h"
20
21 class SkCanvas;
22 class SkPicture;
23 struct AwDrawGLInfo;
24 struct AwDrawSWFunctionTable;
25
26 namespace content {
27 class ContentViewCore;
28 struct SynchronousCompositorMemoryPolicy;
29 class WebContents;
30 }
31
32 namespace android_webview {
33
34 class BrowserViewRendererClient;
35
36 // Delegate to perform rendering actions involving Java objects.
37 class BrowserViewRendererJavaHelper {
38  public:
39   static BrowserViewRendererJavaHelper* GetInstance();
40
41   typedef base::Callback<bool(SkCanvas*)> RenderMethod;
42
43   // Try obtaining the native SkCanvas from |java_canvas| and call
44   // |render_source| with it. If that fails, allocate an auxilary bitmap
45   // for |render_source| to render into, then copy the bitmap into
46   // |java_canvas|.
47   virtual bool RenderViaAuxilaryBitmapIfNeeded(
48       jobject java_canvas,
49       const gfx::Vector2d& scroll_correction,
50       const gfx::Size& auxiliary_bitmap_size,
51       RenderMethod render_source) = 0;
52
53  protected:
54   virtual ~BrowserViewRendererJavaHelper() {}
55 };
56
57 // Interface for all the WebView-specific content rendering operations.
58 // Provides software and hardware rendering and the Capture Picture API.
59 class BrowserViewRenderer : public content::SynchronousCompositorClient,
60                             public GlobalTileManagerClient {
61  public:
62   static void CalculateTileMemoryPolicy();
63
64   BrowserViewRenderer(
65       BrowserViewRendererClient* client,
66       SharedRendererState* shared_renderer_state,
67       content::WebContents* web_contents,
68       const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
69
70   virtual ~BrowserViewRenderer();
71
72   // Main handler for view drawing: performs a SW draw immediately, or sets up
73   // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and
74   // returns true. A false return value indicates nothing was or will be drawn.
75   // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates
76   // a GL Draw maybe possible on this canvas. |scroll| if the view's current
77   // scroll offset. |clip| is the canvas's clip bounds. |global_visible_rect|
78   // is the intersection of the view size and the window in window coordinates.
79   bool OnDraw(jobject java_canvas,
80               bool is_hardware_canvas,
81               const gfx::Vector2d& scroll,
82               const gfx::Rect& global_visible_rect);
83
84   // CapturePicture API methods.
85   skia::RefPtr<SkPicture> CapturePicture(int width, int height);
86   void EnableOnNewPicture(bool enabled);
87
88   void ClearView();
89
90   // View update notifications.
91   void SetIsPaused(bool paused);
92   void SetViewVisibility(bool visible);
93   void SetWindowVisibility(bool visible);
94   void OnSizeChanged(int width, int height);
95   void OnAttachedToWindow(int width, int height);
96   void OnDetachedFromWindow();
97
98   // Sets the scale for logical<->physical pixel conversions.
99   void SetDipScale(float dip_scale);
100
101   // Set the root layer scroll offset to |new_value|.
102   void ScrollTo(gfx::Vector2d new_value);
103
104   // Android views hierarchy gluing.
105   bool IsVisible() const;
106   gfx::Rect GetScreenRect() const;
107   bool attached_to_window() const { return attached_to_window_; }
108   bool hardware_enabled() const { return hardware_enabled_; }
109   void ReleaseHardware();
110
111   // Set the memory policy in shared renderer state and request the tiles from
112   // GlobalTileManager. The actually amount of memory allowed by
113   // GlobalTileManager may not be equal to what's requested in |policy|.
114   void RequestMemoryPolicy(content::SynchronousCompositorMemoryPolicy& policy);
115
116   void TrimMemory(const int level, const bool visible);
117
118   // SynchronousCompositorClient overrides.
119   virtual void DidInitializeCompositor(
120       content::SynchronousCompositor* compositor) OVERRIDE;
121   virtual void DidDestroyCompositor(content::SynchronousCompositor* compositor)
122       OVERRIDE;
123   virtual void SetContinuousInvalidate(bool invalidate) OVERRIDE;
124   virtual void DidUpdateContent() OVERRIDE;
125   virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() OVERRIDE;
126   virtual void UpdateRootLayerState(
127       const gfx::Vector2dF& total_scroll_offset_dip,
128       const gfx::Vector2dF& max_scroll_offset_dip,
129       const gfx::SizeF& scrollable_size_dip,
130       float page_scale_factor,
131       float min_page_scale_factor,
132       float max_page_scale_factor) OVERRIDE;
133   virtual bool IsExternalFlingActive() const OVERRIDE;
134   virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll,
135                              gfx::Vector2dF latest_overscroll_delta,
136                              gfx::Vector2dF current_fling_velocity) OVERRIDE;
137
138   // GlobalTileManagerClient overrides.
139   virtual size_t GetNumTiles() const OVERRIDE;
140   virtual void SetNumTiles(size_t num_tiles,
141                            bool effective_immediately) OVERRIDE;
142
143  private:
144   void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_dip);
145   // Checks the continuous invalidate and block invalidate state, and schedule
146   // invalidates appropriately. If |force_invalidate| is true, then send a view
147   // invalidate regardless of compositor expectation.
148   void EnsureContinuousInvalidation(bool force_invalidate);
149   bool OnDrawSoftware(jobject java_canvas);
150   bool CompositeSW(SkCanvas* canvas);
151   void DidComposite();
152   scoped_ptr<base::Value> RootLayerStateAsValue(
153       const gfx::Vector2dF& total_scroll_offset_dip,
154       const gfx::SizeF& scrollable_size_dip);
155
156   bool OnDrawHardware(jobject java_canvas);
157   void ReturnUnusedResource(scoped_ptr<DrawGLInput> input);
158   void ReturnResourceFromParent();
159
160   // If we call up view invalidate and OnDraw is not called before a deadline,
161   // then we keep ticking the SynchronousCompositor so it can make progress.
162   // Do this in a two stage tick due to native MessageLoop favors delayed task,
163   // so ensure delayed task is inserted only after the draw task returns.
164   void PostFallbackTick();
165   void FallbackTickFired();
166
167   // Force invoke the compositor to run produce a 1x1 software frame that is
168   // immediately discarded. This is a hack to force invoke parts of the
169   // compositor that are not directly exposed here.
170   void ForceFakeCompositeSW();
171
172   void EnforceMemoryPolicyImmediately(
173       content::SynchronousCompositorMemoryPolicy policy);
174
175   gfx::Vector2d max_scroll_offset() const;
176
177   content::SynchronousCompositorMemoryPolicy CalculateDesiredMemoryPolicy();
178   // For debug tracing or logging. Return the string representation of this
179   // view renderer's state and the |draw_info| if provided.
180   std::string ToString(AwDrawGLInfo* draw_info) const;
181
182   BrowserViewRendererClient* client_;
183   SharedRendererState* shared_renderer_state_;
184   content::WebContents* web_contents_;
185   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
186
187   content::SynchronousCompositor* compositor_;
188
189   bool is_paused_;
190   bool view_visible_;
191   bool window_visible_;  // Only applicable if |attached_to_window_| is true.
192   bool attached_to_window_;
193   bool hardware_enabled_;
194   float dip_scale_;
195   float page_scale_factor_;
196   bool on_new_picture_enable_;
197   bool clear_view_;
198
199   gfx::Vector2d last_on_draw_scroll_offset_;
200   gfx::Rect last_on_draw_global_visible_rect_;
201
202   // When true, we should continuously invalidate and keep drawing, for example
203   // to drive animation. This value is set by the compositor and should always
204   // reflect the expectation of the compositor and not be reused for other
205   // states.
206   bool compositor_needs_continuous_invalidate_;
207
208   // Used to block additional invalidates while one is already pending.
209   bool block_invalidates_;
210
211   base::CancelableClosure post_fallback_tick_;
212   base::CancelableClosure fallback_tick_fired_;
213
214   int width_;
215   int height_;
216
217   // Current scroll offset in CSS pixels.
218   gfx::Vector2dF scroll_offset_dip_;
219
220   // Max scroll offset in CSS pixels.
221   gfx::Vector2dF max_scroll_offset_dip_;
222
223   // Used to prevent rounding errors from accumulating enough to generate
224   // visible skew (especially noticeable when scrolling up and down in the same
225   // spot over a period of time).
226   gfx::Vector2dF overscroll_rounding_error_;
227
228   GlobalTileManager::Key tile_manager_key_;
229   content::SynchronousCompositorMemoryPolicy memory_policy_;
230
231   // The following 2 are used to construct a memory policy and set the memory
232   // policy on the shared_renderer_state_ atomically.
233   size_t num_tiles_;
234   size_t num_bytes_;
235
236   DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer);
237 };
238
239 }  // namespace android_webview
240
241 #endif  // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_