blob: 2e14c3e25d6c9b6b5a321b92e1d765801d701089 [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
12// SkCanvas will fail in mysterious ways if it doesn't know the real width and height.
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000013SkRecorder::SkRecorder(SkRecord* record, int width, int height)
14 : SkCanvas(width, height), fRecord(record) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000015
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000016void SkRecorder::forgetRecord() {
17 fRecord = NULL;
18}
19
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000020// To make appending to fRecord a little less verbose.
21#define APPEND(T, ...) \
22 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
23
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000024// For methods which must call back into SkCanvas.
25#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000026
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000027// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
28// framework works, sometimes they happen to technically be copied twice, which is fine and elided
29// into a single copy unless the class has a non-trivial copy constructor. For classes with
30// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
31// with delay_copy(), forcing the argument to be passed by const&.
32//
33// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
34// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
35// unexpectedly on a profile of record time. Otherwise don't bother.
36template <typename T>
37class Reference {
38public:
39 Reference(const T& x) : fX(x) {}
40 operator const T&() const { return fX; }
41private:
42 const T& fX;
43};
44
45template <typename T>
46static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
47
48// Use copy() only for optional arguments, to be copied if present or skipped if not.
49// (For most types we just pass by value and let copy constructors do their thing.)
50template <typename T>
51T* SkRecorder::copy(const T* src) {
52 if (NULL == src) {
53 return NULL;
54 }
55 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
56}
57
58// This copy() is for arrays.
59// It will work with POD or non-POD, though currently we only use it for POD.
60template <typename T>
reed2347b622014-08-07 12:19:50 -070061T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000062 if (NULL == src) {
63 return NULL;
64 }
65 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070066 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000067 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
68 }
69 return dst;
70}
71
72// Specialization for copying strings, using memcpy.
73// This measured around 2x faster for copying code points,
74// but I found no corresponding speedup for other arrays.
75template <>
reed2347b622014-08-07 12:19:50 -070076char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000077 if (NULL == src) {
78 return NULL;
79 }
80 char* dst = fRecord->alloc<char>(count);
81 memcpy(dst, src, count);
82 return dst;
83}
84
85void SkRecorder::clear(SkColor color) {
86 APPEND(Clear, color);
87}
88
89void SkRecorder::drawPaint(const SkPaint& paint) {
90 APPEND(DrawPaint, delay_copy(paint));
91}
92
93void SkRecorder::drawPoints(PointMode mode,
94 size_t count,
95 const SkPoint pts[],
96 const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000097 APPEND(DrawPoints, delay_copy(paint), mode, count, this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000098}
99
100void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000101 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000102}
103
104void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000105 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000106}
107
108void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000109 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000110}
111
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000112void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000113 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000114}
115
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000116void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000117 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000118}
119
120void SkRecorder::drawBitmap(const SkBitmap& bitmap,
121 SkScalar left,
122 SkScalar top,
123 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000124 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000125}
126
127void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
128 const SkRect* src,
129 const SkRect& dst,
130 const SkPaint* paint,
131 DrawBitmapRectFlags flags) {
132 APPEND(DrawBitmapRectToRect,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000133 this->copy(paint), delay_copy(bitmap), this->copy(src), dst, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000134}
135
136void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
137 const SkMatrix& matrix,
138 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000139 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000140}
141
142void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
143 const SkIRect& center,
144 const SkRect& dst,
145 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000146 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000147}
148
149void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000150 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000151}
152
reed@google.come0d9ce82014-04-23 04:00:17 +0000153void SkRecorder::onDrawText(const void* text, size_t byteLength,
154 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000155 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000156 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000157}
158
reed@google.come0d9ce82014-04-23 04:00:17 +0000159void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
160 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000161 const unsigned points = paint.countText(text, byteLength);
162 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000163 delay_copy(paint),
164 this->copy((const char*)text, byteLength),
165 byteLength,
166 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000167}
168
reed@google.come0d9ce82014-04-23 04:00:17 +0000169void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
170 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000171 const unsigned points = paint.countText(text, byteLength);
172 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000173 delay_copy(paint),
174 this->copy((const char*)text, byteLength),
175 byteLength,
176 this->copy(xpos, points),
177 constY);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000178}
179
reed@google.come0d9ce82014-04-23 04:00:17 +0000180void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
181 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000182 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000183 delay_copy(paint),
184 this->copy((const char*)text, byteLength),
185 byteLength,
186 delay_copy(path),
187 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000188}
189
fmalita00d5c2c2014-08-21 08:53:26 -0700190void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
191 const SkPaint& paint) {
192 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
193}
194
reedd5fa1a42014-08-09 11:08:05 -0700195void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
196 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000197}
198
199void SkRecorder::drawVertices(VertexMode vmode,
200 int vertexCount, const SkPoint vertices[],
201 const SkPoint texs[], const SkColor colors[],
202 SkXfermode* xmode,
203 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000204 APPEND(DrawVertices, delay_copy(paint),
205 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206 vertexCount,
207 this->copy(vertices, vertexCount),
208 texs ? this->copy(texs, vertexCount) : NULL,
209 colors ? this->copy(colors, vertexCount) : NULL,
210 xmode,
211 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000212 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000213}
214
dandovb3c9d1c2014-08-12 08:34:29 -0700215void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
216 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
217 APPEND(DrawPatch, delay_copy(paint),
218 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
219 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
220 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
221 xmode);
dandov963137b2014-08-07 07:49:53 -0700222}
223
Florin Malita5f6102d2014-06-30 10:13:28 -0400224void SkRecorder::willSave() {
225 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000226}
227
228SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
229 const SkPaint* paint,
230 SkCanvas::SaveFlags flags) {
231 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
232 return SkCanvas::kNoLayer_SaveLayerStrategy;
233}
234
mtklein6cfa73a2014-08-13 13:33:49 -0700235void SkRecorder::didRestore() {
mtkleina723b572014-08-15 11:49:49 -0700236 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237}
238
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000239void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000240 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000241}
242
243void SkRecorder::onPopCull() {
244 APPEND(PopCull);
245}
246
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000247void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700248 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000249}
250
251void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700252 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700253 matrix.dump();
254 this->getTotalMatrix().dump();
255 SkASSERT(matrix == this->getTotalMatrix());
256 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000257 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000258}
259
260void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000261 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700262 APPEND(ClipRect, this->devBounds(), rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000263}
264
265void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000266 INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false);
mtkleina723b572014-08-15 11:49:49 -0700267 APPEND(ClipRRect, this->devBounds(), rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000268}
269
270void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000271 INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType());
mtkleina723b572014-08-15 11:49:49 -0700272 APPEND(ClipPath, this->devBounds(), delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000273}
274
275void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000276 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700277 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000278}