blob: 67cfb20c4ca4005029d0b3ac02a18b0fc0638e76 [file] [log] [blame]
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +00001#ifndef SkRecords_DEFINED
2#define SkRecords_DEFINED
3
4#include "SkCanvas.h"
5
6namespace SkRecords {
7
8// A list of all the types of canvas calls we can record.
9// Each of these is reified into a struct below.
10//
11// (We're using the macro-of-macro trick here to do several different things with the same list.)
12//
13// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
14// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.)
15#define SK_RECORD_TYPES(M) \
16 M(Restore) \
17 M(Save) \
18 M(SaveLayer) \
19 M(Concat) \
20 M(SetMatrix) \
21 M(ClipPath) \
22 M(ClipRRect) \
23 M(ClipRect) \
24 M(ClipRegion) \
25 M(Clear) \
26 M(DrawBitmap) \
27 M(DrawBitmapMatrix) \
28 M(DrawBitmapNine) \
29 M(DrawBitmapRectToRect) \
30 M(DrawDRRect) \
31 M(DrawOval) \
32 M(DrawPaint) \
33 M(DrawPath) \
34 M(DrawPoints) \
35 M(DrawPosText) \
36 M(DrawPosTextH) \
37 M(DrawRRect) \
38 M(DrawRect) \
39 M(DrawSprite) \
40 M(DrawText) \
41 M(DrawTextOnPath) \
42 M(DrawVertices)
43
44// Defines SkRecords::Type, an enum of all record types.
45#define ENUM(T) T##_Type,
46enum Type { SK_RECORD_TYPES(ENUM) };
47#undef ENUM
48
49// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
50// These should be clearer when you look at their use below.
51#define RECORD0(T) \
52struct T { \
53 static const Type kType = T##_Type; \
54 T() {} \
55};
56
57// We try to be flexible about the types the constructors take. Instead of requring the exact type
58// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
59// work, allowing the caller to decide whether to pass by value or by const&.
60
61#define RECORD1(T, A, a) \
62struct T { \
63 static const Type kType = T##_Type; \
64 template <typename Z> \
65 T(Z a) : a(a) {} \
66 A a; \
67};
68
69#define RECORD2(T, A, a, B, b) \
70struct T { \
71 static const Type kType = T##_Type; \
72 template <typename Z, typename Y> \
73 T(Z a, Y b) : a(a), b(b) {} \
74 A a; B b; \
75};
76
77#define RECORD3(T, A, a, B, b, C, c) \
78struct T { \
79 static const Type kType = T##_Type; \
80 template <typename Z, typename Y, typename X> \
81 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
82 A a; B b; C c; \
83};
84
85#define RECORD4(T, A, a, B, b, C, c, D, d) \
86struct T { \
87 static const Type kType = T##_Type; \
88 template <typename Z, typename Y, typename X, typename W> \
89 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
90 A a; B b; C c; D d; \
91};
92
93#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
94struct T { \
95 static const Type kType = T##_Type; \
96 template <typename Z, typename Y, typename X, typename W, typename V> \
97 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
98 A a; B b; C c; D d; E e; \
99};
100
101// Like SkBitmap, but deep copies pixels if they're not immutable.
102// Using this, we guarantee the immutability of all bitmaps we record.
103class ImmutableBitmap {
104public:
105 explicit ImmutableBitmap(const SkBitmap& bitmap) {
106 if (bitmap.isImmutable()) {
107 fBitmap = bitmap;
108 } else {
109 bitmap.copyTo(&fBitmap);
110 }
111 fBitmap.setImmutable();
112 }
113
114 operator const SkBitmap& () const { return fBitmap; }
115
116private:
117 SkBitmap fBitmap;
118};
119
120// Pointers here represent either an optional value or an array if accompanied by a count.
121// None of these records manages the lifetimes of pointers, except for DrawVertices handling its
122// Xfermode specially.
123
124RECORD0(Restore);
125RECORD1(Save, SkCanvas::SaveFlags, flags);
126RECORD3(SaveLayer, SkRect*, bounds, SkPaint*, paint, SkCanvas::SaveFlags, flags);
127
128RECORD1(Concat, SkMatrix, matrix);
129RECORD1(SetMatrix, SkMatrix, matrix);
130
131RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA);
132RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
133RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA);
134RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op);
135
136RECORD1(Clear, SkColor, color);
137RECORD4(DrawBitmap, ImmutableBitmap, bitmap, SkScalar, left, SkScalar, top, SkPaint*, paint);
138RECORD3(DrawBitmapMatrix, ImmutableBitmap, bitmap, SkMatrix, matrix, SkPaint*, paint);
139RECORD4(DrawBitmapNine, ImmutableBitmap, bitmap, SkIRect, center, SkRect, dst, SkPaint*, paint);
140RECORD5(DrawBitmapRectToRect, ImmutableBitmap, bitmap,
141 SkRect*, src,
142 SkRect, dst,
143 SkPaint*, paint,
144 SkCanvas::DrawBitmapRectFlags, flags);
145RECORD3(DrawDRRect, SkRRect, outer, SkRRect, inner, SkPaint, paint);
146RECORD2(DrawOval, SkRect, oval, SkPaint, paint);
147RECORD1(DrawPaint, SkPaint, paint);
148RECORD2(DrawPath, SkPath, path, SkPaint, paint);
149RECORD4(DrawPoints, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts, SkPaint, paint);
150RECORD4(DrawPosText, char*, text, size_t, byteLength, SkPoint*, pos, SkPaint, paint);
151RECORD5(DrawPosTextH, char*, text,
152 size_t, byteLength,
153 SkScalar*, xpos,
154 SkScalar, y,
155 SkPaint, paint);
156RECORD2(DrawRRect, SkRRect, rrect, SkPaint, paint);
157RECORD2(DrawRect, SkRect, rect, SkPaint, paint);
158RECORD4(DrawSprite, ImmutableBitmap, bitmap, int, left, int, top, SkPaint*, paint);
159RECORD5(DrawText, char*, text, size_t, byteLength, SkScalar, x, SkScalar, y, SkPaint, paint);
160RECORD5(DrawTextOnPath, char*, text,
161 size_t, byteLength,
162 SkPath, path,
163 SkMatrix*, matrix,
164 SkPaint, paint);
165
166// This guy is so ugly we just write it manually.
167struct DrawVertices {
168 static const Type kType = DrawVertices_Type;
169
170 DrawVertices(SkCanvas::VertexMode vmode,
171 int vertexCount,
172 SkPoint* vertices,
173 SkPoint* texs,
174 SkColor* colors,
175 SkXfermode* xmode,
176 uint16_t* indices,
177 int indexCount,
178 const SkPaint& paint)
179 : vmode(vmode)
180 , vertexCount(vertexCount)
181 , vertices(vertices)
182 , texs(texs)
183 , colors(colors)
184 , xmode(SkSafeRef(xmode))
185 , indices(indices)
186 , indexCount(indexCount)
187 , paint(paint) {}
188
189 SkCanvas::VertexMode vmode;
190 int vertexCount;
191 SkPoint* vertices;
192 SkPoint* texs;
193 SkColor* colors;
194 SkAutoTUnref<SkXfermode> xmode;
195 uint16_t* indices;
196 int indexCount;
197 SkPaint paint;
198};
199
200#undef RECORD0
201#undef RECORD1
202#undef RECORD2
203#undef RECORD3
204#undef RECORD4
205#undef RECORD5
206
207} // namespace SkRecords
208
209#endif//SkRecords_DEFINED