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
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)142 void SkRecorder::onDrawPoints(PointMode mode,
143 size_t count,
144 const SkPoint pts[],
145 const SkPaint& paint) {
146 this->append<SkRecords::DrawPoints>(paint, mode, SkToUInt(count), this->copy(pts, count));
147 }
148
onDrawRect(const SkRect & rect,const SkPaint & paint)149 void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
150 TRY_MINIRECORDER(drawRect, rect, paint);
151 this->append<SkRecords::DrawRect>(paint, rect);
152 }
153
onDrawEdgeAARect(const SkRect & rect,SkCanvas::QuadAAFlags aa,SkColor color,SkBlendMode mode)154 void SkRecorder::onDrawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
155 SkBlendMode mode) {
156 this->append<SkRecords::DrawEdgeAARect>(rect, aa, color, mode);
157 }
158
onDrawRegion(const SkRegion & region,const SkPaint & paint)159 void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
160 this->append<SkRecords::DrawRegion>(paint, region);
161 }
162
onDrawOval(const SkRect & oval,const SkPaint & paint)163 void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
164 this->append<SkRecords::DrawOval>(paint, oval);
165 }
166
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)167 void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
168 bool useCenter, const SkPaint& paint) {
169 this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter);
170 }
171
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)172 void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
173 this->append<SkRecords::DrawRRect>(paint, rrect);
174 }
175
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)176 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
177 this->append<SkRecords::DrawDRRect>(paint, outer, inner);
178 }
179
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)180 void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
181 if (fDrawPictureMode == Record_DrawPictureMode) {
182 if (!fDrawableList) {
183 fDrawableList.reset(new SkDrawableList);
184 }
185 fDrawableList->append(drawable);
186 this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
187 } else {
188 SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
189 drawable->draw(this, matrix);
190 }
191 }
192
onDrawPath(const SkPath & path,const SkPaint & paint)193 void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
194 TRY_MINIRECORDER(drawPath, path, paint);
195 this->append<SkRecords::DrawPath>(paint, path);
196 }
197
onDrawBitmap(const SkBitmap & bitmap,SkScalar left,SkScalar top,const SkPaint * paint)198 void SkRecorder::onDrawBitmap(const SkBitmap& bitmap,
199 SkScalar left,
200 SkScalar top,
201 const SkPaint* paint) {
202 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
203 if (image) {
204 this->onDrawImage(image.get(), left, top, paint);
205 }
206 }
207
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)208 void SkRecorder::onDrawBitmapRect(const SkBitmap& bitmap,
209 const SkRect* src,
210 const SkRect& dst,
211 const SkPaint* paint,
212 SrcRectConstraint constraint) {
213 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
214 if (image) {
215 this->onDrawImageRect(image.get(), src, dst, paint, constraint);
216 }
217 }
218
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)219 void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
220 const SkIRect& center,
221 const SkRect& dst,
222 const SkPaint* paint) {
223 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
224 if (image) {
225 this->onDrawImageNine(image.get(), center, dst, paint);
226 }
227 }
228
onDrawBitmapLattice(const SkBitmap & bitmap,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)229 void SkRecorder::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
230 const SkRect& dst, const SkPaint* paint) {
231 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
232 this->onDrawImageLattice(image.get(), lattice, dst, paint);
233 }
234
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)235 void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
236 const SkPaint* paint) {
237 this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), left, top);
238 }
239
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)240 void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
241 const SkPaint* paint, SrcRectConstraint constraint) {
242 this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), this->copy(src), dst, constraint);
243 }
244
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)245 void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center,
246 const SkRect& dst, const SkPaint* paint) {
247 this->append<SkRecords::DrawImageNine>(this->copy(paint), sk_ref_sp(image), center, dst);
248 }
249
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)250 void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
251 const SkPaint* paint) {
252 int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
253 SkASSERT(lattice.fBounds);
254 this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image),
255 lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount),
256 lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount),
257 flagCount, this->copy(lattice.fRectTypes, flagCount),
258 this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst);
259 }
260
onDrawImageSet(const ImageSetEntry set[],int count,SkFilterQuality filterQuality,SkBlendMode mode)261 void SkRecorder::onDrawImageSet(const ImageSetEntry set[], int count, SkFilterQuality filterQuality,
262 SkBlendMode mode) {
263 SkAutoTArray<ImageSetEntry> setCopy(count);
264 for (int i = 0; i < count; ++i) {
265 setCopy[i] = set[i];
266 }
267 this->append<SkRecords::DrawImageSet>(std::move(setCopy), count, filterQuality, mode);
268 }
269
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)270 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
271 const SkPaint& paint) {
272 TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint);
273 this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y);
274 }
275
onDrawPicture(const SkPicture * pic,const SkMatrix * matrix,const SkPaint * paint)276 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
277 if (fDrawPictureMode == Record_DrawPictureMode) {
278 fApproxBytesUsedBySubPictures += pic->approximateBytesUsed();
279 this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
280 } else {
281 SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
282 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
283 pic->playback(this);
284 }
285 }
286
onDrawVerticesObject(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const SkPaint & paint)287 void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[],
288 int boneCount, SkBlendMode bmode, const SkPaint& paint) {
289 this->append<SkRecords::DrawVertices>(paint,
290 sk_ref_sp(const_cast<SkVertices*>(vertices)),
291 this->copy(bones, boneCount),
292 boneCount,
293 bmode);
294 }
295
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)296 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
297 const SkPoint texCoords[4], SkBlendMode bmode,
298 const SkPaint& paint) {
299 this->append<SkRecords::DrawPatch>(paint,
300 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
301 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
302 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
303 bmode);
304 }
305
onDrawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)306 void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
307 const SkColor colors[], int count, SkBlendMode mode,
308 const SkRect* cull, const SkPaint* paint) {
309 this->append<SkRecords::DrawAtlas>(this->copy(paint),
310 sk_ref_sp(atlas),
311 this->copy(xform, count),
312 this->copy(tex, count),
313 this->copy(colors, count),
314 count,
315 mode,
316 this->copy(cull));
317 }
318
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)319 void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
320 this->append<SkRecords::DrawShadowRec>(path, rec);
321 }
322
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)323 void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
324 this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value));
325 }
326
onFlush()327 void SkRecorder::onFlush() {
328 this->append<SkRecords::Flush>();
329 }
330
willSave()331 void SkRecorder::willSave() {
332 this->append<SkRecords::Save>();
333 }
334
getSaveLayerStrategy(const SaveLayerRec & rec)335 SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) {
336 this->append<SkRecords::SaveLayer>(this->copy(rec.fBounds)
337 , this->copy(rec.fPaint)
338 , sk_ref_sp(rec.fBackdrop)
339 , sk_ref_sp(rec.fClipMask)
340 , this->copy(rec.fClipMatrix)
341 , rec.fSaveLayerFlags);
342 return SkCanvas::kNoLayer_SaveLayerStrategy;
343 }
344
onDoSaveBehind(const SkRect * subset)345 bool SkRecorder::onDoSaveBehind(const SkRect* subset) {
346 this->append<SkRecords::SaveBehind>(this->copy(subset));
347 return false;
348 }
349
didRestore()350 void SkRecorder::didRestore() {
351 this->append<SkRecords::Restore>(this->getTotalMatrix());
352 }
353
didConcat(const SkMatrix & matrix)354 void SkRecorder::didConcat(const SkMatrix& matrix) {
355 this->append<SkRecords::Concat>(matrix);
356 }
357
didSetMatrix(const SkMatrix & matrix)358 void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
359 this->append<SkRecords::SetMatrix>(matrix);
360 }
361
didTranslate(SkScalar dx,SkScalar dy)362 void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) {
363 this->append<SkRecords::Translate>(dx, dy);
364 }
365
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)366 void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
367 INHERITED(onClipRect, rect, op, edgeStyle);
368 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
369 this->append<SkRecords::ClipRect>(rect, opAA);
370 }
371
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)372 void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
373 INHERITED(onClipRRect, rrect, op, edgeStyle);
374 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
375 this->append<SkRecords::ClipRRect>(rrect, opAA);
376 }
377
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)378 void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
379 INHERITED(onClipPath, path, op, edgeStyle);
380 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
381 this->append<SkRecords::ClipPath>(path, opAA);
382 }
383
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)384 void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
385 INHERITED(onClipRegion, deviceRgn, op);
386 this->append<SkRecords::ClipRegion>(deviceRgn, op);
387 }
388
onNewSurface(const SkImageInfo &,const SkSurfaceProps &)389 sk_sp<SkSurface> SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
390 return nullptr;
391 }
392