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