blob: c1c5596b1585c1719f789dd075f8f76b614156a1 [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#ifndef SkRecords_DEFINED
9#define SkRecords_DEFINED
10
11#include "SkCanvas.h"
reed2347b622014-08-07 12:19:50 -070012#include "SkPicture.h"
fmalita00d5c2c2014-08-21 08:53:26 -070013#include "SkTextBlob.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000014
15namespace SkRecords {
16
17// A list of all the types of canvas calls we can record.
18// Each of these is reified into a struct below.
19//
20// (We're using the macro-of-macro trick here to do several different things with the same list.)
21//
22// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
23// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000024//
25// Order doesn't technically matter here, but the compiler can generally generate better code if
26// you keep them semantically grouped, especially the Draws. It's also nice to leave NoOp at 0.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000027#define SK_RECORD_TYPES(M) \
28 M(NoOp) \
29 M(Restore) \
30 M(Save) \
31 M(SaveLayer) \
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000032 M(PushCull) \
33 M(PopCull) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000034 M(SetMatrix) \
35 M(ClipPath) \
36 M(ClipRRect) \
37 M(ClipRect) \
38 M(ClipRegion) \
39 M(Clear) \
mtklein5f0e8222014-08-22 11:44:26 -070040 M(BeginCommentGroup) \
41 M(AddComment) \
42 M(EndCommentGroup) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000043 M(DrawBitmap) \
44 M(DrawBitmapMatrix) \
45 M(DrawBitmapNine) \
46 M(DrawBitmapRectToRect) \
47 M(DrawDRRect) \
48 M(DrawOval) \
49 M(DrawPaint) \
50 M(DrawPath) \
dandov963137b2014-08-07 07:49:53 -070051 M(DrawPatch) \
reed2347b622014-08-07 12:19:50 -070052 M(DrawPicture) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000053 M(DrawPoints) \
54 M(DrawPosText) \
55 M(DrawPosTextH) \
mtkleinc551d9f2014-08-20 08:09:46 -070056 M(DrawText) \
57 M(DrawTextOnPath) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000058 M(DrawRRect) \
59 M(DrawRect) \
60 M(DrawSprite) \
mtklein29dfaa82014-09-04 14:12:44 -070061 M(DrawTextBlob) \
62 M(DrawData) \
mtkleinf4078ad2014-08-08 10:05:19 -070063 M(DrawVertices)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000064
65// Defines SkRecords::Type, an enum of all record types.
66#define ENUM(T) T##_Type,
67enum Type { SK_RECORD_TYPES(ENUM) };
68#undef ENUM
69
70// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
71// These should be clearer when you look at their use below.
72#define RECORD0(T) \
73struct T { \
74 static const Type kType = T##_Type; \
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000075};
76
77// We try to be flexible about the types the constructors take. Instead of requring the exact type
78// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
79// work, allowing the caller to decide whether to pass by value or by const&.
80
81#define RECORD1(T, A, a) \
82struct T { \
83 static const Type kType = T##_Type; \
84 template <typename Z> \
85 T(Z a) : a(a) {} \
86 A a; \
87};
88
89#define RECORD2(T, A, a, B, b) \
90struct T { \
91 static const Type kType = T##_Type; \
92 template <typename Z, typename Y> \
93 T(Z a, Y b) : a(a), b(b) {} \
94 A a; B b; \
95};
96
97#define RECORD3(T, A, a, B, b, C, c) \
98struct T { \
99 static const Type kType = T##_Type; \
100 template <typename Z, typename Y, typename X> \
101 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
102 A a; B b; C c; \
103};
104
105#define RECORD4(T, A, a, B, b, C, c, D, d) \
106struct T { \
107 static const Type kType = T##_Type; \
108 template <typename Z, typename Y, typename X, typename W> \
109 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
110 A a; B b; C c; D d; \
111};
112
113#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
114struct T { \
115 static const Type kType = T##_Type; \
116 template <typename Z, typename Y, typename X, typename W, typename V> \
117 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
118 A a; B b; C c; D d; E e; \
119};
120
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000121#define ACT_AS_PTR(ptr) \
122 operator T*() { return ptr; } \
123 operator const T*() const { return ptr; } \
124 T* operator->() { return ptr; } \
125 const T* operator->() const { return ptr; }
126
mtklein53fecfb2014-08-21 09:11:37 -0700127template <typename T>
128class RefBox : SkNoncopyable {
129public:
130 RefBox(T* obj) : fObj(SkRef(obj)) {}
131 ~RefBox() { fObj->unref(); }
132
133 ACT_AS_PTR(fObj);
134
135private:
136 T* fObj;
137};
138
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000139// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
140template <typename T>
141class Optional : SkNoncopyable {
142public:
143 Optional(T* ptr) : fPtr(ptr) {}
144 ~Optional() { if (fPtr) fPtr->~T(); }
145
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000146 ACT_AS_PTR(fPtr);
147private:
148 T* fPtr;
149};
150
151// Like Optional, but ptr must not be NULL.
152template <typename T>
153class Adopted : SkNoncopyable {
154public:
155 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); }
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000156 Adopted(Adopted* source) {
157 // Transfer ownership from source to this.
158 fPtr = source->fPtr;
159 source->fPtr = NULL;
160 }
161 ~Adopted() { if (fPtr) fPtr->~T(); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000162
163 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000164private:
165 T* fPtr;
166};
167
168// PODArray doesn't own the pointer's memory, and we assume the data is POD.
169template <typename T>
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000170class PODArray {
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000171public:
172 PODArray(T* ptr) : fPtr(ptr) {}
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000173 // Default copy and assign.
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000174
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000175 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000176private:
177 T* fPtr;
178};
179
commit-bot@chromium.org16307bd2014-04-22 18:32:58 +0000180#undef ACT_AS_PTR
181
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000182// Like SkBitmap, but deep copies pixels if they're not immutable.
183// Using this, we guarantee the immutability of all bitmaps we record.
mtkleinee369522014-08-27 13:02:24 -0700184class ImmutableBitmap : SkNoncopyable {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000185public:
186 explicit ImmutableBitmap(const SkBitmap& bitmap) {
187 if (bitmap.isImmutable()) {
188 fBitmap = bitmap;
189 } else {
190 bitmap.copyTo(&fBitmap);
191 }
192 fBitmap.setImmutable();
193 }
194
195 operator const SkBitmap& () const { return fBitmap; }
196
197private:
198 SkBitmap fBitmap;
199};
200
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000201RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000202
mtkleina723b572014-08-15 11:49:49 -0700203RECORD2(Restore, SkIRect, devBounds, SkMatrix, matrix);
Florin Malita5f6102d2014-06-30 10:13:28 -0400204RECORD0(Save);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000205RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000207RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000208RECORD0(PopCull);
209
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210RECORD1(SetMatrix, SkMatrix, matrix);
211
mtkleina723b572014-08-15 11:49:49 -0700212RECORD4(ClipPath, SkIRect, devBounds, SkPath, path, SkRegion::Op, op, bool, doAA);
213RECORD4(ClipRRect, SkIRect, devBounds, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
214RECORD4(ClipRect, SkIRect, devBounds, SkRect, rect, SkRegion::Op, op, bool, doAA);
215RECORD3(ClipRegion, SkIRect, devBounds, SkRegion, region, SkRegion::Op, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216
217RECORD1(Clear, SkColor, color);
mtklein5f0e8222014-08-22 11:44:26 -0700218
219RECORD1(BeginCommentGroup, PODArray<char>, description);
220RECORD2(AddComment, PODArray<char>, key, PODArray<char>, value);
221RECORD0(EndCommentGroup);
222
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000223// While not strictly required, if you have an SkPaint, it's fastest to put it first.
224RECORD4(DrawBitmap, Optional<SkPaint>, paint,
225 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000226 SkScalar, left,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000227 SkScalar, top);
228RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix);
229RECORD4(DrawBitmapNine, Optional<SkPaint>, paint,
230 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000231 SkIRect, center,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000232 SkRect, dst);
233RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint,
234 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000235 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236 SkRect, dst,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237 SkCanvas::DrawBitmapRectFlags, flags);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000238RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner);
239RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000240RECORD1(DrawPaint, SkPaint, paint);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000241RECORD2(DrawPath, SkPaint, paint, SkPath, path);
dandovb3c9d1c2014-08-12 08:34:29 -0700242//RECORD2(DrawPatch, SkPaint, paint, SkPatch, patch);
mtklein53fecfb2014-08-21 09:11:37 -0700243RECORD3(DrawPicture, Optional<SkPaint>, paint,
244 RefBox<const SkPicture>, picture,
245 Optional<SkMatrix>, matrix);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000246RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
247RECORD4(DrawPosText, SkPaint, paint,
248 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000249 size_t, byteLength,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000250 PODArray<SkPoint>, pos);
251RECORD5(DrawPosTextH, SkPaint, paint,
252 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000253 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000254 PODArray<SkScalar>, xpos,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000255 SkScalar, y);
256RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect);
257RECORD2(DrawRect, SkPaint, paint, SkRect, rect);
258RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top);
259RECORD5(DrawText, SkPaint, paint,
260 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000261 size_t, byteLength,
262 SkScalar, x,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000263 SkScalar, y);
fmalita00d5c2c2014-08-21 08:53:26 -0700264RECORD4(DrawTextBlob, SkPaint, paint,
mtklein53fecfb2014-08-21 09:11:37 -0700265 RefBox<const SkTextBlob>, blob,
fmalita00d5c2c2014-08-21 08:53:26 -0700266 SkScalar, x,
267 SkScalar, y);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000268RECORD5(DrawTextOnPath, SkPaint, paint,
269 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000270 size_t, byteLength,
271 SkPath, path,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000272 Optional<SkMatrix>, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000273
mtklein29dfaa82014-09-04 14:12:44 -0700274RECORD2(DrawData, PODArray<char>, data, size_t, length);
275
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000276// This guy is so ugly we just write it manually.
277struct DrawVertices {
278 static const Type kType = DrawVertices_Type;
279
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000280 DrawVertices(const SkPaint& paint,
281 SkCanvas::VertexMode vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000282 int vertexCount,
283 SkPoint* vertices,
284 SkPoint* texs,
285 SkColor* colors,
286 SkXfermode* xmode,
287 uint16_t* indices,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000288 int indexCount)
289 : paint(paint)
290 , vmode(vmode)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000291 , vertexCount(vertexCount)
292 , vertices(vertices)
293 , texs(texs)
294 , colors(colors)
295 , xmode(SkSafeRef(xmode))
296 , indices(indices)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000297 , indexCount(indexCount) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000298
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000299 SkPaint paint;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000300 SkCanvas::VertexMode vmode;
301 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000302 PODArray<SkPoint> vertices;
303 PODArray<SkPoint> texs;
304 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000305 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000306 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000307 int indexCount;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000308};
mtklein6cfa73a2014-08-13 13:33:49 -0700309
dandovb3c9d1c2014-08-12 08:34:29 -0700310struct DrawPatch {
311 static const Type kType = DrawPatch_Type;
mtklein6cfa73a2014-08-13 13:33:49 -0700312
dandovb3c9d1c2014-08-12 08:34:29 -0700313 DrawPatch(const SkPaint& paint, SkPoint cubics[12], SkColor colors[4],
314 SkPoint texCoords[4], SkXfermode* xmode)
315 : paint(paint)
316 , cubics(cubics)
317 , colors(colors)
318 , texCoords(texCoords)
319 , xmode(SkSafeRef(xmode)) { }
mtklein6cfa73a2014-08-13 13:33:49 -0700320
dandovb3c9d1c2014-08-12 08:34:29 -0700321 SkPaint paint;
322 PODArray<SkPoint> cubics;
323 PODArray<SkColor> colors;
324 PODArray<SkPoint> texCoords;
325 SkAutoTUnref<SkXfermode> xmode;
326};
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000327
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000328#undef RECORD0
329#undef RECORD1
330#undef RECORD2
331#undef RECORD3
332#undef RECORD4
333#undef RECORD5
334
335} // namespace SkRecords
336
337#endif//SkRecords_DEFINED