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 #include "modifier/rs_property.h"
17 
18 #include "command/rs_node_command.h"
19 #include "modifier/rs_modifier.h"
20 #include "sandbox_utils.h"
21 #include "platform/common/rs_log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr int PID_SHIFT = 32;
27 
GeneratePropertyId()28 PropertyId GeneratePropertyId()
29 {
30     static pid_t pid_ = GetRealPid();
31     static std::atomic<uint32_t> currentId_ = 1;
32 
33     auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
34     if (currentId == UINT32_MAX) {
35         // [PLANNING]:process the overflow situations
36         ROSEN_LOGE("Property Id overflow");
37     }
38 
39     return ((PropertyId)pid_ << PID_SHIFT) | currentId;
40 }
41 } // namespace
42 
RSPropertyBase()43 RSPropertyBase::RSPropertyBase() : id_(GeneratePropertyId())
44 {}
45 
MarkModifierDirty()46 void RSPropertyBase::MarkModifierDirty()
47 {
48     auto modifier = modifier_.lock();
49     if (modifier != nullptr) {
50         modifier->SetDirty(true);
51     }
52 }
53 
MarkNodeDirty()54 void RSPropertyBase::MarkNodeDirty()
55 {
56     if (auto modifier = modifier_.lock()) {
57         modifier->MarkNodeDirty();
58     }
59 }
60 
UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode> & node)61 void RSPropertyBase::UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode>& node)
62 {
63     if (type_ == RSModifierType::BOUNDS || type_ == RSModifierType::FRAME) {
64         node->MarkAllExtendModifierDirty();
65     }
66 }
67 
GetThresholdByThresholdType(ThresholdType thresholdType) const68 float RSPropertyBase::GetThresholdByThresholdType(ThresholdType thresholdType) const
69 {
70     switch (thresholdType) {
71         case ThresholdType::LAYOUT:
72             return LAYOUT_NEAR_ZERO_THRESHOLD;
73         case ThresholdType::COARSE:
74             return FLOAT_NEAR_ZERO_COARSE_THRESHOLD;
75         case ThresholdType::MEDIUM:
76             return FLOAT_NEAR_ZERO_MEDIUM_THRESHOLD;
77         case ThresholdType::FINE:
78             return FLOAT_NEAR_ZERO_FINE_THRESHOLD;
79         case ThresholdType::COLOR:
80             return COLOR_NEAR_ZERO_THRESHOLD;
81         case ThresholdType::ZERO:
82             return ZERO;
83         default:
84             return DEFAULT_NEAR_ZERO_THRESHOLD;
85     }
86 }
87 
operator +=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)88 std::shared_ptr<RSPropertyBase> operator+=(const std::shared_ptr<RSPropertyBase>& a,
89     const std::shared_ptr<const RSPropertyBase>& b)
90 {
91     if (a == nullptr) {
92         return {};
93     }
94 
95     return a->Add(b);
96 }
97 
operator -=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)98 std::shared_ptr<RSPropertyBase> operator-=(const std::shared_ptr<RSPropertyBase>& a,
99     const std::shared_ptr<const RSPropertyBase>& b)
100 {
101     if (a == nullptr) {
102         return {};
103     }
104 
105     return a->Minus(b);
106 }
107 
operator *=(const std::shared_ptr<RSPropertyBase> & value,const float scale)108 std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale)
109 {
110     if (value == nullptr) {
111         return {};
112     }
113 
114     return value->Multiply(scale);
115 }
116 
operator +(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)117 std::shared_ptr<RSPropertyBase> operator+(const std::shared_ptr<const RSPropertyBase>& a,
118     const std::shared_ptr<const RSPropertyBase>& b)
119 {
120     if (a == nullptr) {
121         return {};
122     }
123 
124     return a->Clone()->Add(b);
125 }
126 
operator -(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)127 std::shared_ptr<RSPropertyBase> operator-(const std::shared_ptr<const RSPropertyBase>& a,
128     const std::shared_ptr<const RSPropertyBase>& b)
129 {
130     if (a == nullptr) {
131         return {};
132     }
133 
134     return a->Clone()->Minus(b);
135 }
136 
operator *(const std::shared_ptr<const RSPropertyBase> & value,const float scale)137 std::shared_ptr<RSPropertyBase> operator*(const std::shared_ptr<const RSPropertyBase>& value, const float scale)
138 {
139     if (value == nullptr) {
140         return {};
141     }
142 
143     return value->Clone()->Multiply(scale);
144 }
145 
operator ==(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)146 bool operator==(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
147 {
148     if (a == nullptr) {
149         return false;
150     }
151 
152     return a->IsEqual(b);
153 }
154 
operator !=(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)155 bool operator!=(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
156 {
157     if (a == nullptr) {
158         return false;
159     }
160 
161     return !a->IsEqual(b);
162 }
163 
164 #define UPDATE_TO_RENDER(Command, value, type)                                                                        \
165     do {                                                                                                              \
166         auto node = target_.lock();                                                                                   \
167         auto transactionProxy = RSTransactionProxy::GetInstance();                                                    \
168         if (transactionProxy && node) {                                                                               \
169             std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, type);          \
170             transactionProxy->AddCommand(command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
171             if (node->NeedForcedSendToRemote()) {                                                                     \
172                 std::unique_ptr<RSCommand> commandForRemote =                                                         \
173                     std::make_unique<Command>(node->GetId(), value, id_, type);                                       \
174                 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId());           \
175             }                                                                                                         \
176         }                                                                                                             \
177     } while (0)
178 
179 template<>
UpdateToRender(const bool & value,PropertyUpdateType type) const180 void RSProperty<bool>::UpdateToRender(const bool& value, PropertyUpdateType type) const
181 {
182     UPDATE_TO_RENDER(RSUpdatePropertyBool, value, type);
183 }
184 template<>
UpdateToRender(const float & value,PropertyUpdateType type) const185 void RSProperty<float>::UpdateToRender(const float& value, PropertyUpdateType type) const
186 {
187     UPDATE_TO_RENDER(RSUpdatePropertyFloat, value, type);
188 }
189 template<>
UpdateToRender(const int & value,PropertyUpdateType type) const190 void RSProperty<int>::UpdateToRender(const int& value, PropertyUpdateType type) const
191 {
192     UPDATE_TO_RENDER(RSUpdatePropertyInt, value, type);
193 }
194 template<>
UpdateToRender(const Color & value,PropertyUpdateType type) const195 void RSProperty<Color>::UpdateToRender(const Color& value, PropertyUpdateType type) const
196 {
197     UPDATE_TO_RENDER(RSUpdatePropertyColor, value, type);
198 }
199 template<>
UpdateToRender(const Gravity & value,PropertyUpdateType type) const200 void RSProperty<Gravity>::UpdateToRender(const Gravity& value, PropertyUpdateType type) const
201 {
202     UPDATE_TO_RENDER(RSUpdatePropertyGravity, value, type);
203 }
204 template<>
UpdateToRender(const Matrix3f & value,PropertyUpdateType type) const205 void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, PropertyUpdateType type) const
206 {
207     UPDATE_TO_RENDER(RSUpdatePropertyMatrix3f, value, type);
208 }
209 template<>
UpdateToRender(const Quaternion & value,PropertyUpdateType type) const210 void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, PropertyUpdateType type) const
211 {
212     UPDATE_TO_RENDER(RSUpdatePropertyQuaternion, value, type);
213 }
214 template<>
UpdateToRender(const std::shared_ptr<RSFilter> & value,PropertyUpdateType type) const215 void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
216     const std::shared_ptr<RSFilter>& value, PropertyUpdateType type) const
217 {
218     UPDATE_TO_RENDER(RSUpdatePropertyFilter, value, type);
219 }
220 template<>
UpdateToRender(const std::shared_ptr<RSImage> & value,PropertyUpdateType type) const221 void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
222     const std::shared_ptr<RSImage>& value, PropertyUpdateType type) const
223 {
224     UPDATE_TO_RENDER(RSUpdatePropertyImage, value, type);
225 }
226 template<>
UpdateToRender(const std::shared_ptr<RSMask> & value,PropertyUpdateType type) const227 void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
228     const std::shared_ptr<RSMask>& value, PropertyUpdateType type) const
229 {
230     UPDATE_TO_RENDER(RSUpdatePropertyMask, value, type);
231 }
232 template<>
UpdateToRender(const std::shared_ptr<RSPath> & value,PropertyUpdateType type) const233 void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
234     const std::shared_ptr<RSPath>& value, PropertyUpdateType type) const
235 {
236     UPDATE_TO_RENDER(RSUpdatePropertyPath, value, type);
237 }
238 template<>
UpdateToRender(const RSDynamicBrightnessPara & value,PropertyUpdateType type) const239 void RSProperty<RSDynamicBrightnessPara>::UpdateToRender(
240     const RSDynamicBrightnessPara& value, PropertyUpdateType type) const
241 {
242     UPDATE_TO_RENDER(RSUpdatePropertyDynamicBrightness, value, type);
243 }
244 template<>
UpdateToRender(const std::shared_ptr<RSLinearGradientBlurPara> & value,PropertyUpdateType type) const245 void RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>::UpdateToRender(
246     const std::shared_ptr<RSLinearGradientBlurPara>& value, PropertyUpdateType type) const
247 {
248     UPDATE_TO_RENDER(RSUpdatePropertyLinearGradientBlurPara, value, type);
249 }
250 template<>
UpdateToRender(const RSWaterRipplePara & value,PropertyUpdateType type) const251 void RSProperty<RSWaterRipplePara>::UpdateToRender(
252     const RSWaterRipplePara& value, PropertyUpdateType type) const
253 {
254     UPDATE_TO_RENDER(RSUpdatePropertyWaterRipple, value, type);
255 }
256 template<>
UpdateToRender(const RSFlyOutPara & value,PropertyUpdateType type) const257 void RSProperty<RSFlyOutPara>::UpdateToRender(
258     const RSFlyOutPara& value, PropertyUpdateType type) const
259 {
260     UPDATE_TO_RENDER(RSUpdatePropertyFlyOut, value, type);
261 }
262 template<>
UpdateToRender(const std::shared_ptr<MotionBlurParam> & value,PropertyUpdateType type) const263 void RSProperty<std::shared_ptr<MotionBlurParam>>::UpdateToRender(
264     const std::shared_ptr<MotionBlurParam>& value, PropertyUpdateType type) const
265 {
266     UPDATE_TO_RENDER(RSUpdatePropertyMotionBlurPara, value, type);
267 }
268 template<>
UpdateToRender(const std::shared_ptr<RSMagnifierParams> & value,PropertyUpdateType type) const269 void RSProperty<std::shared_ptr<RSMagnifierParams>>::UpdateToRender(
270     const std::shared_ptr<RSMagnifierParams>& value, PropertyUpdateType type) const
271 {
272     UPDATE_TO_RENDER(RSUpdatePropertyMagnifierPara, value, type);
273 }
274 template<>
UpdateToRender(const std::vector<std::shared_ptr<EmitterUpdater>> & value,PropertyUpdateType type) const275 void RSProperty<std::vector<std::shared_ptr<EmitterUpdater>>>::UpdateToRender(
276     const std::vector<std::shared_ptr<EmitterUpdater>>& value, PropertyUpdateType type) const
277 {
278     UPDATE_TO_RENDER(RSUpdatePropertyEmitterUpdater, value, type);
279 }
280 template<>
UpdateToRender(const std::shared_ptr<ParticleNoiseFields> & value,PropertyUpdateType type) const281 void RSProperty<std::shared_ptr<ParticleNoiseFields>>::UpdateToRender(
282     const std::shared_ptr<ParticleNoiseFields>& value, PropertyUpdateType type) const
283 {
284     UPDATE_TO_RENDER(RSUpdatePropertyParticleNoiseFields, value, type);
285 }
286 template<>
UpdateToRender(const std::shared_ptr<RSShader> & value,PropertyUpdateType type) const287 void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
288     const std::shared_ptr<RSShader>& value, PropertyUpdateType type) const
289 {
290     UPDATE_TO_RENDER(RSUpdatePropertyShader, value, type);
291 }
292 template<>
UpdateToRender(const Vector2f & value,PropertyUpdateType type) const293 void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, PropertyUpdateType type) const
294 {
295     UPDATE_TO_RENDER(RSUpdatePropertyVector2f, value, type);
296 }
297 template<>
UpdateToRender(const Vector4<uint32_t> & value,PropertyUpdateType type) const298 void RSProperty<Vector4<uint32_t>>::UpdateToRender(const Vector4<uint32_t>& value,
299     PropertyUpdateType type) const
300 {
301     UPDATE_TO_RENDER(RSUpdatePropertyBorderStyle, value, type);
302 }
303 template<>
UpdateToRender(const Vector4<Color> & value,PropertyUpdateType type) const304 void RSProperty<Vector4<Color>>::UpdateToRender(const Vector4<Color>& value,
305     PropertyUpdateType type) const
306 {
307     UPDATE_TO_RENDER(RSUpdatePropertyVector4Color, value, type);
308 }
309 template<>
UpdateToRender(const Vector4f & value,PropertyUpdateType type) const310 void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, PropertyUpdateType type) const
311 {
312     UPDATE_TO_RENDER(RSUpdatePropertyVector4f, value, type);
313 }
314 
315 template<>
UpdateToRender(const RRect & value,PropertyUpdateType type) const316 void RSProperty<RRect>::UpdateToRender(const RRect& value, PropertyUpdateType type) const
317 {
318     UPDATE_TO_RENDER(RSUpdatePropertyRRect, value, type);
319 }
320 
321 template<>
IsValid(const float & value)322 bool RSProperty<float>::IsValid(const float& value)
323 {
324     return !isinf(value);
325 }
326 template<>
IsValid(const Vector2f & value)327 bool RSProperty<Vector2f>::IsValid(const Vector2f& value)
328 {
329     return !value.IsInfinite();
330 }
331 template<>
IsValid(const Vector4f & value)332 bool RSProperty<Vector4f>::IsValid(const Vector4f& value)
333 {
334     return !value.IsInfinite();
335 }
336 
337 template<>
GetPropertyType() const338 RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const
339 {
340     return RSRenderPropertyType::PROPERTY_FLOAT;
341 }
342 template<>
GetPropertyType() const343 RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const
344 {
345     return RSRenderPropertyType::PROPERTY_COLOR;
346 }
347 template<>
GetPropertyType() const348 RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const
349 {
350     return RSRenderPropertyType::PROPERTY_MATRIX3F;
351 }
352 template<>
GetPropertyType() const353 RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const
354 {
355     return RSRenderPropertyType::PROPERTY_VECTOR2F;
356 }
357 template<>
GetPropertyType() const358 RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const
359 {
360     return RSRenderPropertyType::PROPERTY_VECTOR4F;
361 }
362 template<>
GetPropertyType() const363 RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const
364 {
365     return RSRenderPropertyType::PROPERTY_QUATERNION;
366 }
367 template<>
GetPropertyType() const368 RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const
369 {
370     return RSRenderPropertyType::PROPERTY_FILTER;
371 }
372 template<>
GetPropertyType() const373 RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const
374 {
375     return RSRenderPropertyType::PROPERTY_VECTOR4_COLOR;
376 }
377 template<>
GetPropertyType() const378 RSRenderPropertyType RSAnimatableProperty<RRect>::GetPropertyType() const
379 {
380     return RSRenderPropertyType::PROPERTY_RRECT;
381 }
382 } // namespace Rosen
383 } // namespace OHOS
384