blob: b28b15aa6cf4df0da05c419fcd0f378c3b24af4a [file] [log] [blame]
Mike Reed74065272021-04-12 09:52:07 -04001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkiaInterpolator.h"
18
19#include "include/core/SkMath.h"
Kevin Lubick46691242022-11-29 19:08:17 +000020#include "include/core/SkScalar.h"
21#include "include/core/SkTypes.h"
Mike Reed74065272021-04-12 09:52:07 -040022#include "include/private/SkFixed.h"
23#include "include/private/SkMalloc.h"
Mike Reed74065272021-04-12 09:52:07 -040024#include "src/core/SkTSearch.h"
25
26typedef int Dot14;
27#define Dot14_ONE (1 << 14)
28#define Dot14_HALF (1 << 13)
29
30#define Dot14ToFloat(x) ((x) / 16384.f)
31
32static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) {
33 return (a * b + Dot14_HALF) >> 14;
34}
35
36static inline Dot14 eval_cubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) {
37 return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t);
38}
39
40static inline Dot14 pin_and_convert(float x) {
41 if (x <= 0) {
42 return 0;
43 }
44 if (x >= SK_Scalar1) {
45 return Dot14_ONE;
46 }
47 return SkScalarToFixed(x) >> 2;
48}
49
50static float SkUnitCubicInterp(float value, float bx, float by, float cx, float cy) {
51 // pin to the unit-square, and convert to 2.14
52 Dot14 x = pin_and_convert(value);
53
54 if (x == 0) return 0;
55 if (x == Dot14_ONE) return SK_Scalar1;
56
57 Dot14 b = pin_and_convert(bx);
58 Dot14 c = pin_and_convert(cx);
59
60 // Now compute our coefficients from the control points
61 // t -> 3b
62 // t^2 -> 3c - 6b
63 // t^3 -> 3b - 3c + 1
64 Dot14 A = 3 * b;
65 Dot14 B = 3 * (c - 2 * b);
66 Dot14 C = 3 * (b - c) + Dot14_ONE;
67
68 // Now search for a t value given x
69 Dot14 t = Dot14_HALF;
70 Dot14 dt = Dot14_HALF;
71 for (int i = 0; i < 13; i++) {
72 dt >>= 1;
73 Dot14 guess = eval_cubic(t, A, B, C);
74 if (x < guess) {
75 t -= dt;
76 } else {
77 t += dt;
78 }
79 }
80
81 // Now we have t, so compute the coeff for Y and evaluate
82 b = pin_and_convert(by);
83 c = pin_and_convert(cy);
84 A = 3 * b;
85 B = 3 * (c - 2 * b);
86 C = 3 * (b - c) + Dot14_ONE;
87 return SkFixedToScalar(eval_cubic(t, A, B, C) << 2);
88}
89
90///////////////////////////////////////////////////////////////////////////////////////////////////
91
92SkiaInterpolatorBase::SkiaInterpolatorBase() {
93 fStorage = nullptr;
94 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -040095}
96
97SkiaInterpolatorBase::~SkiaInterpolatorBase() {
98 if (fStorage) {
99 sk_free(fStorage);
100 }
101}
102
103void SkiaInterpolatorBase::reset(int elemCount, int frameCount) {
104 fFlags = 0;
Kevin Lubickd9ccdf62023-01-05 19:07:26 +0000105 fElemCount = static_cast<uint8_t>(elemCount);
106 fFrameCount = static_cast<int16_t>(frameCount);
Mike Reed74065272021-04-12 09:52:07 -0400107 fRepeat = SK_Scalar1;
108 if (fStorage) {
109 sk_free(fStorage);
110 fStorage = nullptr;
111 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400112 }
113}
114
115/* Each value[] run is formatted as:
116 <time (in msec)>
117 <blend>
118 <data[fElemCount]>
119
120 Totaling fElemCount+2 entries per keyframe
121*/
122
123bool SkiaInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const {
124 if (fFrameCount == 0) {
125 return false;
126 }
127
128 if (startTime) {
129 *startTime = fTimes[0].fTime;
130 }
131 if (endTime) {
132 *endTime = fTimes[fFrameCount - 1].fTime;
133 }
134 return true;
135}
136
137float SkiaInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime, SkMSec nextTime,
138 const float blend[4]) {
139 SkASSERT(time > prevTime && time < nextTime);
140
141 float t = (float)(time - prevTime) / (float)(nextTime - prevTime);
142 return blend ? SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
143}
144
145SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(SkMSec time, float* T, int* indexPtr,
146 bool* exactPtr) const {
147 SkASSERT(fFrameCount > 0);
148 Result result = kNormal_Result;
149 if (fRepeat != SK_Scalar1) {
150 SkMSec startTime = 0, endTime = 0; // initialize to avoid warning
151 this->getDuration(&startTime, &endTime);
152 SkMSec totalTime = endTime - startTime;
153 SkMSec offsetTime = time - startTime;
154 endTime = SkScalarFloorToInt(fRepeat * totalTime);
155 if (offsetTime >= endTime) {
156 float fraction = SkScalarFraction(fRepeat);
157 offsetTime = fraction == 0 && fRepeat > 0
158 ? totalTime
159 : (SkMSec)SkScalarFloorToInt(fraction * totalTime);
160 result = kFreezeEnd_Result;
161 } else {
162 int mirror = fFlags & kMirror;
163 offsetTime = offsetTime % (totalTime << mirror);
164 if (offsetTime > totalTime) { // can only be true if fMirror is true
165 offsetTime = (totalTime << 1) - offsetTime;
166 }
167 }
168 time = offsetTime + startTime;
169 }
170
171 int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time, sizeof(SkTimeCode));
172
173 bool exact = true;
174
175 if (index < 0) {
176 index = ~index;
177 if (index == 0) {
178 result = kFreezeStart_Result;
179 } else if (index == fFrameCount) {
180 if (fFlags & kReset) {
181 index = 0;
182 } else {
183 index -= 1;
184 }
185 result = kFreezeEnd_Result;
186 } else {
187 exact = false;
188 }
189 }
190 SkASSERT(index < fFrameCount);
191 const SkTimeCode* nextTime = &fTimes[index];
192 SkMSec nextT = nextTime[0].fTime;
193 if (exact) {
194 *T = 0;
195 } else {
196 SkMSec prevT = nextTime[-1].fTime;
197 *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
198 }
199 *indexPtr = index;
200 *exactPtr = exact;
201 return result;
202}
203
204SkiaInterpolator::SkiaInterpolator() {
205 INHERITED::reset(0, 0);
206 fValues = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400207}
208
209SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) {
210 SkASSERT(elemCount > 0);
211 this->reset(elemCount, frameCount);
212}
213
214void SkiaInterpolator::reset(int elemCount, int frameCount) {
215 INHERITED::reset(elemCount, frameCount);
216 fStorage = sk_malloc_throw((sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount);
217 fTimes = (SkTimeCode*)fStorage;
218 fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount);
Mike Reed74065272021-04-12 09:52:07 -0400219}
220
221#define SK_Fixed1Third (SK_Fixed1 / 3)
222#define SK_Fixed2Third (SK_Fixed1 * 2 / 3)
223
224static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f};
225
226bool SkiaInterpolator::setKeyFrame(int index, SkMSec time, const float values[],
227 const float blend[4]) {
228 SkASSERT(values != nullptr);
229
230 if (blend == nullptr) {
231 blend = gIdentityBlend;
232 }
233
234 bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode));
235 SkASSERT(success);
236 if (success) {
237 SkTimeCode* timeCode = &fTimes[index];
238 timeCode->fTime = time;
239 memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
240 float* dst = &fValues[fElemCount * index];
241 memcpy(dst, values, fElemCount * sizeof(float));
242 }
243 return success;
244}
245
246SkiaInterpolator::Result SkiaInterpolator::timeToValues(SkMSec time, float values[]) const {
247 float T;
248 int index;
249 bool exact;
250 Result result = timeToT(time, &T, &index, &exact);
251 if (values) {
252 const float* nextSrc = &fValues[index * fElemCount];
253
254 if (exact) {
255 memcpy(values, nextSrc, fElemCount * sizeof(float));
256 } else {
257 SkASSERT(index > 0);
258
259 const float* prevSrc = nextSrc - fElemCount;
260
261 for (int i = fElemCount - 1; i >= 0; --i) {
262 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
263 }
264 }
265 }
266 return result;
267}