3 * Copyright 2008 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkWriter32_DEFINED
11 #define SkWriter32_DEFINED
22 #include "SkTemplates.h"
25 class SK_API SkWriter32 : SkNoncopyable {
28 * The caller can specify an initial block of storage, which the caller manages.
30 * SkWriter32 will try to back reserve and write calls with this external storage until the
31 * first time an allocation doesn't fit. From then it will use dynamically allocated storage.
32 * This used to be optional behavior, but pipe now relies on it.
34 SkWriter32(void* external = NULL, size_t externalBytes = 0) {
35 this->reset(external, externalBytes);
38 // return the current offset (will always be a multiple of 4)
39 size_t bytesWritten() const { return fUsed; }
41 SK_ATTR_DEPRECATED("use bytesWritten")
42 size_t size() const { return this->bytesWritten(); }
44 void reset(void* external = NULL, size_t externalBytes = 0) {
45 SkASSERT(SkIsAlign4((uintptr_t)external));
46 SkASSERT(SkIsAlign4(externalBytes));
48 fSnapshot.reset(NULL);
49 fData = (uint8_t*)external;
50 fCapacity = externalBytes;
55 // Returns the current buffer.
56 // The pointer may be invalidated by any future write calls.
57 const uint32_t* contiguousArray() const {
58 return (uint32_t*)fData;
61 // size MUST be multiple of 4
62 uint32_t* reserve(size_t size) {
63 SkASSERT(SkAlign4(size) == size);
64 size_t offset = fUsed;
65 size_t totalRequired = fUsed + size;
66 if (totalRequired > fCapacity) {
67 this->growToAtLeast(totalRequired);
69 fUsed = totalRequired;
70 return (uint32_t*)(fData + offset);
74 * Read a T record at offset, which must be a multiple of 4. Only legal if the record
75 * was written atomically using the write methods below.
78 const T& readTAt(size_t offset) const {
79 SkASSERT(SkAlign4(offset) == offset);
80 SkASSERT(offset < fUsed);
81 return *(T*)(fData + offset);
85 * Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
86 * was written atomically using the write methods below.
89 void overwriteTAt(size_t offset, const T& value) {
90 SkASSERT(SkAlign4(offset) == offset);
91 SkASSERT(offset < fUsed);
92 SkASSERT(fSnapshot.get() == NULL);
93 *(T*)(fData + offset) = value;
96 bool writeBool(bool value) {
101 void writeInt(int32_t value) {
102 this->write32(value);
105 void write8(int32_t value) {
106 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
109 void write16(int32_t value) {
110 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
113 void write32(int32_t value) {
114 *(int32_t*)this->reserve(sizeof(value)) = value;
117 void writePtr(void* value) {
118 *(void**)this->reserve(sizeof(value)) = value;
121 void writeScalar(SkScalar value) {
122 *(SkScalar*)this->reserve(sizeof(value)) = value;
125 void writePoint(const SkPoint& pt) {
126 *(SkPoint*)this->reserve(sizeof(pt)) = pt;
129 void writeRect(const SkRect& rect) {
130 *(SkRect*)this->reserve(sizeof(rect)) = rect;
133 void writeIRect(const SkIRect& rect) {
134 *(SkIRect*)this->reserve(sizeof(rect)) = rect;
137 void writeRRect(const SkRRect& rrect) {
138 rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
141 void writePath(const SkPath& path) {
142 size_t size = path.writeToMemory(NULL);
143 SkASSERT(SkAlign4(size) == size);
144 path.writeToMemory(this->reserve(size));
147 void writeMatrix(const SkMatrix& matrix) {
148 size_t size = matrix.writeToMemory(NULL);
149 SkASSERT(SkAlign4(size) == size);
150 matrix.writeToMemory(this->reserve(size));
153 void writeRegion(const SkRegion& rgn) {
154 size_t size = rgn.writeToMemory(NULL);
155 SkASSERT(SkAlign4(size) == size);
156 rgn.writeToMemory(this->reserve(size));
159 // write count bytes (must be a multiple of 4)
160 void writeMul4(const void* values, size_t size) {
161 this->write(values, size);
165 * Write size bytes from values. size must be a multiple of 4, though
166 * values need not be 4-byte aligned.
168 void write(const void* values, size_t size) {
169 SkASSERT(SkAlign4(size) == size);
170 memcpy(this->reserve(size), values, size);
174 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be
175 * filled in with zeroes.
177 uint32_t* reservePad(size_t size) {
178 size_t alignedSize = SkAlign4(size);
179 uint32_t* p = this->reserve(alignedSize);
180 if (alignedSize != size) {
181 SkASSERT(alignedSize >= 4);
182 p[alignedSize / 4 - 1] = 0;
188 * Write size bytes from src, and pad to 4 byte alignment with zeroes.
190 void writePad(const void* src, size_t size) {
191 memcpy(this->reservePad(size), src, size);
195 * Writes a string to the writer, which can be retrieved with
196 * SkReader32::readString().
197 * The length can be specified, or if -1 is passed, it will be computed by
198 * calling strlen(). The length must be < max size_t.
200 * If you write NULL, it will be read as "".
202 void writeString(const char* str, size_t len = (size_t)-1);
205 * Computes the size (aligned to multiple of 4) need to write the string
206 * in a call to writeString(). If the length is not specified, it will be
207 * computed by calling strlen().
209 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
212 * Move the cursor back to offset bytes from the beginning.
213 * offset must be a multiple of 4 no greater than size().
215 void rewindToOffset(size_t offset) {
216 SkASSERT(SkAlign4(offset) == offset);
217 SkASSERT(offset <= bytesWritten());
221 // copy into a single buffer (allocated by caller). Must be at least size()
222 void flatten(void* dst) const {
223 memcpy(dst, fData, fUsed);
226 bool writeToStream(SkWStream* stream) const {
227 return stream->write(fData, fUsed);
230 // read from the stream, and write up to length bytes. Return the actual
231 // number of bytes written.
232 size_t readFromStream(SkStream* stream, size_t length) {
233 return stream->read(this->reservePad(length), length);
237 * 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.
247 SkData* snapshotAsData() const;
249 void growToAtLeast(size_t size);
251 uint8_t* fData; // Points to either fInternal or fExternal.
252 size_t fCapacity; // Number of bytes we can write to fData.
253 size_t fUsed; // Number of bytes written.
254 void* fExternal; // Unmanaged memory block.
255 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block.
256 SkAutoTUnref<SkData> fSnapshot; // Holds the result of last asData.
260 * 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.
263 * This wrapper ensures proper alignment rules are met for the storage.
265 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
267 SkSWriter32() { this->reset(); }
269 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); }
274 double fDoubleAlignment;
278 typedef SkWriter32 INHERITED;