Skip to content

Truly independent web browser

LibMediaCodecs: Keep the liveness floor of frame buffers allocated

Zaggy1024f8bea8af4caa
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.cpp b/Libraries/LibMediaCodecs/VideoFramePool.cpp
index 59cf2e69c7e..26eff27c062 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.cpp
+++ b/Libraries/LibMediaCodecs/VideoFramePool.cpp
@@ -71,7 +71,7 @@ Optional<VideoFramePool::AcquiredSlot> VideoFramePool::try_acquire(size_t byte_c
auto const& slot = m_slots[index];
if (slot.is_held)
continue;
- if (!free_slot_index.has_value())
+ if (!free_slot_index.has_value() || slot.buffer.size() > m_slots[free_slot_index.value()].buffer.size())
free_slot_index = index;
if (slot.buffer.is_valid() && slot.buffer.size() == byte_count) {
reusable_slot_index = index;
@@ -83,7 +83,7 @@ Optional<VideoFramePool::AcquiredSlot> VideoFramePool::try_acquire(size_t byte_c
if (reusable_slot_index.has_value()) {
chosen_slot = &m_slots[*reusable_slot_index];
} else {
- // Reallocate a free slot to the requested capacity, growing the pool if every slot is held.
+ // Reallocate the largest free slot to the requested capacity, growing the pool if every slot is held.
if (!free_slot_index.has_value()) {
if (m_slots.size() >= MAX_SLOT_COUNT)
return {};
@@ -133,6 +133,14 @@ u32 VideoFramePool::held_slot_count_while_locked() const
return held_count;
}
+u32 VideoFramePool::allocated_slot_count_while_locked() const
+{
+ u32 allocated_count = 0;
+ for (auto const& slot : m_slots)
+ allocated_count += slot.buffer.is_valid();
+ return allocated_count;
+}
+
void VideoFramePool::drop_slot_storage_while_locked(Slot& slot)
{
if (!slot.buffer.is_valid())
@@ -143,7 +151,7 @@ void VideoFramePool::drop_slot_storage_while_locked(Slot& slot)
void VideoFramePool::free_excess_buffers_while_locked()
{
- while (m_allocated_bytes > m_byte_budget) {
+ while (m_allocated_bytes > m_byte_budget && allocated_slot_count_while_locked() > MIN_SLOT_COUNT) {
Slot* largest_free_slot = nullptr;
for (auto& slot : m_slots) {
if (slot.is_held || !slot.buffer.is_valid())
diff --git a/Libraries/LibMediaCodecs/VideoFramePool.h b/Libraries/LibMediaCodecs/VideoFramePool.h
index 92a7799e658..22d575c7dcd 100644
--- a/Libraries/LibMediaCodecs/VideoFramePool.h
+++ b/Libraries/LibMediaCodecs/VideoFramePool.h
@@ -43,7 +43,7 @@ private:
};
// A pool of shared-memory framebuffers, allocated on demand within a byte budget that the first MIN_SLOT_COUNT
-// held slots may exceed.
+// slots may exceed.
class MEDIACODECS_API VideoFramePool final : public AtomicRefCounted<VideoFramePool> {
public:
static constexpr u32 MIN_SLOT_COUNT = 4;
@@ -80,6 +80,7 @@ private:
void release_slot(u32 slot_index);
u32 held_slot_count_while_locked() const;
+ u32 allocated_slot_count_while_locked() const;
void drop_slot_storage_while_locked(Slot&);
void free_excess_buffers_while_locked();
diff --git a/Tests/LibMedia/TestVideoFramePool.cpp b/Tests/LibMedia/TestVideoFramePool.cpp
index 9c45b87e2dc..f5fa8b9256d 100644
--- a/Tests/LibMedia/TestVideoFramePool.cpp
+++ b/Tests/LibMedia/TestVideoFramePool.cpp
@@ -144,19 +144,49 @@ TEST_CASE(budget_always_allows_the_liveness_floor)
TEST_CASE(excess_free_buffers_are_dropped_to_fit_the_budget)
{
auto slot_buffer_size = measured_slot_buffer_size(FRAME_BYTE_COUNT);
- auto pool = make_pool(nullptr, slot_buffer_size * Media::VideoFramePool::MIN_SLOT_COUNT);
+ auto byte_budget = slot_buffer_size * (Media::VideoFramePool::MIN_SLOT_COUNT + 1);
+ auto pool = make_pool(nullptr, byte_budget);
// Fill the budget with free buffers.
+ {
+ Vector<Media::VideoFramePool::AcquiredSlot> slots;
+ for (u32 i = 0; i < Media::VideoFramePool::MIN_SLOT_COUNT + 1; i++)
+ slots.append(pool->try_acquire(FRAME_BYTE_COUNT).release_value());
+ }
+ EXPECT_EQ(pool->allocated_byte_count(), byte_budget);
+
+ // A larger allocation replaces one free buffer and sheds another to return to the budget.
+ auto larger_byte_count = slot_buffer_size * 2;
+ auto larger = pool->try_acquire(larger_byte_count).release_value();
+ EXPECT_EQ(pool->allocated_byte_count(), measured_slot_buffer_size(larger_byte_count) + (slot_buffer_size * (Media::VideoFramePool::MIN_SLOT_COUNT - 1)));
+}
+
+TEST_CASE(the_liveness_floor_stays_allocated_past_the_budget)
+{
+ auto slot_buffer_size = measured_slot_buffer_size(FRAME_BYTE_COUNT);
+ auto pool = make_pool(nullptr, 0);
{
Vector<Media::VideoFramePool::AcquiredSlot> slots;
for (u32 i = 0; i < Media::VideoFramePool::MIN_SLOT_COUNT; i++)
slots.append(pool->try_acquire(FRAME_BYTE_COUNT).release_value());
}
+
+ (void)pool->try_acquire(FRAME_BYTE_COUNT).release_value();
EXPECT_EQ(pool->allocated_byte_count(), slot_buffer_size * Media::VideoFramePool::MIN_SLOT_COUNT);
+}
- // A larger allocation replaces one free buffer and sheds others to return to the budget.
- auto larger = pool->try_acquire(FRAME_BYTE_COUNT * 2).release_value();
- EXPECT(pool->allocated_byte_count() <= slot_buffer_size * Media::VideoFramePool::MIN_SLOT_COUNT);
+TEST_CASE(reallocation_replaces_the_largest_free_buffer)
+{
+ auto slot_buffer_size = measured_slot_buffer_size(FRAME_BYTE_COUNT);
+ auto pool = make_pool();
+ {
+ auto smaller = pool->try_acquire(slot_buffer_size).release_value();
+ auto larger = pool->try_acquire(slot_buffer_size * 4).release_value();
+ }
+
+ auto middle_byte_count = slot_buffer_size * 2;
+ (void)pool->try_acquire(middle_byte_count).release_value();
+ EXPECT_EQ(pool->allocated_byte_count(), slot_buffer_size + measured_slot_buffer_size(middle_byte_count));
}
TEST_CASE(frame_layouts_beyond_the_size_limits_are_refused)