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