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