• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
23 
24 #include "common/rs_macros.h"
25 
26 #include <parcel.h>
27 #include <refbase.h>
28 
29 #include "common/rs_macros.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 
34 enum InterpolatorType : uint16_t {
35     LINEAR = 1,
36     CUSTOM,
37     CUBIC_BEZIER,
38     SPRING,
39     STEPS,
40 };
41 
42 class RSInterpolator : public Parcelable {
43 public:
44     static RSB_EXPORT const std::shared_ptr<RSInterpolator> DEFAULT;
45     RSInterpolator() = default;
46     virtual ~RSInterpolator() = default;
47 
48     virtual bool Marshalling(Parcel& parcel) const override = 0;
49     static RSB_EXPORT RSInterpolator* Unmarshalling(Parcel& parcel);
50 
51     virtual float Interpolate(float input) const = 0;
52 };
53 
54 class LinearInterpolator : public RSInterpolator {
55 public:
56     LinearInterpolator() = default;
57     virtual ~LinearInterpolator() = default;
58 
Marshalling(Parcel & parcel)59     bool Marshalling(Parcel& parcel) const override
60     {
61         if (!parcel.WriteUint16(InterpolatorType::LINEAR)) {
62             return false;
63         }
64         return true;
65     }
66 
Interpolate(float input)67     float Interpolate(float input) const override
68     {
69         return input;
70     }
71 };
72 
73 class RSB_EXPORT RSCustomInterpolator : public RSInterpolator {
74 public:
75     RSCustomInterpolator(const std::function<float(float)>& func, int duration);
76     virtual ~RSCustomInterpolator() = default;
77 
78     float Interpolate(float input) const override;
79 
Marshalling(Parcel & parcel)80     bool Marshalling(Parcel& parcel) const override
81     {
82         if (!(parcel.WriteUint16(InterpolatorType::CUSTOM) && parcel.WriteFloatVector(times_) &&
83                 parcel.WriteFloatVector(values_))) {
84             return false;
85         }
86         return true;
87     }
88     static RSCustomInterpolator* Unmarshalling(Parcel& parcel);
89 
90 private:
91     RSCustomInterpolator(const std::vector<float>&& times, const std::vector<float>&& values);
92     void Convert(int duration);
93 
94     std::vector<float> times_;
95     std::vector<float> values_;
96     std::function<float(float)> interpolateFunc_;
97 };
98 } // namespace Rosen
99 } // namespace OHOS
100 
101 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_INTERPOLATOR_H
102