1 /*
2 * Copyright (c) 2022 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
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr int PID_SHIFT = 32;
26
GeneratePropertyId()27 PropertyId GeneratePropertyId()
28 {
29 static pid_t pid_ = GetRealPid();
30 static std::atomic<uint32_t> currentId_ = 1;
31
32 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
33 if (currentId == UINT32_MAX) {
34 // [PLANNING]:process the overflow situations
35 ROSEN_LOGE("Property Id overflow");
36 }
37
38 return ((PropertyId)pid_ << PID_SHIFT) | currentId;
39 }
40 } // namespace
41
RSPropertyBase()42 RSPropertyBase::RSPropertyBase() : id_(GeneratePropertyId())
43 {}
44
MarkModifierDirty()45 void RSPropertyBase::MarkModifierDirty()
46 {
47 auto modifier = modifier_.lock();
48 if (modifier != nullptr) {
49 modifier->SetDirty(true);
50 }
51 }
52
UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode> & node)53 void RSPropertyBase::UpdateExtendModifierForGeometry(const std::shared_ptr<RSNode>& node)
54 {
55 if (type_ == RSModifierType::BOUNDS || type_ == RSModifierType::FRAME) {
56 node->MarkAllExtendModifierDirty();
57 }
58 }
59
operator +=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)60 std::shared_ptr<RSPropertyBase> operator+=(const std::shared_ptr<RSPropertyBase>& a,
61 const std::shared_ptr<const RSPropertyBase>& b)
62 {
63 if (a == nullptr) {
64 return {};
65 }
66
67 return a->Add(b);
68 }
69
operator -=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)70 std::shared_ptr<RSPropertyBase> operator-=(const std::shared_ptr<RSPropertyBase>& a,
71 const std::shared_ptr<const RSPropertyBase>& b)
72 {
73 if (a == nullptr) {
74 return {};
75 }
76
77 return a->Minus(b);
78 }
79
operator *=(const std::shared_ptr<RSPropertyBase> & value,const float scale)80 std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale)
81 {
82 if (value == nullptr) {
83 return {};
84 }
85
86 return value->Multiply(scale);
87 }
88
operator +(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)89 std::shared_ptr<RSPropertyBase> operator+(const std::shared_ptr<const RSPropertyBase>& a,
90 const std::shared_ptr<const RSPropertyBase>& b)
91 {
92 if (a == nullptr) {
93 return {};
94 }
95
96 return a->Clone()->Add(b);
97 }
98
operator -(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)99 std::shared_ptr<RSPropertyBase> operator-(const std::shared_ptr<const RSPropertyBase>& a,
100 const std::shared_ptr<const RSPropertyBase>& b)
101 {
102 if (a == nullptr) {
103 return {};
104 }
105
106 return a->Clone()->Minus(b);
107 }
108
operator *(const std::shared_ptr<const RSPropertyBase> & value,const float scale)109 std::shared_ptr<RSPropertyBase> operator*(const std::shared_ptr<const RSPropertyBase>& value, const float scale)
110 {
111 if (value == nullptr) {
112 return {};
113 }
114
115 return value->Clone()->Multiply(scale);
116 }
117
operator ==(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)118 bool operator==(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
119 {
120 if (a == nullptr) {
121 return false;
122 }
123
124 return a->IsEqual(b);
125 }
126
operator !=(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)127 bool operator!=(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
128 {
129 if (a == nullptr) {
130 return false;
131 }
132
133 return !a->IsEqual(b);
134 }
135
136 #define UPDATE_TO_RENDER(Command, value, isDelta, forceUpdate) \
137 do { \
138 auto node = target_.lock(); \
139 auto transactionProxy = RSTransactionProxy::GetInstance(); \
140 if (transactionProxy && node) { \
141 std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, isDelta); \
142 if (forceUpdate) { \
143 transactionProxy->Begin(); \
144 } \
145 transactionProxy->AddCommand(command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
146 if (node->NeedForcedSendToRemote()) { \
147 std::unique_ptr<RSCommand> commandForRemote = \
148 std::make_unique<Command>(node->GetId(), value, id_, isDelta); \
149 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); \
150 } \
151 if (forceUpdate) { \
152 transactionProxy->Commit(); \
153 } \
154 } \
155 } while (0)
156
157 template<>
UpdateToRender(const bool & value,bool isDelta,bool forceUpdate) const158 void RSProperty<bool>::UpdateToRender(const bool& value, bool isDelta, bool forceUpdate) const
159 {
160 UPDATE_TO_RENDER(RSUpdatePropertyBool, value, isDelta, forceUpdate);
161 }
162 template<>
UpdateToRender(const float & value,bool isDelta,bool forceUpdate) const163 void RSProperty<float>::UpdateToRender(const float& value, bool isDelta, bool forceUpdate) const
164 {
165 UPDATE_TO_RENDER(RSUpdatePropertyFloat, value, isDelta, forceUpdate);
166 }
167 template<>
UpdateToRender(const int & value,bool isDelta,bool forceUpdate) const168 void RSProperty<int>::UpdateToRender(const int& value, bool isDelta, bool forceUpdate) const
169 {
170 UPDATE_TO_RENDER(RSUpdatePropertyInt, value, isDelta, forceUpdate);
171 }
172 template<>
UpdateToRender(const Color & value,bool isDelta,bool forceUpdate) const173 void RSProperty<Color>::UpdateToRender(const Color& value, bool isDelta, bool forceUpdate) const
174 {
175 UPDATE_TO_RENDER(RSUpdatePropertyColor, value, isDelta, forceUpdate);
176 }
177 template<>
UpdateToRender(const Gravity & value,bool isDelta,bool forceUpdate) const178 void RSProperty<Gravity>::UpdateToRender(const Gravity& value, bool isDelta, bool forceUpdate) const
179 {
180 UPDATE_TO_RENDER(RSUpdatePropertyGravity, value, isDelta, forceUpdate);
181 }
182 template<>
UpdateToRender(const Matrix3f & value,bool isDelta,bool forceUpdate) const183 void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, bool isDelta, bool forceUpdate) const
184 {
185 UPDATE_TO_RENDER(RSUpdatePropertyMatrix3f, value, isDelta, forceUpdate);
186 }
187 template<>
UpdateToRender(const Quaternion & value,bool isDelta,bool forceUpdate) const188 void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, bool isDelta, bool forceUpdate) const
189 {
190 UPDATE_TO_RENDER(RSUpdatePropertyQuaternion, value, isDelta, forceUpdate);
191 }
192 template<>
UpdateToRender(const std::shared_ptr<RSFilter> & value,bool isDelta,bool forceUpdate) const193 void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
194 const std::shared_ptr<RSFilter>& value, bool isDelta, bool forceUpdate) const
195 {
196 UPDATE_TO_RENDER(RSUpdatePropertyFilter, value, isDelta, forceUpdate);
197 }
198 template<>
UpdateToRender(const std::shared_ptr<RSImage> & value,bool isDelta,bool forceUpdate) const199 void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
200 const std::shared_ptr<RSImage>& value, bool isDelta, bool forceUpdate) const
201 {
202 UPDATE_TO_RENDER(RSUpdatePropertyImage, value, isDelta, forceUpdate);
203 }
204 template<>
UpdateToRender(const std::shared_ptr<RSMask> & value,bool isDelta,bool forceUpdate) const205 void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
206 const std::shared_ptr<RSMask>& value, bool isDelta, bool forceUpdate) const
207 {
208 UPDATE_TO_RENDER(RSUpdatePropertyMask, value, isDelta, forceUpdate);
209 }
210 template<>
UpdateToRender(const std::shared_ptr<RSPath> & value,bool isDelta,bool forceUpdate) const211 void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
212 const std::shared_ptr<RSPath>& value, bool isDelta, bool forceUpdate) const
213 {
214 UPDATE_TO_RENDER(RSUpdatePropertyPath, value, isDelta, forceUpdate);
215 }
216 template<>
UpdateToRender(const std::shared_ptr<RSLinearGradientBlurPara> & value,bool isDelta,bool forceUpdate) const217 void RSProperty<std::shared_ptr<RSLinearGradientBlurPara>>::UpdateToRender(
218 const std::shared_ptr<RSLinearGradientBlurPara>& value, bool isDelta, bool forceUpdate) const
219 {
220 UPDATE_TO_RENDER(RSUpdatePropertyLinearGradientBlurPara, value, isDelta, forceUpdate);
221 }
222 template<>
UpdateToRender(const std::shared_ptr<RSShader> & value,bool isDelta,bool forceUpdate) const223 void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
224 const std::shared_ptr<RSShader>& value, bool isDelta, bool forceUpdate) const
225 {
226 UPDATE_TO_RENDER(RSUpdatePropertyShader, value, isDelta, forceUpdate);
227 }
228 template<>
UpdateToRender(const Vector2f & value,bool isDelta,bool forceUpdate) const229 void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, bool isDelta, bool forceUpdate) const
230 {
231 UPDATE_TO_RENDER(RSUpdatePropertyVector2f, value, isDelta, forceUpdate);
232 }
233 template<>
UpdateToRender(const Vector4<uint32_t> & value,bool isDelta,bool forceUpdate) const234 void RSProperty<Vector4<uint32_t>>::UpdateToRender(const Vector4<uint32_t>& value,
235 bool isDelta, bool forceUpdate) const
236 {
237 UPDATE_TO_RENDER(RSUpdatePropertyBorderStyle, value, isDelta, forceUpdate);
238 }
239 template<>
UpdateToRender(const Vector4<Color> & value,bool isDelta,bool forceUpdate) const240 void RSProperty<Vector4<Color>>::UpdateToRender(const Vector4<Color>& value,
241 bool isDelta, bool forceUpdate) const
242 {
243 UPDATE_TO_RENDER(RSUpdatePropertyVector4Color, value, isDelta, forceUpdate);
244 }
245 template<>
UpdateToRender(const Vector4f & value,bool isDelta,bool forceUpdate) const246 void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, bool isDelta, bool forceUpdate) const
247 {
248 UPDATE_TO_RENDER(RSUpdatePropertyVector4f, value, isDelta, forceUpdate);
249 }
250
251 template<>
UpdateToRender(const RRect & value,bool isDelta,bool forceUpdate) const252 void RSProperty<RRect>::UpdateToRender(const RRect& value, bool isDelta, bool forceUpdate) const
253 {
254 UPDATE_TO_RENDER(RSUpdatePropertyRRect, value, isDelta, forceUpdate);
255 }
256
257 template<>
IsValid(const float & value)258 bool RSProperty<float>::IsValid(const float& value)
259 {
260 return !isinf(value);
261 }
262 template<>
IsValid(const Vector2f & value)263 bool RSProperty<Vector2f>::IsValid(const Vector2f& value)
264 {
265 return !value.IsInfinite();
266 }
267 template<>
IsValid(const Vector4f & value)268 bool RSProperty<Vector4f>::IsValid(const Vector4f& value)
269 {
270 return !value.IsInfinite();
271 }
272
273 template<>
GetPropertyType() const274 RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const
275 {
276 return RSRenderPropertyType::PROPERTY_FLOAT;
277 }
278 template<>
GetPropertyType() const279 RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const
280 {
281 return RSRenderPropertyType::PROPERTY_COLOR;
282 }
283 template<>
GetPropertyType() const284 RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const
285 {
286 return RSRenderPropertyType::PROPERTY_MATRIX3F;
287 }
288 template<>
GetPropertyType() const289 RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const
290 {
291 return RSRenderPropertyType::PROPERTY_VECTOR2F;
292 }
293 template<>
GetPropertyType() const294 RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const
295 {
296 return RSRenderPropertyType::PROPERTY_VECTOR4F;
297 }
298 template<>
GetPropertyType() const299 RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const
300 {
301 return RSRenderPropertyType::PROPERTY_QUATERNION;
302 }
303 template<>
GetPropertyType() const304 RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const
305 {
306 return RSRenderPropertyType::PROPERTY_FILTER;
307 }
308 template<>
GetPropertyType() const309 RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const
310 {
311 return RSRenderPropertyType::PROPERTY_VECTOR4_COLOR;
312 }
313 template<>
GetPropertyType() const314 RSRenderPropertyType RSAnimatableProperty<RRect>::GetPropertyType() const
315 {
316 return RSRenderPropertyType::PROPERTY_RRECT;
317 }
318 } // namespace Rosen
319 } // namespace OHOS
320