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