blob: 9dfb8df64607d6568902194f302f0b2b01921dd4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkStream_DEFINED
18#define SkStream_DEFINED
19
20#include "SkRefCnt.h"
21#include "SkScalar.h"
22
reed@google.com70442a62011-06-23 21:48:04 +000023class SkDataRef;
24
reed@google.comf3166342011-04-26 20:06:08 +000025class SK_API SkStream : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026public:
27 virtual ~SkStream();
28 /** Called to rewind to the beginning of the stream. If this cannot be
29 done, return false.
30 */
31 virtual bool rewind() = 0;
32 /** If this stream represents a file, this method returns the file's name.
33 If it does not, it returns NULL (the default behavior).
34 */
35 virtual const char* getFileName();
36 /** Called to read or skip size number of bytes.
37 If buffer is NULL and size > 0, skip that many bytes, returning how many were skipped.
38 If buffer is NULL and size == 0, return the total length of the stream.
39 If buffer != NULL, copy the requested number of bytes into buffer, returning how many were copied.
40 @param buffer If buffer is NULL, ignore and just skip size bytes, otherwise copy size bytes into buffer
41 @param size The number of bytes to skip or copy
42 @return bytes read on success
43 */
44 virtual size_t read(void* buffer, size_t size) = 0;
45
46 /** Return the total length of the stream.
47 */
48 size_t getLength() { return this->read(NULL, 0); }
49
50 /** Skip the specified number of bytes, returning the actual number
51 of bytes that could be skipped.
52 */
53 size_t skip(size_t bytes);
54
55 /** If the stream is backed by RAM, this method returns the starting
56 address for the data. If not (i.e. it is backed by a file or other
57 structure), this method returns NULL.
58 The default implementation returns NULL.
59 */
60 virtual const void* getMemoryBase();
61
62 int8_t readS8();
63 int16_t readS16();
64 int32_t readS32();
65
66 uint8_t readU8() { return (uint8_t)this->readS8(); }
67 uint16_t readU16() { return (uint16_t)this->readS16(); }
68 uint32_t readU32() { return (uint32_t)this->readS32(); }
69
70 bool readBool() { return this->readU8() != 0; }
71 SkScalar readScalar();
72 size_t readPackedUInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +000073};
74
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000075class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
77 virtual ~SkWStream();
78
79 /** Called to write bytes to a SkWStream. Returns true on success
80 @param buffer the address of at least size bytes to be written to the stream
81 @param size The number of bytes in buffer to write to the stream
82 @return true on success
83 */
84 virtual bool write(const void* buffer, size_t size) = 0;
85 virtual void newline();
86 virtual void flush();
87
88 // helpers
89
90 bool write8(U8CPU);
91 bool write16(U16CPU);
92 bool write32(uint32_t);
93
94 bool writeText(const char text[]);
95 bool writeDecAsText(int32_t);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000096 bool writeBigDecAsText(int64_t, int minDigits = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 bool writeHexAsText(uint32_t, int minDigits = 0);
98 bool writeScalarAsText(SkScalar);
99
100 bool writeBool(bool v) { return this->write8(v); }
101 bool writeScalar(SkScalar);
102 bool writePackedUInt(size_t);
103
104 bool writeStream(SkStream* input, size_t length);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105};
106
107////////////////////////////////////////////////////////////////////////////////////////
108
109#include "SkString.h"
110
111struct SkFILE;
112
113/** A stream that reads from a FILE*, which is opened in the constructor and
114 closed in the destructor
115 */
116class SkFILEStream : public SkStream {
117public:
118 /** Initialize the stream by calling fopen on the specified path. Will be
119 closed in the destructor.
120 */
121 explicit SkFILEStream(const char path[] = NULL);
122 virtual ~SkFILEStream();
123
124 /** Returns true if the current path could be opened.
125 */
126 bool isValid() const { return fFILE != NULL; }
127 /** Close the current file, and open a new file with the specified
128 path. If path is NULL, just close the current file.
129 */
130 void setPath(const char path[]);
131
132 virtual bool rewind();
133 virtual size_t read(void* buffer, size_t size);
134 virtual const char* getFileName();
135
136private:
137 SkFILE* fFILE;
138 SkString fName;
139};
140
141/** A stream that reads from a file descriptor
142 */
143class SkFDStream : public SkStream {
144public:
145 /** Initialize the stream with a dup() of the specified file descriptor.
146 If closeWhenDone is true, then the descriptor will be closed in the
147 destructor.
148 */
149 SkFDStream(int fileDesc, bool closeWhenDone);
150 virtual ~SkFDStream();
151
152 /** Returns true if the current path could be opened.
153 */
154 bool isValid() const { return fFD >= 0; }
155
156 virtual bool rewind();
157 virtual size_t read(void* buffer, size_t size);
158 virtual const char* getFileName() { return NULL; }
159
160private:
161 int fFD;
162 bool fCloseWhenDone;
163};
164
165class SkMemoryStream : public SkStream {
166public:
167 SkMemoryStream();
168 /** We allocate (and free) the memory. Write to it via getMemoryBase()
169 */
170 SkMemoryStream(size_t length);
171 /** if copyData is true, the stream makes a private copy of the data
172 */
173 SkMemoryStream(const void* data, size_t length, bool copyData = false);
174 virtual ~SkMemoryStream();
175
176 /** Resets the stream to the specified data and length,
177 just like the constructor.
178 if copyData is true, the stream makes a private copy of the data
179 */
180 virtual void setMemory(const void* data, size_t length,
181 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000182 /** Replace any memory buffer with the specified buffer. The caller
183 must have allocated data with sk_malloc or sk_realloc, since it
184 will be freed with sk_free.
185 */
186 void setMemoryOwned(const void* data, size_t length);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 void skipToAlign4();
188 virtual bool rewind();
189 virtual size_t read(void* buffer, size_t size);
190 virtual const void* getMemoryBase();
191 const void* getAtPos();
192 size_t seek(size_t offset);
193 size_t peek() const { return fOffset; }
194
195private:
196 const void* fSrc;
197 size_t fSize, fOffset;
198 SkBool8 fWeOwnTheData;
199};
200
201/** \class SkBufferStream
202 This is a wrapper class that adds buffering to another stream.
203 The caller can provide the buffer, or ask SkBufferStream to allocated/free
204 it automatically.
205*/
206class SkBufferStream : public SkStream {
207public:
208 /** Provide the stream to be buffered (proxy), and the size of the buffer that
209 should be used. This will be allocated and freed automatically. If bufferSize is 0,
210 a default buffer size will be used.
211 The proxy stream is referenced, and will be unreferenced in when the
212 bufferstream is destroyed.
213 */
214 SkBufferStream(SkStream* proxy, size_t bufferSize = 0);
215 /** Provide the stream to be buffered (proxy), and a buffer and size to be used.
216 This buffer is owned by the caller, and must be at least bufferSize bytes big.
217 Passing NULL for buffer will cause the buffer to be allocated/freed automatically.
218 If buffer is not NULL, it is an error for bufferSize to be 0.
219 The proxy stream is referenced, and will be unreferenced in when the
220 bufferstream is destroyed.
221 */
222 SkBufferStream(SkStream* proxy, void* buffer, size_t bufferSize);
223 virtual ~SkBufferStream();
224
225 virtual bool rewind();
226 virtual const char* getFileName();
227 virtual size_t read(void* buffer, size_t size);
228 virtual const void* getMemoryBase();
229
230private:
231 enum {
232 kDefaultBufferSize = 128
233 };
234 // illegal
235 SkBufferStream(const SkBufferStream&);
236 SkBufferStream& operator=(const SkBufferStream&);
237
238 SkStream* fProxy;
239 char* fBuffer;
240 size_t fOrigBufferSize, fBufferSize, fBufferOffset;
241 bool fWeOwnTheBuffer;
242
243 void init(void*, size_t);
244};
245
246/////////////////////////////////////////////////////////////////////////////////////////////
247
248class SkFILEWStream : public SkWStream {
249public:
250 SkFILEWStream(const char path[]);
251 virtual ~SkFILEWStream();
252
253 /** Returns true if the current path could be opened.
254 */
255 bool isValid() const { return fFILE != NULL; }
256
257 virtual bool write(const void* buffer, size_t size);
258 virtual void flush();
259private:
260 SkFILE* fFILE;
261};
262
263class SkMemoryWStream : public SkWStream {
264public:
265 SkMemoryWStream(void* buffer, size_t size);
266 virtual bool write(const void* buffer, size_t size);
267
268private:
269 char* fBuffer;
270 size_t fMaxLength;
271 size_t fBytesWritten;
272};
273
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000274class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275public:
276 SkDynamicMemoryWStream();
277 virtual ~SkDynamicMemoryWStream();
278 virtual bool write(const void* buffer, size_t size);
279 // random access write
280 // modifies stream and returns true if offset + size is less than or equal to getOffset()
281 bool write(const void* buffer, size_t offset, size_t size);
282 bool read(void* buffer, size_t offset, size_t size);
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000283 size_t getOffset() const { return fBytesWritten; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285 // copy what has been written to the stream into dst
286 void copyTo(void* dst) const;
287 /* return a cache of the flattened data returned by copyTo().
288 This copy is only valid until the next call to write().
289 The memory is managed by the stream class.
290 */
291 const char* getStream() const;
292
293 // same as getStream, but additionally detach the flattened datat
reed@google.com70442a62011-06-23 21:48:04 +0000294 // DEPRECATED : use copyToData() instead
295 const char* detach() const;
296
297 /**
298 * Return a copy of the data written so far
299 */
300 SkDataRef* copyToData() const;
301
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 // reset the stream to its original state
303 void reset();
304 void padToAlign4();
305private:
306 struct Block;
307 Block* fHead;
308 Block* fTail;
309 size_t fBytesWritten;
310 mutable char* fCopyToCache;
311};
312
313
314class SkDebugWStream : public SkWStream {
315public:
316 // overrides
317 virtual bool write(const void* buffer, size_t size);
318 virtual void newline();
319};
320
321// for now
322typedef SkFILEStream SkURLStream;
323
324#endif