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/SkImage.h"
11 #include "include/core/SkPicture.h"
12 #include "include/core/SkSurface.h"
13 #include "include/private/SkTo.h"
14 #include "src/core/SkBigPicture.h"
15 #include "src/core/SkCanvasPriv.h"
16 #include "src/utils/SkPatchUtils.h"
17
18 #include <new>
19
~SkDrawableList()20 SkDrawableList::~SkDrawableList() {
21 fArray.unrefAll();
22 }
23
newDrawableSnapshot()24 SkBigPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
25 const int count = fArray.count();
26 if (0 == count) {
27 return nullptr;
28 }
29 SkAutoTMalloc<const SkPicture*> pics(count);
30 for (int i = 0; i < count; ++i) {
31 pics[i] = fArray[i]->newPictureSnapshot();
32 }
33 return new SkBigPicture::SnapshotArray(pics.release(), count);
34 }
35
append(SkDrawable * drawable)36 void SkDrawableList::append(SkDrawable* drawable) {
37 *fArray.append() = SkRef(drawable);
38 }
39
40 ///////////////////////////////////////////////////////////////////////////////////////////////
41
SkRecorder(SkRecord * record,int width,int height,SkMiniRecorder * mr)42 SkRecorder::SkRecorder(SkRecord* record, int width, int height, SkMiniRecorder* mr)
43 : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height)
44 , fDrawPictureMode(Record_DrawPictureMode)
45 , fApproxBytesUsedBySubPictures(0)
46 , fRecord(record)
47 , fMiniRecorder(mr) {}
48
SkRecorder(SkRecord * record,const SkRect & bounds,SkMiniRecorder * mr)49 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds, SkMiniRecorder* mr)
50 : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(bounds.roundOut())
51 , fDrawPictureMode(Record_DrawPictureMode)
52 , fApproxBytesUsedBySubPictures(0)
53 , fRecord(record)
54 , fMiniRecorder(mr) {}
55
reset(SkRecord * record,const SkRect & bounds,DrawPictureMode dpm,SkMiniRecorder * mr)56 void SkRecorder::reset(SkRecord* record, const SkRect& bounds,
57 DrawPictureMode dpm, SkMiniRecorder* mr) {
58 this->forgetRecord();
59 fDrawPictureMode = dpm;
60 fRecord = record;
61 this->resetCanvas(bounds.roundOut());
62 fMiniRecorder = mr;
63 }
64
forgetRecord()65 void SkRecorder::forgetRecord() {
66 fDrawableList.reset(nullptr);
67 fApproxBytesUsedBySubPictures = 0;
68 fRecord = nullptr;
69 }
70
71 // To make appending to fRecord a little less verbose.
72 template<typename T, typename... Args>
append(Args &&...args)73 void SkRecorder::append(Args&&... args) {
74 if (fMiniRecorder) {
75 this->flushMiniRecorder();
76 }
77 new (fRecord->append<T>()) T{std::forward<Args>(args)...};
78 }
79
80 #define TRY_MINIRECORDER(method, ...) \
81 if (fMiniRecorder && fMiniRecorder->method(__VA_ARGS__)) return
82
83 // For methods which must call back into SkNoDrawCanvas.
84 #define INHERITED(method, ...) this->SkNoDrawCanvas::method(__VA_ARGS__)
85
86 // Use copy() only for optional arguments, to be copied if present or skipped if not.
87 // (For most types we just pass by value and let copy constructors do their thing.)
88 template <typename T>
copy(const T * src)89 T* SkRecorder::copy(const T* src) {
90 if (nullptr == src) {
91 return nullptr;
92 }
93 return new (fRecord->alloc<T>()) T(*src);
94 }
95
96 // This copy() is for arrays.
97 // It will work with POD or non-POD, though currently we only use it for POD.
98 template <typename T>
copy(const T src[],size_t count)99 T* SkRecorder::copy(const T src[], size_t count) {
100 if (nullptr == src) {
101 return nullptr;
102 }
103 T* dst = fRecord->alloc<T>(count);
104 for (size_t i = 0; i < count; i++) {
105 new (dst + i) T(src[i]);
106 }
107 return dst;
108 }
109
110 // Specialization for copying strings, using memcpy.
111 // This measured around 2x faster for copying code points,
112 // but I found no corresponding speedup for other arrays.
113 template <>
copy(const char src[],size_t count)114 char* SkRecorder::copy(const char src[], size_t count) {
115 if (nullptr == src) {
116 return nullptr;
117 }
118 char* dst = fRecord->alloc<char>(count);
119 memcpy(dst, src, count);
120 return dst;
121 }
122
123 // As above, assuming and copying a terminating \0.
124 template <>
copy(const char * src)125 char* SkRecorder::copy(const char* src) {
126 return this->copy(src, strlen(src)+1);
127 }
128
flushMiniRecorder()129 void SkRecorder::flushMiniRecorder() {
130 if (fMiniRecorder) {
131 SkMiniRecorder* mr = fMiniRecorder;
132 fMiniRecorder = nullptr; // Needs to happen before flushAndReset() or we recurse forever.
133 mr->flushAndReset(this);
134 }
135 }
136
onDrawPaint(const SkPaint & paint)137 void SkRecorder::onDrawPaint(const SkPaint& paint) {
138 this->append<SkRecords::DrawPaint>(paint);
139 }
140
onDrawBehind(const SkPaint & paint)141 void SkRecorder::onDrawBehind(const SkPaint& paint) {
142 this->append<SkRecords::DrawBehind>(paint);
143 }
144
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)145 void SkRecorder::onDrawPoints(PointMode mode,
146 size_t count,
147 const SkPoint pts[],
148 const SkPaint& paint) {
149 this->append<SkRecords::DrawPoints>(paint, mode, SkToUInt(count), this->copy(pts, count));
150 }
151
onDrawRect(const SkRect & rect,const SkPaint & paint)152 void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
153 TRY_MINIRECORDER(drawRect, rect, paint);
154 this->append<SkRecords::DrawRect>(paint, rect);
155 }
156
onDrawRegion(const SkRegion & region,const SkPaint & paint)157 void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
158 this->append<SkRecords::DrawRegion>(paint, region);
159 }
160
onDrawOval(const SkRect & oval,const SkPaint & paint)161 void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
162 this->append<SkRecords::DrawOval>(paint, oval);
163 }
164
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)165 void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
166 bool useCenter, const SkPaint& paint) {
167 this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter);
168 }
169
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)170 void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
171 this->append<SkRecords::DrawRRect>(paint, rrect);
172 }
173
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)174 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
175 this->append<SkRecords::DrawDRRect>(paint, outer, inner);
176 }
177
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)178 void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
179 if (fDrawPictureMode == Record_DrawPictureMode) {
180 if (!fDrawableList) {
181 fDrawableList.reset(new SkDrawableList);
182 }
183 fDrawableList->append(drawable);
184 this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
185 } else {
186 SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
187 drawable->draw(this, matrix);
188 }
189 }
190
onDrawPath(const SkPath & path,const SkPaint & paint)191 void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
192 TRY_MINIRECORDER(drawPath, path, paint);
193 this->append<SkRecords::DrawPath>(paint, path);
194 }
195
onDrawBitmap(const SkBitmap & bitmap,SkScalar left,SkScalar top,const SkPaint * paint)196 void SkRecorder::onDrawBitmap(const SkBitmap& bitmap,
197 SkScalar left,
198 SkScalar top,
199 const SkPaint* paint) {
200 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
201 if (image) {
202 this->onDrawImage(image.get(), left, top, paint);
203 }
204 }
205
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)206 void SkRecorder::onDrawBitmapRect(const SkBitmap& bitmap,
207 const SkRect* src,
208 const SkRect& dst,
209 const SkPaint* paint,
210 SrcRectConstraint constraint) {
211 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
212 if (image) {
213 this->onDrawImageRect(image.get(), src, dst, paint, constraint);
214 }
215 }
216
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)217 void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
218 const SkIRect& center,
219 const SkRect& dst,
220 const SkPaint* paint) {
221 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
222 if (image) {
223 this->onDrawImageNine(image.get(), center, dst, paint);
224 }
225 }
226
onDrawBitmapLattice(const SkBitmap & bitmap,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)227 void SkRecorder::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
228 const SkRect& dst, const SkPaint* paint) {
229 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
230 this->onDrawImageLattice(image.get(), lattice, dst, paint);
231 }
232
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)233 void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
234 const SkPaint* paint) {
235 this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), left, top);
236 }
237
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)238 void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
239 const SkPaint* paint, SrcRectConstraint constraint) {
240 this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), this->copy(src), dst, constraint);
241 }
242
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)243 void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center,
244 const SkRect& dst, const SkPaint* paint) {
245 this->append<SkRecords::DrawImageNine>(this->copy(paint), sk_ref_sp(image), center, dst);
246 }
247
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)248 void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
249 const SkPaint* paint) {
250 int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
251 SkASSERT(lattice.fBounds);
252 this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image),
253 lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount),
254 lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount),
255 flagCount, this->copy(lattice.fRectTypes, flagCount),
256 this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst);
257 }
258
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)259 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
260 const SkPaint& paint) {
261 TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint);
262 this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y);
263 }
264
onDrawPicture(const SkPicture * pic,const SkMatrix * matrix,const SkPaint * paint)265 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
266 if (fDrawPictureMode == Record_DrawPictureMode) {
267 fApproxBytesUsedBySubPictures += pic->approximateBytesUsed();
268 this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
269 } else if (fDrawPictureMode == PlaybackTop_DrawPictureMode) {
270 // temporarily change the mode of this recorder to Record,
271 fDrawPictureMode = Record_DrawPictureMode;
272 // play back the top level picture
273 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
274 pic->playback(this);
275 // restore the mode
276 fDrawPictureMode = PlaybackTop_DrawPictureMode;
277 } else {
278 SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
279 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
280 pic->playback(this);
281 }
282 }
283
onDrawVerticesObject(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const SkPaint & paint)284 void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[],
285 int boneCount, SkBlendMode bmode, const SkPaint& paint) {
286 this->append<SkRecords::DrawVertices>(paint,
287 sk_ref_sp(const_cast<SkVertices*>(vertices)),
288 this->copy(bones, boneCount),
289 boneCount,
290 bmode);
291 }
292
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)293 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
294 const SkPoint texCoords[4], SkBlendMode bmode,
295 const SkPaint& paint) {
296 this->append<SkRecords::DrawPatch>(paint,
297 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
298 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
299 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
300 bmode);
301 }
302
onDrawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)303 void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
304 const SkColor colors[], int count, SkBlendMode mode,
305 const SkRect* cull, const SkPaint* paint) {
306 this->append<SkRecords::DrawAtlas>(this->copy(paint),
307 sk_ref_sp(atlas),
308 this->copy(xform, count),
309 this->copy(tex, count),
310 this->copy(colors, count),
311 count,
312 mode,
313 this->copy(cull));
314 }
315
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)316 void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
317 this->append<SkRecords::DrawShadowRec>(path, rec);
318 }
319
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)320 void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
321 this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value));
322 }
323
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)324 void SkRecorder::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
325 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
326 this->append<SkRecords::DrawEdgeAAQuad>(
327 rect, this->copy(clip, 4), aa, color, mode);
328 }
329
onDrawEdgeAAImageSet(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkPaint * paint,SrcRectConstraint constraint)330 void SkRecorder::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count,
331 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
332 const SkPaint* paint, SrcRectConstraint constraint) {
333 int totalDstClipCount, totalMatrixCount;
334 SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount);
335
336 SkAutoTArray<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), 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 , sk_ref_sp(rec.fClipMask)
359 , this->copy(rec.fClipMatrix)
360 , rec.fSaveLayerFlags);
361 return SkCanvas::kNoLayer_SaveLayerStrategy;
362 }
363
onDoSaveBehind(const SkRect * subset)364 bool SkRecorder::onDoSaveBehind(const SkRect* subset) {
365 this->append<SkRecords::SaveBehind>(this->copy(subset));
366 return false;
367 }
368
didRestore()369 void SkRecorder::didRestore() {
370 this->append<SkRecords::Restore>(this->getTotalMatrix());
371 }
372
didConcat44(const SkScalar m[16])373 void SkRecorder::didConcat44(const SkScalar m[16]) {
374 this->append<SkRecords::Concat44>(m);
375 }
376
didConcat(const SkMatrix & matrix)377 void SkRecorder::didConcat(const SkMatrix& matrix) {
378 this->append<SkRecords::Concat>(matrix);
379 }
380
didSetMatrix(const SkMatrix & matrix)381 void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
382 this->append<SkRecords::SetMatrix>(matrix);
383 }
384
didScale(SkScalar sx,SkScalar sy)385 void SkRecorder::didScale(SkScalar sx, SkScalar sy) {
386 this->append<SkRecords::Scale>(sx, sy);
387 }
388
didTranslate(SkScalar dx,SkScalar dy)389 void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) {
390 this->append<SkRecords::Translate>(dx, dy);
391 }
392
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)393 void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
394 INHERITED(onClipRect, rect, op, edgeStyle);
395 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
396 this->append<SkRecords::ClipRect>(rect, opAA);
397 }
398
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)399 void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
400 INHERITED(onClipRRect, rrect, op, edgeStyle);
401 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
402 this->append<SkRecords::ClipRRect>(rrect, opAA);
403 }
404
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)405 void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
406 INHERITED(onClipPath, path, op, edgeStyle);
407 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
408 this->append<SkRecords::ClipPath>(path, opAA);
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
onNewSurface(const SkImageInfo &,const SkSurfaceProps &)416 sk_sp<SkSurface> SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
417 return nullptr;
418 }
419