skia2/experimental/sksg/SkSGNode.cpp
Florin Malita 0ebf4192f1 [sksg] More inval fiddling
Node subclasses can now control whether their bounds (changes)
contribute to damage.

Tristate:

  * Default:   The node bounds contribute to damage if the node itself was
               invalidated, observing hasSelfInval().  This is the default
               behavior.

  * ForceSelf: The node bounds contribute to damage, regardless of
               hasSelfInval().  Used for domain-boundary nodes (e.g. Draw),
               which gate blocked fragments (e.g. geometry, paint nodes).

  * BlockSelf: The node bounds do not contribute to damage, regardless of
               hasSelfInval().  Used for nodes which do not contribute
               damage directly (e.g. paints, geometry).

TBR=
Change-Id: I7c941c7ea12e14b008d846ec13108e66e34dbc73
Reviewed-on: https://skia-review.googlesource.com/91104
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
2018-01-05 00:42:14 +00:00

150 lines
3.4 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkSGNode.h"
#include "SkSGInvalidationController.h"
namespace sksg {
class Node::ScopedFlag {
public:
ScopedFlag(Node* node, uint32_t flag)
: fNode(node)
, fFlag(flag)
, fWasSet(node->fFlags & flag) {
node->fFlags |= flag;
}
~ScopedFlag() {
if (!fWasSet) {
fNode->fFlags &= ~fFlag;;
}
}
bool wasSet() const { return fWasSet; }
private:
Node* fNode;
uint32_t fFlag;
bool fWasSet;
};
#define TRAVERSAL_GUARD \
ScopedFlag traversal_guard(this, kInTraversal_Flag); \
if (traversal_guard.wasSet()) \
return
Node::Node()
: fInvalReceiver(nullptr)
, fBounds(SkRect::MakeLargestS32())
, fFlags(kInvalSelf_Flag | kInvalDescendant_Flag) {}
Node::~Node() {
if (fFlags & kReceiverArray_Flag) {
SkASSERT(fInvalReceiverArray->isEmpty());
delete fInvalReceiverArray;
} else {
SkASSERT(!fInvalReceiver);
}
}
void Node::addInvalReceiver(Node* receiver) {
if (!(fFlags & kReceiverArray_Flag)) {
if (!fInvalReceiver) {
fInvalReceiver = receiver;
return;
}
auto receivers = new SkTDArray<Node*>();
receivers->setReserve(2);
receivers->push(fInvalReceiver);
fInvalReceiverArray = receivers;
fFlags |= kReceiverArray_Flag;
}
// No duplicate receivers.
SkASSERT(fInvalReceiverArray->find(receiver) < 0);
fInvalReceiverArray->push(receiver);
}
void Node::removeInvalReceiver(Node* receiver) {
if (!(fFlags & kReceiverArray_Flag)) {
SkASSERT(fInvalReceiver == receiver);
fInvalReceiver = nullptr;
return;
}
const auto idx = fInvalReceiverArray->find(receiver);
SkASSERT(idx >= 0);
fInvalReceiverArray->remove(idx);
}
template <typename Func>
void Node::forEachInvalReceiver(Func&& func) const {
if (fFlags & kReceiverArray_Flag) {
for (const auto& parent : *fInvalReceiverArray) {
func(parent);
}
return;
}
if (fInvalReceiver) {
func(fInvalReceiver);
}
}
void Node::invalidateSelf() {
if (this->hasSelfInval()) {
return;
}
fFlags |= kInvalSelf_Flag;
this->invalidateAncestors();
}
void Node::invalidateAncestors() {
TRAVERSAL_GUARD;
forEachInvalReceiver([&](Node* receiver) {
if (receiver->hasDescendantInval()) {
return;
}
receiver->fFlags |= kInvalDescendant_Flag;
receiver->invalidateAncestors();
});
}
const SkRect& Node::revalidate(InvalidationController* ic, const SkMatrix& ctm) {
TRAVERSAL_GUARD fBounds;
if (!this->hasInval()) {
return fBounds;
}
const auto result = this->onRevalidate(ic, ctm);
const auto selfDamage = result.fReval == Damage::kForceSelf ||
(this->hasSelfInval() && result.fReval != Damage::kBlockSelf);
if (selfDamage) {
// old bounds
ic->inval(fBounds, ctm);
if (result.fBounds != fBounds) {
// new bounds
ic->inval(result.fBounds, ctm);
}
}
fBounds = result.fBounds;
fFlags &= ~(kInvalSelf_Flag | kInvalDescendant_Flag);
return fBounds;
}
} // namespace sksg