2017-12-19 17:21:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-08 20:05:02 +00:00
|
|
|
#include "SkRectPriv.h"
|
2017-12-19 17:21:02 +00:00
|
|
|
#include "SkSGNode.h"
|
2018-01-03 21:17:29 +00:00
|
|
|
#include "SkSGInvalidationController.h"
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
class Node::ScopedFlag {
|
|
|
|
public:
|
|
|
|
ScopedFlag(Node* node, uint32_t flag)
|
|
|
|
: fNode(node)
|
2018-01-03 21:17:29 +00:00
|
|
|
, fFlag(flag)
|
|
|
|
, fWasSet(node->fFlags & flag) {
|
|
|
|
node->fFlags |= flag;
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
~ScopedFlag() {
|
2018-01-03 21:17:29 +00:00
|
|
|
if (!fWasSet) {
|
|
|
|
fNode->fFlags &= ~fFlag;;
|
|
|
|
}
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 21:17:29 +00:00
|
|
|
bool wasSet() const { return fWasSet; }
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
private:
|
|
|
|
Node* fNode;
|
|
|
|
uint32_t fFlag;
|
2018-01-03 21:17:29 +00:00
|
|
|
bool fWasSet;
|
2017-12-19 17:21:02 +00:00
|
|
|
};
|
|
|
|
|
2018-01-03 21:17:29 +00:00
|
|
|
#define TRAVERSAL_GUARD \
|
|
|
|
ScopedFlag traversal_guard(this, kInTraversal_Flag); \
|
|
|
|
if (traversal_guard.wasSet()) \
|
|
|
|
return
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
Node::Node(uint32_t invalTraits)
|
2018-01-22 15:19:28 +00:00
|
|
|
: fInvalObserver(nullptr)
|
2018-01-17 17:20:04 +00:00
|
|
|
, fBounds(SkRectPriv::MakeLargeS32())
|
2018-01-05 16:32:31 +00:00
|
|
|
, fInvalTraits(invalTraits)
|
|
|
|
, fFlags(kInvalidated_Flag) {}
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
Node::~Node() {
|
2018-01-22 15:19:28 +00:00
|
|
|
if (fFlags & kObserverArray_Flag) {
|
|
|
|
SkASSERT(fInvalObserverArray->isEmpty());
|
|
|
|
delete fInvalObserverArray;
|
2017-12-19 17:21:02 +00:00
|
|
|
} else {
|
2018-01-22 15:19:28 +00:00
|
|
|
SkASSERT(!fInvalObserver);
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
void Node::observeInval(const sk_sp<Node>& node) {
|
|
|
|
SkASSERT(node);
|
|
|
|
if (!(node->fFlags & kObserverArray_Flag)) {
|
|
|
|
if (!node->fInvalObserver) {
|
|
|
|
node->fInvalObserver = this;
|
2017-12-19 17:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
auto observers = new SkTDArray<Node*>();
|
|
|
|
observers->setReserve(2);
|
|
|
|
observers->push(node->fInvalObserver);
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
node->fInvalObserverArray = observers;
|
|
|
|
node->fFlags |= kObserverArray_Flag;
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
// No duplicate observers.
|
|
|
|
SkASSERT(node->fInvalObserverArray->find(this) < 0);
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
node->fInvalObserverArray->push(this);
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
void Node::unobserveInval(const sk_sp<Node>& node) {
|
|
|
|
SkASSERT(node);
|
|
|
|
if (!(node->fFlags & kObserverArray_Flag)) {
|
|
|
|
SkASSERT(node->fInvalObserver == this);
|
|
|
|
node->fInvalObserver = nullptr;
|
2017-12-19 17:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
const auto idx = node->fInvalObserverArray->find(this);
|
2017-12-19 17:21:02 +00:00
|
|
|
SkASSERT(idx >= 0);
|
2018-01-22 15:19:28 +00:00
|
|
|
node->fInvalObserverArray->remove(idx);
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
2018-01-22 15:19:28 +00:00
|
|
|
void Node::forEachInvalObserver(Func&& func) const {
|
|
|
|
if (fFlags & kObserverArray_Flag) {
|
|
|
|
for (const auto& parent : *fInvalObserverArray) {
|
2017-12-19 17:21:02 +00:00
|
|
|
func(parent);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
if (fInvalObserver) {
|
|
|
|
func(fInvalObserver);
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
void Node::invalidate(bool damageBubbling) {
|
|
|
|
TRAVERSAL_GUARD;
|
|
|
|
|
|
|
|
if (this->hasInval() && (!damageBubbling || (fFlags & kDamage_Flag))) {
|
|
|
|
// All done.
|
2017-12-19 17:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
if (damageBubbling && !(fInvalTraits & kBubbleDamage_Trait)) {
|
2018-01-22 15:19:28 +00:00
|
|
|
// Found a damage observer.
|
2018-01-05 16:32:31 +00:00
|
|
|
fFlags |= kDamage_Flag;
|
|
|
|
damageBubbling = false;
|
|
|
|
}
|
2018-01-03 21:17:29 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
fFlags |= kInvalidated_Flag;
|
2018-01-03 21:17:29 +00:00
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
forEachInvalObserver([&](Node* observer) {
|
|
|
|
observer->invalidate(damageBubbling);
|
2017-12-19 17:21:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-03 21:17:29 +00:00
|
|
|
const SkRect& Node::revalidate(InvalidationController* ic, const SkMatrix& ctm) {
|
|
|
|
TRAVERSAL_GUARD fBounds;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-03 21:17:29 +00:00
|
|
|
if (!this->hasInval()) {
|
|
|
|
return fBounds;
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
2018-01-03 21:17:29 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
SkRect prevBounds;
|
|
|
|
if (fFlags & kDamage_Flag) {
|
|
|
|
prevBounds = fBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
fBounds = this->onRevalidate(ic, ctm);
|
2018-01-05 00:21:58 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
if (fFlags & kDamage_Flag) {
|
|
|
|
ic->inval(prevBounds, ctm);
|
|
|
|
if (fBounds != prevBounds) {
|
|
|
|
ic->inval(fBounds, ctm);
|
2018-01-03 21:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
fFlags &= ~(kInvalidated_Flag | kDamage_Flag);
|
2018-01-03 21:17:29 +00:00
|
|
|
|
|
|
|
return fBounds;
|
2017-12-19 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sksg
|