2020-06-22 19:35:22 +00:00
|
|
|
/*
|
2021-01-31 08:24:46 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-06-22 19:35:22 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 19:35:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-16 22:58:57 +00:00
|
|
|
#include <LibCore/AnonymousBuffer.h>
|
2020-06-22 19:35:22 +00:00
|
|
|
#include <LibImageDecoderClient/Client.h>
|
|
|
|
|
|
|
|
namespace ImageDecoderClient {
|
|
|
|
|
2023-02-08 22:05:44 +00:00
|
|
|
Client::Client(NonnullOwnPtr<Core::LocalSocket> socket)
|
2022-02-25 10:27:37 +00:00
|
|
|
: IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket))
|
2020-06-22 19:35:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:35:00 +00:00
|
|
|
void Client::die()
|
|
|
|
{
|
2024-04-19 20:23:16 +00:00
|
|
|
for (auto& [_, promise] : m_pending_decoded_images) {
|
|
|
|
promise->reject(Error::from_string_literal("ImageDecoder disconnected"));
|
|
|
|
}
|
|
|
|
m_pending_decoded_images.clear();
|
2021-02-20 10:35:00 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:23:16 +00:00
|
|
|
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)
|
2023-01-15 19:50:32 +00:00
|
|
|
{
|
2024-04-19 20:23:16 +00:00
|
|
|
auto promise = Core::Promise<DecodedImage>::construct();
|
|
|
|
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;
|
|
|
|
}
|
2023-01-15 19:50:32 +00:00
|
|
|
|
|
|
|
auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
|
|
|
|
if (encoded_buffer_or_error.is_error()) {
|
2024-04-19 20:23:16 +00:00
|
|
|
dbgln("Could not allocate encoded buffer: {}", encoded_buffer_or_error.error());
|
|
|
|
promise->reject(encoded_buffer_or_error.release_error());
|
|
|
|
return promise;
|
2023-01-15 19:50:32 +00:00
|
|
|
}
|
|
|
|
auto encoded_buffer = encoded_buffer_or_error.release_value();
|
|
|
|
|
|
|
|
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
2021-02-20 10:33:34 +00:00
|
|
|
|
2024-04-19 20:23:16 +00:00
|
|
|
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;
|
2021-02-20 10:33:34 +00:00
|
|
|
}
|
2021-01-29 21:30:48 +00:00
|
|
|
|
2024-04-19 20:23:16 +00:00
|
|
|
m_pending_decoded_images.set(response->image_id(), promise);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2021-05-03 14:51:42 +00:00
|
|
|
|
2024-07-17 17:14:44 +00:00
|
|
|
void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> const& bitmaps, Vector<u32> const& durations, Gfx::FloatPoint scale)
|
2024-04-19 20:23:16 +00:00
|
|
|
{
|
|
|
|
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();
|
2021-05-14 17:40:49 +00:00
|
|
|
|
2021-01-29 21:30:48 +00:00
|
|
|
DecodedImage image;
|
2024-04-19 20:23:16 +00:00
|
|
|
image.is_animated = is_animated;
|
|
|
|
image.loop_count = loop_count;
|
|
|
|
image.scale = scale;
|
|
|
|
image.frames.ensure_capacity(bitmaps.size());
|
2023-10-01 18:56:19 +00:00
|
|
|
for (size_t i = 0; i < bitmaps.size(); ++i) {
|
2024-07-17 17:14:44 +00:00
|
|
|
if (!bitmaps[i].has_value()) {
|
2024-04-19 20:23:16 +00:00
|
|
|
dbgln("ImageDecoderClient: Invalid bitmap for request {} at index {}", image_id, i);
|
|
|
|
promise->reject(Error::from_string_literal("Invalid bitmap"));
|
|
|
|
return;
|
|
|
|
}
|
2023-10-01 18:56:19 +00:00
|
|
|
|
2024-07-17 17:14:44 +00:00
|
|
|
image.frames.empend(*bitmaps[i], durations[i]);
|
2021-01-29 21:30:48 +00:00
|
|
|
}
|
2024-04-19 20:23:16 +00:00
|
|
|
|
|
|
|
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"));
|
2020-06-22 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|