Skip to content

Commit f3b0012

Browse files
author
Jim Shargo
committed
BufferItemConsumer: Add/expose methods
Removes unneccessary flag surrounding releaseBuffer, which is flag gated everywhere else. Adds attachBuffer, which wraps IGBP's version of the same method, allowing users to attach a buffer to the consumer. Bug: 393639203 Flag: EXEMPT covered elsewhere Test: new tests Change-Id: I13283720f0f91380301d1aca180c4681c16997d7
1 parent fd4edb2 commit f3b0012

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

libs/gui/BufferItemConsumer.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//#define LOG_NDEBUG 0
1818
#define LOG_TAG "BufferItemConsumer"
1919
//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20+
#include <utils/Errors.h>
2021
#include <utils/Log.h>
2122

2223
#include <inttypes.h>
@@ -132,13 +133,36 @@ status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
132133
return OK;
133134
}
134135

136+
status_t BufferItemConsumer::attachBuffer(const sp<GraphicBuffer>& buffer) {
137+
if (!buffer) {
138+
BI_LOGE("BufferItemConsumer::attachBuffer no input buffer specified.");
139+
return BAD_VALUE;
140+
}
141+
142+
Mutex::Autolock _l(mMutex);
143+
144+
int slot = INVALID_BUFFER_SLOT;
145+
status_t status = mConsumer->attachBuffer(&slot, buffer);
146+
if (status != OK) {
147+
BI_LOGE("BufferItemConsumer::attachBuffer unable to attach buffer %d", status);
148+
return status;
149+
}
150+
151+
mSlots[slot] = {
152+
.mGraphicBuffer = buffer,
153+
.mFence = nullptr,
154+
.mFrameNumber = 0,
155+
};
156+
157+
return OK;
158+
}
159+
135160
status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
136161
const sp<Fence>& releaseFence) {
137162
Mutex::Autolock _l(mMutex);
138163
return releaseBufferSlotLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
139164
}
140165

141-
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
142166
status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer,
143167
const sp<Fence>& releaseFence) {
144168
Mutex::Autolock _l(mMutex);
@@ -154,7 +178,6 @@ status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer,
154178

155179
return releaseBufferSlotLocked(slotIndex, buffer, releaseFence);
156180
}
157-
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
158181

159182
status_t BufferItemConsumer::releaseBufferSlotLocked(int slotIndex, const sp<GraphicBuffer>& buffer,
160183
const sp<Fence>& releaseFence) {

libs/gui/include/gui/BufferItemConsumer.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ class BufferItemConsumer: public ConsumerBase
9696
status_t acquireBuffer(BufferItem* item, nsecs_t presentWhen,
9797
bool waitForFence = true);
9898

99+
// Transfer ownership of a buffer to the BufferQueue. On NO_ERROR, the buffer
100+
// is considered as if it were acquired. Buffer must not be null.
101+
//
102+
// Returns
103+
// - BAD_VALUE if buffer is null
104+
// - INVALID_OPERATION if too many buffers have already been acquired
105+
status_t attachBuffer(const sp<GraphicBuffer>& buffer);
106+
99107
// Returns an acquired buffer to the queue, allowing it to be reused. Since
100108
// only a fixed number of buffers may be acquired at a time, old buffers
101109
// must be released by calling releaseBuffer to ensure new buffers can be
@@ -105,10 +113,8 @@ class BufferItemConsumer: public ConsumerBase
105113
status_t releaseBuffer(const BufferItem &item,
106114
const sp<Fence>& releaseFence = Fence::NO_FENCE);
107115

108-
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
109116
status_t releaseBuffer(const sp<GraphicBuffer>& buffer,
110117
const sp<Fence>& releaseFence = Fence::NO_FENCE);
111-
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
112118

113119
protected:
114120
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)

libs/gui/tests/BufferItemConsumer_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,29 @@ TEST_F(BufferItemConsumerTest, ResizeAcquireCount) {
245245
EXPECT_EQ(OK, mBIC->setMaxAcquiredBufferCount(kMaxLockedBuffers - 1));
246246
}
247247

248+
TEST_F(BufferItemConsumerTest, AttachBuffer) {
249+
ASSERT_EQ(OK, mBIC->setMaxAcquiredBufferCount(1));
250+
251+
int slot;
252+
DequeueBuffer(&slot);
253+
QueueBuffer(slot);
254+
AcquireBuffer(&slot);
255+
256+
sp<GraphicBuffer> newBuffer1 = sp<GraphicBuffer>::make(kWidth, kHeight, kFormat, kUsage);
257+
sp<GraphicBuffer> newBuffer2 = sp<GraphicBuffer>::make(kWidth, kHeight, kFormat, kUsage);
258+
259+
// For some reason, you can attach an extra buffer?
260+
// b/400973991 to investigate
261+
EXPECT_EQ(OK, mBIC->attachBuffer(newBuffer1));
262+
EXPECT_EQ(INVALID_OPERATION, mBIC->attachBuffer(newBuffer2));
263+
264+
ReleaseBuffer(slot);
265+
266+
EXPECT_EQ(OK, mBIC->attachBuffer(newBuffer2));
267+
EXPECT_EQ(OK, mBIC->releaseBuffer(newBuffer1, Fence::NO_FENCE));
268+
EXPECT_EQ(OK, mBIC->releaseBuffer(newBuffer2, Fence::NO_FENCE));
269+
}
270+
248271
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
249272
// Test that delete BufferItemConsumer triggers onBufferFreed.
250273
TEST_F(BufferItemConsumerTest, DetachBufferWithBuffer) {

0 commit comments

Comments
 (0)