From 24376e7759163ee80ff11065fe4a79ecb0796145 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 15 May 2021 15:01:38 +0200 Subject: [PATCH] LibGfx: Avoid copying ByteBuffers while loading PNG images This wasn't much of a problem before because copying the ByteBuffer merely copied the RefPtr but now that ByteBuffer behaves like Vector this causes unnecessary allocations. --- Userland/Libraries/LibGfx/PNGLoader.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGfx/PNGLoader.cpp b/Userland/Libraries/LibGfx/PNGLoader.cpp index 2e866ffb8b..31d62d9e5d 100644 --- a/Userland/Libraries/LibGfx/PNGLoader.cpp +++ b/Userland/Libraries/LibGfx/PNGLoader.cpp @@ -101,7 +101,7 @@ struct PNGLoadingContext { bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; } Vector scanlines; RefPtr bitmap; - ByteBuffer decompression_buffer; + ByteBuffer* decompression_buffer { nullptr }; Vector compressed_data; Vector palette_data; Vector palette_transparency_data; @@ -580,7 +580,7 @@ static bool decode_png_chunks(PNGLoadingContext& context) static bool decode_png_bitmap_simple(PNGLoadingContext& context) { - Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size()); + Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size()); for (int y = 0; y < context.height; ++y) { u8 filter; @@ -726,7 +726,7 @@ static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, in static bool decode_png_adam7(PNGLoadingContext& context) { - Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size()); + Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size()); context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height }); if (!context.bitmap) return false; @@ -759,7 +759,7 @@ static bool decode_png_bitmap(PNGLoadingContext& context) context.state = PNGLoadingContext::State::Error; return false; } - context.decompression_buffer = result.value(); + context.decompression_buffer = &result.value(); context.compressed_data.clear(); context.scanlines.ensure_capacity(context.height); @@ -777,7 +777,7 @@ static bool decode_png_bitmap(PNGLoadingContext& context) return false; } - context.decompression_buffer.clear(); + context.decompression_buffer = nullptr; context.state = PNGLoadingContext::State::BitmapDecoded; return true;