2017-12-30 17:27:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
#include "SkottieAnimator.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
#include "SkCubicMap.h"
|
|
|
|
#include "SkJSONCPP.h"
|
2018-03-23 17:41:58 +00:00
|
|
|
#include "SkottieValue.h"
|
2018-01-26 03:35:09 +00:00
|
|
|
#include "SkottieParser.h"
|
2018-01-28 17:35:44 +00:00
|
|
|
#include "SkTArray.h"
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
namespace skottie {
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
#define LOG SkDebugf
|
|
|
|
|
|
|
|
bool LogFail(const Json::Value& json, const char* msg) {
|
|
|
|
const auto dump = json.toStyledString();
|
|
|
|
LOG("!! %s: %s", msg, dump.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class KeyframeAnimatorBase : public sksg::Animator {
|
|
|
|
public:
|
2018-01-28 17:35:44 +00:00
|
|
|
int count() const { return fRecs.count(); }
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
KeyframeAnimatorBase() = default;
|
|
|
|
|
|
|
|
struct KeyframeRec {
|
|
|
|
float t0, t1;
|
2018-01-28 17:35:44 +00:00
|
|
|
int vidx0, vidx1, // v0/v1 indices
|
|
|
|
cmidx; // cubic map index
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
bool contains(float t) const { return t0 <= t && t <= t1; }
|
2018-01-28 17:35:44 +00:00
|
|
|
bool isConstant() const { return vidx0 == vidx1; }
|
|
|
|
bool isValid() const {
|
|
|
|
SkASSERT(t0 <= t1);
|
|
|
|
// Constant frames don't need/use t1 and vidx1.
|
|
|
|
return t0 < t1 || this->isConstant();
|
|
|
|
}
|
2018-01-26 03:35:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const KeyframeRec& frame(float t) {
|
|
|
|
if (!fCachedRec || !fCachedRec->contains(t)) {
|
|
|
|
fCachedRec = findFrame(t);
|
2018-01-09 00:23:08 +00:00
|
|
|
}
|
2018-01-26 03:35:09 +00:00
|
|
|
return *fCachedRec;
|
2018-01-09 00:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
float localT(const KeyframeRec& rec, float t) const {
|
|
|
|
SkASSERT(rec.isValid());
|
|
|
|
SkASSERT(!rec.isConstant());
|
|
|
|
SkASSERT(t > rec.t0 && t < rec.t1);
|
2018-01-24 16:17:42 +00:00
|
|
|
|
2018-01-31 22:25:14 +00:00
|
|
|
auto lt = (t - rec.t0) / (rec.t1 - rec.t0);
|
2018-01-24 16:17:42 +00:00
|
|
|
|
2018-01-31 22:25:14 +00:00
|
|
|
return rec.cmidx < 0
|
|
|
|
? lt
|
|
|
|
: SkTPin(fCubicMaps[rec.cmidx].computeYFromX(lt), 0.0f, 1.0f);
|
2018-01-26 03:35:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 17:35:44 +00:00
|
|
|
virtual int parseValue(const Json::Value&) = 0;
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
void parseKeyFrames(const Json::Value& jframes) {
|
|
|
|
if (!jframes.isArray())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto& jframe : jframes) {
|
|
|
|
if (!jframe.isObject())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float t0;
|
|
|
|
if (!Parse(jframe["t"], &t0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fRecs.empty()) {
|
|
|
|
if (fRecs.back().t1 >= t0) {
|
|
|
|
LOG("!! Ignoring out-of-order key frame (t:%f < t:%f)\n", t0, fRecs.back().t1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Back-fill t1 in prev interval. Note: we do this even if we end up discarding
|
|
|
|
// the current interval (to support "t"-only final frames).
|
|
|
|
fRecs.back().t1 = t0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto vidx0 = this->parseValue(jframe["s"]);
|
2018-01-28 17:35:44 +00:00
|
|
|
if (vidx0 < 0) {
|
2018-01-26 03:35:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-28 17:35:44 +00:00
|
|
|
// Defaults for constant frames.
|
|
|
|
int vidx1 = vidx0, cmidx = -1;
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
if (!ParseDefault(jframe["h"], false)) {
|
2018-01-28 17:35:44 +00:00
|
|
|
// Regular frame, requires an end value.
|
2018-01-26 03:35:09 +00:00
|
|
|
vidx1 = this->parseValue(jframe["e"]);
|
2018-01-28 17:35:44 +00:00
|
|
|
if (vidx1 < 0) {
|
2018-01-26 03:35:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is linear lerp
|
|
|
|
static constexpr SkPoint kDefaultC0 = { 0, 0 },
|
|
|
|
kDefaultC1 = { 1, 1 };
|
|
|
|
const auto c0 = ParseDefault(jframe["i"], kDefaultC0),
|
|
|
|
c1 = ParseDefault(jframe["o"], kDefaultC1);
|
|
|
|
|
|
|
|
if (c0 != kDefaultC0 || c1 != kDefaultC1) {
|
|
|
|
// TODO: is it worth de-duping these?
|
2018-01-28 17:35:44 +00:00
|
|
|
cmidx = fCubicMaps.count();
|
2018-01-26 03:35:09 +00:00
|
|
|
fCubicMaps.emplace_back();
|
|
|
|
// TODO: why do we have to plug these inverted?
|
|
|
|
fCubicMaps.back().setPts(c1, c0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fRecs.push_back({t0, t0, vidx0, vidx1, cmidx });
|
2018-01-24 16:17:42 +00:00
|
|
|
}
|
2018-01-26 03:35:09 +00:00
|
|
|
|
2018-01-28 17:35:44 +00:00
|
|
|
// If we couldn't determine a valid t1 for the last frame, discard it.
|
2018-01-26 03:35:09 +00:00
|
|
|
if (!fRecs.empty() && !fRecs.back().isValid()) {
|
|
|
|
fRecs.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(fRecs.empty() || fRecs.back().isValid());
|
2018-01-09 00:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
private:
|
|
|
|
const KeyframeRec* findFrame(float t) const {
|
|
|
|
SkASSERT(!fRecs.empty());
|
2018-01-09 00:23:08 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
auto f0 = &fRecs.front(),
|
|
|
|
f1 = &fRecs.back();
|
2018-01-24 16:17:42 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
SkASSERT(f0->isValid());
|
|
|
|
SkASSERT(f1->isValid());
|
2018-01-09 00:23:08 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
if (t < f0->t0) {
|
|
|
|
return f0;
|
|
|
|
}
|
2018-01-09 00:23:08 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
if (t > f1->t1) {
|
|
|
|
return f1;
|
|
|
|
}
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
while (f0 != f1) {
|
|
|
|
SkASSERT(f0 < f1);
|
|
|
|
SkASSERT(t >= f0->t0 && t <= f1->t1);
|
2018-01-09 00:23:08 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
const auto f = f0 + (f1 - f0) / 2;
|
|
|
|
SkASSERT(f->isValid());
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
if (t > f->t1) {
|
|
|
|
f0 = f + 1;
|
|
|
|
} else {
|
|
|
|
f1 = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(f0 == f1);
|
|
|
|
SkASSERT(f0->contains(t));
|
|
|
|
|
|
|
|
return f0;
|
|
|
|
}
|
|
|
|
|
2018-01-28 17:35:44 +00:00
|
|
|
SkTArray<KeyframeRec> fRecs;
|
|
|
|
SkTArray<SkCubicMap> fCubicMaps;
|
|
|
|
const KeyframeRec* fCachedRec = nullptr;
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
using INHERITED = sksg::Animator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class KeyframeAnimator final : public KeyframeAnimatorBase {
|
|
|
|
public:
|
|
|
|
static std::unique_ptr<KeyframeAnimator> Make(const Json::Value& jframes,
|
|
|
|
std::function<void(const T&)>&& apply) {
|
|
|
|
std::unique_ptr<KeyframeAnimator> animator(new KeyframeAnimator(jframes, std::move(apply)));
|
2018-01-28 17:35:44 +00:00
|
|
|
if (!animator->count())
|
2018-01-26 03:35:09 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return animator;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onTick(float t) override {
|
|
|
|
T val;
|
|
|
|
this->eval(this->frame(t), t, &val);
|
|
|
|
|
|
|
|
fApplyFunc(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
KeyframeAnimator(const Json::Value& jframes,
|
|
|
|
std::function<void(const T&)>&& apply)
|
|
|
|
: fApplyFunc(std::move(apply)) {
|
|
|
|
this->parseKeyFrames(jframes);
|
|
|
|
}
|
|
|
|
|
2018-01-28 17:35:44 +00:00
|
|
|
int parseValue(const Json::Value& jv) override {
|
2018-01-26 03:35:09 +00:00
|
|
|
T val;
|
|
|
|
if (!Parse(jv, &val) || (!fVs.empty() &&
|
|
|
|
ValueTraits<T>::Cardinality(val) != ValueTraits<T>::Cardinality(fVs.back()))) {
|
2018-01-28 17:35:44 +00:00
|
|
|
return -1;
|
2018-01-26 03:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: full deduping?
|
|
|
|
if (fVs.empty() || val != fVs.back()) {
|
|
|
|
fVs.push_back(std::move(val));
|
|
|
|
}
|
2018-01-28 17:35:44 +00:00
|
|
|
return fVs.count() - 1;
|
2018-01-26 03:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void eval(const KeyframeRec& rec, float t, T* v) const {
|
|
|
|
SkASSERT(rec.isValid());
|
|
|
|
if (rec.isConstant() || t <= rec.t0) {
|
|
|
|
*v = fVs[rec.vidx0];
|
|
|
|
} else if (t >= rec.t1) {
|
|
|
|
*v = fVs[rec.vidx1];
|
|
|
|
} else {
|
|
|
|
const auto lt = this->localT(rec, t);
|
|
|
|
const auto& v0 = fVs[rec.vidx0];
|
|
|
|
const auto& v1 = fVs[rec.vidx1];
|
2018-05-01 01:49:41 +00:00
|
|
|
*v = ValueTraits<T>::Lerp(v0, v1, lt);
|
2018-01-26 03:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::function<void(const T&)> fApplyFunc;
|
2018-01-28 17:35:44 +00:00
|
|
|
SkTArray<T> fVs;
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
using INHERITED = KeyframeAnimatorBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline bool BindPropertyImpl(const Json::Value& jprop,
|
2018-01-29 15:49:49 +00:00
|
|
|
sksg::AnimatorList* animators,
|
2018-01-26 03:35:09 +00:00
|
|
|
std::function<void(const T&)>&& apply,
|
2018-02-01 16:33:08 +00:00
|
|
|
const T* noop = nullptr) {
|
2018-01-26 03:35:09 +00:00
|
|
|
if (!jprop.isObject())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto& jpropA = jprop["a"];
|
|
|
|
const auto& jpropK = jprop["k"];
|
|
|
|
|
|
|
|
// Older Json versions don't have an "a" animation marker.
|
|
|
|
// For those, we attempt to parse both ways.
|
|
|
|
if (!ParseDefault(jpropA, false)) {
|
|
|
|
T val;
|
|
|
|
if (Parse<T>(jpropK, &val)) {
|
|
|
|
// Static property.
|
|
|
|
if (noop && val == *noop)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
apply(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jpropA.isNull()) {
|
|
|
|
return LogFail(jprop, "Could not parse (explicit) static property");
|
|
|
|
}
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
2018-01-26 03:35:09 +00:00
|
|
|
|
|
|
|
// Keyframe property.
|
|
|
|
auto animator = KeyframeAnimator<T>::Make(jpropK, std::move(apply));
|
|
|
|
|
|
|
|
if (!animator) {
|
|
|
|
return LogFail(jprop, "Could not parse keyframed property");
|
|
|
|
}
|
|
|
|
|
|
|
|
animators->push_back(std::move(animator));
|
|
|
|
|
|
|
|
return true;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:33:08 +00:00
|
|
|
class SplitPointAnimator final : public sksg::Animator {
|
|
|
|
public:
|
|
|
|
static std::unique_ptr<SplitPointAnimator> Make(const Json::Value& jprop,
|
|
|
|
std::function<void(const VectorValue&)>&& apply,
|
|
|
|
const VectorValue*) {
|
|
|
|
if (!jprop.isObject())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::unique_ptr<SplitPointAnimator> split_animator(
|
|
|
|
new SplitPointAnimator(std::move(apply)));
|
|
|
|
|
|
|
|
// This raw pointer is captured in lambdas below. But the lambdas are owned by
|
|
|
|
// the object itself, so the scope is bound to the life time of the object.
|
|
|
|
auto* split_animator_ptr = split_animator.get();
|
|
|
|
|
|
|
|
if (!BindPropertyImpl<ScalarValue>(jprop["x"], &split_animator->fAnimators,
|
|
|
|
[split_animator_ptr](const ScalarValue& x) { split_animator_ptr->setX(x); }) ||
|
|
|
|
!BindPropertyImpl<ScalarValue>(jprop["y"], &split_animator->fAnimators,
|
|
|
|
[split_animator_ptr](const ScalarValue& y) { split_animator_ptr->setY(y); })) {
|
|
|
|
LogFail(jprop, "Could not parse split property");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split_animator->fAnimators.empty()) {
|
|
|
|
// Static split property, no need to hold on to the split animator.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return split_animator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onTick(float t) override {
|
|
|
|
for (const auto& animator : fAnimators) {
|
|
|
|
animator->tick(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
const VectorValue vec = { fX, fY };
|
|
|
|
fApplyFunc(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setX(const ScalarValue& x) { fX = x; }
|
|
|
|
void setY(const ScalarValue& y) { fY = y; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit SplitPointAnimator(std::function<void(const VectorValue&)>&& apply)
|
|
|
|
: fApplyFunc(std::move(apply)) {}
|
|
|
|
|
|
|
|
const std::function<void(const VectorValue&)> fApplyFunc;
|
|
|
|
sksg::AnimatorList fAnimators;
|
|
|
|
|
|
|
|
ScalarValue fX = 0,
|
|
|
|
fY = 0;
|
|
|
|
|
|
|
|
using INHERITED = sksg::Animator;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool BindSplitPositionProperty(const Json::Value& jprop,
|
|
|
|
sksg::AnimatorList* animators,
|
|
|
|
std::function<void(const VectorValue&)>&& apply,
|
|
|
|
const VectorValue* noop) {
|
|
|
|
if (auto split_animator = SplitPointAnimator::Make(jprop, std::move(apply), noop)) {
|
|
|
|
animators->push_back(std::unique_ptr<sksg::Animator>(split_animator.release()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
template <>
|
2018-01-26 03:35:09 +00:00
|
|
|
bool BindProperty(const Json::Value& jprop,
|
2018-01-29 15:49:49 +00:00
|
|
|
sksg::AnimatorList* animators,
|
2018-01-26 03:35:09 +00:00
|
|
|
std::function<void(const ScalarValue&)>&& apply,
|
|
|
|
const ScalarValue* noop) {
|
|
|
|
return BindPropertyImpl(jprop, animators, std::move(apply), noop);
|
|
|
|
}
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-26 03:35:09 +00:00
|
|
|
template <>
|
|
|
|
bool BindProperty(const Json::Value& jprop,
|
2018-01-29 15:49:49 +00:00
|
|
|
sksg::AnimatorList* animators,
|
2018-01-26 03:35:09 +00:00
|
|
|
std::function<void(const VectorValue&)>&& apply,
|
|
|
|
const VectorValue* noop) {
|
2018-02-01 16:33:08 +00:00
|
|
|
return ParseDefault(jprop["s"], false)
|
|
|
|
? BindSplitPositionProperty(jprop, animators, std::move(apply), noop)
|
|
|
|
: BindPropertyImpl(jprop, animators, std::move(apply), noop);
|
2018-01-26 03:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool BindProperty(const Json::Value& jprop,
|
2018-01-29 15:49:49 +00:00
|
|
|
sksg::AnimatorList* animators,
|
2018-01-26 03:35:09 +00:00
|
|
|
std::function<void(const ShapeValue&)>&& apply,
|
|
|
|
const ShapeValue* noop) {
|
|
|
|
return BindPropertyImpl(jprop, animators, std::move(apply), noop);
|
2018-01-04 18:11:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
} // namespace skottie
|