Skip to content

Commit 8ac81b7

Browse files
fmalitaSkia Commit-Bot
authored andcommitted
[skottie] Add a custom property manager util class
To facilitate demo code consolidation, introduce a custom property manager which filters for node names starting with '$' and treats all properties sharing the same name unitarily. Update the Colorize GM to use this new helper. Also revisit the PropertyObserver interface: * aliases for client-facing value types * introduce a new (decomposed) TransformPropertyValue, to replace component-wise setters * consolidate the PropertyHandle interface to only expose get()/set() Bug: skia: Change-Id: I9aa9ee80c1fb57bbfbacab0fc3f017da909b24d9 Reviewed-on: https://skia-review.googlesource.com/c/173220 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Florin Malita <fmalita@chromium.org>
1 parent 0aed7ea commit 8ac81b7

File tree

7 files changed

+342
-175
lines changed

7 files changed

+342
-175
lines changed

modules/skottie/gm/SkottieGM.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class SkottieWebFontGM : public skiagm::GM {
9090

9191
DEF_GM(return new SkottieWebFontGM;)
9292

93+
using namespace skottie_utils;
94+
9395
class SkottieColorizeGM : public skiagm::GM {
9496
protected:
9597
SkString onShortName() override {
@@ -102,10 +104,12 @@ class SkottieColorizeGM : public skiagm::GM {
102104

103105
void onOnceBeforeDraw() override {
104106
if (auto stream = GetResourceAsStream("skottie/skottie_sample_search.json")) {
105-
fColorizer = sk_make_sp<Colorizer>();
106-
fAnimation = Animation::Builder()
107-
.setPropertyObserver(fColorizer)
108-
.make(stream.get());
107+
auto propBuilder = sk_make_sp<CustomPropertyManagerBuilder>();
108+
fAnimation = Animation::Builder()
109+
.setPropertyObserver(propBuilder)
110+
.make(stream.get());
111+
fPropManager = propBuilder->build();
112+
fColors = fPropManager->getColorProps();
109113
}
110114
}
111115

@@ -139,36 +143,22 @@ class SkottieColorizeGM : public skiagm::GM {
139143

140144
if (uni == 'c') {
141145
fColorIndex = (fColorIndex + 1) % SK_ARRAY_COUNT(kColors);
142-
fColorizer->colorize(kColors[fColorIndex]);
146+
for (const auto& prop : fColors) {
147+
fPropManager->setColor(prop, kColors[fColorIndex]);
148+
}
143149
return true;
144150
}
145151

146152
return false;
147153
}
148154

149155
private:
150-
class Colorizer final : public PropertyObserver {
151-
public:
152-
void onColorProperty(const char node_name[],
153-
const PropertyObserver::LazyHandle<ColorPropertyHandle>& lh) override {
154-
fColorHandles.push_back(lh());
155-
}
156-
157-
void colorize(SkColor c) {
158-
for (const auto& handle : fColorHandles) {
159-
handle->setColor(c);
160-
}
161-
}
162-
163-
private:
164-
std::vector<std::unique_ptr<skottie::ColorPropertyHandle>> fColorHandles;
165-
};
166-
167156
static constexpr SkScalar kSize = 800;
168157

169-
sk_sp<Animation> fAnimation;
170-
sk_sp<Colorizer> fColorizer;
171-
size_t fColorIndex = 0;
158+
sk_sp<Animation> fAnimation;
159+
std::unique_ptr<CustomPropertyManager> fPropManager;
160+
std::vector<CustomPropertyManager::PropKey> fColors;
161+
size_t fColorIndex = 0;
172162

173163
using INHERITED = skiagm::GM;
174164
};

modules/skottie/include/SkottieProperty.h

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,48 @@ class OpacityEffect;
2525

2626
namespace skottie {
2727

28-
class ColorPropertyHandle;
29-
class OpacityPropertyHandle;
30-
class TransformPropertyHandle;
28+
using ColorPropertyValue = SkColor;
29+
using OpacityPropertyValue = float;
30+
31+
struct TransformPropertyValue {
32+
SkPoint fAnchorPoint,
33+
fPosition;
34+
SkVector fScale;
35+
SkScalar fRotation,
36+
fSkew,
37+
fSkewAxis;
38+
39+
bool operator==(const TransformPropertyValue& other) const;
40+
bool operator!=(const TransformPropertyValue& other) const;
41+
};
42+
43+
namespace internal { class AnimationBuilder; }
44+
45+
/**
46+
* Property handles are adapters between user-facing AE model/values
47+
* and the internal scene-graph representation.
48+
*/
49+
template <typename ValueT, typename NodeT>
50+
class SK_API PropertyHandle final {
51+
public:
52+
~PropertyHandle();
53+
54+
ValueT get() const;
55+
void set(const ValueT&);
56+
57+
private:
58+
explicit PropertyHandle(sk_sp<NodeT> node) : fNode(std::move(node)) {}
59+
60+
friend class skottie::internal::AnimationBuilder;
61+
62+
const sk_sp<NodeT> fNode;
63+
};
64+
65+
class TransformAdapter;
66+
67+
using ColorPropertyHandle = PropertyHandle<ColorPropertyValue , sksg::Color >;
68+
using OpacityPropertyHandle = PropertyHandle<OpacityPropertyValue , sksg::OpacityEffect >;
69+
using TransformPropertyHandle = PropertyHandle<TransformPropertyValue, TransformAdapter >;
3170

3271
/**
3372
* A PropertyObserver can be used to track and manipulate certain properties of "interesting"
@@ -37,7 +76,7 @@ class TransformPropertyHandle;
3776
* various properties of layer and shape nodes. The |node_name| argument corresponds to the
3877
* name ("nm") node property.
3978
*/
40-
class PropertyObserver : public SkRefCnt {
79+
class SK_API PropertyObserver : public SkRefCnt {
4180
public:
4281
template <typename T>
4382
using LazyHandle = std::function<std::unique_ptr<T>()>;
@@ -50,72 +89,6 @@ class PropertyObserver : public SkRefCnt {
5089
const LazyHandle<TransformPropertyHandle>&);
5190
};
5291

53-
namespace internal { class AnimationBuilder; }
54-
55-
class ColorPropertyHandle final {
56-
public:
57-
~ColorPropertyHandle();
58-
59-
SkColor getColor() const;
60-
void setColor(SkColor);
61-
62-
private:
63-
explicit ColorPropertyHandle(sk_sp<sksg::Color>);
64-
65-
friend class skottie::internal::AnimationBuilder;
66-
67-
const sk_sp<sksg::Color> fColor;
68-
};
69-
70-
class OpacityPropertyHandle final {
71-
public:
72-
~OpacityPropertyHandle();
73-
74-
float getOpacity() const;
75-
void setOpacity(float);
76-
77-
private:
78-
explicit OpacityPropertyHandle(sk_sp<sksg::OpacityEffect>);
79-
80-
friend class skottie::internal::AnimationBuilder;
81-
82-
const sk_sp<sksg::OpacityEffect> fOpacity;
83-
};
84-
85-
class TransformAdapter;
86-
87-
class TransformPropertyHandle final {
88-
public:
89-
~TransformPropertyHandle();
90-
91-
SkPoint getAnchorPoint() const;
92-
void setAnchorPoint(const SkPoint&);
93-
94-
SkPoint getPosition() const;
95-
void setPosition(const SkPoint&);
96-
97-
SkVector getScale() const;
98-
void setScale(const SkVector&);
99-
100-
SkScalar getRotation() const;
101-
void setRotation(SkScalar);
102-
103-
SkScalar getSkew() const;
104-
void setSkew(SkScalar);
105-
106-
SkScalar getSkewAxis() const;
107-
void setSkewAxis(SkScalar);
108-
109-
SkMatrix getTotalMatrix() const;
110-
111-
private:
112-
explicit TransformPropertyHandle(sk_sp<TransformAdapter>);
113-
114-
friend class skottie::internal::AnimationBuilder;
115-
116-
const sk_sp<TransformAdapter> fTransform;
117-
};
118-
11992
} // namespace skottie
12093

12194
#endif // SkottieProperty_DEFINED

modules/skottie/src/SkottieProperty.cpp

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,93 +13,68 @@
1313

1414
namespace skottie {
1515

16-
ColorPropertyHandle::ColorPropertyHandle(sk_sp<sksg::Color> color)
17-
: fColor(std::move(color)) {
18-
SkASSERT(fColor);
16+
bool TransformPropertyValue::operator==(const TransformPropertyValue& other) const {
17+
return this->fAnchorPoint == other.fAnchorPoint
18+
&& this->fPosition == other.fPosition
19+
&& this->fScale == other.fScale
20+
&& this->fSkew == other.fSkew
21+
&& this->fSkewAxis == other.fSkewAxis;
1922
}
2023

21-
ColorPropertyHandle::~ColorPropertyHandle() = default;
22-
23-
SkColor ColorPropertyHandle::getColor() const {
24-
return fColor->getColor();
25-
}
26-
27-
void ColorPropertyHandle::setColor(SkColor color) {
28-
fColor->setColor(color);
29-
}
30-
31-
OpacityPropertyHandle::OpacityPropertyHandle(sk_sp<sksg::OpacityEffect> opacity)
32-
: fOpacity(std::move(opacity)) {
33-
SkASSERT(fOpacity);
34-
}
35-
36-
OpacityPropertyHandle::~OpacityPropertyHandle() = default;
37-
38-
float OpacityPropertyHandle::getOpacity() const {
39-
return fOpacity->getOpacity() * 100;
40-
}
41-
42-
void OpacityPropertyHandle::setOpacity(float opacity) {
43-
fOpacity->setOpacity(opacity / 100);
44-
}
45-
46-
TransformPropertyHandle::TransformPropertyHandle(sk_sp<TransformAdapter> transform)
47-
: fTransform(std::move(transform)) {
48-
SkASSERT(fTransform);
24+
bool TransformPropertyValue::operator!=(const TransformPropertyValue& other) const {
25+
return !(*this == other);
4926
}
5027

51-
TransformPropertyHandle::~TransformPropertyHandle() = default;
28+
template <>
29+
PropertyHandle<ColorPropertyValue, sksg::Color>::~PropertyHandle() {}
5230

53-
SkPoint TransformPropertyHandle::getAnchorPoint() const {
54-
return fTransform->getAnchorPoint();
31+
template <>
32+
ColorPropertyValue PropertyHandle<ColorPropertyValue, sksg::Color>::get() const {
33+
return fNode->getColor();
5534
}
5635

57-
void TransformPropertyHandle::setAnchorPoint(const SkPoint& ap) {
58-
fTransform->setAnchorPoint(ap);
36+
template <>
37+
void PropertyHandle<ColorPropertyValue, sksg::Color>::set(const ColorPropertyValue& c) {
38+
fNode->setColor(c);
5939
}
6040

61-
SkPoint TransformPropertyHandle::getPosition() const {
62-
return fTransform->getPosition();
63-
}
64-
65-
void TransformPropertyHandle::setPosition(const SkPoint& position) {
66-
fTransform->setPosition(position);
67-
}
68-
69-
SkVector TransformPropertyHandle::getScale() const {
70-
return fTransform->getScale();
71-
}
72-
73-
void TransformPropertyHandle::setScale(const SkVector& scale) {
74-
fTransform->setScale(scale);
75-
}
76-
77-
SkScalar TransformPropertyHandle::getRotation() const {
78-
return fTransform->getRotation();
79-
}
41+
template <>
42+
PropertyHandle<OpacityPropertyValue, sksg::OpacityEffect>::~PropertyHandle() {}
8043

81-
void TransformPropertyHandle::setRotation(SkScalar rotation) {
82-
fTransform->setRotation(rotation);
44+
template <>
45+
OpacityPropertyValue PropertyHandle<OpacityPropertyValue, sksg::OpacityEffect>::get() const {
46+
return fNode->getOpacity() * 100;
8347
}
8448

85-
SkScalar TransformPropertyHandle::getSkew() const {
86-
return fTransform->getSkew();
49+
template <>
50+
void PropertyHandle<OpacityPropertyValue, sksg::OpacityEffect>::set(const OpacityPropertyValue& o) {
51+
fNode->setOpacity(o / 100);
8752
}
8853

89-
void TransformPropertyHandle::setSkew(SkScalar skew) {
90-
fTransform->setSkew(skew);
91-
}
92-
93-
SkScalar TransformPropertyHandle::getSkewAxis() const {
94-
return fTransform->getSkewAxis();
95-
}
54+
template <>
55+
PropertyHandle<TransformPropertyValue, TransformAdapter>::~PropertyHandle() {}
9656

97-
void TransformPropertyHandle::setSkewAxis(SkScalar sa) {
98-
fTransform->setSkewAxis(sa);
57+
template <>
58+
TransformPropertyValue PropertyHandle<TransformPropertyValue, TransformAdapter>::get() const {
59+
return {
60+
fNode->getAnchorPoint(),
61+
fNode->getPosition(),
62+
fNode->getScale(),
63+
fNode->getRotation(),
64+
fNode->getSkew(),
65+
fNode->getSkewAxis()
66+
};
9967
}
10068

101-
SkMatrix TransformPropertyHandle::getTotalMatrix() const {
102-
return fTransform->totalMatrix();
69+
template <>
70+
void PropertyHandle<TransformPropertyValue, TransformAdapter>::set(
71+
const TransformPropertyValue& t) {
72+
fNode->setAnchorPoint(t.fAnchorPoint);
73+
fNode->setPosition(t.fPosition);
74+
fNode->setScale(t.fScale);
75+
fNode->setRotation(t.fRotation);
76+
fNode->setSkew(t.fSkew);
77+
fNode->setSkewAxis(t.fSkewAxis);
10378
}
10479

10580
void PropertyObserver::onColorProperty(const char[],

modules/skottie/src/SkottieTest.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ DEF_TEST(Skottie_Properties, reporter) {
8383
};
8484

8585
struct TransformInfo {
86-
SkString node_name;
87-
SkMatrix matrix;
86+
SkString node_name;
87+
skottie::TransformPropertyValue transform;
8888
};
8989

9090
void onColorProperty(const char node_name[],
9191
const PropertyObserver::LazyHandle<ColorPropertyHandle>& lh) override {
92-
fColors.push_back({SkString(node_name), lh()->getColor()});
92+
fColors.push_back({SkString(node_name), lh()->get()});
9393
}
9494

9595
void onOpacityProperty(const char node_name[],
9696
const PropertyObserver::LazyHandle<OpacityPropertyHandle>& lh) override {
97-
fOpacities.push_back({SkString(node_name), lh()->getOpacity()});
97+
fOpacities.push_back({SkString(node_name), lh()->get()});
9898
}
9999

100100
void onTransformProperty(const char node_name[],
101101
const PropertyObserver::LazyHandle<TransformPropertyHandle>& lh) override {
102-
fTransforms.push_back({SkString(node_name), lh()->getTotalMatrix()});
102+
fTransforms.push_back({SkString(node_name), lh()->get()});
103103
}
104104

105105
const std::vector<ColorInfo>& colors() const { return fColors; }
@@ -136,9 +136,23 @@ DEF_TEST(Skottie_Properties, reporter) {
136136
const auto& transforms = observer->transforms();
137137
REPORTER_ASSERT(reporter, transforms.size() == 2);
138138
REPORTER_ASSERT(reporter, transforms[0].node_name.equals("shape_transform_0"));
139-
REPORTER_ASSERT(reporter, transforms[0].matrix == SkMatrix::MakeScale(0.5, 0.5));
139+
REPORTER_ASSERT(reporter, transforms[0].transform == skottie::TransformPropertyValue({
140+
SkPoint::Make(0, 0),
141+
SkPoint::Make(0, 0),
142+
SkVector::Make(50, 50),
143+
0,
144+
0,
145+
0
146+
}));
140147
REPORTER_ASSERT(reporter, transforms[1].node_name.equals("layer_0"));
141-
REPORTER_ASSERT(reporter, transforms[1].matrix == SkMatrix::I());
148+
REPORTER_ASSERT(reporter, transforms[1].transform == skottie::TransformPropertyValue({
149+
SkPoint::Make(0, 0),
150+
SkPoint::Make(0, 0),
151+
SkVector::Make(100, 100),
152+
0,
153+
0,
154+
0
155+
}));
142156
}
143157

144158
DEF_TEST(Skottie_Annotations, reporter) {

0 commit comments

Comments
 (0)