| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkWriteBuffer.h" | 8 #include "SkWriteBuffer.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkBitmapHeap.h" | 10 #include "SkBitmapHeap.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 /* | 280 /* |
| 281 * If we have a factoryset, then the first 32bits tell us... | 281 * If we have a factoryset, then the first 32bits tell us... |
| 282 * 0: failure to write the flattenable | 282 * 0: failure to write the flattenable |
| 283 * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet | 283 * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet |
| 284 * If we don't have a factoryset, then the first "ptr" is either the | 284 * If we don't have a factoryset, then the first "ptr" is either the |
| 285 * factory, or null for failure. | 285 * factory, or null for failure. |
| 286 * | 286 * |
| 287 * The distinction is important, since 0-index is 32bits (always), but a | 287 * The distinction is important, since 0-index is 32bits (always), but a |
| 288 * 0-functionptr might be 32 or 64 bits. | 288 * 0-functionptr might be 32 or 64 bits. |
| 289 */ | 289 */ |
| 290 bool useString = (fFactorySet == nullptr && fNamedFactorySet == nullptr) || |
| 291 this->isValidating(); |
| 290 if (nullptr == flattenable) { | 292 if (nullptr == flattenable) { |
| 291 if (this->isValidating()) { | 293 if (useString) { |
| 292 this->writeString(""); | 294 this->writeString(""); |
| 293 } else if (fFactorySet != nullptr || fNamedFactorySet != nullptr) { | 295 } else { |
| 294 this->write32(0); | 296 this->write32(0); |
| 295 } else { | |
| 296 this->writeFunctionPtr(nullptr); | |
| 297 } | 297 } |
| 298 return; | 298 return; |
| 299 } | 299 } |
| 300 | 300 |
| 301 SkFlattenable::Factory factory = flattenable->getFactory(); | 301 const char* name; |
| 302 SkASSERT(factory != nullptr); | 302 SkFlattenable::Factory factory; |
| 303 if (useString) { |
| 304 name = flattenable->getTypeName(); |
| 305 SkASSERT(name != nullptr); |
| 306 } else { |
| 307 factory = flattenable->getFactory(); |
| 308 SkASSERT(factory); |
| 309 } |
| 303 | 310 |
| 304 /* | 311 /* |
| 305 * We can write 1 of 3 versions of the flattenable: | 312 * We can write 1 of 3 versions of the flattenable: |
| 306 * 1. function-ptr : this is the fastest for the reader, but assumes that | 313 * 1. string : this allows us flatten even if the flattenable is not in |
| 307 * the writer and reader are in the same process. | 314 * the global registry (as long as it overrides getTypeName()). |
| 308 * 2. index into fFactorySet : This is assumes the writer will later | 315 * 2. index into fFactorySet : This is assumes the writer will later |
| 309 * resolve the function-ptrs into strings for its reader. SkPicture | 316 * resolve the function-ptrs into strings for its reader. SkPicture |
| 310 * does exactly this, by writing a table of names (matching the indices
) | 317 * does exactly this, by writing a table of names (matching the indices
) |
| 311 * up front in its serialized form. | 318 * up front in its serialized form. |
| 312 * 3. index into fNamedFactorySet. fNamedFactorySet will also store the | 319 * 3. index into fNamedFactorySet. fNamedFactorySet will also store the |
| 313 * name. SkGPipe uses this technique so it can write the name to its | 320 * name. SkGPipe uses this technique so it can write the name to its |
| 314 * stream before writing the flattenable. | 321 * stream before writing the flattenable. |
| 315 */ | 322 */ |
| 316 if (this->isValidating()) { | 323 if (useString) { |
| 317 this->writeString(flattenable->getTypeName()); | 324 this->writeString(flattenable->getTypeName()); |
| 318 } else if (fFactorySet) { | 325 } else if (fFactorySet) { |
| 319 this->write32(fFactorySet->add(factory)); | 326 this->write32(fFactorySet->add(factory)); |
| 320 } else if (fNamedFactorySet) { | 327 } else { |
| 328 SkASSERT(fNamedFactorySet); |
| 321 int32_t index = fNamedFactorySet->find(factory); | 329 int32_t index = fNamedFactorySet->find(factory); |
| 322 this->write32(index); | 330 this->write32(index); |
| 323 if (0 == index) { | 331 if (0 == index) { |
| 324 return; | 332 return; |
| 325 } | 333 } |
| 326 } else { | |
| 327 this->writeFunctionPtr((void*)factory); | |
| 328 } | 334 } |
| 329 | 335 |
| 330 // make room for the size of the flattened object | 336 // make room for the size of the flattened object |
| 331 (void)fWriter.reserve(sizeof(uint32_t)); | 337 (void)fWriter.reserve(sizeof(uint32_t)); |
| 332 // record the current size, so we can subtract after the object writes. | 338 // record the current size, so we can subtract after the object writes. |
| 333 size_t offset = fWriter.bytesWritten(); | 339 size_t offset = fWriter.bytesWritten(); |
| 334 // now flatten the object | 340 // now flatten the object |
| 335 flattenable->flatten(*this); | 341 flattenable->flatten(*this); |
| 336 size_t objSize = fWriter.bytesWritten() - offset; | 342 size_t objSize = fWriter.bytesWritten() - offset; |
| 337 // record the obj's size | 343 // record the obj's size |
| 338 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 344 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
| 339 } | 345 } |
| OLD | NEW |