Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkWriter32.h
1
2 /*
3  * Copyright 2008 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 #ifndef SkWriter32_DEFINED
11 #define SkWriter32_DEFINED
12
13 #include "SkMatrix.h"
14 #include "SkPath.h"
15 #include "SkPoint.h"
16 #include "SkRRect.h"
17 #include "SkRect.h"
18 #include "SkRegion.h"
19 #include "SkScalar.h"
20 #include "SkStream.h"
21 #include "SkTemplates.h"
22 #include "SkTypes.h"
23
24 class SkWriter32 : SkNoncopyable {
25 public:
26     /**
27      *  The caller can specify an initial block of storage, which the caller manages.
28      *
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.
32      */
33     SkWriter32(void* external = NULL, size_t externalBytes = 0) {
34         this->reset(external, externalBytes);
35     }
36
37     // return the current offset (will always be a multiple of 4)
38     size_t bytesWritten() const { return fUsed; }
39
40     SK_ATTR_DEPRECATED("use bytesWritten")
41     size_t size() const { return this->bytesWritten(); }
42
43     void reset(void* external = NULL, size_t externalBytes = 0) {
44         SkASSERT(SkIsAlign4((uintptr_t)external));
45         SkASSERT(SkIsAlign4(externalBytes));
46
47         fData = (uint8_t*)external;
48         fCapacity = externalBytes;
49         fUsed = 0;
50         fExternal = external;
51     }
52
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;
57     }
58
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);
66         }
67         fUsed = totalRequired;
68         return (uint32_t*)(fData + offset);
69     }
70
71     /**
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.
74      */
75     template<typename T>
76     const T& readTAt(size_t offset) const {
77         SkASSERT(SkAlign4(offset) == offset);
78         SkASSERT(offset < fUsed);
79         return *(T*)(fData + offset);
80     }
81
82     /**
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.
85      */
86     template<typename T>
87     void overwriteTAt(size_t offset, const T& value) {
88         SkASSERT(SkAlign4(offset) == offset);
89         SkASSERT(offset < fUsed);
90         *(T*)(fData + offset) = value;
91     }
92
93     bool writeBool(bool value) {
94         this->write32(value);
95         return value;
96     }
97
98     void writeInt(int32_t value) {
99         this->write32(value);
100     }
101
102     void write8(int32_t value) {
103         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
104     }
105
106     void write16(int32_t value) {
107         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
108     }
109
110     void write32(int32_t value) {
111         *(int32_t*)this->reserve(sizeof(value)) = value;
112     }
113
114     void writePtr(void* value) {
115         *(void**)this->reserve(sizeof(value)) = value;
116     }
117
118     void writeScalar(SkScalar value) {
119         *(SkScalar*)this->reserve(sizeof(value)) = value;
120     }
121
122     void writePoint(const SkPoint& pt) {
123         *(SkPoint*)this->reserve(sizeof(pt)) = pt;
124     }
125
126     void writeRect(const SkRect& rect) {
127         *(SkRect*)this->reserve(sizeof(rect)) = rect;
128     }
129
130     void writeIRect(const SkIRect& rect) {
131         *(SkIRect*)this->reserve(sizeof(rect)) = rect;
132     }
133
134     void writeRRect(const SkRRect& rrect) {
135         rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
136     }
137
138     void writePath(const SkPath& path) {
139         size_t size = path.writeToMemory(NULL);
140         SkASSERT(SkAlign4(size) == size);
141         path.writeToMemory(this->reserve(size));
142     }
143
144     void writeMatrix(const SkMatrix& matrix) {
145         size_t size = matrix.writeToMemory(NULL);
146         SkASSERT(SkAlign4(size) == size);
147         matrix.writeToMemory(this->reserve(size));
148     }
149
150     void writeRegion(const SkRegion& rgn) {
151         size_t size = rgn.writeToMemory(NULL);
152         SkASSERT(SkAlign4(size) == size);
153         rgn.writeToMemory(this->reserve(size));
154     }
155
156     // write count bytes (must be a multiple of 4)
157     void writeMul4(const void* values, size_t size) {
158         this->write(values, size);
159     }
160
161     /**
162      *  Write size bytes from values. size must be a multiple of 4, though
163      *  values need not be 4-byte aligned.
164      */
165     void write(const void* values, size_t size) {
166         SkASSERT(SkAlign4(size) == size);
167         memcpy(this->reserve(size), values, size);
168     }
169
170     /**
171      *  Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be
172      *  filled in with zeroes.
173      */
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;
180         }
181         return p;
182     }
183
184     /**
185      *  Write size bytes from src, and pad to 4 byte alignment with zeroes.
186      */
187     void writePad(const void* src, size_t size) {
188         memcpy(this->reservePad(size), src, size);
189     }
190
191     /**
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.
196      *
197      *  If you write NULL, it will be read as "".
198      */
199     void writeString(const char* str, size_t len = (size_t)-1);
200
201     /**
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().
205      */
206     static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
207
208     /**
209      *  Move the cursor back to offset bytes from the beginning.
210      *  offset must be a multiple of 4 no greater than size().
211      */
212     void rewindToOffset(size_t offset) {
213         SkASSERT(SkAlign4(offset) == offset);
214         SkASSERT(offset <= bytesWritten());
215         fUsed = offset;
216     }
217
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);
221     }
222
223     bool writeToStream(SkWStream* stream) const {
224         return stream->write(fData, fUsed);
225     }
226
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);
231     }
232
233 private:
234     void growToAtLeast(size_t size);
235
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.
241 };
242
243 /**
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.
246  *
247  *  This wrapper ensures proper alignment rules are met for the storage.
248  */
249 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
250 public:
251     SkSWriter32() { this->reset(); }
252
253     void reset() {this->INHERITED::reset(fData.fStorage, SIZE); }
254
255 private:
256     union {
257         void*   fPtrAlignment;
258         double  fDoubleAlignment;
259         char    fStorage[SIZE];
260     } fData;
261
262     typedef SkWriter32 INHERITED;
263 };
264
265 #endif