Add smartptr SkRWBuffer snapshot methods
Change-Id: Id816a38213a86bf090230ebaaef00d98a9487965 Reviewed-on: https://skia-review.googlesource.com/18266 Reviewed-by: Leon Scroggins <scroggo@google.com> Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
parent
a16f6cb40a
commit
69270fbb2b
@ -30,6 +30,7 @@ public:
|
||||
class SK_API Iter {
|
||||
public:
|
||||
Iter(const SkROBuffer*);
|
||||
Iter(const sk_sp<SkROBuffer>&);
|
||||
|
||||
void reset(const SkROBuffer*);
|
||||
|
||||
@ -92,6 +93,12 @@ public:
|
||||
SkROBuffer* newRBufferSnapshot() const;
|
||||
SkStreamAsset* newStreamSnapshot() const;
|
||||
|
||||
sk_sp<SkROBuffer> makeROBufferSnapshot() const {
|
||||
return sk_sp<SkROBuffer>(new SkROBuffer(fHead, fTotalUsed, fTail));
|
||||
}
|
||||
|
||||
std::unique_ptr<SkStreamAsset> makeStreamSnapshot() const;
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
void validate() const;
|
||||
#else
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "SkAtomics.h"
|
||||
#include "SkMalloc.h"
|
||||
#include "SkMakeUnique.h"
|
||||
#include "SkStream.h"
|
||||
|
||||
// Force small chunks to be a page's worth
|
||||
@ -145,6 +146,10 @@ SkROBuffer::Iter::Iter(const SkROBuffer* buffer) {
|
||||
this->reset(buffer);
|
||||
}
|
||||
|
||||
SkROBuffer::Iter::Iter(const sk_sp<SkROBuffer>& buffer) {
|
||||
this->reset(buffer.get());
|
||||
}
|
||||
|
||||
void SkROBuffer::Iter::reset(const SkROBuffer* buffer) {
|
||||
fBuffer = buffer;
|
||||
if (buffer && buffer->fHead) {
|
||||
@ -268,17 +273,15 @@ class SkROBufferStreamAsset : public SkStreamAsset {
|
||||
#endif
|
||||
|
||||
public:
|
||||
SkROBufferStreamAsset(const SkROBuffer* buffer) : fBuffer(SkRef(buffer)), fIter(buffer) {
|
||||
SkROBufferStreamAsset(sk_sp<SkROBuffer> buffer) : fBuffer(std::move(buffer)), fIter(fBuffer) {
|
||||
fGlobalOffset = fLocalOffset = 0;
|
||||
}
|
||||
|
||||
~SkROBufferStreamAsset() override { fBuffer->unref(); }
|
||||
|
||||
size_t getLength() const override { return fBuffer->size(); }
|
||||
|
||||
bool rewind() override {
|
||||
AUTO_VALIDATE
|
||||
fIter.reset(fBuffer);
|
||||
fIter.reset(fBuffer.get());
|
||||
fGlobalOffset = fLocalOffset = 0;
|
||||
return true;
|
||||
}
|
||||
@ -350,13 +353,16 @@ public:
|
||||
|
||||
|
||||
private:
|
||||
const SkROBuffer* fBuffer;
|
||||
SkROBuffer::Iter fIter;
|
||||
size_t fLocalOffset;
|
||||
size_t fGlobalOffset;
|
||||
sk_sp<SkROBuffer> fBuffer;
|
||||
SkROBuffer::Iter fIter;
|
||||
size_t fLocalOffset;
|
||||
size_t fGlobalOffset;
|
||||
};
|
||||
|
||||
SkStreamAsset* SkRWBuffer::newStreamSnapshot() const {
|
||||
sk_sp<SkROBuffer> buffer(this->newRBufferSnapshot());
|
||||
return new SkROBufferStreamAsset(buffer.get());
|
||||
return new SkROBufferStreamAsset(this->makeROBufferSnapshot());
|
||||
}
|
||||
|
||||
std::unique_ptr<SkStreamAsset> SkRWBuffer::makeStreamSnapshot() const {
|
||||
return skstd::make_unique<SkROBufferStreamAsset>(this->makeROBufferSnapshot());
|
||||
}
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "SkReadBuffer.h"
|
||||
#include "SkWriteBuffer.h"
|
||||
#include "SkStream.h"
|
||||
#include "SkTArray.h"
|
||||
#include "Test.h"
|
||||
|
||||
static void test_is_equal(skiatest::Reporter* reporter,
|
||||
@ -255,16 +256,16 @@ static void check_alphabet_buffer(skiatest::Reporter* reporter, const SkROBuffer
|
||||
DEF_TEST(RWBuffer, reporter) {
|
||||
// Knowing that the default capacity is 4096, choose N large enough so we force it to use
|
||||
// multiple buffers internally.
|
||||
const int N = 1000;
|
||||
SkROBuffer* readers[N];
|
||||
SkStream* streams[N];
|
||||
static constexpr int N = 1000;
|
||||
SkSTArray<N, sk_sp<SkROBuffer>> readers;
|
||||
SkSTArray<N, std::unique_ptr<SkStream>> streams;
|
||||
|
||||
{
|
||||
SkRWBuffer buffer;
|
||||
for (int i = 0; i < N; ++i) {
|
||||
buffer.append(gABC, 26);
|
||||
readers[i] = buffer.newRBufferSnapshot();
|
||||
streams[i] = buffer.newStreamSnapshot();
|
||||
readers.push_back(buffer.makeROBufferSnapshot());
|
||||
streams.push_back(buffer.makeStreamSnapshot());
|
||||
}
|
||||
REPORTER_ASSERT(reporter, N*26 == buffer.size());
|
||||
}
|
||||
@ -272,10 +273,8 @@ DEF_TEST(RWBuffer, reporter) {
|
||||
// Verify that although the SkRWBuffer's destructor has run, the readers are still valid.
|
||||
for (int i = 0; i < N; ++i) {
|
||||
REPORTER_ASSERT(reporter, (i + 1) * 26U == readers[i]->size());
|
||||
check_alphabet_buffer(reporter, readers[i]);
|
||||
check_alphabet_stream(reporter, streams[i]);
|
||||
readers[i]->unref();
|
||||
delete streams[i];
|
||||
check_alphabet_buffer(reporter, readers[i].get());
|
||||
check_alphabet_stream(reporter, streams[i].get());
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,8 +286,8 @@ DEF_TEST(RWBuffer_threaded, reporter) {
|
||||
SkRWBuffer buffer;
|
||||
for (int i = 0; i < N; ++i) {
|
||||
buffer.append(gABC, 26);
|
||||
sk_sp<SkROBuffer> reader = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot());
|
||||
SkStream* stream = buffer.newStreamSnapshot();
|
||||
sk_sp<SkROBuffer> reader = buffer.makeROBufferSnapshot();
|
||||
SkStream* stream = buffer.makeStreamSnapshot().release();
|
||||
REPORTER_ASSERT(reporter, reader->size() == buffer.size());
|
||||
REPORTER_ASSERT(reporter, stream->getLength() == buffer.size());
|
||||
|
||||
@ -313,7 +312,7 @@ DEF_TEST(RWBuffer_size, r) {
|
||||
SkRWBuffer buffer;
|
||||
buffer.append(gABC, 26);
|
||||
|
||||
sk_sp<SkROBuffer> roBuffer(buffer.newRBufferSnapshot());
|
||||
sk_sp<SkROBuffer> roBuffer(buffer.makeROBufferSnapshot());
|
||||
SkROBuffer::Iter iter(roBuffer.get());
|
||||
REPORTER_ASSERT(r, iter.data());
|
||||
REPORTER_ASSERT(r, iter.size() == 26);
|
||||
@ -329,7 +328,7 @@ DEF_TEST(RWBuffer_noAppend, r) {
|
||||
SkRWBuffer buffer;
|
||||
REPORTER_ASSERT(r, 0 == buffer.size());
|
||||
|
||||
sk_sp<SkROBuffer> roBuffer = sk_sp<SkROBuffer>(buffer.newRBufferSnapshot());
|
||||
sk_sp<SkROBuffer> roBuffer = buffer.makeROBufferSnapshot();
|
||||
REPORTER_ASSERT(r, roBuffer);
|
||||
if (roBuffer) {
|
||||
REPORTER_ASSERT(r, roBuffer->size() == 0);
|
||||
@ -339,7 +338,7 @@ DEF_TEST(RWBuffer_noAppend, r) {
|
||||
REPORTER_ASSERT(r, !iter.next());
|
||||
}
|
||||
|
||||
std::unique_ptr<SkStream> stream(buffer.newStreamSnapshot());
|
||||
std::unique_ptr<SkStream> stream(buffer.makeStreamSnapshot());
|
||||
REPORTER_ASSERT(r, stream);
|
||||
if (stream) {
|
||||
REPORTER_ASSERT(r, stream->hasLength());
|
||||
|
Loading…
Reference in New Issue
Block a user