mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
LibImageDecoderClient: Remove sync id fetch from async decode request
This commit is contained in:
committed by
Gregory Bertilson
parent
1477d86b3e
commit
7d902c2a89
Notes:
github-actions[bot]
2026-02-28 06:05:14 +00:00
Author: https://github.com/jonbgamble Commit: https://github.com/LadybirdBrowser/ladybird/commit/7d902c2a89f Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/8073 Reviewed-by: https://github.com/Zaggy1024 ✅
@@ -17,10 +17,10 @@ Client::Client(NonnullOwnPtr<IPC::Transport> transport)
|
||||
void Client::die()
|
||||
{
|
||||
verify_event_loop();
|
||||
for (auto& [_, promise] : m_pending_decoded_images) {
|
||||
promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
||||
}
|
||||
m_pending_decoded_images.clear();
|
||||
auto pending_promises = move(m_token_promises);
|
||||
|
||||
for (auto& promise : pending_promises)
|
||||
promise.value->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -47,27 +47,24 @@ NonnullRefPtr<Core::Promise<DecodedImage>> Client::decode_image(ReadonlyBytes en
|
||||
|
||||
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
||||
|
||||
auto response = send_sync_but_allow_failure<Messages::ImageDecoderServer::DecodeImage>(move(encoded_buffer), ideal_size, mime_type);
|
||||
if (!response) {
|
||||
dbgln("ImageDecoder disconnected trying to decode image");
|
||||
promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
||||
return promise;
|
||||
}
|
||||
i64 request_id = m_next_request_id++;
|
||||
m_token_promises.set(request_id, promise);
|
||||
|
||||
m_pending_decoded_images.set(response->image_id(), promise);
|
||||
async_decode_image(encoded_buffer, ideal_size, mime_type, request_id);
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmap_sequence, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_space, i64 session_id)
|
||||
void Client::did_decode_image(i64 request_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmap_sequence, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_space, i64 session_id)
|
||||
{
|
||||
verify_event_loop();
|
||||
auto bitmaps = move(bitmap_sequence.bitmaps);
|
||||
VERIFY(!bitmaps.is_empty());
|
||||
|
||||
auto maybe_promise = m_pending_decoded_images.take(image_id);
|
||||
Optional<NonnullRefPtr<Core::Promise<DecodedImage>>> maybe_promise = m_token_promises.take(request_id);
|
||||
|
||||
if (!maybe_promise.has_value()) {
|
||||
dbgln("ImageDecoderClient: No pending image with ID {}", image_id);
|
||||
dbgln("ImageDecoderClient: No pending image with request token {}", request_id);
|
||||
return;
|
||||
}
|
||||
auto promise = maybe_promise.release_value();
|
||||
@@ -89,7 +86,7 @@ void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gf
|
||||
|
||||
for (size_t i = 0; i < bitmaps.size(); ++i) {
|
||||
if (!bitmaps[i]) {
|
||||
dbgln("ImageDecoderClient: Invalid bitmap for request {} at index {}", image_id, i);
|
||||
dbgln("ImageDecoderClient: Invalid bitmap for request {} at index {}", request_id, i);
|
||||
promise->reject(Error::from_string_literal("Invalid bitmap"));
|
||||
return;
|
||||
}
|
||||
@@ -101,17 +98,18 @@ void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gf
|
||||
promise->resolve(move(image));
|
||||
}
|
||||
|
||||
void Client::did_fail_to_decode_image(i64 image_id, String error_message)
|
||||
void Client::did_fail_to_decode_image(i64 request_id, String error_message)
|
||||
{
|
||||
verify_event_loop();
|
||||
auto maybe_promise = m_pending_decoded_images.take(image_id);
|
||||
Optional<NonnullRefPtr<Core::Promise<DecodedImage>>> maybe_promise = m_token_promises.take(request_id);
|
||||
|
||||
if (!maybe_promise.has_value()) {
|
||||
dbgln("ImageDecoderClient: No pending image with ID {}", image_id);
|
||||
dbgln("ImageDecoderClient: No pending image with request token {}", request_id);
|
||||
return;
|
||||
}
|
||||
auto promise = maybe_promise.release_value();
|
||||
|
||||
dbgln("ImageDecoderClient: Failed to decode image with ID {}: {}", image_id, error_message);
|
||||
dbgln("ImageDecoderClient: Failed to decode image with request token {}: {}", request_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"));
|
||||
}
|
||||
|
||||
@@ -55,14 +55,15 @@ private:
|
||||
void verify_event_loop() const;
|
||||
virtual void die() override;
|
||||
|
||||
virtual void did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmap_sequence, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_space, i64 session_id) override;
|
||||
virtual void did_fail_to_decode_image(i64 image_id, String error_message) override;
|
||||
virtual void did_decode_image(i64 request_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmap_sequence, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_space, i64 session_id) override;
|
||||
virtual void did_fail_to_decode_image(i64 request_id, String error_message) override;
|
||||
|
||||
virtual void did_decode_animation_frames(i64 session_id, Gfx::BitmapSequence bitmaps) override;
|
||||
virtual void did_fail_animation_decode(i64 session_id, String error_message) override;
|
||||
|
||||
Core::EventLoop* m_creation_event_loop { &Core::EventLoop::current() };
|
||||
HashMap<i64, NonnullRefPtr<Core::Promise<DecodedImage>>> m_pending_decoded_images;
|
||||
i64 m_next_request_id { 0 };
|
||||
HashMap<i64, NonnullRefPtr<Core::Promise<DecodedImage>>> m_token_promises;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -183,13 +183,13 @@ static ErrorOr<ConnectionFromClient::DecodeResult> decode_image_to_details(Core:
|
||||
return result;
|
||||
}
|
||||
|
||||
NonnullRefPtr<ConnectionFromClient::Job> ConnectionFromClient::make_decode_image_job(i64 image_id, Core::AnonymousBuffer encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||
NonnullRefPtr<ConnectionFromClient::Job> ConnectionFromClient::make_decode_image_job(i64 request_id, Core::AnonymousBuffer encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||
{
|
||||
return Job::construct(
|
||||
[encoded_buffer = move(encoded_buffer), ideal_size = move(ideal_size), mime_type = move(mime_type)](auto&) mutable -> ErrorOr<DecodeResult> {
|
||||
return TRY(decode_image_to_details(move(encoded_buffer), ideal_size, mime_type));
|
||||
},
|
||||
[strong_this = NonnullRefPtr(*this), image_id](DecodeResult result) -> ErrorOr<void> {
|
||||
[strong_this = NonnullRefPtr(*this), request_id](DecodeResult result) -> ErrorOr<void> {
|
||||
i64 session_id = 0;
|
||||
|
||||
if (result.decoder) {
|
||||
@@ -202,35 +202,31 @@ NonnullRefPtr<ConnectionFromClient::Job> ConnectionFromClient::make_decode_image
|
||||
strong_this->m_animation_sessions.set(session_id, move(session));
|
||||
}
|
||||
|
||||
strong_this->async_did_decode_image(image_id, result.is_animated, result.loop_count, move(result.bitmaps), move(result.durations), result.scale, move(result.color_profile), session_id);
|
||||
strong_this->m_pending_jobs.remove(image_id);
|
||||
strong_this->async_did_decode_image(request_id, result.is_animated, result.loop_count, move(result.bitmaps), move(result.durations), result.scale, move(result.color_profile), session_id);
|
||||
strong_this->m_pending_jobs.remove(request_id);
|
||||
return {};
|
||||
},
|
||||
[strong_this = NonnullRefPtr(*this), image_id](Error error) -> void {
|
||||
[strong_this = NonnullRefPtr(*this), request_id](Error error) -> void {
|
||||
if (strong_this->is_open())
|
||||
strong_this->async_did_fail_to_decode_image(image_id, MUST(String::formatted("Decoding failed: {}", error)));
|
||||
strong_this->m_pending_jobs.remove(image_id);
|
||||
strong_this->async_did_fail_to_decode_image(request_id, MUST(String::formatted("Decoding failed: {}", error)));
|
||||
strong_this->m_pending_jobs.remove(request_id);
|
||||
});
|
||||
}
|
||||
|
||||
Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type)
|
||||
void ConnectionFromClient::decode_image(Core::AnonymousBuffer encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type, i64 request_id)
|
||||
{
|
||||
auto image_id = m_next_image_id++;
|
||||
|
||||
if (!encoded_buffer.is_valid()) {
|
||||
dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
|
||||
async_did_fail_to_decode_image(image_id, "Encoded data is invalid"_string);
|
||||
return image_id;
|
||||
async_did_fail_to_decode_image(request_id, "Encoded data is invalid"_string);
|
||||
return;
|
||||
}
|
||||
|
||||
m_pending_jobs.set(image_id, make_decode_image_job(image_id, move(encoded_buffer), ideal_size, move(mime_type)));
|
||||
|
||||
return image_id;
|
||||
m_pending_jobs.set(request_id, make_decode_image_job(request_id, move(encoded_buffer), ideal_size, move(mime_type)));
|
||||
}
|
||||
|
||||
void ConnectionFromClient::cancel_decoding(i64 image_id)
|
||||
void ConnectionFromClient::cancel_decoding(i64 request_id)
|
||||
{
|
||||
if (auto job = m_pending_jobs.take(image_id); job.has_value()) {
|
||||
if (auto job = m_pending_jobs.take(request_id); job.has_value()) {
|
||||
job.value()->cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +55,8 @@ private:
|
||||
|
||||
explicit ConnectionFromClient(NonnullOwnPtr<IPC::Transport>);
|
||||
|
||||
virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) override;
|
||||
virtual void cancel_decoding(i64 image_id) override;
|
||||
virtual void decode_image(Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type, i64 request_id) override;
|
||||
virtual void cancel_decoding(i64 request_id) override;
|
||||
virtual void request_animation_frames(i64 session_id, u32 start_frame_index, u32 count) override;
|
||||
virtual void stop_animation_decode(i64 session_id) override;
|
||||
virtual Messages::ImageDecoderServer::ConnectNewClientsResponse connect_new_clients(size_t count) override;
|
||||
@@ -64,9 +64,8 @@ private:
|
||||
|
||||
ErrorOr<IPC::File> connect_new_client();
|
||||
|
||||
NonnullRefPtr<Job> make_decode_image_job(i64 image_id, Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type);
|
||||
NonnullRefPtr<Job> make_decode_image_job(i64 request_id, Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type);
|
||||
|
||||
i64 m_next_image_id { 0 };
|
||||
i64 m_next_session_id { 1 };
|
||||
HashMap<i64, NonnullRefPtr<Job>> m_pending_jobs;
|
||||
HashMap<i64, NonnullOwnPtr<AnimationSession>> m_animation_sessions;
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
endpoint ImageDecoderClient
|
||||
{
|
||||
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmaps, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_profile, i64 session_id) =|
|
||||
did_fail_to_decode_image(i64 image_id, String error_message) =|
|
||||
did_decode_image(i64 request_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmaps, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_profile, i64 session_id) =|
|
||||
did_fail_to_decode_image(i64 request_id, String error_message) =|
|
||||
|
||||
did_decode_animation_frames(i64 session_id, Gfx::BitmapSequence bitmaps) =|
|
||||
did_fail_animation_decode(i64 session_id, String error_message) =|
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
endpoint ImageDecoderServer
|
||||
{
|
||||
init_transport(int peer_pid) => (int peer_pid)
|
||||
decode_image(Core::AnonymousBuffer data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) => (i64 image_id)
|
||||
cancel_decoding(i64 image_id) =|
|
||||
decode_image(Core::AnonymousBuffer data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type, i64 request_id) =|
|
||||
cancel_decoding(i64 request_id) =|
|
||||
|
||||
request_animation_frames(i64 session_id, u32 start_frame_index, u32 count) =|
|
||||
stop_animation_decode(i64 session_id) =|
|
||||
|
||||
Reference in New Issue
Block a user