diff options
author | 2017-12-19 12:21:02 -0500 | |
---|---|---|
committer | 2017-12-21 20:56:32 +0000 | |
commit | 4aa4441186b06565a597ec4a9baac5a972fddb51 (patch) | |
tree | 8ae7cb5e988f3b89dfea886e118931e78013ff63 /experimental/sksg/SkSGNode.cpp | |
parent | f059e7ca181ad20fa94edea65c8bfd839eceab46 (diff) |
Initial scene graph (SkSG)
Sketching a thin (as in close-to-skia-semantics) scene graph API, focused on
external animation, inval tracking and minimal repaint.
Only a few concrete classes/features so far:
* Rect/Color/Transform/Group
* basic inval tracking
* a trivial animated sample with inval visualization
Pretty much everything (especially naming) is volatile, so treat accordingly.
The interesting bits to review are likely in Node.{h,cpp} for inval and
SampleSGInval.cpp for usage.
Initial class hierarchy:
* Node: invalidation/ancestors tracking
|
-- * RenderNode: onRender(SkCanvas)
| |
| -- * Draw (concrete): rendering a [geometry, paint] tuple
| |
| -- * Group (concrete): grouping multiple RenderNodes
| |
| -- * EffectNode: single-descendant effect wrapper
| |
| -- * Transform (concrete): transform effect
|
-- * PaintNode: onMakePaint()
| |
| -- * Color (concrete): SkColor paint wrapper
|
-- * GeometryNode: onComputeBounds(), onDraw(SkCanvas, SkPaint)
|
-- * Rect (concrete): SkRect wrapper
TBR=
Change-Id: Iacf9b773c181a7582ecd31ee968562f179d1aa1b
Reviewed-on: https://skia-review.googlesource.com/85502
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Diffstat (limited to 'experimental/sksg/SkSGNode.cpp')
-rw-r--r-- | experimental/sksg/SkSGNode.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/experimental/sksg/SkSGNode.cpp b/experimental/sksg/SkSGNode.cpp new file mode 100644 index 0000000000..3b404edd50 --- /dev/null +++ b/experimental/sksg/SkSGNode.cpp @@ -0,0 +1,117 @@ +/* + * 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" + +namespace sksg { + +class Node::ScopedFlag { +public: + ScopedFlag(Node* node, uint32_t flag) + : fNode(node) + , fFlag(flag) { + SkASSERT(!(fNode->fFlags & fFlag)); + fNode->fFlags |= fFlag; + } + ~ScopedFlag() { + fNode->fFlags &= ~fFlag;; + } + +private: + Node* fNode; + uint32_t fFlag; +}; + +#define TRAVERSAL_GUARD \ + if (this->fFlags & kInTraversal_Flag) { \ + return; \ + } \ + ScopedFlag traversal_guard(this, kInTraversal_Flag); + +Node::Node() + : fInvalReceiver(nullptr) + , fFlags(kInvalidated_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::invalidate() { + TRAVERSAL_GUARD + + if (this->isInvalidated()) { + return; + } + + fFlags |= kInvalidated_Flag; + forEachInvalReceiver([&](Node* receiver) { + receiver->invalidate(); + }); +} + +void Node::revalidate(InvalidationController* ic, const SkMatrix& ctm) { + TRAVERSAL_GUARD + + if (this->isInvalidated()) { + this->onRevalidate(ic, ctm); + fFlags &= ~kInvalidated_Flag; + } +} + +} // namespace sksg |