blob: 8684a8e2ebf7cb516ff8c8b9f8aa975aedd019e8 [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
reed3cb38402015-02-06 08:36:15 -080012SkDrawableList::~SkDrawableList() {
reed1bdfd3f2014-11-24 14:41:51 -080013 fArray.unrefAll();
14}
15
reed3cb38402015-02-06 08:36:15 -080016SkPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
reed1bdfd3f2014-11-24 14:41:51 -080017 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
reed3cb38402015-02-06 08:36:15 -080028void SkDrawableList::append(SkDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -080029 *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
mtkleinfeaadee2015-04-08 11:25:48 -070042void SkRecorder::reset(SkRecord* record, const SkRect& bounds) {
43 this->forgetRecord();
44 fRecord = record;
45 this->resetForNextPicture(bounds.roundOut());
46}
47
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000048void SkRecorder::forgetRecord() {
reed1bdfd3f2014-11-24 14:41:51 -080049 fDrawableList.reset(NULL);
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000050 fRecord = NULL;
51}
52
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000053// To make appending to fRecord a little less verbose.
54#define APPEND(T, ...) \
55 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
56
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000057// For methods which must call back into SkCanvas.
58#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000059
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000060// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
61// framework works, sometimes they happen to technically be copied twice, which is fine and elided
62// into a single copy unless the class has a non-trivial copy constructor. For classes with
63// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
64// with delay_copy(), forcing the argument to be passed by const&.
65//
66// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
67// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
68// unexpectedly on a profile of record time. Otherwise don't bother.
69template <typename T>
70class Reference {
71public:
72 Reference(const T& x) : fX(x) {}
73 operator const T&() const { return fX; }
74private:
75 const T& fX;
76};
77
78template <typename T>
79static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
80
81// Use copy() only for optional arguments, to be copied if present or skipped if not.
82// (For most types we just pass by value and let copy constructors do their thing.)
83template <typename T>
84T* SkRecorder::copy(const T* src) {
85 if (NULL == src) {
86 return NULL;
87 }
88 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
89}
90
91// This copy() is for arrays.
92// It will work with POD or non-POD, though currently we only use it for POD.
93template <typename T>
reed2347b622014-08-07 12:19:50 -070094T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000095 if (NULL == src) {
96 return NULL;
97 }
98 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070099 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000100 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
101 }
102 return dst;
103}
104
105// Specialization for copying strings, using memcpy.
106// This measured around 2x faster for copying code points,
107// but I found no corresponding speedup for other arrays.
108template <>
reed2347b622014-08-07 12:19:50 -0700109char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000110 if (NULL == src) {
111 return NULL;
112 }
113 char* dst = fRecord->alloc<char>(count);
114 memcpy(dst, src, count);
115 return dst;
116}
117
mtklein5f0e8222014-08-22 11:44:26 -0700118// As above, assuming and copying a terminating \0.
119template <>
120char* SkRecorder::copy(const char* src) {
121 return this->copy(src, strlen(src)+1);
122}
123
124
reed41af9662015-01-05 07:49:08 -0800125void SkRecorder::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000126 APPEND(DrawPaint, delay_copy(paint));
127}
128
reed41af9662015-01-05 07:49:08 -0800129void SkRecorder::onDrawPoints(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
reed41af9662015-01-05 07:49:08 -0800136void SkRecorder::onDrawRect(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
reed41af9662015-01-05 07:49:08 -0800140void SkRecorder::onDrawOval(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
reed41af9662015-01-05 07:49:08 -0800144void SkRecorder::onDrawRRect(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
reed3cb38402015-02-06 08:36:15 -0800152void SkRecorder::onDrawDrawable(SkDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -0800153 if (!fDrawableList) {
reed3cb38402015-02-06 08:36:15 -0800154 fDrawableList.reset(SkNEW(SkDrawableList));
reed1bdfd3f2014-11-24 14:41:51 -0800155 }
156 fDrawableList->append(drawable);
157 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList->count() - 1);
reed6be2aa92014-11-18 11:08:05 -0800158}
159
reed41af9662015-01-05 07:49:08 -0800160void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
mtkleinaf579032014-12-01 11:03:37 -0800161 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000162}
163
reed41af9662015-01-05 07:49:08 -0800164void SkRecorder::onDrawBitmap(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
reed41af9662015-01-05 07:49:08 -0800171void SkRecorder::onDrawBitmapRect(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
reed41af9662015-01-05 07:49:08 -0800186void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
187 const SkIRect& center,
188 const SkRect& dst,
189 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000190 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000191}
192
reed41af9662015-01-05 07:49:08 -0800193void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
194 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700195 APPEND(DrawImage, this->copy(paint), image, left, top);
196}
197
reed41af9662015-01-05 07:49:08 -0800198void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src,
199 const SkRect& dst,
200 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700201 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
202}
203
reed41af9662015-01-05 07:49:08 -0800204void SkRecorder::onDrawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000205 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206}
207
reed@google.come0d9ce82014-04-23 04:00:17 +0000208void SkRecorder::onDrawText(const void* text, size_t byteLength,
209 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000211 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000212}
213
reed@google.come0d9ce82014-04-23 04:00:17 +0000214void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
215 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216 const unsigned points = paint.countText(text, byteLength);
217 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000218 delay_copy(paint),
219 this->copy((const char*)text, byteLength),
220 byteLength,
221 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000222}
223
reed@google.come0d9ce82014-04-23 04:00:17 +0000224void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
225 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000226 const unsigned points = paint.countText(text, byteLength);
227 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000228 delay_copy(paint),
229 this->copy((const char*)text, byteLength),
mtklein42ddcd42014-11-21 08:48:35 -0800230 SkToUInt(byteLength),
231 constY,
232 this->copy(xpos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000233}
234
reed@google.come0d9ce82014-04-23 04:00:17 +0000235void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
236 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000238 delay_copy(paint),
239 this->copy((const char*)text, byteLength),
240 byteLength,
mtkleinaf579032014-12-01 11:03:37 -0800241 delay_copy(path),
242 matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000243}
244
fmalita00d5c2c2014-08-21 08:53:26 -0700245void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
246 const SkPaint& paint) {
247 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
248}
249
reedd5fa1a42014-08-09 11:08:05 -0700250void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
mtkleinaf579032014-12-01 11:03:37 -0800251 APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000252}
253
reed41af9662015-01-05 07:49:08 -0800254void SkRecorder::onDrawVertices(VertexMode vmode,
255 int vertexCount, const SkPoint vertices[],
256 const SkPoint texs[], const SkColor colors[],
257 SkXfermode* xmode,
258 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000259 APPEND(DrawVertices, delay_copy(paint),
260 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000261 vertexCount,
262 this->copy(vertices, vertexCount),
263 texs ? this->copy(texs, vertexCount) : NULL,
264 colors ? this->copy(colors, vertexCount) : NULL,
265 xmode,
266 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000267 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000268}
269
dandovb3c9d1c2014-08-12 08:34:29 -0700270void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
271 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
272 APPEND(DrawPatch, delay_copy(paint),
273 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
274 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
275 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
276 xmode);
dandov963137b2014-08-07 07:49:53 -0700277}
278
Florin Malita5f6102d2014-06-30 10:13:28 -0400279void SkRecorder::willSave() {
280 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000281}
282
283SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
284 const SkPaint* paint,
285 SkCanvas::SaveFlags flags) {
286 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
287 return SkCanvas::kNoLayer_SaveLayerStrategy;
288}
289
mtklein6cfa73a2014-08-13 13:33:49 -0700290void SkRecorder::didRestore() {
mtkleina723b572014-08-15 11:49:49 -0700291 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000292}
293
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000294void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700295 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000296}
297
298void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700299 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700300 matrix.dump();
301 this->getTotalMatrix().dump();
302 SkASSERT(matrix == this->getTotalMatrix());
303 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000304 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000305}
306
307void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000308 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800309 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
310 APPEND(ClipRect, this->devBounds(), rect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000311}
312
313void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700314 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800315 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
316 APPEND(ClipRRect, this->devBounds(), rrect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000317}
318
319void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700320 INHERITED(onClipPath, path, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800321 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
mtkleinaf579032014-12-01 11:03:37 -0800322 APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000323}
324
325void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000326 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700327 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000328}
mtklein5f0e8222014-08-22 11:44:26 -0700329
330void SkRecorder::beginCommentGroup(const char* description) {
331 APPEND(BeginCommentGroup, this->copy(description));
332}
333
334void SkRecorder::addComment(const char* key, const char* value) {
335 APPEND(AddComment, this->copy(key), this->copy(value));
336}
337
338void SkRecorder::endCommentGroup() {
339 APPEND(EndCommentGroup);
340}
mtklein29dfaa82014-09-04 14:12:44 -0700341