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/SkScalar.h"
20 #include "include/core/SkTypes.h"
21 #include "include/private/SkFixed.h"
22 #include "src/core/SkTSearch.h"
23 
24 #include <log/log.h>
25 
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 
Dot14Mul(Dot14 a,Dot14 b)32 static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) {
33     return (a * b + Dot14_HALF) >> 14;
34 }
35 
eval_cubic(Dot14 t,Dot14 A,Dot14 B,Dot14 C)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 
pin_and_convert(float x)40 static inline Dot14 pin_and_convert(float x) {
41     if (x <= 0) {
42         return 0;
43     }
44     if (x >= SK_Scalar1) {
45         return Dot14_ONE;
46     }
47     return SkScalarToFixed(x) >> 2;
48 }
49 
SkUnitCubicInterp(float value,float bx,float by,float cx,float cy)50 static float SkUnitCubicInterp(float value, float bx, float by, float cx, float cy) {
51     // pin to the unit-square, and convert to 2.14
52     Dot14 x = pin_and_convert(value);
53 
54     if (x == 0) return 0;
55     if (x == Dot14_ONE) return SK_Scalar1;
56 
57     Dot14 b = pin_and_convert(bx);
58     Dot14 c = pin_and_convert(cx);
59 
60     // Now compute our coefficients from the control points
61     //  t   -> 3b
62     //  t^2 -> 3c - 6b
63     //  t^3 -> 3b - 3c + 1
64     Dot14 A = 3 * b;
65     Dot14 B = 3 * (c - 2 * b);
66     Dot14 C = 3 * (b - c) + Dot14_ONE;
67 
68     // Now search for a t value given x
69     Dot14 t = Dot14_HALF;
70     Dot14 dt = Dot14_HALF;
71     for (int i = 0; i < 13; i++) {
72         dt >>= 1;
73         Dot14 guess = eval_cubic(t, A, B, C);
74         if (x < guess) {
75             t -= dt;
76         } else {
77             t += dt;
78         }
79     }
80 
81     // Now we have t, so compute the coeff for Y and evaluate
82     b = pin_and_convert(by);
83     c = pin_and_convert(cy);
84     A = 3 * b;
85     B = 3 * (c - 2 * b);
86     C = 3 * (b - c) + Dot14_ONE;
87     return SkFixedToScalar(eval_cubic(t, A, B, C) << 2);
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////////////////////////////
91 
SkiaInterpolatorBase()92 SkiaInterpolatorBase::SkiaInterpolatorBase() {
93     fStorage = nullptr;
94     fTimes = nullptr;
95 }
96 
~SkiaInterpolatorBase()97 SkiaInterpolatorBase::~SkiaInterpolatorBase() {
98     if (fStorage) {
99         free(fStorage);
100     }
101 }
102 
reset(int elemCount,int frameCount)103 void SkiaInterpolatorBase::reset(int elemCount, int frameCount) {
104     fFlags = 0;
105     fElemCount = static_cast<uint8_t>(elemCount);
106     fFrameCount = static_cast<int16_t>(frameCount);
107     fRepeat = SK_Scalar1;
108     if (fStorage) {
109         free(fStorage);
110         fStorage = nullptr;
111         fTimes = nullptr;
112     }
113 }
114 
115 /*  Each value[] run is formatted as:
116         <time (in msec)>
117         <blend>
118         <data[fElemCount]>
119 
120     Totaling fElemCount+2 entries per keyframe
121 */
122 
getDuration(SkMSec * startTime,SkMSec * endTime) const123 bool SkiaInterpolatorBase::getDuration(SkMSec* startTime, SkMSec* endTime) const {
124     if (fFrameCount == 0) {
125         return false;
126     }
127 
128     if (startTime) {
129         *startTime = fTimes[0].fTime;
130     }
131     if (endTime) {
132         *endTime = fTimes[fFrameCount - 1].fTime;
133     }
134     return true;
135 }
136 
ComputeRelativeT(SkMSec time,SkMSec prevTime,SkMSec nextTime,const float blend[4])137 float SkiaInterpolatorBase::ComputeRelativeT(SkMSec time, SkMSec prevTime, SkMSec nextTime,
138                                              const float blend[4]) {
139     SkASSERT(time > prevTime && time < nextTime);
140 
141     float t = (float)(time - prevTime) / (float)(nextTime - prevTime);
142     return blend ? SkUnitCubicInterp(t, blend[0], blend[1], blend[2], blend[3]) : t;
143 }
144 
timeToT(SkMSec time,float * T,int * indexPtr,bool * exactPtr) const145 SkiaInterpolatorBase::Result SkiaInterpolatorBase::timeToT(SkMSec time, float* T, int* indexPtr,
146                                                            bool* exactPtr) const {
147     SkASSERT(fFrameCount > 0);
148     Result result = kNormal_Result;
149     if (fRepeat != SK_Scalar1) {
150         SkMSec startTime = 0, endTime = 0;  // initialize to avoid warning
151         this->getDuration(&startTime, &endTime);
152         SkMSec totalTime = endTime - startTime;
153         SkMSec offsetTime = time - startTime;
154         endTime = SkScalarFloorToInt(fRepeat * totalTime);
155         if (offsetTime >= endTime) {
156             float fraction = SkScalarFraction(fRepeat);
157             offsetTime = fraction == 0 && fRepeat > 0
158                                  ? totalTime
159                                  : (SkMSec)SkScalarFloorToInt(fraction * totalTime);
160             result = kFreezeEnd_Result;
161         } else {
162             int mirror = fFlags & kMirror;
163             offsetTime = offsetTime % (totalTime << mirror);
164             if (offsetTime > totalTime) {  // can only be true if fMirror is true
165                 offsetTime = (totalTime << 1) - offsetTime;
166             }
167         }
168         time = offsetTime + startTime;
169     }
170 
171     int index = SkTSearch<SkMSec>(&fTimes[0].fTime, fFrameCount, time, sizeof(SkTimeCode));
172 
173     bool exact = true;
174 
175     if (index < 0) {
176         index = ~index;
177         if (index == 0) {
178             result = kFreezeStart_Result;
179         } else if (index == fFrameCount) {
180             if (fFlags & kReset) {
181                 index = 0;
182             } else {
183                 index -= 1;
184             }
185             result = kFreezeEnd_Result;
186         } else {
187             exact = false;
188         }
189     }
190     SkASSERT(index < fFrameCount);
191     const SkTimeCode* nextTime = &fTimes[index];
192     SkMSec nextT = nextTime[0].fTime;
193     if (exact) {
194         *T = 0;
195     } else {
196         SkMSec prevT = nextTime[-1].fTime;
197         *T = ComputeRelativeT(time, prevT, nextT, nextTime[-1].fBlend);
198     }
199     *indexPtr = index;
200     *exactPtr = exact;
201     return result;
202 }
203 
SkiaInterpolator()204 SkiaInterpolator::SkiaInterpolator() {
205     INHERITED::reset(0, 0);
206     fValues = nullptr;
207 }
208 
SkiaInterpolator(int elemCount,int frameCount)209 SkiaInterpolator::SkiaInterpolator(int elemCount, int frameCount) {
210     SkASSERT(elemCount > 0);
211     this->reset(elemCount, frameCount);
212 }
213 
reset(int elemCount,int frameCount)214 void SkiaInterpolator::reset(int elemCount, int frameCount) {
215     INHERITED::reset(elemCount, frameCount);
216     size_t numBytes = (sizeof(float) * elemCount + sizeof(SkTimeCode)) * frameCount;
217     fStorage = malloc(numBytes);
218     LOG_ALWAYS_FATAL_IF(!fStorage, "Failed to allocate %zu bytes in %s",
219                         numBytes, __func__);
220     fTimes = (SkTimeCode*)fStorage;
221     fValues = (float*)((char*)fStorage + sizeof(SkTimeCode) * frameCount);
222 }
223 
224 #define SK_Fixed1Third (SK_Fixed1 / 3)
225 #define SK_Fixed2Third (SK_Fixed1 * 2 / 3)
226 
227 static const float gIdentityBlend[4] = {0.33333333f, 0.33333333f, 0.66666667f, 0.66666667f};
228 
setKeyFrame(int index,SkMSec time,const float values[],const float blend[4])229 bool SkiaInterpolator::setKeyFrame(int index, SkMSec time, const float values[],
230                                    const float blend[4]) {
231     SkASSERT(values != nullptr);
232 
233     if (blend == nullptr) {
234         blend = gIdentityBlend;
235     }
236 
237     bool success = ~index == SkTSearch<SkMSec>(&fTimes->fTime, index, time, sizeof(SkTimeCode));
238     SkASSERT(success);
239     if (success) {
240         SkTimeCode* timeCode = &fTimes[index];
241         timeCode->fTime = time;
242         memcpy(timeCode->fBlend, blend, sizeof(timeCode->fBlend));
243         float* dst = &fValues[fElemCount * index];
244         memcpy(dst, values, fElemCount * sizeof(float));
245     }
246     return success;
247 }
248 
timeToValues(SkMSec time,float values[]) const249 SkiaInterpolator::Result SkiaInterpolator::timeToValues(SkMSec time, float values[]) const {
250     float T;
251     int index;
252     bool exact;
253     Result result = timeToT(time, &T, &index, &exact);
254     if (values) {
255         const float* nextSrc = &fValues[index * fElemCount];
256 
257         if (exact) {
258             memcpy(values, nextSrc, fElemCount * sizeof(float));
259         } else {
260             SkASSERT(index > 0);
261 
262             const float* prevSrc = nextSrc - fElemCount;
263 
264             for (int i = fElemCount - 1; i >= 0; --i) {
265                 values[i] = SkScalarInterp(prevSrc[i], nextSrc[i], T);
266             }
267         }
268     }
269     return result;
270 }
271