blob: de16d6244f01e2f478a21ea15977a3824ccffe66 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00008#include "SkRecorder.h"
dandovb3c9d1c2014-08-12 08:34:29 -07009#include "SkPatchUtils.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000010#include "SkPicture.h"
11
reed1bdfd3f2014-11-24 14:41:51 -080012SkCanvasDrawableList::~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
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000034SkRecorder::SkRecorder(SkRecord* record, int width, int height)
reed78e27682014-11-19 08:04:34 -080035 : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
junov3fcc1252014-12-15 11:34:06 -080036 , fRecord(record) {}
reed78e27682014-11-19 08:04:34 -080037
38SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
39 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
junov3fcc1252014-12-15 11:34:06 -080040 , fRecord(record) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000041
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000042void SkRecorder::forgetRecord() {
reed1bdfd3f2014-11-24 14:41:51 -080043 fDrawableList.reset(NULL);
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000044 fRecord = NULL;
45}
46
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000047// To make appending to fRecord a little less verbose.
48#define APPEND(T, ...) \
49 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
50
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000051// For methods which must call back into SkCanvas.
52#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000053
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000054// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
55// framework works, sometimes they happen to technically be copied twice, which is fine and elided
56// into a single copy unless the class has a non-trivial copy constructor. For classes with
57// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
58// with delay_copy(), forcing the argument to be passed by const&.
59//
60// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
61// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
62// unexpectedly on a profile of record time. Otherwise don't bother.
63template <typename T>
64class Reference {
65public:
66 Reference(const T& x) : fX(x) {}
67 operator const T&() const { return fX; }
68private:
69 const T& fX;
70};
71
72template <typename T>
73static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
74
75// Use copy() only for optional arguments, to be copied if present or skipped if not.
76// (For most types we just pass by value and let copy constructors do their thing.)
77template <typename T>
78T* SkRecorder::copy(const T* src) {
79 if (NULL == src) {
80 return NULL;
81 }
82 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
83}
84
85// This copy() is for arrays.
86// It will work with POD or non-POD, though currently we only use it for POD.
87template <typename T>
reed2347b622014-08-07 12:19:50 -070088T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000089 if (NULL == src) {
90 return NULL;
91 }
92 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070093 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000094 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
95 }
96 return dst;
97}
98
99// Specialization for copying strings, using memcpy.
100// This measured around 2x faster for copying code points,
101// but I found no corresponding speedup for other arrays.
102template <>
reed2347b622014-08-07 12:19:50 -0700103char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000104 if (NULL == src) {
105 return NULL;
106 }
107 char* dst = fRecord->alloc<char>(count);
108 memcpy(dst, src, count);
109 return dst;
110}
111
mtklein5f0e8222014-08-22 11:44:26 -0700112// As above, assuming and copying a terminating \0.
113template <>
114char* SkRecorder::copy(const char* src) {
115 return this->copy(src, strlen(src)+1);
116}
117
118
reed41af9662015-01-05 07:49:08 -0800119void SkRecorder::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000120 APPEND(DrawPaint, delay_copy(paint));
121}
122
reed41af9662015-01-05 07:49:08 -0800123void SkRecorder::onDrawPoints(PointMode mode,
124 size_t count,
125 const SkPoint pts[],
126 const SkPaint& paint) {
mtklein42ddcd42014-11-21 08:48:35 -0800127 APPEND(DrawPoints, delay_copy(paint), mode, SkToUInt(count), this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000128}
129
reed41af9662015-01-05 07:49:08 -0800130void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000131 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000132}
133
reed41af9662015-01-05 07:49:08 -0800134void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000135 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000136}
137
reed41af9662015-01-05 07:49:08 -0800138void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000139 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000140}
141
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000142void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000143 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000144}
145
reed6be2aa92014-11-18 11:08:05 -0800146void SkRecorder::onDrawDrawable(SkCanvasDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -0800147 if (!fDrawableList) {
148 fDrawableList.reset(SkNEW(SkCanvasDrawableList));
149 }
150 fDrawableList->append(drawable);
151 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList->count() - 1);
reed6be2aa92014-11-18 11:08:05 -0800152}
153
reed41af9662015-01-05 07:49:08 -0800154void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
mtkleinaf579032014-12-01 11:03:37 -0800155 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156}
157
reed41af9662015-01-05 07:49:08 -0800158void SkRecorder::onDrawBitmap(const SkBitmap& bitmap,
159 SkScalar left,
160 SkScalar top,
161 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000162 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000163}
164
reed41af9662015-01-05 07:49:08 -0800165void SkRecorder::onDrawBitmapRect(const SkBitmap& bitmap,
166 const SkRect* src,
167 const SkRect& dst,
168 const SkPaint* paint,
169 DrawBitmapRectFlags flags) {
mtklein42ddcd42014-11-21 08:48:35 -0800170 if (kBleed_DrawBitmapRectFlag == flags) {
171 APPEND(DrawBitmapRectToRectBleed,
172 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
173 return;
174 }
175 SkASSERT(kNone_DrawBitmapRectFlag == flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000176 APPEND(DrawBitmapRectToRect,
mtklein42ddcd42014-11-21 08:48:35 -0800177 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000178}
179
reed41af9662015-01-05 07:49:08 -0800180void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
181 const SkIRect& center,
182 const SkRect& dst,
183 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000184 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000185}
186
reed41af9662015-01-05 07:49:08 -0800187void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
188 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700189 APPEND(DrawImage, this->copy(paint), image, left, top);
190}
191
reed41af9662015-01-05 07:49:08 -0800192void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src,
193 const SkRect& dst,
194 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700195 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
196}
197
reed41af9662015-01-05 07:49:08 -0800198void SkRecorder::onDrawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000199 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000200}
201
reed@google.come0d9ce82014-04-23 04:00:17 +0000202void SkRecorder::onDrawText(const void* text, size_t byteLength,
203 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000204 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000205 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206}
207
reed@google.come0d9ce82014-04-23 04:00:17 +0000208void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
209 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210 const unsigned points = paint.countText(text, byteLength);
211 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000212 delay_copy(paint),
213 this->copy((const char*)text, byteLength),
214 byteLength,
215 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216}
217
reed@google.come0d9ce82014-04-23 04:00:17 +0000218void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
219 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000220 const unsigned points = paint.countText(text, byteLength);
221 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000222 delay_copy(paint),
223 this->copy((const char*)text, byteLength),
mtklein42ddcd42014-11-21 08:48:35 -0800224 SkToUInt(byteLength),
225 constY,
226 this->copy(xpos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000227}
228
reed@google.come0d9ce82014-04-23 04:00:17 +0000229void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
230 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000231 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000232 delay_copy(paint),
233 this->copy((const char*)text, byteLength),
234 byteLength,
mtkleinaf579032014-12-01 11:03:37 -0800235 delay_copy(path),
236 matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237}
238
fmalita00d5c2c2014-08-21 08:53:26 -0700239void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
240 const SkPaint& paint) {
241 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
242}
243
reedd5fa1a42014-08-09 11:08:05 -0700244void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
mtkleinaf579032014-12-01 11:03:37 -0800245 APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000246}
247
reed41af9662015-01-05 07:49:08 -0800248void SkRecorder::onDrawVertices(VertexMode vmode,
249 int vertexCount, const SkPoint vertices[],
250 const SkPoint texs[], const SkColor colors[],
251 SkXfermode* xmode,
252 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000253 APPEND(DrawVertices, delay_copy(paint),
254 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000255 vertexCount,
256 this->copy(vertices, vertexCount),
257 texs ? this->copy(texs, vertexCount) : NULL,
258 colors ? this->copy(colors, vertexCount) : NULL,
259 xmode,
260 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000261 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000262}
263
dandovb3c9d1c2014-08-12 08:34:29 -0700264void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
265 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
266 APPEND(DrawPatch, delay_copy(paint),
267 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
268 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
269 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
270 xmode);
dandov963137b2014-08-07 07:49:53 -0700271}
272
Florin Malita5f6102d2014-06-30 10:13:28 -0400273void SkRecorder::willSave() {
274 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000275}
276
277SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
278 const SkPaint* paint,
279 SkCanvas::SaveFlags flags) {
280 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
281 return SkCanvas::kNoLayer_SaveLayerStrategy;
282}
283
mtklein6cfa73a2014-08-13 13:33:49 -0700284void SkRecorder::didRestore() {
mtkleina723b572014-08-15 11:49:49 -0700285 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000286}
287
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000288void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700289 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000290}
291
292void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700293 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700294 matrix.dump();
295 this->getTotalMatrix().dump();
296 SkASSERT(matrix == this->getTotalMatrix());
297 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000298 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000299}
300
301void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000302 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800303 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
304 APPEND(ClipRect, this->devBounds(), rect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000305}
306
307void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700308 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800309 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
310 APPEND(ClipRRect, this->devBounds(), rrect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000311}
312
313void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700314 INHERITED(onClipPath, path, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800315 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
mtkleinaf579032014-12-01 11:03:37 -0800316 APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000317}
318
319void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000320 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700321 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000322}
mtklein5f0e8222014-08-22 11:44:26 -0700323
324void SkRecorder::beginCommentGroup(const char* description) {
325 APPEND(BeginCommentGroup, this->copy(description));
326}
327
328void SkRecorder::addComment(const char* key, const char* value) {
329 APPEND(AddComment, this->copy(key), this->copy(value));
330}
331
332void SkRecorder::endCommentGroup() {
333 APPEND(EndCommentGroup);
334}
mtklein29dfaa82014-09-04 14:12:44 -0700335