Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: dm/DMSrcSink.cpp

Issue 978823002: Run CodecSrc DM. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Respond to comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dm/DMSrcSink.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkPictureData.h" 10 #include "SkPictureData.h"
11 #include "SkPictureRecorder.h" 11 #include "SkPictureRecorder.h"
12 #include "SkRandom.h" 12 #include "SkRandom.h"
13 #include "SkSVGCanvas.h" 13 #include "SkSVGCanvas.h"
14 #include "SkStream.h" 14 #include "SkStream.h"
15 #include "SkXMLWriter.h" 15 #include "SkXMLWriter.h"
16 16
17 DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder");
18
19 namespace DM { 17 namespace DM {
20 18
21 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} 19 GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
22 20
23 Error GMSrc::draw(SkCanvas* canvas) const { 21 Error GMSrc::draw(SkCanvas* canvas) const {
24 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); 22 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
25 canvas->concat(gm->getInitialTransform()); 23 canvas->concat(gm->getInitialTransform());
26 gm->draw(canvas); 24 gm->draw(canvas);
27 return ""; 25 return "";
28 } 26 }
29 27
30 SkISize GMSrc::size() const { 28 SkISize GMSrc::size() const {
31 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); 29 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
32 return gm->getISize(); 30 return gm->getISize();
33 } 31 }
34 32
35 Name GMSrc::name() const { 33 Name GMSrc::name() const {
36 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); 34 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
37 return gm->getName(); 35 return gm->getName();
38 } 36 }
39 37
40 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ 38 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
41 39
40 CodecSrc::CodecSrc(Path path) : fPath(path) {}
41
42 Error CodecSrc::draw(SkCanvas* canvas) const {
43 SkImageInfo canvasInfo;
44 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
45 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a de ferred decode to
46 // let the GPU handle it.
47 return Error::Nonfatal("No need to test decoding to non-raster backend." );
48 }
49
50 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
51 if (!encoded) {
52 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
53 }
54
55 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
56 if (!codec) {
57 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
58 }
59
60 SkImageInfo decodeInfo;
61 if (!codec->getInfo(&decodeInfo)) {
62 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str());
63 }
64
65 decodeInfo = decodeInfo.makeColorType(canvasInfo.colorType());
66 if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
67 // FIXME: Currently we cannot draw unpremultiplied sources.
68 decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
69 }
70
71 SkBitmap bitmap;
72 if (!bitmap.tryAllocPixels(decodeInfo)) {
73 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str( ),
74 decodeInfo.width(), decodeInfo.height());
75 }
76
77 SkAutoLockPixels alp(bitmap);
78 switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) {
79 case SkImageGenerator::kSuccess:
80 // We consider incomplete to be valid, since we should still decode what is
81 // available.
82 case SkImageGenerator::kIncompleteInput:
83 canvas->drawBitmap(bitmap, 0, 0);
84 return "";
85 case SkImageGenerator::kInvalidConversion:
86 return Error::Nonfatal("Incompatible colortype conversion");
87 default:
88 // Everything else is considered a failure.
89 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
90 }
91 }
92
93 SkISize CodecSrc::size() const {
94 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
95 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
96 SkImageInfo info;
97 if (codec && codec->getInfo(&info)) {
98 return info.dimensions();
99 }
100 return SkISize::Make(0,0);
101 }
102
103 Name CodecSrc::name() const {
104 return SkOSPath::Basename(fPath.c_str());
105 }
106
107 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
108
42 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} 109 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
43 110
44 Error ImageSrc::draw(SkCanvas* canvas) const { 111 Error ImageSrc::draw(SkCanvas* canvas) const {
112 SkImageInfo canvasInfo;
113 if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
114 // TODO: Instead, use lazy decoding to allow the GPU to handle cases lik e YUV.
115 return Error::Nonfatal("No need to test decoding to non-raster backend." );
116 }
117
45 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); 118 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
46 if (!encoded) { 119 if (!encoded) {
47 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); 120 return SkStringPrintf("Couldn't read %s.", fPath.c_str());
48 } 121 }
49 const SkColorType dstColorType = canvas->imageInfo().colorType(); 122 const SkColorType dstColorType = canvasInfo.colorType();
50 if (fDivisor == 0) { 123 if (fDivisor == 0) {
51 // Decode the full image. 124 // Decode the full image.
52 SkBitmap bitmap; 125 SkBitmap bitmap;
53 if (FLAGS_codec) { 126 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit map,
54 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); 127 dstColorType, SkImageDecoder::kDecodeP ixels_Mode)) {
55 if (!codec) { 128 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
56 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); 129 }
57 } 130 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
58 SkImageInfo info; 131 // Do not draw a bitmap with alpha to a destination without alpha.
59 if (!codec->getInfo(&info)) { 132 return Error::Nonfatal("Uninteresting to decode image with alpha int o 565.");
60 return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str());
61 }
62 info = info.makeColorType(dstColorType);
63 if (info.alphaType() == kUnpremul_SkAlphaType) {
64 // FIXME: Currently we cannot draw unpremultiplied sources.
65 info = info.makeAlphaType(kPremul_SkAlphaType);
66 }
67 if (!bitmap.tryAllocPixels(info)) {
68 return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPat h.c_str(),
69 info.width(), info.height());
70 }
71 SkAutoLockPixels alp(bitmap);
72 const SkImageGenerator::Result result = codec->getPixels(info, bitma p.getPixels(),
73 bitmap.rowB ytes());
74 switch (result) {
75 case SkImageGenerator::kSuccess:
76 // We consider incomplete to be valid, since we should still dec ode what is
77 // available.
78 case SkImageGenerator::kIncompleteInput:
79 break;
80 case SkImageGenerator::kInvalidConversion:
81 return Error::Nonfatal("Incompatible colortype conversion");
82 default:
83 // Everything else is considered a failure.
84 return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str( ));
85 }
86 } else {
87 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
88 dstColorType, SkImageDecoder::kDec odePixels_Mode)) {
89 return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
90 }
91 if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
92 // Do not draw a bitmap with alpha to a destination without alph a.
93 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
94 }
95 } 133 }
96 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.
97 canvas->drawBitmap(bitmap, 0,0); 135 canvas->drawBitmap(bitmap, 0,0);
98 return ""; 136 return "";
99 } 137 }
100 // Decode subsets. This is a little involved. 138 // Decode subsets. This is a little involved.
101 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); 139 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
102 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ; 140 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get())) ;
103 if (!decoder) { 141 if (!decoder) {
104 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 29 matching lines...) Expand all
134 return Error::Nonfatal("Uninteresting to decode image with alpha into 565."); 172 return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
135 } 173 }
136 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); 174 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
137 } 175 }
138 } 176 }
139 return ""; 177 return "";
140 } 178 }
141 179
142 SkISize ImageSrc::size() const { 180 SkISize ImageSrc::size() const {
143 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); 181 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
144 if (FLAGS_codec) { 182 SkBitmap bitmap;
145 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); 183 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
146 if (!codec) { 184 encoded->size(),
147 return SkISize::Make(0,0); 185 &bitmap,
148 } 186 kUnknown_SkColorType,
149 SkImageInfo info; 187 SkImageDecoder::kDecodeBounds_ Mode)) {
150 if (!codec->getInfo(&info)) { 188 return SkISize::Make(0,0);
151 return SkISize::Make(0,0);
152 }
153 return info.dimensions();
154 } else {
155 SkBitmap bitmap;
156 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
157 encoded->size(),
158 &bitmap,
159 kUnknown_SkColorType,
160 SkImageDecoder::kDecodeBou nds_Mode)) {
161 return SkISize::Make(0,0);
162 }
163 return bitmap.dimensions();
164 } 189 }
190 return bitmap.dimensions();
165 } 191 }
166 192
167 Name ImageSrc::name() const { 193 Name ImageSrc::name() const {
168 return SkOSPath::Basename(fPath.c_str()); 194 return SkOSPath::Basename(fPath.c_str());
169 } 195 }
170 196
171 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ 197 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
172 198
173 static const SkRect kSKPViewport = {0,0, 1000,1000}; 199 static const SkRect kSKPViewport = {0,0, 1000,1000};
174 200
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 surfaces.unrefAll(); 583 surfaces.unrefAll();
558 return ""; 584 return "";
559 } 585 }
560 SkISize size() const SK_OVERRIDE { return fSize; } 586 SkISize size() const SK_OVERRIDE { return fSize; }
561 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this. 587 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou ld be calling this.
562 } proxy(fW, fH, pic, src.size()); 588 } proxy(fW, fH, pic, src.size());
563 return fSink->draw(proxy, bitmap, stream, log); 589 return fSink->draw(proxy, bitmap, stream, log);
564 } 590 }
565 591
566 } // namespace DM 592 } // namespace DM
OLDNEW
« no previous file with comments | « dm/DMSrcSink.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698