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/base/SkTPin.h"
12 #include "modules/skottie/src/SkottieJson.h"
13 #include "modules/skottie/src/SkottieValue.h"
14 #include "modules/skottie/src/animator/Animator.h"
15 #include "src/base/SkSafeMath.h"
16 #include "src/base/SkVx.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 = skvx::float4::Load(dst),
116 new_val = Lerp(skvx::float4::Load(v0),
117 skvx::float4::Load(v1),
118 lerp_info.weight);
119
120 changed |= any(new_val != old_val);
121 new_val.store(dst);
122
123 v0 += 4;
124 v1 += 4;
125 dst += 4;
126 count -= 4;
127 }
128
129 while (count-- > 0) {
130 const auto new_val = Lerp(*v0++, *v1++, lerp_info.weight);
131
132 changed |= (new_val != *dst);
133 *dst++ = new_val;
134 }
135
136 return changed;
137 }
138
139 const std::vector<float> fStorage;
140 const size_t fVecLen;
141
142 std::vector<float>* fTarget;
143
144 using INHERITED = KeyframeAnimator;
145 };
146
147 class VectorExpressionAnimator final : public Animator {
148 public:
VectorExpressionAnimator(sk_sp<ExpressionEvaluator<std::vector<float>>> expression_evaluator,std::vector<float> * target_value)149 VectorExpressionAnimator(sk_sp<ExpressionEvaluator<std::vector<float>>> expression_evaluator,
150 std::vector<float>* target_value)
151 : fExpressionEvaluator(std::move(expression_evaluator))
152 , fTarget(target_value) {}
153
154 private:
155
onSeek(float t)156 StateChanged onSeek(float t) override {
157 std::vector<float> result = fExpressionEvaluator->evaluate(t);
158 bool changed = false;
159 for (size_t i = 0; i < fTarget->size(); i++) {
160 // Use 0 as a default if the result is too small.
161 float val = i >= result.size() ? 0 : result[i];
162 if (!SkScalarNearlyEqual(val, (*fTarget)[i])) {
163 changed = true;
164 }
165 (*fTarget)[i] = val;
166 }
167
168 return changed;
169 }
170
171 sk_sp<ExpressionEvaluator<std::vector<float>>> fExpressionEvaluator;
172 std::vector<float>* fTarget;
173 };
174 } // namespace
175
VectorAnimatorBuilder(std::vector<float> * target,VectorLenParser parse_len,VectorDataParser parse_data)176 VectorAnimatorBuilder::VectorAnimatorBuilder(std::vector<float>* target,
177 VectorLenParser parse_len,
178 VectorDataParser parse_data)
179 : INHERITED(Keyframe::Value::Type::kIndex)
180 , fParseLen(parse_len)
181 , fParseData(parse_data)
182 , fTarget(target) {}
183
makeFromKeyframes(const AnimationBuilder & abuilder,const skjson::ArrayValue & jkfs)184 sk_sp<KeyframeAnimator> VectorAnimatorBuilder::makeFromKeyframes(const AnimationBuilder& abuilder,
185 const skjson::ArrayValue& jkfs) {
186 SkASSERT(jkfs.size() > 0);
187
188 // peek at the first keyframe value to find our vector length
189 const skjson::ObjectValue* jkf0 = jkfs[0];
190 if (!jkf0 || !fParseLen((*jkf0)["s"], &fVecLen)) {
191 return nullptr;
192 }
193
194 SkSafeMath safe;
195 // total elements: vector length x number vectors
196 const auto total_size = safe.mul(fVecLen, jkfs.size());
197
198 // we must be able to store all offsets in Keyframe::Value::idx (uint32_t)
199 if (!safe || !SkTFitsIn<uint32_t>(total_size)) {
200 return nullptr;
201 }
202 fStorage.resize(total_size);
203
204 if (!this->parseKeyframes(abuilder, jkfs)) {
205 return nullptr;
206 }
207
208 // parseKFValue() might have stored fewer vectors thanks to tail-deduping.
209 SkASSERT(fCurrentVec <= jkfs.size());
210 fStorage.resize(fCurrentVec * fVecLen);
211 fStorage.shrink_to_fit();
212
213 return sk_sp<VectorKeyframeAnimator>(
214 new VectorKeyframeAnimator(std::move(fKFs),
215 std::move(fCMs),
216 std::move(fStorage),
217 fVecLen,
218 fTarget));
219 }
220
makeFromExpression(ExpressionManager & em,const char * expr)221 sk_sp<Animator> VectorAnimatorBuilder::makeFromExpression(ExpressionManager& em, const char* expr) {
222 sk_sp<ExpressionEvaluator<std::vector<SkScalar>>> expression_evaluator =
223 em.createArrayExpressionEvaluator(expr);
224 return sk_make_sp<VectorExpressionAnimator>(expression_evaluator, fTarget);
225 }
226
parseValue(const AnimationBuilder &,const skjson::Value & jv) const227 bool VectorAnimatorBuilder::parseValue(const AnimationBuilder&,
228 const skjson::Value& jv) const {
229 size_t vec_len;
230 if (!this->fParseLen(jv, &vec_len)) {
231 return false;
232 }
233
234 fTarget->resize(vec_len);
235 return fParseData(jv, vec_len, fTarget->data());
236 }
237
parseKFValue(const AnimationBuilder &,const skjson::ObjectValue &,const skjson::Value & jv,Keyframe::Value * kfv)238 bool VectorAnimatorBuilder::parseKFValue(const AnimationBuilder&,
239 const skjson::ObjectValue&,
240 const skjson::Value& jv,
241 Keyframe::Value* kfv) {
242 auto offset = fCurrentVec * fVecLen;
243 SkASSERT(offset + fVecLen <= fStorage.size());
244
245 if (!fParseData(jv, fVecLen, fStorage.data() + offset)) {
246 return false;
247 }
248
249 SkASSERT(!fCurrentVec || offset >= fVecLen);
250 // compare with previous vector value
251 if (fCurrentVec > 0 && !memcmp(fStorage.data() + offset,
252 fStorage.data() + offset - fVecLen,
253 fVecLen * sizeof(float))) {
254 // repeating value -> use prev offset (dedupe)
255 offset -= fVecLen;
256 } else {
257 // new value -> advance the current index
258 fCurrentVec += 1;
259 }
260
261 // Keyframes record the storage-offset for a given vector value.
262 kfv->idx = SkToU32(offset);
263
264 return true;
265 }
266
267 template <>
bind(const AnimationBuilder & abuilder,const skjson::ObjectValue * jprop,VectorValue * v)268 bool AnimatablePropertyContainer::bind<VectorValue>(const AnimationBuilder& abuilder,
269 const skjson::ObjectValue* jprop,
270 VectorValue* v) {
271 if (!jprop) {
272 return false;
273 }
274
275 if (!ParseDefault<bool>((*jprop)["s"], false)) {
276 // Regular (static or keyframed) vector value.
277 VectorAnimatorBuilder builder(
278 v,
279 // Len parser.
280 [](const skjson::Value& jv, size_t* len) -> bool {
281 if (const skjson::ArrayValue* ja = jv) {
282 *len = ja->size();
283 return true;
284 }
285 return false;
286 },
287 // Data parser.
288 [](const skjson::Value& jv, size_t len, float* data) {
289 return parse_array(jv, data, len);
290 });
291
292 return this->bindImpl(abuilder, jprop, builder);
293 }
294
295 // Separate-dimensions vector value: each component is animated independently.
296 *v = { 0, 0, 0 };
297 bool boundX = this->bind(abuilder, (*jprop)["x"], v->data() + 0);
298 bool boundY = this->bind(abuilder, (*jprop)["y"], v->data() + 1);
299 bool boundZ = this->bind(abuilder, (*jprop)["z"], v->data() + 2);
300 return boundX || boundY || boundZ;
301 }
302
303 } // namespace internal
304 } // namespace skottie
305