From 3b9f110161bc78d8534a29e3f14030e1f5a15574 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 20 Feb 2021 11:35:00 +0100 Subject: LibWeb+LibImageDecoderClient: Reuse ImageDecoder service process The overhead from spawning a new ImageDecoder for every decoding job is way too large and causing other problems as well (#5421) Let's keep the same decoder open and reuse it as long as it's working. --- Userland/Libraries/LibWeb/Loader/ImageResource.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries/LibWeb') diff --git a/Userland/Libraries/LibWeb/Loader/ImageResource.cpp b/Userland/Libraries/LibWeb/Loader/ImageResource.cpp index ba94e8d039..001eb30877 100644 --- a/Userland/Libraries/LibWeb/Loader/ImageResource.cpp +++ b/Userland/Libraries/LibWeb/Loader/ImageResource.cpp @@ -48,6 +48,18 @@ int ImageResource::frame_duration(size_t frame_index) const return m_decoded_frames[frame_index].duration; } +static ImageDecoderClient::Client& image_decoder_client() +{ + static RefPtr image_decoder_client; + if (!image_decoder_client) { + image_decoder_client = ImageDecoderClient::Client::construct(); + image_decoder_client->on_death = [&] { + image_decoder_client = nullptr; + }; + } + return *image_decoder_client; +} + void ImageResource::decode_if_needed() const { if (!has_encoded_data()) @@ -59,8 +71,9 @@ void ImageResource::decode_if_needed() const if (!m_decoded_frames.is_empty()) return; - auto image_decoder_client = ImageDecoderClient::Client::construct(); - auto image = image_decoder_client->decode_image(encoded_data()); + NonnullRefPtr decoder = image_decoder_client(); + auto image = decoder->decode_image(encoded_data()); + if (image.has_value()) { m_loop_count = image.value().loop_count; m_animated = image.value().is_animated; -- cgit v1.2.3