Skia
2D Graphics Library
SkSGNode.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkSGNode_DEFINED
9 #define SkSGNode_DEFINED
10 
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 
14 #include <vector>
15 
16 class SkCanvas;
17 class SkMatrix;
18 
19 namespace sksg {
20 
21 class InvalidationController;
22 
32 class Node : public SkRefCnt {
33 public:
34  // Traverse the DAG and revalidate any dependant/invalidated nodes.
35  // Returns the bounding box for the DAG fragment.
37 
38  // Tag this node for invalidation and optional damage.
39  void invalidate(bool damage = true);
40 
41 protected:
42  enum InvalTraits {
43  // Nodes with this trait never generate direct damage -- instead,
44  // the damage bubbles up to ancestors.
46 
47  // Nodes with this trait obscure the descendants' damage and always override it.
49  };
50 
51  explicit Node(uint32_t invalTraits);
52  ~Node() override;
53 
54  const SkRect& bounds() const {
55  SkASSERT(!this->hasInval());
56  return fBounds;
57  }
58 
59  bool hasInval() const { return fFlags & kInvalidated_Flag; }
60 
61  // Dispatched on revalidation. Subclasses are expected to recompute/cache their properties
62  // and return their bounding box in local coordinates.
64 
65  // Register/unregister |this| to receive invalidation events from a descendant.
66  void observeInval(const sk_sp<Node>&);
68 
69 private:
70  enum Flags {
71  kInvalidated_Flag = 1 << 0, // the node or its descendants require revalidation
72  kDamage_Flag = 1 << 1, // the node contributes damage during revalidation
73  kObserverArray_Flag = 1 << 2, // the node has more than one inval observer
74  kInTraversal_Flag = 1 << 3, // the node is part of a traversal (cycle detection)
75  };
76 
77  template <typename Func>
78  void forEachInvalObserver(Func&&) const;
79 
80  class ScopedFlag;
81 
82  union {
84  std::vector<Node*>* fInvalObserverArray;
85  };
86  SkRect fBounds;
87  const uint32_t fInvalTraits : 2;
88  uint32_t fFlags : 4; // Internal flags.
89  uint32_t fNodeFlags : 8; // Accessible from select subclasses.
90  // Free bits : 18;
91 
92  friend class NodePriv;
93  friend class RenderNode; // node flags access
94 
95  using INHERITED = SkRefCnt;
96 };
97 
98 // Helper for defining attribute getters/setters in subclasses.
99 #define SG_ATTRIBUTE(attr_name, attr_type, attr_container) \
100  const attr_type& get##attr_name() const { return attr_container; } \
101  void set##attr_name(const attr_type& v) { \
102  if (attr_container == v) return; \
103  attr_container = v; \
104  this->invalidate(); \
105  } \
106  void set##attr_name(attr_type&& v) { \
107  if (attr_container == v) return; \
108  attr_container = std::move(v); \
109  this->invalidate(); \
110  }
111 
112 #define SG_MAPPED_ATTRIBUTE(attr_name, attr_type, attr_container) \
113  attr_type get##attr_name() const { return attr_container.get##attr_name(); } \
114  void set##attr_name(const attr_type& v) { \
115  if (attr_container.get##attr_name() == v) return; \
116  attr_container.set##attr_name(v); \
117  this->invalidate(); \
118  } \
119  void set##attr_name(attr_type&& v) { \
120  if (attr_container.get##attr_name() == v) return; \
121  attr_container.set##attr_name(std::move(v)); \
122  this->invalidate(); \
123  }
124 
125 } // namespace sksg
126 
127 #endif // SkSGNode_DEFINED
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
Definition: SkRefCnt.h:119
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220
Receiver for invalidation events.
Definition: SkSGInvalidationController.h:25
Base class for all scene graph nodes.
Definition: SkSGNode.h:32
const SkRect & revalidate(InvalidationController *, const SkMatrix &)
const SkRect & bounds() const
Definition: SkSGNode.h:54
~Node() override
Node * fInvalObserver
Definition: SkSGNode.h:83
friend class NodePriv
Definition: SkSGNode.h:92
void invalidate(bool damage=true)
Node(uint32_t invalTraits)
void observeInval(const sk_sp< Node > &)
bool hasInval() const
Definition: SkSGNode.h:59
InvalTraits
Definition: SkSGNode.h:42
@ kOverrideDamage_Trait
Definition: SkSGNode.h:48
@ kBubbleDamage_Trait
Definition: SkSGNode.h:45
std::vector< Node * > * fInvalObserverArray
Definition: SkSGNode.h:84
virtual SkRect onRevalidate(InvalidationController *, const SkMatrix &ctm)=0
void unobserveInval(const sk_sp< Node > &)
Base class for nodes which can render to a canvas.
Definition: SkSGRenderNode.h:27
Definition: Skottie.h:30
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582