Skip to content

Truly independent web browser

ImageDecoder: Keep animation sessions with the clients that opened them

Andreas Klingb60ef579fa1a
diff --git a/Services/ImageDecoder/ConnectionFromClient.cpp b/Services/ImageDecoder/ConnectionFromClient.cpp
index e9bca246a37..3d52c91468f 100644
--- a/Services/ImageDecoder/ConnectionFromClient.cpp
+++ b/Services/ImageDecoder/ConnectionFromClient.cpp
@@ -22,14 +22,14 @@ namespace ImageDecoder {
static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
static IDAllocator s_client_ids;
-// NB: The process decodes for one client and whoever that client connects, so they all share its sessions.
-static HashMap<i64, NonnullRefPtr<ConnectionFromClient::AnimationSession>> s_animation_sessions;
static i64 s_next_session_id { 1 };
-ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<IPC::Transport> transport)
+ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<IPC::Transport> transport, RefPtr<ClientGroup> group)
: IPC::ConnectionFromClient<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(transport), s_client_ids.allocate())
+ , m_group(group ? group.release_nonnull() : make_ref_counted<ClientGroup>())
{
s_connections.set(client_id(), *this);
+ m_group->clients.set(this);
}
void ConnectionFromClient::die()
@@ -42,6 +42,10 @@ void ConnectionFromClient::die()
job->cancel();
m_pending_frame_jobs.clear();
+ m_group->clients.remove(this);
+ if (m_group->clients.is_empty())
+ m_group->animation_sessions.clear();
+
auto client_id = this->client_id();
s_connections.remove(client_id);
s_client_ids.deallocate(client_id);
@@ -166,7 +170,7 @@ NonnullRefPtr<ConnectionFromClient::PendingJob> ConnectionFromClient::start_deco
session->encoded_data = move(result_value.encoded_data);
session->decoder = move(result_value.decoder);
session->frame_count = result_value.frame_count;
- s_animation_sessions.set(session_id, move(session));
+ strong_this->m_group->animation_sessions.set(session_id, move(session));
}
strong_this->async_did_decode_image(request_id, result_value.is_animated, result_value.loop_count, move(result_value.bitmaps), move(result_value.durations), result_value.scale, move(result_value.color_profile), session_id);
@@ -230,8 +234,8 @@ void ConnectionFromClient::cancel_decoding(i64 request_id)
void ConnectionFromClient::request_animation_frames(i64 session_id, u32 start_frame_index, u32 count)
{
- auto it = s_animation_sessions.find(session_id);
- if (it == s_animation_sessions.end())
+ auto it = m_group->animation_sessions.find(session_id);
+ if (it == m_group->animation_sessions.end())
return;
auto session = it->value;
@@ -314,11 +318,11 @@ NonnullRefPtr<ConnectionFromClient::PendingJob> ConnectionFromClient::start_fram
void ConnectionFromClient::stop_animation_decode(i64 session_id)
{
// NB: The job belongs to whichever of the clients sharing the session asked for the frames.
- for (auto& [_, connection] : s_connections) {
- if (auto job = connection->m_pending_frame_jobs.take(session_id); job.has_value())
+ for (auto* client : m_group->clients) {
+ if (auto job = client->m_pending_frame_jobs.take(session_id); job.has_value())
job.value()->cancel();
}
- s_animation_sessions.remove(session_id);
+ m_group->animation_sessions.remove(session_id);
}
void ConnectionFromClient::connect_frame_client(IPC::TransportHandle handle)
@@ -329,7 +333,7 @@ void ConnectionFromClient::connect_frame_client(IPC::TransportHandle handle)
return;
}
// NB: The new connection keeps itself alive in s_connections until it dies.
- (void)ConnectionFromClient::construct(transport_or_error.release_value());
+ (void)ConnectionFromClient::construct(transport_or_error.release_value(), m_group);
}
}
diff --git a/Services/ImageDecoder/ConnectionFromClient.h b/Services/ImageDecoder/ConnectionFromClient.h
index 9d1f3a9f96c..e64c830c47d 100644
--- a/Services/ImageDecoder/ConnectionFromClient.h
+++ b/Services/ImageDecoder/ConnectionFromClient.h
@@ -9,7 +9,9 @@
#include <AK/Atomic.h>
#include <AK/AtomicRefCounted.h>
#include <AK/HashMap.h>
+#include <AK/HashTable.h>
#include <AK/Mutex.h>
+#include <AK/RefCounted.h>
#include <ImageDecoder/Forward.h>
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
@@ -51,6 +53,13 @@ public:
Mutex decoder_mutex;
};
+ // A client and the further clients it connects, which share its animation sessions. Nothing here reaches the
+ // clients of other groups, and the sessions go once the last client of the group is gone.
+ struct ClientGroup : public RefCounted<ClientGroup> {
+ HashTable<ConnectionFromClient*> clients;
+ HashMap<i64, NonnullRefPtr<AnimationSession>> animation_sessions;
+ };
+
private:
struct PendingJob : public AtomicRefCounted<PendingJob> {
void cancel() { m_canceled.store(true, AK::MemoryOrder::memory_order_relaxed); }
@@ -62,7 +71,7 @@ private:
using FrameDecodeResult = Vector<Gfx::ImageFrameDescriptor>;
- explicit ConnectionFromClient(NonnullOwnPtr<IPC::Transport>);
+ explicit ConnectionFromClient(NonnullOwnPtr<IPC::Transport>, RefPtr<ClientGroup> = nullptr);
virtual void decode_image(Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type, i64 request_id) override;
virtual Messages::ImageDecoderServer::DecodeFirstFrameSynchronouslyResponse decode_first_frame_synchronously(Core::AnonymousBuffer, Optional<ByteString> mime_type) override;
@@ -74,6 +83,7 @@ private:
NonnullRefPtr<PendingJob> start_decode_image_job(i64 request_id, Core::AnonymousBuffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type);
NonnullRefPtr<PendingJob> start_frame_decode_job(i64 session_id, NonnullRefPtr<AnimationSession>, u32 start_frame_index, u32 end_index);
+ NonnullRefPtr<ClientGroup> m_group;
HashMap<i64, NonnullRefPtr<PendingJob>> m_pending_jobs;
HashMap<i64, NonnullRefPtr<PendingJob>> m_pending_frame_jobs;
};