reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkStream_DEFINED |
| 9 | #define SkStream_DEFINED |
| 10 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
| 13 | #include "SkScalar.h" |
| 14 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 15 | class SkStream; |
| 16 | class SkStreamRewindable; |
| 17 | class SkStreamSeekable; |
| 18 | class SkStreamAsset; |
| 19 | class SkStreamMemory; |
| 20 | |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 21 | /** |
| 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.com | 2b34fe0 | 2013-05-08 07:01:40 +0000 | [diff] [blame] | 31 | * |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 32 | * 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 | */ |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 38 | class SK_API SkStream : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | public: |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 40 | virtual ~SkStream() {} |
| 41 | |
| 42 | /** |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 43 | * Attempts to open the specified file as a stream, returns nullptr on failure. |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 44 | */ |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 45 | static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 46 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 47 | /** 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.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 52 | * @return the number of bytes actually read. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 53 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | virtual size_t read(void* buffer, size_t size) = 0; |
| 55 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 56 | /** 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.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 60 | return this->read(NULL, size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 61 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 62 | |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Attempt to peek at size bytes. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 65 | * 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. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 69 | * The stream is guaranteed to be in the same visible state after this |
| 70 | * call, regardless of success or failure. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 71 | * @param buffer Must not be NULL, and must be at least size bytes. Destination |
| 72 | * to copy bytes. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 73 | * @param size Number of bytes to copy. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 74 | * @return The number of bytes peeked/copied. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 75 | */ |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 76 | virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 77 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 78 | /** 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.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 81 | */ |
bungeman@google.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 82 | virtual bool isAtEnd() const = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | |
| 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 95 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 96 | //SkStreamRewindable |
scroggo@google.com | 4d213ab | 2013-08-28 13:08:54 +0000 | [diff] [blame] | 97 | /** 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.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 100 | 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 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 117 | virtual bool seek(size_t /*position*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 118 | |
| 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 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 123 | virtual bool move(long /*offset*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 124 | |
| 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.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 134 | virtual size_t getLength() const { return 0; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 135 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 142 | /** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */ |
| 143 | class SK_API SkStreamRewindable : public SkStream { |
| 144 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 145 | bool rewind() override = 0; |
| 146 | SkStreamRewindable* duplicate() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */ |
| 150 | class SK_API SkStreamSeekable : public SkStreamRewindable { |
| 151 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 152 | SkStreamSeekable* duplicate() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | 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.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |
| 162 | class SK_API SkStreamAsset : public SkStreamSeekable { |
| 163 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 164 | SkStreamAsset* duplicate() const override = 0; |
| 165 | SkStreamAsset* fork() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 166 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 167 | bool hasLength() const override { return true; } |
| 168 | size_t getLength() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */ |
| 172 | class SK_API SkStreamMemory : public SkStreamAsset { |
| 173 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 174 | SkStreamMemory* duplicate() const override = 0; |
| 175 | SkStreamMemory* fork() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 176 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 177 | const void* getMemoryBase() override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 180 | class SK_API SkWStream : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | public: |
| 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; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | virtual void flush(); |
| 191 | |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 192 | virtual size_t bytesWritten() const = 0; |
| 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | // helpers |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 195 | |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 196 | bool write8(U8CPU value) { |
| 197 | uint8_t v = SkToU8(value); |
| 198 | return this->write(&v, 1); |
| 199 | } |
| 200 | bool write16(U16CPU value) { |
| 201 | uint16_t v = SkToU16(value); |
| 202 | return this->write(&v, 2); |
| 203 | } |
| 204 | bool write32(uint32_t v) { |
| 205 | return this->write(&v, 4); |
| 206 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 208 | bool writeText(const char text[]) { |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 209 | SkASSERT(text); |
| 210 | return this->write(text, strlen(text)); |
| 211 | } |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame^] | 212 | |
| 213 | bool newline() { return this->write("\n", strlen("\n")); } |
| 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | bool writeDecAsText(int32_t); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 216 | bool writeBigDecAsText(int64_t, int minDigits = 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | bool writeHexAsText(uint32_t, int minDigits = 0); |
| 218 | bool writeScalarAsText(SkScalar); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 219 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | bool writeBool(bool v) { return this->write8(v); } |
| 221 | bool writeScalar(SkScalar); |
| 222 | bool writePackedUInt(size_t); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 224 | bool writeStream(SkStream* input, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 225 | |
epoger@google.com | 8e3fb2d | 2013-03-20 15:56:03 +0000 | [diff] [blame] | 226 | /** |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 227 | * This returns the number of bytes in the stream required to store |
| 228 | * 'value'. |
| 229 | */ |
| 230 | static int SizeOfPackedUInt(size_t value); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | //////////////////////////////////////////////////////////////////////////////////////// |
| 234 | |
| 235 | #include "SkString.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 236 | #include <stdio.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 238 | /** A stream that wraps a C FILE* file stream. */ |
| 239 | class SK_API SkFILEStream : public SkStreamAsset { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | public: |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 241 | /** Initialize the stream by calling sk_fopen on the specified path. |
| 242 | * This internal stream will be closed in the destructor. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | */ |
| 244 | explicit SkFILEStream(const char path[] = NULL); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 245 | |
| 246 | enum Ownership { |
| 247 | kCallerPasses_Ownership, |
| 248 | kCallerRetains_Ownership |
| 249 | }; |
| 250 | /** Initialize the stream with an existing C file stream. |
| 251 | * While this stream exists, it assumes exclusive access to the C file stream. |
| 252 | * The C file stream will be closed in the destructor unless the caller specifies |
| 253 | * kCallerRetains_Ownership. |
| 254 | */ |
| 255 | explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership); |
| 256 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | virtual ~SkFILEStream(); |
| 258 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 259 | /** Returns true if the current path could be opened. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | bool isValid() const { return fFILE != NULL; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 261 | |
| 262 | /** Close the current file, and open a new file with the specified path. |
| 263 | * If path is NULL, just close the current file. |
| 264 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | void setPath(const char path[]); |
| 266 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 267 | size_t read(void* buffer, size_t size) override; |
| 268 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 269 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 270 | bool rewind() override; |
| 271 | SkStreamAsset* duplicate() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 272 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 273 | size_t getPosition() const override; |
| 274 | bool seek(size_t position) override; |
| 275 | bool move(long offset) override; |
| 276 | SkStreamAsset* fork() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 277 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 278 | size_t getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 279 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 280 | const void* getMemoryBase() override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | |
| 282 | private: |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 283 | FILE* fFILE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | SkString fName; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 285 | Ownership fOwnership; |
| 286 | // fData is lazilly initialized when needed. |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 287 | mutable sk_sp<SkData> fData; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 288 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 289 | typedef SkStreamAsset INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 292 | class SK_API SkMemoryStream : public SkStreamMemory { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | public: |
| 294 | SkMemoryStream(); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 295 | |
| 296 | /** We allocate (and free) the memory. Write to it via getMemoryBase() */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | SkMemoryStream(size_t length); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 298 | |
| 299 | /** If copyData is true, the stream makes a private copy of the data. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | SkMemoryStream(const void* data, size_t length, bool copyData = false); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 301 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 302 | /** Creates the stream to read from the specified data */ |
| 303 | SkMemoryStream(sk_sp<SkData>); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 304 | |
| 305 | /** Resets the stream to the specified data and length, |
| 306 | just like the constructor. |
| 307 | if copyData is true, the stream makes a private copy of the data |
| 308 | */ |
| 309 | virtual void setMemory(const void* data, size_t length, |
| 310 | bool copyData = false); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 311 | /** Replace any memory buffer with the specified buffer. The caller |
| 312 | must have allocated data with sk_malloc or sk_realloc, since it |
| 313 | will be freed with sk_free. |
| 314 | */ |
| 315 | void setMemoryOwned(const void* data, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 316 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 317 | sk_sp<SkData> asData() const { return fData; } |
| 318 | void setData(sk_sp<SkData>); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 319 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | void skipToAlign4(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | const void* getAtPos(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 322 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 323 | size_t read(void* buffer, size_t size) override; |
| 324 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 325 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 326 | size_t peek(void* buffer, size_t size) const override; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 327 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 328 | bool rewind() override; |
| 329 | SkMemoryStream* duplicate() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 330 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 331 | size_t getPosition() const override; |
| 332 | bool seek(size_t position) override; |
| 333 | bool move(long offset) override; |
| 334 | SkMemoryStream* fork() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 335 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 336 | size_t getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 337 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 338 | const void* getMemoryBase() override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | private: |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 341 | sk_sp<SkData> fData; |
| 342 | size_t fOffset; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 343 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 344 | typedef SkStreamMemory INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 348 | |
alokp@chromium.org | f7751ae | 2012-07-17 19:10:36 +0000 | [diff] [blame] | 349 | class SK_API SkFILEWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 351 | SkFILEWStream(const char path[]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | virtual ~SkFILEWStream(); |
| 353 | |
| 354 | /** Returns true if the current path could be opened. |
| 355 | */ |
| 356 | bool isValid() const { return fFILE != NULL; } |
| 357 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 358 | bool write(const void* buffer, size_t size) override; |
| 359 | void flush() override; |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 360 | void fsync(); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 361 | size_t bytesWritten() const override; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 362 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | private: |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 364 | FILE* fFILE; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 365 | |
| 366 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
caryclark | 15e2a53 | 2016-03-16 06:51:55 -0700 | [diff] [blame] | 369 | class SK_API SkMemoryWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 370 | public: |
| 371 | SkMemoryWStream(void* buffer, size_t size); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 372 | bool write(const void* buffer, size_t size) override; |
| 373 | size_t bytesWritten() const override { return fBytesWritten; } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 374 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | private: |
| 376 | char* fBuffer; |
| 377 | size_t fMaxLength; |
| 378 | size_t fBytesWritten; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 379 | |
| 380 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | }; |
| 382 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 383 | class SK_API SkDynamicMemoryWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | public: |
| 385 | SkDynamicMemoryWStream(); |
| 386 | virtual ~SkDynamicMemoryWStream(); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 387 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 388 | bool write(const void* buffer, size_t size) override; |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 389 | size_t bytesWritten() const override; |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | bool read(void* buffer, size_t offset, size_t size); |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 392 | |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 393 | /** More efficient version of read(dst, 0, bytesWritten()). */ |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 394 | void copyTo(void* dst) const; |
halcanary | 7af2150 | 2015-02-23 12:17:59 -0800 | [diff] [blame] | 395 | void writeToStream(SkWStream* dst) const; |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 396 | |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 397 | /** Return the contents as SkData, and then reset the stream. */ |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 398 | sk_sp<SkData> detachAsData(); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 399 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 400 | /** Reset, returning a reader stream with the current content. */ |
bungeman@google.com | c29f3d8 | 2013-07-19 22:32:11 +0000 | [diff] [blame] | 401 | SkStreamAsset* detachAsStream(); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 402 | |
| 403 | /** Reset the stream to its original, empty, state. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 404 | void reset(); |
| 405 | void padToAlign4(); |
| 406 | private: |
| 407 | struct Block; |
| 408 | Block* fHead; |
| 409 | Block* fTail; |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 410 | size_t fBytesWrittenBeforeTail; |
| 411 | |
| 412 | #ifdef SK_DEBUG |
| 413 | void validate() const; |
| 414 | #else |
| 415 | void validate() const {} |
| 416 | #endif |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 417 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 418 | // For access to the Block type. |
| 419 | friend class SkBlockMemoryStream; |
| 420 | friend class SkBlockMemoryRefCnt; |
| 421 | |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 422 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | #endif |