Index: tests/ImageDecodingTest.cpp |
diff --git a/tests/ImageDecodingTest.cpp b/tests/ImageDecodingTest.cpp |
index 69ed7dedad41c9ee76cdf9f5d91396f8927de4fa..160260d9c85eadfac4941f572d0b544b24e84dbf 100644 |
--- a/tests/ImageDecodingTest.cpp |
+++ b/tests/ImageDecodingTest.cpp |
@@ -361,6 +361,19 @@ static const SkColor kExpectedPixels[] = { |
static_assert((kExpectedWidth * kExpectedHeight) == SK_ARRAY_COUNT(kExpectedPixels), |
"array_size_mismatch"); |
+static bool decode_into_bitmap(skiatest::Reporter* r, SkBitmap* bm, SkData* encoded) { |
+ SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(encoded)); |
+ if (!gen) { |
+ REPORTER_ASSERT(r, false); |
+ return false; |
+ } |
+ if (!gen->tryGenerateBitmap(bm)) { |
+ REPORTER_ASSERT(r, false); |
+ return false; |
+ } |
+ return true; |
+} |
+ |
DEF_TEST(WebP, reporter) { |
const unsigned char encodedWebP[] = { |
0x52, 0x49, 0x46, 0x46, 0x2c, 0x01, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, |
@@ -390,69 +403,45 @@ DEF_TEST(WebP, reporter) { |
0xe3, 0xfe, 0x66, 0xa4, 0x7c, 0x1b, 0x6c, 0xd1, 0xa9, 0xd8, 0x14, 0xd0, |
0xc5, 0xb5, 0x39, 0x71, 0x97, 0x19, 0x19, 0x1b |
}; |
- SkAutoDataUnref encoded(SkData::NewWithCopy(encodedWebP, |
- sizeof(encodedWebP))); |
- SkBitmap bm; |
- bool success = SkInstallDiscardablePixelRef(encoded, &bm); |
- |
- REPORTER_ASSERT(reporter, success); |
- if (!success) { |
+ SkBitmap bm; |
+ SkAutoDataUnref encoded(SkData::NewWithoutCopy(encodedWebP, sizeof(encodedWebP))); |
+ if (!decode_into_bitmap(reporter, &bm, encoded)) { |
+ return; |
+ } |
+ if (kExpectedWidth != bm.width() || kExpectedHeight != bm.height()) { |
+ REPORTER_ASSERT(reporter, false); |
return; |
} |
- SkAutoLockPixels alp(bm); |
- bool rightSize = ((kExpectedWidth == bm.width()) |
- && (kExpectedHeight == bm.height())); |
- REPORTER_ASSERT(reporter, rightSize); |
- if (rightSize) { |
- bool error = false; |
- const SkColor* correctPixel = kExpectedPixels; |
- for (int y = 0; y < bm.height(); ++y) { |
- for (int x = 0; x < bm.width(); ++x) { |
- error |= (*correctPixel != bm.getColor(x, y)); |
- ++correctPixel; |
- } |
+ bool error = false; |
+ const SkColor* correctPixel = kExpectedPixels; |
+ for (int y = 0; y < bm.height(); ++y) { |
+ for (int x = 0; x < bm.width(); ++x) { |
+ error |= (*correctPixel != bm.getColor(x, y)); |
+ ++correctPixel; |
} |
- REPORTER_ASSERT(reporter, !error); |
} |
+ REPORTER_ASSERT(reporter, !error); |
} |
//////////////////////////////////////////////////////////////////////////////// |
-// example of how Android will do this inside their BitmapFactory |
-static SkPixelRef* install_pixel_ref(SkBitmap* bitmap, |
- SkStreamRewindable* stream, |
- int sampleSize, bool ditherImage) { |
- SkASSERT(bitmap != nullptr); |
- SkASSERT(stream != nullptr); |
- SkASSERT(stream->rewind()); |
- SkColorType colorType = bitmap->colorType(); |
- SkDecodingImageGenerator::Options opts(sampleSize, ditherImage, colorType); |
- if (SkInstallDiscardablePixelRef( |
- SkDecodingImageGenerator::Create(stream, opts), bitmap)) { |
- return bitmap->pixelRef(); |
- } |
- return nullptr; |
-} |
/** |
- * A test for the SkDecodingImageGenerator::Create and |
- * SkInstallDiscardablePixelRef functions. |
+ * A test for the SkDecodingImageGenerator::Create |
*/ |
DEF_TEST(ImprovedBitmapFactory, reporter) { |
SkString pngFilename = GetResourcePath("randPixels.png"); |
SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(pngFilename.c_str())); |
if (sk_exists(pngFilename.c_str())) { |
+ // example of how Android will do this inside their BitmapFactory |
+ SkDecodingImageGenerator::Options opts(1, true, kN32_SkColorType); |
SkBitmap bm; |
- SkAssertResult(bm.setInfo(SkImageInfo::MakeN32Premul(1, 1))); |
- REPORTER_ASSERT(reporter, |
- install_pixel_ref(&bm, stream.detach(), 1, true)); |
- SkAutoLockPixels alp(bm); |
- REPORTER_ASSERT(reporter, bm.getPixels()); |
+ SkImageGenerator* gen = SkDecodingImageGenerator::Create(stream, opts); |
+ REPORTER_ASSERT(reporter, gen->tryGenerateBitmap(&bm)); |
} |
} |
- |
//////////////////////////////////////////////////////////////////////////////// |
#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
@@ -513,23 +502,21 @@ static void test_options(skiatest::Reporter* reporter, |
bool useData, |
const SkString& path) { |
SkBitmap bm; |
- bool success = false; |
+ SkImageGenerator* gen; |
+ |
if (useData) { |
if (nullptr == encodedData) { |
return; |
} |
- success = SkInstallDiscardablePixelRef( |
- SkDecodingImageGenerator::Create(encodedData, opts), &bm); |
+ gen = SkDecodingImageGenerator::Create(encodedData, opts); |
} else { |
if (nullptr == encodedStream) { |
return; |
} |
- success = SkInstallDiscardablePixelRef( |
- SkDecodingImageGenerator::Create(encodedStream->duplicate(), opts), &bm); |
+ gen = SkDecodingImageGenerator::Create(encodedStream->duplicate(), opts); |
} |
- if (!success) { |
- if (opts.fUseRequestedColorType |
- && (kARGB_4444_SkColorType == opts.fRequestedColorType)) { |
+ if (!gen) { |
+ if (opts.fUseRequestedColorType && (kARGB_4444_SkColorType == opts.fRequestedColorType)) { |
return; // Ignore known conversion inabilities. |
} |
// If we get here, it's a failure and we will need more |
@@ -539,27 +526,22 @@ static void test_options(skiatest::Reporter* reporter, |
options_colorType(opts), path.c_str()); |
return; |
} |
+ if (!gen->tryGenerateBitmap(&bm)) { |
+ return; |
+ } |
+ |
#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
// Android is the only system that use Skia's image decoders in |
// production. For now, we'll only verify that samplesize works |
// on systems where it already is known to work. |
- REPORTER_ASSERT(reporter, check_rounding(bm.height(), kExpectedHeight, |
- opts.fSampleSize)); |
- REPORTER_ASSERT(reporter, check_rounding(bm.width(), kExpectedWidth, |
- opts.fSampleSize)); |
+ REPORTER_ASSERT(reporter, check_rounding(bm.height(), kExpectedHeight, opts.fSampleSize)); |
+ REPORTER_ASSERT(reporter, check_rounding(bm.width(), kExpectedWidth, opts.fSampleSize)); |
// The ImageDecoder API doesn't guarantee that SampleSize does |
// anything at all, but the decoders that this test excercises all |
// produce an output size in the following range: |
// (((sample_size * out_size) > (in_size - sample_size)) |
// && out_size <= SkNextPow2(((in_size - 1) / sample_size) + 1)); |
#endif // SK_BUILD_FOR_ANDROID || SK_BUILD_FOR_UNIX |
- SkAutoLockPixels alp(bm); |
- if (bm.getPixels() == nullptr) { |
- ERRORF(reporter, "Pixel decode failed [sampleSize=%d dither=%s " |
- "colorType=%s %s]", opts.fSampleSize, yn(opts.fDitherImage), |
- options_colorType(opts), path.c_str()); |
- return; |
- } |
SkColorType requestedColorType = opts.fRequestedColorType; |
REPORTER_ASSERT(reporter, |
@@ -661,7 +643,7 @@ DEF_TEST(ImageDecoderOptions, reporter) { |
} |
} |
-DEF_TEST(DiscardablePixelRef_SecondLockColorTableCheck, r) { |
+DEF_TEST(DecodingImageGenerator_ColorTableCheck, r) { |
SkString resourceDir = GetResourcePath(); |
SkString path = SkOSPath::Join(resourceDir.c_str(), "randPixels.gif"); |
if (!sk_exists(path.c_str())) { |
@@ -669,25 +651,20 @@ DEF_TEST(DiscardablePixelRef_SecondLockColorTableCheck, r) { |
} |
SkAutoDataUnref encoded(SkData::NewFromFileName(path.c_str())); |
SkBitmap bitmap; |
- if (!SkInstallDiscardablePixelRef( |
- SkDecodingImageGenerator::Create( |
- encoded, SkDecodingImageGenerator::Options()), &bitmap)) { |
- #ifndef SK_BUILD_FOR_WIN |
- ERRORF(r, "SkInstallDiscardablePixelRef [randPixels.gif] failed."); |
- #endif |
+ SkImageGenerator* gen = SkDecodingImageGenerator::Create(encoded, |
+ SkDecodingImageGenerator::Options()); |
+ if (!gen) { |
+ REPORTER_ASSERT(r, false); |
return; |
} |
- if (kIndex_8_SkColorType != bitmap.colorType()) { |
+ if (!gen->tryGenerateBitmap(&bitmap)) { |
+ REPORTER_ASSERT(r, false); |
return; |
} |
- { |
- SkAutoLockPixels alp(bitmap); |
- REPORTER_ASSERT(r, bitmap.getColorTable() && "first pass"); |
- } |
- { |
- SkAutoLockPixels alp(bitmap); |
- REPORTER_ASSERT(r, bitmap.getColorTable() && "second pass"); |
+ if (kIndex_8_SkColorType != bitmap.colorType()) { |
+ return; |
} |
+ REPORTER_ASSERT(r, bitmap.getColorTable()); |
} |