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" | |
12 #include "SkRecords.h" | 11 #include "SkRecords.h" |
13 #include "SkTLogic.h" | 12 #include "SkTLogic.h" |
14 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
| 14 #include "SkVarAlloc.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 { | 29 enum { |
30 kChunkBytes = 4096, | |
31 kFirstReserveCount = 64 / sizeof(void*), | 30 kFirstReserveCount = 64 / sizeof(void*), |
32 }; | 31 }; |
33 public: | 32 public: |
34 SkRecord() : fAlloc(kChunkBytes), fCount(0), fReserved(0) {} | 33 SkRecord() : fAlloc(1024, 2.0f), fCount(0), fReserved(0) {} |
35 | 34 |
36 ~SkRecord() { | 35 ~SkRecord() { |
37 Destroyer destroyer; | 36 Destroyer destroyer; |
38 for (unsigned i = 0; i < this->count(); i++) { | 37 for (unsigned i = 0; i < this->count(); i++) { |
39 this->mutate<void>(i, destroyer); | 38 this->mutate<void>(i, destroyer); |
40 } | 39 } |
41 } | 40 } |
42 | 41 |
43 // Returns the number of canvas commands in this SkRecord. | 42 // Returns the number of canvas commands in this SkRecord. |
44 unsigned count() const { return fCount; } | 43 unsigned count() const { return fCount; } |
(...skipping 17 matching lines...) Expand all Loading... |
62 SkASSERT(i < this->count()); | 61 SkASSERT(i < this->count()); |
63 return fRecords[i].mutate<R>(fTypes[i], f); | 62 return fRecords[i].mutate<R>(fTypes[i], f); |
64 } | 63 } |
65 // TODO: It'd be nice to infer R from F for visit and mutate if we ever get
std::result_of. | 64 // TODO: It'd be nice to infer R from F for visit and mutate if we ever get
std::result_of. |
66 | 65 |
67 // Allocate contiguous space for count Ts, to be freed when the SkRecord is
destroyed. | 66 // Allocate contiguous space for count Ts, to be freed when the SkRecord is
destroyed. |
68 // Here T can be any class, not just those from SkRecords. Throws on failur
e. | 67 // Here T can be any class, not just those from SkRecords. Throws on failur
e. |
69 template <typename T> | 68 template <typename T> |
70 T* alloc(size_t count = 1) { | 69 T* alloc(size_t count = 1) { |
71 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. | 70 // Bump up to the next pointer width if needed, so all allocations start
pointer-aligned. |
72 return (T*)fAlloc.allocThrow(SkAlignPtr(sizeof(T) * count)); | 71 return (T*)fAlloc.alloc(sizeof(T) * count, SK_MALLOC_THROW); |
73 } | 72 } |
74 | 73 |
75 // Add a new command of type T to the end of this SkRecord. | 74 // Add a new command of type T to the end of this SkRecord. |
76 // You are expected to placement new an object of type T onto this pointer. | 75 // You are expected to placement new an object of type T onto this pointer. |
77 template <typename T> | 76 template <typename T> |
78 T* append() { | 77 T* append() { |
79 if (fCount == fReserved) { | 78 if (fCount == fReserved) { |
80 fReserved = SkTMax<unsigned>(kFirstReserveCount, fReserved*2); | 79 fReserved = SkTMax<unsigned>(kFirstReserveCount, fReserved*2); |
81 fRecords.realloc(fReserved); | 80 fRecords.realloc(fReserved); |
82 fTypes.realloc(fReserved); | 81 fTypes.realloc(fReserved); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 private: | 218 private: |
220 void* fPtr; | 219 void* fPtr; |
221 }; | 220 }; |
222 | 221 |
223 // fAlloc needs to be a data structure which can append variable length data
in contiguous | 222 // fAlloc needs to be a data structure which can append variable length data
in contiguous |
224 // chunks, returning a stable handle to that data for later retrieval. | 223 // chunks, returning a stable handle to that data for later retrieval. |
225 // | 224 // |
226 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to | 225 // fRecords and fTypes need to be data structures that can append fixed leng
th data, and need to |
227 // support efficient random access and forward iteration. (They don't need
to be contiguous.) | 226 // support efficient random access and forward iteration. (They don't need
to be contiguous.) |
228 | 227 |
229 SkChunkAlloc fAlloc; | 228 SkVarAlloc fAlloc; |
230 SkAutoTMalloc<Record> fRecords; | 229 SkAutoTMalloc<Record> fRecords; |
231 SkAutoTMalloc<Type8> fTypes; | 230 SkAutoTMalloc<Type8> fTypes; |
232 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. | 231 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. |
233 unsigned fCount; | 232 unsigned fCount; |
234 unsigned fReserved; | 233 unsigned fReserved; |
235 }; | 234 }; |
236 | 235 |
237 #endif//SkRecord_DEFINED | 236 #endif//SkRecord_DEFINED |
OLD | NEW |