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