blob: d3c0b26fef1f0992f9bf608adfd79256472984be [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
benjaminwagner6c71e0a2016-04-07 08:49:31 -07009#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkInterpolator.h"
11#include "SkMath.h"
12#include "SkTSearch.h"
13
14SkInterpolatorBase::SkInterpolatorBase() {
halcanary96fcdcc2015-08-27 07:41:13 -070015 fStorage = nullptr;
16 fTimes = nullptr;
17 SkDEBUGCODE(fTimesArray = nullptr;)
reed@android.com8a1c16f2008-12-17 15:59:43 +000018}
19
20SkInterpolatorBase::~SkInterpolatorBase() {
21 if (fStorage) {
22 sk_free(fStorage);
23 }
24}
25
26void SkInterpolatorBase::reset(int elemCount, int frameCount) {
27 fFlags = 0;
28 fElemCount = SkToU8(elemCount);
29 fFrameCount = SkToS16(frameCount);
30 fRepeat = SK_Scalar1;
31 if (fStorage) {
32 sk_free(fStorage);
halcanary96fcdcc2015-08-27 07:41:13 -070033 fStorage = nullptr;
34 fTimes = nullptr;
35 SkDEBUGCODE(fTimesArray = nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 }
37}
38
39/* Each value[] run is formated as:
40 <time (in msec)>
41 <blend>
42 <data[fElemCount]>
43
44 Totaling fElemCount+2 entries per keyframe
45*/
46
47bool SkInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const {
48 if (fFrameCount == 0) {
49 return false;
50 }
51
52 if (startTime) {
53 *startTime = fTimes[0].fTime;
54 }
55 if (endTime) {
56 *endTime = fTimes[fFrameCount - 1].fTime;
57 }
58 return true;
59}
60
61SkScalar SkInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime,
62 SkMSec nextTime, const SkScalar blend[4]) {
63 SkASSERT(time > prevTime && time < nextTime);
64
reed80ea19c2015-05-12 10:37:34 -070065 SkScalar t = (SkScalar)(time - prevTime) / (SkScalar)(nextTime - prevTime);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 return blend ?
67 SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
68}
69
70SkInterpolatorBase::Result SkInterpolatorBase::timeToT(SkMSec time, SkScalar* T,
reed9a878a02015-12-27 12:47:25 -080071 int* indexPtr, bool* exactPtr) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkASSERT(fFrameCount > 0);
73 Result result = kNormal_Result;
74 if (fRepeat != SK_Scalar1) {
reed@android.comf523e252009-01-26 23:15:37 +000075 SkMSec startTime = 0, endTime = 0; // initialize to avoid warning
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 this->getDuration(&startTime, &endTime);
77 SkMSec totalTime = endTime - startTime;
78 SkMSec offsetTime = time - startTime;
reed@google.com8015cdd2013-12-18 15:49:32 +000079 endTime = SkScalarFloorToInt(fRepeat * totalTime);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 if (offsetTime >= endTime) {
81 SkScalar fraction = SkScalarFraction(fRepeat);
82 offsetTime = fraction == 0 && fRepeat > 0 ? totalTime :
reed@google.com8015cdd2013-12-18 15:49:32 +000083 (SkMSec) SkScalarFloorToInt(fraction * totalTime);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 result = kFreezeEnd_Result;
85 } else {
86 int mirror = fFlags & kMirror;
87 offsetTime = offsetTime % (totalTime << mirror);
88 if (offsetTime > totalTime) { // can only be true if fMirror is true
89 offsetTime = (totalTime << 1) - offsetTime;
90 }
91 }
92 time = offsetTime + startTime;
93 }
94
95 int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time,
96 sizeof(SkTimeCode));
97
98 bool exact = true;
99
100 if (index < 0) {
101 index = ~index;
102 if (index == 0) {
103 result = kFreezeStart_Result;
104 } else if (index == fFrameCount) {
105 if (fFlags & kReset) {
106 index = 0;
107 } else {
108 index -= 1;
109 }
110 result = kFreezeEnd_Result;
111 } else {
112 exact = false;
113 }
114 }
115 SkASSERT(index < fFrameCount);
116 const SkTimeCode* nextTime = &fTimes[index];
117 SkMSec nextT = nextTime[0].fTime;
118 if (exact) {
119 *T = 0;
120 } else {
121 SkMSec prevT = nextTime[-1].fTime;
122 *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
123 }
124 *indexPtr = index;
125 *exactPtr = exact;
126 return result;
127}
128
129
130SkInterpolator::SkInterpolator() {
131 INHERITED::reset(0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700132 fValues = nullptr;
133 SkDEBUGCODE(fScalarsArray = nullptr;)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134}
135
136SkInterpolator::SkInterpolator(int elemCount, int frameCount) {
137 SkASSERT(elemCount > 0);
138 this->reset(elemCount, frameCount);
139}
140
141void SkInterpolator::reset(int elemCount, int frameCount) {
142 INHERITED::reset(elemCount, frameCount);
143 fStorage = sk_malloc_throw((sizeof(SkScalar) * elemCount +
144 sizeof(SkTimeCode)) * frameCount);
145 fTimes = (SkTimeCode*) fStorage;
146 fValues = (SkScalar*) ((char*) fStorage + sizeof(SkTimeCode) * frameCount);
147#ifdef SK_DEBUG
148 fTimesArray = (SkTimeCode(*)[10]) fTimes;
149 fScalarsArray = (SkScalar(*)[10]) fValues;
150#endif
151}
152
153#define SK_Fixed1Third (SK_Fixed1/3)
154#define SK_Fixed2Third (SK_Fixed1*2/3)
155
156static const SkScalar gIdentityBlend[4] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158};
159
160bool SkInterpolator::setKeyFrame(int index, SkMSec time,
161 const SkScalar values[], const SkScalar blend[4]) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 SkASSERT(values != nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163
halcanary96fcdcc2015-08-27 07:41:13 -0700164 if (blend == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 blend = gIdentityBlend;
166 }
167
168 bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time,
169 sizeof(SkTimeCode));
170 SkASSERT(success);
171 if (success) {
172 SkTimeCode* timeCode = &fTimes[index];
173 timeCode->fTime = time;
174 memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
175 SkScalar* dst = &fValues[fElemCount * index];
176 memcpy(dst, values, fElemCount * sizeof(SkScalar));
177 }
178 return success;
179}
180
181SkInterpolator::Result SkInterpolator::timeToValues(SkMSec time,
182 SkScalar values[]) const {
183 SkScalar T;
184 int index;
reed9a878a02015-12-27 12:47:25 -0800185 bool exact;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 Result result = timeToT(time, &T, &index, &exact);
187 if (values) {
188 const SkScalar* nextSrc = &fValues[index * fElemCount];
189
190 if (exact) {
191 memcpy(values, nextSrc, fElemCount * sizeof(SkScalar));
192 } else {
193 SkASSERT(index > 0);
194
195 const SkScalar* prevSrc = nextSrc - fElemCount;
196
197 for (int i = fElemCount - 1; i >= 0; --i) {
198 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
199 }
200 }
201 }
202 return result;
203}
204
205///////////////////////////////////////////////////////////////////////////////
206
207typedef int Dot14;
208#define Dot14_ONE (1 << 14)
209#define Dot14_HALF (1 << 13)
210
211#define Dot14ToFloat(x) ((x) / 16384.f)
212
213static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) {
214 return (a * b + Dot14_HALF) >> 14;
215}
216
217static inline Dot14 eval_cubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) {
218 return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t);
219}
220
221static inline Dot14 pin_and_convert(SkScalar x) {
222 if (x <= 0) {
223 return 0;
224 }
225 if (x >= SK_Scalar1) {
226 return Dot14_ONE;
227 }
228 return SkScalarToFixed(x) >> 2;
229}
230
231SkScalar SkUnitCubicInterp(SkScalar value, SkScalar bx, SkScalar by,
232 SkScalar cx, SkScalar cy) {
233 // pin to the unit-square, and convert to 2.14
234 Dot14 x = pin_and_convert(value);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 if (x == 0) return 0;
237 if (x == Dot14_ONE) return SK_Scalar1;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 Dot14 b = pin_and_convert(bx);
240 Dot14 c = pin_and_convert(cx);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 // Now compute our coefficients from the control points
243 // t -> 3b
244 // t^2 -> 3c - 6b
245 // t^3 -> 3b - 3c + 1
246 Dot14 A = 3*b;
247 Dot14 B = 3*(c - 2*b);
248 Dot14 C = 3*(b - c) + Dot14_ONE;
249
250 // Now search for a t value given x
251 Dot14 t = Dot14_HALF;
252 Dot14 dt = Dot14_HALF;
253 for (int i = 0; i < 13; i++) {
254 dt >>= 1;
255 Dot14 guess = eval_cubic(t, A, B, C);
256 if (x < guess) {
257 t -= dt;
258 } else {
259 t += dt;
260 }
261 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 // Now we have t, so compute the coeff for Y and evaluate
264 b = pin_and_convert(by);
265 c = pin_and_convert(cy);
266 A = 3*b;
267 B = 3*(c - 2*b);
268 C = 3*(b - c) + Dot14_ONE;
269 return SkFixedToScalar(eval_cubic(t, A, B, C) << 2);
270}