blob: f6292d419b67f3ee90edf2acc42b4610fd604e1e [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"
fmalita00d5c2c2014-08-21 08:53:26 -070013#include "SkTextBlob.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000014
15namespace SkRecords {
16
17// A list of all the types of canvas calls we can record.
18// Each of these is reified into a struct below.
19//
20// (We're using the macro-of-macro trick here to do several different things with the same list.)
21//
22// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords
23// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000024//
25// Order doesn't technically matter here, but the compiler can generally generate better code if
26// 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 +000027#define SK_RECORD_TYPES(M) \
28 M(NoOp) \
29 M(Restore) \
30 M(Save) \
31 M(SaveLayer) \
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000032 M(PushCull) \
33 M(PopCull) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000034 M(SetMatrix) \
35 M(ClipPath) \
36 M(ClipRRect) \
37 M(ClipRect) \
38 M(ClipRegion) \
39 M(Clear) \
mtklein5f0e8222014-08-22 11:44:26 -070040 M(BeginCommentGroup) \
41 M(AddComment) \
42 M(EndCommentGroup) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000043 M(DrawBitmap) \
44 M(DrawBitmapMatrix) \
45 M(DrawBitmapNine) \
46 M(DrawBitmapRectToRect) \
piotaixr65151752014-10-16 11:58:39 -070047 M(DrawImage) \
48 M(DrawImageRect) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000049 M(DrawDRRect) \
50 M(DrawOval) \
51 M(DrawPaint) \
52 M(DrawPath) \
dandov963137b2014-08-07 07:49:53 -070053 M(DrawPatch) \
reed2347b622014-08-07 12:19:50 -070054 M(DrawPicture) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000055 M(DrawPoints) \
56 M(DrawPosText) \
57 M(DrawPosTextH) \
mtkleinc551d9f2014-08-20 08:09:46 -070058 M(DrawText) \
59 M(DrawTextOnPath) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000060 M(DrawRRect) \
61 M(DrawRect) \
62 M(DrawSprite) \
mtklein29dfaa82014-09-04 14:12:44 -070063 M(DrawTextBlob) \
64 M(DrawData) \
mtkleinf4078ad2014-08-08 10:05:19 -070065 M(DrawVertices)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000066
67// Defines SkRecords::Type, an enum of all record types.
68#define ENUM(T) T##_Type,
69enum Type { SK_RECORD_TYPES(ENUM) };
70#undef ENUM
71
72// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
73// These should be clearer when you look at their use below.
74#define RECORD0(T) \
75struct T { \
76 static const Type kType = T##_Type; \
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000077};
78
79// We try to be flexible about the types the constructors take. Instead of requring the exact type
80// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
81// work, allowing the caller to decide whether to pass by value or by const&.
82
83#define RECORD1(T, A, a) \
84struct T { \
85 static const Type kType = T##_Type; \
86 template <typename Z> \
87 T(Z a) : a(a) {} \
88 A a; \
89};
90
91#define RECORD2(T, A, a, B, b) \
92struct T { \
93 static const Type kType = T##_Type; \
94 template <typename Z, typename Y> \
95 T(Z a, Y b) : a(a), b(b) {} \
96 A a; B b; \
97};
98
99#define RECORD3(T, A, a, B, b, C, c) \
100struct T { \
101 static const Type kType = T##_Type; \
102 template <typename Z, typename Y, typename X> \
103 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
104 A a; B b; C c; \
105};
106
107#define RECORD4(T, A, a, B, b, C, c, D, d) \
108struct T { \
109 static const Type kType = T##_Type; \
110 template <typename Z, typename Y, typename X, typename W> \
111 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
112 A a; B b; C c; D d; \
113};
114
115#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
116struct T { \
117 static const Type kType = T##_Type; \
118 template <typename Z, typename Y, typename X, typename W, typename V> \
119 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
120 A a; B b; C c; D d; E e; \
121};
122
mtklein9b222a52014-09-18 11:16:31 -0700123#define ACT_AS_PTR(ptr) \
124 operator T*() const { return ptr; } \
125 T* operator->() const { return ptr; }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000126
mtklein53fecfb2014-08-21 09:11:37 -0700127template <typename T>
128class RefBox : SkNoncopyable {
129public:
mtklein9b222a52014-09-18 11:16:31 -0700130 RefBox(T* obj) : fObj(SkSafeRef(obj)) {}
131 ~RefBox() { SkSafeUnref(fObj); }
mtklein53fecfb2014-08-21 09:11:37 -0700132
133 ACT_AS_PTR(fObj);
134
135private:
136 T* fObj;
137};
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.
mtkleinee369522014-08-27 13:02:24 -0700184class ImmutableBitmap : SkNoncopyable {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000185public:
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
mtkleina723b572014-08-15 11:49:49 -0700203RECORD2(Restore, SkIRect, devBounds, SkMatrix, matrix);
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(SetMatrix, SkMatrix, matrix);
211
mtkleina723b572014-08-15 11:49:49 -0700212RECORD4(ClipPath, SkIRect, devBounds, SkPath, path, SkRegion::Op, op, bool, doAA);
213RECORD4(ClipRRect, SkIRect, devBounds, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
214RECORD4(ClipRect, SkIRect, devBounds, SkRect, rect, SkRegion::Op, op, bool, doAA);
215RECORD3(ClipRegion, SkIRect, devBounds, SkRegion, region, SkRegion::Op, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000216
217RECORD1(Clear, SkColor, color);
mtklein5f0e8222014-08-22 11:44:26 -0700218
219RECORD1(BeginCommentGroup, PODArray<char>, description);
220RECORD2(AddComment, PODArray<char>, key, PODArray<char>, value);
221RECORD0(EndCommentGroup);
222
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000223// While not strictly required, if you have an SkPaint, it's fastest to put it first.
224RECORD4(DrawBitmap, Optional<SkPaint>, paint,
225 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000226 SkScalar, left,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000227 SkScalar, top);
228RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix);
229RECORD4(DrawBitmapNine, Optional<SkPaint>, paint,
230 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000231 SkIRect, center,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000232 SkRect, dst);
233RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint,
234 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000235 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236 SkRect, dst,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000237 SkCanvas::DrawBitmapRectFlags, flags);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000238RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner);
piotaixr65151752014-10-16 11:58:39 -0700239RECORD4(DrawImage, Optional<SkPaint>, paint,
240 RefBox<const SkImage>, image,
241 SkScalar, left,
242 SkScalar, top);
243RECORD4(DrawImageRect, Optional<SkPaint>, paint,
244 RefBox<const SkImage>, image,
245 Optional<SkRect>, src,
246 SkRect, dst);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000247RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000248RECORD1(DrawPaint, SkPaint, paint);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000249RECORD2(DrawPath, SkPaint, paint, SkPath, path);
mtklein53fecfb2014-08-21 09:11:37 -0700250RECORD3(DrawPicture, Optional<SkPaint>, paint,
251 RefBox<const SkPicture>, picture,
252 Optional<SkMatrix>, matrix);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000253RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
254RECORD4(DrawPosText, SkPaint, paint,
255 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000256 size_t, byteLength,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000257 PODArray<SkPoint>, pos);
258RECORD5(DrawPosTextH, SkPaint, paint,
259 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000260 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000261 PODArray<SkScalar>, xpos,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000262 SkScalar, y);
263RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect);
264RECORD2(DrawRect, SkPaint, paint, SkRect, rect);
265RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top);
266RECORD5(DrawText, SkPaint, paint,
267 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000268 size_t, byteLength,
269 SkScalar, x,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000270 SkScalar, y);
fmalita00d5c2c2014-08-21 08:53:26 -0700271RECORD4(DrawTextBlob, SkPaint, paint,
mtklein53fecfb2014-08-21 09:11:37 -0700272 RefBox<const SkTextBlob>, blob,
fmalita00d5c2c2014-08-21 08:53:26 -0700273 SkScalar, x,
274 SkScalar, y);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000275RECORD5(DrawTextOnPath, SkPaint, paint,
276 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000277 size_t, byteLength,
278 SkPath, path,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000279 Optional<SkMatrix>, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000280
mtklein29dfaa82014-09-04 14:12:44 -0700281RECORD2(DrawData, PODArray<char>, data, size_t, length);
282
mtklein9b222a52014-09-18 11:16:31 -0700283RECORD5(DrawPatch, SkPaint, paint,
284 PODArray<SkPoint>, cubics,
285 PODArray<SkColor>, colors,
286 PODArray<SkPoint>, texCoords,
287 RefBox<SkXfermode>, xmode);
288
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000289// This guy is so ugly we just write it manually.
290struct DrawVertices {
291 static const Type kType = DrawVertices_Type;
292
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000293 DrawVertices(const SkPaint& paint,
294 SkCanvas::VertexMode vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000295 int vertexCount,
296 SkPoint* vertices,
297 SkPoint* texs,
298 SkColor* colors,
299 SkXfermode* xmode,
300 uint16_t* indices,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000301 int indexCount)
302 : paint(paint)
303 , vmode(vmode)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000304 , vertexCount(vertexCount)
305 , vertices(vertices)
306 , texs(texs)
307 , colors(colors)
308 , xmode(SkSafeRef(xmode))
309 , indices(indices)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000310 , indexCount(indexCount) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000311
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000312 SkPaint paint;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000313 SkCanvas::VertexMode vmode;
314 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000315 PODArray<SkPoint> vertices;
316 PODArray<SkPoint> texs;
317 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000318 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000319 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000320 int indexCount;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000321};
mtklein6cfa73a2014-08-13 13:33:49 -0700322
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000323#undef RECORD0
324#undef RECORD1
325#undef RECORD2
326#undef RECORD3
327#undef RECORD4
328#undef RECORD5
329
330} // namespace SkRecords
331
332#endif//SkRecords_DEFINED