blob: fe4f35f54b1aa77372506ef86fc692a252cb95a8 [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.orgd9ce2be2014-04-09 23:30:28 +000012SkRecorder::SkRecorder(SkRecorder::Mode mode, SkRecord* record, int width, int height)
13 : SkCanvas(width, height), fMode(mode), 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.orgd9ce2be2014-04-09 23:30:28 +000023// For methods which must call back into SkCanvas in kReadWrite_Mode.
24#define INHERITED(method, ...) if (fMode == kReadWrite_Mode) this->SkCanvas::method(__VA_ARGS__)
25
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>
60T* SkRecorder::copy(const T src[], unsigned count) {
61 if (NULL == src) {
62 return NULL;
63 }
64 T* dst = fRecord->alloc<T>(count);
65 for (unsigned i = 0; i < count; i++) {
66 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 <>
75char* SkRecorder::copy(const char src[], unsigned count) {
76 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) {
96 APPEND(DrawPoints, mode, count, this->copy(pts, count), delay_copy(paint));
97}
98
99void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
100 APPEND(DrawRect, rect, delay_copy(paint));
101}
102
103void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
104 APPEND(DrawOval, oval, delay_copy(paint));
105}
106
107void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
108 APPEND(DrawRRect, rrect, delay_copy(paint));
109}
110
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000111void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
112 APPEND(DrawDRRect, outer, inner, delay_copy(paint));
113}
114
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000115void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
116 APPEND(DrawPath, delay_copy(path), delay_copy(paint));
117}
118
119void SkRecorder::drawBitmap(const SkBitmap& bitmap,
120 SkScalar left,
121 SkScalar top,
122 const SkPaint* paint) {
123 APPEND(DrawBitmap, delay_copy(bitmap), left, top, this->copy(paint));
124}
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,
132 delay_copy(bitmap), this->copy(src), dst, this->copy(paint), flags);
133}
134
135void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
136 const SkMatrix& matrix,
137 const SkPaint* paint) {
138 APPEND(DrawBitmapMatrix, delay_copy(bitmap), matrix, this->copy(paint));
139}
140
141void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
142 const SkIRect& center,
143 const SkRect& dst,
144 const SkPaint* paint) {
145 APPEND(DrawBitmapNine, delay_copy(bitmap), center, dst, this->copy(paint));
146}
147
148void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
149 APPEND(DrawSprite, delay_copy(bitmap), left, top, this->copy(paint));
150}
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,
155 this->copy((const char*)text, byteLength), byteLength, x, y, delay_copy(paint));
156}
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,
162 this->copy((const char*)text, byteLength), byteLength,
163 this->copy(pos, points), delay_copy(paint));
164}
165
reed@google.come0d9ce82014-04-23 04:00:17 +0000166void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
167 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000168 const unsigned points = paint.countText(text, byteLength);
169 APPEND(DrawPosTextH,
170 this->copy((const char*)text, byteLength), byteLength,
171 this->copy(xpos, points), constY, delay_copy(paint));
172}
173
reed@google.come0d9ce82014-04-23 04:00:17 +0000174void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
175 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000176 APPEND(DrawTextOnPath,
177 this->copy((const char*)text, byteLength), byteLength,
178 delay_copy(path), this->copy(matrix), delay_copy(paint));
179}
180
181void SkRecorder::drawPicture(SkPicture& picture) {
182 picture.draw(this);
183}
184
185void SkRecorder::drawVertices(VertexMode vmode,
186 int vertexCount, const SkPoint vertices[],
187 const SkPoint texs[], const SkColor colors[],
188 SkXfermode* xmode,
189 const uint16_t indices[], int indexCount, const SkPaint& paint) {
190 APPEND(DrawVertices, vmode,
191 vertexCount,
192 this->copy(vertices, vertexCount),
193 texs ? this->copy(texs, vertexCount) : NULL,
194 colors ? this->copy(colors, vertexCount) : NULL,
195 xmode,
196 this->copy(indices, indexCount),
197 indexCount,
198 delay_copy(paint));
199}
200
201void SkRecorder::willSave(SkCanvas::SaveFlags flags) {
202 APPEND(Save, flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000203 INHERITED(willSave, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000204}
205
206SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
207 const SkPaint* paint,
208 SkCanvas::SaveFlags flags) {
209 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000210 INHERITED(willSaveLayer, bounds, paint, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000211 return SkCanvas::kNoLayer_SaveLayerStrategy;
212}
213
214void SkRecorder::willRestore() {
215 APPEND(Restore);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000216 INHERITED(willRestore);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000217}
218
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000219void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000220 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000221}
222
223void SkRecorder::onPopCull() {
224 APPEND(PopCull);
225}
226
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000227void SkRecorder::didConcat(const SkMatrix& matrix) {
228 APPEND(Concat, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000229 INHERITED(didConcat, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000230}
231
232void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
233 APPEND(SetMatrix, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000234 INHERITED(didSetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000235}
236
237void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
238 APPEND(ClipRect, rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000239 INHERITED(onClipRect, rect, op, edgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000240}
241
242void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
243 APPEND(ClipRRect, rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000244 INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000245}
246
247void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
248 APPEND(ClipPath, delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000249 INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000250}
251
252void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
253 APPEND(ClipRegion, delay_copy(deviceRgn), op);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000254 INHERITED(onClipRegion, deviceRgn, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000255}