Skip to content

Truly independent web browser

LibMediaCodecs: Use a new slot buffer for new video frame surfaces

Zaggy1024510546678899
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.cpp b/Libraries/LibMediaCodecs/VideoFramePool.cpp
index c11e85047fc..0a319130f64 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.cpp
+++ b/Libraries/LibMediaCodecs/VideoFramePool.cpp
@@ -290,6 +290,9 @@ Optional<VideoFrameSurfacePool::AcquiredSlot> VideoFrameSurfacePool::try_acquire
};
}
+ if (held_slot_count_while_locked() >= MAX_HELD_SLOT_COUNT)
+ return {};
+
if (!slot_index.has_value()) {
Optional<u32> unused_slot_index;
Optional<u32> evictable_slot_index;
@@ -313,19 +316,16 @@ Optional<VideoFrameSurfacePool::AcquiredSlot> VideoFrameSurfacePool::try_acquire
if (!unused_slot_index.has_value())
return {};
- auto& unused_slot = m_slots[*unused_slot_index];
- if (!unused_slot.buffer.is_valid()) {
- auto buffer_result = Core::AnonymousBuffer::create_with_size(slot_data_offset());
- if (buffer_result.is_error())
- return {};
- unused_slot.buffer = buffer_result.release_value();
- new (unused_slot.buffer.data<void>()) SlotHeader;
- }
+ auto buffer_result = Core::AnonymousBuffer::create_with_size(slot_data_offset());
+ if (buffer_result.is_error())
+ return {};
if (m_slot_indices_by_surface_id.try_set(surface->id(), *unused_slot_index).is_error())
return {};
+ auto& unused_slot = m_slots[*unused_slot_index];
drop_slot_storage_while_locked(unused_slot);
- // The slot is announced again, with its new surface, as though its buffer were new.
+ unused_slot.buffer = buffer_result.release_value();
+ new (unused_slot.buffer.data<void>()) SlotHeader;
unused_slot.allocated_buffer_id++;
slot_index = unused_slot_index;
}
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.h b/Libraries/LibMediaCodecs/VideoFramePool.h
index cd4344b2008..7a998c4817d 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.h
+++ b/Libraries/LibMediaCodecs/VideoFramePool.h
@@ -120,11 +120,14 @@ private:
};
// A pool of surfaces produced by a hardware decoder. The decoder allocates and recycles them, so this pool tracks
-// their identity and lifetime rather than their memory. Its slot count is what bounds how many frames a hardware
-// decoder may have in flight, since the platform's own allocator will keep handing out surfaces indefinitely.
+// their identity and lifetime rather than their memory. Each slot stands for one surface, and the decoder recycles
+// more surfaces than it can have held at once, so the pool remembers more slots than it lets be held. The held slot
+// count is what bounds how many frames a hardware decoder may have in flight, since the platform's own allocator will
+// keep handing out surfaces indefinitely.
class MEDIACODECS_API VideoFrameSurfacePool final : public VideoFrameEntryLedger {
public:
- static constexpr u32 MAX_SLOT_COUNT = 8;
+ static constexpr u32 MAX_HELD_SLOT_COUNT = 8;
+ static constexpr u32 MAX_SLOT_COUNT = 32;
static ErrorOr<NonnullRefPtr<VideoFrameSurfacePool>> create();
@@ -153,7 +156,7 @@ private:
// adoptions, by lending a frame onward, are this process's own, so they are not released to the other process.
class MEDIACODECS_API VideoFrameRelayPool final : public VideoFrameEntryLedger {
public:
- static constexpr u32 MAX_SLOT_COUNT = 16;
+ static constexpr u32 MAX_SLOT_COUNT = max(VideoFramePool::MAX_SLOT_COUNT, VideoFrameSurfacePool::MAX_SLOT_COUNT);
// Called once for each adoption of a slot, once the slot is no longer held here at all.
using AdoptionReleasedCallback = Function<void(u32 slot_index)>;
diff --git a/Tests/LibMedia/TestVideoFramePool.cpp b/Tests/LibMedia/TestVideoFramePool.cpp
index cfc623b229e..4ece61f5660 100644
--- a/Tests/LibMedia/TestVideoFramePool.cpp
+++ b/Tests/LibMedia/TestVideoFramePool.cpp
@@ -6,6 +6,7 @@
#include <AK/ConditionVariable.h>
#include <AK/Mutex.h>
+#include <AK/Queue.h>
#include <AK/Time.h>
#include <LibCore/System.h>
#include <LibMediaCodecs/VideoFrame.h>
@@ -731,14 +732,14 @@ TEST_CASE(surface_pool_gives_distinct_surfaces_distinct_slots)
EXPECT_NE(second.index, first.index);
}
-TEST_CASE(surface_pool_refuses_to_exceed_its_slot_count)
+TEST_CASE(surface_pool_refuses_to_exceed_its_held_slot_count)
{
auto pool = MUST(Media::VideoFrameSurfacePool::create());
- // The platform allocator hands out surfaces without limit, so this pool's slot count is what stops a decoder
+ // The platform allocator hands out surfaces without limit, so this pool's held slot count is what stops a decoder
// from running arbitrarily far ahead of whoever is displaying its frames.
Vector<NonnullRefPtr<Media::VideoSurface>> surfaces;
- for (u32 index = 0; index < Media::VideoFrameSurfacePool::MAX_SLOT_COUNT; index++) {
+ for (u32 index = 0; index < Media::VideoFrameSurfacePool::MAX_HELD_SLOT_COUNT; index++) {
surfaces.append(make_surface());
EXPECT(pool->try_acquire(surfaces.last(), make_contents()).has_value());
}
@@ -746,10 +747,43 @@ TEST_CASE(surface_pool_refuses_to_exceed_its_slot_count)
auto surface_too_many = make_surface();
EXPECT(!pool->try_acquire(surface_too_many, make_contents()).has_value());
+ // A held surface handed back again takes no further slot.
+ EXPECT(pool->try_acquire(surfaces.first(), make_contents()).has_value());
+ pool->release_hold(0);
+
pool->release_hold(0);
EXPECT(pool->try_acquire(surface_too_many, make_contents()).has_value());
}
+TEST_CASE(surface_pool_remembers_more_surfaces_than_it_lets_be_held)
+{
+ auto pool = MUST(Media::VideoFrameSurfacePool::create());
+
+ // Decoders cycle through more surfaces than may be held at once, and each one should be announced only once.
+ static constexpr u32 decoder_surface_count = Media::VideoFrameSurfacePool::MAX_HELD_SLOT_COUNT * 2 + 1;
+ static_assert(decoder_surface_count <= Media::VideoFrameSurfacePool::MAX_SLOT_COUNT);
+ Vector<NonnullRefPtr<Media::VideoSurface>> surfaces;
+ for (u32 index = 0; index < decoder_surface_count; index++)
+ surfaces.append(make_surface());
+
+ Vector<Media::VideoFrameSurfacePool::AcquiredSlot> first_cycle_acquisitions;
+ Queue<u32> held_slot_indices;
+ for (u32 cycle = 0; cycle < 3; cycle++) {
+ for (u32 index = 0; index < decoder_surface_count; index++) {
+ if (held_slot_indices.size() == Media::VideoFrameSurfacePool::MAX_HELD_SLOT_COUNT)
+ pool->release_hold(held_slot_indices.dequeue());
+ auto acquired = pool->try_acquire(surfaces[index], make_contents()).value();
+ held_slot_indices.enqueue(acquired.index);
+ if (cycle == 0) {
+ first_cycle_acquisitions.append(acquired);
+ continue;
+ }
+ EXPECT_EQ(acquired.index, first_cycle_acquisitions[index].index);
+ EXPECT_EQ(acquired.allocated_buffer_id, first_cycle_acquisitions[index].allocated_buffer_id);
+ }
+ }
+}
+
TEST_CASE(surface_pool_evicts_the_least_recently_acquired_identity_when_full)
{
auto pool = MUST(Media::VideoFrameSurfacePool::create());
@@ -759,9 +793,8 @@ TEST_CASE(surface_pool_evicts_the_least_recently_acquired_identity_when_full)
for (u32 index = 0; index < Media::VideoFrameSurfacePool::MAX_SLOT_COUNT; index++) {
surfaces.append(make_surface());
acquisitions.append(pool->try_acquire(surfaces.last(), make_contents()).value());
+ pool->release_hold(acquisitions.last().index);
}
- for (auto const& acquisition : acquisitions)
- pool->release_hold(acquisition.index);
// Every slot remembers a surface, so a surface the pool has never seen has to displace one of them.
auto new_surface = make_surface();
@@ -827,6 +860,44 @@ TEST_CASE(directory_resolves_an_announced_surface_slot)
EXPECT(!frame->yuv_data().has_value());
}
+TEST_CASE(directory_waits_for_a_slot_to_be_announced_with_its_new_surface)
+{
+ auto pool = MUST(Media::VideoFrameSurfacePool::create());
+ auto directory = Media::VideoFrameSlotDirectory::create();
+
+ Vector<NonnullRefPtr<Media::VideoSurface>> surfaces;
+ Vector<Media::VideoFrameSurfacePool::AcquiredSlot> acquisitions;
+ for (u32 index = 0; index < Media::VideoFrameSurfacePool::MAX_SLOT_COUNT; index++) {
+ surfaces.append(make_surface());
+ acquisitions.append(pool->try_acquire(surfaces.last(), make_contents()).value());
+ directory->notify_slot_announced(pool->id(), acquisitions.last().index, pool->slot_buffer(acquisitions.last().index), pool->slot_surface(acquisitions.last().index));
+ pool->release_hold(acquisitions.last().index);
+ }
+
+ // The new surface displaces the first one from its slot, but the directory has yet to hear of that.
+ auto new_surface = make_surface();
+ auto acquired = pool->try_acquire(new_surface, make_contents()).value();
+ EXPECT_EQ(acquired.index, acquisitions.first().index);
+
+ Media::VideoFrameHandle handle {
+ .pool_id = pool->id(),
+ .slot_index = acquired.index,
+ .slot_acquisition_id = acquired.slot_acquisition_id,
+ .timestamp = {},
+ .duration = {},
+ .size = { 16, 16 },
+ .bit_depth = 8,
+ .subsampling = Media::Subsampling(true, true),
+ .cicp = {},
+ };
+ EXPECT_EQ(directory->resolve_frame(handle, [] { }), nullptr);
+
+ directory->notify_slot_announced(pool->id(), acquired.index, pool->slot_buffer(acquired.index), pool->slot_surface(acquired.index));
+ auto frame = directory->resolve_frame(handle, [] { });
+ EXPECT_NE(frame, nullptr);
+ EXPECT_EQ(frame->surface()->id(), new_surface->id());
+}
+
TEST_CASE(surface_slots_hold_and_free_through_the_shared_ledger)
{
auto freed_count = 0u;
diff --git a/Tests/LibMedia/TestVideoToolboxDecode.cpp b/Tests/LibMedia/TestVideoToolboxDecode.cpp
index 0fd9eb2e6f2..cf4b8693d21 100644
--- a/Tests/LibMedia/TestVideoToolboxDecode.cpp
+++ b/Tests/LibMedia/TestVideoToolboxDecode.cpp
@@ -126,7 +126,7 @@ TEST_CASE(surfaces_are_not_reused_while_their_frames_are_held)
// one in use. Were that claim missing, these frames would be a handful of surfaces overwriting one another.
Vector<NonnullRefPtr<Media::VideoFrame>> held_frames;
HashTable<u32> held_surface_ids;
- while (held_frames.size() < Media::VideoFrameSurfacePool::MAX_SLOT_COUNT) {
+ while (held_frames.size() < Media::VideoFrameSurfacePool::MAX_HELD_SLOT_COUNT) {
auto frame_result = decoding.next_frame();
if (frame_result.is_error() && decoding_is_unavailable(frame_result.error()))
return;
@@ -177,7 +177,7 @@ TEST_CASE(storage_runs_out_while_every_frame_is_held)
auto decoding = open_decoding("./vp9_in_webm.webm"sv, Media::CodecID::VP9);
Vector<NonnullRefPtr<Media::VideoFrame>> held_frames;
- while (held_frames.size() < Media::VideoFrameSurfacePool::MAX_SLOT_COUNT) {
+ while (held_frames.size() < Media::VideoFrameSurfacePool::MAX_HELD_SLOT_COUNT) {
auto frame_result = decoding.next_frame();
if (frame_result.is_error() && decoding_is_unavailable(frame_result.error()))
return;