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