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_render_property.h"
17
18 #include "pipeline/rs_render_node.h"
19
20 namespace OHOS {
21 namespace Rosen {
22
OnChange() const23 void RSRenderPropertyBase::OnChange() const
24 {
25 if (auto node = node_.lock()) {
26 node->SetDirty();
27 node->AddDirtyType(modifierType_);
28 if (modifierType_ < RSModifierType::BOUNDS || modifierType_ > RSModifierType::TRANSLATE_Z ||
29 modifierType_ == RSModifierType::POSITION_Z) {
30 node->MarkNonGeometryChanged();
31 }
32 }
33 }
34
Marshalling(Parcel & parcel,const std::shared_ptr<RSRenderPropertyBase> & val)35 bool RSRenderPropertyBase::Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val)
36 {
37 RSRenderPropertyType type = val->GetPropertyType();
38 if (!(parcel.WriteInt16(static_cast<int16_t>(type)))) {
39 return false;
40 }
41 switch (type) {
42 case RSRenderPropertyType::PROPERTY_FLOAT: {
43 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<float>>(val);
44 if (property == nullptr) {
45 return false;
46 }
47 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
48 }
49 case RSRenderPropertyType::PROPERTY_COLOR: {
50 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Color>>(val);
51 if (property == nullptr) {
52 return false;
53 }
54 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
55 }
56 case RSRenderPropertyType::PROPERTY_MATRIX3F: {
57 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Matrix3f>>(val);
58 if (property == nullptr) {
59 return false;
60 }
61 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
62 }
63 case RSRenderPropertyType::PROPERTY_QUATERNION: {
64 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Quaternion>>(val);
65 if (property == nullptr) {
66 return false;
67 }
68 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
69 }
70 case RSRenderPropertyType::PROPERTY_FILTER: {
71 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>>(val);
72 if (property == nullptr) {
73 return false;
74 }
75 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
76 }
77 case RSRenderPropertyType::PROPERTY_VECTOR2F: {
78 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector2f>>(val);
79 if (property == nullptr) {
80 return false;
81 }
82 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
83 }
84 case RSRenderPropertyType::PROPERTY_VECTOR4F: {
85 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector4f>>(val);
86 if (property == nullptr) {
87 return false;
88 }
89 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
90 }
91 case RSRenderPropertyType::PROPERTY_VECTOR4_COLOR: {
92 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector4<Color>>>(val);
93 if (property == nullptr) {
94 return false;
95 }
96 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
97 }
98 case RSRenderPropertyType::PROPERTY_RRECT: {
99 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<RRect>>(val);
100 if (property == nullptr) {
101 return false;
102 }
103 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
104 }
105 default: {
106 return false;
107 }
108 }
109 return true;
110 }
111
Unmarshalling(Parcel & parcel,std::shared_ptr<RSRenderPropertyBase> & val)112 bool RSRenderPropertyBase::Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val)
113 {
114 int16_t typeId = 0;
115 if (!parcel.ReadInt16(typeId)) {
116 return false;
117 }
118 RSRenderPropertyType type = static_cast<RSRenderPropertyType>(typeId);
119 PropertyId id = 0;
120 if (!parcel.ReadUint64(id)) {
121 return false;
122 }
123 switch (type) {
124 case RSRenderPropertyType::PROPERTY_FLOAT: {
125 float value;
126 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
127 return false;
128 }
129 val.reset(new RSRenderAnimatableProperty<float>(value, id, type));
130 break;
131 }
132 case RSRenderPropertyType::PROPERTY_COLOR: {
133 Color value;
134 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
135 return false;
136 }
137 val.reset(new RSRenderAnimatableProperty<Color>(value, id, type));
138 break;
139 }
140 case RSRenderPropertyType::PROPERTY_MATRIX3F: {
141 Matrix3f value;
142 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
143 return false;
144 }
145 val.reset(new RSRenderAnimatableProperty<Matrix3f>(value, id, type));
146 break;
147 }
148 case RSRenderPropertyType::PROPERTY_QUATERNION: {
149 Quaternion value;
150 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
151 return false;
152 }
153 val.reset(new RSRenderAnimatableProperty<Quaternion>(value, id, type));
154 break;
155 }
156 case RSRenderPropertyType::PROPERTY_FILTER: {
157 std::shared_ptr<RSFilter> value;
158 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
159 return false;
160 }
161 val.reset(new RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>(value, id, type));
162 break;
163 }
164 case RSRenderPropertyType::PROPERTY_VECTOR2F: {
165 Vector2f value;
166 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
167 return false;
168 }
169 val.reset(new RSRenderAnimatableProperty<Vector2f>(value, id, type));
170 break;
171 }
172 case RSRenderPropertyType::PROPERTY_VECTOR4F: {
173 Vector4f value;
174 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
175 return false;
176 }
177 val.reset(new RSRenderAnimatableProperty<Vector4f>(value, id, type));
178 break;
179 }
180 case RSRenderPropertyType::PROPERTY_VECTOR4_COLOR: {
181 Vector4<Color> value;
182 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
183 return false;
184 }
185 val.reset(new RSRenderAnimatableProperty<Vector4<Color>>(value, id, type));
186 break;
187 }
188 case RSRenderPropertyType::PROPERTY_RRECT: {
189 RRect value;
190 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
191 return false;
192 }
193 val.reset(new RSRenderAnimatableProperty<RRect>(value, id, type));
194 break;
195 }
196 default: {
197 return false;
198 }
199 }
200 return val != nullptr;
201 }
202
203 template<>
ToFloat() const204 float RSRenderAnimatableProperty<float>::ToFloat() const
205 {
206 return std::fabs(RSRenderProperty<float>::stagingValue_);
207 }
208
209 template<>
ToFloat() const210 float RSRenderAnimatableProperty<Vector4f>::ToFloat() const
211 {
212 return RSRenderProperty<Vector4f>::stagingValue_.GetLength();
213 }
214
215 template<>
ToFloat() const216 float RSRenderAnimatableProperty<Quaternion>::ToFloat() const
217 {
218 return RSRenderProperty<Quaternion>::stagingValue_.GetLength();
219 }
220
221 template<>
ToFloat() const222 float RSRenderAnimatableProperty<Vector2f>::ToFloat() const
223 {
224 return RSRenderProperty<Vector2f>::stagingValue_.GetLength();
225 }
226
227 template<>
IsNeedUpdate(const float & value) const228 bool RSRenderProperty<float>::IsNeedUpdate(const float& value) const
229 {
230 switch (modifierType_) {
231 case RSModifierType::ALPHA:
232 case RSModifierType::SHADOW_ALPHA: {
233 const float SHADOW_ALPHA_MINMODIFY = 1.0 / 256;
234 return std::abs(value - stagingValue_) >= SHADOW_ALPHA_MINMODIFY;
235 }
236 case RSModifierType::BG_IMAGE_WIDTH:
237 case RSModifierType::BG_IMAGE_HEIGHT:
238 case RSModifierType::BG_IMAGE_POSITION_X:
239 case RSModifierType::BG_IMAGE_POSITION_Y:
240 case RSModifierType::SHADOW_RADIUS: {
241 return std::abs(value - stagingValue_) >= 0.1f;
242 }
243 default: {
244 return true;
245 }
246 }
247 }
248
249 template class RSRenderProperty<float>;
250
operator +=(const std::shared_ptr<RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)251 std::shared_ptr<RSRenderPropertyBase> operator+=(
252 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
253 {
254 if (a == nullptr) {
255 return {};
256 }
257
258 return a->Add(b);
259 }
260
operator -=(const std::shared_ptr<RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)261 std::shared_ptr<RSRenderPropertyBase> operator-=(
262 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
263 {
264 if (a == nullptr) {
265 return {};
266 }
267
268 return a->Minus(b);
269 }
270
operator *=(const std::shared_ptr<RSRenderPropertyBase> & value,const float scale)271 std::shared_ptr<RSRenderPropertyBase> operator*=(const std::shared_ptr<RSRenderPropertyBase>& value, const float scale)
272 {
273 if (value == nullptr) {
274 return {};
275 }
276
277 return value->Multiply(scale);
278 }
279
operator +(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)280 std::shared_ptr<RSRenderPropertyBase> operator+(
281 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
282 {
283 if (a == nullptr) {
284 return {};
285 }
286
287 return a->Clone()->Add(b);
288 }
289
operator -(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)290 std::shared_ptr<RSRenderPropertyBase> operator-(
291 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
292 {
293 if (a == nullptr) {
294 return {};
295 }
296
297 return a->Clone()->Minus(b);
298 }
299
operator *(const std::shared_ptr<const RSRenderPropertyBase> & value,const float scale)300 std::shared_ptr<RSRenderPropertyBase> operator*(
301 const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale)
302 {
303 if (value == nullptr) {
304 return {};
305 }
306
307 return value->Clone()->Multiply(scale);
308 }
309
operator ==(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)310 bool operator==(
311 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
312 {
313 if (a == nullptr) {
314 return {};
315 }
316
317 return a->IsEqual(b);
318 }
319
operator !=(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)320 bool operator!=(
321 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
322 {
323 if (a == nullptr) {
324 return {};
325 }
326
327 return !a->IsEqual(b);
328 }
329
330 template class RSRenderAnimatableProperty<float>;
331 template class RSRenderAnimatableProperty<Vector4f>;
332 template class RSRenderAnimatableProperty<Quaternion>;
333 template class RSRenderAnimatableProperty<Vector2f>;
334
335 } // namespace Rosen
336 } // namespace OHOS
337