blob: a0dbeb6a7a0db28d936b323368be5faa9493d5bc [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:
14 // Called by Create.
15 FrontBufferedStream(SkStream*, size_t bufferSize);
16
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
halcanary96fcdcc2015-08-27 07:41:13 -070029 SkStreamRewindable* duplicate() const override { return nullptr; }
scroggo@google.com09a53832013-11-12 20:53:05 +000030
31private:
scroggoa1193e42015-01-21 12:09:53 -080032 SkAutoTDelete<SkStream> fStream;
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000033 const bool fHasLength;
34 const size_t fLength;
scroggo@google.com09a53832013-11-12 20:53:05 +000035 // Current offset into the stream. Always >= 0.
36 size_t fOffset;
37 // Amount that has been buffered by calls to read. Will always be less than
38 // fBufferSize.
39 size_t fBufferedSoFar;
40 // Total size of the buffer.
41 const size_t fBufferSize;
42 // FIXME: SkAutoTMalloc throws on failure. Instead, Create should return a
halcanary96fcdcc2015-08-27 07:41:13 -070043 // nullptr stream.
scroggo@google.com09a53832013-11-12 20:53:05 +000044 SkAutoTMalloc<char> fBuffer;
45
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
64SkStreamRewindable* SkFrontBufferedStream::Create(SkStream* stream, size_t bufferSize) {
halcanary96fcdcc2015-08-27 07:41:13 -070065 if (nullptr == stream) {
66 return nullptr;
scroggo@google.com83fd2c72013-09-26 21:35:39 +000067 }
halcanary385fe4d2015-08-26 13:07:48 -070068 return new FrontBufferedStream(stream, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000069}
70
scroggo@google.com09a53832013-11-12 20:53:05 +000071FrontBufferedStream::FrontBufferedStream(SkStream* stream, size_t bufferSize)
scroggoa1193e42015-01-21 12:09:53 -080072 : fStream(stream)
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000073 , fHasLength(stream->hasPosition() && stream->hasLength())
74 , fLength(stream->getLength() - stream->getPosition())
scroggo@google.com83fd2c72013-09-26 21:35:39 +000075 , fOffset(0)
76 , fBufferedSoFar(0)
77 , fBufferSize(bufferSize)
78 , fBuffer(bufferSize) {}
79
scroggo@google.com09a53832013-11-12 20:53:05 +000080bool FrontBufferedStream::isAtEnd() const {
scroggo@google.com83fd2c72013-09-26 21:35:39 +000081 if (fOffset < fBufferedSoFar) {
82 // Even if the underlying stream is at the end, this stream has been
83 // rewound after buffering, so it is not at the end.
84 return false;
85 }
86
87 return fStream->isAtEnd();
88}
89
scroggo@google.com09a53832013-11-12 20:53:05 +000090bool FrontBufferedStream::rewind() {
scroggo@google.com83fd2c72013-09-26 21:35:39 +000091 // Only allow a rewind if we have not exceeded the buffer.
92 if (fOffset <= fBufferSize) {
93 fOffset = 0;
94 return true;
95 }
96 return false;
97}
98
scroggo@google.com09a53832013-11-12 20:53:05 +000099size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000100 SkASSERT(fOffset < fBufferedSoFar);
101 // Some data has already been copied to fBuffer. Read up to the
102 // lesser of the size requested and the remainder of the buffered
103 // data.
104 const size_t bytesToCopy = SkTMin(size, fBufferedSoFar - fOffset);
halcanary96fcdcc2015-08-27 07:41:13 -0700105 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000106 memcpy(dst, fBuffer + fOffset, bytesToCopy);
107 }
108
109 // Update fOffset to the new position. It is guaranteed to be
110 // within the buffered data.
111 fOffset += bytesToCopy;
112 SkASSERT(fOffset <= fBufferedSoFar);
113
114 return bytesToCopy;
115}
116
scroggo@google.com09a53832013-11-12 20:53:05 +0000117size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000118 SkASSERT(size > 0);
119 SkASSERT(fOffset >= fBufferedSoFar);
mtklein1f66e452014-10-21 07:12:52 -0700120 SkASSERT(fBuffer);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000121 // Data needs to be buffered. Buffer up to the lesser of the size requested
122 // and the remainder of the max buffer size.
123 const size_t bytesToBuffer = SkTMin(size, fBufferSize - fBufferedSoFar);
124 char* buffer = fBuffer + fOffset;
125 const size_t buffered = fStream->read(buffer, bytesToBuffer);
126
127 fBufferedSoFar += buffered;
128 fOffset = fBufferedSoFar;
129 SkASSERT(fBufferedSoFar <= fBufferSize);
130
131 // Copy the buffer to the destination buffer and update the amount read.
halcanary96fcdcc2015-08-27 07:41:13 -0700132 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000133 memcpy(dst, buffer, buffered);
134 }
135
136 return buffered;
137}
138
scroggo@google.com09a53832013-11-12 20:53:05 +0000139size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000140 SkASSERT(size > 0);
141 // If we get here, we have buffered all that can be buffered.
142 SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
143
144 const size_t bytesReadDirectly = fStream->read(dst, size);
145 fOffset += bytesReadDirectly;
146
147 // If we have read past the end of the buffer, rewinding is no longer
148 // supported, so we can go ahead and free the memory.
149 if (bytesReadDirectly > 0) {
mtklein1f66e452014-10-21 07:12:52 -0700150 sk_free(fBuffer.detach());
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000151 }
152
153 return bytesReadDirectly;
154}
155
scroggod61c3842015-12-07 11:37:13 -0800156size_t FrontBufferedStream::peek(void* dst, size_t size) const {
scroggo028a4132015-04-02 13:19:51 -0700157 // Keep track of the offset so we can return to it.
158 const size_t start = fOffset;
scroggod61c3842015-12-07 11:37:13 -0800159
160 if (start >= fBufferSize) {
161 // This stream is not able to buffer.
162 return 0;
scroggo028a4132015-04-02 13:19:51 -0700163 }
scroggod61c3842015-12-07 11:37:13 -0800164
165 size = SkTMin(size, fBufferSize - start);
scroggo028a4132015-04-02 13:19:51 -0700166 FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this);
scroggod61c3842015-12-07 11:37:13 -0800167 const size_t bytesRead = nonConstThis->read(dst, size);
scroggo028a4132015-04-02 13:19:51 -0700168 nonConstThis->fOffset = start;
scroggod61c3842015-12-07 11:37:13 -0800169 return bytesRead;
scroggo028a4132015-04-02 13:19:51 -0700170}
171
scroggo@google.com09a53832013-11-12 20:53:05 +0000172size_t FrontBufferedStream::read(void* voidDst, size_t size) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000173 // Cast voidDst to a char* for easy addition.
174 char* dst = reinterpret_cast<char*>(voidDst);
175 SkDEBUGCODE(const size_t totalSize = size;)
176 const size_t start = fOffset;
177
178 // First, read any data that was previously buffered.
179 if (fOffset < fBufferedSoFar) {
180 const size_t bytesCopied = this->readFromBuffer(dst, size);
181
182 // Update the remaining number of bytes needed to read
183 // and the destination buffer.
184 size -= bytesCopied;
185 SkASSERT(size + (fOffset - start) == totalSize);
halcanary96fcdcc2015-08-27 07:41:13 -0700186 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000187 dst += bytesCopied;
188 }
189 }
190
191 // Buffer any more data that should be buffered, and copy it to the
192 // destination.
scroggodd5a1e02014-10-21 08:06:05 -0700193 if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000194 const size_t buffered = this->bufferAndWriteTo(dst, size);
195
196 // Update the remaining number of bytes needed to read
197 // and the destination buffer.
198 size -= buffered;
199 SkASSERT(size + (fOffset - start) == totalSize);
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (dst != nullptr) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000201 dst += buffered;
202 }
203 }
204
205 if (size > 0 && !fStream->isAtEnd()) {
scroggo@google.comfd67f2f2013-09-26 21:49:46 +0000206 SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStream(dst, size);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000207 SkDEBUGCODE(size -= bytesReadDirectly;)
208 SkASSERT(size + (fOffset - start) == totalSize);
209 }
210
211 return fOffset - start;
212}