Truly independent web browser
Compositor: Limit the video frames kept past presentation for holds
Zaggy1024002ca8b7b111
diff --git a/Services/Compositor/VideoFrameHolds.cpp b/Services/Compositor/VideoFrameHolds.cppindex 7392db5a476..373174eee21 100644--- a/Services/Compositor/VideoFrameHolds.cpp+++ b/Services/Compositor/VideoFrameHolds.cpp@@ -81,7 +81,16 @@ void VideoFrameHolds::retire_current_generation(Media::VideoSinkHandle handle, Sif (replaced.hold_count == 0)return;auto& retained_frames = m_retained_frames.ensure(handle);- retained_frames.set(sink.generation, RetainedFrame { .frame = sink.frame, .hold_count = replaced.hold_count });+ if (auto previous = retained_frames.get(sink.generation); previous.has_value() && previous.value().frame)+ m_retained_frame_count--;+ auto frame = sink.frame;+ if (frame && m_retained_frame_count >= MAX_RETAINED_FRAME_COUNT) {+ dbgln("Compositor: Not keeping the frame of video sink {} held past its presentation, since {} already are", handle.value(), m_retained_frame_count);+ frame = nullptr;+ }+ if (frame)+ m_retained_frame_count++;+ retained_frames.set(sink.generation, RetainedFrame { .frame = move(frame), .hold_count = replaced.hold_count });}ErrorOr<void> VideoFrameHolds::release_hold(Media::VideoSinkHandle handle, u64 generation)@@ -100,6 +109,8 @@ ErrorOr<void> VideoFrameHolds::release_hold(Media::VideoSinkHandle handle, u64 greturn Error::from_string_literal("The video frame was not retained");if (--retained_frame->hold_count > 0)return {};+ if (retained_frame->frame)+ m_retained_frame_count--;retained_frames->remove(generation);if (retained_frames->is_empty())m_retained_frames.remove(handle);diff --git a/Services/Compositor/VideoFrameHolds.h b/Services/Compositor/VideoFrameHolds.hindex 6753fc408e8..92492dd00e3 100644--- a/Services/Compositor/VideoFrameHolds.h+++ b/Services/Compositor/VideoFrameHolds.h@@ -19,6 +19,9 @@ namespace Compositor {class VideoFrameHolds {public:+ // A hold past this many retained frames keeps no frame.+ static constexpr size_t MAX_RETAINED_FRAME_COUNT = 64;+void add_sink(Media::VideoSinkHandle, NonnullRefPtr<Compositing::VideoFramePresentationPage>);void remove_sink(Media::VideoSinkHandle);@@ -44,6 +47,7 @@ private:HashMap<Media::VideoSinkHandle, Sink> m_sinks;HashMap<Media::VideoSinkHandle, HashMap<u64, RetainedFrame>> m_retained_frames;+ size_t m_retained_frame_count { 0 };};}diff --git a/Tests/Compositor/CMakeLists.txt b/Tests/Compositor/CMakeLists.txtindex 52cfd5c5e2b..83bf0ab252f 100644--- a/Tests/Compositor/CMakeLists.txt+++ b/Tests/Compositor/CMakeLists.txt@@ -4,6 +4,7 @@ set(TEST_SOURCESTestCanvasSurfaceRegistry.cppTestContextState.cppTestScrollbarController.cpp+ TestVideoFrameHolds.cppTestVSyncScheduler.cpp)diff --git a/Tests/Compositor/TestVideoFrameHolds.cpp b/Tests/Compositor/TestVideoFrameHolds.cppnew file mode 100644index 00000000000..d9639511f9d--- /dev/null+++ b/Tests/Compositor/TestVideoFrameHolds.cpp@@ -0,0 +1,59 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <Compositor/VideoFrameHolds.h>+#include <LibCompositing/Video/VideoFramePresentationPage.h>+#include <LibMediaCodecs/VideoFrame.h>+#include <LibMediaCodecs/VideoFramePool.h>+#include <LibTest/TestCase.h>++using Compositing::VideoFramePresentationPage;++static NonnullRefPtr<Media::VideoFrame> create_pooled_frame(Media::VideoFramePool& pool)+{+ static constexpr Gfx::IntSize frame_size { 2, 2 };+ auto subsampling = Media::Subsampling::yuv420();+ auto layout = MUST(Media::frame_plane_layout(frame_size, 8, subsampling));+ auto acquired = pool.try_acquire(layout.total_byte_count).release_value();+ auto slot = MUST(pool.try_adopt_acquired_slot(acquired));+ return make_ref_counted<Media::VideoFrame>(AK::Duration::zero(), AK::Duration::from_milliseconds(33), Gfx::Size<u32>(frame_size), 8, subsampling, Media::CodingIndependentCodePoints {}, move(slot));+}++// Every frame kept for a hold keeps its buffer open in the Compositor, so a WebContent holding one frame after another+// must not make it keep them all.+TEST_CASE(holds_keep_only_so_many_frames_past_their_presentation)+{+ auto web_content_page = MUST(VideoFramePresentationPage::create());+ auto compositor_page = MUST(VideoFramePresentationPage::create(web_content_page->buffer()));+ Media::VideoSinkHandle handle { 1 };++ Compositor::VideoFrameHolds holds;+ holds.add_sink(handle, compositor_page);++ auto pool = MUST(Media::VideoFramePool::create());+ Array frames { create_pooled_frame(*pool), create_pooled_frame(*pool) };++ // Each frame is held while it is presented, then kept for its hold once the next one replaces it.+ Vector<u64> held_generations;+ for (size_t i = 0; i <= Compositor::VideoFrameHolds::MAX_RETAINED_FRAME_COUNT + 1; i++) {+ holds.present_frame(handle, frames[i % frames.size()], {});+ if (i > Compositor::VideoFrameHolds::MAX_RETAINED_FRAME_COUNT)+ break;+ held_generations.append(web_content_page->hold_current_generation().value());+ }++ EXPECT(holds.held_frame(handle, held_generations.first()) != nullptr);+ EXPECT(holds.held_frame(handle, held_generations[Compositor::VideoFrameHolds::MAX_RETAINED_FRAME_COUNT - 1]) != nullptr);+ EXPECT(holds.held_frame(handle, held_generations.last()) == nullptr);++ // The hold that kept no frame is still released as any other, and a released frame makes room for another.+ EXPECT(!holds.release_hold(handle, held_generations.last()).is_error());+ EXPECT(!holds.release_hold(handle, held_generations.first()).is_error());+ holds.present_frame(handle, frames[0], {});+ auto generation = web_content_page->hold_current_generation().value();+ holds.present_frame(handle, frames[1], {});+ EXPECT(holds.held_frame(handle, generation) != nullptr);+}