1 /*
2 * Copyright 2020 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 #include "include/private/SkTPin.h"
9 #include "modules/skottie/src/Adapter.h"
10 #include "modules/skottie/src/SkottieJson.h"
11 #include "modules/skottie/src/SkottiePriv.h"
12 #include "modules/skottie/src/SkottieValue.h"
13 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
14 #include "modules/sksg/include/SkSGRenderNode.h"
15
16 #include <vector>
17
18 namespace skottie {
19 namespace internal {
20
21 namespace {
22
23 class RepeaterRenderNode final : public sksg::CustomRenderNode {
24 public:
25 enum class CompositeMode { kBelow, kAbove };
26
RepeaterRenderNode(std::vector<sk_sp<RenderNode>> && children,CompositeMode mode)27 RepeaterRenderNode(std::vector<sk_sp<RenderNode>>&& children, CompositeMode mode)
28 : INHERITED(std::move(children))
29 , fMode(mode) {}
30
31 SG_ATTRIBUTE(Count , size_t, fCount )
32 SG_ATTRIBUTE(Offset , float , fOffset )
33 SG_ATTRIBUTE(AnchorPoint , SkV2 , fAnchorPoint )
34 SG_ATTRIBUTE(Position , SkV2 , fPosition )
35 SG_ATTRIBUTE(Scale , SkV2 , fScale )
36 SG_ATTRIBUTE(Rotation , float , fRotation )
37 SG_ATTRIBUTE(StartOpacity, float , fStartOpacity)
38 SG_ATTRIBUTE(EndOpacity , float , fEndOpacity )
39
40 private:
onNodeAt(const SkPoint &) const41 const RenderNode* onNodeAt(const SkPoint&) const override { return nullptr; } // no hit-testing
42
instanceTransform(size_t i) const43 SkMatrix instanceTransform(size_t i) const {
44 const auto t = fOffset + i;
45
46 // Position, scale & rotation are "scaled" by index/offset.
47 return SkMatrix::Translate(t * fPosition.x + fAnchorPoint.x,
48 t * fPosition.y + fAnchorPoint.y)
49 * SkMatrix::RotateDeg(t * fRotation)
50 * SkMatrix::Scale(std::pow(fScale.x, t),
51 std::pow(fScale.y, t))
52 * SkMatrix::Translate(-fAnchorPoint.x,
53 -fAnchorPoint.y);
54 }
55
onRevalidate(sksg::InvalidationController * ic,const SkMatrix & ctm)56 SkRect onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) override {
57 fChildrenBounds = SkRect::MakeEmpty();
58 for (const auto& child : this->children()) {
59 fChildrenBounds.join(child->revalidate(ic, ctm));
60 }
61
62 auto bounds = SkRect::MakeEmpty();
63 for (size_t i = 0; i < fCount; ++i) {
64 bounds.join(this->instanceTransform(i).mapRect(fChildrenBounds));
65 }
66
67 return bounds;
68 }
69
onRender(SkCanvas * canvas,const RenderContext * ctx) const70 void onRender(SkCanvas* canvas, const RenderContext* ctx) const override {
71 // To cover the full opacity range, the denominator below should be (fCount - 1).
72 // Interstingly, that's not what AE does. Off-by-one bug?
73 const auto dOpacity = fCount > 1 ? (fEndOpacity - fStartOpacity) / fCount : 0.0f;
74
75 for (size_t i = 0; i < fCount; ++i) {
76 const auto render_index = fMode == CompositeMode::kAbove ? i : fCount - i - 1;
77 const auto opacity = fStartOpacity + dOpacity * render_index;
78
79 if (opacity <= 0) {
80 continue;
81 }
82
83 SkAutoCanvasRestore acr(canvas, true);
84 canvas->concat(this->instanceTransform(render_index));
85
86 const auto& children = this->children();
87 const auto local_ctx = ScopedRenderContext(canvas, ctx)
88 .modulateOpacity(opacity)
89 .setIsolation(fChildrenBounds,
90 canvas->getTotalMatrix(),
91 children.size() > 1);
92 for (const auto& child : children) {
93 child->render(canvas, local_ctx);
94 }
95 }
96 }
97
98 const CompositeMode fMode;
99
100 SkRect fChildrenBounds = SkRect::MakeEmpty(); // cached
101
102 size_t fCount = 0;
103 float fOffset = 0,
104 fRotation = 0,
105 fStartOpacity = 1,
106 fEndOpacity = 1;
107 SkV2 fAnchorPoint = {0,0},
108 fPosition = {0,0},
109 fScale = {1,1};
110
111 using INHERITED = sksg::CustomRenderNode;
112 };
113
114 class RepeaterAdapter final : public DiscardableAdapterBase<RepeaterAdapter, RepeaterRenderNode> {
115 public:
RepeaterAdapter(const skjson::ObjectValue & jrepeater,const skjson::ObjectValue & jtransform,const AnimationBuilder & abuilder,std::vector<sk_sp<sksg::RenderNode>> && draws)116 RepeaterAdapter(const skjson::ObjectValue& jrepeater,
117 const skjson::ObjectValue& jtransform,
118 const AnimationBuilder& abuilder,
119 std::vector<sk_sp<sksg::RenderNode>>&& draws)
120 : INHERITED(sk_make_sp<RepeaterRenderNode>(std::move(draws),
121 (ParseDefault(jrepeater["m"], 1) == 1)
122 ? RepeaterRenderNode::CompositeMode::kBelow
123 : RepeaterRenderNode::CompositeMode::kAbove))
124 {
125 this->bind(abuilder, jrepeater["c"], fCount);
126 this->bind(abuilder, jrepeater["o"], fOffset);
127
128 this->bind(abuilder, jtransform["a" ], fAnchorPoint);
129 this->bind(abuilder, jtransform["p" ], fPosition);
130 this->bind(abuilder, jtransform["s" ], fScale);
131 this->bind(abuilder, jtransform["r" ], fRotation);
132 this->bind(abuilder, jtransform["so"], fStartOpacity);
133 this->bind(abuilder, jtransform["eo"], fEndOpacity);
134 }
135
136 private:
onSync()137 void onSync() override {
138 static constexpr SkScalar kMaxCount = 1024;
139 this->node()->setCount(static_cast<size_t>(SkTPin(fCount, 0.0f, kMaxCount) + 0.5f));
140 this->node()->setOffset(fOffset);
141 this->node()->setAnchorPoint(fAnchorPoint);
142 this->node()->setPosition(fPosition);
143 this->node()->setScale(fScale * 0.01f);
144 this->node()->setRotation(fRotation);
145 this->node()->setStartOpacity(SkTPin(fStartOpacity * 0.01f, 0.0f, 1.0f));
146 this->node()->setEndOpacity (SkTPin(fEndOpacity * 0.01f, 0.0f, 1.0f));
147 }
148
149 // Repeater props
150 ScalarValue fCount = 0,
151 fOffset = 0;
152
153 // Transform props
154 Vec2Value fAnchorPoint = { 0, 0 },
155 fPosition = { 0, 0 },
156 fScale = { 100, 100 };
157 ScalarValue fRotation = 0,
158 fStartOpacity = 100,
159 fEndOpacity = 100;
160
161 using INHERITED = DiscardableAdapterBase<RepeaterAdapter, RepeaterRenderNode>;
162 };
163
164 } // namespace
165
AttachRepeaterDrawEffect(const skjson::ObjectValue & jrepeater,const AnimationBuilder * abuilder,std::vector<sk_sp<sksg::RenderNode>> && draws)166 std::vector<sk_sp<sksg::RenderNode>> ShapeBuilder::AttachRepeaterDrawEffect(
167 const skjson::ObjectValue& jrepeater,
168 const AnimationBuilder* abuilder,
169 std::vector<sk_sp<sksg::RenderNode>>&& draws) {
170 std::vector<sk_sp<sksg::RenderNode>> repeater_draws;
171
172 if (const skjson::ObjectValue* jtransform = jrepeater["tr"]) {
173 // input draws are in top->bottom order - reverse for paint order
174 std::reverse(draws.begin(), draws.end());
175
176 repeater_draws.reserve(1);
177 repeater_draws.push_back(
178 abuilder->attachDiscardableAdapter<RepeaterAdapter>(jrepeater,
179 *jtransform,
180 *abuilder,
181 std::move(draws)));
182 } else {
183 repeater_draws = std::move(draws);
184 }
185
186 return repeater_draws;
187 }
188
189 } // namespace internal
190 } // namespace skottie
191