[skrive] Reset the node system
Tacking another hierarchy on top of SkSG doesn't work well. Let's start fresh. TBR= Change-Id: Ieb379b57e1a77df3c62048d3be7e81e1429f9b23 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/297807 Reviewed-by: Florin Malita <fmalita@google.com> Commit-Queue: Florin Malita <fmalita@google.com>
This commit is contained in:
parent
312669e3b0
commit
579e63af00
@ -32,6 +32,7 @@ if (skia_enable_skrive) {
|
|||||||
]
|
]
|
||||||
sources = [
|
sources = [
|
||||||
"tests/BinaryReader.cpp",
|
"tests/BinaryReader.cpp",
|
||||||
|
"tests/DomTypes.cpp",
|
||||||
"tests/JsonReader.cpp",
|
"tests/JsonReader.cpp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -9,100 +9,159 @@
|
|||||||
#define SkRive_DEFINED
|
#define SkRive_DEFINED
|
||||||
|
|
||||||
#include "include/core/SkBlendMode.h"
|
#include "include/core/SkBlendMode.h"
|
||||||
|
#include "include/core/SkColor.h"
|
||||||
#include "include/core/SkM44.h"
|
#include "include/core/SkM44.h"
|
||||||
#include "include/core/SkRefCnt.h"
|
#include "include/core/SkRefCnt.h"
|
||||||
#include "include/core/SkString.h"
|
#include "include/core/SkString.h"
|
||||||
#include "modules/sksg/include/SkSGGroup.h"
|
|
||||||
#include "modules/sksg/include/SkSGRenderNode.h"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class SkCanvas;
|
||||||
class SkStreamAsset;
|
class SkStreamAsset;
|
||||||
|
|
||||||
namespace skrive {
|
namespace skrive {
|
||||||
|
|
||||||
class Node : public sksg::Group {
|
#define ACTOR_ATTR(attr_name, attr_type, attr_default) \
|
||||||
public:
|
private: \
|
||||||
SG_ATTRIBUTE(Name , SkString , fName )
|
attr_type f##attr_name = attr_default; \
|
||||||
SG_ATTRIBUTE(Translation , SkV2 , fTranslation )
|
public: \
|
||||||
SG_ATTRIBUTE(Scale , SkV2 , fScale )
|
const attr_type& get##attr_name() const { return f##attr_name; } \
|
||||||
SG_ATTRIBUTE(Rotation , float , fRotation )
|
void set##attr_name(const attr_type& v) { \
|
||||||
SG_ATTRIBUTE(Opacity , float , fOpacity )
|
if (f##attr_name == v) return; \
|
||||||
SG_ATTRIBUTE(CollapsedVisibility, bool , fCollapsedVisibility)
|
f##attr_name = v; \
|
||||||
|
this->invalidate(); \
|
||||||
|
} \
|
||||||
|
void set##attr_name(attr_type&& v) { \
|
||||||
|
if (f##attr_name == v) return; \
|
||||||
|
f##attr_name = std::move(v); \
|
||||||
|
this->invalidate(); \
|
||||||
|
}
|
||||||
|
|
||||||
Node() : Node(Type::kNode) {}
|
class Node;
|
||||||
|
|
||||||
|
class Component : public SkRefCnt {
|
||||||
|
public:
|
||||||
|
ACTOR_ATTR(Name, SkString, SkString())
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::enable_if_t<std::is_base_of<Component, T>::value, bool>
|
||||||
|
is() const {
|
||||||
|
if constexpr(std::is_same<Component, T>::value) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return is_base_of<T>(fType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
operator const T*() const {
|
||||||
|
return this->is<T>() ? reinterpret_cast<const T*>(this) : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
operator T*() {
|
||||||
|
return this->is<T>() ? reinterpret_cast<T*>(this) : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void revalidate();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
enum class Type : uint32_t {
|
enum class Type : uint32_t {
|
||||||
kNode, // base group node
|
kNode,
|
||||||
|
kShape,
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit Node(Type t) : fType(t) {}
|
explicit Component(Type t) : fType(t) {}
|
||||||
|
|
||||||
SkRect onRevalidate(sksg::InvalidationController*, const SkMatrix&) override;
|
void invalidate();
|
||||||
|
|
||||||
Type type() const { return fType; }
|
bool hasInval() const { return fDirty; }
|
||||||
|
|
||||||
|
virtual void onRevalidate() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class Node; // parent access
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr bool is_base_of(Type t);
|
||||||
|
|
||||||
const Type fType;
|
const Type fType;
|
||||||
|
|
||||||
SkString fName;
|
Node* fParent = nullptr;
|
||||||
SkV2 fTranslation = {0, 0},
|
bool fDirty = true;
|
||||||
fScale = {1, 1};
|
};
|
||||||
float fRotation = 0,
|
|
||||||
fOpacity = 1;
|
|
||||||
bool fCollapsedVisibility = false;
|
|
||||||
|
|
||||||
using INHERITED = sksg::Group;
|
class Node : public Component {
|
||||||
|
public:
|
||||||
|
Node() : INHERITED(Type::kNode) {}
|
||||||
|
|
||||||
|
ACTOR_ATTR(Translation , SkV2 , SkV2({0, 0}))
|
||||||
|
ACTOR_ATTR(Scale , SkV2 , SkV2({1, 1}))
|
||||||
|
ACTOR_ATTR(Rotation , float, 0 )
|
||||||
|
ACTOR_ATTR(Opacity , float, 1 )
|
||||||
|
ACTOR_ATTR(CollapsedVisibility, bool , false )
|
||||||
|
|
||||||
|
void addChild(sk_sp<Component>);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit Node(Type t) : INHERITED(t) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onRevalidate() override;
|
||||||
|
|
||||||
|
std::vector<sk_sp<Component>> fChildren;
|
||||||
|
|
||||||
|
using INHERITED = Component;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Drawable : public Node {
|
class Drawable : public Node {
|
||||||
public:
|
public:
|
||||||
SG_ATTRIBUTE(DrawOrder, size_t , fDrawOrder)
|
ACTOR_ATTR(DrawOrder, size_t , 0 )
|
||||||
SG_ATTRIBUTE(BlendMode, SkBlendMode, fBlendMode)
|
ACTOR_ATTR(BlendMode, SkBlendMode, SkBlendMode::kSrcOver)
|
||||||
SG_ATTRIBUTE(IsHidden , bool , fIsHidden )
|
ACTOR_ATTR(IsHidden , bool , false )
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit Drawable(Type t) : INHERITED(t) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t fDrawOrder = 0;
|
|
||||||
SkBlendMode fBlendMode = SkBlendMode::kSrcOver;
|
|
||||||
bool fIsHidden = false;
|
|
||||||
|
|
||||||
using INHERITED = Node;
|
using INHERITED = Node;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Shape final : public Drawable {
|
class Shape final : public Drawable {
|
||||||
public:
|
public:
|
||||||
SG_ATTRIBUTE(TransformAffectsStroke, bool, fTransformAffectsStroke)
|
Shape() : INHERITED(Type::kShape) {}
|
||||||
|
|
||||||
|
ACTOR_ATTR(TransformAffectsStroke, bool, true)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool fTransformAffectsStroke = true;
|
void onRevalidate() override;
|
||||||
|
|
||||||
using INHERITED = Drawable;
|
using INHERITED = Drawable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Artboard final : public sksg::RenderNode {
|
template <typename T>
|
||||||
|
constexpr bool Component::is_base_of(Type t) {
|
||||||
|
if (t == Type::kNode ) return std::is_base_of<T, Node >::value;
|
||||||
|
if (t == Type::kShape) return std::is_base_of<T, Shape>::value;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Artboard final : public SkRefCnt {
|
||||||
public:
|
public:
|
||||||
SG_ATTRIBUTE(Name , SkString , fName ) // TODO: non-invalidating attributes?
|
ACTOR_ATTR(Name , SkString , SkString() )
|
||||||
SG_ATTRIBUTE(Color , SkColor4f, fColor )
|
ACTOR_ATTR(Color , SkColor4f, SkColors::kBlack)
|
||||||
SG_ATTRIBUTE(Size , SkV2 , fSize )
|
ACTOR_ATTR(Size , SkV2 , SkV2({0,0}) )
|
||||||
SG_ATTRIBUTE(Origin , SkV2 , fOrigin )
|
ACTOR_ATTR(Origin , SkV2 , SkV2({0,0}) )
|
||||||
SG_ATTRIBUTE(Translation , SkV2 , fTranslation )
|
ACTOR_ATTR(Translation , SkV2 , SkV2({0,0}) )
|
||||||
SG_ATTRIBUTE(ClipContents, bool , fClipContents)
|
ACTOR_ATTR(ClipContents, bool , false )
|
||||||
|
|
||||||
|
void render(SkCanvas*) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SkRect onRevalidate(sksg::InvalidationController*, const SkMatrix&) override;
|
void invalidate() {}
|
||||||
void onRender(SkCanvas*, const RenderContext*) const override;
|
|
||||||
const RenderNode* onNodeAt(const SkPoint&) const override;
|
|
||||||
|
|
||||||
SkString fName;
|
|
||||||
SkColor4f fColor = {0, 0, 0, 1};
|
|
||||||
SkV2 fSize = {0, 0},
|
|
||||||
fOrigin = {0, 0},
|
|
||||||
fTranslation = {0, 0};
|
|
||||||
bool fClipContents = false;
|
|
||||||
|
|
||||||
using INHERITED = RenderNode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SK_API SkRive final : public SkNVRefCnt<SkRive> {
|
class SK_API SkRive final : public SkNVRefCnt<SkRive> {
|
||||||
|
@ -11,6 +11,7 @@ skia_skrive_public = [ "$_include/SkRive.h" ]
|
|||||||
|
|
||||||
skia_skrive_sources = [
|
skia_skrive_sources = [
|
||||||
"$_src/Artboard.cpp",
|
"$_src/Artboard.cpp",
|
||||||
|
"$_src/Component.cpp",
|
||||||
"$_src/Drawable.cpp",
|
"$_src/Drawable.cpp",
|
||||||
"$_src/Node.cpp",
|
"$_src/Node.cpp",
|
||||||
"$_src/Shape.cpp",
|
"$_src/Shape.cpp",
|
||||||
|
@ -28,7 +28,7 @@ std::tuple<sk_sp<Node>, size_t> make_from_stream(StreamReader* sr) {
|
|||||||
return std::make_tuple(std::move(node), parent_index);
|
return std::make_tuple(std::move(node), parent_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<sk_sp<Node>, size_t> parse_component(StreamReader* sr) {
|
std::tuple<sk_sp<Component>, size_t> parse_component(StreamReader* sr) {
|
||||||
StreamReader::AutoBlock block(sr);
|
StreamReader::AutoBlock block(sr);
|
||||||
switch (block.type()) {
|
switch (block.type()) {
|
||||||
case StreamReader::BlockType::kActorNode : return make_from_stream<Node >(sr);
|
case StreamReader::BlockType::kActorNode : return make_from_stream<Node >(sr);
|
||||||
@ -44,24 +44,31 @@ std::tuple<sk_sp<Node>, size_t> parse_component(StreamReader* sr) {
|
|||||||
sk_sp<Node> parse_components(StreamReader* sr) {
|
sk_sp<Node> parse_components(StreamReader* sr) {
|
||||||
const auto count = sr->readLength16();
|
const auto count = sr->readLength16();
|
||||||
|
|
||||||
std::vector<sk_sp<Node>> nodes;
|
std::vector<sk_sp<Component>> components;
|
||||||
nodes.reserve(count);
|
components.reserve(count);
|
||||||
|
|
||||||
for (size_t i = 0; i < count; ++i) {
|
for (size_t i = 0; i < count; ++i) {
|
||||||
auto [ node, parent_index ] = parse_component(sr);
|
auto [ component, parent_index ] = parse_component(sr);
|
||||||
|
|
||||||
if (node && parent_index < i && nodes[parent_index]) {
|
if (component && parent_index < i && components[parent_index]) {
|
||||||
nodes[parent_index]->addChild(node);
|
if (Node* node = *components[parent_index]) {
|
||||||
|
node->addChild(component);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes.push_back(std::move(node));
|
components.push_back(std::move(component));
|
||||||
}
|
}
|
||||||
|
|
||||||
SkDebugf(".. parsed %zu nodes\n", nodes.size());
|
SkDebugf(".. parsed %zu components\n", components.size());
|
||||||
|
|
||||||
return count > 0
|
// hmm...
|
||||||
? std::move(nodes[0])
|
for (const auto& comp : components) {
|
||||||
: nullptr;
|
if (comp && comp->is<Node>()) {
|
||||||
|
return sk_ref_sp(static_cast<Node*>(*comp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
sk_sp<Artboard> parse_artboard(StreamReader* sr) {
|
sk_sp<Artboard> parse_artboard(StreamReader* sr) {
|
||||||
@ -99,19 +106,12 @@ sk_sp<Artboard> parse_artboard(StreamReader* sr) {
|
|||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
SkRect Artboard::onRevalidate(sksg::InvalidationController*, const SkMatrix&) {
|
void Artboard::render(SkCanvas* canvas) const {
|
||||||
return SkRect::MakeXYWH(fTranslation.x, fTranslation.y, fSize.x, fSize.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
const sksg::RenderNode* Artboard::onNodeAt(const SkPoint&) const {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Artboard::onRender(SkCanvas* canvas, const RenderContext*) const {
|
|
||||||
SkPaint paint;
|
SkPaint paint;
|
||||||
paint.setColor4f(fColor);
|
paint.setColor4f(fColor);
|
||||||
|
|
||||||
canvas->drawRect(this->bounds(), paint);
|
const auto rect = SkRect::MakeXYWH(fTranslation.x, fTranslation.y, fSize.x, fSize.y);
|
||||||
|
canvas->drawRect(rect, paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace skrive
|
} // namespace skrive
|
||||||
|
47
experimental/skrive/src/Component.cpp
Normal file
47
experimental/skrive/src/Component.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "experimental/skrive/include/SkRive.h"
|
||||||
|
|
||||||
|
#include "experimental/skrive/src/reader/StreamReader.h"
|
||||||
|
|
||||||
|
namespace skrive {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
size_t parse_node(StreamReader*, T*);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
size_t parse_node<Component>(StreamReader* sr, Component* node) {
|
||||||
|
node->setName(sr->readString("name"));
|
||||||
|
|
||||||
|
const auto parent_index = sr->readUInt16("parent");
|
||||||
|
|
||||||
|
SkDebugf(".. %s -> %d\n", node->getName().c_str(), parent_index);
|
||||||
|
|
||||||
|
return parent_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
void Component::invalidate() {
|
||||||
|
auto* node = this;
|
||||||
|
|
||||||
|
do {
|
||||||
|
node->fDirty = true;
|
||||||
|
node = node->fParent;
|
||||||
|
} while (node && !node->hasInval());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Component::revalidate() {
|
||||||
|
if (this->hasInval()) {
|
||||||
|
this->onRevalidate();
|
||||||
|
fDirty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace skrive
|
@ -19,8 +19,7 @@ size_t parse_node(StreamReader*, T*);
|
|||||||
|
|
||||||
template <>
|
template <>
|
||||||
size_t parse_node<Node>(StreamReader* sr, Node* node) {
|
size_t parse_node<Node>(StreamReader* sr, Node* node) {
|
||||||
node->setName(sr->readString("name"));
|
const auto parent_index = parse_node<Component>(sr, node);
|
||||||
const auto parent = sr->readUInt16("parent");
|
|
||||||
|
|
||||||
node->setTranslation(sr->readV2("translation"));
|
node->setTranslation(sr->readV2("translation"));
|
||||||
node->setRotation(sr->readFloat("rotation"));
|
node->setRotation(sr->readFloat("rotation"));
|
||||||
@ -28,8 +27,6 @@ size_t parse_node<Node>(StreamReader* sr, Node* node) {
|
|||||||
node->setOpacity(sr->readFloat("opacity"));
|
node->setOpacity(sr->readFloat("opacity"));
|
||||||
node->setCollapsedVisibility(sr->readBool("isCollapsed"));
|
node->setCollapsedVisibility(sr->readBool("isCollapsed"));
|
||||||
|
|
||||||
SkDebugf(".. '%s' -> %d\n", node->getName().c_str(), parent);
|
|
||||||
|
|
||||||
if (sr->openArray("clips")) {
|
if (sr->openArray("clips")) {
|
||||||
const auto count = sr->readLength8();
|
const auto count = sr->readLength8();
|
||||||
|
|
||||||
@ -48,13 +45,25 @@ size_t parse_node<Node>(StreamReader* sr, Node* node) {
|
|||||||
sr->closeArray();
|
sr->closeArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent;
|
return parent_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace skrive
|
} // namespace skrive
|
||||||
|
|
||||||
SkRect Node::onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) {
|
void Node::addChild(sk_sp<Component> child) {
|
||||||
return this->INHERITED::onRevalidate(ic, ctm);
|
child->fParent = this;
|
||||||
|
fChildren.push_back(std::move(child));
|
||||||
|
this->invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Node::onRevalidate() {
|
||||||
|
SkASSERT(this->hasInval());
|
||||||
|
|
||||||
|
for (const auto& child : fChildren) {
|
||||||
|
if (child) {
|
||||||
|
child->revalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
@ -9,7 +9,9 @@
|
|||||||
|
|
||||||
#include "experimental/skrive/src/reader/StreamReader.h"
|
#include "experimental/skrive/src/reader/StreamReader.h"
|
||||||
|
|
||||||
namespace skrive::internal {
|
namespace skrive {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
size_t parse_node(StreamReader*, T*);
|
size_t parse_node(StreamReader*, T*);
|
||||||
|
|
||||||
@ -22,4 +24,8 @@ size_t parse_node<Shape>(StreamReader* sr, Shape* node) {
|
|||||||
return parent_index;
|
return parent_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace skrive::internal
|
} // namespace internal
|
||||||
|
|
||||||
|
void Shape::onRevalidate() {}
|
||||||
|
|
||||||
|
} // namespace skrive
|
||||||
|
35
experimental/skrive/tests/DomTypes.cpp
Normal file
35
experimental/skrive/tests/DomTypes.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "experimental/skrive/include/SkRive.h"
|
||||||
|
#include "tests/Test.h"
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
void check_type(skiatest::Reporter* reporter, const sk_sp<U>& node, bool expected) {
|
||||||
|
REPORTER_ASSERT(reporter, node->template is<T>() == expected);
|
||||||
|
REPORTER_ASSERT(reporter, !!static_cast<const T*>(*node.get()) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
DEF_TEST(SkRive_DomTypes, reporter) {
|
||||||
|
{
|
||||||
|
auto node = sk_make_sp<skrive::Node>();
|
||||||
|
|
||||||
|
check_type<skrive::Component>(reporter, node, true);
|
||||||
|
check_type<skrive::Node >(reporter, node, true);
|
||||||
|
check_type<skrive::Drawable >(reporter, node, false);
|
||||||
|
check_type<skrive::Shape >(reporter, node, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto node = sk_make_sp<skrive::Shape>();
|
||||||
|
|
||||||
|
check_type<skrive::Component>(reporter, node, true);
|
||||||
|
check_type<skrive::Node >(reporter, node, true);
|
||||||
|
check_type<skrive::Drawable >(reporter, node, true);
|
||||||
|
check_type<skrive::Shape >(reporter, node, true);
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,6 @@ void SkRiveSlide::draw(SkCanvas* canvas) {
|
|||||||
SkMatrix::kCenter_ScaleToFit ));
|
SkMatrix::kCenter_ScaleToFit ));
|
||||||
|
|
||||||
for (const auto& ab : fRive->artboards()) {
|
for (const auto& ab : fRive->artboards()) {
|
||||||
ab->revalidate(nullptr, SkMatrix::I());
|
|
||||||
ab->render(canvas);
|
ab->render(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user