1 /* 2 * Copyright (c) 2021-2023 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 <mutex> 23 #include <parcel.h> 24 #include <refbase.h> 25 #include <unordered_map> 26 #include <vector> 27 28 #include "common/rs_macros.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 static constexpr int MAX_SAMPLE_POINTS = 300; 33 static constexpr int MIN_SAMPLE_POINTS = 2; 34 35 enum InterpolatorType : uint16_t { 36 LINEAR = 1, 37 CUSTOM, 38 CUBIC_BEZIER, 39 SPRING, 40 STEPS, 41 }; 42 43 class RSB_EXPORT RSInterpolator : public Parcelable { 44 public: 45 static RSB_EXPORT const std::shared_ptr<RSInterpolator> DEFAULT; 46 ~RSInterpolator() override = default; 47 48 bool Marshalling(Parcel& parcel) const override = 0; 49 [[nodiscard]] static RSB_EXPORT std::shared_ptr<RSInterpolator> Unmarshalling(Parcel& parcel); 50 51 float Interpolate(float input); 52 virtual InterpolatorType GetType() = 0; 53 static void Init(); 54 protected: 55 RSInterpolator(); RSInterpolator(uint64_t id)56 RSInterpolator(uint64_t id) : id_(id) {}; 57 uint64_t id_ { 0 }; 58 59 private: 60 virtual float InterpolateImpl(float input) const = 0; 61 static uint64_t GenerateId(); 62 [[nodiscard]] static RSInterpolator* UnmarshallingFromParcel(Parcel& parcel); 63 float prevInput_ { -1.0f }; 64 float prevOutput_ { -1.0f }; 65 }; 66 67 class RSB_EXPORT LinearInterpolator : public RSInterpolator { 68 public: 69 LinearInterpolator() = default; 70 ~LinearInterpolator() override = default; 71 72 bool Marshalling(Parcel& parcel) const override; 73 [[nodiscard]] static LinearInterpolator* Unmarshalling(Parcel& parcel); 74 GetType()75 InterpolatorType GetType() override { return InterpolatorType::LINEAR; } 76 private: LinearInterpolator(uint64_t id)77 LinearInterpolator(uint64_t id) : RSInterpolator(id) {} InterpolateImpl(float input)78 float InterpolateImpl(float input) const override 79 { 80 return input; 81 } 82 }; 83 84 class RSB_EXPORT RSCustomInterpolator : public RSInterpolator { 85 public: 86 RSCustomInterpolator(const std::function<float(float)>& func, int duration); 87 ~RSCustomInterpolator() override = default; 88 89 bool Marshalling(Parcel& parcel) const override; 90 [[nodiscard]] static RSCustomInterpolator* Unmarshalling(Parcel& parcel); 91 GetType()92 InterpolatorType GetType() override { return InterpolatorType::CUSTOM; } 93 private: 94 RSCustomInterpolator(uint64_t id, const std::vector<float>&& times, const std::vector<float>&& values); 95 float InterpolateImpl(float input) const override; 96 void Convert(int duration); 97 98 std::vector<float> times_; 99 std::vector<float> values_; 100 std::function<float(float)> interpolateFunc_; 101 }; 102 } // namespace Rosen 103 } // namespace OHOS 104 105 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H 106