Flutter Engine
The Flutter Engine
PrecompLayer.cpp
Go to the documentation of this file.
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
12#include "include/core/SkSize.h"
25#include "src/base/SkTLazy.h"
26#include "src/utils/SkJSON.h"
27
28#include <cmath>
29#include <utility>
30
31class SkMatrix;
32
33namespace sksg {
34class InvalidationController;
35}
36
37namespace skottie {
38namespace internal {
39
40namespace {
41
42// "Animates" time based on the layer's "tm" property.
43class TimeRemapper final : public AnimatablePropertyContainer {
44public:
45 TimeRemapper(const skjson::ObjectValue& jtm, const AnimationBuilder* abuilder, float scale)
46 : fScale(scale) {
47 this->bind(*abuilder, jtm, fT);
48 }
49
50 float t() const { return fT * fScale; }
51
52private:
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.
63class CompTimeMapper final : public Animator {
64public:
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
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
91private:
92 const AnimatorScope fAnimators;
93 const sk_sp<TimeRemapper> fRemapper;
94 const float fTimeBias,
95 fTimeScale;
96};
97
98} // namespace
99
100sk_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
181sk_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
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr float sk_ieee_float_divide(float numer, float denom)
#define SG_ATTRIBUTE(attr_name, attr_type, attr_container)
Definition: SkSGNode.h:100
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
SkMatrix getTotalMatrix() const
Definition: SkCanvas.cpp:1629
T * init(Args &&... args)
Definition: SkTLazy.h:45
Optional< SkRect > bounds
Definition: SkRecords.h:189
std::vector< sk_sp< Animator > > AnimatorScope
Definition: SkottiePriv.h:55
SkScalar ScalarValue
Definition: SkottieValue.h:22
Definition: Skottie.h:32
constexpr bool contains(std::string_view str, std::string_view needle)
Definition: SkStringView.h:41
SINT bool isfinite(const Vec< N, T > &v)
Definition: SkVx.h:1003
Definition: ref_ptr.h:256
#define T
Definition: precompiler.cc:65
const Scalar scale
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static constexpr SkRect MakeSize(const SkSize &size)
Definition: SkRect.h:633
Definition: SkSize.h:52
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
const uintptr_t id