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