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_draw_cmd.h"
17
18 #include "platform/common/rs_log.h"
19 #include "pipeline/rs_paint_filter_canvas.h"
20 #include "pipeline/rs_root_render_node.h"
21 #include "securec.h"
22 namespace OHOS {
23 namespace Rosen {
RectOpItem(SkRect rect,const SkPaint & paint)24 RectOpItem::RectOpItem(SkRect rect, const SkPaint& paint) : OpItemWithPaint(sizeof(RectOpItem)), rect_(rect)
25 {
26 paint_ = paint;
27 }
28
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const29 void RectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
30 {
31 canvas.drawRect(rect_, paint_);
32 }
33
RoundRectOpItem(const SkRRect & rrect,const SkPaint & paint)34 RoundRectOpItem::RoundRectOpItem(const SkRRect& rrect, const SkPaint& paint)
35 : OpItemWithPaint(sizeof(RoundRectOpItem)), rrect_(rrect)
36 {
37 paint_ = paint;
38 }
39
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const40 void RoundRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
41 {
42 canvas.drawRRect(rrect_, paint_);
43 }
44
DRRectOpItem(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)45 DRRectOpItem::DRRectOpItem(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
46 : OpItemWithPaint(sizeof(DRRectOpItem))
47 {
48 outer_ = outer;
49 inner_ = inner;
50 paint_ = paint;
51 }
52
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const53 void DRRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
54 {
55 canvas.drawDRRect(outer_, inner_, paint_);
56 }
57
OvalOpItem(SkRect rect,const SkPaint & paint)58 OvalOpItem::OvalOpItem(SkRect rect, const SkPaint& paint) : OpItemWithPaint(sizeof(OvalOpItem)), rect_(rect)
59 {
60 paint_ = paint;
61 }
62
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const63 void OvalOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
64 {
65 canvas.drawOval(rect_, paint_);
66 }
67
RegionOpItem(SkRegion region,const SkPaint & paint)68 RegionOpItem::RegionOpItem(SkRegion region, const SkPaint& paint) : OpItemWithPaint(sizeof(RegionOpItem))
69 {
70 region_ = region;
71 paint_ = paint;
72 }
73
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const74 void RegionOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
75 {
76 canvas.drawRegion(region_, paint_);
77 }
78
ArcOpItem(const SkRect & rect,float startAngle,float sweepAngle,bool useCenter,const SkPaint & paint)79 ArcOpItem::ArcOpItem(const SkRect& rect, float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint)
80 : OpItemWithPaint(sizeof(ArcOpItem)), rect_(rect), startAngle_(startAngle), sweepAngle_(sweepAngle),
81 useCenter_(useCenter)
82 {
83 paint_ = paint;
84 }
85
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const86 void ArcOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
87 {
88 canvas.drawArc(rect_, startAngle_, sweepAngle_, useCenter_, paint_);
89 }
90
SaveOpItem()91 SaveOpItem::SaveOpItem() : OpItem(sizeof(SaveOpItem)) {}
92
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const93 void SaveOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
94 {
95 canvas.save();
96 }
97
RestoreOpItem()98 RestoreOpItem::RestoreOpItem() : OpItem(sizeof(RestoreOpItem)) {}
99
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const100 void RestoreOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
101 {
102 canvas.restore();
103 }
104
FlushOpItem()105 FlushOpItem::FlushOpItem() : OpItem(sizeof(FlushOpItem)) {}
106
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const107 void FlushOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
108 {
109 canvas.flush();
110 }
111
MatrixOpItem(const SkMatrix & matrix)112 MatrixOpItem::MatrixOpItem(const SkMatrix& matrix) : OpItem(sizeof(MatrixOpItem)), matrix_(matrix) {}
113
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const114 void MatrixOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
115 {
116 canvas.setMatrix(matrix_);
117 }
118
ClipRectOpItem(const SkRect & rect,SkClipOp op,bool doAA)119 ClipRectOpItem::ClipRectOpItem(const SkRect& rect, SkClipOp op, bool doAA)
120 : OpItem(sizeof(ClipRectOpItem)), rect_(rect), clipOp_(op), doAA_(doAA)
121 {}
122
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const123 void ClipRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
124 {
125 canvas.clipRect(rect_, clipOp_, doAA_);
126 }
127
ClipRRectOpItem(const SkRRect & rrect,SkClipOp op,bool doAA)128 ClipRRectOpItem::ClipRRectOpItem(const SkRRect& rrect, SkClipOp op, bool doAA)
129 : OpItem(sizeof(ClipRRectOpItem)), rrect_(rrect), clipOp_(op), doAA_(doAA)
130 {}
131
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const132 void ClipRRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
133 {
134 canvas.clipRRect(rrect_, clipOp_, doAA_);
135 }
136
ClipRegionOpItem(const SkRegion & region,SkClipOp op)137 ClipRegionOpItem::ClipRegionOpItem(const SkRegion& region, SkClipOp op)
138 : OpItem(sizeof(ClipRegionOpItem)), region_(region), clipOp_(op)
139 {}
140
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const141 void ClipRegionOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
142 {
143 canvas.clipRegion(region_, clipOp_);
144 }
145
TranslateOpItem(float distanceX,float distanceY)146 TranslateOpItem::TranslateOpItem(float distanceX, float distanceY)
147 : OpItem(sizeof(TranslateOpItem)), distanceX_(distanceX), distanceY_(distanceY)
148 {}
149
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const150 void TranslateOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
151 {
152 canvas.translate(distanceX_, distanceY_);
153 }
154
TextBlobOpItem(const sk_sp<SkTextBlob> textBlob,float x,float y,const SkPaint & paint)155 TextBlobOpItem::TextBlobOpItem(const sk_sp<SkTextBlob> textBlob, float x, float y, const SkPaint& paint)
156 : OpItemWithPaint(sizeof(TextBlobOpItem)), textBlob_(textBlob), x_(x), y_(y)
157 {
158 paint_ = paint;
159 }
160
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const161 void TextBlobOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
162 {
163 canvas.drawTextBlob(textBlob_, x_, y_, paint_);
164 }
165
BitmapOpItem(const sk_sp<SkImage> bitmapInfo,float left,float top,const SkPaint * paint)166 BitmapOpItem::BitmapOpItem(const sk_sp<SkImage> bitmapInfo, float left, float top, const SkPaint* paint)
167 : OpItemWithPaint(sizeof(BitmapOpItem)), left_(left), top_(top)
168 {
169 if (bitmapInfo != nullptr) {
170 bitmapInfo_ = bitmapInfo;
171 }
172 if (paint) {
173 paint_ = *paint;
174 }
175 }
176
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const177 void BitmapOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
178 {
179 canvas.drawImage(bitmapInfo_, left_, top_, &paint_);
180 }
181
BitmapRectOpItem(const sk_sp<SkImage> bitmapInfo,const SkRect * rectSrc,const SkRect & rectDst,const SkPaint * paint)182 BitmapRectOpItem::BitmapRectOpItem(
183 const sk_sp<SkImage> bitmapInfo, const SkRect* rectSrc, const SkRect& rectDst, const SkPaint* paint)
184 : OpItemWithPaint(sizeof(BitmapRectOpItem)), rectDst_(rectDst)
185 {
186 if (bitmapInfo != nullptr) {
187 rectSrc_ = (rectSrc == nullptr) ? SkRect::MakeWH(bitmapInfo->width(), bitmapInfo->height()) : *rectSrc;
188 bitmapInfo_ = bitmapInfo;
189 } else {
190 if (rectSrc != nullptr) {
191 rectSrc_ = *rectSrc;
192 }
193 }
194 if (paint) {
195 paint_ = *paint;
196 }
197 }
198
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const199 void BitmapRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
200 {
201 canvas.drawImageRect(bitmapInfo_, rectSrc_, rectDst_, &paint_);
202 }
203
BitmapLatticeOpItem(const sk_sp<SkImage> bitmapInfo,const SkCanvas::Lattice & lattice,const SkRect & rect,const SkPaint * paint)204 BitmapLatticeOpItem::BitmapLatticeOpItem(
205 const sk_sp<SkImage> bitmapInfo, const SkCanvas::Lattice& lattice, const SkRect& rect, const SkPaint* paint)
206 : OpItemWithPaint(sizeof(BitmapLatticeOpItem))
207 {
208 rect_ = rect;
209 lattice_ = lattice;
210 if (bitmapInfo != nullptr) {
211 bitmapInfo_ = bitmapInfo;
212 }
213 if (paint) {
214 paint_ = *paint;
215 }
216 }
217
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const218 void BitmapLatticeOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
219 {
220 canvas.drawImageLattice(bitmapInfo_.get(), lattice_, rect_, &paint_);
221 }
222
BitmapNineOpItem(const sk_sp<SkImage> bitmapInfo,const SkIRect & center,const SkRect & rectDst,const SkPaint * paint)223 BitmapNineOpItem::BitmapNineOpItem(
224 const sk_sp<SkImage> bitmapInfo, const SkIRect& center, const SkRect& rectDst, const SkPaint* paint)
225 : OpItemWithPaint(sizeof(BitmapNineOpItem)), center_(center), rectDst_(rectDst)
226 {
227 if (bitmapInfo != nullptr) {
228 bitmapInfo_ = bitmapInfo;
229 }
230 if (paint) {
231 paint_ = *paint;
232 }
233 }
234
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const235 void BitmapNineOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
236 {
237 canvas.drawImageNine(bitmapInfo_, center_, rectDst_, &paint_);
238 }
239
AdaptiveRRectOpItem(float radius,const SkPaint & paint)240 AdaptiveRRectOpItem::AdaptiveRRectOpItem(float radius, const SkPaint& paint)
241 : OpItemWithPaint(sizeof(AdaptiveRRectOpItem)), radius_(radius), paint_(paint)
242 {}
243
Draw(RSPaintFilterCanvas & canvas,const SkRect * rect) const244 void AdaptiveRRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect* rect) const
245 {
246 if (!rect) {
247 ROSEN_LOGE("AdaptiveRRectOpItem::Draw, skrect is null");
248 return;
249 }
250 SkRRect rrect = SkRRect::MakeRectXY(*rect, radius_, radius_);
251 canvas.drawRRect(rrect, paint_);
252 }
253
ClipAdaptiveRRectOpItem(float radius)254 ClipAdaptiveRRectOpItem::ClipAdaptiveRRectOpItem(float radius)
255 : OpItemWithPaint(sizeof(ClipAdaptiveRRectOpItem)), radius_(radius)
256 {}
257
Draw(RSPaintFilterCanvas & canvas,const SkRect * rect) const258 void ClipAdaptiveRRectOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect* rect) const
259 {
260 if (!rect) {
261 ROSEN_LOGE("ClipAdaptiveRRectOpItem::Draw skrect is null");
262 return;
263 }
264 SkRRect rrect = SkRRect::MakeRectXY(*rect, radius_, radius_);
265 canvas.clipRRect(rrect, true);
266 }
267
PathOpItem(const SkPath & path,const SkPaint & paint)268 PathOpItem::PathOpItem(const SkPath& path, const SkPaint& paint) : OpItemWithPaint(sizeof(PathOpItem))
269 {
270 path_ = path;
271 paint_ = paint;
272 }
273
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const274 void PathOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
275 {
276 canvas.drawPath(path_, paint_);
277 }
278
ClipPathOpItem(const SkPath & path,SkClipOp clipOp,bool doAA)279 ClipPathOpItem::ClipPathOpItem(const SkPath& path, SkClipOp clipOp, bool doAA)
280 : OpItem(sizeof(ClipPathOpItem)), path_(path), clipOp_(clipOp), doAA_(doAA)
281 {}
282
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const283 void ClipPathOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
284 {
285 canvas.clipPath(path_, clipOp_, doAA_);
286 }
287
PaintOpItem(const SkPaint & paint)288 PaintOpItem::PaintOpItem(const SkPaint& paint) : OpItemWithPaint(sizeof(PaintOpItem))
289 {
290 paint_ = paint;
291 }
292
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const293 void PaintOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
294 {
295 canvas.drawPaint(paint_);
296 }
297
ImageWithParmOpItem(const sk_sp<SkImage> img,int fitNum,int repeatNum,float radius,const SkPaint & paint)298 ImageWithParmOpItem::ImageWithParmOpItem(
299 const sk_sp<SkImage> img, int fitNum, int repeatNum, float radius, const SkPaint& paint)
300 : OpItemWithPaint(sizeof(ImageWithParmOpItem))
301 {
302 rsImage_ = std::make_shared<RSImage>();
303 rsImage_->SetImage(img);
304 rsImage_->SetImageFit(fitNum);
305 rsImage_->SetImageRepeat(repeatNum);
306 rsImage_->SetRadius(radius);
307 paint_ = paint;
308 }
309
Draw(RSPaintFilterCanvas & canvas,const SkRect * rect) const310 void ImageWithParmOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect* rect) const
311 {
312 if (!rect) {
313 ROSEN_LOGE("agp_ace: no rect");
314 return;
315 }
316 rsImage_->CanvasDrawImage(canvas, *rect, paint_);
317 }
318
ConcatOpItem(const SkMatrix & matrix)319 ConcatOpItem::ConcatOpItem(const SkMatrix& matrix) : OpItem(sizeof(ConcatOpItem)), matrix_(matrix) {}
320
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const321 void ConcatOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
322 {
323 canvas.concat(matrix_);
324 }
325
SaveLayerOpItem(const SkCanvas::SaveLayerRec & rec)326 SaveLayerOpItem::SaveLayerOpItem(const SkCanvas::SaveLayerRec& rec) : OpItemWithPaint(sizeof(SaveLayerOpItem))
327 {
328 if (rec.fBounds) {
329 rect_ = *rec.fBounds;
330 rectPtr_ = &rect_;
331 }
332 if (rec.fPaint) {
333 paint_ = *rec.fPaint;
334 }
335 backdrop_ = sk_ref_sp(rec.fBackdrop);
336 mask_ = sk_ref_sp(rec.fClipMask);
337 matrix_ = rec.fClipMatrix ? *(rec.fClipMatrix) : SkMatrix::I();
338 flags_ = rec.fSaveLayerFlags;
339 }
340
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const341 void SaveLayerOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
342 {
343 canvas.saveLayer(
344 { rectPtr_, &paint_, backdrop_.get(), mask_.get(), matrix_.isIdentity() ? nullptr : &matrix_, flags_ });
345 if (paint_.getImageFilter() || paint_.getColorFilter()) {
346 RSRootRenderNode::MarkForceRaster();
347 }
348 }
349
DrawableOpItem(SkDrawable * drawable,const SkMatrix * matrix)350 DrawableOpItem::DrawableOpItem(SkDrawable* drawable, const SkMatrix* matrix) : OpItem(sizeof(DrawableOpItem))
351 {
352 drawable_ = sk_ref_sp(drawable);
353 if (matrix) {
354 matrix_ = *matrix;
355 }
356 }
357
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const358 void DrawableOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
359 {
360 canvas.drawDrawable(drawable_.get(), &matrix_);
361 }
362
PictureOpItem(const sk_sp<SkPicture> picture,const SkMatrix * matrix,const SkPaint * paint)363 PictureOpItem::PictureOpItem(const sk_sp<SkPicture> picture, const SkMatrix* matrix, const SkPaint* paint)
364 : OpItemWithPaint(sizeof(PictureOpItem)), picture_(picture)
365 {
366 if (matrix) {
367 matrix_ = *matrix;
368 }
369 if (paint) {
370 paint_ = *paint;
371 }
372 }
373
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const374 void PictureOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
375 {
376 canvas.drawPicture(picture_, &matrix_, &paint_);
377 }
378
PointsOpItem(SkCanvas::PointMode mode,int count,const SkPoint processedPoints[],const SkPaint & paint)379 PointsOpItem::PointsOpItem(SkCanvas::PointMode mode, int count, const SkPoint processedPoints[], const SkPaint& paint)
380 : OpItemWithPaint(sizeof(PointsOpItem)), mode_(mode), count_(count), processedPoints_(new SkPoint[count])
381 {
382 errno_t ret = memcpy_s(processedPoints_, count * sizeof(SkPoint), processedPoints, count * sizeof(SkPoint));
383 if (ret != EOK) {
384 ROSEN_LOGE("PointsOpItem: memcpy failed!");
385 }
386 paint_ = paint;
387 }
388
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const389 void PointsOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
390 {
391 canvas.drawPoints(mode_, count_, processedPoints_, paint_);
392 }
393
VerticesOpItem(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode mode,const SkPaint & paint)394 VerticesOpItem::VerticesOpItem(const SkVertices* vertices, const SkVertices::Bone bones[],
395 int boneCount, SkBlendMode mode, const SkPaint& paint)
396 : OpItemWithPaint(sizeof(VerticesOpItem)), vertices_(sk_ref_sp(const_cast<SkVertices*>(vertices))),
397 bones_(new SkVertices::Bone[boneCount]), boneCount_(boneCount), mode_(mode)
398 {
399 errno_t ret = memcpy_s(bones_, boneCount * sizeof(SkVertices::Bone), bones, boneCount * sizeof(SkVertices::Bone));
400 if (ret != EOK) {
401 ROSEN_LOGE("VerticesOpItem: memcpy failed!");
402 }
403 paint_ = paint;
404 }
405
~VerticesOpItem()406 VerticesOpItem::~VerticesOpItem()
407 {
408 delete[] bones_;
409 }
410
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const411 void VerticesOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
412 {
413 canvas.drawVertices(vertices_, bones_, boneCount_, mode_, paint_);
414 }
415
MultiplyAlphaOpItem(float alpha)416 MultiplyAlphaOpItem::MultiplyAlphaOpItem(float alpha) : OpItem(sizeof(MultiplyAlphaOpItem)), alpha_(alpha) {}
417
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const418 void MultiplyAlphaOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
419 {
420 canvas.MultiplyAlpha(alpha_);
421 }
422
SaveAlphaOpItem()423 SaveAlphaOpItem::SaveAlphaOpItem() : OpItem(sizeof(SaveAlphaOpItem)) {}
424
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const425 void SaveAlphaOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
426 {
427 canvas.SaveAlpha();
428 }
429
RestoreAlphaOpItem()430 RestoreAlphaOpItem::RestoreAlphaOpItem() : OpItem(sizeof(RestoreAlphaOpItem)) {}
431
Draw(RSPaintFilterCanvas & canvas,const SkRect *) const432 void RestoreAlphaOpItem::Draw(RSPaintFilterCanvas& canvas, const SkRect*) const
433 {
434 canvas.RestoreAlpha();
435 }
436 } // namespace Rosen
437 } // namespace OHOS
438