Skia
2DGraphicsLibrary
 All Classes Namespaces 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 #include <memory.h>
16 
17 class SkStream;
18 class SkStreamRewindable;
19 class SkStreamSeekable;
20 class SkStreamAsset;
21 class SkStreamMemory;
22 
40 class SK_API SkStream : public SkNoncopyable {
41 public:
42  virtual ~SkStream() {}
43 
47  static std::unique_ptr<SkStreamAsset> MakeFromFile(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 flush();
193 
194  virtual size_t bytesWritten() const = 0;
195 
196  // helpers
197 
198  bool write8(U8CPU value) {
199  uint8_t v = SkToU8(value);
200  return this->write(&v, 1);
201  }
202  bool write16(U16CPU value) {
203  uint16_t v = SkToU16(value);
204  return this->write(&v, 2);
205  }
206  bool write32(uint32_t v) {
207  return this->write(&v, 4);
208  }
209 
210  bool writeText(const char text[]) {
211  SkASSERT(text);
212  return this->write(text, strlen(text));
213  }
214 
215  bool newline() { return this->write("\n", strlen("\n")); }
216 
217  bool writeDecAsText(int32_t);
218  bool writeBigDecAsText(int64_t, int minDigits = 0);
219  bool writeHexAsText(uint32_t, int minDigits = 0);
220  bool writeScalarAsText(SkScalar);
221 
222  bool writeBool(bool v) { return this->write8(v); }
223  bool writeScalar(SkScalar);
224  bool writePackedUInt(size_t);
225 
226  bool writeStream(SkStream* input, size_t length);
227 
232  static int SizeOfPackedUInt(size_t value);
233 };
234 
235 class SK_API SkNullWStream : public SkWStream {
236 public:
237  SkNullWStream() : fBytesWritten(0) {}
238 
239  bool write(const void*, size_t n) override { fBytesWritten += n; return true; }
240  void flush() override {}
241  size_t bytesWritten() const override { return fBytesWritten; }
242 
243 private:
244  size_t fBytesWritten;
245 };
246 
248 
249 #include <stdio.h>
250 
252 class SK_API SkFILEStream : public SkStreamAsset {
253 public:
257  explicit SkFILEStream(const char path[] = nullptr);
258 
262  explicit SkFILEStream(FILE* file);
263 
264  ~SkFILEStream() override;
265 
267  bool isValid() const { return fFILE != nullptr; }
268 
270  void close();
271 
272  size_t read(void* buffer, size_t size) override;
273  bool isAtEnd() const override;
274 
275  bool rewind() override;
276  SkStreamAsset* duplicate() const override;
277 
278  size_t getPosition() const override;
279  bool seek(size_t position) override;
280  bool move(long offset) override;
281  SkStreamAsset* fork() const override;
282 
283  size_t getLength() const override;
284 
285 private:
286  explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset);
287  explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset, size_t originalOffset);
288 
289  std::shared_ptr<FILE> fFILE;
290  // My own council will I keep on sizes and offsets.
291  size_t fSize;
292  size_t fOffset;
293  size_t fOriginalOffset;
294 
295  typedef SkStreamAsset INHERITED;
296 };
297 
298 class SK_API SkMemoryStream : public SkStreamMemory {
299 public:
300  SkMemoryStream();
301 
303  SkMemoryStream(size_t length);
304 
306  SkMemoryStream(const void* data, size_t length, bool copyData = false);
307 
310 
315  virtual void setMemory(const void* data, size_t length,
316  bool copyData = false);
321  void setMemoryOwned(const void* data, size_t length);
322 
323  sk_sp<SkData> asData() const { return fData; }
324  void setData(sk_sp<SkData>);
325 
326  void skipToAlign4();
327  const void* getAtPos();
328 
329  size_t read(void* buffer, size_t size) override;
330  bool isAtEnd() const override;
331 
332  size_t peek(void* buffer, size_t size) const override;
333 
334  bool rewind() override;
335  SkMemoryStream* duplicate() const override;
336 
337  size_t getPosition() const override;
338  bool seek(size_t position) override;
339  bool move(long offset) override;
340  SkMemoryStream* fork() const override;
341 
342  size_t getLength() const override;
343 
344  const void* getMemoryBase() override;
345 
346 private:
347  sk_sp<SkData> fData;
348  size_t fOffset;
349 
350  typedef SkStreamMemory INHERITED;
351 };
352 
354 
355 class SK_API SkFILEWStream : public SkWStream {
356 public:
357  SkFILEWStream(const char path[]);
358  ~SkFILEWStream() override;
359 
362  bool isValid() const { return fFILE != NULL; }
363 
364  bool write(const void* buffer, size_t size) override;
365  void flush() override;
366  void fsync();
367  size_t bytesWritten() const override;
368 
369 private:
370  FILE* fFILE;
371 
372  typedef SkWStream INHERITED;
373 };
374 
375 class SK_API SkDynamicMemoryWStream : public SkWStream {
376 public:
378  ~SkDynamicMemoryWStream() override;
379 
380  bool write(const void* buffer, size_t size) override;
381  size_t bytesWritten() const override;
382 
383  bool read(void* buffer, size_t offset, size_t size);
384 
386  void copyTo(void* dst) const;
387  bool writeToStream(SkWStream* dst) const;
388 
390  void copyToAndReset(void* dst);
391 
393  bool writeToAndReset(SkWStream* dst);
394 
396  sk_sp<SkData> detachAsData();
397 
399  std::unique_ptr<SkStreamAsset> detachAsStream();
400 
402  void reset();
403  void padToAlign4();
404 private:
405  struct Block;
406  Block* fHead;
407  Block* fTail;
408  size_t fBytesWrittenBeforeTail;
409 
410 #ifdef SK_DEBUG
411  void validate() const;
412 #else
413  void validate() const {}
414 #endif
415 
416  // For access to the Block type.
417  friend class SkBlockMemoryStream;
418  friend class SkBlockMemoryRefCnt;
419 
420  typedef SkWStream INHERITED;
421 };
422 
423 #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
Definition: SkStream.h:235
A stream that wraps a C FILE* file stream.
Definition: SkStream.h:252
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
bool write(const void *, size_t n) override
Called to write bytes to a SkWStream.
Definition: SkStream.h:239
Definition: SkStream.h:375
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:355
unsigned U16CPU
Fast type for unsigned 16 bits.
Definition: SkTypes.h:203
bool isValid() const
Returns true if the current path could be opened.
Definition: SkStream.h:362
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:267
Definition: SkStream.h:298
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.
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:40
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:191
virtual size_t getPosition() const
Returns the current position in the stream.
Definition: SkStream.h:113