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