aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/core/SkWriteBuffer.cpp4
-rw-r--r--src/core/SkWriteBuffer.h4
-rw-r--r--src/core/SkWriter32.h4
-rw-r--r--tests/SerializationTest.cpp29
4 files changed, 41 insertions, 0 deletions
diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp
index 2bfcd05adb..de0b613996 100644
--- a/src/core/SkWriteBuffer.cpp
+++ b/src/core/SkWriteBuffer.cpp
@@ -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);
diff --git a/src/core/SkWriteBuffer.h b/src/core/SkWriteBuffer.h
index 82691b3c49..c3d6f570bf 100644
--- a/src/core/SkWriteBuffer.h
+++ b/src/core/SkWriteBuffer.h
@@ -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;
diff --git a/src/core/SkWriter32.h b/src/core/SkWriter32.h
index e81178d47e..1e004a8b1d 100644
--- a/src/core/SkWriter32.h
+++ b/src/core/SkWriter32.h
@@ -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(); }
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index 2543870667..da797398c1 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -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);
+}