Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkStream.h
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkStream_DEFINED
9 #define SkStream_DEFINED
10 
11 #include "SkData.h"
12 #include "SkRefCnt.h"
13 #include "SkScalar.h"
14 
15 class SkStream;
16 class SkStreamRewindable;
17 class SkStreamSeekable;
18 class SkStreamAsset;
19 class SkStreamMemory;
20 
38 class SK_API SkStream : public SkNoncopyable {
39 public:
40  virtual ~SkStream() {}
41 
47  static SkStreamAsset* NewFromFile(const char path[]);
48 
56  virtual size_t read(void* buffer, size_t size) = 0;
57 
61  size_t skip(size_t size) {
62  return this->read(NULL, size);
63  }
64 
78  virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
79 
84  virtual bool isAtEnd() const = 0;
85 
86  int8_t readS8();
87  int16_t readS16();
88  int32_t readS32();
89 
90  uint8_t readU8() { return (uint8_t)this->readS8(); }
91  uint16_t readU16() { return (uint16_t)this->readS16(); }
92  uint32_t readU32() { return (uint32_t)this->readS32(); }
93 
94  bool readBool() { return this->readU8() != 0; }
95  SkScalar readScalar();
96  size_t readPackedUInt();
97 
98 //SkStreamRewindable
102  virtual bool rewind() { return false; }
103 
107  virtual SkStreamRewindable* duplicate() const { return NULL; }
108 
109 //SkStreamSeekable
111  virtual bool hasPosition() const { return false; }
113  virtual size_t getPosition() const { return 0; }
114 
119  virtual bool seek(size_t /*position*/) { return false; }
120 
125  virtual bool move(long /*offset*/) { return false; }
126 
130  virtual SkStreamSeekable* fork() const { return NULL; }
131 
132 //SkStreamAsset
134  virtual bool hasLength() const { return false; }
136  virtual size_t getLength() const { return 0; }
137 
138 //SkStreamMemory
140  //TODO: replace with virtual const SkData* getData()
141  virtual const void* getMemoryBase() { return NULL; }
142 };
143 
145 class SK_API SkStreamRewindable : public SkStream {
146 public:
147  bool rewind() override = 0;
148  SkStreamRewindable* duplicate() const override = 0;
149 };
150 
152 class SK_API SkStreamSeekable : public SkStreamRewindable {
153 public:
154  SkStreamSeekable* duplicate() const override = 0;
155 
156  bool hasPosition() const override { return true; }
157  size_t getPosition() const override = 0;
158  bool seek(size_t position) override = 0;
159  bool move(long offset) override = 0;
160  SkStreamSeekable* fork() const override = 0;
161 };
162 
164 class SK_API SkStreamAsset : public SkStreamSeekable {
165 public:
166  SkStreamAsset* duplicate() const override = 0;
167  SkStreamAsset* fork() const override = 0;
168 
169  bool hasLength() const override { return true; }
170  size_t getLength() const override = 0;
171 };
172 
174 class SK_API SkStreamMemory : public SkStreamAsset {
175 public:
176  SkStreamMemory* duplicate() const override = 0;
177  SkStreamMemory* fork() const override = 0;
178 
179  const void* getMemoryBase() override = 0;
180 };
181 
182 class SK_API SkWStream : SkNoncopyable {
183 public:
184  virtual ~SkWStream();
185 
191  virtual bool write(const void* buffer, size_t size) = 0;
192  virtual void newline();
193  virtual void flush();
194 
195  virtual size_t bytesWritten() const = 0;
196 
197  // helpers
198 
199  bool write8(U8CPU);
200  bool write16(U16CPU);
201  bool write32(uint32_t);
202 
203  bool writeText(const char text[]) {
204  SkASSERT(text);
205  return this->write(text, strlen(text));
206  }
207  bool writeDecAsText(int32_t);
208  bool writeBigDecAsText(int64_t, int minDigits = 0);
209  bool writeHexAsText(uint32_t, int minDigits = 0);
210  bool writeScalarAsText(SkScalar);
211 
212  bool writeBool(bool v) { return this->write8(v); }
213  bool writeScalar(SkScalar);
214  bool writePackedUInt(size_t);
215 
216  bool writeStream(SkStream* input, size_t length);
217 
222  static int SizeOfPackedUInt(size_t value);
223 };
224 
226 
227 #include "SkString.h"
228 #include <stdio.h>
229 
231 class SK_API SkFILEStream : public SkStreamAsset {
232 public:
236  explicit SkFILEStream(const char path[] = NULL);
237 
238  enum Ownership {
239  kCallerPasses_Ownership,
240  kCallerRetains_Ownership
241  };
247  explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
248 
249  virtual ~SkFILEStream();
250 
252  bool isValid() const { return fFILE != NULL; }
253 
257  void setPath(const char path[]);
258 
259  size_t read(void* buffer, size_t size) override;
260  bool isAtEnd() const override;
261 
262  bool rewind() override;
263  SkStreamAsset* duplicate() const override;
264 
265  size_t getPosition() const override;
266  bool seek(size_t position) override;
267  bool move(long offset) override;
268  SkStreamAsset* fork() const override;
269 
270  size_t getLength() const override;
271 
272  const void* getMemoryBase() override;
273 
274 private:
275  FILE* fFILE;
276  SkString fName;
277  Ownership fOwnership;
278  // fData is lazilly initialized when needed.
279  mutable sk_sp<SkData> fData;
280 
281  typedef SkStreamAsset INHERITED;
282 };
283 
284 class SK_API SkMemoryStream : public SkStreamMemory {
285 public:
286  SkMemoryStream();
287 
289  SkMemoryStream(size_t length);
290 
292  SkMemoryStream(const void* data, size_t length, bool copyData = false);
293 
299 
302 
307  virtual void setMemory(const void* data, size_t length,
308  bool copyData = false);
313  void setMemoryOwned(const void* data, size_t length);
314 
318  SkData* copyToData() const;
319 
325  SkData* setData(SkData*);
326 
327  void skipToAlign4();
328  const void* getAtPos();
329 
330  size_t read(void* buffer, size_t size) override;
331  bool isAtEnd() const override;
332 
333  size_t peek(void* buffer, size_t size) const override;
334 
335  bool rewind() override;
336  SkMemoryStream* duplicate() const override;
337 
338  size_t getPosition() const override;
339  bool seek(size_t position) override;
340  bool move(long offset) override;
341  SkMemoryStream* fork() const override;
342 
343  size_t getLength() const override;
344 
345  const void* getMemoryBase() override;
346 
347 private:
348  sk_sp<SkData> fData;
349  size_t fOffset;
350 
351  typedef SkStreamMemory INHERITED;
352 };
353 
355 
356 class SK_API SkFILEWStream : public SkWStream {
357 public:
358  SkFILEWStream(const char path[]);
359  virtual ~SkFILEWStream();
360 
363  bool isValid() const { return fFILE != NULL; }
364 
365  bool write(const void* buffer, size_t size) override;
366  void flush() override;
367  void fsync();
368  size_t bytesWritten() const override;
369 
370 private:
371  FILE* fFILE;
372 
373  typedef SkWStream INHERITED;
374 };
375 
376 class SK_API SkMemoryWStream : public SkWStream {
377 public:
378  SkMemoryWStream(void* buffer, size_t size);
379  bool write(const void* buffer, size_t size) override;
380  size_t bytesWritten() const override { return fBytesWritten; }
381 
382 private:
383  char* fBuffer;
384  size_t fMaxLength;
385  size_t fBytesWritten;
386 
387  typedef SkWStream INHERITED;
388 };
389 
390 class SK_API SkDynamicMemoryWStream : public SkWStream {
391 public:
393  virtual ~SkDynamicMemoryWStream();
394 
395  bool write(const void* buffer, size_t size) override;
396  size_t bytesWritten() const override { return fBytesWritten; }
397  // random access write
398  // modifies stream and returns true if offset + size is less than or equal to getOffset()
399  bool write(const void* buffer, size_t offset, size_t size);
400  bool read(void* buffer, size_t offset, size_t size);
401  size_t getOffset() const { return fBytesWritten; }
402 
403  // copy what has been written to the stream into dst
404  void copyTo(void* dst) const;
405  void writeToStream(SkWStream* dst) const;
406 
411  SkData* copyToData() const;
412 
414  SkStreamAsset* detachAsStream();
415 
417  void reset();
418  void padToAlign4();
419 private:
420  struct Block;
421  Block* fHead;
422  Block* fTail;
423  size_t fBytesWritten;
424  mutable sk_sp<SkData> fCopy; // is invalidated if we write after it is created
425 
426  void invalidateCopy();
427 
428  // For access to the Block type.
429  friend class SkBlockMemoryStream;
430  friend class SkBlockMemoryRefCnt;
431 
432  typedef SkWStream INHERITED;
433 };
434 
435 
436 class SK_API SkDebugWStream : public SkWStream {
437 public:
438  SkDebugWStream() : fBytesWritten(0) {}
439 
440  // overrides
441  bool write(const void* buffer, size_t size) override;
442  void newline() override;
443  size_t bytesWritten() const override { return fBytesWritten; }
444 
445 private:
446  size_t fBytesWritten;
447  typedef SkWStream INHERITED;
448 };
449 
450 // for now
451 typedef SkFILEStream SkURLStream;
452 
453 #endif
SkStreamMemory * duplicate() const override=0
Duplicates this stream.
bool hasLength() const override
Returns true if this stream can report it's total length.
Definition: SkStream.h:169
A stream that wraps a C FILE* file stream.
Definition: SkStream.h:231
virtual bool rewind()
Rewinds to the beginning of the stream.
Definition: SkStream.h:102
virtual SkStreamRewindable * duplicate() const
Duplicates this stream.
Definition: SkStream.h:107
SkStreamAsset is a SkStreamSeekable for which getLength is required.
Definition: SkStream.h:164
Definition: SkStream.h:390
SkStreamMemory is a SkStreamAsset for which getMemoryBase is required.
Definition: SkStream.h:174
SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required...
Definition: SkStream.h:152
bool seek(size_t position) override=0
Seeks to an absolute position in the stream.
SkStreamSeekable * fork() const override=0
Duplicates this stream.
virtual SkStreamSeekable * fork() const
Duplicates this stream.
Definition: SkStream.h:130
virtual bool isAtEnd() const =0
Returns true when all the bytes in the stream have been read.
virtual bool seek(size_t)
Seeks to an absolute position in the stream.
Definition: SkStream.h:119
Definition: SkStream.h:356
unsigned U16CPU
Fast type for unsigned 16 bits.
Definition: SkTypes.h:263
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:363
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkStreamSeekable * duplicate() const override=0
Duplicates this stream.
virtual bool move(long)
Seeks to an relative offset in the stream.
Definition: SkStream.h:125
bool rewind() override=0
Rewinds to the beginning of the stream.
SkStreamAsset * duplicate() const override=0
Duplicates this stream.
size_t getPosition() const override=0
Returns the current position in the stream.
const void * getMemoryBase() override=0
Returns the starting address for the data.
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:252
Definition: SkStream.h:284
bool hasPosition() const override
Returns true if this stream can report it's current position.
Definition: SkStream.h:156
SkStreamMemory * fork() const override=0
Duplicates this stream.
Definition: SkStream.h:376
SkStreamRewindable is a SkStream for which rewind and duplicate are required.
Definition: SkStream.h:145
size_t skip(size_t size)
Skip size number of bytes.
Definition: SkStream.h:61
SkStreamAsset * fork() const override=0
Duplicates this stream.
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:38
virtual size_t peek(void *, size_t) const
Attempt to peek at size bytes.
Definition: SkStream.h:78
virtual const void * getMemoryBase()
Returns the starting address for the data.
Definition: SkStream.h:141
Definition: SkStream.h:182
virtual size_t getLength() const
Returns the total length of the stream.
Definition: SkStream.h:136
bool move(long offset) override=0
Seeks to an relative offset in the stream.
virtual size_t read(void *buffer, size_t size)=0
Reads or skips size number of bytes.
SkStreamRewindable * duplicate() const override=0
Duplicates this stream.
virtual bool hasLength() const
Returns true if this stream can report it's total length.
Definition: SkStream.h:134
virtual bool write(const void *buffer, size_t size)=0
Called to write bytes to a SkWStream.
virtual bool hasPosition() const
Returns true if this stream can report it's current position.
Definition: SkStream.h:111
size_t getLength() const override=0
Returns the total length of the stream.
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:251
Definition: SkStream.h:436
Light weight class for managing strings.
Definition: SkString.h:121
virtual size_t getPosition() const
Returns the current position in the stream.
Definition: SkStream.h:113