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 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 15 | #include <memory.h> |
| 16 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 17 | class SkStream; |
| 18 | class SkStreamRewindable; |
| 19 | class SkStreamSeekable; |
| 20 | class SkStreamAsset; |
| 21 | class SkStreamMemory; |
| 22 | |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 23 | /** |
| 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.com | 2b34fe0 | 2013-05-08 07:01:40 +0000 | [diff] [blame] | 33 | * |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 34 | * 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 | */ |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 40 | class SK_API SkStream : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | public: |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 42 | virtual ~SkStream() {} |
| 43 | |
| 44 | /** |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 45 | * 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] | 46 | */ |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 47 | static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 48 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 49 | /** 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.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 54 | * @return the number of bytes actually read. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 55 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | virtual size_t read(void* buffer, size_t size) = 0; |
| 57 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 58 | /** Skip size number of bytes. |
| 59 | * @return the actual number bytes that could be skipped. |
| 60 | */ |
| 61 | size_t skip(size_t size) { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 62 | return this->read(nullptr, size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 63 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 64 | |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Attempt to peek at size bytes. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 67 | * 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. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 71 | * The stream is guaranteed to be in the same visible state after this |
| 72 | * call, regardless of success or failure. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 73 | * @param buffer Must not be NULL, and must be at least size bytes. Destination |
| 74 | * to copy bytes. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 75 | * @param size Number of bytes to copy. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 76 | * @return The number of bytes peeked/copied. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 77 | */ |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 78 | virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 79 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 80 | /** 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.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 83 | */ |
bungeman@google.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 84 | virtual bool isAtEnd() const = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | |
| 86 | int8_t readS8(); |
| 87 | int16_t readS16(); |
| 88 | int32_t readS32(); |
| 89 | |
| 90 | uint8_t readU8() { return (uint8_t)this->readS8(); } |
| 91 | uint16_t readU16() { return (uint16_t)this->readS16(); } |
| 92 | uint32_t readU32() { return (uint32_t)this->readS32(); } |
| 93 | |
| 94 | bool readBool() { return this->readU8() != 0; } |
| 95 | SkScalar readScalar(); |
| 96 | size_t readPackedUInt(); |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 97 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 98 | //SkStreamRewindable |
scroggo@google.com | 4d213ab | 2013-08-28 13:08:54 +0000 | [diff] [blame] | 99 | /** Rewinds to the beginning of the stream. Returns true if the stream is known |
| 100 | * to be at the beginning after this call returns. |
| 101 | */ |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 102 | virtual bool rewind() { return false; } |
| 103 | |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 104 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 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 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 108 | virtual SkStreamRewindable* duplicate() const { return nullptr; } |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 109 | /** Duplicates this stream. If this cannot be done, returns NULL. |
| 110 | * The returned stream will be positioned the same as this stream. |
| 111 | */ |
| 112 | virtual SkStreamSeekable* fork() const { return nullptr; } |
| 113 | #else |
| 114 | /** Duplicates this stream. If this cannot be done, returns NULL. |
| 115 | * The returned stream will be positioned at the beginning of its data. |
| 116 | */ |
| 117 | std::unique_ptr<SkStream> duplicate() const { |
| 118 | return std::unique_ptr<SkStream>(this->onDuplicate()); |
| 119 | } |
| 120 | /** Duplicates this stream. If this cannot be done, returns NULL. |
| 121 | * The returned stream will be positioned the same as this stream. |
| 122 | */ |
| 123 | std::unique_ptr<SkStream> fork() const { |
| 124 | return std::unique_ptr<SkStream>(this->onFork()); |
| 125 | } |
| 126 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 127 | |
| 128 | //SkStreamSeekable |
| 129 | /** Returns true if this stream can report it's current position. */ |
| 130 | virtual bool hasPosition() const { return false; } |
| 131 | /** Returns the current position in the stream. If this cannot be done, returns 0. */ |
| 132 | virtual size_t getPosition() const { return 0; } |
| 133 | |
| 134 | /** Seeks to an absolute position in the stream. If this cannot be done, returns false. |
| 135 | * If an attempt is made to seek past the end of the stream, the position will be set |
| 136 | * to the end of the stream. |
| 137 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 138 | virtual bool seek(size_t /*position*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 139 | |
| 140 | /** Seeks to an relative offset in the stream. If this cannot be done, returns false. |
| 141 | * If an attempt is made to move to a position outside the stream, the position will be set |
| 142 | * to the closest point within the stream (beginning or end). |
| 143 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 144 | virtual bool move(long /*offset*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 145 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 146 | //SkStreamAsset |
| 147 | /** Returns true if this stream can report it's total length. */ |
| 148 | virtual bool hasLength() const { return false; } |
| 149 | /** 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] | 150 | virtual size_t getLength() const { return 0; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 151 | |
| 152 | //SkStreamMemory |
| 153 | /** Returns the starting address for the data. If this cannot be done, returns NULL. */ |
| 154 | //TODO: replace with virtual const SkData* getData() |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 155 | virtual const void* getMemoryBase() { return nullptr; } |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 156 | |
| 157 | private: |
| 158 | #ifndef SK_SUPPORT_LEGACY_STREAM_API |
| 159 | virtual SkStream* onDuplicate() const { return nullptr; } |
| 160 | virtual SkStream* onFork() const { return nullptr; } |
| 161 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 164 | /** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */ |
| 165 | class SK_API SkStreamRewindable : public SkStream { |
| 166 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 167 | bool rewind() override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 168 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 169 | SkStreamRewindable* duplicate() const override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 170 | #else |
| 171 | std::unique_ptr<SkStreamRewindable> duplicate() const { |
| 172 | return std::unique_ptr<SkStreamRewindable>(this->onDuplicate()); |
| 173 | } |
| 174 | private: |
| 175 | SkStreamRewindable* onDuplicate() const override = 0; |
| 176 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */ |
| 180 | class SK_API SkStreamSeekable : public SkStreamRewindable { |
| 181 | public: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 182 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 183 | SkStreamSeekable* duplicate() const override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 184 | #else |
| 185 | std::unique_ptr<SkStreamSeekable> duplicate() const { |
| 186 | return std::unique_ptr<SkStreamSeekable>(this->onDuplicate()); |
| 187 | } |
| 188 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 189 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 190 | bool hasPosition() const override { return true; } |
| 191 | size_t getPosition() const override = 0; |
| 192 | bool seek(size_t position) override = 0; |
| 193 | bool move(long offset) override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 194 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 195 | SkStreamSeekable* fork() const override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 196 | #else |
| 197 | std::unique_ptr<SkStreamSeekable> fork() const { |
| 198 | return std::unique_ptr<SkStreamSeekable>(this->onFork()); |
| 199 | } |
| 200 | private: |
| 201 | SkStreamSeekable* onDuplicate() const override = 0; |
| 202 | SkStreamSeekable* onFork() const override = 0; |
| 203 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |
| 207 | class SK_API SkStreamAsset : public SkStreamSeekable { |
| 208 | public: |
Mike Reed | 7031b24 | 2017-09-15 21:03:54 +0000 | [diff] [blame] | 209 | bool hasLength() const override { return true; } |
| 210 | size_t getLength() const override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 211 | |
| 212 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
| 213 | SkStreamAsset* duplicate() const override = 0; |
| 214 | SkStreamAsset* fork() const override = 0; |
| 215 | #else |
| 216 | std::unique_ptr<SkStreamAsset> duplicate() const { |
| 217 | return std::unique_ptr<SkStreamAsset>(this->onDuplicate()); |
| 218 | } |
| 219 | std::unique_ptr<SkStreamAsset> fork() const { |
| 220 | return std::unique_ptr<SkStreamAsset>(this->onFork()); |
| 221 | } |
| 222 | private: |
| 223 | SkStreamAsset* onDuplicate() const override = 0; |
| 224 | SkStreamAsset* onFork() const override = 0; |
| 225 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */ |
| 229 | class SK_API SkStreamMemory : public SkStreamAsset { |
| 230 | public: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 231 | const void* getMemoryBase() override = 0; |
| 232 | |
| 233 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | SkStreamMemory* duplicate() const override = 0; |
| 235 | SkStreamMemory* fork() const override = 0; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 236 | #else |
| 237 | std::unique_ptr<SkStreamMemory> duplicate() const { |
| 238 | return std::unique_ptr<SkStreamMemory>(this->onDuplicate()); |
| 239 | } |
| 240 | std::unique_ptr<SkStreamMemory> fork() const { |
| 241 | return std::unique_ptr<SkStreamMemory>(this->onFork()); |
| 242 | } |
| 243 | private: |
| 244 | SkStreamMemory* onDuplicate() const override = 0; |
| 245 | SkStreamMemory* onFork() const override = 0; |
| 246 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 249 | class SK_API SkWStream : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | public: |
| 251 | virtual ~SkWStream(); |
| 252 | |
| 253 | /** Called to write bytes to a SkWStream. Returns true on success |
| 254 | @param buffer the address of at least size bytes to be written to the stream |
| 255 | @param size The number of bytes in buffer to write to the stream |
| 256 | @return true on success |
| 257 | */ |
| 258 | virtual bool write(const void* buffer, size_t size) = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | virtual void flush(); |
| 260 | |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 261 | virtual size_t bytesWritten() const = 0; |
| 262 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | // helpers |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 264 | |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 265 | bool write8(U8CPU value) { |
| 266 | uint8_t v = SkToU8(value); |
| 267 | return this->write(&v, 1); |
| 268 | } |
| 269 | bool write16(U16CPU value) { |
| 270 | uint16_t v = SkToU16(value); |
| 271 | return this->write(&v, 2); |
| 272 | } |
| 273 | bool write32(uint32_t v) { |
| 274 | return this->write(&v, 4); |
| 275 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 277 | bool writeText(const char text[]) { |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 278 | SkASSERT(text); |
| 279 | return this->write(text, strlen(text)); |
| 280 | } |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 281 | |
| 282 | bool newline() { return this->write("\n", strlen("\n")); } |
| 283 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | bool writeDecAsText(int32_t); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 285 | bool writeBigDecAsText(int64_t, int minDigits = 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | bool writeHexAsText(uint32_t, int minDigits = 0); |
| 287 | bool writeScalarAsText(SkScalar); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | bool writeBool(bool v) { return this->write8(v); } |
| 290 | bool writeScalar(SkScalar); |
| 291 | bool writePackedUInt(size_t); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 292 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 293 | bool writeStream(SkStream* input, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 294 | |
epoger@google.com | 8e3fb2d | 2013-03-20 15:56:03 +0000 | [diff] [blame] | 295 | /** |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 296 | * This returns the number of bytes in the stream required to store |
| 297 | * 'value'. |
| 298 | */ |
| 299 | static int SizeOfPackedUInt(size_t value); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 302 | class SK_API SkNullWStream : public SkWStream { |
| 303 | public: |
| 304 | SkNullWStream() : fBytesWritten(0) {} |
| 305 | |
| 306 | bool write(const void*, size_t n) override { fBytesWritten += n; return true; } |
| 307 | void flush() override {} |
| 308 | size_t bytesWritten() const override { return fBytesWritten; } |
| 309 | |
| 310 | private: |
| 311 | size_t fBytesWritten; |
| 312 | }; |
| 313 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | //////////////////////////////////////////////////////////////////////////////////////// |
| 315 | |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 316 | #include <stdio.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 318 | /** A stream that wraps a C FILE* file stream. */ |
| 319 | class SK_API SkFILEStream : public SkStreamAsset { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | public: |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 321 | /** Initialize the stream by calling sk_fopen on the specified path. |
| 322 | * This internal stream will be closed in the destructor. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | */ |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 324 | explicit SkFILEStream(const char path[] = nullptr); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 325 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 326 | /** Initialize the stream with an existing C file stream. |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 327 | * The C file stream will be closed in the destructor. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 328 | */ |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 329 | explicit SkFILEStream(FILE* file); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 330 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 331 | ~SkFILEStream() override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 333 | static std::unique_ptr<SkFILEStream> Make(const char path[]) { |
Hal Canary | 4b972d1 | 2017-08-26 19:31:12 -0400 | [diff] [blame] | 334 | std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path)); |
| 335 | return stream->isValid() ? std::move(stream) : nullptr; |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 336 | } |
| 337 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 338 | /** Returns true if the current path could be opened. */ |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 339 | bool isValid() const { return fFILE != nullptr; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 340 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 341 | /** Close this SkFILEStream. */ |
| 342 | void close(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 344 | size_t read(void* buffer, size_t size) override; |
| 345 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 346 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 347 | bool rewind() override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 348 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 349 | SkStreamAsset* duplicate() const override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 350 | #else |
| 351 | std::unique_ptr<SkStreamAsset> duplicate() const { |
| 352 | return std::unique_ptr<SkStreamAsset>(this->onDuplicate()); |
| 353 | } |
| 354 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 355 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 356 | size_t getPosition() const override; |
| 357 | bool seek(size_t position) override; |
| 358 | bool move(long offset) override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 359 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 360 | SkStreamAsset* fork() const override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 361 | #else |
| 362 | std::unique_ptr<SkStreamAsset> fork() const { |
| 363 | return std::unique_ptr<SkStreamAsset>(this->onFork()); |
| 364 | } |
| 365 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 366 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 367 | size_t getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 368 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | private: |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 370 | explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset); |
| 371 | explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset, size_t originalOffset); |
| 372 | |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 373 | #ifndef SK_SUPPORT_LEGACY_STREAM_API |
| 374 | SkStreamAsset* onDuplicate() const override; |
| 375 | SkStreamAsset* onFork() const override; |
| 376 | #endif |
| 377 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 378 | std::shared_ptr<FILE> fFILE; |
| 379 | // My own council will I keep on sizes and offsets. |
| 380 | size_t fSize; |
| 381 | size_t fOffset; |
| 382 | size_t fOriginalOffset; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 383 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 384 | typedef SkStreamAsset INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 387 | class SK_API SkMemoryStream : public SkStreamMemory { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | public: |
| 389 | SkMemoryStream(); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 390 | |
| 391 | /** We allocate (and free) the memory. Write to it via getMemoryBase() */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | SkMemoryStream(size_t length); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 393 | |
| 394 | /** 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] | 395 | SkMemoryStream(const void* data, size_t length, bool copyData = false); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 396 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 397 | /** Creates the stream to read from the specified data */ |
| 398 | SkMemoryStream(sk_sp<SkData>); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 399 | |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 400 | /** Returns a stream with a copy of the input data. */ |
| 401 | static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length); |
| 402 | |
| 403 | /** Returns a stream with a bare pointer reference to the input data. */ |
| 404 | static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length); |
| 405 | |
| 406 | /** Returns a stream with a shared reference to the input data. */ |
| 407 | static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data); |
| 408 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | /** Resets the stream to the specified data and length, |
| 410 | just like the constructor. |
| 411 | if copyData is true, the stream makes a private copy of the data |
| 412 | */ |
| 413 | virtual void setMemory(const void* data, size_t length, |
| 414 | bool copyData = false); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 415 | /** Replace any memory buffer with the specified buffer. The caller |
| 416 | must have allocated data with sk_malloc or sk_realloc, since it |
| 417 | will be freed with sk_free. |
| 418 | */ |
| 419 | void setMemoryOwned(const void* data, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 420 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 421 | sk_sp<SkData> asData() const { return fData; } |
| 422 | void setData(sk_sp<SkData>); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 423 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | void skipToAlign4(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | const void* getAtPos(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 426 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 427 | size_t read(void* buffer, size_t size) override; |
| 428 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 429 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 430 | size_t peek(void* buffer, size_t size) const override; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 431 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 432 | bool rewind() override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 433 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 434 | SkMemoryStream* duplicate() const override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 435 | #else |
| 436 | std::unique_ptr<SkMemoryStream> duplicate() const { |
| 437 | return std::unique_ptr<SkMemoryStream>(this->onDuplicate()); |
| 438 | } |
| 439 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 440 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 441 | size_t getPosition() const override; |
| 442 | bool seek(size_t position) override; |
| 443 | bool move(long offset) override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 444 | #ifdef SK_SUPPORT_LEGACY_STREAM_API |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 445 | SkMemoryStream* fork() const override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 446 | #else |
| 447 | std::unique_ptr<SkMemoryStream> fork() const { |
| 448 | return std::unique_ptr<SkMemoryStream>(this->onFork()); |
| 449 | } |
| 450 | #endif |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 451 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 452 | size_t getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 453 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 454 | const void* getMemoryBase() override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 455 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | private: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame^] | 457 | #ifndef SK_SUPPORT_LEGACY_STREAM_API |
| 458 | SkMemoryStream* onDuplicate() const override; |
| 459 | SkMemoryStream* onFork() const override; |
| 460 | #endif |
| 461 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 462 | sk_sp<SkData> fData; |
| 463 | size_t fOffset; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 464 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 465 | typedef SkStreamMemory INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 469 | |
alokp@chromium.org | f7751ae | 2012-07-17 19:10:36 +0000 | [diff] [blame] | 470 | class SK_API SkFILEWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 471 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 472 | SkFILEWStream(const char path[]); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 473 | ~SkFILEWStream() override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | |
| 475 | /** Returns true if the current path could be opened. |
| 476 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 477 | bool isValid() const { return fFILE != nullptr; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 479 | bool write(const void* buffer, size_t size) override; |
| 480 | void flush() override; |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 481 | void fsync(); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 482 | size_t bytesWritten() const override; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 483 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | private: |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 485 | FILE* fFILE; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 486 | |
| 487 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 488 | }; |
| 489 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 490 | class SK_API SkDynamicMemoryWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 491 | public: |
| 492 | SkDynamicMemoryWStream(); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 493 | ~SkDynamicMemoryWStream() override; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 494 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 495 | bool write(const void* buffer, size_t size) override; |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 496 | size_t bytesWritten() const override; |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 497 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 498 | bool read(void* buffer, size_t offset, size_t size); |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 499 | |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 500 | /** More efficient version of read(dst, 0, bytesWritten()). */ |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 501 | void copyTo(void* dst) const; |
Hal Canary | 6fee59b | 2017-05-23 11:47:05 -0400 | [diff] [blame] | 502 | bool writeToStream(SkWStream* dst) const; |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 503 | |
Hal Canary | dabe8ac | 2017-03-14 15:52:12 -0400 | [diff] [blame] | 504 | /** Equivalent to copyTo() followed by reset(), but may save memory use. */ |
| 505 | void copyToAndReset(void* dst); |
| 506 | |
Hal Canary | 6fee59b | 2017-05-23 11:47:05 -0400 | [diff] [blame] | 507 | /** Equivalent to writeToStream() followed by reset(), but may save memory use. */ |
| 508 | bool writeToAndReset(SkWStream* dst); |
| 509 | |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 510 | /** Return the contents as SkData, and then reset the stream. */ |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 511 | sk_sp<SkData> detachAsData(); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 512 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 513 | /** Reset, returning a reader stream with the current content. */ |
Hal Canary | 0b83319 | 2017-03-14 14:35:02 -0400 | [diff] [blame] | 514 | std::unique_ptr<SkStreamAsset> detachAsStream(); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 515 | |
| 516 | /** Reset the stream to its original, empty, state. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 517 | void reset(); |
| 518 | void padToAlign4(); |
| 519 | private: |
| 520 | struct Block; |
| 521 | Block* fHead; |
| 522 | Block* fTail; |
Mike Reed | 9c457ad | 2016-12-15 14:11:37 -0500 | [diff] [blame] | 523 | size_t fBytesWrittenBeforeTail; |
| 524 | |
| 525 | #ifdef SK_DEBUG |
| 526 | void validate() const; |
| 527 | #else |
| 528 | void validate() const {} |
| 529 | #endif |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 530 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 531 | // For access to the Block type. |
| 532 | friend class SkBlockMemoryStream; |
| 533 | friend class SkBlockMemoryRefCnt; |
| 534 | |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 535 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 538 | #endif |