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