3 * Copyright 2008 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkInterpolator.h"
12 #include "SkTSearch.h"
14 SkInterpolatorBase::SkInterpolatorBase() {
17 SkDEBUGCODE(fTimesArray = NULL;)
20 SkInterpolatorBase::~SkInterpolatorBase() {
26 void SkInterpolatorBase::reset(int elemCount, int frameCount) {
28 fElemCount = SkToU8(elemCount);
29 fFrameCount = SkToS16(frameCount);
35 SkDEBUGCODE(fTimesArray = NULL);
39 /* Each value[] run is formated as:
44 Totaling fElemCount+2 entries per keyframe
47 bool SkInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const {
48 if (fFrameCount == 0) {
53 *startTime = fTimes[0].fTime;
56 *endTime = fTimes[fFrameCount - 1].fTime;
61 SkScalar SkInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime,
62 SkMSec nextTime, const SkScalar blend[4]) {
63 SkASSERT(time > prevTime && time < nextTime);
65 SkScalar t = SkScalarDiv((SkScalar)(time - prevTime),
66 (SkScalar)(nextTime - prevTime));
68 SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
71 SkInterpolatorBase::Result SkInterpolatorBase::timeToT(SkMSec time, SkScalar* T,
72 int* indexPtr, SkBool* exactPtr) const {
73 SkASSERT(fFrameCount > 0);
74 Result result = kNormal_Result;
75 if (fRepeat != SK_Scalar1) {
76 SkMSec startTime = 0, endTime = 0; // initialize to avoid warning
77 this->getDuration(&startTime, &endTime);
78 SkMSec totalTime = endTime - startTime;
79 SkMSec offsetTime = time - startTime;
80 endTime = SkScalarFloorToInt(fRepeat * totalTime);
81 if (offsetTime >= endTime) {
82 SkScalar fraction = SkScalarFraction(fRepeat);
83 offsetTime = fraction == 0 && fRepeat > 0 ? totalTime :
84 (SkMSec) SkScalarFloorToInt(fraction * totalTime);
85 result = kFreezeEnd_Result;
87 int mirror = fFlags & kMirror;
88 offsetTime = offsetTime % (totalTime << mirror);
89 if (offsetTime > totalTime) { // can only be true if fMirror is true
90 offsetTime = (totalTime << 1) - offsetTime;
93 time = offsetTime + startTime;
96 int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time,
104 result = kFreezeStart_Result;
105 } else if (index == fFrameCount) {
106 if (fFlags & kReset) {
111 result = kFreezeEnd_Result;
116 SkASSERT(index < fFrameCount);
117 const SkTimeCode* nextTime = &fTimes[index];
118 SkMSec nextT = nextTime[0].fTime;
122 SkMSec prevT = nextTime[-1].fTime;
123 *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
131 SkInterpolator::SkInterpolator() {
132 INHERITED::reset(0, 0);
134 SkDEBUGCODE(fScalarsArray = NULL;)
137 SkInterpolator::SkInterpolator(int elemCount, int frameCount) {
138 SkASSERT(elemCount > 0);
139 this->reset(elemCount, frameCount);
142 void SkInterpolator::reset(int elemCount, int frameCount) {
143 INHERITED::reset(elemCount, frameCount);
144 fStorage = sk_malloc_throw((sizeof(SkScalar) * elemCount +
145 sizeof(SkTimeCode)) * frameCount);
146 fTimes = (SkTimeCode*) fStorage;
147 fValues = (SkScalar*) ((char*) fStorage + sizeof(SkTimeCode) * frameCount);
149 fTimesArray = (SkTimeCode(*)[10]) fTimes;
150 fScalarsArray = (SkScalar(*)[10]) fValues;
154 #define SK_Fixed1Third (SK_Fixed1/3)
155 #define SK_Fixed2Third (SK_Fixed1*2/3)
157 static const SkScalar gIdentityBlend[4] = {
158 0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f
161 bool SkInterpolator::setKeyFrame(int index, SkMSec time,
162 const SkScalar values[], const SkScalar blend[4]) {
163 SkASSERT(values != NULL);
166 blend = gIdentityBlend;
169 bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time,
173 SkTimeCode* timeCode = &fTimes[index];
174 timeCode->fTime = time;
175 memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
176 SkScalar* dst = &fValues[fElemCount * index];
177 memcpy(dst, values, fElemCount * sizeof(SkScalar));
182 SkInterpolator::Result SkInterpolator::timeToValues(SkMSec time,
183 SkScalar values[]) const {
187 Result result = timeToT(time, &T, &index, &exact);
189 const SkScalar* nextSrc = &fValues[index * fElemCount];
192 memcpy(values, nextSrc, fElemCount * sizeof(SkScalar));
196 const SkScalar* prevSrc = nextSrc - fElemCount;
198 for (int i = fElemCount - 1; i >= 0; --i) {
199 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
206 ///////////////////////////////////////////////////////////////////////////////
209 #define Dot14_ONE (1 << 14)
210 #define Dot14_HALF (1 << 13)
212 #define Dot14ToFloat(x) ((x) / 16384.f)
214 static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) {
215 return (a * b + Dot14_HALF) >> 14;
218 static inline Dot14 eval_cubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) {
219 return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t);
222 static inline Dot14 pin_and_convert(SkScalar x) {
226 if (x >= SK_Scalar1) {
229 return SkScalarToFixed(x) >> 2;
232 SkScalar SkUnitCubicInterp(SkScalar value, SkScalar bx, SkScalar by,
233 SkScalar cx, SkScalar cy) {
234 // pin to the unit-square, and convert to 2.14
235 Dot14 x = pin_and_convert(value);
237 if (x == 0) return 0;
238 if (x == Dot14_ONE) return SK_Scalar1;
240 Dot14 b = pin_and_convert(bx);
241 Dot14 c = pin_and_convert(cx);
243 // Now compute our coefficients from the control points
246 // t^3 -> 3b - 3c + 1
248 Dot14 B = 3*(c - 2*b);
249 Dot14 C = 3*(b - c) + Dot14_ONE;
251 // Now search for a t value given x
252 Dot14 t = Dot14_HALF;
253 Dot14 dt = Dot14_HALF;
254 for (int i = 0; i < 13; i++) {
256 Dot14 guess = eval_cubic(t, A, B, C);
264 // Now we have t, so compute the coeff for Y and evaluate
265 b = pin_and_convert(by);
266 c = pin_and_convert(cy);
269 C = 3*(b - c) + Dot14_ONE;
270 return SkFixedToScalar(eval_cubic(t, A, B, C) << 2);