2013-09-26 21:35:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
#include "include/codec/SkCodec.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkStream.h"
|
2020-04-24 17:00:48 +00:00
|
|
|
#include "FrontBufferedStream.h"
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
#include <algorithm>
|
2020-08-03 17:21:46 +00:00
|
|
|
#include <memory>
|
2020-04-24 17:00:48 +00:00
|
|
|
|
|
|
|
namespace {
|
2013-11-12 20:53:05 +00:00
|
|
|
class FrontBufferedStream : public SkStreamRewindable {
|
|
|
|
public:
|
2017-09-16 01:39:47 +00:00
|
|
|
// Called by Make.
|
|
|
|
FrontBufferedStream(std::unique_ptr<SkStream>, size_t bufferSize);
|
2020-04-24 17:00:48 +00:00
|
|
|
~FrontBufferedStream() override;
|
|
|
|
|
|
|
|
bool failedToAllocateBuffer() const { return !fBuffer; }
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void* buffer, size_t size) override;
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2015-12-07 19:37:13 +00:00
|
|
|
size_t peek(void* buffer, size_t size) const override;
|
2015-04-02 20:19:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isAtEnd() const override;
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool rewind() override;
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool hasLength() const override { return fHasLength; }
|
2013-11-12 20:53:05 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t getLength() const override { return fLength; }
|
2013-11-12 20:53:05 +00:00
|
|
|
|
|
|
|
private:
|
2017-09-16 01:39:47 +00:00
|
|
|
SkStreamRewindable* onDuplicate() const override { return nullptr; }
|
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
std::unique_ptr<SkStream> fStream;
|
|
|
|
const bool fHasLength;
|
|
|
|
const size_t fLength;
|
2013-11-12 20:53:05 +00:00
|
|
|
// Current offset into the stream. Always >= 0.
|
2021-10-08 22:48:26 +00:00
|
|
|
size_t fOffset;
|
2013-11-12 20:53:05 +00:00
|
|
|
// Amount that has been buffered by calls to read. Will always be less than
|
|
|
|
// fBufferSize.
|
2021-10-08 22:48:26 +00:00
|
|
|
size_t fBufferedSoFar;
|
2013-11-12 20:53:05 +00:00
|
|
|
// Total size of the buffer.
|
2021-10-08 22:48:26 +00:00
|
|
|
const size_t fBufferSize;
|
|
|
|
char* fBuffer;
|
|
|
|
inline static constexpr size_t kStorageSize = SkCodec::MinBufferedBytesNeeded();
|
|
|
|
char fStorage[kStorageSize];
|
2013-11-12 20:53:05 +00:00
|
|
|
|
|
|
|
// Read up to size bytes from already buffered data, and copy to
|
2015-08-27 14:41:13 +00:00
|
|
|
// dst, if non-nullptr. Updates fOffset. Assumes that fOffset is less
|
2013-11-12 20:53:05 +00:00
|
|
|
// than fBufferedSoFar.
|
|
|
|
size_t readFromBuffer(char* dst, size_t size);
|
|
|
|
|
|
|
|
// Buffer up to size bytes from the stream, and copy to dst if non-
|
2015-08-27 14:41:13 +00:00
|
|
|
// nullptr. Updates fOffset and fBufferedSoFar. Assumes that fOffset is
|
2013-11-12 20:53:05 +00:00
|
|
|
// less than fBufferedSoFar, and size is greater than 0.
|
|
|
|
size_t bufferAndWriteTo(char* dst, size_t size);
|
|
|
|
|
|
|
|
// Read up to size bytes directly from the stream and into dst if non-
|
2015-08-27 14:41:13 +00:00
|
|
|
// nullptr. Updates fOffset. Assumes fOffset is at or beyond the buffered
|
2013-11-12 20:53:05 +00:00
|
|
|
// data, and size is greater than 0.
|
|
|
|
size_t readDirectlyFromStream(char* dst, size_t size);
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkStream;
|
2013-11-12 20:53:05 +00:00
|
|
|
};
|
2020-04-24 17:00:48 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace skia {
|
2013-09-26 21:35:39 +00:00
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
std::unique_ptr<SkStreamRewindable> FrontBufferedStream::Make(std::unique_ptr<SkStream> stream,
|
|
|
|
size_t bufferSize) {
|
2017-09-16 01:39:47 +00:00
|
|
|
if (!stream) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
2020-08-03 17:21:46 +00:00
|
|
|
auto frontBufferedStream = std::make_unique<::FrontBufferedStream>(
|
|
|
|
std::move(stream), bufferSize);
|
2020-04-24 17:00:48 +00:00
|
|
|
if (frontBufferedStream->failedToAllocateBuffer()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work around a warning regarding a copy on older compilers.
|
|
|
|
return std::move(frontBufferedStream);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
2020-04-24 17:00:48 +00:00
|
|
|
} // namespace skia
|
|
|
|
} // namespace android
|
2013-09-26 21:35:39 +00:00
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
namespace {
|
2017-09-16 01:39:47 +00:00
|
|
|
FrontBufferedStream::FrontBufferedStream(std::unique_ptr<SkStream> stream, size_t bufferSize)
|
|
|
|
: fStream(std::move(stream))
|
|
|
|
, fHasLength(fStream->hasPosition() && fStream->hasLength())
|
|
|
|
, fLength(fStream->getLength() - fStream->getPosition())
|
2013-09-26 21:35:39 +00:00
|
|
|
, fOffset(0)
|
|
|
|
, fBufferedSoFar(0)
|
|
|
|
, fBufferSize(bufferSize)
|
2020-04-24 17:00:48 +00:00
|
|
|
, fBuffer(bufferSize <= kStorageSize ? fStorage
|
|
|
|
: reinterpret_cast<char*>(malloc(bufferSize))) {}
|
|
|
|
|
|
|
|
FrontBufferedStream::~FrontBufferedStream() {
|
|
|
|
if (fBuffer != fStorage) {
|
|
|
|
free(fBuffer);
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:35:39 +00:00
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
bool FrontBufferedStream::isAtEnd() const {
|
2013-09-26 21:35:39 +00:00
|
|
|
if (fOffset < fBufferedSoFar) {
|
|
|
|
// Even if the underlying stream is at the end, this stream has been
|
|
|
|
// rewound after buffering, so it is not at the end.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fStream->isAtEnd();
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
bool FrontBufferedStream::rewind() {
|
2013-09-26 21:35:39 +00:00
|
|
|
// Only allow a rewind if we have not exceeded the buffer.
|
|
|
|
if (fOffset <= fBufferSize) {
|
|
|
|
fOffset = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
size_t FrontBufferedStream::readFromBuffer(char* dst, size_t size) {
|
2013-09-26 21:35:39 +00:00
|
|
|
SkASSERT(fOffset < fBufferedSoFar);
|
|
|
|
// Some data has already been copied to fBuffer. Read up to the
|
|
|
|
// lesser of the size requested and the remainder of the buffered
|
|
|
|
// data.
|
2020-02-07 15:36:46 +00:00
|
|
|
const size_t bytesToCopy = std::min(size, fBufferedSoFar - fOffset);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (dst != nullptr) {
|
2013-09-26 21:35:39 +00:00
|
|
|
memcpy(dst, fBuffer + fOffset, bytesToCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update fOffset to the new position. It is guaranteed to be
|
|
|
|
// within the buffered data.
|
|
|
|
fOffset += bytesToCopy;
|
|
|
|
SkASSERT(fOffset <= fBufferedSoFar);
|
|
|
|
|
|
|
|
return bytesToCopy;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
size_t FrontBufferedStream::bufferAndWriteTo(char* dst, size_t size) {
|
2013-09-26 21:35:39 +00:00
|
|
|
SkASSERT(size > 0);
|
|
|
|
SkASSERT(fOffset >= fBufferedSoFar);
|
2014-10-21 14:12:52 +00:00
|
|
|
SkASSERT(fBuffer);
|
2013-09-26 21:35:39 +00:00
|
|
|
// Data needs to be buffered. Buffer up to the lesser of the size requested
|
|
|
|
// and the remainder of the max buffer size.
|
2020-02-07 15:36:46 +00:00
|
|
|
const size_t bytesToBuffer = std::min(size, fBufferSize - fBufferedSoFar);
|
2013-09-26 21:35:39 +00:00
|
|
|
char* buffer = fBuffer + fOffset;
|
|
|
|
const size_t buffered = fStream->read(buffer, bytesToBuffer);
|
|
|
|
|
|
|
|
fBufferedSoFar += buffered;
|
|
|
|
fOffset = fBufferedSoFar;
|
|
|
|
SkASSERT(fBufferedSoFar <= fBufferSize);
|
|
|
|
|
|
|
|
// Copy the buffer to the destination buffer and update the amount read.
|
2015-08-27 14:41:13 +00:00
|
|
|
if (dst != nullptr) {
|
2013-09-26 21:35:39 +00:00
|
|
|
memcpy(dst, buffer, buffered);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffered;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
size_t FrontBufferedStream::readDirectlyFromStream(char* dst, size_t size) {
|
2013-09-26 21:35:39 +00:00
|
|
|
SkASSERT(size > 0);
|
|
|
|
// If we get here, we have buffered all that can be buffered.
|
|
|
|
SkASSERT(fBufferSize == fBufferedSoFar && fOffset >= fBufferSize);
|
|
|
|
|
|
|
|
const size_t bytesReadDirectly = fStream->read(dst, size);
|
|
|
|
fOffset += bytesReadDirectly;
|
|
|
|
|
|
|
|
// If we have read past the end of the buffer, rewinding is no longer
|
|
|
|
// supported, so we can go ahead and free the memory.
|
2020-04-24 17:00:48 +00:00
|
|
|
if (bytesReadDirectly > 0 && fBuffer != fStorage) {
|
|
|
|
free(fBuffer);
|
|
|
|
fBuffer = nullptr;
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytesReadDirectly;
|
|
|
|
}
|
|
|
|
|
2015-12-07 19:37:13 +00:00
|
|
|
size_t FrontBufferedStream::peek(void* dst, size_t size) const {
|
2015-04-02 20:19:51 +00:00
|
|
|
// Keep track of the offset so we can return to it.
|
|
|
|
const size_t start = fOffset;
|
2015-12-07 19:37:13 +00:00
|
|
|
|
|
|
|
if (start >= fBufferSize) {
|
|
|
|
// This stream is not able to buffer.
|
|
|
|
return 0;
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
2015-12-07 19:37:13 +00:00
|
|
|
|
2020-02-07 15:36:46 +00:00
|
|
|
size = std::min(size, fBufferSize - start);
|
2015-04-02 20:19:51 +00:00
|
|
|
FrontBufferedStream* nonConstThis = const_cast<FrontBufferedStream*>(this);
|
2015-12-07 19:37:13 +00:00
|
|
|
const size_t bytesRead = nonConstThis->read(dst, size);
|
2015-04-02 20:19:51 +00:00
|
|
|
nonConstThis->fOffset = start;
|
2015-12-07 19:37:13 +00:00
|
|
|
return bytesRead;
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:05 +00:00
|
|
|
size_t FrontBufferedStream::read(void* voidDst, size_t size) {
|
2013-09-26 21:35:39 +00:00
|
|
|
// Cast voidDst to a char* for easy addition.
|
|
|
|
char* dst = reinterpret_cast<char*>(voidDst);
|
|
|
|
SkDEBUGCODE(const size_t totalSize = size;)
|
|
|
|
const size_t start = fOffset;
|
|
|
|
|
|
|
|
// First, read any data that was previously buffered.
|
|
|
|
if (fOffset < fBufferedSoFar) {
|
|
|
|
const size_t bytesCopied = this->readFromBuffer(dst, size);
|
|
|
|
|
|
|
|
// Update the remaining number of bytes needed to read
|
|
|
|
// and the destination buffer.
|
|
|
|
size -= bytesCopied;
|
|
|
|
SkASSERT(size + (fOffset - start) == totalSize);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (dst != nullptr) {
|
2013-09-26 21:35:39 +00:00
|
|
|
dst += bytesCopied;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer any more data that should be buffered, and copy it to the
|
|
|
|
// destination.
|
2014-10-21 15:06:05 +00:00
|
|
|
if (size > 0 && fBufferedSoFar < fBufferSize && !fStream->isAtEnd()) {
|
2013-09-26 21:35:39 +00:00
|
|
|
const size_t buffered = this->bufferAndWriteTo(dst, size);
|
|
|
|
|
|
|
|
// Update the remaining number of bytes needed to read
|
|
|
|
// and the destination buffer.
|
|
|
|
size -= buffered;
|
|
|
|
SkASSERT(size + (fOffset - start) == totalSize);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (dst != nullptr) {
|
2013-09-26 21:35:39 +00:00
|
|
|
dst += buffered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0 && !fStream->isAtEnd()) {
|
2013-09-26 21:49:46 +00:00
|
|
|
SkDEBUGCODE(const size_t bytesReadDirectly =) this->readDirectlyFromStream(dst, size);
|
2013-09-26 21:35:39 +00:00
|
|
|
SkDEBUGCODE(size -= bytesReadDirectly;)
|
|
|
|
SkASSERT(size + (fOffset - start) == totalSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fOffset - start;
|
|
|
|
}
|
2020-04-24 17:00:48 +00:00
|
|
|
} // anonymous namespace
|