blob: 3548851a72ca6d5aa121189aa122dd252229c667 [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)
36 , fRecord(record)
37 , fSaveLayerCount(0) {}
38
39SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
40 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
mtklein29dfaa82014-09-04 14:12:44 -070041 , fRecord(record)
42 , fSaveLayerCount(0) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000043
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000044void SkRecorder::forgetRecord() {
reed1bdfd3f2014-11-24 14:41:51 -080045 fDrawableList.reset(NULL);
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000046 fRecord = NULL;
47}
48
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000049// 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
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000053// For methods which must call back into SkCanvas.
54#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000055
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000056// 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>
reed2347b622014-08-07 12:19:50 -070090T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000091 if (NULL == src) {
92 return NULL;
93 }
94 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070095 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000096 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 <>
reed2347b622014-08-07 12:19:50 -0700105char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000106 if (NULL == src) {
107 return NULL;
108 }
109 char* dst = fRecord->alloc<char>(count);
110 memcpy(dst, src, count);
111 return dst;
112}
113
mtklein5f0e8222014-08-22 11:44:26 -0700114// 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
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000121void SkRecorder::clear(SkColor color) {
122 APPEND(Clear, color);
123}
124
125void SkRecorder::drawPaint(const SkPaint& paint) {
126 APPEND(DrawPaint, delay_copy(paint));
127}
128
129void SkRecorder::drawPoints(PointMode mode,
130 size_t count,
131 const SkPoint pts[],
132 const SkPaint& paint) {
mtklein42ddcd42014-11-21 08:48:35 -0800133 APPEND(DrawPoints, delay_copy(paint), mode, SkToUInt(count), this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000134}
135
136void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000137 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000138}
139
140void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000141 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000142}
143
144void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000145 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000146}
147
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000148void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000149 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000150}
151
reed6be2aa92014-11-18 11:08:05 -0800152void SkRecorder::onDrawDrawable(SkCanvasDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -0800153 if (!fDrawableList) {
154 fDrawableList.reset(SkNEW(SkCanvasDrawableList));
155 }
156 fDrawableList->append(drawable);
157 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList->count() - 1);
reed6be2aa92014-11-18 11:08:05 -0800158}
159
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000160void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000161 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000162}
163
164void SkRecorder::drawBitmap(const SkBitmap& bitmap,
165 SkScalar left,
166 SkScalar top,
167 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000168 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000169}
170
171void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
172 const SkRect* src,
173 const SkRect& dst,
174 const SkPaint* paint,
175 DrawBitmapRectFlags flags) {
mtklein42ddcd42014-11-21 08:48:35 -0800176 if (kBleed_DrawBitmapRectFlag == flags) {
177 APPEND(DrawBitmapRectToRectBleed,
178 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
179 return;
180 }
181 SkASSERT(kNone_DrawBitmapRectFlag == flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000182 APPEND(DrawBitmapRectToRect,
mtklein42ddcd42014-11-21 08:48:35 -0800183 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000184}
185
186void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
187 const SkMatrix& matrix,
188 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000189 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000190}
191
192void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
193 const SkIRect& center,
194 const SkRect& dst,
195 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000196 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000197}
198
piotaixr65151752014-10-16 11:58:39 -0700199void SkRecorder::drawImage(const SkImage* image, SkScalar left, SkScalar top,
200 const SkPaint* paint) {
201 APPEND(DrawImage, this->copy(paint), image, left, top);
202}
203
204void SkRecorder::drawImageRect(const SkImage* image, const SkRect* src,
205 const SkRect& dst,
206 const SkPaint* paint) {
207 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
208}
209
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000211 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000212}
213
reed@google.come0d9ce82014-04-23 04:00:17 +0000214void SkRecorder::onDrawText(const void* text, size_t byteLength,
215 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000217 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000218}
219
reed@google.come0d9ce82014-04-23 04:00:17 +0000220void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
221 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000222 const unsigned points = paint.countText(text, byteLength);
223 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000224 delay_copy(paint),
225 this->copy((const char*)text, byteLength),
226 byteLength,
227 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000228}
229
reed@google.come0d9ce82014-04-23 04:00:17 +0000230void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
231 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000232 const unsigned points = paint.countText(text, byteLength);
233 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000234 delay_copy(paint),
235 this->copy((const char*)text, byteLength),
mtklein42ddcd42014-11-21 08:48:35 -0800236 SkToUInt(byteLength),
237 constY,
238 this->copy(xpos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000239}
240
reed@google.come0d9ce82014-04-23 04:00:17 +0000241void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
242 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000243 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000244 delay_copy(paint),
245 this->copy((const char*)text, byteLength),
246 byteLength,
247 delay_copy(path),
248 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000249}
250
fmalita00d5c2c2014-08-21 08:53:26 -0700251void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
252 const SkPaint& paint) {
253 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
254}
255
reedd5fa1a42014-08-09 11:08:05 -0700256void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
257 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000258}
259
260void SkRecorder::drawVertices(VertexMode vmode,
261 int vertexCount, const SkPoint vertices[],
262 const SkPoint texs[], const SkColor colors[],
263 SkXfermode* xmode,
264 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000265 APPEND(DrawVertices, delay_copy(paint),
266 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000267 vertexCount,
268 this->copy(vertices, vertexCount),
269 texs ? this->copy(texs, vertexCount) : NULL,
270 colors ? this->copy(colors, vertexCount) : NULL,
271 xmode,
272 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000273 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000274}
275
dandovb3c9d1c2014-08-12 08:34:29 -0700276void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
277 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
278 APPEND(DrawPatch, delay_copy(paint),
279 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
280 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
281 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
282 xmode);
dandov963137b2014-08-07 07:49:53 -0700283}
284
Florin Malita5f6102d2014-06-30 10:13:28 -0400285void SkRecorder::willSave() {
mtklein29dfaa82014-09-04 14:12:44 -0700286 fSaveIsSaveLayer.push(false);
Florin Malita5f6102d2014-06-30 10:13:28 -0400287 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000288}
289
290SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
291 const SkPaint* paint,
292 SkCanvas::SaveFlags flags) {
mtklein29dfaa82014-09-04 14:12:44 -0700293 fSaveLayerCount++;
294 fSaveIsSaveLayer.push(true);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000295 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
296 return SkCanvas::kNoLayer_SaveLayerStrategy;
297}
298
mtklein6cfa73a2014-08-13 13:33:49 -0700299void SkRecorder::didRestore() {
mtklein29dfaa82014-09-04 14:12:44 -0700300 SkBool8 saveLayer;
301 fSaveIsSaveLayer.pop(&saveLayer);
302 if (saveLayer) {
303 fSaveLayerCount--;
304 }
mtkleina723b572014-08-15 11:49:49 -0700305 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000306}
307
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000308void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000309 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000310}
311
312void SkRecorder::onPopCull() {
313 APPEND(PopCull);
314}
315
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000316void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700317 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000318}
319
320void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700321 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700322 matrix.dump();
323 this->getTotalMatrix().dump();
324 SkASSERT(matrix == this->getTotalMatrix());
325 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000326 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000327}
328
329void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000330 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800331 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
332 APPEND(ClipRect, this->devBounds(), rect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000333}
334
335void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700336 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800337 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
338 APPEND(ClipRRect, this->devBounds(), rrect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000339}
340
341void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700342 INHERITED(onClipPath, path, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800343 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
344 APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000345}
346
347void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000348 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700349 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000350}
mtklein5f0e8222014-08-22 11:44:26 -0700351
352void SkRecorder::beginCommentGroup(const char* description) {
353 APPEND(BeginCommentGroup, this->copy(description));
354}
355
356void SkRecorder::addComment(const char* key, const char* value) {
357 APPEND(AddComment, this->copy(key), this->copy(value));
358}
359
360void SkRecorder::endCommentGroup() {
361 APPEND(EndCommentGroup);
362}
mtklein29dfaa82014-09-04 14:12:44 -0700363
364bool SkRecorder::isDrawingToLayer() const {
365 return fSaveLayerCount > 0;
366}
367
368void SkRecorder::drawData(const void* data, size_t length) {
369 APPEND(DrawData, copy((const char*)data), length);
370}