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/animator/VectorKeyframeAnimator.h"
9
10 #include "include/core/SkTypes.h"
11 #include "include/private/SkNx.h"
12 #include "include/private/SkTPin.h"
13 #include "modules/skottie/src/SkottieJson.h"
14 #include "modules/skottie/src/SkottieValue.h"
15 #include "modules/skottie/src/animator/Animator.h"
16 #include "src/core/SkSafeMath.h"
17
18 #include <algorithm>
19 #include <cstring>
20
21 namespace skottie {
22
23 // Parses an array of exact size.
parse_array(const skjson::ArrayValue * ja,float * a,size_t count)24 static bool parse_array(const skjson::ArrayValue* ja, float* a, size_t count) {
25 if (!ja || ja->size() != count) {
26 return false;
27 }
28
29 for (size_t i = 0; i < count; ++i) {
30 if (!Parse((*ja)[i], a + i)) {
31 return false;
32 }
33 }
34
35 return true;
36 }
37
operator SkV3() const38 VectorValue::operator SkV3() const {
39 // best effort to turn this into a 3D point
40 return SkV3 {
41 this->size() > 0 ? (*this)[0] : 0,
42 this->size() > 1 ? (*this)[1] : 0,
43 this->size() > 2 ? (*this)[2] : 0,
44 };
45 }
46
operator SkColor() const47 VectorValue::operator SkColor() const {
48 return static_cast<SkColor4f>(*this).toSkColor();
49 }
50
operator SkColor4f() const51 VectorValue::operator SkColor4f() const {
52 // best effort to turn a vector into a color
53 const auto r = this->size() > 0 ? SkTPin((*this)[0], 0.0f, 1.0f) : 0,
54 g = this->size() > 1 ? SkTPin((*this)[1], 0.0f, 1.0f) : 0,
55 b = this->size() > 2 ? SkTPin((*this)[2], 0.0f, 1.0f) : 0,
56 a = this->size() > 3 ? SkTPin((*this)[3], 0.0f, 1.0f) : 1;
57
58 return { r, g, b, a };
59 }
60
61 namespace internal {
62 namespace {
63
64 // Vector specialization - stores float vector values (of same length) in consolidated/contiguous
65 // storage. Keyframe records hold the storage offset for each value:
66 //
67 // fStorage: [ vec0 ][ vec1 ] ... [ vecN ]
68 // <- vec_len -> <- vec_len -> <- vec_len ->
69 //
70 // ^ ^ ^
71 // fKFs[]: .idx .idx ... .idx
72 //
73 class VectorKeyframeAnimator final : public KeyframeAnimator {
74 public:
VectorKeyframeAnimator(std::vector<Keyframe> kfs,std::vector<SkCubicMap> cms,std::vector<float> storage,size_t vec_len,std::vector<float> * target_value)75 VectorKeyframeAnimator(std::vector<Keyframe> kfs,
76 std::vector<SkCubicMap> cms,
77 std::vector<float> storage,
78 size_t vec_len,
79 std::vector<float>* target_value)
80 : INHERITED(std::move(kfs), std::move(cms))
81 , fStorage(std::move(storage))
82 , fVecLen(vec_len)
83 , fTarget(target_value) {
84
85 // Resize the target value appropriately.
86 fTarget->resize(fVecLen);
87 }
88
89 private:
onSeek(float t)90 StateChanged onSeek(float t) override {
91 const auto& lerp_info = this->getLERPInfo(t);
92
93 SkASSERT(lerp_info.vrec0.idx + fVecLen <= fStorage.size());
94 SkASSERT(lerp_info.vrec1.idx + fVecLen <= fStorage.size());
95 SkASSERT(fTarget->size() == fVecLen);
96
97 const auto* v0 = fStorage.data() + lerp_info.vrec0.idx;
98 const auto* v1 = fStorage.data() + lerp_info.vrec1.idx;
99 auto* dst = fTarget->data();
100
101 const auto is_constant = lerp_info.vrec0.equals(lerp_info.vrec1,
102 Keyframe::Value::Type::kIndex);
103 if (is_constant) {
104 if (0 != std::memcmp(dst, v0, fVecLen * sizeof(float))) {
105 std::copy(v0, v0 + fVecLen, dst);
106 return true;
107 }
108 return false;
109 }
110
111 size_t count = fVecLen;
112 bool changed = false;
113
114 while (count >= 4) {
115 const auto old_val = Sk4f::Load(dst),
116 new_val = Lerp(Sk4f::Load(v0), Sk4f::Load(v1), lerp_info.weight);
117
118 changed |= (new_val != old_val).anyTrue();
119 new_val.store(dst);
120
121 v0 += 4;
122 v1 += 4;
123 dst += 4;
124 count -= 4;
125 }
126
127 while (count-- > 0) {
128 const auto new_val = Lerp(*v0++, *v1++, lerp_info.weight);
129
130 changed |= (new_val != *dst);
131 *dst++ = new_val;
132 }
133
134 return changed;
135 }
136
137 const std::vector<float> fStorage;
138 const size_t fVecLen;
139
140 std::vector<float>* fTarget;
141
142 using INHERITED = KeyframeAnimator;
143 };
144
145 class VectorExpressionAnimator final : public Animator {
146 public:
VectorExpressionAnimator(sk_sp<ExpressionEvaluator<std::vector<float>>> expression_evaluator,std::vector<float> * target_value)147 VectorExpressionAnimator(sk_sp<ExpressionEvaluator<std::vector<float>>> expression_evaluator,
148 std::vector<float>* target_value)
149 : fExpressionEvaluator(std::move(expression_evaluator))
150 , fTarget(target_value) {}
151
152 private:
153
onSeek(float t)154 StateChanged onSeek(float t) override {
155 std::vector<float> result = fExpressionEvaluator->evaluate(t);
156 bool changed = false;
157 for (size_t i = 0; i < fTarget->size(); i++) {
158 // Use 0 as a default if the result is too small.
159 float val = i >= result.size() ? 0 : result[i];
160 if (!SkScalarNearlyEqual(val, (*fTarget)[i])) {
161 changed = true;
162 }
163 (*fTarget)[i] = val;
164 }
165
166 return changed;
167 }
168
169 sk_sp<ExpressionEvaluator<std::vector<float>>> fExpressionEvaluator;
170 std::vector<float>* fTarget;
171 };
172 } // namespace
173
VectorAnimatorBuilder(std::vector<float> * target,VectorLenParser parse_len,VectorDataParser parse_data)174 VectorAnimatorBuilder::VectorAnimatorBuilder(std::vector<float>* target,
175 VectorLenParser parse_len,
176 VectorDataParser parse_data)
177 : INHERITED(Keyframe::Value::Type::kIndex)
178 , fParseLen(parse_len)
179 , fParseData(parse_data)
180 , fTarget(target) {}
181
makeFromKeyframes(const AnimationBuilder & abuilder,const skjson::ArrayValue & jkfs)182 sk_sp<KeyframeAnimator> VectorAnimatorBuilder::makeFromKeyframes(const AnimationBuilder& abuilder,
183 const skjson::ArrayValue& jkfs) {
184 SkASSERT(jkfs.size() > 0);
185
186 // peek at the first keyframe value to find our vector length
187 const skjson::ObjectValue* jkf0 = jkfs[0];
188 if (!jkf0 || !fParseLen((*jkf0)["s"], &fVecLen)) {
189 return nullptr;
190 }
191
192 SkSafeMath safe;
193 // total elements: vector length x number vectors
194 const auto total_size = safe.mul(fVecLen, jkfs.size());
195
196 // we must be able to store all offsets in Keyframe::Value::idx (uint32_t)
197 if (!safe || !SkTFitsIn<uint32_t>(total_size)) {
198 return nullptr;
199 }
200 fStorage.resize(total_size);
201
202 if (!this->parseKeyframes(abuilder, jkfs)) {
203 return nullptr;
204 }
205
206 // parseKFValue() might have stored fewer vectors thanks to tail-deduping.
207 SkASSERT(fCurrentVec <= jkfs.size());
208 fStorage.resize(fCurrentVec * fVecLen);
209 fStorage.shrink_to_fit();
210
211 return sk_sp<VectorKeyframeAnimator>(
212 new VectorKeyframeAnimator(std::move(fKFs),
213 std::move(fCMs),
214 std::move(fStorage),
215 fVecLen,
216 fTarget));
217 }
218
makeFromExpression(ExpressionManager & em,const char * expr)219 sk_sp<Animator> VectorAnimatorBuilder::makeFromExpression(ExpressionManager& em, const char* expr) {
220 sk_sp<ExpressionEvaluator<std::vector<SkScalar>>> expression_evaluator =
221 em.createArrayExpressionEvaluator(expr);
222 return sk_make_sp<VectorExpressionAnimator>(expression_evaluator, fTarget);
223 }
224
parseValue(const AnimationBuilder &,const skjson::Value & jv) const225 bool VectorAnimatorBuilder::parseValue(const AnimationBuilder&,
226 const skjson::Value& jv) const {
227 size_t vec_len;
228 if (!this->fParseLen(jv, &vec_len)) {
229 return false;
230 }
231
232 fTarget->resize(vec_len);
233 return fParseData(jv, vec_len, fTarget->data());
234 }
235
parseKFValue(const AnimationBuilder &,const skjson::ObjectValue &,const skjson::Value & jv,Keyframe::Value * kfv)236 bool VectorAnimatorBuilder::parseKFValue(const AnimationBuilder&,
237 const skjson::ObjectValue&,
238 const skjson::Value& jv,
239 Keyframe::Value* kfv) {
240 auto offset = fCurrentVec * fVecLen;
241 SkASSERT(offset + fVecLen <= fStorage.size());
242
243 if (!fParseData(jv, fVecLen, fStorage.data() + offset)) {
244 return false;
245 }
246
247 SkASSERT(!fCurrentVec || offset >= fVecLen);
248 // compare with previous vector value
249 if (fCurrentVec > 0 && !memcmp(fStorage.data() + offset,
250 fStorage.data() + offset - fVecLen,
251 fVecLen * sizeof(float))) {
252 // repeating value -> use prev offset (dedupe)
253 offset -= fVecLen;
254 } else {
255 // new value -> advance the current index
256 fCurrentVec += 1;
257 }
258
259 // Keyframes record the storage-offset for a given vector value.
260 kfv->idx = SkToU32(offset);
261
262 return true;
263 }
264
265 template <>
bind(const AnimationBuilder & abuilder,const skjson::ObjectValue * jprop,VectorValue * v)266 bool AnimatablePropertyContainer::bind<VectorValue>(const AnimationBuilder& abuilder,
267 const skjson::ObjectValue* jprop,
268 VectorValue* v) {
269 if (!jprop) {
270 return false;
271 }
272
273 if (!ParseDefault<bool>((*jprop)["s"], false)) {
274 // Regular (static or keyframed) vector value.
275 VectorAnimatorBuilder builder(
276 v,
277 // Len parser.
278 [](const skjson::Value& jv, size_t* len) -> bool {
279 if (const skjson::ArrayValue* ja = jv) {
280 *len = ja->size();
281 return true;
282 }
283 return false;
284 },
285 // Data parser.
286 [](const skjson::Value& jv, size_t len, float* data) {
287 return parse_array(jv, data, len);
288 });
289
290 return this->bindImpl(abuilder, jprop, builder);
291 }
292
293 // Separate-dimensions vector value: each component is animated independently.
294 *v = { 0, 0, 0 };
295 bool boundX = this->bind(abuilder, (*jprop)["x"], v->data() + 0);
296 bool boundY = this->bind(abuilder, (*jprop)["y"], v->data() + 1);
297 bool boundZ = this->bind(abuilder, (*jprop)["z"], v->data() + 2);
298 return boundX || boundY || boundZ;
299 }
300
301 } // namespace internal
302 } // namespace skottie
303