Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 1 | /* |
| 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 Lubick | 4669124 | 2022-11-29 19:08:17 +0000 | [diff] [blame] | 19 | #include "include/core/SkScalar.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 21 | |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 22 | #include <cstdlib> |
Nolan Scobie | d84b3da | 2024-04-18 20:49:08 +0000 | [diff] [blame] | 23 | #include <cstring> |
Kevin Lubick | 9804698 | 2023-01-05 18:42:26 +0000 | [diff] [blame] | 24 | #include <log/log.h> |
| 25 | |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 26 | typedef int Dot14; |
| 27 | #define Dot14_ONE (1 << 14) |
| 28 | #define Dot14_HALF (1 << 13) |
| 29 | |
| 30 | #define Dot14ToFloat(x) ((x) / 16384.f) |
| 31 | |
| 32 | static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) { |
| 33 | return (a * b + Dot14_HALF) >> 14; |
| 34 | } |
| 35 | |
| 36 | static 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 | |
| 40 | static inline Dot14 pin_and_convert(float x) { |
| 41 | if (x <= 0) { |
| 42 | return 0; |
| 43 | } |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 44 | if (x >= 1.0f) { |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 45 | return Dot14_ONE; |
| 46 | } |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 47 | return static_cast<Dot14>(x * Dot14_ONE); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 50 | using MSec = uint32_t; // millisecond duration |
| 51 | |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 52 | static 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 56 | if (x == 0) return 0.0f; |
| 57 | if (x == Dot14_ONE) return 1.0f; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 58 | |
| 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 89 | return Dot14ToFloat(eval_cubic(t, A, B, C)); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | SkiaInterpolatorBase::SkiaInterpolatorBase() { |
| 95 | fStorage = nullptr; |
| 96 | fTimes = nullptr; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | SkiaInterpolatorBase::~SkiaInterpolatorBase() { |
| 100 | if (fStorage) { |
Kevin Lubick | 9804698 | 2023-01-05 18:42:26 +0000 | [diff] [blame] | 101 | free(fStorage); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | void SkiaInterpolatorBase::reset(int elemCount, int frameCount) { |
| 106 | fFlags = 0; |
Kevin Lubick | d9ccdf6 | 2023-01-05 19:07:26 +0000 | [diff] [blame] | 107 | fElemCount = static_cast<uint8_t>(elemCount); |
| 108 | fFrameCount = static_cast<int16_t>(frameCount); |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 109 | fRepeat = 1.0f; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 110 | if (fStorage) { |
Kevin Lubick | 9804698 | 2023-01-05 18:42:26 +0000 | [diff] [blame] | 111 | free(fStorage); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 112 | fStorage = nullptr; |
| 113 | fTimes = nullptr; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 114 | } |
| 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 125 | bool SkiaInterpolatorBase::getDuration(MSec* startTime, MSec* endTime) const { |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 126 | 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 139 | float SkiaInterpolatorBase::ComputeRelativeT(MSec time, MSec prevTime, MSec nextTime, |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 140 | const float blend[4]) { |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 141 | LOG_FATAL_IF(time < prevTime || time > nextTime); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 142 | |
| 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 147 | // 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 149 | int SkiaInterpolatorBase::binarySearch(const SkTimeCode* arr, int count, MSec target) { |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 150 | 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 159 | MSec elem = arr[mid].fTime; |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 160 | 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 176 | SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(MSec time, float* T, int* indexPtr, |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 177 | bool* exactPtr) const { |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 178 | LOG_FATAL_IF(fFrameCount <= 0); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 179 | Result result = kNormal_Result; |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 180 | if (fRepeat != 1.0f) { |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 181 | MSec startTime = 0, endTime = 0; // initialize to avoid warning |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 182 | this->getDuration(&startTime, &endTime); |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 183 | MSec totalTime = endTime - startTime; |
| 184 | MSec offsetTime = time - startTime; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 185 | endTime = SkScalarFloorToInt(fRepeat * totalTime); |
| 186 | if (offsetTime >= endTime) { |
| 187 | float fraction = SkScalarFraction(fRepeat); |
| 188 | offsetTime = fraction == 0 && fRepeat > 0 |
| 189 | ? totalTime |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 190 | : (MSec)SkScalarFloorToInt(fraction * totalTime); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 191 | 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 202 | int index = SkiaInterpolatorBase::binarySearch(fTimes, fFrameCount, time); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 203 | bool exact = true; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 204 | 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 216 | // Need to interpolate between two frames. |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 217 | exact = false; |
| 218 | } |
| 219 | } |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 220 | LOG_FATAL_IF(index >= fFrameCount); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 221 | const SkTimeCode* nextTime = &fTimes[index]; |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 222 | MSec nextT = nextTime[0].fTime; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 223 | if (exact) { |
| 224 | *T = 0; |
| 225 | } else { |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 226 | MSec prevT = nextTime[-1].fTime; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 227 | *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend); |
| 228 | } |
| 229 | *indexPtr = index; |
| 230 | *exactPtr = exact; |
| 231 | return result; |
| 232 | } |
| 233 | |
| 234 | SkiaInterpolator::SkiaInterpolator() { |
| 235 | INHERITED::reset(0, 0); |
| 236 | fValues = nullptr; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) { |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 240 | LOG_FATAL_IF(elemCount <= 0); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 241 | this->reset(elemCount, frameCount); |
| 242 | } |
| 243 | |
| 244 | void SkiaInterpolator::reset(int elemCount, int frameCount) { |
| 245 | INHERITED::reset(elemCount, frameCount); |
Kevin Lubick | 9804698 | 2023-01-05 18:42:26 +0000 | [diff] [blame] | 246 | 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 Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 250 | fTimes = (SkTimeCode*)fStorage; |
| 251 | fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 254 | static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f}; |
| 255 | |
Kaylee Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 256 | bool SkiaInterpolator::setKeyFrame(int index, MSec time, const float values[], |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 257 | const float blend[4]) { |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 258 | LOG_FATAL_IF(values == nullptr); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 259 | |
| 260 | if (blend == nullptr) { |
| 261 | blend = gIdentityBlend; |
| 262 | } |
| 263 | |
Kevin Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 264 | // 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 Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 267 | 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 Lubick | 84642f8 | 2024-09-09 19:44:27 +0000 | [diff] [blame] | 277 | SkiaInterpolator::Result SkiaInterpolator::timeToValues(MSec time, float values[]) const { |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 278 | 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 Lubick | 963ce9f | 2023-02-17 12:51:14 +0000 | [diff] [blame] | 288 | LOG_FATAL_IF(index <= 0); |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 289 | |
| 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 | } |