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