render_surface_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2011 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 CC_LAYERS_RENDER_SURFACE_IMPL_H_
  5. #define CC_LAYERS_RENDER_SURFACE_IMPL_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/memory/raw_ptr.h"
  12. #include "cc/cc_export.h"
  13. #include "cc/document_transition/document_transition_shared_element_id.h"
  14. #include "cc/layers/draw_mode.h"
  15. #include "cc/layers/layer_collections.h"
  16. #include "cc/trees/occlusion.h"
  17. #include "cc/trees/property_tree.h"
  18. #include "components/viz/common/quads/compositor_render_pass.h"
  19. #include "components/viz/common/quads/shared_quad_state.h"
  20. #include "components/viz/common/surfaces/subtree_capture_id.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #include "ui/gfx/geometry/mask_filter_info.h"
  23. #include "ui/gfx/geometry/rect.h"
  24. #include "ui/gfx/geometry/rect_f.h"
  25. #include "ui/gfx/geometry/size.h"
  26. #include "ui/gfx/geometry/transform.h"
  27. namespace cc {
  28. class AppendQuadsData;
  29. class DamageTracker;
  30. class FilterOperations;
  31. class Occlusion;
  32. class LayerImpl;
  33. class LayerTreeImpl;
  34. class PictureLayerImpl;
  35. struct RenderSurfacePropertyChangedFlags {
  36. public:
  37. RenderSurfacePropertyChangedFlags() = default;
  38. RenderSurfacePropertyChangedFlags(bool self_changed, bool ancestor_changed)
  39. : self_changed_(self_changed), ancestor_changed_(ancestor_changed) {}
  40. bool self_changed() const { return self_changed_; }
  41. bool ancestor_changed() const { return ancestor_changed_; }
  42. private:
  43. bool self_changed_ = false;
  44. bool ancestor_changed_ = false;
  45. };
  46. class CC_EXPORT RenderSurfaceImpl {
  47. public:
  48. RenderSurfaceImpl(LayerTreeImpl* layer_tree_impl, uint64_t stable_id);
  49. RenderSurfaceImpl(const RenderSurfaceImpl&) = delete;
  50. virtual ~RenderSurfaceImpl();
  51. RenderSurfaceImpl& operator=(const RenderSurfaceImpl&) = delete;
  52. // Returns the RenderSurfaceImpl that this render surface contributes to. Root
  53. // render surface's render_target is itself.
  54. RenderSurfaceImpl* render_target();
  55. const RenderSurfaceImpl* render_target() const;
  56. // Returns the rect that encloses the RenderSurfaceImpl including any
  57. // reflection.
  58. gfx::RectF DrawableContentRect() const;
  59. void SetDrawOpacity(float opacity) {
  60. draw_properties_.draw_opacity = opacity;
  61. }
  62. float draw_opacity() const { return draw_properties_.draw_opacity; }
  63. void SetMaskFilterInfo(const gfx::MaskFilterInfo& mask_filter_info) {
  64. draw_properties_.mask_filter_info = mask_filter_info;
  65. }
  66. const gfx::MaskFilterInfo& mask_filter_info() const {
  67. return draw_properties_.mask_filter_info;
  68. }
  69. SkBlendMode BlendMode() const;
  70. void SetNearestOcclusionImmuneAncestor(const RenderSurfaceImpl* surface) {
  71. nearest_occlusion_immune_ancestor_ = surface;
  72. }
  73. const RenderSurfaceImpl* nearest_occlusion_immune_ancestor() const {
  74. return nearest_occlusion_immune_ancestor_;
  75. }
  76. SkColor4f GetDebugBorderColor() const;
  77. float GetDebugBorderWidth() const;
  78. void SetDrawTransform(const gfx::Transform& draw_transform) {
  79. draw_properties_.draw_transform = draw_transform;
  80. }
  81. const gfx::Transform& draw_transform() const {
  82. return draw_properties_.draw_transform;
  83. }
  84. void SetScreenSpaceTransform(const gfx::Transform& screen_space_transform) {
  85. draw_properties_.screen_space_transform = screen_space_transform;
  86. }
  87. const gfx::Transform& screen_space_transform() const {
  88. return draw_properties_.screen_space_transform;
  89. }
  90. void SetIsClipped(bool is_clipped) {
  91. draw_properties_.is_clipped = is_clipped;
  92. }
  93. bool is_clipped() const { return draw_properties_.is_clipped; }
  94. void SetClipRect(const gfx::Rect& clip_rect);
  95. gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
  96. // When false, the RenderSurface does not contribute to another target
  97. // RenderSurface that is being drawn for the current frame. It could still be
  98. // drawn to as a target, but its output will not be a part of any other
  99. // surface.
  100. bool contributes_to_drawn_surface() const {
  101. return contributes_to_drawn_surface_;
  102. }
  103. void set_contributes_to_drawn_surface(bool contributes_to_drawn_surface) {
  104. contributes_to_drawn_surface_ = contributes_to_drawn_surface;
  105. }
  106. void set_has_contributing_layer_that_escapes_clip(
  107. bool contributing_layer_escapes_clip) {
  108. has_contributing_layer_that_escapes_clip_ = contributing_layer_escapes_clip;
  109. }
  110. bool has_contributing_layer_that_escapes_clip() const {
  111. return has_contributing_layer_that_escapes_clip_;
  112. }
  113. void set_is_render_surface_list_member(bool is_render_surface_list_member) {
  114. is_render_surface_list_member_ = is_render_surface_list_member;
  115. }
  116. bool is_render_surface_list_member() const {
  117. return is_render_surface_list_member_;
  118. }
  119. void set_intersects_damage_under(bool intersects_damage_under) {
  120. intersects_damage_under_ = intersects_damage_under;
  121. }
  122. bool intersects_damage_under() const { return intersects_damage_under_; }
  123. void CalculateContentRectFromAccumulatedContentRect(int max_texture_size);
  124. void SetContentRectToViewport();
  125. void SetContentRectForTesting(const gfx::Rect& rect);
  126. gfx::Rect content_rect() const { return draw_properties_.content_rect; }
  127. void ClearAccumulatedContentRect();
  128. void AccumulateContentRectFromContributingLayer(
  129. LayerImpl* contributing_layer);
  130. void AccumulateContentRectFromContributingRenderSurface(
  131. RenderSurfaceImpl* contributing_surface);
  132. gfx::Rect accumulated_content_rect() const {
  133. return accumulated_content_rect_;
  134. }
  135. void increment_num_contributors() { num_contributors_++; }
  136. void decrement_num_contributors() {
  137. num_contributors_--;
  138. DCHECK_GE(num_contributors_, 0);
  139. }
  140. void reset_num_contributors() { num_contributors_ = 0; }
  141. int num_contributors() const { return num_contributors_; }
  142. const Occlusion& occlusion_in_content_space() const {
  143. return occlusion_in_content_space_;
  144. }
  145. void set_occlusion_in_content_space(const Occlusion& occlusion) {
  146. occlusion_in_content_space_ = occlusion;
  147. }
  148. uint64_t id() const { return stable_id_; }
  149. viz::CompositorRenderPassId render_pass_id() const {
  150. return viz::CompositorRenderPassId{id()};
  151. }
  152. bool HasMaskingContributingSurface() const;
  153. const FilterOperations& Filters() const;
  154. const FilterOperations& BackdropFilters() const;
  155. absl::optional<gfx::RRectF> BackdropFilterBounds() const;
  156. LayerImpl* BackdropMaskLayer() const;
  157. gfx::Transform SurfaceScale() const;
  158. bool TrilinearFiltering() const;
  159. bool HasCopyRequest() const;
  160. // The capture identifier for this render surface and its originating effect
  161. // node. If empty, this surface has not been selected as a subtree capture and
  162. // is either a root surface or will not be rendered separately.
  163. viz::SubtreeCaptureId SubtreeCaptureId() const;
  164. // The size of this surface that should be used for cropping capture. If
  165. // empty, the entire size of this surface should be used for capture.
  166. gfx::Size SubtreeSize() const;
  167. bool ShouldCacheRenderSurface() const;
  168. // Returns true if it's required to copy the output of this surface (i.e. when
  169. // it has copy requests, should be cached, or has a valid subtree capture ID),
  170. // and should be e.g. immune from occlusion, etc. Returns false otherise.
  171. bool CopyOfOutputRequired() const;
  172. // These are to enable commit, where we need to snapshot these flags from the
  173. // main thread property trees, and then apply them to the sync tree.
  174. RenderSurfacePropertyChangedFlags GetPropertyChangeFlags() const;
  175. void ApplyPropertyChangeFlags(const RenderSurfacePropertyChangedFlags& flags);
  176. void ResetPropertyChangedFlags();
  177. bool SurfacePropertyChanged() const;
  178. bool SurfacePropertyChangedOnlyFromDescendant() const;
  179. bool AncestorPropertyChanged() const;
  180. void NoteAncestorPropertyChanged();
  181. bool HasDamageFromeContributingContent() const;
  182. DamageTracker* damage_tracker() const { return damage_tracker_.get(); }
  183. gfx::Rect GetDamageRect() const;
  184. std::unique_ptr<viz::CompositorRenderPass> CreateRenderPass();
  185. viz::ResourceId GetMaskResourceFromLayer(PictureLayerImpl* mask_layer,
  186. gfx::Size* mask_texture_size,
  187. gfx::RectF* mask_uv_rect) const;
  188. void AppendQuads(DrawMode draw_mode,
  189. viz::CompositorRenderPass* render_pass,
  190. AppendQuadsData* append_quads_data);
  191. int TransformTreeIndex() const;
  192. int ClipTreeIndex() const;
  193. void set_effect_tree_index(int index) { effect_tree_index_ = index; }
  194. int EffectTreeIndex() const;
  195. const EffectNode* OwningEffectNode() const;
  196. const DocumentTransitionSharedElementId&
  197. GetDocumentTransitionSharedElementId() const;
  198. private:
  199. void SetContentRect(const gfx::Rect& content_rect);
  200. gfx::Rect CalculateClippedAccumulatedContentRect();
  201. gfx::Rect CalculateExpandedClipForFilters(
  202. const gfx::Transform& target_to_surface);
  203. void TileMaskLayer(viz::CompositorRenderPass* render_pass,
  204. viz::SharedQuadState* shared_quad_state,
  205. const gfx::Rect& unoccluded_content_rect);
  206. raw_ptr<LayerTreeImpl> layer_tree_impl_;
  207. uint64_t stable_id_;
  208. int effect_tree_index_;
  209. // Container for properties that render surfaces need to compute before they
  210. // can be drawn.
  211. struct DrawProperties {
  212. DrawProperties();
  213. ~DrawProperties();
  214. float draw_opacity;
  215. // Transforms from the surface's own space to the space of its target
  216. // surface.
  217. gfx::Transform draw_transform;
  218. // Transforms from the surface's own space to the viewport.
  219. gfx::Transform screen_space_transform;
  220. // This is in the surface's own space.
  221. gfx::Rect content_rect;
  222. // This is in the space of the surface's target surface.
  223. gfx::Rect clip_rect;
  224. // True if the surface needs to be clipped by clip_rect.
  225. bool is_clipped : 1;
  226. // Contains a mask information applied to the layer. The coordinates is in
  227. // the target space of the render surface. The root render surface will
  228. // never have this set.
  229. gfx::MaskFilterInfo mask_filter_info;
  230. };
  231. DrawProperties draw_properties_;
  232. // Is used to calculate the content rect from property trees.
  233. gfx::Rect accumulated_content_rect_;
  234. int num_contributors_;
  235. // Is used to decide if the surface is clipped.
  236. bool has_contributing_layer_that_escapes_clip_ : 1;
  237. bool surface_property_changed_ : 1;
  238. bool ancestor_property_changed_ : 1;
  239. bool contributes_to_drawn_surface_ : 1;
  240. bool is_render_surface_list_member_ : 1;
  241. bool intersects_damage_under_ : 1;
  242. Occlusion occlusion_in_content_space_;
  243. // The nearest ancestor target surface that will contain the contents of this
  244. // surface, and that ignores outside occlusion. This can point to itself.
  245. raw_ptr<const RenderSurfaceImpl> nearest_occlusion_immune_ancestor_;
  246. std::unique_ptr<DamageTracker> damage_tracker_;
  247. };
  248. } // namespace cc
  249. #endif // CC_LAYERS_RENDER_SURFACE_IMPL_H_