commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 8 | #ifndef SkRecords_DEFINED |
| 9 | #define SkRecords_DEFINED |
| 10 | |
| 11 | #include "SkCanvas.h" |
| 12 | |
| 13 | namespace SkRecords { |
| 14 | |
| 15 | // A list of all the types of canvas calls we can record. |
| 16 | // Each of these is reified into a struct below. |
| 17 | // |
| 18 | // (We're using the macro-of-macro trick here to do several different things with the same list.) |
| 19 | // |
| 20 | // We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords |
| 21 | // types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.) |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 22 | // |
| 23 | // Order doesn't technically matter here, but the compiler can generally generate better code if |
| 24 | // you keep them semantically grouped, especially the Draws. It's also nice to leave NoOp at 0. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 25 | #define SK_RECORD_TYPES(M) \ |
| 26 | M(NoOp) \ |
| 27 | M(Restore) \ |
| 28 | M(Save) \ |
| 29 | M(SaveLayer) \ |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 30 | M(PushCull) \ |
| 31 | M(PopCull) \ |
| 32 | M(PairedPushCull) /*From SkRecordAnnotateCullingPairs*/ \ |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 33 | M(Concat) \ |
| 34 | M(SetMatrix) \ |
| 35 | M(ClipPath) \ |
| 36 | M(ClipRRect) \ |
| 37 | M(ClipRect) \ |
| 38 | M(ClipRegion) \ |
| 39 | M(Clear) \ |
| 40 | M(DrawBitmap) \ |
| 41 | M(DrawBitmapMatrix) \ |
| 42 | M(DrawBitmapNine) \ |
| 43 | M(DrawBitmapRectToRect) \ |
| 44 | M(DrawDRRect) \ |
| 45 | M(DrawOval) \ |
| 46 | M(DrawPaint) \ |
| 47 | M(DrawPath) \ |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame^] | 48 | M(DrawPatch) \ |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 49 | M(DrawPoints) \ |
| 50 | M(DrawPosText) \ |
| 51 | M(DrawPosTextH) \ |
| 52 | M(DrawRRect) \ |
| 53 | M(DrawRect) \ |
| 54 | M(DrawSprite) \ |
| 55 | M(DrawText) \ |
| 56 | M(DrawTextOnPath) \ |
| 57 | M(DrawVertices) \ |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 58 | M(BoundedDrawPosTextH) /*From SkRecordBoundDrawPosTextH*/ |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 59 | |
| 60 | // Defines SkRecords::Type, an enum of all record types. |
| 61 | #define ENUM(T) T##_Type, |
| 62 | enum Type { SK_RECORD_TYPES(ENUM) }; |
| 63 | #undef ENUM |
| 64 | |
| 65 | // Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc. |
| 66 | // These should be clearer when you look at their use below. |
| 67 | #define RECORD0(T) \ |
| 68 | struct T { \ |
| 69 | static const Type kType = T##_Type; \ |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // We try to be flexible about the types the constructors take. Instead of requring the exact type |
| 73 | // A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to |
| 74 | // work, allowing the caller to decide whether to pass by value or by const&. |
| 75 | |
| 76 | #define RECORD1(T, A, a) \ |
| 77 | struct T { \ |
| 78 | static const Type kType = T##_Type; \ |
| 79 | template <typename Z> \ |
| 80 | T(Z a) : a(a) {} \ |
| 81 | A a; \ |
| 82 | }; |
| 83 | |
| 84 | #define RECORD2(T, A, a, B, b) \ |
| 85 | struct T { \ |
| 86 | static const Type kType = T##_Type; \ |
| 87 | template <typename Z, typename Y> \ |
| 88 | T(Z a, Y b) : a(a), b(b) {} \ |
| 89 | A a; B b; \ |
| 90 | }; |
| 91 | |
| 92 | #define RECORD3(T, A, a, B, b, C, c) \ |
| 93 | struct T { \ |
| 94 | static const Type kType = T##_Type; \ |
| 95 | template <typename Z, typename Y, typename X> \ |
| 96 | T(Z a, Y b, X c) : a(a), b(b), c(c) {} \ |
| 97 | A a; B b; C c; \ |
| 98 | }; |
| 99 | |
| 100 | #define RECORD4(T, A, a, B, b, C, c, D, d) \ |
| 101 | struct T { \ |
| 102 | static const Type kType = T##_Type; \ |
| 103 | template <typename Z, typename Y, typename X, typename W> \ |
| 104 | T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \ |
| 105 | A a; B b; C c; D d; \ |
| 106 | }; |
| 107 | |
| 108 | #define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \ |
| 109 | struct T { \ |
| 110 | static const Type kType = T##_Type; \ |
| 111 | template <typename Z, typename Y, typename X, typename W, typename V> \ |
| 112 | T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \ |
| 113 | A a; B b; C c; D d; E e; \ |
| 114 | }; |
| 115 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 116 | #define ACT_AS_PTR(ptr) \ |
| 117 | operator T*() { return ptr; } \ |
| 118 | operator const T*() const { return ptr; } \ |
| 119 | T* operator->() { return ptr; } \ |
| 120 | const T* operator->() const { return ptr; } |
| 121 | |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 122 | // An Optional doesn't own the pointer's memory, but may need to destroy non-POD data. |
| 123 | template <typename T> |
| 124 | class Optional : SkNoncopyable { |
| 125 | public: |
| 126 | Optional(T* ptr) : fPtr(ptr) {} |
| 127 | ~Optional() { if (fPtr) fPtr->~T(); } |
| 128 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 129 | ACT_AS_PTR(fPtr); |
| 130 | private: |
| 131 | T* fPtr; |
| 132 | }; |
| 133 | |
| 134 | // Like Optional, but ptr must not be NULL. |
| 135 | template <typename T> |
| 136 | class Adopted : SkNoncopyable { |
| 137 | public: |
| 138 | Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); } |
commit-bot@chromium.org | f0ae5e4 | 2014-04-24 15:33:48 +0000 | [diff] [blame] | 139 | Adopted(Adopted* source) { |
| 140 | // Transfer ownership from source to this. |
| 141 | fPtr = source->fPtr; |
| 142 | source->fPtr = NULL; |
| 143 | } |
| 144 | ~Adopted() { if (fPtr) fPtr->~T(); } |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 145 | |
| 146 | ACT_AS_PTR(fPtr); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 147 | private: |
| 148 | T* fPtr; |
| 149 | }; |
| 150 | |
| 151 | // PODArray doesn't own the pointer's memory, and we assume the data is POD. |
| 152 | template <typename T> |
commit-bot@chromium.org | f0ae5e4 | 2014-04-24 15:33:48 +0000 | [diff] [blame] | 153 | class PODArray { |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 154 | public: |
| 155 | PODArray(T* ptr) : fPtr(ptr) {} |
commit-bot@chromium.org | f0ae5e4 | 2014-04-24 15:33:48 +0000 | [diff] [blame] | 156 | // Default copy and assign. |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 157 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 158 | ACT_AS_PTR(fPtr); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 159 | private: |
| 160 | T* fPtr; |
| 161 | }; |
| 162 | |
commit-bot@chromium.org | 16307bd | 2014-04-22 18:32:58 +0000 | [diff] [blame] | 163 | #undef ACT_AS_PTR |
| 164 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 165 | // Like SkBitmap, but deep copies pixels if they're not immutable. |
| 166 | // Using this, we guarantee the immutability of all bitmaps we record. |
| 167 | class ImmutableBitmap { |
| 168 | public: |
| 169 | explicit ImmutableBitmap(const SkBitmap& bitmap) { |
| 170 | if (bitmap.isImmutable()) { |
| 171 | fBitmap = bitmap; |
| 172 | } else { |
| 173 | bitmap.copyTo(&fBitmap); |
| 174 | } |
| 175 | fBitmap.setImmutable(); |
| 176 | } |
| 177 | |
| 178 | operator const SkBitmap& () const { return fBitmap; } |
| 179 | |
| 180 | private: |
| 181 | SkBitmap fBitmap; |
| 182 | }; |
| 183 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 184 | RECORD0(NoOp); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 185 | |
| 186 | RECORD0(Restore); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 187 | RECORD0(Save); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 188 | RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas::SaveFlags, flags); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 189 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 190 | RECORD1(PushCull, SkRect, rect); |
commit-bot@chromium.org | 03a99b8 | 2014-04-08 15:17:17 +0000 | [diff] [blame] | 191 | RECORD0(PopCull); |
| 192 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 193 | RECORD1(Concat, SkMatrix, matrix); |
| 194 | RECORD1(SetMatrix, SkMatrix, matrix); |
| 195 | |
| 196 | RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA); |
| 197 | RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA); |
| 198 | RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA); |
| 199 | RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op); |
| 200 | |
| 201 | RECORD1(Clear, SkColor, color); |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 202 | // While not strictly required, if you have an SkPaint, it's fastest to put it first. |
| 203 | RECORD4(DrawBitmap, Optional<SkPaint>, paint, |
| 204 | ImmutableBitmap, bitmap, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 205 | SkScalar, left, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 206 | SkScalar, top); |
| 207 | RECORD3(DrawBitmapMatrix, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, SkMatrix, matrix); |
| 208 | RECORD4(DrawBitmapNine, Optional<SkPaint>, paint, |
| 209 | ImmutableBitmap, bitmap, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 210 | SkIRect, center, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 211 | SkRect, dst); |
| 212 | RECORD5(DrawBitmapRectToRect, Optional<SkPaint>, paint, |
| 213 | ImmutableBitmap, bitmap, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 214 | Optional<SkRect>, src, |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 215 | SkRect, dst, |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 216 | SkCanvas::DrawBitmapRectFlags, flags); |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 217 | RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner); |
| 218 | RECORD2(DrawOval, SkPaint, paint, SkRect, oval); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 219 | RECORD1(DrawPaint, SkPaint, paint); |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 220 | RECORD2(DrawPath, SkPaint, paint, SkPath, path); |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame^] | 221 | RECORD2(DrawPatch, SkPaint, paint, SkPatch, patch); |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 222 | RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts); |
| 223 | RECORD4(DrawPosText, SkPaint, paint, |
| 224 | PODArray<char>, text, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 225 | size_t, byteLength, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 226 | PODArray<SkPoint>, pos); |
| 227 | RECORD5(DrawPosTextH, SkPaint, paint, |
| 228 | PODArray<char>, text, |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 229 | size_t, byteLength, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 230 | PODArray<SkScalar>, xpos, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 231 | SkScalar, y); |
| 232 | RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect); |
| 233 | RECORD2(DrawRect, SkPaint, paint, SkRect, rect); |
| 234 | RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left, int, top); |
| 235 | RECORD5(DrawText, SkPaint, paint, |
| 236 | PODArray<char>, text, |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 237 | size_t, byteLength, |
| 238 | SkScalar, x, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 239 | SkScalar, y); |
| 240 | RECORD5(DrawTextOnPath, SkPaint, paint, |
| 241 | PODArray<char>, text, |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 242 | size_t, byteLength, |
| 243 | SkPath, path, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 244 | Optional<SkMatrix>, matrix); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 245 | |
| 246 | // This guy is so ugly we just write it manually. |
| 247 | struct DrawVertices { |
| 248 | static const Type kType = DrawVertices_Type; |
| 249 | |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 250 | DrawVertices(const SkPaint& paint, |
| 251 | SkCanvas::VertexMode vmode, |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 252 | int vertexCount, |
| 253 | SkPoint* vertices, |
| 254 | SkPoint* texs, |
| 255 | SkColor* colors, |
| 256 | SkXfermode* xmode, |
| 257 | uint16_t* indices, |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 258 | int indexCount) |
| 259 | : paint(paint) |
| 260 | , vmode(vmode) |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 261 | , vertexCount(vertexCount) |
| 262 | , vertices(vertices) |
| 263 | , texs(texs) |
| 264 | , colors(colors) |
| 265 | , xmode(SkSafeRef(xmode)) |
| 266 | , indices(indices) |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 267 | , indexCount(indexCount) {} |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 268 | |
commit-bot@chromium.org | 37f6e62 | 2014-05-07 22:59:38 +0000 | [diff] [blame] | 269 | SkPaint paint; |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 270 | SkCanvas::VertexMode vmode; |
| 271 | int vertexCount; |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 272 | PODArray<SkPoint> vertices; |
| 273 | PODArray<SkPoint> texs; |
| 274 | PODArray<SkColor> colors; |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 275 | SkAutoTUnref<SkXfermode> xmode; |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 276 | PODArray<uint16_t> indices; |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 277 | int indexCount; |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 280 | // Records added by optimizations. |
| 281 | RECORD2(PairedPushCull, Adopted<PushCull>, base, unsigned, skip); |
| 282 | RECORD3(BoundedDrawPosTextH, Adopted<DrawPosTextH>, base, SkScalar, minY, SkScalar, maxY); |
| 283 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 284 | #undef RECORD0 |
| 285 | #undef RECORD1 |
| 286 | #undef RECORD2 |
| 287 | #undef RECORD3 |
| 288 | #undef RECORD4 |
| 289 | #undef RECORD5 |
| 290 | |
| 291 | } // namespace SkRecords |
| 292 | |
| 293 | #endif//SkRecords_DEFINED |