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