blob: 4502416fd964e5c24134d6a721df393ee08c3efe [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
11#include "SkRefCnt.h"
12#include "SkScalar.h"
13
reed@google.com8d0b5772011-06-24 13:07:31 +000014class SkData;
reed@google.com70442a62011-06-23 21:48:04 +000015
bungeman@google.com6cab1a42013-05-29 13:43:31 +000016class SkStream;
17class SkStreamRewindable;
18class SkStreamSeekable;
19class SkStreamAsset;
20class SkStreamMemory;
21
reed@google.com3b345052013-05-07 15:59:16 +000022/**
23 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
24 * memory, or a file, or something else.
25 *
26 * NOTE:
27 *
28 * Classic "streams" APIs are sort of async, in that on a request for N
29 * bytes, they may return fewer than N bytes on a given call, in which case
30 * the caller can "try again" to get more bytes, eventually (modulo an error)
31 * receiving their total N bytes.
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000032 *
reed@google.com3b345052013-05-07 15:59:16 +000033 * Skia streams behave differently. They are effectively synchronous, and will
34 * always return all N bytes of the request if possible. If they return fewer
35 * (the read() call returns the number of bytes read) then that means there is
36 * no more data (at EOF or hit an error). The caller should *not* call again
37 * in hopes of fulfilling more of the request.
38 */
scroggoa1193e42015-01-21 12:09:53 -080039class SK_API SkStream : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040public:
scroggoa1193e42015-01-21 12:09:53 -080041 virtual ~SkStream() {}
42
43 /**
reed@google.come1575aa2013-03-18 21:08:46 +000044 * Attempts to open the specified file, and return a stream to it (using
scroggoa1193e42015-01-21 12:09:53 -080045 * mmap if available). On success, the caller is responsible for deleting.
46 * On failure, returns NULL.
reed@google.come1575aa2013-03-18 21:08:46 +000047 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +000048 static SkStreamAsset* NewFromFile(const char path[]);
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +000049
bungeman@google.com6cab1a42013-05-29 13:43:31 +000050 /** Reads or skips size number of bytes.
51 * If buffer == NULL, skip size bytes, return how many were skipped.
52 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
53 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
54 * @param size the number of bytes to skip or copy
bungeman@google.com88682b72013-07-19 13:55:41 +000055 * @return the number of bytes actually read.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000056 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 virtual size_t read(void* buffer, size_t size) = 0;
58
bungeman@google.com6cab1a42013-05-29 13:43:31 +000059 /** Skip size number of bytes.
60 * @return the actual number bytes that could be skipped.
61 */
62 size_t skip(size_t size) {
bungeman@google.com04306922013-11-13 19:53:46 +000063 return this->read(NULL, size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000064 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065
scroggo028a4132015-04-02 13:19:51 -070066 /**
67 * Attempt to peek at size bytes.
scroggod61c3842015-12-07 11:37:13 -080068 * If this stream supports peeking, copy min(size, peekable bytes) into
69 * buffer, and return the number of bytes copied.
70 * If the stream does not support peeking, or cannot peek any bytes,
71 * return 0 and leave buffer unchanged.
scroggo028a4132015-04-02 13:19:51 -070072 * The stream is guaranteed to be in the same visible state after this
73 * call, regardless of success or failure.
scroggod61c3842015-12-07 11:37:13 -080074 * @param buffer Must not be NULL, and must be at least size bytes. Destination
75 * to copy bytes.
scroggo028a4132015-04-02 13:19:51 -070076 * @param size Number of bytes to copy.
scroggod61c3842015-12-07 11:37:13 -080077 * @return The number of bytes peeked/copied.
scroggo028a4132015-04-02 13:19:51 -070078 */
scroggod61c3842015-12-07 11:37:13 -080079 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
scroggo028a4132015-04-02 13:19:51 -070080
bungeman@google.com88682b72013-07-19 13:55:41 +000081 /** Returns true when all the bytes in the stream have been read.
82 * This may return true early (when there are no more bytes to be read)
83 * or late (after the first unsuccessful read).
bungeman@google.com6cab1a42013-05-29 13:43:31 +000084 */
bungeman@google.com04306922013-11-13 19:53:46 +000085 virtual bool isAtEnd() const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
87 int8_t readS8();
88 int16_t readS16();
89 int32_t readS32();
90
91 uint8_t readU8() { return (uint8_t)this->readS8(); }
92 uint16_t readU16() { return (uint16_t)this->readS16(); }
93 uint32_t readU32() { return (uint32_t)this->readS32(); }
94
95 bool readBool() { return this->readU8() != 0; }
96 SkScalar readScalar();
97 size_t readPackedUInt();
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000098
bungeman@google.com6cab1a42013-05-29 13:43:31 +000099//SkStreamRewindable
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000100 /** Rewinds to the beginning of the stream. Returns true if the stream is known
101 * to be at the beginning after this call returns.
102 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000103 virtual bool rewind() { return false; }
104
105 /** Duplicates this stream. If this cannot be done, returns NULL.
106 * The returned stream will be positioned at the beginning of its data.
107 */
108 virtual SkStreamRewindable* duplicate() const { return NULL; }
109
110//SkStreamSeekable
111 /** Returns true if this stream can report it's current position. */
112 virtual bool hasPosition() const { return false; }
113 /** Returns the current position in the stream. If this cannot be done, returns 0. */
114 virtual size_t getPosition() const { return 0; }
115
116 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
117 * If an attempt is made to seek past the end of the stream, the position will be set
118 * to the end of the stream.
119 */
djsollenc87dd2c2014-11-14 11:11:46 -0800120 virtual bool seek(size_t /*position*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000121
122 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
123 * If an attempt is made to move to a position outside the stream, the position will be set
124 * to the closest point within the stream (beginning or end).
125 */
djsollenc87dd2c2014-11-14 11:11:46 -0800126 virtual bool move(long /*offset*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000127
128 /** Duplicates this stream. If this cannot be done, returns NULL.
129 * The returned stream will be positioned the same as this stream.
130 */
131 virtual SkStreamSeekable* fork() const { return NULL; }
132
133//SkStreamAsset
134 /** Returns true if this stream can report it's total length. */
135 virtual bool hasLength() const { return false; }
136 /** Returns the total length of the stream. If this cannot be done, returns 0. */
bungeman@google.com04306922013-11-13 19:53:46 +0000137 virtual size_t getLength() const { return 0; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000138
139//SkStreamMemory
140 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
141 //TODO: replace with virtual const SkData* getData()
142 virtual const void* getMemoryBase() { return NULL; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143};
144
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000145/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
146class SK_API SkStreamRewindable : public SkStream {
147public:
mtklein36352bf2015-03-25 18:17:31 -0700148 bool rewind() override = 0;
149 SkStreamRewindable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000150};
151
152/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
153class SK_API SkStreamSeekable : public SkStreamRewindable {
154public:
mtklein36352bf2015-03-25 18:17:31 -0700155 SkStreamSeekable* duplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000156
mtklein36352bf2015-03-25 18:17:31 -0700157 bool hasPosition() const override { return true; }
158 size_t getPosition() const override = 0;
159 bool seek(size_t position) override = 0;
160 bool move(long offset) override = 0;
161 SkStreamSeekable* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000162};
163
164/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
165class SK_API SkStreamAsset : public SkStreamSeekable {
166public:
mtklein36352bf2015-03-25 18:17:31 -0700167 SkStreamAsset* duplicate() const override = 0;
168 SkStreamAsset* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000169
mtklein36352bf2015-03-25 18:17:31 -0700170 bool hasLength() const override { return true; }
171 size_t getLength() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000172};
173
174/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
175class SK_API SkStreamMemory : public SkStreamAsset {
176public:
mtklein36352bf2015-03-25 18:17:31 -0700177 SkStreamMemory* duplicate() const override = 0;
178 SkStreamMemory* fork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000179
mtklein36352bf2015-03-25 18:17:31 -0700180 const void* getMemoryBase() override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000181};
182
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000183class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184public:
185 virtual ~SkWStream();
186
187 /** Called to write bytes to a SkWStream. Returns true on success
188 @param buffer the address of at least size bytes to be written to the stream
189 @param size The number of bytes in buffer to write to the stream
190 @return true on success
191 */
192 virtual bool write(const void* buffer, size_t size) = 0;
193 virtual void newline();
194 virtual void flush();
195
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000196 virtual size_t bytesWritten() const = 0;
197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 // helpers
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 bool write8(U8CPU);
201 bool write16(U16CPU);
202 bool write32(uint32_t);
203
204 bool writeText(const char text[]);
205 bool writeDecAsText(int32_t);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000206 bool writeBigDecAsText(int64_t, int minDigits = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 bool writeHexAsText(uint32_t, int minDigits = 0);
208 bool writeScalarAsText(SkScalar);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 bool writeBool(bool v) { return this->write8(v); }
211 bool writeScalar(SkScalar);
212 bool writePackedUInt(size_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000214 bool writeStream(SkStream* input, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000215
epoger@google.com8e3fb2d2013-03-20 15:56:03 +0000216 /**
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000217 * This returns the number of bytes in the stream required to store
218 * 'value'.
219 */
220 static int SizeOfPackedUInt(size_t value);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221};
222
223////////////////////////////////////////////////////////////////////////////////////////
224
225#include "SkString.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +0000226#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000228/** A stream that wraps a C FILE* file stream. */
229class SK_API SkFILEStream : public SkStreamAsset {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230public:
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000231 /** Initialize the stream by calling sk_fopen on the specified path.
232 * This internal stream will be closed in the destructor.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 */
234 explicit SkFILEStream(const char path[] = NULL);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000235
236 enum Ownership {
237 kCallerPasses_Ownership,
238 kCallerRetains_Ownership
239 };
240 /** Initialize the stream with an existing C file stream.
241 * While this stream exists, it assumes exclusive access to the C file stream.
242 * The C file stream will be closed in the destructor unless the caller specifies
243 * kCallerRetains_Ownership.
244 */
245 explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership);
246
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 virtual ~SkFILEStream();
248
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000249 /** Returns true if the current path could be opened. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 bool isValid() const { return fFILE != NULL; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000251
252 /** Close the current file, and open a new file with the specified path.
253 * If path is NULL, just close the current file.
254 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 void setPath(const char path[]);
256
mtklein36352bf2015-03-25 18:17:31 -0700257 size_t read(void* buffer, size_t size) override;
258 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000259
mtklein36352bf2015-03-25 18:17:31 -0700260 bool rewind() override;
261 SkStreamAsset* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000262
mtklein36352bf2015-03-25 18:17:31 -0700263 size_t getPosition() const override;
264 bool seek(size_t position) override;
265 bool move(long offset) override;
266 SkStreamAsset* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000267
mtklein36352bf2015-03-25 18:17:31 -0700268 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000269
mtklein36352bf2015-03-25 18:17:31 -0700270 const void* getMemoryBase() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271
272private:
halcanaryd76be9c2015-11-20 13:47:49 -0800273 FILE* fFILE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 SkString fName;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000275 Ownership fOwnership;
276 // fData is lazilly initialized when needed.
277 mutable SkAutoTUnref<SkData> fData;
reed@google.com3b429982012-06-26 15:30:08 +0000278
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000279 typedef SkStreamAsset INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280};
281
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000282class SK_API SkMemoryStream : public SkStreamMemory {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283public:
284 SkMemoryStream();
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000285
286 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 SkMemoryStream(size_t length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000288
289 /** If copyData is true, the stream makes a private copy of the data. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 SkMemoryStream(const void* data, size_t length, bool copyData = false);
scroggo@google.come4904202013-01-09 22:02:58 +0000291
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000292 /** Use the specified data as the memory for this stream.
293 * The stream will call ref() on the data (assuming it is not NULL).
scroggo@google.come4904202013-01-09 22:02:58 +0000294 */
295 SkMemoryStream(SkData*);
296
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 virtual ~SkMemoryStream();
298
299 /** Resets the stream to the specified data and length,
300 just like the constructor.
301 if copyData is true, the stream makes a private copy of the data
302 */
303 virtual void setMemory(const void* data, size_t length,
304 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000305 /** Replace any memory buffer with the specified buffer. The caller
306 must have allocated data with sk_malloc or sk_realloc, since it
307 will be freed with sk_free.
308 */
309 void setMemoryOwned(const void* data, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000310
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000311 /** Return the stream's data in a SkData.
312 * The caller must call unref() when it is finished using the data.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000313 */
314 SkData* copyToData() const;
315
316 /**
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000317 * Use the specified data as the memory for this stream.
318 * The stream will call ref() on the data (assuming it is not NULL).
319 * The function returns the data parameter as a convenience.
reed@google.com8a85d0c2011-06-24 19:12:12 +0000320 */
321 SkData* setData(SkData*);
322
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 void skipToAlign4();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 const void* getAtPos();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325
mtklein36352bf2015-03-25 18:17:31 -0700326 size_t read(void* buffer, size_t size) override;
327 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000328
scroggod61c3842015-12-07 11:37:13 -0800329 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -0700330
mtklein36352bf2015-03-25 18:17:31 -0700331 bool rewind() override;
332 SkMemoryStream* duplicate() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000333
mtklein36352bf2015-03-25 18:17:31 -0700334 size_t getPosition() const override;
335 bool seek(size_t position) override;
336 bool move(long offset) override;
337 SkMemoryStream* fork() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000338
mtklein36352bf2015-03-25 18:17:31 -0700339 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000340
mtklein36352bf2015-03-25 18:17:31 -0700341 const void* getMemoryBase() override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343private:
reed@google.com8a85d0c2011-06-24 19:12:12 +0000344 SkData* fData;
345 size_t fOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000346
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000347 typedef SkStreamMemory INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348};
349
350/////////////////////////////////////////////////////////////////////////////////////////////
351
alokp@chromium.orgf7751ae2012-07-17 19:10:36 +0000352class SK_API SkFILEWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353public:
reed@google.com3b429982012-06-26 15:30:08 +0000354 SkFILEWStream(const char path[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 virtual ~SkFILEWStream();
356
357 /** Returns true if the current path could be opened.
358 */
359 bool isValid() const { return fFILE != NULL; }
360
mtklein36352bf2015-03-25 18:17:31 -0700361 bool write(const void* buffer, size_t size) override;
362 void flush() override;
caryclark7471fa42015-12-16 13:41:23 -0800363 void fsync();
mtklein36352bf2015-03-25 18:17:31 -0700364 size_t bytesWritten() const override;
reed@google.com3b429982012-06-26 15:30:08 +0000365
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366private:
halcanaryd76be9c2015-11-20 13:47:49 -0800367 FILE* fFILE;
reed@google.com3b429982012-06-26 15:30:08 +0000368
369 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370};
371
372class SkMemoryWStream : public SkWStream {
373public:
374 SkMemoryWStream(void* buffer, size_t size);
mtklein36352bf2015-03-25 18:17:31 -0700375 bool write(const void* buffer, size_t size) override;
376 size_t bytesWritten() const override { return fBytesWritten; }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000377
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378private:
379 char* fBuffer;
380 size_t fMaxLength;
381 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000382
383 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384};
385
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000386class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387public:
388 SkDynamicMemoryWStream();
389 virtual ~SkDynamicMemoryWStream();
reed@google.com8a85d0c2011-06-24 19:12:12 +0000390
mtklein36352bf2015-03-25 18:17:31 -0700391 bool write(const void* buffer, size_t size) override;
392 size_t bytesWritten() const override { return fBytesWritten; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 // random access write
394 // modifies stream and returns true if offset + size is less than or equal to getOffset()
395 bool write(const void* buffer, size_t offset, size_t size);
396 bool read(void* buffer, size_t offset, size_t size);
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000397 size_t getOffset() const { return fBytesWritten; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398
399 // copy what has been written to the stream into dst
reed@google.com8a85d0c2011-06-24 19:12:12 +0000400 void copyTo(void* dst) const;
halcanary7af21502015-02-23 12:17:59 -0800401 void writeToStream(SkWStream* dst) const;
reed@google.com70442a62011-06-23 21:48:04 +0000402
403 /**
reed@google.com8a85d0c2011-06-24 19:12:12 +0000404 * Return a copy of the data written so far. This call is responsible for
405 * calling unref() when they are finished with the data.
reed@google.com70442a62011-06-23 21:48:04 +0000406 */
reed@google.com8d0b5772011-06-24 13:07:31 +0000407 SkData* copyToData() const;
reed@google.com70442a62011-06-23 21:48:04 +0000408
bungeman@google.com88682b72013-07-19 13:55:41 +0000409 /** Reset, returning a reader stream with the current content. */
bungeman@google.comc29f3d82013-07-19 22:32:11 +0000410 SkStreamAsset* detachAsStream();
bungeman@google.com88682b72013-07-19 13:55:41 +0000411
412 /** Reset the stream to its original, empty, state. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413 void reset();
414 void padToAlign4();
415private:
416 struct Block;
417 Block* fHead;
418 Block* fTail;
419 size_t fBytesWritten;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000420 mutable SkData* fCopy; // is invalidated if we write after it is created
421
422 void invalidateCopy();
reed@google.com3b429982012-06-26 15:30:08 +0000423
bungeman@google.com88682b72013-07-19 13:55:41 +0000424 // For access to the Block type.
425 friend class SkBlockMemoryStream;
426 friend class SkBlockMemoryRefCnt;
427
reed@google.com3b429982012-06-26 15:30:08 +0000428 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429};
430
431
reed@google.coma718c5e2013-03-05 17:49:10 +0000432class SK_API SkDebugWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433public:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000434 SkDebugWStream() : fBytesWritten(0) {}
reed@google.com3b429982012-06-26 15:30:08 +0000435
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 // overrides
mtklein36352bf2015-03-25 18:17:31 -0700437 bool write(const void* buffer, size_t size) override;
438 void newline() override;
439 size_t bytesWritten() const override { return fBytesWritten; }
reed@google.com3b429982012-06-26 15:30:08 +0000440
441private:
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000442 size_t fBytesWritten;
reed@google.com3b429982012-06-26 15:30:08 +0000443 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444};
445
446// for now
447typedef SkFILEStream SkURLStream;
448
449#endif