xref: /aosp_15_r20/external/skia/src/core/SkRecorder.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/core/SkRecorder.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkDrawable.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPicture.h"
19 #include "include/core/SkPoint.h"
20 #include "include/core/SkRSXform.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTextBlob.h"
26 #include "include/core/SkVertices.h"
27 #include "include/private/base/SkFloatingPoint.h"
28 #include "include/private/base/SkTemplates.h"
29 #include "include/private/base/SkTo.h"
30 #include "include/private/chromium/Slug.h"
31 #include "src/core/SkBigPicture.h"
32 #include "src/core/SkCanvasPriv.h"
33 #include "src/core/SkRecord.h"
34 #include "src/core/SkRecords.h"
35 #include "src/text/GlyphRun.h"
36 #include "src/utils/SkPatchUtils.h"
37 
38 #include <cstdint>
39 #include <cstring>
40 #include <memory>
41 #include <new>
42 
43 class SkBlender;
44 class SkMesh;
45 class SkPath;
46 class SkRRect;
47 class SkRegion;
48 class SkSurfaceProps;
49 enum class SkBlendMode;
50 enum class SkClipOp;
51 struct SkDrawShadowRec;
52 
53 using namespace skia_private;
54 
~SkDrawableList()55 SkDrawableList::~SkDrawableList() {
56     for(SkDrawable* p : fArray) {
57         p->unref();
58     }
59     fArray.reset();
60 }
61 
newDrawableSnapshot()62 SkBigPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
63     const int count = fArray.size();
64     if (0 == count) {
65         return nullptr;
66     }
67     AutoTMalloc<const SkPicture*> pics(count);
68     for (int i = 0; i < count; ++i) {
69         pics[i] = fArray[i]->makePictureSnapshot().release();
70     }
71     return new SkBigPicture::SnapshotArray(pics.release(), count);
72 }
73 
append(SkDrawable * drawable)74 void SkDrawableList::append(SkDrawable* drawable) {
75     *fArray.append() = SkRef(drawable);
76 }
77 
78 ///////////////////////////////////////////////////////////////////////////////////////////////
79 
safe_picture_bounds(const SkRect & bounds)80 static SkIRect safe_picture_bounds(const SkRect& bounds) {
81     SkIRect picBounds = bounds.roundOut();
82     // roundOut() saturates the float edges to +/-SK_MaxS32FitsInFloat (~2billion), but this is
83     // large enough that width/height calculations will overflow, leading to negative dimensions.
84     static constexpr int32_t kSafeEdge = SK_MaxS32FitsInFloat / 2 - 1;
85     static constexpr SkIRect kSafeBounds = {-kSafeEdge, -kSafeEdge, kSafeEdge, kSafeEdge};
86     static_assert((kSafeBounds.fRight - kSafeBounds.fLeft) >= 0 &&
87                   (kSafeBounds.fBottom - kSafeBounds.fTop) >= 0);
88     if (!picBounds.intersect(kSafeBounds)) {
89         picBounds.setEmpty();
90     }
91     return picBounds;
92 }
93 
SkRecorder(SkRecord * record,int width,int height)94 SkRecorder::SkRecorder(SkRecord* record, int width, int height)
95         : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height)
96         , fApproxBytesUsedBySubPictures(0)
97         , fRecord(record) {
98     SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
99 }
100 
SkRecorder(SkRecord * record,const SkRect & bounds)101 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
102         : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(safe_picture_bounds(bounds))
103         , fApproxBytesUsedBySubPictures(0)
104         , fRecord(record) {
105     SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
106 }
107 
reset(SkRecord * record,const SkRect & bounds)108 void SkRecorder::reset(SkRecord* record, const SkRect& bounds) {
109     this->forgetRecord();
110     fRecord = record;
111     this->resetCanvas(safe_picture_bounds(bounds));
112     SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
113 }
114 
forgetRecord()115 void SkRecorder::forgetRecord() {
116     fDrawableList.reset(nullptr);
117     fApproxBytesUsedBySubPictures = 0;
118     fRecord = nullptr;
119 }
120 
121 // To make appending to fRecord a little less verbose.
122 template<typename T, typename... Args>
append(Args &&...args)123 void SkRecorder::append(Args&&... args) {
124     new (fRecord->append<T>()) T{std::forward<Args>(args)...};
125 }
126 
127 // For methods which must call back into SkNoDrawCanvas.
128 #define INHERITED(method, ...) this->SkNoDrawCanvas::method(__VA_ARGS__)
129 
130 // Use copy() only for optional arguments, to be copied if present or skipped if not.
131 // (For most types we just pass by value and let copy constructors do their thing.)
132 template <typename T>
copy(const T * src)133 T* SkRecorder::copy(const T* src) {
134     if (nullptr == src) {
135         return nullptr;
136     }
137     return new (fRecord->alloc<T>()) T(*src);
138 }
139 
140 // This copy() is for arrays.
141 // It will work with POD or non-POD, though currently we only use it for POD.
142 template <typename T>
copy(const T src[],size_t count)143 T* SkRecorder::copy(const T src[], size_t count) {
144     if (nullptr == src) {
145         return nullptr;
146     }
147     T* dst = fRecord->alloc<T>(count);
148     for (size_t i = 0; i < count; i++) {
149         new (dst + i) T(src[i]);
150     }
151     return dst;
152 }
153 
154 // Specialization for copying strings, using memcpy.
155 // This measured around 2x faster for copying code points,
156 // but I found no corresponding speedup for other arrays.
157 template <>
copy(const char src[],size_t count)158 char* SkRecorder::copy(const char src[], size_t count) {
159     if (nullptr == src) {
160         return nullptr;
161     }
162     char* dst = fRecord->alloc<char>(count);
163     memcpy(dst, src, count);
164     return dst;
165 }
166 
167 // As above, assuming and copying a terminating \0.
168 template <>
copy(const char * src)169 char* SkRecorder::copy(const char* src) {
170     return this->copy(src, strlen(src)+1);
171 }
172 
onDrawPaint(const SkPaint & paint)173 void SkRecorder::onDrawPaint(const SkPaint& paint) {
174     this->append<SkRecords::DrawPaint>(paint);
175 }
176 
onDrawBehind(const SkPaint & paint)177 void SkRecorder::onDrawBehind(const SkPaint& paint) {
178     this->append<SkRecords::DrawBehind>(paint);
179 }
180 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)181 void SkRecorder::onDrawPoints(PointMode mode,
182                               size_t count,
183                               const SkPoint pts[],
184                               const SkPaint& paint) {
185     this->append<SkRecords::DrawPoints>(paint, mode, SkToUInt(count), this->copy(pts, count));
186 }
187 
onDrawRect(const SkRect & rect,const SkPaint & paint)188 void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
189     this->append<SkRecords::DrawRect>(paint, rect);
190 }
191 
onDrawRegion(const SkRegion & region,const SkPaint & paint)192 void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
193     this->append<SkRecords::DrawRegion>(paint, region);
194 }
195 
onDrawOval(const SkRect & oval,const SkPaint & paint)196 void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
197     this->append<SkRecords::DrawOval>(paint, oval);
198 }
199 
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)200 void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
201                            bool useCenter, const SkPaint& paint) {
202     this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter);
203 }
204 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)205 void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
206     this->append<SkRecords::DrawRRect>(paint, rrect);
207 }
208 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)209 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
210     this->append<SkRecords::DrawDRRect>(paint, outer, inner);
211 }
212 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)213 void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
214     if (!fDrawableList) {
215         fDrawableList = std::make_unique<SkDrawableList>();
216     }
217     fDrawableList->append(drawable);
218     this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
219 }
220 
onDrawPath(const SkPath & path,const SkPaint & paint)221 void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
222     this->append<SkRecords::DrawPath>(paint, path);
223 }
224 
onDrawImage2(const SkImage * image,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)225 void SkRecorder::onDrawImage2(const SkImage* image, SkScalar x, SkScalar y,
226                               const SkSamplingOptions& sampling, const SkPaint* paint) {
227     this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), x, y, sampling);
228 }
229 
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)230 void SkRecorder::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
231                                   const SkSamplingOptions& sampling, const SkPaint* paint,
232                                   SrcRectConstraint constraint) {
233     this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), src, dst,
234                                            sampling, constraint);
235 }
236 
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)237 void SkRecorder::onDrawImageLattice2(const SkImage* image, const Lattice& lattice, const SkRect& dst,
238                                      SkFilterMode filter, const SkPaint* paint) {
239     int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
240     SkASSERT(lattice.fBounds);
241     this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image),
242            lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount),
243            lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount),
244            flagCount, this->copy(lattice.fRectTypes, flagCount),
245            this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst, filter);
246 }
247 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)248 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
249                                 const SkPaint& paint) {
250     this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y);
251 }
252 
onDrawSlug(const sktext::gpu::Slug * slug,const SkPaint & paint)253 void SkRecorder::onDrawSlug(const sktext::gpu::Slug* slug, const SkPaint& paint) {
254     this->append<SkRecords::DrawSlug>(paint, sk_ref_sp(slug));
255 }
256 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)257 void SkRecorder::onDrawGlyphRunList(
258         const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
259     sk_sp<SkTextBlob> blob = sk_ref_sp(glyphRunList.blob());
260     if (glyphRunList.blob() == nullptr) {
261         blob = glyphRunList.makeBlob();
262     }
263 
264     this->onDrawTextBlob(blob.get(), glyphRunList.origin().x(), glyphRunList.origin().y(), paint);
265 }
266 
onDrawPicture(const SkPicture * pic,const SkMatrix * matrix,const SkPaint * paint)267 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
268     fApproxBytesUsedBySubPictures += pic->approximateBytesUsed();
269     this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
270 }
271 
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)272 void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
273                                       const SkPaint& paint) {
274     this->append<SkRecords::DrawVertices>(paint,
275                                           sk_ref_sp(const_cast<SkVertices*>(vertices)),
276                                           bmode);
277 }
278 
onDrawMesh(const SkMesh & mesh,sk_sp<SkBlender> blender,const SkPaint & paint)279 void SkRecorder::onDrawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
280     this->append<SkRecords::DrawMesh>(paint, mesh, std::move(blender));
281 }
282 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)283 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
284                              const SkPoint texCoords[4], SkBlendMode bmode,
285                              const SkPaint& paint) {
286     this->append<SkRecords::DrawPatch>(paint,
287            cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
288            colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
289            texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
290            bmode);
291 }
292 
onDrawAtlas2(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)293 void SkRecorder::onDrawAtlas2(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
294                               const SkColor colors[], int count, SkBlendMode mode,
295                               const SkSamplingOptions& sampling, const SkRect* cull,
296                               const SkPaint* paint) {
297     this->append<SkRecords::DrawAtlas>(this->copy(paint),
298            sk_ref_sp(atlas),
299            this->copy(xform, count),
300            this->copy(tex, count),
301            this->copy(colors, count),
302            count,
303            mode,
304            sampling,
305            this->copy(cull));
306 }
307 
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)308 void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
309     this->append<SkRecords::DrawShadowRec>(path, rec);
310 }
311 
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)312 void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
313     this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value));
314 }
315 
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)316 void SkRecorder::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
317                                   QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
318     this->append<SkRecords::DrawEdgeAAQuad>(
319             rect, this->copy(clip, 4), aa, color, mode);
320 }
321 
onDrawEdgeAAImageSet2(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)322 void SkRecorder::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
323                                        const SkPoint dstClips[], const SkMatrix preViewMatrices[],
324                                        const SkSamplingOptions& sampling, const SkPaint* paint,
325                                        SrcRectConstraint constraint) {
326     int totalDstClipCount, totalMatrixCount;
327     SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount);
328 
329     AutoTArray<ImageSetEntry> setCopy(count);
330     for (int i = 0; i < count; ++i) {
331         setCopy[i] = set[i];
332     }
333 
334     this->append<SkRecords::DrawEdgeAAImageSet>(this->copy(paint), std::move(setCopy), count,
335             this->copy(dstClips, totalDstClipCount),
336             this->copy(preViewMatrices, totalMatrixCount), sampling, constraint);
337 }
338 
willSave()339 void SkRecorder::willSave() {
340     this->append<SkRecords::Save>();
341 }
342 
getSaveLayerStrategy(const SaveLayerRec & rec)343 SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) {
344     AutoTArray<sk_sp<SkImageFilter>> filters(rec.fFilters.size());
345     for (size_t i = 0; i < rec.fFilters.size(); ++i) {
346         filters[i] = rec.fFilters[i];
347     }
348 
349     this->append<SkRecords::SaveLayer>(this->copy(rec.fBounds),
350                                        this->copy(rec.fPaint),
351                                        sk_ref_sp(rec.fBackdrop),
352                                        rec.fSaveLayerFlags,
353                                        SkCanvasPriv::GetBackdropScaleFactor(rec),
354                                        rec.fBackdropTileMode,
355                                        std::move(filters));
356     return SkCanvas::kNoLayer_SaveLayerStrategy;
357 }
358 
onDoSaveBehind(const SkRect * subset)359 bool SkRecorder::onDoSaveBehind(const SkRect* subset) {
360     this->append<SkRecords::SaveBehind>(this->copy(subset));
361     return false;
362 }
363 
didRestore()364 void SkRecorder::didRestore() {
365     this->append<SkRecords::Restore>(this->getTotalMatrix());
366 }
367 
didConcat44(const SkM44 & m)368 void SkRecorder::didConcat44(const SkM44& m) {
369     this->append<SkRecords::Concat44>(m);
370 }
371 
didSetM44(const SkM44 & m)372 void SkRecorder::didSetM44(const SkM44& m) {
373     this->append<SkRecords::SetM44>(m);
374 }
375 
didScale(SkScalar sx,SkScalar sy)376 void SkRecorder::didScale(SkScalar sx, SkScalar sy) {
377     this->append<SkRecords::Scale>(sx, sy);
378 }
379 
didTranslate(SkScalar dx,SkScalar dy)380 void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) {
381     this->append<SkRecords::Translate>(dx, dy);
382 }
383 
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)384 void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
385     INHERITED(onClipRect, rect, op, edgeStyle);
386     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
387     this->append<SkRecords::ClipRect>(rect, opAA);
388 }
389 
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)390 void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
391     INHERITED(onClipRRect, rrect, op, edgeStyle);
392     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
393     this->append<SkRecords::ClipRRect>(rrect, opAA);
394 }
395 
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)396 void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
397     INHERITED(onClipPath, path, op, edgeStyle);
398     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
399     this->append<SkRecords::ClipPath>(path, opAA);
400 }
401 
onClipShader(sk_sp<SkShader> cs,SkClipOp op)402 void SkRecorder::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
403     INHERITED(onClipShader, cs, op);
404     this->append<SkRecords::ClipShader>(std::move(cs), op);
405 }
406 
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)407 void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
408     INHERITED(onClipRegion, deviceRgn, op);
409     this->append<SkRecords::ClipRegion>(deviceRgn, op);
410 }
411 
onResetClip()412 void SkRecorder::onResetClip() {
413     INHERITED(onResetClip);
414     this->append<SkRecords::ResetClip>();
415 }
416 
onNewSurface(const SkImageInfo &,const SkSurfaceProps &)417 sk_sp<SkSurface> SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
418     return nullptr;
419 }
420