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     virtual InterpolatorType GetType() = 0;
51     static void Init();
52 protected:
53     RSInterpolator();
RSInterpolator(uint64_t id)54     RSInterpolator(uint64_t id) : id_(id) {};
55     uint64_t id_ { 0 };
56 
57 private:
58     virtual float InterpolateImpl(float input) const = 0;
59     static uint64_t GenerateId();
60     float prevInput_ { -1.0f };
61     float prevOutput_ { -1.0f };
62 };
63 
64 class RSB_EXPORT LinearInterpolator : public RSInterpolator {
65 public:
66     LinearInterpolator() = default;
67     ~LinearInterpolator() override = default;
68 
69     bool Marshalling(Parcel& parcel) const override;
70     [[nodiscard]] static LinearInterpolator* Unmarshalling(Parcel& parcel);
71 
GetType()72     InterpolatorType GetType() override { return InterpolatorType::LINEAR; }
73 private:
LinearInterpolator(uint64_t id)74     LinearInterpolator(uint64_t id) : RSInterpolator(id) {}
InterpolateImpl(float input)75     float InterpolateImpl(float input) const override
76     {
77         return input;
78     }
79 };
80 
81 class RSB_EXPORT RSCustomInterpolator : public RSInterpolator {
82 public:
83     RSCustomInterpolator(const std::function<float(float)>& func, int duration);
84     ~RSCustomInterpolator() override = default;
85 
86     bool Marshalling(Parcel& parcel) const override;
87     [[nodiscard]] static RSCustomInterpolator* Unmarshalling(Parcel& parcel);
88 
GetType()89     InterpolatorType GetType() override { return InterpolatorType::CUSTOM; }
90 private:
91     RSCustomInterpolator(uint64_t id, const std::vector<float>&& times, const std::vector<float>&& values);
92     float InterpolateImpl(float input) const override;
93     void Convert(int duration);
94 
95     std::vector<float> times_;
96     std::vector<float> values_;
97     std::function<float(float)> interpolateFunc_;
98 };
99 } // namespace Rosen
100 } // namespace OHOS
101 
102 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H
103