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 27 matching lines...) Expand all Loading... |
38 // return the current offset (will always be a multiple of 4) | 38 // return the current offset (will always be a multiple of 4) |
39 size_t bytesWritten() const { return fUsed; } | 39 size_t bytesWritten() const { return fUsed; } |
40 | 40 |
41 SK_ATTR_DEPRECATED("use bytesWritten") | 41 SK_ATTR_DEPRECATED("use bytesWritten") |
42 size_t size() const { return this->bytesWritten(); } | 42 size_t size() const { return this->bytesWritten(); } |
43 | 43 |
44 void reset(void* external = NULL, size_t externalBytes = 0) { | 44 void reset(void* external = NULL, size_t externalBytes = 0) { |
45 SkASSERT(SkIsAlign4((uintptr_t)external)); | 45 SkASSERT(SkIsAlign4((uintptr_t)external)); |
46 SkASSERT(SkIsAlign4(externalBytes)); | 46 SkASSERT(SkIsAlign4(externalBytes)); |
47 | 47 |
48 fSnapshot.reset(NULL); | |
49 fData = (uint8_t*)external; | 48 fData = (uint8_t*)external; |
50 fCapacity = externalBytes; | 49 fCapacity = externalBytes; |
51 fUsed = 0; | 50 fUsed = 0; |
52 fExternal = external; | 51 fExternal = external; |
53 } | 52 } |
54 | 53 |
55 // Returns the current buffer. | 54 // Returns the current buffer. |
56 // The pointer may be invalidated by any future write calls. | 55 // The pointer may be invalidated by any future write calls. |
57 const uint32_t* contiguousArray() const { | 56 const uint32_t* contiguousArray() const { |
58 return (uint32_t*)fData; | 57 return (uint32_t*)fData; |
(...skipping 23 matching lines...) Expand all Loading... |
82 } | 81 } |
83 | 82 |
84 /** | 83 /** |
85 * Overwrite a T record at offset, which must be a multiple of 4. Only lega
l if the record | 84 * Overwrite a T record at offset, which must be a multiple of 4. Only lega
l if the record |
86 * was written atomically using the write methods below. | 85 * was written atomically using the write methods below. |
87 */ | 86 */ |
88 template<typename T> | 87 template<typename T> |
89 void overwriteTAt(size_t offset, const T& value) { | 88 void overwriteTAt(size_t offset, const T& value) { |
90 SkASSERT(SkAlign4(offset) == offset); | 89 SkASSERT(SkAlign4(offset) == offset); |
91 SkASSERT(offset < fUsed); | 90 SkASSERT(offset < fUsed); |
92 SkASSERT(fSnapshot.get() == NULL); | |
93 *(T*)(fData + offset) = value; | 91 *(T*)(fData + offset) = value; |
94 } | 92 } |
95 | 93 |
96 bool writeBool(bool value) { | 94 bool writeBool(bool value) { |
97 this->write32(value); | 95 this->write32(value); |
98 return value; | 96 return value; |
99 } | 97 } |
100 | 98 |
101 void writeInt(int32_t value) { | 99 void writeInt(int32_t value) { |
102 this->write32(value); | 100 this->write32(value); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 } | 226 } |
229 | 227 |
230 // read from the stream, and write up to length bytes. Return the actual | 228 // read from the stream, and write up to length bytes. Return the actual |
231 // number of bytes written. | 229 // number of bytes written. |
232 size_t readFromStream(SkStream* stream, size_t length) { | 230 size_t readFromStream(SkStream* stream, size_t length) { |
233 return stream->read(this->reservePad(length), length); | 231 return stream->read(this->reservePad(length), length); |
234 } | 232 } |
235 | 233 |
236 /** | 234 /** |
237 * Captures a snapshot of the data as it is right now, and return it. | 235 * Captures a snapshot of the data as it is right now, and return it. |
238 * Multiple calls without intervening writes may return the same SkData, | |
239 * but this is not guaranteed. | |
240 * Future appends will not affect the returned buffer. | |
241 * It is illegal to call overwriteTAt after this without an intervening | |
242 * append. It may cause the snapshot buffer to be corrupted. | |
243 * Callers must unref the returned SkData. | |
244 * This is not thread safe, it should only be called on the writing thread, | |
245 * the result however can be shared across threads. | |
246 */ | 236 */ |
247 SkData* snapshotAsData() const; | 237 SkData* snapshotAsData() const; |
248 private: | 238 private: |
249 void growToAtLeast(size_t size); | 239 void growToAtLeast(size_t size); |
250 | 240 |
251 uint8_t* fData; // Points to either fInternal or fExterna
l. | 241 uint8_t* fData; // Points to either fInternal or fExterna
l. |
252 size_t fCapacity; // Number of bytes we can write to fData. | 242 size_t fCapacity; // Number of bytes we can write to fData. |
253 size_t fUsed; // Number of bytes written. | 243 size_t fUsed; // Number of bytes written. |
254 void* fExternal; // Unmanaged memory block. | 244 void* fExternal; // Unmanaged memory block. |
255 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. | 245 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. |
256 SkAutoTUnref<SkData> fSnapshot; // Holds the result of last asData. | |
257 }; | 246 }; |
258 | 247 |
259 /** | 248 /** |
260 * Helper class to allocated SIZE bytes as part of the writer, and to provide | 249 * Helper class to allocated SIZE bytes as part of the writer, and to provide |
261 * that storage to the constructor as its initial storage buffer. | 250 * that storage to the constructor as its initial storage buffer. |
262 * | 251 * |
263 * This wrapper ensures proper alignment rules are met for the storage. | 252 * This wrapper ensures proper alignment rules are met for the storage. |
264 */ | 253 */ |
265 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { | 254 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
266 public: | 255 public: |
267 SkSWriter32() { this->reset(); } | 256 SkSWriter32() { this->reset(); } |
268 | 257 |
269 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); } | 258 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); } |
270 | 259 |
271 private: | 260 private: |
272 union { | 261 union { |
273 void* fPtrAlignment; | 262 void* fPtrAlignment; |
274 double fDoubleAlignment; | 263 double fDoubleAlignment; |
275 char fStorage[SIZE]; | 264 char fStorage[SIZE]; |
276 } fData; | 265 } fData; |
277 | 266 |
278 typedef SkWriter32 INHERITED; | 267 typedef SkWriter32 INHERITED; |
279 }; | 268 }; |
280 | 269 |
281 #endif | 270 #endif |
OLD | NEW |