• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkPatchUtils.h"
11 #include "SkPicture.h"
12 #include "SkPictureUtils.h"
13 #include "SkRecorder.h"
14 
15 //#define WRAP_BITMAP_AS_IMAGE
16 
~SkDrawableList()17 SkDrawableList::~SkDrawableList() {
18     fArray.unrefAll();
19 }
20 
newDrawableSnapshot()21 SkBigPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
22     const int count = fArray.count();
23     if (0 == count) {
24         return nullptr;
25     }
26     SkAutoTMalloc<const SkPicture*> pics(count);
27     for (int i = 0; i < count; ++i) {
28         pics[i] = fArray[i]->newPictureSnapshot();
29     }
30     return new SkBigPicture::SnapshotArray(pics.detach(), count);
31 }
32 
append(SkDrawable * drawable)33 void SkDrawableList::append(SkDrawable* drawable) {
34     *fArray.append() = SkRef(drawable);
35 }
36 
37 ///////////////////////////////////////////////////////////////////////////////////////////////
38 
SkRecorder(SkRecord * record,int width,int height,SkMiniRecorder * mr)39 SkRecorder::SkRecorder(SkRecord* record, int width, int height, SkMiniRecorder* mr)
40     : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
41     , fDrawPictureMode(Record_DrawPictureMode)
42     , fApproxBytesUsedBySubPictures(0)
43     , fRecord(record)
44     , fMiniRecorder(mr) {}
45 
SkRecorder(SkRecord * record,const SkRect & bounds,SkMiniRecorder * mr)46 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds, SkMiniRecorder* mr)
47     : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
48     , fDrawPictureMode(Record_DrawPictureMode)
49     , fApproxBytesUsedBySubPictures(0)
50     , fRecord(record)
51     , fMiniRecorder(mr) {}
52 
reset(SkRecord * record,const SkRect & bounds,DrawPictureMode dpm,SkMiniRecorder * mr)53 void SkRecorder::reset(SkRecord* record, const SkRect& bounds,
54                        DrawPictureMode dpm, SkMiniRecorder* mr) {
55     this->forgetRecord();
56     fDrawPictureMode = dpm;
57     fRecord = record;
58     this->resetForNextPicture(bounds.roundOut());
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 SkCanvas.
79 #define INHERITED(method, ...) this->SkCanvas::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 
onDrawOval(const SkRect & oval,const SkPaint & paint)148 void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
149     APPEND(DrawOval, paint, oval);
150 }
151 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)152 void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
153     APPEND(DrawRRect, paint, rrect);
154 }
155 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)156 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
157     APPEND(DrawDRRect, paint, outer, inner);
158 }
159 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)160 void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
161     if (fDrawPictureMode == Record_DrawPictureMode) {
162         if (!fDrawableList) {
163             fDrawableList.reset(new SkDrawableList);
164         }
165         fDrawableList->append(drawable);
166         APPEND(DrawDrawable, this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
167     } else {
168         SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
169         drawable->draw(this, matrix);
170     }
171 }
172 
onDrawPath(const SkPath & path,const SkPaint & paint)173 void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
174     TRY_MINIRECORDER(drawPath, path, paint);
175     APPEND(DrawPath, paint, path);
176 }
177 
onDrawBitmap(const SkBitmap & bitmap,SkScalar left,SkScalar top,const SkPaint * paint)178 void SkRecorder::onDrawBitmap(const SkBitmap& bitmap,
179                               SkScalar left,
180                               SkScalar top,
181                               const SkPaint* paint) {
182 #ifdef WRAP_BITMAP_AS_IMAGE
183     SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
184     if (image) {
185         this->onDrawImage(image, left, top, paint);
186     }
187 #else
188     APPEND(DrawBitmap, this->copy(paint), bitmap, left, top);
189 #endif
190 }
191 
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)192 void SkRecorder::onDrawBitmapRect(const SkBitmap& bitmap,
193                                   const SkRect* src,
194                                   const SkRect& dst,
195                                   const SkPaint* paint,
196                                   SrcRectConstraint constraint) {
197 #ifdef WRAP_BITMAP_AS_IMAGE
198     // TODO: need a way to support the flags for images...
199     SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
200     if (image) {
201         this->onDrawImageRect(image, src, dst, paint);
202     }
203 #else
204     TRY_MINIRECORDER(drawBitmapRect, bitmap, src, dst, paint, constraint);
205     if (kFast_SrcRectConstraint == constraint) {
206         APPEND(DrawBitmapRectFast, this->copy(paint), bitmap, this->copy(src), dst);
207         return;
208     }
209     SkASSERT(kStrict_SrcRectConstraint == constraint);
210     APPEND(DrawBitmapRect, this->copy(paint), bitmap, this->copy(src), dst);
211 #endif
212 }
213 
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)214 void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
215                                   const SkIRect& center,
216                                   const SkRect& dst,
217                                   const SkPaint* paint) {
218 #ifdef WRAP_BITMAP_AS_IMAGE
219     SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
220     if (image) {
221         this->onDrawImageNine(image, center, dst, paint);
222     }
223 #else
224     APPEND(DrawBitmapNine, this->copy(paint), bitmap, center, dst);
225 #endif
226 }
227 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)228 void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
229                              const SkPaint* paint) {
230     APPEND(DrawImage, this->copy(paint), image, left, top);
231 }
232 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)233 void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
234                                  const SkPaint* paint, SrcRectConstraint constraint) {
235     APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst, constraint);
236 }
237 
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)238 void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center,
239                                  const SkRect& dst, const SkPaint* paint) {
240     APPEND(DrawImageNine, this->copy(paint), image, center, dst);
241 }
242 
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)243 void SkRecorder::onDrawText(const void* text, size_t byteLength,
244                             SkScalar x, SkScalar y, const SkPaint& paint) {
245     APPEND(DrawText,
246            paint, this->copy((const char*)text, byteLength), byteLength, x, y);
247 }
248 
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)249 void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
250                                const SkPoint pos[], const SkPaint& paint) {
251     const int points = paint.countText(text, byteLength);
252     APPEND(DrawPosText,
253            paint,
254            this->copy((const char*)text, byteLength),
255            byteLength,
256            this->copy(pos, points));
257 }
258 
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)259 void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
260                                 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
261     const int points = paint.countText(text, byteLength);
262     APPEND(DrawPosTextH,
263            paint,
264            this->copy((const char*)text, byteLength),
265            SkToUInt(byteLength),
266            constY,
267            this->copy(xpos, points));
268 }
269 
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)270 void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
271                                   const SkMatrix* matrix, const SkPaint& paint) {
272     APPEND(DrawTextOnPath,
273            paint,
274            this->copy((const char*)text, byteLength),
275            byteLength,
276            path,
277            matrix ? *matrix : SkMatrix::I());
278 }
279 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)280 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
281                                 const SkPaint& paint) {
282     TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint);
283     APPEND(DrawTextBlob, paint, blob, x, y);
284 }
285 
onDrawPicture(const SkPicture * pic,const SkMatrix * matrix,const SkPaint * paint)286 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
287     if (fDrawPictureMode == Record_DrawPictureMode) {
288         fApproxBytesUsedBySubPictures += SkPictureUtils::ApproximateBytesUsed(pic);
289         APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
290     } else {
291         SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
292         SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
293         pic->playback(this);
294     }
295 }
296 
onDrawVertices(VertexMode vmode,int vertexCount,const SkPoint vertices[],const SkPoint texs[],const SkColor colors[],SkXfermode * xmode,const uint16_t indices[],int indexCount,const SkPaint & paint)297 void SkRecorder::onDrawVertices(VertexMode vmode,
298                                 int vertexCount, const SkPoint vertices[],
299                                 const SkPoint texs[], const SkColor colors[],
300                                 SkXfermode* xmode,
301                                 const uint16_t indices[], int indexCount, const SkPaint& paint) {
302     APPEND(DrawVertices, paint,
303                          vmode,
304                          vertexCount,
305                          this->copy(vertices, vertexCount),
306                          texs ? this->copy(texs, vertexCount) : nullptr,
307                          colors ? this->copy(colors, vertexCount) : nullptr,
308                          xmode,
309                          this->copy(indices, indexCount),
310                          indexCount);
311 }
312 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkXfermode * xmode,const SkPaint & paint)313 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
314                              const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
315     APPEND(DrawPatch, paint,
316            cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
317            colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
318            texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
319            xmode);
320 }
321 
onDrawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkXfermode::Mode mode,const SkRect * cull,const SkPaint * paint)322 void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
323                              const SkColor colors[], int count, SkXfermode::Mode mode,
324                              const SkRect* cull, const SkPaint* paint) {
325     APPEND(DrawAtlas, this->copy(paint),
326            atlas,
327            this->copy(xform, count),
328            this->copy(tex, count),
329            this->copy(colors, count),
330            count,
331            mode,
332            this->copy(cull));
333 }
334 
willSave()335 void SkRecorder::willSave() {
336     APPEND(Save);
337 }
338 
getSaveLayerStrategy(const SaveLayerRec & rec)339 SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) {
340     APPEND(SaveLayer,
341            this->copy(rec.fBounds), this->copy(rec.fPaint), rec.fBackdrop, rec.fSaveLayerFlags);
342     return SkCanvas::kNoLayer_SaveLayerStrategy;
343 }
344 
didRestore()345 void SkRecorder::didRestore() {
346     APPEND(Restore, this->devBounds(), this->getTotalMatrix());
347 }
348 
didConcat(const SkMatrix & matrix)349 void SkRecorder::didConcat(const SkMatrix& matrix) {
350     APPEND(Concat, matrix);
351 }
352 
didSetMatrix(const SkMatrix & matrix)353 void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
354     APPEND(SetMatrix, matrix);
355 }
356 
onClipRect(const SkRect & rect,SkRegion::Op op,ClipEdgeStyle edgeStyle)357 void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
358     INHERITED(onClipRect, rect, op, edgeStyle);
359     SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
360     APPEND(ClipRect, this->devBounds(), rect, opAA);
361 }
362 
onClipRRect(const SkRRect & rrect,SkRegion::Op op,ClipEdgeStyle edgeStyle)363 void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
364     INHERITED(onClipRRect, rrect, op, edgeStyle);
365     SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
366     APPEND(ClipRRect, this->devBounds(), rrect, opAA);
367 }
368 
onClipPath(const SkPath & path,SkRegion::Op op,ClipEdgeStyle edgeStyle)369 void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
370     INHERITED(onClipPath, path, op, edgeStyle);
371     SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
372     APPEND(ClipPath, this->devBounds(), path, opAA);
373 }
374 
onClipRegion(const SkRegion & deviceRgn,SkRegion::Op op)375 void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
376     INHERITED(onClipRegion, deviceRgn, op);
377     APPEND(ClipRegion, this->devBounds(), deviceRgn, op);
378 }
379 
380