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 "sandbox_utils.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 constexpr int PID_SHIFT = 32;
25
GeneratePropertyId()26 PropertyId GeneratePropertyId()
27 {
28 static pid_t pid_ = GetRealPid();
29 static std::atomic<uint32_t> currentId_ = 1;
30
31 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
32 if (currentId == UINT32_MAX) {
33 // [PLANNING]:process the overflow situations
34 ROSEN_LOGE("Property Id overflow");
35 }
36
37 return ((PropertyId)pid_ << PID_SHIFT) | currentId;
38 }
39 } // namespace
40
RSPropertyBase()41 RSPropertyBase::RSPropertyBase() : id_(GeneratePropertyId())
42 {}
43
operator +=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)44 std::shared_ptr<RSPropertyBase> operator+=(const std::shared_ptr<RSPropertyBase>& a,
45 const std::shared_ptr<const RSPropertyBase>& b)
46 {
47 if (a == nullptr) {
48 return {};
49 }
50
51 return a->Add(b);
52 }
53
operator -=(const std::shared_ptr<RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)54 std::shared_ptr<RSPropertyBase> operator-=(const std::shared_ptr<RSPropertyBase>& a,
55 const std::shared_ptr<const RSPropertyBase>& b)
56 {
57 if (a == nullptr) {
58 return {};
59 }
60
61 return a->Minus(b);
62 }
63
operator *=(const std::shared_ptr<RSPropertyBase> & value,const float scale)64 std::shared_ptr<RSPropertyBase> operator*=(const std::shared_ptr<RSPropertyBase>& value, const float scale)
65 {
66 if (value == nullptr) {
67 return {};
68 }
69
70 return value->Multiply(scale);
71 }
72
operator +(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)73 std::shared_ptr<RSPropertyBase> operator+(const std::shared_ptr<const RSPropertyBase>& a,
74 const std::shared_ptr<const RSPropertyBase>& b)
75 {
76 if (a == nullptr) {
77 return {};
78 }
79
80 return a->Clone()->Add(b);
81 }
82
operator -(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)83 std::shared_ptr<RSPropertyBase> operator-(const std::shared_ptr<const RSPropertyBase>& a,
84 const std::shared_ptr<const RSPropertyBase>& b)
85 {
86 if (a == nullptr) {
87 return {};
88 }
89
90 return a->Clone()->Minus(b);
91 }
92
operator *(const std::shared_ptr<const RSPropertyBase> & value,const float scale)93 std::shared_ptr<RSPropertyBase> operator*(const std::shared_ptr<const RSPropertyBase>& value, const float scale)
94 {
95 if (value == nullptr) {
96 return {};
97 }
98
99 return value->Clone()->Multiply(scale);
100 }
101
operator ==(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)102 bool operator==(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
103 {
104 if (a == nullptr) {
105 return false;
106 }
107
108 return a->IsEqual(b);
109 }
110
operator !=(const std::shared_ptr<const RSPropertyBase> & a,const std::shared_ptr<const RSPropertyBase> & b)111 bool operator!=(const std::shared_ptr<const RSPropertyBase>& a, const std::shared_ptr<const RSPropertyBase>& b)
112 {
113 if (a == nullptr) {
114 return false;
115 }
116
117 return !a->IsEqual(b);
118 }
119
120 #define UPDATE_TO_RENDER(Command, value, isDelta, forceUpdate) \
121 do { \
122 auto node = target_.lock(); \
123 auto transactionProxy = RSTransactionProxy::GetInstance(); \
124 if (transactionProxy && node) { \
125 std::unique_ptr<RSCommand> command = std::make_unique<Command>(node->GetId(), value, id_, isDelta); \
126 if (forceUpdate) { \
127 transactionProxy->Begin(); \
128 } \
129 transactionProxy->AddCommand(command, node->IsRenderServiceNode(), node->GetFollowType(), node->GetId()); \
130 if (node->NeedForcedSendToRemote()) { \
131 std::unique_ptr<RSCommand> commandForRemote = \
132 std::make_unique<Command>(node->GetId(), value, id_, isDelta); \
133 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); \
134 } \
135 if (node->NeedSendExtraCommand()) { \
136 std::unique_ptr<RSCommand> extraCommand = \
137 std::make_unique<Command>(node->GetId(), value, id_, isDelta); \
138 transactionProxy->AddCommand(extraCommand, !node->IsRenderServiceNode(), \
139 node->GetFollowType(), node->GetId()); \
140 } \
141 if (forceUpdate) { \
142 transactionProxy->Commit(); \
143 } \
144 } \
145 } while (0)
146
147 template<>
UpdateToRender(const bool & value,bool isDelta,bool forceUpdate) const148 void RSProperty<bool>::UpdateToRender(const bool& value, bool isDelta, bool forceUpdate) const
149 {
150 UPDATE_TO_RENDER(RSUpdatePropertyBool, value, isDelta, forceUpdate);
151 }
152 template<>
UpdateToRender(const float & value,bool isDelta,bool forceUpdate) const153 void RSProperty<float>::UpdateToRender(const float& value, bool isDelta, bool forceUpdate) const
154 {
155 UPDATE_TO_RENDER(RSUpdatePropertyFloat, value, isDelta, forceUpdate);
156 }
157 template<>
UpdateToRender(const int & value,bool isDelta,bool forceUpdate) const158 void RSProperty<int>::UpdateToRender(const int& value, bool isDelta, bool forceUpdate) const
159 {
160 UPDATE_TO_RENDER(RSUpdatePropertyInt, value, isDelta, forceUpdate);
161 }
162 template<>
UpdateToRender(const Color & value,bool isDelta,bool forceUpdate) const163 void RSProperty<Color>::UpdateToRender(const Color& value, bool isDelta, bool forceUpdate) const
164 {
165 UPDATE_TO_RENDER(RSUpdatePropertyColor, value, isDelta, forceUpdate);
166 }
167 template<>
UpdateToRender(const Gravity & value,bool isDelta,bool forceUpdate) const168 void RSProperty<Gravity>::UpdateToRender(const Gravity& value, bool isDelta, bool forceUpdate) const
169 {
170 UPDATE_TO_RENDER(RSUpdatePropertyGravity, value, isDelta, forceUpdate);
171 }
172 template<>
UpdateToRender(const Matrix3f & value,bool isDelta,bool forceUpdate) const173 void RSProperty<Matrix3f>::UpdateToRender(const Matrix3f& value, bool isDelta, bool forceUpdate) const
174 {
175 UPDATE_TO_RENDER(RSUpdatePropertyMatrix3f, value, isDelta, forceUpdate);
176 }
177 template<>
UpdateToRender(const Quaternion & value,bool isDelta,bool forceUpdate) const178 void RSProperty<Quaternion>::UpdateToRender(const Quaternion& value, bool isDelta, bool forceUpdate) const
179 {
180 UPDATE_TO_RENDER(RSUpdatePropertyQuaternion, value, isDelta, forceUpdate);
181 }
182 template<>
UpdateToRender(const std::shared_ptr<RSFilter> & value,bool isDelta,bool forceUpdate) const183 void RSProperty<std::shared_ptr<RSFilter>>::UpdateToRender(
184 const std::shared_ptr<RSFilter>& value, bool isDelta, bool forceUpdate) const
185 {
186 UPDATE_TO_RENDER(RSUpdatePropertyFilter, value, isDelta, forceUpdate);
187 }
188 template<>
UpdateToRender(const std::shared_ptr<RSImage> & value,bool isDelta,bool forceUpdate) const189 void RSProperty<std::shared_ptr<RSImage>>::UpdateToRender(
190 const std::shared_ptr<RSImage>& value, bool isDelta, bool forceUpdate) const
191 {
192 UPDATE_TO_RENDER(RSUpdatePropertyImage, value, isDelta, forceUpdate);
193 }
194 template<>
UpdateToRender(const std::shared_ptr<RSMask> & value,bool isDelta,bool forceUpdate) const195 void RSProperty<std::shared_ptr<RSMask>>::UpdateToRender(
196 const std::shared_ptr<RSMask>& value, bool isDelta, bool forceUpdate) const
197 {
198 UPDATE_TO_RENDER(RSUpdatePropertyMask, value, isDelta, forceUpdate);
199 }
200 template<>
UpdateToRender(const std::shared_ptr<RSPath> & value,bool isDelta,bool forceUpdate) const201 void RSProperty<std::shared_ptr<RSPath>>::UpdateToRender(
202 const std::shared_ptr<RSPath>& value, bool isDelta, bool forceUpdate) const
203 {
204 UPDATE_TO_RENDER(RSUpdatePropertyPath, value, isDelta, forceUpdate);
205 }
206 template<>
UpdateToRender(const std::shared_ptr<RSShader> & value,bool isDelta,bool forceUpdate) const207 void RSProperty<std::shared_ptr<RSShader>>::UpdateToRender(
208 const std::shared_ptr<RSShader>& value, bool isDelta, bool forceUpdate) const
209 {
210 UPDATE_TO_RENDER(RSUpdatePropertyShader, value, isDelta, forceUpdate);
211 }
212 template<>
UpdateToRender(const Vector2f & value,bool isDelta,bool forceUpdate) const213 void RSProperty<Vector2f>::UpdateToRender(const Vector2f& value, bool isDelta, bool forceUpdate) const
214 {
215 UPDATE_TO_RENDER(RSUpdatePropertyVector2f, value, isDelta, forceUpdate);
216 }
217 template<>
UpdateToRender(const Vector4<uint32_t> & value,bool isDelta,bool forceUpdate) const218 void RSProperty<Vector4<uint32_t>>::UpdateToRender(const Vector4<uint32_t>& value,
219 bool isDelta, bool forceUpdate) const
220 {
221 UPDATE_TO_RENDER(RSUpdatePropertyBorderStyle, value, isDelta, forceUpdate);
222 }
223 template<>
UpdateToRender(const Vector4<Color> & value,bool isDelta,bool forceUpdate) const224 void RSProperty<Vector4<Color>>::UpdateToRender(const Vector4<Color>& value,
225 bool isDelta, bool forceUpdate) const
226 {
227 UPDATE_TO_RENDER(RSUpdatePropertyVector4Color, value, isDelta, forceUpdate);
228 }
229 template<>
UpdateToRender(const Vector4f & value,bool isDelta,bool forceUpdate) const230 void RSProperty<Vector4f>::UpdateToRender(const Vector4f& value, bool isDelta, bool forceUpdate) const
231 {
232 UPDATE_TO_RENDER(RSUpdatePropertyVector4f, value, isDelta, forceUpdate);
233 }
234
235 template<>
IsValid(const float & value)236 bool RSProperty<float>::IsValid(const float& value)
237 {
238 return !isinf(value);
239 }
240 template<>
IsValid(const Vector2f & value)241 bool RSProperty<Vector2f>::IsValid(const Vector2f& value)
242 {
243 return !value.IsInfinite();
244 }
245 template<>
IsValid(const Vector4f & value)246 bool RSProperty<Vector4f>::IsValid(const Vector4f& value)
247 {
248 return !value.IsInfinite();
249 }
250
251 template<>
GetPropertyType() const252 RSRenderPropertyType RSAnimatableProperty<float>::GetPropertyType() const
253 {
254 return RSRenderPropertyType::PROPERTY_FLOAT;
255 }
256 template<>
GetPropertyType() const257 RSRenderPropertyType RSAnimatableProperty<Color>::GetPropertyType() const
258 {
259 return RSRenderPropertyType::PROPERTY_COLOR;
260 }
261 template<>
GetPropertyType() const262 RSRenderPropertyType RSAnimatableProperty<Matrix3f>::GetPropertyType() const
263 {
264 return RSRenderPropertyType::PROPERTY_MATRIX3F;
265 }
266 template<>
GetPropertyType() const267 RSRenderPropertyType RSAnimatableProperty<Vector2f>::GetPropertyType() const
268 {
269 return RSRenderPropertyType::PROPERTY_VECTOR2F;
270 }
271 template<>
GetPropertyType() const272 RSRenderPropertyType RSAnimatableProperty<Vector4f>::GetPropertyType() const
273 {
274 return RSRenderPropertyType::PROPERTY_VECTOR4F;
275 }
276 template<>
GetPropertyType() const277 RSRenderPropertyType RSAnimatableProperty<Quaternion>::GetPropertyType() const
278 {
279 return RSRenderPropertyType::PROPERTY_QUATERNION;
280 }
281 template<>
GetPropertyType() const282 RSRenderPropertyType RSAnimatableProperty<std::shared_ptr<RSFilter>>::GetPropertyType() const
283 {
284 return RSRenderPropertyType::PROPERTY_FILTER;
285 }
286 template<>
GetPropertyType() const287 RSRenderPropertyType RSAnimatableProperty<Vector4<Color>>::GetPropertyType() const
288 {
289 return RSRenderPropertyType::PROPERTY_VECTOR4_COLOR;
290 }
291 } // namespace Rosen
292 } // namespace OHOS
293