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 "SkAndroidCodec.h" |
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 // We will replicate the names used by CodecSrc so that images can | 759 // We will replicate the names used by CodecSrc so that images can |
760 // be compared in Gold. | 760 // be compared in Gold. |
761 if (1 == fSampleSize) { | 761 if (1 == fSampleSize) { |
762 return SkOSPath::Basename(fPath.c_str()); | 762 return SkOSPath::Basename(fPath.c_str()); |
763 } | 763 } |
764 return get_scaled_name(fPath, get_scale_from_sample_size(fSampleSize)); | 764 return get_scaled_name(fPath, get_scale_from_sample_size(fSampleSize)); |
765 } | 765 } |
766 | 766 |
767 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 767 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
768 | 768 |
769 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} | 769 ImageSrc::ImageSrc(Path path) : fPath(path) {} |
770 | 770 |
771 bool ImageSrc::veto(SinkFlags flags) const { | 771 bool ImageSrc::veto(SinkFlags flags) const { |
772 // No need to test decoding to non-raster or indirect backend. | 772 // No need to test decoding to non-raster or indirect backend. |
773 // 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. |
774 return flags.type != SinkFlags::kRaster | 774 return flags.type != SinkFlags::kRaster |
775 || flags.approach != SinkFlags::kDirect; | 775 || flags.approach != SinkFlags::kDirect; |
776 } | 776 } |
777 | 777 |
778 Error ImageSrc::draw(SkCanvas* canvas) const { | 778 Error ImageSrc::draw(SkCanvas* canvas) const { |
779 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 779 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
780 if (!encoded) { | 780 if (!encoded) { |
781 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 781 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
782 } | 782 } |
783 const SkColorType dstColorType = canvas->imageInfo().colorType(); | 783 const SkColorType dstColorType = canvas->imageInfo().colorType(); |
784 if (fDivisor == 0) { | 784 |
785 // Decode the full image. | 785 // Decode the full image. |
786 SkBitmap bitmap; | 786 SkBitmap bitmap; |
787 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit
map, | 787 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
788 dstColorType, SkImageDecoder::kDecodeP
ixels_Mode)) { | 788 dstColorType, SkImageDecoder::kDecodePixel
s_Mode)) { |
789 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | 789 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
790 } | |
791 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { | |
792 // Do not draw a bitmap with alpha to a destination without alpha. | |
793 return Error::Nonfatal("Uninteresting to decode image with alpha int
o 565."); | |
794 } | |
795 encoded.reset((SkData*)nullptr); // Might as well drop this when we're
done with it. | |
796 canvas->drawBitmap(bitmap, 0,0); | |
797 return ""; | |
798 } | 790 } |
799 // Decode subsets. This is a little involved. | 791 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
800 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); | 792 // Do not draw a bitmap with alpha to a destination without alpha. |
801 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()))
; | 793 return Error::Nonfatal("Uninteresting to decode image with alpha into 56
5."); |
802 if (!decoder) { | |
803 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()
); | |
804 } | 794 } |
805 stream->rewind(); | 795 encoded.reset((SkData*)nullptr); // Might as well drop this when we're done
with it. |
806 int w,h; | 796 canvas->drawBitmap(bitmap, 0,0); |
807 if (!decoder->buildTileIndex(stream.detach(), &w, &h)) { | |
808 return Error::Nonfatal("Subset decoding not supported."); | |
809 } | |
810 | |
811 // Divide the image into subsets that cover the entire image. | |
812 if (fDivisor > w || fDivisor > h) { | |
813 return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d
is too big" | |
814 "for %s with dimensions (%d x %d)", fDivisor, fPath.c_str(), w,
h)); | |
815 } | |
816 const int subsetWidth = w / fDivisor, | |
817 subsetHeight = h / fDivisor; | |
818 for (int y = 0; y < h; y += subsetHeight) { | |
819 for (int x = 0; x < w; x += subsetWidth) { | |
820 SkBitmap subset; | |
821 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); | |
822 if (!decoder->decodeSubset(&subset, rect, dstColorType)) { | |
823 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).
", | |
824 x, y, x+subsetWidth, y+subsetHeight); | |
825 } | |
826 if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) { | |
827 // Do not draw a bitmap with alpha to a destination without alph
a. | |
828 // This is not an error, but there is nothing interesting to sho
w. | |
829 | |
830 // This should only happen on the first iteration through the lo
op. | |
831 SkASSERT(0 == x && 0 == y); | |
832 | |
833 return Error::Nonfatal("Uninteresting to decode image with alpha
into 565."); | |
834 } | |
835 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); | |
836 } | |
837 } | |
838 return ""; | 797 return ""; |
839 } | 798 } |
840 | 799 |
841 SkISize ImageSrc::size() const { | 800 SkISize ImageSrc::size() const { |
842 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 801 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
843 SkBitmap bitmap; | 802 SkBitmap bitmap; |
844 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), | 803 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
845 encoded->size(), | 804 encoded->size(), |
846 &bitmap, | 805 &bitmap, |
847 kUnknown_SkColorType, | 806 kUnknown_SkColorType, |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1374 skr.visit<void>(i, drawsAsSingletonPictures); | 1333 skr.visit<void>(i, drawsAsSingletonPictures); |
1375 } | 1334 } |
1376 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1335 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
1377 | 1336 |
1378 canvas->drawPicture(macroPic); | 1337 canvas->drawPicture(macroPic); |
1379 return ""; | 1338 return ""; |
1380 }); | 1339 }); |
1381 } | 1340 } |
1382 | 1341 |
1383 } // namespace DM | 1342 } // namespace DM |
OLD | NEW |