[tests] Normalize SkImage before comparison. (flutter/engine#23489)

This commit is contained in:
Kaushik Iska
2021-01-07 16:47:06 -06:00
committed by GitHub
parent cbafc317cd
commit 5cae004d4d
3 changed files with 30 additions and 48 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@@ -30,61 +30,43 @@ sk_sp<SkSurface> CreateRenderSurface(const FlutterLayer& layer,
return surface;
}
bool RasterImagesAreSame(sk_sp<SkImage> a, sk_sp<SkImage> b) {
FML_CHECK(!a->isTextureBacked());
FML_CHECK(!b->isTextureBacked());
// Normalizes the color-space, color-type and alpha-type for comparison.
static sk_sp<SkData> NormalizeImage(sk_sp<SkImage> image) {
// To avoid clipping, convert to a very wide gamut, and a high bit depth.
sk_sp<SkColorSpace> norm_colorspace = SkColorSpace::MakeRGB(
SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
SkImageInfo norm_image_info =
SkImageInfo::Make(image->width(), image->height(),
SkColorType::kR16G16B16A16_unorm_SkColorType,
SkAlphaType::kUnpremul_SkAlphaType, norm_colorspace);
size_t row_bytes = norm_image_info.minRowBytes();
size_t size = norm_image_info.computeByteSize(row_bytes);
sk_sp<SkData> data = SkData::MakeUninitialized(size);
if (!data) {
FML_CHECK(false) << "Unable to allocate data.";
}
bool success = image->readPixels(norm_image_info, data->writable_data(),
row_bytes, 0, 0);
if (!success) {
FML_CHECK(false) << "Unable to read pixels.";
}
return data;
}
bool RasterImagesAreSame(sk_sp<SkImage> a, sk_sp<SkImage> b) {
if (!a || !b) {
return false;
}
SkPixmap pixmapA;
SkPixmap pixmapB;
FML_CHECK(!a->isTextureBacked());
FML_CHECK(!b->isTextureBacked());
if (!a->peekPixels(&pixmapA)) {
FML_LOG(ERROR) << "Could not peek pixels of image A.";
return false;
}
sk_sp<SkData> normalized_a = NormalizeImage(a);
sk_sp<SkData> normalized_b = NormalizeImage(b);
if (!b->peekPixels(&pixmapB)) {
FML_LOG(ERROR) << "Could not peek pixels of image B.";
return false;
}
const auto sizeA = pixmapA.rowBytes() * pixmapA.height();
const auto sizeB = pixmapB.rowBytes() * pixmapB.height();
if (sizeA != sizeB) {
FML_LOG(ERROR) << "Pixmap sizes were inconsistent.";
return false;
}
if (pixmapA.info() != pixmapB.info()) {
// Given that the sizes are equal but the infos aren't equal
// we have a situation where the color-type, color-space and
// alpha type are potentially mismatched. We fallback to compating
// pixel-wise values for alpha and color.
for (int x = 0; x < pixmapA.width(); x++) {
for (int y = 0; y < pixmapA.height(); y++) {
SkColor color_a = pixmapA.getColor(x, y);
SkColor color_b = pixmapB.getColor(x, y);
if (color_a != color_b) {
return false;
}
float alpha_a = pixmapA.getAlphaf(x, y);
float alpha_b = pixmapB.getAlphaf(x, y);
if (std::abs(alpha_a - alpha_b) >
std::numeric_limits<float>::epsilon()) {
return false;
}
}
}
return true;
}
return ::memcmp(pixmapA.addr(), pixmapB.addr(), sizeA) == 0;
return normalized_a->equals(normalized_b.get());
}
bool WriteImageToDisk(const fml::UniqueFD& directory,