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_TRANSITION_RS_RENDER_TRANSITION_EFFECT_H
17 #define RENDER_SERVICE_CLIENT_CORE_TRANSITION_RS_RENDER_TRANSITION_EFFECT_H
18 
19 #include <parcel.h>
20 #include <refbase.h>
21 #include <memory>
22 #include <cinttypes>
23 
24 #include "animation/rs_animation_common.h"
25 #include "animation/rs_value_estimator.h"
26 #include "common/rs_macros.h"
27 #include "modifier/rs_render_property.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 class RSB_EXPORT RSRenderTransitionEffect : public Parcelable {
32 public:
33     RSRenderTransitionEffect() = default;
34     virtual ~RSRenderTransitionEffect() = default;
35     const std::shared_ptr<RSRenderModifier>& GetModifier();
36     virtual void UpdateFraction(float fraction) const = 0;
37 
Marshalling(Parcel & parcel)38     bool Marshalling(Parcel& parcel) const override
39     {
40         return false;
41     }
42     [[nodiscard]] static RSRenderTransitionEffect* Unmarshalling(Parcel& parcel);
43 private:
44     std::shared_ptr<RSRenderModifier> modifier_;
45     virtual const std::shared_ptr<RSRenderModifier> CreateModifier() = 0;
46 };
47 
48 class RSB_EXPORT RSTransitionFade : public RSRenderTransitionEffect {
49 public:
RSTransitionFade(float alpha)50     explicit RSTransitionFade(float alpha) : alpha_(alpha) {}
51     ~RSTransitionFade() override = default;
52     void UpdateFraction(float fraction) const override;
53 
54     bool Marshalling(Parcel& parcel) const override;
55     [[nodiscard]] static RSRenderTransitionEffect* Unmarshalling(Parcel& parcel);
56 private:
57     float alpha_ { 0.0 };
58     std::shared_ptr<RSRenderAnimatableProperty<float>> property_;
59     const std::shared_ptr<RSRenderModifier> CreateModifier() override;
60 };
61 
62 class RSB_EXPORT RSTransitionScale : public RSRenderTransitionEffect {
63 public:
64     explicit RSTransitionScale(float scaleX = 0.0f, float scaleY = 0.0f, float scaleZ = 0.0f)
65         : scaleX_(scaleX), scaleY_(scaleY), scaleZ_(scaleZ)
66     {}
67     ~RSTransitionScale() override = default;
68     void UpdateFraction(float fraction) const override;
69 
70     bool Marshalling(Parcel& parcel) const override;
71     [[nodiscard]] static RSRenderTransitionEffect* Unmarshalling(Parcel& parcel);
72 private:
73     float scaleX_ { 0.0 };
74     float scaleY_ { 0.0 };
75     float scaleZ_ { 0.0 };
76     std::shared_ptr<RSRenderAnimatableProperty<Vector2<float>>> property_;
77     const std::shared_ptr<RSRenderModifier> CreateModifier() override;
78 };
79 
80 class RSB_EXPORT RSTransitionTranslate : public RSRenderTransitionEffect {
81 public:
RSTransitionTranslate(float translateX,float translateY,float translateZ)82     explicit RSTransitionTranslate(float translateX, float translateY, float translateZ)
83         : translateX_(translateX), translateY_(translateY), translateZ_(translateZ)
84     {}
85     ~RSTransitionTranslate() override = default;
86     void UpdateFraction(float fraction) const override;
87 
88     bool Marshalling(Parcel& parcel) const override;
89     [[nodiscard]] static RSRenderTransitionEffect* Unmarshalling(Parcel& parcel);
90 private:
91     float translateX_ { 0.0 };
92     float translateY_ { 0.0 };
93     float translateZ_ { 0.0 };
94     std::shared_ptr<RSRenderAnimatableProperty<Vector2<float>>> property_;
95     const std::shared_ptr<RSRenderModifier> CreateModifier() override;
96 };
97 
98 class RSB_EXPORT RSTransitionRotate : public RSRenderTransitionEffect {
99 public:
RSTransitionRotate(float dx,float dy,float dz,float radian)100     explicit RSTransitionRotate(float dx, float dy, float dz, float radian) : dx_(dx), dy_(dy), dz_(dz), radian_(radian)
101     {}
102     ~RSTransitionRotate() override = default;
103     void UpdateFraction(float fraction) const override;
104 
105     bool Marshalling(Parcel& parcel) const override;
106     [[nodiscard]] static RSRenderTransitionEffect* Unmarshalling(Parcel& parcel);
107 private:
108     float dx_ { 0.0 };
109     float dy_ { 0.0 };
110     float dz_ { 0.0 };
111     float radian_ { 0.0 };
112     std::shared_ptr<RSRenderAnimatableProperty<Quaternion>> property_;
113     const std::shared_ptr<RSRenderModifier> CreateModifier() override;
114 };
115 
116 class RSTransitionCustom : public RSRenderTransitionEffect {
117 public:
RSTransitionCustom(std::shared_ptr<RSRenderPropertyBase> property,std::shared_ptr<RSRenderPropertyBase> startValue,std::shared_ptr<RSRenderPropertyBase> endValue)118     RSTransitionCustom(std::shared_ptr<RSRenderPropertyBase> property, std::shared_ptr<RSRenderPropertyBase> startValue,
119         std::shared_ptr<RSRenderPropertyBase> endValue)
120         : property_(property), startValue_(startValue), endValue_(endValue)
121     {
122         InitValueEstimator();
123     }
124     ~RSTransitionCustom() override = default;
125 
UpdateFraction(float fraction)126     void UpdateFraction(float fraction) const override
127     {
128         if (!valueEstimator_) {
129             return;
130         }
131         valueEstimator_->UpdateAnimationValue(fraction, true);
132     }
133 
134 private:
CreateModifier()135     const std::shared_ptr<RSRenderModifier> CreateModifier() override
136     {
137         return nullptr;
138     }
139 
InitValueEstimator()140     void InitValueEstimator()
141     {
142         if (valueEstimator_ == nullptr) {
143             valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::CURVE_VALUE_ESTIMATOR);
144         }
145         if (valueEstimator_ == nullptr) {
146             return;
147         }
148         valueEstimator_->InitCurveAnimationValue(property_, endValue_, startValue_, startValue_);
149     }
150 
151     std::shared_ptr<RSRenderPropertyBase> property_;
152     std::shared_ptr<RSRenderPropertyBase> startValue_;
153     std::shared_ptr<RSRenderPropertyBase> endValue_;
154     std::shared_ptr<RSValueEstimator> valueEstimator_;
155 };
156 } // namespace Rosen
157 } // namespace OHOS
158 
159 #endif
160