add explicit API to know if the writebuffer is using its external storage

To help with https://skia-review.googlesource.com/c/skia/+/114262

Bug: skia:
Change-Id: I83fcccdec0dbaef28d0e557ea6fb9bd0ed59ed44
Reviewed-on: https://skia-review.googlesource.com/114284
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-03-14 09:52:02 -04:00 committed by Skia Commit-Bot
parent 7d2b16ad13
commit 253258419b
4 changed files with 41 additions and 0 deletions

View File

@ -33,6 +33,10 @@ SkBinaryWriteBuffer::~SkBinaryWriteBuffer() {
SkSafeUnref(fTFSet);
}
bool SkBinaryWriteBuffer::usingInitialStorage() const {
return fWriter.usingInitialStorage();
}
void SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) {
fWriter.write32(SkToU32(size));
fWriter.writePad(data, size);

View File

@ -92,6 +92,10 @@ public:
size_t bytesWritten() const { return fWriter.bytesWritten(); }
// Returns true iff all of the bytes written so far are stored in the initial storage
// buffer provided in the constructor or the most recent call to reset.
bool usingInitialStorage() const;
void writeByteArray(const void* data, size_t size) override;
void writeBool(bool value) override;
void writeScalar(SkScalar value) override;

View File

@ -39,6 +39,10 @@ public:
// return the current offset (will always be a multiple of 4)
size_t bytesWritten() const { return fUsed; }
// Returns true iff all of the bytes written so far are stored in the initial storage
// buffer provided in the constructor or the most recent call to reset.
bool usingInitialStorage() const { return fData == fExternal; }
SK_ATTR_DEPRECATED("use bytesWritten")
size_t size() const { return this->bytesWritten(); }

View File

@ -637,3 +637,32 @@ DEF_TEST(Annotations, reporter) {
TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
canvas.drawPicture(pict1);
}
DEF_TEST(WriteBuffer_storage, reporter) {
enum {
kSize = 32
};
int32_t storage[kSize/4];
char src[kSize];
sk_bzero(src, kSize);
SkBinaryWriteBuffer writer(storage, kSize);
REPORTER_ASSERT(reporter, writer.usingInitialStorage());
REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
writer.write(src, kSize - 4);
REPORTER_ASSERT(reporter, writer.usingInitialStorage());
REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
writer.writeInt(0);
REPORTER_ASSERT(reporter, writer.usingInitialStorage());
REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
writer.reset(storage, kSize-4);
REPORTER_ASSERT(reporter, writer.usingInitialStorage());
REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
writer.write(src, kSize - 4);
REPORTER_ASSERT(reporter, writer.usingInitialStorage());
REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
writer.writeInt(0);
REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change
REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
}