Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: include/private/SkRecords.h

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

Powered by Google App Engine
This is Rietveld 408576698