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 |
11 #define SkWriter32_DEFINED | 11 #define SkWriter32_DEFINED |
12 | 12 |
13 #include "SkData.h" | |
13 #include "SkMatrix.h" | 14 #include "SkMatrix.h" |
14 #include "SkPath.h" | 15 #include "SkPath.h" |
15 #include "SkPoint.h" | 16 #include "SkPoint.h" |
16 #include "SkRRect.h" | 17 #include "SkRRect.h" |
17 #include "SkRect.h" | 18 #include "SkRect.h" |
18 #include "SkRegion.h" | 19 #include "SkRegion.h" |
19 #include "SkScalar.h" | 20 #include "SkScalar.h" |
20 #include "SkStream.h" | 21 #include "SkStream.h" |
21 #include "SkTemplates.h" | 22 #include "SkTemplates.h" |
22 #include "SkTypes.h" | 23 #include "SkTypes.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
37 // return the current offset (will always be a multiple of 4) | 38 // return the current offset (will always be a multiple of 4) |
38 size_t bytesWritten() const { return fUsed; } | 39 size_t bytesWritten() const { return fUsed; } |
39 | 40 |
40 SK_ATTR_DEPRECATED("use bytesWritten") | 41 SK_ATTR_DEPRECATED("use bytesWritten") |
41 size_t size() const { return this->bytesWritten(); } | 42 size_t size() const { return this->bytesWritten(); } |
42 | 43 |
43 void reset(void* external = NULL, size_t externalBytes = 0) { | 44 void reset(void* external = NULL, size_t externalBytes = 0) { |
44 SkASSERT(SkIsAlign4((uintptr_t)external)); | 45 SkASSERT(SkIsAlign4((uintptr_t)external)); |
45 SkASSERT(SkIsAlign4(externalBytes)); | 46 SkASSERT(SkIsAlign4(externalBytes)); |
46 | 47 |
48 fSnapshot.reset(NULL); | |
47 fData = (uint8_t*)external; | 49 fData = (uint8_t*)external; |
48 fCapacity = externalBytes; | 50 fCapacity = externalBytes; |
49 fUsed = 0; | 51 fUsed = 0; |
50 fExternal = external; | 52 fExternal = external; |
51 } | 53 } |
52 | 54 |
53 // Returns the current buffer. | 55 // Returns the current buffer. |
54 // The pointer may be invalidated by any future write calls. | 56 // The pointer may be invalidated by any future write calls. |
55 const uint32_t* contiguousArray() const { | 57 const uint32_t* contiguousArray() const { |
56 return (uint32_t*)fData; | 58 return (uint32_t*)fData; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 bool writeToStream(SkWStream* stream) const { | 225 bool writeToStream(SkWStream* stream) const { |
224 return stream->write(fData, fUsed); | 226 return stream->write(fData, fUsed); |
225 } | 227 } |
226 | 228 |
227 // read from the stream, and write up to length bytes. Return the actual | 229 // read from the stream, and write up to length bytes. Return the actual |
228 // number of bytes written. | 230 // number of bytes written. |
229 size_t readFromStream(SkStream* stream, size_t length) { | 231 size_t readFromStream(SkStream* stream, size_t length) { |
230 return stream->read(this->reservePad(length), length); | 232 return stream->read(this->reservePad(length), length); |
231 } | 233 } |
232 | 234 |
235 /** | |
236 * Captures a snapshot of the data as it is right now, and return it. | |
237 * Multiple calls without intervening writes may return the same buffer, | |
reed1
2014/02/14 17:31:08
I like noting this possible optimization, but perh
iancottrell
2014/02/14 18:28:36
Sure, I deliberately used the word may, but I agre
| |
238 * and future appends will not affect the returned buffer. | |
239 * Callers must unref the returned SkData. | |
240 */ | |
241 SkData* snapshotAsData(); | |
242 SkData* snapshotAsData() const { | |
mtklein
2014/02/14 17:42:18
Why have both const and non-const versions? Why n
iancottrell
2014/02/14 18:28:36
Would we rather make it threadsafe?
| |
243 return const_cast<SkWriter32*>(this)->snapshotAsData(); | |
reed1
2014/02/14 17:31:08
Do we want a mutex guard around this, since we are
iancottrell
2014/02/14 18:28:36
Maybe? This is an area where I just don't have eno
iancottrell
2014/02/27 20:16:19
Added a mutex guard in the implementation.
| |
244 } | |
233 private: | 245 private: |
234 void growToAtLeast(size_t size); | 246 void growToAtLeast(size_t size); |
235 | 247 |
236 uint8_t* fData; // Points to either fInternal or fExterna l. | 248 uint8_t* fData; // Points to either fInternal or fExterna l. |
237 size_t fCapacity; // Number of bytes we can write to fData. | 249 size_t fCapacity; // Number of bytes we can write to fData. |
238 size_t fUsed; // Number of bytes written. | 250 size_t fUsed; // Number of bytes written. |
239 void* fExternal; // Unmanaged memory block. | 251 void* fExternal; // Unmanaged memory block. |
240 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. | 252 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. |
253 SkAutoTUnref<SkData> fSnapshot; // Holds the result of last asData. | |
241 }; | 254 }; |
242 | 255 |
243 /** | 256 /** |
244 * Helper class to allocated SIZE bytes as part of the writer, and to provide | 257 * Helper class to allocated SIZE bytes as part of the writer, and to provide |
245 * that storage to the constructor as its initial storage buffer. | 258 * that storage to the constructor as its initial storage buffer. |
246 * | 259 * |
247 * This wrapper ensures proper alignment rules are met for the storage. | 260 * This wrapper ensures proper alignment rules are met for the storage. |
248 */ | 261 */ |
249 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { | 262 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
250 public: | 263 public: |
251 SkSWriter32() { this->reset(); } | 264 SkSWriter32() { this->reset(); } |
252 | 265 |
253 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); } | 266 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); } |
254 | 267 |
255 private: | 268 private: |
256 union { | 269 union { |
257 void* fPtrAlignment; | 270 void* fPtrAlignment; |
258 double fDoubleAlignment; | 271 double fDoubleAlignment; |
259 char fStorage[SIZE]; | 272 char fStorage[SIZE]; |
260 } fData; | 273 } fData; |
261 | 274 |
262 typedef SkWriter32 INHERITED; | 275 typedef SkWriter32 INHERITED; |
263 }; | 276 }; |
264 | 277 |
265 #endif | 278 #endif |
OLD | NEW |