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