blob: 8b96e8d91387873488ca468c8fd28921e1e867b6 [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.org16307bd2014-04-22 18:32:58 +0000154#undef ACT_AS_PTR
155
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000156// Like SkBitmap, but deep copies pixels if they're not immutable.
157// Using this, we guarantee the immutability of all bitmaps we record.
158class ImmutableBitmap {
159public:
160 explicit ImmutableBitmap(const SkBitmap& bitmap) {
161 if (bitmap.isImmutable()) {
162 fBitmap = bitmap;
163 } else {
164 bitmap.copyTo(&fBitmap);
165 }
166 fBitmap.setImmutable();
167 }
168
169 operator const SkBitmap& () const { return fBitmap; }
170
171private:
172 SkBitmap fBitmap;
173};
174
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000175RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000176
177RECORD0(Restore);
178RECORD1(Save, SkCanvas::SaveFlags, flags);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000179RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000180
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000181RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000182RECORD0(PopCull);
183
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000184RECORD1(Concat, SkMatrix, matrix);
185RECORD1(SetMatrix, SkMatrix, matrix);
186
187RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA);
188RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
189RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA);
190RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op);
191
192RECORD1(Clear, SkColor, color);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000193RECORD4(DrawBitmap, ImmutableBitmap, bitmap,
194 SkScalar, left,
195 SkScalar, top,
196 Optional<SkPaint>, paint);
197RECORD3(DrawBitmapMatrix, ImmutableBitmap, bitmap, SkMatrix, matrix, Optional<SkPaint>, paint);
198RECORD4(DrawBitmapNine, ImmutableBitmap, bitmap,
199 SkIRect, center,
200 SkRect, dst,
201 Optional<SkPaint>, paint);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000202RECORD5(DrawBitmapRectToRect, ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000203 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000204 SkRect, dst,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000205 Optional<SkPaint>, paint,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206 SkCanvas::DrawBitmapRectFlags, flags);
207RECORD3(DrawDRRect, SkRRect, outer, SkRRect, inner, SkPaint, paint);
208RECORD2(DrawOval, SkRect, oval, SkPaint, paint);
209RECORD1(DrawPaint, SkPaint, paint);
210RECORD2(DrawPath, SkPath, path, SkPaint, paint);
211RECORD4(DrawPoints, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts, SkPaint, paint);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000212RECORD4(DrawPosText, PODArray<char>, text,
213 size_t, byteLength,
214 PODArray<SkPoint>, pos,
215 SkPaint, paint);
216RECORD5(DrawPosTextH, PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000217 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000218 PODArray<SkScalar>, xpos,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000219 SkScalar, y,
220 SkPaint, paint);
221RECORD2(DrawRRect, SkRRect, rrect, SkPaint, paint);
222RECORD2(DrawRect, SkRect, rect, SkPaint, paint);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000223RECORD4(DrawSprite, ImmutableBitmap, bitmap, int, left, int, top, Optional<SkPaint>, paint);
224RECORD5(DrawText, PODArray<char>, text,
225 size_t, byteLength,
226 SkScalar, x,
227 SkScalar, y,
228 SkPaint, paint);
229RECORD5(DrawTextOnPath, PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000230 size_t, byteLength,
231 SkPath, path,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000232 Optional<SkMatrix>, matrix,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000233 SkPaint, paint);
234
235// This guy is so ugly we just write it manually.
236struct DrawVertices {
237 static const Type kType = DrawVertices_Type;
238
239 DrawVertices(SkCanvas::VertexMode vmode,
240 int vertexCount,
241 SkPoint* vertices,
242 SkPoint* texs,
243 SkColor* colors,
244 SkXfermode* xmode,
245 uint16_t* indices,
246 int indexCount,
247 const SkPaint& paint)
248 : vmode(vmode)
249 , vertexCount(vertexCount)
250 , vertices(vertices)
251 , texs(texs)
252 , colors(colors)
253 , xmode(SkSafeRef(xmode))
254 , indices(indices)
255 , indexCount(indexCount)
256 , paint(paint) {}
257
258 SkCanvas::VertexMode vmode;
259 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000260 PODArray<SkPoint> vertices;
261 PODArray<SkPoint> texs;
262 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000263 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000264 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000265 int indexCount;
266 SkPaint paint;
267};
268
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000269// Records added by optimizations.
270RECORD2(PairedPushCull, Adopted<PushCull>, base, unsigned, skip);
271RECORD3(BoundedDrawPosTextH, Adopted<DrawPosTextH>, base, SkScalar, minY, SkScalar, maxY);
272
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000273#undef RECORD0
274#undef RECORD1
275#undef RECORD2
276#undef RECORD3
277#undef RECORD4
278#undef RECORD5
279
280} // namespace SkRecords
281
282#endif//SkRecords_DEFINED