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