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 "modules/skottie/src/Adapter.h"
9#include "modules/skottie/src/SkottieJson.h"
10#include "modules/skottie/src/SkottiePriv.h"
11#include "modules/skottie/src/SkottieValue.h"
12#include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
13#include "modules/sksg/include/SkSGGroup.h"
14#include "modules/sksg/include/SkSGTransform.h"
15
16#include <vector>
17
18namespace skottie {
19namespace internal {
20
21namespace {
22
23class RepeaterAdapter final : public DiscardableAdapterBase<RepeaterAdapter, sksg::Group> {
24public:
25 RepeaterAdapter(const skjson::ObjectValue& jrepeater,
26 const skjson::ObjectValue& jtransform,
27 const AnimationBuilder& abuilder,
28 sk_sp<sksg::RenderNode> repeater_node)
29 : fRepeaterNode(std::move(repeater_node))
30 , fComposite((ParseDefault(jrepeater["m"], 1) == 1) ? Composite::kAbove
31 : Composite::kBelow) {
32 this->bind(abuilder, jrepeater["c"], fCount);
33 this->bind(abuilder, jrepeater["o"], fOffset);
34
35 this->bind(abuilder, jtransform["a" ], fAnchorPoint);
36 this->bind(abuilder, jtransform["p" ], fPosition);
37 this->bind(abuilder, jtransform["s" ], fScale);
38 this->bind(abuilder, jtransform["r" ], fRotation);
39 this->bind(abuilder, jtransform["so"], fStartOpacity);
40 this->bind(abuilder, jtransform["eo"], fEndOpacity);
41 }
42
43private:
44 void onSync() override {
45 static constexpr SkScalar kMaxCount = 512;
46 const auto count = static_cast<size_t>(SkTPin(fCount, 0.0f, kMaxCount) + 0.5f);
47
48 const auto& compute_transform = [&] (size_t index) {
49 const auto t = fOffset + index;
50
51 // Position, scale & rotation are "scaled" by index/offset.
52 SkMatrix m = SkMatrix::MakeTrans(-fAnchorPoint.x,
53 -fAnchorPoint.y);
54 m.postScale(std::pow(fScale.x * .01f, fOffset),
55 std::pow(fScale.y * .01f, fOffset));
56 m.postRotate(t * fRotation);
57 m.postTranslate(t * fPosition.x + fAnchorPoint.x,
58 t * fPosition.y + fAnchorPoint.y);
59
60 return m;
61 };
62
63 // TODO: start/end opacity support.
64
65 // TODO: we can avoid rebuilding all the fragments in most cases.
66 this->node()->clear();
67 for (size_t i = 0; i < count; ++i) {
68 const auto insert_index = (fComposite == Composite::kAbove) ? i : count - i - 1;
69 this->node()->addChild(sksg::TransformEffect::Make(fRepeaterNode,
70 compute_transform(insert_index)));
71 }
72 }
73
74 enum class Composite { kAbove, kBelow };
75
76 const sk_sp<sksg::RenderNode> fRepeaterNode;
77 const Composite fComposite;
78
79 // Repeater props
80 ScalarValue fCount = 0,
81 fOffset = 0;
82
83 // Transform props
84 Vec2Value fAnchorPoint = { 0, 0 },
85 fPosition = { 0, 0 },
86 fScale = { 100, 100 };
87 ScalarValue fRotation = 0,
88 fStartOpacity = 100,
89 fEndOpacity = 100;
90};
91
92} // namespace
93
94std::vector<sk_sp<sksg::RenderNode>> ShapeBuilder::AttachRepeaterDrawEffect(
95 const skjson::ObjectValue& jrepeater,
96 const AnimationBuilder* abuilder,
97 std::vector<sk_sp<sksg::RenderNode>>&& draws) {
98 std::vector<sk_sp<sksg::RenderNode>> repeater_draws;
99
100 if (const skjson::ObjectValue* jtransform = jrepeater["tr"]) {
101 // We can skip the group if only one draw.
102 auto repeater_node = (draws.size() > 1) ? sksg::Group::Make(std::move(draws))
103 : std::move(draws[0]);
104
105 auto repeater_root =
106 abuilder->attachDiscardableAdapter<RepeaterAdapter>(jrepeater,
107 *jtransform,
108 *abuilder,
109 std::move(repeater_node));
110 repeater_draws.reserve(1);
111 repeater_draws.push_back(std::move(repeater_root));
112 } else {
113 repeater_draws = std::move(draws);
114 }
115
116 return repeater_draws;
117}
118
119} // namespace internal
120} // namespace skottie
121