blob: 345597c5bb5300e49db15d659762aef6d18236b4 [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
15// To make appending to fRecord a little less verbose.
16#define APPEND(T, ...) \
17 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
18
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000019// For methods which must call back into SkCanvas in kReadWrite_Mode.
20#define INHERITED(method, ...) if (fMode == kReadWrite_Mode) this->SkCanvas::method(__VA_ARGS__)
21
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000022// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
23// framework works, sometimes they happen to technically be copied twice, which is fine and elided
24// into a single copy unless the class has a non-trivial copy constructor. For classes with
25// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
26// with delay_copy(), forcing the argument to be passed by const&.
27//
28// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
29// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
30// unexpectedly on a profile of record time. Otherwise don't bother.
31template <typename T>
32class Reference {
33public:
34 Reference(const T& x) : fX(x) {}
35 operator const T&() const { return fX; }
36private:
37 const T& fX;
38};
39
40template <typename T>
41static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
42
43// Use copy() only for optional arguments, to be copied if present or skipped if not.
44// (For most types we just pass by value and let copy constructors do their thing.)
45template <typename T>
46T* SkRecorder::copy(const T* src) {
47 if (NULL == src) {
48 return NULL;
49 }
50 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
51}
52
53// This copy() is for arrays.
54// It will work with POD or non-POD, though currently we only use it for POD.
55template <typename T>
56T* SkRecorder::copy(const T src[], unsigned count) {
57 if (NULL == src) {
58 return NULL;
59 }
60 T* dst = fRecord->alloc<T>(count);
61 for (unsigned i = 0; i < count; i++) {
62 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
63 }
64 return dst;
65}
66
67// Specialization for copying strings, using memcpy.
68// This measured around 2x faster for copying code points,
69// but I found no corresponding speedup for other arrays.
70template <>
71char* SkRecorder::copy(const char src[], unsigned count) {
72 if (NULL == src) {
73 return NULL;
74 }
75 char* dst = fRecord->alloc<char>(count);
76 memcpy(dst, src, count);
77 return dst;
78}
79
80void SkRecorder::clear(SkColor color) {
81 APPEND(Clear, color);
82}
83
84void SkRecorder::drawPaint(const SkPaint& paint) {
85 APPEND(DrawPaint, delay_copy(paint));
86}
87
88void SkRecorder::drawPoints(PointMode mode,
89 size_t count,
90 const SkPoint pts[],
91 const SkPaint& paint) {
92 APPEND(DrawPoints, mode, count, this->copy(pts, count), delay_copy(paint));
93}
94
95void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
96 APPEND(DrawRect, rect, delay_copy(paint));
97}
98
99void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
100 APPEND(DrawOval, oval, delay_copy(paint));
101}
102
103void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
104 APPEND(DrawRRect, rrect, delay_copy(paint));
105}
106
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000107void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
108 APPEND(DrawDRRect, outer, inner, delay_copy(paint));
109}
110
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000111void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
112 APPEND(DrawPath, delay_copy(path), delay_copy(paint));
113}
114
115void SkRecorder::drawBitmap(const SkBitmap& bitmap,
116 SkScalar left,
117 SkScalar top,
118 const SkPaint* paint) {
119 APPEND(DrawBitmap, delay_copy(bitmap), left, top, this->copy(paint));
120}
121
122void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
123 const SkRect* src,
124 const SkRect& dst,
125 const SkPaint* paint,
126 DrawBitmapRectFlags flags) {
127 APPEND(DrawBitmapRectToRect,
128 delay_copy(bitmap), this->copy(src), dst, this->copy(paint), flags);
129}
130
131void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
132 const SkMatrix& matrix,
133 const SkPaint* paint) {
134 APPEND(DrawBitmapMatrix, delay_copy(bitmap), matrix, this->copy(paint));
135}
136
137void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
138 const SkIRect& center,
139 const SkRect& dst,
140 const SkPaint* paint) {
141 APPEND(DrawBitmapNine, delay_copy(bitmap), center, dst, this->copy(paint));
142}
143
144void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
145 APPEND(DrawSprite, delay_copy(bitmap), left, top, this->copy(paint));
146}
147
reed@google.come0d9ce82014-04-23 04:00:17 +0000148void SkRecorder::onDrawText(const void* text, size_t byteLength,
149 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000150 APPEND(DrawText,
151 this->copy((const char*)text, byteLength), byteLength, x, y, delay_copy(paint));
152}
153
reed@google.come0d9ce82014-04-23 04:00:17 +0000154void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
155 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156 const unsigned points = paint.countText(text, byteLength);
157 APPEND(DrawPosText,
158 this->copy((const char*)text, byteLength), byteLength,
159 this->copy(pos, points), delay_copy(paint));
160}
161
reed@google.come0d9ce82014-04-23 04:00:17 +0000162void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
163 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000164 const unsigned points = paint.countText(text, byteLength);
165 APPEND(DrawPosTextH,
166 this->copy((const char*)text, byteLength), byteLength,
167 this->copy(xpos, points), constY, delay_copy(paint));
168}
169
reed@google.come0d9ce82014-04-23 04:00:17 +0000170void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
171 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000172 APPEND(DrawTextOnPath,
173 this->copy((const char*)text, byteLength), byteLength,
174 delay_copy(path), this->copy(matrix), delay_copy(paint));
175}
176
177void SkRecorder::drawPicture(SkPicture& picture) {
178 picture.draw(this);
179}
180
181void SkRecorder::drawVertices(VertexMode vmode,
182 int vertexCount, const SkPoint vertices[],
183 const SkPoint texs[], const SkColor colors[],
184 SkXfermode* xmode,
185 const uint16_t indices[], int indexCount, const SkPaint& paint) {
186 APPEND(DrawVertices, vmode,
187 vertexCount,
188 this->copy(vertices, vertexCount),
189 texs ? this->copy(texs, vertexCount) : NULL,
190 colors ? this->copy(colors, vertexCount) : NULL,
191 xmode,
192 this->copy(indices, indexCount),
193 indexCount,
194 delay_copy(paint));
195}
196
197void SkRecorder::willSave(SkCanvas::SaveFlags flags) {
198 APPEND(Save, flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000199 INHERITED(willSave, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000200}
201
202SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
203 const SkPaint* paint,
204 SkCanvas::SaveFlags flags) {
205 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000206 INHERITED(willSaveLayer, bounds, paint, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000207 return SkCanvas::kNoLayer_SaveLayerStrategy;
208}
209
210void SkRecorder::willRestore() {
211 APPEND(Restore);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000212 INHERITED(willRestore);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000213}
214
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000215void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000216 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000217}
218
219void SkRecorder::onPopCull() {
220 APPEND(PopCull);
221}
222
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000223void SkRecorder::didConcat(const SkMatrix& matrix) {
224 APPEND(Concat, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000225 INHERITED(didConcat, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000226}
227
228void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
229 APPEND(SetMatrix, matrix);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000230 INHERITED(didSetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000231}
232
233void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
234 APPEND(ClipRect, rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000235 INHERITED(onClipRect, rect, op, edgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236}
237
238void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
239 APPEND(ClipRRect, rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000240 INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000241}
242
243void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
244 APPEND(ClipPath, delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000245 INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000246}
247
248void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
249 APPEND(ClipRegion, delay_copy(deviceRgn), op);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000250 INHERITED(onClipRegion, deviceRgn, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000251}