blob: 7a897406e94a7ded98f38f4ba0e3db314c6f45a0 [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.)
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000022#define SK_RECORD_TYPES(M) \
23 M(NoOp) \
24 M(Restore) \
25 M(Save) \
26 M(SaveLayer) \
27 M(Concat) \
28 M(SetMatrix) \
29 M(ClipPath) \
30 M(ClipRRect) \
31 M(ClipRect) \
32 M(ClipRegion) \
33 M(Clear) \
34 M(DrawBitmap) \
35 M(DrawBitmapMatrix) \
36 M(DrawBitmapNine) \
37 M(DrawBitmapRectToRect) \
38 M(DrawDRRect) \
39 M(DrawOval) \
40 M(DrawPaint) \
41 M(DrawPath) \
42 M(DrawPoints) \
43 M(DrawPosText) \
44 M(DrawPosTextH) \
45 M(DrawRRect) \
46 M(DrawRect) \
47 M(DrawSprite) \
48 M(DrawText) \
49 M(DrawTextOnPath) \
50 M(DrawVertices) \
51 M(PushCull) \
52 M(PopCull) \
53 M(PairedPushCull) /*From SkRecordAnnotateCullingPairs*/ \
54 M(BoundedDrawPosTextH) /*From SkRecordBoundDrawPosTextH*/
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000055
56// Defines SkRecords::Type, an enum of all record types.
57#define ENUM(T) T##_Type,
58enum Type { SK_RECORD_TYPES(ENUM) };
59#undef ENUM
60
61// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
62// These should be clearer when you look at their use below.
63#define RECORD0(T) \
64struct T { \
65 static const Type kType = T##_Type; \
66 T() {} \
67};
68
69// We try to be flexible about the types the constructors take. Instead of requring the exact type
70// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
71// work, allowing the caller to decide whether to pass by value or by const&.
72
73#define RECORD1(T, A, a) \
74struct T { \
75 static const Type kType = T##_Type; \
76 template <typename Z> \
77 T(Z a) : a(a) {} \
78 A a; \
79};
80
81#define RECORD2(T, A, a, B, b) \
82struct T { \
83 static const Type kType = T##_Type; \
84 template <typename Z, typename Y> \
85 T(Z a, Y b) : a(a), b(b) {} \
86 A a; B b; \
87};
88
89#define RECORD3(T, A, a, B, b, C, c) \
90struct T { \
91 static const Type kType = T##_Type; \
92 template <typename Z, typename Y, typename X> \
93 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
94 A a; B b; C c; \
95};
96
97#define RECORD4(T, A, a, B, b, C, c, D, d) \
98struct T { \
99 static const Type kType = T##_Type; \
100 template <typename Z, typename Y, typename X, typename W> \
101 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
102 A a; B b; C c; D d; \
103};
104
105#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
106struct T { \
107 static const Type kType = T##_Type; \
108 template <typename Z, typename Y, typename X, typename W, typename V> \
109 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
110 A a; B b; C c; D d; E e; \
111};
112
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000113#define ACT_AS_PTR(ptr) \
114 operator T*() { return ptr; } \
115 operator const T*() const { return ptr; } \
116 T* operator->() { return ptr; } \
117 const T* operator->() const { return ptr; }
118
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000119// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
120template <typename T>
121class Optional : SkNoncopyable {
122public:
123 Optional(T* ptr) : fPtr(ptr) {}
124 ~Optional() { if (fPtr) fPtr->~T(); }
125
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000126 ACT_AS_PTR(fPtr);
127private:
128 T* fPtr;
129};
130
131// Like Optional, but ptr must not be NULL.
132template <typename T>
133class Adopted : SkNoncopyable {
134public:
135 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); }
136 ~Adopted() { fPtr->~T(); }
137
138 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000139private:
140 T* fPtr;
141};
142
143// PODArray doesn't own the pointer's memory, and we assume the data is POD.
144template <typename T>
145class PODArray : SkNoncopyable {
146public:
147 PODArray(T* ptr) : fPtr(ptr) {}
148
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000149 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000150private:
151 T* fPtr;
152};
153
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000154// Like SkBitmap, but deep copies pixels if they're not immutable.
155// Using this, we guarantee the immutability of all bitmaps we record.
156class ImmutableBitmap {
157public:
158 explicit ImmutableBitmap(const SkBitmap& bitmap) {
159 if (bitmap.isImmutable()) {
160 fBitmap = bitmap;
161 } else {
162 bitmap.copyTo(&fBitmap);
163 }
164 fBitmap.setImmutable();
165 }
166
167 operator const SkBitmap& () const { return fBitmap; }
168
169private:
170 SkBitmap fBitmap;
171};
172
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000173RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000174
175RECORD0(Restore);
176RECORD1(Save, SkCanvas::SaveFlags, flags);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000177RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000178
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000179RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000180RECORD0(PopCull);
181
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000182RECORD1(Concat, SkMatrix, matrix);
183RECORD1(SetMatrix, SkMatrix, matrix);
184
185RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA);
186RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
187RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA);
188RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op);
189
190RECORD1(Clear, SkColor, color);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000191RECORD4(DrawBitmap, ImmutableBitmap, bitmap,
192 SkScalar, left,
193 SkScalar, top,
194 Optional<SkPaint>, paint);
195RECORD3(DrawBitmapMatrix, ImmutableBitmap, bitmap, SkMatrix, matrix, Optional<SkPaint>, paint);
196RECORD4(DrawBitmapNine, ImmutableBitmap, bitmap,
197 SkIRect, center,
198 SkRect, dst,
199 Optional<SkPaint>, paint);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000200RECORD5(DrawBitmapRectToRect, ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000201 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000202 SkRect, dst,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000203 Optional<SkPaint>, paint,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000204 SkCanvas::DrawBitmapRectFlags, flags);
205RECORD3(DrawDRRect, SkRRect, outer, SkRRect, inner, SkPaint, paint);
206RECORD2(DrawOval, SkRect, oval, SkPaint, paint);
207RECORD1(DrawPaint, SkPaint, paint);
208RECORD2(DrawPath, SkPath, path, SkPaint, paint);
209RECORD4(DrawPoints, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts, SkPaint, paint);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000210RECORD4(DrawPosText, PODArray<char>, text,
211 size_t, byteLength,
212 PODArray<SkPoint>, pos,
213 SkPaint, paint);
214RECORD5(DrawPosTextH, PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000215 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000216 PODArray<SkScalar>, xpos,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000217 SkScalar, y,
218 SkPaint, paint);
219RECORD2(DrawRRect, SkRRect, rrect, SkPaint, paint);
220RECORD2(DrawRect, SkRect, rect, SkPaint, paint);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000221RECORD4(DrawSprite, ImmutableBitmap, bitmap, int, left, int, top, Optional<SkPaint>, paint);
222RECORD5(DrawText, PODArray<char>, text,
223 size_t, byteLength,
224 SkScalar, x,
225 SkScalar, y,
226 SkPaint, paint);
227RECORD5(DrawTextOnPath, PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000228 size_t, byteLength,
229 SkPath, path,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000230 Optional<SkMatrix>, matrix,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000231 SkPaint, paint);
232
233// This guy is so ugly we just write it manually.
234struct DrawVertices {
235 static const Type kType = DrawVertices_Type;
236
237 DrawVertices(SkCanvas::VertexMode vmode,
238 int vertexCount,
239 SkPoint* vertices,
240 SkPoint* texs,
241 SkColor* colors,
242 SkXfermode* xmode,
243 uint16_t* indices,
244 int indexCount,
245 const SkPaint& paint)
246 : vmode(vmode)
247 , vertexCount(vertexCount)
248 , vertices(vertices)
249 , texs(texs)
250 , colors(colors)
251 , xmode(SkSafeRef(xmode))
252 , indices(indices)
253 , indexCount(indexCount)
254 , paint(paint) {}
255
256 SkCanvas::VertexMode vmode;
257 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000258 PODArray<SkPoint> vertices;
259 PODArray<SkPoint> texs;
260 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000261 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000262 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000263 int indexCount;
264 SkPaint paint;
265};
266
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000267// Records added by optimizations.
268RECORD2(PairedPushCull, Adopted<PushCull>, base, unsigned, skip);
269RECORD3(BoundedDrawPosTextH, Adopted<DrawPosTextH>, base, SkScalar, minY, SkScalar, maxY);
270
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000271#undef RECORD0
272#undef RECORD1
273#undef RECORD2
274#undef RECORD3
275#undef RECORD4
276#undef RECORD5
277
278} // namespace SkRecords
279
280#endif//SkRecords_DEFINED