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 SkRecord_DEFINED | 8 #ifndef SkRecord_DEFINED |
9 #define SkRecord_DEFINED | 9 #define SkRecord_DEFINED |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 // get this wrong. | 26 // get this wrong. |
27 | 27 |
28 class SkRecord : SkNoncopyable { | 28 class SkRecord : SkNoncopyable { |
29 public: | 29 public: |
30 SkRecord(size_t chunkBytes = 4096, unsigned firstReserveCount = 64 / sizeof(
void*)) | 30 SkRecord(size_t chunkBytes = 4096, unsigned firstReserveCount = 64 / sizeof(
void*)) |
31 : fAlloc(chunkBytes), fCount(0), fReserved(0), kFirstReserveCount(firstR
eserveCount) {} | 31 : fAlloc(chunkBytes), fCount(0), fReserved(0), kFirstReserveCount(firstR
eserveCount) {} |
32 | 32 |
33 ~SkRecord() { | 33 ~SkRecord() { |
34 Destroyer destroyer; | 34 Destroyer destroyer; |
35 for (unsigned i = 0; i < this->count(); i++) { | 35 for (unsigned i = 0; i < this->count(); i++) { |
36 this->mutate(i, destroyer); | 36 this->mutate<void>(i, destroyer); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 // Returns the number of canvas commands in this SkRecord. | 40 // Returns the number of canvas commands in this SkRecord. |
41 unsigned count() const { return fCount; } | 41 unsigned count() const { return fCount; } |
42 | 42 |
43 // Visit the i-th canvas command with a functor matching this interface: | 43 // Visit the i-th canvas command with a functor matching this interface: |
44 // template <typename T> | 44 // template <typename T> |
45 // void operator()(const T& record) { ... } | 45 // R operator()(const T& record) { ... } |
46 // This operator() must be defined for at least all SkRecords::*. | 46 // This operator() must be defined for at least all SkRecords::*. |
47 template <typename F> | 47 template <typename R, typename F> |
48 void visit(unsigned i, F& f) const { | 48 R visit(unsigned i, F& f) const { |
49 SkASSERT(i < this->count()); | 49 SkASSERT(i < this->count()); |
50 fRecords[i].visit(fTypes[i], f); | 50 return fRecords[i].visit<R>(fTypes[i], f); |
51 } | 51 } |
52 | 52 |
53 // Mutate the i-th canvas command with a functor matching this interface: | 53 // Mutate the i-th canvas command with a functor matching this interface: |
54 // template <typename T> | 54 // template <typename T> |
55 // void operator()(T* record) { ... } | 55 // R operator()(T* record) { ... } |
56 // This operator() must be defined for at least all SkRecords::*. | 56 // This operator() must be defined for at least all SkRecords::*. |
57 template <typename F> | 57 template <typename R, typename F> |
58 void mutate(unsigned i, F& f) { | 58 R mutate(unsigned i, F& f) { |
59 SkASSERT(i < this->count()); | 59 SkASSERT(i < this->count()); |
60 fRecords[i].mutate(fTypes[i], f); | 60 return fRecords[i].mutate<R>(fTypes[i], f); |
61 } | 61 } |
| 62 // TODO: It'd be nice to infer R from F for visit and mutate if we ever get
std::result_of. |
62 | 63 |
63 // Allocate contiguous space for count Ts, to be freed when the SkRecord is
destroyed. | 64 // Allocate contiguous space for count Ts, to be freed when the SkRecord is
destroyed. |
64 // Here T can be any class, not just those from SkRecords. Throws on failur
e. | 65 // Here T can be any class, not just those from SkRecords. Throws on failur
e. |
65 template <typename T> | 66 template <typename T> |
66 T* alloc(unsigned count = 1) { | 67 T* alloc(unsigned count = 1) { |
67 return (T*)fAlloc.allocThrow(sizeof(T) * count); | 68 return (T*)fAlloc.allocThrow(sizeof(T) * count); |
68 } | 69 } |
69 | 70 |
70 // Add a new command of type T to the end of this SkRecord. | 71 // Add a new command of type T to the end of this SkRecord. |
71 // You are expected to placement new an object of type T onto this pointer. | 72 // You are expected to placement new an object of type T onto this pointer. |
(...skipping 10 matching lines...) Expand all Loading... |
82 } | 83 } |
83 | 84 |
84 // Replace the i-th command with a new command of type T. | 85 // Replace the i-th command with a new command of type T. |
85 // You are expected to placement new an object of type T onto this pointer. | 86 // You are expected to placement new an object of type T onto this pointer. |
86 // References to the original command are invalidated. | 87 // References to the original command are invalidated. |
87 template <typename T> | 88 template <typename T> |
88 T* replace(unsigned i) { | 89 T* replace(unsigned i) { |
89 SkASSERT(i < this->count()); | 90 SkASSERT(i < this->count()); |
90 | 91 |
91 Destroyer destroyer; | 92 Destroyer destroyer; |
92 this->mutate(i, destroyer); | 93 this->mutate<void>(i, destroyer); |
93 | 94 |
94 fTypes[i] = T::kType; | 95 fTypes[i] = T::kType; |
95 return fRecords[i].set(this->allocCommand<T>()); | 96 return fRecords[i].set(this->allocCommand<T>()); |
96 } | 97 } |
97 | 98 |
98 // Replace the i-th command with a new command of type T. | 99 // Replace the i-th command with a new command of type T. |
99 // You are expected to placement new an object of type T onto this pointer. | 100 // You are expected to placement new an object of type T onto this pointer. |
100 // You must show proof that you've already adopted the existing command. | 101 // You must show proof that you've already adopted the existing command. |
101 template <typename T, typename Existing> | 102 template <typename T, typename Existing> |
102 T* replace(unsigned i, const SkRecords::Adopted<Existing>& proofOfAdoption)
{ | 103 T* replace(unsigned i, const SkRecords::Adopted<Existing>& proofOfAdoption)
{ |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 fPtr = ptr; | 185 fPtr = ptr; |
185 return ptr; | 186 return ptr; |
186 } | 187 } |
187 | 188 |
188 // Get the data in fAlloc, assuming it's of type T. | 189 // Get the data in fAlloc, assuming it's of type T. |
189 template <typename T> | 190 template <typename T> |
190 T* ptr() const { return (T*)fPtr; } | 191 T* ptr() const { return (T*)fPtr; } |
191 | 192 |
192 // Visit this record with functor F (see public API above) assuming the
record we're | 193 // Visit this record with functor F (see public API above) assuming the
record we're |
193 // pointing to has this type. | 194 // pointing to has this type. |
194 template <typename F> | 195 template <typename R, typename F> |
195 void visit(Type8 type, F& f) const { | 196 R visit(Type8 type, F& f) const { |
196 #define CASE(T) case SkRecords::T##_Type: return f(*this->ptr<SkRecords:
:T>()); | 197 #define CASE(T) case SkRecords::T##_Type: return f(*this->ptr<SkRecords:
:T>()); |
197 switch(type) { SK_RECORD_TYPES(CASE) } | 198 switch(type) { SK_RECORD_TYPES(CASE) } |
198 #undef CASE | 199 #undef CASE |
| 200 SkDEBUGFAIL("Unreachable"); |
| 201 return R(); |
199 } | 202 } |
200 | 203 |
201 // Mutate this record with functor F (see public API above) assuming the
record we're | 204 // Mutate this record with functor F (see public API above) assuming the
record we're |
202 // pointing to has this type. | 205 // pointing to has this type. |
203 template <typename F> | 206 template <typename R, typename F> |
204 void mutate(Type8 type, F& f) { | 207 R mutate(Type8 type, F& f) { |
205 #define CASE(T) case SkRecords::T##_Type: return f(this->ptr<SkRecords::
T>()); | 208 #define CASE(T) case SkRecords::T##_Type: return f(this->ptr<SkRecords::
T>()); |
206 switch(type) { SK_RECORD_TYPES(CASE) } | 209 switch(type) { SK_RECORD_TYPES(CASE) } |
207 #undef CASE | 210 #undef CASE |
| 211 SkDEBUGFAIL("Unreachable"); |
| 212 return R(); |
208 } | 213 } |
209 | 214 |
210 private: | 215 private: |
211 void* fPtr; | 216 void* fPtr; |
212 }; | 217 }; |
213 | 218 |
214 // fAlloc needs to be a data structure which can append variable length data
in contiguous | 219 // fAlloc needs to be a data structure which can append variable length data
in contiguous |
215 // chunks, returning a stable handle to that data for later retrieval. | 220 // chunks, returning a stable handle to that data for later retrieval. |
216 // | 221 // |
217 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to | 222 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to |
218 // support efficient forward iteration. (They don't need to be contiguous o
r indexable.) | 223 // support efficient forward iteration. (They don't need to be contiguous o
r indexable.) |
219 | 224 |
220 SkChunkAlloc fAlloc; | 225 SkChunkAlloc fAlloc; |
221 SkAutoTMalloc<Record> fRecords; | 226 SkAutoTMalloc<Record> fRecords; |
222 SkAutoTMalloc<Type8> fTypes; | 227 SkAutoTMalloc<Type8> fTypes; |
223 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. | 228 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. |
224 unsigned fCount; | 229 unsigned fCount; |
225 unsigned fReserved; | 230 unsigned fReserved; |
226 const unsigned kFirstReserveCount; | 231 const unsigned kFirstReserveCount; |
227 }; | 232 }; |
228 | 233 |
229 #endif//SkRecord_DEFINED | 234 #endif//SkRecord_DEFINED |
OLD | NEW |