Flutter Engine
The Flutter Engine
SkSGNode.cpp
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
9
12#include "src/core/SkRectPriv.h"
13
14#include <algorithm>
15
16namespace sksg {
17
19public:
20 ScopedFlag(Node* node, uint32_t flag)
21 : fNode(node)
22 , fFlag(flag)
23 , fWasSet(node->fFlags & flag) {
24 node->fFlags |= flag;
25 }
27 if (!fWasSet) {
28 fNode->fFlags &= ~fFlag;
29 }
30 }
31
32 bool wasSet() const { return fWasSet; }
33
34private:
35 Node* fNode;
36 uint32_t fFlag;
37 bool fWasSet;
38};
39
40#define TRAVERSAL_GUARD \
41 ScopedFlag traversal_guard(this, kInTraversal_Flag); \
42 if (traversal_guard.wasSet()) \
43 return
44
45Node::Node(uint32_t invalTraits)
46 : fInvalObserver(nullptr)
47 , fBounds(SkRectPriv::MakeLargeS32())
48 , fInvalTraits(invalTraits)
49 , fFlags(kInvalidated_Flag)
50 , fNodeFlags(0) {}
51
53 if (fFlags & kObserverArray_Flag) {
56 } else {
58 }
59}
60
62 SkASSERT(node);
63 if (!(node->fFlags & kObserverArray_Flag)) {
64 if (!node->fInvalObserver) {
65 node->fInvalObserver = this;
66 return;
67 }
68
69 auto observers = new std::vector<Node*>();
70 observers->reserve(2);
71 observers->push_back(node->fInvalObserver);
72
73 node->fInvalObserverArray = observers;
74 node->fFlags |= kObserverArray_Flag;
75 }
76
77 // No duplicate observers.
78 SkASSERT(std::find(node->fInvalObserverArray->begin(),
79 node->fInvalObserverArray->end(), this) == node->fInvalObserverArray->end());
80
81 node->fInvalObserverArray->push_back(this);
82}
83
85 SkASSERT(node);
86 if (!(node->fFlags & kObserverArray_Flag)) {
87 SkASSERT(node->fInvalObserver == this);
88 node->fInvalObserver = nullptr;
89 return;
90 }
91
92 SkDEBUGCODE(const auto origSize = node->fInvalObserverArray->size());
93 node->fInvalObserverArray->erase(std::remove(node->fInvalObserverArray->begin(),
94 node->fInvalObserverArray->end(), this),
95 node->fInvalObserverArray->end());
96 SkASSERT(node->fInvalObserverArray->size() == origSize - 1);
97}
98
99template <typename Func>
100void Node::forEachInvalObserver(Func&& func) const {
101 if (fFlags & kObserverArray_Flag) {
102 for (const auto& parent : *fInvalObserverArray) {
103 func(parent);
104 }
105 return;
106 }
107
108 if (fInvalObserver) {
109 func(fInvalObserver);
110 }
111}
112
113void Node::invalidate(bool damageBubbling) {
115
116 if (this->hasInval() && (!damageBubbling || (fFlags & kDamage_Flag))) {
117 // All done.
118 return;
119 }
120
121 if (damageBubbling && !(fInvalTraits & kBubbleDamage_Trait)) {
122 // Found a damage observer.
123 fFlags |= kDamage_Flag;
124 damageBubbling = false;
125 }
126
127 fFlags |= kInvalidated_Flag;
128
129 forEachInvalObserver([&](Node* observer) {
130 observer->invalidate(damageBubbling);
131 });
132}
133
135 TRAVERSAL_GUARD fBounds;
136
137 if (!this->hasInval()) {
138 return fBounds;
139 }
140
141 const auto generate_damage =
142 ic && ((fFlags & kDamage_Flag) || (fInvalTraits & kOverrideDamage_Trait));
143 if (!generate_damage) {
144 // Trivial transitive revalidation.
145 fBounds = this->onRevalidate(ic, ctm);
146 } else {
147 // Revalidate and emit damage for old-bounds, new-bounds.
148 const auto prev_bounds = fBounds;
149
150 auto* ic_override = (fInvalTraits & kOverrideDamage_Trait) ? nullptr : ic;
151 fBounds = this->onRevalidate(ic_override, ctm);
152
153 ic->inval(prev_bounds, ctm);
154 if (fBounds != prev_bounds) {
155 ic->inval(fBounds, ctm);
156 }
157 }
158
159 fFlags &= ~(kInvalidated_Flag | kDamage_Flag);
160
161 return fBounds;
162}
163
164} // namespace sksg
const SkRect fBounds
uint16_t fFlags
Definition: ShapeLayer.cpp:106
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define TRAVERSAL_GUARD
Definition: SkSGNode.cpp:40
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
int find(T *array, int N, T item)
void inval(const SkRect &, const SkMatrix &ctm=SkMatrix::I())
bool wasSet() const
Definition: SkSGNode.cpp:32
ScopedFlag(Node *node, uint32_t flag)
Definition: SkSGNode.cpp:20
Node * fInvalObserver
Definition: SkSGNode.h:84
~Node() override
Definition: SkSGNode.cpp:52
const SkRect & revalidate(InvalidationController *, const SkMatrix &)
Definition: SkSGNode.cpp:134
void observeInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:61
void unobserveInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:84
bool hasInval() const
Definition: SkSGNode.h:60
@ kOverrideDamage_Trait
Definition: SkSGNode.h:49
@ kBubbleDamage_Trait
Definition: SkSGNode.h:46
std::vector< Node * > * fInvalObserverArray
Definition: SkSGNode.h:85
void invalidate(bool damage=true)
Definition: SkSGNode.cpp:113
Node(uint32_t invalTraits)
Definition: SkSGNode.cpp:45
virtual SkRect onRevalidate(InvalidationController *, const SkMatrix &ctm)=0
FlutterSemanticsFlag flag
def remove(*paths)
Definition: Skottie.h:32