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
UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode> & node)54 void RSPropertyBase::UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode>& node)
55 {
56 if (type_ == RSModifierType::BOUNDS || type_ == RSModifierType::FRAME) {
57 node->MarkAllExtendModifierDirty();
58 }
59 }
60
GetThresholdByThresholdType(ThresholdType thresholdType) const61 float RSPropertyBase::GetThresholdByThresholdType(ThresholdType thresholdType) const
62 {
63 switch (thresholdType) {
64 case ThresholdType::LAYOUT:
65 return LAYOUT_NEAR_ZERO_THRESHOLD;
66 case ThresholdType::COARSE:
67 return FLOAT_NEAR_ZERO_COARSE_THRESHOLD;
68 case ThresholdType::MEDIUM:
69 return FLOAT_NEAR_ZERO_MEDIUM_THRESHOLD;
70 case ThresholdType::FINE:
71 return FLOAT_NEAR_ZERO_FINE_THRESHOLD;
72 case ThresholdType::COLOR:
73 return COLOR_NEAR_ZERO_THRESHOLD;
74 case ThresholdType::ZERO:
75 return ZERO;
76 default:
77 return DEFAULT_NEAR_ZERO_THRESHOLD;
78 }
79 }
80
operator +=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)81 std::shared_ptr<RSPropertyBase> operator+=(const std::shared_ptr<RSPropertyBase>& a,
82 const std::shared_ptr<const RSPropertyBase>& b)
83 {
84 if (a == nullptr) {
85 return {};
86 }
87
88 return a->Add(b);
89 }
90
operator -=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)91 std::shared_ptr<RSPropertyBase> operator-=(const std::shared_ptr<RSPropertyBase>& a,
92 const std::shared_ptr<const RSPropertyBase>& b)
93 {
94 if (a == nullptr) {
95 return {};
96 }
97
98 return a->Minus(b);
99 }
100
operator *=(const std::shared_ptr<RSPropertyBase> & value,const float scale)101 std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale)
102 {
103 if (value == nullptr) {
104 return {};
105 }
106
107 return value->Multiply(scale);
108 }
109
operator +(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)110 std::shared_ptr<RSPropertyBase> operator+(const std::shared_ptr<const RSPropertyBase>& a,
111 const std::shared_ptr<const RSPropertyBase>& b)
112 {
113 if (a == nullptr) {
114 return {};
115 }
116
117 return a->Clone()->Add(b);
118 }
119
operator -(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)120 std::shared_ptr<RSPropertyBase> operator-(const std::shared_ptr<const RSPropertyBase>& a,
121 const std::shared_ptr<const RSPropertyBase>& b)
122 {
123 if (a == nullptr) {
124 return {};
125 }
126
127 return a->Clone()->Minus(b);
128 }
129
operator *(const std::shared_ptr<const RSPropertyBase> & value,const float scale)130 std::shared_ptr<RSPropertyBase> operator*(const std::shared_ptr<const RSPropertyBase>& value, const float scale)
131 {
132 if (value == nullptr) {
133 return {};
134 }
135
136 return value->Clone()->Multiply(scale);
137 }
138
operator ==(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)139 bool operator==(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
140 {
141 if (a == nullptr) {
142 return false;
143 }
144
145 return a->IsEqual(b);
146 }
147
operator !=(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)148 bool operator!=(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
149 {
150 if (a == nullptr) {
151 return false;
152 }
153
154 return !a->IsEqual(b);
155 }
156
157 #define UPDATE_TO_RENDER(Command, value, type) \
158 do { \
159 auto node = target_.lock(); \
160 auto transactionProxy = RSTransactionProxy::GetInstance(); \
161 if (transactionProxy && node) { \
162 std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, type); \
163 transactionProxy->AddCommand(command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
164 if (node->NeedForcedSendToRemote()) { \
165 std::unique_ptr<RSCommand> commandForRemote = \
166 std::make_unique<Command>(node->GetId(), value, id_, type); \
167 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); \
168 } \
169 } \
170 } while (0)
171
172 template<>
UpdateToRender(const bool & value,PropertyUpdateType type) const173 void RSProperty<bool>::UpdateToRender(const bool& value, PropertyUpdateType type) const
174 {
175 UPDATE_TO_RENDER(RSUpdatePropertyBool, value, type);
176 }
177 template<>
UpdateToRender(const float & value,PropertyUpdateType type) const178 void RSProperty<float>::UpdateToRender(const float& value, PropertyUpdateType type) const
179 {
180 UPDATE_TO_RENDER(RSUpdatePropertyFloat, value, type);
181 }
182 template<>
UpdateToRender(const int & value,PropertyUpdateType type) const183 void RSProperty<int>::UpdateToRender(const int& value, PropertyUpdateType type) const
184 {
185 UPDATE_TO_RENDER(RSUpdatePropertyInt, value, type);
186 }
187 template<>
UpdateToRender(const Color & value,PropertyUpdateType type) const188 void RSProperty<Color>::UpdateToRender(const Color& value, PropertyUpdateType type) const
189 {
190 UPDATE_TO_RENDER(RSUpdatePropertyColor, value, type);
191 }
192 template<>
UpdateToRender(const Gravity & value,PropertyUpdateType type) const193 void RSProperty<Gravity>::UpdateToRender(const Gravity& value, PropertyUpdateType type) const
194 {
195 UPDATE_TO_RENDER(RSUpdatePropertyGravity, value, type);
196 }
197 template<>
UpdateToRender(const Matrix3f & value,PropertyUpdateType type) const198 void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, PropertyUpdateType type) const
199 {
200 UPDATE_TO_RENDER(RSUpdatePropertyMatrix3f, value, type);
201 }
202 template<>
UpdateToRender(const Quaternion & value,PropertyUpdateType type) const203 void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, PropertyUpdateType type) const
204 {
205 UPDATE_TO_RENDER(RSUpdatePropertyQuaternion, value, type);
206 }
207 template<>
UpdateToRender(const std::shared_ptr<RSFilter> & value,PropertyUpdateType type) const208 void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
209 const std::shared_ptr<RSFilter>& value, PropertyUpdateType type) const
210 {
211 UPDATE_TO_RENDER(RSUpdatePropertyFilter, value, type);
212 }
213 template<>
UpdateToRender(const std::shared_ptr<RSImage> & value,PropertyUpdateType type) const214 void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
215 const std::shared_ptr<RSImage>& value, PropertyUpdateType type) const
216 {
217 UPDATE_TO_RENDER(RSUpdatePropertyImage, value, type);
218 }
219 template<>
UpdateToRender(const std::shared_ptr<RSMask> & value,PropertyUpdateType type) const220 void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
221 const std::shared_ptr<RSMask>& value, PropertyUpdateType type) const
222 {
223 UPDATE_TO_RENDER(RSUpdatePropertyMask, value, type);
224 }
225 template<>
UpdateToRender(const std::shared_ptr<RSPath> & value,PropertyUpdateType type) const226 void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
227 const std::shared_ptr<RSPath>& value, PropertyUpdateType type) const
228 {
229 UPDATE_TO_RENDER(RSUpdatePropertyPath, value, type);
230 }
231 template<>
UpdateToRender(const std::shared_ptr<RSLinearGradientBlurPara> & value,PropertyUpdateType type) const232 void RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>::UpdateToRender(
233 const std::shared_ptr<RSLinearGradientBlurPara>& value, PropertyUpdateType type) const
234 {
235 UPDATE_TO_RENDER(RSUpdatePropertyLinearGradientBlurPara, value, type);
236 }
237 template<>
UpdateToRender(const std::shared_ptr<RSShader> & value,PropertyUpdateType type) const238 void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
239 const std::shared_ptr<RSShader>& value, PropertyUpdateType type) const
240 {
241 UPDATE_TO_RENDER(RSUpdatePropertyShader, value, type);
242 }
243 template<>
UpdateToRender(const Vector2f & value,PropertyUpdateType type) const244 void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, PropertyUpdateType type) const
245 {
246 UPDATE_TO_RENDER(RSUpdatePropertyVector2f, value, type);
247 }
248 template<>
UpdateToRender(const Vector4<uint32_t> & value,PropertyUpdateType type) const249 void RSProperty<Vector4<uint32_t>>::UpdateToRender(const Vector4<uint32_t>& value,
250 PropertyUpdateType type) const
251 {
252 UPDATE_TO_RENDER(RSUpdatePropertyBorderStyle, value, type);
253 }
254 template<>
UpdateToRender(const Vector4<Color> & value,PropertyUpdateType type) const255 void RSProperty<Vector4<Color>>::UpdateToRender(const Vector4<Color>& value,
256 PropertyUpdateType type) const
257 {
258 UPDATE_TO_RENDER(RSUpdatePropertyVector4Color, value, type);
259 }
260 template<>
UpdateToRender(const Vector4f & value,PropertyUpdateType type) const261 void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, PropertyUpdateType type) const
262 {
263 UPDATE_TO_RENDER(RSUpdatePropertyVector4f, value, type);
264 }
265
266 template<>
UpdateToRender(const RRect & value,PropertyUpdateType type) const267 void RSProperty<RRect>::UpdateToRender(const RRect& value, PropertyUpdateType type) const
268 {
269 UPDATE_TO_RENDER(RSUpdatePropertyRRect, value, type);
270 }
271
272 template<>
IsValid(const float & value)273 bool RSProperty<float>::IsValid(const float& value)
274 {
275 return !isinf(value);
276 }
277 template<>
IsValid(const Vector2f & value)278 bool RSProperty<Vector2f>::IsValid(const Vector2f& value)
279 {
280 return !value.IsInfinite();
281 }
282 template<>
IsValid(const Vector4f & value)283 bool RSProperty<Vector4f>::IsValid(const Vector4f& value)
284 {
285 return !value.IsInfinite();
286 }
287
288 template<>
GetPropertyType() const289 RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const
290 {
291 return RSRenderPropertyType::PROPERTY_FLOAT;
292 }
293 template<>
GetPropertyType() const294 RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const
295 {
296 return RSRenderPropertyType::PROPERTY_COLOR;
297 }
298 template<>
GetPropertyType() const299 RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const
300 {
301 return RSRenderPropertyType::PROPERTY_MATRIX3F;
302 }
303 template<>
GetPropertyType() const304 RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const
305 {
306 return RSRenderPropertyType::PROPERTY_VECTOR2F;
307 }
308 template<>
GetPropertyType() const309 RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const
310 {
311 return RSRenderPropertyType::PROPERTY_VECTOR4F;
312 }
313 template<>
GetPropertyType() const314 RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const
315 {
316 return RSRenderPropertyType::PROPERTY_QUATERNION;
317 }
318 template<>
GetPropertyType() const319 RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const
320 {
321 return RSRenderPropertyType::PROPERTY_FILTER;
322 }
323 template<>
GetPropertyType() const324 RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const
325 {
326 return RSRenderPropertyType::PROPERTY_VECTOR4_COLOR;
327 }
328 template<>
GetPropertyType() const329 RSRenderPropertyType RSAnimatableProperty<RRect>::GetPropertyType() const
330 {
331 return RSRenderPropertyType::PROPERTY_RRECT;
332 }
333 } // namespace Rosen
334 } // namespace OHOS
335