OLD | NEW |
| (Empty) |
1 /* | |
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 | |
8 #ifndef SkRecords_DEFINED | |
9 #define SkRecords_DEFINED | |
10 | |
11 #include "SkCanvas.h" | |
12 #include "SkDrawable.h" | |
13 #include "SkMatrix.h" | |
14 #include "SkPicture.h" | |
15 #include "SkRSXform.h" | |
16 #include "SkTextBlob.h" | |
17 | |
18 namespace SkRecords { | |
19 | |
20 // A list of all the types of canvas calls we can record. | |
21 // Each of these is reified into a struct below. | |
22 // | |
23 // (We're using the macro-of-macro trick here to do several different things wit
h the same list.) | |
24 // | |
25 // We leave this SK_RECORD_TYPES macro defined for use by code that wants to ope
rate on SkRecords | |
26 // types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.
) | |
27 // | |
28 // Order doesn't technically matter here, but the compiler can generally generat
e better code if | |
29 // you keep them semantically grouped, especially the Draws. It's also nice to
leave NoOp at 0. | |
30 #define SK_RECORD_TYPES(M) \ | |
31 M(NoOp) \ | |
32 M(Restore) \ | |
33 M(Save) \ | |
34 M(SaveLayer) \ | |
35 M(SetMatrix) \ | |
36 M(ClipPath) \ | |
37 M(ClipRRect) \ | |
38 M(ClipRect) \ | |
39 M(ClipRegion) \ | |
40 M(DrawBitmap) \ | |
41 M(DrawBitmapNine) \ | |
42 M(DrawBitmapRect) \ | |
43 M(DrawBitmapRectFast) \ | |
44 M(DrawBitmapRectFixedSize) \ | |
45 M(DrawDrawable) \ | |
46 M(DrawImage) \ | |
47 M(DrawImageRect) \ | |
48 M(DrawImageNine) \ | |
49 M(DrawDRRect) \ | |
50 M(DrawOval) \ | |
51 M(DrawPaint) \ | |
52 M(DrawPath) \ | |
53 M(DrawPatch) \ | |
54 M(DrawPicture) \ | |
55 M(DrawPoints) \ | |
56 M(DrawPosText) \ | |
57 M(DrawPosTextH) \ | |
58 M(DrawText) \ | |
59 M(DrawTextOnPath) \ | |
60 M(DrawRRect) \ | |
61 M(DrawRect) \ | |
62 M(DrawSprite) \ | |
63 M(DrawTextBlob) \ | |
64 M(DrawAtlas) \ | |
65 M(DrawVertices) | |
66 | |
67 // Defines SkRecords::Type, an enum of all record types. | |
68 #define ENUM(T) T##_Type, | |
69 enum 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 ar
gs, 2 args, etc. | |
73 // These should be clearer when you look at their use below. | |
74 #define RECORD0(T) \ | |
75 struct T { \ | |
76 static const Type kType = T##_Type; \ | |
77 }; | |
78 | |
79 // Instead of requring the exact type A here, we take any type Z which implicitl
y casts to A. | |
80 // This lets our wrappers like ImmutableBitmap work seamlessly. | |
81 | |
82 #define RECORD1(T, A, a) \ | |
83 struct T { \ | |
84 static const Type kType = T##_Type; \ | |
85 T() {} \ | |
86 template <typename Z> \ | |
87 T(const Z& a) : a(a) {} \ | |
88 A a; \ | |
89 }; | |
90 | |
91 #define RECORD2(T, A, a, B, b) \ | |
92 struct T { \ | |
93 static const Type kType = T##_Type; \ | |
94 T() {} \ | |
95 template <typename Z, typename Y> \ | |
96 T(const Z& a, const Y& b) : a(a), b(b) {} \ | |
97 A a; B b; \ | |
98 }; | |
99 | |
100 #define RECORD3(T, A, a, B, b, C, c) \ | |
101 struct T { \ | |
102 static const Type kType = T##_Type; \ | |
103 T() {} \ | |
104 template <typename Z, typename Y, typename X> \ | |
105 T(const Z& a, const Y& b, const X& c) : a(a), b(b), c(c) {} \ | |
106 A a; B b; C c; \ | |
107 }; | |
108 | |
109 #define RECORD4(T, A, a, B, b, C, c, D, d)
\ | |
110 struct T {
\ | |
111 static const Type kType = T##_Type;
\ | |
112 T() {}
\ | |
113 template <typename Z, typename Y, typename X, typename W>
\ | |
114 T(const Z& a, const Y& b, const X& c, const W& d) : a(a), b(b), c(c), d(d) {
} \ | |
115 A a; B b; C c; D d;
\ | |
116 }; | |
117 | |
118 #define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \ | |
119 struct T { \ | |
120 static const Type kType = T##_Type; \ | |
121 T() {} \ | |
122 template <typename Z, typename Y, typename X, typename W, typename V> \ | |
123 T(const Z& a, const Y& b, const X& c, const W& d, const V& e) \ | |
124 : a(a), b(b), c(c), d(d), e(e) {} \ | |
125 A a; B b; C c; D d; E e; \ | |
126 }; | |
127 | |
128 #define RECORD8(T, A, a, B, b, C, c, D, d, E, e, F, f, G, g, H, h) \ | |
129 struct T { \ | |
130 static const Type kType = T##_Type; \ | |
131 T() {} \ | |
132 template <typename Z, typename Y, typename X, typename W, \ | |
133 typename V, typename U, typename S, typename R> \ | |
134 T(const Z& a, const Y& b, const X& c, const W& d, \ | |
135 const V& e, const U& f, const S& g, const R& h) \ | |
136 : a(a), b(b), c(c), d(d), e(e), f(f), g(g), h(h) {} \ | |
137 A a; B b; C c; D d; E e; F f; G g; H h; \ | |
138 }; | |
139 | |
140 #define ACT_AS_PTR(ptr) \ | |
141 operator T*() const { return ptr; } \ | |
142 T* operator->() const { return ptr; } | |
143 | |
144 template <typename T> | |
145 class RefBox : SkNoncopyable { | |
146 public: | |
147 RefBox() {} | |
148 RefBox(T* obj) : fObj(SkSafeRef(obj)) {} | |
149 ~RefBox() { SkSafeUnref(fObj); } | |
150 | |
151 ACT_AS_PTR(fObj); | |
152 | |
153 private: | |
154 T* fObj; | |
155 }; | |
156 | |
157 // An Optional doesn't own the pointer's memory, but may need to destroy non-POD
data. | |
158 template <typename T> | |
159 class Optional : SkNoncopyable { | |
160 public: | |
161 Optional() : fPtr(nullptr) {} | |
162 Optional(T* ptr) : fPtr(ptr) {} | |
163 ~Optional() { if (fPtr) fPtr->~T(); } | |
164 | |
165 ACT_AS_PTR(fPtr); | |
166 private: | |
167 T* fPtr; | |
168 }; | |
169 | |
170 // Like Optional, but ptr must not be NULL. | |
171 template <typename T> | |
172 class Adopted : SkNoncopyable { | |
173 public: | |
174 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); } | |
175 Adopted(Adopted* source) { | |
176 // Transfer ownership from source to this. | |
177 fPtr = source->fPtr; | |
178 source->fPtr = NULL; | |
179 } | |
180 ~Adopted() { if (fPtr) fPtr->~T(); } | |
181 | |
182 ACT_AS_PTR(fPtr); | |
183 private: | |
184 T* fPtr; | |
185 }; | |
186 | |
187 // PODArray doesn't own the pointer's memory, and we assume the data is POD. | |
188 template <typename T> | |
189 class PODArray { | |
190 public: | |
191 PODArray() {} | |
192 PODArray(T* ptr) : fPtr(ptr) {} | |
193 // Default copy and assign. | |
194 | |
195 ACT_AS_PTR(fPtr); | |
196 private: | |
197 T* fPtr; | |
198 }; | |
199 | |
200 #undef ACT_AS_PTR | |
201 | |
202 // Like SkBitmap, but deep copies pixels if they're not immutable. | |
203 // Using this, we guarantee the immutability of all bitmaps we record. | |
204 class ImmutableBitmap : SkNoncopyable { | |
205 public: | |
206 ImmutableBitmap() {} | |
207 explicit ImmutableBitmap(const SkBitmap& bitmap); | |
208 | |
209 int width() const { return fBitmap.width(); } | |
210 int height() const { return fBitmap.height(); } | |
211 | |
212 // While the pixels are immutable, SkBitmap itself is not thread-safe, so re
turn a copy. | |
213 SkBitmap shallowCopy() const { return fBitmap; } | |
214 private: | |
215 SkBitmap fBitmap; | |
216 }; | |
217 | |
218 // SkPath::getBounds() isn't thread safe unless we precache the bounds in a sing
lethreaded context. | |
219 // SkPath::cheapComputeDirection() is similar. | |
220 // Recording is a convenient time to cache these, or we can delay it to between
record and playback. | |
221 struct PreCachedPath : public SkPath { | |
222 PreCachedPath() {} | |
223 explicit PreCachedPath(const SkPath& path); | |
224 }; | |
225 | |
226 // Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we pre
cache it. | |
227 // This may not cover all SkMatrices used by the picture (e.g. some could be hid
ing in a shader). | |
228 struct TypedMatrix : public SkMatrix { | |
229 TypedMatrix() {} | |
230 explicit TypedMatrix(const SkMatrix& matrix); | |
231 }; | |
232 | |
233 RECORD0(NoOp); | |
234 | |
235 RECORD2(Restore, SkIRect, devBounds, TypedMatrix, matrix); | |
236 RECORD0(Save); | |
237 RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas:
:SaveFlags, flags); | |
238 | |
239 RECORD1(SetMatrix, TypedMatrix, matrix); | |
240 | |
241 struct RegionOpAndAA { | |
242 RegionOpAndAA() {} | |
243 RegionOpAndAA(SkRegion::Op op, bool aa) : op(op), aa(aa) {} | |
244 SkRegion::Op op : 31; // This really only needs to be 3, but there's no win
today to do so. | |
245 unsigned aa : 1; // MSVC won't pack an enum with an bool, so we call t
his an unsigned. | |
246 }; | |
247 SK_COMPILE_ASSERT(sizeof(RegionOpAndAA) == 4, RegionOpAndAASize); | |
248 | |
249 RECORD3(ClipPath, SkIRect, devBounds, PreCachedPath, path, RegionOpAndAA, opA
A); | |
250 RECORD3(ClipRRect, SkIRect, devBounds, SkRRect, rrect, RegionOpAndAA, opA
A); | |
251 RECORD3(ClipRect, SkIRect, devBounds, SkRect, rect, RegionOpAndAA, opA
A); | |
252 RECORD3(ClipRegion, SkIRect, devBounds, SkRegion, region, SkRegion::Op, o
p); | |
253 | |
254 // While not strictly required, if you have an SkPaint, it's fastest to put it f
irst. | |
255 RECORD4(DrawBitmap, Optional<SkPaint>, paint, | |
256 ImmutableBitmap, bitmap, | |
257 SkScalar, left, | |
258 SkScalar, top); | |
259 RECORD4(DrawBitmapNine, Optional<SkPaint>, paint, | |
260 ImmutableBitmap, bitmap, | |
261 SkIRect, center, | |
262 SkRect, dst); | |
263 RECORD4(DrawBitmapRect, Optional<SkPaint>, paint, | |
264 ImmutableBitmap, bitmap, | |
265 Optional<SkRect>, src, | |
266 SkRect, dst); | |
267 RECORD4(DrawBitmapRectFast, Optional<SkPaint>, paint, | |
268 ImmutableBitmap, bitmap, | |
269 Optional<SkRect>, src, | |
270 SkRect, dst); | |
271 RECORD5(DrawBitmapRectFixedSize, SkPaint, paint, | |
272 ImmutableBitmap, bitmap, | |
273 SkRect, src, | |
274 SkRect, dst, | |
275 SkCanvas::SrcRectConstraint, constraint); | |
276 RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner); | |
277 RECORD3(DrawDrawable, Optional<SkMatrix>, matrix, SkRect, worstCaseBounds, int32
_t, index); | |
278 RECORD4(DrawImage, Optional<SkPaint>, paint, | |
279 RefBox<const SkImage>, image, | |
280 SkScalar, left, | |
281 SkScalar, top); | |
282 RECORD5(DrawImageRect, Optional<SkPaint>, paint, | |
283 RefBox<const SkImage>, image, | |
284 Optional<SkRect>, src, | |
285 SkRect, dst, | |
286 SkCanvas::SrcRectConstraint, constraint); | |
287 RECORD4(DrawImageNine, Optional<SkPaint>, paint, | |
288 RefBox<const SkImage>, image, | |
289 SkIRect, center, | |
290 SkRect, dst); | |
291 RECORD2(DrawOval, SkPaint, paint, SkRect, oval); | |
292 RECORD1(DrawPaint, SkPaint, paint); | |
293 RECORD2(DrawPath, SkPaint, paint, PreCachedPath, path); | |
294 RECORD3(DrawPicture, Optional<SkPaint>, paint, | |
295 RefBox<const SkPicture>, picture, | |
296 TypedMatrix, matrix); | |
297 RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, unsigned, count,
SkPoint*, pts); | |
298 RECORD4(DrawPosText, SkPaint, paint, | |
299 PODArray<char>, text, | |
300 size_t, byteLength, | |
301 PODArray<SkPoint>, pos); | |
302 RECORD5(DrawPosTextH, SkPaint, paint, | |
303 PODArray<char>, text, | |
304 unsigned, byteLength, | |
305 SkScalar, y, | |
306 PODArray<SkScalar>, xpos); | |
307 RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect); | |
308 RECORD2(DrawRect, SkPaint, paint, SkRect, rect); | |
309 RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left
, int, top); | |
310 RECORD5(DrawText, SkPaint, paint, | |
311 PODArray<char>, text, | |
312 size_t, byteLength, | |
313 SkScalar, x, | |
314 SkScalar, y); | |
315 RECORD4(DrawTextBlob, SkPaint, paint, | |
316 RefBox<const SkTextBlob>, blob, | |
317 SkScalar, x, | |
318 SkScalar, y); | |
319 RECORD5(DrawTextOnPath, SkPaint, paint, | |
320 PODArray<char>, text, | |
321 size_t, byteLength, | |
322 PreCachedPath, path, | |
323 TypedMatrix, matrix); | |
324 | |
325 RECORD5(DrawPatch, SkPaint, paint, | |
326 PODArray<SkPoint>, cubics, | |
327 PODArray<SkColor>, colors, | |
328 PODArray<SkPoint>, texCoords, | |
329 RefBox<SkXfermode>, xmode); | |
330 | |
331 RECORD8(DrawAtlas, Optional<SkPaint>, paint, | |
332 RefBox<const SkImage>, atlas, | |
333 PODArray<SkRSXform>, xforms, | |
334 PODArray<SkRect>, texs, | |
335 PODArray<SkColor>, colors, | |
336 int, count, | |
337 SkXfermode::Mode, mode, | |
338 Optional<SkRect>, cull); | |
339 | |
340 // This guy is so ugly we just write it manually. | |
341 struct DrawVertices { | |
342 static const Type kType = DrawVertices_Type; | |
343 | |
344 DrawVertices(const SkPaint& paint, | |
345 SkCanvas::VertexMode vmode, | |
346 int vertexCount, | |
347 SkPoint* vertices, | |
348 SkPoint* texs, | |
349 SkColor* colors, | |
350 SkXfermode* xmode, | |
351 uint16_t* indices, | |
352 int indexCount) | |
353 : paint(paint) | |
354 , vmode(vmode) | |
355 , vertexCount(vertexCount) | |
356 , vertices(vertices) | |
357 , texs(texs) | |
358 , colors(colors) | |
359 , xmode(SkSafeRef(xmode)) | |
360 , indices(indices) | |
361 , indexCount(indexCount) {} | |
362 | |
363 SkPaint paint; | |
364 SkCanvas::VertexMode vmode; | |
365 int vertexCount; | |
366 PODArray<SkPoint> vertices; | |
367 PODArray<SkPoint> texs; | |
368 PODArray<SkColor> colors; | |
369 SkAutoTUnref<SkXfermode> xmode; | |
370 PODArray<uint16_t> indices; | |
371 int indexCount; | |
372 }; | |
373 | |
374 #undef RECORD0 | |
375 #undef RECORD1 | |
376 #undef RECORD2 | |
377 #undef RECORD3 | |
378 #undef RECORD4 | |
379 #undef RECORD5 | |
380 #undef RECORD8 | |
381 | |
382 } // namespace SkRecords | |
383 | |
384 #endif//SkRecords_DEFINED | |
OLD | NEW |