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 | |
| 11 | #include "SkRefCnt.h" |
| 12 | #include "SkScalar.h" |
| 13 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 14 | class SkData; |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 15 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 16 | class SkStream; |
| 17 | class SkStreamRewindable; |
| 18 | class SkStreamSeekable; |
| 19 | class SkStreamAsset; |
| 20 | class SkStreamMemory; |
| 21 | |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 22 | /** |
| 23 | * SkStream -- abstraction for a source of bytes. Subclasses can be backed by |
| 24 | * memory, or a file, or something else. |
| 25 | * |
| 26 | * NOTE: |
| 27 | * |
| 28 | * Classic "streams" APIs are sort of async, in that on a request for N |
| 29 | * bytes, they may return fewer than N bytes on a given call, in which case |
| 30 | * the caller can "try again" to get more bytes, eventually (modulo an error) |
| 31 | * receiving their total N bytes. |
skia.committer@gmail.com | 2b34fe0 | 2013-05-08 07:01:40 +0000 | [diff] [blame] | 32 | * |
reed@google.com | 3b34505 | 2013-05-07 15:59:16 +0000 | [diff] [blame] | 33 | * Skia streams behave differently. They are effectively synchronous, and will |
| 34 | * always return all N bytes of the request if possible. If they return fewer |
| 35 | * (the read() call returns the number of bytes read) then that means there is |
| 36 | * no more data (at EOF or hit an error). The caller should *not* call again |
| 37 | * in hopes of fulfilling more of the request. |
| 38 | */ |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 39 | class SK_API SkStream : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | public: |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 41 | virtual ~SkStream() {} |
| 42 | |
| 43 | /** |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 44 | * Attempts to open the specified file, and return a stream to it (using |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 45 | * mmap if available). On success, the caller is responsible for deleting. |
| 46 | * On failure, returns NULL. |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 47 | */ |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 48 | static SkStreamAsset* NewFromFile(const char path[]); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 49 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 50 | /** Reads or skips size number of bytes. |
| 51 | * If buffer == NULL, skip size bytes, return how many were skipped. |
| 52 | * If buffer != NULL, copy size bytes into buffer, return how many were copied. |
| 53 | * @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer |
| 54 | * @param size the number of bytes to skip or copy |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 55 | * @return the number of bytes actually read. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 56 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | virtual size_t read(void* buffer, size_t size) = 0; |
| 58 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 59 | /** Skip size number of bytes. |
| 60 | * @return the actual number bytes that could be skipped. |
| 61 | */ |
| 62 | size_t skip(size_t size) { |
bungeman@google.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 63 | return this->read(NULL, size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 64 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 65 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 66 | /** Returns true when all the bytes in the stream have been read. |
| 67 | * This may return true early (when there are no more bytes to be read) |
| 68 | * or late (after the first unsuccessful read). |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 69 | */ |
bungeman@google.com | 0430692 | 2013-11-13 19:53:46 +0000 | [diff] [blame] | 70 | virtual bool isAtEnd() const = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
| 72 | int8_t readS8(); |
| 73 | int16_t readS16(); |
| 74 | int32_t readS32(); |
| 75 | |
| 76 | uint8_t readU8() { return (uint8_t)this->readS8(); } |
| 77 | uint16_t readU16() { return (uint16_t)this->readS16(); } |
| 78 | uint32_t readU32() { return (uint32_t)this->readS32(); } |
| 79 | |
| 80 | bool readBool() { return this->readU8() != 0; } |
| 81 | SkScalar readScalar(); |
| 82 | size_t readPackedUInt(); |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 83 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 84 | //SkStreamRewindable |
scroggo@google.com | 4d213ab | 2013-08-28 13:08:54 +0000 | [diff] [blame] | 85 | /** Rewinds to the beginning of the stream. Returns true if the stream is known |
| 86 | * to be at the beginning after this call returns. |
| 87 | */ |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 88 | virtual bool rewind() { return false; } |
| 89 | |
| 90 | /** Duplicates this stream. If this cannot be done, returns NULL. |
| 91 | * The returned stream will be positioned at the beginning of its data. |
| 92 | */ |
| 93 | virtual SkStreamRewindable* duplicate() const { return NULL; } |
| 94 | |
| 95 | //SkStreamSeekable |
| 96 | /** Returns true if this stream can report it's current position. */ |
| 97 | virtual bool hasPosition() const { return false; } |
| 98 | /** Returns the current position in the stream. If this cannot be done, returns 0. */ |
| 99 | virtual size_t getPosition() const { return 0; } |
| 100 | |
| 101 | /** Seeks to an absolute position in the stream. If this cannot be done, returns false. |
| 102 | * If an attempt is made to seek past the end of the stream, the position will be set |
| 103 | * to the end of the stream. |
| 104 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 105 | virtual bool seek(size_t /*position*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 106 | |
| 107 | /** Seeks to an relative offset in the stream. If this cannot be done, returns false. |
| 108 | * If an attempt is made to move to a position outside the stream, the position will be set |
| 109 | * to the closest point within the stream (beginning or end). |
| 110 | */ |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 111 | virtual bool move(long /*offset*/) { return false; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 112 | |
| 113 | /** Duplicates this stream. If this cannot be done, returns NULL. |
| 114 | * The returned stream will be positioned the same as this stream. |
| 115 | */ |
| 116 | virtual SkStreamSeekable* fork() const { return NULL; } |
| 117 | |
| 118 | //SkStreamAsset |
| 119 | /** Returns true if this stream can report it's total length. */ |
| 120 | virtual bool hasLength() const { return false; } |
| 121 | /** 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] | 122 | virtual size_t getLength() const { return 0; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 123 | |
| 124 | //SkStreamMemory |
| 125 | /** Returns the starting address for the data. If this cannot be done, returns NULL. */ |
| 126 | //TODO: replace with virtual const SkData* getData() |
| 127 | virtual const void* getMemoryBase() { return NULL; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 130 | /** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */ |
| 131 | class SK_API SkStreamRewindable : public SkStream { |
| 132 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 133 | bool rewind() override = 0; |
| 134 | SkStreamRewindable* duplicate() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */ |
| 138 | class SK_API SkStreamSeekable : public SkStreamRewindable { |
| 139 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 140 | SkStreamSeekable* duplicate() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 142 | bool hasPosition() const override { return true; } |
| 143 | size_t getPosition() const override = 0; |
| 144 | bool seek(size_t position) override = 0; |
| 145 | bool move(long offset) override = 0; |
| 146 | SkStreamSeekable* fork() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** SkStreamAsset is a SkStreamSeekable for which getLength is required. */ |
| 150 | class SK_API SkStreamAsset : public SkStreamSeekable { |
| 151 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 152 | SkStreamAsset* duplicate() const override = 0; |
| 153 | SkStreamAsset* fork() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 154 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 155 | bool hasLength() const override { return true; } |
| 156 | size_t getLength() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */ |
| 160 | class SK_API SkStreamMemory : public SkStreamAsset { |
| 161 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 162 | SkStreamMemory* duplicate() const override = 0; |
| 163 | SkStreamMemory* fork() const override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 164 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 165 | const void* getMemoryBase() override = 0; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 168 | class SK_API SkWStream : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | public: |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 170 | SK_DECLARE_INST_COUNT(SkWStream) |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 171 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | virtual ~SkWStream(); |
| 173 | |
| 174 | /** Called to write bytes to a SkWStream. Returns true on success |
| 175 | @param buffer the address of at least size bytes to be written to the stream |
| 176 | @param size The number of bytes in buffer to write to the stream |
| 177 | @return true on success |
| 178 | */ |
| 179 | virtual bool write(const void* buffer, size_t size) = 0; |
| 180 | virtual void newline(); |
| 181 | virtual void flush(); |
| 182 | |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 183 | virtual size_t bytesWritten() const = 0; |
| 184 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | // helpers |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 186 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | bool write8(U8CPU); |
| 188 | bool write16(U16CPU); |
| 189 | bool write32(uint32_t); |
| 190 | |
| 191 | bool writeText(const char text[]); |
| 192 | bool writeDecAsText(int32_t); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 193 | bool writeBigDecAsText(int64_t, int minDigits = 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | bool writeHexAsText(uint32_t, int minDigits = 0); |
| 195 | bool writeScalarAsText(SkScalar); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | bool writeBool(bool v) { return this->write8(v); } |
| 198 | bool writeScalar(SkScalar); |
| 199 | bool writePackedUInt(size_t); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 200 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 201 | bool writeStream(SkStream* input, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 202 | |
epoger@google.com | 8e3fb2d | 2013-03-20 15:56:03 +0000 | [diff] [blame] | 203 | /** |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 204 | * This returns the number of bytes in the stream required to store |
| 205 | * 'value'. |
| 206 | */ |
| 207 | static int SizeOfPackedUInt(size_t value); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | //////////////////////////////////////////////////////////////////////////////////////// |
| 211 | |
| 212 | #include "SkString.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 213 | #include <stdio.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | |
| 215 | struct SkFILE; |
| 216 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 217 | /** A stream that wraps a C FILE* file stream. */ |
| 218 | class SK_API SkFILEStream : public SkStreamAsset { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 220 | SK_DECLARE_INST_COUNT(SkFILEStream) |
| 221 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 222 | /** Initialize the stream by calling sk_fopen on the specified path. |
| 223 | * This internal stream will be closed in the destructor. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | */ |
| 225 | explicit SkFILEStream(const char path[] = NULL); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 226 | |
| 227 | enum Ownership { |
| 228 | kCallerPasses_Ownership, |
| 229 | kCallerRetains_Ownership |
| 230 | }; |
| 231 | /** Initialize the stream with an existing C file stream. |
| 232 | * While this stream exists, it assumes exclusive access to the C file stream. |
| 233 | * The C file stream will be closed in the destructor unless the caller specifies |
| 234 | * kCallerRetains_Ownership. |
| 235 | */ |
| 236 | explicit SkFILEStream(FILE* file, Ownership ownership = kCallerPasses_Ownership); |
| 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | virtual ~SkFILEStream(); |
| 239 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 240 | /** Returns true if the current path could be opened. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | bool isValid() const { return fFILE != NULL; } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 242 | |
| 243 | /** Close the current file, and open a new file with the specified path. |
| 244 | * If path is NULL, just close the current file. |
| 245 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | void setPath(const char path[]); |
| 247 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 248 | size_t read(void* buffer, size_t size) override; |
| 249 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 250 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 251 | bool rewind() override; |
| 252 | SkStreamAsset* duplicate() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 253 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 254 | size_t getPosition() const override; |
| 255 | bool seek(size_t position) override; |
| 256 | bool move(long offset) override; |
| 257 | SkStreamAsset* fork() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 258 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 259 | size_t getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 260 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 261 | const void* getMemoryBase() override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | |
| 263 | private: |
| 264 | SkFILE* fFILE; |
| 265 | SkString fName; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 266 | Ownership fOwnership; |
| 267 | // fData is lazilly initialized when needed. |
| 268 | mutable SkAutoTUnref<SkData> fData; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 269 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 270 | typedef SkStreamAsset INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 273 | class SK_API SkMemoryStream : public SkStreamMemory { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 275 | SK_DECLARE_INST_COUNT(SkMemoryStream) |
| 276 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | SkMemoryStream(); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 278 | |
| 279 | /** We allocate (and free) the memory. Write to it via getMemoryBase() */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | SkMemoryStream(size_t length); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 281 | |
| 282 | /** 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] | 283 | SkMemoryStream(const void* data, size_t length, bool copyData = false); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 284 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 285 | /** Use the specified data as the memory for this stream. |
| 286 | * The stream will call ref() on the data (assuming it is not NULL). |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 287 | */ |
| 288 | SkMemoryStream(SkData*); |
| 289 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | virtual ~SkMemoryStream(); |
| 291 | |
| 292 | /** Resets the stream to the specified data and length, |
| 293 | just like the constructor. |
| 294 | if copyData is true, the stream makes a private copy of the data |
| 295 | */ |
| 296 | virtual void setMemory(const void* data, size_t length, |
| 297 | bool copyData = false); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 298 | /** Replace any memory buffer with the specified buffer. The caller |
| 299 | must have allocated data with sk_malloc or sk_realloc, since it |
| 300 | will be freed with sk_free. |
| 301 | */ |
| 302 | void setMemoryOwned(const void* data, size_t length); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 303 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 304 | /** Return the stream's data in a SkData. |
| 305 | * The caller must call unref() when it is finished using the data. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 306 | */ |
| 307 | SkData* copyToData() const; |
| 308 | |
| 309 | /** |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 310 | * Use the specified data as the memory for this stream. |
| 311 | * The stream will call ref() on the data (assuming it is not NULL). |
| 312 | * The function returns the data parameter as a convenience. |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 313 | */ |
| 314 | SkData* setData(SkData*); |
| 315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | void skipToAlign4(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | const void* getAtPos(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | size_t peek() const { return fOffset; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 319 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 320 | size_t read(void* buffer, size_t size) override; |
| 321 | bool isAtEnd() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 322 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 323 | bool rewind() override; |
| 324 | SkMemoryStream* duplicate() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 325 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 326 | size_t getPosition() const override; |
| 327 | bool seek(size_t position) override; |
| 328 | bool move(long offset) override; |
| 329 | SkMemoryStream* fork() 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 getLength() const override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 332 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 333 | const void* getMemoryBase() override; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 334 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | private: |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 336 | SkData* fData; |
| 337 | size_t fOffset; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 338 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 339 | typedef SkStreamMemory INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 343 | |
alokp@chromium.org | f7751ae | 2012-07-17 19:10:36 +0000 | [diff] [blame] | 344 | class SK_API SkFILEWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 346 | SK_DECLARE_INST_COUNT(SkFILEWStream) |
| 347 | |
| 348 | SkFILEWStream(const char path[]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | virtual ~SkFILEWStream(); |
| 350 | |
| 351 | /** Returns true if the current path could be opened. |
| 352 | */ |
| 353 | bool isValid() const { return fFILE != NULL; } |
| 354 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 355 | bool write(const void* buffer, size_t size) override; |
| 356 | void flush() override; |
| 357 | size_t bytesWritten() const override; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 358 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 359 | private: |
| 360 | SkFILE* fFILE; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 361 | |
| 362 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | class SkMemoryWStream : public SkWStream { |
| 366 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 367 | SK_DECLARE_INST_COUNT(SkMemoryWStream) |
| 368 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | SkMemoryWStream(void* buffer, size_t size); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 370 | bool write(const void* buffer, size_t size) override; |
| 371 | size_t bytesWritten() const override { return fBytesWritten; } |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 372 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | private: |
| 374 | char* fBuffer; |
| 375 | size_t fMaxLength; |
| 376 | size_t fBytesWritten; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 377 | |
| 378 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 381 | class SK_API SkDynamicMemoryWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 382 | public: |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 383 | SK_DECLARE_INST_COUNT(SkDynamicMemoryWStream) |
| 384 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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; |
| 389 | size_t bytesWritten() const override { return fBytesWritten; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | // random access write |
| 391 | // modifies stream and returns true if offset + size is less than or equal to getOffset() |
| 392 | bool write(const void* buffer, size_t offset, size_t size); |
| 393 | bool read(void* buffer, size_t offset, size_t size); |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 394 | size_t getOffset() const { return fBytesWritten; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | |
| 396 | // copy what has been written to the stream into dst |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 397 | void copyTo(void* dst) const; |
halcanary | 7af2150 | 2015-02-23 12:17:59 -0800 | [diff] [blame] | 398 | void writeToStream(SkWStream* dst) const; |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 399 | |
| 400 | /** |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 401 | * Return a copy of the data written so far. This call is responsible for |
| 402 | * calling unref() when they are finished with the data. |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 403 | */ |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 404 | SkData* copyToData() const; |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 405 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 406 | /** Reset, returning a reader stream with the current content. */ |
bungeman@google.com | c29f3d8 | 2013-07-19 22:32:11 +0000 | [diff] [blame] | 407 | SkStreamAsset* detachAsStream(); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 408 | |
| 409 | /** Reset the stream to its original, empty, state. */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | void reset(); |
| 411 | void padToAlign4(); |
| 412 | private: |
| 413 | struct Block; |
| 414 | Block* fHead; |
| 415 | Block* fTail; |
| 416 | size_t fBytesWritten; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 417 | mutable SkData* fCopy; // is invalidated if we write after it is created |
| 418 | |
| 419 | void invalidateCopy(); |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 420 | |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 421 | // For access to the Block type. |
| 422 | friend class SkBlockMemoryStream; |
| 423 | friend class SkBlockMemoryRefCnt; |
| 424 | |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 425 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | |
reed@google.com | a718c5e | 2013-03-05 17:49:10 +0000 | [diff] [blame] | 429 | class SK_API SkDebugWStream : public SkWStream { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | public: |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 431 | SkDebugWStream() : fBytesWritten(0) {} |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 432 | SK_DECLARE_INST_COUNT(SkDebugWStream) |
| 433 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 434 | // overrides |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 435 | bool write(const void* buffer, size_t size) override; |
| 436 | void newline() override; |
| 437 | size_t bytesWritten() const override { return fBytesWritten; } |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 438 | |
| 439 | private: |
commit-bot@chromium.org | 490fb6b | 2014-03-06 17:16:26 +0000 | [diff] [blame] | 440 | size_t fBytesWritten; |
reed@google.com | 3b42998 | 2012-06-26 15:30:08 +0000 | [diff] [blame] | 441 | typedef SkWStream INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | // for now |
| 445 | typedef SkFILEStream SkURLStream; |
| 446 | |
| 447 | #endif |