From 8f45e4d1b5351f8c2eb92a59edad76bf43afb21d Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Tue, 9 Jun 2020 09:56:18 -0500 Subject: [PATCH] use type name as identifier instead of factory This allows serializable types that can't be deserialized, e.g. for creating font descriptors even with effect deserialization disabled. Change-Id: I70a38ce2e34b192e5c87f6a341d090e5e444fc70 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/295193 Reviewed-by: Brian Osman Commit-Queue: Mike Klein --- src/core/SkWriteBuffer.cpp | 21 +++++++++------------ src/core/SkWriteBuffer.h | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp index f12099ab16..4e0424fac5 100644 --- a/src/core/SkWriteBuffer.cpp +++ b/src/core/SkWriteBuffer.cpp @@ -219,32 +219,29 @@ void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { * already written the string, we write its index instead. */ - SkFlattenable::Factory factory = flattenable->getFactory(); - SkASSERT(factory); if (fFactorySet) { + SkFlattenable::Factory factory = flattenable->getFactory(); + SkASSERT(factory); + this->write32(fFactorySet->add(factory)); } else { + const char* name = flattenable->getTypeName(); + SkASSERT(name); + SkASSERT(0 != strcmp("", name)); - if (uint32_t* indexPtr = fFlattenableDict.find(factory)) { + if (uint32_t* indexPtr = fFlattenableDict.find(name)) { // We will write the index as a 32-bit int. We want the first byte // that we send to be zero - this will act as a sentinel that we // have an index (not a string). This means that we will send the // the index shifted left by 8. The remaining 24-bits should be // plenty to store the index. Note that this strategy depends on - // being little endian. + // being little endian, and type names being non-empty. SkASSERT(0 == *indexPtr >> 24); this->write32(*indexPtr << 8); } else { - const char* name = flattenable->getTypeName(); - SkASSERT(name); - // Otherwise write the string. Clients should not use the empty - // string as a name, or we will have a problem. - SkASSERT(0 != strcmp("", name)); this->writeString(name); - - // Add key to dictionary. - fFlattenableDict.set(factory, fFlattenableDict.count() + 1); + fFlattenableDict.set(name, fFlattenableDict.count() + 1); } } diff --git a/src/core/SkWriteBuffer.h b/src/core/SkWriteBuffer.h index 5bd58e9156..0d6b42a384 100644 --- a/src/core/SkWriteBuffer.h +++ b/src/core/SkWriteBuffer.h @@ -133,7 +133,7 @@ private: SkWriter32 fWriter; // Only used if we do not have an fFactorySet - SkTHashMap fFlattenableDict; + SkTHashMap fFlattenableDict; }; #endif // SkWriteBuffer_DEFINED