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