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"); | 16 DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder"); |
mtklein
2015/03/17 20:24:39
Forgot: remove this?
scroggo
2015/03/19 12:57:54
Good catch. I lost track of that when moving betwe
| |
17 | 17 |
18 namespace DM { | 18 namespace DM { |
19 | 19 |
20 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} | 20 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} |
21 | 21 |
22 Error GMSrc::draw(SkCanvas* canvas) const { | 22 Error GMSrc::draw(SkCanvas* canvas) const { |
23 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 23 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
24 canvas->concat(gm->getInitialTransform()); | 24 canvas->concat(gm->getInitialTransform()); |
25 gm->draw(canvas); | 25 gm->draw(canvas); |
26 return ""; | 26 return ""; |
27 } | 27 } |
28 | 28 |
29 SkISize GMSrc::size() const { | 29 SkISize GMSrc::size() const { |
30 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 30 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
31 return gm->getISize(); | 31 return gm->getISize(); |
32 } | 32 } |
33 | 33 |
34 Name GMSrc::name() const { | 34 Name GMSrc::name() const { |
35 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 35 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
36 return gm->getName(); | 36 return gm->getName(); |
37 } | 37 } |
38 | 38 |
39 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | 39 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ |
40 | 40 |
41 CodecSrc::CodecSrc(Path path) : fPath(path) {} | |
42 | |
43 Error CodecSrc::draw(SkCanvas* canvas) const { | |
44 SkImageInfo canvasInfo; | |
45 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) { | |
46 return Error::Nonfatal("No need to test decoding to non-raster backend." ); | |
mtklein
2015/03/17 20:24:15
// TODO: rethink with JPGs ?
scroggo
2015/03/19 12:57:55
Done.
| |
47 } | |
48 | |
49 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | |
50 if (!encoded) { | |
51 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | |
52 } | |
53 | |
54 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | |
55 if (!codec) { | |
56 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | |
57 } | |
58 | |
59 SkImageInfo decodeInfo; | |
60 if (!codec->getInfo(&decodeInfo)) { | |
61 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str()); | |
62 } | |
63 | |
64 decodeInfo = decodeInfo.makeColorType(canvasInfo.colorType()); | |
65 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) { | |
66 // FIXME: Currently we cannot draw unpremultiplied sources. | |
67 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType); | |
68 } | |
69 | |
70 SkBitmap bitmap; | |
71 if (!bitmap.tryAllocPixels(decodeInfo)) { | |
72 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str( ), | |
73 decodeInfo.width(), decodeInfo.height()); | |
74 } | |
75 | |
76 SkAutoLockPixels alp(bitmap); | |
mtklein
2015/03/17 20:24:15
This seems fine, but it's not needed right? SkBit
scroggo
2015/03/19 12:57:54
The implementation of tryAllocPixels does lock aut
| |
77 const SkImageGenerator::Result result = codec->getPixels(decodeInfo, bitmap. getPixels(), | |
78 bitmap.rowBytes()); | |
79 switch (result) { | |
mtklein
2015/03/17 20:24:15
Might consider fusing into
switch (codec->getPixe
scroggo
2015/03/19 12:57:54
Done.
| |
80 case SkImageGenerator::kSuccess: | |
81 // We consider incomplete to be valid, since we should still decode what is | |
82 // available. | |
83 case SkImageGenerator::kIncompleteInput: | |
84 canvas->drawBitmap(bitmap, 0, 0); | |
85 return ""; | |
86 case SkImageGenerator::kInvalidConversion: | |
87 return Error::Nonfatal("Incompatible colortype conversion"); | |
88 default: | |
89 // Everything else is considered a failure. | |
90 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); | |
91 } | |
92 } | |
93 | |
94 SkISize CodecSrc::size() const { | |
95 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | |
96 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | |
97 SkImageInfo info; | |
98 if (codec && codec->getInfo(&info)) { | |
99 return info.dimensions(); | |
100 } | |
101 return SkISize::Make(0,0); | |
102 } | |
103 | |
104 Name CodecSrc::name() const { | |
105 return SkOSPath::Basename(fPath.c_str()); | |
106 } | |
107 | |
108 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ | |
109 | |
41 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} | 110 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
42 | 111 |
43 Error ImageSrc::draw(SkCanvas* canvas) const { | 112 Error ImageSrc::draw(SkCanvas* canvas) const { |
113 SkImageInfo canvasInfo; | |
114 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) { | |
115 return Error::Nonfatal("No need to test decoding to non-raster backend." ); | |
scroggo
2015/03/19 12:57:54
I've also added a TODO to ImageSrc to use deferred
| |
116 } | |
117 | |
44 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 118 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
45 if (!encoded) { | 119 if (!encoded) { |
46 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 120 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
47 } | 121 } |
48 const SkColorType dstColorType = canvas->imageInfo().colorType(); | 122 const SkColorType dstColorType = canvasInfo.colorType(); |
49 if (fDivisor == 0) { | 123 if (fDivisor == 0) { |
50 // Decode the full image. | 124 // Decode the full image. |
51 SkBitmap bitmap; | 125 SkBitmap bitmap; |
52 if (FLAGS_codec) { | 126 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit map, |
53 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 127 dstColorType, SkImageDecoder::kDecodeP ixels_Mode)) { |
54 if (!codec) { | 128 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
55 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | 129 } |
56 } | 130 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) { |
57 SkImageInfo info; | 131 // Do not draw a bitmap with alpha to a destination without alpha. |
58 if (!codec->getInfo(&info)) { | 132 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 } | 133 } |
95 encoded.reset((SkData*)NULL); // Might as well drop this when we're don e with it. | 134 encoded.reset((SkData*)NULL); // Might as well drop this when we're don e with it. |
96 canvas->drawBitmap(bitmap, 0,0); | 135 canvas->drawBitmap(bitmap, 0,0); |
97 return ""; | 136 return ""; |
98 } | 137 } |
99 // Decode subsets. This is a little involved. | 138 // Decode subsets. This is a little involved. |
100 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); | 139 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
101 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ; | 140 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ; |
102 if (!decoder) { | 141 if (!decoder) { |
103 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str() ); | 142 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(); | 572 surfaces.unrefAll(); |
534 return ""; | 573 return ""; |
535 } | 574 } |
536 SkISize size() const SK_OVERRIDE { return fSize; } | 575 SkISize size() const SK_OVERRIDE { return fSize; } |
537 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this. | 576 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this. |
538 } proxy(fW, fH, pic, src.size()); | 577 } proxy(fW, fH, pic, src.size()); |
539 return fSink->draw(proxy, bitmap, stream, log); | 578 return fSink->draw(proxy, bitmap, stream, log); |
540 } | 579 } |
541 | 580 |
542 } // namespace DM | 581 } // namespace DM |
OLD | NEW |