blob: 0867d98846c0e3d8a3cb46cbb46fdc2f73f199cc [file] [log] [blame]
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00001#include "SkRecorder.h"
2#include "SkPicture.h"
3
4// SkCanvas will fail in mysterious ways if it doesn't know the real width and height.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +00005SkRecorder::SkRecorder(SkRecorder::Mode mode, SkRecord* record, int width, int height)
6 : SkCanvas(width, height), fMode(mode), fRecord(record) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00007
8// To make appending to fRecord a little less verbose.
9#define APPEND(T, ...) \
10 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
11
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000012// For methods which must call back into SkCanvas in kReadWrite_Mode.
13#define INHERITED(method, ...) if (fMode == kReadWrite_Mode) this->SkCanvas::method(__VA_ARGS__)
14
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000015// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
16// framework works, sometimes they happen to technically be copied twice, which is fine and elided
17// into a single copy unless the class has a non-trivial copy constructor. For classes with
18// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
19// with delay_copy(), forcing the argument to be passed by const&.
20//
21// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
22// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
23// unexpectedly on a profile of record time. Otherwise don't bother.
24template <typename T>
25class Reference {
26public:
27 Reference(const T& x) : fX(x) {}
28 operator const T&() const { return fX; }
29private:
30 const T& fX;
31};
32
33template <typename T>
34static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
35
36// Use copy() only for optional arguments, to be copied if present or skipped if not.
37// (For most types we just pass by value and let copy constructors do their thing.)
38template <typename T>
39T* SkRecorder::copy(const T* src) {
40 if (NULL == src) {
41 return NULL;
42 }
43 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
44}
45
46// This copy() is for arrays.
47// It will work with POD or non-POD, though currently we only use it for POD.
48template <typename T>
49T* SkRecorder::copy(const T src[], unsigned count) {
50 if (NULL == src) {
51 return NULL;
52 }
53 T* dst = fRecord->alloc<T>(count);
54 for (unsigned i = 0; i < count; i++) {
55 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
56 }
57 return dst;
58}
59
60// Specialization for copying strings, using memcpy.
61// This measured around 2x faster for copying code points,
62// but I found no corresponding speedup for other arrays.
63template <>
64char* SkRecorder::copy(const char src[], unsigned count) {
65 if (NULL == src) {
66 return NULL;
67 }
68 char* dst = fRecord->alloc<char>(count);
69 memcpy(dst, src, count);
70 return dst;
71}
72
73void SkRecorder::clear(SkColor color) {
74 APPEND(Clear, color);
75}
76
77void SkRecorder::drawPaint(const SkPaint& paint) {
78 APPEND(DrawPaint, delay_copy(paint));
79}
80
81void SkRecorder::drawPoints(PointMode mode,
82 size_t count,
83 const SkPoint pts[],
84 const SkPaint& paint) {
85 APPEND(DrawPoints, mode, count, this->copy(pts, count), delay_copy(paint));
86}
87
88void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
89 APPEND(DrawRect, rect, delay_copy(paint));
90}
91
92void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
93 APPEND(DrawOval, oval, delay_copy(paint));
94}
95
96void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
97 APPEND(DrawRRect, rrect, delay_copy(paint));
98}
99
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000100void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
101 APPEND(DrawDRRect, outer, inner, delay_copy(paint));
102}
103
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000104void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
105 APPEND(DrawPath, delay_copy(path), delay_copy(paint));
106}
107
108void SkRecorder::drawBitmap(const SkBitmap& bitmap,
109 SkScalar left,
110 SkScalar top,
111 const SkPaint* paint) {
112 APPEND(DrawBitmap, delay_copy(bitmap), left, top, this->copy(paint));
113}
114
115void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
116 const SkRect* src,
117 const SkRect& dst,
118 const SkPaint* paint,
119 DrawBitmapRectFlags flags) {
120 APPEND(DrawBitmapRectToRect,
121 delay_copy(bitmap), this->copy(src), dst, this->copy(paint), flags);
122}
123
124void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
125 const SkMatrix& matrix,
126 const SkPaint* paint) {
127 APPEND(DrawBitmapMatrix, delay_copy(bitmap), matrix, this->copy(paint));
128}
129
130void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
131 const SkIRect& center,
132 const SkRect& dst,
133 const SkPaint* paint) {
134 APPEND(DrawBitmapNine, delay_copy(bitmap), center, dst, this->copy(paint));
135}
136
137void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
138 APPEND(DrawSprite, delay_copy(bitmap), left, top, this->copy(paint));
139}
140
141void SkRecorder::drawText(const void* text, size_t byteLength,
142 SkScalar x, SkScalar y, const SkPaint& paint) {
143 APPEND(DrawText,
144 this->copy((const char*)text, byteLength), byteLength, x, y, delay_copy(paint));
145}
146
147void SkRecorder::drawPosText(const void* text, size_t byteLength,
148 const SkPoint pos[], const SkPaint& paint) {
149 const unsigned points = paint.countText(text, byteLength);
150 APPEND(DrawPosText,
151 this->copy((const char*)text, byteLength), byteLength,
152 this->copy(pos, points), delay_copy(paint));
153}
154
155void SkRecorder::drawPosTextH(const void* text, size_t byteLength,
156 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
157 const unsigned points = paint.countText(text, byteLength);
158 APPEND(DrawPosTextH,
159 this->copy((const char*)text, byteLength), byteLength,
160 this->copy(xpos, points), constY, delay_copy(paint));
161}
162
163void SkRecorder::drawTextOnPath(const void* text, size_t byteLength,
164 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
165 APPEND(DrawTextOnPath,
166 this->copy((const char*)text, byteLength), byteLength,
167 delay_copy(path), this->copy(matrix), delay_copy(paint));
168}
169
170void SkRecorder::drawPicture(SkPicture& picture) {
171 picture.draw(this);
172}
173
174void SkRecorder::drawVertices(VertexMode vmode,
175 int vertexCount, const SkPoint vertices[],
176 const SkPoint texs[], const SkColor colors[],
177 SkXfermode* xmode,
178 const uint16_t indices[], int indexCount, const SkPaint& paint) {
179 APPEND(DrawVertices, vmode,
180 vertexCount,
181 this->copy(vertices, vertexCount),
182 texs ? this->copy(texs, vertexCount) : NULL,
183 colors ? this->copy(colors, vertexCount) : NULL,
184 xmode,
185 this->copy(indices, indexCount),
186 indexCount,
187 delay_copy(paint));
188}
189
190void SkRecorder::willSave(SkCanvas::SaveFlags flags) {
191 APPEND(Save, flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000192 INHERITED(willSave, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000193}
194
195SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
196 const SkPaint* paint,
197 SkCanvas::SaveFlags flags) {
198 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000199 INHERITED(willSaveLayer, bounds, paint, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000200 return SkCanvas::kNoLayer_SaveLayerStrategy;
201}
202
203void SkRecorder::willRestore() {
204 APPEND(Restore);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000205 INHERITED(willRestore);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206}
207
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000208void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000209 APPEND(PushCull, rect, SkRecords::kUnsetPopOffset);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000210}
211
212void SkRecorder::onPopCull() {
213 APPEND(PopCull);
214}
215
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216void SkRecorder::didConcat(const SkMatrix& matrix) {
217 APPEND(Concat, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000218 INHERITED(didConcat, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000219}
220
221void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
222 APPEND(SetMatrix, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000223 INHERITED(didSetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000224}
225
226void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
227 APPEND(ClipRect, rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000228 INHERITED(onClipRect, rect, op, edgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000229}
230
231void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
232 APPEND(ClipRRect, rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000233 INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000234}
235
236void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
237 APPEND(ClipPath, delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000238 INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000239}
240
241void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
242 APPEND(ClipRegion, delay_copy(deviceRgn), op);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000243 INHERITED(onClipRegion, deviceRgn, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000244}