1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H 18 19 #include <cmath> 20 #include <functional> 21 #include <memory> 22 #include <vector> 23 24 #ifdef ROSEN_OHOS 25 #include <parcel.h> 26 #include <refbase.h> 27 #endif 28 29 namespace OHOS { 30 namespace Rosen { 31 32 enum InterpolatorType : uint16_t { 33 LINEAR = 1, 34 CUSTOM, 35 CUBIC_BEZIER, 36 SPRING, 37 }; 38 39 #ifdef ROSEN_OHOS 40 class RSInterpolator : public Parcelable { 41 #else 42 class RSInterpolator { 43 #endif 44 public: 45 static const std::shared_ptr<RSInterpolator> DEFAULT; 46 RSInterpolator() = default; 47 virtual ~RSInterpolator() = default; 48 49 #ifdef ROSEN_OHOS 50 virtual bool Marshalling(Parcel& parcel) const override = 0; 51 static RSInterpolator* Unmarshalling(Parcel& parcel); 52 #endif 53 54 virtual float Interpolate(float input) const = 0; 55 }; 56 57 class LinearInterpolator : public RSInterpolator { 58 public: 59 LinearInterpolator() = default; 60 virtual ~LinearInterpolator() = default; 61 62 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)63 bool Marshalling(Parcel& parcel) const override 64 { 65 if (!parcel.WriteUint16(InterpolatorType::LINEAR)) { 66 return false; 67 } 68 return true; 69 } 70 #endif 71 Interpolate(float input)72 float Interpolate(float input) const override 73 { 74 return input; 75 } 76 }; 77 78 class RSCustomInterpolator : public RSInterpolator { 79 public: 80 RSCustomInterpolator(const std::function<float(float)>& func, int duration); 81 virtual ~RSCustomInterpolator() = default; 82 83 float Interpolate(float input) const override; 84 85 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)86 bool Marshalling(Parcel& parcel) const override 87 { 88 if (!(parcel.WriteUint16(InterpolatorType::CUSTOM) && parcel.WriteFloatVector(times_) && 89 parcel.WriteFloatVector(values_))) { 90 return false; 91 } 92 return true; 93 } 94 static RSCustomInterpolator* Unmarshalling(Parcel& parcel); 95 #endif 96 97 private: 98 RSCustomInterpolator(const std::vector<float>&& times, const std::vector<float>&& values); 99 void Convert(int duration); 100 101 std::vector<float> times_; 102 std::vector<float> values_; 103 std::function<float(float)> interpolateFunc_; 104 }; 105 } // namespace Rosen 106 } // namespace OHOS 107 108 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H 109