Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "../private/SkTemplates.h"
14 #include "SkData.h"
15 #include "SkMatrix.h"
16 #include "SkPath.h"
17 #include "SkPoint.h"
18 #include "SkRRect.h"
19 #include "SkRect.h"
20 #include "SkRegion.h"
21 #include "SkScalar.h"
22 #include "SkStream.h"
23 #include "SkTypes.h"
24 
25 class SK_API SkWriter32 : SkNoncopyable {
26 public:
34  SkWriter32(void* external = NULL, size_t externalBytes = 0) {
35  this->reset(external, externalBytes);
36  }
37 
38  // return the current offset (will always be a multiple of 4)
39  size_t bytesWritten() const { return fUsed; }
40 
41  SK_ATTR_DEPRECATED("use bytesWritten")
42  size_t size() const { return this->bytesWritten(); }
43 
44  void reset(void* external = NULL, size_t externalBytes = 0) {
45  SkASSERT(SkIsAlign4((uintptr_t)external));
46  SkASSERT(SkIsAlign4(externalBytes));
47 
48  fData = (uint8_t*)external;
49  fCapacity = externalBytes;
50  fUsed = 0;
51  fExternal = external;
52  }
53 
54  // size MUST be multiple of 4
55  uint32_t* reserve(size_t size) {
56  SkASSERT(SkAlign4(size) == size);
57  size_t offset = fUsed;
58  size_t totalRequired = fUsed + size;
59  if (totalRequired > fCapacity) {
60  this->growToAtLeast(totalRequired);
61  }
62  fUsed = totalRequired;
63  return (uint32_t*)(fData + offset);
64  }
65 
70  template<typename T>
71  const T& readTAt(size_t offset) const {
72  SkASSERT(SkAlign4(offset) == offset);
73  SkASSERT(offset < fUsed);
74  return *(T*)(fData + offset);
75  }
76 
81  template<typename T>
82  void overwriteTAt(size_t offset, const T& value) {
83  SkASSERT(SkAlign4(offset) == offset);
84  SkASSERT(offset < fUsed);
85  *(T*)(fData + offset) = value;
86  }
87 
88  bool writeBool(bool value) {
89  this->write32(value);
90  return value;
91  }
92 
93  void writeInt(int32_t value) {
94  this->write32(value);
95  }
96 
97  void write8(int32_t value) {
98  *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
99  }
100 
101  void write16(int32_t value) {
102  *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
103  }
104 
105  void write32(int32_t value) {
106  *(int32_t*)this->reserve(sizeof(value)) = value;
107  }
108 
109  void writePtr(void* value) {
110  *(void**)this->reserve(sizeof(value)) = value;
111  }
112 
113  void writeScalar(SkScalar value) {
114  *(SkScalar*)this->reserve(sizeof(value)) = value;
115  }
116 
117  void writePoint(const SkPoint& pt) {
118  *(SkPoint*)this->reserve(sizeof(pt)) = pt;
119  }
120 
121  void writeRect(const SkRect& rect) {
122  *(SkRect*)this->reserve(sizeof(rect)) = rect;
123  }
124 
125  void writeIRect(const SkIRect& rect) {
126  *(SkIRect*)this->reserve(sizeof(rect)) = rect;
127  }
128 
129  void writeRRect(const SkRRect& rrect) {
130  rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
131  }
132 
133  void writePath(const SkPath& path) {
134  size_t size = path.writeToMemory(NULL);
135  SkASSERT(SkAlign4(size) == size);
136  path.writeToMemory(this->reserve(size));
137  }
138 
139  void writeMatrix(const SkMatrix& matrix) {
140  size_t size = matrix.writeToMemory(NULL);
141  SkASSERT(SkAlign4(size) == size);
142  matrix.writeToMemory(this->reserve(size));
143  }
144 
145  void writeRegion(const SkRegion& rgn) {
146  size_t size = rgn.writeToMemory(NULL);
147  SkASSERT(SkAlign4(size) == size);
148  rgn.writeToMemory(this->reserve(size));
149  }
150 
151  // write count bytes (must be a multiple of 4)
152  void writeMul4(const void* values, size_t size) {
153  this->write(values, size);
154  }
155 
160  void write(const void* values, size_t size) {
161  SkASSERT(SkAlign4(size) == size);
162  sk_careful_memcpy(this->reserve(size), values, size);
163  }
164 
169  uint32_t* reservePad(size_t size) {
170  size_t alignedSize = SkAlign4(size);
171  uint32_t* p = this->reserve(alignedSize);
172  if (alignedSize != size) {
173  SkASSERT(alignedSize >= 4);
174  p[alignedSize / 4 - 1] = 0;
175  }
176  return p;
177  }
178 
182  void writePad(const void* src, size_t size) {
183  sk_careful_memcpy(this->reservePad(size), src, size);
184  }
185 
194  void writeString(const char* str, size_t len = (size_t)-1);
195 
201  static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
202 
203  void writeData(const SkData* data) {
204  uint32_t len = data ? SkToU32(data->size()) : 0;
205  this->write32(len);
206  if (data) {
207  this->writePad(data->data(), len);
208  }
209  }
210 
211  static size_t WriteDataSize(const SkData* data) {
212  return 4 + SkAlign4(data ? data->size() : 0);
213  }
214 
219  void rewindToOffset(size_t offset) {
220  SkASSERT(SkAlign4(offset) == offset);
221  SkASSERT(offset <= bytesWritten());
222  fUsed = offset;
223  }
224 
225  // copy into a single buffer (allocated by caller). Must be at least size()
226  void flatten(void* dst) const {
227  memcpy(dst, fData, fUsed);
228  }
229 
230  bool writeToStream(SkWStream* stream) const {
231  return stream->write(fData, fUsed);
232  }
233 
234  // read from the stream, and write up to length bytes. Return the actual
235  // number of bytes written.
236  size_t readFromStream(SkStream* stream, size_t length) {
237  return stream->read(this->reservePad(length), length);
238  }
239 
243  sk_sp<SkData> snapshotAsData() const;
244 private:
245  void growToAtLeast(size_t size);
246 
247  uint8_t* fData; // Points to either fInternal or fExternal.
248  size_t fCapacity; // Number of bytes we can write to fData.
249  size_t fUsed; // Number of bytes written.
250  void* fExternal; // Unmanaged memory block.
251  SkAutoTMalloc<uint8_t> fInternal; // Managed memory block.
252 };
253 
260 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
261 public:
262  SkSWriter32() { this->reset(); }
263 
264  void reset() {this->INHERITED::reset(fData.fStorage, SIZE); }
265 
266 private:
267  union {
268  void* fPtrAlignment;
269  double fDoubleAlignment;
270  char fStorage[SIZE];
271  } fData;
272 
273  typedef SkWriter32 INHERITED;
274 };
275 
276 #endif
void overwriteTAt(size_t offset, const T &value)
Overwrite a T record at offset, which must be a multiple of 4.
Definition: SkWriter32.h:82
uint32_t * reservePad(size_t size)
Reserve size bytes.
Definition: SkWriter32.h:169
The SkRRect class represents a rounded rect with a potentially different radii for each corner...
Definition: SkRRect.h:48
The SkPath class encapsulates compound (multiple contour) geometric paths consisting of straight line...
Definition: SkPath.h:27
size_t writeToMemory(void *buffer) const
Write the region to the buffer, and return the number of bytes written.
Definition: SkPoint.h:156
size_t size() const
Returns the number of bytes stored.
Definition: SkData.h:27
SkWriter32(void *external=NULL, size_t externalBytes=0)
The caller can specify an initial block of storage, which the caller manages.
Definition: SkWriter32.h:34
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:26
Definition: SkWriter32.h:25
SkData holds an immutable data buffer.
Definition: SkData.h:22
size_t writeToMemory(void *buffer) const
Write the path to the buffer, and return the number of bytes written.
const T & readTAt(size_t offset) const
Read a T record at offset, which must be a multiple of 4.
Definition: SkWriter32.h:71
size_t writeToMemory(void *buffer) const
Write the rrect into the specified buffer.
void rewindToOffset(size_t offset)
Move the cursor back to offset bytes from the beginning.
Definition: SkWriter32.h:219
The SkRegion class encapsulates the geometric region used to specify clipping areas for drawing...
Definition: SkRegion.h:30
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:38
Definition: SkStream.h:182
Definition: SkRect.h:390
void writePad(const void *src, size_t size)
Write size bytes from src, and pad to 4 byte alignment with zeroes.
Definition: SkWriter32.h:182
virtual size_t read(void *buffer, size_t size)=0
Reads or skips size number of bytes.
void write(const void *values, size_t size)
Write size bytes from values.
Definition: SkWriter32.h:160
static void * sk_careful_memcpy(void *dst, const void *src, size_t len)
sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior. ...
Definition: SkTypes.h:51
virtual bool write(const void *buffer, size_t size)=0
Called to write bytes to a SkWStream.
const void * data() const
Returns the ptr to the data.
Definition: SkData.h:34
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
Helper class to allocated SIZE bytes as part of the writer, and to provide that storage to the constr...
Definition: SkWriter32.h:260