| OLD | NEW | 
|---|
| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 74     static const Type kType = T##_Type; \ | 74     static const Type kType = T##_Type; \ | 
| 75 }; | 75 }; | 
| 76 | 76 | 
| 77 // We try to be flexible about the types the constructors take.  Instead of requ
     ring the exact type | 77 // We try to be flexible about the types the constructors take.  Instead of requ
     ring the exact type | 
| 78 // A here, we take any type Z which implicitly casts to A.  This allows the dela
     y_copy() trick to | 78 // A here, we take any type Z which implicitly casts to A.  This allows the dela
     y_copy() trick to | 
| 79 // work, allowing the caller to decide whether to pass by value or by const&. | 79 // work, allowing the caller to decide whether to pass by value or by const&. | 
| 80 | 80 | 
| 81 #define RECORD1(T, A, a)                \ | 81 #define RECORD1(T, A, a)                \ | 
| 82 struct T {                              \ | 82 struct T {                              \ | 
| 83     static const Type kType = T##_Type; \ | 83     static const Type kType = T##_Type; \ | 
| 84     T() {}                              \ |  | 
| 85     template <typename Z>               \ | 84     template <typename Z>               \ | 
| 86     T(Z a) : a(a) {}                    \ | 85     T(Z a) : a(a) {}                    \ | 
| 87     A a;                                \ | 86     A a;                                \ | 
| 88 }; | 87 }; | 
| 89 | 88 | 
| 90 #define RECORD2(T, A, a, B, b)          \ | 89 #define RECORD2(T, A, a, B, b)          \ | 
| 91 struct T {                              \ | 90 struct T {                              \ | 
| 92     static const Type kType = T##_Type; \ | 91     static const Type kType = T##_Type; \ | 
| 93     T() {}                              \ |  | 
| 94     template <typename Z, typename Y>   \ | 92     template <typename Z, typename Y>   \ | 
| 95     T(Z a, Y b) : a(a), b(b) {}         \ | 93     T(Z a, Y b) : a(a), b(b) {}         \ | 
| 96     A a; B b;                           \ | 94     A a; B b;                           \ | 
| 97 }; | 95 }; | 
| 98 | 96 | 
| 99 #define RECORD3(T, A, a, B, b, C, c)              \ | 97 #define RECORD3(T, A, a, B, b, C, c)              \ | 
| 100 struct T {                                        \ | 98 struct T {                                        \ | 
| 101     static const Type kType = T##_Type;           \ | 99     static const Type kType = T##_Type;           \ | 
| 102     T() {}                                        \ |  | 
| 103     template <typename Z, typename Y, typename X> \ | 100     template <typename Z, typename Y, typename X> \ | 
| 104     T(Z a, Y b, X c) : a(a), b(b), c(c) {}        \ | 101     T(Z a, Y b, X c) : a(a), b(b), c(c) {}        \ | 
| 105     A a; B b; C c;                                \ | 102     A a; B b; C c;                                \ | 
| 106 }; | 103 }; | 
| 107 | 104 | 
| 108 #define RECORD4(T, A, a, B, b, C, c, D, d)                    \ | 105 #define RECORD4(T, A, a, B, b, C, c, D, d)                    \ | 
| 109 struct T {                                                    \ | 106 struct T {                                                    \ | 
| 110     static const Type kType = T##_Type;                       \ | 107     static const Type kType = T##_Type;                       \ | 
| 111     T() {}                                                    \ |  | 
| 112     template <typename Z, typename Y, typename X, typename W> \ | 108     template <typename Z, typename Y, typename X, typename W> \ | 
| 113     T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {}         \ | 109     T(Z a, Y b, X c, W d) : a(a), b(b), c(c), d(d) {}         \ | 
| 114     A a; B b; C c; D d;                                       \ | 110     A a; B b; C c; D d;                                       \ | 
| 115 }; | 111 }; | 
| 116 | 112 | 
| 117 #define RECORD5(T, A, a, B, b, C, c, D, d, E, e)                          \ | 113 #define RECORD5(T, A, a, B, b, C, c, D, d, E, e)                          \ | 
| 118 struct T {                                                                \ | 114 struct T {                                                                \ | 
| 119     static const Type kType = T##_Type;                                   \ | 115     static const Type kType = T##_Type;                                   \ | 
| 120     T() {}                                                                \ |  | 
| 121     template <typename Z, typename Y, typename X, typename W, typename V> \ | 116     template <typename Z, typename Y, typename X, typename W, typename V> \ | 
| 122     T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {}          \ | 117     T(Z a, Y b, X c, W d, V e) : a(a), b(b), c(c), d(d), e(e) {}          \ | 
| 123     A a; B b; C c; D d; E e;                                              \ | 118     A a; B b; C c; D d; E e;                                              \ | 
| 124 }; | 119 }; | 
| 125 | 120 | 
| 126 #define ACT_AS_PTR(ptr)                 \ | 121 #define ACT_AS_PTR(ptr)                 \ | 
| 127     operator T*() const { return ptr; } \ | 122     operator T*() const { return ptr; } \ | 
| 128     T* operator->() const { return ptr; } | 123     T* operator->() const { return ptr; } | 
| 129 | 124 | 
| 130 template <typename T> | 125 template <typename T> | 
| 131 class RefBox : SkNoncopyable { | 126 class RefBox : SkNoncopyable { | 
| 132 public: | 127 public: | 
| 133     RefBox() {} |  | 
| 134     RefBox(T* obj) : fObj(SkSafeRef(obj)) {} | 128     RefBox(T* obj) : fObj(SkSafeRef(obj)) {} | 
| 135     ~RefBox() { SkSafeUnref(fObj); } | 129     ~RefBox() { SkSafeUnref(fObj); } | 
| 136 | 130 | 
| 137     ACT_AS_PTR(fObj); | 131     ACT_AS_PTR(fObj); | 
| 138 | 132 | 
| 139 private: | 133 private: | 
| 140     T* fObj; | 134     T* fObj; | 
| 141 }; | 135 }; | 
| 142 | 136 | 
| 143 // An Optional doesn't own the pointer's memory, but may need to destroy non-POD
      data. | 137 // An Optional doesn't own the pointer's memory, but may need to destroy non-POD
      data. | 
| 144 template <typename T> | 138 template <typename T> | 
| 145 class Optional : SkNoncopyable { | 139 class Optional : SkNoncopyable { | 
| 146 public: | 140 public: | 
| 147     Optional() : fPtr(nullptr) {} |  | 
| 148     Optional(T* ptr) : fPtr(ptr) {} | 141     Optional(T* ptr) : fPtr(ptr) {} | 
| 149     ~Optional() { if (fPtr) fPtr->~T(); } | 142     ~Optional() { if (fPtr) fPtr->~T(); } | 
| 150 | 143 | 
| 151     ACT_AS_PTR(fPtr); | 144     ACT_AS_PTR(fPtr); | 
| 152 private: | 145 private: | 
| 153     T* fPtr; | 146     T* fPtr; | 
| 154 }; | 147 }; | 
| 155 | 148 | 
| 156 // Like Optional, but ptr must not be NULL. | 149 // Like Optional, but ptr must not be NULL. | 
| 157 template <typename T> | 150 template <typename T> | 
| 158 class Adopted : SkNoncopyable { | 151 class Adopted : SkNoncopyable { | 
| 159 public: | 152 public: | 
| 160     Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); } | 153     Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); } | 
| 161     Adopted(Adopted* source) { | 154     Adopted(Adopted* source) { | 
| 162         // Transfer ownership from source to this. | 155         // Transfer ownership from source to this. | 
| 163         fPtr = source->fPtr; | 156         fPtr = source->fPtr; | 
| 164         source->fPtr = NULL; | 157         source->fPtr = NULL; | 
| 165     } | 158     } | 
| 166     ~Adopted() { if (fPtr) fPtr->~T(); } | 159     ~Adopted() { if (fPtr) fPtr->~T(); } | 
| 167 | 160 | 
| 168     ACT_AS_PTR(fPtr); | 161     ACT_AS_PTR(fPtr); | 
| 169 private: | 162 private: | 
| 170     T* fPtr; | 163     T* fPtr; | 
| 171 }; | 164 }; | 
| 172 | 165 | 
| 173 // PODArray doesn't own the pointer's memory, and we assume the data is POD. | 166 // PODArray doesn't own the pointer's memory, and we assume the data is POD. | 
| 174 template <typename T> | 167 template <typename T> | 
| 175 class PODArray { | 168 class PODArray { | 
| 176 public: | 169 public: | 
| 177     PODArray() {} |  | 
| 178     PODArray(T* ptr) : fPtr(ptr) {} | 170     PODArray(T* ptr) : fPtr(ptr) {} | 
| 179     // Default copy and assign. | 171     // Default copy and assign. | 
| 180 | 172 | 
| 181     ACT_AS_PTR(fPtr); | 173     ACT_AS_PTR(fPtr); | 
| 182 private: | 174 private: | 
| 183     T* fPtr; | 175     T* fPtr; | 
| 184 }; | 176 }; | 
| 185 | 177 | 
| 186 #undef ACT_AS_PTR | 178 #undef ACT_AS_PTR | 
| 187 | 179 | 
| 188 // Like SkBitmap, but deep copies pixels if they're not immutable. | 180 // Like SkBitmap, but deep copies pixels if they're not immutable. | 
| 189 // Using this, we guarantee the immutability of all bitmaps we record. | 181 // Using this, we guarantee the immutability of all bitmaps we record. | 
| 190 class ImmutableBitmap : SkNoncopyable { | 182 class ImmutableBitmap : SkNoncopyable { | 
| 191 public: | 183 public: | 
| 192     ImmutableBitmap() {} |  | 
| 193     explicit ImmutableBitmap(const SkBitmap& bitmap) { | 184     explicit ImmutableBitmap(const SkBitmap& bitmap) { | 
| 194         if (bitmap.isImmutable()) { | 185         if (bitmap.isImmutable()) { | 
| 195             fBitmap = bitmap; | 186             fBitmap = bitmap; | 
| 196         } else { | 187         } else { | 
| 197             bitmap.copyTo(&fBitmap); | 188             bitmap.copyTo(&fBitmap); | 
| 198         } | 189         } | 
| 199         fBitmap.setImmutable(); | 190         fBitmap.setImmutable(); | 
| 200     } | 191     } | 
| 201 | 192 | 
| 202     int width()  const { return fBitmap.width();  } | 193     int width()  const { return fBitmap.width();  } | 
| 203     int height() const { return fBitmap.height(); } | 194     int height() const { return fBitmap.height(); } | 
| 204 | 195 | 
| 205     // While the pixels are immutable, SkBitmap itself is not thread-safe, so re
     turn a copy. | 196     // While the pixels are immutable, SkBitmap itself is not thread-safe, so re
     turn a copy. | 
| 206     SkBitmap shallowCopy() const { return fBitmap; } | 197     SkBitmap shallowCopy() const { return fBitmap; } | 
| 207 private: | 198 private: | 
| 208     SkBitmap fBitmap; | 199     SkBitmap fBitmap; | 
| 209 }; | 200 }; | 
| 210 | 201 | 
| 211 // SkPath::getBounds() isn't thread safe unless we precache the bounds in a sing
     lethreaded context. | 202 // SkPath::getBounds() isn't thread safe unless we precache the bounds in a sing
     lethreaded context. | 
| 212 // SkPath::cheapComputeDirection() is similar. | 203 // SkPath::cheapComputeDirection() is similar. | 
| 213 // Recording is a convenient time to cache these, or we can delay it to between 
     record and playback. | 204 // Recording is a convenient time to cache these, or we can delay it to between 
     record and playback. | 
| 214 struct PreCachedPath : public SkPath { | 205 struct PreCachedPath : public SkPath { | 
| 215     PreCachedPath() {} |  | 
| 216     explicit PreCachedPath(const SkPath& path) : SkPath(path) { | 206     explicit PreCachedPath(const SkPath& path) : SkPath(path) { | 
| 217         this->updateBoundsCache(); | 207         this->updateBoundsCache(); | 
| 218         SkPath::Direction junk; | 208         SkPath::Direction junk; | 
| 219         (void)this->cheapComputeDirection(&junk); | 209         (void)this->cheapComputeDirection(&junk); | 
| 220     } | 210     } | 
| 221 }; | 211 }; | 
| 222 | 212 | 
| 223 // Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we pre
     cache it. | 213 // Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we pre
     cache it. | 
| 224 // This may not cover all SkMatrices used by the picture (e.g. some could be hid
     ing in a shader). | 214 // This may not cover all SkMatrices used by the picture (e.g. some could be hid
     ing in a shader). | 
| 225 struct TypedMatrix : public SkMatrix { | 215 struct TypedMatrix : public SkMatrix { | 
| 226     TypedMatrix() {} |  | 
| 227     explicit TypedMatrix(const SkMatrix& matrix) : SkMatrix(matrix) { | 216     explicit TypedMatrix(const SkMatrix& matrix) : SkMatrix(matrix) { | 
| 228         (void)this->getType(); | 217         (void)this->getType(); | 
| 229     } | 218     } | 
| 230 }; | 219 }; | 
| 231 | 220 | 
| 232 RECORD0(NoOp); | 221 RECORD0(NoOp); | 
| 233 | 222 | 
| 234 RECORD2(Restore, SkIRect, devBounds, TypedMatrix, matrix); | 223 RECORD2(Restore, SkIRect, devBounds, TypedMatrix, matrix); | 
| 235 RECORD0(Save); | 224 RECORD0(Save); | 
| 236 RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas:
     :SaveFlags, flags); | 225 RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas:
     :SaveFlags, flags); | 
| 237 | 226 | 
| 238 RECORD1(SetMatrix, TypedMatrix, matrix); | 227 RECORD1(SetMatrix, TypedMatrix, matrix); | 
| 239 | 228 | 
| 240 struct RegionOpAndAA { | 229 struct RegionOpAndAA { | 
| 241     RegionOpAndAA() {} |  | 
| 242     RegionOpAndAA(SkRegion::Op op, bool aa) : op(op), aa(aa) {} | 230     RegionOpAndAA(SkRegion::Op op, bool aa) : op(op), aa(aa) {} | 
| 243     SkRegion::Op op : 31;  // This really only needs to be 3, but there's no win
      today to do so. | 231     SkRegion::Op op : 31;  // This really only needs to be 3, but there's no win
      today to do so. | 
| 244     unsigned     aa :  1;  // MSVC won't pack an enum with an bool, so we call t
     his an unsigned. | 232     unsigned     aa :  1;  // MSVC won't pack an enum with an bool, so we call t
     his an unsigned. | 
| 245 }; | 233 }; | 
| 246 SK_COMPILE_ASSERT(sizeof(RegionOpAndAA) == 4, RegionOpAndAASize); | 234 SK_COMPILE_ASSERT(sizeof(RegionOpAndAA) == 4, RegionOpAndAASize); | 
| 247 | 235 | 
| 248 RECORD3(ClipPath,   SkIRect, devBounds, PreCachedPath,  path, RegionOpAndAA, opA
     A); | 236 RECORD3(ClipPath,   SkIRect, devBounds, PreCachedPath,  path, RegionOpAndAA, opA
     A); | 
| 249 RECORD3(ClipRRect,  SkIRect, devBounds, SkRRect,       rrect, RegionOpAndAA, opA
     A); | 237 RECORD3(ClipRRect,  SkIRect, devBounds, SkRRect,       rrect, RegionOpAndAA, opA
     A); | 
| 250 RECORD3(ClipRect,   SkIRect, devBounds, SkRect,         rect, RegionOpAndAA, opA
     A); | 238 RECORD3(ClipRect,   SkIRect, devBounds, SkRect,         rect, RegionOpAndAA, opA
     A); | 
| 251 RECORD3(ClipRegion, SkIRect, devBounds, SkRegion,     region, SkRegion::Op,    o
     p); | 239 RECORD3(ClipRegion, SkIRect, devBounds, SkRegion,     region, SkRegion::Op,    o
     p); | 
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 358 #undef RECORD0 | 346 #undef RECORD0 | 
| 359 #undef RECORD1 | 347 #undef RECORD1 | 
| 360 #undef RECORD2 | 348 #undef RECORD2 | 
| 361 #undef RECORD3 | 349 #undef RECORD3 | 
| 362 #undef RECORD4 | 350 #undef RECORD4 | 
| 363 #undef RECORD5 | 351 #undef RECORD5 | 
| 364 | 352 | 
| 365 }  // namespace SkRecords | 353 }  // namespace SkRecords | 
| 366 | 354 | 
| 367 #endif//SkRecords_DEFINED | 355 #endif//SkRecords_DEFINED | 
| OLD | NEW | 
|---|