blob: 8deac61e8baa7b6425c58288290b73c0d0aacdd3 [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
Kevin Lubick46691242022-11-29 19:08:17 +000019#include "include/core/SkScalar.h"
20#include "include/core/SkTypes.h"
Mike Reed74065272021-04-12 09:52:07 -040021
Kevin Lubick963ce9f2023-02-17 12:51:14 +000022#include <cstdlib>
Nolan Scobied84b3da2024-04-18 20:49:08 +000023#include <cstring>
Kevin Lubick98046982023-01-05 18:42:26 +000024#include <log/log.h>
25
Mike Reed74065272021-04-12 09:52:07 -040026typedef 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 }
Kevin Lubick963ce9f2023-02-17 12:51:14 +000044 if (x >= 1.0f) {
Mike Reed74065272021-04-12 09:52:07 -040045 return Dot14_ONE;
46 }
Kevin Lubick963ce9f2023-02-17 12:51:14 +000047 return static_cast<Dot14>(x * Dot14_ONE);
Mike Reed74065272021-04-12 09:52:07 -040048}
49
Kaylee Lubick84642f82024-09-09 19:44:27 +000050using MSec = uint32_t; // millisecond duration
51
Mike Reed74065272021-04-12 09:52:07 -040052static float SkUnitCubicInterp(float value, float bx, float by, float cx, float cy) {
53 // pin to the unit-square, and convert to 2.14
54 Dot14 x = pin_and_convert(value);
55
Kevin Lubick963ce9f2023-02-17 12:51:14 +000056 if (x == 0) return 0.0f;
57 if (x == Dot14_ONE) return 1.0f;
Mike Reed74065272021-04-12 09:52:07 -040058
59 Dot14 b = pin_and_convert(bx);
60 Dot14 c = pin_and_convert(cx);
61
62 // Now compute our coefficients from the control points
63 // t -> 3b
64 // t^2 -> 3c - 6b
65 // t^3 -> 3b - 3c + 1
66 Dot14 A = 3 * b;
67 Dot14 B = 3 * (c - 2 * b);
68 Dot14 C = 3 * (b - c) + Dot14_ONE;
69
70 // Now search for a t value given x
71 Dot14 t = Dot14_HALF;
72 Dot14 dt = Dot14_HALF;
73 for (int i = 0; i < 13; i++) {
74 dt >>= 1;
75 Dot14 guess = eval_cubic(t, A, B, C);
76 if (x < guess) {
77 t -= dt;
78 } else {
79 t += dt;
80 }
81 }
82
83 // Now we have t, so compute the coeff for Y and evaluate
84 b = pin_and_convert(by);
85 c = pin_and_convert(cy);
86 A = 3 * b;
87 B = 3 * (c - 2 * b);
88 C = 3 * (b - c) + Dot14_ONE;
Kevin Lubick963ce9f2023-02-17 12:51:14 +000089 return Dot14ToFloat(eval_cubic(t, A, B, C));
Mike Reed74065272021-04-12 09:52:07 -040090}
91
92///////////////////////////////////////////////////////////////////////////////////////////////////
93
94SkiaInterpolatorBase::SkiaInterpolatorBase() {
95 fStorage = nullptr;
96 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -040097}
98
99SkiaInterpolatorBase::~SkiaInterpolatorBase() {
100 if (fStorage) {
Kevin Lubick98046982023-01-05 18:42:26 +0000101 free(fStorage);
Mike Reed74065272021-04-12 09:52:07 -0400102 }
103}
104
105void SkiaInterpolatorBase::reset(int elemCount, int frameCount) {
106 fFlags = 0;
Kevin Lubickd9ccdf62023-01-05 19:07:26 +0000107 fElemCount = static_cast<uint8_t>(elemCount);
108 fFrameCount = static_cast<int16_t>(frameCount);
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000109 fRepeat = 1.0f;
Mike Reed74065272021-04-12 09:52:07 -0400110 if (fStorage) {
Kevin Lubick98046982023-01-05 18:42:26 +0000111 free(fStorage);
Mike Reed74065272021-04-12 09:52:07 -0400112 fStorage = nullptr;
113 fTimes = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400114 }
115}
116
117/* Each value[] run is formatted as:
118 <time (in msec)>
119 <blend>
120 <data[fElemCount]>
121
122 Totaling fElemCount+2 entries per keyframe
123*/
124
Kaylee Lubick84642f82024-09-09 19:44:27 +0000125bool SkiaInterpolatorBase::getDuration(MSec* startTime, MSec* endTime) const {
Mike Reed74065272021-04-12 09:52:07 -0400126 if (fFrameCount == 0) {
127 return false;
128 }
129
130 if (startTime) {
131 *startTime = fTimes[0].fTime;
132 }
133 if (endTime) {
134 *endTime = fTimes[fFrameCount - 1].fTime;
135 }
136 return true;
137}
138
Kaylee Lubick84642f82024-09-09 19:44:27 +0000139float SkiaInterpolatorBase::ComputeRelativeT(MSec time, MSec prevTime, MSec nextTime,
Mike Reed74065272021-04-12 09:52:07 -0400140 const float blend[4]) {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000141 LOG_FATAL_IF(time < prevTime || time > nextTime);
Mike Reed74065272021-04-12 09:52:07 -0400142
143 float t = (float)(time - prevTime) / (float)(nextTime - prevTime);
144 return blend ? SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
145}
146
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000147// Returns the index of where the item is or the bit not of the index
148// where the item should go in order to keep arr sorted in ascending order.
Kaylee Lubick84642f82024-09-09 19:44:27 +0000149int SkiaInterpolatorBase::binarySearch(const SkTimeCode* arr, int count, MSec target) {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000150 if (count <= 0) {
151 return ~0;
152 }
153
154 int lo = 0;
155 int hi = count - 1;
156
157 while (lo < hi) {
158 int mid = (hi + lo) / 2;
Kaylee Lubick84642f82024-09-09 19:44:27 +0000159 MSec elem = arr[mid].fTime;
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000160 if (elem == target) {
161 return mid;
162 } else if (elem < target) {
163 lo = mid + 1;
164 } else {
165 hi = mid;
166 }
167 }
168 // Check to see if target is greater or less than where we stopped
169 if (target < arr[lo].fTime) {
170 return ~lo;
171 }
172 // e.g. it should go at the end.
173 return ~(lo + 1);
174}
175
Kaylee Lubick84642f82024-09-09 19:44:27 +0000176SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(MSec time, float* T, int* indexPtr,
Mike Reed74065272021-04-12 09:52:07 -0400177 bool* exactPtr) const {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000178 LOG_FATAL_IF(fFrameCount <= 0);
Mike Reed74065272021-04-12 09:52:07 -0400179 Result result = kNormal_Result;
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000180 if (fRepeat != 1.0f) {
Kaylee Lubick84642f82024-09-09 19:44:27 +0000181 MSec startTime = 0, endTime = 0; // initialize to avoid warning
Mike Reed74065272021-04-12 09:52:07 -0400182 this->getDuration(&startTime, &endTime);
Kaylee Lubick84642f82024-09-09 19:44:27 +0000183 MSec totalTime = endTime - startTime;
184 MSec offsetTime = time - startTime;
Mike Reed74065272021-04-12 09:52:07 -0400185 endTime = SkScalarFloorToInt(fRepeat * totalTime);
186 if (offsetTime >= endTime) {
187 float fraction = SkScalarFraction(fRepeat);
188 offsetTime = fraction == 0 && fRepeat > 0
189 ? totalTime
Kaylee Lubick84642f82024-09-09 19:44:27 +0000190 : (MSec)SkScalarFloorToInt(fraction * totalTime);
Mike Reed74065272021-04-12 09:52:07 -0400191 result = kFreezeEnd_Result;
192 } else {
193 int mirror = fFlags & kMirror;
194 offsetTime = offsetTime % (totalTime << mirror);
195 if (offsetTime > totalTime) { // can only be true if fMirror is true
196 offsetTime = (totalTime << 1) - offsetTime;
197 }
198 }
199 time = offsetTime + startTime;
200 }
201
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000202 int index = SkiaInterpolatorBase::binarySearch(fTimes, fFrameCount, time);
Mike Reed74065272021-04-12 09:52:07 -0400203 bool exact = true;
Mike Reed74065272021-04-12 09:52:07 -0400204 if (index < 0) {
205 index = ~index;
206 if (index == 0) {
207 result = kFreezeStart_Result;
208 } else if (index == fFrameCount) {
209 if (fFlags & kReset) {
210 index = 0;
211 } else {
212 index -= 1;
213 }
214 result = kFreezeEnd_Result;
215 } else {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000216 // Need to interpolate between two frames.
Mike Reed74065272021-04-12 09:52:07 -0400217 exact = false;
218 }
219 }
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000220 LOG_FATAL_IF(index >= fFrameCount);
Mike Reed74065272021-04-12 09:52:07 -0400221 const SkTimeCode* nextTime = &fTimes[index];
Kaylee Lubick84642f82024-09-09 19:44:27 +0000222 MSec nextT = nextTime[0].fTime;
Mike Reed74065272021-04-12 09:52:07 -0400223 if (exact) {
224 *T = 0;
225 } else {
Kaylee Lubick84642f82024-09-09 19:44:27 +0000226 MSec prevT = nextTime[-1].fTime;
Mike Reed74065272021-04-12 09:52:07 -0400227 *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
228 }
229 *indexPtr = index;
230 *exactPtr = exact;
231 return result;
232}
233
234SkiaInterpolator::SkiaInterpolator() {
235 INHERITED::reset(0, 0);
236 fValues = nullptr;
Mike Reed74065272021-04-12 09:52:07 -0400237}
238
239SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000240 LOG_FATAL_IF(elemCount <= 0);
Mike Reed74065272021-04-12 09:52:07 -0400241 this->reset(elemCount, frameCount);
242}
243
244void SkiaInterpolator::reset(int elemCount, int frameCount) {
245 INHERITED::reset(elemCount, frameCount);
Kevin Lubick98046982023-01-05 18:42:26 +0000246 size_t numBytes = (sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount;
247 fStorage = malloc(numBytes);
248 LOG_ALWAYS_FATAL_IF(!fStorage, "Failed to allocate %zu bytes in %s",
249 numBytes, __func__);
Mike Reed74065272021-04-12 09:52:07 -0400250 fTimes = (SkTimeCode*)fStorage;
251 fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount);
Mike Reed74065272021-04-12 09:52:07 -0400252}
253
Mike Reed74065272021-04-12 09:52:07 -0400254static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f};
255
Kaylee Lubick84642f82024-09-09 19:44:27 +0000256bool SkiaInterpolator::setKeyFrame(int index, MSec time, const float values[],
Mike Reed74065272021-04-12 09:52:07 -0400257 const float blend[4]) {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000258 LOG_FATAL_IF(values == nullptr);
Mike Reed74065272021-04-12 09:52:07 -0400259
260 if (blend == nullptr) {
261 blend = gIdentityBlend;
262 }
263
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000264 // Verify the time should go after all the frames before index
265 bool success = ~index == SkiaInterpolatorBase::binarySearch(fTimes, index, time);
266 LOG_FATAL_IF(!success);
Mike Reed74065272021-04-12 09:52:07 -0400267 if (success) {
268 SkTimeCode* timeCode = &fTimes[index];
269 timeCode->fTime = time;
270 memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
271 float* dst = &fValues[fElemCount * index];
272 memcpy(dst, values, fElemCount * sizeof(float));
273 }
274 return success;
275}
276
Kaylee Lubick84642f82024-09-09 19:44:27 +0000277SkiaInterpolator::Result SkiaInterpolator::timeToValues(MSec time, float values[]) const {
Mike Reed74065272021-04-12 09:52:07 -0400278 float T;
279 int index;
280 bool exact;
281 Result result = timeToT(time, &T, &index, &exact);
282 if (values) {
283 const float* nextSrc = &fValues[index * fElemCount];
284
285 if (exact) {
286 memcpy(values, nextSrc, fElemCount * sizeof(float));
287 } else {
Kevin Lubick963ce9f2023-02-17 12:51:14 +0000288 LOG_FATAL_IF(index <= 0);
Mike Reed74065272021-04-12 09:52:07 -0400289
290 const float* prevSrc = nextSrc - fElemCount;
291
292 for (int i = fElemCount - 1; i >= 0; --i) {
293 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
294 }
295 }
296 }
297 return result;
298}