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" |
| 20 | #include "include/private/SkFixed.h" |
| 21 | #include "include/private/SkMalloc.h" |
| 22 | #include "include/private/SkTo.h" |
| 23 | #include "src/core/SkTSearch.h" |
| 24 | |
| 25 | typedef int Dot14; |
| 26 | #define Dot14_ONE (1 << 14) |
| 27 | #define Dot14_HALF (1 << 13) |
| 28 | |
| 29 | #define Dot14ToFloat(x) ((x) / 16384.f) |
| 30 | |
| 31 | static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) { |
| 32 | return (a * b + Dot14_HALF) >> 14; |
| 33 | } |
| 34 | |
| 35 | static inline Dot14 eval_cubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) { |
| 36 | return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t); |
| 37 | } |
| 38 | |
| 39 | static inline Dot14 pin_and_convert(float x) { |
| 40 | if (x <= 0) { |
| 41 | return 0; |
| 42 | } |
| 43 | if (x >= SK_Scalar1) { |
| 44 | return Dot14_ONE; |
| 45 | } |
| 46 | return SkScalarToFixed(x) >> 2; |
| 47 | } |
| 48 | |
| 49 | static float SkUnitCubicInterp(float value, float bx, float by, float cx, float cy) { |
| 50 | // pin to the unit-square, and convert to 2.14 |
| 51 | Dot14 x = pin_and_convert(value); |
| 52 | |
| 53 | if (x == 0) return 0; |
| 54 | if (x == Dot14_ONE) return SK_Scalar1; |
| 55 | |
| 56 | Dot14 b = pin_and_convert(bx); |
| 57 | Dot14 c = pin_and_convert(cx); |
| 58 | |
| 59 | // Now compute our coefficients from the control points |
| 60 | // t -> 3b |
| 61 | // t^2 -> 3c - 6b |
| 62 | // t^3 -> 3b - 3c + 1 |
| 63 | Dot14 A = 3 * b; |
| 64 | Dot14 B = 3 * (c - 2 * b); |
| 65 | Dot14 C = 3 * (b - c) + Dot14_ONE; |
| 66 | |
| 67 | // Now search for a t value given x |
| 68 | Dot14 t = Dot14_HALF; |
| 69 | Dot14 dt = Dot14_HALF; |
| 70 | for (int i = 0; i < 13; i++) { |
| 71 | dt >>= 1; |
| 72 | Dot14 guess = eval_cubic(t, A, B, C); |
| 73 | if (x < guess) { |
| 74 | t -= dt; |
| 75 | } else { |
| 76 | t += dt; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Now we have t, so compute the coeff for Y and evaluate |
| 81 | b = pin_and_convert(by); |
| 82 | c = pin_and_convert(cy); |
| 83 | A = 3 * b; |
| 84 | B = 3 * (c - 2 * b); |
| 85 | C = 3 * (b - c) + Dot14_ONE; |
| 86 | return SkFixedToScalar(eval_cubic(t, A, B, C) << 2); |
| 87 | } |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | SkiaInterpolatorBase::SkiaInterpolatorBase() { |
| 92 | fStorage = nullptr; |
| 93 | fTimes = nullptr; |
| 94 | SkDEBUGCODE(fTimesArray = nullptr;) |
| 95 | } |
| 96 | |
| 97 | SkiaInterpolatorBase::~SkiaInterpolatorBase() { |
| 98 | if (fStorage) { |
| 99 | sk_free(fStorage); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void SkiaInterpolatorBase::reset(int elemCount, int frameCount) { |
| 104 | fFlags = 0; |
| 105 | fElemCount = SkToU8(elemCount); |
| 106 | fFrameCount = SkToS16(frameCount); |
| 107 | fRepeat = SK_Scalar1; |
| 108 | if (fStorage) { |
| 109 | sk_free(fStorage); |
| 110 | fStorage = nullptr; |
| 111 | fTimes = nullptr; |
| 112 | SkDEBUGCODE(fTimesArray = nullptr); |
| 113 | } |
| 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 | |
| 124 | bool 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 | |
| 138 | float 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 | |
| 146 | SkiaInterpolatorBase::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 | |
| 205 | SkiaInterpolator::SkiaInterpolator() { |
| 206 | INHERITED::reset(0, 0); |
| 207 | fValues = nullptr; |
| 208 | SkDEBUGCODE(fScalarsArray = nullptr;) |
| 209 | } |
| 210 | |
| 211 | SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) { |
| 212 | SkASSERT(elemCount > 0); |
| 213 | this->reset(elemCount, frameCount); |
| 214 | } |
| 215 | |
| 216 | void SkiaInterpolator::reset(int elemCount, int frameCount) { |
| 217 | INHERITED::reset(elemCount, frameCount); |
| 218 | fStorage = sk_malloc_throw((sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount); |
| 219 | fTimes = (SkTimeCode*)fStorage; |
| 220 | fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount); |
| 221 | #ifdef SK_DEBUG |
| 222 | fTimesArray = (SkTimeCode(*)[10])fTimes; |
| 223 | fScalarsArray = (float(*)[10])fValues; |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | #define SK_Fixed1Third (SK_Fixed1 / 3) |
| 228 | #define SK_Fixed2Third (SK_Fixed1 * 2 / 3) |
| 229 | |
| 230 | static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f}; |
| 231 | |
| 232 | bool SkiaInterpolator::setKeyFrame(int index, SkMSec time, const float values[], |
| 233 | const float blend[4]) { |
| 234 | SkASSERT(values != nullptr); |
| 235 | |
| 236 | if (blend == nullptr) { |
| 237 | blend = gIdentityBlend; |
| 238 | } |
| 239 | |
| 240 | bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode)); |
| 241 | SkASSERT(success); |
| 242 | if (success) { |
| 243 | SkTimeCode* timeCode = &fTimes[index]; |
| 244 | timeCode->fTime = time; |
| 245 | memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend)); |
| 246 | float* dst = &fValues[fElemCount * index]; |
| 247 | memcpy(dst, values, fElemCount * sizeof(float)); |
| 248 | } |
| 249 | return success; |
| 250 | } |
| 251 | |
| 252 | SkiaInterpolator::Result SkiaInterpolator::timeToValues(SkMSec time, float values[]) const { |
| 253 | float T; |
| 254 | int index; |
| 255 | bool exact; |
| 256 | Result result = timeToT(time, &T, &index, &exact); |
| 257 | if (values) { |
| 258 | const float* nextSrc = &fValues[index * fElemCount]; |
| 259 | |
| 260 | if (exact) { |
| 261 | memcpy(values, nextSrc, fElemCount * sizeof(float)); |
| 262 | } else { |
| 263 | SkASSERT(index > 0); |
| 264 | |
| 265 | const float* prevSrc = nextSrc - fElemCount; |
| 266 | |
| 267 | for (int i = fElemCount - 1; i >= 0; --i) { |
| 268 | values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | return result; |
| 273 | } |