Files
ladybird/Services/ImageDecoder/ConnectionFromClient.h
Andreas Kling a4f126554d ImageDecoder: Add streaming animation decode sessions
Add IPC messages and server-side implementation for streaming
animated image decode. Instead of decoding all frames upfront,
only decode an initial batch and keep the decoder alive for
on-demand frame requests.

New IPC messages:
- request_animation_frames: request decode of a batch of frames
- stop_animation_decode: clean up a decode session
- did_decode_animation_frames: deliver decoded frames to client
- did_fail_animation_decode: report decode errors

The existing did_decode_image message gains a session_id parameter
(0 for single-shot decode, non-zero for streaming sessions).
2026-02-13 18:34:24 +01:00

77 lines
2.7 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <ImageDecoder/Forward.h>
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibGfx/BitmapSequence.h>
#include <LibGfx/ColorSpace.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <LibIPC/ConnectionFromClient.h>
#include <LibThreading/BackgroundAction.h>
namespace ImageDecoder {
class ConnectionFromClient final
: public IPC::ConnectionFromClient<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint> {
C_OBJECT(ConnectionFromClient);
public:
~ConnectionFromClient() override = default;
virtual void die() override;
struct DecodeResult {
bool is_animated = false;
u32 loop_count = 0;
u32 frame_count = 0;
Gfx::FloatPoint scale { 1, 1 };
Gfx::BitmapSequence bitmaps;
Vector<u32> durations;
Gfx::ColorSpace color_profile;
// Non-null for streaming animated sessions:
RefPtr<Gfx::ImageDecoder> decoder;
Core::AnonymousBuffer encoded_data;
};
struct AnimationSession {
Core::AnonymousBuffer encoded_data;
RefPtr<Gfx::ImageDecoder> decoder;
u32 frame_count { 0 };
};
private:
using Job = Threading::BackgroundAction<DecodeResult>;
using FrameDecodeResult = Vector<Gfx::ImageFrameDescriptor>;
using FrameDecodeJob = Threading::BackgroundAction<FrameDecodeResult>;
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 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;
virtual Messages::ImageDecoderServer::InitTransportResponse init_transport(int peer_pid) override;
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);
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;
HashMap<i64, NonnullRefPtr<FrameDecodeJob>> m_pending_frame_jobs;
};
}