commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 1 | #ifndef SkRecords_DEFINED |
| 2 | #define SkRecords_DEFINED |
| 3 | |
| 4 | #include "SkCanvas.h" |
| 5 | |
| 6 | namespace SkRecords { |
| 7 | |
| 8 | // A list of all the types of canvas calls we can record. |
| 9 | // Each of these is reified into a struct below. |
| 10 | // |
| 11 | // (We're using the macro-of-macro trick here to do several different things with the same list.) |
| 12 | // |
| 13 | // We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords |
| 14 | // types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.) |
| 15 | #define SK_RECORD_TYPES(M) \ |
| 16 | M(Restore) \ |
| 17 | M(Save) \ |
| 18 | M(SaveLayer) \ |
| 19 | M(Concat) \ |
| 20 | M(SetMatrix) \ |
| 21 | M(ClipPath) \ |
| 22 | M(ClipRRect) \ |
| 23 | M(ClipRect) \ |
| 24 | M(ClipRegion) \ |
| 25 | M(Clear) \ |
| 26 | M(DrawBitmap) \ |
| 27 | M(DrawBitmapMatrix) \ |
| 28 | M(DrawBitmapNine) \ |
| 29 | M(DrawBitmapRectToRect) \ |
| 30 | M(DrawDRRect) \ |
| 31 | M(DrawOval) \ |
| 32 | M(DrawPaint) \ |
| 33 | M(DrawPath) \ |
| 34 | M(DrawPoints) \ |
| 35 | M(DrawPosText) \ |
| 36 | M(DrawPosTextH) \ |
| 37 | M(DrawRRect) \ |
| 38 | M(DrawRect) \ |
| 39 | M(DrawSprite) \ |
| 40 | M(DrawText) \ |
| 41 | M(DrawTextOnPath) \ |
commit-bot@chromium.org | 03a99b8 | 2014-04-08 15:17:17 +0000 | [diff] [blame^] | 42 | M(DrawVertices) \ |
| 43 | M(PushCull) \ |
| 44 | M(PopCull) |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 45 | |
| 46 | // Defines SkRecords::Type, an enum of all record types. |
| 47 | #define ENUM(T) T##_Type, |
| 48 | enum Type { SK_RECORD_TYPES(ENUM) }; |
| 49 | #undef ENUM |
| 50 | |
| 51 | // Macros to make it easier to define a record for a draw call with 0 args, 1 args, 2 args, etc. |
| 52 | // These should be clearer when you look at their use below. |
| 53 | #define RECORD0(T) \ |
| 54 | struct T { \ |
| 55 | static const Type kType = T##_Type; \ |
| 56 | T() {} \ |
| 57 | }; |
| 58 | |
| 59 | // We try to be flexible about the types the constructors take. Instead of requring the exact type |
| 60 | // A here, we take any type Z which implicitly casts to A. This allows the delay_copy() trick to |
| 61 | // work, allowing the caller to decide whether to pass by value or by const&. |
| 62 | |
| 63 | #define RECORD1(T, A, a) \ |
| 64 | struct T { \ |
| 65 | static const Type kType = T##_Type; \ |
| 66 | template <typename Z> \ |
| 67 | T(Z a) : a(a) {} \ |
| 68 | A a; \ |
| 69 | }; |
| 70 | |
| 71 | #define RECORD2(T, A, a, B, b) \ |
| 72 | struct T { \ |
| 73 | static const Type kType = T##_Type; \ |
| 74 | template <typename Z, typename Y> \ |
| 75 | T(Z a, Y b) : a(a), b(b) {} \ |
| 76 | A a; B b; \ |
| 77 | }; |
| 78 | |
| 79 | #define RECORD3(T, A, a, B, b, C, c) \ |
| 80 | struct T { \ |
| 81 | static const Type kType = T##_Type; \ |
| 82 | template <typename Z, typename Y, typename X> \ |
| 83 | T(Z a, Y b, X c) : a(a), b(b), c(c) {} \ |
| 84 | A a; B b; C c; \ |
| 85 | }; |
| 86 | |
| 87 | #define RECORD4(T, A, a, B, b, C, c, D, d) \ |
| 88 | struct T { \ |
| 89 | static const Type kType = T##_Type; \ |
| 90 | template <typename Z, typename Y, typename X, typename W> \ |
| 91 | T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {} \ |
| 92 | A a; B b; C c; D d; \ |
| 93 | }; |
| 94 | |
| 95 | #define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \ |
| 96 | struct T { \ |
| 97 | static const Type kType = T##_Type; \ |
| 98 | template <typename Z, typename Y, typename X, typename W, typename V> \ |
| 99 | T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {} \ |
| 100 | A a; B b; C c; D d; E e; \ |
| 101 | }; |
| 102 | |
| 103 | // Like SkBitmap, but deep copies pixels if they're not immutable. |
| 104 | // Using this, we guarantee the immutability of all bitmaps we record. |
| 105 | class ImmutableBitmap { |
| 106 | public: |
| 107 | explicit ImmutableBitmap(const SkBitmap& bitmap) { |
| 108 | if (bitmap.isImmutable()) { |
| 109 | fBitmap = bitmap; |
| 110 | } else { |
| 111 | bitmap.copyTo(&fBitmap); |
| 112 | } |
| 113 | fBitmap.setImmutable(); |
| 114 | } |
| 115 | |
| 116 | operator const SkBitmap& () const { return fBitmap; } |
| 117 | |
| 118 | private: |
| 119 | SkBitmap fBitmap; |
| 120 | }; |
| 121 | |
| 122 | // Pointers here represent either an optional value or an array if accompanied by a count. |
| 123 | // None of these records manages the lifetimes of pointers, except for DrawVertices handling its |
| 124 | // Xfermode specially. |
| 125 | |
| 126 | RECORD0(Restore); |
| 127 | RECORD1(Save, SkCanvas::SaveFlags, flags); |
| 128 | RECORD3(SaveLayer, SkRect*, bounds, SkPaint*, paint, SkCanvas::SaveFlags, flags); |
| 129 | |
commit-bot@chromium.org | 03a99b8 | 2014-04-08 15:17:17 +0000 | [diff] [blame^] | 130 | RECORD1(PushCull, SkRect, rect); |
| 131 | RECORD0(PopCull); |
| 132 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 133 | RECORD1(Concat, SkMatrix, matrix); |
| 134 | RECORD1(SetMatrix, SkMatrix, matrix); |
| 135 | |
| 136 | RECORD3(ClipPath, SkPath, path, SkRegion::Op, op, bool, doAA); |
| 137 | RECORD3(ClipRRect, SkRRect, rrect, SkRegion::Op, op, bool, doAA); |
| 138 | RECORD3(ClipRect, SkRect, rect, SkRegion::Op, op, bool, doAA); |
| 139 | RECORD2(ClipRegion, SkRegion, region, SkRegion::Op, op); |
| 140 | |
| 141 | RECORD1(Clear, SkColor, color); |
| 142 | RECORD4(DrawBitmap, ImmutableBitmap, bitmap, SkScalar, left, SkScalar, top, SkPaint*, paint); |
| 143 | RECORD3(DrawBitmapMatrix, ImmutableBitmap, bitmap, SkMatrix, matrix, SkPaint*, paint); |
| 144 | RECORD4(DrawBitmapNine, ImmutableBitmap, bitmap, SkIRect, center, SkRect, dst, SkPaint*, paint); |
| 145 | RECORD5(DrawBitmapRectToRect, ImmutableBitmap, bitmap, |
| 146 | SkRect*, src, |
| 147 | SkRect, dst, |
| 148 | SkPaint*, paint, |
| 149 | SkCanvas::DrawBitmapRectFlags, flags); |
| 150 | RECORD3(DrawDRRect, SkRRect, outer, SkRRect, inner, SkPaint, paint); |
| 151 | RECORD2(DrawOval, SkRect, oval, SkPaint, paint); |
| 152 | RECORD1(DrawPaint, SkPaint, paint); |
| 153 | RECORD2(DrawPath, SkPath, path, SkPaint, paint); |
| 154 | RECORD4(DrawPoints, SkCanvas::PointMode, mode, size_t, count, SkPoint*, pts, SkPaint, paint); |
| 155 | RECORD4(DrawPosText, char*, text, size_t, byteLength, SkPoint*, pos, SkPaint, paint); |
| 156 | RECORD5(DrawPosTextH, char*, text, |
| 157 | size_t, byteLength, |
| 158 | SkScalar*, xpos, |
| 159 | SkScalar, y, |
| 160 | SkPaint, paint); |
| 161 | RECORD2(DrawRRect, SkRRect, rrect, SkPaint, paint); |
| 162 | RECORD2(DrawRect, SkRect, rect, SkPaint, paint); |
| 163 | RECORD4(DrawSprite, ImmutableBitmap, bitmap, int, left, int, top, SkPaint*, paint); |
| 164 | RECORD5(DrawText, char*, text, size_t, byteLength, SkScalar, x, SkScalar, y, SkPaint, paint); |
| 165 | RECORD5(DrawTextOnPath, char*, text, |
| 166 | size_t, byteLength, |
| 167 | SkPath, path, |
| 168 | SkMatrix*, matrix, |
| 169 | SkPaint, paint); |
| 170 | |
| 171 | // This guy is so ugly we just write it manually. |
| 172 | struct DrawVertices { |
| 173 | static const Type kType = DrawVertices_Type; |
| 174 | |
| 175 | DrawVertices(SkCanvas::VertexMode vmode, |
| 176 | int vertexCount, |
| 177 | SkPoint* vertices, |
| 178 | SkPoint* texs, |
| 179 | SkColor* colors, |
| 180 | SkXfermode* xmode, |
| 181 | uint16_t* indices, |
| 182 | int indexCount, |
| 183 | const SkPaint& paint) |
| 184 | : vmode(vmode) |
| 185 | , vertexCount(vertexCount) |
| 186 | , vertices(vertices) |
| 187 | , texs(texs) |
| 188 | , colors(colors) |
| 189 | , xmode(SkSafeRef(xmode)) |
| 190 | , indices(indices) |
| 191 | , indexCount(indexCount) |
| 192 | , paint(paint) {} |
| 193 | |
| 194 | SkCanvas::VertexMode vmode; |
| 195 | int vertexCount; |
| 196 | SkPoint* vertices; |
| 197 | SkPoint* texs; |
| 198 | SkColor* colors; |
| 199 | SkAutoTUnref<SkXfermode> xmode; |
| 200 | uint16_t* indices; |
| 201 | int indexCount; |
| 202 | SkPaint paint; |
| 203 | }; |
| 204 | |
| 205 | #undef RECORD0 |
| 206 | #undef RECORD1 |
| 207 | #undef RECORD2 |
| 208 | #undef RECORD3 |
| 209 | #undef RECORD4 |
| 210 | #undef RECORD5 |
| 211 | |
| 212 | } // namespace SkRecords |
| 213 | |
| 214 | #endif//SkRecords_DEFINED |