OLD | NEW |
---|---|
1 #include "DMSrcSink.h" | 1 #include "DMSrcSink.h" |
2 #include "SamplePipeControllers.h" | 2 #include "SamplePipeControllers.h" |
3 #include "SkCommonFlags.h" | 3 #include "SkCommonFlags.h" |
4 #include "SkCodec.h" | 4 #include "SkCodec.h" |
5 #include "SkDocument.h" | 5 #include "SkDocument.h" |
6 #include "SkError.h" | 6 #include "SkError.h" |
7 #include "SkMultiPictureDraw.h" | 7 #include "SkMultiPictureDraw.h" |
8 #include "SkNullCanvas.h" | 8 #include "SkNullCanvas.h" |
9 #include "SkOSFile.h" | 9 #include "SkOSFile.h" |
10 #include "SkPictureRecorder.h" | 10 #include "SkPictureRecorder.h" |
11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
12 #include "SkSVGCanvas.h" | 12 #include "SkSVGCanvas.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 #include "SkXMLWriter.h" | 14 #include "SkXMLWriter.h" |
15 | 15 |
16 DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder"); | |
17 | |
18 namespace DM { | 16 namespace DM { |
19 | 17 |
20 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} | 18 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
21 | 19 |
22 Error GMSrc::draw(SkCanvas* canvas) const { | 20 Error GMSrc::draw(SkCanvas* canvas) const { |
23 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 21 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
24 canvas->concat(gm->getInitialTransform()); | 22 canvas->concat(gm->getInitialTransform()); |
25 gm->draw(canvas); | 23 gm->draw(canvas); |
26 return ""; | 24 return ""; |
27 } | 25 } |
28 | 26 |
29 SkISize GMSrc::size() const { | 27 SkISize GMSrc::size() const { |
30 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 28 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
31 return gm->getISize(); | 29 return gm->getISize(); |
32 } | 30 } |
33 | 31 |
34 Name GMSrc::name() const { | 32 Name GMSrc::name() const { |
35 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 33 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
36 return gm->getName(); | 34 return gm->getName(); |
37 } | 35 } |
38 | 36 |
39 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | 37 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ |
40 | 38 |
39 CodecSrc::CodecSrc(Path path, SkColorType dstCT, SkAlphaType dstAT, SkipZeroes s kip, | |
40 Scanlines scanlines) | |
41 : fPath(path) | |
42 , fDstColorType(dstCT) | |
43 , fDstAlphaType(dstAT) | |
44 , fSkipZeroes(skip) | |
45 , fScanlines(scanlines) {} | |
46 | |
47 Error CodecSrc::draw(SkCanvas* canvas) const { | |
mtklein
2015/03/06 00:25:25
Can we use non-fatal errors here to have this Code
scroggo
2015/03/06 20:40:27
That could work. Although I'm still trying to wrap
scroggo
2015/03/06 20:43:45
My other thought is that the config you choose wil
scroggo
2015/03/13 21:34:33
Done.
| |
48 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | |
49 if (!encoded) { | |
50 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | |
51 } | |
52 | |
53 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | |
54 if (!codec) { | |
55 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | |
56 } | |
57 SkImageInfo info; | |
58 if (!codec->getInfo(&info)) { | |
59 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str()); | |
60 } | |
61 info = SkImageInfo::Make(info.width(), info.height(), fDstColorType, | |
62 fDstAlphaType, info.profileType()); | |
63 SkBitmap bitmap; | |
64 if (!bitmap.tryAllocPixels(info)) { | |
65 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str( ), | |
66 info.width(), info.height()); | |
67 } | |
68 | |
69 SkAutoLockPixels alp(bitmap); | |
70 if (kSkipZeroes == fSkipZeroes) { | |
71 memset(bitmap.getPixels(), 0, bitmap.getSafeSize()); | |
72 // TODO: modify the setting on codec. | |
73 } | |
74 | |
75 // TODO: Support other scanline modes. | |
76 SkASSERT(kNormal_Scanlines == fScanlines); | |
77 | |
78 const SkImageGenerator::Result result = codec->getPixels(info, bitmap.getPix els(), | |
79 bitmap.rowBytes()); | |
80 switch (result) { | |
81 case SkImageGenerator::kSuccess: | |
82 // We consider incomplete to be valid, since we should still decode what is | |
83 // available. | |
84 case SkImageGenerator::kIncompleteInput: | |
85 canvas->drawBitmap(bitmap, 0, 0); | |
86 return ""; | |
87 case SkImageGenerator::kInvalidConversion: | |
88 return Error::Nonfatal("Incompatible colortype conversion"); | |
89 default: | |
90 // Everything else is considered a failure. | |
91 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); | |
92 } | |
93 } | |
94 | |
95 SkISize CodecSrc::size() const { | |
96 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | |
97 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | |
98 SkImageInfo info; | |
99 if (codec && codec->getInfo(&info)) { | |
100 return info.dimensions(); | |
101 } | |
102 return SkISize::Make(0,0); | |
103 } | |
104 | |
105 // This code mimics code in DecodingBench. Can we unify? | |
106 const char* color_type_name(SkColorType colorType) { | |
107 switch(colorType) { | |
108 case kN32_SkColorType: | |
109 return "N32"; | |
110 case kRGB_565_SkColorType: | |
111 return "565"; | |
112 case kAlpha_8_SkColorType: | |
113 return "Alpha8"; | |
114 default: | |
115 return "Unknown"; | |
116 } | |
117 } | |
118 | |
119 const char* alpha_type_name(SkAlphaType alphaType) { | |
120 switch (alphaType) { | |
121 case kUnpremul_SkAlphaType: | |
122 return "Unpremul"; | |
123 case kPremul_SkAlphaType: | |
124 return "Premul"; | |
125 case kOpaque_SkAlphaType: | |
126 return "Opaque"; | |
127 default: | |
128 // We only support the above three. | |
129 SkASSERT(false); | |
130 return ""; | |
131 } | |
132 } | |
133 | |
134 Name CodecSrc::name() const { | |
135 return SkStringPrintf("%s_%s_%s", SkOSPath::Basename(fPath.c_str()).c_str(), | |
136 color_type_name(fDstColorType), | |
137 alpha_type_name(fDstAlphaType)); | |
138 } | |
139 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | |
140 | |
41 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} | 141 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
42 | 142 |
43 Error ImageSrc::draw(SkCanvas* canvas) const { | 143 Error ImageSrc::draw(SkCanvas* canvas) const { |
44 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 144 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
45 if (!encoded) { | 145 if (!encoded) { |
46 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 146 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
47 } | 147 } |
48 const SkColorType dstColorType = canvas->imageInfo().colorType(); | 148 const SkColorType dstColorType = canvas->imageInfo().colorType(); |
49 if (fDivisor == 0) { | 149 if (fDivisor == 0) { |
50 // Decode the full image. | 150 // Decode the full image. |
51 SkBitmap bitmap; | 151 SkBitmap bitmap; |
52 if (FLAGS_codec) { | 152 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit map, |
53 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 153 dstColorType, SkImageDecoder::kDecodeP ixels_Mode)) { |
54 if (!codec) { | 154 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
55 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | 155 } |
56 } | 156 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
57 SkImageInfo info; | 157 // Do not draw a bitmap with alpha to a destination without alpha. |
58 if (!codec->getInfo(&info)) { | 158 return Error::Nonfatal("Uninteresting to decode image with alpha int o 565."); |
59 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str()); | |
60 } | |
61 info = info.makeColorType(dstColorType); | |
62 if (info.alphaType() == kUnpremul_SkAlphaType) { | |
63 // FIXME: Currently we cannot draw unpremultiplied sources. | |
64 info = info.makeAlphaType(kPremul_SkAlphaType); | |
65 } | |
66 if (!bitmap.tryAllocPixels(info)) { | |
67 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPat h.c_str(), | |
68 info.width(), info.height()); | |
69 } | |
70 SkAutoLockPixels alp(bitmap); | |
71 const SkImageGenerator::Result result = codec->getPixels(info, bitma p.getPixels(), | |
72 bitmap.rowB ytes()); | |
73 switch (result) { | |
74 case SkImageGenerator::kSuccess: | |
75 // We consider incomplete to be valid, since we should still dec ode what is | |
76 // available. | |
77 case SkImageGenerator::kIncompleteInput: | |
78 break; | |
79 case SkImageGenerator::kInvalidConversion: | |
80 return Error::Nonfatal("Incompatible colortype conversion"); | |
81 default: | |
82 // Everything else is considered a failure. | |
83 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( )); | |
84 } | |
85 } else { | |
86 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, | |
87 dstColorType, SkImageDecoder::kDec odePixels_Mode)) { | |
88 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | |
89 } | |
90 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { | |
91 // Do not draw a bitmap with alpha to a destination without alph a. | |
92 return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); | |
93 } | |
94 } | 159 } |
95 encoded.reset((SkData*)NULL); // Might as well drop this when we're don e with it. | 160 encoded.reset((SkData*)NULL); // Might as well drop this when we're don e with it. |
96 canvas->drawBitmap(bitmap, 0,0); | 161 canvas->drawBitmap(bitmap, 0,0); |
97 return ""; | 162 return ""; |
98 } | 163 } |
99 // Decode subsets. This is a little involved. | 164 // Decode subsets. This is a little involved. |
100 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); | 165 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
101 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ; | 166 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ; |
102 if (!decoder) { | 167 if (!decoder) { |
103 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str() ); | 168 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str() ); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 surfaces.unrefAll(); | 598 surfaces.unrefAll(); |
534 return ""; | 599 return ""; |
535 } | 600 } |
536 SkISize size() const SK_OVERRIDE { return fSize; } | 601 SkISize size() const SK_OVERRIDE { return fSize; } |
537 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this. | 602 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this. |
538 } proxy(fW, fH, pic, src.size()); | 603 } proxy(fW, fH, pic, src.size()); |
539 return fSink->draw(proxy, bitmap, stream, log); | 604 return fSink->draw(proxy, bitmap, stream, log); |
540 } | 605 } |
541 | 606 |
542 } // namespace DM | 607 } // namespace DM |
OLD | NEW |