2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkStream_DEFINED
|
|
|
|
#define SkStream_DEFINED
|
|
|
|
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "../private/SkTo.h"
|
2016-03-11 21:02:28 +00:00
|
|
|
#include "SkData.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
|
2017-03-10 18:08:15 +00:00
|
|
|
#include <memory.h>
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
class SkStream;
|
|
|
|
class SkStreamRewindable;
|
|
|
|
class SkStreamSeekable;
|
|
|
|
class SkStreamAsset;
|
|
|
|
class SkStreamMemory;
|
|
|
|
|
2013-05-07 15:59:16 +00:00
|
|
|
/**
|
|
|
|
* SkStream -- abstraction for a source of bytes. Subclasses can be backed by
|
|
|
|
* memory, or a file, or something else.
|
|
|
|
*
|
|
|
|
* NOTE:
|
|
|
|
*
|
|
|
|
* Classic "streams" APIs are sort of async, in that on a request for N
|
|
|
|
* bytes, they may return fewer than N bytes on a given call, in which case
|
|
|
|
* the caller can "try again" to get more bytes, eventually (modulo an error)
|
|
|
|
* receiving their total N bytes.
|
2013-05-08 07:01:40 +00:00
|
|
|
*
|
2013-05-07 15:59:16 +00:00
|
|
|
* Skia streams behave differently. They are effectively synchronous, and will
|
|
|
|
* always return all N bytes of the request if possible. If they return fewer
|
|
|
|
* (the read() call returns the number of bytes read) then that means there is
|
|
|
|
* no more data (at EOF or hit an error). The caller should *not* call again
|
|
|
|
* in hopes of fulfilling more of the request.
|
|
|
|
*/
|
2018-06-14 18:57:12 +00:00
|
|
|
class SK_API SkStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2015-01-21 20:09:53 +00:00
|
|
|
virtual ~SkStream() {}
|
2018-06-14 18:57:12 +00:00
|
|
|
SkStream() {}
|
2015-01-21 20:09:53 +00:00
|
|
|
|
2013-03-18 21:08:46 +00:00
|
|
|
/**
|
2016-09-16 13:24:20 +00:00
|
|
|
* Attempts to open the specified file as a stream, returns nullptr on failure.
|
2013-03-18 21:08:46 +00:00
|
|
|
*/
|
2016-09-16 13:24:20 +00:00
|
|
|
static std::unique_ptr<SkStreamAsset> MakeFromFile(const char path[]);
|
2013-03-19 07:15:10 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Reads or skips size number of bytes.
|
|
|
|
* If buffer == NULL, skip size bytes, return how many were skipped.
|
|
|
|
* If buffer != NULL, copy size bytes into buffer, return how many were copied.
|
|
|
|
* @param buffer when NULL skip size bytes, otherwise copy size bytes into buffer
|
|
|
|
* @param size the number of bytes to skip or copy
|
2013-07-19 13:55:41 +00:00
|
|
|
* @return the number of bytes actually read.
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual size_t read(void* buffer, size_t size) = 0;
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Skip size number of bytes.
|
|
|
|
* @return the actual number bytes that could be skipped.
|
|
|
|
*/
|
|
|
|
size_t skip(size_t size) {
|
2017-08-28 14:34:05 +00:00
|
|
|
return this->read(nullptr, size);
|
2013-05-29 13:43:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 20:19:51 +00:00
|
|
|
/**
|
|
|
|
* Attempt to peek at size bytes.
|
2015-12-07 19:37:13 +00:00
|
|
|
* If this stream supports peeking, copy min(size, peekable bytes) into
|
|
|
|
* buffer, and return the number of bytes copied.
|
|
|
|
* If the stream does not support peeking, or cannot peek any bytes,
|
|
|
|
* return 0 and leave buffer unchanged.
|
2015-04-02 20:19:51 +00:00
|
|
|
* The stream is guaranteed to be in the same visible state after this
|
|
|
|
* call, regardless of success or failure.
|
2015-12-07 19:37:13 +00:00
|
|
|
* @param buffer Must not be NULL, and must be at least size bytes. Destination
|
|
|
|
* to copy bytes.
|
2015-04-02 20:19:51 +00:00
|
|
|
* @param size Number of bytes to copy.
|
2015-12-07 19:37:13 +00:00
|
|
|
* @return The number of bytes peeked/copied.
|
2015-04-02 20:19:51 +00:00
|
|
|
*/
|
2015-12-07 19:37:13 +00:00
|
|
|
virtual size_t peek(void* /*buffer*/, size_t /*size*/) const { return 0; }
|
2015-04-02 20:19:51 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
/** Returns true when all the bytes in the stream have been read.
|
|
|
|
* This may return true early (when there are no more bytes to be read)
|
|
|
|
* or late (after the first unsuccessful read).
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2013-11-13 19:53:46 +00:00
|
|
|
virtual bool isAtEnd() const = 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-10-07 19:50:53 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT readS8(int8_t*);
|
|
|
|
bool SK_WARN_UNUSED_RESULT readS16(int16_t*);
|
|
|
|
bool SK_WARN_UNUSED_RESULT readS32(int32_t*);
|
|
|
|
|
|
|
|
bool SK_WARN_UNUSED_RESULT readU8(uint8_t* i) { return this->readS8((int8_t*)i); }
|
|
|
|
bool SK_WARN_UNUSED_RESULT readU16(uint16_t* i) { return this->readS16((int16_t*)i); }
|
|
|
|
bool SK_WARN_UNUSED_RESULT readU32(uint32_t* i) { return this->readS32((int32_t*)i); }
|
|
|
|
|
|
|
|
bool SK_WARN_UNUSED_RESULT readBool(bool* b) {
|
|
|
|
uint8_t i;
|
|
|
|
if (!this->readU8(&i)) { return false; }
|
|
|
|
*b = (i != 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool SK_WARN_UNUSED_RESULT readScalar(SkScalar*);
|
|
|
|
bool SK_WARN_UNUSED_RESULT readPackedUInt(size_t*);
|
2012-06-21 20:25:03 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
//SkStreamRewindable
|
2013-08-28 13:08:54 +00:00
|
|
|
/** Rewinds to the beginning of the stream. Returns true if the stream is known
|
|
|
|
* to be at the beginning after this call returns.
|
|
|
|
*/
|
2013-05-29 13:43:31 +00:00
|
|
|
virtual bool rewind() { return false; }
|
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
/** Duplicates this stream. If this cannot be done, returns NULL.
|
|
|
|
* The returned stream will be positioned at the beginning of its data.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<SkStream> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStream>(this->onDuplicate());
|
|
|
|
}
|
|
|
|
/** Duplicates this stream. If this cannot be done, returns NULL.
|
|
|
|
* The returned stream will be positioned the same as this stream.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<SkStream> fork() const {
|
|
|
|
return std::unique_ptr<SkStream>(this->onFork());
|
|
|
|
}
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
//SkStreamSeekable
|
|
|
|
/** Returns true if this stream can report it's current position. */
|
|
|
|
virtual bool hasPosition() const { return false; }
|
|
|
|
/** Returns the current position in the stream. If this cannot be done, returns 0. */
|
|
|
|
virtual size_t getPosition() const { return 0; }
|
|
|
|
|
|
|
|
/** Seeks to an absolute position in the stream. If this cannot be done, returns false.
|
|
|
|
* If an attempt is made to seek past the end of the stream, the position will be set
|
|
|
|
* to the end of the stream.
|
|
|
|
*/
|
2014-11-14 19:11:46 +00:00
|
|
|
virtual bool seek(size_t /*position*/) { return false; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** Seeks to an relative offset in the stream. If this cannot be done, returns false.
|
|
|
|
* If an attempt is made to move to a position outside the stream, the position will be set
|
|
|
|
* to the closest point within the stream (beginning or end).
|
|
|
|
*/
|
2014-11-14 19:11:46 +00:00
|
|
|
virtual bool move(long /*offset*/) { return false; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
//SkStreamAsset
|
|
|
|
/** Returns true if this stream can report it's total length. */
|
|
|
|
virtual bool hasLength() const { return false; }
|
|
|
|
/** Returns the total length of the stream. If this cannot be done, returns 0. */
|
2013-11-13 19:53:46 +00:00
|
|
|
virtual size_t getLength() const { return 0; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
//SkStreamMemory
|
|
|
|
/** Returns the starting address for the data. If this cannot be done, returns NULL. */
|
|
|
|
//TODO: replace with virtual const SkData* getData()
|
2017-08-28 14:34:05 +00:00
|
|
|
virtual const void* getMemoryBase() { return nullptr; }
|
2017-09-16 01:39:47 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual SkStream* onDuplicate() const { return nullptr; }
|
|
|
|
virtual SkStream* onFork() const { return nullptr; }
|
2018-06-16 00:27:52 +00:00
|
|
|
|
|
|
|
SkStream(SkStream&&) = delete;
|
|
|
|
SkStream(const SkStream&) = delete;
|
|
|
|
SkStream& operator=(SkStream&&) = delete;
|
|
|
|
SkStream& operator=(const SkStream&) = delete;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** SkStreamRewindable is a SkStream for which rewind and duplicate are required. */
|
|
|
|
class SK_API SkStreamRewindable : public SkStream {
|
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool rewind() override = 0;
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStreamRewindable> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStreamRewindable>(this->onDuplicate());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SkStreamRewindable* onDuplicate() const override = 0;
|
2013-05-29 13:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamSeekable is a SkStreamRewindable for which position, seek, move, and fork are required. */
|
|
|
|
class SK_API SkStreamSeekable : public SkStreamRewindable {
|
|
|
|
public:
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStreamSeekable> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStreamSeekable>(this->onDuplicate());
|
|
|
|
}
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool hasPosition() const override { return true; }
|
|
|
|
size_t getPosition() const override = 0;
|
|
|
|
bool seek(size_t position) override = 0;
|
|
|
|
bool move(long offset) override = 0;
|
2017-09-20 19:37:14 +00:00
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStreamSeekable> fork() const {
|
|
|
|
return std::unique_ptr<SkStreamSeekable>(this->onFork());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SkStreamSeekable* onDuplicate() const override = 0;
|
|
|
|
SkStreamSeekable* onFork() const override = 0;
|
2013-05-29 13:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamAsset is a SkStreamSeekable for which getLength is required. */
|
|
|
|
class SK_API SkStreamAsset : public SkStreamSeekable {
|
|
|
|
public:
|
2017-09-15 21:03:54 +00:00
|
|
|
bool hasLength() const override { return true; }
|
|
|
|
size_t getLength() const override = 0;
|
2017-09-16 01:39:47 +00:00
|
|
|
|
|
|
|
std::unique_ptr<SkStreamAsset> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
|
|
|
|
}
|
|
|
|
std::unique_ptr<SkStreamAsset> fork() const {
|
|
|
|
return std::unique_ptr<SkStreamAsset>(this->onFork());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SkStreamAsset* onDuplicate() const override = 0;
|
|
|
|
SkStreamAsset* onFork() const override = 0;
|
2013-05-29 13:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** SkStreamMemory is a SkStreamAsset for which getMemoryBase is required. */
|
|
|
|
class SK_API SkStreamMemory : public SkStreamAsset {
|
|
|
|
public:
|
2017-09-16 01:39:47 +00:00
|
|
|
const void* getMemoryBase() override = 0;
|
|
|
|
|
|
|
|
std::unique_ptr<SkStreamMemory> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStreamMemory>(this->onDuplicate());
|
|
|
|
}
|
|
|
|
std::unique_ptr<SkStreamMemory> fork() const {
|
|
|
|
return std::unique_ptr<SkStreamMemory>(this->onFork());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SkStreamMemory* onDuplicate() const override = 0;
|
|
|
|
SkStreamMemory* onFork() const override = 0;
|
2013-05-29 13:43:31 +00:00
|
|
|
};
|
|
|
|
|
2018-06-14 18:57:12 +00:00
|
|
|
class SK_API SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
virtual ~SkWStream();
|
2018-06-14 18:57:12 +00:00
|
|
|
SkWStream() {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Called to write bytes to a SkWStream. Returns true on success
|
|
|
|
@param buffer the address of at least size bytes to be written to the stream
|
|
|
|
@param size The number of bytes in buffer to write to the stream
|
|
|
|
@return true on success
|
|
|
|
*/
|
|
|
|
virtual bool write(const void* buffer, size_t size) = 0;
|
|
|
|
virtual void flush();
|
|
|
|
|
2014-03-06 17:16:26 +00:00
|
|
|
virtual size_t bytesWritten() const = 0;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// helpers
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2016-12-15 19:11:37 +00:00
|
|
|
bool write8(U8CPU value) {
|
|
|
|
uint8_t v = SkToU8(value);
|
|
|
|
return this->write(&v, 1);
|
|
|
|
}
|
|
|
|
bool write16(U16CPU value) {
|
|
|
|
uint16_t v = SkToU16(value);
|
|
|
|
return this->write(&v, 2);
|
|
|
|
}
|
|
|
|
bool write32(uint32_t v) {
|
|
|
|
return this->write(&v, 4);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-12-15 19:11:37 +00:00
|
|
|
bool writeText(const char text[]) {
|
2016-04-12 02:41:48 +00:00
|
|
|
SkASSERT(text);
|
|
|
|
return this->write(text, strlen(text));
|
|
|
|
}
|
2017-01-07 21:16:02 +00:00
|
|
|
|
|
|
|
bool newline() { return this->write("\n", strlen("\n")); }
|
|
|
|
|
2018-09-06 12:22:33 +00:00
|
|
|
bool writeDecAsText(int32_t);
|
|
|
|
bool writeBigDecAsText(int64_t, int minDigits = 0);
|
|
|
|
bool writeHexAsText(uint32_t, int minDigits = 0);
|
|
|
|
bool writeScalarAsText(SkScalar);
|
|
|
|
|
|
|
|
bool writeBool(bool v) { return this->write8(v); }
|
|
|
|
bool writeScalar(SkScalar);
|
2016-10-07 19:50:53 +00:00
|
|
|
bool writePackedUInt(size_t);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2016-10-07 19:50:53 +00:00
|
|
|
bool writeStream(SkStream* input, size_t length);
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2014-03-05 18:25:20 +00:00
|
|
|
/**
|
|
|
|
* This returns the number of bytes in the stream required to store
|
|
|
|
* 'value'.
|
|
|
|
*/
|
|
|
|
static int SizeOfPackedUInt(size_t value);
|
2018-06-16 00:27:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SkWStream(const SkWStream&) = delete;
|
|
|
|
SkWStream& operator=(const SkWStream&) = delete;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2017-03-22 14:01:53 +00:00
|
|
|
class SK_API SkNullWStream : public SkWStream {
|
|
|
|
public:
|
|
|
|
SkNullWStream() : fBytesWritten(0) {}
|
|
|
|
|
2018-07-28 14:14:27 +00:00
|
|
|
bool write(const void* , size_t n) override { fBytesWritten += n; return true; }
|
2017-03-22 14:01:53 +00:00
|
|
|
void flush() override {}
|
|
|
|
size_t bytesWritten() const override { return fBytesWritten; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t fBytesWritten;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-11 18:50:45 +00:00
|
|
|
#include <stdio.h>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** A stream that wraps a C FILE* file stream. */
|
|
|
|
class SK_API SkFILEStream : public SkStreamAsset {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Initialize the stream by calling sk_fopen on the specified path.
|
|
|
|
* This internal stream will be closed in the destructor.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-03-10 18:08:15 +00:00
|
|
|
explicit SkFILEStream(const char path[] = nullptr);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2018-05-08 13:35:32 +00:00
|
|
|
/** Initialize the stream with an existing C FILE stream.
|
|
|
|
* The current position of the C FILE stream will be considered the
|
|
|
|
* beginning of the SkFILEStream.
|
|
|
|
* The C FILE stream will be closed in the destructor.
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2017-03-10 18:08:15 +00:00
|
|
|
explicit SkFILEStream(FILE* file);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2017-03-22 16:05:03 +00:00
|
|
|
~SkFILEStream() override;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-07-26 15:35:53 +00:00
|
|
|
static std::unique_ptr<SkFILEStream> Make(const char path[]) {
|
2017-08-26 23:31:12 +00:00
|
|
|
std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path));
|
|
|
|
return stream->isValid() ? std::move(stream) : nullptr;
|
2017-07-26 15:35:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Returns true if the current path could be opened. */
|
2017-03-10 18:08:15 +00:00
|
|
|
bool isValid() const { return fFILE != nullptr; }
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2017-03-10 18:08:15 +00:00
|
|
|
/** Close this SkFILEStream. */
|
|
|
|
void close();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void* buffer, size_t size) override;
|
|
|
|
bool isAtEnd() const override;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool rewind() override;
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> duplicate() const {
|
|
|
|
return std::unique_ptr<SkStreamAsset>(this->onDuplicate());
|
|
|
|
}
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t getPosition() const override;
|
|
|
|
bool seek(size_t position) override;
|
|
|
|
bool move(long offset) override;
|
2017-09-20 19:37:14 +00:00
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> fork() const {
|
|
|
|
return std::unique_ptr<SkStreamAsset>(this->onFork());
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t getLength() const override;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2017-03-10 18:08:15 +00:00
|
|
|
explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset);
|
|
|
|
explicit SkFILEStream(std::shared_ptr<FILE>, size_t size, size_t offset, size_t originalOffset);
|
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
SkStreamAsset* onDuplicate() const override;
|
|
|
|
SkStreamAsset* onFork() const override;
|
|
|
|
|
2017-03-10 18:08:15 +00:00
|
|
|
std::shared_ptr<FILE> fFILE;
|
|
|
|
// My own council will I keep on sizes and offsets.
|
|
|
|
size_t fSize;
|
|
|
|
size_t fOffset;
|
|
|
|
size_t fOriginalOffset;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
typedef SkStreamAsset INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
class SK_API SkMemoryStream : public SkStreamMemory {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkMemoryStream();
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** We allocate (and free) the memory. Write to it via getMemoryBase() */
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream(size_t length);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
|
|
|
/** If copyData is true, the stream makes a private copy of the data. */
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream(const void* data, size_t length, bool copyData = false);
|
2013-01-09 22:02:58 +00:00
|
|
|
|
2016-03-11 21:02:28 +00:00
|
|
|
/** Creates the stream to read from the specified data */
|
2018-07-28 14:14:27 +00:00
|
|
|
SkMemoryStream(sk_sp<SkData> data);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-07-26 15:35:53 +00:00
|
|
|
/** Returns a stream with a copy of the input data. */
|
|
|
|
static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
|
|
|
|
|
|
|
|
/** Returns a stream with a bare pointer reference to the input data. */
|
|
|
|
static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
|
|
|
|
|
|
|
|
/** Returns a stream with a shared reference to the input data. */
|
|
|
|
static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Resets the stream to the specified data and length,
|
|
|
|
just like the constructor.
|
|
|
|
if copyData is true, the stream makes a private copy of the data
|
|
|
|
*/
|
|
|
|
virtual void setMemory(const void* data, size_t length,
|
|
|
|
bool copyData = false);
|
2011-02-23 20:46:31 +00:00
|
|
|
/** Replace any memory buffer with the specified buffer. The caller
|
|
|
|
must have allocated data with sk_malloc or sk_realloc, since it
|
|
|
|
will be freed with sk_free.
|
|
|
|
*/
|
|
|
|
void setMemoryOwned(const void* data, size_t length);
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> asData() const { return fData; }
|
2018-07-28 14:14:27 +00:00
|
|
|
void setData(sk_sp<SkData> data);
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void skipToAlign4();
|
|
|
|
const void* getAtPos();
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void* buffer, size_t size) override;
|
|
|
|
bool isAtEnd() const override;
|
2012-06-26 15:30:08 +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 rewind() override;
|
2017-09-20 19:37:14 +00:00
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkMemoryStream> duplicate() const {
|
|
|
|
return std::unique_ptr<SkMemoryStream>(this->onDuplicate());
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t getPosition() const override;
|
|
|
|
bool seek(size_t position) override;
|
|
|
|
bool move(long offset) override;
|
2017-09-20 19:37:14 +00:00
|
|
|
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkMemoryStream> fork() const {
|
|
|
|
return std::unique_ptr<SkMemoryStream>(this->onFork());
|
|
|
|
}
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t getLength() const override;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const void* getMemoryBase() override;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
private:
|
2017-09-16 01:39:47 +00:00
|
|
|
SkMemoryStream* onDuplicate() const override;
|
|
|
|
SkMemoryStream* onFork() const override;
|
|
|
|
|
2016-03-11 21:02:28 +00:00
|
|
|
sk_sp<SkData> fData;
|
|
|
|
size_t fOffset;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
typedef SkStreamMemory INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-17 19:10:36 +00:00
|
|
|
class SK_API SkFILEWStream : public SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-26 15:30:08 +00:00
|
|
|
SkFILEWStream(const char path[]);
|
2017-03-22 16:05:03 +00:00
|
|
|
~SkFILEWStream() override;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Returns true if the current path could be opened.
|
|
|
|
*/
|
2017-08-28 14:34:05 +00:00
|
|
|
bool isValid() const { return fFILE != nullptr; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool write(const void* buffer, size_t size) override;
|
|
|
|
void flush() override;
|
2015-12-16 21:41:23 +00:00
|
|
|
void fsync();
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t bytesWritten() const override;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2015-11-20 21:47:49 +00:00
|
|
|
FILE* fFILE;
|
2012-06-26 15:30:08 +00:00
|
|
|
|
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2011-03-30 20:14:49 +00:00
|
|
|
class SK_API SkDynamicMemoryWStream : public SkWStream {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2018-10-03 20:30:09 +00:00
|
|
|
SkDynamicMemoryWStream() = default;
|
|
|
|
SkDynamicMemoryWStream(SkDynamicMemoryWStream&&);
|
|
|
|
SkDynamicMemoryWStream& operator=(SkDynamicMemoryWStream&&);
|
2017-03-22 16:05:03 +00:00
|
|
|
~SkDynamicMemoryWStream() override;
|
2011-06-24 19:12:12 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool write(const void* buffer, size_t size) override;
|
2016-12-15 19:11:37 +00:00
|
|
|
size_t bytesWritten() const override;
|
|
|
|
|
2016-12-16 16:51:41 +00:00
|
|
|
bool read(void* buffer, size_t offset, size_t size);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-12-16 16:51:41 +00:00
|
|
|
/** More efficient version of read(dst, 0, bytesWritten()). */
|
2011-06-24 19:12:12 +00:00
|
|
|
void copyTo(void* dst) const;
|
2017-05-23 15:47:05 +00:00
|
|
|
bool writeToStream(SkWStream* dst) const;
|
2011-06-23 21:48:04 +00:00
|
|
|
|
2017-03-14 19:52:12 +00:00
|
|
|
/** Equivalent to copyTo() followed by reset(), but may save memory use. */
|
|
|
|
void copyToAndReset(void* dst);
|
|
|
|
|
2017-05-23 15:47:05 +00:00
|
|
|
/** Equivalent to writeToStream() followed by reset(), but may save memory use. */
|
|
|
|
bool writeToAndReset(SkWStream* dst);
|
|
|
|
|
2016-12-16 16:51:41 +00:00
|
|
|
/** Return the contents as SkData, and then reset the stream. */
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> detachAsData();
|
2011-06-23 21:48:04 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
/** Reset, returning a reader stream with the current content. */
|
2017-03-14 18:35:02 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> detachAsStream();
|
2013-07-19 13:55:41 +00:00
|
|
|
|
|
|
|
/** Reset the stream to its original, empty, state. */
|
2008-12-17 15:59:43 +00:00
|
|
|
void reset();
|
|
|
|
void padToAlign4();
|
|
|
|
private:
|
|
|
|
struct Block;
|
2018-10-03 20:30:09 +00:00
|
|
|
Block* fHead = nullptr;
|
|
|
|
Block* fTail = nullptr;
|
|
|
|
size_t fBytesWrittenBeforeTail = 0;
|
2016-12-15 19:11:37 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
void validate() const;
|
|
|
|
#else
|
|
|
|
void validate() const {}
|
|
|
|
#endif
|
2012-06-26 15:30:08 +00:00
|
|
|
|
2013-07-19 13:55:41 +00:00
|
|
|
// For access to the Block type.
|
|
|
|
friend class SkBlockMemoryStream;
|
|
|
|
friend class SkBlockMemoryRefCnt;
|
|
|
|
|
2012-06-26 15:30:08 +00:00
|
|
|
typedef SkWStream INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|