blob: f6c16d1c306a1bd9a192ab06f44c8c6fa50efc8c [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)
reedd9544982014-09-09 18:46:22 -070014 : SkCanvas(width, height, SkCanvas::kConservativeRasterClip_InitFlag)
mtklein29dfaa82014-09-04 14:12:44 -070015 , fRecord(record)
16 , fSaveLayerCount(0) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000017
commit-bot@chromium.org732bd662014-04-24 15:22:55 +000018void SkRecorder::forgetRecord() {
19 fRecord = NULL;
20}
21
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000022// To make appending to fRecord a little less verbose.
23#define APPEND(T, ...) \
24 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__))
25
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000026// For methods which must call back into SkCanvas.
27#define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__)
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000028
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000029// The structs we're creating all copy their constructor arguments. Given the way the SkRecords
30// framework works, sometimes they happen to technically be copied twice, which is fine and elided
31// into a single copy unless the class has a non-trivial copy constructor. For classes with
32// non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value
33// with delay_copy(), forcing the argument to be passed by const&.
34//
35// This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy
36// constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up
37// unexpectedly on a profile of record time. Otherwise don't bother.
38template <typename T>
39class Reference {
40public:
41 Reference(const T& x) : fX(x) {}
42 operator const T&() const { return fX; }
43private:
44 const T& fX;
45};
46
47template <typename T>
48static Reference<T> delay_copy(const T& x) { return Reference<T>(x); }
49
50// Use copy() only for optional arguments, to be copied if present or skipped if not.
51// (For most types we just pass by value and let copy constructors do their thing.)
52template <typename T>
53T* SkRecorder::copy(const T* src) {
54 if (NULL == src) {
55 return NULL;
56 }
57 return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src));
58}
59
60// This copy() is for arrays.
61// It will work with POD or non-POD, though currently we only use it for POD.
62template <typename T>
reed2347b622014-08-07 12:19:50 -070063T* SkRecorder::copy(const T src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000064 if (NULL == src) {
65 return NULL;
66 }
67 T* dst = fRecord->alloc<T>(count);
reed2347b622014-08-07 12:19:50 -070068 for (size_t i = 0; i < count; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000069 SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i]));
70 }
71 return dst;
72}
73
74// Specialization for copying strings, using memcpy.
75// This measured around 2x faster for copying code points,
76// but I found no corresponding speedup for other arrays.
77template <>
reed2347b622014-08-07 12:19:50 -070078char* SkRecorder::copy(const char src[], size_t count) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000079 if (NULL == src) {
80 return NULL;
81 }
82 char* dst = fRecord->alloc<char>(count);
83 memcpy(dst, src, count);
84 return dst;
85}
86
mtklein5f0e8222014-08-22 11:44:26 -070087// As above, assuming and copying a terminating \0.
88template <>
89char* SkRecorder::copy(const char* src) {
90 return this->copy(src, strlen(src)+1);
91}
92
93
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000094void SkRecorder::clear(SkColor color) {
95 APPEND(Clear, color);
96}
97
98void SkRecorder::drawPaint(const SkPaint& paint) {
99 APPEND(DrawPaint, delay_copy(paint));
100}
101
102void SkRecorder::drawPoints(PointMode mode,
103 size_t count,
104 const SkPoint pts[],
105 const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000106 APPEND(DrawPoints, delay_copy(paint), mode, count, this->copy(pts, count));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000107}
108
109void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000110 APPEND(DrawRect, delay_copy(paint), rect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000111}
112
113void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000114 APPEND(DrawOval, delay_copy(paint), oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000115}
116
117void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000118 APPEND(DrawRRect, delay_copy(paint), rrect);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000119}
120
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000121void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000122 APPEND(DrawDRRect, delay_copy(paint), outer, inner);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000123}
124
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000125void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000126 APPEND(DrawPath, delay_copy(paint), delay_copy(path));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000127}
128
129void SkRecorder::drawBitmap(const SkBitmap& bitmap,
130 SkScalar left,
131 SkScalar top,
132 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000133 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000134}
135
136void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap,
137 const SkRect* src,
138 const SkRect& dst,
139 const SkPaint* paint,
140 DrawBitmapRectFlags flags) {
141 APPEND(DrawBitmapRectToRect,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000142 this->copy(paint), delay_copy(bitmap), this->copy(src), dst, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000143}
144
145void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap,
146 const SkMatrix& matrix,
147 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000148 APPEND(DrawBitmapMatrix, this->copy(paint), delay_copy(bitmap), matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000149}
150
151void SkRecorder::drawBitmapNine(const SkBitmap& bitmap,
152 const SkIRect& center,
153 const SkRect& dst,
154 const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000155 APPEND(DrawBitmapNine, this->copy(paint), delay_copy(bitmap), center, dst);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156}
157
158void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000159 APPEND(DrawSprite, this->copy(paint), delay_copy(bitmap), left, top);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000160}
161
reed@google.come0d9ce82014-04-23 04:00:17 +0000162void SkRecorder::onDrawText(const void* text, size_t byteLength,
163 SkScalar x, SkScalar y, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000164 APPEND(DrawText,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000165 delay_copy(paint), this->copy((const char*)text, byteLength), byteLength, x, y);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000166}
167
reed@google.come0d9ce82014-04-23 04:00:17 +0000168void SkRecorder::onDrawPosText(const void* text, size_t byteLength,
169 const SkPoint pos[], const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000170 const unsigned points = paint.countText(text, byteLength);
171 APPEND(DrawPosText,
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(pos, points));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000176}
177
reed@google.come0d9ce82014-04-23 04:00:17 +0000178void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength,
179 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000180 const unsigned points = paint.countText(text, byteLength);
181 APPEND(DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000182 delay_copy(paint),
183 this->copy((const char*)text, byteLength),
184 byteLength,
185 this->copy(xpos, points),
186 constY);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000187}
188
reed@google.come0d9ce82014-04-23 04:00:17 +0000189void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
190 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000191 APPEND(DrawTextOnPath,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000192 delay_copy(paint),
193 this->copy((const char*)text, byteLength),
194 byteLength,
195 delay_copy(path),
196 this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000197}
198
fmalita00d5c2c2014-08-21 08:53:26 -0700199void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
200 const SkPaint& paint) {
201 APPEND(DrawTextBlob, delay_copy(paint), blob, x, y);
202}
203
reedd5fa1a42014-08-09 11:08:05 -0700204void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
205 APPEND(DrawPicture, this->copy(paint), pic, this->copy(matrix));
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206}
207
208void SkRecorder::drawVertices(VertexMode vmode,
209 int vertexCount, const SkPoint vertices[],
210 const SkPoint texs[], const SkColor colors[],
211 SkXfermode* xmode,
212 const uint16_t indices[], int indexCount, const SkPaint& paint) {
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000213 APPEND(DrawVertices, delay_copy(paint),
214 vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000215 vertexCount,
216 this->copy(vertices, vertexCount),
217 texs ? this->copy(texs, vertexCount) : NULL,
218 colors ? this->copy(colors, vertexCount) : NULL,
219 xmode,
220 this->copy(indices, indexCount),
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000221 indexCount);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000222}
223
dandovb3c9d1c2014-08-12 08:34:29 -0700224void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
225 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
226 APPEND(DrawPatch, delay_copy(paint),
227 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : NULL,
228 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : NULL,
229 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : NULL,
230 xmode);
dandov963137b2014-08-07 07:49:53 -0700231}
232
Florin Malita5f6102d2014-06-30 10:13:28 -0400233void SkRecorder::willSave() {
mtklein29dfaa82014-09-04 14:12:44 -0700234 fSaveIsSaveLayer.push(false);
Florin Malita5f6102d2014-06-30 10:13:28 -0400235 APPEND(Save);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236}
237
238SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
239 const SkPaint* paint,
240 SkCanvas::SaveFlags flags) {
mtklein29dfaa82014-09-04 14:12:44 -0700241 fSaveLayerCount++;
242 fSaveIsSaveLayer.push(true);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000243 APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
244 return SkCanvas::kNoLayer_SaveLayerStrategy;
245}
246
mtklein6cfa73a2014-08-13 13:33:49 -0700247void SkRecorder::didRestore() {
mtklein29dfaa82014-09-04 14:12:44 -0700248 SkBool8 saveLayer;
249 fSaveIsSaveLayer.pop(&saveLayer);
250 if (saveLayer) {
251 fSaveLayerCount--;
252 }
mtkleina723b572014-08-15 11:49:49 -0700253 APPEND(Restore, this->devBounds(), this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000254}
255
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000256void SkRecorder::onPushCull(const SkRect& rect) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000257 APPEND(PushCull, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000258}
259
260void SkRecorder::onPopCull() {
261 APPEND(PopCull);
262}
263
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000264void SkRecorder::didConcat(const SkMatrix& matrix) {
mtklein6332f1d2014-08-19 07:09:40 -0700265 this->didSetMatrix(this->getTotalMatrix());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000266}
267
268void SkRecorder::didSetMatrix(const SkMatrix& matrix) {
mtklein46bc6212014-08-20 09:44:28 -0700269 SkDEVCODE(if (matrix != this->getTotalMatrix()) {
mtkleinec924b92014-08-20 09:22:28 -0700270 matrix.dump();
271 this->getTotalMatrix().dump();
272 SkASSERT(matrix == this->getTotalMatrix());
273 })
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000274 APPEND(SetMatrix, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000275}
276
277void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000278 INHERITED(onClipRect, rect, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700279 APPEND(ClipRect, this->devBounds(), rect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000280}
281
282void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700283 INHERITED(onClipRRect, rrect, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700284 APPEND(ClipRRect, this->devBounds(), rrect, op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000285}
286
287void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reedd9544982014-09-09 18:46:22 -0700288 INHERITED(onClipPath, path, op, edgeStyle);
mtkleina723b572014-08-15 11:49:49 -0700289 APPEND(ClipPath, this->devBounds(), delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000290}
291
292void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000293 INHERITED(onClipRegion, deviceRgn, op);
mtkleina723b572014-08-15 11:49:49 -0700294 APPEND(ClipRegion, this->devBounds(), delay_copy(deviceRgn), op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000295}
mtklein5f0e8222014-08-22 11:44:26 -0700296
297void SkRecorder::beginCommentGroup(const char* description) {
298 APPEND(BeginCommentGroup, this->copy(description));
299}
300
301void SkRecorder::addComment(const char* key, const char* value) {
302 APPEND(AddComment, this->copy(key), this->copy(value));
303}
304
305void SkRecorder::endCommentGroup() {
306 APPEND(EndCommentGroup);
307}
mtklein29dfaa82014-09-04 14:12:44 -0700308
309bool SkRecorder::isDrawingToLayer() const {
310 return fSaveLayerCount > 0;
311}
312
313void SkRecorder::drawData(const void* data, size_t length) {
314 APPEND(DrawData, copy((const char*)data), length);
315}