2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkRecorder.h"
9 #include "SkPatchUtils.h"
10 #include "SkPicture.h"
12 // SkCanvas will fail in mysterious ways if it doesn't know the real width and height.
13 SkRecorder::SkRecorder(SkRecord* record, int width, int height)
14 : SkCanvas(width, height, SkCanvas::kConservativeRasterClip_InitFlag)
16 , fSaveLayerCount(0) {}
18 void SkRecorder::forgetRecord() {
22 // To make appending to fRecord a little less verbose.
23 #define APPEND(T, ...) \
24 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
26 // For methods which must call back into SkCanvas.
27 #define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
29 // The structs we're creating all copy their constructor arguments. Given the way the SkRecords
30 // framework works, sometimes they happen to technically be copied twice, which is fine and elided
31 // into a single copy unless the class has a non-trivial copy constructor. For classes with
32 // non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
33 // with delay_copy(), forcing the argument to be passed by const&.
35 // This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
36 // constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
37 // unexpectedly on a profile of record time. Otherwise don't bother.
41 Reference(const T& x) : fX(x) {}
42 operator const T&() const { return fX; }
48 static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
50 // Use copy() only for optional arguments, to be copied if present or skipped if not.
51 // (For most types we just pass by value and let copy constructors do their thing.)
53 T* SkRecorder::copy(const T* src) {
57 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
60 // This copy() is for arrays.
61 // It will work with POD or non-POD, though currently we only use it for POD.
63 T* SkRecorder::copy(const T src[], size_t count) {
67 T* dst = fRecord->alloc<T>(count);
68 for (size_t i = 0; i < count; i++) {
69 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
74 // Specialization for copying strings, using memcpy.
75 // This measured around 2x faster for copying code points,
76 // but I found no corresponding speedup for other arrays.
78 char* SkRecorder::copy(const char src[], size_t count) {
82 char* dst = fRecord->alloc<char>(count);
83 memcpy(dst, src, count);
87 // As above, assuming and copying a terminating \0.
89 char* SkRecorder::copy(const char* src) {
90 return this->copy(src, strlen(src)+1);
94 void SkRecorder::clear(SkColor color) {
98 void SkRecorder::drawPaint(const SkPaint& paint) {
99 APPEND(DrawPaint, delay_copy(paint));
102 void SkRecorder::drawPoints(PointMode mode,
105 const SkPaint& paint) {
106 APPEND(DrawPoints, delay_copy(paint), mode, count, this->copy(pts, count));
109 void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
110 APPEND(DrawRect, delay_copy(paint), rect);
113 void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
114 APPEND(DrawOval, delay_copy(paint), oval);
117 void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
118 APPEND(DrawRRect, delay_copy(paint), rrect);
121 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
122 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
125 void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
126 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
129 void SkRecorder::drawBitmap(const SkBitmap& bitmap,
132 const SkPaint* paint) {
133 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
136 void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
139 const SkPaint* paint,
140 DrawBitmapRectFlags flags) {
141 APPEND(DrawBitmapRectToRect,
142 this->copy(paint), delay_copy(bitmap), this->copy(src), dst, flags);
145 void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
146 const SkMatrix& matrix,
147 const SkPaint* paint) {
148 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
151 void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
152 const SkIRect& center,
154 const SkPaint* paint) {
155 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
158 void SkRecorder::drawImage(const SkImage* image, SkScalar left, SkScalar top,
159 const SkPaint* paint) {
160 APPEND(DrawImage, this->copy(paint), image, left, top);
163 void SkRecorder::drawImageRect(const SkImage* image, const SkRect* src,
165 const SkPaint* paint) {
166 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
169 void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
170 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
173 void SkRecorder::onDrawText(const void* text, size_t byteLength,
174 SkScalar x, SkScalar y, const SkPaint& paint) {
176 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
179 void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
180 const SkPoint pos[], const SkPaint& paint) {
181 const unsigned points = paint.countText(text, byteLength);
184 this->copy((const char*)text, byteLength),
186 this->copy(pos, points));
189 void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
190 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
191 const unsigned points = paint.countText(text, byteLength);
194 this->copy((const char*)text, byteLength),
196 this->copy(xpos, points),
200 void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
201 const SkMatrix* matrix, const SkPaint& paint) {
202 APPEND(DrawTextOnPath,
204 this->copy((const char*)text, byteLength),
210 void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
211 const SkPaint& paint) {
212 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
215 void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
216 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
219 void SkRecorder::drawVertices(VertexMode vmode,
220 int vertexCount, const SkPoint vertices[],
221 const SkPoint texs[], const SkColor colors[],
223 const uint16_t indices[], int indexCount, const SkPaint& paint) {
224 APPEND(DrawVertices, delay_copy(paint),
227 this->copy(vertices, vertexCount),
228 texs ? this->copy(texs, vertexCount) : NULL,
229 colors ? this->copy(colors, vertexCount) : NULL,
231 this->copy(indices, indexCount),
235 void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
236 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
237 APPEND(DrawPatch, delay_copy(paint),
238 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
239 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
240 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
244 void SkRecorder::willSave() {
245 fSaveIsSaveLayer.push(false);
249 SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
250 const SkPaint* paint,
251 SkCanvas::SaveFlags flags) {
253 fSaveIsSaveLayer.push(true);
254 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
255 return SkCanvas::kNoLayer_SaveLayerStrategy;
258 void SkRecorder::didRestore() {
260 fSaveIsSaveLayer.pop(&saveLayer);
264 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
267 void SkRecorder::onPushCull(const SkRect& rect) {
268 APPEND(PushCull, rect);
271 void SkRecorder::onPopCull() {
275 void SkRecorder::didConcat(const SkMatrix& matrix) {
276 this->didSetMatrix(this->getTotalMatrix());
279 void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
280 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
282 this->getTotalMatrix().dump();
283 SkASSERT(matrix == this->getTotalMatrix());
285 APPEND(SetMatrix, matrix);
288 void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
289 INHERITED(onClipRect, rect, op, edgeStyle);
290 APPEND(ClipRect, this->devBounds(), rect, op, edgeStyle == kSoft_ClipEdgeStyle);
293 void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
294 INHERITED(onClipRRect, rrect, op, edgeStyle);
295 APPEND(ClipRRect, this->devBounds(), rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
298 void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
299 INHERITED(onClipPath, path, op, edgeStyle);
300 APPEND(ClipPath, this->devBounds(), delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
303 void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
304 INHERITED(onClipRegion, deviceRgn, op);
305 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
308 void SkRecorder::beginCommentGroup(const char* description) {
309 APPEND(BeginCommentGroup, this->copy(description));
312 void SkRecorder::addComment(const char* key, const char* value) {
313 APPEND(AddComment, this->copy(key), this->copy(value));
316 void SkRecorder::endCommentGroup() {
317 APPEND(EndCommentGroup);
320 bool SkRecorder::isDrawingToLayer() const {
321 return fSaveLayerCount > 0;
324 void SkRecorder::drawData(const void* data, size_t length) {
325 APPEND(DrawData, copy((const char*)data), length);