diff --git a/include/core/SkWriteBuffer.h b/include/core/SkWriteBuffer.h index ab56f9d48d..332dfaea40 100644 --- a/include/core/SkWriteBuffer.h +++ b/include/core/SkWriteBuffer.h @@ -24,18 +24,15 @@ class SkRefCntSet; class SkWriteBuffer { public: - SkWriteBuffer(); - SkWriteBuffer(void* initialStorage, size_t storageSize); - ~SkWriteBuffer(); - enum Flags { kCrossProcess_Flag = 1 << 0, kValidation_Flag = 1 << 1, }; - void setFlags(uint32_t flags) { fFlags = flags; } - uint32_t getFlags() const { return fFlags; } - bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } + SkWriteBuffer(uint32_t flags = 0); + SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0); + ~SkWriteBuffer(); + bool isCrossProcess() const { return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); } @@ -107,7 +104,9 @@ public: void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder); private: - uint32_t fFlags; + bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } + + const uint32_t fFlags; SkFactorySet* fFactorySet; SkNamedFactorySet* fNamedFactorySet; SkWriter32 fWriter; diff --git a/src/core/SkFlattenableSerialization.cpp b/src/core/SkFlattenableSerialization.cpp index 13fbe6edbf..f34e64170c 100644 --- a/src/core/SkFlattenableSerialization.cpp +++ b/src/core/SkFlattenableSerialization.cpp @@ -12,8 +12,7 @@ #include "SkWriteBuffer.h" SkData* SkValidatingSerializeFlattenable(SkFlattenable* flattenable) { - SkWriteBuffer writer; - writer.setFlags(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); writer.writeFlattenable(flattenable); uint32_t size = writer.bytesWritten(); void* data = sk_malloc_throw(size); diff --git a/src/core/SkPictureFlat.cpp b/src/core/SkPictureFlat.cpp index e3a6f1325a..7b2e67f5ef 100644 --- a/src/core/SkPictureFlat.cpp +++ b/src/core/SkPictureFlat.cpp @@ -60,12 +60,12 @@ SkRefCnt* SkTypefacePlayback::set(int index, SkRefCnt* obj) { /////////////////////////////////////////////////////////////////////////////// -SkFlatController::SkFlatController() +SkFlatController::SkFlatController(uint32_t writeBufferFlags) : fBitmapHeap(NULL) , fTypefaceSet(NULL) , fTypefacePlayback(NULL) , fFactorySet(NULL) -, fWriteBufferFlags(0) {} +, fWriteBufferFlags(writeBufferFlags) {} SkFlatController::~SkFlatController() { SkSafeUnref(fBitmapHeap); diff --git a/src/core/SkPictureFlat.h b/src/core/SkPictureFlat.h index 431fa96037..7973e65398 100644 --- a/src/core/SkPictureFlat.h +++ b/src/core/SkPictureFlat.h @@ -168,7 +168,7 @@ class SkFlatController : public SkRefCnt { public: SK_DECLARE_INST_COUNT(SkFlatController) - SkFlatController(); + SkFlatController(uint32_t writeBufferFlags = 0); virtual ~SkFlatController(); /** * Return a new block of memory for the SkFlatDictionary to use. @@ -242,17 +242,12 @@ protected: */ SkNamedFactorySet* setNamedFactorySet(SkNamedFactorySet*); - /** - * Set the flags to be used during flattening. - */ - void setWriteBufferFlags(uint32_t flags) { fWriteBufferFlags = flags; } - private: SkBitmapHeap* fBitmapHeap; SkRefCntSet* fTypefaceSet; SkTypefacePlayback* fTypefacePlayback; SkNamedFactorySet* fFactorySet; - uint32_t fWriteBufferFlags; + const uint32_t fWriteBufferFlags; typedef SkRefCnt INHERITED; }; @@ -264,12 +259,11 @@ public: static SkFlatData* Create(SkFlatController* controller, const T& obj, int index) { // A buffer of 256 bytes should fit most paints, regions, and matrices. uint32_t storage[64]; - SkWriteBuffer buffer(storage, sizeof(storage)); + SkWriteBuffer buffer(storage, sizeof(storage), controller->getWriteBufferFlags()); buffer.setBitmapHeap(controller->getBitmapHeap()); buffer.setTypefaceRecorder(controller->getTypefaceSet()); buffer.setNamedFactoryRecorder(controller->getNamedFactorySet()); - buffer.setFlags(controller->getWriteBufferFlags()); Traits::flatten(buffer, obj); size_t size = buffer.bytesWritten(); @@ -367,6 +361,7 @@ class SkFlatDictionary { public: explicit SkFlatDictionary(SkFlatController* controller) : fController(SkRef(controller)) + , fScratch(controller->getWriteBufferFlags()) , fReady(false) { this->reset(); } @@ -496,7 +491,6 @@ private: fScratch.setBitmapHeap(fController->getBitmapHeap()); fScratch.setTypefaceRecorder(fController->getTypefaceSet()); fScratch.setNamedFactoryRecorder(fController->getNamedFactorySet()); - fScratch.setFlags(fController->getWriteBufferFlags()); fReady = true; } diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp index 02d8828977..e71a17fda5 100644 --- a/src/core/SkPicturePlayback.cpp +++ b/src/core/SkPicturePlayback.cpp @@ -411,8 +411,7 @@ void SkPicturePlayback::serialize(SkWStream* stream, SkRefCntSet typefaceSet; SkFactorySet factSet; - SkWriteBuffer buffer; - buffer.setFlags(SkWriteBuffer::kCrossProcess_Flag); + SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); buffer.setTypefaceRecorder(&typefaceSet); buffer.setFactoryRecorder(&factSet); buffer.setBitmapEncoder(encoder); diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp index cbbb4c94fc..a295a8c88c 100644 --- a/src/core/SkWriteBuffer.cpp +++ b/src/core/SkWriteBuffer.cpp @@ -14,8 +14,8 @@ #include "SkStream.h" #include "SkTypeface.h" -SkWriteBuffer::SkWriteBuffer() - : fFlags(0) +SkWriteBuffer::SkWriteBuffer(uint32_t flags) + : fFlags(flags) , fFactorySet(NULL) , fNamedFactorySet(NULL) , fBitmapHeap(NULL) @@ -23,8 +23,8 @@ SkWriteBuffer::SkWriteBuffer() , fBitmapEncoder(NULL) { } -SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize) - : fFlags(0) +SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) + : fFlags(flags) , fFactorySet(NULL) , fNamedFactorySet(NULL) , fWriter(storage, storageSize) diff --git a/src/pipe/SkGPipeWrite.cpp b/src/pipe/SkGPipeWrite.cpp index ce58ca6122..54e3bead69 100644 --- a/src/pipe/SkGPipeWrite.cpp +++ b/src/pipe/SkGPipeWrite.cpp @@ -71,11 +71,11 @@ static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { class FlattenableHeap : public SkFlatController { public: FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess) - : fNumFlatsToKeep(numFlatsToKeep) { + : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0) + , fNumFlatsToKeep(numFlatsToKeep) { SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset)); if (isCrossProcess) { this->setNamedFactorySet(fset); - this->setWriteBufferFlags(SkWriteBuffer::kCrossProcess_Flag); } } @@ -109,6 +109,8 @@ private: SkTDArray fFlatsThatMustBeKept; SkTDArray fPointers; const int fNumFlatsToKeep; + + typedef SkFlatController INHERITED; }; void FlattenableHeap::unalloc(void* ptr) { diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp index 2e02c3e714..f35f568a64 100644 --- a/tests/SerializationTest.cpp +++ b/tests/SerializationTest.cpp @@ -110,8 +110,7 @@ template<> struct SerializationUtils { template static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { - SkWriteBuffer writer; - writer.setFlags(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); SerializationUtils::Write(writer, testObj); size_t bytesWritten = writer.bytesWritten(); REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); @@ -141,8 +140,7 @@ static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { template static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, skiatest::Reporter* reporter) { - SkWriteBuffer writer; - writer.setFlags(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); SerializationUtils::Write(writer, testObj); size_t bytesWritten = writer.bytesWritten(); REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); @@ -180,8 +178,7 @@ static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, template static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { - SkWriteBuffer writer; - writer.setFlags(SkWriteBuffer::kValidation_Flag); + SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); SerializationUtils::Write(writer, data, kArraySize); size_t bytesWritten = writer.bytesWritten(); // This should write the length (in 4 bytes) and the array