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 "modifier/rs_modifier_manager_map.h"
21 #include "sandbox_utils.h"
22 #include "platform/common/rs_log.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr int PID_SHIFT = 32;
28
GeneratePropertyId()29 PropertyId GeneratePropertyId()
30 {
31 static pid_t pid_ = GetRealPid();
32 static std::atomic<uint32_t> currentId_ = 1;
33
34 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
35 if (currentId == UINT32_MAX) {
36 // [PLANNING]:process the overflow situations
37 ROSEN_LOGE("Property Id overflow");
38 }
39
40 return ((PropertyId)pid_ << PID_SHIFT) | currentId;
41 }
42 } // namespace
43
RSPropertyBase()44 RSPropertyBase::RSPropertyBase() : id_(GeneratePropertyId())
45 {}
46
MarkModifierDirty()47 void RSPropertyBase::MarkModifierDirty()
48 {
49 auto modifier = modifier_.lock();
50 if (modifier != nullptr) {
51 auto node = target_.lock();
52 if (node && node->GetRSUIContext()) {
53 modifier->SetDirty(true, node->GetRSUIContext()->GetRSModifierManager());
54 } else {
55 modifier->SetDirty(true, RSModifierManagerMap::Instance()->GetModifierManager(gettid()));
56 }
57 }
58 }
59
MarkNodeDirty()60 void RSPropertyBase::MarkNodeDirty()
61 {
62 if (auto modifier = modifier_.lock()) {
63 modifier->MarkNodeDirty();
64 }
65 }
66
UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode> & node)67 void RSPropertyBase::UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode>& node)
68 {
69 if (type_ == RSModifierType::BOUNDS || type_ == RSModifierType::FRAME) {
70 node->MarkAllExtendModifierDirty();
71 }
72 }
73
GetThresholdByThresholdType(ThresholdType thresholdType) const74 float RSPropertyBase::GetThresholdByThresholdType(ThresholdType thresholdType) const
75 {
76 switch (thresholdType) {
77 case ThresholdType::LAYOUT:
78 return LAYOUT_NEAR_ZERO_THRESHOLD;
79 case ThresholdType::COARSE:
80 return FLOAT_NEAR_ZERO_COARSE_THRESHOLD;
81 case ThresholdType::MEDIUM:
82 return FLOAT_NEAR_ZERO_MEDIUM_THRESHOLD;
83 case ThresholdType::FINE:
84 return FLOAT_NEAR_ZERO_FINE_THRESHOLD;
85 case ThresholdType::COLOR:
86 return COLOR_NEAR_ZERO_THRESHOLD;
87 case ThresholdType::ZERO:
88 return ZERO;
89 default:
90 return DEFAULT_NEAR_ZERO_THRESHOLD;
91 }
92 }
93
operator +=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)94 std::shared_ptr<RSPropertyBase> operator+=(const std::shared_ptr<RSPropertyBase>& a,
95 const std::shared_ptr<const RSPropertyBase>& b)
96 {
97 if (a == nullptr) {
98 return {};
99 }
100
101 return a->Add(b);
102 }
103
operator -=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)104 std::shared_ptr<RSPropertyBase> operator-=(const std::shared_ptr<RSPropertyBase>& a,
105 const std::shared_ptr<const RSPropertyBase>& b)
106 {
107 if (a == nullptr) {
108 return {};
109 }
110
111 return a->Minus(b);
112 }
113
operator *=(const std::shared_ptr<RSPropertyBase> & value,const float scale)114 std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale)
115 {
116 if (value == nullptr) {
117 return {};
118 }
119
120 return value->Multiply(scale);
121 }
122
operator +(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)123 std::shared_ptr<RSPropertyBase> operator+(const std::shared_ptr<const RSPropertyBase>& a,
124 const std::shared_ptr<const RSPropertyBase>& b)
125 {
126 if (a == nullptr) {
127 return {};
128 }
129
130 const auto clone = a->Clone();
131 if (clone == nullptr) {
132 return {};
133 }
134 return clone->Add(b);
135 }
136
operator -(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)137 std::shared_ptr<RSPropertyBase> operator-(const std::shared_ptr<const RSPropertyBase>& a,
138 const std::shared_ptr<const RSPropertyBase>& b)
139 {
140 if (a == nullptr) {
141 return {};
142 }
143
144 const auto clone = a->Clone();
145 if (clone == nullptr) {
146 return {};
147 }
148 return clone->Minus(b);
149 }
150
operator *(const std::shared_ptr<const RSPropertyBase> & value,const float scale)151 std::shared_ptr<RSPropertyBase> operator*(const std::shared_ptr<const RSPropertyBase>& value, const float scale)
152 {
153 if (value == nullptr) {
154 return {};
155 }
156
157 const auto clone = value->Clone();
158 if (clone == nullptr) {
159 return {};
160 }
161 return clone->Multiply(scale);
162 }
163
operator ==(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)164 bool operator==(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
165 {
166 if (a == nullptr) {
167 return false;
168 }
169
170 return a->IsEqual(b);
171 }
172
operator !=(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)173 bool operator!=(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
174 {
175 if (a == nullptr) {
176 return false;
177 }
178
179 return !a->IsEqual(b);
180 }
181
182 #define UPDATE_TO_RENDER(Command, value, type) \
183 auto node = target_.lock(); \
184 if (node != nullptr) { \
185 auto transaction = node->GetRSTransaction(); \
186 if (!transaction) { \
187 do { \
188 auto transactionProxy = RSTransactionProxy::GetInstance(); \
189 if (transactionProxy) { \
190 std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, type); \
191 transactionProxy->AddCommand( \
192 command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
193 if (node->NeedForcedSendToRemote()) { \
194 std::unique_ptr<RSCommand> commandForRemote = \
195 std::make_unique<Command>(node->GetId(), value, id_, type); \
196 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); \
197 } \
198 } \
199 } while (0); \
200 } else { \
201 do { \
202 std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, type); \
203 transaction->AddCommand(command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
204 if (node->NeedForcedSendToRemote()) { \
205 std::unique_ptr<RSCommand> commandForRemote = \
206 std::make_unique<Command>(node->GetId(), value, id_, type); \
207 transaction->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); \
208 } \
209 } while (0); \
210 } \
211 }
212
213 template<>
UpdateToRender(const bool & value,PropertyUpdateType type) const214 void RSProperty<bool>::UpdateToRender(const bool& value, PropertyUpdateType type) const
215 {
216 UPDATE_TO_RENDER(RSUpdatePropertyBool, value, type);
217 }
218 template<>
UpdateToRender(const float & value,PropertyUpdateType type) const219 void RSProperty<float>::UpdateToRender(const float& value, PropertyUpdateType type) const
220 {
221 UPDATE_TO_RENDER(RSUpdatePropertyFloat, value, type);
222 }
223 template<>
UpdateToRender(const int & value,PropertyUpdateType type) const224 void RSProperty<int>::UpdateToRender(const int& value, PropertyUpdateType type) const
225 {
226 UPDATE_TO_RENDER(RSUpdatePropertyInt, value, type);
227 }
228 template<>
UpdateToRender(const Color & value,PropertyUpdateType type) const229 void RSProperty<Color>::UpdateToRender(const Color& value, PropertyUpdateType type) const
230 {
231 UPDATE_TO_RENDER(RSUpdatePropertyColor, value, type);
232 }
233 template<>
UpdateToRender(const Gravity & value,PropertyUpdateType type) const234 void RSProperty<Gravity>::UpdateToRender(const Gravity& value, PropertyUpdateType type) const
235 {
236 UPDATE_TO_RENDER(RSUpdatePropertyGravity, value, type);
237 }
238 template<>
UpdateToRender(const Matrix3f & value,PropertyUpdateType type) const239 void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, PropertyUpdateType type) const
240 {
241 UPDATE_TO_RENDER(RSUpdatePropertyMatrix3f, value, type);
242 }
243 template<>
UpdateToRender(const Quaternion & value,PropertyUpdateType type) const244 void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, PropertyUpdateType type) const
245 {
246 UPDATE_TO_RENDER(RSUpdatePropertyQuaternion, value, type);
247 }
248 template<>
UpdateToRender(const std::shared_ptr<RSFilter> & value,PropertyUpdateType type) const249 void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
250 const std::shared_ptr<RSFilter>& value, PropertyUpdateType type) const
251 {
252 UPDATE_TO_RENDER(RSUpdatePropertyFilter, value, type);
253 }
254 template<>
UpdateToRender(const std::shared_ptr<RSImage> & value,PropertyUpdateType type) const255 void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
256 const std::shared_ptr<RSImage>& value, PropertyUpdateType type) const
257 {
258 UPDATE_TO_RENDER(RSUpdatePropertyImage, value, type);
259 }
260 template<>
UpdateToRender(const std::shared_ptr<RSMask> & value,PropertyUpdateType type) const261 void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
262 const std::shared_ptr<RSMask>& value, PropertyUpdateType type) const
263 {
264 UPDATE_TO_RENDER(RSUpdatePropertyMask, value, type);
265 }
266 template<>
UpdateToRender(const std::shared_ptr<RSPath> & value,PropertyUpdateType type) const267 void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
268 const std::shared_ptr<RSPath>& value, PropertyUpdateType type) const
269 {
270 UPDATE_TO_RENDER(RSUpdatePropertyPath, value, type);
271 }
272 template<>
UpdateToRender(const RSDynamicBrightnessPara & value,PropertyUpdateType type) const273 void RSProperty<RSDynamicBrightnessPara>::UpdateToRender(
274 const RSDynamicBrightnessPara& value, PropertyUpdateType type) const
275 {
276 UPDATE_TO_RENDER(RSUpdatePropertyDynamicBrightness, value, type);
277 }
278 template<>
UpdateToRender(const std::shared_ptr<RSLinearGradientBlurPara> & value,PropertyUpdateType type) const279 void RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>::UpdateToRender(
280 const std::shared_ptr<RSLinearGradientBlurPara>& value, PropertyUpdateType type) const
281 {
282 UPDATE_TO_RENDER(RSUpdatePropertyLinearGradientBlurPara, value, type);
283 }
284 template<>
UpdateToRender(const RSWaterRipplePara & value,PropertyUpdateType type) const285 void RSProperty<RSWaterRipplePara>::UpdateToRender(
286 const RSWaterRipplePara& value, PropertyUpdateType type) const
287 {
288 UPDATE_TO_RENDER(RSUpdatePropertyWaterRipple, value, type);
289 }
290 template<>
UpdateToRender(const RSFlyOutPara & value,PropertyUpdateType type) const291 void RSProperty<RSFlyOutPara>::UpdateToRender(
292 const RSFlyOutPara& value, PropertyUpdateType type) const
293 {
294 UPDATE_TO_RENDER(RSUpdatePropertyFlyOut, value, type);
295 }
296 template<>
UpdateToRender(const std::shared_ptr<MotionBlurParam> & value,PropertyUpdateType type) const297 void RSProperty<std::shared_ptr<MotionBlurParam>>::UpdateToRender(
298 const std::shared_ptr<MotionBlurParam>& value, PropertyUpdateType type) const
299 {
300 UPDATE_TO_RENDER(RSUpdatePropertyMotionBlurPara, value, type);
301 }
302 template<>
UpdateToRender(const std::shared_ptr<RSMagnifierParams> & value,PropertyUpdateType type) const303 void RSProperty<std::shared_ptr<RSMagnifierParams>>::UpdateToRender(
304 const std::shared_ptr<RSMagnifierParams>& value, PropertyUpdateType type) const
305 {
306 UPDATE_TO_RENDER(RSUpdatePropertyMagnifierPara, value, type);
307 }
308 template<>
UpdateToRender(const std::vector<std::shared_ptr<EmitterUpdater>> & value,PropertyUpdateType type) const309 void RSProperty<std::vector<std::shared_ptr<EmitterUpdater>>>::UpdateToRender(
310 const std::vector<std::shared_ptr<EmitterUpdater>>& value, PropertyUpdateType type) const
311 {
312 UPDATE_TO_RENDER(RSUpdatePropertyEmitterUpdater, value, type);
313 }
314 template<>
UpdateToRender(const std::shared_ptr<ParticleNoiseFields> & value,PropertyUpdateType type) const315 void RSProperty<std::shared_ptr<ParticleNoiseFields>>::UpdateToRender(
316 const std::shared_ptr<ParticleNoiseFields>& value, PropertyUpdateType type) const
317 {
318 UPDATE_TO_RENDER(RSUpdatePropertyParticleNoiseFields, value, type);
319 }
320 template<>
UpdateToRender(const std::shared_ptr<RSShader> & value,PropertyUpdateType type) const321 void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
322 const std::shared_ptr<RSShader>& value, PropertyUpdateType type) const
323 {
324 UPDATE_TO_RENDER(RSUpdatePropertyShader, value, type);
325 }
326 template<>
UpdateToRender(const Vector2f & value,PropertyUpdateType type) const327 void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, PropertyUpdateType type) const
328 {
329 UPDATE_TO_RENDER(RSUpdatePropertyVector2f, value, type);
330 }
331 template<>
UpdateToRender(const Vector3f & value,PropertyUpdateType type) const332 void RSProperty<Vector3f>::UpdateToRender(const Vector3f& value, PropertyUpdateType type) const
333 {
334 UPDATE_TO_RENDER(RSUpdatePropertyVector3f, value, type);
335 }
336 template<>
UpdateToRender(const Vector4<uint32_t> & value,PropertyUpdateType type) const337 void RSProperty<Vector4<uint32_t>>::UpdateToRender(const Vector4<uint32_t>& value,
338 PropertyUpdateType type) const
339 {
340 UPDATE_TO_RENDER(RSUpdatePropertyBorderStyle, value, type);
341 }
342 template<>
UpdateToRender(const Vector4<Color> & value,PropertyUpdateType type) const343 void RSProperty<Vector4<Color>>::UpdateToRender(const Vector4<Color>& value,
344 PropertyUpdateType type) const
345 {
346 UPDATE_TO_RENDER(RSUpdatePropertyVector4Color, value, type);
347 }
348 template<>
UpdateToRender(const Vector4f & value,PropertyUpdateType type) const349 void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, PropertyUpdateType type) const
350 {
351 UPDATE_TO_RENDER(RSUpdatePropertyVector4f, value, type);
352 }
353
354 template<>
UpdateToRender(const RRect & value,PropertyUpdateType type) const355 void RSProperty<RRect>::UpdateToRender(const RRect& value, PropertyUpdateType type) const
356 {
357 UPDATE_TO_RENDER(RSUpdatePropertyRRect, value, type);
358 }
359
360 template<>
IsValid(const float & value)361 bool RSProperty<float>::IsValid(const float& value)
362 {
363 return !isinf(value);
364 }
365 template<>
IsValid(const Vector2f & value)366 bool RSProperty<Vector2f>::IsValid(const Vector2f& value)
367 {
368 return !value.IsInfinite();
369 }
370 template<>
IsValid(const Vector4f & value)371 bool RSProperty<Vector4f>::IsValid(const Vector4f& value)
372 {
373 return !value.IsInfinite();
374 }
375
376 template<>
GetPropertyType() const377 RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const
378 {
379 return RSRenderPropertyType::PROPERTY_FLOAT;
380 }
381 template<>
GetPropertyType() const382 RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const
383 {
384 return RSRenderPropertyType::PROPERTY_COLOR;
385 }
386 template<>
GetPropertyType() const387 RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const
388 {
389 return RSRenderPropertyType::PROPERTY_MATRIX3F;
390 }
391 template<>
GetPropertyType() const392 RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const
393 {
394 return RSRenderPropertyType::PROPERTY_VECTOR2F;
395 }
396 template<>
GetPropertyType() const397 RSRenderPropertyType RSAnimatableProperty<Vector3f>::GetPropertyType() const
398 {
399 return RSRenderPropertyType::PROPERTY_VECTOR3F;
400 }
401 template<>
GetPropertyType() const402 RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const
403 {
404 return RSRenderPropertyType::PROPERTY_VECTOR4F;
405 }
406 template<>
GetPropertyType() const407 RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const
408 {
409 return RSRenderPropertyType::PROPERTY_QUATERNION;
410 }
411 template<>
GetPropertyType() const412 RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const
413 {
414 return RSRenderPropertyType::PROPERTY_FILTER;
415 }
416 template<>
GetPropertyType() const417 RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const
418 {
419 return RSRenderPropertyType::PROPERTY_VECTOR4_COLOR;
420 }
421 template<>
GetPropertyType() const422 RSRenderPropertyType RSAnimatableProperty<RRect>::GetPropertyType() const
423 {
424 return RSRenderPropertyType::PROPERTY_RRECT;
425 }
426 } // namespace Rosen
427 } // namespace OHOS
428