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 <memory>
19 #include <new>
20 
~SkDrawableList()21 SkDrawableList::~SkDrawableList() {
22     fArray.unrefAll();
23 }
24 
newDrawableSnapshot()25 SkBigPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
26     const int count = fArray.count();
27     if (0 == count) {
28         return nullptr;
29     }
30     SkAutoTMalloc<const SkPicture*> pics(count);
31     for (int i = 0; i < count; ++i) {
32         pics[i] = fArray[i]->newPictureSnapshot();
33     }
34     return new SkBigPicture::SnapshotArray(pics.release(), count);
35 }
36 
append(SkDrawable * drawable)37 void SkDrawableList::append(SkDrawable* drawable) {
38     *fArray.append() = SkRef(drawable);
39 }
40 
41 ///////////////////////////////////////////////////////////////////////////////////////////////
42 
SkRecorder(SkRecord * record,int width,int height,SkMiniRecorder * mr)43 SkRecorder::SkRecorder(SkRecord* record, int width, int height, SkMiniRecorder* mr)
44     : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height)
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     , fApproxBytesUsedBySubPictures(0)
52     , fRecord(record)
53     , fMiniRecorder(mr) {}
54 
reset(SkRecord * record,const SkRect & bounds,SkMiniRecorder * mr)55 void SkRecorder::reset(SkRecord* record, const SkRect& bounds, SkMiniRecorder* mr) {
56     this->forgetRecord();
57     fRecord = record;
58     this->resetCanvas(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 template<typename T, typename... Args>
append(Args &&...args)70 void SkRecorder::append(Args&&... args) {
71     if (fMiniRecorder) {
72         this->flushMiniRecorder();
73     }
74     new (fRecord->append<T>()) T{std::forward<Args>(args)...};
75 }
76 
77 #define TRY_MINIRECORDER(method, ...) \
78     if (fMiniRecorder && fMiniRecorder->method(__VA_ARGS__)) return
79 
80 // For methods which must call back into SkNoDrawCanvas.
81 #define INHERITED(method, ...) this->SkNoDrawCanvas::method(__VA_ARGS__)
82 
83 // Use copy() only for optional arguments, to be copied if present or skipped if not.
84 // (For most types we just pass by value and let copy constructors do their thing.)
85 template <typename T>
copy(const T * src)86 T* SkRecorder::copy(const T* src) {
87     if (nullptr == src) {
88         return nullptr;
89     }
90     return new (fRecord->alloc<T>()) T(*src);
91 }
92 
93 // This copy() is for arrays.
94 // It will work with POD or non-POD, though currently we only use it for POD.
95 template <typename T>
copy(const T src[],size_t count)96 T* SkRecorder::copy(const T src[], size_t count) {
97     if (nullptr == src) {
98         return nullptr;
99     }
100     T* dst = fRecord->alloc<T>(count);
101     for (size_t i = 0; i < count; i++) {
102         new (dst + i) T(src[i]);
103     }
104     return dst;
105 }
106 
107 // Specialization for copying strings, using memcpy.
108 // This measured around 2x faster for copying code points,
109 // but I found no corresponding speedup for other arrays.
110 template <>
copy(const char src[],size_t count)111 char* SkRecorder::copy(const char src[], size_t count) {
112     if (nullptr == src) {
113         return nullptr;
114     }
115     char* dst = fRecord->alloc<char>(count);
116     memcpy(dst, src, count);
117     return dst;
118 }
119 
120 // As above, assuming and copying a terminating \0.
121 template <>
copy(const char * src)122 char* SkRecorder::copy(const char* src) {
123     return this->copy(src, strlen(src)+1);
124 }
125 
flushMiniRecorder()126 void SkRecorder::flushMiniRecorder() {
127     if (fMiniRecorder) {
128         SkMiniRecorder* mr = fMiniRecorder;
129         fMiniRecorder = nullptr;  // Needs to happen before flushAndReset() or we recurse forever.
130         mr->flushAndReset(this);
131     }
132 }
133 
onDrawPaint(const SkPaint & paint)134 void SkRecorder::onDrawPaint(const SkPaint& paint) {
135     this->append<SkRecords::DrawPaint>(paint);
136 }
137 
onDrawBehind(const SkPaint & paint)138 void SkRecorder::onDrawBehind(const SkPaint& paint) {
139     this->append<SkRecords::DrawBehind>(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 
onDrawRegion(const SkRegion & region,const SkPaint & paint)154 void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
155     this->append<SkRecords::DrawRegion>(paint, region);
156 }
157 
onDrawOval(const SkRect & oval,const SkPaint & paint)158 void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
159     this->append<SkRecords::DrawOval>(paint, oval);
160 }
161 
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)162 void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
163                            bool useCenter, const SkPaint& paint) {
164     this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter);
165 }
166 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)167 void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
168     this->append<SkRecords::DrawRRect>(paint, rrect);
169 }
170 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)171 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
172     this->append<SkRecords::DrawDRRect>(paint, outer, inner);
173 }
174 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)175 void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
176     if (!fDrawableList) {
177         fDrawableList = std::make_unique<SkDrawableList>();
178     }
179     fDrawableList->append(drawable);
180     this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
181 }
182 
onDrawPath(const SkPath & path,const SkPaint & paint)183 void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
184     TRY_MINIRECORDER(drawPath, path, paint);
185     this->append<SkRecords::DrawPath>(paint, path);
186 }
187 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)188 void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
189                              const SkPaint* paint) {
190     this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), left, top);
191 }
192 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)193 void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
194                                  const SkPaint* paint, SrcRectConstraint constraint) {
195     this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), this->copy(src), dst, constraint);
196 }
197 
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)198 void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center,
199                                  const SkRect& dst, const SkPaint* paint) {
200     this->append<SkRecords::DrawImageNine>(this->copy(paint), sk_ref_sp(image), center, dst);
201 }
202 
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)203 void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
204                                     const SkPaint* paint) {
205     int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
206     SkASSERT(lattice.fBounds);
207     this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image),
208            lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount),
209            lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount),
210            flagCount, this->copy(lattice.fRectTypes, flagCount),
211            this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst);
212 }
213 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)214 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
215                                 const SkPaint& paint) {
216     TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint);
217     this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y);
218 }
219 
onDrawPicture(const SkPicture * pic,const SkMatrix * matrix,const SkPaint * paint)220 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
221     fApproxBytesUsedBySubPictures += pic->approximateBytesUsed();
222     this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
223 }
224 
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)225 void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
226                                       const SkPaint& paint) {
227     this->append<SkRecords::DrawVertices>(paint,
228                                           sk_ref_sp(const_cast<SkVertices*>(vertices)),
229                                           bmode);
230 }
231 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)232 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
233                              const SkPoint texCoords[4], SkBlendMode bmode,
234                              const SkPaint& paint) {
235     this->append<SkRecords::DrawPatch>(paint,
236            cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
237            colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
238            texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
239            bmode);
240 }
241 
onDrawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)242 void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
243                              const SkColor colors[], int count, SkBlendMode mode,
244                              const SkRect* cull, const SkPaint* paint) {
245     this->append<SkRecords::DrawAtlas>(this->copy(paint),
246            sk_ref_sp(atlas),
247            this->copy(xform, count),
248            this->copy(tex, count),
249            this->copy(colors, count),
250            count,
251            mode,
252            this->copy(cull));
253 }
254 
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)255 void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
256     this->append<SkRecords::DrawShadowRec>(path, rec);
257 }
258 
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)259 void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
260     this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value));
261 }
262 
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)263 void SkRecorder::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
264                                   QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
265     this->append<SkRecords::DrawEdgeAAQuad>(
266             rect, this->copy(clip, 4), aa, color, mode);
267 }
268 
onDrawEdgeAAImageSet(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkPaint * paint,SrcRectConstraint constraint)269 void SkRecorder::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count,
270                                       const SkPoint dstClips[], const SkMatrix preViewMatrices[],
271                                       const SkPaint* paint, SrcRectConstraint constraint) {
272     int totalDstClipCount, totalMatrixCount;
273     SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount);
274 
275     SkAutoTArray<ImageSetEntry> setCopy(count);
276     for (int i = 0; i < count; ++i) {
277         setCopy[i] = set[i];
278     }
279 
280     this->append<SkRecords::DrawEdgeAAImageSet>(this->copy(paint), std::move(setCopy), count,
281             this->copy(dstClips, totalDstClipCount),
282             this->copy(preViewMatrices, totalMatrixCount), constraint);
283 }
284 
onFlush()285 void SkRecorder::onFlush() {
286     this->append<SkRecords::Flush>();
287 }
288 
willSave()289 void SkRecorder::willSave() {
290     this->append<SkRecords::Save>();
291 }
292 
getSaveLayerStrategy(const SaveLayerRec & rec)293 SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) {
294     this->append<SkRecords::SaveLayer>(this->copy(rec.fBounds)
295                     , this->copy(rec.fPaint)
296                     , sk_ref_sp(rec.fBackdrop)
297                     , rec.fSaveLayerFlags);
298     return SkCanvas::kNoLayer_SaveLayerStrategy;
299 }
300 
onDoSaveBehind(const SkRect * subset)301 bool SkRecorder::onDoSaveBehind(const SkRect* subset) {
302     this->append<SkRecords::SaveBehind>(this->copy(subset));
303     return false;
304 }
305 
didRestore()306 void SkRecorder::didRestore() {
307     this->append<SkRecords::Restore>(this->getTotalMatrix());
308 }
309 
onMarkCTM(const char * name)310 void SkRecorder::onMarkCTM(const char* name) {
311     this->append<SkRecords::MarkCTM>(SkString(name));
312 }
313 
didConcat44(const SkM44 & m)314 void SkRecorder::didConcat44(const SkM44& m) {
315     this->append<SkRecords::Concat44>(m);
316 }
317 
didConcat(const SkMatrix & matrix)318 void SkRecorder::didConcat(const SkMatrix& matrix) {
319     this->append<SkRecords::Concat>(matrix);
320 }
321 
didSetMatrix(const SkMatrix & matrix)322 void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
323     this->append<SkRecords::SetMatrix>(matrix);
324 }
325 
didScale(SkScalar sx,SkScalar sy)326 void SkRecorder::didScale(SkScalar sx, SkScalar sy) {
327     this->append<SkRecords::Scale>(sx, sy);
328 }
329 
didTranslate(SkScalar dx,SkScalar dy)330 void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) {
331     this->append<SkRecords::Translate>(dx, dy);
332 }
333 
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)334 void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
335     INHERITED(onClipRect, rect, op, edgeStyle);
336     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
337     this->append<SkRecords::ClipRect>(rect, opAA);
338 }
339 
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)340 void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
341     INHERITED(onClipRRect, rrect, op, edgeStyle);
342     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
343     this->append<SkRecords::ClipRRect>(rrect, opAA);
344 }
345 
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)346 void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
347     INHERITED(onClipPath, path, op, edgeStyle);
348     SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
349     this->append<SkRecords::ClipPath>(path, opAA);
350 }
351 
onClipShader(sk_sp<SkShader> cs,SkClipOp op)352 void SkRecorder::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
353     INHERITED(onClipShader, cs, op);
354     this->append<SkRecords::ClipShader>(std::move(cs), op);
355 }
356 
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)357 void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
358     INHERITED(onClipRegion, deviceRgn, op);
359     this->append<SkRecords::ClipRegion>(deviceRgn, op);
360 }
361 
onNewSurface(const SkImageInfo &,const SkSurfaceProps &)362 sk_sp<SkSurface> SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
363     return nullptr;
364 }
365