1 /*
2 * Copyright (c) 2025 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 "ui_effect/mask/include/radial_gradient_mask_para.h"
17 #include "platform/common/rs_log.h"
18
19 namespace OHOS {
20 namespace Rosen {
21
RadialGradientMaskPara(const RadialGradientMaskPara & other)22 RadialGradientMaskPara::RadialGradientMaskPara(const RadialGradientMaskPara& other)
23 {
24 this->type_ = other.type_;
25 this->center_ = other.center_;
26 this->radiusX_ = other.radiusX_;
27 this->radiusY_ = other.radiusY_;
28 this->colors_ = other.colors_;
29 this->positions_ = other.positions_;
30 }
31
Marshalling(Parcel & parcel) const32 bool RadialGradientMaskPara::Marshalling(Parcel& parcel) const
33 {
34 auto isSuccess = parcel.WriteUint16(static_cast<uint16_t>(type_)) &&
35 parcel.WriteUint16(static_cast<uint16_t>(type_)) &&
36 parcel.WriteFloat(center_.x_) && parcel.WriteFloat(center_.y_) &&
37 parcel.WriteFloat(radiusX_) && parcel.WriteFloat(radiusY_);
38 if (!isSuccess) {
39 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling write type failed");
40 return false;
41 }
42
43 if (colors_.size() > MaskPara::UNMARSHALLING_MAX_VECTOR_SIZE ||
44 positions_.size() > MaskPara::UNMARSHALLING_MAX_VECTOR_SIZE) {
45 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling colors or positions size is invalid");
46 return false;
47 }
48 if (!parcel.WriteUint32(colors_.size())) {
49 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling write colors size failed");
50 return false;
51 }
52 for (const auto& color : colors_) {
53 if (!parcel.WriteFloat(color)) {
54 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling write color failed");
55 return false;
56 }
57 }
58 if (!parcel.WriteUint32(positions_.size())) {
59 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling write positions size failed");
60 return false;
61 }
62 for (const auto& position : positions_) {
63 if (!parcel.WriteFloat(position)) {
64 RS_LOGE("[ui_effect] RadialGradientMaskPara Marshalling write position failed");
65 return false;
66 }
67 }
68 return true;
69 }
70
RegisterUnmarshallingCallback()71 void RadialGradientMaskPara::RegisterUnmarshallingCallback()
72 {
73 MaskPara::RegisterUnmarshallingCallback(MaskPara::Type::RADIAL_GRADIENT_MASK, OnUnmarshalling);
74 }
75
OnUnmarshalling(Parcel & parcel,std::shared_ptr<MaskPara> & val)76 bool RadialGradientMaskPara::OnUnmarshalling(Parcel& parcel, std::shared_ptr<MaskPara>& val)
77 {
78 uint16_t type = MaskPara::Type::NONE;
79 if (!parcel.ReadUint16(type) || type != MaskPara::Type::RADIAL_GRADIENT_MASK) {
80 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read type failed");
81 return false;
82 }
83
84 auto para = std::make_shared<RadialGradientMaskPara>();
85 Vector2f center = { 0.0f, 0.0f };
86 Vector2f radius = { 0.0f, 0.0f };
87 if (!(parcel.ReadFloat(center.x_) && parcel.ReadFloat(center.y_) &&
88 parcel.ReadFloat(radius.x_) && parcel.ReadFloat(radius.y_))) {
89 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read center or radius failed");
90 return false;
91 }
92 para->SetCenter(center);
93 para->SetRadiusX(radius.x_);
94 para->SetRadiusY(radius.y_);
95
96 uint32_t colorsSize = 0;
97 if (!parcel.ReadUint32(colorsSize) || colorsSize > MaskPara::UNMARSHALLING_MAX_VECTOR_SIZE) {
98 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read colors size failed");
99 return false;
100 }
101 std::vector<float> colors;
102 for (uint32_t i = 0; i < colorsSize; ++i) {
103 float color = 0.0f;
104 if (!parcel.ReadFloat(color)) {
105 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read color failed");
106 return false;
107 }
108 colors.emplace_back(color);
109 }
110 para->SetColors(colors);
111
112 uint32_t positionsSize = 0;
113 if (!parcel.ReadUint32(positionsSize) || positionsSize > MaskPara::UNMARSHALLING_MAX_VECTOR_SIZE) {
114 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read positions size failed");
115 return false;
116 }
117 std::vector<float> positions;
118 for (uint32_t i = 0; i < positionsSize; ++i) {
119 float position = 0.0f;
120 if (!parcel.ReadFloat(position)) {
121 RS_LOGE("[ui_effect] RadialGradientMaskPara OnUnmarshalling read position failed");
122 return false;
123 }
124 positions.emplace_back(position);
125 }
126 para->SetPositions(positions);
127
128 val = std::move(para);
129 return true;
130 }
131
Clone() const132 std::shared_ptr<MaskPara> RadialGradientMaskPara::Clone() const
133 {
134 return std::make_shared<RadialGradientMaskPara>(*this);
135 }
136
137 } // namespace Rosen
138 } // namespace OHOS