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