Debian Codegraph @

modules/skottie/src/layers/PrecompLayer.cpp

raw
1 /*
2 * Copyright 2018 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/core/SkCanvas.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSize.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkFloatingPoint.h"
16 #include "modules/jsonreader/SkJSONReader.h"
17 #include "modules/skottie/include/ExternalLayer.h"
18 #include "modules/skottie/include/SkottieProperty.h"
19 #include "modules/skottie/src/Composition.h"
20 #include "modules/skottie/src/SkottieJson.h"
21 #include "modules/skottie/src/SkottiePriv.h"
22 #include "modules/skottie/src/SkottieValue.h"
23 #include "modules/skottie/src/animator/Animator.h"
24 #include "modules/sksg/include/SkSGNode.h"
25 #include "modules/sksg/include/SkSGRenderNode.h"
26
27 #include <cmath>
28 #include <utility>
29
30 class SkMatrix;
31
32 namespace sksg {
33 class InvalidationController;
34 }
35
36 namespace skottie {
37 namespace internal {
38
39 namespace {
40
41 // "Animates" time based on the layer's "tm" property.
42 class TimeRemapper final : public AnimatablePropertyContainer {
43 public:
44 TimeRemapper(const skjson::ObjectValue& jtm, const AnimationBuilder* abuilder, float scale)
45 : fScale(scale) {
46 this->bind(*abuilder, jtm, fT);
47 }
48
49 float t() const { return fT * fScale; }
50
51 private:
52 void onSync() override {
53 // nothing to sync - we just track t
54 }
55
56 const float fScale;
57
58 ScalarValue fT = 0;
59 };
60
61 // Applies a bias/scale/remap t-adjustment to child animators.
62 class CompTimeMapper final : public Animator {
63 public:
64 CompTimeMapper(AnimatorScope&& layer_animators,
65 sk_sp<TimeRemapper> remapper,
66 float time_bias, float time_scale)
67 : fAnimators(std::move(layer_animators))
68 , fRemapper(std::move(remapper))
69 , fTimeBias(time_bias)
70 , fTimeScale(time_scale) {}
71
72 StateChanged onSeek(float t) override {
73 if (fRemapper) {
74 // When time remapping is active, |t| is fully driven externally.
75 fRemapper->seek(t);
76 t = fRemapper->t();
77 } else {
78 t = (t + fTimeBias) * fTimeScale;
79 }
80
81 bool changed = false;
82
83 for (const auto& anim : fAnimators) {
84 changed |= anim->seek(t);
85 }
86
87 return changed;
88 }
89
90 private:
91 const AnimatorScope fAnimators;
92 const sk_sp<TimeRemapper> fRemapper;
93 const float fTimeBias,
94 fTimeScale;
95 };
96
97 } // namespace
98
99 sk_sp<sksg::RenderNode> AnimationBuilder::attachExternalPrecompLayer(
100 const skjson::ObjectValue& jlayer,
101 const LayerInfo& layer_info) const {
102
103 if (!fPrecompInterceptor) {
104 return nullptr;
105 }
106
107 const skjson::StringValue* id = jlayer["refId"];
108 const skjson::StringValue* nm = jlayer["nm"];
109
110 if (!id || !nm) {
111 return nullptr;
112 }
113
114 auto external_layer = fPrecompInterceptor->onLoadPrecomp(id->begin(),
115 nm->begin(),
116 layer_info.fSize);
117 if (!external_layer) {
118 return nullptr;
119 }
120
121 // Attaches an ExternalLayer implementation to the animation scene graph.
122 class SGAdapter final : public sksg::RenderNode {
123 public:
124 SG_ATTRIBUTE(T, float, fCurrentT)
125
126 SGAdapter(sk_sp<ExternalLayer> external, const SkSize& layer_size)
127 : fExternal(std::move(external))
128 , fSize(layer_size) {}
129
130 private:
131 SkRect onRevalidate(sksg::InvalidationController*, const SkMatrix&) override {
132 return SkRect::MakeSize(fSize);
133 }
134
135 void onRender(SkCanvas* canvas, const RenderContext* ctx) const override {
136 // Commit all pending effects via a layer if needed,
137 // since we don't have knowledge of the external content.
138 const auto local_scope =
139 ScopedRenderContext(canvas, ctx).setIsolation(this->bounds(),
140 canvas->getTotalMatrix(),
141 true);
142 fExternal->render(canvas, static_cast<double>(fCurrentT));
143 }
144
145 const RenderNode* onNodeAt(const SkPoint& pt) const override {
146 SkASSERT(this->bounds().contains(pt.fX, pt.fY));
147 return this;
148 }
149
150 const sk_sp<ExternalLayer> fExternal;
151 const SkSize fSize;
152 float fCurrentT = 0;
153 };
154
155 // Connects an SGAdapter to the animator tree and dispatches seek events.
156 class AnimatorAdapter final : public Animator {
157 public:
158 AnimatorAdapter(sk_sp<SGAdapter> sg_adapter, float fps)
159 : fSGAdapter(std::move(sg_adapter))
160 , fFps(fps) {}
161
162 private:
163 StateChanged onSeek(float t) override {
164 fSGAdapter->setT(t / fFps);
165
166 return true;
167 }
168
169 const sk_sp<SGAdapter> fSGAdapter;
170 const float fFps;
171 };
172
173 auto sg_adapter = sk_make_sp<SGAdapter>(std::move(external_layer), layer_info.fSize);
174
175 fCurrentAnimatorScope->push_back(sk_make_sp<AnimatorAdapter>(sg_adapter, fFrameRate));
176
177 return sg_adapter;
178 }
179
180 sk_sp<sksg::RenderNode> AnimationBuilder::attachPrecompLayer(const skjson::ObjectValue& jlayer,
181 LayerInfo* layer_info) const {
182 sk_sp<TimeRemapper> time_remapper;
183 if (const skjson::ObjectValue* jtm = jlayer["tm"]) {
184 time_remapper = sk_make_sp<TimeRemapper>(*jtm, this, fFrameRate);
185 }
186
187 const auto start_time = ParseDefault<float>(jlayer["st"], 0.0f),
188 stretch_time = ParseDefault<float>(jlayer["sr"], 1.0f);
189 const auto requires_time_mapping = !SkScalarNearlyEqual(start_time , 0) ||
190 !SkScalarNearlyEqual(stretch_time, 1) ||
191 time_remapper;
192
193 // Precomp layers are sized explicitly.
194 auto parse_size = [](const skjson::ObjectValue& jlayer) {
195 return SkSize::Make(ParseDefault<float>(jlayer["w"], 0.0f),
196 ParseDefault<float>(jlayer["h"], 0.0f));
197 };
198 layer_info->fSize = parse_size(jlayer);
199
200 std::optional<AutoScope> local_scope;
201 if (requires_time_mapping) {
202 local_scope.emplace(this);
203 }
204
205 auto precomp_layer = this->attachExternalPrecompLayer(jlayer, *layer_info);
206
207 if (!precomp_layer) {
208 const ScopedAssetRef precomp_asset(this, jlayer);
209 if (precomp_asset) {
210 // Unlike regular precomp layers, glyph precomps don't have an explicit size - they
211 // use the actual asset comp size.
212 if (layer_info->fSize.isEmpty()) {
213 layer_info->fSize = parse_size(*precomp_asset);
214 }
215
216 AutoPropertyTracker apt(this, *precomp_asset, PropertyObserver::NodeType::COMPOSITION);
217 precomp_layer =
218 CompositionBuilder(*this, layer_info->fSize, *precomp_asset).build(*this);
219 }
220 }
221
222 if (requires_time_mapping) {
223 const auto t_bias = -start_time,
224 t_scale = sk_ieee_float_divide(1, stretch_time);
225 auto time_mapper = sk_make_sp<CompTimeMapper>(local_scope->release(),
226 std::move(time_remapper),
227 t_bias,
228 std::isfinite(t_scale) ? t_scale : 0);
229
230 fCurrentAnimatorScope->push_back(std::move(time_mapper));
231 }
232
233 return precomp_layer;
234 }
235
236 } // namespace internal
237 } // namespace skottie