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