blob: 83eee4708b0fa8b259a2fd25d84c8884cd6e82d6 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkStream_DEFINED
9#define SkStream_DEFINED
10
reedfde05112016-03-11 13:02:28 -080011#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkRefCnt.h"
13#include "SkScalar.h"
14
bungeman@google.com6cab1a42013-05-29 13:43:31 +000015class SkStream;
16class SkStreamRewindable;
17class SkStreamSeekable;
18class SkStreamAsset;
19class SkStreamMemory;
20
reed@google.com3b345052013-05-07 15:59:16 +000021/**
22 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
23 * memory, or a file, or something else.
24 *
25 * NOTE:
26 *
27 * Classic "streams" APIs are sort of async, in that on a request for N
28 * bytes, they may return fewer than N bytes on a given call, in which case
29 * the caller can "try again" to get more bytes, eventually (modulo an error)
30 * receiving their total N bytes.
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000031 *
reed@google.com3b345052013-05-07 15:59:16 +000032 * Skia streams behave differently. They are effectively synchronous, and will
33 * always return all N bytes of the request if possible. If they return fewer
34 * (the read() call returns the number of bytes read) then that means there is
35 * no more data (at EOF or hit an error). The caller should *not* call again
36 * in hopes of fulfilling more of the request.
37 */
scroggoa1193e42015-01-21 12:09:53 -080038class SK_API SkStream : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
scroggoa1193e42015-01-21 12:09:53 -080040 virtual ~SkStream() {}
41
42 /**
bungemanf93d7112016-09-16 06:24:20 -070043 * Attempts to open the specified file as a stream, returns nullptr on failure.
reed@google.come1575aa2013-03-18 21:08:46 +000044 */
bungemanf93d7112016-09-16 06:24:20 -070045 static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +000046
bungeman@google.com6cab1a42013-05-29 13:43:31 +000047 /** Reads or skips size number of bytes.
48 * If buffer == NULL, skip size bytes, return how many were skipped.
49 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
50 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
51 * @param size the number of bytes to skip or copy
bungeman@google.com88682b72013-07-19 13:55:41 +000052 * @return the number of bytes actually read.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000053 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 virtual size_t read(void* buffer, size_t size) = 0;
55
bungeman@google.com6cab1a42013-05-29 13:43:31 +000056 /** Skip size number of bytes.
57 * @return the actual number bytes that could be skipped.
58 */
59 size_t skip(size_t size) {
bungeman@google.com04306922013-11-13 19:53:46 +000060 return this->read(NULL, size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000061 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062
scroggo028a4132015-04-02 13:19:51 -070063 /**
64 * Attempt to peek at size bytes.
scroggod61c3842015-12-07 11:37:13 -080065 * If this stream supports peeking, copy min(size, peekable bytes) into
66 * buffer, and return the number of bytes copied.
67 * If the stream does not support peeking, or cannot peek any bytes,
68 * return 0 and leave buffer unchanged.
scroggo028a4132015-04-02 13:19:51 -070069 * The stream is guaranteed to be in the same visible state after this
70 * call, regardless of success or failure.
scroggod61c3842015-12-07 11:37:13 -080071 * @param buffer Must not be NULL, and must be at least size bytes. Destination
72 * to copy bytes.
scroggo028a4132015-04-02 13:19:51 -070073 * @param size Number of bytes to copy.
scroggod61c3842015-12-07 11:37:13 -080074 * @return The number of bytes peeked/copied.
scroggo028a4132015-04-02 13:19:51 -070075 */
scroggod61c3842015-12-07 11:37:13 -080076 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
scroggo028a4132015-04-02 13:19:51 -070077
bungeman@google.com88682b72013-07-19 13:55:41 +000078 /** Returns true when all the bytes in the stream have been read.
79 * This may return true early (when there are no more bytes to be read)
80 * or late (after the first unsuccessful read).
bungeman@google.com6cab1a42013-05-29 13:43:31 +000081 */
bungeman@google.com04306922013-11-13 19:53:46 +000082 virtual bool isAtEnd() const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
84 int8_t readS8();
85 int16_t readS16();
86 int32_t readS32();
87
88 uint8_t readU8() { return (uint8_t)this->readS8(); }
89 uint16_t readU16() { return (uint16_t)this->readS16(); }
90 uint32_t readU32() { return (uint32_t)this->readS32(); }
91
92 bool readBool() { return this->readU8() != 0; }
93 SkScalar readScalar();
94 size_t readPackedUInt();
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000095
bungeman@google.com6cab1a42013-05-29 13:43:31 +000096//SkStreamRewindable
scroggo@google.com4d213ab2013-08-28 13:08:54 +000097 /** Rewinds to the beginning of the stream. Returns true if the stream is known
98 * to be at the beginning after this call returns.
99 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000100 virtual bool rewind() { return false; }
101
102 /** Duplicates this stream. If this cannot be done, returns NULL.
103 * The returned stream will be positioned at the beginning of its data.
104 */
105 virtual SkStreamRewindable* duplicate() const { return NULL; }
106
107//SkStreamSeekable
108 /** Returns true if this stream can report it's current position. */
109 virtual bool hasPosition() const { return false; }
110 /** Returns the current position in the stream. If this cannot be done, returns 0. */
111 virtual size_t getPosition() const { return 0; }
112
113 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
114 * If an attempt is made to seek past the end of the stream, the position will be set
115 * to the end of the stream.
116 */
djsollenc87dd2c2014-11-14 11:11:46 -0800117 virtual bool seek(size_t /*position*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000118
119 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
120 * If an attempt is made to move to a position outside the stream, the position will be set
121 * to the closest point within the stream (beginning or end).
122 */
djsollenc87dd2c2014-11-14 11:11:46 -0800123 virtual bool move(long /*offset*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000124
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 true if this stream can report it's total length. */
132 virtual bool hasLength() const { return false; }
133 /** Returns the total length of the stream. If this cannot be done, returns 0. */
bungeman@google.com04306922013-11-13 19:53:46 +0000134 virtual size_t getLength() const { return 0; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000135
136//SkStreamMemory
137 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
138 //TODO: replace with virtual const SkData* getData()
139 virtual const void* getMemoryBase() { return NULL; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140};
141
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000142/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
143class SK_API SkStreamRewindable : public SkStream {
144public:
mtklein36352bf2015-03-25 18:17:31 -0700145 bool rewind() override = 0;
146 SkStreamRewindable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000147};
148
149/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
150class SK_API SkStreamSeekable : public SkStreamRewindable {
151public:
mtklein36352bf2015-03-25 18:17:31 -0700152 SkStreamSeekable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000153
mtklein36352bf2015-03-25 18:17:31 -0700154 bool hasPosition() const override { return true; }
155 size_t getPosition() const override = 0;
156 bool seek(size_t position) override = 0;
157 bool move(long offset) override = 0;
158 SkStreamSeekable* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000159};
160
161/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
162class SK_API SkStreamAsset : public SkStreamSeekable {
163public:
mtklein36352bf2015-03-25 18:17:31 -0700164 SkStreamAsset* duplicate() const override = 0;
165 SkStreamAsset* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000166
mtklein36352bf2015-03-25 18:17:31 -0700167 bool hasLength() const override { return true; }
168 size_t getLength() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000169};
170
171/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
172class SK_API SkStreamMemory : public SkStreamAsset {
173public:
mtklein36352bf2015-03-25 18:17:31 -0700174 SkStreamMemory* duplicate() const override = 0;
175 SkStreamMemory* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 const void* getMemoryBase() override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000178};
179
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000180class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181public:
182 virtual ~SkWStream();
183
184 /** Called to write bytes to a SkWStream. Returns true on success
185 @param buffer the address of at least size bytes to be written to the stream
186 @param size The number of bytes in buffer to write to the stream
187 @return true on success
188 */
189 virtual bool write(const void* buffer, size_t size) = 0;
190 virtual void newline();
191 virtual void flush();
192
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000193 virtual size_t bytesWritten() const = 0;
194
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 // helpers
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196
Mike Reed9c457ad2016-12-15 14:11:37 -0500197 bool write8(U8CPU value) {
198 uint8_t v = SkToU8(value);
199 return this->write(&v, 1);
200 }
201 bool write16(U16CPU value) {
202 uint16_t v = SkToU16(value);
203 return this->write(&v, 2);
204 }
205 bool write32(uint32_t v) {
206 return this->write(&v, 4);
207 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
Mike Reed9c457ad2016-12-15 14:11:37 -0500209 bool writeText(const char text[]) {
halcanarycbc060a2016-04-11 19:41:48 -0700210 SkASSERT(text);
211 return this->write(text, strlen(text));
212 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 bool writeDecAsText(int32_t);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000214 bool writeBigDecAsText(int64_t, int minDigits = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 bool writeHexAsText(uint32_t, int minDigits = 0);
216 bool writeScalarAsText(SkScalar);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 bool writeBool(bool v) { return this->write8(v); }
219 bool writeScalar(SkScalar);
220 bool writePackedUInt(size_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000221
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000222 bool writeStream(SkStream* input, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000223
epoger@google.com8e3fb2d2013-03-20 15:56:03 +0000224 /**
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000225 * This returns the number of bytes in the stream required to store
226 * 'value'.
227 */
228 static int SizeOfPackedUInt(size_t value);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229};
230
231////////////////////////////////////////////////////////////////////////////////////////
232
233#include "SkString.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +0000234#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000236/** A stream that wraps a C FILE* file stream. */
237class SK_API SkFILEStream : public SkStreamAsset {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238public:
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000239 /** Initialize the stream by calling sk_fopen on the specified path.
240 * This internal stream will be closed in the destructor.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 */
242 explicit SkFILEStream(const char path[] = NULL);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000243
244 enum Ownership {
245 kCallerPasses_Ownership,
246 kCallerRetains_Ownership
247 };
248 /** Initialize the stream with an existing C file stream.
249 * While this stream exists, it assumes exclusive access to the C file stream.
250 * The C file stream will be closed in the destructor unless the caller specifies
251 * kCallerRetains_Ownership.
252 */
253 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 virtual ~SkFILEStream();
256
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000257 /** Returns true if the current path could be opened. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 bool isValid() const { return fFILE != NULL; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000259
260 /** Close the current file, and open a new file with the specified path.
261 * If path is NULL, just close the current file.
262 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 void setPath(const char path[]);
264
mtklein36352bf2015-03-25 18:17:31 -0700265 size_t read(void* buffer, size_t size) override;
266 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000267
mtklein36352bf2015-03-25 18:17:31 -0700268 bool rewind() override;
269 SkStreamAsset* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000270
mtklein36352bf2015-03-25 18:17:31 -0700271 size_t getPosition() const override;
272 bool seek(size_t position) override;
273 bool move(long offset) override;
274 SkStreamAsset* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000275
mtklein36352bf2015-03-25 18:17:31 -0700276 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000277
mtklein36352bf2015-03-25 18:17:31 -0700278 const void* getMemoryBase() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279
280private:
reedfde05112016-03-11 13:02:28 -0800281 FILE* fFILE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 SkString fName;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000283 Ownership fOwnership;
284 // fData is lazilly initialized when needed.
reedfde05112016-03-11 13:02:28 -0800285 mutable sk_sp<SkData> fData;
reed@google.com3b429982012-06-26 15:30:08 +0000286
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000287 typedef SkStreamAsset INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288};
289
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000290class SK_API SkMemoryStream : public SkStreamMemory {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291public:
292 SkMemoryStream();
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000293
294 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 SkMemoryStream(size_t length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000296
297 /** If copyData is true, the stream makes a private copy of the data. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 SkMemoryStream(const void* data, size_t length, bool copyData = false);
scroggo@google.come4904202013-01-09 22:02:58 +0000299
reedfde05112016-03-11 13:02:28 -0800300 /** Creates the stream to read from the specified data */
301 SkMemoryStream(sk_sp<SkData>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302
303 /** Resets the stream to the specified data and length,
304 just like the constructor.
305 if copyData is true, the stream makes a private copy of the data
306 */
307 virtual void setMemory(const void* data, size_t length,
308 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000309 /** Replace any memory buffer with the specified buffer. The caller
310 must have allocated data with sk_malloc or sk_realloc, since it
311 will be freed with sk_free.
312 */
313 void setMemoryOwned(const void* data, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000314
reed42943c82016-09-12 12:01:44 -0700315 sk_sp<SkData> asData() const { return fData; }
316 void setData(sk_sp<SkData>);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000317
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 void skipToAlign4();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 const void* getAtPos();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320
mtklein36352bf2015-03-25 18:17:31 -0700321 size_t read(void* buffer, size_t size) override;
322 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000323
scroggod61c3842015-12-07 11:37:13 -0800324 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -0700325
mtklein36352bf2015-03-25 18:17:31 -0700326 bool rewind() override;
327 SkMemoryStream* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000328
mtklein36352bf2015-03-25 18:17:31 -0700329 size_t getPosition() const override;
330 bool seek(size_t position) override;
331 bool move(long offset) override;
332 SkMemoryStream* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000333
mtklein36352bf2015-03-25 18:17:31 -0700334 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000335
mtklein36352bf2015-03-25 18:17:31 -0700336 const void* getMemoryBase() override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000337
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338private:
reedfde05112016-03-11 13:02:28 -0800339 sk_sp<SkData> fData;
340 size_t fOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000341
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000342 typedef SkStreamMemory INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343};
344
345/////////////////////////////////////////////////////////////////////////////////////////////
346
alokp@chromium.orgf7751ae2012-07-17 19:10:36 +0000347class SK_API SkFILEWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348public:
reed@google.com3b429982012-06-26 15:30:08 +0000349 SkFILEWStream(const char path[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 virtual ~SkFILEWStream();
351
352 /** Returns true if the current path could be opened.
353 */
354 bool isValid() const { return fFILE != NULL; }
355
mtklein36352bf2015-03-25 18:17:31 -0700356 bool write(const void* buffer, size_t size) override;
357 void flush() override;
caryclark7471fa42015-12-16 13:41:23 -0800358 void fsync();
mtklein36352bf2015-03-25 18:17:31 -0700359 size_t bytesWritten() const override;
reed@google.com3b429982012-06-26 15:30:08 +0000360
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361private:
halcanaryd76be9c2015-11-20 13:47:49 -0800362 FILE* fFILE;
reed@google.com3b429982012-06-26 15:30:08 +0000363
364 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365};
366
caryclark15e2a532016-03-16 06:51:55 -0700367class SK_API SkMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368public:
369 SkMemoryWStream(void* buffer, size_t size);
mtklein36352bf2015-03-25 18:17:31 -0700370 bool write(const void* buffer, size_t size) override;
371 size_t bytesWritten() const override { return fBytesWritten; }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000372
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373private:
374 char* fBuffer;
375 size_t fMaxLength;
376 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000377
378 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379};
380
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000381class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382public:
383 SkDynamicMemoryWStream();
384 virtual ~SkDynamicMemoryWStream();
reed@google.com8a85d0c2011-06-24 19:12:12 +0000385
mtklein36352bf2015-03-25 18:17:31 -0700386 bool write(const void* buffer, size_t size) override;
Mike Reed9c457ad2016-12-15 14:11:37 -0500387 size_t bytesWritten() const override;
Ben Wagner884300d2016-12-16 16:51:41 +0000388
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 bool read(void* buffer, size_t offset, size_t size);
Mike Reed9c457ad2016-12-15 14:11:37 -0500390
Ben Wagner884300d2016-12-16 16:51:41 +0000391 /** More efficient version of read(dst, 0, bytesWritten()). */
reed@google.com8a85d0c2011-06-24 19:12:12 +0000392 void copyTo(void* dst) const;
halcanary7af21502015-02-23 12:17:59 -0800393 void writeToStream(SkWStream* dst) const;
reed@google.com70442a62011-06-23 21:48:04 +0000394
Ben Wagner884300d2016-12-16 16:51:41 +0000395 /** Return the contents as SkData, and then reset the stream. */
reed42943c82016-09-12 12:01:44 -0700396 sk_sp<SkData> detachAsData();
reed@google.com70442a62011-06-23 21:48:04 +0000397
bungeman@google.com88682b72013-07-19 13:55:41 +0000398 /** Reset, returning a reader stream with the current content. */
bungeman@google.comc29f3d82013-07-19 22:32:11 +0000399 SkStreamAsset* detachAsStream();
bungeman@google.com88682b72013-07-19 13:55:41 +0000400
401 /** Reset the stream to its original, empty, state. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 void reset();
403 void padToAlign4();
404private:
405 struct Block;
406 Block* fHead;
407 Block* fTail;
Mike Reed9c457ad2016-12-15 14:11:37 -0500408 size_t fBytesWrittenBeforeTail;
409
410#ifdef SK_DEBUG
411 void validate() const;
412#else
413 void validate() const {}
414#endif
reed@google.com3b429982012-06-26 15:30:08 +0000415
bungeman@google.com88682b72013-07-19 13:55:41 +0000416 // For access to the Block type.
417 friend class SkBlockMemoryStream;
418 friend class SkBlockMemoryRefCnt;
419
reed@google.com3b429982012-06-26 15:30:08 +0000420 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421};
422
423
reed@google.coma718c5e2013-03-05 17:49:10 +0000424class SK_API SkDebugWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425public:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000426 SkDebugWStream() : fBytesWritten(0) {}
reed@google.com3b429982012-06-26 15:30:08 +0000427
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 // overrides
mtklein36352bf2015-03-25 18:17:31 -0700429 bool write(const void* buffer, size_t size) override;
430 void newline() override;
431 size_t bytesWritten() const override { return fBytesWritten; }
reed@google.com3b429982012-06-26 15:30:08 +0000432
433private:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000434 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000435 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436};
437
438// for now
439typedef SkFILEStream SkURLStream;
440
441#endif