blob: 0a2d43edbf22c74956bdd17b83c70ec40b686e8c [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
dandovb3c9d1c2014-08-12 08:34:29 -07008#include "SkPatchUtils.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00009#include "SkPicture.h"
mtklein98b84852015-04-21 15:23:59 -070010#include "SkPictureUtils.h"
11#include "SkRecorder.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000012
reed3cb38402015-02-06 08:36:15 -080013SkDrawableList::~SkDrawableList() {
reed1bdfd3f2014-11-24 14:41:51 -080014 fArray.unrefAll();
15}
16
reed3cb38402015-02-06 08:36:15 -080017SkPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() {
reed1bdfd3f2014-11-24 14:41:51 -080018 const int count = fArray.count();
19 if (0 == count) {
20 return NULL;
21 }
22 SkAutoTMalloc<const SkPicture*> pics(count);
23 for (int i = 0; i < count; ++i) {
24 pics[i] = fArray[i]->newPictureSnapshot();
25 }
26 return SkNEW_ARGS(SkPicture::SnapshotArray, (pics.detach(), count));
27}
28
reed3cb38402015-02-06 08:36:15 -080029void SkDrawableList::append(SkDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -080030 *fArray.append() = SkRef(drawable);
31}
32
33///////////////////////////////////////////////////////////////////////////////////////////////
34
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000035SkRecorder::SkRecorder(SkRecord* record, int width, int height)
reed78e27682014-11-19 08:04:34 -080036 : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
mtklein98b84852015-04-21 15:23:59 -070037 , fApproxBytesUsedBySubPictures(0)
junov3fcc1252014-12-15 11:34:06 -080038 , fRecord(record) {}
reed78e27682014-11-19 08:04:34 -080039
40SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
41 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
mtklein98b84852015-04-21 15:23:59 -070042 , fApproxBytesUsedBySubPictures(0)
junov3fcc1252014-12-15 11:34:06 -080043 , fRecord(record) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000044
mtkleinfeaadee2015-04-08 11:25:48 -070045void SkRecorder::reset(SkRecord* record, const SkRect& bounds) {
46 this->forgetRecord();
47 fRecord = record;
48 this->resetForNextPicture(bounds.roundOut());
49}
50
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000051void SkRecorder::forgetRecord() {
reed1bdfd3f2014-11-24 14:41:51 -080052 fDrawableList.reset(NULL);
mtklein98b84852015-04-21 15:23:59 -070053 fApproxBytesUsedBySubPictures = 0;
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000054 fRecord = NULL;
55}
56
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000057// To make appending to fRecord a little less verbose.
58#define APPEND(T, ...) \
59 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
60
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000061// For methods which must call back into SkCanvas.
62#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000063
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000064// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
65// framework works, sometimes they happen to technically be copied twice, which is fine and elided
66// into a single copy unless the class has a non-trivial copy constructor. For classes with
67// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
68// with delay_copy(), forcing the argument to be passed by const&.
69//
70// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
71// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
72// unexpectedly on a profile of record time. Otherwise don't bother.
73template <typename T>
74class Reference {
75public:
76 Reference(const T& x) : fX(x) {}
77 operator const T&() const { return fX; }
78private:
79 const T& fX;
80};
81
82template <typename T>
83static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
84
85// Use copy() only for optional arguments, to be copied if present or skipped if not.
86// (For most types we just pass by value and let copy constructors do their thing.)
87template <typename T>
88T* SkRecorder::copy(const T* src) {
89 if (NULL == src) {
90 return NULL;
91 }
92 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
93}
94
95// This copy() is for arrays.
96// It will work with POD or non-POD, though currently we only use it for POD.
97template <typename T>
reed2347b622014-08-07 12:19:50 -070098T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000099 if (NULL == src) {
100 return NULL;
101 }
102 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -0700103 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000104 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
105 }
106 return dst;
107}
108
109// Specialization for copying strings, using memcpy.
110// This measured around 2x faster for copying code points,
111// but I found no corresponding speedup for other arrays.
112template <>
reed2347b622014-08-07 12:19:50 -0700113char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000114 if (NULL == src) {
115 return NULL;
116 }
117 char* dst = fRecord->alloc<char>(count);
118 memcpy(dst, src, count);
119 return dst;
120}
121
mtklein5f0e8222014-08-22 11:44:26 -0700122// As above, assuming and copying a terminating \0.
123template <>
124char* SkRecorder::copy(const char* src) {
125 return this->copy(src, strlen(src)+1);
126}
127
128
reed41af9662015-01-05 07:49:08 -0800129void SkRecorder::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000130 APPEND(DrawPaint, delay_copy(paint));
131}
132
reed41af9662015-01-05 07:49:08 -0800133void SkRecorder::onDrawPoints(PointMode mode,
134 size_t count,
135 const SkPoint pts[],
136 const SkPaint& paint) {
mtklein42ddcd42014-11-21 08:48:35 -0800137 APPEND(DrawPoints, delay_copy(paint), mode, SkToUInt(count), this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000138}
139
reed41af9662015-01-05 07:49:08 -0800140void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000141 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000142}
143
reed41af9662015-01-05 07:49:08 -0800144void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000145 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000146}
147
reed41af9662015-01-05 07:49:08 -0800148void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000149 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000150}
151
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000152void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000153 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000154}
155
reed3cb38402015-02-06 08:36:15 -0800156void SkRecorder::onDrawDrawable(SkDrawable* drawable) {
reed1bdfd3f2014-11-24 14:41:51 -0800157 if (!fDrawableList) {
reed3cb38402015-02-06 08:36:15 -0800158 fDrawableList.reset(SkNEW(SkDrawableList));
reed1bdfd3f2014-11-24 14:41:51 -0800159 }
160 fDrawableList->append(drawable);
161 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList->count() - 1);
reed6be2aa92014-11-18 11:08:05 -0800162}
163
reed41af9662015-01-05 07:49:08 -0800164void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
mtkleinaf579032014-12-01 11:03:37 -0800165 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000166}
167
reed41af9662015-01-05 07:49:08 -0800168void SkRecorder::onDrawBitmap(const SkBitmap& bitmap,
169 SkScalar left,
170 SkScalar top,
171 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000172 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000173}
174
reed41af9662015-01-05 07:49:08 -0800175void SkRecorder::onDrawBitmapRect(const SkBitmap& bitmap,
176 const SkRect* src,
177 const SkRect& dst,
178 const SkPaint* paint,
179 DrawBitmapRectFlags flags) {
mtklein42ddcd42014-11-21 08:48:35 -0800180 if (kBleed_DrawBitmapRectFlag == flags) {
181 APPEND(DrawBitmapRectToRectBleed,
182 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
183 return;
184 }
185 SkASSERT(kNone_DrawBitmapRectFlag == flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000186 APPEND(DrawBitmapRectToRect,
mtklein42ddcd42014-11-21 08:48:35 -0800187 this->copy(paint), delay_copy(bitmap), this->copy(src), dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000188}
189
reed41af9662015-01-05 07:49:08 -0800190void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
191 const SkIRect& center,
192 const SkRect& dst,
193 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000194 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000195}
196
reed41af9662015-01-05 07:49:08 -0800197void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
198 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700199 APPEND(DrawImage, this->copy(paint), image, left, top);
200}
201
reed41af9662015-01-05 07:49:08 -0800202void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src,
203 const SkRect& dst,
204 const SkPaint* paint) {
piotaixr65151752014-10-16 11:58:39 -0700205 APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst);
206}
207
reed41af9662015-01-05 07:49:08 -0800208void SkRecorder::onDrawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000209 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210}
211
reed@google.come0d9ce82014-04-23 04:00:17 +0000212void SkRecorder::onDrawText(const void* text, size_t byteLength,
213 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000214 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000215 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216}
217
reed@google.come0d9ce82014-04-23 04:00:17 +0000218void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
219 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000220 const unsigned points = paint.countText(text, byteLength);
221 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000222 delay_copy(paint),
223 this->copy((const char*)text, byteLength),
224 byteLength,
225 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000226}
227
reed@google.come0d9ce82014-04-23 04:00:17 +0000228void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
229 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000230 const unsigned points = paint.countText(text, byteLength);
231 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000232 delay_copy(paint),
233 this->copy((const char*)text, byteLength),
mtklein42ddcd42014-11-21 08:48:35 -0800234 SkToUInt(byteLength),
235 constY,
236 this->copy(xpos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237}
238
reed@google.come0d9ce82014-04-23 04:00:17 +0000239void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
240 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000241 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000242 delay_copy(paint),
243 this->copy((const char*)text, byteLength),
244 byteLength,
mtkleinaf579032014-12-01 11:03:37 -0800245 delay_copy(path),
246 matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000247}
248
fmalita00d5c2c2014-08-21 08:53:26 -0700249void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
250 const SkPaint& paint) {
251 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
252}
253
reedd5fa1a42014-08-09 11:08:05 -0700254void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
mtklein98b84852015-04-21 15:23:59 -0700255 fApproxBytesUsedBySubPictures += SkPictureUtils::ApproximateBytesUsed(pic);
mtkleinaf579032014-12-01 11:03:37 -0800256 APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000257}
258
reed41af9662015-01-05 07:49:08 -0800259void SkRecorder::onDrawVertices(VertexMode vmode,
260 int vertexCount, const SkPoint vertices[],
261 const SkPoint texs[], const SkColor colors[],
262 SkXfermode* xmode,
263 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000264 APPEND(DrawVertices, delay_copy(paint),
265 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000266 vertexCount,
267 this->copy(vertices, vertexCount),
268 texs ? this->copy(texs, vertexCount) : NULL,
269 colors ? this->copy(colors, vertexCount) : NULL,
270 xmode,
271 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000272 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000273}
274
dandovb3c9d1c2014-08-12 08:34:29 -0700275void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
276 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
277 APPEND(DrawPatch, delay_copy(paint),
278 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
279 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
280 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
281 xmode);
dandov963137b2014-08-07 07:49:53 -0700282}
283
Florin Malita5f6102d2014-06-30 10:13:28 -0400284void SkRecorder::willSave() {
285 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000286}
287
288SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
289 const SkPaint* paint,
290 SkCanvas::SaveFlags flags) {
291 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
292 return SkCanvas::kNoLayer_SaveLayerStrategy;
293}
294
mtklein6cfa73a2014-08-13 13:33:49 -0700295void SkRecorder::didRestore() {
mtkleina723b572014-08-15 11:49:49 -0700296 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000297}
298
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000299void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700300 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000301}
302
303void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700304 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700305 matrix.dump();
306 this->getTotalMatrix().dump();
307 SkASSERT(matrix == this->getTotalMatrix());
308 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000309 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000310}
311
312void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000313 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800314 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
315 APPEND(ClipRect, this->devBounds(), rect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000316}
317
318void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700319 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800320 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
321 APPEND(ClipRRect, this->devBounds(), rrect, opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000322}
323
324void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700325 INHERITED(onClipPath, path, op, edgeStyle);
mtkleincdeeb092014-11-20 09:14:28 -0800326 SkRecords::RegionOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
mtkleinaf579032014-12-01 11:03:37 -0800327 APPEND(ClipPath, this->devBounds(), delay_copy(path), opAA);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000328}
329
330void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000331 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700332 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000333}
mtklein5f0e8222014-08-22 11:44:26 -0700334
335void SkRecorder::beginCommentGroup(const char* description) {
336 APPEND(BeginCommentGroup, this->copy(description));
337}
338
339void SkRecorder::addComment(const char* key, const char* value) {
340 APPEND(AddComment, this->copy(key), this->copy(value));
341}
342
343void SkRecorder::endCommentGroup() {
344 APPEND(EndCommentGroup);
345}
mtklein29dfaa82014-09-04 14:12:44 -0700346