blob: f216897dca30e016af1339415ae8ca07df554204 [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) \
47 M(DrawDRRect) \
48 M(DrawOval) \
49 M(DrawPaint) \
50 M(DrawPath) \
dandov963137b2014-08-07 07:49:53 -070051 M(DrawPatch) \
reed2347b622014-08-07 12:19:50 -070052 M(DrawPicture) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000053 M(DrawPoints) \
54 M(DrawPosText) \
55 M(DrawPosTextH) \
mtkleinc551d9f2014-08-20 08:09:46 -070056 M(DrawText) \
57 M(DrawTextOnPath) \
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000058 M(DrawRRect) \
59 M(DrawRect) \
60 M(DrawSprite) \
fmalita00d5c2c2014-08-21 08:53:26 -070061 M(DrawTextBlob) \
mtkleinf4078ad2014-08-08 10:05:19 -070062 M(DrawVertices)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000063
64// Defines SkRecords::Type, an enum of all record types.
65#define ENUM(T) T##_Type,
66enum Type { SK_RECORD_TYPES(ENUM) };
67#undef ENUM
68
69// Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc.
70// These should be clearer when you look at their use below.
71#define RECORD0(T) \
72struct T { \
73 static const Type kType = T##_Type; \
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000074};
75
76// We try to be flexible about the types the constructors take. Instead of requring the exact type
77// A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to
78// work, allowing the caller to decide whether to pass by value or by const&.
79
80#define RECORD1(T, A, a) \
81struct T { \
82 static const Type kType = T##_Type; \
83 template <typename Z> \
84 T(Z a) : a(a) {} \
85 A a; \
86};
87
88#define RECORD2(T, A, a, B, b) \
89struct T { \
90 static const Type kType = T##_Type; \
91 template <typename Z, typename Y> \
92 T(Z a, Y b) : a(a), b(b) {} \
93 A a; B b; \
94};
95
96#define RECORD3(T, A, a, B, b, C, c) \
97struct T { \
98 static const Type kType = T##_Type; \
99 template <typename Z, typename Y, typename X> \
100 T(Z a, Y b, X c) : a(a), b(b), c(c) {} \
101 A a; B b; C c; \
102};
103
104#define RECORD4(T, A, a, B, b, C, c, D, d) \
105struct T { \
106 static const Type kType = T##_Type; \
107 template <typename Z, typename Y, typename X, typename W> \
108 T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \
109 A a; B b; C c; D d; \
110};
111
112#define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \
113struct T { \
114 static const Type kType = T##_Type; \
115 template <typename Z, typename Y, typename X, typename W, typename V> \
116 T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \
117 A a; B b; C c; D d; E e; \
118};
119
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000120#define ACT_AS_PTR(ptr) \
121 operator T*() { return ptr; } \
122 operator const T*() const { return ptr; } \
123 T* operator->() { return ptr; } \
124 const T* operator->() const { return ptr; }
125
mtklein53fecfb2014-08-21 09:11:37 -0700126template <typename T>
127class RefBox : SkNoncopyable {
128public:
129 RefBox(T* obj) : fObj(SkRef(obj)) {}
130 ~RefBox() { fObj->unref(); }
131
132 ACT_AS_PTR(fObj);
133
134private:
135 T* fObj;
136};
137
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000138// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
139template <typename T>
140class Optional : SkNoncopyable {
141public:
142 Optional(T* ptr) : fPtr(ptr) {}
143 ~Optional() { if (fPtr) fPtr->~T(); }
144
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000145 ACT_AS_PTR(fPtr);
146private:
147 T* fPtr;
148};
149
150// Like Optional, but ptr must not be NULL.
151template <typename T>
152class Adopted : SkNoncopyable {
153public:
154 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); }
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000155 Adopted(Adopted* source) {
156 // Transfer ownership from source to this.
157 fPtr = source->fPtr;
158 source->fPtr = NULL;
159 }
160 ~Adopted() { if (fPtr) fPtr->~T(); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000161
162 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000163private:
164 T* fPtr;
165};
166
167// PODArray doesn't own the pointer's memory, and we assume the data is POD.
168template <typename T>
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000169class PODArray {
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000170public:
171 PODArray(T* ptr) : fPtr(ptr) {}
commit-bot@chromium.orgf0ae5e42014-04-24 15:33:48 +0000172 // Default copy and assign.
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000173
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000174 ACT_AS_PTR(fPtr);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000175private:
176 T* fPtr;
177};
178
commit-bot@chromium.org16307bd2014-04-22 18:32:58 +0000179#undef ACT_AS_PTR
180
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000181// Like SkBitmap, but deep copies pixels if they're not immutable.
182// Using this, we guarantee the immutability of all bitmaps we record.
183class ImmutableBitmap {
184public:
185 explicit ImmutableBitmap(const SkBitmap& bitmap) {
186 if (bitmap.isImmutable()) {
187 fBitmap = bitmap;
188 } else {
189 bitmap.copyTo(&fBitmap);
190 }
191 fBitmap.setImmutable();
192 }
193
194 operator const SkBitmap& () const { return fBitmap; }
195
196private:
197 SkBitmap fBitmap;
198};
199
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000200RECORD0(NoOp);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000201
mtkleina723b572014-08-15 11:49:49 -0700202RECORD2(Restore, SkIRect, devBounds, SkMatrix, matrix);
Florin Malita5f6102d2014-06-30 10:13:28 -0400203RECORD0(Save);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000204RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000205
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000206RECORD1(PushCull, SkRect, rect);
commit-bot@chromium.org03a99b82014-04-08 15:17:17 +0000207RECORD0(PopCull);
208
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000209RECORD1(SetMatrix, SkMatrix, matrix);
210
mtkleina723b572014-08-15 11:49:49 -0700211RECORD4(ClipPath, SkIRect, devBounds, SkPath, path, SkRegion::Op, op, bool, doAA);
212RECORD4(ClipRRect, SkIRect, devBounds, SkRRect, rrect, SkRegion::Op, op, bool, doAA);
213RECORD4(ClipRect, SkIRect, devBounds, SkRect, rect, SkRegion::Op, op, bool, doAA);
214RECORD3(ClipRegion, SkIRect, devBounds, SkRegion, region, SkRegion::Op, op);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000215
216RECORD1(Clear, SkColor, color);
mtklein5f0e8222014-08-22 11:44:26 -0700217
218RECORD1(BeginCommentGroup, PODArray<char>, description);
219RECORD2(AddComment, PODArray<char>, key, PODArray<char>, value);
220RECORD0(EndCommentGroup);
221
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000222// While not strictly required, if you have an SkPaint, it's fastest to put it first.
223RECORD4(DrawBitmap, Optional<SkPaint>, paint,
224 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000225 SkScalar, left,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000226 SkScalar, top);
227RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix);
228RECORD4(DrawBitmapNine, Optional<SkPaint>, paint,
229 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000230 SkIRect, center,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000231 SkRect, dst);
232RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint,
233 ImmutableBitmap, bitmap,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000234 Optional<SkRect>, src,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000235 SkRect, dst,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000236 SkCanvas::DrawBitmapRectFlags, flags);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000237RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner);
238RECORD2(DrawOval, SkPaint, paint, SkRect, oval);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000239RECORD1(DrawPaint, SkPaint, paint);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000240RECORD2(DrawPath, SkPaint, paint, SkPath, path);
dandovb3c9d1c2014-08-12 08:34:29 -0700241//RECORD2(DrawPatch, SkPaint, paint, SkPatch, patch);
mtklein53fecfb2014-08-21 09:11:37 -0700242RECORD3(DrawPicture, Optional<SkPaint>, paint,
243 RefBox<const SkPicture>, picture,
244 Optional<SkMatrix>, matrix);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000245RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts);
246RECORD4(DrawPosText, SkPaint, paint,
247 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000248 size_t, byteLength,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000249 PODArray<SkPoint>, pos);
250RECORD5(DrawPosTextH, SkPaint, paint,
251 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000252 size_t, byteLength,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000253 PODArray<SkScalar>, xpos,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000254 SkScalar, y);
255RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect);
256RECORD2(DrawRect, SkPaint, paint, SkRect, rect);
257RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top);
258RECORD5(DrawText, SkPaint, paint,
259 PODArray<char>, text,
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000260 size_t, byteLength,
261 SkScalar, x,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000262 SkScalar, y);
fmalita00d5c2c2014-08-21 08:53:26 -0700263RECORD4(DrawTextBlob, SkPaint, paint,
mtklein53fecfb2014-08-21 09:11:37 -0700264 RefBox<const SkTextBlob>, blob,
fmalita00d5c2c2014-08-21 08:53:26 -0700265 SkScalar, x,
266 SkScalar, y);
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000267RECORD5(DrawTextOnPath, SkPaint, paint,
268 PODArray<char>, text,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000269 size_t, byteLength,
270 SkPath, path,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000271 Optional<SkMatrix>, matrix);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000272
273// This guy is so ugly we just write it manually.
274struct DrawVertices {
275 static const Type kType = DrawVertices_Type;
276
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000277 DrawVertices(const SkPaint& paint,
278 SkCanvas::VertexMode vmode,
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000279 int vertexCount,
280 SkPoint* vertices,
281 SkPoint* texs,
282 SkColor* colors,
283 SkXfermode* xmode,
284 uint16_t* indices,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000285 int indexCount)
286 : paint(paint)
287 , vmode(vmode)
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000288 , vertexCount(vertexCount)
289 , vertices(vertices)
290 , texs(texs)
291 , colors(colors)
292 , xmode(SkSafeRef(xmode))
293 , indices(indices)
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000294 , indexCount(indexCount) {}
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000295
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000296 SkPaint paint;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000297 SkCanvas::VertexMode vmode;
298 int vertexCount;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000299 PODArray<SkPoint> vertices;
300 PODArray<SkPoint> texs;
301 PODArray<SkColor> colors;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000302 SkAutoTUnref<SkXfermode> xmode;
commit-bot@chromium.org653d5182014-04-15 14:27:14 +0000303 PODArray<uint16_t> indices;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000304 int indexCount;
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000305};
mtklein6cfa73a2014-08-13 13:33:49 -0700306
dandovb3c9d1c2014-08-12 08:34:29 -0700307struct DrawPatch {
308 static const Type kType = DrawPatch_Type;
mtklein6cfa73a2014-08-13 13:33:49 -0700309
dandovb3c9d1c2014-08-12 08:34:29 -0700310 DrawPatch(const SkPaint& paint, SkPoint cubics[12], SkColor colors[4],
311 SkPoint texCoords[4], SkXfermode* xmode)
312 : paint(paint)
313 , cubics(cubics)
314 , colors(colors)
315 , texCoords(texCoords)
316 , xmode(SkSafeRef(xmode)) { }
mtklein6cfa73a2014-08-13 13:33:49 -0700317
dandovb3c9d1c2014-08-12 08:34:29 -0700318 SkPaint paint;
319 PODArray<SkPoint> cubics;
320 PODArray<SkColor> colors;
321 PODArray<SkPoint> texCoords;
322 SkAutoTUnref<SkXfermode> xmode;
323};
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000324
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000325#undef RECORD0
326#undef RECORD1
327#undef RECORD2
328#undef RECORD3
329#undef RECORD4
330#undef RECORD5
331
332} // namespace SkRecords
333
334#endif//SkRecords_DEFINED