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 26 matching lines...) Expand all Loading... |
37 // return the current offset (will always be a multiple of 4) | 37 // return the current offset (will always be a multiple of 4) |
38 size_t bytesWritten() const { return fCount * 4; } | 38 size_t bytesWritten() const { return fCount * 4; } |
39 | 39 |
40 SK_ATTR_DEPRECATED("use bytesWritten") | 40 SK_ATTR_DEPRECATED("use bytesWritten") |
41 size_t size() const { return this->bytesWritten(); } | 41 size_t size() const { return this->bytesWritten(); } |
42 | 42 |
43 void reset(void* external = NULL, size_t externalBytes = 0) { | 43 void reset(void* external = NULL, size_t externalBytes = 0) { |
44 SkASSERT(SkIsAlign4((uintptr_t)external)); | 44 SkASSERT(SkIsAlign4((uintptr_t)external)); |
45 SkASSERT(SkIsAlign4(externalBytes)); | 45 SkASSERT(SkIsAlign4(externalBytes)); |
46 fExternal = (uint32_t*)external; | 46 fExternal = (uint32_t*)external; |
47 fExternalLimit = externalBytes/4; | 47 fExternalLimit = SkToInt(externalBytes/4); |
48 fCount = 0; | 48 fCount = 0; |
49 fInternal.rewind(); | 49 fInternal.rewind(); |
50 } | 50 } |
51 | 51 |
52 // If all data written is contiguous, then this returns a pointer to it, oth
erwise NULL. | 52 // If all data written is contiguous, then this returns a pointer to it, oth
erwise NULL. |
53 // This will work if we've only written to the externally supplied block of
storage, or if we've | 53 // This will work if we've only written to the externally supplied block of
storage, or if we've |
54 // only written to our internal dynamic storage, but will fail if we have wr
itten into both. | 54 // only written to our internal dynamic storage, but will fail if we have wr
itten into both. |
55 const uint32_t* contiguousArray() const { | 55 const uint32_t* contiguousArray() const { |
56 if (this->externalCount() == 0) { | 56 if (this->externalCount() == 0) { |
57 return fInternal.begin(); | 57 return fInternal.begin(); |
58 } else if (fInternal.isEmpty()) { | 58 } else if (fInternal.isEmpty()) { |
59 return fExternal; | 59 return fExternal; |
60 } | 60 } |
61 return NULL; | 61 return NULL; |
62 } | 62 } |
63 | 63 |
64 // size MUST be multiple of 4 | 64 // size MUST be multiple of 4 |
65 uint32_t* reserve(size_t size) { | 65 uint32_t* reserve(size_t size) { |
66 SkASSERT(SkAlign4(size) == size); | 66 SkASSERT(SkAlign4(size) == size); |
67 const int count = size/4; | 67 const int count = SkToInt(size/4); |
68 | 68 |
69 uint32_t* p; | 69 uint32_t* p; |
70 // Once we start writing to fInternal, we never write to fExternal again
. | 70 // Once we start writing to fInternal, we never write to fExternal again
. |
71 // This simplifies tracking what data is where. | 71 // This simplifies tracking what data is where. |
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 // return the address of the 4byte int at the specified offset (which must |
83 // be a multiple of 4. This does not allocate any new space, so the returned | 83 // be a multiple of 4. This does not allocate any new space, so the returned |
84 // address is only valid for 1 int. | 84 // address is only valid for 1 int. |
85 uint32_t* peek32(size_t offset) { | 85 uint32_t* peek32(size_t offset) { |
86 SkASSERT(SkAlign4(offset) == offset); | 86 SkASSERT(SkAlign4(offset) == offset); |
87 const int count = offset/4; | 87 const int count = SkToInt(offset/4); |
88 SkASSERT(count < fCount); | 88 SkASSERT(count < fCount); |
89 | 89 |
90 if (count < this->externalCount()) { | 90 if (count < this->externalCount()) { |
91 return fExternal + count; | 91 return fExternal + count; |
92 } | 92 } |
93 return &fInternal[count - this->externalCount()]; | 93 return &fInternal[count - this->externalCount()]; |
94 } | 94 } |
95 | 95 |
96 bool writeBool(bool value) { | 96 bool writeBool(bool value) { |
97 this->write32(value); | 97 this->write32(value); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 */ | 213 */ |
214 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1); | 214 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1); |
215 | 215 |
216 /** | 216 /** |
217 * Move the cursor back to offset bytes from the beginning. | 217 * Move the cursor back to offset bytes from the beginning. |
218 * This has the same restrictions as peek32: offset must be <= size() and | 218 * This has the same restrictions as peek32: offset must be <= size() and |
219 * offset must be a multiple of 4. | 219 * offset must be a multiple of 4. |
220 */ | 220 */ |
221 void rewindToOffset(size_t offset) { | 221 void rewindToOffset(size_t offset) { |
222 SkASSERT(SkAlign4(offset) == offset); | 222 SkASSERT(SkAlign4(offset) == offset); |
223 const int count = offset/4; | 223 const int count = SkToInt(offset/4); |
224 if (count < this->externalCount()) { | 224 if (count < this->externalCount()) { |
225 fInternal.setCount(0); | 225 fInternal.setCount(0); |
226 } else { | 226 } else { |
227 fInternal.setCount(count - this->externalCount()); | 227 fInternal.setCount(count - this->externalCount()); |
228 } | 228 } |
229 fCount = count; | 229 fCount = count; |
230 } | 230 } |
231 | 231 |
232 // copy into a single buffer (allocated by caller). Must be at least size() | 232 // copy into a single buffer (allocated by caller). Must be at least size() |
233 void flatten(void* dst) const { | 233 void flatten(void* dst) const { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 270 |
271 private: | 271 private: |
272 union { | 272 union { |
273 void* fPtrAlignment; | 273 void* fPtrAlignment; |
274 double fDoubleAlignment; | 274 double fDoubleAlignment; |
275 char fStorage[SIZE]; | 275 char fStorage[SIZE]; |
276 } fData; | 276 } fData; |
277 }; | 277 }; |
278 | 278 |
279 #endif | 279 #endif |
OLD | NEW |