blob: 02cfc64032cabc501b1adb8a6bce23c1f8874488 [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.org37f6e622014-05-07 22:59:38 +000022//
23// Order doesn't technically matter here, but the compiler can generally generate better code if
24// 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 +000025#define SK_RECORD_TYPES(M) \
26 M(NoOp) \
27 M(Restore) \
28 M(Save) \
29 M(SaveLayer) \
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000030 M(PushCull) \
31 M(PopCull) \
32 M(PairedPushCull) /*From SkRecordAnnotateCullingPairs*/ \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000033 M(Concat) \
34 M(SetMatrix) \
35 M(ClipPath) \
36 M(ClipRRect) \
37 M(ClipRect) \
38 M(ClipRegion) \
39 M(Clear) \
40 M(DrawBitmap) \
41 M(DrawBitmapMatrix) \
42 M(DrawBitmapNine) \
43 M(DrawBitmapRectToRect) \
44 M(DrawDRRect) \
45 M(DrawOval) \
46 M(DrawPaint) \
47 M(DrawPath) \
48 M(DrawPoints) \
49 M(DrawPosText) \
50 M(DrawPosTextH) \
51 M(DrawRRect) \
52 M(DrawRect) \
53 M(DrawSprite) \
54 M(DrawText) \
55 M(DrawTextOnPath) \
56 M(DrawVertices) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000057 M(BoundedDrawPosTextH) /*From SkRecordBoundDrawPosTextH*/
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000058
59// Defines SkRecords::Type, an enum of all record types.
60#define ENUM(T) T##_Type,
61enum Type { SK_RECORD_TYPES(ENUM) };
62#undef ENUM
63
64// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
65// These should be clearer when you look at their use below.
66#define RECORD0(T) \
67struct T { \
68 static const Type kType = T##_Type; \
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000069};
70
71// We try to be flexible about the types the constructors take. Instead of requring the exact type
72// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
73// work, allowing the caller to decide whether to pass by value or by const&.
74
75#define RECORD1(T, A, a) \
76struct T { \
77 static const Type kType = T##_Type; \
78 template <typename Z> \
79 T(Z a) : a(a) {} \
80 A a; \
81};
82
83#define RECORD2(T, A, a, B, b) \
84struct T { \
85 static const Type kType = T##_Type; \
86 template <typename Z, typename Y> \
87 T(Z a, Y b) : a(a), b(b) {} \
88 A a; B b; \
89};
90
91#define RECORD3(T, A, a, B, b, C, c) \
92struct T { \
93 static const Type kType = T##_Type; \
94 template <typename Z, typename Y, typename X> \
95 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
96 A a; B b; C c; \
97};
98
99#define RECORD4(T, A, a, B, b, C, c, D, d) \
100struct T { \
101 static const Type kType = T##_Type; \
102 template <typename Z, typename Y, typename X, typename W> \
103 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
104 A a; B b; C c; D d; \
105};
106
107#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
108struct T { \
109 static const Type kType = T##_Type; \
110 template <typename Z, typename Y, typename X, typename W, typename V> \
111 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
112 A a; B b; C c; D d; E e; \
113};
114
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000115#define ACT_AS_PTR(ptr) \
116 operator T*() { return ptr; } \
117 operator const T*() const { return ptr; } \
118 T* operator->() { return ptr; } \
119 const T* operator->() const { return ptr; }
120
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000121// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
122template <typename T>
123class Optional : SkNoncopyable {
124public:
125 Optional(T* ptr) : fPtr(ptr) {}
126 ~Optional() { if (fPtr) fPtr->~T(); }
127
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000128 ACT_AS_PTR(fPtr);
129private:
130 T* fPtr;
131};
132
133// Like Optional, but ptr must not be NULL.
134template <typename T>
135class Adopted : SkNoncopyable {
136public:
137 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); }
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000138 Adopted(Adopted* source) {
139 // Transfer ownership from source to this.
140 fPtr = source->fPtr;
141 source->fPtr = NULL;
142 }
143 ~Adopted() { if (fPtr) fPtr->~T(); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000144
145 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000146private:
147 T* fPtr;
148};
149
150// PODArray doesn't own the pointer's memory, and we assume the data is POD.
151template <typename T>
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000152class PODArray {
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000153public:
154 PODArray(T* ptr) : fPtr(ptr) {}
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000155 // Default copy and assign.
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000156
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000157 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000158private:
159 T* fPtr;
160};
161
commit-bot@chromium.org16307bd2014-04-22 18:32:58 +0000162#undef ACT_AS_PTR
163
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000164// Like SkBitmap, but deep copies pixels if they're not immutable.
165// Using this, we guarantee the immutability of all bitmaps we record.
166class ImmutableBitmap {
167public:
168 explicit ImmutableBitmap(const SkBitmap& bitmap) {
169 if (bitmap.isImmutable()) {
170 fBitmap = bitmap;
171 } else {
172 bitmap.copyTo(&fBitmap);
173 }
174 fBitmap.setImmutable();
175 }
176
177 operator const SkBitmap& () const { return fBitmap; }
178
179private:
180 SkBitmap fBitmap;
181};
182
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000183RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000184
185RECORD0(Restore);
Florin Malita5f6102d2014-06-30 10:13:28 -0400186RECORD0(Save);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000187RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000188
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000189RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000190RECORD0(PopCull);
191
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000192RECORD1(Concat, SkMatrix, matrix);
193RECORD1(SetMatrix, SkMatrix, matrix);
194
195RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA);
196RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
197RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA);
198RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op);
199
200RECORD1(Clear, SkColor, color);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000201// While not strictly required, if you have an SkPaint, it's fastest to put it first.
202RECORD4(DrawBitmap, Optional<SkPaint>, paint,
203 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000204 SkScalar, left,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000205 SkScalar, top);
206RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix);
207RECORD4(DrawBitmapNine, Optional<SkPaint>, paint,
208 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000209 SkIRect, center,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000210 SkRect, dst);
211RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint,
212 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000213 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000214 SkRect, dst,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000215 SkCanvas::DrawBitmapRectFlags, flags);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000216RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner);
217RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000218RECORD1(DrawPaint, SkPaint, paint);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000219RECORD2(DrawPath, SkPaint, paint, SkPath, path);
220RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
221RECORD4(DrawPosText, SkPaint, paint,
222 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000223 size_t, byteLength,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000224 PODArray<SkPoint>, pos);
225RECORD5(DrawPosTextH, SkPaint, paint,
226 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000227 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000228 PODArray<SkScalar>, xpos,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000229 SkScalar, y);
230RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect);
231RECORD2(DrawRect, SkPaint, paint, SkRect, rect);
232RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top);
233RECORD5(DrawText, SkPaint, paint,
234 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000235 size_t, byteLength,
236 SkScalar, x,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000237 SkScalar, y);
238RECORD5(DrawTextOnPath, SkPaint, paint,
239 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000240 size_t, byteLength,
241 SkPath, path,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000242 Optional<SkMatrix>, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000243
244// This guy is so ugly we just write it manually.
245struct DrawVertices {
246 static const Type kType = DrawVertices_Type;
247
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000248 DrawVertices(const SkPaint& paint,
249 SkCanvas::VertexMode vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000250 int vertexCount,
251 SkPoint* vertices,
252 SkPoint* texs,
253 SkColor* colors,
254 SkXfermode* xmode,
255 uint16_t* indices,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000256 int indexCount)
257 : paint(paint)
258 , vmode(vmode)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000259 , vertexCount(vertexCount)
260 , vertices(vertices)
261 , texs(texs)
262 , colors(colors)
263 , xmode(SkSafeRef(xmode))
264 , indices(indices)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000265 , indexCount(indexCount) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000266
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000267 SkPaint paint;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000268 SkCanvas::VertexMode vmode;
269 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000270 PODArray<SkPoint> vertices;
271 PODArray<SkPoint> texs;
272 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000273 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000274 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000275 int indexCount;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000276};
277
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000278// Records added by optimizations.
279RECORD2(PairedPushCull, Adopted<PushCull>, base, unsigned, skip);
280RECORD3(BoundedDrawPosTextH, Adopted<DrawPosTextH>, base, SkScalar, minY, SkScalar, maxY);
281
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000282#undef RECORD0
283#undef RECORD1
284#undef RECORD2
285#undef RECORD3
286#undef RECORD4
287#undef RECORD5
288
289} // namespace SkRecords
290
291#endif//SkRecords_DEFINED