diff options
author | 2016-04-22 12:43:07 -0700 | |
---|---|---|
committer | 2016-04-22 12:43:07 -0700 | |
commit | a3b3b238f507a6ec7f43febc6bf0bb17e04e770f (patch) | |
tree | 4a699b7a262309f9695db86c2e565109309aeb27 /tests | |
parent | 4ff7c7423661db10ebaabda782fc8329e7a5f7ee (diff) |
Enable flattening/unflattening with custom unflatten procs
Now flattenables are serialized using a string name, so that
flattenables do not necessarily need to be registered before
serialization. They just need to override getTypeName().
Allows custom unflatten procs to be set on the SkReadBuffer.
This is optional if the flattenable is registered, but otherwise
must be called.
This was split off from:
https://codereview.chromium.org/1837913003/
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1858323002
Review URL: https://codereview.chromium.org/1858323002
Diffstat (limited to 'tests')
-rw-r--r-- | tests/BitmapHeapTest.cpp | 2 | ||||
-rw-r--r-- | tests/FlattenableCustomFactory.cpp | 95 | ||||
-rw-r--r-- | tests/SerializationTest.cpp | 8 |
3 files changed, 99 insertions, 6 deletions
diff --git a/tests/BitmapHeapTest.cpp b/tests/BitmapHeapTest.cpp index 89e6faf764..f9d4ee19bb 100644 --- a/tests/BitmapHeapTest.cpp +++ b/tests/BitmapHeapTest.cpp @@ -88,9 +88,7 @@ DEF_TEST(BitmapHeap, reporter) { index = dictionary.find(*bitmapShader); heap.endAddingOwnersDeferral(false); - // The dictionary should report the same index since the new entry is identical. // The bitmap heap should contain the bitmap, but with no references. - REPORTER_ASSERT(reporter, 1 == index); REPORTER_ASSERT(reporter, heap.count() == 1); REPORTER_ASSERT(reporter, SkBitmapHeapTester::GetRefCount(heap.getEntry(0)) == 0); } diff --git a/tests/FlattenableCustomFactory.cpp b/tests/FlattenableCustomFactory.cpp new file mode 100644 index 0000000000..794f76872a --- /dev/null +++ b/tests/FlattenableCustomFactory.cpp @@ -0,0 +1,95 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkFlattenable.h" +#include "SkReadBuffer.h" +#include "SkWriteBuffer.h" +#include "Test.h" + +class IntFlattenable : public SkFlattenable { +public: + IntFlattenable(uint32_t a, uint32_t b, uint32_t c, uint32_t d) + : fA(a) + , fB(b) + , fC(c) + , fD(d) + {} + + void flatten(SkWriteBuffer& buffer) const override { + buffer.writeUInt(fA); + buffer.writeUInt(fB); + buffer.writeUInt(fC); + buffer.writeUInt(fD); + } + + Factory getFactory() const override { return nullptr; } + + uint32_t a() const { return fA; } + uint32_t b() const { return fB; } + uint32_t c() const { return fC; } + uint32_t d() const { return fD; } + + const char* getTypeName() const override { return "IntFlattenable"; } + +private: + uint32_t fA; + uint32_t fB; + uint32_t fC; + uint32_t fD; +}; + +static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) { + uint32_t a = buffer.readUInt(); + uint32_t b = buffer.readUInt(); + uint32_t c = buffer.readUInt(); + uint32_t d = buffer.readUInt(); + return sk_sp<SkFlattenable>(new IntFlattenable(a + 1, b + 1, c + 1, d + 1)); +} + +DEF_TEST(UnflattenWithCustomFactory, r) { + // Create and flatten the test flattenable + SkWriteBuffer writeBuffer; + SkAutoTUnref<SkFlattenable> flattenable1(new IntFlattenable(1, 2, 3, 4)); + writeBuffer.writeFlattenable(flattenable1); + SkAutoTUnref<SkFlattenable> flattenable2(new IntFlattenable(2, 3, 4, 5)); + writeBuffer.writeFlattenable(flattenable2); + SkAutoTUnref<SkFlattenable> flattenable3(new IntFlattenable(3, 4, 5, 6)); + writeBuffer.writeFlattenable(flattenable3); + + // Copy the contents of the write buffer into a read buffer + sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); + writeBuffer.writeToMemory(data->writable_data()); + SkReadBuffer readBuffer(data->data(), data->size()); + + // Register a custom factory with the read buffer + readBuffer.setCustomFactory(SkString("IntFlattenable"), &custom_create_proc); + + // Unflatten and verify the flattenables + SkAutoTUnref<IntFlattenable> out1((IntFlattenable*) readBuffer.readFlattenable( + SkFlattenable::kSkUnused_Type)); + REPORTER_ASSERT(r, out1); + REPORTER_ASSERT(r, 2 == out1->a()); + REPORTER_ASSERT(r, 3 == out1->b()); + REPORTER_ASSERT(r, 4 == out1->c()); + REPORTER_ASSERT(r, 5 == out1->d()); + + SkAutoTUnref<IntFlattenable> out2((IntFlattenable*) readBuffer.readFlattenable( + SkFlattenable::kSkUnused_Type)); + REPORTER_ASSERT(r, out2); + REPORTER_ASSERT(r, 3 == out2->a()); + REPORTER_ASSERT(r, 4 == out2->b()); + REPORTER_ASSERT(r, 5 == out2->c()); + REPORTER_ASSERT(r, 6 == out2->d()); + + SkAutoTUnref<IntFlattenable> out3((IntFlattenable*) readBuffer.readFlattenable( + SkFlattenable::kSkUnused_Type)); + REPORTER_ASSERT(r, out3); + REPORTER_ASSERT(r, 4 == out3->a()); + REPORTER_ASSERT(r, 5 == out3->b()); + REPORTER_ASSERT(r, 6 == out3->c()); + REPORTER_ASSERT(r, 7 == out3->d()); +} diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp index 9bdfe1e0e6..a8f253f025 100644 --- a/tests/SerializationTest.cpp +++ b/tests/SerializationTest.cpp @@ -139,7 +139,7 @@ template<> struct SerializationTestUtils<SkString, true> { template<typename T, bool testInvalid> static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { - SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer; SerializationUtils<T>::Write(writer, testObj); size_t bytesWritten = writer.bytesWritten(); REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); @@ -177,7 +177,7 @@ static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { template<typename T> static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, skiatest::Reporter* reporter) { - SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer; SerializationUtils<T>::Write(writer, testObj); size_t bytesWritten = writer.bytesWritten(); REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); @@ -215,7 +215,7 @@ static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, template<typename T> static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { - SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer; SerializationUtils<T>::Write(writer, data, kArraySize); size_t bytesWritten = writer.bytesWritten(); // This should write the length (in 4 bytes) and the array @@ -533,7 +533,7 @@ DEF_TEST(Serialization, reporter) { sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); // Serialize picture - SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer; pict->flatten(writer); size_t size = writer.bytesWritten(); SkAutoTMalloc<unsigned char> data(size); |