blob: b872541fec2c65f9563a51424dc94a5483652c10 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070038#include <gui/TraceUtils.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080039#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080040
41#include <utils/Log.h>
42#include <utils/Trace.h>
43
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070044#include <system/window.h>
45
Dan Stoza289ade12014-02-28 11:17:17 -080046namespace android {
47
Iris Chang430193f2019-12-04 16:25:46 +080048// Macros for include BufferQueueCore information in log messages
49#define BQ_LOGV(x, ...) \
50 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
51 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
52 ##__VA_ARGS__)
53#define BQ_LOGD(x, ...) \
54 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
55 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
56 ##__VA_ARGS__)
57#define BQ_LOGI(x, ...) \
58 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
59 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
60 ##__VA_ARGS__)
61#define BQ_LOGW(x, ...) \
62 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
63 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
64 ##__VA_ARGS__)
65#define BQ_LOGE(x, ...) \
66 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
67 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
68 ##__VA_ARGS__)
69
Craig Donner6ebc46a2016-10-21 15:23:44 -070070static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080071ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070072
Irvel468051e2016-06-13 16:44:44 -070073BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
74 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080075 mCore(core),
76 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070077 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070078 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070079 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080080 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070081 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080082 mCallbackMutex(),
83 mNextCallbackTicket(0),
84 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070085 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010086 mDequeueTimeout(-1),
87 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080088
89BufferQueueProducer::~BufferQueueProducer() {}
90
91status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
92 ATRACE_CALL();
93 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020094 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080095
96 if (mCore->mIsAbandoned) {
97 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
98 return NO_INIT;
99 }
100
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700101 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
102 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
103 return NO_INIT;
104 }
105
Dan Stoza3e96f192014-03-03 10:16:19 -0800106 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800107 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800108 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800109 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700110 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800111 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800113 return BAD_VALUE;
114 }
115
116 mSlots[slot].mRequestBufferCalled = true;
117 *buf = mSlots[slot].mGraphicBuffer;
118 return NO_ERROR;
119}
120
Pablo Ceballosfa455352015-08-12 17:47:47 -0700121status_t BufferQueueProducer::setMaxDequeuedBufferCount(
122 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700123 int maxBufferCount;
124 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
125}
126
127status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
128 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700129 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
131 maxDequeuedBuffers);
132
Pablo Ceballos981066c2016-02-18 12:54:37 -0800133 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700134 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200135 std::unique_lock<std::mutex> lock(mCore->mMutex);
136 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137
138 if (mCore->mIsAbandoned) {
139 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
140 "abandoned");
141 return NO_INIT;
142 }
143
Brian Lindahlc794b692023-01-31 15:42:47 -0700144 *maxBufferCount = mCore->getMaxBufferCountLocked();
145
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700146 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
147 return NO_ERROR;
148 }
149
Pablo Ceballos72daab62015-12-07 16:38:43 -0800150 // The new maxDequeuedBuffer count should not be violated by the number
151 // of currently dequeued buffers
152 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800153 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700154 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700156 }
157 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 if (dequeuedCount > maxDequeuedBuffers) {
159 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
160 "count (%d) exceeds the current dequeued buffer count (%d)",
161 maxDequeuedBuffers, dequeuedCount);
162 return BAD_VALUE;
163 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700164
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700165 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 bufferCount += maxDequeuedBuffers;
167
168 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
169 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
170 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
171 return BAD_VALUE;
172 }
173
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700174 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700175 if (bufferCount < minBufferSlots) {
176 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
177 "less than minimum %d", bufferCount, minBufferSlots);
178 return BAD_VALUE;
179 }
180
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700181 if (bufferCount > mCore->mMaxBufferCount) {
182 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700183 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
184 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
185 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
186 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 return BAD_VALUE;
188 }
189
Pablo Ceballos72daab62015-12-07 16:38:43 -0800190 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800191 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800192 return BAD_VALUE;
193 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700194 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700195 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800196 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800197 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800198 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800199 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200200 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700201 } // Autolock scope
202
203 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700204 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800205 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700206 }
207
208 return NO_ERROR;
209}
210
211status_t BufferQueueProducer::setAsyncMode(bool async) {
212 ATRACE_CALL();
213 BQ_LOGV("setAsyncMode: async = %d", async);
214
Pablo Ceballos981066c2016-02-18 12:54:37 -0800215 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200217 std::unique_lock<std::mutex> lock(mCore->mMutex);
218 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700219
220 if (mCore->mIsAbandoned) {
221 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
222 return NO_INIT;
223 }
224
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700225 if (async == mCore->mAsyncMode) {
226 return NO_ERROR;
227 }
228
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700229 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700230 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
231 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700232 BQ_LOGE("setAsyncMode(%d): this call would cause the "
233 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700234 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
235 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
236 mCore->mMaxDequeuedBufferCount,
237 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700238 return BAD_VALUE;
239 }
240
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241 int delta = mCore->getMaxBufferCountLocked(async,
242 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
243 - mCore->getMaxBufferCountLocked();
244
Pablo Ceballos981066c2016-02-18 12:54:37 -0800245 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
247 "available slots. Delta = %d", delta);
248 return BAD_VALUE;
249 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700250 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800251 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200252 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700253 if (delta < 0) {
254 listener = mCore->mConsumerListener;
255 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700256 } // Autolock scope
257
258 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700259 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800260 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700261 }
262 return NO_ERROR;
263}
264
Dan Stoza5ecfb682016-01-04 17:01:02 -0800265int BufferQueueProducer::getFreeBufferLocked() const {
266 if (mCore->mFreeBuffers.empty()) {
267 return BufferQueueCore::INVALID_BUFFER_SLOT;
268 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800269 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270 mCore->mFreeBuffers.pop_front();
271 return slot;
272}
273
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800274int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800275 if (mCore->mFreeSlots.empty()) {
276 return BufferQueueCore::INVALID_BUFFER_SLOT;
277 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800278 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800280 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800281}
282
283status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200284 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800285 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
286 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800287 bool tryAgain = true;
288 while (tryAgain) {
289 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800291 return NO_INIT;
292 }
293
Dan Stoza9f3053d2014-03-06 15:14:33 -0800294 int dequeuedCount = 0;
295 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800296 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700297 if (mSlots[s].mBufferState.isDequeued()) {
298 ++dequeuedCount;
299 }
300 if (mSlots[s].mBufferState.isAcquired()) {
301 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800302 }
303 }
304
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700305 // Producers are not allowed to dequeue more than
306 // mMaxDequeuedBufferCount buffers.
307 // This check is only done if a buffer has already been queued
308 if (mCore->mBufferHasBeenQueued &&
309 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700310 // Supress error logs when timeout is non-negative.
311 if (mDequeueTimeout < 0) {
312 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
313 "count (%d)", callerString,
314 mCore->mMaxDequeuedBufferCount);
315 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 return INVALID_OPERATION;
317 }
318
Dan Stoza0de7ea72015-04-23 13:20:51 -0700319 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
320
Dan Stozaae3c3682014-04-18 15:43:35 -0700321 // If we disconnect and reconnect quickly, we can be in a state where
322 // our slots are empty but we have many buffers in the queue. This can
323 // cause us to run out of memory if we outrun the consumer. Wait here if
324 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800325 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700326 bool tooManyBuffers = mCore->mQueue.size()
327 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700328 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800329 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700330 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700331 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700332 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700333 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700335 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700336 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800337 } else {
338 if (caller == FreeSlotCaller::Dequeue) {
339 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800340 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800341 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
342 *found = slot;
343 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800344 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800345 }
346 } else {
347 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800348 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800349 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
350 *found = slot;
351 } else {
352 *found = getFreeBufferLocked();
353 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700354 }
355 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700356 }
357
358 // If no buffer is found, or if the queue has too many buffers
359 // outstanding, wait for a buffer to be acquired or released, or for the
360 // max buffer count to change.
361 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
362 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800363 if (tryAgain) {
364 // Return an error if we're in non-blocking mode (producer and
365 // consumer are controlled by the application).
366 // However, the consumer is allowed to briefly acquire an extra
367 // buffer (which could cause us to have to wait here), which is
368 // okay, since it is only used to implement an atomic acquire +
369 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700370 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
372 return WOULD_BLOCK;
373 }
Dan Stoza127fc632015-06-30 13:43:32 -0700374 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200375 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
376 std::chrono::nanoseconds(mDequeueTimeout));
377 if (result == std::cv_status::timeout) {
378 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700379 }
380 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200381 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700382 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383 }
384 } // while (tryAgain)
385
386 return NO_ERROR;
387}
388
Ian Elliottd11b0442017-07-18 11:05:49 -0600389status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
390 uint32_t width, uint32_t height, PixelFormat format,
391 uint64_t usage, uint64_t* outBufferAge,
392 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800393 ATRACE_CALL();
394 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200395 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800396 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700397
398 if (mCore->mIsAbandoned) {
399 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
400 return NO_INIT;
401 }
402
403 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
404 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
405 return NO_INIT;
406 }
Dan Stoza289ade12014-02-28 11:17:17 -0800407 } // Autolock scope
408
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700409 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800410
411 if ((width && !height) || (!width && height)) {
412 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
413 return BAD_VALUE;
414 }
415
416 status_t returnFlags = NO_ERROR;
417 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
418 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800419 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800420
Shuzhen Wang624375f2023-07-24 22:45:44 +0000421 sp<IConsumerListener> listener;
422 bool callOnFrameDequeued = false;
423 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
Dan Stoza289ade12014-02-28 11:17:17 -0800424 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200425 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800426
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100427 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
428 // is finished such that we don't allocate in parallel.
429 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
430 mDequeueWaitingForAllocation = true;
431 mCore->waitWhileAllocatingLocked(lock);
432 mDequeueWaitingForAllocation = false;
433 mDequeueWaitingForAllocationCondition.notify_all();
434 }
Dan Stoza289ade12014-02-28 11:17:17 -0800435
436 if (format == 0) {
437 format = mCore->mDefaultBufferFormat;
438 }
439
440 // Enable the usage bits the consumer requested
441 usage |= mCore->mConsumerUsageBits;
442
Dan Stoza9de72932015-04-16 17:28:43 -0700443 const bool useDefaultSize = !width && !height;
444 if (useDefaultSize) {
445 width = mCore->mDefaultWidth;
446 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700447 if (mCore->mAutoPrerotation &&
448 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
449 std::swap(width, height);
450 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800451 }
Dan Stoza289ade12014-02-28 11:17:17 -0800452
Pablo Ceballos981066c2016-02-18 12:54:37 -0800453 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700454 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200455 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700456 if (status != NO_ERROR) {
457 return status;
458 }
459
460 // This should not happen
461 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
462 BQ_LOGE("dequeueBuffer: no available buffer slots");
463 return -EBUSY;
464 }
465
466 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
467
468 // If we are not allowed to allocate new buffers,
469 // waitForFreeSlotThenRelock must have returned a slot containing a
470 // buffer. If this buffer would require reallocation to meet the
471 // requested attributes, we free it and attempt to get another one.
472 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700473 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700474 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700475 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700476 return BAD_VALUE;
477 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800478 mCore->mFreeSlots.insert(found);
479 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700480 found = BufferItem::INVALID_BUFFER_SLOT;
481 continue;
482 }
483 }
Dan Stoza289ade12014-02-28 11:17:17 -0800484 }
485
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800486 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700487 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700488 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800489 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
490 "buffer");
491
492 return BAD_VALUE;
493 }
494
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700495 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800496 mCore->mActiveBuffers.insert(found);
497 }
Dan Stoza289ade12014-02-28 11:17:17 -0800498 *outSlot = found;
499 ATRACE_BUFFER_INDEX(found);
500
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800501 attachedByConsumer = mSlots[found].mNeedsReallocation;
502 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800503
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700504 mSlots[found].mBufferState.dequeue();
505
Yi Kong48a619f2018-06-05 16:34:59 -0700506 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700507 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800508 {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700509 if (CC_UNLIKELY(ATRACE_ENABLED())) {
510 if (buffer == nullptr) {
511 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.string());
512 } else {
513 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
514 "layerCount:%d "
515 "usage:%d requested: %dx%d format:%d layerCount:%d "
516 "usage:%d ",
517 mConsumerName.string(), width, height, format,
518 BQ_LAYER_COUNT, usage, buffer->getWidth(),
519 buffer->getHeight(), buffer->getPixelFormat(),
520 buffer->getLayerCount(), buffer->getUsage());
521 }
522 }
Dan Stoza289ade12014-02-28 11:17:17 -0800523 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700524 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800525 mSlots[found].mRequestBufferCalled = false;
526 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
527 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
528 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800529 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700530 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800531
532 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800533 } else {
534 // We add 1 because that will be the frame number when this buffer
535 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700536 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800537 }
538
Dan Stoza800b41a2015-04-28 14:20:04 -0700539 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
540 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800541
Yi Kong48a619f2018-06-05 16:34:59 -0700542 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800543 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
544 "slot=%d w=%d h=%d format=%u",
545 found, buffer->width, buffer->height, buffer->format);
546 }
547
548 eglDisplay = mSlots[found].mEglDisplay;
549 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700550 // Don't return a fence in shared buffer mode, except for the first
551 // frame.
552 *outFence = (mCore->mSharedBufferMode &&
553 mCore->mSharedBufferSlot == found) ?
554 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800555 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
556 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700557
558 // If shared buffer mode has just been enabled, cache the slot of the
559 // first buffer that is dequeued and mark it as the shared buffer.
560 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
561 BufferQueueCore::INVALID_BUFFER_SLOT) {
562 mCore->mSharedBufferSlot = found;
563 mSlots[found].mBufferState.mShared = true;
564 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700565
566 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang624375f2023-07-24 22:45:44 +0000567 callOnFrameDequeued = true;
568 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700569 }
Shuzhen Wang624375f2023-07-24 22:45:44 +0000570
571 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800572 } // Autolock scope
573
574 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700575 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700576 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700577 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700578 {mConsumerName.string(), mConsumerName.size()});
579
580 status_t error = graphicBuffer->initCheck();
581
Dan Stoza289ade12014-02-28 11:17:17 -0800582 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200583 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800584
Chia-I Wufeec3b12017-05-25 09:34:56 -0700585 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700586 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
587 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Shuzhen Wang624375f2023-07-24 22:45:44 +0000588 callOnFrameDequeued = true;
589 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700590 }
591
592 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200593 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700594
Chia-I Wufeec3b12017-05-25 09:34:56 -0700595 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700596 mCore->mFreeSlots.insert(*outSlot);
597 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700598 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
599 return error;
600 }
601
Dan Stoza289ade12014-02-28 11:17:17 -0800602 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700603 mCore->mFreeSlots.insert(*outSlot);
604 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800605 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
606 return NO_INIT;
607 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800608
Pablo Ceballos9e314332016-01-12 13:49:19 -0800609 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800610 } // Autolock scope
611 }
612
Shuzhen Wang624375f2023-07-24 22:45:44 +0000613 if (listener != nullptr && callOnFrameDequeued) {
614 listener->onFrameDequeued(bufferId);
615 }
616
Dan Stoza9f3053d2014-03-06 15:14:33 -0800617 if (attachedByConsumer) {
618 returnFlags |= BUFFER_NEEDS_REALLOCATION;
619 }
620
Dan Stoza289ade12014-02-28 11:17:17 -0800621 if (eglFence != EGL_NO_SYNC_KHR) {
622 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
623 1000000000);
624 // If something goes wrong, log the error, but return the buffer without
625 // synchronizing access to it. It's too late at this point to abort the
626 // dequeue operation.
627 if (result == EGL_FALSE) {
628 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
629 eglGetError());
630 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
631 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
632 }
633 eglDestroySyncKHR(eglDisplay, eglFence);
634 }
635
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700636 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
637 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800638 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800639 mSlots[*outSlot].mGraphicBuffer != nullptr ?
640 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800641
Ian Elliottd11b0442017-07-18 11:05:49 -0600642 if (outBufferAge) {
643 *outBufferAge = mCore->mBufferAge;
644 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700645 addAndGetFrameTimestamps(nullptr, outTimestamps);
646
Dan Stoza289ade12014-02-28 11:17:17 -0800647 return returnFlags;
648}
649
Dan Stoza9f3053d2014-03-06 15:14:33 -0800650status_t BufferQueueProducer::detachBuffer(int slot) {
651 ATRACE_CALL();
652 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700653 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800654
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700655 sp<IConsumerListener> listener;
Shuzhen Wang624375f2023-07-24 22:45:44 +0000656 bool callOnFrameDetached = false;
657 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700658 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200659 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700660
661 if (mCore->mIsAbandoned) {
662 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
663 return NO_INIT;
664 }
665
666 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
667 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
668 return NO_INIT;
669 }
670
671 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
672 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
673 return BAD_VALUE;
674 }
675
676 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
677 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
678 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
679 return BAD_VALUE;
680 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000681 // TODO(http://b/140581935): This message is BQ_LOGW because it
682 // often logs when no actionable errors are present. Return to
683 // using BQ_LOGE after ensuring this only logs during errors.
684 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700685 "(state = %s)", slot, mSlots[slot].mBufferState.string());
686 return BAD_VALUE;
687 } else if (!mSlots[slot].mRequestBufferCalled) {
688 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
689 slot);
690 return BAD_VALUE;
691 }
692
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700693 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700694 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang624375f2023-07-24 22:45:44 +0000695 if (gb != nullptr) {
696 callOnFrameDetached = true;
697 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700698 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700699 mSlots[slot].mBufferState.detachProducer();
700 mCore->mActiveBuffers.erase(slot);
701 mCore->mFreeSlots.insert(slot);
702 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200703 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700704 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800705 }
706
Shuzhen Wang624375f2023-07-24 22:45:44 +0000707 if (listener != nullptr && callOnFrameDetached) {
708 listener->onFrameDetached(bufferId);
709 }
710
Yi Kong48a619f2018-06-05 16:34:59 -0700711 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700712 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700713 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800714
Dan Stoza9f3053d2014-03-06 15:14:33 -0800715 return NO_ERROR;
716}
717
Dan Stozad9822a32014-03-28 15:25:31 -0700718status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
719 sp<Fence>* outFence) {
720 ATRACE_CALL();
721
Yi Kong48a619f2018-06-05 16:34:59 -0700722 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700723 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
724 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700725 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700726 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
727 return BAD_VALUE;
728 }
729
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700730 sp<IConsumerListener> listener;
731 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200732 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700733
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700734 if (mCore->mIsAbandoned) {
735 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
736 return NO_INIT;
737 }
738
739 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
740 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
741 return NO_INIT;
742 }
743
744 if (mCore->mSharedBufferMode) {
745 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
746 "mode");
747 return BAD_VALUE;
748 }
749
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200750 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700751
752 if (mCore->mFreeBuffers.empty()) {
753 return NO_MEMORY;
754 }
755
756 int found = mCore->mFreeBuffers.front();
757 mCore->mFreeBuffers.remove(found);
758 mCore->mFreeSlots.insert(found);
759
760 BQ_LOGV("detachNextBuffer detached slot %d", found);
761
762 *outBuffer = mSlots[found].mGraphicBuffer;
763 *outFence = mSlots[found].mFence;
764 mCore->clearBufferSlotLocked(found);
765 VALIDATE_CONSISTENCY();
766 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700767 }
768
Yi Kong48a619f2018-06-05 16:34:59 -0700769 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700770 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700771 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800772
Dan Stozad9822a32014-03-28 15:25:31 -0700773 return NO_ERROR;
774}
775
Dan Stoza9f3053d2014-03-06 15:14:33 -0800776status_t BufferQueueProducer::attachBuffer(int* outSlot,
777 const sp<android::GraphicBuffer>& buffer) {
778 ATRACE_CALL();
779
Yi Kong48a619f2018-06-05 16:34:59 -0700780 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700781 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800782 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700783 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700784 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800785 return BAD_VALUE;
786 }
787
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200788 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700789
790 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700791 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700792 return NO_INIT;
793 }
794
795 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700796 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700797 return NO_INIT;
798 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800799
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700800 if (mCore->mSharedBufferMode) {
801 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700802 return BAD_VALUE;
803 }
804
Dan Stoza812ed062015-06-02 15:45:22 -0700805 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
806 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
807 "[queue %u]", buffer->getGenerationNumber(),
808 mCore->mGenerationNumber);
809 return BAD_VALUE;
810 }
811
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200812 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700813
Dan Stoza9f3053d2014-03-06 15:14:33 -0800814 status_t returnFlags = NO_ERROR;
815 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200816 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800817 if (status != NO_ERROR) {
818 return status;
819 }
820
821 // This should not happen
822 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700823 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800824 return -EBUSY;
825 }
826
827 *outSlot = found;
828 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700829 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800830 *outSlot, returnFlags);
831
832 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700833 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800834 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
835 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700836 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800837 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800838 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800839 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800840 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700841
Dan Stoza9f3053d2014-03-06 15:14:33 -0800842 return returnFlags;
843}
844
Dan Stoza289ade12014-02-28 11:17:17 -0800845status_t BufferQueueProducer::queueBuffer(int slot,
846 const QueueBufferInput &input, QueueBufferOutput *output) {
847 ATRACE_CALL();
848 ATRACE_BUFFER_INDEX(slot);
849
Brian Andersond6927fb2016-07-23 23:37:30 -0700850 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800851 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800852 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700853 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800854 int scalingMode;
855 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700856 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700857 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700858 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700859 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700860 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
861 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700862 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700863 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800864
Yi Kong48a619f2018-06-05 16:34:59 -0700865 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800866 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800867 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800868 }
869
Brian Anderson3d4039d2016-09-23 16:31:30 -0700870 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
871
Dan Stoza289ade12014-02-28 11:17:17 -0800872 switch (scalingMode) {
873 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
874 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
875 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
876 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
877 break;
878 default:
879 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800880 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800881 }
882
Dan Stoza8dc55392014-11-04 11:37:46 -0800883 sp<IConsumerListener> frameAvailableListener;
884 sp<IConsumerListener> frameReplacedListener;
885 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700886 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800887 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800888 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200889 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800890
891 if (mCore->mIsAbandoned) {
892 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
893 return NO_INIT;
894 }
895
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700896 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
897 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
898 return NO_INIT;
899 }
900
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800901 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800902 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800903 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800904 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700905 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800906 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700907 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800908 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800909 } else if (!mSlots[slot].mRequestBufferCalled) {
910 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
911 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800912 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800913 }
914
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700915 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700916 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700917 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700918 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700919 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700920 mSlots[slot].mBufferState.mShared = true;
921 }
922
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800923 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700924 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
925 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
926 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700927 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800928 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800929
930 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
931 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700932 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800933 crop.intersect(bufferRect, &croppedRect);
934 if (croppedRect != crop) {
935 BQ_LOGE("queueBuffer: crop rect is not contained within the "
936 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800937 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 }
939
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800940 // Override UNKNOWN dataspace with consumer default
941 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
942 dataSpace = mCore->mDefaultBufferDataSpace;
943 }
944
Brian Andersond6927fb2016-07-23 23:37:30 -0700945 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700946 mSlots[slot].mBufferState.queue();
947
Brian Andersond6927fb2016-07-23 23:37:30 -0700948 // Increment the frame counter and store a local version of it
949 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800950 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700951 currentFrameNumber = mCore->mFrameCounter;
952 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800953
Dan Stoza289ade12014-02-28 11:17:17 -0800954 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
955 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
956 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800957 item.mTransform = transform &
958 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800959 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800960 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
961 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700962 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800963 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800964 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700965 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700966 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800967 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700968 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700969 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700970 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700971 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800972 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700973 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700974 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700975 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700976 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800977 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800978
Ruben Brunk1681d952014-06-27 15:51:55 -0700979 mStickyTransform = stickyTransform;
980
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700981 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700982 if (mCore->mSharedBufferMode) {
983 mCore->mSharedBufferCache.crop = crop;
984 mCore->mSharedBufferCache.transform = transform;
985 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700986 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700987 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 }
989
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800990 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800991 if (mCore->mQueue.empty()) {
992 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
993 // and simply queue this buffer
994 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800995 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800996 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700997 // When the queue is not empty, we need to look at the last buffer
998 // in the queue to see if we need to replace it
999 const BufferItem& last = mCore->mQueue.itemAt(
1000 mCore->mQueue.size() - 1);
1001 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001002
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001003 if (!last.mIsStale) {
1004 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001005
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001006 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007 // still be around. Mark it as no longer shared if this
1008 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001009 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001010 mSlots[last.mSlot].mBufferState.isFree()) {
1011 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001012 }
1013 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001014 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1015 mCore->mActiveBuffers.erase(last.mSlot);
1016 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001017 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001018 }
Dan Stoza289ade12014-02-28 11:17:17 -08001019 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001020
Steven Thomas862e7532019-07-10 19:21:03 -07001021 // Make sure to merge the damage rect from the frame we're about
1022 // to drop into the new frame's damage rect.
1023 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1024 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1025 item.mSurfaceDamage = Region::INVALID_REGION;
1026 } else {
1027 item.mSurfaceDamage |= last.mSurfaceDamage;
1028 }
1029
Dan Stoza289ade12014-02-28 11:17:17 -08001030 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001031 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001032 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001033 } else {
1034 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001035 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001036 }
1037 }
1038
1039 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001040 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001041 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001042
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001043 output->width = mCore->mDefaultWidth;
1044 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001045 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001046 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1047 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001048
Colin Cross152c3b72016-09-27 14:08:19 -07001049 ATRACE_INT(mCore->mConsumerName.string(),
1050 static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001051#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001052 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001053#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001054 // Take a ticket for the callback functions
1055 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001056
Pablo Ceballos9e314332016-01-12 13:49:19 -08001057 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001058 } // Autolock scope
1059
Irvel468051e2016-06-13 16:44:44 -07001060 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1061 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1062 // there will be no Binder call
1063 if (!mConsumerIsSurfaceFlinger) {
1064 item.mGraphicBuffer.clear();
1065 }
1066
JihCheng Chiu544c5222019-04-02 20:50:58 +08001067 // Update and get FrameEventHistory.
1068 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1069 NewFrameEventsEntry newFrameEventsEntry = {
1070 currentFrameNumber,
1071 postedTime,
1072 requestedPresentTimestamp,
1073 std::move(acquireFenceTime)
1074 };
1075 addAndGetFrameTimestamps(&newFrameEventsEntry,
1076 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1077
Dan Stoza8dc55392014-11-04 11:37:46 -08001078 // Call back without the main BufferQueue lock held, but with the callback
1079 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001080
1081 int connectedApi;
1082 sp<Fence> lastQueuedFence;
1083
1084 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001085 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001086 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001087 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001088 }
1089
Yi Kong48a619f2018-06-05 16:34:59 -07001090 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001091 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001092 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001093 frameReplacedListener->onFrameReplaced(item);
1094 }
1095
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001096 connectedApi = mCore->mConnectedApi;
1097 lastQueuedFence = std::move(mLastQueueBufferFence);
1098
1099 mLastQueueBufferFence = std::move(acquireFence);
1100 mLastQueuedCrop = item.mCrop;
1101 mLastQueuedTransform = item.mTransform;
1102
Dan Stoza8dc55392014-11-04 11:37:46 -08001103 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001104 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001105 }
1106
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001107 // Wait without lock held
1108 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1109 // Waiting here allows for two full buffers to be queued but not a
1110 // third. In the event that frames take varying time, this makes a
1111 // small trade-off in favor of latency rather than throughput.
1112 lastQueuedFence->waitForever("Throttling EGL Production");
1113 }
1114
Dan Stoza289ade12014-02-28 11:17:17 -08001115 return NO_ERROR;
1116}
1117
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001118status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001119 ATRACE_CALL();
1120 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001121
Shuzhen Wang624375f2023-07-24 22:45:44 +00001122 sp<IConsumerListener> listener;
1123 bool callOnFrameCancelled = false;
1124 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1125 {
1126 std::lock_guard<std::mutex> lock(mCore->mMutex);
1127
1128 if (mCore->mIsAbandoned) {
1129 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1130 return NO_INIT;
1131 }
1132
1133 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1134 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1135 return NO_INIT;
1136 }
1137
1138 if (mCore->mSharedBufferMode) {
1139 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1140 return BAD_VALUE;
1141 }
1142
1143 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1144 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1145 BufferQueueDefs::NUM_BUFFER_SLOTS);
1146 return BAD_VALUE;
1147 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1148 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1149 "(state = %s)",
1150 slot, mSlots[slot].mBufferState.string());
1151 return BAD_VALUE;
1152 } else if (fence == nullptr) {
1153 BQ_LOGE("cancelBuffer: fence is NULL");
1154 return BAD_VALUE;
1155 }
1156
1157 mSlots[slot].mBufferState.cancel();
1158
1159 // After leaving shared buffer mode, the shared buffer will still be around.
1160 // Mark it as no longer shared if this operation causes it to be free.
1161 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1162 mSlots[slot].mBufferState.mShared = false;
1163 }
1164
1165 // Don't put the shared buffer on the free list.
1166 if (!mSlots[slot].mBufferState.isShared()) {
1167 mCore->mActiveBuffers.erase(slot);
1168 mCore->mFreeBuffers.push_back(slot);
1169 }
1170
1171 auto gb = mSlots[slot].mGraphicBuffer;
1172 if (gb != nullptr) {
1173 callOnFrameCancelled = true;
1174 bufferId = gb->getId();
1175 }
1176 mSlots[slot].mFence = fence;
1177 mCore->mDequeueCondition.notify_all();
1178 listener = mCore->mConsumerListener;
1179 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001180 }
1181
Shuzhen Wang624375f2023-07-24 22:45:44 +00001182 if (listener != nullptr && callOnFrameCancelled) {
1183 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001184 }
1185
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001186 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001187}
1188
1189int BufferQueueProducer::query(int what, int *outValue) {
1190 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001191 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001192
Yi Kong48a619f2018-06-05 16:34:59 -07001193 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001194 BQ_LOGE("query: outValue was NULL");
1195 return BAD_VALUE;
1196 }
1197
1198 if (mCore->mIsAbandoned) {
1199 BQ_LOGE("query: BufferQueue has been abandoned");
1200 return NO_INIT;
1201 }
1202
1203 int value;
1204 switch (what) {
1205 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001206 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001207 break;
1208 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001209 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001210 break;
1211 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001212 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001213 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001214 case NATIVE_WINDOW_LAYER_COUNT:
1215 // All BufferQueue buffers have a single layer.
1216 value = BQ_LAYER_COUNT;
1217 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001218 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001219 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001220 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001221 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001222 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001223 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001224 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1225 value = (mCore->mQueue.size() > 1);
1226 break;
1227 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001228 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001229 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001230 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001231 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1232 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1233 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001234 case NATIVE_WINDOW_BUFFER_AGE:
1235 if (mCore->mBufferAge > INT32_MAX) {
1236 value = 0;
1237 } else {
1238 value = static_cast<int32_t>(mCore->mBufferAge);
1239 }
1240 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001241 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1242 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1243 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001244 default:
1245 return BAD_VALUE;
1246 }
1247
1248 BQ_LOGV("query: %d? %d", what, value);
1249 *outValue = value;
1250 return NO_ERROR;
1251}
1252
Dan Stozaf0eaf252014-03-21 13:05:51 -07001253status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001254 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1255 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001256 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001257 mConsumerName = mCore->mConsumerName;
1258 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1259 producerControlledByApp ? "true" : "false");
1260
1261 if (mCore->mIsAbandoned) {
1262 BQ_LOGE("connect: BufferQueue has been abandoned");
1263 return NO_INIT;
1264 }
1265
Yi Kong48a619f2018-06-05 16:34:59 -07001266 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001267 BQ_LOGE("connect: BufferQueue has no consumer");
1268 return NO_INIT;
1269 }
1270
Yi Kong48a619f2018-06-05 16:34:59 -07001271 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001272 BQ_LOGE("connect: output was NULL");
1273 return BAD_VALUE;
1274 }
1275
1276 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1277 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1278 mCore->mConnectedApi, api);
1279 return BAD_VALUE;
1280 }
1281
1282 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1283 mDequeueTimeout < 0 ?
1284 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1285 mCore->mMaxBufferCount) -
1286 mCore->getMaxBufferCountLocked();
1287 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1288 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1289 "slots. Delta = %d", delta);
1290 return BAD_VALUE;
1291 }
1292
Dan Stoza289ade12014-02-28 11:17:17 -08001293 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001294 switch (api) {
1295 case NATIVE_WINDOW_API_EGL:
1296 case NATIVE_WINDOW_API_CPU:
1297 case NATIVE_WINDOW_API_MEDIA:
1298 case NATIVE_WINDOW_API_CAMERA:
1299 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001300
1301 output->width = mCore->mDefaultWidth;
1302 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001303 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001304 output->numPendingBuffers =
1305 static_cast<uint32_t>(mCore->mQueue.size());
1306 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001307 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001308 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001309
Yi Kong48a619f2018-06-05 16:34:59 -07001310 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001311 // Set up a death notification so that we can disconnect
1312 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001313#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001314 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001315 status = IInterface::asBinder(listener)->linkToDeath(
1316 static_cast<IBinder::DeathRecipient*>(this));
1317 if (status != NO_ERROR) {
1318 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1319 strerror(-status), status);
1320 }
1321 mCore->mLinkedToDeath = listener;
1322 }
Chong Zhang62493092020-01-15 16:04:47 -08001323#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001324 mCore->mConnectedProducerListener = listener;
1325 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001326 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001327 break;
1328 default:
1329 BQ_LOGE("connect: unknown API %d", api);
1330 status = BAD_VALUE;
1331 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001332 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001333 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001334 mCore->mBufferHasBeenQueued = false;
1335 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001336 mCore->mQueueBufferCanDrop = false;
1337 mCore->mLegacyBufferDrop = true;
1338 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1339 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1340 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001341 }
Dan Stoza289ade12014-02-28 11:17:17 -08001342
Pablo Ceballos981066c2016-02-18 12:54:37 -08001343 mCore->mAllowAllocation = true;
1344 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001345 return status;
1346}
1347
Robert Carr97b9c862016-09-08 13:54:35 -07001348status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001349 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001350 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001351
1352 int status = NO_ERROR;
1353 sp<IConsumerListener> listener;
1354 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001355 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001356
1357 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001358 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001359 return NO_ERROR;
1360 }
1361 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1362 }
1363
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001364 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001365
1366 if (mCore->mIsAbandoned) {
1367 // It's not really an error to disconnect after the surface has
1368 // been abandoned; it should just be a no-op.
1369 return NO_ERROR;
1370 }
1371
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001372 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001373 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1374 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1375 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001376 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001377 // If we're asked to disconnect the currently connected api but
1378 // nobody is connected, it's not really an error.
1379 if (api == BufferQueueCore::NO_CONNECTED_API) {
1380 return NO_ERROR;
1381 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001382 }
1383
Dan Stoza289ade12014-02-28 11:17:17 -08001384 switch (api) {
1385 case NATIVE_WINDOW_API_EGL:
1386 case NATIVE_WINDOW_API_CPU:
1387 case NATIVE_WINDOW_API_MEDIA:
1388 case NATIVE_WINDOW_API_CAMERA:
1389 if (mCore->mConnectedApi == api) {
1390 mCore->freeAllBuffersLocked();
1391
Chong Zhang62493092020-01-15 16:04:47 -08001392#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001393 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001394 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001395 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001396 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001397 // This can fail if we're here because of the death
1398 // notification, but we just ignore it
1399 token->unlinkToDeath(
1400 static_cast<IBinder::DeathRecipient*>(this));
1401 }
Chong Zhang62493092020-01-15 16:04:47 -08001402#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001403 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001404 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001405 mCore->mLinkedToDeath = nullptr;
1406 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001407 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001408 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001409 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001410 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001411 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001412 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001413 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1414 BQ_LOGE("disconnect: not connected (req=%d)", api);
1415 status = NO_INIT;
1416 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001417 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001418 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001419 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001420 }
1421 break;
1422 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001423 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001424 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001425 break;
1426 }
1427 } // Autolock scope
1428
1429 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001430 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001431 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001432 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001433 }
1434
1435 return status;
1436}
1437
Jesse Hall399184a2014-03-03 15:42:54 -08001438status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001439 sp<IConsumerListener> listener;
1440 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001441 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001442 mCore->mSidebandStream = stream;
1443 listener = mCore->mConsumerListener;
1444 } // Autolock scope
1445
Yi Kong48a619f2018-06-05 16:34:59 -07001446 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001447 listener->onSidebandStreamChanged();
1448 }
Jesse Hall399184a2014-03-03 15:42:54 -08001449 return NO_ERROR;
1450}
1451
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001452void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001453 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001454 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001455
1456 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001457 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001458 size_t newBufferCount = 0;
1459 uint32_t allocWidth = 0;
1460 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001461 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001462 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001463 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001464 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001465 std::unique_lock<std::mutex> lock(mCore->mMutex);
1466 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001467
Dan Stoza9de72932015-04-16 17:28:43 -07001468 if (!mCore->mAllowAllocation) {
1469 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1470 "BufferQueue");
1471 return;
1472 }
1473
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001474 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1475 // both allocateBuffers and dequeueBuffer.
1476 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001477 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001478 return;
1479 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001480
Antoine Labour78014f32014-07-15 21:17:03 -07001481 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1482 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001483 if (useDefaultSize && mCore->mAutoPrerotation &&
1484 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1485 std::swap(allocWidth, allocHeight);
1486 }
1487
Antoine Labour78014f32014-07-15 21:17:03 -07001488 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1489 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001490 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001491
1492 mCore->mIsAllocating = true;
1493 } // Autolock scope
1494
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001495 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001496 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001497 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001498 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001499 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001500
1501 status_t result = graphicBuffer->initCheck();
1502
Antoine Labour78014f32014-07-15 21:17:03 -07001503 if (result != NO_ERROR) {
1504 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001505 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001506 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001507 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001508 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001509 return;
1510 }
1511 buffers.push_back(graphicBuffer);
1512 }
1513
1514 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001515 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001516 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1517 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001518 if (useDefaultSize && mCore->mAutoPrerotation &&
1519 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1520 std::swap(checkWidth, checkHeight);
1521 }
1522
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001523 PixelFormat checkFormat = format != 0 ?
1524 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001525 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001526 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1527 checkFormat != allocFormat || checkUsage != allocUsage) {
1528 // Something changed while we released the lock. Retry.
1529 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1530 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001531 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001532 continue;
1533 }
1534
Antoine Labour78014f32014-07-15 21:17:03 -07001535 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001536 if (mCore->mFreeSlots.empty()) {
1537 BQ_LOGV("allocateBuffers: a slot was occupied while "
1538 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001539 continue;
1540 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001541 auto slot = mCore->mFreeSlots.begin();
1542 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1543 mSlots[*slot].mGraphicBuffer = buffers[i];
1544 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001545
1546 // freeBufferLocked puts this slot on the free slots list. Since
1547 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001548 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001549
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001550 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1551 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001552
1553 // Make sure the erase is done after all uses of the slot
1554 // iterator since it will be invalid after this point.
1555 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001556 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001557
Antoine Labour78014f32014-07-15 21:17:03 -07001558 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001559 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001560 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001561
1562 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1563 // waiting anymore so it can use the buffer we just allocated.
1564 while (mDequeueWaitingForAllocation) {
1565 mDequeueWaitingForAllocationCondition.wait(lock);
1566 }
Antoine Labour78014f32014-07-15 21:17:03 -07001567 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001568 }
1569}
1570
Dan Stoza9de72932015-04-16 17:28:43 -07001571status_t BufferQueueProducer::allowAllocation(bool allow) {
1572 ATRACE_CALL();
1573 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1574
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001575 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001576 mCore->mAllowAllocation = allow;
1577 return NO_ERROR;
1578}
1579
Dan Stoza812ed062015-06-02 15:45:22 -07001580status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1581 ATRACE_CALL();
1582 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1583
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001584 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001585 mCore->mGenerationNumber = generationNumber;
1586 return NO_ERROR;
1587}
1588
Dan Stozac6f30bd2015-06-08 09:32:50 -07001589String8 BufferQueueProducer::getConsumerName() const {
1590 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001591 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001592 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1593 return mConsumerName;
1594}
1595
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001596status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001597 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001598 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001599
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001600 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001601 if (!sharedBufferMode) {
1602 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001603 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001604 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001605 return NO_ERROR;
1606}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001607
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001608status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1609 ATRACE_CALL();
1610 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1611
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001612 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001613
1614 mCore->mAutoRefresh = autoRefresh;
1615 return NO_ERROR;
1616}
1617
Dan Stoza127fc632015-06-30 13:43:32 -07001618status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1619 ATRACE_CALL();
1620 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1621
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001622 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001623 bool dequeueBufferCannotBlock =
1624 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1625 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001626 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1627 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1628 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1629 "available slots. Delta = %d", delta);
1630 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001631 }
1632
Pablo Ceballos981066c2016-02-18 12:54:37 -08001633 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001634 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1635 if (timeout > 0) {
1636 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001637 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001638
Pablo Ceballos981066c2016-02-18 12:54:37 -08001639 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001640 return NO_ERROR;
1641}
1642
Sungtak Lee3249fb62019-03-02 16:40:47 -08001643status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1644 ATRACE_CALL();
1645 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1646
1647 std::lock_guard<std::mutex> lock(mCore->mMutex);
1648 mCore->mLegacyBufferDrop = drop;
1649 return NO_ERROR;
1650}
1651
Dan Stoza50101d02016-04-07 16:53:23 -07001652status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001653 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001654 ATRACE_CALL();
1655 BQ_LOGV("getLastQueuedBuffer");
1656
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001657 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001658 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1659 *outBuffer = nullptr;
1660 *outFence = Fence::NO_FENCE;
1661 return NO_ERROR;
1662 }
1663
1664 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1665 *outFence = mLastQueueBufferFence;
1666
John Reck1a61da52016-04-28 13:18:15 -07001667 // Currently only SurfaceFlinger internally ever changes
1668 // GLConsumer's filtering mode, so we just use 'true' here as
1669 // this is slightly specialized for the current client of this API,
1670 // which does want filtering.
1671 GLConsumer::computeTransformMatrix(outTransformMatrix,
1672 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1673 mLastQueuedTransform, true /* filter */);
1674
Dan Stoza50101d02016-04-07 16:53:23 -07001675 return NO_ERROR;
1676}
1677
John Reckf0f13e82021-05-18 00:42:56 -04001678status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1679 Rect* outRect, uint32_t* outTransform) {
1680 ATRACE_CALL();
1681 BQ_LOGV("getLastQueuedBuffer");
1682
1683 std::lock_guard<std::mutex> lock(mCore->mMutex);
1684 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1685 *outBuffer = nullptr;
1686 *outFence = Fence::NO_FENCE;
1687 return NO_ERROR;
1688 }
1689
1690 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1691 *outFence = mLastQueueBufferFence;
1692 *outRect = mLastQueuedCrop;
1693 *outTransform = mLastQueuedTransform;
1694
1695 return NO_ERROR;
1696}
1697
Brian Anderson3890c392016-07-25 12:48:08 -07001698void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1699 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001700}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001701
Brian Anderson3890c392016-07-25 12:48:08 -07001702void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001703 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001704 FrameEventHistoryDelta* outDelta) {
1705 if (newTimestamps == nullptr && outDelta == nullptr) {
1706 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001707 }
1708
1709 ATRACE_CALL();
1710 BQ_LOGV("addAndGetFrameTimestamps");
1711 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001712 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001713 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001714 listener = mCore->mConsumerListener;
1715 }
Yi Kong48a619f2018-06-05 16:34:59 -07001716 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001717 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001718 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001719}
1720
Dan Stoza289ade12014-02-28 11:17:17 -08001721void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1722 // If we're here, it means that a producer we were connected to died.
1723 // We're guaranteed that we are still connected to it because we remove
1724 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1725 // without synchronization here.
1726 int api = mCore->mConnectedApi;
1727 disconnect(api);
1728}
1729
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001730status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1731 BQ_LOGV("getUniqueId");
1732
1733 *outId = mCore->mUniqueId;
1734 return NO_ERROR;
1735}
1736
Chia-I Wue2786ea2017-08-07 10:36:08 -07001737status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1738 BQ_LOGV("getConsumerUsage");
1739
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001740 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001741 *outUsage = mCore->mConsumerUsageBits;
1742 return NO_ERROR;
1743}
1744
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001745status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1746 ATRACE_CALL();
1747 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1748
1749 std::lock_guard<std::mutex> lock(mCore->mMutex);
1750
1751 mCore->mAutoPrerotation = autoPrerotation;
1752 return NO_ERROR;
1753}
1754
Dan Stoza289ade12014-02-28 11:17:17 -08001755} // namespace android