blob: d73c8a097e90972c94f5acf5b76a55bca87d7c2f [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
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "../private/SkTo.h"
reedfde05112016-03-11 13:02:28 -080012#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkRefCnt.h"
14#include "SkScalar.h"
15
Ben Wagner4d1955c2017-03-10 13:08:15 -050016#include <memory.h>
17
bungeman@google.com6cab1a42013-05-29 13:43:31 +000018class SkStream;
19class SkStreamRewindable;
20class SkStreamSeekable;
21class SkStreamAsset;
22class SkStreamMemory;
23
reed@google.com3b345052013-05-07 15:59:16 +000024/**
25 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
26 * memory, or a file, or something else.
27 *
28 * NOTE:
29 *
30 * Classic "streams" APIs are sort of async, in that on a request for N
31 * bytes, they may return fewer than N bytes on a given call, in which case
32 * the caller can "try again" to get more bytes, eventually (modulo an error)
33 * receiving their total N bytes.
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000034 *
reed@google.com3b345052013-05-07 15:59:16 +000035 * Skia streams behave differently. They are effectively synchronous, and will
36 * always return all N bytes of the request if possible. If they return fewer
37 * (the read() call returns the number of bytes read) then that means there is
38 * no more data (at EOF or hit an error). The caller should *not* call again
39 * in hopes of fulfilling more of the request.
40 */
scroggoa1193e42015-01-21 12:09:53 -080041class SK_API SkStream : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042public:
scroggoa1193e42015-01-21 12:09:53 -080043 virtual ~SkStream() {}
44
45 /**
bungemanf93d7112016-09-16 06:24:20 -070046 * Attempts to open the specified file as a stream, returns nullptr on failure.
reed@google.come1575aa2013-03-18 21:08:46 +000047 */
bungemanf93d7112016-09-16 06:24:20 -070048 static std::unique_ptr<SkStreamAsset> MakeFromFile(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) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040063 return this->read(nullptr, 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
Ben Wagner255ab8d2016-10-07 15:50:53 -040087 bool SK_WARN_UNUSED_RESULT readS8(int8_t*);
88 bool SK_WARN_UNUSED_RESULT readS16(int16_t*);
89 bool SK_WARN_UNUSED_RESULT readS32(int32_t*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
Ben Wagner255ab8d2016-10-07 15:50:53 -040091 bool SK_WARN_UNUSED_RESULT readU8(uint8_t* i) { return this->readS8((int8_t*)i); }
92 bool SK_WARN_UNUSED_RESULT readU16(uint16_t* i) { return this->readS16((int16_t*)i); }
93 bool SK_WARN_UNUSED_RESULT readU32(uint32_t* i) { return this->readS32((int32_t*)i); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
Ben Wagner255ab8d2016-10-07 15:50:53 -040095 bool SK_WARN_UNUSED_RESULT readBool(bool* b) {
96 uint8_t i;
97 if (!this->readU8(&i)) { return false; }
98 *b = (i != 0);
99 return true;
100 }
101 bool SK_WARN_UNUSED_RESULT readScalar(SkScalar*);
102 bool SK_WARN_UNUSED_RESULT readPackedUInt(size_t*);
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000103
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000104//SkStreamRewindable
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000105 /** Rewinds to the beginning of the stream. Returns true if the stream is known
106 * to be at the beginning after this call returns.
107 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000108 virtual bool rewind() { return false; }
109
Mike Reed98c5d922017-09-15 21:39:47 -0400110 /** Duplicates this stream. If this cannot be done, returns NULL.
111 * The returned stream will be positioned at the beginning of its data.
112 */
113 std::unique_ptr<SkStream> duplicate() const {
114 return std::unique_ptr<SkStream>(this->onDuplicate());
115 }
116 /** Duplicates this stream. If this cannot be done, returns NULL.
117 * The returned stream will be positioned the same as this stream.
118 */
119 std::unique_ptr<SkStream> fork() const {
120 return std::unique_ptr<SkStream>(this->onFork());
121 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000122
123//SkStreamSeekable
124 /** Returns true if this stream can report it's current position. */
125 virtual bool hasPosition() const { return false; }
126 /** Returns the current position in the stream. If this cannot be done, returns 0. */
127 virtual size_t getPosition() const { return 0; }
128
129 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
130 * If an attempt is made to seek past the end of the stream, the position will be set
131 * to the end of the stream.
132 */
djsollenc87dd2c2014-11-14 11:11:46 -0800133 virtual bool seek(size_t /*position*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000134
135 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
136 * If an attempt is made to move to a position outside the stream, the position will be set
137 * to the closest point within the stream (beginning or end).
138 */
djsollenc87dd2c2014-11-14 11:11:46 -0800139 virtual bool move(long /*offset*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000140
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000141//SkStreamAsset
142 /** Returns true if this stream can report it's total length. */
143 virtual bool hasLength() const { return false; }
144 /** Returns the total length of the stream. If this cannot be done, returns 0. */
bungeman@google.com04306922013-11-13 19:53:46 +0000145 virtual size_t getLength() const { return 0; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000146
147//SkStreamMemory
148 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
149 //TODO: replace with virtual const SkData* getData()
Ben Wagnera93a14a2017-08-28 10:34:05 -0400150 virtual const void* getMemoryBase() { return nullptr; }
Mike Reed98c5d922017-09-15 21:39:47 -0400151
152private:
Mike Reed98c5d922017-09-15 21:39:47 -0400153 virtual SkStream* onDuplicate() const { return nullptr; }
154 virtual SkStream* onFork() const { return nullptr; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155};
156
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000157/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
158class SK_API SkStreamRewindable : public SkStream {
159public:
mtklein36352bf2015-03-25 18:17:31 -0700160 bool rewind() override = 0;
Mike Reed98c5d922017-09-15 21:39:47 -0400161 std::unique_ptr<SkStreamRewindable> duplicate() const {
162 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
163 }
164private:
165 SkStreamRewindable* onDuplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000166};
167
168/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
169class SK_API SkStreamSeekable : public SkStreamRewindable {
170public:
Mike Reed98c5d922017-09-15 21:39:47 -0400171 std::unique_ptr<SkStreamSeekable> duplicate() const {
172 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
173 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000174
mtklein36352bf2015-03-25 18:17:31 -0700175 bool hasPosition() const override { return true; }
176 size_t getPosition() const override = 0;
177 bool seek(size_t position) override = 0;
178 bool move(long offset) override = 0;
Mike Reed0ca21462017-09-20 15:37:14 -0400179
Mike Reed98c5d922017-09-15 21:39:47 -0400180 std::unique_ptr<SkStreamSeekable> fork() const {
181 return std::unique_ptr<SkStreamSeekable>(this->onFork());
182 }
183private:
184 SkStreamSeekable* onDuplicate() const override = 0;
185 SkStreamSeekable* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000186};
187
188/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
189class SK_API SkStreamAsset : public SkStreamSeekable {
190public:
Mike Reed7031b242017-09-15 21:03:54 +0000191 bool hasLength() const override { return true; }
192 size_t getLength() const override = 0;
Mike Reed98c5d922017-09-15 21:39:47 -0400193
Mike Reed98c5d922017-09-15 21:39:47 -0400194 std::unique_ptr<SkStreamAsset> duplicate() const {
195 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
196 }
197 std::unique_ptr<SkStreamAsset> fork() const {
198 return std::unique_ptr<SkStreamAsset>(this->onFork());
199 }
200private:
201 SkStreamAsset* onDuplicate() const override = 0;
202 SkStreamAsset* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000203};
204
205/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
206class SK_API SkStreamMemory : public SkStreamAsset {
207public:
Mike Reed98c5d922017-09-15 21:39:47 -0400208 const void* getMemoryBase() override = 0;
209
Mike Reed98c5d922017-09-15 21:39:47 -0400210 std::unique_ptr<SkStreamMemory> duplicate() const {
211 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
212 }
213 std::unique_ptr<SkStreamMemory> fork() const {
214 return std::unique_ptr<SkStreamMemory>(this->onFork());
215 }
216private:
217 SkStreamMemory* onDuplicate() const override = 0;
218 SkStreamMemory* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000219};
220
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000221class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222public:
223 virtual ~SkWStream();
224
225 /** Called to write bytes to a SkWStream. Returns true on success
226 @param buffer the address of at least size bytes to be written to the stream
227 @param size The number of bytes in buffer to write to the stream
228 @return true on success
229 */
230 virtual bool write(const void* buffer, size_t size) = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 virtual void flush();
232
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000233 virtual size_t bytesWritten() const = 0;
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 // helpers
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000236
Mike Reed9c457ad2016-12-15 14:11:37 -0500237 bool write8(U8CPU value) {
238 uint8_t v = SkToU8(value);
239 return this->write(&v, 1);
240 }
241 bool write16(U16CPU value) {
242 uint16_t v = SkToU16(value);
243 return this->write(&v, 2);
244 }
245 bool write32(uint32_t v) {
246 return this->write(&v, 4);
247 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
Mike Reed9c457ad2016-12-15 14:11:37 -0500249 bool writeText(const char text[]) {
halcanarycbc060a2016-04-11 19:41:48 -0700250 SkASSERT(text);
251 return this->write(text, strlen(text));
252 }
Mike Reedc3063e52017-01-07 16:16:02 -0500253
254 bool newline() { return this->write("\n", strlen("\n")); }
255
Ben Wagner255ab8d2016-10-07 15:50:53 -0400256 bool writeDecAsText(int32_t);
257 bool writeBigDecAsText(int64_t, int minDigits = 0);
258 bool writeHexAsText(uint32_t, int minDigits = 0);
259 bool writeScalarAsText(SkScalar);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260
Ben Wagner255ab8d2016-10-07 15:50:53 -0400261 bool writeBool(bool v) { return this->write8(v); }
262 bool writeScalar(SkScalar);
263 bool writePackedUInt(size_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264
Ben Wagner255ab8d2016-10-07 15:50:53 -0400265 bool writeStream(SkStream* input, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000266
epoger@google.com8e3fb2d2013-03-20 15:56:03 +0000267 /**
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000268 * This returns the number of bytes in the stream required to store
269 * 'value'.
270 */
271 static int SizeOfPackedUInt(size_t value);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272};
273
Mike Reed2a65cc02017-03-22 10:01:53 -0400274class SK_API SkNullWStream : public SkWStream {
275public:
276 SkNullWStream() : fBytesWritten(0) {}
277
278 bool write(const void*, size_t n) override { fBytesWritten += n; return true; }
279 void flush() override {}
280 size_t bytesWritten() const override { return fBytesWritten; }
281
282private:
283 size_t fBytesWritten;
284};
285
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286////////////////////////////////////////////////////////////////////////////////////////
287
bungeman@google.comfab44db2013-10-11 18:50:45 +0000288#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000290/** A stream that wraps a C FILE* file stream. */
291class SK_API SkFILEStream : public SkStreamAsset {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292public:
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000293 /** Initialize the stream by calling sk_fopen on the specified path.
294 * This internal stream will be closed in the destructor.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500296 explicit SkFILEStream(const char path[] = nullptr);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000297
Leon Scroggins III8a8a1442018-05-08 09:35:32 -0400298 /** Initialize the stream with an existing C FILE stream.
299 * The current position of the C FILE stream will be considered the
300 * beginning of the SkFILEStream.
301 * The C FILE stream will be closed in the destructor.
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000302 */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500303 explicit SkFILEStream(FILE* file);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000304
Brian Salomond3b65972017-03-22 12:05:03 -0400305 ~SkFILEStream() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306
Mike Reed847068c2017-07-26 11:35:53 -0400307 static std::unique_ptr<SkFILEStream> Make(const char path[]) {
Hal Canary4b972d12017-08-26 19:31:12 -0400308 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path));
309 return stream->isValid() ? std::move(stream) : nullptr;
Mike Reed847068c2017-07-26 11:35:53 -0400310 }
311
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000312 /** Returns true if the current path could be opened. */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500313 bool isValid() const { return fFILE != nullptr; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000314
Ben Wagner4d1955c2017-03-10 13:08:15 -0500315 /** Close this SkFILEStream. */
316 void close();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317
mtklein36352bf2015-03-25 18:17:31 -0700318 size_t read(void* buffer, size_t size) override;
319 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000320
mtklein36352bf2015-03-25 18:17:31 -0700321 bool rewind() override;
Mike Reed98c5d922017-09-15 21:39:47 -0400322 std::unique_ptr<SkStreamAsset> duplicate() const {
323 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
324 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000325
mtklein36352bf2015-03-25 18:17:31 -0700326 size_t getPosition() const override;
327 bool seek(size_t position) override;
328 bool move(long offset) override;
Mike Reed0ca21462017-09-20 15:37:14 -0400329
Mike Reed98c5d922017-09-15 21:39:47 -0400330 std::unique_ptr<SkStreamAsset> fork() const {
331 return std::unique_ptr<SkStreamAsset>(this->onFork());
332 }
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336private:
Ben Wagner4d1955c2017-03-10 13:08:15 -0500337 explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset);
338 explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset, size_t originalOffset);
339
Mike Reed98c5d922017-09-15 21:39:47 -0400340 SkStreamAsset* onDuplicate() const override;
341 SkStreamAsset* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -0400342
Ben Wagner4d1955c2017-03-10 13:08:15 -0500343 std::shared_ptr<FILE> fFILE;
344 // My own council will I keep on sizes and offsets.
345 size_t fSize;
346 size_t fOffset;
347 size_t fOriginalOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000348
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000349 typedef SkStreamAsset INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350};
351
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000352class SK_API SkMemoryStream : public SkStreamMemory {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353public:
354 SkMemoryStream();
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000355
356 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 SkMemoryStream(size_t length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000358
359 /** If copyData is true, the stream makes a private copy of the data. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 SkMemoryStream(const void* data, size_t length, bool copyData = false);
scroggo@google.come4904202013-01-09 22:02:58 +0000361
reedfde05112016-03-11 13:02:28 -0800362 /** Creates the stream to read from the specified data */
363 SkMemoryStream(sk_sp<SkData>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364
Mike Reed847068c2017-07-26 11:35:53 -0400365 /** Returns a stream with a copy of the input data. */
366 static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
367
368 /** Returns a stream with a bare pointer reference to the input data. */
369 static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
370
371 /** Returns a stream with a shared reference to the input data. */
372 static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
373
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 /** Resets the stream to the specified data and length,
375 just like the constructor.
376 if copyData is true, the stream makes a private copy of the data
377 */
378 virtual void setMemory(const void* data, size_t length,
379 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000380 /** Replace any memory buffer with the specified buffer. The caller
381 must have allocated data with sk_malloc or sk_realloc, since it
382 will be freed with sk_free.
383 */
384 void setMemoryOwned(const void* data, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000385
reed42943c82016-09-12 12:01:44 -0700386 sk_sp<SkData> asData() const { return fData; }
387 void setData(sk_sp<SkData>);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000388
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 void skipToAlign4();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 const void* getAtPos();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000391
mtklein36352bf2015-03-25 18:17:31 -0700392 size_t read(void* buffer, size_t size) override;
393 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000394
scroggod61c3842015-12-07 11:37:13 -0800395 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -0700396
mtklein36352bf2015-03-25 18:17:31 -0700397 bool rewind() override;
Mike Reed0ca21462017-09-20 15:37:14 -0400398
Mike Reed98c5d922017-09-15 21:39:47 -0400399 std::unique_ptr<SkMemoryStream> duplicate() const {
400 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
401 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000402
mtklein36352bf2015-03-25 18:17:31 -0700403 size_t getPosition() const override;
404 bool seek(size_t position) override;
405 bool move(long offset) override;
Mike Reed0ca21462017-09-20 15:37:14 -0400406
Mike Reed98c5d922017-09-15 21:39:47 -0400407 std::unique_ptr<SkMemoryStream> fork() const {
408 return std::unique_ptr<SkMemoryStream>(this->onFork());
409 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000410
mtklein36352bf2015-03-25 18:17:31 -0700411 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000412
mtklein36352bf2015-03-25 18:17:31 -0700413 const void* getMemoryBase() override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000414
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415private:
Mike Reed98c5d922017-09-15 21:39:47 -0400416 SkMemoryStream* onDuplicate() const override;
417 SkMemoryStream* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -0400418
reedfde05112016-03-11 13:02:28 -0800419 sk_sp<SkData> fData;
420 size_t fOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000421
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000422 typedef SkStreamMemory INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423};
424
425/////////////////////////////////////////////////////////////////////////////////////////////
426
alokp@chromium.orgf7751ae2012-07-17 19:10:36 +0000427class SK_API SkFILEWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428public:
reed@google.com3b429982012-06-26 15:30:08 +0000429 SkFILEWStream(const char path[]);
Brian Salomond3b65972017-03-22 12:05:03 -0400430 ~SkFILEWStream() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431
432 /** Returns true if the current path could be opened.
433 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400434 bool isValid() const { return fFILE != nullptr; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435
mtklein36352bf2015-03-25 18:17:31 -0700436 bool write(const void* buffer, size_t size) override;
437 void flush() override;
caryclark7471fa42015-12-16 13:41:23 -0800438 void fsync();
mtklein36352bf2015-03-25 18:17:31 -0700439 size_t bytesWritten() const override;
reed@google.com3b429982012-06-26 15:30:08 +0000440
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441private:
halcanaryd76be9c2015-11-20 13:47:49 -0800442 FILE* fFILE;
reed@google.com3b429982012-06-26 15:30:08 +0000443
444 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445};
446
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000447class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448public:
449 SkDynamicMemoryWStream();
Brian Salomond3b65972017-03-22 12:05:03 -0400450 ~SkDynamicMemoryWStream() override;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000451
mtklein36352bf2015-03-25 18:17:31 -0700452 bool write(const void* buffer, size_t size) override;
Mike Reed9c457ad2016-12-15 14:11:37 -0500453 size_t bytesWritten() const override;
Ben Wagner884300d2016-12-16 16:51:41 +0000454
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 bool read(void* buffer, size_t offset, size_t size);
Mike Reed9c457ad2016-12-15 14:11:37 -0500456
Ben Wagner884300d2016-12-16 16:51:41 +0000457 /** More efficient version of read(dst, 0, bytesWritten()). */
reed@google.com8a85d0c2011-06-24 19:12:12 +0000458 void copyTo(void* dst) const;
Hal Canary6fee59b2017-05-23 11:47:05 -0400459 bool writeToStream(SkWStream* dst) const;
reed@google.com70442a62011-06-23 21:48:04 +0000460
Hal Canarydabe8ac2017-03-14 15:52:12 -0400461 /** Equivalent to copyTo() followed by reset(), but may save memory use. */
462 void copyToAndReset(void* dst);
463
Hal Canary6fee59b2017-05-23 11:47:05 -0400464 /** Equivalent to writeToStream() followed by reset(), but may save memory use. */
465 bool writeToAndReset(SkWStream* dst);
466
Ben Wagner884300d2016-12-16 16:51:41 +0000467 /** Return the contents as SkData, and then reset the stream. */
reed42943c82016-09-12 12:01:44 -0700468 sk_sp<SkData> detachAsData();
reed@google.com70442a62011-06-23 21:48:04 +0000469
bungeman@google.com88682b72013-07-19 13:55:41 +0000470 /** Reset, returning a reader stream with the current content. */
Hal Canary0b833192017-03-14 14:35:02 -0400471 std::unique_ptr<SkStreamAsset> detachAsStream();
bungeman@google.com88682b72013-07-19 13:55:41 +0000472
473 /** Reset the stream to its original, empty, state. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 void reset();
475 void padToAlign4();
476private:
477 struct Block;
478 Block* fHead;
479 Block* fTail;
Mike Reed9c457ad2016-12-15 14:11:37 -0500480 size_t fBytesWrittenBeforeTail;
481
482#ifdef SK_DEBUG
483 void validate() const;
484#else
485 void validate() const {}
486#endif
reed@google.com3b429982012-06-26 15:30:08 +0000487
bungeman@google.com88682b72013-07-19 13:55:41 +0000488 // For access to the Block type.
489 friend class SkBlockMemoryStream;
490 friend class SkBlockMemoryRefCnt;
491
reed@google.com3b429982012-06-26 15:30:08 +0000492 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493};
494
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495#endif