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 "render/rs_mask.h"
17
18 #include "platform/common/rs_log.h"
19 #include "third_party/skia/include/core/SkPictureRecorder.h"
20
21 namespace OHOS {
22 namespace Rosen {
CreateGradientMask(const SkPaint & maskPaint)23 std::shared_ptr<RSMask> RSMask::CreateGradientMask(const SkPaint& maskPaint)
24 {
25 auto mask = std::make_shared<RSMask>();
26 if (mask) {
27 mask->SetMaskPaint(maskPaint);
28 mask->SetMaskType(MaskType::GRADIENT);
29 }
30 return mask;
31 }
32
CreatePathMask(const SkPath & maskPath,const SkPaint & maskPaint)33 std::shared_ptr<RSMask> RSMask::CreatePathMask(const SkPath& maskPath, const SkPaint& maskPaint)
34 {
35 auto mask = std::make_shared<RSMask>();
36 if (mask) {
37 mask->SetMaskPath(maskPath);
38 mask->SetMaskPaint(maskPaint);
39 mask->SetMaskType(MaskType::PATH);
40 }
41 return mask;
42 }
43
CreateSVGMask(double x,double y,double scaleX,double scaleY,const sk_sp<SkSVGDOM> & svgDom)44 std::shared_ptr<RSMask> RSMask::CreateSVGMask(double x, double y, double scaleX, double scaleY,
45 const sk_sp<SkSVGDOM>& svgDom)
46 {
47 auto mask = std::make_shared<RSMask>();
48 if (mask) {
49 mask->SetSvgX(x);
50 mask->SetSvgY(y);
51 mask->SetScaleX(scaleX);
52 mask->SetScaleY(scaleY);
53 mask->SetSvgDom(svgDom);
54 mask->SetMaskType(MaskType::SVG);
55 }
56 return mask;
57 }
58
RSMask()59 RSMask::RSMask()
60 {
61 }
62
~RSMask()63 RSMask::~RSMask()
64 {
65 }
66
SetSvgX(double x)67 void RSMask::SetSvgX(double x)
68 {
69 svgX_ = x;
70 }
71
GetSvgX() const72 double RSMask::GetSvgX() const
73 {
74 return svgX_;
75 }
76
SetSvgY(double y)77 void RSMask::SetSvgY(double y)
78 {
79 svgY_ = y;
80 }
81
GetSvgY() const82 double RSMask::GetSvgY() const
83 {
84 return svgY_;
85 }
86
SetScaleX(double scaleX)87 void RSMask::SetScaleX(double scaleX)
88 {
89 scaleX_ = scaleX;
90 }
91
GetScaleX() const92 double RSMask::GetScaleX() const
93 {
94 return scaleX_;
95 }
96
SetScaleY(double scaleY)97 void RSMask::SetScaleY(double scaleY)
98 {
99 scaleY_ = scaleY;
100 }
101
GetScaleY() const102 double RSMask::GetScaleY() const
103 {
104 return scaleY_;
105 }
106
SetMaskPath(const SkPath & path)107 void RSMask::SetMaskPath(const SkPath& path)
108 {
109 maskPath_ = path;
110 }
111
GetMaskPath() const112 SkPath RSMask::GetMaskPath() const
113 {
114 return maskPath_;
115 }
116
SetMaskPaint(const SkPaint & paint)117 void RSMask::SetMaskPaint(const SkPaint& paint)
118 {
119 maskPaint_ = paint;
120 }
121
GetMaskPaint() const122 SkPaint RSMask::GetMaskPaint() const
123 {
124 return maskPaint_;
125 }
126
SetSvgDom(const sk_sp<SkSVGDOM> & svgDom)127 void RSMask::SetSvgDom(const sk_sp<SkSVGDOM>& svgDom)
128 {
129 svgDom_ = svgDom;
130 }
131
GetSvgDom() const132 sk_sp<SkSVGDOM> RSMask::GetSvgDom() const
133 {
134 return svgDom_;
135 }
136
GetSvgPicture() const137 sk_sp<SkPicture> RSMask::GetSvgPicture() const
138 {
139 return svgPicture_;
140 }
141
SetMaskType(MaskType type)142 void RSMask::SetMaskType(MaskType type)
143 {
144 type_ = type;
145 }
146
IsSvgMask() const147 bool RSMask::IsSvgMask() const
148 {
149 return (type_ == MaskType::SVG);
150 }
151
IsGradientMask() const152 bool RSMask::IsGradientMask() const
153 {
154 return (type_ == MaskType::GRADIENT);
155 }
156
IsPathMask() const157 bool RSMask::IsPathMask() const
158 {
159 return (type_ == MaskType::PATH);
160 }
161
162 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const163 bool RSMask::Marshalling(Parcel& parcel) const
164 {
165 if (!(RSMarshallingHelper::Marshalling(parcel, type_) &&
166 RSMarshallingHelper::Marshalling(parcel, svgX_) &&
167 RSMarshallingHelper::Marshalling(parcel, svgY_) &&
168 RSMarshallingHelper::Marshalling(parcel, scaleX_) &&
169 RSMarshallingHelper::Marshalling(parcel, scaleY_) &&
170 RSMarshallingHelper::Marshalling(parcel, maskPaint_) &&
171 RSMarshallingHelper::Marshalling(parcel, maskPath_))) {
172 ROSEN_LOGE("RSMask::Marshalling failed!");
173 return false;
174 }
175 if (IsSvgMask()) {
176 ROSEN_LOGD("SVG RSMask::Marshalling");
177 SkPictureRecorder recorder;
178 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeSize(svgDom_->containerSize()));
179 svgDom_->render(recordingCanvas);
180 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
181 if (!RSMarshallingHelper::Marshalling(parcel, picture)) {
182 ROSEN_LOGE("RSMask::Marshalling SkPicture failed!");
183 return false;
184 }
185 }
186 return true;
187 }
188
Unmarshalling(Parcel & parcel)189 RSMask* RSMask::Unmarshalling(Parcel& parcel)
190 {
191 auto rsMask = std::make_unique<RSMask>();
192 if (!(RSMarshallingHelper::Unmarshalling(parcel, rsMask->type_) &&
193 RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgX_) &&
194 RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgY_) &&
195 RSMarshallingHelper::Unmarshalling(parcel, rsMask->scaleX_) &&
196 RSMarshallingHelper::Unmarshalling(parcel, rsMask->scaleY_) &&
197 RSMarshallingHelper::Unmarshalling(parcel, rsMask->maskPaint_) &&
198 RSMarshallingHelper::Unmarshalling(parcel, rsMask->maskPath_))) {
199 ROSEN_LOGE("RSMask::Unmarshalling failed!");
200 return nullptr;
201 }
202 if (rsMask->IsSvgMask()) {
203 ROSEN_LOGD("SVG RSMask::Unmarshalling");
204 if (!RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgPicture_)) {
205 ROSEN_LOGE("RSMask::Unmarshalling SkPicture failed!");
206 return nullptr;
207 }
208 }
209 return rsMask.release();
210 }
211 #endif
212 } // namespace Rosen
213 } // namespace OHOS