Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(652)

Side by Side Diff: include/core/SkStream.h

Issue 15298009: Change SkStream. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Clean up, address comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/core/SkOSFile.h ('k') | src/core/SkData.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkStream_DEFINED 8 #ifndef SkStream_DEFINED
9 #define SkStream_DEFINED 9 #define SkStream_DEFINED
10 10
11 #include "SkRefCnt.h" 11 #include "SkRefCnt.h"
12 #include "SkScalar.h" 12 #include "SkScalar.h"
13 13
14 class SkData; 14 class SkData;
15 15
16 class SkStream;
17 class SkStreamRewindable;
18 class SkStreamSeekable;
19 class SkStreamAsset;
20 class SkStreamMemory;
21
16 /** 22 /**
17 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by 23 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
18 * memory, or a file, or something else. 24 * memory, or a file, or something else.
19 * 25 *
20 * NOTE: 26 * NOTE:
21 * 27 *
22 * Classic "streams" APIs are sort of async, in that on a request for N 28 * Classic "streams" APIs are sort of async, in that on a request for N
23 * bytes, they may return fewer than N bytes on a given call, in which case 29 * bytes, they may return fewer than N bytes on a given call, in which case
24 * the caller can "try again" to get more bytes, eventually (modulo an error) 30 * the caller can "try again" to get more bytes, eventually (modulo an error)
25 * receiving their total N bytes. 31 * receiving their total N bytes.
26 * 32 *
27 * Skia streams behave differently. They are effectively synchronous, and will 33 * Skia streams behave differently. They are effectively synchronous, and will
28 * always return all N bytes of the request if possible. If they return fewer 34 * always return all N bytes of the request if possible. If they return fewer
29 * (the read() call returns the number of bytes read) then that means there is 35 * (the read() call returns the number of bytes read) then that means there is
30 * no more data (at EOF or hit an error). The caller should *not* call again 36 * no more data (at EOF or hit an error). The caller should *not* call again
31 * in hopes of fulfilling more of the request. 37 * in hopes of fulfilling more of the request.
32 */ 38 */
33 class SK_API SkStream : public SkRefCnt { 39 class SK_API SkStream : public SkRefCnt {
34 public: 40 public:
35 /** 41 /**
36 * Attempts to open the specified file, and return a stream to it (using 42 * Attempts to open the specified file, and return a stream to it (using
37 * mmap if available). On success, the caller must call unref() on the 43 * mmap if available). On success, the caller must call unref() on the
38 * returned object. On failure, returns NULL. 44 * returned object. On failure, returns NULL.
39 */ 45 */
40 static SkStream* NewFromFile(const char path[]); 46 static SkStreamAsset* NewFromFile(const char path[]);
41 47
42 SK_DECLARE_INST_COUNT(SkStream) 48 SK_DECLARE_INST_COUNT(SkStream)
43 49
44 /** Called to rewind to the beginning of the stream. If this cannot be 50 /** Reads or skips size number of bytes.
45 done, return false. 51 * If buffer == NULL, skip size bytes, return how many were skipped.
46 */ 52 * If buffer != NULL, copy size bytes into buffer, return how many were cop ied.
47 virtual bool rewind() = 0; 53 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
48 /** If this stream represents a file, this method returns the file's name. 54 * @param size the number of bytes to skip or copy
49 If it does not, it returns NULL (the default behavior). 55 * @return bytes read on success
50 */ 56 */
51 virtual const char* getFileName();
52 /** Called to read or skip size number of bytes.
53 If buffer is NULL and size > 0, skip that many bytes, returning how many were skipped.
54 If buffer is NULL and size == 0, return the total length of the stream.
55 If buffer != NULL, copy the requested number of bytes into buffer, retur ning how many were copied.
56 @param buffer If buffer is NULL, ignore and just skip size bytes, othe rwise copy size bytes into buffer
57 @param size The number of bytes to skip or copy
58 @return bytes read on success
59 */
60 virtual size_t read(void* buffer, size_t size) = 0; 57 virtual size_t read(void* buffer, size_t size) = 0;
61 58
62 /** Return the total length of the stream. 59 /** Skip size number of bytes.
63 */ 60 * @return the actual number bytes that could be skipped.
64 size_t getLength() { return this->read(NULL, 0); } 61 */
62 size_t skip(size_t size) {
63 //return this->read(NULL, size);
64 //TODO: remove this old logic after updating existing implementations
65 return 0 == size ? 0 : this->read(NULL, size);
66 }
65 67
66 /** Skip the specified number of bytes, returning the actual number 68 /** Returns true if there are no more bytes to be read.
67 of bytes that could be skipped. 69 * In Progress: do not use until all implementations are updated.
68 */ 70 * TODO: after this is implemented everywhere, make pure virtual.
69 size_t skip(size_t bytes); 71 */
70 72 virtual bool atEnd() const {
71 /** If the stream is backed by RAM, this method returns the starting 73 SkASSERT(false);
72 address for the data. If not (i.e. it is backed by a file or other 74 return true;
73 structure), this method returns NULL. 75 }
74 The default implementation returns NULL.
75 */
76 virtual const void* getMemoryBase();
77 76
78 int8_t readS8(); 77 int8_t readS8();
79 int16_t readS16(); 78 int16_t readS16();
80 int32_t readS32(); 79 int32_t readS32();
81 80
82 uint8_t readU8() { return (uint8_t)this->readS8(); } 81 uint8_t readU8() { return (uint8_t)this->readS8(); }
83 uint16_t readU16() { return (uint16_t)this->readS16(); } 82 uint16_t readU16() { return (uint16_t)this->readS16(); }
84 uint32_t readU32() { return (uint32_t)this->readS32(); } 83 uint32_t readU32() { return (uint32_t)this->readS32(); }
85 84
86 bool readBool() { return this->readU8() != 0; } 85 bool readBool() { return this->readU8() != 0; }
87 SkScalar readScalar(); 86 SkScalar readScalar();
88 size_t readPackedUInt(); 87 size_t readPackedUInt();
89 88
90 /** 89 /**
91 * Reconstitute an SkData object that was written to the stream 90 * Reconstitute an SkData object that was written to the stream
92 * using SkWStream::writeData(). 91 * using SkWStream::writeData().
93 */ 92 */
94 SkData* readData(); 93 SkData* readData();
95 94
95 //SkStreamRewindable
96 /** Rewinds to the beginning of the stream. If this cannot be done, return f alse. */
97 virtual bool rewind() { return false; }
98
99 /** Duplicates this stream. If this cannot be done, returns NULL.
100 * The returned stream will be positioned at the beginning of its data.
101 */
102 virtual SkStreamRewindable* duplicate() const { return NULL; }
103
104 //SkStreamSeekable
105 /** Returns true if this stream can report it's current position. */
106 virtual bool hasPosition() const { return false; }
107 /** Returns the current position in the stream. If this cannot be done, retu rns 0. */
108 virtual size_t getPosition() const { return 0; }
109
110 /** Seeks to an absolute position in the stream. If this cannot be done, ret urns false.
111 * If an attempt is made to seek past the end of the stream, the position w ill be set
112 * to the end of the stream.
113 */
114 virtual bool seek(size_t position) { return false; }
115
116 /** Seeks to an relative offset in the stream. If this cannot be done, retur ns false.
117 * If an attempt is made to move to a position outside the stream, the posi tion will be set
118 * to the closest point within the stream (beginning or end).
119 */
120 virtual bool move(long offset) { return false; }
121
122 /** Duplicates this stream. If this cannot be done, returns NULL.
123 * The returned stream will be positioned the same as this stream.
124 */
125 virtual SkStreamSeekable* fork() const { return NULL; }
126
127 //SkStreamAsset
128 /** Returns true if this stream can report it's total length. */
129 virtual bool hasLength() const { return false; }
130 /** Returns the total length of the stream. If this cannot be done, returns 0. */
131 virtual size_t getLength() const {
132 //return 0;
133 //TODO: remove the following after everyone is updated.
134 return ((SkStream*)this)->read(NULL, 0);
135 }
136
137 //SkStreamMemory
138 /** Returns the starting address for the data. If this cannot be done, retur ns NULL. */
139 virtual const void* getMemoryBase() { return NULL; }
140
96 private: 141 private:
97 typedef SkRefCnt INHERITED; 142 typedef SkRefCnt INHERITED;
98 }; 143 };
99 144
145 /** SkStreamRewindable is a SkStream for which rewind and duplicate are required . */
146 class SK_API SkStreamRewindable : public SkStream {
147 public:
148 //TODO: remove the following after everyone is updated (ensures new behavior on new classes).
149 virtual bool atEnd() const SK_OVERRIDE = 0;
150 //TODO: remove the following after everyone is updated (ensures new behavior on new classes).
151 virtual size_t getLength() const SK_OVERRIDE { return 0; }
152
153 virtual bool rewind() SK_OVERRIDE = 0;
154 virtual SkStreamRewindable* duplicate() const SK_OVERRIDE = 0;
155 };
156
157 /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
158 class SK_API SkStreamSeekable : public SkStreamRewindable {
159 public:
160 virtual SkStreamSeekable* duplicate() const SK_OVERRIDE = 0;
161
162 virtual bool hasPosition() const SK_OVERRIDE { return true; }
163 virtual size_t getPosition() const SK_OVERRIDE = 0;
164 virtual bool seek(size_t position) SK_OVERRIDE = 0;
165 virtual bool move(long offset) SK_OVERRIDE = 0;
166 virtual SkStreamSeekable* fork() const SK_OVERRIDE = 0;
167 };
168
169 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
170 class SK_API SkStreamAsset : public SkStreamSeekable {
171 public:
172 virtual SkStreamAsset* duplicate() const SK_OVERRIDE = 0;
173 virtual SkStreamAsset* fork() const SK_OVERRIDE = 0;
174
175 virtual bool hasLength() const SK_OVERRIDE { return true; }
176 virtual size_t getLength() const SK_OVERRIDE = 0;
177 };
178
179 /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
180 class SK_API SkStreamMemory : public SkStreamAsset {
181 public:
182 virtual SkStreamMemory* duplicate() const SK_OVERRIDE = 0;
183 virtual SkStreamMemory* fork() const SK_OVERRIDE = 0;
184
185 virtual const void* getMemoryBase() SK_OVERRIDE = 0;
186 };
187
100 class SK_API SkWStream : SkNoncopyable { 188 class SK_API SkWStream : SkNoncopyable {
101 public: 189 public:
102 SK_DECLARE_INST_COUNT_ROOT(SkWStream) 190 SK_DECLARE_INST_COUNT_ROOT(SkWStream)
103 191
104 virtual ~SkWStream(); 192 virtual ~SkWStream();
105 193
106 /** Called to write bytes to a SkWStream. Returns true on success 194 /** Called to write bytes to a SkWStream. Returns true on success
107 @param buffer the address of at least size bytes to be written to the st ream 195 @param buffer the address of at least size bytes to be written to the st ream
108 @param size The number of bytes in buffer to write to the stream 196 @param size The number of bytes in buffer to write to the stream
109 @return true on success 197 @return true on success
(...skipping 30 matching lines...) Expand all
140 */ 228 */
141 bool writeData(const SkData*); 229 bool writeData(const SkData*);
142 }; 230 };
143 231
144 //////////////////////////////////////////////////////////////////////////////// //////// 232 //////////////////////////////////////////////////////////////////////////////// ////////
145 233
146 #include "SkString.h" 234 #include "SkString.h"
147 235
148 struct SkFILE; 236 struct SkFILE;
149 237
150 /** A stream that reads from a FILE*, which is opened in the constructor and 238 /** A stream that wraps a C FILE* file stream. */
151 closed in the destructor 239 class SK_API SkFILEStream : public SkStreamAsset {
152 */
153 class SK_API SkFILEStream : public SkStream {
154 public: 240 public:
155 SK_DECLARE_INST_COUNT(SkFILEStream) 241 SK_DECLARE_INST_COUNT(SkFILEStream)
156 242
157 /** Initialize the stream by calling fopen on the specified path. Will be 243 /** Initialize the stream by calling sk_fopen on the specified path.
158 closed in the destructor. 244 * This internal stream will be closed in the destructor.
159 */ 245 */
160 explicit SkFILEStream(const char path[] = NULL); 246 explicit SkFILEStream(const char path[] = NULL);
247
248 enum Ownership {
249 kCallerPasses_Ownership,
250 kCallerRetains_Ownership
251 };
252 /** Initialize the stream with an existing C file stream.
253 * While this stream exists, it assumes exclusive access to the C file stre am.
254 * The C file stream will be closed in the destructor unless the caller spe cifies
255 * kCallerRetains_Ownership.
256 */
257 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Owners hip);
258
161 virtual ~SkFILEStream(); 259 virtual ~SkFILEStream();
162 260
163 /** Returns true if the current path could be opened. 261 /** Returns true if the current path could be opened. */
164 */
165 bool isValid() const { return fFILE != NULL; } 262 bool isValid() const { return fFILE != NULL; }
166 /** Close the current file, and open a new file with the specified 263
167 path. If path is NULL, just close the current file. 264 /** Close the current file, and open a new file with the specified path.
168 */ 265 * If path is NULL, just close the current file.
266 */
169 void setPath(const char path[]); 267 void setPath(const char path[]);
170 268
269 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
270 virtual bool atEnd() const SK_OVERRIDE;
271
171 virtual bool rewind() SK_OVERRIDE; 272 virtual bool rewind() SK_OVERRIDE;
172 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; 273 virtual SkStreamAsset* duplicate() const SK_OVERRIDE;
173 virtual const char* getFileName() SK_OVERRIDE; 274
275 virtual size_t getPosition() const SK_OVERRIDE;
276 virtual bool seek(size_t position) SK_OVERRIDE;
277 virtual bool move(long offset) SK_OVERRIDE;
278 virtual SkStreamAsset* fork() const SK_OVERRIDE;
279
280 virtual size_t getLength() const SK_OVERRIDE;
281
282 const void* getMemoryBase() SK_OVERRIDE;
174 283
175 private: 284 private:
176 SkFILE* fFILE; 285 SkFILE* fFILE;
177 SkString fName; 286 SkString fName;
287 Ownership fOwnership;
288 // fData is lazilly initialized when needed.
289 mutable SkAutoTUnref<SkData> fData;
178 290
179 typedef SkStream INHERITED; 291 typedef SkStreamAsset INHERITED;
180 }; 292 };
181 293
182 /** A stream that reads from a file descriptor 294 class SK_API SkMemoryStream : public SkStreamMemory {
183 */
184 class SK_API SkFDStream : public SkStream {
185 public:
186 SK_DECLARE_INST_COUNT(SkFDStream)
187
188 /** Initialize the stream with a dup() of the specified file descriptor.
189 If closeWhenDone is true, then the descriptor will be closed in the
190 destructor.
191 */
192 SkFDStream(int fileDesc, bool closeWhenDone);
193 virtual ~SkFDStream();
194
195 /** Returns true if the current path could be opened.
196 */
197 bool isValid() const { return fFD >= 0; }
198
199 virtual bool rewind() SK_OVERRIDE;
200 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
201 virtual const char* getFileName() SK_OVERRIDE { return NULL; }
202
203 private:
204 int fFD;
205 bool fCloseWhenDone;
206
207 typedef SkStream INHERITED;
208 };
209
210 class SK_API SkMemoryStream : public SkStream {
211 public: 295 public:
212 SK_DECLARE_INST_COUNT(SkMemoryStream) 296 SK_DECLARE_INST_COUNT(SkMemoryStream)
213 297
214 SkMemoryStream(); 298 SkMemoryStream();
215 /** We allocate (and free) the memory. Write to it via getMemoryBase() 299
216 */ 300 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
217 SkMemoryStream(size_t length); 301 SkMemoryStream(size_t length);
218 /** if copyData is true, the stream makes a private copy of the data 302
219 */ 303 /** If copyData is true, the stream makes a private copy of the data. */
220 SkMemoryStream(const void* data, size_t length, bool copyData = false); 304 SkMemoryStream(const void* data, size_t length, bool copyData = false);
221 305
222 /** 306 /** Use the specified data as the memory for this stream.
223 * Use the specified data as the memory for this stream. The stream will 307 * The stream will call ref() on the data (assuming it is not NULL).
224 * call ref() on the data (assuming it is not null).
225 */ 308 */
226 SkMemoryStream(SkData*); 309 SkMemoryStream(SkData*);
227 310
228 virtual ~SkMemoryStream(); 311 virtual ~SkMemoryStream();
229 312
230 /** Resets the stream to the specified data and length, 313 /** Resets the stream to the specified data and length,
231 just like the constructor. 314 just like the constructor.
232 if copyData is true, the stream makes a private copy of the data 315 if copyData is true, the stream makes a private copy of the data
233 */ 316 */
234 virtual void setMemory(const void* data, size_t length, 317 virtual void setMemory(const void* data, size_t length,
235 bool copyData = false); 318 bool copyData = false);
236 /** Replace any memory buffer with the specified buffer. The caller 319 /** Replace any memory buffer with the specified buffer. The caller
237 must have allocated data with sk_malloc or sk_realloc, since it 320 must have allocated data with sk_malloc or sk_realloc, since it
238 will be freed with sk_free. 321 will be freed with sk_free.
239 */ 322 */
240 void setMemoryOwned(const void* data, size_t length); 323 void setMemoryOwned(const void* data, size_t length);
241 324
242 /** 325 /** Return the stream's data in a SkData.
243 * Return the stream's data in a SkData. The caller must call unref() when 326 * The caller must call unref() when it is finished using the data.
244 * it is finished using the data.
245 */ 327 */
246 SkData* copyToData() const; 328 SkData* copyToData() const;
247 329
248 /** 330 /**
249 * Use the specified data as the memory for this stream. The stream will 331 * Use the specified data as the memory for this stream.
250 * call ref() on the data (assuming it is not null). The function returns 332 * The stream will call ref() on the data (assuming it is not NULL).
251 * the data parameter as a convenience. 333 * The function returns the data parameter as a convenience.
252 */ 334 */
253 SkData* setData(SkData*); 335 SkData* setData(SkData*);
254 336
255 void skipToAlign4(); 337 void skipToAlign4();
338 const void* getAtPos();
339 size_t peek() const { return fOffset; }
340
341 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
342 virtual bool atEnd() const SK_OVERRIDE;
343
256 virtual bool rewind() SK_OVERRIDE; 344 virtual bool rewind() SK_OVERRIDE;
257 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; 345 virtual SkMemoryStream* duplicate() const SK_OVERRIDE;
346
347 virtual size_t getPosition() const SK_OVERRIDE;
348 virtual bool seek(size_t position) SK_OVERRIDE;
349 virtual bool move(long offset) SK_OVERRIDE;
350 virtual SkMemoryStream* fork() const SK_OVERRIDE;
351
352 virtual size_t getLength() const SK_OVERRIDE;
353
258 virtual const void* getMemoryBase() SK_OVERRIDE; 354 virtual const void* getMemoryBase() SK_OVERRIDE;
259 const void* getAtPos();
260 size_t seek(size_t offset);
261 size_t peek() const { return fOffset; }
262 355
263 private: 356 private:
264 SkData* fData; 357 SkData* fData;
265 size_t fOffset; 358 size_t fOffset;
266 359
267 typedef SkStream INHERITED; 360 typedef SkStreamMemory INHERITED;
268 };
269
270 /** \class SkBufferStream
271 This is a wrapper class that adds buffering to another stream.
272 The caller can provide the buffer, or ask SkBufferStream to allocated/free
273 it automatically.
274 */
275 class SK_API SkBufferStream : public SkStream {
276 public:
277 SK_DECLARE_INST_COUNT(SkBufferStream)
278
279 /** Provide the stream to be buffered (proxy), and the size of the buffer th at
280 should be used. This will be allocated and freed automatically. If buffe rSize is 0,
281 a default buffer size will be used.
282 The proxy stream is referenced, and will be unreferenced in when the
283 bufferstream is destroyed.
284 */
285 SkBufferStream(SkStream* proxy, size_t bufferSize = 0);
286 /** Provide the stream to be buffered (proxy), and a buffer and size to be u sed.
287 This buffer is owned by the caller, and must be at least bufferSize byte s big.
288 Passing NULL for buffer will cause the buffer to be allocated/freed auto matically.
289 If buffer is not NULL, it is an error for bufferSize to be 0.
290 The proxy stream is referenced, and will be unreferenced in when the
291 bufferstream is destroyed.
292 */
293 SkBufferStream(SkStream* proxy, void* buffer, size_t bufferSize);
294 virtual ~SkBufferStream();
295
296 virtual bool rewind() SK_OVERRIDE;
297 virtual const char* getFileName() SK_OVERRIDE;
298 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
299 virtual const void* getMemoryBase() SK_OVERRIDE;
300
301 private:
302 enum {
303 kDefaultBufferSize = 128
304 };
305 // illegal
306 SkBufferStream(const SkBufferStream&);
307 SkBufferStream& operator=(const SkBufferStream&);
308
309 SkStream* fProxy;
310 char* fBuffer;
311 size_t fOrigBufferSize, fBufferSize, fBufferOffset;
312 bool fWeOwnTheBuffer;
313
314 void init(void*, size_t);
315
316 typedef SkStream INHERITED;
317 }; 361 };
318 362
319 //////////////////////////////////////////////////////////////////////////////// ///////////// 363 //////////////////////////////////////////////////////////////////////////////// /////////////
320 364
321 class SK_API SkFILEWStream : public SkWStream { 365 class SK_API SkFILEWStream : public SkWStream {
322 public: 366 public:
323 SK_DECLARE_INST_COUNT(SkFILEWStream) 367 SK_DECLARE_INST_COUNT(SkFILEWStream)
324 368
325 SkFILEWStream(const char path[]); 369 SkFILEWStream(const char path[]);
326 virtual ~SkFILEWStream(); 370 virtual ~SkFILEWStream();
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 virtual void newline() SK_OVERRIDE; 447 virtual void newline() SK_OVERRIDE;
404 448
405 private: 449 private:
406 typedef SkWStream INHERITED; 450 typedef SkWStream INHERITED;
407 }; 451 };
408 452
409 // for now 453 // for now
410 typedef SkFILEStream SkURLStream; 454 typedef SkFILEStream SkURLStream;
411 455
412 #endif 456 #endif
OLDNEW
« no previous file with comments | « include/core/SkOSFile.h ('k') | src/core/SkData.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698