browser_view_renderer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #ifndef ANDROID_WEBVIEW_BROWSER_GFX_BROWSER_VIEW_RENDERER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_GFX_BROWSER_VIEW_RENDERER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <set>
  9. #include "android_webview/browser/gfx/begin_frame_source_webview.h"
  10. #include "android_webview/browser/gfx/child_frame.h"
  11. #include "android_webview/browser/gfx/compositor_frame_producer.h"
  12. #include "android_webview/browser/gfx/parent_compositor_draw_constraints.h"
  13. #include "android_webview/browser/gfx/root_frame_sink_proxy.h"
  14. #include "base/callback.h"
  15. #include "base/cancelable_callback.h"
  16. #include "base/memory/raw_ptr.h"
  17. #include "base/memory/ref_counted.h"
  18. #include "base/trace_event/trace_event.h"
  19. #include "components/viz/common/surfaces/frame_sink_id.h"
  20. #include "content/public/browser/android/synchronous_compositor.h"
  21. #include "content/public/browser/android/synchronous_compositor_client.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. #include "third_party/skia/include/core/SkRefCnt.h"
  24. #include "ui/gfx/geometry/point_f.h"
  25. #include "ui/gfx/geometry/rect.h"
  26. #include "ui/gfx/geometry/size_f.h"
  27. #include "ui/gfx/geometry/vector2d_f.h"
  28. class SkCanvas;
  29. class SkPicture;
  30. namespace content {
  31. class WebContents;
  32. }
  33. namespace android_webview {
  34. class BrowserViewRendererClient;
  35. class ChildFrame;
  36. class CompositorFrameConsumer;
  37. class RootFrameSinkProxy;
  38. // Interface for all the WebView-specific content rendering operations.
  39. // Provides software and hardware rendering and the Capture Picture API.
  40. class BrowserViewRenderer : public content::SynchronousCompositorClient,
  41. public CompositorFrameProducer,
  42. public RootFrameSinkProxyClient {
  43. public:
  44. static void CalculateTileMemoryPolicy();
  45. static BrowserViewRenderer* FromWebContents(
  46. content::WebContents* web_contents);
  47. BrowserViewRenderer(
  48. BrowserViewRendererClient* client,
  49. const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
  50. BrowserViewRenderer(const BrowserViewRenderer&) = delete;
  51. BrowserViewRenderer& operator=(const BrowserViewRenderer&) = delete;
  52. ~BrowserViewRenderer() override;
  53. void RegisterWithWebContents(content::WebContents* web_contents);
  54. // The BrowserViewRenderer client is responsible for ensuring that
  55. // the current compositor frame consumer has been set correctly via
  56. // this method. The consumer is added to the set of registered
  57. // consumers if it is not already registered.
  58. void SetCurrentCompositorFrameConsumer(
  59. CompositorFrameConsumer* compositor_frame_consumer);
  60. // Called before either OnDrawHardware or OnDrawSoftware to set the view
  61. // state of this frame. |scroll| is the view's current scroll offset.
  62. // |global_visible_rect| is the intersection of the view size and the window
  63. // in window coordinates.
  64. void PrepareToDraw(const gfx::Point& scroll,
  65. const gfx::Rect& global_visible_rect);
  66. // Main handlers for view drawing. A false return value indicates no new
  67. // frame is produced.
  68. bool OnDrawHardware();
  69. bool OnDrawSoftware(SkCanvas* canvas);
  70. bool NeedToDrawBackgroundColor();
  71. // CapturePicture API methods.
  72. sk_sp<SkPicture> CapturePicture(int width, int height);
  73. void EnableOnNewPicture(bool enabled);
  74. void ClearView();
  75. void SetOffscreenPreRaster(bool enabled);
  76. // View update notifications.
  77. void SetIsPaused(bool paused);
  78. void SetViewVisibility(bool visible);
  79. void SetWindowVisibility(bool visible);
  80. void OnSizeChanged(int width, int height);
  81. void OnAttachedToWindow(int width, int height);
  82. void OnDetachedFromWindow();
  83. void ZoomBy(float delta);
  84. void OnComputeScroll(base::TimeTicks animation_time);
  85. // Sets the scale for logical<->physical pixel conversions.
  86. void SetDipScale(float dip_scale);
  87. float dip_scale() const { return dip_scale_; }
  88. float page_scale_factor() const { return page_scale_factor_; }
  89. // Set the root layer scroll offset to |new_value|. The |new_value| here is in
  90. // physical pixel.
  91. void ScrollTo(const gfx::Point& new_value);
  92. // Set root layer scroll offset on the next scroll state update.
  93. void RestoreScrollAfterTransition(const gfx::Point& new_value);
  94. // Android views hierarchy gluing.
  95. bool IsVisible() const;
  96. gfx::Rect GetScreenRect() const;
  97. bool view_visible() const { return view_visible_; }
  98. bool window_visible() const { return window_visible_; }
  99. bool attached_to_window() const { return attached_to_window_; }
  100. bool was_attached() const { return was_attached_; }
  101. gfx::Size size() const { return size_; }
  102. bool IsClientVisible() const;
  103. void TrimMemory();
  104. // SynchronousCompositorClient overrides.
  105. void DidInitializeCompositor(content::SynchronousCompositor* compositor,
  106. const viz::FrameSinkId& frame_sink_id) override;
  107. void DidDestroyCompositor(content::SynchronousCompositor* compositor,
  108. const viz::FrameSinkId& frame_sink_id) override;
  109. void PostInvalidate(content::SynchronousCompositor* compositor) override;
  110. void DidUpdateContent(content::SynchronousCompositor* compositor) override;
  111. void OnInputEvent();
  112. // |total_scroll_offset|, |total_max_scroll_offset|, and |scrollable_size| are
  113. // in DIP scale when --use-zoom-for-dsf is disabled. Otherwise, they are in
  114. // physical pixel scale.
  115. void UpdateRootLayerState(content::SynchronousCompositor* compositor,
  116. const gfx::PointF& total_scroll_offset,
  117. const gfx::PointF& total_max_scroll_offset,
  118. const gfx::SizeF& scrollable_size,
  119. float page_scale_factor,
  120. float min_page_scale_factor,
  121. float max_page_scale_factor) override;
  122. void DidOverscroll(content::SynchronousCompositor* compositor,
  123. const gfx::Vector2dF& accumulated_overscroll,
  124. const gfx::Vector2dF& latest_overscroll_delta,
  125. const gfx::Vector2dF& current_fling_velocity) override;
  126. ui::TouchHandleDrawable* CreateDrawable() override;
  127. void CopyOutput(
  128. content::SynchronousCompositor* compositor,
  129. std::unique_ptr<viz::CopyOutputRequest> copy_request) override;
  130. void AddBeginFrameCompletionCallback(base::OnceClosure callback) override;
  131. // CompositorFrameProducer overrides
  132. base::WeakPtr<CompositorFrameProducer> GetWeakPtr() override;
  133. void RemoveCompositorFrameConsumer(
  134. CompositorFrameConsumer* consumer) override;
  135. void ReturnUsedResources(std::vector<viz::ReturnedResource> resources,
  136. const viz::FrameSinkId& frame_sink_id,
  137. uint32_t layer_tree_frame_sink_id) override;
  138. void OnParentDrawDataUpdated(
  139. CompositorFrameConsumer* compositor_frame_consumer) override;
  140. void OnViewTreeForceDarkStateChanged(
  141. bool view_tree_force_dark_state) override;
  142. void ChildSurfaceWasEvicted() override;
  143. void SetActiveFrameSinkId(const viz::FrameSinkId& frame_sink_id);
  144. // RootFrameSinkProxy overrides
  145. void Invalidate() override;
  146. void ReturnResourcesFromViz(
  147. viz::FrameSinkId frame_sink_id,
  148. uint32_t layer_tree_frame_sink_id,
  149. std::vector<viz::ReturnedResource> resources) override;
  150. void OnCompositorFrameTransitionDirectiveProcessed(
  151. viz::FrameSinkId frame_sink_id,
  152. uint32_t layer_tree_frame_sink_id,
  153. uint32_t sequence_id) override;
  154. // Visible for testing.
  155. content::SynchronousCompositor* GetActiveCompositorForTesting() const {
  156. return compositor_;
  157. }
  158. bool window_visible_for_tests() const { return window_visible_; }
  159. private:
  160. void SetActiveCompositor(content::SynchronousCompositor* compositor);
  161. void SetTotalRootLayerScrollOffset(const gfx::PointF& new_value_dip);
  162. bool CanOnDraw();
  163. bool CompositeSW(SkCanvas* canvas, bool software_canvas);
  164. std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
  165. RootLayerStateAsValue(const gfx::PointF& total_scroll_offset_dip,
  166. const gfx::SizeF& scrollable_size_dip);
  167. void ReturnUncommittedFrames(ChildFrameQueue frame);
  168. void ReturnUnusedResource(std::unique_ptr<ChildFrame> frame);
  169. void ReturnResourceFromParent(
  170. CompositorFrameConsumer* compositor_frame_consumer);
  171. void ReleaseHardware();
  172. bool DoUpdateParentDrawData();
  173. void UpdateBeginFrameSource();
  174. gfx::Point max_scroll_offset() const;
  175. // Return the tile rect in view space.
  176. gfx::Rect ComputeTileRectAndUpdateMemoryPolicy();
  177. content::SynchronousCompositor* FindCompositor(
  178. const viz::FrameSinkId& frame_sink_id) const;
  179. // For debug tracing or logging. Return the string representation of this
  180. // view renderer's state.
  181. std::string ToString() const;
  182. const raw_ptr<BrowserViewRendererClient> client_;
  183. const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
  184. raw_ptr<CompositorFrameConsumer> current_compositor_frame_consumer_;
  185. std::unique_ptr<RootFrameSinkProxy> root_frame_sink_proxy_;
  186. // The current compositor that's owned by the current RVH.
  187. raw_ptr<content::SynchronousCompositor> compositor_;
  188. // The id of the most recent RVH according to RVHChanged.
  189. viz::FrameSinkId frame_sink_id_;
  190. // A map from compositor's per-WebView unique ID to the compositor's raw
  191. // pointer. A raw pointer here is fine because the entry will be erased when
  192. // a compositor is destroyed.
  193. std::map<viz::FrameSinkId, content::SynchronousCompositor*> compositor_map_;
  194. bool is_paused_;
  195. bool view_visible_;
  196. bool window_visible_; // Only applicable if |attached_to_window_| is true.
  197. bool attached_to_window_;
  198. bool was_attached_; // Whether the view was attached to window at least once.
  199. bool hardware_enabled_;
  200. float dip_scale_;
  201. float page_scale_factor_;
  202. float min_page_scale_factor_;
  203. float max_page_scale_factor_;
  204. bool on_new_picture_enable_;
  205. bool clear_view_;
  206. // Used for metrics, indicates if we called invalidate since last draw.
  207. bool did_invalidate_since_last_draw_ = false;
  208. // Approximates whether render thread functor has a frame to draw. It is safe
  209. // for Java side to stop blitting the background color once this is true.
  210. bool has_rendered_frame_ = false;
  211. bool offscreen_pre_raster_;
  212. CopyOutputRequestQueue copy_requests_;
  213. gfx::Point last_on_draw_scroll_offset_;
  214. gfx::Rect last_on_draw_global_visible_rect_;
  215. gfx::Size size_;
  216. gfx::SizeF scrollable_size_dip_;
  217. // When zoom-for-dsf enabled |max_scroll_offset_unscaled_| and
  218. // |scroll_offset_unscaled_| is in physical pixel; otherwise, they are in dip
  219. gfx::PointF scroll_offset_unscaled_;
  220. gfx::PointF max_scroll_offset_unscaled_;
  221. // Used to prevent rounding errors from accumulating enough to generate
  222. // visible skew (especially noticeable when scrolling up and down in the same
  223. // spot over a period of time).
  224. gfx::Vector2dF overscroll_rounding_error_;
  225. // The scroll to apply after the next scroll state update.
  226. absl::optional<gfx::Point> scroll_on_scroll_state_update_;
  227. ParentCompositorDrawConstraints external_draw_constraints_;
  228. std::unique_ptr<BeginFrameSourceWebView> begin_frame_source_;
  229. base::WeakPtrFactory<CompositorFrameProducer> weak_ptr_factory_{this};
  230. };
  231. } // namespace android_webview
  232. #endif // ANDROID_WEBVIEW_BROWSER_GFX_BROWSER_VIEW_RENDERER_H_