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

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

Issue 15298009: Change SkStream. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Add DirectWrite stream to change. 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:
41 class Optimizer {
42 public:
43 virtual void operator()(SkStreamMemory& stream) = 0;
44 virtual void operator()(SkStreamAsset& stream) = 0;
45 virtual void operator()(SkStreamSeekable& stream) = 0;
46 virtual void operator()(SkStreamRewindable& stream) = 0;
47 virtual void operator()(SkStream& stream) = 0;
48 };
49 /** Stream/Optimizer double dispatch entry point.
50 *
51 * Users of stream can call this method, which will then call the operator( ) on optimizer
52 * which accepts the runtime type of this stream.
53 *
54 * All subclasses which wish to support optimization must override this met hod.
55 * When overridden the implementation must be 'optimizer(*this);'.
56 */
57 virtual void optimize(Optimizer& optimizer) { optimizer(*this); }
djsollen 2013/05/21 15:41:22 remove the optimizer until we have a demonstrable
bungeman-skia 2013/05/21 23:24:59 Done.
58
35 /** 59 /**
36 * Attempts to open the specified file, and return a stream to it (using 60 * 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 61 * mmap if available). On success, the caller must call unref() on the
38 * returned object. On failure, returns NULL. 62 * returned object. On failure, returns NULL.
39 */ 63 */
40 static SkStream* NewFromFile(const char path[]); 64 static SkStreamAsset* NewFromFile(const char path[]);
41 65
42 SK_DECLARE_INST_COUNT(SkStream) 66 SK_DECLARE_INST_COUNT(SkStream)
43 67
44 /** Called to rewind to the beginning of the stream. If this cannot be 68 //SkStream
djsollen 2013/05/21 15:41:22 remove this
bungeman-skia 2013/05/21 23:24:59 Done.
45 done, return false. 69 /** Reads or skips size number of bytes.
46 */ 70 * If buffer == NULL, skip size bytes, returning how many were skipped.
47 virtual bool rewind() = 0; 71 * If buffer != NULL, copy the requested number of bytes into buffer, retur ning how many were copied.
48 /** If this stream represents a file, this method returns the file's name. 72 * @param buffer when NULL, ignore and just skip size bytes, otherwise copy size bytes into buffer
49 If it does not, it returns NULL (the default behavior). 73 * @param size the number of bytes to skip or copy
50 */ 74 * @return bytes read on success
51 virtual const char* getFileName(); 75 */
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; 76 virtual size_t read(void* buffer, size_t size) = 0;
61 77
62 /** Return the total length of the stream. 78 /** Skip size number of bytes.
63 */ 79 * @return the actual number bytes that could be skipped.
64 size_t getLength() { return this->read(NULL, 0); } 80 */
65 81 size_t skip(size_t size) {
66 /** Skip the specified number of bytes, returning the actual number 82 return this->read(NULL, size);
67 of bytes that could be skipped. 83 }
68 */
69 size_t skip(size_t bytes);
70
71 /** If the stream is backed by RAM, this method returns the starting
72 address for the data. If not (i.e. it is backed by a file or other
73 structure), this method returns NULL.
74 The default implementation returns NULL.
75 */
76 virtual const void* getMemoryBase();
77 84
78 int8_t readS8(); 85 int8_t readS8();
79 int16_t readS16(); 86 int16_t readS16();
80 int32_t readS32(); 87 int32_t readS32();
81 88
82 uint8_t readU8() { return (uint8_t)this->readS8(); } 89 uint8_t readU8() { return (uint8_t)this->readS8(); }
83 uint16_t readU16() { return (uint16_t)this->readS16(); } 90 uint16_t readU16() { return (uint16_t)this->readS16(); }
84 uint32_t readU32() { return (uint32_t)this->readS32(); } 91 uint32_t readU32() { return (uint32_t)this->readS32(); }
85 92
86 bool readBool() { return this->readU8() != 0; } 93 bool readBool() { return this->readU8() != 0; }
87 SkScalar readScalar(); 94 SkScalar readScalar();
88 size_t readPackedUInt(); 95 size_t readPackedUInt();
89 96
90 /** 97 /**
91 * Reconstitute an SkData object that was written to the stream 98 * Reconstitute an SkData object that was written to the stream
92 * using SkWStream::writeData(). 99 * using SkWStream::writeData().
93 */ 100 */
94 SkData* readData(); 101 SkData* readData();
95 102
103 //SkStreamRewindable
104 /** Rewinds to the beginning of the stream. If this cannot be done, return f alse. */
105 virtual bool rewind() { return false; }
106
107 /** Duplicates this stream. If this cannot be done, returns NULL.
108 * The returned stream will be positioned at the beginning of its data.
109 */
110 virtual SkStreamRewindable* duplicate() const { return NULL; }
111
112 //SkStreamSeekable
113 /** Seeks to an absolute position in the stream. If this cannot be done, ret urns false.
114 * If an attempt is made to seek past the end of the stream, the position w ill be set
115 * to the end of the stream.
116 */
117 virtual bool seek(size_t position) { return false; }
118
119 /** Seeks to an relative offset in the stream. If this cannot be done, retur ns false.
120 * If an attempt is made to move to a position outside the stream, the posi tion will be set
121 * to the closest point within the stream (beginning or end).
122 */
123 virtual bool move(long offset) { return false; }
124
125 /** Duplicates this stream. If this cannot be done, returns NULL.
126 * The returned stream will be positioned the same as this stream.
127 */
128 virtual SkStreamSeekable* fork() const { return NULL; }
129
130 //SkStreamAsset
131 /** Returns the total length of the stream. If this cannot be done, returns zero. */
132 virtual size_t getLength() const {
133 //return 0;
134 // Until we can update all implementations, keep legacy behavior.
135 return ((SkStream*)this)->read(NULL, 0);
136 }
137
138 //SkStreamMemory
139 /** Returns the starting address for the data. If this cannot be done, retur ns NULL. */
140 virtual const void* getMemoryBase() { return NULL; }
141
96 private: 142 private:
97 typedef SkRefCnt INHERITED; 143 typedef SkRefCnt INHERITED;
98 }; 144 };
99 145
146 /** SkStreamRewindable is a SkStream for which rewind and duplicate are required . */
147 class SK_API SkStreamRewindable : public SkStream {
148 public:
149 virtual void optimize(Optimizer& optimizer) SK_OVERRIDE { optimizer(*this); }
150
151 virtual bool rewind() SK_OVERRIDE = 0;
152 virtual SkStreamRewindable* duplicate() const SK_OVERRIDE = 0;
153 };
154
155 /** SkStreamSeekable is a SkStreamRewindable for which seek, move, and fork are required. */
156 class SK_API SkStreamSeekable : public SkStreamRewindable {
157 public:
158 virtual void optimize(Optimizer& optimizer) SK_OVERRIDE { optimizer(*this); }
159
160 virtual SkStreamSeekable* duplicate() const SK_OVERRIDE = 0;
161
162 virtual bool seek(size_t position) SK_OVERRIDE = 0;
163 virtual bool move(long offset) SK_OVERRIDE = 0;
164 virtual SkStreamSeekable* fork() const SK_OVERRIDE = 0;
165 };
166
167 /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
168 class SK_API SkStreamAsset : public SkStreamSeekable {
169 public:
170 virtual void optimize(Optimizer& optimizer) SK_OVERRIDE { optimizer(*this); }
171
172 virtual SkStreamAsset* duplicate() const SK_OVERRIDE = 0;
173 virtual SkStreamAsset* fork() const SK_OVERRIDE = 0;
174
175 virtual size_t getLength() const SK_OVERRIDE = 0;
176 };
177
178 /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
179 class SK_API SkStreamMemory : public SkStreamAsset {
180 public:
181 virtual void optimize(Optimizer& optimizer) SK_OVERRIDE { optimizer(*this); }
182
183 virtual SkStreamMemory* duplicate() const SK_OVERRIDE = 0;
184 virtual SkStreamMemory* fork() const SK_OVERRIDE = 0;
185
186 virtual const void* getMemoryBase() SK_OVERRIDE = 0;
187 };
188
100 class SK_API SkWStream : SkNoncopyable { 189 class SK_API SkWStream : SkNoncopyable {
101 public: 190 public:
102 SK_DECLARE_INST_COUNT_ROOT(SkWStream) 191 SK_DECLARE_INST_COUNT_ROOT(SkWStream)
103 192
104 virtual ~SkWStream(); 193 virtual ~SkWStream();
105 194
106 /** Called to write bytes to a SkWStream. Returns true on success 195 /** 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 196 @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 197 @param size The number of bytes in buffer to write to the stream
109 @return true on success 198 @return true on success
(...skipping 30 matching lines...) Expand all
140 */ 229 */
141 bool writeData(const SkData*); 230 bool writeData(const SkData*);
142 }; 231 };
143 232
144 //////////////////////////////////////////////////////////////////////////////// //////// 233 //////////////////////////////////////////////////////////////////////////////// ////////
145 234
146 #include "SkString.h" 235 #include "SkString.h"
147 236
148 struct SkFILE; 237 struct SkFILE;
149 238
150 /** A stream that reads from a FILE*, which is opened in the constructor and 239 /** A stream that wraps a C FILE* file stream. */
151 closed in the destructor 240 class SK_API SkFILEStream : public SkStreamAsset {
152 */
153 class SK_API SkFILEStream : public SkStream {
154 public: 241 public:
155 SK_DECLARE_INST_COUNT(SkFILEStream) 242 SK_DECLARE_INST_COUNT(SkFILEStream)
156 243
157 /** Initialize the stream by calling fopen on the specified path. Will be 244 /** Initialize the stream by calling sk_fopen on the specified path.
158 closed in the destructor. 245 * This internal stream will be closed in the destructor.
159 */ 246 */
160 explicit SkFILEStream(const char path[] = NULL); 247 explicit SkFILEStream(const char path[] = NULL);
248
249 enum Ownership {
250 kCallerPasses_Ownership,
251 kCallerRetains_Ownership
252 };
253 /** Initialize the stream with an existing C file stream.
254 * While this stream exists, it assumes exclusive access to the C file stre am.
255 * The C file stream will be closed in the destructor unless the caller spe cifies
256 * kCallerRetains_Ownership.
257 */
258 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Owners hip);
259
161 virtual ~SkFILEStream(); 260 virtual ~SkFILEStream();
162 261
163 /** Returns true if the current path could be opened. 262 /** Returns true if the current path could be opened. */
164 */
165 bool isValid() const { return fFILE != NULL; } 263 bool isValid() const { return fFILE != NULL; }
166 /** Close the current file, and open a new file with the specified 264
167 path. If path is NULL, just close the current file. 265 /** Close the current file, and open a new file with the specified path.
168 */ 266 * If path is NULL, just close the current file.
267 */
169 void setPath(const char path[]); 268 void setPath(const char path[]);
170 269
270 virtual size_t read(void* buffer, size_t size) 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 bool seek(size_t position) SK_OVERRIDE;
276 virtual bool move(long offset) SK_OVERRIDE;
277 virtual SkStreamAsset* fork() const SK_OVERRIDE;
278
279 virtual size_t getLength() const SK_OVERRIDE;
280
281 const void* getMemoryBase() SK_OVERRIDE;
174 282
175 private: 283 private:
176 SkFILE* fFILE; 284 SkFILE* fFILE;
177 SkString fName; 285 SkString fName;
286 Ownership fOwnership;
287 // fData is lazilly initialized when needed.
288 mutable SkAutoTUnref<SkData> fData;
178 289
179 typedef SkStream INHERITED; 290 typedef SkStreamAsset INHERITED;
180 }; 291 };
181 292
182 /** A stream that reads from a file descriptor 293 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: 294 public:
212 SK_DECLARE_INST_COUNT(SkMemoryStream) 295 SK_DECLARE_INST_COUNT(SkMemoryStream)
213 296
214 SkMemoryStream(); 297 SkMemoryStream();
215 /** We allocate (and free) the memory. Write to it via getMemoryBase() 298
216 */ 299 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
217 SkMemoryStream(size_t length); 300 SkMemoryStream(size_t length);
218 /** if copyData is true, the stream makes a private copy of the data 301
219 */ 302 /** If copyData is true, the stream makes a private copy of the data. */
220 SkMemoryStream(const void* data, size_t length, bool copyData = false); 303 SkMemoryStream(const void* data, size_t length, bool copyData = false);
221 304
222 /** 305 /** Use the specified data as the memory for this stream.
223 * Use the specified data as the memory for this stream. The stream will 306 * 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 */ 307 */
226 SkMemoryStream(SkData*); 308 SkMemoryStream(SkData*);
227 309
228 virtual ~SkMemoryStream(); 310 virtual ~SkMemoryStream();
229 311
230 /** Resets the stream to the specified data and length, 312 /** Resets the stream to the specified data and length,
231 just like the constructor. 313 just like the constructor.
232 if copyData is true, the stream makes a private copy of the data 314 if copyData is true, the stream makes a private copy of the data
233 */ 315 */
234 virtual void setMemory(const void* data, size_t length, 316 virtual void setMemory(const void* data, size_t length,
235 bool copyData = false); 317 bool copyData = false);
236 /** Replace any memory buffer with the specified buffer. The caller 318 /** Replace any memory buffer with the specified buffer. The caller
237 must have allocated data with sk_malloc or sk_realloc, since it 319 must have allocated data with sk_malloc or sk_realloc, since it
238 will be freed with sk_free. 320 will be freed with sk_free.
239 */ 321 */
240 void setMemoryOwned(const void* data, size_t length); 322 void setMemoryOwned(const void* data, size_t length);
241 323
242 /** 324 /** Return the stream's data in a SkData.
243 * Return the stream's data in a SkData. The caller must call unref() when 325 * The caller must call unref() when it is finished using the data.
244 * it is finished using the data.
245 */ 326 */
246 SkData* copyToData() const; 327 SkData* copyToData() const;
247 328
248 /** 329 /**
249 * Use the specified data as the memory for this stream. The stream will 330 * Use the specified data as the memory for this stream.
250 * call ref() on the data (assuming it is not null). The function returns 331 * The stream will call ref() on the data (assuming it is not NULL).
251 * the data parameter as a convenience. 332 * The function returns the data parameter as a convenience.
252 */ 333 */
253 SkData* setData(SkData*); 334 SkData* setData(SkData*);
254 335
255 void skipToAlign4(); 336 void skipToAlign4();
337 const void* getAtPos();
338 size_t peek() const { return fOffset; }
339
340 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE;
341
256 virtual bool rewind() SK_OVERRIDE; 342 virtual bool rewind() SK_OVERRIDE;
257 virtual size_t read(void* buffer, size_t size) SK_OVERRIDE; 343 virtual SkMemoryStream* duplicate() const SK_OVERRIDE;
344
345 virtual bool seek(size_t position) SK_OVERRIDE;
346 virtual bool move(long offset) SK_OVERRIDE;
347 virtual SkMemoryStream* fork() const SK_OVERRIDE;
348
349 virtual size_t getLength() const SK_OVERRIDE;
350
258 virtual const void* getMemoryBase() SK_OVERRIDE; 351 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 352
263 private: 353 private:
264 SkData* fData; 354 SkData* fData;
265 size_t fOffset; 355 size_t fOffset;
266 356
267 typedef SkStream INHERITED; 357 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 }; 358 };
318 359
319 //////////////////////////////////////////////////////////////////////////////// ///////////// 360 //////////////////////////////////////////////////////////////////////////////// /////////////
320 361
321 class SK_API SkFILEWStream : public SkWStream { 362 class SK_API SkFILEWStream : public SkWStream {
322 public: 363 public:
323 SK_DECLARE_INST_COUNT(SkFILEWStream) 364 SK_DECLARE_INST_COUNT(SkFILEWStream)
324 365
325 SkFILEWStream(const char path[]); 366 SkFILEWStream(const char path[]);
326 virtual ~SkFILEWStream(); 367 virtual ~SkFILEWStream();
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 virtual void newline() SK_OVERRIDE; 444 virtual void newline() SK_OVERRIDE;
404 445
405 private: 446 private:
406 typedef SkWStream INHERITED; 447 typedef SkWStream INHERITED;
407 }; 448 };
408 449
409 // for now 450 // for now
410 typedef SkFILEStream SkURLStream; 451 typedef SkFILEStream SkURLStream;
411 452
412 #endif 453 #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