1 /* 2 * Copyright (C) 2006 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 #ifndef SkiaInterpolator_DEFINED 18 #define SkiaInterpolator_DEFINED 19 20 #include "include/private/SkTo.h" 21 22 class SkiaInterpolatorBase { 23 public: 24 enum Result { kNormal_Result, kFreezeStart_Result, kFreezeEnd_Result }; 25 26 protected: 27 SkiaInterpolatorBase(); 28 ~SkiaInterpolatorBase(); 29 30 public: 31 void reset(int elemCount, int frameCount); 32 33 /** Return the start and end time for this interpolator. 34 If there are no key frames, return false. 35 @param startTime If not null, returns the time (in milliseconds) of the 36 first keyframe. If there are no keyframes, this param 37 is ignored (left unchanged). 38 @param endTime If not null, returns the time (in milliseconds) of the 39 last keyframe. If there are no keyframes, this parameter 40 is ignored (left unchanged). 41 @return True if there are key frames, or false if there are none. 42 */ 43 bool getDuration(uint32_t* startTime, uint32_t* endTime) const; 44 45 /** Set the whether the repeat is mirrored. 46 @param mirror If true, the odd repeats interpolate from the last key 47 frame and the first. 48 */ setMirror(bool mirror)49 void setMirror(bool mirror) { fFlags = SkToU8((fFlags & ~kMirror) | (int)mirror); } 50 51 /** Set the repeat count. The repeat count may be fractional. 52 @param repeatCount Multiplies the total time by this scalar. 53 */ setRepeatCount(float repeatCount)54 void setRepeatCount(float repeatCount) { fRepeat = repeatCount; } 55 56 /** Set the whether the repeat is mirrored. 57 @param reset If true, the odd repeats interpolate from the last key 58 frame and the first. 59 */ setReset(bool reset)60 void setReset(bool reset) { fFlags = SkToU8((fFlags & ~kReset) | (int)reset); } 61 62 Result timeToT(uint32_t time, float* T, int* index, bool* exact) const; 63 64 protected: 65 enum Flags { kMirror = 1, kReset = 2, kHasBlend = 4 }; 66 static float ComputeRelativeT(uint32_t time, uint32_t prevTime, uint32_t nextTime, 67 const float blend[4] = nullptr); 68 int16_t fFrameCount; 69 uint8_t fElemCount; 70 uint8_t fFlags; 71 float fRepeat; 72 struct SkTimeCode { 73 uint32_t fTime; 74 float fBlend[4]; 75 }; 76 SkTimeCode* fTimes; // pointer into fStorage 77 void* fStorage; 78 #ifdef SK_DEBUG 79 SkTimeCode (*fTimesArray)[10]; 80 #endif 81 }; 82 83 class SkiaInterpolator : public SkiaInterpolatorBase { 84 public: 85 SkiaInterpolator(); 86 SkiaInterpolator(int elemCount, int frameCount); 87 88 void reset(int elemCount, int frameCount); 89 90 /** Add or replace a key frame, copying the values[] data into the 91 interpolator. 92 @param index The index of this frame (frames must be ordered by time) 93 @param time The millisecond time for this frame 94 @param values The array of values [elemCount] for this frame. The data 95 is copied into the interpolator. 96 @param blend A positive scalar specifying how to blend between this 97 and the next key frame. [0...1) is a cubic lag/log/lag 98 blend (slow to change at the beginning and end) 99 1 is a linear blend (default) 100 */ 101 bool setKeyFrame(int index, uint32_t time, const float values[], 102 const float blend[4] = nullptr); 103 104 /** Return the computed values given the specified time. Return whether 105 those values are the result of pinning to either the first 106 (kFreezeStart) or last (kFreezeEnd), or from interpolated the two 107 nearest key values (kNormal). 108 @param time The time to sample (in milliseconds) 109 @param (may be null) where to write the computed values. 110 */ 111 Result timeToValues(uint32_t time, float values[] = nullptr) const; 112 113 private: 114 float* fValues; // pointer into fStorage 115 116 using INHERITED = SkiaInterpolatorBase; 117 }; 118 119 #endif 120