blob: 998fb6645babc16f3aa2b740c795a1ab90175271 [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
reed6be2aa92014-11-18 11:08:05 -08008#include "SkData.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00009#include "SkRecorder.h"
dandovb3c9d1c2014-08-12 08:34:29 -070010#include "SkPatchUtils.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000011#include "SkPicture.h"
12
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000013SkRecorder::SkRecorder(SkRecord* record, int width, int height)
reed78e27682014-11-19 08:04:34 -080014 : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
15 , fRecord(record)
16 , fSaveLayerCount(0) {}
17
18SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
19 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
mtklein29dfaa82014-09-04 14:12:44 -070020 , fRecord(record)
21 , fSaveLayerCount(0) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000022
reed6be2aa92014-11-18 11:08:05 -080023SkRecorder::~SkRecorder() {
24 fDrawableList.unrefAll();
25}
26
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000027void SkRecorder::forgetRecord() {
reed6be2aa92014-11-18 11:08:05 -080028 fDrawableList.unrefAll();
29 fDrawableList.reset();
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000030 fRecord = NULL;
31}
32
reed6be2aa92014-11-18 11:08:05 -080033// ReleaseProc for SkData, assuming the data was allocated via sk_malloc, and its contents are an
34// array of SkRefCnt* which need to be unref'd.
35//
36static void unref_all_malloc_releaseProc(const void* ptr, size_t length, void* context) {
37 SkASSERT(ptr == context); // our context is our ptr, allocated via sk_malloc
38 int count = SkToInt(length / sizeof(SkRefCnt*));
39 SkASSERT(count * sizeof(SkRefCnt*) == length); // our length is snug for the array
40
41 SkRefCnt* const* array = reinterpret_cast<SkRefCnt* const*>(ptr);
42 for (int i = 0; i < count; ++i) {
43 SkSafeUnref(array[i]);
44 }
45 sk_free(context);
46}
47
48// Return an uninitialized SkData sized for "count" SkRefCnt pointers. They will be unref'd when
49// the SkData is destroyed.
50//
51static SkData* new_uninitialized_refcnt_ptrs(int count) {
52 size_t length = count * sizeof(SkRefCnt*);
53 void* array = sk_malloc_throw(length);
54 void* context = array;
55 return SkData::NewWithProc(array, length, unref_all_malloc_releaseProc, context);
56}
57
58SkData* SkRecorder::newDrawableSnapshot(SkBBHFactory* factory, uint32_t recordFlags) {
59 const int count = fDrawableList.count();
60 if (0 == count) {
61 return NULL;
62 }
63 SkData* data = new_uninitialized_refcnt_ptrs(count);
64 SkPicture** pics = reinterpret_cast<SkPicture**>(data->writable_data());
65 for (int i = 0; i < count; ++i) {
66 pics[i] = fDrawableList[i]->newPictureSnapshot(factory, recordFlags);
67 }
68 return data;
69}
70
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000071// To make appending to fRecord a little less verbose.
72#define APPEND(T, ...) \
73 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
74
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000075// For methods which must call back into SkCanvas.
76#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000077
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000078// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
79// framework works, sometimes they happen to technically be copied twice, which is fine and elided
80// into a single copy unless the class has a non-trivial copy constructor. For classes with
81// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
82// with delay_copy(), forcing the argument to be passed by const&.
83//
84// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
85// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
86// unexpectedly on a profile of record time. Otherwise don't bother.
87template <typename T>
88class Reference {
89public:
90 Reference(const T& x) : fX(x) {}
91 operator const T&() const { return fX; }
92private:
93 const T& fX;
94};
95
96template <typename T>
97static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
98
99// Use copy() only for optional arguments, to be copied if present or skipped if not.
100// (For most types we just pass by value and let copy constructors do their thing.)
101template <typename T>
102T* SkRecorder::copy(const T* src) {
103 if (NULL == src) {
104 return NULL;
105 }
106 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
107}
108
109// This copy() is for arrays.
110// It will work with POD or non-POD, though currently we only use it for POD.
111template <typename T>
reed2347b622014-08-07 12:19:50 -0700112T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000113 if (NULL == src) {
114 return NULL;
115 }
116 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -0700117 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000118 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
119 }
120 return dst;
121}
122
123// Specialization for copying strings, using memcpy.
124// This measured around 2x faster for copying code points,
125// but I found no corresponding speedup for other arrays.
126template <>
reed2347b622014-08-07 12:19:50 -0700127char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000128 if (NULL == src) {
129 return NULL;
130 }
131 char* dst = fRecord->alloc<char>(count);
132 memcpy(dst, src, count);
133 return dst;
134}
135
mtklein5f0e8222014-08-22 11:44:26 -0700136// As above, assuming and copying a terminating \0.
137template <>
138char* SkRecorder::copy(const char* src) {
139 return this->copy(src, strlen(src)+1);
140}
141
142
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000143void SkRecorder::clear(SkColor color) {
144 APPEND(Clear, color);
145}
146
147void SkRecorder::drawPaint(const SkPaint& paint) {
148 APPEND(DrawPaint, delay_copy(paint));
149}
150
151void SkRecorder::drawPoints(PointMode mode,
152 size_t count,
153 const SkPoint pts[],
154 const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000155 APPEND(DrawPoints, delay_copy(paint), mode, count, this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156}
157
158void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000159 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000160}
161
162void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000163 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000164}
165
166void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000167 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000168}
169
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000170void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000171 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000172}
173
reed6be2aa92014-11-18 11:08:05 -0800174void SkRecorder::onDrawDrawable(SkCanvasDrawable* drawable) {
175 *fDrawableList.append() = SkRef(drawable);
176 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList.count() - 1);
177}
178
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000179void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000180 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000181}
182
183void SkRecorder::drawBitmap(const SkBitmap& bitmap,
184 SkScalar left,
185 SkScalar top,
186 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000187 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000188}
189
190void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
191 const SkRect* src,
192 const SkRect& dst,
193 const SkPaint* paint,
194 DrawBitmapRectFlags flags) {
195 APPEND(DrawBitmapRectToRect,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000196 this->copy(paint), delay_copy(bitmap), this->copy(src), dst, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000197}
198
199void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
200 const SkMatrix& matrix,
201 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000202 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000203}
204
205void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
206 const SkIRect& center,
207 const SkRect& dst,
208 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000209 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210}
211
piotaixr65151752014-10-16 11:58:39 -0700212void SkRecorder::drawImage(const SkImage* image, SkScalar left, SkScalar top,
213 const SkPaint* paint) {
214 APPEND(DrawImage, this->copy(paint), image, left, top);
215}
216
217void SkRecorder::drawImageRect(const SkImage* image, const SkRect* src,
218 const SkRect& dst,
219 const SkPaint* paint) {
220 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
221}
222
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000223void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000224 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000225}
226
reed@google.come0d9ce82014-04-23 04:00:17 +0000227void SkRecorder::onDrawText(const void* text, size_t byteLength,
228 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000229 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000230 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000231}
232
reed@google.come0d9ce82014-04-23 04:00:17 +0000233void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
234 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000235 const unsigned points = paint.countText(text, byteLength);
236 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000237 delay_copy(paint),
238 this->copy((const char*)text, byteLength),
239 byteLength,
240 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000241}
242
reed@google.come0d9ce82014-04-23 04:00:17 +0000243void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
244 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000245 const unsigned points = paint.countText(text, byteLength);
246 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000247 delay_copy(paint),
248 this->copy((const char*)text, byteLength),
249 byteLength,
250 this->copy(xpos, points),
251 constY);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000252}
253
reed@google.come0d9ce82014-04-23 04:00:17 +0000254void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
255 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000256 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000257 delay_copy(paint),
258 this->copy((const char*)text, byteLength),
259 byteLength,
260 delay_copy(path),
261 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000262}
263
fmalita00d5c2c2014-08-21 08:53:26 -0700264void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
265 const SkPaint& paint) {
266 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
267}
268
reedd5fa1a42014-08-09 11:08:05 -0700269void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
270 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000271}
272
273void SkRecorder::drawVertices(VertexMode vmode,
274 int vertexCount, const SkPoint vertices[],
275 const SkPoint texs[], const SkColor colors[],
276 SkXfermode* xmode,
277 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000278 APPEND(DrawVertices, delay_copy(paint),
279 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000280 vertexCount,
281 this->copy(vertices, vertexCount),
282 texs ? this->copy(texs, vertexCount) : NULL,
283 colors ? this->copy(colors, vertexCount) : NULL,
284 xmode,
285 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000286 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000287}
288
dandovb3c9d1c2014-08-12 08:34:29 -0700289void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
290 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
291 APPEND(DrawPatch, delay_copy(paint),
292 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
293 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
294 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
295 xmode);
dandov963137b2014-08-07 07:49:53 -0700296}
297
Florin Malita5f6102d2014-06-30 10:13:28 -0400298void SkRecorder::willSave() {
mtklein29dfaa82014-09-04 14:12:44 -0700299 fSaveIsSaveLayer.push(false);
Florin Malita5f6102d2014-06-30 10:13:28 -0400300 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000301}
302
303SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
304 const SkPaint* paint,
305 SkCanvas::SaveFlags flags) {
mtklein29dfaa82014-09-04 14:12:44 -0700306 fSaveLayerCount++;
307 fSaveIsSaveLayer.push(true);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000308 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
309 return SkCanvas::kNoLayer_SaveLayerStrategy;
310}
311
mtklein6cfa73a2014-08-13 13:33:49 -0700312void SkRecorder::didRestore() {
mtklein29dfaa82014-09-04 14:12:44 -0700313 SkBool8 saveLayer;
314 fSaveIsSaveLayer.pop(&saveLayer);
315 if (saveLayer) {
316 fSaveLayerCount--;
317 }
mtkleina723b572014-08-15 11:49:49 -0700318 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000319}
320
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000321void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000322 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000323}
324
325void SkRecorder::onPopCull() {
326 APPEND(PopCull);
327}
328
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000329void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700330 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000331}
332
333void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700334 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700335 matrix.dump();
336 this->getTotalMatrix().dump();
337 SkASSERT(matrix == this->getTotalMatrix());
338 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000339 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000340}
341
342void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000343 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700344 APPEND(ClipRect, this->devBounds(), rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000345}
346
347void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700348 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700349 APPEND(ClipRRect, this->devBounds(), rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000350}
351
352void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700353 INHERITED(onClipPath, path, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700354 APPEND(ClipPath, this->devBounds(), delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000355}
356
357void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000358 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700359 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000360}
mtklein5f0e8222014-08-22 11:44:26 -0700361
362void SkRecorder::beginCommentGroup(const char* description) {
363 APPEND(BeginCommentGroup, this->copy(description));
364}
365
366void SkRecorder::addComment(const char* key, const char* value) {
367 APPEND(AddComment, this->copy(key), this->copy(value));
368}
369
370void SkRecorder::endCommentGroup() {
371 APPEND(EndCommentGroup);
372}
mtklein29dfaa82014-09-04 14:12:44 -0700373
374bool SkRecorder::isDrawingToLayer() const {
375 return fSaveLayerCount > 0;
376}
377
378void SkRecorder::drawData(const void* data, size_t length) {
379 APPEND(DrawData, copy((const char*)data), length);
380}