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

Unified 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: Use nullptr instead of NULL Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: dm/DMSrcSink.cpp
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index 12530142d29f013eff4f65162721aff7e0437dcc..9a42f8b405ba2d91449c208998c2f11f66103095 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -64,6 +64,118 @@ void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+BRDSrc::BRDSrc(Path path, SkBitmapRegionDecoder::Strategy strategy, Mode mode,
+ SkColorType dstColorType, uint32_t sampleSize)
+ : fPath(path)
+ , fStrategy(strategy)
+ , fMode(mode)
+ , fColorType(dstColorType)
+ , fSampleSize(sampleSize)
+{}
+
+bool BRDSrc::veto(SinkFlags flags) const {
+ // No need to test to non-raster or indirect backends.
+ return flags.type != SinkFlags::kRaster
+ || flags.approach != SinkFlags::kDirect;
+}
+
+Error BRDSrc::draw(SkCanvas* canvas) const {
+ if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
+ return Error::Nonfatal("Testing to multiple backends is uninteresting.");
scroggo 2015/09/01 22:05:28 More importantly, I think we never run this for 56
msarett 2015/09/02 18:02:23 Sorry, forgot about this. Otherwise I might have
scroggo 2015/09/02 21:32:24 I agree that it is a little awkward. I assume your
msarett 2015/09/03 19:20:51 I like this idea.
+ }
+
+ SkAutoTDelete<SkBitmapRegionDecoder> brd(
+ SkBitmapRegionDecoder::CreateBitmapRegionDecoder(
+ SkNEW_ARGS(SkMemoryStream, (SkData::NewFromFileName(fPath.c_str()))),
scroggo 2015/09/01 22:05:28 nit: no need for SkNEW anymore
msarett 2015/09/02 18:02:23 Done.
+ fStrategy));
+ if (nullptr == brd.get()) {
+ return Error::Nonfatal(SkStringPrintf("Could not create brd for %s.", fPath.c_str()));
+ }
+
+ switch (fMode) {
+ case kFullImage_Mode: {
+ SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, brd->width(),
+ brd->height(), fSampleSize, fColorType));
+ if (nullptr == bitmap.get() || fColorType != bitmap->colorType()) {
+ return Error::Nonfatal("Cannot convert to color type.\n");
+ }
+ canvas->drawBitmap(*bitmap, 0, 0);
+ return "";
+ }
+ case kDivisor_Mode: {
+ uint32_t divisor = 2;
+ // Use a border to test subsets that extend outside the image.
+ // We will not allow the border to be larger than the image dimensions. Allowing
+ // these large borders causes off by one errors that indicate a problem with the
+ // test suite, not a problem with the implementation.
+ uint32_t maxBorder = SkTMin(brd->width(), brd->height()) / (fSampleSize * divisor);
+ uint32_t scaledBorder = SkTMin(5u, maxBorder);
+ uint32_t unscaledBorder = scaledBorder * fSampleSize;
+
+ uint32_t width = brd->width();
+ uint32_t height = brd->height();
+ for (uint32_t x = 0; x < divisor; x++) {
+ for (uint32_t y = 0; y < divisor; y++) {
+ // Calculate the subset dimensions
+ uint32_t subsetWidth = width / divisor;
+ uint32_t subsetHeight = height / divisor;
+ int left = x * subsetWidth;
+ int top = y * subsetHeight;
+
+ // Increase the size of the last subset in each row or column, when the
+ // divisor does not divide evenly into the image dimensions
+ subsetWidth += (x + 1 == divisor) ? (width % divisor) : 0;
+ subsetHeight += (y + 1 == divisor) ? (height % divisor) : 0;
+
+ // Increase the size of the subset in order to have a border on each side
+ int decodeLeft = left - unscaledBorder;
+ int decodeTop = top - unscaledBorder;
+ uint32_t decodeWidth = subsetWidth + unscaledBorder * 2;
+ uint32_t decodeHeight = subsetHeight + unscaledBorder * 2;
+
+ SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(decodeLeft,
+ decodeTop, decodeWidth, decodeHeight, fSampleSize, fColorType));
+ if (nullptr == bitmap.get() || fColorType != bitmap->colorType()) {
+ return Error::Nonfatal("Cannot convert to color type.\n");
+ }
+ canvas->drawBitmapRect(*bitmap,
+ SkRect::MakeXYWH(scaledBorder, scaledBorder,
+ subsetWidth / fSampleSize, subsetHeight / fSampleSize),
+ SkRect::MakeXYWH(left / fSampleSize, top / fSampleSize,
+ subsetWidth / fSampleSize, subsetHeight / fSampleSize),
+ nullptr);
+ }
+ }
+ return "";
+ }
+ default:
+ SkASSERT(false);
+ return "Error: Should not be reached.\n";
+ }
+}
+
+SkISize BRDSrc::size() const {
+ SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
+ SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
+ if (codec) {
+ return SkISize::Make(SkTMax(1, codec->getInfo().width() / (int) fSampleSize),
scroggo 2015/09/01 22:05:28 It seems a little weird that we calculate this usi
msarett 2015/09/02 18:02:23 Agreed. Fixed to use a BRD.
+ SkTMax(1, codec->getInfo().height() / (int) fSampleSize));
+ }
+ return SkISize::Make(0, 0);
+}
+
+Name BRDSrc::name() const {
+ // We will replicate the names used by CodecSrc so that images can
+ // be compared in Gold.
+ if (1.0f == fSampleSize) {
+ return SkOSPath::Basename(fPath.c_str());
+ }
+ float scale = 1.0f / fSampleSize;
+ return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str(), scale);
scroggo 2015/09/01 22:05:28 Maybe this should be in a helper function?
msarett 2015/09/02 18:02:23 Yeah I think so.
+}
+
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
: fPath(path)
, fMode(mode)
@@ -471,9 +583,8 @@ SkISize CodecSrc::size() const {
Name CodecSrc::name() const {
if (1.0f == fScale) {
return SkOSPath::Basename(fPath.c_str());
- } else {
- return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str(), fScale);
}
+ return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str(), fScale);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

Powered by Google App Engine
This is Rietveld 408576698