blob: 19d60d5bf840d54929fe47ab425a40c35baec052 [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"
9#include "SkPicture.h"
10
11// 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 +000012SkRecorder::SkRecorder(SkRecord* record, int width, int height)
13 : SkCanvas(width, height), fRecord(record) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000014
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000015void SkRecorder::forgetRecord() {
16 fRecord = NULL;
17}
18
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000019// To make appending to fRecord a little less verbose.
20#define APPEND(T, ...) \
21 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
22
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000023// For methods which must call back into SkCanvas.
24#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000025
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000026// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
27// framework works, sometimes they happen to technically be copied twice, which is fine and elided
28// into a single copy unless the class has a non-trivial copy constructor. For classes with
29// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
30// with delay_copy(), forcing the argument to be passed by const&.
31//
32// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
33// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
34// unexpectedly on a profile of record time. Otherwise don't bother.
35template <typename T>
36class Reference {
37public:
38 Reference(const T& x) : fX(x) {}
39 operator const T&() const { return fX; }
40private:
41 const T& fX;
42};
43
44template <typename T>
45static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
46
47// Use copy() only for optional arguments, to be copied if present or skipped if not.
48// (For most types we just pass by value and let copy constructors do their thing.)
49template <typename T>
50T* SkRecorder::copy(const T* src) {
51 if (NULL == src) {
52 return NULL;
53 }
54 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
55}
56
57// This copy() is for arrays.
58// It will work with POD or non-POD, though currently we only use it for POD.
59template <typename T>
reed2347b622014-08-07 12:19:50 -070060T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000061 if (NULL == src) {
62 return NULL;
63 }
64 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070065 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000066 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
67 }
68 return dst;
69}
70
71// Specialization for copying strings, using memcpy.
72// This measured around 2x faster for copying code points,
73// but I found no corresponding speedup for other arrays.
74template <>
reed2347b622014-08-07 12:19:50 -070075char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000076 if (NULL == src) {
77 return NULL;
78 }
79 char* dst = fRecord->alloc<char>(count);
80 memcpy(dst, src, count);
81 return dst;
82}
83
84void SkRecorder::clear(SkColor color) {
85 APPEND(Clear, color);
86}
87
88void SkRecorder::drawPaint(const SkPaint& paint) {
89 APPEND(DrawPaint, delay_copy(paint));
90}
91
92void SkRecorder::drawPoints(PointMode mode,
93 size_t count,
94 const SkPoint pts[],
95 const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000096 APPEND(DrawPoints, delay_copy(paint), mode, count, this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000097}
98
99void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000100 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000101}
102
103void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000104 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000105}
106
107void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000108 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000109}
110
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000111void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000112 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000113}
114
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000115void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000116 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000117}
118
119void SkRecorder::drawBitmap(const SkBitmap& bitmap,
120 SkScalar left,
121 SkScalar top,
122 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000123 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000124}
125
126void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
127 const SkRect* src,
128 const SkRect& dst,
129 const SkPaint* paint,
130 DrawBitmapRectFlags flags) {
131 APPEND(DrawBitmapRectToRect,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000132 this->copy(paint), delay_copy(bitmap), this->copy(src), dst, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000133}
134
135void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
136 const SkMatrix& matrix,
137 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000138 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000139}
140
141void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
142 const SkIRect& center,
143 const SkRect& dst,
144 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000145 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000146}
147
148void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000149 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000150}
151
reed@google.come0d9ce82014-04-23 04:00:17 +0000152void SkRecorder::onDrawText(const void* text, size_t byteLength,
153 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000154 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000155 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156}
157
reed@google.come0d9ce82014-04-23 04:00:17 +0000158void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
159 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000160 const unsigned points = paint.countText(text, byteLength);
161 APPEND(DrawPosText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000162 delay_copy(paint),
163 this->copy((const char*)text, byteLength),
164 byteLength,
165 this->copy(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000166}
167
reed@google.come0d9ce82014-04-23 04:00:17 +0000168void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
169 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000170 const unsigned points = paint.countText(text, byteLength);
171 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000172 delay_copy(paint),
173 this->copy((const char*)text, byteLength),
174 byteLength,
175 this->copy(xpos, points),
176 constY);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000177}
178
reed@google.come0d9ce82014-04-23 04:00:17 +0000179void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
180 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000181 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000182 delay_copy(paint),
183 this->copy((const char*)text, byteLength),
184 byteLength,
185 delay_copy(path),
186 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000187}
188
robertphillips9b14f262014-06-04 05:40:44 -0700189void SkRecorder::onDrawPicture(const SkPicture* picture) {
reed2347b622014-08-07 12:19:50 -0700190 APPEND(DrawPicture, picture);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000191}
192
193void SkRecorder::drawVertices(VertexMode vmode,
194 int vertexCount, const SkPoint vertices[],
195 const SkPoint texs[], const SkColor colors[],
196 SkXfermode* xmode,
197 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000198 APPEND(DrawVertices, delay_copy(paint),
199 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000200 vertexCount,
201 this->copy(vertices, vertexCount),
202 texs ? this->copy(texs, vertexCount) : NULL,
203 colors ? this->copy(colors, vertexCount) : NULL,
204 xmode,
205 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000206 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000207}
208
dandov963137b2014-08-07 07:49:53 -0700209void SkRecorder::drawPatch(const SkPatch& patch, const SkPaint& paint) {
210 APPEND(DrawPatch, delay_copy(paint), delay_copy(patch));
211}
212
Florin Malita5f6102d2014-06-30 10:13:28 -0400213void SkRecorder::willSave() {
214 APPEND(Save);
215 INHERITED(willSave);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216}
217
218SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
219 const SkPaint* paint,
220 SkCanvas::SaveFlags flags) {
221 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000222 INHERITED(willSaveLayer, bounds, paint, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000223 return SkCanvas::kNoLayer_SaveLayerStrategy;
224}
225
226void SkRecorder::willRestore() {
227 APPEND(Restore);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000228 INHERITED(willRestore);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000229}
230
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000231void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000232 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000233}
234
235void SkRecorder::onPopCull() {
236 APPEND(PopCull);
237}
238
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000239void SkRecorder::didConcat(const SkMatrix& matrix) {
240 APPEND(Concat, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000241 INHERITED(didConcat, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000242}
243
244void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
245 APPEND(SetMatrix, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000246 INHERITED(didSetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000247}
248
249void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
250 APPEND(ClipRect, rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000251 INHERITED(onClipRect, rect, op, edgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000252}
253
254void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
255 APPEND(ClipRRect, rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000256 INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000257}
258
259void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
260 APPEND(ClipPath, delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000261 INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000262}
263
264void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
265 APPEND(ClipRegion, delay_copy(deviceRgn), op);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000266 INHERITED(onClipRegion, deviceRgn, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000267}