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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSGNode_DEFINED
|
|
|
|
#define SkSGNode_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2018-09-02 17:44:13 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
class SkMatrix;
|
|
|
|
|
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
class InvalidationController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for all scene graph nodes.
|
|
|
|
*
|
|
|
|
* Handles ingress edge management for the DAG (i.e. node -> "parent" node mapping),
|
|
|
|
* and invalidation.
|
|
|
|
*
|
|
|
|
* Note: egress edges are only implemented/supported in container subclasses
|
|
|
|
* (e.g. Group, Effect, Draw).
|
|
|
|
*/
|
|
|
|
class Node : public SkRefCnt {
|
|
|
|
public:
|
2018-01-03 21:17:29 +00:00
|
|
|
// Traverse the DAG and revalidate any dependant/invalidated nodes.
|
|
|
|
// Returns the bounding box for the DAG fragment.
|
|
|
|
const SkRect& revalidate(InvalidationController*, const SkMatrix&);
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
protected:
|
2018-01-05 16:32:31 +00:00
|
|
|
enum InvalTraits {
|
|
|
|
// Nodes with this trait never generate direct damage -- instead,
|
|
|
|
// the damage bubbles up to ancestors.
|
2019-02-10 17:54:20 +00:00
|
|
|
kBubbleDamage_Trait = 1 << 0,
|
|
|
|
|
|
|
|
// Nodes with this trait obscure the descendants' damage and always override it.
|
|
|
|
kOverrideDamage_Trait = 1 << 1,
|
2018-01-05 16:32:31 +00:00
|
|
|
};
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
explicit Node(uint32_t invalTraits);
|
|
|
|
~Node() override;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-08 21:42:59 +00:00
|
|
|
const SkRect& bounds() const {
|
|
|
|
SkASSERT(!this->hasInval());
|
|
|
|
return fBounds;
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:32:31 +00:00
|
|
|
// Tag this node for invalidation and optional damage.
|
|
|
|
void invalidate(bool damage = true);
|
|
|
|
bool hasInval() const { return fFlags & kInvalidated_Flag; }
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-03 21:17:29 +00:00
|
|
|
// Dispatched on revalidation. Subclasses are expected to recompute/cache their properties
|
|
|
|
// and return their bounding box in local coordinates.
|
2018-01-05 16:32:31 +00:00
|
|
|
virtual SkRect onRevalidate(InvalidationController*, const SkMatrix& ctm) = 0;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-01-22 15:19:28 +00:00
|
|
|
// Register/unregister |this| to receive invalidation events from a descendant.
|
|
|
|
void observeInval(const sk_sp<Node>&);
|
|
|
|
void unobserveInval(const sk_sp<Node>&);
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
private:
|
2018-01-05 16:32:31 +00:00
|
|
|
enum Flags {
|
|
|
|
kInvalidated_Flag = 1 << 0, // the node or its descendants require revalidation
|
|
|
|
kDamage_Flag = 1 << 1, // the node contributes damage during revalidation
|
2018-01-22 15:19:28 +00:00
|
|
|
kObserverArray_Flag = 1 << 2, // the node has more than one inval observer
|
2018-01-05 16:32:31 +00:00
|
|
|
kInTraversal_Flag = 1 << 3, // the node is part of a traversal (cycle detection)
|
|
|
|
};
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
template <typename Func>
|
2018-01-22 15:19:28 +00:00
|
|
|
void forEachInvalObserver(Func&&) const;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
class ScopedFlag;
|
|
|
|
|
|
|
|
union {
|
2018-09-02 17:44:13 +00:00
|
|
|
Node* fInvalObserver;
|
|
|
|
std::vector<Node*>* fInvalObserverArray;
|
2017-12-19 17:21:02 +00:00
|
|
|
};
|
2018-09-02 17:44:13 +00:00
|
|
|
SkRect fBounds;
|
2019-06-19 00:16:45 +00:00
|
|
|
const uint32_t fInvalTraits : 2;
|
|
|
|
uint32_t fFlags : 4; // Internal flags.
|
|
|
|
uint32_t fNodeFlags : 8; // Accessible from select subclasses.
|
|
|
|
// Free bits : 18;
|
|
|
|
|
2019-07-10 17:38:48 +00:00
|
|
|
friend class NodePriv;
|
2019-06-19 00:16:45 +00:00
|
|
|
friend class RenderNode; // node flags access
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkRefCnt;
|
2017-12-19 17:21:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helper for defining attribute getters/setters in subclasses.
|
2018-01-19 20:07:29 +00:00
|
|
|
#define SG_ATTRIBUTE(attr_name, attr_type, attr_container) \
|
|
|
|
const attr_type& get##attr_name() const { return attr_container; } \
|
|
|
|
void set##attr_name(const attr_type& v) { \
|
|
|
|
if (attr_container == v) return; \
|
|
|
|
attr_container = v; \
|
|
|
|
this->invalidate(); \
|
2018-10-17 21:07:05 +00:00
|
|
|
} \
|
|
|
|
void set##attr_name(attr_type&& v) { \
|
|
|
|
if (attr_container == v) return; \
|
|
|
|
attr_container = std::move(v); \
|
|
|
|
this->invalidate(); \
|
2018-04-26 01:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SG_MAPPED_ATTRIBUTE(attr_name, attr_type, attr_container) \
|
|
|
|
attr_type get##attr_name() const { return attr_container.get##attr_name(); } \
|
|
|
|
void set##attr_name(const attr_type& v) { \
|
|
|
|
if (attr_container.get##attr_name() == v) return; \
|
|
|
|
attr_container.set##attr_name(v); \
|
|
|
|
this->invalidate(); \
|
2018-10-17 21:07:05 +00:00
|
|
|
} \
|
|
|
|
void set##attr_name(attr_type&& v) { \
|
|
|
|
if (attr_container.get##attr_name() == v) return; \
|
|
|
|
attr_container.set##attr_name(std::move(v)); \
|
|
|
|
this->invalidate(); \
|
2018-04-26 01:43:03 +00:00
|
|
|
}
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
} // namespace sksg
|
|
|
|
|
|
|
|
#endif // SkSGNode_DEFINED
|