• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_RENDER_PROP_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H
18 
19 #include "animation/rs_value_estimator.h"
20 #include "common/rs_common_def.h"
21 #include "common/rs_macros.h"
22 #include "modifier/rs_animatable_arithmetic.h"
23 #include "modifier/rs_modifier_type.h"
24 #include "pipeline/rs_base_render_node.h"
25 #include "transaction/rs_marshalling_helper.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class RSB_EXPORT RSRenderPropertyBase : public std::enable_shared_from_this<RSRenderPropertyBase> {
30 public:
31     RSRenderPropertyBase() = default;
RSRenderPropertyBase(const PropertyId & id)32     RSRenderPropertyBase(const PropertyId& id) : id_(id) {}
33     virtual ~RSRenderPropertyBase() = default;
34 
GetId()35     PropertyId GetId() const
36     {
37         return id_;
38     }
39 
Attach(std::weak_ptr<RSBaseRenderNode> node)40     void Attach(std::weak_ptr<RSBaseRenderNode> node)
41     {
42         node_ = node;
43     }
44 
45     static bool Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val);
46     static bool Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val);
47 
48 protected:
OnChange()49     void OnChange() const
50     {
51         if (auto node = node_.lock()) {
52             node->SetDirty();
53         }
54     }
55 
Clone()56     virtual const std::shared_ptr<RSRenderPropertyBase> Clone() const
57     {
58         return nullptr;
59     }
60 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)61     virtual void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) {}
62 
SetPropertyType(const RSRenderPropertyType type)63     virtual void SetPropertyType(const RSRenderPropertyType type) {}
64 
GetPropertyType()65     virtual RSRenderPropertyType GetPropertyType() const
66     {
67         return RSRenderPropertyType::INVALID;
68     }
69 
ToFloat()70     virtual float ToFloat() const
71     {
72         return 1.f;
73     }
74 
CreateRSValueEstimator(const RSValueEstimatorType type)75     virtual std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type)
76     {
77         return nullptr;
78     }
79 
80     PropertyId id_;
81     std::weak_ptr<RSBaseRenderNode> node_;
82 
83 private:
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)84     virtual std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value)
85     {
86         return shared_from_this();
87     }
88 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)89     virtual std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value)
90     {
91         return shared_from_this();
92     }
93 
Multiply(const float scale)94     virtual std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale)
95     {
96         return shared_from_this();
97     }
98 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)99     virtual bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const
100     {
101         return true;
102     }
103 
104     friend std::shared_ptr<RSRenderPropertyBase> operator+=(
105         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
106     friend std::shared_ptr<RSRenderPropertyBase> operator-=(
107         const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
108     friend std::shared_ptr<RSRenderPropertyBase> operator*=(
109         const std::shared_ptr<RSRenderPropertyBase>& value, const float scale);
110     friend std::shared_ptr<RSRenderPropertyBase> operator+(
111         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
112     friend std::shared_ptr<RSRenderPropertyBase> operator-(
113         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
114     friend std::shared_ptr<RSRenderPropertyBase> operator*(
115         const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale);
116     friend bool operator==(
117         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
118     friend bool operator!=(
119         const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b);
120     friend class RSRenderPropertyAnimation;
121     friend class RSMarshallingHelper;
122     friend class RSValueEstimator;
123     friend class RSRenderPathAnimation;
124     friend class RSRenderSpringAnimation;
125     friend class RSRenderCurveAnimation;
126     friend class RSRenderKeyframeAnimation;
127     template<typename T>
128     friend class RSSpringModel;
129 };
130 
131 template<typename T>
132 class RSRenderProperty : public RSRenderPropertyBase {
133 public:
RSRenderProperty()134     RSRenderProperty() : RSRenderPropertyBase(0) {}
RSRenderProperty(const T & value,const PropertyId & id)135     RSRenderProperty(const T& value, const PropertyId& id) : RSRenderPropertyBase(id), stagingValue_(value) {}
136     virtual ~RSRenderProperty() = default;
137 
Set(const T & value)138     void Set(const T& value)
139     {
140         if (value == stagingValue_) {
141             return;
142         }
143         stagingValue_ = value;
144         OnChange();
145     }
146 
Get()147     T Get() const
148     {
149         return stagingValue_;
150     }
151 
152 protected:
153     T stagingValue_;
154 };
155 
156 template<typename T>
157 class RSB_EXPORT_TMP RSRenderAnimatableProperty : public RSRenderProperty<T> {
158 public:
RSRenderAnimatableProperty()159     RSRenderAnimatableProperty() : RSRenderProperty<T>() {}
RSRenderAnimatableProperty(const T & value)160     RSRenderAnimatableProperty(const T& value) : RSRenderProperty<T>(value, 0) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id)161     RSRenderAnimatableProperty(const T& value, const PropertyId& id) : RSRenderProperty<T>(value, id) {}
RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)162     RSRenderAnimatableProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type)
163         : RSRenderProperty<T>(value, id), type_(type)
164     {}
165     virtual ~RSRenderAnimatableProperty() = default;
166 
167 protected:
Clone()168     const std::shared_ptr<RSRenderPropertyBase> Clone() const override
169     {
170         return std::make_shared<RSRenderAnimatableProperty<T>>(
171             RSRenderProperty<T>::stagingValue_, RSRenderProperty<T>::id_, type_);
172     }
173 
SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)174     void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) override
175     {
176         auto property = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(value);
177         if (property != nullptr && property->GetPropertyType() == type_) {
178             RSRenderProperty<T>::Set(property->Get());
179         }
180     }
181 
SetPropertyType(const RSRenderPropertyType type)182     void SetPropertyType(const RSRenderPropertyType type) override
183     {
184         type_ = type;
185     }
186 
GetPropertyType()187     virtual RSRenderPropertyType GetPropertyType() const override
188     {
189         return type_;
190     }
191 
ToFloat()192     float ToFloat() const override
193     {
194         return 1.f;
195     }
196 
CreateRSValueEstimator(const RSValueEstimatorType type)197     std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type) override
198     {
199         switch (type) {
200             case RSValueEstimatorType::CURVE_VALUE_ESTIMATOR: {
201                 return std::make_shared<RSCurveValueEstimator<T>>();
202             }
203             case RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR: {
204                 return std::make_shared<RSKeyframeValueEstimator<T>>();
205             }
206             default: {
207                 return nullptr;
208             }
209         }
210     }
211 
212 private:
213     RSRenderPropertyType type_ = RSRenderPropertyType::INVALID;
214 
Add(const std::shared_ptr<const RSRenderPropertyBase> & value)215     std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value) override
216     {
217         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
218         if (animatableProperty != nullptr) {
219             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ + animatableProperty->stagingValue_;
220         }
221         return RSRenderProperty<T>::shared_from_this();
222     }
223 
Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)224     std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value) override
225     {
226         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
227         if (animatableProperty != nullptr) {
228             RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ - animatableProperty->stagingValue_;
229         }
230         return RSRenderProperty<T>::shared_from_this();
231     }
232 
Multiply(const float scale)233     std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale) override
234     {
235         RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ * scale;
236         return RSRenderProperty<T>::shared_from_this();
237     }
238 
IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)239     bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const override
240     {
241         auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value);
242         if (animatableProperty != nullptr) {
243             return RSRenderProperty<T>::stagingValue_ == animatableProperty->stagingValue_;
244         }
245         return true;
246     }
247 
248     friend class RSMarshallingHelper;
249     friend class RSRenderPathAnimation;
250     friend class RSRenderPropertyBase;
251 };
252 
253 template<>
254 RSB_EXPORT float RSRenderAnimatableProperty<float>::ToFloat() const;
255 template<>
256 RSB_EXPORT float RSRenderAnimatableProperty<Vector4f>::ToFloat() const;
257 template<>
258 RSB_EXPORT float RSRenderAnimatableProperty<Quaternion>::ToFloat() const;
259 template<>
260 RSB_EXPORT float RSRenderAnimatableProperty<Vector2f>::ToFloat() const;
261 
262 #if defined(_WIN32)
263 extern template class RSRenderAnimatableProperty<float>;
264 extern template class RSRenderAnimatableProperty<Vector4f>;
265 extern template class RSRenderAnimatableProperty<Quaternion>;
266 extern template class RSRenderAnimatableProperty<Vector2f>;
267 #endif
268 } // namespace Rosen
269 } // namespace OHOS
270 
271 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H
272