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 |
11 #include "SkChunkAlloc.h" | 11 #include "SkChunkAlloc.h" |
12 #include "SkRecords.h" | 12 #include "SkRecords.h" |
13 #include "SkTLogic.h" | 13 #include "SkTLogic.h" |
14 #include "SkTemplates.h" | 14 #include "SkTemplates.h" |
15 | 15 |
16 // SkRecord (REC-ord) represents a sequence of SkCanvas calls, saved for future
use. | 16 // SkRecord (REC-ord) represents a sequence of SkCanvas calls, saved for future
use. |
17 // These future uses may include: replay, optimization, serialization, or combin
ations of those. | 17 // These future uses may include: replay, optimization, serialization, or combin
ations of those. |
18 // | 18 // |
19 // Though an enterprising user may find calling alloc(), append(), visit(), and
mutate() enough to | 19 // Though an enterprising user may find calling alloc(), append(), visit(), and
mutate() enough to |
20 // work with SkRecord, you probably want to look at SkRecorder which presents an
SkCanvas interface | 20 // work with SkRecord, you probably want to look at SkRecorder which presents an
SkCanvas interface |
21 // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into
another SkCanvas. | 21 // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into
another SkCanvas. |
22 // | 22 // |
23 // SkRecord often looks like it's compatible with any type T, but really it's co
mpatible with any | 23 // SkRecord often looks like it's compatible with any type T, but really it's co
mpatible with any |
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 { |
| 30 kChunkBytes = 4096, |
| 31 kFirstReserveCount = 64 / sizeof(void*), |
| 32 }; |
29 public: | 33 public: |
30 SkRecord(size_t chunkBytes = 4096, unsigned firstReserveCount = 64 / sizeof(
void*)) | 34 SkRecord() : fAlloc(kChunkBytes), fCount(0), fReserved(0) {} |
31 : fAlloc(chunkBytes), fCount(0), fReserved(0), kFirstReserveCount(firstR
eserveCount) {} | |
32 | 35 |
33 ~SkRecord() { | 36 ~SkRecord() { |
34 Destroyer destroyer; | 37 Destroyer destroyer; |
35 for (unsigned i = 0; i < this->count(); i++) { | 38 for (unsigned i = 0; i < this->count(); i++) { |
36 this->mutate<void>(i, destroyer); | 39 this->mutate<void>(i, destroyer); |
37 } | 40 } |
38 } | 41 } |
39 | 42 |
40 // Returns the number of canvas commands in this SkRecord. | 43 // Returns the number of canvas commands in this SkRecord. |
41 unsigned count() const { return fCount; } | 44 unsigned count() const { return fCount; } |
(...skipping 25 matching lines...) Expand all Loading... |
67 T* alloc(size_t count = 1) { | 70 T* alloc(size_t count = 1) { |
68 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. | 71 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. |
69 return (T*)fAlloc.allocThrow(SkAlignPtr(sizeof(T) * count)); | 72 return (T*)fAlloc.allocThrow(SkAlignPtr(sizeof(T) * count)); |
70 } | 73 } |
71 | 74 |
72 // Add a new command of type T to the end of this SkRecord. | 75 // Add a new command of type T to the end of this SkRecord. |
73 // You are expected to placement new an object of type T onto this pointer. | 76 // You are expected to placement new an object of type T onto this pointer. |
74 template <typename T> | 77 template <typename T> |
75 T* append() { | 78 T* append() { |
76 if (fCount == fReserved) { | 79 if (fCount == fReserved) { |
77 fReserved = SkTMax(kFirstReserveCount, fReserved*2); | 80 fReserved = SkTMax<unsigned>(kFirstReserveCount, fReserved*2); |
78 fRecords.realloc(fReserved); | 81 fRecords.realloc(fReserved); |
79 fTypes.realloc(fReserved); | 82 fTypes.realloc(fReserved); |
80 } | 83 } |
81 | 84 |
82 fTypes[fCount] = T::kType; | 85 fTypes[fCount] = T::kType; |
83 return fRecords[fCount++].set(this->allocCommand<T>()); | 86 return fRecords[fCount++].set(this->allocCommand<T>()); |
84 } | 87 } |
85 | 88 |
86 // Replace the i-th command with a new command of type T. | 89 // Replace the i-th command with a new command of type T. |
87 // You are expected to placement new an object of type T onto this pointer. | 90 // You are expected to placement new an object of type T onto this pointer. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 217 } |
215 | 218 |
216 private: | 219 private: |
217 void* fPtr; | 220 void* fPtr; |
218 }; | 221 }; |
219 | 222 |
220 // fAlloc needs to be a data structure which can append variable length data
in contiguous | 223 // fAlloc needs to be a data structure which can append variable length data
in contiguous |
221 // chunks, returning a stable handle to that data for later retrieval. | 224 // chunks, returning a stable handle to that data for later retrieval. |
222 // | 225 // |
223 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to | 226 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to |
224 // support efficient forward iteration. (They don't need to be contiguous o
r indexable.) | 227 // support efficient random access and forward iteration. (They don't need
to be contiguous.) |
225 | 228 |
226 SkChunkAlloc fAlloc; | 229 SkChunkAlloc fAlloc; |
227 SkAutoTMalloc<Record> fRecords; | 230 SkAutoTMalloc<Record> fRecords; |
228 SkAutoTMalloc<Type8> fTypes; | 231 SkAutoTMalloc<Type8> fTypes; |
229 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. | 232 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. |
230 unsigned fCount; | 233 unsigned fCount; |
231 unsigned fReserved; | 234 unsigned fReserved; |
232 const unsigned kFirstReserveCount; | |
233 }; | 235 }; |
234 | 236 |
235 #endif//SkRecord_DEFINED | 237 #endif//SkRecord_DEFINED |
OLD | NEW |