0

Handle empty SkBitmap results in WebTestControlHost::OnImageDump().

In some cases, we'll fail to create an SkBitmap (for instance, when
printing via `TestRunner::PrintFrameToBitmap()`. We handle those cases
safely in general, but we missed a check against the color space here.
This CL punts out of image encoding early when it's unnecessary (and
impossible).

Bug: 397018488
Change-Id: If5a43f24312639391d482081671963e151040cf1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6287486
Reviewed-by: Jonathan Hao <phao@chromium.org>
Commit-Queue: Mike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1423778}
This commit is contained in:
Mike West
2025-02-23 23:34:40 -08:00
committed by Chromium LUCI CQ
parent 48792bd8f4
commit d885a8be06

@ -1450,28 +1450,30 @@ void WebTestControlHost::OnImageDump(const std::string& actual_pixel_hash,
if (web_test_runtime_flags().dump_drag_image())
discard_transparency = false;
gfx::PNGCodec::ColorFormat pixel_format;
switch (image.info().colorType()) {
case kBGRA_8888_SkColorType:
pixel_format = gfx::PNGCodec::FORMAT_BGRA;
break;
case kRGBA_8888_SkColorType:
pixel_format = gfx::PNGCodec::FORMAT_RGBA;
break;
default:
NOTREACHED();
}
if (!image.drawsNothing()) {
gfx::PNGCodec::ColorFormat pixel_format;
switch (image.info().colorType()) {
case kBGRA_8888_SkColorType:
pixel_format = gfx::PNGCodec::FORMAT_BGRA;
break;
case kRGBA_8888_SkColorType:
pixel_format = gfx::PNGCodec::FORMAT_RGBA;
break;
default:
NOTREACHED();
}
std::vector<gfx::PNGCodec::Comment> comments;
// Used by
// //third_party/blink/tools/blinkpy/common/read_checksum_from_png.py
comments.emplace_back("checksum", actual_pixel_hash);
std::optional<std::vector<uint8_t>> png = gfx::PNGCodec::Encode(
static_cast<const unsigned char*>(image.getPixels()), pixel_format,
gfx::Size(image.width(), image.height()),
static_cast<int>(image.rowBytes()), discard_transparency, comments);
if (png) {
printer_->PrintImageBlock(png.value());
std::vector<gfx::PNGCodec::Comment> comments;
// Used by
// //third_party/blink/tools/blinkpy/common/read_checksum_from_png.py
comments.emplace_back("checksum", actual_pixel_hash);
std::optional<std::vector<uint8_t>> png = gfx::PNGCodec::Encode(
static_cast<const unsigned char*>(image.getPixels()), pixel_format,
gfx::Size(image.width(), image.height()),
static_cast<int>(image.rowBytes()), discard_transparency, comments);
if (png) {
printer_->PrintImageBlock(png.value());
}
}
}
printer_->PrintImageFooter();