blob: 43ff54f3c8dfd4f08ddcbb9bdc14f098e0fa032f [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 Wang266f31a2023-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 Wang266f31a2023-07-24 22:45:44 +0000567 callOnFrameDequeued = true;
568 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700569 }
Shuzhen Wang266f31a2023-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 Wang266f31a2023-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 Wang266f31a2023-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,
639 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
640
Ian Elliottd11b0442017-07-18 11:05:49 -0600641 if (outBufferAge) {
642 *outBufferAge = mCore->mBufferAge;
643 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700644 addAndGetFrameTimestamps(nullptr, outTimestamps);
645
Dan Stoza289ade12014-02-28 11:17:17 -0800646 return returnFlags;
647}
648
Dan Stoza9f3053d2014-03-06 15:14:33 -0800649status_t BufferQueueProducer::detachBuffer(int slot) {
650 ATRACE_CALL();
651 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700652 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800653
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700654 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000655 bool callOnFrameDetached = false;
656 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700657 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200658 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700659
660 if (mCore->mIsAbandoned) {
661 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
662 return NO_INIT;
663 }
664
665 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
666 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
667 return NO_INIT;
668 }
669
670 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
671 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
672 return BAD_VALUE;
673 }
674
675 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
676 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
677 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
678 return BAD_VALUE;
679 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000680 // TODO(http://b/140581935): This message is BQ_LOGW because it
681 // often logs when no actionable errors are present. Return to
682 // using BQ_LOGE after ensuring this only logs during errors.
683 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700684 "(state = %s)", slot, mSlots[slot].mBufferState.string());
685 return BAD_VALUE;
686 } else if (!mSlots[slot].mRequestBufferCalled) {
687 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
688 slot);
689 return BAD_VALUE;
690 }
691
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700692 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700693 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000694 if (gb != nullptr) {
695 callOnFrameDetached = true;
696 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700697 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700698 mSlots[slot].mBufferState.detachProducer();
699 mCore->mActiveBuffers.erase(slot);
700 mCore->mFreeSlots.insert(slot);
701 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200702 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700703 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800704 }
705
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000706 if (listener != nullptr && callOnFrameDetached) {
707 listener->onFrameDetached(bufferId);
708 }
709
Yi Kong48a619f2018-06-05 16:34:59 -0700710 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700711 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700712 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800713
Dan Stoza9f3053d2014-03-06 15:14:33 -0800714 return NO_ERROR;
715}
716
Dan Stozad9822a32014-03-28 15:25:31 -0700717status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
718 sp<Fence>* outFence) {
719 ATRACE_CALL();
720
Yi Kong48a619f2018-06-05 16:34:59 -0700721 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700722 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
723 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700724 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700725 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
726 return BAD_VALUE;
727 }
728
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700729 sp<IConsumerListener> listener;
730 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200731 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700732
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700733 if (mCore->mIsAbandoned) {
734 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
735 return NO_INIT;
736 }
737
738 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
739 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
740 return NO_INIT;
741 }
742
743 if (mCore->mSharedBufferMode) {
744 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
745 "mode");
746 return BAD_VALUE;
747 }
748
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200749 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700750
751 if (mCore->mFreeBuffers.empty()) {
752 return NO_MEMORY;
753 }
754
755 int found = mCore->mFreeBuffers.front();
756 mCore->mFreeBuffers.remove(found);
757 mCore->mFreeSlots.insert(found);
758
759 BQ_LOGV("detachNextBuffer detached slot %d", found);
760
761 *outBuffer = mSlots[found].mGraphicBuffer;
762 *outFence = mSlots[found].mFence;
763 mCore->clearBufferSlotLocked(found);
764 VALIDATE_CONSISTENCY();
765 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700766 }
767
Yi Kong48a619f2018-06-05 16:34:59 -0700768 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700769 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700770 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800771
Dan Stozad9822a32014-03-28 15:25:31 -0700772 return NO_ERROR;
773}
774
Dan Stoza9f3053d2014-03-06 15:14:33 -0800775status_t BufferQueueProducer::attachBuffer(int* outSlot,
776 const sp<android::GraphicBuffer>& buffer) {
777 ATRACE_CALL();
778
Yi Kong48a619f2018-06-05 16:34:59 -0700779 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700780 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800781 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700782 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700783 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 return BAD_VALUE;
785 }
786
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200787 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700788
789 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700790 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700791 return NO_INIT;
792 }
793
794 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700795 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700796 return NO_INIT;
797 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800798
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700799 if (mCore->mSharedBufferMode) {
800 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700801 return BAD_VALUE;
802 }
803
Dan Stoza812ed062015-06-02 15:45:22 -0700804 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
805 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
806 "[queue %u]", buffer->getGenerationNumber(),
807 mCore->mGenerationNumber);
808 return BAD_VALUE;
809 }
810
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200811 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700812
Dan Stoza9f3053d2014-03-06 15:14:33 -0800813 status_t returnFlags = NO_ERROR;
814 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200815 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 if (status != NO_ERROR) {
817 return status;
818 }
819
820 // This should not happen
821 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700822 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800823 return -EBUSY;
824 }
825
826 *outSlot = found;
827 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700828 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800829 *outSlot, returnFlags);
830
831 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700832 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800833 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
834 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700835 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800836 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800837 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800838 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800839 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700840
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841 return returnFlags;
842}
843
Dan Stoza289ade12014-02-28 11:17:17 -0800844status_t BufferQueueProducer::queueBuffer(int slot,
845 const QueueBufferInput &input, QueueBufferOutput *output) {
846 ATRACE_CALL();
847 ATRACE_BUFFER_INDEX(slot);
848
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800850 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800851 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700852 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800853 int scalingMode;
854 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700855 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700856 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700857 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700858 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700859 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
860 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700861 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700862 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800863
Yi Kong48a619f2018-06-05 16:34:59 -0700864 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800865 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800866 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 }
868
Brian Anderson3d4039d2016-09-23 16:31:30 -0700869 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
870
Dan Stoza289ade12014-02-28 11:17:17 -0800871 switch (scalingMode) {
872 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
873 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
874 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
875 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
876 break;
877 default:
878 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800879 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800880 }
881
Dan Stoza8dc55392014-11-04 11:37:46 -0800882 sp<IConsumerListener> frameAvailableListener;
883 sp<IConsumerListener> frameReplacedListener;
884 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700885 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800886 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800887 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200888 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800889
890 if (mCore->mIsAbandoned) {
891 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
892 return NO_INIT;
893 }
894
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700895 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
896 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
897 return NO_INIT;
898 }
899
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800900 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800901 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800902 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800903 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700904 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800905 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700906 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800907 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800908 } else if (!mSlots[slot].mRequestBufferCalled) {
909 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
910 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800911 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800912 }
913
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700914 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700915 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700916 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700917 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700918 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700919 mSlots[slot].mBufferState.mShared = true;
920 }
921
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800922 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700923 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
924 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
925 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700926 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800927 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800928
929 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
930 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700931 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800932 crop.intersect(bufferRect, &croppedRect);
933 if (croppedRect != crop) {
934 BQ_LOGE("queueBuffer: crop rect is not contained within the "
935 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800936 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800937 }
938
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800939 // Override UNKNOWN dataspace with consumer default
940 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
941 dataSpace = mCore->mDefaultBufferDataSpace;
942 }
943
Brian Andersond6927fb2016-07-23 23:37:30 -0700944 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700945 mSlots[slot].mBufferState.queue();
946
Brian Andersond6927fb2016-07-23 23:37:30 -0700947 // Increment the frame counter and store a local version of it
948 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800949 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700950 currentFrameNumber = mCore->mFrameCounter;
951 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800952
Dan Stoza289ade12014-02-28 11:17:17 -0800953 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
954 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
955 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800956 item.mTransform = transform &
957 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800958 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800959 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
960 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700961 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800962 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800963 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700964 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700965 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800966 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700967 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700968 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700969 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700970 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800971 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700972 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700973 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700974 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700975 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800976 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800977
Ruben Brunk1681d952014-06-27 15:51:55 -0700978 mStickyTransform = stickyTransform;
979
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700980 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700981 if (mCore->mSharedBufferMode) {
982 mCore->mSharedBufferCache.crop = crop;
983 mCore->mSharedBufferCache.transform = transform;
984 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700985 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700986 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700987 }
988
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800989 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800990 if (mCore->mQueue.empty()) {
991 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
992 // and simply queue this buffer
993 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800994 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800995 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700996 // When the queue is not empty, we need to look at the last buffer
997 // in the queue to see if we need to replace it
998 const BufferItem& last = mCore->mQueue.itemAt(
999 mCore->mQueue.size() - 1);
1000 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001001
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001002 if (!last.mIsStale) {
1003 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001004
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001005 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001006 // still be around. Mark it as no longer shared if this
1007 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001008 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001009 mSlots[last.mSlot].mBufferState.isFree()) {
1010 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001011 }
1012 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001013 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1014 mCore->mActiveBuffers.erase(last.mSlot);
1015 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001016 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001017 }
Dan Stoza289ade12014-02-28 11:17:17 -08001018 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001019
Steven Thomas862e7532019-07-10 19:21:03 -07001020 // Make sure to merge the damage rect from the frame we're about
1021 // to drop into the new frame's damage rect.
1022 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1023 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1024 item.mSurfaceDamage = Region::INVALID_REGION;
1025 } else {
1026 item.mSurfaceDamage |= last.mSurfaceDamage;
1027 }
1028
Dan Stoza289ade12014-02-28 11:17:17 -08001029 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001030 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001031 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001032 } else {
1033 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001034 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001035 }
1036 }
1037
1038 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001039 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001040 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001041
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001042 output->width = mCore->mDefaultWidth;
1043 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001044 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001045 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1046 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001047
Colin Cross152c3b72016-09-27 14:08:19 -07001048 ATRACE_INT(mCore->mConsumerName.string(),
1049 static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001050#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001051 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001052#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001053 // Take a ticket for the callback functions
1054 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001055
Pablo Ceballos9e314332016-01-12 13:49:19 -08001056 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001057 } // Autolock scope
1058
Irvel468051e2016-06-13 16:44:44 -07001059 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1060 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1061 // there will be no Binder call
1062 if (!mConsumerIsSurfaceFlinger) {
1063 item.mGraphicBuffer.clear();
1064 }
1065
JihCheng Chiu544c5222019-04-02 20:50:58 +08001066 // Update and get FrameEventHistory.
1067 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1068 NewFrameEventsEntry newFrameEventsEntry = {
1069 currentFrameNumber,
1070 postedTime,
1071 requestedPresentTimestamp,
1072 std::move(acquireFenceTime)
1073 };
1074 addAndGetFrameTimestamps(&newFrameEventsEntry,
1075 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1076
Dan Stoza8dc55392014-11-04 11:37:46 -08001077 // Call back without the main BufferQueue lock held, but with the callback
1078 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001079
1080 int connectedApi;
1081 sp<Fence> lastQueuedFence;
1082
1083 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001084 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001085 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001086 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001087 }
1088
Yi Kong48a619f2018-06-05 16:34:59 -07001089 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001090 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001091 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001092 frameReplacedListener->onFrameReplaced(item);
1093 }
1094
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001095 connectedApi = mCore->mConnectedApi;
1096 lastQueuedFence = std::move(mLastQueueBufferFence);
1097
1098 mLastQueueBufferFence = std::move(acquireFence);
1099 mLastQueuedCrop = item.mCrop;
1100 mLastQueuedTransform = item.mTransform;
1101
Dan Stoza8dc55392014-11-04 11:37:46 -08001102 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001103 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001104 }
1105
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001106 // Wait without lock held
1107 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1108 // Waiting here allows for two full buffers to be queued but not a
1109 // third. In the event that frames take varying time, this makes a
1110 // small trade-off in favor of latency rather than throughput.
1111 lastQueuedFence->waitForever("Throttling EGL Production");
1112 }
1113
Dan Stoza289ade12014-02-28 11:17:17 -08001114 return NO_ERROR;
1115}
1116
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001117status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001118 ATRACE_CALL();
1119 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001120
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001121 sp<IConsumerListener> listener;
1122 bool callOnFrameCancelled = false;
1123 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1124 {
1125 std::lock_guard<std::mutex> lock(mCore->mMutex);
1126
1127 if (mCore->mIsAbandoned) {
1128 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1129 return NO_INIT;
1130 }
1131
1132 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1133 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1134 return NO_INIT;
1135 }
1136
1137 if (mCore->mSharedBufferMode) {
1138 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1139 return BAD_VALUE;
1140 }
1141
1142 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1143 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1144 BufferQueueDefs::NUM_BUFFER_SLOTS);
1145 return BAD_VALUE;
1146 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1147 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1148 "(state = %s)",
1149 slot, mSlots[slot].mBufferState.string());
1150 return BAD_VALUE;
1151 } else if (fence == nullptr) {
1152 BQ_LOGE("cancelBuffer: fence is NULL");
1153 return BAD_VALUE;
1154 }
1155
1156 mSlots[slot].mBufferState.cancel();
1157
1158 // After leaving shared buffer mode, the shared buffer will still be around.
1159 // Mark it as no longer shared if this operation causes it to be free.
1160 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1161 mSlots[slot].mBufferState.mShared = false;
1162 }
1163
1164 // Don't put the shared buffer on the free list.
1165 if (!mSlots[slot].mBufferState.isShared()) {
1166 mCore->mActiveBuffers.erase(slot);
1167 mCore->mFreeBuffers.push_back(slot);
1168 }
1169
1170 auto gb = mSlots[slot].mGraphicBuffer;
1171 if (gb != nullptr) {
1172 callOnFrameCancelled = true;
1173 bufferId = gb->getId();
1174 }
1175 mSlots[slot].mFence = fence;
1176 mCore->mDequeueCondition.notify_all();
1177 listener = mCore->mConsumerListener;
1178 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001179 }
1180
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001181 if (listener != nullptr && callOnFrameCancelled) {
1182 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001183 }
1184
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001185 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001186}
1187
1188int BufferQueueProducer::query(int what, int *outValue) {
1189 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001190 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001191
Yi Kong48a619f2018-06-05 16:34:59 -07001192 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001193 BQ_LOGE("query: outValue was NULL");
1194 return BAD_VALUE;
1195 }
1196
1197 if (mCore->mIsAbandoned) {
1198 BQ_LOGE("query: BufferQueue has been abandoned");
1199 return NO_INIT;
1200 }
1201
1202 int value;
1203 switch (what) {
1204 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001205 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001206 break;
1207 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001208 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001209 break;
1210 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001211 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001212 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001213 case NATIVE_WINDOW_LAYER_COUNT:
1214 // All BufferQueue buffers have a single layer.
1215 value = BQ_LAYER_COUNT;
1216 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001217 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001218 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001219 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001220 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001221 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001222 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001223 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1224 value = (mCore->mQueue.size() > 1);
1225 break;
1226 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001227 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001228 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001229 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001230 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1231 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1232 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001233 case NATIVE_WINDOW_BUFFER_AGE:
1234 if (mCore->mBufferAge > INT32_MAX) {
1235 value = 0;
1236 } else {
1237 value = static_cast<int32_t>(mCore->mBufferAge);
1238 }
1239 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001240 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1241 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1242 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001243 default:
1244 return BAD_VALUE;
1245 }
1246
1247 BQ_LOGV("query: %d? %d", what, value);
1248 *outValue = value;
1249 return NO_ERROR;
1250}
1251
Dan Stozaf0eaf252014-03-21 13:05:51 -07001252status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001253 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1254 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001255 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001256 mConsumerName = mCore->mConsumerName;
1257 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1258 producerControlledByApp ? "true" : "false");
1259
1260 if (mCore->mIsAbandoned) {
1261 BQ_LOGE("connect: BufferQueue has been abandoned");
1262 return NO_INIT;
1263 }
1264
Yi Kong48a619f2018-06-05 16:34:59 -07001265 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001266 BQ_LOGE("connect: BufferQueue has no consumer");
1267 return NO_INIT;
1268 }
1269
Yi Kong48a619f2018-06-05 16:34:59 -07001270 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001271 BQ_LOGE("connect: output was NULL");
1272 return BAD_VALUE;
1273 }
1274
1275 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1276 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1277 mCore->mConnectedApi, api);
1278 return BAD_VALUE;
1279 }
1280
1281 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1282 mDequeueTimeout < 0 ?
1283 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1284 mCore->mMaxBufferCount) -
1285 mCore->getMaxBufferCountLocked();
1286 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1287 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1288 "slots. Delta = %d", delta);
1289 return BAD_VALUE;
1290 }
1291
Dan Stoza289ade12014-02-28 11:17:17 -08001292 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001293 switch (api) {
1294 case NATIVE_WINDOW_API_EGL:
1295 case NATIVE_WINDOW_API_CPU:
1296 case NATIVE_WINDOW_API_MEDIA:
1297 case NATIVE_WINDOW_API_CAMERA:
1298 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001299
1300 output->width = mCore->mDefaultWidth;
1301 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001302 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001303 output->numPendingBuffers =
1304 static_cast<uint32_t>(mCore->mQueue.size());
1305 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001306 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001307 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001308
Yi Kong48a619f2018-06-05 16:34:59 -07001309 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001310 // Set up a death notification so that we can disconnect
1311 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001312#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001313 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001314 status = IInterface::asBinder(listener)->linkToDeath(
1315 static_cast<IBinder::DeathRecipient*>(this));
1316 if (status != NO_ERROR) {
1317 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1318 strerror(-status), status);
1319 }
1320 mCore->mLinkedToDeath = listener;
1321 }
Chong Zhang62493092020-01-15 16:04:47 -08001322#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001323 mCore->mConnectedProducerListener = listener;
1324 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001325 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001326 break;
1327 default:
1328 BQ_LOGE("connect: unknown API %d", api);
1329 status = BAD_VALUE;
1330 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001331 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001332 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001333 mCore->mBufferHasBeenQueued = false;
1334 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001335 mCore->mQueueBufferCanDrop = false;
1336 mCore->mLegacyBufferDrop = true;
1337 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1338 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1339 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001340 }
Dan Stoza289ade12014-02-28 11:17:17 -08001341
Pablo Ceballos981066c2016-02-18 12:54:37 -08001342 mCore->mAllowAllocation = true;
1343 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001344 return status;
1345}
1346
Robert Carr97b9c862016-09-08 13:54:35 -07001347status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001348 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001349 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001350
1351 int status = NO_ERROR;
1352 sp<IConsumerListener> listener;
1353 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001354 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001355
1356 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001357 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001358 return NO_ERROR;
1359 }
1360 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1361 }
1362
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001363 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001364
1365 if (mCore->mIsAbandoned) {
1366 // It's not really an error to disconnect after the surface has
1367 // been abandoned; it should just be a no-op.
1368 return NO_ERROR;
1369 }
1370
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001371 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001372 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1373 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1374 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001375 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001376 // If we're asked to disconnect the currently connected api but
1377 // nobody is connected, it's not really an error.
1378 if (api == BufferQueueCore::NO_CONNECTED_API) {
1379 return NO_ERROR;
1380 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001381 }
1382
Dan Stoza289ade12014-02-28 11:17:17 -08001383 switch (api) {
1384 case NATIVE_WINDOW_API_EGL:
1385 case NATIVE_WINDOW_API_CPU:
1386 case NATIVE_WINDOW_API_MEDIA:
1387 case NATIVE_WINDOW_API_CAMERA:
1388 if (mCore->mConnectedApi == api) {
1389 mCore->freeAllBuffersLocked();
1390
Chong Zhang62493092020-01-15 16:04:47 -08001391#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001392 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001393 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001394 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001395 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001396 // This can fail if we're here because of the death
1397 // notification, but we just ignore it
1398 token->unlinkToDeath(
1399 static_cast<IBinder::DeathRecipient*>(this));
1400 }
Chong Zhang62493092020-01-15 16:04:47 -08001401#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001402 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001403 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001404 mCore->mLinkedToDeath = nullptr;
1405 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001406 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001407 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001408 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001409 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001410 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001411 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001412 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1413 BQ_LOGE("disconnect: not connected (req=%d)", api);
1414 status = NO_INIT;
1415 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001416 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001417 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001418 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001419 }
1420 break;
1421 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001422 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001423 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001424 break;
1425 }
1426 } // Autolock scope
1427
1428 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001429 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001430 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001431 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001432 }
1433
1434 return status;
1435}
1436
Jesse Hall399184a2014-03-03 15:42:54 -08001437status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001438 sp<IConsumerListener> listener;
1439 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001440 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001441 mCore->mSidebandStream = stream;
1442 listener = mCore->mConsumerListener;
1443 } // Autolock scope
1444
Yi Kong48a619f2018-06-05 16:34:59 -07001445 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001446 listener->onSidebandStreamChanged();
1447 }
Jesse Hall399184a2014-03-03 15:42:54 -08001448 return NO_ERROR;
1449}
1450
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001451void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001452 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001453 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001454
1455 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001456 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001457 size_t newBufferCount = 0;
1458 uint32_t allocWidth = 0;
1459 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001460 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001461 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001462 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001463 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001464 std::unique_lock<std::mutex> lock(mCore->mMutex);
1465 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001466
Dan Stoza9de72932015-04-16 17:28:43 -07001467 if (!mCore->mAllowAllocation) {
1468 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1469 "BufferQueue");
1470 return;
1471 }
1472
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001473 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1474 // both allocateBuffers and dequeueBuffer.
1475 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001476 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001477 return;
1478 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001479
Antoine Labour78014f32014-07-15 21:17:03 -07001480 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1481 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001482 if (useDefaultSize && mCore->mAutoPrerotation &&
1483 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1484 std::swap(allocWidth, allocHeight);
1485 }
1486
Antoine Labour78014f32014-07-15 21:17:03 -07001487 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1488 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001489 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001490
1491 mCore->mIsAllocating = true;
1492 } // Autolock scope
1493
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001494 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001495 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001496 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001497 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001498 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001499
1500 status_t result = graphicBuffer->initCheck();
1501
Antoine Labour78014f32014-07-15 21:17:03 -07001502 if (result != NO_ERROR) {
1503 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001504 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001505 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001506 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001507 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001508 return;
1509 }
1510 buffers.push_back(graphicBuffer);
1511 }
1512
1513 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001514 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001515 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1516 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001517 if (useDefaultSize && mCore->mAutoPrerotation &&
1518 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1519 std::swap(checkWidth, checkHeight);
1520 }
1521
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001522 PixelFormat checkFormat = format != 0 ?
1523 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001524 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001525 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1526 checkFormat != allocFormat || checkUsage != allocUsage) {
1527 // Something changed while we released the lock. Retry.
1528 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1529 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001530 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001531 continue;
1532 }
1533
Antoine Labour78014f32014-07-15 21:17:03 -07001534 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001535 if (mCore->mFreeSlots.empty()) {
1536 BQ_LOGV("allocateBuffers: a slot was occupied while "
1537 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001538 continue;
1539 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001540 auto slot = mCore->mFreeSlots.begin();
1541 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1542 mSlots[*slot].mGraphicBuffer = buffers[i];
1543 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001544
1545 // freeBufferLocked puts this slot on the free slots list. Since
1546 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001547 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001548
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001549 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1550 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001551
1552 // Make sure the erase is done after all uses of the slot
1553 // iterator since it will be invalid after this point.
1554 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001555 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001556
Antoine Labour78014f32014-07-15 21:17:03 -07001557 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001558 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001559 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001560
1561 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1562 // waiting anymore so it can use the buffer we just allocated.
1563 while (mDequeueWaitingForAllocation) {
1564 mDequeueWaitingForAllocationCondition.wait(lock);
1565 }
Antoine Labour78014f32014-07-15 21:17:03 -07001566 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001567 }
1568}
1569
Dan Stoza9de72932015-04-16 17:28:43 -07001570status_t BufferQueueProducer::allowAllocation(bool allow) {
1571 ATRACE_CALL();
1572 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1573
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001574 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001575 mCore->mAllowAllocation = allow;
1576 return NO_ERROR;
1577}
1578
Dan Stoza812ed062015-06-02 15:45:22 -07001579status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1580 ATRACE_CALL();
1581 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1582
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001583 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001584 mCore->mGenerationNumber = generationNumber;
1585 return NO_ERROR;
1586}
1587
Dan Stozac6f30bd2015-06-08 09:32:50 -07001588String8 BufferQueueProducer::getConsumerName() const {
1589 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001590 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001591 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1592 return mConsumerName;
1593}
1594
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001595status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001596 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001597 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001598
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001599 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001600 if (!sharedBufferMode) {
1601 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001602 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001603 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001604 return NO_ERROR;
1605}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001606
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001607status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1608 ATRACE_CALL();
1609 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1610
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001611 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001612
1613 mCore->mAutoRefresh = autoRefresh;
1614 return NO_ERROR;
1615}
1616
Dan Stoza127fc632015-06-30 13:43:32 -07001617status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1618 ATRACE_CALL();
1619 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1620
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001621 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001622 bool dequeueBufferCannotBlock =
1623 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1624 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001625 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1626 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1627 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1628 "available slots. Delta = %d", delta);
1629 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001630 }
1631
Pablo Ceballos981066c2016-02-18 12:54:37 -08001632 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001633 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1634 if (timeout > 0) {
1635 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001636 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001637
Pablo Ceballos981066c2016-02-18 12:54:37 -08001638 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001639 return NO_ERROR;
1640}
1641
Sungtak Lee3249fb62019-03-02 16:40:47 -08001642status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1643 ATRACE_CALL();
1644 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1645
1646 std::lock_guard<std::mutex> lock(mCore->mMutex);
1647 mCore->mLegacyBufferDrop = drop;
1648 return NO_ERROR;
1649}
1650
Dan Stoza50101d02016-04-07 16:53:23 -07001651status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001652 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001653 ATRACE_CALL();
1654 BQ_LOGV("getLastQueuedBuffer");
1655
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001656 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001657 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1658 *outBuffer = nullptr;
1659 *outFence = Fence::NO_FENCE;
1660 return NO_ERROR;
1661 }
1662
1663 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1664 *outFence = mLastQueueBufferFence;
1665
John Reck1a61da52016-04-28 13:18:15 -07001666 // Currently only SurfaceFlinger internally ever changes
1667 // GLConsumer's filtering mode, so we just use 'true' here as
1668 // this is slightly specialized for the current client of this API,
1669 // which does want filtering.
1670 GLConsumer::computeTransformMatrix(outTransformMatrix,
1671 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1672 mLastQueuedTransform, true /* filter */);
1673
Dan Stoza50101d02016-04-07 16:53:23 -07001674 return NO_ERROR;
1675}
1676
John Reckf0f13e82021-05-18 00:42:56 -04001677status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1678 Rect* outRect, uint32_t* outTransform) {
1679 ATRACE_CALL();
1680 BQ_LOGV("getLastQueuedBuffer");
1681
1682 std::lock_guard<std::mutex> lock(mCore->mMutex);
1683 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1684 *outBuffer = nullptr;
1685 *outFence = Fence::NO_FENCE;
1686 return NO_ERROR;
1687 }
1688
1689 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1690 *outFence = mLastQueueBufferFence;
1691 *outRect = mLastQueuedCrop;
1692 *outTransform = mLastQueuedTransform;
1693
1694 return NO_ERROR;
1695}
1696
Brian Anderson3890c392016-07-25 12:48:08 -07001697void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1698 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001699}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001700
Brian Anderson3890c392016-07-25 12:48:08 -07001701void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001702 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001703 FrameEventHistoryDelta* outDelta) {
1704 if (newTimestamps == nullptr && outDelta == nullptr) {
1705 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001706 }
1707
1708 ATRACE_CALL();
1709 BQ_LOGV("addAndGetFrameTimestamps");
1710 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001711 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001712 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001713 listener = mCore->mConsumerListener;
1714 }
Yi Kong48a619f2018-06-05 16:34:59 -07001715 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001716 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001717 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001718}
1719
Dan Stoza289ade12014-02-28 11:17:17 -08001720void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1721 // If we're here, it means that a producer we were connected to died.
1722 // We're guaranteed that we are still connected to it because we remove
1723 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1724 // without synchronization here.
1725 int api = mCore->mConnectedApi;
1726 disconnect(api);
1727}
1728
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001729status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1730 BQ_LOGV("getUniqueId");
1731
1732 *outId = mCore->mUniqueId;
1733 return NO_ERROR;
1734}
1735
Chia-I Wue2786ea2017-08-07 10:36:08 -07001736status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1737 BQ_LOGV("getConsumerUsage");
1738
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001739 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001740 *outUsage = mCore->mConsumerUsageBits;
1741 return NO_ERROR;
1742}
1743
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001744status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1745 ATRACE_CALL();
1746 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1747
1748 std::lock_guard<std::mutex> lock(mCore->mMutex);
1749
1750 mCore->mAutoPrerotation = autoPrerotation;
1751 return NO_ERROR;
1752}
1753
Dan Stoza289ade12014-02-28 11:17:17 -08001754} // namespace android