Skip to content

Truly independent web browser

LibGfx+LibWeb: Encode WebDriver screenshots with fast PNG compression

Shannon Booth1154decd58a8
diff --git a/Libraries/LibGfx/ImageFormats/PNGWriter.cpp b/Libraries/LibGfx/ImageFormats/PNGWriter.cpp
index b3b1979bdc5..1933b47a464 100644
--- a/Libraries/LibGfx/ImageFormats/PNGWriter.cpp
+++ b/Libraries/LibGfx/ImageFormats/PNGWriter.cpp
@@ -10,6 +10,7 @@
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <png.h>
+#include <zlib.h>
namespace Gfx {
@@ -56,6 +57,13 @@ ErrorOr<ByteBuffer> PNGWriter::encode(Gfx::Bitmap const& bitmap, Options options
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ if (options.compression == PNGCompression::Fast) {
+ // NB: Trying every filter on each row and compressing with zlib's default level dominate the encoding time.
+ png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
+ png_set_compression_level(png_ptr, Z_BEST_SPEED);
+ png_set_compression_strategy(png_ptr, Z_RLE);
+ }
+
context->row_pointers.resize(height);
for (int y = 0; y < height; ++y) {
context->row_pointers[y] = const_cast<u8*>(bitmap.scanline_u8(y));
diff --git a/Libraries/LibGfx/ImageFormats/PNGWriter.h b/Libraries/LibGfx/ImageFormats/PNGWriter.h
index 054425fbe63..7ce1f3aeb3e 100644
--- a/Libraries/LibGfx/ImageFormats/PNGWriter.h
+++ b/Libraries/LibGfx/ImageFormats/PNGWriter.h
@@ -13,11 +13,18 @@
namespace Gfx {
+enum class PNGCompression : u8 {
+ Default,
+ // Encodes much faster, at the cost of a larger file.
+ Fast,
+};
+
// This is not a nested struct to work around https://llvm.org/PR36684
struct PNGWriterOptions {
// Data for the iCCP chunk.
// FIXME: Allow writing cICP, sRGB, or gAMA instead too.
Optional<ReadonlyBytes> icc_data;
+ PNGCompression compression { PNGCompression::Default };
};
class PNGWriter {
diff --git a/Libraries/LibWeb/WebDriver/Screenshot.cpp b/Libraries/LibWeb/WebDriver/Screenshot.cpp
index a000121ab45..0b10ec1b8b9 100644
--- a/Libraries/LibWeb/WebDriver/Screenshot.cpp
+++ b/Libraries/LibWeb/WebDriver/Screenshot.cpp
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Base64.h>
#include <LibGfx/Bitmap.h>
+#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/HTML/BrowsingContext.h>
@@ -93,18 +95,20 @@ Response encode_canvas_element(HTML::HTMLCanvasElement& canvas)
return Error::from_code(ErrorCode::UnableToCaptureScreen, "Captured screenshot is empty"sv);
// 3. Let file be a serialization of the canvas element’s bitmap as a file, using "image/png" as an argument.
- // 4. Let data url be a data: URL representing file. [RFC2397]
- auto data_url_result = canvas.to_data_url("image/png"sv, {});
- if (data_url_result.is_exception())
+ auto bitmap = canvas.get_bitmap_from_surface();
+ if (!bitmap)
+ return Error::from_code(ErrorCode::UnableToCaptureScreen, "Captured screenshot is empty"sv);
+ Gfx::PNGWriterOptions png_options;
+ png_options.compression = Gfx::PNGCompression::Fast;
+ auto file = Gfx::PNGWriter::encode(*bitmap, png_options);
+ if (file.is_error())
return Error::from_code(ErrorCode::UnableToCaptureScreen, "Failed to serialize canvas"sv);
- auto data_url = data_url_result.release_value();
+ // 4. Let data url be a data: URL representing file. [RFC2397]
// 5. Let index be the index of "," in data url.
- auto index = data_url.utf16_view().find_code_unit_offset(',');
- VERIFY(index.has_value());
-
// 6. Let encoded string be a substring of data url using (index + 1) as the start argument.
- auto encoded_string = MUST(data_url.utf16_view().substring_view(*index + 1).to_utf8());
+ // NB: That substring is file's base64 encoding.
+ auto encoded_string = MUST(encode_base64(file.value()));
// 7. Return success with data encoded string.
return JsonValue { move(encoded_string) };