Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: dm/DMSrcSink.cpp

Issue 2045293002: Add support for multiple frames in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Disable DNG if Raw is disabled Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 SkCodec::MultiFrameOptions multiOptions;
441 options.fFrameOptions = &multiOptions;
442 for (size_t i = 0; i < frameInfos.size(); i++) {
443 multiOptions.fIndex = i;
444 // Check for a prior frame
445 const size_t reqFrame = frameInfos[i].fRequiredFrame;
446 if (reqFrame != SkCodec::kNone && reqFrame == cachedFrame
447 && priorFramePixels.get()) {
448 // Copy into pixels
449 memcpy(pixels.get(), priorFramePixels.get(), safeSize);
450 multiOptions.fHasPriorFrame = true;
451 } else {
452 multiOptions.fHasPriorFrame = false;
453 }
454 const SkCodec::Result result = codec->getPixels(decodeInfo, pixe ls.get(),
455 rowBytes, &optio ns,
456 colorPtr, &color Count);
457 switch (result) {
458 case SkCodec::kSuccess:
459 case SkCodec::kIncompleteInput:
460 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowByte s,
461 colorPtr, colorCount, fDstColorType);
462 if (result == SkCodec::kIncompleteInput) {
463 return "";
464 }
465 break;
466 default:
467 return SkStringPrintf("Couldn't getPixels for frame %i i n %s.",
468 i, fPath.c_str());
469 }
470
471 // If a future frame depends on this one, store it in priorFrame .
472 // (Note that if i+1 does *not* depend on i, then no future fram e can.)
473 if (i+1 < frameInfos.size() && frameInfos[i+1].fRequiredFrame == i) {
474 memcpy(priorFramePixels.reset(safeSize), pixels.get(), safeS ize);
475 cachedFrame = i;
476 }
477
478 canvas->translate(SkIntToScalar(0), SkIntToScalar(decodeInfo.hei ght()));
479 }
480 break;
481 }
429 case kCodecZeroInit_Mode: 482 case kCodecZeroInit_Mode:
430 case kCodec_Mode: { 483 case kCodec_Mode: {
431 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &option s, 484 switch (codec->getPixels(decodeInfo, pixels.get(), rowBytes, &option s,
432 colorPtr, &colorCount)) { 485 colorPtr, &colorCount)) {
433 case SkCodec::kSuccess: 486 case SkCodec::kSuccess:
434 // We consider incomplete to be valid, since we should still decode what is 487 // We consider incomplete to be valid, since we should still decode what is
435 // available. 488 // available.
436 case SkCodec::kIncompleteInput: 489 case SkCodec::kIncompleteInput:
437 break; 490 break;
438 default: 491 default:
439 // Everything else is considered a failure. 492 // Everything else is considered a failure.
440 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); 493 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( ));
441 } 494 }
442 495
443 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount, 496 draw_to_canvas(canvas, bitmapInfo, pixels.get(), rowBytes, colorPtr, colorCount,
444 fDstColorType); 497 fDstColorType);
445 break; 498 break;
446 } 499 }
447 case kScanline_Mode: { 500 case kScanline_Mode: {
448 void* dst = pixels.get(); 501 void* dst = pixels.get();
449 uint32_t height = decodeInfo.height(); 502 uint32_t height = decodeInfo.height();
450 const bool png = fPath.endsWith("png"); 503 const bool useIncremental = [this]() {
504 auto exts = { "png", "PNG", "gif", "GIF" };
505 for (auto ext : exts) {
506 if (fPath.endsWith(ext)) {
507 return true;
508 }
509 }
510 return false;
511 }();
512 // ico may use the old scanline method or the new one, depending on whether it
513 // internally holds a bmp or a png.
451 const bool ico = fPath.endsWith("ico"); 514 const bool ico = fPath.endsWith("ico");
452 bool useOldScanlineMethod = !png && !ico; 515 bool useOldScanlineMethod = !useIncremental && !ico;
453 if (png || ico) { 516 if (useIncremental || ico) {
454 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInf o, dst, 517 if (SkCodec::kSuccess == codec->startIncrementalDecode(decodeInf o, dst,
455 rowBytes, nullptr, colorPtr, &colorCount)) { 518 rowBytes, nullptr, colorPtr, &colorCount)) {
456 int rowsDecoded; 519 int rowsDecoded;
457 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&r owsDecoded)) { 520 if (SkCodec::kIncompleteInput == codec->incrementalDecode(&r owsDecoded)) {
458 codec->fillIncompleteImage(decodeInfo, dst, rowBytes, 521 codec->fillIncompleteImage(decodeInfo, dst, rowBytes,
459 SkCodec::kNo_ZeroInitialized, height, 522 SkCodec::kNo_ZeroInitialized, height,
460 rowsDecoded); 523 rowsDecoded);
461 } 524 }
462 } else { 525 } else {
463 if (png) { 526 if (useIncremental) {
464 // Error: PNG should support incremental decode. 527 // Error: These should support incremental decode.
465 return "Could not start incremental decode"; 528 return "Could not start incremental decode";
466 } 529 }
467 // Otherwise, this is an ICO. Since incremental failed, it m ust contain a BMP, 530 // Otherwise, this is an ICO. Since incremental failed, it m ust contain a BMP,
468 // which should work via startScanlineDecode 531 // which should work via startScanlineDecode
469 useOldScanlineMethod = true; 532 useOldScanlineMethod = true;
470 } 533 }
471 } 534 }
472 535
473 if (useOldScanlineMethod) { 536 if (useOldScanlineMethod) {
474 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr, 537 if (SkCodec::kSuccess != codec->startScanlineDecode(decodeInfo, NULL, colorPtr,
475 &colorCount) ) { 538 &colorCount) ) {
476 return "Could not start scanline decoder"; 539 return "Could not start scanline decoder";
477 } 540 }
478 541
479 switch (codec->getScanlineOrder()) { 542 switch (codec->getScanlineOrder()) {
480 case SkCodec::kTopDown_SkScanlineOrder: 543 case SkCodec::kTopDown_SkScanlineOrder:
481 case SkCodec::kBottomUp_SkScanlineOrder: 544 case SkCodec::kBottomUp_SkScanlineOrder:
482 // We do not need to check the return value. On an inco mplete 545 // We do not need to check the return value. On an inco mplete
483 // image, memory will be filled with a default value. 546 // image, memory will be filled with a default value.
484 codec->getScanlines(dst, height, rowBytes); 547 codec->getScanlines(dst, height, rowBytes);
485 break; 548 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 } 549 }
498 } 550 }
499 551
500 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCou nt, fDstColorType); 552 draw_to_canvas(canvas, bitmapInfo, dst, rowBytes, colorPtr, colorCou nt, fDstColorType);
501 break; 553 break;
502 } 554 }
503 case kStripe_Mode: { 555 case kStripe_Mode: {
504 const int height = decodeInfo.height(); 556 const int height = decodeInfo.height();
505 // This value is chosen arbitrarily. We exercise more cases by choo sing a value that 557 // This value is chosen arbitrarily. We exercise more cases by choo sing a value that
506 // does not align with image blocks. 558 // does not align with image blocks.
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 } 704 }
653 return ""; 705 return "";
654 } 706 }
655 707
656 SkISize CodecSrc::size() const { 708 SkISize CodecSrc::size() const {
657 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str())); 709 sk_sp<SkData> encoded(SkData::MakeFromFileName(fPath.c_str()));
658 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); 710 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
659 if (nullptr == codec) { 711 if (nullptr == codec) {
660 return SkISize::Make(0, 0); 712 return SkISize::Make(0, 0);
661 } 713 }
662 return codec->getScaledDimensions(fScale); 714
715 auto imageSize = codec->getScaledDimensions(fScale);
716 if (fMode == kAnimated_Mode) {
717 // We'll draw one of each frame, so make it big enough to hold them all.
718 const size_t count = codec->getFrameInfo().size();
719 imageSize.fHeight = imageSize.fHeight * count;
720 }
721 return imageSize;
663 } 722 }
664 723
665 Name CodecSrc::name() const { 724 Name CodecSrc::name() const {
666 if (1.0f == fScale) { 725 if (1.0f == fScale) {
667 return SkOSPath::Basename(fPath.c_str()); 726 return SkOSPath::Basename(fPath.c_str());
668 } 727 }
669 return get_scaled_name(fPath, fScale); 728 return get_scaled_name(fPath, fScale);
670 } 729 }
671 730
672 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ 731 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
(...skipping 1041 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 Error err = src.draw(&rec); 1773 Error err = src.draw(&rec);
1715 if (!err.isEmpty()) { 1774 if (!err.isEmpty()) {
1716 return err; 1775 return err;
1717 } 1776 }
1718 dl->draw(canvas); 1777 dl->draw(canvas);
1719 return check_against_reference(bitmap, src, fSink); 1778 return check_against_reference(bitmap, src, fSink);
1720 }); 1779 });
1721 } 1780 }
1722 1781
1723 } // namespace DM 1782 } // namespace DM
OLDNEW
« no previous file with comments | « dm/DMSrcSink.h ('k') | fuzz/fuzz.cpp » ('j') | src/codec/SkGifCodec.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698