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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // You are expected to placement new an object of type T onto this pointer. | 70 // You are expected to placement new an object of type T onto this pointer. |
71 template <typename T> | 71 template <typename T> |
72 T* append() { | 72 T* append() { |
73 if (fCount == fReserved) { | 73 if (fCount == fReserved) { |
74 fReserved = SkTMax(kFirstReserveCount, fReserved*2); | 74 fReserved = SkTMax(kFirstReserveCount, fReserved*2); |
75 fRecords.realloc(fReserved); | 75 fRecords.realloc(fReserved); |
76 fTypes.realloc(fReserved); | 76 fTypes.realloc(fReserved); |
77 } | 77 } |
78 | 78 |
79 fTypes[fCount] = T::kType; | 79 fTypes[fCount] = T::kType; |
80 return fRecords[fCount++].set(this->alloc<T>()); | 80 return fRecords[fCount++].set(this->allocCommand<T>()); |
81 } | 81 } |
82 | 82 |
83 // Replace the i-th command with a new command of type T. | 83 // Replace the i-th command with a new command of type T. |
84 // You are expected to placement new an object of type T onto this pointer. | 84 // You are expected to placement new an object of type T onto this pointer. |
85 // References to the original command are invalidated. | 85 // References to the original command are invalidated. |
86 template <typename T> | 86 template <typename T> |
87 T* replace(unsigned i) { | 87 T* replace(unsigned i) { |
88 SkASSERT(i < this->count()); | 88 SkASSERT(i < this->count()); |
89 | 89 |
90 Destroyer destroyer; | 90 Destroyer destroyer; |
91 this->mutate(i, destroyer); | 91 this->mutate(i, destroyer); |
92 | 92 |
93 fTypes[i] = T::kType; | 93 fTypes[i] = T::kType; |
94 return fRecords[i].set(this->alloc<T>()); | 94 return fRecords[i].set(this->allocCommand<T>()); |
95 } | 95 } |
96 | 96 |
97 // Replace the i-th command with a new command of type T. | 97 // Replace the i-th command with a new command of type T. |
98 // You are expected to placement new an object of type T onto this pointer. | 98 // You are expected to placement new an object of type T onto this pointer. |
99 // You must show proof that you've already adopted the existing command. | 99 // You must show proof that you've already adopted the existing command. |
100 template <typename T, typename Existing> | 100 template <typename T, typename Existing> |
101 T* replace(unsigned i, const SkRecords::Adopted<Existing>& proofOfAdoption) { | 101 T* replace(unsigned i, const SkRecords::Adopted<Existing>& proofOfAdoption) { |
102 SkASSERT(i < this->count()); | 102 SkASSERT(i < this->count()); |
103 | 103 |
104 SkASSERT(Existing::kType == fTypes[i]); | 104 SkASSERT(Existing::kType == fTypes[i]); |
105 SkASSERT(proofOfAdoption == fRecords[i].ptr<Existing>()); | 105 SkASSERT(proofOfAdoption == fRecords[i].ptr<Existing>()); |
106 | 106 |
107 fTypes[i] = T::kType; | 107 fTypes[i] = T::kType; |
108 return fRecords[i].set(this->alloc<T>()); | 108 return fRecords[i].set(this->allocCommand<T>()); |
109 } | 109 } |
110 | 110 |
111 private: | 111 private: |
112 // Implementation notes! | 112 // Implementation notes! |
113 // | 113 // |
114 // Logically an SkRecord is structured as an array of pointers into a big ch unk of memory where | 114 // Logically an SkRecord is structured as an array of pointers into a big ch unk of memory where |
115 // records representing each canvas draw call are stored: | 115 // records representing each canvas draw call are stored: |
116 // | 116 // |
117 // fRecords: [*][*][*]... | 117 // fRecords: [*][*][*]... |
118 // | | | | 118 // | | | |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 struct Type8 { | 155 struct Type8 { |
156 public: | 156 public: |
157 // This intentionally converts implicitly back and forth. | 157 // This intentionally converts implicitly back and forth. |
158 Type8(SkRecords::Type type) : fType(type) { SkASSERT(*this == type); } | 158 Type8(SkRecords::Type type) : fType(type) { SkASSERT(*this == type); } |
159 operator SkRecords::Type () { return (SkRecords::Type)fType; } | 159 operator SkRecords::Type () { return (SkRecords::Type)fType; } |
160 | 160 |
161 private: | 161 private: |
162 uint8_t fType; | 162 uint8_t fType; |
163 }; | 163 }; |
164 | 164 |
165 template <typename T> | |
166 T* allocCommand() { | |
167 struct AddChar : public T { char unused; }; | |
168 if (sizeof(AddChar) == sizeof(char)) { | |
169 // T is an empty struct. | |
170 return NULL; | |
171 } | |
172 return this->alloc<T>(); | |
173 } | |
bungeman-skia
2014/05/07 14:35:24
I understand what you're doing here, but eck. Can
mtklein
2014/05/07 15:27:43
Done. Leaving rearranging / renaming the traits f
| |
174 | |
165 // An untyped pointer to some bytes in fAlloc. This is the interface for po lymorphic dispatch: | 175 // An untyped pointer to some bytes in fAlloc. This is the interface for po lymorphic dispatch: |
166 // visit() and mutate() work with the parallel fTypes array to do the work o f a vtable. | 176 // visit() and mutate() work with the parallel fTypes array to do the work o f a vtable. |
167 struct Record { | 177 struct Record { |
168 public: | 178 public: |
169 // Point this record to its data in fAlloc. Returns ptr for convenience . | 179 // Point this record to its data in fAlloc. Returns ptr for convenience . |
170 template <typename T> | 180 template <typename T> |
171 T* set(T* ptr) { | 181 T* set(T* ptr) { |
172 fPtr = ptr; | 182 fPtr = ptr; |
173 return ptr; | 183 return ptr; |
174 } | 184 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 SkChunkAlloc fAlloc; | 218 SkChunkAlloc fAlloc; |
209 SkAutoTMalloc<Record> fRecords; | 219 SkAutoTMalloc<Record> fRecords; |
210 SkAutoTMalloc<Type8> fTypes; | 220 SkAutoTMalloc<Type8> fTypes; |
211 // fCount and fReserved measure both fRecords and fTypes, which always grow in lock step. | 221 // fCount and fReserved measure both fRecords and fTypes, which always grow in lock step. |
212 unsigned fCount; | 222 unsigned fCount; |
213 unsigned fReserved; | 223 unsigned fReserved; |
214 const unsigned kFirstReserveCount; | 224 const unsigned kFirstReserveCount; |
215 }; | 225 }; |
216 | 226 |
217 #endif//SkRecord_DEFINED | 227 #endif//SkRecord_DEFINED |
OLD | NEW |