Chromium Code Reviews| Index: src/core/SkWriteBuffer.cpp |
| diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp |
| index 1674b931eec9498d7f38f7c3ecb6326686d36616..bac143afd02d03effbd5b18aecba318351338c04 100644 |
| --- a/src/core/SkWriteBuffer.cpp |
| +++ b/src/core/SkWriteBuffer.cpp |
| @@ -18,7 +18,8 @@ SkWriteBuffer::SkWriteBuffer(uint32_t flags) |
| : fFlags(flags) |
| , fFactorySet(nullptr) |
| , fBitmapHeap(nullptr) |
| - , fTFSet(nullptr) { |
| + , fTFSet(nullptr) |
| + , fNextIndex(1) { |
| } |
| SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
| @@ -26,7 +27,8 @@ SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
| , fFactorySet(nullptr) |
| , fWriter(storage, storageSize) |
| , fBitmapHeap(nullptr) |
| - , fTFSet(nullptr) { |
| + , fTFSet(nullptr) |
| + , fNextIndex(1) { |
| } |
| SkWriteBuffer::~SkWriteBuffer() { |
| @@ -258,47 +260,60 @@ void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
| void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| /* |
| - * If we have a factoryset, then the first 32bits tell us... |
| + * If this is not a validating buffer, then the first 32bits tell us... |
| * 0: failure to write the flattenable |
| - * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet |
| - * If we don't have a factoryset, then the first "ptr" is either the |
| - * factory, or null for failure. |
| - * |
| - * The distinction is important, since 0-index is 32bits (always), but a |
| - * 0-functionptr might be 32 or 64 bits. |
| + * >0: (1-based) index into fFactorySet or fFlattenableDict |
| */ |
| if (nullptr == flattenable) { |
| if (this->isValidating()) { |
| this->writeString(""); |
| - } else if (fFactorySet != nullptr) { |
| - this->write32(0); |
| } else { |
| - this->writeFunctionPtr(nullptr); |
| + this->write32(0); |
| } |
| return; |
| } |
| - SkFlattenable::Factory factory = flattenable->getFactory(); |
| - SkASSERT(factory != nullptr); |
| + const char* name; |
| + SkFlattenable::Factory factory; |
| + if (this->isValidating() || !fFactorySet) { |
| + name = flattenable->getTypeName(); |
| + SkASSERT(name); |
| + } else { |
| + factory = flattenable->getFactory(); |
| + SkASSERT(factory); |
| + } |
| /* |
| * We can write 1 of 3 versions of the flattenable: |
| - * 1. function-ptr : this is the fastest for the reader, but assumes that |
| - * the writer and reader are in the same process. |
| + * 1. string : this is used by the validating read/write buffers. |
| * 2. index into fFactorySet : This is assumes the writer will later |
| * resolve the function-ptrs into strings for its reader. SkPicture |
| * does exactly this, by writing a table of names (matching the indices) |
| * up front in its serialized form. |
| - * 3. index into fNamedFactorySet. fNamedFactorySet will also store the |
| - * name. SkGPipe uses this technique so it can write the name to its |
| - * stream before writing the flattenable. |
| + * 3. index into fFlattenableDict (plus string if necessary): We store |
| + * the string to allow the reader to specify its own factories after |
| + * write time. In order to improve compression, if we have already |
| + * written the string, we only write its index. |
| */ |
| if (this->isValidating()) { |
| - this->writeString(flattenable->getTypeName()); |
| + this->writeString(name); |
| } else if (fFactorySet) { |
| this->write32(fFactorySet->add(factory)); |
| } else { |
| - this->writeFunctionPtr((void*)factory); |
| + SkString key(name); |
| + uint32_t* indexPtr = fFlattenableDict.find(key); |
| + if (indexPtr) { |
|
mtklein
2016/04/19 18:14:55
I find it nice to scope these lookups a little tig
msarett
2016/04/19 18:55:51
Done.
|
| + // Only write the index if the key is already in our dictionary. |
| + this->write32(*indexPtr); |
| + } else { |
| + // Write the index and the key |
| + this->write32(fNextIndex); |
| + this->writeString(name); |
| + |
| + // Add key to dictionary |
| + fFlattenableDict.set(key, fNextIndex); |
| + fNextIndex++; |
|
mtklein
2016/04/19 18:14:55
Is fNextIndex redundant with fFlattenableDict.coun
msarett
2016/04/19 18:55:51
Hmm yeah... Nice!
|
| + } |
| } |
| // make room for the size of the flattened object |