blob: 347bc36276d055e313b06331f2adfb3348c6b40a [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"
reed2347b622014-08-07 12:19:50 -070012#include "SkPicture.h"
13
14class SkPictureBox {
15public:
16 SkPictureBox(const SkPicture* obj) : fObj(SkRef(obj)) {}
17 SkPictureBox(const SkPictureBox& src) : fObj(SkRef(src.fObj)) {}
18 ~SkPictureBox() { fObj->unref(); }
19
20 SkPictureBox& operator=(const SkPictureBox& src) {
21 SkRefCnt_SafeAssign(fObj, src.fObj);
22 return *this;
23 }
24
25 operator const SkPicture*() const { return fObj; }
26
27private:
28 const SkPicture* fObj;
29};
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000030
31namespace SkRecords {
32
33// A list of all the types of canvas calls we can record.
34// Each of these is reified into a struct below.
35//
36// (We're using the macro-of-macro trick here to do several different things with the same list.)
37//
38// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
39// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000040//
41// Order doesn't technically matter here, but the compiler can generally generate better code if
42// 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 +000043#define SK_RECORD_TYPES(M) \
44 M(NoOp) \
45 M(Restore) \
46 M(Save) \
47 M(SaveLayer) \
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000048 M(PushCull) \
49 M(PopCull) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000050 M(Concat) \
51 M(SetMatrix) \
52 M(ClipPath) \
53 M(ClipRRect) \
54 M(ClipRect) \
55 M(ClipRegion) \
56 M(Clear) \
57 M(DrawBitmap) \
58 M(DrawBitmapMatrix) \
59 M(DrawBitmapNine) \
60 M(DrawBitmapRectToRect) \
61 M(DrawDRRect) \
62 M(DrawOval) \
63 M(DrawPaint) \
64 M(DrawPath) \
dandov963137b2014-08-07 07:49:53 -070065 M(DrawPatch) \
reed2347b622014-08-07 12:19:50 -070066 M(DrawPicture) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000067 M(DrawPoints) \
68 M(DrawPosText) \
69 M(DrawPosTextH) \
70 M(DrawRRect) \
71 M(DrawRect) \
72 M(DrawSprite) \
73 M(DrawText) \
74 M(DrawTextOnPath) \
mtkleinf4078ad2014-08-08 10:05:19 -070075 M(DrawVertices)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000076
77// Defines SkRecords::Type, an enum of all record types.
78#define ENUM(T) T##_Type,
79enum Type { SK_RECORD_TYPES(ENUM) };
80#undef ENUM
81
82// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
83// These should be clearer when you look at their use below.
84#define RECORD0(T) \
85struct T { \
86 static const Type kType = T##_Type; \
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000087};
88
89// We try to be flexible about the types the constructors take. Instead of requring the exact type
90// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
91// work, allowing the caller to decide whether to pass by value or by const&.
92
93#define RECORD1(T, A, a) \
94struct T { \
95 static const Type kType = T##_Type; \
96 template <typename Z> \
97 T(Z a) : a(a) {} \
98 A a; \
99};
100
101#define RECORD2(T, A, a, B, b) \
102struct T { \
103 static const Type kType = T##_Type; \
104 template <typename Z, typename Y> \
105 T(Z a, Y b) : a(a), b(b) {} \
106 A a; B b; \
107};
108
109#define RECORD3(T, A, a, B, b, C, c) \
110struct T { \
111 static const Type kType = T##_Type; \
112 template <typename Z, typename Y, typename X> \
113 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
114 A a; B b; C c; \
115};
116
117#define RECORD4(T, A, a, B, b, C, c, D, d) \
118struct T { \
119 static const Type kType = T##_Type; \
120 template <typename Z, typename Y, typename X, typename W> \
121 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
122 A a; B b; C c; D d; \
123};
124
125#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
126struct T { \
127 static const Type kType = T##_Type; \
128 template <typename Z, typename Y, typename X, typename W, typename V> \
129 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
130 A a; B b; C c; D d; E e; \
131};
132
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000133#define ACT_AS_PTR(ptr) \
134 operator T*() { return ptr; } \
135 operator const T*() const { return ptr; } \
136 T* operator->() { return ptr; } \
137 const T* operator->() const { return ptr; }
138
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000139// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
140template <typename T>
141class Optional : SkNoncopyable {
142public:
143 Optional(T* ptr) : fPtr(ptr) {}
144 ~Optional() { if (fPtr) fPtr->~T(); }
145
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000146 ACT_AS_PTR(fPtr);
147private:
148 T* fPtr;
149};
150
151// Like Optional, but ptr must not be NULL.
152template <typename T>
153class Adopted : SkNoncopyable {
154public:
155 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); }
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000156 Adopted(Adopted* source) {
157 // Transfer ownership from source to this.
158 fPtr = source->fPtr;
159 source->fPtr = NULL;
160 }
161 ~Adopted() { if (fPtr) fPtr->~T(); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000162
163 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000164private:
165 T* fPtr;
166};
167
168// PODArray doesn't own the pointer's memory, and we assume the data is POD.
169template <typename T>
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000170class PODArray {
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000171public:
172 PODArray(T* ptr) : fPtr(ptr) {}
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000173 // Default copy and assign.
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000174
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000175 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000176private:
177 T* fPtr;
178};
179
commit-bot@chromium.org16307bd2014-04-22 18:32:58 +0000180#undef ACT_AS_PTR
181
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000182// Like SkBitmap, but deep copies pixels if they're not immutable.
183// Using this, we guarantee the immutability of all bitmaps we record.
184class ImmutableBitmap {
185public:
186 explicit ImmutableBitmap(const SkBitmap& bitmap) {
187 if (bitmap.isImmutable()) {
188 fBitmap = bitmap;
189 } else {
190 bitmap.copyTo(&fBitmap);
191 }
192 fBitmap.setImmutable();
193 }
194
195 operator const SkBitmap& () const { return fBitmap; }
196
197private:
198 SkBitmap fBitmap;
199};
200
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000201RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000202
203RECORD0(Restore);
Florin Malita5f6102d2014-06-30 10:13:28 -0400204RECORD0(Save);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000205RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000206
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000207RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000208RECORD0(PopCull);
209
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000210RECORD1(Concat, SkMatrix, matrix);
211RECORD1(SetMatrix, SkMatrix, matrix);
212
213RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA);
214RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
215RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA);
216RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op);
217
218RECORD1(Clear, SkColor, color);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000219// While not strictly required, if you have an SkPaint, it's fastest to put it first.
220RECORD4(DrawBitmap, Optional<SkPaint>, paint,
221 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000222 SkScalar, left,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000223 SkScalar, top);
224RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix);
225RECORD4(DrawBitmapNine, Optional<SkPaint>, paint,
226 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000227 SkIRect, center,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000228 SkRect, dst);
229RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint,
230 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000231 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000232 SkRect, dst,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000233 SkCanvas::DrawBitmapRectFlags, flags);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000234RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner);
235RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236RECORD1(DrawPaint, SkPaint, paint);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000237RECORD2(DrawPath, SkPaint, paint, SkPath, path);
dandov963137b2014-08-07 07:49:53 -0700238RECORD2(DrawPatch, SkPaint, paint, SkPatch, patch);
reed2347b622014-08-07 12:19:50 -0700239RECORD1(DrawPicture, SkPictureBox, picture);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000240RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
241RECORD4(DrawPosText, SkPaint, paint,
242 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000243 size_t, byteLength,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000244 PODArray<SkPoint>, pos);
245RECORD5(DrawPosTextH, SkPaint, paint,
246 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000247 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000248 PODArray<SkScalar>, xpos,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000249 SkScalar, y);
250RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect);
251RECORD2(DrawRect, SkPaint, paint, SkRect, rect);
252RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top);
253RECORD5(DrawText, SkPaint, paint,
254 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000255 size_t, byteLength,
256 SkScalar, x,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000257 SkScalar, y);
258RECORD5(DrawTextOnPath, SkPaint, paint,
259 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000260 size_t, byteLength,
261 SkPath, path,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000262 Optional<SkMatrix>, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000263
264// This guy is so ugly we just write it manually.
265struct DrawVertices {
266 static const Type kType = DrawVertices_Type;
267
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000268 DrawVertices(const SkPaint& paint,
269 SkCanvas::VertexMode vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000270 int vertexCount,
271 SkPoint* vertices,
272 SkPoint* texs,
273 SkColor* colors,
274 SkXfermode* xmode,
275 uint16_t* indices,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000276 int indexCount)
277 : paint(paint)
278 , vmode(vmode)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000279 , vertexCount(vertexCount)
280 , vertices(vertices)
281 , texs(texs)
282 , colors(colors)
283 , xmode(SkSafeRef(xmode))
284 , indices(indices)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000285 , indexCount(indexCount) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000286
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000287 SkPaint paint;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000288 SkCanvas::VertexMode vmode;
289 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000290 PODArray<SkPoint> vertices;
291 PODArray<SkPoint> texs;
292 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000293 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000294 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000295 int indexCount;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000296};
297
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000298#undef RECORD0
299#undef RECORD1
300#undef RECORD2
301#undef RECORD3
302#undef RECORD4
303#undef RECORD5
304
305} // namespace SkRecords
306
307#endif//SkRecords_DEFINED