Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 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 | |
Kevin Lubick | d9ccdf6 | 2023-01-05 19:07:26 +0000 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | #include <cstdint> |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 22 | |
| 23 | class SkiaInterpolatorBase { |
| 24 | public: |
| 25 | enum Result { kNormal_Result, kFreezeStart_Result, kFreezeEnd_Result }; |
| 26 | |
| 27 | protected: |
| 28 | SkiaInterpolatorBase(); |
| 29 | ~SkiaInterpolatorBase(); |
| 30 | |
| 31 | public: |
| 32 | void reset(int elemCount, int frameCount); |
| 33 | |
| 34 | /** Return the start and end time for this interpolator. |
| 35 | If there are no key frames, return false. |
| 36 | @param startTime If not null, returns the time (in milliseconds) of the |
| 37 | first keyframe. If there are no keyframes, this param |
| 38 | is ignored (left unchanged). |
| 39 | @param endTime If not null, returns the time (in milliseconds) of the |
| 40 | last keyframe. If there are no keyframes, this parameter |
| 41 | is ignored (left unchanged). |
| 42 | @return True if there are key frames, or false if there are none. |
| 43 | */ |
| 44 | bool getDuration(uint32_t* startTime, uint32_t* endTime) const; |
| 45 | |
| 46 | /** Set the whether the repeat is mirrored. |
| 47 | @param mirror If true, the odd repeats interpolate from the last key |
| 48 | frame and the first. |
| 49 | */ |
Kevin Lubick | d9ccdf6 | 2023-01-05 19:07:26 +0000 | [diff] [blame] | 50 | void setMirror(bool mirror) { |
| 51 | fFlags = static_cast<uint8_t>((fFlags & ~kMirror) | (int)mirror); |
| 52 | } |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 53 | |
| 54 | /** Set the repeat count. The repeat count may be fractional. |
| 55 | @param repeatCount Multiplies the total time by this scalar. |
| 56 | */ |
| 57 | void setRepeatCount(float repeatCount) { fRepeat = repeatCount; } |
| 58 | |
| 59 | /** Set the whether the repeat is mirrored. |
| 60 | @param reset If true, the odd repeats interpolate from the last key |
| 61 | frame and the first. |
| 62 | */ |
Kevin Lubick | d9ccdf6 | 2023-01-05 19:07:26 +0000 | [diff] [blame] | 63 | void setReset(bool reset) { fFlags = static_cast<uint8_t>((fFlags & ~kReset) | (int)reset); } |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 64 | |
| 65 | Result timeToT(uint32_t time, float* T, int* index, bool* exact) const; |
| 66 | |
| 67 | protected: |
| 68 | enum Flags { kMirror = 1, kReset = 2, kHasBlend = 4 }; |
| 69 | static float ComputeRelativeT(uint32_t time, uint32_t prevTime, uint32_t nextTime, |
| 70 | const float blend[4] = nullptr); |
| 71 | int16_t fFrameCount; |
| 72 | uint8_t fElemCount; |
| 73 | uint8_t fFlags; |
| 74 | float fRepeat; |
| 75 | struct SkTimeCode { |
| 76 | uint32_t fTime; |
| 77 | float fBlend[4]; |
| 78 | }; |
| 79 | SkTimeCode* fTimes; // pointer into fStorage |
| 80 | void* fStorage; |
Mike Reed | 7406527 | 2021-04-12 09:52:07 -0400 | [diff] [blame] | 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 |