blob: 47bd0b96ebd34ba6fd86b24f2fefd2d774ce2254 [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"
Mike Reed74065272021-04-12 09:52:07 -040023#include "src/core/SkTSearch.h"
24
Kevin Lubick98046982023-01-05 18:42:26 +000025#include <log/log.h>
26
Mike Reed74065272021-04-12 09:52:07 -040027typedef int Dot14;
28#define Dot14_ONE (1 << 14)
29#define Dot14_HALF (1 << 13)
30
31#define Dot14ToFloat(x) ((x) / 16384.f)
32
33static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) {
34 return (a * b + Dot14_HALF) >> 14;
35}
36
37static inline Dot14 eval_cubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) {
38 return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t);
39}
40
41static inline Dot14 pin_and_convert(float x) {
42 if (x <= 0) {
43 return 0;
44 }
45 if (x >= SK_Scalar1) {
46 return Dot14_ONE;
47 }
48 return SkScalarToFixed(x) >> 2;
49}
50
51static float SkUnitCubicInterp(float value, float bx, float by, float cx, float cy) {
52 // pin to the unit-square, and convert to 2.14
53 Dot14 x = pin_and_convert(value);
54
55 if (x == 0) return 0;
56 if (x == Dot14_ONE) return SK_Scalar1;
57
58 Dot14 b = pin_and_convert(bx);
59 Dot14 c = pin_and_convert(cx);
60
61 // Now compute our coefficients from the control points
62 // t -> 3b
63 // t^2 -> 3c - 6b
64 // t^3 -> 3b - 3c + 1
65 Dot14 A = 3 * b;
66 Dot14 B = 3 * (c - 2 * b);
67 Dot14 C = 3 * (b - c) + Dot14_ONE;
68
69 // Now search for a t value given x
70 Dot14 t = Dot14_HALF;
71 Dot14 dt = Dot14_HALF;
72 for (int i = 0; i < 13; i++) {
73 dt >>= 1;
74 Dot14 guess = eval_cubic(t, A, B, C);
75 if (x < guess) {
76 t -= dt;
77 } else {
78 t += dt;
79 }
80 }
81
82 // Now we have t, so compute the coeff for Y and evaluate
83 b = pin_and_convert(by);
84 c = pin_and_convert(cy);
85 A = 3 * b;
86 B = 3 * (c - 2 * b);
87 C = 3 * (b - c) + Dot14_ONE;
88 return SkFixedToScalar(eval_cubic(t, A, B, C) << 2);
89}
90
91///////////////////////////////////////////////////////////////////////////////////////////////////
92
93SkiaInterpolatorBase::SkiaInterpolatorBase() {
94 fStorage = nullptr;
95 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -040096}
97
98SkiaInterpolatorBase::~SkiaInterpolatorBase() {
99 if (fStorage) {
Kevin Lubick98046982023-01-05 18:42:26 +0000100 free(fStorage);
Mike Reed74065272021-04-12 09:52:07 -0400101 }
102}
103
104void SkiaInterpolatorBase::reset(int elemCount, int frameCount) {
105 fFlags = 0;
Kevin Lubickd9ccdf62023-01-05 19:07:26 +0000106 fElemCount = static_cast<uint8_t>(elemCount);
107 fFrameCount = static_cast<int16_t>(frameCount);
Mike Reed74065272021-04-12 09:52:07 -0400108 fRepeat = SK_Scalar1;
109 if (fStorage) {
Kevin Lubick98046982023-01-05 18:42:26 +0000110 free(fStorage);
Mike Reed74065272021-04-12 09:52:07 -0400111 fStorage = nullptr;
112 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400113 }
114}
115
116/* Each value[] run is formatted as:
117 <time (in msec)>
118 <blend>
119 <data[fElemCount]>
120
121 Totaling fElemCount+2 entries per keyframe
122*/
123
124bool SkiaInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const {
125 if (fFrameCount == 0) {
126 return false;
127 }
128
129 if (startTime) {
130 *startTime = fTimes[0].fTime;
131 }
132 if (endTime) {
133 *endTime = fTimes[fFrameCount - 1].fTime;
134 }
135 return true;
136}
137
138float SkiaInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime, SkMSec nextTime,
139 const float blend[4]) {
140 SkASSERT(time > prevTime && time < nextTime);
141
142 float t = (float)(time - prevTime) / (float)(nextTime - prevTime);
143 return blend ? SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
144}
145
146SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(SkMSec time, float* T, int* indexPtr,
147 bool* exactPtr) const {
148 SkASSERT(fFrameCount > 0);
149 Result result = kNormal_Result;
150 if (fRepeat != SK_Scalar1) {
151 SkMSec startTime = 0, endTime = 0; // initialize to avoid warning
152 this->getDuration(&startTime, &endTime);
153 SkMSec totalTime = endTime - startTime;
154 SkMSec offsetTime = time - startTime;
155 endTime = SkScalarFloorToInt(fRepeat * totalTime);
156 if (offsetTime >= endTime) {
157 float fraction = SkScalarFraction(fRepeat);
158 offsetTime = fraction == 0 && fRepeat > 0
159 ? totalTime
160 : (SkMSec)SkScalarFloorToInt(fraction * totalTime);
161 result = kFreezeEnd_Result;
162 } else {
163 int mirror = fFlags & kMirror;
164 offsetTime = offsetTime % (totalTime << mirror);
165 if (offsetTime > totalTime) { // can only be true if fMirror is true
166 offsetTime = (totalTime << 1) - offsetTime;
167 }
168 }
169 time = offsetTime + startTime;
170 }
171
172 int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time, sizeof(SkTimeCode));
173
174 bool exact = true;
175
176 if (index < 0) {
177 index = ~index;
178 if (index == 0) {
179 result = kFreezeStart_Result;
180 } else if (index == fFrameCount) {
181 if (fFlags & kReset) {
182 index = 0;
183 } else {
184 index -= 1;
185 }
186 result = kFreezeEnd_Result;
187 } else {
188 exact = false;
189 }
190 }
191 SkASSERT(index < fFrameCount);
192 const SkTimeCode* nextTime = &fTimes[index];
193 SkMSec nextT = nextTime[0].fTime;
194 if (exact) {
195 *T = 0;
196 } else {
197 SkMSec prevT = nextTime[-1].fTime;
198 *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
199 }
200 *indexPtr = index;
201 *exactPtr = exact;
202 return result;
203}
204
205SkiaInterpolator::SkiaInterpolator() {
206 INHERITED::reset(0, 0);
207 fValues = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400208}
209
210SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) {
211 SkASSERT(elemCount > 0);
212 this->reset(elemCount, frameCount);
213}
214
215void SkiaInterpolator::reset(int elemCount, int frameCount) {
216 INHERITED::reset(elemCount, frameCount);
Kevin Lubick98046982023-01-05 18:42:26 +0000217 size_t numBytes = (sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount;
218 fStorage = malloc(numBytes);
219 LOG_ALWAYS_FATAL_IF(!fStorage, "Failed to allocate %zu bytes in %s",
220 numBytes, __func__);
Mike Reed74065272021-04-12 09:52:07 -0400221 fTimes = (SkTimeCode*)fStorage;
222 fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount);
Mike Reed74065272021-04-12 09:52:07 -0400223}
224
225#define SK_Fixed1Third (SK_Fixed1 / 3)
226#define SK_Fixed2Third (SK_Fixed1 * 2 / 3)
227
228static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f};
229
230bool SkiaInterpolator::setKeyFrame(int index, SkMSec time, const float values[],
231 const float blend[4]) {
232 SkASSERT(values != nullptr);
233
234 if (blend == nullptr) {
235 blend = gIdentityBlend;
236 }
237
238 bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode));
239 SkASSERT(success);
240 if (success) {
241 SkTimeCode* timeCode = &fTimes[index];
242 timeCode->fTime = time;
243 memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
244 float* dst = &fValues[fElemCount * index];
245 memcpy(dst, values, fElemCount * sizeof(float));
246 }
247 return success;
248}
249
250SkiaInterpolator::Result SkiaInterpolator::timeToValues(SkMSec time, float values[]) const {
251 float T;
252 int index;
253 bool exact;
254 Result result = timeToT(time, &T, &index, &exact);
255 if (values) {
256 const float* nextSrc = &fValues[index * fElemCount];
257
258 if (exact) {
259 memcpy(values, nextSrc, fElemCount * sizeof(float));
260 } else {
261 SkASSERT(index > 0);
262
263 const float* prevSrc = nextSrc - fElemCount;
264
265 for (int i = fElemCount - 1; i >= 0; --i) {
266 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
267 }
268 }
269 }
270 return result;
271}