skia2/experimental/sksg/SkSGNode.cpp
Florin Malita c75e2401a8 [sksg] Refine invalidation logic
We need to discriminate between nodes whose bounds updates contribute to the dirty
region, and nodes whose bounds changes do not.

E.g. animated shape in a group: the animated shape node bounds should yield damage,
but the ancestor group bounds should not.

To accomplish this, we refine the invalidation state:

  1) self invalidation == the node itself was invalidated, and its bounds updates
     yield damage.
  2) descendant invalidation == the node has some (self-)invalidated descendant,
     but its own bounds are not contributing damage.

Also:

  * hoist the bounding box invalidation logic into the base class (Node::revalidate)
    and update to respect the states described above.
  * remove (now-redundant) GeometryNode bbox logic.
  * update revalidation methods to return the node bbox instead of void

TBR=
Change-Id: I8023d1793fb501c945a53f2dc2d2983e5b620ade
Reviewed-on: https://skia-review.googlesource.com/90581
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
2018-01-04 00:59:20 +00:00

150 lines
3.3 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;
}
SkRect prevBounds;
if (this->hasSelfInval()) {
prevBounds = fBounds;
}
fBounds = this->onRevalidate(ic, ctm);
if (this->hasSelfInval()) {
ic->inval(prevBounds, ctm);
if (fBounds != prevBounds) {
ic->inval(fBounds, ctm);
}
}
fFlags &= ~(kInvalSelf_Flag | kInvalDescendant_Flag);
return fBounds;
}
} // namespace sksg