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
21 #include "SkTemplates.h"
24 class SkWriter32 : SkNoncopyable {
27 * The caller can specify an initial block of storage, which the caller manages.
29 * SkWriter32 will try to back reserve and write calls with this external storage until the
30 * first time an allocation doesn't fit. From then it will use dynamically allocated storage.
31 * This used to be optional behavior, but pipe now relies on it.
33 SkWriter32(void* external = NULL, size_t externalBytes = 0) {
34 this->reset(external, externalBytes);
37 // return the current offset (will always be a multiple of 4)
38 size_t bytesWritten() const { return fUsed; }
40 SK_ATTR_DEPRECATED("use bytesWritten")
41 size_t size() const { return this->bytesWritten(); }
43 void reset(void* external = NULL, size_t externalBytes = 0) {
44 SkASSERT(SkIsAlign4((uintptr_t)external));
45 SkASSERT(SkIsAlign4(externalBytes));
47 fData = (uint8_t*)external;
48 fCapacity = externalBytes;
53 // Returns the current buffer.
54 // The pointer may be invalidated by any future write calls.
55 const uint32_t* contiguousArray() const {
56 return (uint32_t*)fData;
59 // size MUST be multiple of 4
60 uint32_t* reserve(size_t size) {
61 SkASSERT(SkAlign4(size) == size);
62 size_t offset = fUsed;
63 size_t totalRequired = fUsed + size;
64 if (totalRequired > fCapacity) {
65 this->growToAtLeast(totalRequired);
67 fUsed = totalRequired;
68 return (uint32_t*)(fData + offset);
72 * Read a T record at offset, which must be a multiple of 4. Only legal if the record
73 * was writtern atomically using the write methods below.
76 const T& readTAt(size_t offset) const {
77 SkASSERT(SkAlign4(offset) == offset);
78 SkASSERT(offset < fUsed);
79 return *(T*)(fData + offset);
83 * Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
84 * was writtern atomically using the write methods below.
87 void overwriteTAt(size_t offset, const T& value) {
88 SkASSERT(SkAlign4(offset) == offset);
89 SkASSERT(offset < fUsed);
90 *(T*)(fData + offset) = value;
93 bool writeBool(bool value) {
98 void writeInt(int32_t value) {
102 void write8(int32_t value) {
103 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
106 void write16(int32_t value) {
107 *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
110 void write32(int32_t value) {
111 *(int32_t*)this->reserve(sizeof(value)) = value;
114 void writePtr(void* value) {
115 *(void**)this->reserve(sizeof(value)) = value;
118 void writeScalar(SkScalar value) {
119 *(SkScalar*)this->reserve(sizeof(value)) = value;
122 void writePoint(const SkPoint& pt) {
123 *(SkPoint*)this->reserve(sizeof(pt)) = pt;
126 void writeRect(const SkRect& rect) {
127 *(SkRect*)this->reserve(sizeof(rect)) = rect;
130 void writeIRect(const SkIRect& rect) {
131 *(SkIRect*)this->reserve(sizeof(rect)) = rect;
134 void writeRRect(const SkRRect& rrect) {
135 rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
138 void writePath(const SkPath& path) {
139 size_t size = path.writeToMemory(NULL);
140 SkASSERT(SkAlign4(size) == size);
141 path.writeToMemory(this->reserve(size));
144 void writeMatrix(const SkMatrix& matrix) {
145 size_t size = matrix.writeToMemory(NULL);
146 SkASSERT(SkAlign4(size) == size);
147 matrix.writeToMemory(this->reserve(size));
150 void writeRegion(const SkRegion& rgn) {
151 size_t size = rgn.writeToMemory(NULL);
152 SkASSERT(SkAlign4(size) == size);
153 rgn.writeToMemory(this->reserve(size));
156 // write count bytes (must be a multiple of 4)
157 void writeMul4(const void* values, size_t size) {
158 this->write(values, size);
162 * Write size bytes from values. size must be a multiple of 4, though
163 * values need not be 4-byte aligned.
165 void write(const void* values, size_t size) {
166 SkASSERT(SkAlign4(size) == size);
167 memcpy(this->reserve(size), values, size);
171 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be
172 * filled in with zeroes.
174 uint32_t* reservePad(size_t size) {
175 size_t alignedSize = SkAlign4(size);
176 uint32_t* p = this->reserve(alignedSize);
177 if (alignedSize != size) {
178 SkASSERT(alignedSize >= 4);
179 p[alignedSize / 4 - 1] = 0;
185 * Write size bytes from src, and pad to 4 byte alignment with zeroes.
187 void writePad(const void* src, size_t size) {
188 memcpy(this->reservePad(size), src, size);
192 * Writes a string to the writer, which can be retrieved with
193 * SkReader32::readString().
194 * The length can be specified, or if -1 is passed, it will be computed by
195 * calling strlen(). The length must be < max size_t.
197 * If you write NULL, it will be read as "".
199 void writeString(const char* str, size_t len = (size_t)-1);
202 * Computes the size (aligned to multiple of 4) need to write the string
203 * in a call to writeString(). If the length is not specified, it will be
204 * computed by calling strlen().
206 static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
209 * Move the cursor back to offset bytes from the beginning.
210 * offset must be a multiple of 4 no greater than size().
212 void rewindToOffset(size_t offset) {
213 SkASSERT(SkAlign4(offset) == offset);
214 SkASSERT(offset <= bytesWritten());
218 // copy into a single buffer (allocated by caller). Must be at least size()
219 void flatten(void* dst) const {
220 memcpy(dst, fData, fUsed);
223 bool writeToStream(SkWStream* stream) const {
224 return stream->write(fData, fUsed);
227 // read from the stream, and write up to length bytes. Return the actual
228 // number of bytes written.
229 size_t readFromStream(SkStream* stream, size_t length) {
230 return stream->read(this->reservePad(length), length);
234 void growToAtLeast(size_t size);
236 uint8_t* fData; // Points to either fInternal or fExternal.
237 size_t fCapacity; // Number of bytes we can write to fData.
238 size_t fUsed; // Number of bytes written.
239 void* fExternal; // Unmanaged memory block.
240 SkAutoTMalloc<uint8_t> fInternal; // Managed memory block.
244 * 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.
247 * This wrapper ensures proper alignment rules are met for the storage.
249 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
251 SkSWriter32() { this->reset(); }
253 void reset() {this->INHERITED::reset(fData.fStorage, SIZE); }
258 double fDoubleAlignment;
262 typedef SkWriter32 INHERITED;