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 "SkCodec.h" | 10 #include "SkCodec.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); | 56 SkAutoTDelete<skiagm::GM> gm(fFactory(nullptr)); |
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 | |
67 BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoderInterface::Strategy strategy, Mod
e 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 SkBitmapRegionDecoderInterface* create_brd(Path path, | |
83 SkBitmapRegionDecoderInterface::Strategy strategy) { | |
84 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); | |
85 if (!encoded) { | |
86 return NULL; | |
87 } | |
88 return SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(new SkMemor
yStream(encoded), | |
89 strategy); | |
90 } | |
91 | |
92 Error BRDSrc::draw(SkCanvas* canvas) const { | |
93 SkColorType colorType = canvas->imageInfo().colorType(); | |
94 if (kRGB_565_SkColorType == colorType && | |
95 CodecSrc::kGetFromCanvas_DstColorType != fDstColorType) { | |
96 return Error::Nonfatal("Testing non-565 to 565 is uninteresting."); | |
97 } | |
98 switch (fDstColorType) { | |
99 case CodecSrc::kGetFromCanvas_DstColorType: | |
100 break; | |
101 case CodecSrc::kIndex8_Always_DstColorType: | |
102 colorType = kIndex_8_SkColorType; | |
103 break; | |
104 case CodecSrc::kGrayscale_Always_DstColorType: | |
105 colorType = kGray_8_SkColorType; | |
106 break; | |
107 } | |
108 | |
109 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrateg
y)); | |
110 if (nullptr == brd.get()) { | |
111 return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fP
ath.c_str())); | |
112 } | |
113 | |
114 const uint32_t width = brd->width(); | |
115 const uint32_t height = brd->height(); | |
116 // Visually inspecting very small output images is not necessary. | |
117 if ((width / fSampleSize <= 10 || height / fSampleSize <= 10) && 1 != fSampl
eSize) { | |
118 return Error::Nonfatal("Scaling very small images is uninteresting."); | |
119 } | |
120 switch (fMode) { | |
121 case kFullImage_Mode: { | |
122 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, width, height
, fSampleSize, | |
123 colorType)); | |
124 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) { | |
125 return Error::Nonfatal("Cannot convert to color type.\n"); | |
126 } | |
127 canvas->drawBitmap(*bitmap, 0, 0); | |
128 return ""; | |
129 } | |
130 case kDivisor_Mode: { | |
131 const uint32_t divisor = 2; | |
132 if (width < divisor || height < divisor) { | |
133 return Error::Nonfatal("Divisor is larger than image dimension.\
n"); | |
134 } | |
135 | |
136 // Use a border to test subsets that extend outside the image. | |
137 // We will not allow the border to be larger than the image dimensio
ns. Allowing | |
138 // these large borders causes off by one errors that indicate a prob
lem with the | |
139 // test suite, not a problem with the implementation. | |
140 const uint32_t maxBorder = SkTMin(width, height) / (fSampleSize * di
visor); | |
141 const uint32_t scaledBorder = SkTMin(5u, maxBorder); | |
142 const uint32_t unscaledBorder = scaledBorder * fSampleSize; | |
143 | |
144 // We may need to clear the canvas to avoid uninitialized memory. | |
145 // Assume we are scaling a 780x780 image with sampleSize = 8. | |
146 // The output image should be 97x97. | |
147 // Each subset will be 390x390. | |
148 // Each scaled subset be 48x48. | |
149 // Four scaled subsets will only fill a 96x96 image. | |
150 // The bottom row and last column will not be touched. | |
151 // This is an unfortunate result of our rounding rules when scaling. | |
152 // Maybe we need to consider testing scaled subsets without trying t
o | |
153 // combine them to match the full scaled image? Or maybe this is th
e | |
154 // best we can do? | |
155 canvas->clear(0); | |
156 | |
157 for (uint32_t x = 0; x < divisor; x++) { | |
158 for (uint32_t y = 0; y < divisor; y++) { | |
159 // Calculate the subset dimensions | |
160 uint32_t subsetWidth = width / divisor; | |
161 uint32_t subsetHeight = height / divisor; | |
162 const int left = x * subsetWidth; | |
163 const int top = y * subsetHeight; | |
164 | |
165 // Increase the size of the last subset in each row or colum
n, when the | |
166 // divisor does not divide evenly into the image dimensions | |
167 subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0; | |
168 subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0; | |
169 | |
170 // Increase the size of the subset in order to have a border
on each side | |
171 const int decodeLeft = left - unscaledBorder; | |
172 const int decodeTop = top - unscaledBorder; | |
173 const uint32_t decodeWidth = subsetWidth + unscaledBorder *
2; | |
174 const uint32_t decodeHeight = subsetHeight + unscaledBorder
* 2; | |
175 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(decodeLeft, | |
176 decodeTop, decodeWidth, decodeHeight, fSampleSize, c
olorType)); | |
177 if (nullptr == bitmap.get() || colorType != bitmap->colorTyp
e()) { | |
178 return Error::Nonfatal("Cannot convert to color type.\n"
); | |
179 } | |
180 | |
181 canvas->drawBitmapRect(*bitmap, | |
182 SkRect::MakeXYWH((SkScalar) scaledBorder, (SkScalar)
scaledBorder, | |
183 (SkScalar) (subsetWidth / fSampleSize), | |
184 (SkScalar) (subsetHeight / fSampleSize)), | |
185 SkRect::MakeXYWH((SkScalar) (left / fSampleSize), | |
186 (SkScalar) (top / fSampleSize), | |
187 (SkScalar) (subsetWidth / fSampleSize), | |
188 (SkScalar) (subsetHeight / fSampleSize)), | |
189 nullptr); | |
190 } | |
191 } | |
192 return ""; | |
193 } | |
194 default: | |
195 SkASSERT(false); | |
196 return "Error: Should not be reached.\n"; | |
197 } | |
198 } | |
199 | |
200 SkISize BRDSrc::size() const { | |
201 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(create_brd(fPath, fStrateg
y)); | |
202 if (brd) { | |
203 return SkISize::Make(SkTMax(1, brd->width() / (int) fSampleSize), | |
204 SkTMax(1, brd->height() / (int) fSampleSize)); | |
205 } | |
206 return SkISize::Make(0, 0); | |
207 } | |
208 | |
209 static SkString get_scaled_name(const Path& path, float scale) { | |
210 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(path.c_str()).c_str(), s
cale); | |
211 } | |
212 | |
213 Name BRDSrc::name() const { | |
214 // We will replicate the names used by CodecSrc so that images can | |
215 // be compared in Gold. | |
216 if (1 == fSampleSize) { | |
217 return SkOSPath::Basename(fPath.c_str()); | |
218 } | |
219 return get_scaled_name(fPath, BRDSrc::GetScale(fSampleSize)); | |
220 } | |
221 | |
222 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | |
223 | 66 |
224 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) | 67 CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale) |
225 : fPath(path) | 68 : fPath(path) |
226 , fMode(mode) | 69 , fMode(mode) |
227 , fDstColorType(dstColorType) | 70 , fDstColorType(dstColorType) |
228 , fScale(scale) | 71 , fScale(scale) |
229 {} | 72 {} |
230 | 73 |
231 bool CodecSrc::veto(SinkFlags flags) const { | 74 bool CodecSrc::veto(SinkFlags flags) const { |
232 // No need to test decoding to non-raster or indirect backend. | 75 // No need to test decoding to non-raster or indirect backend. |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 return SkISize::Make(0, 0); | 486 return SkISize::Make(0, 0); |
644 } | 487 } |
645 } | 488 } |
646 SkISize size = codec->getScaledDimensions(fScale); | 489 SkISize size = codec->getScaledDimensions(fScale); |
647 return size; | 490 return size; |
648 } | 491 } |
649 | 492 |
650 Name CodecSrc::name() const { | 493 Name CodecSrc::name() const { |
651 if (1.0f == fScale) { | 494 if (1.0f == fScale) { |
652 return SkOSPath::Basename(fPath.c_str()); | 495 return SkOSPath::Basename(fPath.c_str()); |
| 496 } else { |
| 497 return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str
(), fScale); |
653 } | 498 } |
654 return get_scaled_name(fPath, fScale); | |
655 } | 499 } |
656 | 500 |
657 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 501 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
658 | 502 |
659 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} | 503 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
660 | 504 |
661 bool ImageSrc::veto(SinkFlags flags) const { | 505 bool ImageSrc::veto(SinkFlags flags) const { |
662 // No need to test decoding to non-raster or indirect backend. | 506 // No need to test decoding to non-raster or indirect backend. |
663 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU
V. | 507 // TODO: Instead, use lazy decoding to allow the GPU to handle cases like YU
V. |
664 return flags.type != SinkFlags::kRaster | 508 return flags.type != SinkFlags::kRaster |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1242 skr.visit<void>(i, drawsAsSingletonPictures); | 1086 skr.visit<void>(i, drawsAsSingletonPictures); |
1243 } | 1087 } |
1244 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1088 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
1245 | 1089 |
1246 canvas->drawPicture(macroPic); | 1090 canvas->drawPicture(macroPic); |
1247 return ""; | 1091 return ""; |
1248 }); | 1092 }); |
1249 } | 1093 } |
1250 | 1094 |
1251 } // namespace DM | 1095 } // namespace DM |
OLD | NEW |