scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 8 | #include "include/codec/SkCodec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkStream.h" |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 10 | #include "FrontBufferedStream.h" |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 11 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | |
| 14 | namespace { |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 15 | class FrontBufferedStream : public SkStreamRewindable { |
| 16 | public: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 17 | // Called by Make. |
| 18 | FrontBufferedStream(std::unique_ptr<SkStream>, size_t bufferSize); |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 19 | ~FrontBufferedStream() override; |
| 20 | |
| 21 | bool failedToAllocateBuffer() const { return !fBuffer; } |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | size_t read(void* buffer, size_t size) override; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 24 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 25 | size_t peek(void* buffer, size_t size) const override; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | bool isAtEnd() const override; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | bool rewind() override; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | bool hasLength() const override { return fHasLength; } |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | size_t getLength() const override { return fLength; } |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 34 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 35 | private: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 36 | SkStreamRewindable* onDuplicate() const override { return nullptr; } |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 37 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkStream> fStream; |
| 39 | const bool fHasLength; |
| 40 | const size_t fLength; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 41 | // Current offset into the stream. Always >= 0. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 42 | size_t fOffset; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 43 | // Amount that has been buffered by calls to read. Will always be less than |
| 44 | // fBufferSize. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 45 | size_t fBufferedSoFar; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 46 | // Total size of the buffer. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 47 | const size_t fBufferSize; |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 48 | char* fBuffer; |
| 49 | static constexpr size_t kStorageSize = SkCodec::MinBufferedBytesNeeded(); |
| 50 | char fStorage[kStorageSize]; |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 51 | |
| 52 | // Read up to size bytes from already buffered data, and copy to |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | // dst, if non-nullptr. Updates fOffset. Assumes that fOffset is less |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 54 | // than fBufferedSoFar. |
| 55 | size_t readFromBuffer(char* dst, size_t size); |
| 56 | |
| 57 | // Buffer up to size bytes from the stream, and copy to dst if non- |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | // nullptr. Updates fOffset and fBufferedSoFar. Assumes that fOffset is |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 59 | // less than fBufferedSoFar, and size is greater than 0. |
| 60 | size_t bufferAndWriteTo(char* dst, size_t size); |
| 61 | |
| 62 | // Read up to size bytes directly from the stream and into dst if non- |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | // nullptr. Updates fOffset. Assumes fOffset is at or beyond the buffered |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 64 | // data, and size is greater than 0. |
| 65 | size_t readDirectlyFromStream(char* dst, size_t size); |
| 66 | |
| 67 | typedef SkStream INHERITED; |
| 68 | }; |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 69 | } // anonymous namespace |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 70 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 71 | namespace android { |
| 72 | namespace skia { |
| 73 | |
| 74 | std::unique_ptr<SkStreamRewindable> FrontBufferedStream::Make(std::unique_ptr<SkStream> stream, |
| 75 | size_t bufferSize) { |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 76 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | return nullptr; |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 78 | } |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 79 | auto frontBufferedStream = std::unique_ptr<::FrontBufferedStream>( |
| 80 | new ::FrontBufferedStream(std::move(stream), bufferSize)); |
| 81 | if (frontBufferedStream->failedToAllocateBuffer()) { |
| 82 | return nullptr; |
| 83 | } |
Leon Scroggins III | 513720f | 2020-04-23 12:35:30 -0400 | [diff] [blame] | 84 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 85 | // Work around a warning regarding a copy on older compilers. |
| 86 | return std::move(frontBufferedStream); |
| 87 | } |
| 88 | } // namespace skia |
| 89 | } // namespace android |
| 90 | |
| 91 | namespace { |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 92 | FrontBufferedStream::FrontBufferedStream(std::unique_ptr<SkStream> stream, size_t bufferSize) |
| 93 | : fStream(std::move(stream)) |
| 94 | , fHasLength(fStream->hasPosition() && fStream->hasLength()) |
| 95 | , fLength(fStream->getLength() - fStream->getPosition()) |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 96 | , fOffset(0) |
| 97 | , fBufferedSoFar(0) |
| 98 | , fBufferSize(bufferSize) |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 99 | , fBuffer(bufferSize <= kStorageSize ? fStorage |
| 100 | : reinterpret_cast<char*>(malloc(bufferSize))) {} |
| 101 | |
| 102 | FrontBufferedStream::~FrontBufferedStream() { |
| 103 | if (fBuffer != fStorage) { |
| 104 | free(fBuffer); |
| 105 | } |
| 106 | } |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 107 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 108 | bool FrontBufferedStream::isAtEnd() const { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 109 | if (fOffset < fBufferedSoFar) { |
| 110 | // Even if the underlying stream is at the end, this stream has been |
| 111 | // rewound after buffering, so it is not at the end. |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return fStream->isAtEnd(); |
| 116 | } |
| 117 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 118 | bool FrontBufferedStream::rewind() { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 119 | // Only allow a rewind if we have not exceeded the buffer. |
| 120 | if (fOffset <= fBufferSize) { |
| 121 | fOffset = 0; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 127 | size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 128 | SkASSERT(fOffset < fBufferedSoFar); |
| 129 | // Some data has already been copied to fBuffer. Read up to the |
| 130 | // lesser of the size requested and the remainder of the buffered |
| 131 | // data. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 132 | const size_t bytesToCopy = std::min(size, fBufferedSoFar - fOffset); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | if (dst != nullptr) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 134 | memcpy(dst, fBuffer + fOffset, bytesToCopy); |
| 135 | } |
| 136 | |
| 137 | // Update fOffset to the new position. It is guaranteed to be |
| 138 | // within the buffered data. |
| 139 | fOffset += bytesToCopy; |
| 140 | SkASSERT(fOffset <= fBufferedSoFar); |
| 141 | |
| 142 | return bytesToCopy; |
| 143 | } |
| 144 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 145 | size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 146 | SkASSERT(size > 0); |
| 147 | SkASSERT(fOffset >= fBufferedSoFar); |
mtklein | 1f66e45 | 2014-10-21 07:12:52 -0700 | [diff] [blame] | 148 | SkASSERT(fBuffer); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 149 | // Data needs to be buffered. Buffer up to the lesser of the size requested |
| 150 | // and the remainder of the max buffer size. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 151 | const size_t bytesToBuffer = std::min(size, fBufferSize - fBufferedSoFar); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 152 | char* buffer = fBuffer + fOffset; |
| 153 | const size_t buffered = fStream->read(buffer, bytesToBuffer); |
| 154 | |
| 155 | fBufferedSoFar += buffered; |
| 156 | fOffset = fBufferedSoFar; |
| 157 | SkASSERT(fBufferedSoFar <= fBufferSize); |
| 158 | |
| 159 | // Copy the buffer to the destination buffer and update the amount read. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | if (dst != nullptr) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 161 | memcpy(dst, buffer, buffered); |
| 162 | } |
| 163 | |
| 164 | return buffered; |
| 165 | } |
| 166 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 167 | size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 168 | SkASSERT(size > 0); |
| 169 | // If we get here, we have buffered all that can be buffered. |
| 170 | SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize); |
| 171 | |
| 172 | const size_t bytesReadDirectly = fStream->read(dst, size); |
| 173 | fOffset += bytesReadDirectly; |
| 174 | |
| 175 | // If we have read past the end of the buffer, rewinding is no longer |
| 176 | // supported, so we can go ahead and free the memory. |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 177 | if (bytesReadDirectly > 0 && fBuffer != fStorage) { |
| 178 | free(fBuffer); |
| 179 | fBuffer = nullptr; |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return bytesReadDirectly; |
| 183 | } |
| 184 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 185 | size_t FrontBufferedStream::peek(void* dst, size_t size) const { |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 186 | // Keep track of the offset so we can return to it. |
| 187 | const size_t start = fOffset; |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 188 | |
| 189 | if (start >= fBufferSize) { |
| 190 | // This stream is not able to buffer. |
| 191 | return 0; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 192 | } |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 193 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 194 | size = std::min(size, fBufferSize - start); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 195 | FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 196 | const size_t bytesRead = nonConstThis->read(dst, size); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 197 | nonConstThis->fOffset = start; |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 198 | return bytesRead; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 199 | } |
| 200 | |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 201 | size_t FrontBufferedStream::read(void* voidDst, size_t size) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 202 | // Cast voidDst to a char* for easy addition. |
| 203 | char* dst = reinterpret_cast<char*>(voidDst); |
| 204 | SkDEBUGCODE(const size_t totalSize = size;) |
| 205 | const size_t start = fOffset; |
| 206 | |
| 207 | // First, read any data that was previously buffered. |
| 208 | if (fOffset < fBufferedSoFar) { |
| 209 | const size_t bytesCopied = this->readFromBuffer(dst, size); |
| 210 | |
| 211 | // Update the remaining number of bytes needed to read |
| 212 | // and the destination buffer. |
| 213 | size -= bytesCopied; |
| 214 | SkASSERT(size + (fOffset - start) == totalSize); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | if (dst != nullptr) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 216 | dst += bytesCopied; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // Buffer any more data that should be buffered, and copy it to the |
| 221 | // destination. |
scroggo | dd5a1e0 | 2014-10-21 08:06:05 -0700 | [diff] [blame] | 222 | if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 223 | const size_t buffered = this->bufferAndWriteTo(dst, size); |
| 224 | |
| 225 | // Update the remaining number of bytes needed to read |
| 226 | // and the destination buffer. |
| 227 | size -= buffered; |
| 228 | SkASSERT(size + (fOffset - start) == totalSize); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 229 | if (dst != nullptr) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 230 | dst += buffered; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (size > 0 && !fStream->isAtEnd()) { |
scroggo@google.com | fd67f2f | 2013-09-26 21:49:46 +0000 | [diff] [blame] | 235 | SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStream(dst, size); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 236 | SkDEBUGCODE(size -= bytesReadDirectly;) |
| 237 | SkASSERT(size + (fOffset - start) == totalSize); |
| 238 | } |
| 239 | |
| 240 | return fOffset - start; |
| 241 | } |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 242 | } // anonymous namespace |