blob: 3252e59f62d969f9fecccc714d7887fcd0fca66a [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
mtklein08d1fcc2014-11-20 09:18:31 -080034// array of SkPicture* which need to be unref'd.
reed6be2aa92014-11-18 11:08:05 -080035//
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
mtklein08d1fcc2014-11-20 09:18:31 -080038 int count = SkToInt(length / sizeof(SkPicture*));
39 SkASSERT(count * sizeof(SkPicture*) == length); // our length is snug for the array
reed6be2aa92014-11-18 11:08:05 -080040
mtklein08d1fcc2014-11-20 09:18:31 -080041 SkPicture* const* array = reinterpret_cast<SkPicture* const*>(ptr);
reed6be2aa92014-11-18 11:08:05 -080042 for (int i = 0; i < count; ++i) {
43 SkSafeUnref(array[i]);
44 }
45 sk_free(context);
46}
47
mtklein08d1fcc2014-11-20 09:18:31 -080048// Return an uninitialized SkData sized for "count" SkPicture pointers. They will be unref'd when
reed6be2aa92014-11-18 11:08:05 -080049// the SkData is destroyed.
50//
mtklein08d1fcc2014-11-20 09:18:31 -080051static SkData* new_uninitialized_picture_ptrs(int count) {
52 size_t length = count * sizeof(SkPicture*);
reed6be2aa92014-11-18 11:08:05 -080053 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 }
mtklein08d1fcc2014-11-20 09:18:31 -080063 SkData* data = new_uninitialized_picture_ptrs(count);
reed6be2aa92014-11-18 11:08:05 -080064 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) {
mtklein42ddcd42014-11-21 08:48:35 -0800155 APPEND(DrawPoints, delay_copy(paint), mode, SkToUInt(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) {
mtklein42ddcd42014-11-21 08:48:35 -0800195 if (kBleed_DrawBitmapRectFlag == flags) {
196 APPEND(DrawBitmapRectToRectBleed,
197 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
198 return;
199 }
200 SkASSERT(kNone_DrawBitmapRectFlag == flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000201 APPEND(DrawBitmapRectToRect,
mtklein42ddcd42014-11-21 08:48:35 -0800202 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000203}
204
205void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
206 const SkMatrix& matrix,
207 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000208 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000209}
210
211void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
212 const SkIRect& center,
213 const SkRect& dst,
214 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000215 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216}
217
piotaixr65151752014-10-16 11:58:39 -0700218void SkRecorder::drawImage(const SkImage* image, SkScalar left, SkScalar top,
219 const SkPaint* paint) {
220 APPEND(DrawImage, this->copy(paint), image, left, top);
221}
222
223void SkRecorder::drawImageRect(const SkImage* image, const SkRect* src,
224 const SkRect& dst,
225 const SkPaint* paint) {
226 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
227}
228
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000229void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000230 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000231}
232
reed@google.come0d9ce82014-04-23 04:00:17 +0000233void SkRecorder::onDrawText(const void* text, size_t byteLength,
234 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000235 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000236 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237}
238
reed@google.come0d9ce82014-04-23 04:00:17 +0000239void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
240 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000241 const unsigned points = paint.countText(text, byteLength);
242 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000243 delay_copy(paint),
244 this->copy((const char*)text, byteLength),
245 byteLength,
246 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000247}
248
reed@google.come0d9ce82014-04-23 04:00:17 +0000249void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
250 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000251 const unsigned points = paint.countText(text, byteLength);
252 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000253 delay_copy(paint),
254 this->copy((const char*)text, byteLength),
mtklein42ddcd42014-11-21 08:48:35 -0800255 SkToUInt(byteLength),
256 constY,
257 this->copy(xpos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000258}
259
reed@google.come0d9ce82014-04-23 04:00:17 +0000260void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
261 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000262 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000263 delay_copy(paint),
264 this->copy((const char*)text, byteLength),
265 byteLength,
266 delay_copy(path),
267 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000268}
269
fmalita00d5c2c2014-08-21 08:53:26 -0700270void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
271 const SkPaint& paint) {
272 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
273}
274
reedd5fa1a42014-08-09 11:08:05 -0700275void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
276 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000277}
278
279void SkRecorder::drawVertices(VertexMode vmode,
280 int vertexCount, const SkPoint vertices[],
281 const SkPoint texs[], const SkColor colors[],
282 SkXfermode* xmode,
283 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000284 APPEND(DrawVertices, delay_copy(paint),
285 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000286 vertexCount,
287 this->copy(vertices, vertexCount),
288 texs ? this->copy(texs, vertexCount) : NULL,
289 colors ? this->copy(colors, vertexCount) : NULL,
290 xmode,
291 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000292 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000293}
294
dandovb3c9d1c2014-08-12 08:34:29 -0700295void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
296 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
297 APPEND(DrawPatch, delay_copy(paint),
298 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
299 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
300 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
301 xmode);
dandov963137b2014-08-07 07:49:53 -0700302}
303
Florin Malita5f6102d2014-06-30 10:13:28 -0400304void SkRecorder::willSave() {
mtklein29dfaa82014-09-04 14:12:44 -0700305 fSaveIsSaveLayer.push(false);
Florin Malita5f6102d2014-06-30 10:13:28 -0400306 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000307}
308
309SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
310 const SkPaint* paint,
311 SkCanvas::SaveFlags flags) {
mtklein29dfaa82014-09-04 14:12:44 -0700312 fSaveLayerCount++;
313 fSaveIsSaveLayer.push(true);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000314 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
315 return SkCanvas::kNoLayer_SaveLayerStrategy;
316}
317
mtklein6cfa73a2014-08-13 13:33:49 -0700318void SkRecorder::didRestore() {
mtklein29dfaa82014-09-04 14:12:44 -0700319 SkBool8 saveLayer;
320 fSaveIsSaveLayer.pop(&saveLayer);
321 if (saveLayer) {
322 fSaveLayerCount--;
323 }
mtkleina723b572014-08-15 11:49:49 -0700324 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000325}
326
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000327void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000328 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000329}
330
331void SkRecorder::onPopCull() {
332 APPEND(PopCull);
333}
334
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000335void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700336 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000337}
338
339void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700340 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700341 matrix.dump();
342 this->getTotalMatrix().dump();
343 SkASSERT(matrix == this->getTotalMatrix());
344 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000345 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000346}
347
348void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000349 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800350 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
351 APPEND(ClipRect, this->devBounds(), rect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000352}
353
354void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700355 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800356 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
357 APPEND(ClipRRect, this->devBounds(), rrect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000358}
359
360void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700361 INHERITED(onClipPath, path, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800362 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
363 APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000364}
365
366void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000367 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700368 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000369}
mtklein5f0e8222014-08-22 11:44:26 -0700370
371void SkRecorder::beginCommentGroup(const char* description) {
372 APPEND(BeginCommentGroup, this->copy(description));
373}
374
375void SkRecorder::addComment(const char* key, const char* value) {
376 APPEND(AddComment, this->copy(key), this->copy(value));
377}
378
379void SkRecorder::endCommentGroup() {
380 APPEND(EndCommentGroup);
381}
mtklein29dfaa82014-09-04 14:12:44 -0700382
383bool SkRecorder::isDrawingToLayer() const {
384 return fSaveLayerCount > 0;
385}
386
387void SkRecorder::drawData(const void* data, size_t length) {
388 APPEND(DrawData, copy((const char*)data), length);
389}