| 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 "SamplePipeControllers.h" | 9 #include "SamplePipeControllers.h" |
| 10 #include "SkAndroidCodec.h" |
| 10 #include "SkCodec.h" | 11 #include "SkCodec.h" |
| 11 #include "SkCodecTools.h" | 12 #include "SkCodecTools.h" |
| 12 #include "SkCommonFlags.h" | 13 #include "SkCommonFlags.h" |
| 13 #include "SkData.h" | 14 #include "SkData.h" |
| 14 #include "SkDocument.h" | 15 #include "SkDocument.h" |
| 15 #include "SkError.h" | 16 #include "SkError.h" |
| 16 #include "SkFunction.h" | 17 #include "SkFunction.h" |
| 17 #include "SkImageGenerator.h" | 18 #include "SkImageGenerator.h" |
| 18 #include "SkMultiPictureDraw.h" | 19 #include "SkMultiPictureDraw.h" |
| 19 #include "SkNullCanvas.h" | 20 #include "SkNullCanvas.h" |
| 20 #include "SkOSFile.h" | 21 #include "SkOSFile.h" |
| 21 #include "SkPictureData.h" | 22 #include "SkPictureData.h" |
| 22 #include "SkPictureRecorder.h" | 23 #include "SkPictureRecorder.h" |
| 23 #include "SkRandom.h" | 24 #include "SkRandom.h" |
| 24 #include "SkRecordDraw.h" | 25 #include "SkRecordDraw.h" |
| 25 #include "SkRecorder.h" | 26 #include "SkRecorder.h" |
| 26 #include "SkRemote.h" | 27 #include "SkRemote.h" |
| 27 #include "SkSVGCanvas.h" | 28 #include "SkSVGCanvas.h" |
| 28 #include "SkScaledCodec.h" | |
| 29 #include "SkStream.h" | 29 #include "SkStream.h" |
| 30 #include "SkTLogic.h" | 30 #include "SkTLogic.h" |
| 31 #include "SkXMLWriter.h" | 31 #include "SkXMLWriter.h" |
| 32 #include "SkScaledCodec.h" | |
| 33 #include "SkSwizzler.h" | 32 #include "SkSwizzler.h" |
| 34 | 33 |
| 35 DEFINE_bool(multiPage, false, "For document-type backends, render the source" | 34 DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
| 36 " into multiple pages"); | 35 " into multiple pages"); |
| 37 | 36 |
| 38 static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { | 37 static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
| 39 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); | 38 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
| 40 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst); | 39 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst); |
| 41 } | 40 } |
| 42 | 41 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // let the GPU handle it. | 241 // let the GPU handle it. |
| 243 return flags.type != SinkFlags::kRaster | 242 return flags.type != SinkFlags::kRaster |
| 244 || flags.approach != SinkFlags::kDirect; | 243 || flags.approach != SinkFlags::kDirect; |
| 245 } | 244 } |
| 246 | 245 |
| 247 Error CodecSrc::draw(SkCanvas* canvas) const { | 246 Error CodecSrc::draw(SkCanvas* canvas) const { |
| 248 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 247 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 249 if (!encoded) { | 248 if (!encoded) { |
| 250 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 249 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 251 } | 250 } |
| 252 SkAutoTDelete<SkCodec> codec(NULL); | 251 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 253 if (kScaledCodec_Mode == fMode) { | |
| 254 codec.reset(SkScaledCodec::NewFromData(encoded)); | |
| 255 // TODO (msarett): This should fall through to a fatal error once we sup
port scaled | |
| 256 // codecs for all image types. | |
| 257 if (nullptr == codec.get()) { | |
| 258 return Error::Nonfatal(SkStringPrintf("Couldn't create scaled codec
for %s.", | |
| 259 fPath.c_str())); | |
| 260 } | |
| 261 } else { | |
| 262 codec.reset(SkCodec::NewFromData(encoded)); | |
| 263 } | |
| 264 if (nullptr == codec.get()) { | 252 if (nullptr == codec.get()) { |
| 265 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); | 253 return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| 266 } | 254 } |
| 267 | 255 |
| 268 // Choose the color type to decode to | 256 // Choose the color type to decode to |
| 269 SkImageInfo decodeInfo = codec->getInfo(); | 257 SkImageInfo decodeInfo = codec->getInfo(); |
| 270 SkColorType canvasColorType = canvas->imageInfo().colorType(); | 258 SkColorType canvasColorType = canvas->imageInfo().colorType(); |
| 271 switch (fDstColorType) { | 259 switch (fDstColorType) { |
| 272 case kIndex8_Always_DstColorType: | 260 case kIndex8_Always_DstColorType: |
| 273 decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType); | 261 decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); | 304 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 317 } | 305 } |
| 318 | 306 |
| 319 SkBitmap bitmap; | 307 SkBitmap bitmap; |
| 320 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) { | 308 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) { |
| 321 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(
), | 309 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(
), |
| 322 decodeInfo.width(), decodeInfo.height()); | 310 decodeInfo.width(), decodeInfo.height()); |
| 323 } | 311 } |
| 324 | 312 |
| 325 switch (fMode) { | 313 switch (fMode) { |
| 326 case kScaledCodec_Mode: | |
| 327 case kCodec_Mode: { | 314 case kCodec_Mode: { |
| 328 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowB
ytes(), nullptr, | 315 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowB
ytes(), nullptr, |
| 329 colorPtr, colorCountPtr)) { | 316 colorPtr, colorCountPtr)) { |
| 330 case SkCodec::kSuccess: | 317 case SkCodec::kSuccess: |
| 331 // We consider incomplete to be valid, since we should still
decode what is | 318 // We consider incomplete to be valid, since we should still
decode what is |
| 332 // available. | 319 // available. |
| 333 case SkCodec::kIncompleteInput: | 320 case SkCodec::kIncompleteInput: |
| 334 break; | 321 break; |
| 335 case SkCodec::kInvalidConversion: | 322 case SkCodec::kInvalidConversion: |
| 336 return Error::Nonfatal("Incompatible colortype conversion"); | 323 return Error::Nonfatal("Incompatible colortype conversion"); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 left += decodeInfo.width(); | 565 left += decodeInfo.width(); |
| 579 } | 566 } |
| 580 return ""; | 567 return ""; |
| 581 } | 568 } |
| 582 } | 569 } |
| 583 return ""; | 570 return ""; |
| 584 } | 571 } |
| 585 | 572 |
| 586 SkISize CodecSrc::size() const { | 573 SkISize CodecSrc::size() const { |
| 587 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 574 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 588 SkAutoTDelete<SkCodec> codec(nullptr); | 575 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
| 589 | |
| 590 if (kScaledCodec_Mode == fMode) { | |
| 591 codec.reset(SkScaledCodec::NewFromData(encoded)); | |
| 592 } else { | |
| 593 codec.reset(SkCodec::NewFromData(encoded)); | |
| 594 } | |
| 595 | |
| 596 if (nullptr == codec) { | 576 if (nullptr == codec) { |
| 597 return SkISize::Make(0, 0); | 577 return SkISize::Make(0, 0); |
| 598 } | 578 } |
| 599 return codec->getScaledDimensions(fScale); | 579 return codec->getScaledDimensions(fScale); |
| 600 } | 580 } |
| 601 | 581 |
| 602 Name CodecSrc::name() const { | 582 Name CodecSrc::name() const { |
| 603 if (1.0f == fScale) { | 583 if (1.0f == fScale) { |
| 604 return SkOSPath::Basename(fPath.c_str()); | 584 return SkOSPath::Basename(fPath.c_str()); |
| 605 } | 585 } |
| 606 return get_scaled_name(fPath, fScale); | 586 return get_scaled_name(fPath, fScale); |
| 607 } | 587 } |
| 608 | 588 |
| 609 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 589 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 610 | 590 |
| 591 AndroidCodecSrc::AndroidCodecSrc(Path path, Mode mode, CodecSrc::DstColorType ds
tColorType, |
| 592 int sampleSize) |
| 593 : fPath(path) |
| 594 , fMode(mode) |
| 595 , fDstColorType(dstColorType) |
| 596 , fSampleSize(sampleSize) |
| 597 {} |
| 598 |
| 599 bool AndroidCodecSrc::veto(SinkFlags flags) const { |
| 600 // No need to test decoding to non-raster or indirect backend. |
| 601 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferr
ed decode to |
| 602 // let the GPU handle it. |
| 603 return flags.type != SinkFlags::kRaster |
| 604 || flags.approach != SinkFlags::kDirect; |
| 605 } |
| 606 |
| 607 Error AndroidCodecSrc::draw(SkCanvas* canvas) const { |
| 608 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 609 if (!encoded) { |
| 610 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
| 611 } |
| 612 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| 613 if (nullptr == codec.get()) { |
| 614 return SkStringPrintf("Couldn't create android codec for %s.", fPath.c_s
tr()); |
| 615 } |
| 616 |
| 617 // Choose the color type to decode to |
| 618 SkImageInfo decodeInfo = codec->getInfo(); |
| 619 SkColorType canvasColorType = canvas->imageInfo().colorType(); |
| 620 switch (fDstColorType) { |
| 621 case CodecSrc::kIndex8_Always_DstColorType: |
| 622 decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType); |
| 623 if (kRGB_565_SkColorType == canvasColorType) { |
| 624 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.
"); |
| 625 } |
| 626 break; |
| 627 case CodecSrc::kGrayscale_Always_DstColorType: |
| 628 decodeInfo = codec->getInfo().makeColorType(kGray_8_SkColorType); |
| 629 if (kRGB_565_SkColorType == canvasColorType) { |
| 630 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.
"); |
| 631 } |
| 632 break; |
| 633 default: |
| 634 decodeInfo = decodeInfo.makeColorType(canvasColorType); |
| 635 break; |
| 636 } |
| 637 |
| 638 // Scale the image if it is desired. |
| 639 SkISize size = codec->getSampledDimensions(fSampleSize); |
| 640 |
| 641 // Visually inspecting very small output images is not necessary. We will |
| 642 // cover these cases in unit testing. |
| 643 if ((size.width() <= 10 || size.height() <= 10) && 1 != fSampleSize) { |
| 644 return Error::Nonfatal("Scaling very small images is uninteresting."); |
| 645 } |
| 646 decodeInfo = decodeInfo.makeWH(size.width(), size.height()); |
| 647 |
| 648 // Construct a color table for the decode if necessary |
| 649 SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 650 SkPMColor* colorPtr = nullptr; |
| 651 int* colorCountPtr = nullptr; |
| 652 int maxColors = 256; |
| 653 if (kIndex_8_SkColorType == decodeInfo.colorType()) { |
| 654 SkPMColor colors[256]; |
| 655 colorTable.reset(new SkColorTable(colors, maxColors)); |
| 656 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 657 colorCountPtr = &maxColors; |
| 658 } |
| 659 |
| 660 // FIXME: Currently we cannot draw unpremultiplied sources. |
| 661 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) { |
| 662 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); |
| 663 } |
| 664 |
| 665 SkBitmap bitmap; |
| 666 if (!bitmap.tryAllocPixels(decodeInfo, nullptr, colorTable.get())) { |
| 667 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(
), |
| 668 decodeInfo.width(), decodeInfo.height()); |
| 669 } |
| 670 |
| 671 // Create options for the codec. |
| 672 SkAndroidCodec::AndroidOptions options; |
| 673 options.fColorPtr = colorPtr; |
| 674 options.fColorCount = colorCountPtr; |
| 675 options.fSampleSize = fSampleSize; |
| 676 |
| 677 switch (fMode) { |
| 678 case kFullImage_Mode: { |
| 679 switch (codec->getAndroidPixels(decodeInfo, bitmap.getPixels(), bitm
ap.rowBytes(), |
| 680 &options)) { |
| 681 case SkCodec::kSuccess: |
| 682 case SkCodec::kIncompleteInput: |
| 683 break; |
| 684 case SkCodec::kInvalidConversion: |
| 685 return Error::Nonfatal("Cannot convert to requested color ty
pe.\n"); |
| 686 default: |
| 687 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str(
)); |
| 688 } |
| 689 canvas->drawBitmap(bitmap, 0, 0); |
| 690 return ""; |
| 691 } |
| 692 case kDivisor_Mode: { |
| 693 const int width = codec->getInfo().width(); |
| 694 const int height = codec->getInfo().height(); |
| 695 const int divisor = 2; |
| 696 if (width < divisor || height < divisor) { |
| 697 return Error::Nonfatal("Divisor is larger than image dimension.\
n"); |
| 698 } |
| 699 |
| 700 // Rounding the size of the subsets may leave some pixels uninitiali
zed on the bottom |
| 701 // and right edges of the bitmap. |
| 702 bitmap.eraseColor(0); |
| 703 for (uint32_t x = 0; x < divisor; x++) { |
| 704 for (uint32_t y = 0; y < divisor; y++) { |
| 705 // Calculate the subset dimensions |
| 706 int subsetWidth = width / divisor; |
| 707 int subsetHeight = height / divisor; |
| 708 const int left = x * subsetWidth; |
| 709 const int top = y * subsetHeight; |
| 710 |
| 711 // Increase the size of the last subset in each row or colum
n, when the |
| 712 // divisor does not divide evenly into the image dimensions |
| 713 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; |
| 714 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; |
| 715 SkIRect subset = SkIRect::MakeXYWH(left, top, subsetWidth, s
ubsetHeight); |
| 716 if (!codec->getSupportedSubset(&subset)) { |
| 717 return "Could not get supported subset to decode.\n"; |
| 718 } |
| 719 options.fSubset = ⊂ |
| 720 void* pixels = bitmap.getAddr(subset.left() / fSampleSize, |
| 721 subset.top() / fSampleSize); |
| 722 SkISize scaledSubsetSize = codec->getSampledSubsetDimensions
(fSampleSize, |
| 723 subset); |
| 724 SkImageInfo subsetDecodeInfo = decodeInfo.makeWH(scaledSubse
tSize.width(), |
| 725 scaledSubsetSize.height()); |
| 726 |
| 727 switch (codec->getAndroidPixels(subsetDecodeInfo, pixels, bi
tmap.rowBytes(), |
| 728 &options)) { |
| 729 case SkCodec::kSuccess: |
| 730 case SkCodec::kIncompleteInput: |
| 731 break; |
| 732 case SkCodec::kInvalidConversion: |
| 733 return Error::Nonfatal("Cannot convert to requested
color type.\n"); |
| 734 default: |
| 735 return SkStringPrintf("Couldn't getPixels %s.", fPat
h.c_str()); |
| 736 } |
| 737 } |
| 738 } |
| 739 canvas->drawBitmap(bitmap, 0, 0); |
| 740 return ""; |
| 741 } |
| 742 default: |
| 743 SkASSERT(false); |
| 744 return "Error: Should not be reached.\n"; |
| 745 } |
| 746 } |
| 747 |
| 748 SkISize AndroidCodecSrc::size() const { |
| 749 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
| 750 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| 751 if (nullptr == codec) { |
| 752 return SkISize::Make(0, 0); |
| 753 } |
| 754 return codec->getSampledDimensions(fSampleSize); |
| 755 } |
| 756 |
| 757 Name AndroidCodecSrc::name() const { |
| 758 // We will replicate the names used by CodecSrc so that images can |
| 759 // be compared in Gold. |
| 760 if (1 == fSampleSize) { |
| 761 return SkOSPath::Basename(fPath.c_str()); |
| 762 } |
| 763 return get_scaled_name(fPath, get_scale_from_sample_size(fSampleSize)); |
| 764 } |
| 765 |
| 766 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 767 |
| 611 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} | 768 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
| 612 | 769 |
| 613 bool ImageSrc::veto(SinkFlags flags) const { | 770 bool ImageSrc::veto(SinkFlags flags) const { |
| 614 // No need to test decoding to non-raster or indirect backend. | 771 // No need to test decoding to non-raster or indirect backend. |
| 615 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU
V. | 772 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU
V. |
| 616 return flags.type != SinkFlags::kRaster | 773 return flags.type != SinkFlags::kRaster |
| 617 || flags.approach != SinkFlags::kDirect; | 774 || flags.approach != SinkFlags::kDirect; |
| 618 } | 775 } |
| 619 | 776 |
| 620 Error ImageSrc::draw(SkCanvas* canvas) const { | 777 Error ImageSrc::draw(SkCanvas* canvas) const { |
| (...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1211 skr.visit<void>(i, drawsAsSingletonPictures); | 1368 skr.visit<void>(i, drawsAsSingletonPictures); |
| 1212 } | 1369 } |
| 1213 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1370 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
| 1214 | 1371 |
| 1215 canvas->drawPicture(macroPic); | 1372 canvas->drawPicture(macroPic); |
| 1216 return ""; | 1373 return ""; |
| 1217 }); | 1374 }); |
| 1218 } | 1375 } |
| 1219 | 1376 |
| 1220 } // namespace DM | 1377 } // namespace DM |
| OLD | NEW |