libgui: Fix attaching buffers without allocation
This changes the way that BufferQueue selects slots in
waitForFreeSlotThenRelock. This method is called from both
dequeueBuffer and attachBuffer, but those two methods actually have
different preferences:
dequeueBuffer wants a slot with a buffer if possible (to avoid
unnecessary allocations), but will settle for a slot without a buffer
if no free buffers are available.
attachBuffer wants a slot without a buffer if possible (to avoid
clobbering an existing buffer), but will settle with clobbering a free
buffer if no empty slots are available.
These preferences are now respected, which has the side-effect of
fixing a bug where it was not possible to attach a buffer if allocation
is disabled (since the previous implementation assumed finding a slot
without a buffer meant that the caller intended to allocate a buffer,
which would accordingly be blocked since allocation is disabled).
Bug: 26387372
Change-Id: Iefd550fd01925d8c51d6f062d5708d1f6d517edd
diff --git a/include/gui/BufferQueueProducer.h b/include/gui/BufferQueueProducer.h
index 835593f..645a07b 100644
--- a/include/gui/BufferQueueProducer.h
+++ b/include/gui/BufferQueueProducer.h
@@ -183,12 +183,24 @@
// This is required by the IBinder::DeathRecipient interface
virtual void binderDied(const wp<IBinder>& who);
+ // Returns the slot of the next free buffer if one is available or
+ // BufferQueueCore::INVALID_BUFFER_SLOT otherwise
+ int getFreeBufferLocked() const;
+
+ // Returns the next free slot if one less than or equal to maxBufferCount
+ // is available or BufferQueueCore::INVALID_BUFFER_SLOT otherwise
+ int getFreeSlotLocked(int maxBufferCount) const;
+
// waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may
// block if there are no available slots and we are not in non-blocking
// mode (producer and consumer controlled by the application). If it blocks,
// it will release mCore->mMutex while blocked so that other operations on
// the BufferQueue may succeed.
- status_t waitForFreeSlotThenRelock(const char* caller, int* found,
+ enum class FreeSlotCaller {
+ Dequeue,
+ Attach,
+ };
+ status_t waitForFreeSlotThenRelock(FreeSlotCaller caller, int* found,
status_t* returnFlags) const;
sp<BufferQueueCore> mCore;