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 namespace OHOS {
19 namespace Rosen {
20
Marshalling(Parcel & parcel,const std::shared_ptr<RSRenderPropertyBase> & val)21 bool RSRenderPropertyBase::Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val)
22 {
23 RSRenderPropertyType type = val->GetPropertyType();
24 if (!(parcel.WriteInt16(static_cast<int16_t>(type)))) {
25 return false;
26 }
27 switch (type) {
28 case RSRenderPropertyType::PROPERTY_FLOAT: {
29 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<float>>(val);
30 if (property == nullptr) {
31 return false;
32 }
33 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
34 }
35 case RSRenderPropertyType::PROPERTY_COLOR: {
36 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Color>>(val);
37 if (property == nullptr) {
38 return false;
39 }
40 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
41 }
42 case RSRenderPropertyType::PROPERTY_MATRIX3F: {
43 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Matrix3f>>(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_QUATERNION: {
50 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Quaternion>>(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_FILTER: {
57 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>>(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_VECTOR2F: {
64 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector2f>>(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_VECTOR4F: {
71 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector4f>>(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_VECTOR4_COLOR: {
78 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<Vector4<Color>>>(val);
79 if (property == nullptr) {
80 return false;
81 }
82 return parcel.WriteUint64(property->GetId()) && RSMarshallingHelper::Marshalling(parcel, property->Get());
83 }
84 default: {
85 return false;
86 }
87 }
88 return true;
89 }
90
Unmarshalling(Parcel & parcel,std::shared_ptr<RSRenderPropertyBase> & val)91 bool RSRenderPropertyBase::Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val)
92 {
93 int16_t typeId = 0;
94 if (!parcel.ReadInt16(typeId)) {
95 return false;
96 }
97 RSRenderPropertyType type = static_cast<RSRenderPropertyType>(typeId);
98 PropertyId id = 0;
99 if (!parcel.ReadUint64(id)) {
100 return false;
101 }
102 switch (type) {
103 case RSRenderPropertyType::PROPERTY_FLOAT: {
104 float value;
105 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
106 return false;
107 }
108 val.reset(new RSRenderAnimatableProperty<float>(value, id, type));
109 break;
110 }
111 case RSRenderPropertyType::PROPERTY_COLOR: {
112 Color value;
113 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
114 return false;
115 }
116 val.reset(new RSRenderAnimatableProperty<Color>(value, id, type));
117 break;
118 }
119 case RSRenderPropertyType::PROPERTY_MATRIX3F: {
120 Matrix3f value;
121 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
122 return false;
123 }
124 val.reset(new RSRenderAnimatableProperty<Matrix3f>(value, id, type));
125 break;
126 }
127 case RSRenderPropertyType::PROPERTY_QUATERNION: {
128 Quaternion value;
129 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
130 return false;
131 }
132 val.reset(new RSRenderAnimatableProperty<Quaternion>(value, id, type));
133 break;
134 }
135 case RSRenderPropertyType::PROPERTY_FILTER: {
136 std::shared_ptr<RSFilter> value;
137 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
138 return false;
139 }
140 val.reset(new RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>(value, id, type));
141 break;
142 }
143 case RSRenderPropertyType::PROPERTY_VECTOR2F: {
144 Vector2f value;
145 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
146 return false;
147 }
148 val.reset(new RSRenderAnimatableProperty<Vector2f>(value, id, type));
149 break;
150 }
151 case RSRenderPropertyType::PROPERTY_VECTOR4F: {
152 Vector4f value;
153 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
154 return false;
155 }
156 val.reset(new RSRenderAnimatableProperty<Vector4f>(value, id, type));
157 break;
158 }
159 case RSRenderPropertyType::PROPERTY_VECTOR4_COLOR: {
160 Vector4<Color> value;
161 if (!RSMarshallingHelper::Unmarshalling(parcel, value)) {
162 return false;
163 }
164 val.reset(new RSRenderAnimatableProperty<Vector4<Color>>(value, id, type));
165 break;
166 }
167 default: {
168 return false;
169 }
170 }
171 return val != nullptr;
172 }
173
174 template<>
ToFloat() const175 float RSRenderAnimatableProperty<float>::ToFloat() const
176 {
177 return std::fabs(RSRenderProperty<float>::stagingValue_);
178 }
179
180 template<>
ToFloat() const181 float RSRenderAnimatableProperty<Vector4f>::ToFloat() const
182 {
183 return RSRenderProperty<Vector4f>::stagingValue_.GetLength();
184 }
185
186 template<>
ToFloat() const187 float RSRenderAnimatableProperty<Quaternion>::ToFloat() const
188 {
189 return RSRenderProperty<Quaternion>::stagingValue_.GetLength();
190 }
191
192 template<>
ToFloat() const193 float RSRenderAnimatableProperty<Vector2f>::ToFloat() const
194 {
195 return RSRenderProperty<Vector2f>::stagingValue_.GetLength();
196 }
197
operator +=(const std::shared_ptr<RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)198 std::shared_ptr<RSRenderPropertyBase> operator+=(
199 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
200 {
201 if (a == nullptr) {
202 return {};
203 }
204
205 return a->Add(b);
206 }
207
operator -=(const std::shared_ptr<RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)208 std::shared_ptr<RSRenderPropertyBase> operator-=(
209 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
210 {
211 if (a == nullptr) {
212 return {};
213 }
214
215 return a->Minus(b);
216 }
217
operator *=(const std::shared_ptr<RSRenderPropertyBase> & value,const float scale)218 std::shared_ptr<RSRenderPropertyBase> operator*=(const std::shared_ptr<RSRenderPropertyBase>& value, const float scale)
219 {
220 if (value == nullptr) {
221 return {};
222 }
223
224 return value->Multiply(scale);
225 }
226
operator +(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)227 std::shared_ptr<RSRenderPropertyBase> operator+(
228 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
229 {
230 if (a == nullptr) {
231 return {};
232 }
233
234 return a->Clone()->Add(b);
235 }
236
operator -(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)237 std::shared_ptr<RSRenderPropertyBase> operator-(
238 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
239 {
240 if (a == nullptr) {
241 return {};
242 }
243
244 return a->Clone()->Minus(b);
245 }
246
operator *(const std::shared_ptr<const RSRenderPropertyBase> & value,const float scale)247 std::shared_ptr<RSRenderPropertyBase> operator*(
248 const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale)
249 {
250 if (value == nullptr) {
251 return {};
252 }
253
254 return value->Clone()->Multiply(scale);
255 }
256
operator ==(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)257 bool operator==(
258 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
259 {
260 if (a == nullptr) {
261 return {};
262 }
263
264 return a->IsEqual(b);
265 }
266
operator !=(const std::shared_ptr<const RSRenderPropertyBase> & a,const std::shared_ptr<const RSRenderPropertyBase> & b)267 bool operator!=(
268 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b)
269 {
270 if (a == nullptr) {
271 return {};
272 }
273
274 return !a->IsEqual(b);
275 }
276
277 template class RSRenderAnimatableProperty<float>;
278 template class RSRenderAnimatableProperty<Vector4f>;
279 template class RSRenderAnimatableProperty<Quaternion>;
280 template class RSRenderAnimatableProperty<Vector2f>;
281
282 } // namespace Rosen
283 } // namespace OHOS
284