| OLD | NEW |
| 1 #ifndef SkRecord_DEFINED | 1 #ifndef SkRecord_DEFINED |
| 2 #define SkRecord_DEFINED | 2 #define SkRecord_DEFINED |
| 3 | 3 |
| 4 #include "SkChunkAlloc.h" | 4 #include "SkChunkAlloc.h" |
| 5 #include "SkRecords.h" | 5 #include "SkRecords.h" |
| 6 #include "SkTemplates.h" | 6 #include "SkTemplates.h" |
| 7 | 7 |
| 8 // SkRecord (REC-ord) represents a sequence of SkCanvas calls, saved for future
use. | 8 // SkRecord (REC-ord) represents a sequence of SkCanvas calls, saved for future
use. |
| 9 // These future uses may include: replay, optimization, serialization, or combin
ations of those. | 9 // These future uses may include: replay, optimization, serialization, or combin
ations of those. |
| 10 // | 10 // |
| 11 // Though an enterprising user may find calling alloc(), append(), visit(), and
mutate() enough to | 11 // Though an enterprising user may find calling alloc(), append(), visit(), and
mutate() enough to |
| 12 // work with SkRecord, you probably want to look at SkRecorder which presents an
SkCanvas interface | 12 // work with SkRecord, you probably want to look at SkRecorder which presents an
SkCanvas interface |
| 13 // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into
another SkCanvas. | 13 // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into
another SkCanvas. |
| 14 // | 14 // |
| 15 // SkRecord often looks like it's compatible with any type T, but really it's co
mpatible with any | 15 // SkRecord often looks like it's compatible with any type T, but really it's co
mpatible with any |
| 16 // type T which has a static const SkRecords::Type kType. That is to say, SkRec
ord is compatible | 16 // type T which has a static const SkRecords::Type kType. That is to say, SkRec
ord is compatible |
| 17 // only with SkRecords::* structs defined in SkRecords.h. Your compiler will he
lpfully yell if you | 17 // only with SkRecords::* structs defined in SkRecords.h. Your compiler will he
lpfully yell if you |
| 18 // get this wrong. | 18 // get this wrong. |
| 19 | 19 |
| 20 class SkRecord : SkNoncopyable { | 20 class SkRecord : SkNoncopyable { |
| 21 public: | 21 public: |
| 22 SkRecord(size_t chunkBytes = 4096, unsigned firstReserveCount = 64 / sizeof(
void*)) | 22 SkRecord(size_t chunkBytes = 4096, unsigned firstReserveCount = 64 / sizeof(
void*)) |
| 23 : fAlloc(chunkBytes), fCount(0), fReserved(0), kFirstReserveCount(firstR
eserveCount) {} | 23 : fAlloc(chunkBytes), fCount(0), fReserved(0), kFirstReserveCount(firstR
eserveCount) {} |
| 24 ~SkRecord() { this->mutate(Destroyer()); } | 24 |
| 25 ~SkRecord() { |
| 26 Destroyer destroyer; |
| 27 this->mutate(destroyer); |
| 28 } |
| 29 |
| 30 unsigned count() const { return fCount; } |
| 25 | 31 |
| 26 // Accepts a visitor functor with this interface: | 32 // Accepts a visitor functor with this interface: |
| 27 // template <typename T> | 33 // template <typename T> |
| 28 // void operator()()(const T& record) { ... } | 34 // void operator()(const T& record) { ... } |
| 29 // This operator() must be defined for at least all SkRecords::*; your compi
ler will help you | 35 // This operator() must be defined for at least all SkRecords::*; your compi
ler will help you |
| 30 // get this right. | 36 // get this right. |
| 31 // | |
| 32 // f will be called on each recorded canvas call in the order they were appe
nd()ed. | |
| 33 template <typename F> | 37 template <typename F> |
| 34 void visit(F f) const { | 38 void visit(unsigned i, F& f) const { |
| 39 SkASSERT(i < this->count()); |
| 40 fRecords[i].visit(fTypes[i], f); |
| 41 } |
| 42 |
| 43 // As above. f will be called on each recorded canvas call in the order the
y were append()ed. |
| 44 template <typename F> |
| 45 void visit(F& f) const { |
| 35 for (unsigned i = 0; i < fCount; i++) { | 46 for (unsigned i = 0; i < fCount; i++) { |
| 36 fRecords[i].visit(fTypes[i], f); | 47 this->visit(i, f); |
| 37 } | 48 } |
| 38 } | 49 } |
| 39 | 50 |
| 40 // Accepts a visitor functor with this interface: | 51 // Accepts a visitor functor with this interface: |
| 41 // template <typename T> | 52 // template <typename T> |
| 42 // void operator()()(T* record) { ... } | 53 // void operator()(T* record) { ... } |
| 43 // This operator() must be defined for at least all SkRecords::*; again, you
r compiler will help | 54 // This operator() must be defined for at least all SkRecords::*; again, you
r compiler will help |
| 44 // you get this right. | 55 // you get this right. |
| 45 // | |
| 46 // f will be called on each recorded canvas call in the order they were appe
nd()ed. | |
| 47 template <typename F> | 56 template <typename F> |
| 48 void mutate(F f) { | 57 void mutate(unsigned i, F& f) { |
| 58 SkASSERT(i < this->count()); |
| 59 fRecords[i].mutate(fTypes[i], f); |
| 60 } |
| 61 |
| 62 // As above. f will be called on each recorded canvas call in the order the
y were append()ed. |
| 63 template <typename F> |
| 64 void mutate(F& f) { |
| 49 for (unsigned i = 0; i < fCount; i++) { | 65 for (unsigned i = 0; i < fCount; i++) { |
| 50 fRecords[i].mutate(fTypes[i], f); | 66 this->mutate(i, f); |
| 51 } | 67 } |
| 52 } | 68 } |
| 53 | 69 |
| 54 // Allocate contiguous space for count Ts, to be destroyed (not just freed)
when the SkRecord is | 70 // Allocate contiguous space for count Ts, to be destroyed (not just freed)
when the SkRecord is |
| 55 // destroyed. For classes with constructors, placement new into this array.
Throws on failure. | 71 // destroyed. For classes with constructors, placement new into this array.
Throws on failure. |
| 56 // Here T can really be any class, not just those from SkRecords. | 72 // Here T can really be any class, not just those from SkRecords. |
| 57 template <typename T> | 73 template <typename T> |
| 58 T* alloc(unsigned count = 1) { | 74 T* alloc(unsigned count = 1) { |
| 59 return (T*)fAlloc.allocThrow(sizeof(T) * count); | 75 return (T*)fAlloc.allocThrow(sizeof(T) * count); |
| 60 } | 76 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 T* alloc(SkRecord* record) { | 163 T* alloc(SkRecord* record) { |
| 148 if (IsLarge<T>()) { | 164 if (IsLarge<T>()) { |
| 149 fRecord = (uintptr_t)record->alloc<T>(); | 165 fRecord = (uintptr_t)record->alloc<T>(); |
| 150 } | 166 } |
| 151 return this->ptr<T>(); | 167 return this->ptr<T>(); |
| 152 } | 168 } |
| 153 | 169 |
| 154 // Visit this record with functor F (see public API above) assuming the
record we're | 170 // Visit this record with functor F (see public API above) assuming the
record we're |
| 155 // pointing to has this type. | 171 // pointing to has this type. |
| 156 template <typename F> | 172 template <typename F> |
| 157 void visit(Type8 type, F f) const { | 173 void visit(Type8 type, F& f) const { |
| 158 #define CASE(T) case SkRecords::T##_Type: return f(*this->ptr<SkRecords:
:T>()); | 174 #define CASE(T) case SkRecords::T##_Type: return f(*this->ptr<SkRecords:
:T>()); |
| 159 switch(type) { SK_RECORD_TYPES(CASE) } | 175 switch(type) { SK_RECORD_TYPES(CASE) } |
| 160 #undef CASE | 176 #undef CASE |
| 161 } | 177 } |
| 162 | 178 |
| 163 // Mutate this record with functor F (see public API above) assuming the
record we're | 179 // Mutate this record with functor F (see public API above) assuming the
record we're |
| 164 // pointing to has this type. | 180 // pointing to has this type. |
| 165 template <typename F> | 181 template <typename F> |
| 166 void mutate(Type8 type, F f) { | 182 void mutate(Type8 type, F& f) { |
| 167 #define CASE(T) case SkRecords::T##_Type: return f(this->ptr<SkRecords::
T>()); | 183 #define CASE(T) case SkRecords::T##_Type: return f(this->ptr<SkRecords::
T>()); |
| 168 switch(type) { SK_RECORD_TYPES(CASE) } | 184 switch(type) { SK_RECORD_TYPES(CASE) } |
| 169 #undef CASE | 185 #undef CASE |
| 170 } | 186 } |
| 171 | 187 |
| 172 private: | 188 private: |
| 173 template <typename T> | 189 template <typename T> |
| 174 T* ptr() const { return (T*)(IsLarge<T>() ? (void*)fRecord : &fRecord);
} | 190 T* ptr() const { return (T*)(IsLarge<T>() ? (void*)fRecord : &fRecord);
} |
| 175 | 191 |
| 176 // Is T too big to fit directly into a uintptr_t, neededing external all
ocation? | 192 // Is T too big to fit directly into a uintptr_t, neededing external all
ocation? |
| (...skipping 12 matching lines...) Expand all Loading... |
| 189 SkChunkAlloc fAlloc; | 205 SkChunkAlloc fAlloc; |
| 190 SkAutoTMalloc<Record> fRecords; | 206 SkAutoTMalloc<Record> fRecords; |
| 191 SkAutoTMalloc<Type8> fTypes; | 207 SkAutoTMalloc<Type8> fTypes; |
| 192 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. | 208 // fCount and fReserved measure both fRecords and fTypes, which always grow
in lock step. |
| 193 unsigned fCount; | 209 unsigned fCount; |
| 194 unsigned fReserved; | 210 unsigned fReserved; |
| 195 const unsigned kFirstReserveCount; | 211 const unsigned kFirstReserveCount; |
| 196 }; | 212 }; |
| 197 | 213 |
| 198 #endif//SkRecord_DEFINED | 214 #endif//SkRecord_DEFINED |
| OLD | NEW |