blob: 2d9f9db120c5d6bd2d708ec47ec0dc266219d750 [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
Ben Wagner4d1955c2017-03-10 13:08:15 -050015#include <memory.h>
16
bungeman@google.com6cab1a42013-05-29 13:43:31 +000017class SkStream;
18class SkStreamRewindable;
19class SkStreamSeekable;
20class SkStreamAsset;
21class SkStreamMemory;
22
reed@google.com3b345052013-05-07 15:59:16 +000023/**
24 * SkStream -- abstraction for a source of bytes. Subclasses can be backed by
25 * memory, or a file, or something else.
26 *
27 * NOTE:
28 *
29 * Classic "streams" APIs are sort of async, in that on a request for N
30 * bytes, they may return fewer than N bytes on a given call, in which case
31 * the caller can "try again" to get more bytes, eventually (modulo an error)
32 * receiving their total N bytes.
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000033 *
reed@google.com3b345052013-05-07 15:59:16 +000034 * Skia streams behave differently. They are effectively synchronous, and will
35 * always return all N bytes of the request if possible. If they return fewer
36 * (the read() call returns the number of bytes read) then that means there is
37 * no more data (at EOF or hit an error). The caller should *not* call again
38 * in hopes of fulfilling more of the request.
39 */
scroggoa1193e42015-01-21 12:09:53 -080040class SK_API SkStream : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041public:
scroggoa1193e42015-01-21 12:09:53 -080042 virtual ~SkStream() {}
43
44 /**
bungemanf93d7112016-09-16 06:24:20 -070045 * Attempts to open the specified file as a stream, returns nullptr on failure.
reed@google.come1575aa2013-03-18 21:08:46 +000046 */
bungemanf93d7112016-09-16 06:24:20 -070047 static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +000048
bungeman@google.com6cab1a42013-05-29 13:43:31 +000049 /** Reads or skips size number of bytes.
50 * If buffer == NULL, skip size bytes, return how many were skipped.
51 * If buffer != NULL, copy size bytes into buffer, return how many were copied.
52 * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
53 * @param size the number of bytes to skip or copy
bungeman@google.com88682b72013-07-19 13:55:41 +000054 * @return the number of bytes actually read.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000055 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual size_t read(void* buffer, size_t size) = 0;
57
bungeman@google.com6cab1a42013-05-29 13:43:31 +000058 /** Skip size number of bytes.
59 * @return the actual number bytes that could be skipped.
60 */
61 size_t skip(size_t size) {
Ben Wagnera93a14a2017-08-28 10:34:05 -040062 return this->read(nullptr, size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000063 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
scroggo028a4132015-04-02 13:19:51 -070065 /**
66 * Attempt to peek at size bytes.
scroggod61c3842015-12-07 11:37:13 -080067 * If this stream supports peeking, copy min(size, peekable bytes) into
68 * buffer, and return the number of bytes copied.
69 * If the stream does not support peeking, or cannot peek any bytes,
70 * return 0 and leave buffer unchanged.
scroggo028a4132015-04-02 13:19:51 -070071 * The stream is guaranteed to be in the same visible state after this
72 * call, regardless of success or failure.
scroggod61c3842015-12-07 11:37:13 -080073 * @param buffer Must not be NULL, and must be at least size bytes. Destination
74 * to copy bytes.
scroggo028a4132015-04-02 13:19:51 -070075 * @param size Number of bytes to copy.
scroggod61c3842015-12-07 11:37:13 -080076 * @return The number of bytes peeked/copied.
scroggo028a4132015-04-02 13:19:51 -070077 */
scroggod61c3842015-12-07 11:37:13 -080078 virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
scroggo028a4132015-04-02 13:19:51 -070079
bungeman@google.com88682b72013-07-19 13:55:41 +000080 /** Returns true when all the bytes in the stream have been read.
81 * This may return true early (when there are no more bytes to be read)
82 * or late (after the first unsuccessful read).
bungeman@google.com6cab1a42013-05-29 13:43:31 +000083 */
bungeman@google.com04306922013-11-13 19:53:46 +000084 virtual bool isAtEnd() const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
Ben Wagner255ab8d2016-10-07 15:50:53 -040086 bool SK_WARN_UNUSED_RESULT readS8(int8_t*);
87 bool SK_WARN_UNUSED_RESULT readS16(int16_t*);
88 bool SK_WARN_UNUSED_RESULT readS32(int32_t*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
Ben Wagner255ab8d2016-10-07 15:50:53 -040090 bool SK_WARN_UNUSED_RESULT readU8(uint8_t* i) { return this->readS8((int8_t*)i); }
91 bool SK_WARN_UNUSED_RESULT readU16(uint16_t* i) { return this->readS16((int16_t*)i); }
92 bool SK_WARN_UNUSED_RESULT readU32(uint32_t* i) { return this->readS32((int32_t*)i); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
Ben Wagner255ab8d2016-10-07 15:50:53 -040094 bool SK_WARN_UNUSED_RESULT readBool(bool* b) {
95 uint8_t i;
96 if (!this->readU8(&i)) { return false; }
97 *b = (i != 0);
98 return true;
99 }
100 bool SK_WARN_UNUSED_RESULT readScalar(SkScalar*);
101 bool SK_WARN_UNUSED_RESULT readPackedUInt(size_t*);
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000102
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000103//SkStreamRewindable
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000104 /** Rewinds to the beginning of the stream. Returns true if the stream is known
105 * to be at the beginning after this call returns.
106 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000107 virtual bool rewind() { return false; }
108
Mike Reed98c5d922017-09-15 21:39:47 -0400109 /** Duplicates this stream. If this cannot be done, returns NULL.
110 * The returned stream will be positioned at the beginning of its data.
111 */
112 std::unique_ptr<SkStream> duplicate() const {
113 return std::unique_ptr<SkStream>(this->onDuplicate());
114 }
115 /** Duplicates this stream. If this cannot be done, returns NULL.
116 * The returned stream will be positioned the same as this stream.
117 */
118 std::unique_ptr<SkStream> fork() const {
119 return std::unique_ptr<SkStream>(this->onFork());
120 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000121
122//SkStreamSeekable
123 /** Returns true if this stream can report it's current position. */
124 virtual bool hasPosition() const { return false; }
125 /** Returns the current position in the stream. If this cannot be done, returns 0. */
126 virtual size_t getPosition() const { return 0; }
127
128 /** Seeks to an absolute position in the stream. If this cannot be done, returns false.
129 * If an attempt is made to seek past the end of the stream, the position will be set
130 * to the end of the stream.
131 */
djsollenc87dd2c2014-11-14 11:11:46 -0800132 virtual bool seek(size_t /*position*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000133
134 /** Seeks to an relative offset in the stream. If this cannot be done, returns false.
135 * If an attempt is made to move to a position outside the stream, the position will be set
136 * to the closest point within the stream (beginning or end).
137 */
djsollenc87dd2c2014-11-14 11:11:46 -0800138 virtual bool move(long /*offset*/) { return false; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000139
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000140//SkStreamAsset
141 /** Returns true if this stream can report it's total length. */
142 virtual bool hasLength() const { return false; }
143 /** Returns the total length of the stream. If this cannot be done, returns 0. */
bungeman@google.com04306922013-11-13 19:53:46 +0000144 virtual size_t getLength() const { return 0; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000145
146//SkStreamMemory
147 /** Returns the starting address for the data. If this cannot be done, returns NULL. */
148 //TODO: replace with virtual const SkData* getData()
Ben Wagnera93a14a2017-08-28 10:34:05 -0400149 virtual const void* getMemoryBase() { return nullptr; }
Mike Reed98c5d922017-09-15 21:39:47 -0400150
151private:
Mike Reed98c5d922017-09-15 21:39:47 -0400152 virtual SkStream* onDuplicate() const { return nullptr; }
153 virtual SkStream* onFork() const { return nullptr; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154};
155
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000156/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
157class SK_API SkStreamRewindable : public SkStream {
158public:
mtklein36352bf2015-03-25 18:17:31 -0700159 bool rewind() override = 0;
Mike Reed98c5d922017-09-15 21:39:47 -0400160 std::unique_ptr<SkStreamRewindable> duplicate() const {
161 return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
162 }
163private:
164 SkStreamRewindable* onDuplicate() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000165};
166
167/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
168class SK_API SkStreamSeekable : public SkStreamRewindable {
169public:
Mike Reed98c5d922017-09-15 21:39:47 -0400170 std::unique_ptr<SkStreamSeekable> duplicate() const {
171 return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
172 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000173
mtklein36352bf2015-03-25 18:17:31 -0700174 bool hasPosition() const override { return true; }
175 size_t getPosition() const override = 0;
176 bool seek(size_t position) override = 0;
177 bool move(long offset) override = 0;
Mike Reed0ca21462017-09-20 15:37:14 -0400178
Mike Reed98c5d922017-09-15 21:39:47 -0400179 std::unique_ptr<SkStreamSeekable> fork() const {
180 return std::unique_ptr<SkStreamSeekable>(this->onFork());
181 }
182private:
183 SkStreamSeekable* onDuplicate() const override = 0;
184 SkStreamSeekable* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000185};
186
187/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
188class SK_API SkStreamAsset : public SkStreamSeekable {
189public:
Mike Reed7031b242017-09-15 21:03:54 +0000190 bool hasLength() const override { return true; }
191 size_t getLength() const override = 0;
Mike Reed98c5d922017-09-15 21:39:47 -0400192
Mike Reed98c5d922017-09-15 21:39:47 -0400193 std::unique_ptr<SkStreamAsset> duplicate() const {
194 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
195 }
196 std::unique_ptr<SkStreamAsset> fork() const {
197 return std::unique_ptr<SkStreamAsset>(this->onFork());
198 }
199private:
200 SkStreamAsset* onDuplicate() const override = 0;
201 SkStreamAsset* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000202};
203
204/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
205class SK_API SkStreamMemory : public SkStreamAsset {
206public:
Mike Reed98c5d922017-09-15 21:39:47 -0400207 const void* getMemoryBase() override = 0;
208
Mike Reed98c5d922017-09-15 21:39:47 -0400209 std::unique_ptr<SkStreamMemory> duplicate() const {
210 return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
211 }
212 std::unique_ptr<SkStreamMemory> fork() const {
213 return std::unique_ptr<SkStreamMemory>(this->onFork());
214 }
215private:
216 SkStreamMemory* onDuplicate() const override = 0;
217 SkStreamMemory* onFork() const override = 0;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000218};
219
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000220class SK_API SkWStream : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221public:
222 virtual ~SkWStream();
223
224 /** Called to write bytes to a SkWStream. Returns true on success
225 @param buffer the address of at least size bytes to be written to the stream
226 @param size The number of bytes in buffer to write to the stream
227 @return true on success
228 */
229 virtual bool write(const void* buffer, size_t size) = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 virtual void flush();
231
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000232 virtual size_t bytesWritten() const = 0;
233
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 // helpers
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000235
Mike Reed9c457ad2016-12-15 14:11:37 -0500236 bool write8(U8CPU value) {
237 uint8_t v = SkToU8(value);
238 return this->write(&v, 1);
239 }
240 bool write16(U16CPU value) {
241 uint16_t v = SkToU16(value);
242 return this->write(&v, 2);
243 }
244 bool write32(uint32_t v) {
245 return this->write(&v, 4);
246 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247
Mike Reed9c457ad2016-12-15 14:11:37 -0500248 bool writeText(const char text[]) {
halcanarycbc060a2016-04-11 19:41:48 -0700249 SkASSERT(text);
250 return this->write(text, strlen(text));
251 }
Mike Reedc3063e52017-01-07 16:16:02 -0500252
253 bool newline() { return this->write("\n", strlen("\n")); }
254
Ben Wagner255ab8d2016-10-07 15:50:53 -0400255 bool writeDecAsText(int32_t);
256 bool writeBigDecAsText(int64_t, int minDigits = 0);
257 bool writeHexAsText(uint32_t, int minDigits = 0);
258 bool writeScalarAsText(SkScalar);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259
Ben Wagner255ab8d2016-10-07 15:50:53 -0400260 bool writeBool(bool v) { return this->write8(v); }
261 bool writeScalar(SkScalar);
262 bool writePackedUInt(size_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000263
Ben Wagner255ab8d2016-10-07 15:50:53 -0400264 bool writeStream(SkStream* input, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000265
epoger@google.com8e3fb2d2013-03-20 15:56:03 +0000266 /**
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000267 * This returns the number of bytes in the stream required to store
268 * 'value'.
269 */
270 static int SizeOfPackedUInt(size_t value);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271};
272
Mike Reed2a65cc02017-03-22 10:01:53 -0400273class SK_API SkNullWStream : public SkWStream {
274public:
275 SkNullWStream() : fBytesWritten(0) {}
276
277 bool write(const void*, size_t n) override { fBytesWritten += n; return true; }
278 void flush() override {}
279 size_t bytesWritten() const override { return fBytesWritten; }
280
281private:
282 size_t fBytesWritten;
283};
284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285////////////////////////////////////////////////////////////////////////////////////////
286
bungeman@google.comfab44db2013-10-11 18:50:45 +0000287#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000289/** A stream that wraps a C FILE* file stream. */
290class SK_API SkFILEStream : public SkStreamAsset {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291public:
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000292 /** Initialize the stream by calling sk_fopen on the specified path.
293 * This internal stream will be closed in the destructor.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500295 explicit SkFILEStream(const char path[] = nullptr);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000296
Leon Scroggins III8a8a1442018-05-08 09:35:32 -0400297 /** Initialize the stream with an existing C FILE stream.
298 * The current position of the C FILE stream will be considered the
299 * beginning of the SkFILEStream.
300 * The C FILE stream will be closed in the destructor.
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000301 */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500302 explicit SkFILEStream(FILE* file);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000303
Brian Salomond3b65972017-03-22 12:05:03 -0400304 ~SkFILEStream() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305
Mike Reed847068c2017-07-26 11:35:53 -0400306 static std::unique_ptr<SkFILEStream> Make(const char path[]) {
Hal Canary4b972d12017-08-26 19:31:12 -0400307 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path));
308 return stream->isValid() ? std::move(stream) : nullptr;
Mike Reed847068c2017-07-26 11:35:53 -0400309 }
310
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000311 /** Returns true if the current path could be opened. */
Ben Wagner4d1955c2017-03-10 13:08:15 -0500312 bool isValid() const { return fFILE != nullptr; }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000313
Ben Wagner4d1955c2017-03-10 13:08:15 -0500314 /** Close this SkFILEStream. */
315 void close();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316
mtklein36352bf2015-03-25 18:17:31 -0700317 size_t read(void* buffer, size_t size) override;
318 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000319
mtklein36352bf2015-03-25 18:17:31 -0700320 bool rewind() override;
Mike Reed98c5d922017-09-15 21:39:47 -0400321 std::unique_ptr<SkStreamAsset> duplicate() const {
322 return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
323 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000324
mtklein36352bf2015-03-25 18:17:31 -0700325 size_t getPosition() const override;
326 bool seek(size_t position) override;
327 bool move(long offset) override;
Mike Reed0ca21462017-09-20 15:37:14 -0400328
Mike Reed98c5d922017-09-15 21:39:47 -0400329 std::unique_ptr<SkStreamAsset> fork() const {
330 return std::unique_ptr<SkStreamAsset>(this->onFork());
331 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000332
mtklein36352bf2015-03-25 18:17:31 -0700333 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335private:
Ben Wagner4d1955c2017-03-10 13:08:15 -0500336 explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset);
337 explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset, size_t originalOffset);
338
Mike Reed98c5d922017-09-15 21:39:47 -0400339 SkStreamAsset* onDuplicate() const override;
340 SkStreamAsset* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -0400341
Ben Wagner4d1955c2017-03-10 13:08:15 -0500342 std::shared_ptr<FILE> fFILE;
343 // My own council will I keep on sizes and offsets.
344 size_t fSize;
345 size_t fOffset;
346 size_t fOriginalOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000347
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000348 typedef SkStreamAsset INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349};
350
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000351class SK_API SkMemoryStream : public SkStreamMemory {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352public:
353 SkMemoryStream();
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000354
355 /** We allocate (and free) the memory. Write to it via getMemoryBase() */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 SkMemoryStream(size_t length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000357
358 /** If copyData is true, the stream makes a private copy of the data. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkMemoryStream(const void* data, size_t length, bool copyData = false);
scroggo@google.come4904202013-01-09 22:02:58 +0000360
reedfde05112016-03-11 13:02:28 -0800361 /** Creates the stream to read from the specified data */
362 SkMemoryStream(sk_sp<SkData>);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363
Mike Reed847068c2017-07-26 11:35:53 -0400364 /** Returns a stream with a copy of the input data. */
365 static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
366
367 /** Returns a stream with a bare pointer reference to the input data. */
368 static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
369
370 /** Returns a stream with a shared reference to the input data. */
371 static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
372
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 /** Resets the stream to the specified data and length,
374 just like the constructor.
375 if copyData is true, the stream makes a private copy of the data
376 */
377 virtual void setMemory(const void* data, size_t length,
378 bool copyData = false);
djsollen@google.com57f49692011-02-23 20:46:31 +0000379 /** Replace any memory buffer with the specified buffer. The caller
380 must have allocated data with sk_malloc or sk_realloc, since it
381 will be freed with sk_free.
382 */
383 void setMemoryOwned(const void* data, size_t length);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000384
reed42943c82016-09-12 12:01:44 -0700385 sk_sp<SkData> asData() const { return fData; }
386 void setData(sk_sp<SkData>);
reed@google.com8a85d0c2011-06-24 19:12:12 +0000387
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 void skipToAlign4();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 const void* getAtPos();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000390
mtklein36352bf2015-03-25 18:17:31 -0700391 size_t read(void* buffer, size_t size) override;
392 bool isAtEnd() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000393
scroggod61c3842015-12-07 11:37:13 -0800394 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -0700395
mtklein36352bf2015-03-25 18:17:31 -0700396 bool rewind() override;
Mike Reed0ca21462017-09-20 15:37:14 -0400397
Mike Reed98c5d922017-09-15 21:39:47 -0400398 std::unique_ptr<SkMemoryStream> duplicate() const {
399 return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
400 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000401
mtklein36352bf2015-03-25 18:17:31 -0700402 size_t getPosition() const override;
403 bool seek(size_t position) override;
404 bool move(long offset) override;
Mike Reed0ca21462017-09-20 15:37:14 -0400405
Mike Reed98c5d922017-09-15 21:39:47 -0400406 std::unique_ptr<SkMemoryStream> fork() const {
407 return std::unique_ptr<SkMemoryStream>(this->onFork());
408 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000409
mtklein36352bf2015-03-25 18:17:31 -0700410 size_t getLength() const override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000411
mtklein36352bf2015-03-25 18:17:31 -0700412 const void* getMemoryBase() override;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000413
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414private:
Mike Reed98c5d922017-09-15 21:39:47 -0400415 SkMemoryStream* onDuplicate() const override;
416 SkMemoryStream* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -0400417
reedfde05112016-03-11 13:02:28 -0800418 sk_sp<SkData> fData;
419 size_t fOffset;
reed@google.com3b429982012-06-26 15:30:08 +0000420
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000421 typedef SkStreamMemory INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422};
423
424/////////////////////////////////////////////////////////////////////////////////////////////
425
alokp@chromium.orgf7751ae2012-07-17 19:10:36 +0000426class SK_API SkFILEWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427public:
reed@google.com3b429982012-06-26 15:30:08 +0000428 SkFILEWStream(const char path[]);
Brian Salomond3b65972017-03-22 12:05:03 -0400429 ~SkFILEWStream() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430
431 /** Returns true if the current path could be opened.
432 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400433 bool isValid() const { return fFILE != nullptr; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434
mtklein36352bf2015-03-25 18:17:31 -0700435 bool write(const void* buffer, size_t size) override;
436 void flush() override;
caryclark7471fa42015-12-16 13:41:23 -0800437 void fsync();
mtklein36352bf2015-03-25 18:17:31 -0700438 size_t bytesWritten() const override;
reed@google.com3b429982012-06-26 15:30:08 +0000439
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440private:
halcanaryd76be9c2015-11-20 13:47:49 -0800441 FILE* fFILE;
reed@google.com3b429982012-06-26 15:30:08 +0000442
443 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444};
445
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +0000446class SK_API SkDynamicMemoryWStream : public SkWStream {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447public:
448 SkDynamicMemoryWStream();
Brian Salomond3b65972017-03-22 12:05:03 -0400449 ~SkDynamicMemoryWStream() override;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000450
mtklein36352bf2015-03-25 18:17:31 -0700451 bool write(const void* buffer, size_t size) override;
Mike Reed9c457ad2016-12-15 14:11:37 -0500452 size_t bytesWritten() const override;
Ben Wagner884300d2016-12-16 16:51:41 +0000453
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 bool read(void* buffer, size_t offset, size_t size);
Mike Reed9c457ad2016-12-15 14:11:37 -0500455
Ben Wagner884300d2016-12-16 16:51:41 +0000456 /** More efficient version of read(dst, 0, bytesWritten()). */
reed@google.com8a85d0c2011-06-24 19:12:12 +0000457 void copyTo(void* dst) const;
Hal Canary6fee59b2017-05-23 11:47:05 -0400458 bool writeToStream(SkWStream* dst) const;
reed@google.com70442a62011-06-23 21:48:04 +0000459
Hal Canarydabe8ac2017-03-14 15:52:12 -0400460 /** Equivalent to copyTo() followed by reset(), but may save memory use. */
461 void copyToAndReset(void* dst);
462
Hal Canary6fee59b2017-05-23 11:47:05 -0400463 /** Equivalent to writeToStream() followed by reset(), but may save memory use. */
464 bool writeToAndReset(SkWStream* dst);
465
Ben Wagner884300d2016-12-16 16:51:41 +0000466 /** Return the contents as SkData, and then reset the stream. */
reed42943c82016-09-12 12:01:44 -0700467 sk_sp<SkData> detachAsData();
reed@google.com70442a62011-06-23 21:48:04 +0000468
bungeman@google.com88682b72013-07-19 13:55:41 +0000469 /** Reset, returning a reader stream with the current content. */
Hal Canary0b833192017-03-14 14:35:02 -0400470 std::unique_ptr<SkStreamAsset> detachAsStream();
bungeman@google.com88682b72013-07-19 13:55:41 +0000471
472 /** Reset the stream to its original, empty, state. */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 void reset();
474 void padToAlign4();
475private:
476 struct Block;
477 Block* fHead;
478 Block* fTail;
Mike Reed9c457ad2016-12-15 14:11:37 -0500479 size_t fBytesWrittenBeforeTail;
480
481#ifdef SK_DEBUG
482 void validate() const;
483#else
484 void validate() const {}
485#endif
reed@google.com3b429982012-06-26 15:30:08 +0000486
bungeman@google.com88682b72013-07-19 13:55:41 +0000487 // For access to the Block type.
488 friend class SkBlockMemoryStream;
489 friend class SkBlockMemoryRefCnt;
490
reed@google.com3b429982012-06-26 15:30:08 +0000491 typedef SkWStream INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492};
493
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494#endif