Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 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 | |
| 8 | #ifndef SkSGNode_DEFINED |
| 9 | #define SkSGNode_DEFINED |
| 10 | |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 11 | #include "SkRect.h" |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 13 | |
| 14 | #include <vector> |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 15 | |
| 16 | class SkCanvas; |
| 17 | class SkMatrix; |
| 18 | |
| 19 | namespace sksg { |
| 20 | |
| 21 | class InvalidationController; |
| 22 | |
| 23 | /** |
| 24 | * Base class for all scene graph nodes. |
| 25 | * |
| 26 | * Handles ingress edge management for the DAG (i.e. node -> "parent" node mapping), |
| 27 | * and invalidation. |
| 28 | * |
| 29 | * Note: egress edges are only implemented/supported in container subclasses |
| 30 | * (e.g. Group, Effect, Draw). |
| 31 | */ |
| 32 | class Node : public SkRefCnt { |
| 33 | public: |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 34 | // Traverse the DAG and revalidate any dependant/invalidated nodes. |
| 35 | // Returns the bounding box for the DAG fragment. |
| 36 | const SkRect& revalidate(InvalidationController*, const SkMatrix&); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 37 | |
| 38 | protected: |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 39 | enum InvalTraits { |
| 40 | // Nodes with this trait never generate direct damage -- instead, |
| 41 | // the damage bubbles up to ancestors. |
| 42 | kBubbleDamage_Trait = 1 << 0, |
| 43 | }; |
| 44 | |
| 45 | explicit Node(uint32_t invalTraits); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 46 | ~Node() override; |
| 47 | |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 48 | const SkRect& bounds() const { |
| 49 | SkASSERT(!this->hasInval()); |
| 50 | return fBounds; |
| 51 | } |
| 52 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 53 | // Tag this node for invalidation and optional damage. |
| 54 | void invalidate(bool damage = true); |
| 55 | bool hasInval() const { return fFlags & kInvalidated_Flag; } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 56 | |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 57 | // Dispatched on revalidation. Subclasses are expected to recompute/cache their properties |
| 58 | // and return their bounding box in local coordinates. |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 59 | virtual SkRect onRevalidate(InvalidationController*, const SkMatrix& ctm) = 0; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 60 | |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 61 | // Register/unregister |this| to receive invalidation events from a descendant. |
| 62 | void observeInval(const sk_sp<Node>&); |
| 63 | void unobserveInval(const sk_sp<Node>&); |
| 64 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 65 | private: |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 66 | enum Flags { |
| 67 | kInvalidated_Flag = 1 << 0, // the node or its descendants require revalidation |
| 68 | kDamage_Flag = 1 << 1, // the node contributes damage during revalidation |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 69 | kObserverArray_Flag = 1 << 2, // the node has more than one inval observer |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 70 | kInTraversal_Flag = 1 << 3, // the node is part of a traversal (cycle detection) |
| 71 | }; |
| 72 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 73 | template <typename Func> |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 74 | void forEachInvalObserver(Func&&) const; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 75 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 76 | class ScopedFlag; |
| 77 | |
| 78 | union { |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 79 | Node* fInvalObserver; |
| 80 | std::vector<Node*>* fInvalObserverArray; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 81 | }; |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 82 | SkRect fBounds; |
| 83 | const uint32_t fInvalTraits : 16; |
| 84 | uint32_t fFlags : 16; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 85 | |
| 86 | typedef SkRefCnt INHERITED; |
| 87 | }; |
| 88 | |
| 89 | // Helper for defining attribute getters/setters in subclasses. |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 90 | #define SG_ATTRIBUTE(attr_name, attr_type, attr_container) \ |
| 91 | const attr_type& get##attr_name() const { return attr_container; } \ |
| 92 | void set##attr_name(const attr_type& v) { \ |
| 93 | if (attr_container == v) return; \ |
| 94 | attr_container = v; \ |
| 95 | this->invalidate(); \ |
Florin Malita | cd05b19 | 2018-04-25 21:43:03 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #define SG_MAPPED_ATTRIBUTE(attr_name, attr_type, attr_container) \ |
| 99 | attr_type get##attr_name() const { return attr_container.get##attr_name(); } \ |
| 100 | void set##attr_name(const attr_type& v) { \ |
| 101 | if (attr_container.get##attr_name() == v) return; \ |
| 102 | attr_container.set##attr_name(v); \ |
| 103 | this->invalidate(); \ |
| 104 | } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 105 | |
| 106 | } // namespace sksg |
| 107 | |
| 108 | #endif // SkSGNode_DEFINED |