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 13 matching lines...) Expand all Loading... |
24 // type T which has a static const SkRecords::Type kType. That is to say, SkRec
ord is compatible | 24 // type T which has a static const SkRecords::Type kType. That is to say, SkRec
ord is compatible |
25 // only with SkRecords::* structs defined in SkRecords.h. Your compiler will he
lpfully yell if you | 25 // only with SkRecords::* structs defined in SkRecords.h. Your compiler will he
lpfully yell if you |
26 // get this wrong. | 26 // get this wrong. |
27 | 27 |
28 class SkRecord : SkNoncopyable { | 28 class SkRecord : SkNoncopyable { |
29 enum { | 29 enum { |
30 kFirstReserveCount = 64 / sizeof(void*), | 30 kFirstReserveCount = 64 / sizeof(void*), |
31 }; | 31 }; |
32 public: | 32 public: |
33 SkRecord() : fCount(0), fReserved(0) {} | 33 SkRecord() : fCount(0), fReserved(0) {} |
34 | 34 ~SkRecord(); |
35 ~SkRecord() { | |
36 Destroyer destroyer; | |
37 for (unsigned i = 0; i < this->count(); i++) { | |
38 this->mutate<void>(i, destroyer); | |
39 } | |
40 } | |
41 | 35 |
42 // Returns the number of canvas commands in this SkRecord. | 36 // Returns the number of canvas commands in this SkRecord. |
43 unsigned count() const { return fCount; } | 37 unsigned count() const { return fCount; } |
44 | 38 |
45 // Visit the i-th canvas command with a functor matching this interface: | 39 // Visit the i-th canvas command with a functor matching this interface: |
46 // template <typename T> | 40 // template <typename T> |
47 // R operator()(const T& record) { ... } | 41 // R operator()(const T& record) { ... } |
48 // This operator() must be defined for at least all SkRecords::*. | 42 // This operator() must be defined for at least all SkRecords::*. |
49 template <typename R, typename F> | 43 template <typename R, typename F> |
50 R visit(unsigned i, F& f) const { | 44 R visit(unsigned i, F& f) const { |
(...skipping 18 matching lines...) Expand all Loading... |
69 T* alloc(size_t count = 1) { | 63 T* alloc(size_t count = 1) { |
70 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. | 64 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. |
71 return (T*)fAlloc.alloc(sizeof(T) * count, SK_MALLOC_THROW); | 65 return (T*)fAlloc.alloc(sizeof(T) * count, SK_MALLOC_THROW); |
72 } | 66 } |
73 | 67 |
74 // Add a new command of type T to the end of this SkRecord. | 68 // Add a new command of type T to the end of this SkRecord. |
75 // You are expected to placement new an object of type T onto this pointer. | 69 // You are expected to placement new an object of type T onto this pointer. |
76 template <typename T> | 70 template <typename T> |
77 T* append() { | 71 T* append() { |
78 if (fCount == fReserved) { | 72 if (fCount == fReserved) { |
79 fReserved = SkTMax<unsigned>(kFirstReserveCount, fReserved*2); | 73 this->grow(); |
80 fRecords.realloc(fReserved); | |
81 fTypes.realloc(fReserved); | |
82 } | 74 } |
83 | |
84 fTypes[fCount] = T::kType; | 75 fTypes[fCount] = T::kType; |
85 return fRecords[fCount++].set(this->allocCommand<T>()); | 76 return fRecords[fCount++].set(this->allocCommand<T>()); |
86 } | 77 } |
87 | 78 |
88 // Replace the i-th command with a new command of type T. | 79 // Replace the i-th command with a new command of type T. |
89 // You are expected to placement new an object of type T onto this pointer. | 80 // You are expected to placement new an object of type T onto this pointer. |
90 // References to the original command are invalidated. | 81 // References to the original command are invalidated. |
91 template <typename T> | 82 template <typename T> |
92 T* replace(unsigned i) { | 83 T* replace(unsigned i) { |
93 SkASSERT(i < this->count()); | 84 SkASSERT(i < this->count()); |
(...skipping 14 matching lines...) Expand all Loading... |
108 | 99 |
109 SkASSERT(Existing::kType == fTypes[i]); | 100 SkASSERT(Existing::kType == fTypes[i]); |
110 SkASSERT(proofOfAdoption == fRecords[i].ptr<Existing>()); | 101 SkASSERT(proofOfAdoption == fRecords[i].ptr<Existing>()); |
111 | 102 |
112 fTypes[i] = T::kType; | 103 fTypes[i] = T::kType; |
113 return fRecords[i].set(this->allocCommand<T>()); | 104 return fRecords[i].set(this->allocCommand<T>()); |
114 } | 105 } |
115 | 106 |
116 // Does not return the bytes in any pointers embedded in the Records; caller
s | 107 // Does not return the bytes in any pointers embedded in the Records; caller
s |
117 // need to iterate with a visitor to measure those they care for. | 108 // need to iterate with a visitor to measure those they care for. |
118 size_t bytesUsed() const { return fAlloc.approxBytesAllocated() + | 109 size_t bytesUsed() const; |
119 fReserved * (sizeof(Record) + sizeof(Type8
)) + | |
120 sizeof(SkRecord); } | |
121 | 110 |
122 private: | 111 private: |
123 // Implementation notes! | 112 // Implementation notes! |
124 // | 113 // |
125 // 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 |
126 // records representing each canvas draw call are stored: | 115 // records representing each canvas draw call are stored: |
127 // | 116 // |
128 // fRecords: [*][*][*]... | 117 // fRecords: [*][*][*]... |
129 // | | | | 118 // | | | |
130 // | | | | 119 // | | | |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // We could just return NULL but it's sort of confusing to return NULL on su
ccess. | 166 // We could just return NULL but it's sort of confusing to return NULL on su
ccess. |
178 template <typename T> | 167 template <typename T> |
179 SK_WHEN(SkTIsEmpty<T>, T*) allocCommand() { | 168 SK_WHEN(SkTIsEmpty<T>, T*) allocCommand() { |
180 static T singleton = {}; | 169 static T singleton = {}; |
181 return &singleton; | 170 return &singleton; |
182 } | 171 } |
183 | 172 |
184 template <typename T> | 173 template <typename T> |
185 SK_WHEN(!SkTIsEmpty<T>, T*) allocCommand() { return this->alloc<T>(); } | 174 SK_WHEN(!SkTIsEmpty<T>, T*) allocCommand() { return this->alloc<T>(); } |
186 | 175 |
| 176 // Called when we've run out of room to record new commands. |
| 177 void grow(); |
| 178 |
187 // An untyped pointer to some bytes in fAlloc. This is the interface for po
lymorphic dispatch: | 179 // An untyped pointer to some bytes in fAlloc. This is the interface for po
lymorphic dispatch: |
188 // visit() and mutate() work with the parallel fTypes array to do the work o
f a vtable. | 180 // visit() and mutate() work with the parallel fTypes array to do the work o
f a vtable. |
189 struct Record { | 181 struct Record { |
190 public: | 182 public: |
191 // Point this record to its data in fAlloc. Returns ptr for convenience
. | 183 // Point this record to its data in fAlloc. Returns ptr for convenience
. |
192 template <typename T> | 184 template <typename T> |
193 T* set(T* ptr) { | 185 T* set(T* ptr) { |
194 fPtr = ptr; | 186 fPtr = ptr; |
195 return ptr; | 187 return ptr; |
196 } | 188 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 unsigned fReserved; | 228 unsigned fReserved; |
237 SkAutoTMalloc<Record> fRecords; | 229 SkAutoTMalloc<Record> fRecords; |
238 SkAutoTMalloc<Type8> fTypes; | 230 SkAutoTMalloc<Type8> fTypes; |
239 SkVarAlloc fAlloc; | 231 SkVarAlloc fAlloc; |
240 // Strangely the order of these fields matters. If the unsigneds don't go f
irst we're 56 bytes. | 232 // Strangely the order of these fields matters. If the unsigneds don't go f
irst we're 56 bytes. |
241 // tomhudson and mtklein have no idea why. | 233 // tomhudson and mtklein have no idea why. |
242 }; | 234 }; |
243 SK_COMPILE_ASSERT(sizeof(SkRecord) <= 48, SkRecordSize); | 235 SK_COMPILE_ASSERT(sizeof(SkRecord) <= 48, SkRecordSize); |
244 | 236 |
245 #endif//SkRecord_DEFINED | 237 #endif//SkRecord_DEFINED |
OLD | NEW |