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