OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
9 #include "Resources.h" | 9 #include "Resources.h" |
10 #include "SkAndroidCodec.h" | 10 #include "SkAndroidCodec.h" |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 402 |
403 // Visually inspecting very small output images is not necessary. We will | 403 // Visually inspecting very small output images is not necessary. We will |
404 // cover these cases in unit testing. | 404 // cover these cases in unit testing. |
405 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { | 405 if ((size.width() <= 10 || size.height() <= 10) && 1.0f != fScale) { |
406 return Error::Nonfatal("Scaling very small images is uninteresting."); | 406 return Error::Nonfatal("Scaling very small images is uninteresting."); |
407 } | 407 } |
408 decodeInfo = decodeInfo.makeWH(size.width(), size.height()); | 408 decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
409 | 409 |
410 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType()); | 410 const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType()); |
411 const size_t rowBytes = size.width() * bpp; | 411 const size_t rowBytes = size.width() * bpp; |
412 SkAutoMalloc pixels(decodeInfo.getSafeSize(rowBytes)); | 412 const size_t safeSize = decodeInfo.getSafeSize(rowBytes); |
| 413 SkAutoMalloc pixels(safeSize); |
413 SkPMColor colorPtr[256]; | 414 SkPMColor colorPtr[256]; |
414 int colorCount = 256; | 415 int colorCount = 256; |
415 | 416 |
416 SkCodec::Options options; | 417 SkCodec::Options options; |
417 if (kCodecZeroInit_Mode == fMode) { | 418 if (kCodecZeroInit_Mode == fMode) { |
418 memset(pixels.get(), 0, size.height() * rowBytes); | 419 memset(pixels.get(), 0, size.height() * rowBytes); |
419 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | 420 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; |
420 } | 421 } |
421 | 422 |
422 SkImageInfo bitmapInfo = decodeInfo; | 423 SkImageInfo bitmapInfo = decodeInfo; |
423 if (kRGBA_8888_SkColorType == decodeInfo.colorType() || | 424 if (kRGBA_8888_SkColorType == decodeInfo.colorType() || |
424 kBGRA_8888_SkColorType == decodeInfo.colorType()) { | 425 kBGRA_8888_SkColorType == decodeInfo.colorType()) { |
425 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); | 426 bitmapInfo = bitmapInfo.makeColorType(kN32_SkColorType); |
426 } | 427 } |
427 | 428 |
428 switch (fMode) { | 429 switch (fMode) { |
| 430 case kAnimated_Mode: { |
| 431 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo(); |
| 432 if (frameInfos.size() <= 1) { |
| 433 return SkStringPrintf("%s is not an animated image.", fPath.c_st
r()); |
| 434 } |
| 435 |
| 436 SkAutoCanvasRestore acr(canvas, true); |
| 437 // Used to cache a frame that future frames will depend on. |
| 438 SkAutoMalloc priorFramePixels; |
| 439 size_t cachedFrame = SkCodec::kNone; |
| 440 for (size_t i = 0; i < frameInfos.size(); i++) { |
| 441 options.fFrameIndex = i; |
| 442 // Check for a prior frame |
| 443 const size_t reqFrame = frameInfos[i].fRequiredFrame; |
| 444 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame |
| 445 && priorFramePixels.get()) { |
| 446 // Copy into pixels |
| 447 memcpy(pixels.get(), priorFramePixels.get(), safeSize); |
| 448 options.fHasPriorFrame = true; |
| 449 } else { |
| 450 options.fHasPriorFrame = false; |
| 451 } |
| 452 const SkCodec::Result result = codec->getPixels(decodeInfo, pixe
ls.get(), |
| 453 rowBytes, &optio
ns, |
| 454 colorPtr, &color
Count); |
| 455 switch (result) { |
| 456 case SkCodec::kSuccess: |
| 457 case SkCodec::kIncompleteInput: |
| 458 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowByte
s, |
| 459 colorPtr, colorCount, fDstColorType); |
| 460 if (result == SkCodec::kIncompleteInput) { |
| 461 return ""; |
| 462 } |
| 463 break; |
| 464 default: |
| 465 return SkStringPrintf("Couldn't getPixels for frame %i i
n %s.", |
| 466 i, fPath.c_str()); |
| 467 } |
| 468 |
| 469 // If a future frame depends on this one, store it in priorFrame
. |
| 470 // (Note that if i+1 does *not* depend on i, then no future fram
e can.) |
| 471 if (i+1 < frameInfos.size() && frameInfos[i+1].fRequiredFrame ==
i) { |
| 472 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeS
ize); |
| 473 cachedFrame = i; |
| 474 } |
| 475 |
| 476 canvas->translate(SkIntToScalar(0), SkIntToScalar(decodeInfo.hei
ght())); |
| 477 } |
| 478 break; |
| 479 } |
429 case kCodecZeroInit_Mode: | 480 case kCodecZeroInit_Mode: |
430 case kCodec_Mode: { | 481 case kCodec_Mode: { |
431 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &option
s, | 482 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &option
s, |
432 colorPtr, &colorCount)) { | 483 colorPtr, &colorCount)) { |
433 case SkCodec::kSuccess: | 484 case SkCodec::kSuccess: |
434 // We consider incomplete to be valid, since we should still
decode what is | 485 // We consider incomplete to be valid, since we should still
decode what is |
435 // available. | 486 // available. |
436 case SkCodec::kIncompleteInput: | 487 case SkCodec::kIncompleteInput: |
437 break; | 488 break; |
438 default: | 489 default: |
439 // Everything else is considered a failure. | 490 // Everything else is considered a failure. |
440 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str(
)); | 491 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str(
)); |
441 } | 492 } |
442 | 493 |
443 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr,
colorCount, | 494 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr,
colorCount, |
444 fDstColorType); | 495 fDstColorType); |
445 break; | 496 break; |
446 } | 497 } |
447 case kScanline_Mode: { | 498 case kScanline_Mode: { |
448 void* dst = pixels.get(); | 499 void* dst = pixels.get(); |
449 uint32_t height = decodeInfo.height(); | 500 uint32_t height = decodeInfo.height(); |
450 const bool png = fPath.endsWith("png"); | 501 const bool useIncremental = [this]() { |
| 502 auto exts = { "png", "PNG", "gif", "GIF" }; |
| 503 for (auto ext : exts) { |
| 504 if (fPath.endsWith(ext)) { |
| 505 return true; |
| 506 } |
| 507 } |
| 508 return false; |
| 509 }(); |
| 510 // ico may use the old scanline method or the new one, depending on
whether it |
| 511 // internally holds a bmp or a png. |
451 const bool ico = fPath.endsWith("ico"); | 512 const bool ico = fPath.endsWith("ico"); |
452 bool useOldScanlineMethod = !png && !ico; | 513 bool useOldScanlineMethod = !useIncremental && !ico; |
453 if (png || ico) { | 514 if (useIncremental || ico) { |
454 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInf
o, dst, | 515 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInf
o, dst, |
455 rowBytes, nullptr, colorPtr, &colorCount)) { | 516 rowBytes, nullptr, colorPtr, &colorCount)) { |
456 int rowsDecoded; | 517 int rowsDecoded; |
457 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&r
owsDecoded)) { | 518 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&r
owsDecoded)) { |
458 codec->fillIncompleteImage(decodeInfo, dst, rowBytes, | 519 codec->fillIncompleteImage(decodeInfo, dst, rowBytes, |
459 SkCodec::kNo_ZeroInitialized,
height, | 520 SkCodec::kNo_ZeroInitialized,
height, |
460 rowsDecoded); | 521 rowsDecoded); |
461 } | 522 } |
462 } else { | 523 } else { |
463 if (png) { | 524 if (useIncremental) { |
464 // Error: PNG should support incremental decode. | 525 // Error: These should support incremental decode. |
465 return "Could not start incremental decode"; | 526 return "Could not start incremental decode"; |
466 } | 527 } |
467 // Otherwise, this is an ICO. Since incremental failed, it m
ust contain a BMP, | 528 // Otherwise, this is an ICO. Since incremental failed, it m
ust contain a BMP, |
468 // which should work via startScanlineDecode | 529 // which should work via startScanlineDecode |
469 useOldScanlineMethod = true; | 530 useOldScanlineMethod = true; |
470 } | 531 } |
471 } | 532 } |
472 | 533 |
473 if (useOldScanlineMethod) { | 534 if (useOldScanlineMethod) { |
474 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo,
NULL, colorPtr, | 535 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo,
NULL, colorPtr, |
475 &colorCount)
) { | 536 &colorCount)
) { |
476 return "Could not start scanline decoder"; | 537 return "Could not start scanline decoder"; |
477 } | 538 } |
478 | 539 |
479 switch (codec->getScanlineOrder()) { | 540 switch (codec->getScanlineOrder()) { |
480 case SkCodec::kTopDown_SkScanlineOrder: | 541 case SkCodec::kTopDown_SkScanlineOrder: |
481 case SkCodec::kBottomUp_SkScanlineOrder: | 542 case SkCodec::kBottomUp_SkScanlineOrder: |
482 // We do not need to check the return value. On an inco
mplete | 543 // We do not need to check the return value. On an inco
mplete |
483 // image, memory will be filled with a default value. | 544 // image, memory will be filled with a default value. |
484 codec->getScanlines(dst, height, rowBytes); | 545 codec->getScanlines(dst, height, rowBytes); |
485 break; | 546 break; |
486 case SkCodec::kOutOfOrder_SkScanlineOrder: { | |
487 for (int y = 0; y < decodeInfo.height(); y++) { | |
488 int dstY = codec->outputScanline(y); | |
489 void* dstPtr = SkTAddOffset<void>(dst, rowBytes * ds
tY); | |
490 // We complete the loop, even if this call begins to
fail | |
491 // due to an incomplete image. This ensures any uni
nitialized | |
492 // memory will be filled with the proper value. | |
493 codec->getScanlines(dstPtr, 1, rowBytes); | |
494 } | |
495 break; | |
496 } | |
497 } | 547 } |
498 } | 548 } |
499 | 549 |
500 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCou
nt, fDstColorType); | 550 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCou
nt, fDstColorType); |
501 break; | 551 break; |
502 } | 552 } |
503 case kStripe_Mode: { | 553 case kStripe_Mode: { |
504 const int height = decodeInfo.height(); | 554 const int height = decodeInfo.height(); |
505 // This value is chosen arbitrarily. We exercise more cases by choo
sing a value that | 555 // This value is chosen arbitrarily. We exercise more cases by choo
sing a value that |
506 // does not align with image blocks. | 556 // does not align with image blocks. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 } | 702 } |
653 return ""; | 703 return ""; |
654 } | 704 } |
655 | 705 |
656 SkISize CodecSrc::size() const { | 706 SkISize CodecSrc::size() const { |
657 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); | 707 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); |
658 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 708 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
659 if (nullptr == codec) { | 709 if (nullptr == codec) { |
660 return SkISize::Make(0, 0); | 710 return SkISize::Make(0, 0); |
661 } | 711 } |
662 return codec->getScaledDimensions(fScale); | 712 |
| 713 auto imageSize = codec->getScaledDimensions(fScale); |
| 714 if (fMode == kAnimated_Mode) { |
| 715 // We'll draw one of each frame, so make it big enough to hold them all. |
| 716 const size_t count = codec->getFrameInfo().size(); |
| 717 imageSize.fHeight = imageSize.fHeight * count; |
| 718 } |
| 719 return imageSize; |
663 } | 720 } |
664 | 721 |
665 Name CodecSrc::name() const { | 722 Name CodecSrc::name() const { |
666 if (1.0f == fScale) { | 723 if (1.0f == fScale) { |
667 return SkOSPath::Basename(fPath.c_str()); | 724 return SkOSPath::Basename(fPath.c_str()); |
668 } | 725 } |
669 return get_scaled_name(fPath, fScale); | 726 return get_scaled_name(fPath, fScale); |
670 } | 727 } |
671 | 728 |
672 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 729 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1710 Error err = src.draw(&rec); | 1767 Error err = src.draw(&rec); |
1711 if (!err.isEmpty()) { | 1768 if (!err.isEmpty()) { |
1712 return err; | 1769 return err; |
1713 } | 1770 } |
1714 dl->draw(canvas); | 1771 dl->draw(canvas); |
1715 return check_against_reference(bitmap, src, fSink); | 1772 return check_against_reference(bitmap, src, fSink); |
1716 }); | 1773 }); |
1717 } | 1774 } |
1718 | 1775 |
1719 } // namespace DM | 1776 } // namespace DM |
OLD | NEW |