| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include <new> | 7 #include <new> |
| 8 #include "SkImageGenerator.h" | 8 #include "SkImageGenerator.h" |
| 9 #include "SkPictureData.h" | 9 #include "SkPictureData.h" |
| 10 #include "SkPictureRecord.h" | 10 #include "SkPictureRecord.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 SkPictureData::SkPictureData(const SkPictureRecord& record, | 36 SkPictureData::SkPictureData(const SkPictureRecord& record, |
| 37 const SkPictInfo& info) | 37 const SkPictInfo& info) |
| 38 : fInfo(info) { | 38 : fInfo(info) { |
| 39 | 39 |
| 40 this->init(); | 40 this->init(); |
| 41 | 41 |
| 42 fOpData = record.opData(); | 42 fOpData = record.opData(); |
| 43 | 43 |
| 44 fContentInfo.set(record.fContentInfo); | 44 fContentInfo.set(record.fContentInfo); |
| 45 | 45 |
| 46 fBitmaps.reset(); // we never make bitmaps (anymore) during recording | |
| 47 fPaints = record.fPaints; | 46 fPaints = record.fPaints; |
| 48 | 47 |
| 49 fPaths.reset(record.fPaths.count()); | 48 fPaths.reset(record.fPaths.count()); |
| 50 record.fPaths.foreach([this](const SkPath& path, int n) { | 49 record.fPaths.foreach([this](const SkPath& path, int n) { |
| 51 // These indices are logically 1-based, but we need to serialize them | 50 // These indices are logically 1-based, but we need to serialize them |
| 52 // 0-based to keep the deserializing SkPictureData::getPath() working. | 51 // 0-based to keep the deserializing SkPictureData::getPath() working. |
| 53 fPaths[n-1] = path; | 52 fPaths[n-1] = path; |
| 54 }); | 53 }); |
| 55 | 54 |
| 56 this->initForPlayback(); | 55 this->initForPlayback(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 127 |
| 129 for (int i = 0; i < fImageCount; i++) { | 128 for (int i = 0; i < fImageCount; i++) { |
| 130 fImageRefs[i]->unref(); | 129 fImageRefs[i]->unref(); |
| 131 } | 130 } |
| 132 delete[] fImageRefs; | 131 delete[] fImageRefs; |
| 133 | 132 |
| 134 delete fFactoryPlayback; | 133 delete fFactoryPlayback; |
| 135 } | 134 } |
| 136 | 135 |
| 137 bool SkPictureData::containsBitmaps() const { | 136 bool SkPictureData::containsBitmaps() const { |
| 138 if (fBitmaps.count() > 0 || fImageCount > 0) { | 137 if (fBitmapImageCount > 0 || fImageCount > 0) { |
| 139 return true; | 138 return true; |
| 140 } | 139 } |
| 141 for (int i = 0; i < fPictureCount; ++i) { | 140 for (int i = 0; i < fPictureCount; ++i) { |
| 142 if (fPictureRefs[i]->willPlayBackBitmaps()) { | 141 if (fPictureRefs[i]->willPlayBackBitmaps()) { |
| 143 return true; | 142 return true; |
| 144 } | 143 } |
| 145 } | 144 } |
| 146 return false; | 145 return false; |
| 147 } | 146 } |
| 148 | 147 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 rec.copyToArray((SkRefCnt**)array); | 215 rec.copyToArray((SkRefCnt**)array); |
| 217 | 216 |
| 218 for (int i = 0; i < count; i++) { | 217 for (int i = 0; i < count; i++) { |
| 219 array[i]->serialize(stream); | 218 array[i]->serialize(stream); |
| 220 } | 219 } |
| 221 } | 220 } |
| 222 | 221 |
| 223 void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const { | 222 void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const { |
| 224 int i, n; | 223 int i, n; |
| 225 | 224 |
| 226 // we never record bitmaps anymore, only images | |
| 227 SkASSERT(fBitmaps.count() == 0); | |
| 228 | |
| 229 if ((n = fPaints.count()) > 0) { | 225 if ((n = fPaints.count()) > 0) { |
| 230 write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); | 226 write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); |
| 231 for (i = 0; i < n; i++) { | 227 for (i = 0; i < n; i++) { |
| 232 buffer.writePaint(fPaints[i]); | 228 buffer.writePaint(fPaints[i]); |
| 233 } | 229 } |
| 234 } | 230 } |
| 235 | 231 |
| 236 if ((n = fPaths.count()) > 0) { | 232 if ((n = fPaths.count()) > 0) { |
| 237 write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); | 233 write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); |
| 238 buffer.writeInt(n); | 234 buffer.writeInt(n); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 if (pictInfoFlags & gSD[i].fSrc) { | 350 if (pictInfoFlags & gSD[i].fSrc) { |
| 355 rbMask |= gSD[i].fDst; | 351 rbMask |= gSD[i].fDst; |
| 356 } | 352 } |
| 357 } | 353 } |
| 358 return rbMask; | 354 return rbMask; |
| 359 } | 355 } |
| 360 | 356 |
| 361 bool SkPictureData::parseStreamTag(SkStream* stream, | 357 bool SkPictureData::parseStreamTag(SkStream* stream, |
| 362 uint32_t tag, | 358 uint32_t tag, |
| 363 uint32_t size, | 359 uint32_t size, |
| 364 SkPicture::InstallPixelRefProc proc, | 360 SkImageDeserializer* factory, |
| 365 SkTypefacePlayback* topLevelTFPlayback) { | 361 SkTypefacePlayback* topLevelTFPlayback) { |
| 366 /* | 362 /* |
| 367 * By the time we encounter BUFFER_SIZE_TAG, we need to have already seen | 363 * By the time we encounter BUFFER_SIZE_TAG, we need to have already seen |
| 368 * its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required | 364 * its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required |
| 369 * but if they are present, they need to have been seen before the buffer. | 365 * but if they are present, they need to have been seen before the buffer. |
| 370 * | 366 * |
| 371 * We assert that if/when we see either of these, that we have not yet seen | 367 * We assert that if/when we see either of these, that we have not yet seen |
| 372 * the buffer tag, because if we have, then its too-late to deal with the | 368 * the buffer tag, because if we have, then its too-late to deal with the |
| 373 * factories or typefaces. | 369 * factories or typefaces. |
| 374 */ | 370 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 // the default here. | 403 // the default here. |
| 408 tf = SkTypeface::MakeDefault(); | 404 tf = SkTypeface::MakeDefault(); |
| 409 } | 405 } |
| 410 fTFPlayback.set(i, tf.get()); | 406 fTFPlayback.set(i, tf.get()); |
| 411 } | 407 } |
| 412 } break; | 408 } break; |
| 413 case SK_PICT_PICTURE_TAG: { | 409 case SK_PICT_PICTURE_TAG: { |
| 414 fPictureCount = 0; | 410 fPictureCount = 0; |
| 415 fPictureRefs = new const SkPicture* [size]; | 411 fPictureRefs = new const SkPicture* [size]; |
| 416 for (uint32_t i = 0; i < size; i++) { | 412 for (uint32_t i = 0; i < size; i++) { |
| 417 fPictureRefs[i] = SkPicture::MakeFromStream(stream, proc, topLev
elTFPlayback).release(); | 413 fPictureRefs[i] = SkPicture::MakeFromStream(stream, factory, top
LevelTFPlayback).release(); |
| 418 if (!fPictureRefs[i]) { | 414 if (!fPictureRefs[i]) { |
| 419 return false; | 415 return false; |
| 420 } | 416 } |
| 421 fPictureCount++; | 417 fPictureCount++; |
| 422 } | 418 } |
| 423 } break; | 419 } break; |
| 424 case SK_PICT_BUFFER_SIZE_TAG: { | 420 case SK_PICT_BUFFER_SIZE_TAG: { |
| 425 SkAutoMalloc storage(size); | 421 SkAutoMalloc storage(size); |
| 426 if (stream->read(storage.get(), size) != size) { | 422 if (stream->read(storage.get(), size) != size) { |
| 427 return false; | 423 return false; |
| 428 } | 424 } |
| 429 | 425 |
| 430 /* Should we use SkValidatingReadBuffer instead? */ | 426 /* Should we use SkValidatingReadBuffer instead? */ |
| 431 SkReadBuffer buffer(storage.get(), size); | 427 SkReadBuffer buffer(storage.get(), size); |
| 432 buffer.setFlags(pictInfoFlagsToReadBufferFlags(fInfo.fFlags)); | 428 buffer.setFlags(pictInfoFlagsToReadBufferFlags(fInfo.fFlags)); |
| 433 buffer.setVersion(fInfo.fVersion); | 429 buffer.setVersion(fInfo.fVersion); |
| 434 | 430 |
| 435 if (!fFactoryPlayback) { | 431 if (!fFactoryPlayback) { |
| 436 return false; | 432 return false; |
| 437 } | 433 } |
| 438 fFactoryPlayback->setupBuffer(buffer); | 434 fFactoryPlayback->setupBuffer(buffer); |
| 439 buffer.setBitmapDecoder(proc); | 435 buffer.setImageDeserializer(factory); |
| 440 | 436 |
| 441 if (fTFPlayback.count() > 0) { | 437 if (fTFPlayback.count() > 0) { |
| 442 // .skp files <= v43 have typefaces serialized with each sub pic
ture. | 438 // .skp files <= v43 have typefaces serialized with each sub pic
ture. |
| 443 fTFPlayback.setupBuffer(buffer); | 439 fTFPlayback.setupBuffer(buffer); |
| 444 } else { | 440 } else { |
| 445 // Newer .skp files serialize all typefaces with the top picture
. | 441 // Newer .skp files serialize all typefaces with the top picture
. |
| 446 topLevelTFPlayback->setupBuffer(buffer); | 442 topLevelTFPlayback->setupBuffer(buffer); |
| 447 } | 443 } |
| 448 | 444 |
| 449 while (!buffer.eof() && buffer.isValid()) { | 445 while (!buffer.eof() && buffer.isValid()) { |
| 450 tag = buffer.readUInt(); | 446 tag = buffer.readUInt(); |
| 451 size = buffer.readUInt(); | 447 size = buffer.readUInt(); |
| 452 if (!this->parseBufferTag(buffer, tag, size)) { | 448 if (!this->parseBufferTag(buffer, tag, size)) { |
| 453 return false; | 449 return false; |
| 454 } | 450 } |
| 455 } | 451 } |
| 456 if (!buffer.isValid()) { | 452 if (!buffer.isValid()) { |
| 457 return false; | 453 return false; |
| 458 } | 454 } |
| 459 SkDEBUGCODE(haveBuffer = true;) | 455 SkDEBUGCODE(haveBuffer = true;) |
| 460 } break; | 456 } break; |
| 461 } | 457 } |
| 462 return true; // success | 458 return true; // success |
| 463 } | 459 } |
| 464 | 460 |
| 465 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { | 461 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { |
| 466 return buffer.readImage(); | 462 return buffer.readImage().release(); |
| 463 } |
| 464 |
| 465 static const SkImage* create_bitmap_image_from_buffer(SkReadBuffer& buffer) { |
| 466 return buffer.readBitmapAsImage().release(); |
| 467 } | 467 } |
| 468 | 468 |
| 469 // Need a shallow wrapper to return const SkPicture* to match the other factorie
s, | 469 // Need a shallow wrapper to return const SkPicture* to match the other factorie
s, |
| 470 // as SkPicture::CreateFromBuffer() returns SkPicture* | 470 // as SkPicture::CreateFromBuffer() returns SkPicture* |
| 471 static const SkPicture* create_picture_from_buffer(SkReadBuffer& buffer) { | 471 static const SkPicture* create_picture_from_buffer(SkReadBuffer& buffer) { |
| 472 return SkPicture::MakeFromBuffer(buffer).release(); | 472 return SkPicture::MakeFromBuffer(buffer).release(); |
| 473 } | 473 } |
| 474 | 474 |
| 475 static const SkDrawable* create_drawable_from_buffer(SkReadBuffer& buffer) { | 475 static const SkDrawable* create_drawable_from_buffer(SkReadBuffer& buffer) { |
| 476 return (SkDrawable*) buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)
; | 476 return (SkDrawable*) buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)
; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 505 delete[] * array; | 505 delete[] * array; |
| 506 *array = nullptr; | 506 *array = nullptr; |
| 507 *outCount = 0; | 507 *outCount = 0; |
| 508 return false; | 508 return false; |
| 509 } | 509 } |
| 510 return true; | 510 return true; |
| 511 } | 511 } |
| 512 | 512 |
| 513 bool SkPictureData::parseBufferTag(SkReadBuffer& buffer, uint32_t tag, uint32_t
size) { | 513 bool SkPictureData::parseBufferTag(SkReadBuffer& buffer, uint32_t tag, uint32_t
size) { |
| 514 switch (tag) { | 514 switch (tag) { |
| 515 case SK_PICT_BITMAP_BUFFER_TAG: { | 515 case SK_PICT_BITMAP_BUFFER_TAG: |
| 516 const int count = SkToInt(size); | 516 if (!new_array_from_buffer(buffer, size, &fBitmapImageRefs, &fBitmap
ImageCount, |
| 517 fBitmaps.reset(count); | 517 create_bitmap_image_from_buffer)) { |
| 518 for (int i = 0; i < count; ++i) { | 518 return false; |
| 519 SkBitmap* bm = &fBitmaps[i]; | |
| 520 if (buffer.readBitmap(bm)) { | |
| 521 bm->setImmutable(); | |
| 522 } else { | |
| 523 return false; | |
| 524 } | |
| 525 } | 519 } |
| 526 } break; | 520 break; |
| 527 case SK_PICT_PAINT_BUFFER_TAG: { | 521 case SK_PICT_PAINT_BUFFER_TAG: { |
| 528 const int count = SkToInt(size); | 522 const int count = SkToInt(size); |
| 529 fPaints.reset(count); | 523 fPaints.reset(count); |
| 530 for (int i = 0; i < count; ++i) { | 524 for (int i = 0; i < count; ++i) { |
| 531 buffer.readPaint(&fPaints[i]); | 525 buffer.readPaint(&fPaints[i]); |
| 532 } | 526 } |
| 533 } break; | 527 } break; |
| 534 case SK_PICT_PATH_BUFFER_TAG: | 528 case SK_PICT_PATH_BUFFER_TAG: |
| 535 if (size > 0) { | 529 if (size > 0) { |
| 536 const int count = buffer.readInt(); | 530 const int count = buffer.readInt(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 break; | 568 break; |
| 575 default: | 569 default: |
| 576 // The tag was invalid. | 570 // The tag was invalid. |
| 577 return false; | 571 return false; |
| 578 } | 572 } |
| 579 return true; // success | 573 return true; // success |
| 580 } | 574 } |
| 581 | 575 |
| 582 SkPictureData* SkPictureData::CreateFromStream(SkStream* stream, | 576 SkPictureData* SkPictureData::CreateFromStream(SkStream* stream, |
| 583 const SkPictInfo& info, | 577 const SkPictInfo& info, |
| 584 SkPicture::InstallPixelRefProc pr
oc, | 578 SkImageDeserializer* factory, |
| 585 SkTypefacePlayback* topLevelTFPla
yback) { | 579 SkTypefacePlayback* topLevelTFPla
yback) { |
| 586 SkAutoTDelete<SkPictureData> data(new SkPictureData(info)); | 580 SkAutoTDelete<SkPictureData> data(new SkPictureData(info)); |
| 587 if (!topLevelTFPlayback) { | 581 if (!topLevelTFPlayback) { |
| 588 topLevelTFPlayback = &data->fTFPlayback; | 582 topLevelTFPlayback = &data->fTFPlayback; |
| 589 } | 583 } |
| 590 | 584 |
| 591 if (!data->parseStream(stream, proc, topLevelTFPlayback)) { | 585 if (!data->parseStream(stream, factory, topLevelTFPlayback)) { |
| 592 return nullptr; | 586 return nullptr; |
| 593 } | 587 } |
| 594 return data.release(); | 588 return data.release(); |
| 595 } | 589 } |
| 596 | 590 |
| 597 SkPictureData* SkPictureData::CreateFromBuffer(SkReadBuffer& buffer, | 591 SkPictureData* SkPictureData::CreateFromBuffer(SkReadBuffer& buffer, |
| 598 const SkPictInfo& info) { | 592 const SkPictInfo& info) { |
| 599 SkAutoTDelete<SkPictureData> data(new SkPictureData(info)); | 593 SkAutoTDelete<SkPictureData> data(new SkPictureData(info)); |
| 600 buffer.setVersion(info.fVersion); | 594 buffer.setVersion(info.fVersion); |
| 601 | 595 |
| 602 if (!data->parseBuffer(buffer)) { | 596 if (!data->parseBuffer(buffer)) { |
| 603 return nullptr; | 597 return nullptr; |
| 604 } | 598 } |
| 605 return data.release(); | 599 return data.release(); |
| 606 } | 600 } |
| 607 | 601 |
| 608 bool SkPictureData::parseStream(SkStream* stream, | 602 bool SkPictureData::parseStream(SkStream* stream, |
| 609 SkPicture::InstallPixelRefProc proc, | 603 SkImageDeserializer* factory, |
| 610 SkTypefacePlayback* topLevelTFPlayback) { | 604 SkTypefacePlayback* topLevelTFPlayback) { |
| 611 for (;;) { | 605 for (;;) { |
| 612 uint32_t tag = stream->readU32(); | 606 uint32_t tag = stream->readU32(); |
| 613 if (SK_PICT_EOF_TAG == tag) { | 607 if (SK_PICT_EOF_TAG == tag) { |
| 614 break; | 608 break; |
| 615 } | 609 } |
| 616 | 610 |
| 617 uint32_t size = stream->readU32(); | 611 uint32_t size = stream->readU32(); |
| 618 if (!this->parseStreamTag(stream, tag, size, proc, topLevelTFPlayback))
{ | 612 if (!this->parseStreamTag(stream, tag, size, factory, topLevelTFPlayback
)) { |
| 619 return false; // we're invalid | 613 return false; // we're invalid |
| 620 } | 614 } |
| 621 } | 615 } |
| 622 return true; | 616 return true; |
| 623 } | 617 } |
| 624 | 618 |
| 625 bool SkPictureData::parseBuffer(SkReadBuffer& buffer) { | 619 bool SkPictureData::parseBuffer(SkReadBuffer& buffer) { |
| 626 for (;;) { | 620 for (;;) { |
| 627 uint32_t tag = buffer.readUInt(); | 621 uint32_t tag = buffer.readUInt(); |
| 628 if (SK_PICT_EOF_TAG == tag) { | 622 if (SK_PICT_EOF_TAG == tag) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 655 } else { | 649 } else { |
| 656 return this->suitableForGpuRasterization(nullptr, reason); | 650 return this->suitableForGpuRasterization(nullptr, reason); |
| 657 } | 651 } |
| 658 } | 652 } |
| 659 | 653 |
| 660 bool SkPictureData::suitableForLayerOptimization() const { | 654 bool SkPictureData::suitableForLayerOptimization() const { |
| 661 return fContentInfo.numLayers() > 0; | 655 return fContentInfo.numLayers() > 0; |
| 662 } | 656 } |
| 663 #endif | 657 #endif |
| 664 /////////////////////////////////////////////////////////////////////////////// | 658 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |