OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkPicturePlayback.h" | 8 #include "SkPicturePlayback.h" |
9 #include "SkPictureRecord.h" | 9 #include "SkPictureRecord.h" |
10 #include "SkTypeface.h" | 10 #include "SkTypeface.h" |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 300 } |
301 } | 301 } |
302 return false; | 302 return false; |
303 } | 303 } |
304 | 304 |
305 /////////////////////////////////////////////////////////////////////////////// | 305 /////////////////////////////////////////////////////////////////////////////// |
306 /////////////////////////////////////////////////////////////////////////////// | 306 /////////////////////////////////////////////////////////////////////////////// |
307 | 307 |
308 #include "SkStream.h" | 308 #include "SkStream.h" |
309 | 309 |
310 static void writeTagSize(SkWriteBuffer& buffer, uint32_t tag, | 310 static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, uint32_t size) { |
311 uint32_t size) { | |
312 buffer.writeUInt(tag); | 311 buffer.writeUInt(tag); |
313 buffer.writeUInt(size); | 312 buffer.writeUInt(size); |
314 } | 313 } |
315 | 314 |
316 static void writeTagSize(SkWStream* stream, uint32_t tag, | 315 static void write_tag_size(SkWStream* stream, uint32_t tag, uint32_t size) { |
317 uint32_t size) { | |
318 stream->write32(tag); | 316 stream->write32(tag); |
319 stream->write32(size); | 317 stream->write32(size); |
320 } | 318 } |
321 | 319 |
322 static void writeFactories(SkWStream* stream, const SkFactorySet& rec) { | 320 static size_t compute_chunk_size(SkFlattenable::Factory* array, int count) { |
| 321 size_t size = 4; // for 'count' |
| 322 |
| 323 for (int i = 0; i < count; i++) { |
| 324 const char* name = SkFlattenable::FactoryToName(array[i]); |
| 325 if (NULL == name || 0 == *name) { |
| 326 size += SkWStream::SizeOfPackedUInt(0); |
| 327 } else { |
| 328 size_t len = strlen(name); |
| 329 size += SkWStream::SizeOfPackedUInt(len); |
| 330 size += len; |
| 331 } |
| 332 } |
| 333 |
| 334 return size; |
| 335 } |
| 336 |
| 337 static void write_factories(SkWStream* stream, const SkFactorySet& rec) { |
323 int count = rec.count(); | 338 int count = rec.count(); |
324 | 339 |
325 writeTagSize(stream, SK_PICT_FACTORY_TAG, count); | |
326 | |
327 SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); | 340 SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); |
328 SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); | 341 SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); |
329 rec.copyToArray(array); | 342 rec.copyToArray(array); |
330 | 343 |
| 344 size_t size = compute_chunk_size(array, count); |
| 345 |
| 346 // TODO: write_tag_size should really take a size_t |
| 347 write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); |
| 348 stream->write32(count); |
| 349 |
331 for (int i = 0; i < count; i++) { | 350 for (int i = 0; i < count; i++) { |
332 const char* name = SkFlattenable::FactoryToName(array[i]); | 351 const char* name = SkFlattenable::FactoryToName(array[i]); |
333 // SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); | 352 // SkDebugf("---- write factories [%d] %p <%s>\n", i, array[i], name); |
334 if (NULL == name || 0 == *name) { | 353 if (NULL == name || 0 == *name) { |
335 stream->writePackedUInt(0); | 354 stream->writePackedUInt(0); |
336 } else { | 355 } else { |
337 uint32_t len = strlen(name); | 356 uint32_t len = strlen(name); |
338 stream->writePackedUInt(len); | 357 stream->writePackedUInt(len); |
339 stream->write(name, len); | 358 stream->write(name, len); |
340 } | 359 } |
341 } | 360 } |
| 361 |
| 362 |
342 } | 363 } |
343 | 364 |
344 static void writeTypefaces(SkWStream* stream, const SkRefCntSet& rec) { | 365 static void writeTypefaces(SkWStream* stream, const SkRefCntSet& rec) { |
345 int count = rec.count(); | 366 int count = rec.count(); |
346 | 367 |
347 writeTagSize(stream, SK_PICT_TYPEFACE_TAG, count); | 368 write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count); |
348 | 369 |
349 SkAutoSTMalloc<16, SkTypeface*> storage(count); | 370 SkAutoSTMalloc<16, SkTypeface*> storage(count); |
350 SkTypeface** array = (SkTypeface**)storage.get(); | 371 SkTypeface** array = (SkTypeface**)storage.get(); |
351 rec.copyToArray((SkRefCnt**)array); | 372 rec.copyToArray((SkRefCnt**)array); |
352 | 373 |
353 for (int i = 0; i < count; i++) { | 374 for (int i = 0; i < count; i++) { |
354 array[i]->serialize(stream); | 375 array[i]->serialize(stream); |
355 } | 376 } |
356 } | 377 } |
357 | 378 |
358 void SkPicturePlayback::flattenToBuffer(SkWriteBuffer& buffer) const { | 379 void SkPicturePlayback::flattenToBuffer(SkWriteBuffer& buffer) const { |
359 int i, n; | 380 int i, n; |
360 | 381 |
361 if ((n = SafeCount(fBitmaps)) > 0) { | 382 if ((n = SafeCount(fBitmaps)) > 0) { |
362 writeTagSize(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); | 383 write_tag_size(buffer, SK_PICT_BITMAP_BUFFER_TAG, n); |
363 for (i = 0; i < n; i++) { | 384 for (i = 0; i < n; i++) { |
364 buffer.writeBitmap((*fBitmaps)[i]); | 385 buffer.writeBitmap((*fBitmaps)[i]); |
365 } | 386 } |
366 } | 387 } |
367 | 388 |
368 if ((n = SafeCount(fPaints)) > 0) { | 389 if ((n = SafeCount(fPaints)) > 0) { |
369 writeTagSize(buffer, SK_PICT_PAINT_BUFFER_TAG, n); | 390 write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); |
370 for (i = 0; i < n; i++) { | 391 for (i = 0; i < n; i++) { |
371 buffer.writePaint((*fPaints)[i]); | 392 buffer.writePaint((*fPaints)[i]); |
372 } | 393 } |
373 } | 394 } |
374 | 395 |
375 if ((n = SafeCount(fPathHeap.get())) > 0) { | 396 if ((n = SafeCount(fPathHeap.get())) > 0) { |
376 writeTagSize(buffer, SK_PICT_PATH_BUFFER_TAG, n); | 397 write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); |
377 fPathHeap->flatten(buffer); | 398 fPathHeap->flatten(buffer); |
378 } | 399 } |
379 } | 400 } |
380 | 401 |
381 void SkPicturePlayback::serialize(SkWStream* stream, | 402 void SkPicturePlayback::serialize(SkWStream* stream, |
382 SkPicture::EncodeBitmap encoder) const { | 403 SkPicture::EncodeBitmap encoder) const { |
383 writeTagSize(stream, SK_PICT_READER_TAG, fOpData->size()); | 404 write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size()); |
384 stream->write(fOpData->bytes(), fOpData->size()); | 405 stream->write(fOpData->bytes(), fOpData->size()); |
385 | 406 |
386 if (fPictureCount > 0) { | 407 if (fPictureCount > 0) { |
387 writeTagSize(stream, SK_PICT_PICTURE_TAG, fPictureCount); | 408 write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount); |
388 for (int i = 0; i < fPictureCount; i++) { | 409 for (int i = 0; i < fPictureCount; i++) { |
389 fPictureRefs[i]->serialize(stream, encoder); | 410 fPictureRefs[i]->serialize(stream, encoder); |
390 } | 411 } |
391 } | 412 } |
392 | 413 |
393 // Write some of our data into a writebuffer, and then serialize that | 414 // Write some of our data into a writebuffer, and then serialize that |
394 // into our stream | 415 // into our stream |
395 { | 416 { |
396 SkRefCntSet typefaceSet; | 417 SkRefCntSet typefaceSet; |
397 SkFactorySet factSet; | 418 SkFactorySet factSet; |
398 | 419 |
399 SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); | 420 SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag); |
400 buffer.setTypefaceRecorder(&typefaceSet); | 421 buffer.setTypefaceRecorder(&typefaceSet); |
401 buffer.setFactoryRecorder(&factSet); | 422 buffer.setFactoryRecorder(&factSet); |
402 buffer.setBitmapEncoder(encoder); | 423 buffer.setBitmapEncoder(encoder); |
403 | 424 |
404 this->flattenToBuffer(buffer); | 425 this->flattenToBuffer(buffer); |
405 | 426 |
406 // We have to write these to sets into the stream *before* we write | 427 // We have to write these two sets into the stream *before* we write |
407 // the buffer, since parsing that buffer will require that we already | 428 // the buffer, since parsing that buffer will require that we already |
408 // have these sets available to use. | 429 // have these sets available to use. |
409 writeFactories(stream, factSet); | 430 write_factories(stream, factSet); |
410 writeTypefaces(stream, typefaceSet); | 431 writeTypefaces(stream, typefaceSet); |
411 | 432 |
412 writeTagSize(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); | 433 write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); |
413 buffer.writeToStream(stream); | 434 buffer.writeToStream(stream); |
414 } | 435 } |
415 | 436 |
416 stream->write32(SK_PICT_EOF_TAG); | 437 stream->write32(SK_PICT_EOF_TAG); |
417 } | 438 } |
418 | 439 |
419 void SkPicturePlayback::flatten(SkWriteBuffer& buffer) const { | 440 void SkPicturePlayback::flatten(SkWriteBuffer& buffer) const { |
420 writeTagSize(buffer, SK_PICT_READER_TAG, fOpData->size()); | 441 write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size()); |
421 buffer.writeByteArray(fOpData->bytes(), fOpData->size()); | 442 buffer.writeByteArray(fOpData->bytes(), fOpData->size()); |
422 | 443 |
423 if (fPictureCount > 0) { | 444 if (fPictureCount > 0) { |
424 writeTagSize(buffer, SK_PICT_PICTURE_TAG, fPictureCount); | 445 write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount); |
425 for (int i = 0; i < fPictureCount; i++) { | 446 for (int i = 0; i < fPictureCount; i++) { |
426 fPictureRefs[i]->flatten(buffer); | 447 fPictureRefs[i]->flatten(buffer); |
427 } | 448 } |
428 } | 449 } |
429 | 450 |
430 // Write this picture playback's data into a writebuffer | 451 // Write this picture playback's data into a writebuffer |
431 this->flattenToBuffer(buffer); | 452 this->flattenToBuffer(buffer); |
432 buffer.write32(SK_PICT_EOF_TAG); | 453 buffer.write32(SK_PICT_EOF_TAG); |
433 } | 454 } |
434 | 455 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 case SK_PICT_READER_TAG: { | 495 case SK_PICT_READER_TAG: { |
475 SkAutoMalloc storage(size); | 496 SkAutoMalloc storage(size); |
476 if (stream->read(storage.get(), size) != size) { | 497 if (stream->read(storage.get(), size) != size) { |
477 return false; | 498 return false; |
478 } | 499 } |
479 SkASSERT(NULL == fOpData); | 500 SkASSERT(NULL == fOpData); |
480 fOpData = SkData::NewFromMalloc(storage.detach(), size); | 501 fOpData = SkData::NewFromMalloc(storage.detach(), size); |
481 } break; | 502 } break; |
482 case SK_PICT_FACTORY_TAG: { | 503 case SK_PICT_FACTORY_TAG: { |
483 SkASSERT(!haveBuffer); | 504 SkASSERT(!haveBuffer); |
| 505 // Remove this code when v21 and below are no longer supported. At the |
| 506 // same time add a new 'count' variable and use it rather then reusing '
size'. |
| 507 #ifndef DISABLE_V21_COMPATIBILITY_CODE |
| 508 if (info.fVersion >= 22) { |
| 509 // in v22 this tag's size represents the size of the chunk in by
tes |
| 510 // and the number of factory strings is written out separately |
| 511 #endif |
| 512 size = stream->readU32(); |
| 513 #ifndef DISABLE_V21_COMPATIBILITY_CODE |
| 514 } |
| 515 #endif |
484 fFactoryPlayback = SkNEW_ARGS(SkFactoryPlayback, (size)); | 516 fFactoryPlayback = SkNEW_ARGS(SkFactoryPlayback, (size)); |
485 for (size_t i = 0; i < size; i++) { | 517 for (size_t i = 0; i < size; i++) { |
486 SkString str; | 518 SkString str; |
487 const size_t len = stream->readPackedUInt(); | 519 const size_t len = stream->readPackedUInt(); |
488 str.resize(len); | 520 str.resize(len); |
489 if (stream->read(str.writable_str(), len) != len) { | 521 if (stream->read(str.writable_str(), len) != len) { |
490 return false; | 522 return false; |
491 } | 523 } |
492 fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c
_str()); | 524 fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c
_str()); |
493 } | 525 } |
(...skipping 1154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1648 for (index = 0; index < fPictureCount; index++) | 1680 for (index = 0; index < fPictureCount; index++) |
1649 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer
), | 1681 bufferPtr += snprintf(bufferPtr, DUMP_BUFFER_SIZE - (bufferPtr - pBuffer
), |
1650 "picture%p, ", fPictureRefs[index]); | 1682 "picture%p, ", fPictureRefs[index]); |
1651 if (fPictureCount > 0) | 1683 if (fPictureCount > 0) |
1652 SkDebugf("%s0};\n", pBuffer); | 1684 SkDebugf("%s0};\n", pBuffer); |
1653 | 1685 |
1654 const_cast<SkPicturePlayback*>(this)->dumpStream(); | 1686 const_cast<SkPicturePlayback*>(this)->dumpStream(); |
1655 } | 1687 } |
1656 | 1688 |
1657 #endif | 1689 #endif |
OLD | NEW |