blob: 9205d104a305711a7a2ee8f4c1d1a319ee41c278 [file] [log] [blame]
scroggo@google.com83fd2c72013-09-26 21:35:39 +00001/*
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
8#include "SkFrontBufferedStream.h"
scroggo@google.com09a53832013-11-12 20:53:05 +00009#include "SkStream.h"
10#include "SkTemplates.h"
11
12class FrontBufferedStream : public SkStreamRewindable {
13public:
Mike Reed98c5d922017-09-15 21:39:47 -040014 // Called by Make.
15 FrontBufferedStream(std::unique_ptr<SkStream>, size_t bufferSize);
scroggo@google.com09a53832013-11-12 20:53:05 +000016
mtklein36352bf2015-03-25 18:17:31 -070017 size_t read(void* buffer, size_t size) override;
scroggo@google.com09a53832013-11-12 20:53:05 +000018
scroggod61c3842015-12-07 11:37:13 -080019 size_t peek(void* buffer, size_t size) const override;
scroggo028a4132015-04-02 13:19:51 -070020
mtklein36352bf2015-03-25 18:17:31 -070021 bool isAtEnd() const override;
scroggo@google.com09a53832013-11-12 20:53:05 +000022
mtklein36352bf2015-03-25 18:17:31 -070023 bool rewind() override;
scroggo@google.com09a53832013-11-12 20:53:05 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 bool hasLength() const override { return fHasLength; }
scroggo@google.com09a53832013-11-12 20:53:05 +000026
mtklein36352bf2015-03-25 18:17:31 -070027 size_t getLength() const override { return fLength; }
scroggo@google.com09a53832013-11-12 20:53:05 +000028
scroggo@google.com09a53832013-11-12 20:53:05 +000029private:
Mike Reed98c5d922017-09-15 21:39:47 -040030 SkStreamRewindable* onDuplicate() const override { return nullptr; }
Mike Reed98c5d922017-09-15 21:39:47 -040031
Ben Wagner145dbcd2016-11-03 14:40:50 -040032 std::unique_ptr<SkStream> fStream;
33 const bool fHasLength;
34 const size_t fLength;
scroggo@google.com09a53832013-11-12 20:53:05 +000035 // Current offset into the stream. Always >= 0.
Ben Wagner145dbcd2016-11-03 14:40:50 -040036 size_t fOffset;
scroggo@google.com09a53832013-11-12 20:53:05 +000037 // Amount that has been buffered by calls to read. Will always be less than
38 // fBufferSize.
Ben Wagner145dbcd2016-11-03 14:40:50 -040039 size_t fBufferedSoFar;
scroggo@google.com09a53832013-11-12 20:53:05 +000040 // Total size of the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -040041 const size_t fBufferSize;
scroggo@google.com09a53832013-11-12 20:53:05 +000042 // FIXME: SkAutoTMalloc throws on failure. Instead, Create should return a
halcanary96fcdcc2015-08-27 07:41:13 -070043 // nullptr stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -040044 SkAutoTMalloc<char> fBuffer;
scroggo@google.com09a53832013-11-12 20:53:05 +000045
46 // Read up to size bytes from already buffered data, and copy to
halcanary96fcdcc2015-08-27 07:41:13 -070047 // dst, if non-nullptr. Updates fOffset. Assumes that fOffset is less
scroggo@google.com09a53832013-11-12 20:53:05 +000048 // than fBufferedSoFar.
49 size_t readFromBuffer(char* dst, size_t size);
50
51 // Buffer up to size bytes from the stream, and copy to dst if non-
halcanary96fcdcc2015-08-27 07:41:13 -070052 // nullptr. Updates fOffset and fBufferedSoFar. Assumes that fOffset is
scroggo@google.com09a53832013-11-12 20:53:05 +000053 // less than fBufferedSoFar, and size is greater than 0.
54 size_t bufferAndWriteTo(char* dst, size_t size);
55
56 // Read up to size bytes directly from the stream and into dst if non-
halcanary96fcdcc2015-08-27 07:41:13 -070057 // nullptr. Updates fOffset. Assumes fOffset is at or beyond the buffered
scroggo@google.com09a53832013-11-12 20:53:05 +000058 // data, and size is greater than 0.
59 size_t readDirectlyFromStream(char* dst, size_t size);
60
61 typedef SkStream INHERITED;
62};
scroggo@google.com83fd2c72013-09-26 21:35:39 +000063
Mike Reed98c5d922017-09-15 21:39:47 -040064std::unique_ptr<SkStreamRewindable> SkFrontBufferedStream::Make(std::unique_ptr<SkStream> stream,
65 size_t bufferSize) {
66 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070067 return nullptr;
scroggo@google.com83fd2c72013-09-26 21:35:39 +000068 }
Mike Reed98c5d922017-09-15 21:39:47 -040069 return std::unique_ptr<SkStreamRewindable>(new FrontBufferedStream(std::move(stream),
70 bufferSize));
scroggo@google.com83fd2c72013-09-26 21:35:39 +000071}
72
Mike Reed98c5d922017-09-15 21:39:47 -040073FrontBufferedStream::FrontBufferedStream(std::unique_ptr<SkStream> stream, size_t bufferSize)
74 : fStream(std::move(stream))
75 , fHasLength(fStream->hasPosition() && fStream->hasLength())
76 , fLength(fStream->getLength() - fStream->getPosition())
scroggo@google.com83fd2c72013-09-26 21:35:39 +000077 , fOffset(0)
78 , fBufferedSoFar(0)
79 , fBufferSize(bufferSize)
80 , fBuffer(bufferSize) {}
81
scroggo@google.com09a53832013-11-12 20:53:05 +000082bool FrontBufferedStream::isAtEnd() const {
scroggo@google.com83fd2c72013-09-26 21:35:39 +000083 if (fOffset < fBufferedSoFar) {
84 // Even if the underlying stream is at the end, this stream has been
85 // rewound after buffering, so it is not at the end.
86 return false;
87 }
88
89 return fStream->isAtEnd();
90}
91
scroggo@google.com09a53832013-11-12 20:53:05 +000092bool FrontBufferedStream::rewind() {
scroggo@google.com83fd2c72013-09-26 21:35:39 +000093 // Only allow a rewind if we have not exceeded the buffer.
94 if (fOffset <= fBufferSize) {
95 fOffset = 0;
96 return true;
97 }
98 return false;
99}
100
scroggo@google.com09a53832013-11-12 20:53:05 +0000101size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000102 SkASSERT(fOffset < fBufferedSoFar);
103 // Some data has already been copied to fBuffer. Read up to the
104 // lesser of the size requested and the remainder of the buffered
105 // data.
106 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset);
halcanary96fcdcc2015-08-27 07:41:13 -0700107 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000108 memcpy(dst, fBuffer + fOffset, bytesToCopy);
109 }
110
111 // Update fOffset to the new position. It is guaranteed to be
112 // within the buffered data.
113 fOffset += bytesToCopy;
114 SkASSERT(fOffset <= fBufferedSoFar);
115
116 return bytesToCopy;
117}
118
scroggo@google.com09a53832013-11-12 20:53:05 +0000119size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000120 SkASSERT(size > 0);
121 SkASSERT(fOffset >= fBufferedSoFar);
mtklein1f66e452014-10-21 07:12:52 -0700122 SkASSERT(fBuffer);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000123 // Data needs to be buffered. Buffer up to the lesser of the size requested
124 // and the remainder of the max buffer size.
125 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar);
126 char* buffer = fBuffer + fOffset;
127 const size_t buffered = fStream->read(buffer, bytesToBuffer);
128
129 fBufferedSoFar += buffered;
130 fOffset = fBufferedSoFar;
131 SkASSERT(fBufferedSoFar <= fBufferSize);
132
133 // Copy the buffer to the destination buffer and update the amount read.
halcanary96fcdcc2015-08-27 07:41:13 -0700134 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000135 memcpy(dst, buffer, buffered);
136 }
137
138 return buffered;
139}
140
scroggo@google.com09a53832013-11-12 20:53:05 +0000141size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000142 SkASSERT(size > 0);
143 // If we get here, we have buffered all that can be buffered.
144 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
145
146 const size_t bytesReadDirectly = fStream->read(dst, size);
147 fOffset += bytesReadDirectly;
148
149 // If we have read past the end of the buffer, rewinding is no longer
150 // supported, so we can go ahead and free the memory.
151 if (bytesReadDirectly > 0) {
mtklein18300a32016-03-16 13:53:35 -0700152 sk_free(fBuffer.release());
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000153 }
154
155 return bytesReadDirectly;
156}
157
scroggod61c3842015-12-07 11:37:13 -0800158size_t FrontBufferedStream::peek(void* dst, size_t size) const {
scroggo028a4132015-04-02 13:19:51 -0700159 // Keep track of the offset so we can return to it.
160 const size_t start = fOffset;
scroggod61c3842015-12-07 11:37:13 -0800161
162 if (start >= fBufferSize) {
163 // This stream is not able to buffer.
164 return 0;
scroggo028a4132015-04-02 13:19:51 -0700165 }
scroggod61c3842015-12-07 11:37:13 -0800166
167 size = SkTMin(size, fBufferSize - start);
scroggo028a4132015-04-02 13:19:51 -0700168 FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this);
scroggod61c3842015-12-07 11:37:13 -0800169 const size_t bytesRead = nonConstThis->read(dst, size);
scroggo028a4132015-04-02 13:19:51 -0700170 nonConstThis->fOffset = start;
scroggod61c3842015-12-07 11:37:13 -0800171 return bytesRead;
scroggo028a4132015-04-02 13:19:51 -0700172}
173
scroggo@google.com09a53832013-11-12 20:53:05 +0000174size_t FrontBufferedStream::read(void* voidDst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000175 // Cast voidDst to a char* for easy addition.
176 char* dst = reinterpret_cast<char*>(voidDst);
177 SkDEBUGCODE(const size_t totalSize = size;)
178 const size_t start = fOffset;
179
180 // First, read any data that was previously buffered.
181 if (fOffset < fBufferedSoFar) {
182 const size_t bytesCopied = this->readFromBuffer(dst, size);
183
184 // Update the remaining number of bytes needed to read
185 // and the destination buffer.
186 size -= bytesCopied;
187 SkASSERT(size + (fOffset - start) == totalSize);
halcanary96fcdcc2015-08-27 07:41:13 -0700188 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000189 dst += bytesCopied;
190 }
191 }
192
193 // Buffer any more data that should be buffered, and copy it to the
194 // destination.
scroggodd5a1e02014-10-21 08:06:05 -0700195 if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000196 const size_t buffered = this->bufferAndWriteTo(dst, size);
197
198 // Update the remaining number of bytes needed to read
199 // and the destination buffer.
200 size -= buffered;
201 SkASSERT(size + (fOffset - start) == totalSize);
halcanary96fcdcc2015-08-27 07:41:13 -0700202 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000203 dst += buffered;
204 }
205 }
206
207 if (size > 0 && !fStream->isAtEnd()) {
scroggo@google.comfd67f2f2013-09-26 21:49:46 +0000208 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStream(dst, size);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000209 SkDEBUGCODE(size -= bytesReadDirectly;)
210 SkASSERT(size + (fOffset - start) == totalSize);
211 }
212
213 return fOffset - start;
214}