• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkPictureRecorder.h"
19 
20 #include "platform/common/rs_log.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 #ifndef USE_ROSEN_DRAWING
CreateGradientMask(const SkPaint & maskPaint)25 std::shared_ptr<RSMask> RSMask::CreateGradientMask(const SkPaint& maskPaint)
26 #else
27 std::shared_ptr<RSMask> RSMask::CreateGradientMask(const Drawing::Brush& maskBrush)
28 #endif
29 {
30     auto mask = std::make_shared<RSMask>();
31     if (mask) {
32 #ifndef USE_ROSEN_DRAWING
33         mask->SetMaskPaint(maskPaint);
34 #else
35         mask->SetMaskBrush(maskBrush);
36 #endif
37         mask->SetMaskType(MaskType::GRADIENT);
38     }
39     return mask;
40 }
41 
42 #ifndef USE_ROSEN_DRAWING
CreatePathMask(const SkPath & maskPath,const SkPaint & maskPaint)43 std::shared_ptr<RSMask> RSMask::CreatePathMask(const SkPath& maskPath, const SkPaint& maskPaint)
44 #else
45 std::shared_ptr<RSMask> RSMask::CreatePathMask(const Drawing::Path& maskPath, const Drawing::Brush& maskBrush)
46 #endif
47 {
48     auto mask = std::make_shared<RSMask>();
49     if (mask) {
50         mask->SetMaskPath(maskPath);
51 #ifndef USE_ROSEN_DRAWING
52         mask->SetMaskPaint(maskPaint);
53 #else
54         mask->SetMaskBrush(maskBrush);
55 #endif
56         mask->SetMaskType(MaskType::PATH);
57     }
58     return mask;
59 }
60 
CreateSVGMask(double x,double y,double scaleX,double scaleY,const sk_sp<SkSVGDOM> & svgDom)61 std::shared_ptr<RSMask> RSMask::CreateSVGMask(double x, double y, double scaleX, double scaleY,
62     const sk_sp<SkSVGDOM>& svgDom)
63 {
64     auto mask = std::make_shared<RSMask>();
65     if (mask) {
66         mask->SetSvgX(x);
67         mask->SetSvgY(y);
68         mask->SetScaleX(scaleX);
69         mask->SetScaleY(scaleY);
70         mask->SetSvgDom(svgDom);
71         mask->SetMaskType(MaskType::SVG);
72     }
73     return mask;
74 }
75 
RSMask()76 RSMask::RSMask()
77 {
78 }
79 
~RSMask()80 RSMask::~RSMask()
81 {
82 }
83 
SetSvgX(double x)84 void RSMask::SetSvgX(double x)
85 {
86     svgX_ = x;
87 }
88 
GetSvgX() const89 double RSMask::GetSvgX() const
90 {
91     return svgX_;
92 }
93 
SetSvgY(double y)94 void RSMask::SetSvgY(double y)
95 {
96     svgY_ = y;
97 }
98 
GetSvgY() const99 double RSMask::GetSvgY() const
100 {
101     return svgY_;
102 }
103 
SetScaleX(double scaleX)104 void RSMask::SetScaleX(double scaleX)
105 {
106     scaleX_ = scaleX;
107 }
108 
GetScaleX() const109 double RSMask::GetScaleX() const
110 {
111     return scaleX_;
112 }
113 
SetScaleY(double scaleY)114 void RSMask::SetScaleY(double scaleY)
115 {
116     scaleY_ = scaleY;
117 }
118 
GetScaleY() const119 double RSMask::GetScaleY() const
120 {
121     return scaleY_;
122 }
123 
124 #ifndef USE_ROSEN_DRAWING
SetMaskPath(const SkPath & path)125 void RSMask::SetMaskPath(const SkPath& path)
126 #else
127 void RSMask::SetMaskPath(const Drawing::Path& path)
128 #endif
129 {
130     maskPath_ = path;
131 }
132 
133 #ifndef USE_ROSEN_DRAWING
GetMaskPath() const134 SkPath RSMask::GetMaskPath() const
135 #else
136 Drawing::Path RSMask::GetMaskPath() const
137 #endif
138 {
139     return maskPath_;
140 }
141 
142 #ifndef USE_ROSEN_DRAWING
SetMaskPaint(const SkPaint & paint)143 void RSMask::SetMaskPaint(const SkPaint& paint)
144 {
145     maskPaint_ = paint;
146 }
147 
GetMaskPaint() const148 SkPaint RSMask::GetMaskPaint() const
149 {
150     return maskPaint_;
151 }
152 #else
SetMaskBrush(const Drawing::Brush & brush)153 void RSMask::SetMaskBrush(const Drawing::Brush& brush)
154 {
155     maskBrush_ = brush;
156 }
157 
GetMaskBrush() const158 Drawing::Brush RSMask::GetMaskBrush() const
159 {
160     return maskBrush_;
161 }
162 #endif
163 
SetSvgDom(const sk_sp<SkSVGDOM> & svgDom)164 void RSMask::SetSvgDom(const sk_sp<SkSVGDOM>& svgDom)
165 {
166     svgDom_ = svgDom;
167 }
168 
GetSvgDom() const169 sk_sp<SkSVGDOM> RSMask::GetSvgDom() const
170 {
171     return svgDom_;
172 }
173 
174 #ifndef USE_ROSEN_DRAWING
GetSvgPicture() const175 sk_sp<SkPicture> RSMask::GetSvgPicture() const
176 #else
177 std::shared_ptr<Drawing::Picture> RSMask::GetSvgPicture() const
178 #endif
179 {
180     return svgPicture_;
181 }
182 
SetMaskType(MaskType type)183 void RSMask::SetMaskType(MaskType type)
184 {
185     type_ = type;
186 }
187 
IsSvgMask() const188 bool RSMask::IsSvgMask() const
189 {
190     return (type_ == MaskType::SVG);
191 }
192 
IsGradientMask() const193 bool RSMask::IsGradientMask() const
194 {
195     return (type_ == MaskType::GRADIENT);
196 }
197 
IsPathMask() const198 bool RSMask::IsPathMask() const
199 {
200     return (type_ == MaskType::PATH);
201 }
202 
203 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const204 bool RSMask::Marshalling(Parcel& parcel) const
205 {
206     if (!(RSMarshallingHelper::Marshalling(parcel, type_) &&
207             RSMarshallingHelper::Marshalling(parcel, svgX_) &&
208             RSMarshallingHelper::Marshalling(parcel, svgY_) &&
209             RSMarshallingHelper::Marshalling(parcel, scaleX_) &&
210             RSMarshallingHelper::Marshalling(parcel, scaleY_) &&
211 #ifndef USE_ROSEN_DRAWING
212             RSMarshallingHelper::Marshalling(parcel, maskPaint_) &&
213 #else
214             RSMarshallingHelper::Marshalling(parcel, maskBrush_) &&
215 #endif
216             RSMarshallingHelper::Marshalling(parcel, maskPath_))) {
217         ROSEN_LOGE("RSMask::Marshalling failed!");
218         return false;
219     }
220     if (IsSvgMask()) {
221         ROSEN_LOGD("SVG RSMask::Marshalling");
222         SkPictureRecorder recorder;
223         SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeSize(svgDom_->containerSize()));
224         svgDom_->render(recordingCanvas);
225         sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
226         if (!RSMarshallingHelper::Marshalling(parcel, picture)) {
227             ROSEN_LOGE("RSMask::Marshalling SkPicture failed!");
228             return false;
229         }
230     }
231     return true;
232 }
233 
Unmarshalling(Parcel & parcel)234 RSMask* RSMask::Unmarshalling(Parcel& parcel)
235 {
236     auto rsMask = std::make_unique<RSMask>();
237     if (!(RSMarshallingHelper::Unmarshalling(parcel, rsMask->type_) &&
238             RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgX_) &&
239             RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgY_) &&
240             RSMarshallingHelper::Unmarshalling(parcel, rsMask->scaleX_) &&
241             RSMarshallingHelper::Unmarshalling(parcel, rsMask->scaleY_) &&
242 #ifndef USE_ROSEN_DRAWING
243             RSMarshallingHelper::Unmarshalling(parcel, rsMask->maskPaint_) &&
244 #else
245             RSMarshallingHelper::Unmarshalling(parcel, rsMask->maskBrush_) &&
246 #endif
247             RSMarshallingHelper::Unmarshalling(parcel, rsMask->maskPath_))) {
248         ROSEN_LOGE("RSMask::Unmarshalling failed!");
249         return nullptr;
250     }
251     if (rsMask->IsSvgMask()) {
252         ROSEN_LOGD("SVG RSMask::Unmarshalling");
253         if (!RSMarshallingHelper::Unmarshalling(parcel, rsMask->svgPicture_)) {
254             ROSEN_LOGE("RSMask::Unmarshalling SkPicture failed!");
255             return nullptr;
256         }
257     }
258     return rsMask.release();
259 }
260 #endif
261 } // namespace Rosen
262 } // namespace OHOS