mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 04:39:06 +00:00
ImageDecoder: Handle decoding of images in an asynchronous manner
ImageDecoder now queues up image decoding requests and returns the images back to the caller later. ImageDecoderClient has a new promise-based API that allows callers to attach their own resolve/reject handlers to the responses from ImageDecoder.
This commit is contained in:
parent
0d8064fafc
commit
b871bc082c
Notes:
sideshowbarker
2024-07-16 21:45:42 +09:00
Author: https://github.com/ADKaster
Commit: b871bc082c
Pull-request: https://github.com/SerenityOS/serenity/pull/24028
Reviewed-by: https://github.com/LucasChollet
Reviewed-by: https://github.com/awesomekling
6 changed files with 132 additions and 31 deletions
|
@ -16,48 +16,101 @@ Client::Client(NonnullOwnPtr<Core::LocalSocket> socket)
|
||||||
|
|
||||||
void Client::die()
|
void Client::die()
|
||||||
{
|
{
|
||||||
|
for (auto& [_, promise] : m_pending_decoded_images) {
|
||||||
|
promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
||||||
|
}
|
||||||
|
m_pending_decoded_images.clear();
|
||||||
|
|
||||||
if (on_death)
|
if (on_death)
|
||||||
on_death();
|
on_death();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
NonnullRefPtr<Core::Promise<DecodedImage>> Client::decode_image(ReadonlyBytes encoded_data, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||||
{
|
{
|
||||||
if (encoded_data.is_empty())
|
auto promise = Core::Promise<DecodedImage>::construct();
|
||||||
return {};
|
if (on_resolved)
|
||||||
|
promise->on_resolution = move(on_resolved);
|
||||||
|
if (on_rejected)
|
||||||
|
promise->on_rejection = move(on_rejected);
|
||||||
|
|
||||||
|
if (encoded_data.is_empty()) {
|
||||||
|
promise->reject(Error::from_string_literal("No encoded data"));
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
|
auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
|
||||||
if (encoded_buffer_or_error.is_error()) {
|
if (encoded_buffer_or_error.is_error()) {
|
||||||
dbgln("Could not allocate encoded buffer");
|
dbgln("Could not allocate encoded buffer: {}", encoded_buffer_or_error.error());
|
||||||
return {};
|
promise->reject(encoded_buffer_or_error.release_error());
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
auto encoded_buffer = encoded_buffer_or_error.release_value();
|
auto encoded_buffer = encoded_buffer_or_error.release_value();
|
||||||
|
|
||||||
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
||||||
auto response_or_error = try_decode_image(move(encoded_buffer), ideal_size, mime_type);
|
|
||||||
|
|
||||||
if (response_or_error.is_error()) {
|
auto response = send_sync_but_allow_failure<Messages::ImageDecoderServer::DecodeImage>(move(encoded_buffer), ideal_size, mime_type);
|
||||||
dbgln("ImageDecoder died heroically");
|
if (!response) {
|
||||||
return {};
|
dbgln("ImageDecoder disconnected trying to decode image");
|
||||||
|
promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& response = response_or_error.value();
|
m_pending_decoded_images.set(response->image_id(), promise);
|
||||||
|
|
||||||
if (response.bitmaps().is_empty())
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||||
|
{
|
||||||
|
auto promise = decode_image(
|
||||||
|
encoded_data, [](auto) -> ErrorOr<void> { return {}; }, [](Error&) -> void {}, ideal_size, mime_type);
|
||||||
|
auto result = promise->await();
|
||||||
|
if (result.is_error())
|
||||||
return {};
|
return {};
|
||||||
|
return result.release_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> const& bitmaps, Vector<u32> const& durations, Gfx::FloatPoint scale)
|
||||||
|
{
|
||||||
|
VERIFY(!bitmaps.is_empty());
|
||||||
|
|
||||||
|
auto maybe_promise = m_pending_decoded_images.take(image_id);
|
||||||
|
if (!maybe_promise.has_value()) {
|
||||||
|
dbgln("ImageDecoderClient: No pending image with ID {}", image_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto promise = maybe_promise.release_value();
|
||||||
|
|
||||||
DecodedImage image;
|
DecodedImage image;
|
||||||
image.is_animated = response.is_animated();
|
image.is_animated = is_animated;
|
||||||
image.loop_count = response.loop_count();
|
image.loop_count = loop_count;
|
||||||
image.scale = response.scale();
|
image.scale = scale;
|
||||||
image.frames.ensure_capacity(response.bitmaps().size());
|
image.frames.ensure_capacity(bitmaps.size());
|
||||||
auto bitmaps = response.take_bitmaps();
|
|
||||||
for (size_t i = 0; i < bitmaps.size(); ++i) {
|
for (size_t i = 0; i < bitmaps.size(); ++i) {
|
||||||
if (!bitmaps[i].is_valid())
|
if (!bitmaps[i].is_valid()) {
|
||||||
return {};
|
dbgln("ImageDecoderClient: Invalid bitmap for request {} at index {}", image_id, i);
|
||||||
|
promise->reject(Error::from_string_literal("Invalid bitmap"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
image.frames.empend(*bitmaps[i].bitmap(), response.durations()[i]);
|
image.frames.empend(*bitmaps[i].bitmap(), durations[i]);
|
||||||
}
|
}
|
||||||
return image;
|
|
||||||
|
promise->resolve(move(image));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::did_fail_to_decode_image(i64 image_id, String const& error_message)
|
||||||
|
{
|
||||||
|
auto maybe_promise = m_pending_decoded_images.take(image_id);
|
||||||
|
if (!maybe_promise.has_value()) {
|
||||||
|
dbgln("ImageDecoderClient: No pending image with ID {}", image_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto promise = maybe_promise.release_value();
|
||||||
|
|
||||||
|
dbgln("ImageDecoderClient: Failed to decode image with ID {}: {}", image_id, error_message);
|
||||||
|
// FIXME: Include the error message in the Error object when Errors are allowed to hold Strings
|
||||||
|
promise->reject(Error::from_string_literal("Image decoding failed or aborted"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
|
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
|
||||||
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
|
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
|
||||||
|
#include <LibCore/Promise.h>
|
||||||
#include <LibIPC/ConnectionToServer.h>
|
#include <LibIPC/ConnectionToServer.h>
|
||||||
|
|
||||||
namespace ImageDecoderClient {
|
namespace ImageDecoderClient {
|
||||||
|
@ -33,12 +34,20 @@ class Client final
|
||||||
public:
|
public:
|
||||||
Client(NonnullOwnPtr<Core::LocalSocket>);
|
Client(NonnullOwnPtr<Core::LocalSocket>);
|
||||||
|
|
||||||
|
NonnullRefPtr<Core::Promise<DecodedImage>> decode_image(ReadonlyBytes, Function<ErrorOr<void>(DecodedImage&)> on_resolved, Function<void(Error&)> on_rejected, Optional<Gfx::IntSize> ideal_size = {}, Optional<ByteString> mime_type = {});
|
||||||
|
|
||||||
|
// FIXME: Move all clients to the promise-based API and get rid of this synchronous (i.e. EventLoop-spinning) one.
|
||||||
Optional<DecodedImage> decode_image(ReadonlyBytes, Optional<Gfx::IntSize> ideal_size = {}, Optional<ByteString> mime_type = {});
|
Optional<DecodedImage> decode_image(ReadonlyBytes, Optional<Gfx::IntSize> ideal_size = {}, Optional<ByteString> mime_type = {});
|
||||||
|
|
||||||
Function<void()> on_death;
|
Function<void()> on_death;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void die() override;
|
virtual void die() override;
|
||||||
|
|
||||||
|
virtual void did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> const& bitmaps, Vector<u32> const& durations, Gfx::FloatPoint scale) override;
|
||||||
|
virtual void did_fail_to_decode_image(i64 image_id, String const& error_message) override;
|
||||||
|
|
||||||
|
HashMap<i64, NonnullRefPtr<Core::Promise<DecodedImage>>> m_pending_decoded_images;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,20 +72,50 @@ static void decode_image_to_details(Core::AnonymousBuffer const& encoded_buffer,
|
||||||
decode_image_to_bitmaps_and_durations_with_decoder(*decoder, ideal_size, bitmaps, durations);
|
decode_image_to_bitmaps_and_durations_with_decoder(*decoder, ideal_size, bitmaps, durations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConnectionFromClient::Job ConnectionFromClient::make_decode_image_job(i64 image_id, Core::AnonymousBuffer encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||||
|
{
|
||||||
|
return [strong_this = NonnullRefPtr(*this), image_id, encoded_buffer = move(encoded_buffer), ideal_size = move(ideal_size), mime_type = move(mime_type)]() {
|
||||||
|
bool is_animated = false;
|
||||||
|
u32 loop_count = 0;
|
||||||
|
Gfx::FloatPoint scale { 1, 1 };
|
||||||
|
Vector<Gfx::ShareableBitmap> bitmaps;
|
||||||
|
Vector<u32> durations;
|
||||||
|
|
||||||
|
decode_image_to_details(encoded_buffer, ideal_size, mime_type, is_animated, loop_count, bitmaps, durations, scale);
|
||||||
|
if (bitmaps.is_empty()) {
|
||||||
|
strong_this->async_did_fail_to_decode_image(image_id, "Could not decode image"_string);
|
||||||
|
} else {
|
||||||
|
strong_this->async_did_decode_image(image_id, is_animated, loop_count, bitmaps, durations, scale);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type)
|
Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type)
|
||||||
{
|
{
|
||||||
|
auto image_id = m_next_image_id++;
|
||||||
|
|
||||||
if (!encoded_buffer.is_valid()) {
|
if (!encoded_buffer.is_valid()) {
|
||||||
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
|
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
|
||||||
return nullptr;
|
async_did_fail_to_decode_image(image_id, "Encoded data is invalid"_string);
|
||||||
|
return image_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_animated = false;
|
m_pending_jobs.set(image_id, make_decode_image_job(image_id, encoded_buffer, ideal_size, mime_type));
|
||||||
u32 loop_count = 0;
|
|
||||||
Gfx::FloatPoint scale { 1, 1 };
|
Core::deferred_invoke([strong_this = NonnullRefPtr(*this), image_id]() {
|
||||||
Vector<Gfx::ShareableBitmap> bitmaps;
|
if (auto job = strong_this->m_pending_jobs.take(image_id); job.has_value()) {
|
||||||
Vector<u32> durations;
|
job.value()();
|
||||||
decode_image_to_details(encoded_buffer, ideal_size, mime_type, is_animated, loop_count, bitmaps, durations, scale);
|
}
|
||||||
return { is_animated, loop_count, bitmaps, durations, scale };
|
});
|
||||||
|
|
||||||
|
return image_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionFromClient::cancel_decoding(i64 image_id)
|
||||||
|
{
|
||||||
|
if (auto job = m_pending_jobs.take(image_id); job.has_value()) {
|
||||||
|
async_did_fail_to_decode_image(image_id, "Decoding was canceled"_string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,17 @@ public:
|
||||||
virtual void die() override;
|
virtual void die() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
using Job = Function<void()>;
|
||||||
|
|
||||||
explicit ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket>);
|
explicit ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket>);
|
||||||
|
|
||||||
virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer const&, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type) override;
|
virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer const&, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type) override;
|
||||||
|
virtual void cancel_decoding(i64 image_id) override;
|
||||||
|
|
||||||
|
Job make_decode_image_job(i64 image_id, Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type);
|
||||||
|
|
||||||
|
i64 m_next_image_id { 0 };
|
||||||
|
HashMap<i64, Job> m_pending_jobs;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <LibCore/AnonymousBuffer.h>
|
|
||||||
#include <LibGfx/ShareableBitmap.h>
|
#include <LibGfx/ShareableBitmap.h>
|
||||||
|
|
||||||
endpoint ImageDecoderClient
|
endpoint ImageDecoderClient
|
||||||
{
|
{
|
||||||
|
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> bitmaps, Vector<u32> durations, Gfx::FloatPoint scale) =|
|
||||||
|
did_fail_to_decode_image(i64 image_id, String error_message) =|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <LibCore/AnonymousBuffer.h>
|
#include <LibCore/AnonymousBuffer.h>
|
||||||
#include <LibGfx/ShareableBitmap.h>
|
|
||||||
|
|
||||||
endpoint ImageDecoderServer
|
endpoint ImageDecoderServer
|
||||||
{
|
{
|
||||||
decode_image(Core::AnonymousBuffer data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) => (bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> bitmaps, Vector<u32> durations, Gfx::FloatPoint scale)
|
decode_image(Core::AnonymousBuffer data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) => (i64 image_id)
|
||||||
|
cancel_decoding(i64 image_id) =|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue