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