SkRecorder.cpp revision f0f14113431ace669f278fdd97b50950f2cf4c80
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 "SkRecorder.h"
9#include "SkPatchUtils.h"
10#include "SkPicture.h"
11
12SkCanvasDrawableList::~SkCanvasDrawableList() {
13    fArray.unrefAll();
14}
15
16SkPicture::SnapshotArray* SkCanvasDrawableList::newDrawableSnapshot() {
17    const int count = fArray.count();
18    if (0 == count) {
19        return NULL;
20    }
21    SkAutoTMalloc<const SkPicture*> pics(count);
22    for (int i = 0; i < count; ++i) {
23        pics[i] = fArray[i]->newPictureSnapshot();
24    }
25    return SkNEW_ARGS(SkPicture::SnapshotArray, (pics.detach(), count));
26}
27
28void SkCanvasDrawableList::append(SkCanvasDrawable* drawable) {
29    *fArray.append() = SkRef(drawable);
30}
31
32///////////////////////////////////////////////////////////////////////////////////////////////
33
34SkRecorder::SkRecorder(SkRecord* record, int width, int height)
35    : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
36    , fRecord(record)
37    , fSaveLayerCount(0) {}
38
39SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
40    : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
41    , fRecord(record)
42    , fSaveLayerCount(0) {}
43
44void SkRecorder::forgetRecord() {
45    fDrawableList.reset(NULL);
46    fRecord = NULL;
47}
48
49// To make appending to fRecord a little less verbose.
50#define APPEND(T, ...) \
51        SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
52
53// For methods which must call back into SkCanvas.
54#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
55
56// The structs we're creating all copy their constructor arguments.  Given the way the SkRecords
57// framework works, sometimes they happen to technically be copied twice, which is fine and elided
58// into a single copy unless the class has a non-trivial copy constructor.  For classes with
59// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
60// with delay_copy(), forcing the argument to be passed by const&.
61//
62// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
63// constructors and destructors.  You'll know you've got a good candidate T if you see ~T() show up
64// unexpectedly on a profile of record time.  Otherwise don't bother.
65template <typename T>
66class Reference {
67public:
68    Reference(const T& x) : fX(x) {}
69    operator const T&() const { return fX; }
70private:
71    const T& fX;
72};
73
74template <typename T>
75static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
76
77// Use copy() only for optional arguments, to be copied if present or skipped if not.
78// (For most types we just pass by value and let copy constructors do their thing.)
79template <typename T>
80T* SkRecorder::copy(const T* src) {
81    if (NULL == src) {
82        return NULL;
83    }
84    return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
85}
86
87// This copy() is for arrays.
88// It will work with POD or non-POD, though currently we only use it for POD.
89template <typename T>
90T* SkRecorder::copy(const T src[], size_t count) {
91    if (NULL == src) {
92        return NULL;
93    }
94    T* dst = fRecord->alloc<T>(count);
95    for (size_t i = 0; i < count; i++) {
96        SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
97    }
98    return dst;
99}
100
101// Specialization for copying strings, using memcpy.
102// This measured around 2x faster for copying code points,
103// but I found no corresponding speedup for other arrays.
104template <>
105char* SkRecorder::copy(const char src[], size_t count) {
106    if (NULL == src) {
107        return NULL;
108    }
109    char* dst = fRecord->alloc<char>(count);
110    memcpy(dst, src, count);
111    return dst;
112}
113
114// As above, assuming and copying a terminating \0.
115template <>
116char* SkRecorder::copy(const char* src) {
117    return this->copy(src, strlen(src)+1);
118}
119
120
121void SkRecorder::drawPaint(const SkPaint& paint) {
122    APPEND(DrawPaint, delay_copy(paint));
123}
124
125void SkRecorder::drawPoints(PointMode mode,
126                            size_t count,
127                            const SkPoint pts[],
128                            const SkPaint& paint) {
129    APPEND(DrawPoints, delay_copy(paint), mode, SkToUInt(count), this->copy(pts, count));
130}
131
132void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
133    APPEND(DrawRect, delay_copy(paint), rect);
134}
135
136void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
137    APPEND(DrawOval, delay_copy(paint), oval);
138}
139
140void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
141    APPEND(DrawRRect, delay_copy(paint), rrect);
142}
143
144void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
145    APPEND(DrawDRRect, delay_copy(paint), outer, inner);
146}
147
148void SkRecorder::onDrawDrawable(SkCanvasDrawable* drawable) {
149    if (!fDrawableList) {
150        fDrawableList.reset(SkNEW(SkCanvasDrawableList));
151    }
152    fDrawableList->append(drawable);
153    APPEND(DrawDrawable, drawable->getBounds(), fDrawableList->count() - 1);
154}
155
156void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
157    APPEND(DrawPath, delay_copy(paint), delay_copy(path));
158}
159
160void SkRecorder::drawBitmap(const SkBitmap& bitmap,
161                            SkScalar left,
162                            SkScalar top,
163                            const SkPaint* paint) {
164    APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
165}
166
167void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
168                                      const SkRect* src,
169                                      const SkRect& dst,
170                                      const SkPaint* paint,
171                                      DrawBitmapRectFlags flags) {
172    if (kBleed_DrawBitmapRectFlag == flags) {
173        APPEND(DrawBitmapRectToRectBleed,
174               this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
175        return;
176    }
177    SkASSERT(kNone_DrawBitmapRectFlag == flags);
178    APPEND(DrawBitmapRectToRect,
179           this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
180}
181
182void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
183                                const SkIRect& center,
184                                const SkRect& dst,
185                                const SkPaint* paint) {
186    APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
187}
188
189void SkRecorder::drawImage(const SkImage* image, SkScalar left, SkScalar top,
190                           const SkPaint* paint) {
191    APPEND(DrawImage, this->copy(paint), image, left, top);
192}
193
194void SkRecorder::drawImageRect(const SkImage* image, const SkRect* src,
195                               const SkRect& dst,
196                               const SkPaint* paint) {
197    APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
198}
199
200void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
201    APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
202}
203
204void SkRecorder::onDrawText(const void* text, size_t byteLength,
205                            SkScalar x, SkScalar y, const SkPaint& paint) {
206    APPEND(DrawText,
207           delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
208}
209
210void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
211                               const SkPoint pos[], const SkPaint& paint) {
212    const unsigned points = paint.countText(text, byteLength);
213    APPEND(DrawPosText,
214           delay_copy(paint),
215           this->copy((const char*)text, byteLength),
216           byteLength,
217           this->copy(pos, points));
218}
219
220void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
221                                const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
222    const unsigned points = paint.countText(text, byteLength);
223    APPEND(DrawPosTextH,
224           delay_copy(paint),
225           this->copy((const char*)text, byteLength),
226           SkToUInt(byteLength),
227           constY,
228           this->copy(xpos, points));
229}
230
231void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
232                                  const SkMatrix* matrix, const SkPaint& paint) {
233    APPEND(DrawTextOnPath,
234           delay_copy(paint),
235           this->copy((const char*)text, byteLength),
236           byteLength,
237           delay_copy(path),
238           matrix ? *matrix : SkMatrix::I());
239}
240
241void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
242                                const SkPaint& paint) {
243    APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
244}
245
246void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
247    APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
248}
249
250void SkRecorder::drawVertices(VertexMode vmode,
251                              int vertexCount, const SkPoint vertices[],
252                              const SkPoint texs[], const SkColor colors[],
253                              SkXfermode* xmode,
254                              const uint16_t indices[], int indexCount, const SkPaint& paint) {
255    APPEND(DrawVertices, delay_copy(paint),
256                         vmode,
257                         vertexCount,
258                         this->copy(vertices, vertexCount),
259                         texs ? this->copy(texs, vertexCount) : NULL,
260                         colors ? this->copy(colors, vertexCount) : NULL,
261                         xmode,
262                         this->copy(indices, indexCount),
263                         indexCount);
264}
265
266void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
267                             const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
268    APPEND(DrawPatch, delay_copy(paint),
269           cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
270           colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
271           texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
272           xmode);
273}
274
275void SkRecorder::willSave() {
276    fSaveIsSaveLayer.push(false);
277    APPEND(Save);
278}
279
280SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
281                                                      const SkPaint* paint,
282                                                      SkCanvas::SaveFlags flags) {
283    fSaveLayerCount++;
284    fSaveIsSaveLayer.push(true);
285    APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
286    return SkCanvas::kNoLayer_SaveLayerStrategy;
287}
288
289void SkRecorder::didRestore() {
290    SkBool8 saveLayer;
291    fSaveIsSaveLayer.pop(&saveLayer);
292    if (saveLayer) {
293        fSaveLayerCount--;
294    }
295    APPEND(Restore, this->devBounds(), this->getTotalMatrix());
296}
297
298void SkRecorder::didConcat(const SkMatrix& matrix) {
299    this->didSetMatrix(this->getTotalMatrix());
300}
301
302void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
303    SkDEVCODE(if (matrix != this->getTotalMatrix()) {
304        matrix.dump();
305        this->getTotalMatrix().dump();
306        SkASSERT(matrix == this->getTotalMatrix());
307    })
308    APPEND(SetMatrix, matrix);
309}
310
311void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
312    INHERITED(onClipRect, rect, op, edgeStyle);
313    SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
314    APPEND(ClipRect, this->devBounds(), rect, opAA);
315}
316
317void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
318    INHERITED(onClipRRect, rrect, op, edgeStyle);
319    SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
320    APPEND(ClipRRect, this->devBounds(), rrect, opAA);
321}
322
323void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
324    INHERITED(onClipPath, path, op, edgeStyle);
325    SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
326    APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
327}
328
329void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
330    INHERITED(onClipRegion, deviceRgn, op);
331    APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
332}
333
334void SkRecorder::beginCommentGroup(const char* description) {
335    APPEND(BeginCommentGroup, this->copy(description));
336}
337
338void SkRecorder::addComment(const char* key, const char* value) {
339    APPEND(AddComment, this->copy(key), this->copy(value));
340}
341
342void SkRecorder::endCommentGroup() {
343    APPEND(EndCommentGroup);
344}
345
346bool SkRecorder::isDrawingToLayer() const {
347    return fSaveLayerCount > 0;
348}
349
350void SkRecorder::drawData(const void* data, size_t length) {
351    APPEND(DrawData, copy((const char*)data), length);
352}
353