layer.h 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // Copyright 2010 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_LAYER_H_
  5. #define CC_LAYERS_LAYER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <array>
  9. #include <memory>
  10. #include <set>
  11. #include <string>
  12. #include <vector>
  13. #include "base/auto_reset.h"
  14. #include "base/callback.h"
  15. #include "base/memory/raw_ptr.h"
  16. #include "base/memory/ref_counted.h"
  17. #include "cc/base/protected_sequence_synchronizer.h"
  18. #include "cc/base/region.h"
  19. #include "cc/benchmarks/micro_benchmark.h"
  20. #include "cc/cc_export.h"
  21. #include "cc/input/scroll_snap_data.h"
  22. #include "cc/layers/layer_collections.h"
  23. #include "cc/layers/touch_action_region.h"
  24. #include "cc/paint/element_id.h"
  25. #include "cc/paint/filter_operations.h"
  26. #include "cc/paint/paint_record.h"
  27. #include "cc/trees/effect_node.h"
  28. #include "cc/trees/property_tree.h"
  29. #include "cc/trees/target_property.h"
  30. #include "components/viz/common/shared_element_resource_id.h"
  31. #include "components/viz/common/surfaces/region_capture_bounds.h"
  32. #include "components/viz/common/surfaces/subtree_capture_id.h"
  33. #include "third_party/skia/include/core/SkColor.h"
  34. #include "ui/gfx/geometry/linear_gradient.h"
  35. #include "ui/gfx/geometry/point3_f.h"
  36. #include "ui/gfx/geometry/point_f.h"
  37. #include "ui/gfx/geometry/rect.h"
  38. #include "ui/gfx/geometry/rounded_corners_f.h"
  39. namespace viz {
  40. class CopyOutputRequest;
  41. }
  42. namespace cc {
  43. class LayerImpl;
  44. class LayerTreeHost;
  45. class LayerTreeHostCommon;
  46. class LayerTreeImpl;
  47. class PictureLayer;
  48. struct CommitState;
  49. struct ThreadUnsafeCommitState;
  50. // For tracing and debugging. The info will be attached to this layer's tracing
  51. // output.
  52. struct CC_EXPORT LayerDebugInfo {
  53. LayerDebugInfo();
  54. LayerDebugInfo(const LayerDebugInfo&);
  55. ~LayerDebugInfo();
  56. std::string name;
  57. NodeId owner_node_id = kInvalidNodeId;
  58. int paint_count = 0;
  59. std::vector<const char*> compositing_reasons;
  60. std::vector<const char*> compositing_reason_ids;
  61. struct Invalidation {
  62. gfx::Rect rect;
  63. const char* reason;
  64. std::string client;
  65. };
  66. std::vector<Invalidation> invalidations;
  67. };
  68. // Base class for composited layers. Special layer types are derived from
  69. // this class. Each layer is an independent unit in the compositor, be that
  70. // for transforming or for content. If a layer has content it can be
  71. // transformed efficiently without requiring the content to be recreated.
  72. // Layers form a tree, with each layer having 0 or more children, and a single
  73. // parent (or none at the root). Layers within the tree, other than the root
  74. // layer, are kept alive by that tree relationship, with refpointer ownership
  75. // from parents to children.
  76. class CC_EXPORT Layer : public base::RefCounted<Layer>,
  77. public ProtectedSequenceSynchronizer {
  78. public:
  79. // An invalid layer id, as all layer ids are positive.
  80. enum LayerIdLabels {
  81. INVALID_ID = -1,
  82. };
  83. // Factory to create a new Layer, with a unique id.
  84. static scoped_refptr<Layer> Create();
  85. Layer(const Layer&) = delete;
  86. Layer& operator=(const Layer&) = delete;
  87. // ProtectedSequenceSynchronizer implementation
  88. bool IsOwnerThread() const override;
  89. bool InProtectedSequence() const override;
  90. void WaitForProtectedSequenceCompletion() const override;
  91. // A unique and stable id for the Layer. Ids are always positive.
  92. int id() const { return layer_id_; }
  93. // Returns a pointer to the highest ancestor of this layer, or itself.
  94. Layer* RootLayer();
  95. // Returns a pointer to the direct ancestor of this layer if it exists,
  96. // or null.
  97. Layer* mutable_parent() { return parent_.Write(*this); }
  98. const Layer* parent() const { return parent_.Read(*this); }
  99. // Appends |child| to the list of children of this layer, and maintains
  100. // ownership of a reference to that |child|.
  101. void AddChild(scoped_refptr<Layer> child);
  102. // Inserts |child| into the list of children of this layer, before position
  103. // |index| (0 based) and maintains ownership of a reference to that |child|.
  104. void InsertChild(scoped_refptr<Layer> child, size_t index);
  105. // Removes an existing child |reference| from this layer's list of children,
  106. // and inserts |new_layer| it its place in the list. This layer maintains
  107. // ownership of a reference to the |new_layer|. The |new_layer| may be null,
  108. // in which case |reference| is simply removed from the list of children,
  109. // which ends this layers ownership of the child.
  110. void ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer);
  111. // Removes this layer from the list of children in its parent, removing the
  112. // parent's ownership of this layer.
  113. void RemoveFromParent();
  114. // Removes all children from this layer's list of children, removing ownership
  115. // of those children.
  116. void RemoveAllChildren();
  117. // Sets the children while minimizing changes to layers that are already
  118. // children of this layer.
  119. void SetChildLayerList(LayerList children);
  120. // Returns true if |ancestor| is this layer's parent or higher ancestor.
  121. bool HasAncestor(const Layer* ancestor) const;
  122. // The list of children of this layer.
  123. const LayerList& children() const { return inputs_.Read(*this).children; }
  124. // These methods provide information from layer_tree_host_ in a way that is
  125. // safe to query from either the main or impl thread.
  126. bool IsAttached() const { return layer_tree_host_; }
  127. bool IsMainThread() const;
  128. bool IsUsingLayerLists() const;
  129. // Gets the LayerTreeHost that this layer is attached to, or null if not.
  130. // A layer is attached to a LayerTreeHost if it or an ancestor layer is set as
  131. // the root layer of a LayerTreeHost (while noting only a layer without a
  132. // parent may be set as the root layer).
  133. LayerTreeHost* layer_tree_host() {
  134. DCHECK(!IsAttached() || IsMainThread());
  135. return layer_tree_host_.get();
  136. }
  137. const LayerTreeHost* layer_tree_host() const {
  138. DCHECK(!IsAttached() || IsMainThread());
  139. return layer_tree_host_.get();
  140. }
  141. // This requests the layer and its subtree be rendered and given to the
  142. // callback. If the copy is unable to be produced (the layer is destroyed
  143. // first), then the callback is called with a nullptr/empty result. If the
  144. // request's source property is set, any prior uncommitted requests having the
  145. // same source will be aborted.
  146. void RequestCopyOfOutput(std::unique_ptr<viz::CopyOutputRequest> request);
  147. // True if a copy request has been inserted on this layer and a commit has not
  148. // occurred yet.
  149. bool HasCopyRequest() const {
  150. return layer_tree_inputs() && !layer_tree_inputs()->copy_requests.empty();
  151. }
  152. // Set and get the background color for the layer. This color is used to
  153. // calculate the safe opaque background color. Subclasses may also use the
  154. // color for other purposes.
  155. virtual void SetBackgroundColor(SkColor4f background_color);
  156. SkColor4f background_color() const {
  157. return inputs_.Read(*this).background_color;
  158. }
  159. // For layer tree mode only. In layer list mode, client doesn't need to set
  160. // it. Sets an opaque background color for the layer, to be used in place of
  161. // the background_color() if the layer says contents_opaque() is true.
  162. void SetSafeOpaqueBackgroundColor(SkColor4f background_color);
  163. // Returns a background color with opaqueness equal to the value of
  164. // contents_opaque().
  165. // If the layer says contents_opaque() is true, in layer tree mode, this
  166. // returns the value set by SetSafeOpaqueBackgroundColor() which should be an
  167. // opaque color, and in layer list mode, returns background_color() which
  168. // should be opaque (otherwise SetBackgroundColor() should have set
  169. // contents_opaque to false).
  170. // Otherwise, it returns something non-opaque. It prefers to return the
  171. // background_color(), but if the background_color() is opaque (and this layer
  172. // claims to not be), then SkColors::kTransparent is returned to avoid
  173. // intrusive checkerboard where the layer is not covered by the
  174. // background_color().
  175. SkColor4f SafeOpaqueBackgroundColor() const;
  176. // For layer tree mode only.
  177. // Set and get the position of this layer, relative to its parent. This is
  178. // specified in layer space, which excludes device scale and page scale
  179. // factors, and ignoring transforms for this layer or ancestor layers. The
  180. // root layer's position is not used as it always appears at the origin of
  181. // the viewport.
  182. void SetPosition(const gfx::PointF& position);
  183. const gfx::PointF position() const {
  184. return layer_tree_inputs() ? layer_tree_inputs()->position : gfx::PointF();
  185. }
  186. // Reorder the entirety of the children() vector to follow new_children_order.
  187. // All elements inside new_children_order must be inside children(), and vice
  188. // versa. Will empty the |new_children_order| LayerList passed into this
  189. // method.
  190. void ReorderChildren(LayerList* new_children_order);
  191. // Set and get the layers bounds. This is specified in layer space, which
  192. // excludes device scale and page scale factors, and ignoring transforms for
  193. // this layer or ancestor layers.
  194. //
  195. // The root layer in the tree has bounds in viewport space, which includes
  196. // the device scale factor.
  197. void SetBounds(const gfx::Size& bounds);
  198. const gfx::Size& bounds() const { return inputs_.Read(*this).bounds; }
  199. // For layer tree mode only.
  200. // Set or get that this layer clips its subtree to within its bounds. Content
  201. // of children will be intersected with the bounds of this layer when true.
  202. void SetMasksToBounds(bool masks_to_bounds);
  203. bool masks_to_bounds() const {
  204. return layer_tree_inputs() && layer_tree_inputs()->masks_to_bounds;
  205. }
  206. // For layer tree mode only.
  207. // Set or get the clip rect for this layer. |clip_rect| is relative to |this|
  208. // layer. If you are trying to clip the subtree to the bounds of this layer,
  209. // SetMasksToBounds() would be a better alternative.
  210. void SetClipRect(const gfx::Rect& clip_rect);
  211. gfx::Rect clip_rect() const {
  212. return layer_tree_inputs() ? layer_tree_inputs()->clip_rect : gfx::Rect();
  213. }
  214. // Returns the bounds which is clipped by the clip rect.
  215. gfx::RectF EffectiveClipRect() const;
  216. // For layer tree mode only.
  217. // Set or get a layer that will mask the contents of this layer. The alpha
  218. // channel of the mask layer's content is used as an alpha mask of this
  219. // layer's content. IOW the mask's alpha is multiplied by this layer's alpha
  220. // for each matching pixel.
  221. void SetMaskLayer(scoped_refptr<PictureLayer> mask_layer);
  222. const PictureLayer* mask_layer() const {
  223. return layer_tree_inputs() ? layer_tree_inputs()->mask_layer.get()
  224. : nullptr;
  225. }
  226. PictureLayer* mask_layer() {
  227. return layer_tree_inputs() ? layer_tree_inputs()->mask_layer.get()
  228. : nullptr;
  229. }
  230. // Marks the |dirty_rect| as being changed, which will cause a commit and
  231. // the compositor to submit a new frame with a damage rect that includes the
  232. // layer's dirty area. This rect is in layer space, the same as bounds().
  233. virtual void SetNeedsDisplayRect(const gfx::Rect& dirty_rect);
  234. // Marks the entire layer's bounds as being changed, which will cause a commit
  235. // and the compositor to submit a new frame with a damage rect that includes
  236. // the entire layer. Note that if the layer resizes afterward, but before
  237. // commit, the dirty rect would not cover the layer, however then the layer
  238. // bounds change would implicitly damage the full layer.
  239. void SetNeedsDisplay() { SetNeedsDisplayRect(gfx::Rect(bounds())); }
  240. // Returns the union of previous calls to SetNeedsDisplayRect() and
  241. // SetNeedsDisplay() that have not been committed to the compositor thread.
  242. const gfx::Rect& update_rect() const { return update_rect_.Read(*this); }
  243. void ResetUpdateRectForTesting() { update_rect_.Write(*this) = gfx::Rect(); }
  244. // For layer tree mode only.
  245. // Set or get the rounded corner radii which is applied to the layer and its
  246. // subtree (as if they are together as a single composited entity) when
  247. // blitting into their target. Setting this makes the layer masked to bounds.
  248. // If the layer has a clip of its own, the rounded corner will be applied
  249. // along the layer's clip rect corners. TODO(sashamcintosh): Apply rounded
  250. // corner when the layer has a transform that is not 2d axis aligned.
  251. // Currently the rounded corner is ignored in this case.
  252. void SetRoundedCorner(const gfx::RoundedCornersF& corner_radii);
  253. const gfx::RoundedCornersF& corner_radii() const {
  254. return layer_tree_inputs() ? layer_tree_inputs()->corner_radii
  255. : kNoRoundedCornersF;
  256. }
  257. // Returns true if any of the corner has a non-zero radius set.
  258. bool HasRoundedCorner() const { return !corner_radii().IsEmpty(); }
  259. // For layer tree mode only.
  260. // Set or get the flag that disables the requirement of a render surface for
  261. // this layer due to it having rounded corners. This improves performance at
  262. // the cost of maybe having some blending artifacts. Not having a render
  263. // surface is not guaranteed however.
  264. void SetIsFastRoundedCorner(bool enable);
  265. bool is_fast_rounded_corner() const {
  266. return layer_tree_inputs() && layer_tree_inputs()->is_fast_rounded_corner;
  267. }
  268. // For layer tree mode only.
  269. // Set or get the gradient mask which is applied to the layer and its
  270. // subtree (as if they are together as a single composited entity) when
  271. // blitting into their target. Setting applies a linear gradient to the layer
  272. // bounds and optionally the rounded corner defined by SetRoundedCorner.
  273. // TODO(sashamcintosh): Apply gradient mask when the layer has a transform
  274. // that is not 2d axis aligned. Currently the gradient mask is ignored in this
  275. // case.
  276. void SetGradientMask(const gfx::LinearGradient& gradient_mask);
  277. const gfx::LinearGradient& gradient_mask() const {
  278. return layer_tree_inputs() ? layer_tree_inputs()->gradient_mask
  279. : gfx::LinearGradient::GetEmpty();
  280. }
  281. bool HasGradientMask() const { return !gradient_mask().IsEmpty(); }
  282. bool HasMaskFilter() const { return HasRoundedCorner() || HasGradientMask(); }
  283. // For layer tree mode only.
  284. // Set or get the opacity which should be applied to the contents of the layer
  285. // and its subtree (together as a single composited entity) when blending them
  286. // into their target. Note that this does not speak to the contents of this
  287. // layer, which may be opaque or not (see contents_opaque()). Note that the
  288. // opacity is cumulative since it applies to the layer's subtree.
  289. virtual void SetOpacity(float opacity);
  290. float opacity() const {
  291. return layer_tree_inputs() ? layer_tree_inputs()->opacity : 1.0f;
  292. }
  293. // Gets the true opacity that will be used for blending the contents of this
  294. // layer and its subtree into its target during composite. This value is the
  295. // same as the user-specified opacity() unless the layer should not be visible
  296. // at all for other reasons, in which case the opacity here becomes 0.
  297. float EffectiveOpacity() const;
  298. // For layer tree mode only.
  299. // Set or get the blend mode to be applied when blending the contents of the
  300. // layer and its subtree (together as a single composited entity) when
  301. // blending them into their target.
  302. void SetBlendMode(SkBlendMode blend_mode);
  303. SkBlendMode blend_mode() const {
  304. return layer_tree_inputs() ? layer_tree_inputs()->blend_mode
  305. : SkBlendMode::kSrcOver;
  306. }
  307. // For layer tree mode only.
  308. // Set or get the list of filter effects to be applied to the contents of the
  309. // layer and its subtree (together as a single composited entity) when
  310. // drawing them into their target.
  311. void SetFilters(const FilterOperations& filters);
  312. FilterOperations filters() const {
  313. return layer_tree_inputs() ? layer_tree_inputs()->filters
  314. : FilterOperations();
  315. }
  316. // For layer tree mode only.
  317. // Set or get the list of filters that should be applied to the content this
  318. // layer and its subtree will be drawn into. The effect is clipped by
  319. // backdrop_filter_bounds.
  320. void SetBackdropFilters(const FilterOperations& filters);
  321. FilterOperations backdrop_filters() const {
  322. return layer_tree_inputs() ? layer_tree_inputs()->backdrop_filters
  323. : FilterOperations();
  324. }
  325. // For layer tree mode only.
  326. void SetBackdropFilterBounds(const gfx::RRectF& backdrop_filter_bounds);
  327. void ClearBackdropFilterBounds();
  328. absl::optional<gfx::RRectF> backdrop_filter_bounds() const {
  329. return layer_tree_inputs() ? layer_tree_inputs()->backdrop_filter_bounds
  330. : absl::nullopt;
  331. }
  332. // For layer tree mode only.
  333. void SetBackdropFilterQuality(const float quality);
  334. float backdrop_filter_quality() const {
  335. return layer_tree_inputs() ? layer_tree_inputs()->backdrop_filter_quality
  336. : 1.0f;
  337. }
  338. // Set or get an optimization hint that the contents of this layer are fully
  339. // opaque or not. If true, every pixel of content inside the layer's bounds
  340. // must be opaque or visual errors can occur. This applies only to this layer
  341. // and not to children, and does not imply the layer should be composited
  342. // opaquely, as effects may be applied such as opacity() or filters().
  343. // Note that this also calls SetContentsOpaqueForText(opaque) internally.
  344. // To override a different contents_opaque_for_text, the client should call
  345. // SetContentsOpaqueForText() after SetContentsOpaque().
  346. void SetContentsOpaque(bool opaque);
  347. bool contents_opaque() const { return inputs_.Read(*this).contents_opaque; }
  348. // Whether the contents area containing text is known to be opaque.
  349. // For example, blink will SetContentsOpaque(false) but
  350. // SetContentsOpaqueForText(true) for the following case:
  351. // <div style="overflow: hidden; border-radius: 10px; background: white">
  352. // TEXT
  353. // </div>
  354. // See also the note for SetContentsOpaque().
  355. void SetContentsOpaqueForText(bool opaque);
  356. bool contents_opaque_for_text() const {
  357. return inputs_.Read(*this).contents_opaque_for_text;
  358. }
  359. // Set or get whether this layer should be a hit test target
  360. void SetHitTestable(bool should_hit_test);
  361. virtual bool HitTestable() const;
  362. // For layer tree mode only.
  363. // Set or get the transform to be used when compositing this layer into its
  364. // target. The transform is inherited by this layers children.
  365. void SetTransform(const gfx::Transform& transform);
  366. const gfx::Transform& transform() const {
  367. return layer_tree_inputs() ? layer_tree_inputs()->transform
  368. : kIdentityTransform;
  369. }
  370. // Gets the transform, including transform origin and position, of this layer
  371. // and its ancestors, device scale and page scale factors, into the device
  372. // viewport.
  373. gfx::Transform ScreenSpaceTransform() const;
  374. // For layer tree mode only.
  375. // Set or get the origin to be used when applying the transform. The value is
  376. // a position in layer space, relative to the top left corner of this layer.
  377. // For instance, if set to the center of the layer, with a transform to rotate
  378. // 180deg around the X axis, it would flip the layer vertically around the
  379. // center of the layer, leaving it occupying the same space. Whereas set to
  380. // the top left of the layer, the rotation wouldoccur around the top of the
  381. // layer, moving it vertically while flipping it.
  382. void SetTransformOrigin(const gfx::Point3F&);
  383. gfx::Point3F transform_origin() const {
  384. return layer_tree_inputs() ? layer_tree_inputs()->transform_origin
  385. : gfx::Point3F();
  386. }
  387. // For layer tree mode only.
  388. // Set or get the scroll offset of the layer. The content of the layer, and
  389. // position of its subtree, as well as other layers for which this layer is
  390. // their scroll parent, and their subtrees) is moved up by the amount of
  391. // offset specified here.
  392. void SetScrollOffset(const gfx::PointF& scroll_offset);
  393. gfx::PointF scroll_offset() const {
  394. return layer_tree_inputs() ? layer_tree_inputs()->scroll_offset
  395. : gfx::PointF();
  396. }
  397. // For layer tree mode only.
  398. // Called internally during commit to update the layer with state from the
  399. // compositor thread. Not to be called externally by users of this class.
  400. void SetScrollOffsetFromImplSide(const gfx::PointF& scroll_offset);
  401. // For layer tree mode only.
  402. // Marks this layer as being scrollable and needing an associated scroll node,
  403. // and specifies the size of the container in which the scrolling contents are
  404. // visible. (Use SetBounds to set the size of the content to be scrolled.)
  405. // Once scrollable, a Layer cannot become un-scrollable.
  406. void SetScrollable(const gfx::Size& scroll_container_bounds);
  407. bool scrollable() const {
  408. return layer_tree_inputs() && layer_tree_inputs()->scrollable;
  409. }
  410. gfx::Size scroll_container_bounds() const {
  411. return layer_tree_inputs() ? layer_tree_inputs()->scroll_container_bounds
  412. : gfx::Size();
  413. }
  414. virtual bool IsScrollbarLayerForTesting() const;
  415. // For layer tree mode only.
  416. // Set or get if this layer is able to be scrolled along each axis. These are
  417. // independent of the scrollable state, or size of the scrollable area
  418. // specified in SetScrollable(), as these may be enabled or disabled
  419. // dynamically, while SetScrollable() defines what would be possible if these
  420. // are enabled.
  421. // When disabled, overscroll elasticity will not be used if the scroll offset
  422. // ends up past the maximum range. And when enabled, with overlay scrollbars,
  423. // the scrollbars will be shown when the scroll offset changes if these are
  424. // set to true.
  425. void SetUserScrollable(bool horizontal, bool vertical);
  426. bool GetUserScrollableHorizontal() const;
  427. bool GetUserScrollableVertical() const;
  428. // Set or get an area of this layer within which initiating a scroll can not
  429. // be done from the compositor thread. Within this area, if the user attempts
  430. // to start a scroll, the events must be sent to the main thread and processed
  431. // there.
  432. void SetNonFastScrollableRegion(const Region& non_fast_scrollable_region);
  433. const Region& non_fast_scrollable_region() const {
  434. if (const auto& rare_inputs = inputs_.Read(*this).rare_inputs)
  435. return rare_inputs->non_fast_scrollable_region;
  436. return Region::Empty();
  437. }
  438. // Set or get the set of touch actions allowed across each point of this
  439. // layer. The |touch_action_region| can specify, for any number of areas,
  440. // which touch actions are allowed in each area. The result is the
  441. // intersection of overlapping areas. These allowed actions control if
  442. // a touch event can initiate a scroll or zoom on the compositor thread.
  443. void SetTouchActionRegion(TouchActionRegion touch_action_region);
  444. const TouchActionRegion& touch_action_region() const {
  445. return inputs_.Read(*this).touch_action_region;
  446. }
  447. // Set or get the region that should be used for capture.
  448. void SetCaptureBounds(viz::RegionCaptureBounds bounds);
  449. const viz::RegionCaptureBounds& capture_bounds() const {
  450. if (const auto& rare_inputs = inputs_.Read(*this).rare_inputs)
  451. return rare_inputs->capture_bounds;
  452. return viz::RegionCaptureBounds::Empty();
  453. }
  454. // Set or get the set of blocking wheel rects of this layer. The
  455. // |wheel_event_region| is the set of rects for which there is a non-passive
  456. // wheel event listener that paints into this layer. Mouse wheel messages
  457. // that intersect these rects must execute their relevant JS handler before we
  458. // can start scrolling.
  459. void SetWheelEventRegion(Region wheel_event_region);
  460. const Region& wheel_event_region() const {
  461. if (const auto& rare_inputs = inputs_.Read(*this).rare_inputs)
  462. return rare_inputs->wheel_event_region;
  463. return Region::Empty();
  464. }
  465. // For layer tree mode only.
  466. // In layer list mode, use ScrollTree::SetScrollCallbacks() instead.
  467. // Sets a RepeatingCallback that is run during a main frame, before layers are
  468. // asked to prepare content with Update(), if the scroll offset for the layer
  469. // was changed by the InputHandlerClient, on the compositor thread (or on the
  470. // main thread in single-thread mode). It may be set to a null callback, in
  471. // which case nothing is called. This is for layer tree mode only. Should use
  472. // ScrollTree::SetScrollCallbacks() in layer list mode.
  473. void SetDidScrollCallback(
  474. base::RepeatingCallback<void(const gfx::PointF&, const ElementId&)>);
  475. // For layer tree mode only.
  476. // Sets the given |subtree_id| on this layer, so that the layer subtree rooted
  477. // at this layer can be uniquely identified by a FrameSinkVideoCapturer.
  478. // The existence of a valid SubtreeCaptureId on this layer will force it to be
  479. // drawn into a separate CompositorRenderPass.
  480. // Setting a non-valid (i.e. default-constructed SubtreeCaptureId) will clear
  481. // this property.
  482. // It is not allowed to change this ID from a valid ID to another valid ID,
  483. // since a client might already using the existing valid ID to make this layer
  484. // subtree identifiable by a capturer.
  485. //
  486. // Note that this is useful when it's desired to video record a layer subtree
  487. // of a non-root layer using a FrameSinkVideoCapturer, since non-root layers
  488. // are usually not drawn into their own CompositorRenderPass.
  489. void SetSubtreeCaptureId(viz::SubtreeCaptureId subtree_id);
  490. viz::SubtreeCaptureId subtree_capture_id() const {
  491. if (layer_tree_inputs())
  492. return layer_tree_inputs()->subtree_capture_id;
  493. return viz::SubtreeCaptureId();
  494. }
  495. // Set or get if the layer and its subtree should be cached as a texture in
  496. // the display compositor. This is used as an optimization when it is known
  497. // that the layer will be animated without changing its content, or any of its
  498. // subtree.
  499. //
  500. // Note that this also disables occlusion culling, as the entire texture will
  501. // be drawn so that it is not left with incomplete areas. This should only be
  502. // used when paying the cost of creating an intermediate texture is worth it,
  503. // even when the layer's subtree may be occluded, or not visible in the final
  504. // output.
  505. void SetCacheRenderSurface(bool value) {
  506. DCHECK(IsPropertyChangeAllowed());
  507. SetBitFlag(value, kCacheRenderSurfaceFlagMask, /*invalidate=*/true);
  508. }
  509. bool cache_render_surface() const {
  510. return GetBitFlag(kCacheRenderSurfaceFlagMask);
  511. }
  512. // If the layer induces a render surface, this returns the cause for the
  513. // render surface. If the layer does not induce a render surface, this returns
  514. // kNone.
  515. RenderSurfaceReason GetRenderSurfaceReason() const;
  516. // Set or get if the layer and its subtree will be drawn through an
  517. // intermediate texture, called a RenderSurface. This mimics the need
  518. // for a RenderSurface that is caused by compositing effects such as masks
  519. // without needing to set up such effects.
  520. void SetForceRenderSurfaceForTesting(bool value) {
  521. DCHECK(IsPropertyChangeAllowed());
  522. SetBitFlag(value, kForceRenderSurfaceForTestingFlagMask,
  523. /*invalidate=*/true);
  524. }
  525. bool force_render_surface_for_testing() const {
  526. return GetBitFlag(kForceRenderSurfaceForTestingFlagMask);
  527. }
  528. // When true the layer may contribute to the compositor's output. When false,
  529. // it does not. This property does not apply to children of the layer, they
  530. // may contribute while this layer does not. The layer itself will determine
  531. // if it has content to contribute, but when false, this prevents it from
  532. // doing so.
  533. void SetIsDrawable(bool is_drawable);
  534. // Is true if the layer will contribute content to the compositor's output.
  535. // Will be false if SetIsDrawable(false) is called. But will also be false if
  536. // the layer itself has no content to contribute, even though the layer was
  537. // given SetIsDrawable(true).
  538. bool draws_content() const { return GetBitFlag(kDrawsContentFlagMask); }
  539. void SetDrawsContent(bool value);
  540. // Returns the number of layers in this layers subtree (excluding itself) for
  541. // which DrawsContent() is true.
  542. int NumDescendantsThatDrawContent() const;
  543. // For layer tree mode only.
  544. // Set or get if this layer and its subtree should be part of the compositor's
  545. // output to the screen. When set to true, the layer's subtree does not appear
  546. // to the user, but still remains part of the tree with all its normal drawing
  547. // properties. This can be used to execute a CopyOutputRequest on this layer
  548. // or another in its subtree, since the layers are still able to be drawn by
  549. // the compositor, while not being composed into the result shown to the user.
  550. void SetHideLayerAndSubtree(bool hide);
  551. bool hide_layer_and_subtree() const {
  552. return layer_tree_inputs() && layer_tree_inputs()->hide_layer_and_subtree;
  553. }
  554. // The index of this layer's node in the various property trees. These are
  555. // only valid after a main frame, when Update() is called on the layer, and
  556. // remain valid and in in the same state until the next main frame, or until
  557. // the layer is removed from its LayerTreeHost. Otherwise kInvalidNodeId is
  558. // returned.
  559. int transform_tree_index() const;
  560. int clip_tree_index() const;
  561. int effect_tree_index() const;
  562. int scroll_tree_index() const;
  563. bool transform_tree_index_is_valid(const PropertyTrees&) const;
  564. bool clip_tree_index_is_valid(const PropertyTrees&) const;
  565. bool effect_tree_index_is_valid(const PropertyTrees&) const;
  566. bool scroll_tree_index_is_valid(const PropertyTrees&) const;
  567. // While all layers have an index into the transform tree, this value
  568. // indicates whether the transform tree node was created for this layer.
  569. void SetHasTransformNode(bool value) {
  570. SetBitFlag(value, kHasTransformNodeFlagMask);
  571. }
  572. bool has_transform_node() const {
  573. return GetBitFlag(kHasTransformNodeFlagMask);
  574. }
  575. // This value indicates whether a clip node was created for |this| layer.
  576. void SetHasClipNode(bool val) { SetBitFlag(val, kHasClipNodeFlagMask); }
  577. bool has_clip_node() const { return GetBitFlag(kHasClipNodeFlagMask); }
  578. // Sets that the content shown in this layer may be a video. This may be used
  579. // by the system compositor to distinguish between animations updating the
  580. // screen and video, which the user would be watching. This allows
  581. // optimizations like turning off the display when video is not playing,
  582. // without interfering with video playback.
  583. void SetMayContainVideo(bool value) {
  584. SetBitFlag(value, kMayContainVideoFlagMask, /*invalidate=*/false,
  585. /*needs_push=*/true);
  586. }
  587. bool may_contain_video() const {
  588. return GetBitFlag(kMayContainVideoFlagMask);
  589. }
  590. // Stable identifier for clients. See comment in cc/trees/element_id.h.
  591. void SetElementId(ElementId id);
  592. ElementId element_id() const { return inputs_.Read(*this).element_id; }
  593. // For layer tree mode only.
  594. // Sets or gets if trilinear filtering should be used to scaling the contents
  595. // of this layer and its subtree. When set the layer and its subtree will be
  596. // composited together as a single unit, mip maps will be generated of the
  597. // subtree together, and trilinear filtering applied when supported, if
  598. // scaling during composite of the content from this layer and its subtree
  599. // into the target.
  600. void SetTrilinearFiltering(bool trilinear_filtering);
  601. bool trilinear_filtering() const {
  602. return layer_tree_inputs() && layer_tree_inputs()->trilinear_filtering;
  603. }
  604. // For layer tree mode only.
  605. // Increments/decrements/gets number of layers mirroring this layer.
  606. void IncrementMirrorCount();
  607. void DecrementMirrorCount();
  608. int mirror_count() const {
  609. return layer_tree_inputs() ? layer_tree_inputs()->mirror_count : 0;
  610. }
  611. // Captures text content within the given |rect| and returns the associated
  612. // NodeInfo in |content|.
  613. virtual void CaptureContent(const gfx::Rect& rect,
  614. std::vector<NodeInfo>* content) const;
  615. // For tracing. Gets a recorded rasterization of this layer's contents that
  616. // can be displayed inside representations of this layer. May return null, in
  617. // which case the layer won't be shown with any content in the tracing
  618. // display.
  619. virtual sk_sp<const SkPicture> GetPicture() const;
  620. const LayerDebugInfo* debug_info() const { return debug_info_.Read(*this); }
  621. LayerDebugInfo& EnsureDebugInfo();
  622. void ClearDebugInfo();
  623. // For telemetry testing. Runs a given test behaviour implemented in
  624. // |benchmark| for this layer. The base class does nothing as benchmarks
  625. // only exist for subclass layer types. For each subclass that the
  626. // MicroBenchmark supports, the class should override this method and run the
  627. // |benchmark| against this layer.
  628. virtual void RunMicroBenchmark(MicroBenchmark* benchmark);
  629. // Internal method to create the compositor thread type for this Layer.
  630. // Subclasses should override this method if they want to return their own
  631. // subclass of LayerImpl instead.
  632. virtual std::unique_ptr<LayerImpl> CreateLayerImpl(
  633. LayerTreeImpl* tree_impl) const;
  634. // Internal method to copy all state from this Layer to the compositor thread.
  635. // Should be overridden by any subclass that has additional state, to copy
  636. // that state as well. The |layer| passed in will be of the type created by
  637. // CreateLayerImpl(), so can be safely down-casted if the subclass uses a
  638. // different type for the compositor thread.
  639. virtual void PushPropertiesTo(LayerImpl* layer,
  640. const CommitState& commit_state,
  641. const ThreadUnsafeCommitState& unsafe_state);
  642. // Internal method to be overridden by Layer subclasses that need to do work
  643. // during a main frame. The method should compute any state that will need to
  644. // propagated to the compositor thread for the next commit, and return true
  645. // if there is anything new to commit. If all layers return false, the commit
  646. // may be aborted.
  647. virtual bool Update();
  648. // Internal to property tree construction. This allows a layer to request that
  649. // its transform should be snapped such that the layer aligns with the pixel
  650. // grid in its rendering target. This ensures that the layer is not fuzzy
  651. // (unless it is being scaled). Layers may override this to return true, by
  652. // default layers are not snapped.
  653. virtual bool IsSnappedToPixelGridInTarget() const;
  654. // Internal method that is called when a Layer is attached to a LayerTreeHost.
  655. // This would happen when
  656. // a) the Layer is added to an existing Layer tree that is attached to a
  657. // LayerTreeHost.
  658. // b) the Layer is made the root layer of a LayerTreeHost.
  659. // c) the Layer is part of a Layer tree, and an ancestor is attached to a
  660. // LayerTreeHost via a) or b).
  661. // The |host| is the new LayerTreeHost which the Layer is now attached to.
  662. // Subclasses may override this if they have data or resources which are
  663. // specific to a LayerTreeHost that should be updated or reset. After this
  664. // returns the Layer will hold a pointer to the new LayerTreeHost.
  665. virtual void SetLayerTreeHost(LayerTreeHost* host);
  666. // Internal method to mark this layer as needing to push its state to the
  667. // compositor thread during the next commit. The PushPropertiesTo() method
  668. // will be called for this layer during the next commit only if this method
  669. // was called before it.
  670. void SetNeedsPushProperties();
  671. // Internal to property tree construction. A generation number for the
  672. // property trees, to verify the layer's indices are pointers into the trees
  673. // currently held by the LayerTreeHost. The number is updated when property
  674. // trees are built from the Layer tree.
  675. void set_property_tree_sequence_number(int sequence_number) {
  676. property_tree_sequence_number_.Write(*this) = sequence_number;
  677. }
  678. int property_tree_sequence_number() const {
  679. return property_tree_sequence_number_.Read(*this);
  680. }
  681. // Internal to property tree construction. Sets the index for this Layer's
  682. // node in each property tree.
  683. void SetTransformTreeIndex(int index);
  684. void SetClipTreeIndex(int index);
  685. void SetEffectTreeIndex(int index);
  686. void SetScrollTreeIndex(int index);
  687. // The position of this layer after transforming by the layer's transform
  688. // node. When property trees are built by cc (when IsUsingLayerLists is false)
  689. // this is set by property_tree_builder.cc.
  690. void SetOffsetToTransformParent(gfx::Vector2dF offset);
  691. gfx::Vector2dF offset_to_transform_parent() const {
  692. return offset_to_transform_parent_.Read(*this);
  693. }
  694. // Internal to property tree construction. Indicates that a property changed
  695. // on this layer that may affect the position or content of all layers in this
  696. // layer's subtree, including itself. This causes the layer's subtree to be
  697. // considered damaged and re-displayed to the user.
  698. void SetSubtreePropertyChanged();
  699. void ClearSubtreePropertyChangedForTesting() {
  700. subtree_property_changed_.Write(*this) = false;
  701. }
  702. bool subtree_property_changed() const {
  703. return subtree_property_changed_.Read(*this);
  704. }
  705. // Internal to property tree construction. Returns ElementListType::ACTIVE
  706. // as main thread layers do not have a pending/active tree split, and
  707. // animations should run normally on the main thread layer tree.
  708. ElementListType GetElementTypeForAnimation() const;
  709. // Internal to property tree construction. Whether this layer may animate its
  710. // opacity on the compositor thread. Layer subclasses may override this to
  711. // report true. If true, assumptions about opacity can not be made on the main
  712. // thread.
  713. virtual bool OpacityCanAnimateOnImplThread() const;
  714. // For layer tree mode only.
  715. // Internal to property tree construction. Set to true if this layer or any
  716. // layer below it in the tree has a CopyOutputRequest pending commit.
  717. // This flag is valid only when LayerTreeHost::has_copy_request() is true
  718. void SetSubtreeHasCopyRequest(bool value) {
  719. SetBitFlag(value, kSubtreeHasCopyRequestFlagMask);
  720. }
  721. bool subtree_has_copy_request() const {
  722. return GetBitFlag(kSubtreeHasCopyRequestFlagMask);
  723. }
  724. // Internal to property tree construction. Removes all CopyOutputRequests from
  725. // this layer, moving them into |requests|.
  726. void TakeCopyRequests(
  727. std::vector<std::unique_ptr<viz::CopyOutputRequest>>* requests);
  728. // Internal to property tree construction. Set if the layer should not be
  729. // shown when its back face is visible to the user. This is a derived value
  730. // from SetDoubleSided().
  731. void SetShouldCheckBackfaceVisibility(bool value) {
  732. SetBitFlag(value, kShouldCheckBackfaceVisibilityFlagMask,
  733. /*invalidate=*/false, /*needs_push=*/true);
  734. }
  735. bool should_check_backface_visibility() const {
  736. return GetBitFlag(kShouldCheckBackfaceVisibilityFlagMask);
  737. }
  738. // For debugging, containing information about the associated DOM, etc.
  739. std::string DebugName() const;
  740. std::string ToString() const;
  741. // Called when a property has been modified in a way that the layer knows
  742. // immediately that a commit is required. This implies SetNeedsPushProperties
  743. // to push that property.
  744. // This is public, so that it can be called directly when needed, for example
  745. // in PropertyTreeManager when handling scroll offsets.
  746. void SetNeedsCommit();
  747. void SetDebugName(const std::string& name);
  748. // If the content of this layer is provided by a cached or live render
  749. // surface, returns the ID of that resource.
  750. virtual viz::SharedElementResourceId DocumentTransitionResourceId() const;
  751. protected:
  752. friend class LayerImpl;
  753. friend class TreeSynchronizer;
  754. Layer();
  755. ~Layer() override;
  756. // These SetNeeds functions are in order of severity of update:
  757. // See SetNeedsCommit() above - it belongs here in the order of severity.
  758. // Called when there's been a change in layer structure. Implies
  759. // SetNeedsCommit and property tree rebuld, but not SetNeedsPushProperties
  760. // (the full tree is synced over).
  761. void SetNeedsFullTreeSync();
  762. // May be overridden by subclasses if they have optional content, to return
  763. // false if there is no content to be displayed. If they do have content, then
  764. // they should return the value from this base class method.
  765. virtual bool HasDrawableContent() const;
  766. // Called when the layer's number of drawable descendants changes.
  767. void AddDrawableDescendants(int num);
  768. // For debugging. Returns false if the LayerTreeHost this layer is attached to
  769. // is in the process of updating layers or performing commit for a
  770. // BeginMainFrame. Layer properties should be changed by the client before the
  771. // BeginMainFrame, and should not be changed while the frame is being
  772. // generated or committed.
  773. bool IsPropertyChangeAllowed() const;
  774. void IncreasePaintCount() {
  775. if (debug_info_.Read(*this))
  776. ++debug_info_.Write(*this)->paint_count;
  777. }
  778. base::AutoReset<bool> IgnoreSetNeedsCommitForTest() {
  779. return base::AutoReset<bool>(
  780. &ignore_set_needs_commit_for_test_.Write(*this), true);
  781. }
  782. private:
  783. friend class base::RefCounted<Layer>;
  784. friend class LayerTreeHostCommon;
  785. friend class LayerTreeHost;
  786. // For layer tree mode only.
  787. struct LayerTreeInputs;
  788. LayerTreeInputs& EnsureLayerTreeInputs();
  789. #if DCHECK_IS_ON()
  790. const LayerTreeInputs* layer_tree_inputs() const;
  791. #else
  792. const LayerTreeInputs* layer_tree_inputs() const {
  793. return layer_tree_inputs_.Read(*this);
  794. }
  795. #endif
  796. // Interactions with attached animations.
  797. void OnFilterAnimated(const FilterOperations& filters);
  798. void OnBackdropFilterAnimated(const FilterOperations& backdrop_filters);
  799. void OnOpacityAnimated(float opacity);
  800. void OnTransformAnimated(const gfx::Transform& transform);
  801. void AddClipChild(Layer* child);
  802. void RemoveClipChild(Layer* child);
  803. // For functions that do or (as SetParent) might remove a child layer,
  804. // passing kForReadd causes the removal to *not* call SetLayerTreeHost.
  805. // This variation assumes that the caller will re-add the layer (probably to
  806. // the same layer tree host) and then call SetLayerTreeHost.
  807. enum class RemovalReason {
  808. kNormal,
  809. kForReadd,
  810. };
  811. void SetParent(Layer* layer, RemovalReason reason);
  812. // This should only be called from RemoveFromParent().
  813. void RemoveChild(Layer* child, RemovalReason reason);
  814. // Variant (for internal use) of RemoveFromParent (which is a widely-used
  815. // public API) as though it were passed RemovalReason::kForReadd.
  816. void RemoveFromParentForReadd();
  817. bool GetBitFlag(uint8_t mask) const;
  818. // invalidate: if true and the flag's value changes, the host is marked as
  819. // needing a property tree update and commit.
  820. // needs_push: if true and the flag's value changes, the layer is marked as
  821. // needing to push its properties to its corresponding LayerImpl, but
  822. // without marking the host as needing a property update or commit.
  823. // return value: 'true' if the flag's value changes.
  824. bool SetBitFlag(bool new_value,
  825. uint8_t mask,
  826. bool invalidate = false,
  827. bool needs_push = false);
  828. // When we detach or attach layer to new LayerTreeHost, all property trees'
  829. // indices becomes invalid.
  830. void InvalidatePropertyTreesIndices();
  831. // This is set whenever a property changed on layer that affects whether this
  832. // layer should own a property tree node or not.
  833. void SetPropertyTreesNeedRebuild();
  834. // For layer tree mode only.
  835. // Fast-path for |SetScrollOffset| and |SetScrollOffsetFromImplSide| to
  836. // directly update scroll offset values in the property tree without needing a
  837. // full property tree update. If property trees do not exist yet, ensures
  838. // they are marked as needing to be rebuilt.
  839. void UpdatePropertyTreeScrollOffset();
  840. void SetMirrorCount(int mirror_count);
  841. int transform_tree_index(const PropertyTrees&) const;
  842. int clip_tree_index(const PropertyTrees&) const;
  843. int effect_tree_index(const PropertyTrees&) const;
  844. int scroll_tree_index(const PropertyTrees&) const;
  845. // Contains a set of input properties that are infrequently set on layers,
  846. // generally speaking in <10% of use cases. When adding new values to this
  847. // struct, consider the memory implications versus simply adding to Inputs.
  848. struct RareInputs {
  849. viz::RegionCaptureBounds capture_bounds;
  850. Region non_fast_scrollable_region;
  851. Region wheel_event_region;
  852. };
  853. RareInputs& EnsureRareInputs() {
  854. auto& rare_inputs = inputs_.Write(*this).rare_inputs;
  855. if (!rare_inputs)
  856. rare_inputs = std::make_unique<RareInputs>();
  857. return *rare_inputs;
  858. }
  859. // Encapsulates all data, callbacks or interfaces received from the embedder.
  860. struct Inputs {
  861. Inputs();
  862. ~Inputs();
  863. // In layer list mode, only the root layer can have children.
  864. // TODO(wangxianzhu): Move this field into LayerTreeHost when we remove
  865. // layer tree mode.
  866. LayerList children;
  867. gfx::Size bounds;
  868. // Hit testing depends on this bit.
  869. bool hit_testable : 1;
  870. bool contents_opaque : 1;
  871. bool contents_opaque_for_text : 1;
  872. bool is_drawable : 1;
  873. bool double_sided : 1;
  874. SkColor4f background_color;
  875. TouchActionRegion touch_action_region;
  876. ElementId element_id;
  877. std::unique_ptr<RareInputs> rare_inputs;
  878. };
  879. // These inputs are used in layer tree mode (ui compositor) only. Most of them
  880. // are inputs of PropertyTreeBuilder for this layer. A few of them are for
  881. // ui-compositor-specific features (i.e. mirror and copy request) which will
  882. // be still used after the ui compositor switch to layer tree mode, but for
  883. // now they work in layer tree mode only.
  884. struct LayerTreeInputs {
  885. LayerTreeInputs();
  886. ~LayerTreeInputs();
  887. gfx::Rect clip_rect;
  888. // If not null, points to one of child layers which is set as mask layer
  889. // by SetMaskLayer().
  890. raw_ptr<PictureLayer> mask_layer = nullptr;
  891. float opacity = 1.0f;
  892. SkBlendMode blend_mode = SkBlendMode::kSrcOver;
  893. bool masks_to_bounds : 1;
  894. // If set, disables this layer's rounded corner from triggering a render
  895. // surface on itself if possible.
  896. bool is_fast_rounded_corner : 1;
  897. bool user_scrollable_horizontal : 1;
  898. bool user_scrollable_vertical : 1;
  899. bool trilinear_filtering : 1;
  900. bool hide_layer_and_subtree : 1;
  901. // Indicates that this layer will need a scroll property node and that this
  902. // layer's bounds correspond to the scroll node's bounds (both |bounds| and
  903. // |scroll_container_bounds|).
  904. bool scrollable : 1;
  905. gfx::PointF position;
  906. gfx::Transform transform;
  907. gfx::Point3F transform_origin;
  908. // A unique ID that identifies the layer subtree rooted at this layer, so
  909. // that it can be independently captured by the FrameSinkVideoCapturer. If
  910. // this ID is set (i.e. valid), it would force this subtree into a render
  911. // surface that darws in a render pass.
  912. viz::SubtreeCaptureId subtree_capture_id;
  913. SkColor4f safe_opaque_background_color = SkColors::kTransparent;
  914. FilterOperations filters;
  915. FilterOperations backdrop_filters;
  916. absl::optional<gfx::RRectF> backdrop_filter_bounds;
  917. float backdrop_filter_quality = 1.0f;
  918. int mirror_count = 0;
  919. gfx::PointF scroll_offset;
  920. // Size of the scroll container that this layer scrolls in.
  921. gfx::Size scroll_container_bounds;
  922. // Corner clip radius for the 4 corners of the layer in the following order:
  923. // top left, top right, bottom right, bottom left
  924. gfx::RoundedCornersF corner_radii;
  925. // Linear gradient mask applied to the layer's clip bounds and optionally
  926. // the rounded corner given by |corner_radii|.
  927. gfx::LinearGradient gradient_mask;
  928. base::RepeatingCallback<void(const gfx::PointF&, const ElementId&)>
  929. did_scroll_callback;
  930. std::vector<std::unique_ptr<viz::CopyOutputRequest>> copy_requests;
  931. };
  932. // Set either one or both components of the mask filter info which is then
  933. // applied to the layer and its
  934. // subtree (as if they are together as a single composited entity) when
  935. // blitting into their target.
  936. void UpdateMaskFilterInfo(const gfx::RoundedCornersF* corner_radii,
  937. const gfx::LinearGradient* gradient_mask);
  938. ProtectedSequenceReadable<raw_ptr<Layer>> parent_;
  939. // Layer instances have a weak pointer to their LayerTreeHost.
  940. // This pointer value is nil when a Layer is not in a tree and is
  941. // updated via SetLayerTreeHost() if a layer moves between trees.
  942. //
  943. // Note about const-ness: layer_tree_host_ cannot be
  944. // ProtectedSequence(Readable|Writable), because that would create a circular
  945. // reference in WaitForProtectedSequenceCompletion(). However, it's definitely
  946. // *not* OK to modify layer_tree_host_ while in a protected sequence. To make
  947. // it hard to do the wrong thing, layer_tree_host_ is const, and
  948. // SetLayerTreeHost() uses a custom protected sequence check, and then uses
  949. // const_cast to do the assignment.
  950. const raw_ptr<LayerTreeHost> layer_tree_host_;
  951. ProtectedSequenceReadable<Inputs> inputs_;
  952. ProtectedSequenceReadable<std::unique_ptr<LayerTreeInputs>>
  953. layer_tree_inputs_;
  954. ProtectedSequenceWritable<gfx::Rect> update_rect_;
  955. const int layer_id_;
  956. ProtectedSequenceReadable<int> num_descendants_that_draw_content_;
  957. ProtectedSequenceReadable<int> transform_tree_index_;
  958. ProtectedSequenceReadable<int> effect_tree_index_;
  959. ProtectedSequenceReadable<int> clip_tree_index_;
  960. ProtectedSequenceReadable<int> scroll_tree_index_;
  961. ProtectedSequenceReadable<int> property_tree_sequence_number_;
  962. ProtectedSequenceReadable<gfx::Vector2dF> offset_to_transform_parent_;
  963. // When true, the layer is about to perform an update. Any commit requests
  964. // will be handled implicitly after the update completes. Not a bitfield
  965. // because it's used in base::AutoReset.
  966. ProtectedSequenceReadable<bool> ignore_set_needs_commit_for_test_;
  967. enum : uint8_t {
  968. kDrawsContentFlagMask = 1 << 0,
  969. kShouldCheckBackfaceVisibilityFlagMask = 1 << 1,
  970. kCacheRenderSurfaceFlagMask = 1 << 2,
  971. kForceRenderSurfaceForTestingFlagMask = 1 << 3,
  972. kMayContainVideoFlagMask = 1 << 4,
  973. kHasTransformNodeFlagMask = 1 << 5,
  974. kHasClipNodeFlagMask = 1 << 6,
  975. kSubtreeHasCopyRequestFlagMask = 1 << 7
  976. };
  977. ProtectedSequenceReadable<uint8_t> bitflags_;
  978. ProtectedSequenceWritable<bool> subtree_property_changed_;
  979. #if DCHECK_IS_ON()
  980. class AllowRemoveForReadd {
  981. public:
  982. explicit AllowRemoveForReadd(Layer* layer) : layer_(layer) {
  983. // Assume these will never be nested. If this DCHECK() fails due to
  984. // nesting, we could convert to using base::AutoReset.
  985. DCHECK(!layer_->allow_remove_for_readd_);
  986. layer_->allow_remove_for_readd_ = true;
  987. }
  988. ~AllowRemoveForReadd() {
  989. // Check that the layer has actually been re-added.
  990. DCHECK(layer_->parent());
  991. // Assume these will never be nested. If this DCHECK() fails due to
  992. // nesting, we could convert to using base::AutoReset.
  993. DCHECK(layer_->allow_remove_for_readd_);
  994. layer_->allow_remove_for_readd_ = false;
  995. }
  996. AllowRemoveForReadd(const AllowRemoveForReadd&) = delete;
  997. AllowRemoveForReadd& operator=(const AllowRemoveForReadd&) = delete;
  998. private:
  999. raw_ptr<Layer> layer_;
  1000. };
  1001. bool allow_remove_for_readd_ = false;
  1002. #else
  1003. class AllowRemoveForReadd {
  1004. public:
  1005. explicit AllowRemoveForReadd(Layer* layer) {}
  1006. AllowRemoveForReadd(const AllowRemoveForReadd&) = delete;
  1007. AllowRemoveForReadd& operator=(const AllowRemoveForReadd&) = delete;
  1008. };
  1009. #endif
  1010. ProtectedSequenceWritable<std::unique_ptr<LayerDebugInfo>> debug_info_;
  1011. static constexpr gfx::Transform kIdentityTransform{};
  1012. static constexpr gfx::RoundedCornersF kNoRoundedCornersF{};
  1013. };
  1014. } // namespace cc
  1015. #endif // CC_LAYERS_LAYER_H_