| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkWriter32_DEFINED | 10 #ifndef SkWriter32_DEFINED |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim
it) { | 72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim
it) { |
| 73 p = fExternal + fCount; | 73 p = fExternal + fCount; |
| 74 } else { | 74 } else { |
| 75 p = fInternal.append(count); | 75 p = fInternal.append(count); |
| 76 } | 76 } |
| 77 | 77 |
| 78 fCount += count; | 78 fCount += count; |
| 79 return p; | 79 return p; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // return the address of the 4byte int at the specified offset (which must | 82 // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). |
| 83 // be a multiple of 4. This does not allocate any new space, so the returned | 83 uint32_t read32At(size_t offset) { return this->atOffset(offset); } |
| 84 // address is only valid for 1 int. | 84 void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val;
} |
| 85 uint32_t* peek32(size_t offset) { | |
| 86 SkASSERT(SkAlign4(offset) == offset); | |
| 87 const int count = SkToInt(offset/4); | |
| 88 SkASSERT(count < fCount); | |
| 89 | |
| 90 if (count < this->externalCount()) { | |
| 91 return fExternal + count; | |
| 92 } | |
| 93 return &fInternal[count - this->externalCount()]; | |
| 94 } | |
| 95 | 85 |
| 96 bool writeBool(bool value) { | 86 bool writeBool(bool value) { |
| 97 this->write32(value); | 87 this->write32(value); |
| 98 return value; | 88 return value; |
| 99 } | 89 } |
| 100 | 90 |
| 101 void writeInt(int32_t value) { | 91 void writeInt(int32_t value) { |
| 102 this->write32(value); | 92 this->write32(value); |
| 103 } | 93 } |
| 104 | 94 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 198 |
| 209 /** | 199 /** |
| 210 * Computes the size (aligned to multiple of 4) need to write the string | 200 * Computes the size (aligned to multiple of 4) need to write the string |
| 211 * in a call to writeString(). If the length is not specified, it will be | 201 * in a call to writeString(). If the length is not specified, it will be |
| 212 * computed by calling strlen(). | 202 * computed by calling strlen(). |
| 213 */ | 203 */ |
| 214 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1); | 204 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1); |
| 215 | 205 |
| 216 /** | 206 /** |
| 217 * Move the cursor back to offset bytes from the beginning. | 207 * Move the cursor back to offset bytes from the beginning. |
| 218 * This has the same restrictions as peek32: offset must be <= size() and | 208 * offset must be a multiple of 4 no greater than size(). |
| 219 * offset must be a multiple of 4. | |
| 220 */ | 209 */ |
| 221 void rewindToOffset(size_t offset) { | 210 void rewindToOffset(size_t offset) { |
| 222 SkASSERT(SkAlign4(offset) == offset); | 211 SkASSERT(SkAlign4(offset) == offset); |
| 223 const int count = SkToInt(offset/4); | 212 const int count = SkToInt(offset/4); |
| 224 if (count < this->externalCount()) { | 213 if (count < this->externalCount()) { |
| 225 fInternal.setCount(0); | 214 fInternal.setCount(0); |
| 226 } else { | 215 } else { |
| 227 fInternal.setCount(count - this->externalCount()); | 216 fInternal.setCount(count - this->externalCount()); |
| 228 } | 217 } |
| 229 fCount = count; | 218 fCount = count; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 242 && stream->write(fInternal.begin(), fInternal.bytes()); | 231 && stream->write(fInternal.begin(), fInternal.bytes()); |
| 243 } | 232 } |
| 244 | 233 |
| 245 // read from the stream, and write up to length bytes. Return the actual | 234 // read from the stream, and write up to length bytes. Return the actual |
| 246 // number of bytes written. | 235 // number of bytes written. |
| 247 size_t readFromStream(SkStream* stream, size_t length) { | 236 size_t readFromStream(SkStream* stream, size_t length) { |
| 248 return stream->read(this->reservePad(length), length); | 237 return stream->read(this->reservePad(length), length); |
| 249 } | 238 } |
| 250 | 239 |
| 251 private: | 240 private: |
| 241 uint32_t& atOffset(size_t offset) { |
| 242 SkASSERT(SkAlign4(offset) == offset); |
| 243 const int count = SkToInt(offset/4); |
| 244 SkASSERT(count < fCount); |
| 245 |
| 246 if (count < this->externalCount()) { |
| 247 return fExternal[count]; |
| 248 } |
| 249 return fInternal[count - this->externalCount()]; |
| 250 } |
| 251 |
| 252 |
| 252 // Number of uint32_t written into fExternal. <= fExternalLimit. | 253 // Number of uint32_t written into fExternal. <= fExternalLimit. |
| 253 int externalCount() const { return fCount - fInternal.count(); } | 254 int externalCount() const { return fCount - fInternal.count(); } |
| 254 | 255 |
| 255 int fCount; // Total number of uint32_t written. | 256 int fCount; // Total number of uint32_t written. |
| 256 int fExternalLimit; // Number of uint32_t we can write to fExter
nal. | 257 int fExternalLimit; // Number of uint32_t we can write to fExter
nal. |
| 257 uint32_t* fExternal; // Unmanaged memory block. | 258 uint32_t* fExternal; // Unmanaged memory block. |
| 258 SkTDArray<uint32_t> fInternal; // Managed memory block. | 259 SkTDArray<uint32_t> fInternal; // Managed memory block. |
| 259 }; | 260 }; |
| 260 | 261 |
| 261 /** | 262 /** |
| 262 * Helper class to allocated SIZE bytes as part of the writer, and to provide | 263 * Helper class to allocated SIZE bytes as part of the writer, and to provide |
| 263 * that storage to the constructor as its initial storage buffer. | 264 * that storage to the constructor as its initial storage buffer. |
| 264 * | 265 * |
| 265 * This wrapper ensures proper alignment rules are met for the storage. | 266 * This wrapper ensures proper alignment rules are met for the storage. |
| 266 */ | 267 */ |
| 267 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { | 268 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
| 268 public: | 269 public: |
| 269 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} | 270 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} |
| 270 | 271 |
| 271 private: | 272 private: |
| 272 union { | 273 union { |
| 273 void* fPtrAlignment; | 274 void* fPtrAlignment; |
| 274 double fDoubleAlignment; | 275 double fDoubleAlignment; |
| 275 char fStorage[SIZE]; | 276 char fStorage[SIZE]; |
| 276 } fData; | 277 } fData; |
| 277 }; | 278 }; |
| 278 | 279 |
| 279 #endif | 280 #endif |
| OLD | NEW |