aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/skottie/SkottieAnimator.cpp
blob: da00beb2fef7639708ebe999c4b00d516ca5b2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkottieAnimator.h"

namespace skottie {

namespace {

SkScalar lerp_scalar(float v0, float v1, float t) {
    SkASSERT(t >= 0 && t <= 1);
    return v0 * (1 - t) + v1 * t;
}

static inline SkPoint ParsePoint(const Json::Value& v, const SkPoint& defaultValue) {
    if (!v.isObject())
        return defaultValue;

    const auto& vx = v["x"];
    const auto& vy = v["y"];

    // Some BM versions seem to store x/y as single-element arrays.
    return SkPoint::Make(ParseScalar(vx.isArray() ? vx[0] : vx, defaultValue.x()),
                         ParseScalar(vy.isArray() ? vy[0] : vy, defaultValue.y()));
}

} // namespace

bool KeyframeIntervalBase::parse(const Json::Value& k, KeyframeIntervalBase* prev) {
    SkASSERT(k.isObject());

    fT0 = fT1 = ParseScalar(k["t"], SK_ScalarMin);
    if (fT0 == SK_ScalarMin) {
        return false;
    }

    if (prev) {
        if (prev->fT1 >= fT0) {
            LOG("!! Dropping out-of-order key frame (t: %f < t: %f)\n", fT0, prev->fT1);
            return false;
        }
        // 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).
        prev->fT1 = fT0;
    }

    fHold = ParseBool(k["h"], false);

    if (!fHold) {
        // default is linear lerp
        static constexpr SkPoint kDefaultC0 = { 0, 0 },
                                 kDefaultC1 = { 1, 1 };
        const auto c0 = ParsePoint(k["i"], kDefaultC0),
                   c1 = ParsePoint(k["o"], kDefaultC1);

        if (c0 != kDefaultC0 || c1 != kDefaultC1) {
            fCubicMap = skstd::make_unique<SkCubicMap>();
            // TODO: why do we have to plug these inverted?
            fCubicMap->setPts(c1, c0);
        }
    }

    return true;
}

float KeyframeIntervalBase::localT(float t) const {
    SkASSERT(this->isValid());

    // 'hold' pins to v0
    if (fHold) {
        return 0;
    }

    auto lt = (t - fT0) / (fT1 - fT0);

    if (fCubicMap) {
        lt = fCubicMap->computeYFromX(lt);
    }

    return SkTPin<float>(lt, 0, 1);
}

template <>
void KeyframeInterval<ScalarValue>::lerp(float t, ScalarValue* v) const {
    const auto lt = this->localT(t);
    *v = lerp_scalar(fV0, fV1, lt);
}

template <>
void KeyframeInterval<VectorValue>::lerp(float t, VectorValue* v) const {
    SkASSERT(fV0.size() == fV1.size());
    SkASSERT(v->size() == 0);

    const auto lt = this->localT(t);

    v->reserve(fV0.size());
    for (size_t i = 0; i < fV0.size(); ++i) {
        v->push_back(lerp_scalar(fV0[i], fV1[i], lt));
    }
}

template <>
void KeyframeInterval<ShapeValue>::lerp(float t, ShapeValue* v) const {
    SkASSERT(fV0.countVerbs() == fV1.countVerbs());
    SkASSERT(v->isEmpty());

    const auto lt = this->localT(t);
    SkAssertResult(fV1.interpolate(fV0, lt, v));
    v->setIsVolatile(true);
}

} // namespace skottie