ca_renderer_layer_tree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright 2016 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 UI_ACCELERATED_WIDGET_MAC_CA_RENDERER_LAYER_TREE_H_
  5. #define UI_ACCELERATED_WIDGET_MAC_CA_RENDERER_LAYER_TREE_H_
  6. #include <IOSurface/IOSurface.h>
  7. #include <QuartzCore/QuartzCore.h>
  8. #include <list>
  9. #include <memory>
  10. #include "base/containers/flat_map.h"
  11. #include "base/mac/scoped_cftyperef.h"
  12. #include "base/mac/scoped_nsobject.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "ui/accelerated_widget_mac/accelerated_widget_mac_export.h"
  17. #include "ui/gfx/geometry/rect.h"
  18. #include "ui/gfx/geometry/rect_f.h"
  19. #include "ui/gfx/geometry/rrect_f.h"
  20. #include "ui/gfx/geometry/transform.h"
  21. #include "ui/gfx/hdr_metadata.h"
  22. #include "ui/gfx/mac/io_surface.h"
  23. #include "ui/gfx/video_types.h"
  24. @class AVSampleBufferDisplayLayer;
  25. namespace ui {
  26. struct CARendererLayerParams;
  27. enum class CALayerType {
  28. // A CALayer with contents set to an IOSurface by setContents.
  29. kDefault,
  30. // An AVSampleBufferDisplayLayer.
  31. kVideo,
  32. // A CAMetalLayer that copies half-float or 10-bit IOSurfaces.
  33. kHDRCopier,
  34. };
  35. // The CARendererLayerTree will construct a hierarchy of CALayers from a linear
  36. // list provided by the CoreAnimation renderer using the algorithm and structure
  37. // referenced described in
  38. // https://docs.google.com/document/d/1DtSN9zzvCF44_FQPM7ie01UxGHagQ66zfF5L9HnigQY/edit?usp=sharing
  39. class ACCELERATED_WIDGET_MAC_EXPORT CARendererLayerTree {
  40. public:
  41. CARendererLayerTree(bool allow_av_sample_buffer_display_layer,
  42. bool allow_solid_color_layers);
  43. CARendererLayerTree(const CARendererLayerTree&) = delete;
  44. CARendererLayerTree& operator=(const CARendererLayerTree&) = delete;
  45. // This will remove all CALayers from this tree from their superlayer.
  46. ~CARendererLayerTree();
  47. // Append the description of a new CALayer to the tree. This will not
  48. // create any new CALayers until CommitScheduledCALayers is called. This
  49. // cannot be called anymore after CommitScheduledCALayers has been called.
  50. bool ScheduleCALayer(const CARendererLayerParams& params);
  51. // Create a CALayer tree for the scheduled layers, and set |superlayer| to
  52. // have only this tree as its sublayers. If |old_tree| is non-null, then try
  53. // to re-use the CALayers of |old_tree| as much as possible. |old_tree| will
  54. // be destroyed at the end of the function, and any CALayers in it which were
  55. // not re-used by |this| will be removed from the CALayer hierarchy.
  56. void CommitScheduledCALayers(CALayer* superlayer,
  57. std::unique_ptr<CARendererLayerTree> old_tree,
  58. const gfx::Size& pixel_size,
  59. float scale_factor);
  60. // Returns the contents used for a given solid color.
  61. id ContentsForSolidColorForTesting(unsigned int color);
  62. // If there exists only a single content layer, return the IOSurface of that
  63. // layer.
  64. IOSurfaceRef GetContentIOSurface() const;
  65. private:
  66. class SolidColorContents;
  67. class RootLayer;
  68. class ClipAndSortingLayer;
  69. class TransformLayer;
  70. class ContentLayer;
  71. friend class ContentLayer;
  72. using CALayerMap = base::flat_map<IOSurfaceRef, base::WeakPtr<ContentLayer>>;
  73. void MatchLayersToOldTreeDefault(CARendererLayerTree* old_tree);
  74. void MatchLayersToOldTree(CARendererLayerTree* old_tree);
  75. void VerifyCommittedCALayers();
  76. class RootLayer {
  77. public:
  78. RootLayer(CARendererLayerTree* tree);
  79. RootLayer(RootLayer&&) = delete;
  80. RootLayer(const RootLayer&) = delete;
  81. RootLayer& operator=(const RootLayer&) = delete;
  82. // This will remove |ca_layer| from its superlayer, if |ca_layer| is
  83. // non-nil.
  84. ~RootLayer();
  85. // Append a new content layer, without modifying the actual CALayer
  86. // structure.
  87. bool AddContentLayer(const CARendererLayerParams& params);
  88. // Downgrade all downgradeable AVSampleBufferDisplayLayers to be normal
  89. // CALayers.
  90. // https://crbug.com/923427, https://crbug.com/1143477
  91. void DowngradeAVLayersToCALayers();
  92. // Allocate CALayers for this layer and its children, and set their
  93. // properties appropriately. Re-use the CALayers from |old_layer| if
  94. // possible. If re-using a CALayer from |old_layer|, reset its |ca_layer|
  95. // to nil, so that its destructor will not remove an active CALayer.
  96. void CommitToCA(CALayer* superlayer, const gfx::Size& pixel_size);
  97. void CALayerFallBack();
  98. // Return true if the CALayer tree is just a video layer on a black or
  99. // transparent background, false otherwise.
  100. bool WantsFullscreenLowPowerBackdrop() const;
  101. // Tree that owns `this`.
  102. const raw_ptr<CARendererLayerTree> tree_;
  103. std::list<ClipAndSortingLayer> clip_and_sorting_layers_;
  104. base::scoped_nsobject<CALayer> ca_layer_;
  105. // Weak pointer to the layer in the old CARendererLayerTree that will be
  106. // reused by this layer, and the weak factory used to make that pointer.
  107. base::WeakPtr<RootLayer> old_layer_;
  108. base::WeakPtrFactory<RootLayer> weak_factory_for_new_layer_{this};
  109. };
  110. class ClipAndSortingLayer {
  111. public:
  112. ClipAndSortingLayer(RootLayer* root_layer,
  113. bool is_clipped,
  114. gfx::Rect clip_rect,
  115. gfx::RRectF rounded_corner_bounds,
  116. unsigned sorting_context_id,
  117. bool is_singleton_sorting_context);
  118. ClipAndSortingLayer(ClipAndSortingLayer&& layer) = delete;
  119. ClipAndSortingLayer(const ClipAndSortingLayer&) = delete;
  120. ClipAndSortingLayer& operator=(const ClipAndSortingLayer&) = delete;
  121. // See the behavior of RootLayer for the effects of these functions on the
  122. // |ca_layer| member and |old_layer| argument.
  123. ~ClipAndSortingLayer();
  124. void AddContentLayer(const CARendererLayerParams& params);
  125. void CommitToCA(CALayer* last_committed_clip_ca_layer);
  126. void CALayerFallBack();
  127. CARendererLayerTree* tree() { return parent_layer_->tree_; }
  128. // Parent layer that owns `this`, and child layers that `this` owns.
  129. const raw_ptr<RootLayer> parent_layer_;
  130. std::list<TransformLayer> transform_layers_;
  131. bool is_clipped_ = false;
  132. gfx::Rect clip_rect_;
  133. gfx::RRectF rounded_corner_bounds_;
  134. unsigned sorting_context_id_ = 0;
  135. bool is_singleton_sorting_context_ = false;
  136. base::scoped_nsobject<CALayer> clipping_ca_layer_;
  137. base::scoped_nsobject<CALayer> rounded_corner_ca_layer_;
  138. // The status when used as an old layer.
  139. bool ca_layer_used_ = false;
  140. // Weak pointer to the layer in the old CARendererLayerTree that will be
  141. // reused by this layer, and the weak factory used to make that pointer.
  142. base::WeakPtr<ClipAndSortingLayer> old_layer_;
  143. base::WeakPtrFactory<ClipAndSortingLayer> weak_factory_for_new_layer_{this};
  144. };
  145. class TransformLayer {
  146. public:
  147. TransformLayer(ClipAndSortingLayer* parent_layer,
  148. const gfx::Transform& transform);
  149. TransformLayer(TransformLayer&& layer) = delete;
  150. TransformLayer(const TransformLayer&) = delete;
  151. TransformLayer& operator=(const TransformLayer&) = delete;
  152. // See the behavior of RootLayer for the effects of these functions on the
  153. // |ca_layer| member and |old_layer| argument.
  154. ~TransformLayer();
  155. void AddContentLayer(const CARendererLayerParams& params);
  156. void CommitToCA(CALayer* last_committed_transform_ca_layer);
  157. void CALayerFallBack();
  158. CARendererLayerTree* tree() { return parent_layer_->tree(); }
  159. // Parent layer that owns `this`, and child layers that `this` owns.
  160. const raw_ptr<ClipAndSortingLayer> parent_layer_;
  161. std::list<ContentLayer> content_layers_;
  162. gfx::Transform transform_;
  163. base::scoped_nsobject<CALayer> ca_layer_;
  164. // The ca layer status when used as an old layer.
  165. bool ca_layer_used_ = false;
  166. // Weak pointer to the layer in the old CARendererLayerTree that will be
  167. // reused by this layer, and the weak factory used to make that pointer.
  168. base::WeakPtr<TransformLayer> old_layer_;
  169. base::WeakPtrFactory<TransformLayer> weak_factory_for_new_layer_{this};
  170. };
  171. class ContentLayer {
  172. public:
  173. ContentLayer(TransformLayer* parent_layer,
  174. base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
  175. base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer,
  176. const gfx::RectF& contents_rect,
  177. const gfx::Rect& rect,
  178. unsigned background_color,
  179. const gfx::ColorSpace& color_space,
  180. unsigned edge_aa_mask,
  181. float opacity,
  182. unsigned filter,
  183. absl::optional<gfx::HDRMetadata> hdr_metadata,
  184. gfx::ProtectedVideoType protected_video_type);
  185. ContentLayer(ContentLayer&& layer) = delete;
  186. ContentLayer(const ContentLayer&) = delete;
  187. ContentLayer& operator=(const ContentLayer&) = delete;
  188. // See the behavior of RootLayer for the effects of these functions.
  189. ~ContentLayer();
  190. void CommitToCA(CALayer* last_committed_ca_layer);
  191. CARendererLayerTree* tree() { return parent_layer_->tree(); }
  192. void UpdateMapAndMatchOldLayers(CALayerMap& old_ca_layer_map,
  193. int& layer_order,
  194. int& last_old_layer_order);
  195. // Parent layer that owns `this`.
  196. const raw_ptr<TransformLayer> parent_layer_;
  197. // Ensure that the IOSurface be marked as in-use as soon as it is received.
  198. // When they are committed to the window server, that will also increment
  199. // their use count.
  200. const gfx::ScopedInUseIOSurface io_surface_;
  201. const base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer_;
  202. scoped_refptr<SolidColorContents> solid_color_contents_;
  203. gfx::RectF contents_rect_;
  204. gfx::RectF rect_;
  205. unsigned background_color_ = 0;
  206. // The color space of |io_surface|. Used for HDR tonemapping.
  207. gfx::ColorSpace io_surface_color_space_;
  208. // Note that the CoreAnimation edge antialiasing mask is not the same as
  209. // the edge antialiasing mask passed to the constructor.
  210. CAEdgeAntialiasingMask ca_edge_aa_mask_ = 0;
  211. float opacity_ = 1;
  212. NSString* const ca_filter_ = nil;
  213. CALayerType type_ = CALayerType::kDefault;
  214. // If |type| is CALayerType::kVideo and |video_type_can_downgrade| then
  215. // |type| can be downgraded to kDefault. This can be set to false for
  216. // HDR video (that cannot be displayed by a regular CALayer) or for
  217. // protected content (see https://crbug.com/1026703).
  218. bool video_type_can_downgrade_ = true;
  219. absl::optional<gfx::HDRMetadata> hdr_metadata_;
  220. gfx::ProtectedVideoType protected_video_type_ =
  221. gfx::ProtectedVideoType::kClear;
  222. base::scoped_nsobject<CALayer> ca_layer_;
  223. // If this layer's contents can be represented as an
  224. // AVSampleBufferDisplayLayer, then |ca_layer| will point to |av_layer|.
  225. base::scoped_nsobject<AVSampleBufferDisplayLayer> av_layer_;
  226. // Layer used to colorize content when it updates, if borders are
  227. // enabled.
  228. base::scoped_nsobject<CALayer> update_indicator_layer_;
  229. // Indicate the content layer order in the whole layer tree.
  230. int layer_order_ = 0;
  231. // The status when used as an old layer.
  232. bool ca_layer_used_ = false;
  233. // Weak pointer to the layer in the old CARendererLayerTree that will be
  234. // reused by this layer, and the weak factory used to make that pointer.
  235. base::WeakPtr<ContentLayer> old_layer_;
  236. base::WeakPtrFactory<ContentLayer> weak_factory_for_new_layer_{this};
  237. };
  238. RootLayer root_layer_{this};
  239. float scale_factor_ = 1;
  240. bool has_committed_ = false;
  241. const bool allow_av_sample_buffer_display_layer_ = true;
  242. const bool allow_solid_color_layers_ = true;
  243. // Used for uma.
  244. int changed_io_surfaces_during_commit_ = 0;
  245. int unchanged_io_surfaces_during_commit_ = 0;
  246. int total_updated_io_surface_size_during_commit_ = 0;
  247. // Enable CALayerTree optimization that will try to reuse the CALayer with a
  248. // matched CALayer from the old CALayerTree in the previous frame.
  249. const bool ca_layer_tree_optimization_;
  250. // Map of content IOSurface.
  251. CALayerMap ca_layer_map_;
  252. };
  253. } // namespace ui
  254. #endif // UI_ACCELERATED_WIDGET_MAC_CA_RENDERER_LAYER_TREE_H_