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 | |
| 19 | #include "include/core/SkMath.h" |
Kevin Lubick | 4669124 | 2022-11-29 19:08:17 +0000 | [diff] [blame] | 20 | #include "include/core/SkScalar.h" |
| 21 | #include "include/core/SkTypes.h" |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 22 | #include "include/private/SkFixed.h" |
| 23 | #include "include/private/SkMalloc.h" |
| 24 | #include "include/private/SkTo.h" |
| 25 | #include "src/core/SkTSearch.h" |
| 26 | |
| 27 | typedef int Dot14; |
| 28 | #define Dot14_ONE (1 << 14) |
| 29 | #define Dot14_HALF (1 << 13) |
| 30 | |
| 31 | #define Dot14ToFloat(x) ((x) / 16384.f) |
| 32 | |
| 33 | static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) { |
| 34 | return (a * b + Dot14_HALF) >> 14; |
| 35 | } |
| 36 | |
| 37 | static 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 | |
| 41 | static 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 | |
| 51 | static 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 | |
| 93 | SkiaInterpolatorBase::SkiaInterpolatorBase() { |
| 94 | fStorage = nullptr; |
| 95 | fTimes = nullptr; |
| 96 | SkDEBUGCODE(fTimesArray = nullptr;) |
| 97 | } |
| 98 | |
| 99 | SkiaInterpolatorBase::~SkiaInterpolatorBase() { |
| 100 | if (fStorage) { |
| 101 | sk_free(fStorage); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void SkiaInterpolatorBase::reset(int elemCount, int frameCount) { |
| 106 | fFlags = 0; |
| 107 | fElemCount = SkToU8(elemCount); |
| 108 | fFrameCount = SkToS16(frameCount); |
| 109 | fRepeat = SK_Scalar1; |
| 110 | if (fStorage) { |
| 111 | sk_free(fStorage); |
| 112 | fStorage = nullptr; |
| 113 | fTimes = nullptr; |
| 114 | SkDEBUGCODE(fTimesArray = nullptr); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* Each value[] run is formatted as: |
| 119 | <time (in msec)> |
| 120 | <blend> |
| 121 | <data[fElemCount]> |
| 122 | |
| 123 | Totaling fElemCount+2 entries per keyframe |
| 124 | */ |
| 125 | |
| 126 | bool SkiaInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const { |
| 127 | if (fFrameCount == 0) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (startTime) { |
| 132 | *startTime = fTimes[0].fTime; |
| 133 | } |
| 134 | if (endTime) { |
| 135 | *endTime = fTimes[fFrameCount - 1].fTime; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | float SkiaInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime, SkMSec nextTime, |
| 141 | const float blend[4]) { |
| 142 | SkASSERT(time > prevTime && time < nextTime); |
| 143 | |
| 144 | float t = (float)(time - prevTime) / (float)(nextTime - prevTime); |
| 145 | return blend ? SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t; |
| 146 | } |
| 147 | |
| 148 | SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(SkMSec time, float* T, int* indexPtr, |
| 149 | bool* exactPtr) const { |
| 150 | SkASSERT(fFrameCount > 0); |
| 151 | Result result = kNormal_Result; |
| 152 | if (fRepeat != SK_Scalar1) { |
| 153 | SkMSec startTime = 0, endTime = 0; // initialize to avoid warning |
| 154 | this->getDuration(&startTime, &endTime); |
| 155 | SkMSec totalTime = endTime - startTime; |
| 156 | SkMSec offsetTime = time - startTime; |
| 157 | endTime = SkScalarFloorToInt(fRepeat * totalTime); |
| 158 | if (offsetTime >= endTime) { |
| 159 | float fraction = SkScalarFraction(fRepeat); |
| 160 | offsetTime = fraction == 0 && fRepeat > 0 |
| 161 | ? totalTime |
| 162 | : (SkMSec)SkScalarFloorToInt(fraction * totalTime); |
| 163 | result = kFreezeEnd_Result; |
| 164 | } else { |
| 165 | int mirror = fFlags & kMirror; |
| 166 | offsetTime = offsetTime % (totalTime << mirror); |
| 167 | if (offsetTime > totalTime) { // can only be true if fMirror is true |
| 168 | offsetTime = (totalTime << 1) - offsetTime; |
| 169 | } |
| 170 | } |
| 171 | time = offsetTime + startTime; |
| 172 | } |
| 173 | |
| 174 | int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time, sizeof(SkTimeCode)); |
| 175 | |
| 176 | bool exact = true; |
| 177 | |
| 178 | if (index < 0) { |
| 179 | index = ~index; |
| 180 | if (index == 0) { |
| 181 | result = kFreezeStart_Result; |
| 182 | } else if (index == fFrameCount) { |
| 183 | if (fFlags & kReset) { |
| 184 | index = 0; |
| 185 | } else { |
| 186 | index -= 1; |
| 187 | } |
| 188 | result = kFreezeEnd_Result; |
| 189 | } else { |
| 190 | exact = false; |
| 191 | } |
| 192 | } |
| 193 | SkASSERT(index < fFrameCount); |
| 194 | const SkTimeCode* nextTime = &fTimes[index]; |
| 195 | SkMSec nextT = nextTime[0].fTime; |
| 196 | if (exact) { |
| 197 | *T = 0; |
| 198 | } else { |
| 199 | SkMSec prevT = nextTime[-1].fTime; |
| 200 | *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend); |
| 201 | } |
| 202 | *indexPtr = index; |
| 203 | *exactPtr = exact; |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | SkiaInterpolator::SkiaInterpolator() { |
| 208 | INHERITED::reset(0, 0); |
| 209 | fValues = nullptr; |
| 210 | SkDEBUGCODE(fScalarsArray = nullptr;) |
| 211 | } |
| 212 | |
| 213 | SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) { |
| 214 | SkASSERT(elemCount > 0); |
| 215 | this->reset(elemCount, frameCount); |
| 216 | } |
| 217 | |
| 218 | void SkiaInterpolator::reset(int elemCount, int frameCount) { |
| 219 | INHERITED::reset(elemCount, frameCount); |
| 220 | fStorage = sk_malloc_throw((sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount); |
| 221 | fTimes = (SkTimeCode*)fStorage; |
| 222 | fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount); |
| 223 | #ifdef SK_DEBUG |
| 224 | fTimesArray = (SkTimeCode(*)[10])fTimes; |
| 225 | fScalarsArray = (float(*)[10])fValues; |
| 226 | #endif |
| 227 | } |
| 228 | |
| 229 | #define SK_Fixed1Third (SK_Fixed1 / 3) |
| 230 | #define SK_Fixed2Third (SK_Fixed1 * 2 / 3) |
| 231 | |
| 232 | static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f}; |
| 233 | |
| 234 | bool SkiaInterpolator::setKeyFrame(int index, SkMSec time, const float values[], |
| 235 | const float blend[4]) { |
| 236 | SkASSERT(values != nullptr); |
| 237 | |
| 238 | if (blend == nullptr) { |
| 239 | blend = gIdentityBlend; |
| 240 | } |
| 241 | |
| 242 | bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode)); |
| 243 | SkASSERT(success); |
| 244 | if (success) { |
| 245 | SkTimeCode* timeCode = &fTimes[index]; |
| 246 | timeCode->fTime = time; |
| 247 | memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend)); |
| 248 | float* dst = &fValues[fElemCount * index]; |
| 249 | memcpy(dst, values, fElemCount * sizeof(float)); |
| 250 | } |
| 251 | return success; |
| 252 | } |
| 253 | |
| 254 | SkiaInterpolator::Result SkiaInterpolator::timeToValues(SkMSec time, float values[]) const { |
| 255 | float T; |
| 256 | int index; |
| 257 | bool exact; |
| 258 | Result result = timeToT(time, &T, &index, &exact); |
| 259 | if (values) { |
| 260 | const float* nextSrc = &fValues[index * fElemCount]; |
| 261 | |
| 262 | if (exact) { |
| 263 | memcpy(values, nextSrc, fElemCount * sizeof(float)); |
| 264 | } else { |
| 265 | SkASSERT(index > 0); |
| 266 | |
| 267 | const float* prevSrc = nextSrc - fElemCount; |
| 268 | |
| 269 | for (int i = fElemCount - 1; i >= 0; --i) { |
| 270 | values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | return result; |
| 275 | } |