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

Side by Side Diff: dm/DMSrcSink.cpp

Issue 1288963002: Provides multiple implementations of Android's SkBitmapRegionDecoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Acknowledged a subtle bug in the test Created 5 years, 3 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
OLDNEW
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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 return gm->getName(); 57 return gm->getName();
58 } 58 }
59 59
60 void GMSrc::modifyGrContextOptions(GrContextOptions* options) const { 60 void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
61 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); 61 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr));
62 gm->modifyGrContextOptions(options); 62 gm->modifyGrContextOptions(options);
63 } 63 }
64 64
65 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ 65 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
66 66
67 BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
68 CodecSrc::DstColorType dstColorType, uint32_t sampleSize)
69 : fPath(path)
70 , fStrategy(strategy)
71 , fMode(mode)
72 , fDstColorType(dstColorType)
73 , fSampleSize(sampleSize)
74 {}
75
76 bool BRDSrc::veto(SinkFlags flags) const {
77 // No need to test to non-raster or indirect backends.
78 return flags.type != SinkFlags::kRaster
79 || flags.approach != SinkFlags::kDirect;
80 }
81
82 static SkBitmapRegionDecoder* create_brd(Path path, SkBitmapRegionDecoder::Strat egy strategy) {
83 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
84 if (!encoded) {
85 return NULL;
86 }
87 return SkBitmapRegionDecoder::CreateBitmapRegionDecoder(new SkMemoryStream(e ncoded), strategy);
88 }
89
90 Error BRDSrc::draw(SkCanvas* canvas) const {
91 SkColorType colorType = canvas->imageInfo().colorType();
92 if (kRGB_565_SkColorType == colorType &&
93 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) {
94 return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
95 }
96 switch (fDstColorType) {
97 case CodecSrc::kGetFromCanvas_DstColorType:
98 break;
99 case CodecSrc::kIndex8_Always_DstColorType:
100 colorType = kIndex_8_SkColorType;
101 break;
102 case CodecSrc::kGrayscale_Always_DstColorType:
103 colorType = kGray_8_SkColorType;
104 break;
105 }
106
107 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
108 if (nullptr == brd.get()) {
109 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fP ath.c_str()));
110 }
111
112 const uint32_t width = brd->width();
113 const uint32_t height = brd->height();
114 switch (fMode) {
115 case kFullImage_Mode: {
116 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, width, height , fSampleSize,
117 colorType));
118 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
119 return Error::Nonfatal("Cannot convert to color type.\n");
120 }
121 canvas->drawBitmap(*bitmap, 0, 0);
122 return "";
123 }
124 case kDivisor_Mode: {
125 const uint32_t divisor = 2;
126 // Use a border to test subsets that extend outside the image.
127 // We will not allow the border to be larger than the image dimensio ns. Allowing
128 // these large borders causes off by one errors that indicate a prob lem with the
129 // test suite, not a problem with the implementation.
130 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * di visor);
131 const uint32_t scaledBorder = SkTMin(5u, maxBorder);
132 const uint32_t unscaledBorder = scaledBorder * fSampleSize;
133
134
135 for (uint32_t x = 0; x < divisor; x++) {
136 for (uint32_t y = 0; y < divisor; y++) {
137 // Calculate the subset dimensions
138 uint32_t subsetWidth = width / divisor;
139 uint32_t subsetHeight = height / divisor;
140 const int left = x * subsetWidth;
141 const int top = y * subsetHeight;
142
143 // Increase the size of the last subset in each row or colum n, when the
144 // divisor does not divide evenly into the image dimensions
145 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
146 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
147
148 // Increase the size of the subset in order to have a border on each side
149 const int decodeLeft = left - unscaledBorder;
150 const int decodeTop = top - unscaledBorder;
151 const uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
152 const uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
153 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(decodeLeft,
154 decodeTop, decodeWidth, decodeHeight, fSampleSize, c olorType));
155 if (nullptr == bitmap.get() || colorType != bitmap->colorTyp e()) {
156 return Error::Nonfatal("Cannot convert to color type.\n" );
157 }
158
159 // We may need to clear the canvas to avoid uninitialized me mory.
160 // Assume we are scaling a 780x780 image with sampleSize = 8 .
161 // The output image should be 97x97.
scroggo 2015/09/04 17:55:06 I think it's okay that the final image is less tha
msarett 2015/09/04 18:54:10 Yeah I agree with that assessment. BRD is behavin
162 // Each subset will be 390x390.
163 // Each scaled subset be 48x48.
164 // Four scaled subsets will only fill a 96x96 image.
165 // The bottom row and last column will not be touched.
166 // This is an unfortunate result of our rounding rules when scaling.
167 // Maybe we need to consider testing scaled subsets without trying to
168 // combine them to match the full scaled image? Or maybe th is is the
169 // best we can do?
170 canvas->clear(0);
171
172 canvas->drawBitmapRect(*bitmap,
173 SkRect::MakeXYWH(scaledBorder, scaledBorder,
174 subsetWidth / fSampleSize, subsetHeight / fS ampleSize),
175 SkRect::MakeXYWH(left / fSampleSize, top / fSampleSi ze,
176 subsetWidth / fSampleSize, subsetHeight / fS ampleSize),
177 nullptr);
178 }
179 }
180 return "";
181 }
182 default:
183 SkASSERT(false);
184 return "Error: Should not be reached.\n";
185 }
186 }
187
188 SkISize BRDSrc::size() const {
189 SkAutoTDelete<SkBitmapRegionDecoder> brd(create_brd(fPath, fStrategy));
190 if (brd) {
191 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize),
192 SkTMax(1, brd->height() / (int) fSampleSize));
193 }
194 return SkISize::Make(0, 0);
195 }
196
197 static SkString get_scaled_name(const Path& path, float scale) {
198 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), s cale);
199 }
200
201 Name BRDSrc::name() const {
202 // We will replicate the names used by CodecSrc so that images can
203 // be compared in Gold.
204 if (1 == fSampleSize) {
205 return SkOSPath::Basename(fPath.c_str());
206 }
207 return get_scaled_name(fPath, BRDSrc::GetScale(fSampleSize));
208 }
209
210 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
211
67 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) 212 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
68 : fPath(path) 213 : fPath(path)
69 , fMode(mode) 214 , fMode(mode)
70 , fDstColorType(dstColorType) 215 , fDstColorType(dstColorType)
71 , fScale(scale) 216 , fScale(scale)
72 {} 217 {}
73 218
74 bool CodecSrc::veto(SinkFlags flags) const { 219 bool CodecSrc::veto(SinkFlags flags) const {
75 // No need to test decoding to non-raster or indirect backend. 220 // No need to test decoding to non-raster or indirect backend.
76 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferr ed decode to 221 // TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferr ed decode to
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 return SkISize::Make(0, 0); 631 return SkISize::Make(0, 0);
487 } 632 }
488 } 633 }
489 SkISize size = codec->getScaledDimensions(fScale); 634 SkISize size = codec->getScaledDimensions(fScale);
490 return size; 635 return size;
491 } 636 }
492 637
493 Name CodecSrc::name() const { 638 Name CodecSrc::name() const {
494 if (1.0f == fScale) { 639 if (1.0f == fScale) {
495 return SkOSPath::Basename(fPath.c_str()); 640 return SkOSPath::Basename(fPath.c_str());
496 } else {
497 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str (), fScale);
498 } 641 }
642 return get_scaled_name(fPath, fScale);
499 } 643 }
500 644
501 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/ 645 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~*/
502 646
503 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} 647 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
504 648
505 bool ImageSrc::veto(SinkFlags flags) const { 649 bool ImageSrc::veto(SinkFlags flags) const {
506 // No need to test decoding to non-raster or indirect backend. 650 // No need to test decoding to non-raster or indirect backend.
507 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU V. 651 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU V.
508 return flags.type != SinkFlags::kRaster 652 return flags.type != SinkFlags::kRaster
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 skr.visit<void>(i, drawsAsSingletonPictures); 1230 skr.visit<void>(i, drawsAsSingletonPictures);
1087 } 1231 }
1088 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); 1232 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
1089 1233
1090 canvas->drawPicture(macroPic); 1234 canvas->drawPicture(macroPic);
1091 return ""; 1235 return "";
1092 }); 1236 });
1093 } 1237 }
1094 1238
1095 } // namespace DM 1239 } // namespace DM
OLDNEW
« no previous file with comments | « dm/DMSrcSink.h ('k') | gyp/utils.gyp » ('j') | src/utils/SkBitmapRegionCanvas.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698