• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "pipeline/rs_recording_canvas.h"
17 
18 #include "platform/common/rs_log.h"
19 #include "pipeline/rs_draw_cmd.h"
20 
21 namespace OHOS {
22 namespace Rosen {
RSRecordingCanvas(int width,int height)23 RSRecordingCanvas::RSRecordingCanvas(int width, int height) : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height)
24 {
25     drawCmdList_ = std::make_shared<DrawCmdList>(width, height);
26 }
27 
~RSRecordingCanvas()28 RSRecordingCanvas::~RSRecordingCanvas() {}
29 
GetDrawCmdList() const30 std::shared_ptr<DrawCmdList> RSRecordingCanvas::GetDrawCmdList() const
31 {
32     return drawCmdList_;
33 }
34 
Clear() const35 void RSRecordingCanvas::Clear() const
36 {
37     if (drawCmdList_ == nullptr) {
38         return;
39     }
40     drawCmdList_->ClearOp();
41 }
42 
AddOp(std::unique_ptr<OpItem> && opItem)43 void RSRecordingCanvas::AddOp(std::unique_ptr<OpItem>&& opItem)
44 {
45     if (drawCmdList_ == nullptr || opItem == nullptr) {
46         ROSEN_LOGE("RSRecordingCanvas:AddOp, drawCmdList_ or opItem is nullptr");
47         return;
48     }
49     drawCmdList_->AddOp(std::move(opItem));
50 }
51 
onNewSurface(const SkImageInfo & info,const SkSurfaceProps & props)52 sk_sp<SkSurface> RSRecordingCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props)
53 {
54     return nullptr;
55 }
56 
onFlush()57 void RSRecordingCanvas::onFlush()
58 {
59     std::unique_ptr<OpItem> op = std::make_unique<FlushOpItem>();
60     AddOp(std::move(op));
61 }
62 
willSave()63 void RSRecordingCanvas::willSave()
64 {
65     std::unique_ptr<OpItem> op = std::make_unique<SaveOpItem>();
66     AddOp(std::move(op));
67     saveCount_++;
68 }
69 
getSaveLayerStrategy(const SaveLayerRec & rec)70 SkCanvas::SaveLayerStrategy RSRecordingCanvas::getSaveLayerStrategy(const SaveLayerRec& rec)
71 {
72     std::unique_ptr<OpItem> op = std::make_unique<SaveLayerOpItem>(rec);
73     AddOp(std::move(op));
74     saveCount_++;
75     return SkCanvas::kNoLayer_SaveLayerStrategy;
76 }
77 
willRestore()78 void RSRecordingCanvas::willRestore()
79 {
80     if (saveCount_ > 0) {
81         std::unique_ptr<OpItem> op = std::make_unique<RestoreOpItem>();
82         AddOp(std::move(op));
83         --saveCount_;
84     }
85 }
86 
didConcat(const SkMatrix & matrix)87 void RSRecordingCanvas::didConcat(const SkMatrix& matrix)
88 {
89     std::unique_ptr<OpItem> op = std::make_unique<ConcatOpItem>(matrix);
90     AddOp(std::move(op));
91 }
92 
didSetMatrix(const SkMatrix & matrix)93 void RSRecordingCanvas::didSetMatrix(const SkMatrix& matrix)
94 {
95     std::unique_ptr<OpItem> op = std::make_unique<MatrixOpItem>(matrix);
96     AddOp(std::move(op));
97 }
98 
didTranslate(SkScalar dx,SkScalar dy)99 void RSRecordingCanvas::didTranslate(SkScalar dx, SkScalar dy)
100 {
101     std::unique_ptr<OpItem> op = std::make_unique<TranslateOpItem>(dx, dy);
102     AddOp(std::move(op));
103 }
104 
onClipRect(const SkRect & rect,SkClipOp clipOp,ClipEdgeStyle style)105 void RSRecordingCanvas::onClipRect(const SkRect& rect, SkClipOp clipOp, ClipEdgeStyle style)
106 {
107     std::unique_ptr<OpItem> op = std::make_unique<ClipRectOpItem>(rect, clipOp, style == kSoft_ClipEdgeStyle);
108     AddOp(std::move(op));
109 }
110 
onClipRRect(const SkRRect & rrect,SkClipOp clipOp,ClipEdgeStyle style)111 void RSRecordingCanvas::onClipRRect(const SkRRect& rrect, SkClipOp clipOp, ClipEdgeStyle style)
112 {
113     std::unique_ptr<OpItem> op = std::make_unique<ClipRRectOpItem>(rrect, clipOp, style == kSoft_ClipEdgeStyle);
114     AddOp(std::move(op));
115 }
116 
onClipPath(const SkPath & path,SkClipOp clipOp,ClipEdgeStyle style)117 void RSRecordingCanvas::onClipPath(const SkPath& path, SkClipOp clipOp, ClipEdgeStyle style)
118 {
119     std::unique_ptr<OpItem> op = std::make_unique<ClipPathOpItem>(path, clipOp, style == kSoft_ClipEdgeStyle);
120     AddOp(std::move(op));
121 }
122 
onClipRegion(const SkRegion & region,SkClipOp clipop)123 void RSRecordingCanvas::onClipRegion(const SkRegion& region, SkClipOp clipop)
124 {
125     std::unique_ptr<OpItem> op = std::make_unique<ClipRegionOpItem>(region, clipop);
126     AddOp(std::move(op));
127 }
128 
onDrawPaint(const SkPaint & paint)129 void RSRecordingCanvas::onDrawPaint(const SkPaint& paint)
130 {
131     std::unique_ptr<OpItem> op = std::make_unique<PaintOpItem>(paint);
132     AddOp(std::move(op));
133 }
134 
DrawImageWithParm(const sk_sp<SkImage> img,int fitNum,int repeatNum,float radius,const SkPaint & paint)135 void RSRecordingCanvas::DrawImageWithParm(const sk_sp<SkImage>img, int fitNum, int repeatNum, float radius,
136     const SkPaint& paint)
137 {
138     std::unique_ptr<OpItem> op = std::make_unique<ImageWithParmOpItem>(img, fitNum, repeatNum, radius, paint);
139     AddOp(std::move(op));
140 }
141 
onDrawBehind(const SkPaint & paint)142 void RSRecordingCanvas::onDrawBehind(const SkPaint& paint)
143 {
144     // [PLANNING]: To be implemented
145     ROSEN_LOGE("RSRecordingCanvas::onDrawBehind not support yet");
146 }
147 
onDrawPath(const SkPath & path,const SkPaint & paint)148 void RSRecordingCanvas::onDrawPath(const SkPath& path, const SkPaint& paint)
149 {
150     std::unique_ptr<OpItem> op = std::make_unique<PathOpItem>(path, paint);
151     AddOp(std::move(op));
152 }
153 
onDrawRect(const SkRect & rect,const SkPaint & paint)154 void RSRecordingCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint)
155 {
156     std::unique_ptr<OpItem> op = std::make_unique<RectOpItem>(rect, paint);
157     AddOp(std::move(op));
158 }
159 
onDrawRegion(const SkRegion & region,const SkPaint & paint)160 void RSRecordingCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint)
161 {
162     std::unique_ptr<OpItem> op = std::make_unique<RegionOpItem>(region, paint);
163     AddOp(std::move(op));
164 }
165 
onDrawOval(const SkRect & oval,const SkPaint & paint)166 void RSRecordingCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint)
167 {
168     std::unique_ptr<OpItem> op = std::make_unique<OvalOpItem>(oval, paint);
169     AddOp(std::move(op));
170 }
171 
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)172 void RSRecordingCanvas::onDrawArc(
173     const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint& paint)
174 {
175     std::unique_ptr<OpItem> op = std::make_unique<ArcOpItem>(oval, startAngle, sweepAngle, useCenter, paint);
176     AddOp(std::move(op));
177 }
178 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)179 void RSRecordingCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint)
180 {
181     std::unique_ptr<OpItem> op = std::make_unique<RoundRectOpItem>(rrect, paint);
182     AddOp(std::move(op));
183 }
184 
onDrawDRRect(const SkRRect & out,const SkRRect & in,const SkPaint & paint)185 void RSRecordingCanvas::onDrawDRRect(const SkRRect& out, const SkRRect& in, const SkPaint& paint)
186 {
187     std::unique_ptr<OpItem> op = std::make_unique<DRRectOpItem>(out, in, paint);
188     AddOp(std::move(op));
189 }
190 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)191 void RSRecordingCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix)
192 {
193     std::unique_ptr<OpItem> op = std::make_unique<DrawableOpItem>(drawable, matrix);
194     AddOp(std::move(op));
195 }
196 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)197 void RSRecordingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
198 {
199     std::unique_ptr<OpItem> op = std::make_unique<PictureOpItem>(sk_ref_sp(picture), matrix, paint);
200     AddOp(std::move(op));
201 }
202 
onDrawAnnotation(const SkRect & rect,const char key[],SkData * val)203 void RSRecordingCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* val)
204 {
205     // [PLANNING]: To be implemented
206     ROSEN_LOGE("RSRecordingCanvas::onDrawAnnotation not support yet");
207 }
208 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)209 void RSRecordingCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
210 {
211     std::unique_ptr<OpItem> op = std::make_unique<TextBlobOpItem>(sk_ref_sp(blob), x, y, paint);
212     AddOp(std::move(op));
213 }
214 
onDrawBitmap(const SkBitmap & bm,SkScalar x,SkScalar y,const SkPaint * paint)215 void RSRecordingCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint* paint)
216 {
217     std::unique_ptr<OpItem> op = std::make_unique<BitmapOpItem>(SkImage::MakeFromBitmap(bm), x, y, paint);
218     AddOp(std::move(op));
219 }
220 
onDrawBitmapNine(const SkBitmap & bm,const SkIRect & center,const SkRect & dst,const SkPaint * paint)221 void RSRecordingCanvas::onDrawBitmapNine(
222     const SkBitmap& bm, const SkIRect& center, const SkRect& dst, const SkPaint* paint)
223 {
224     std::unique_ptr<OpItem> op = std::make_unique<BitmapNineOpItem>(SkImage::MakeFromBitmap(bm), center, dst, paint);
225     AddOp(std::move(op));
226 }
227 
onDrawBitmapRect(const SkBitmap & bm,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)228 void RSRecordingCanvas::onDrawBitmapRect(
229     const SkBitmap& bm, const SkRect* src, const SkRect& dst, const SkPaint* paint, SrcRectConstraint constraint)
230 {
231     std::unique_ptr<OpItem> op = std::make_unique<BitmapRectOpItem>(SkImage::MakeFromBitmap(bm), src, dst, paint);
232     AddOp(std::move(op));
233 }
234 
onDrawBitmapLattice(const SkBitmap & bm,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint * paint)235 void RSRecordingCanvas::onDrawBitmapLattice(
236     const SkBitmap& bm, const SkCanvas::Lattice& lattice, const SkRect& dst, const SkPaint* paint)
237 {
238     std::unique_ptr<OpItem> op =
239         std::make_unique<BitmapLatticeOpItem>(SkImage::MakeFromBitmap(bm), lattice, dst, paint);
240     AddOp(std::move(op));
241 }
242 
onDrawImage(const SkImage * img,SkScalar x,SkScalar y,const SkPaint * paint)243 void RSRecordingCanvas::onDrawImage(const SkImage* img, SkScalar x, SkScalar y, const SkPaint* paint)
244 {
245     std::unique_ptr<OpItem> op = std::make_unique<BitmapOpItem>(sk_ref_sp(img), x, y, paint);
246     AddOp(std::move(op));
247 }
248 
onDrawImageNine(const SkImage * img,const SkIRect & center,const SkRect & dst,const SkPaint * paint)249 void RSRecordingCanvas::onDrawImageNine(
250     const SkImage* img, const SkIRect& center, const SkRect& dst, const SkPaint* paint)
251 {
252     std::unique_ptr<OpItem> op = std::make_unique<BitmapNineOpItem>(sk_ref_sp(img), center, dst, paint);
253     AddOp(std::move(op));
254 }
255 
onDrawImageRect(const SkImage * img,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)256 void RSRecordingCanvas::onDrawImageRect(
257     const SkImage* img, const SkRect* src, const SkRect& dst, const SkPaint* paint, SrcRectConstraint constraint)
258 {
259     std::unique_ptr<OpItem> op = std::make_unique<BitmapRectOpItem>(sk_ref_sp(img), src, dst, paint);
260     AddOp(std::move(op));
261 }
262 
onDrawImageLattice(const SkImage * img,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint * paint)263 void RSRecordingCanvas::onDrawImageLattice(
264     const SkImage* img, const SkCanvas::Lattice& lattice, const SkRect& dst, const SkPaint* paint)
265 {
266     std::unique_ptr<OpItem> op = std::make_unique<BitmapLatticeOpItem>(sk_ref_sp(img), lattice, dst, paint);
267     AddOp(std::move(op));
268 }
269 
DrawAdaptiveRRect(float radius,const SkPaint & paint)270 void RSRecordingCanvas::DrawAdaptiveRRect(float radius, const SkPaint& paint)
271 {
272     std::unique_ptr<OpItem> op = std::make_unique<AdaptiveRRectOpItem>(radius, paint);
273     AddOp(std::move(op));
274 }
275 
ClipAdaptiveRRect(float radius)276 void RSRecordingCanvas::ClipAdaptiveRRect(float radius)
277 {
278     std::unique_ptr<OpItem> op = std::make_unique<ClipAdaptiveRRectOpItem>(radius);
279     AddOp(std::move(op));
280 }
281 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)282 void RSRecordingCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
283     SkBlendMode bmode, const SkPaint& paint)
284 {
285     // [PLANNING]: To be implemented
286     ROSEN_LOGE("RSRecordingCanvas::onDrawPatch not support yet");
287 }
288 
onDrawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)289 void RSRecordingCanvas::onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint)
290 {
291     std::unique_ptr<OpItem> op = std::make_unique<PointsOpItem>(mode, count, pts, paint);
292     AddOp(std::move(op));
293 }
294 
onDrawVerticesObject(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode mode,const SkPaint & paint)295 void RSRecordingCanvas::onDrawVerticesObject(
296     const SkVertices* vertices, const SkVertices::Bone bones[], int boneCount, SkBlendMode mode, const SkPaint& paint)
297 {
298     std::unique_ptr<OpItem> op = std::make_unique<VerticesOpItem>(vertices, bones, boneCount, mode, paint);
299     AddOp(std::move(op));
300 }
301 
onDrawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode bmode,const SkRect * cull,const SkPaint * paint)302 void RSRecordingCanvas::onDrawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
303     const SkColor colors[], int count, SkBlendMode bmode, const SkRect* cull, const SkPaint* paint)
304 {
305     // [PLANNING]: To be implemented
306     ROSEN_LOGE("RSRecordingCanvas::onDrawAtlas not support yet");
307 }
308 
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)309 void RSRecordingCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec)
310 {
311     // [PLANNING]: To be implemented
312     ROSEN_LOGE("RSRecordingCanvas::onDrawShadowRec not support yet");
313 }
314 
MultiplyAlpha(float alpha)315 void RSRecordingCanvas::MultiplyAlpha(float alpha)
316 {
317     std::unique_ptr<OpItem> op = std::make_unique<MultiplyAlphaOpItem>(alpha);
318     AddOp(std::move(op));
319 }
320 
SaveAlpha()321 void RSRecordingCanvas::SaveAlpha()
322 {
323     std::unique_ptr<OpItem> op = std::make_unique<SaveAlphaOpItem>();
324     AddOp(std::move(op));
325 }
326 
RestoreAlpha()327 void RSRecordingCanvas::RestoreAlpha()
328 {
329     std::unique_ptr<OpItem> op = std::make_unique<RestoreAlphaOpItem>();
330     AddOp(std::move(op));
331 }
332 } // namespace Rosen
333 } // namespace OHOS
334