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