blob: 69345a971eb5f7e893b83d67a67bb62c8e81ffc7 [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>
Ady Abraham107788e2023-10-17 12:31:08 -070035
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070036#include <gui/FrameRateUtils.h>
John Reck1a61da52016-04-28 13:18:15 -070037#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070039#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070040#include <gui/TraceUtils.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080041#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080042
43#include <utils/Log.h>
44#include <utils/Trace.h>
45
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070046#include <system/window.h>
47
Dan Stoza289ade12014-02-28 11:17:17 -080048namespace android {
49
Iris Chang430193f2019-12-04 16:25:46 +080050// Macros for include BufferQueueCore information in log messages
51#define BQ_LOGV(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000052 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080053 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
54 ##__VA_ARGS__)
55#define BQ_LOGD(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000056 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080057 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
58 ##__VA_ARGS__)
59#define BQ_LOGI(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000060 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080061 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
62 ##__VA_ARGS__)
63#define BQ_LOGW(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000064 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080065 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
66 ##__VA_ARGS__)
67#define BQ_LOGE(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000068 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080069 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
70 ##__VA_ARGS__)
71
Craig Donner6ebc46a2016-10-21 15:23:44 -070072static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080073ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070074
Irvel468051e2016-06-13 16:44:44 -070075BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
76 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080077 mCore(core),
78 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070079 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070080 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070081 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080082 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070083 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080084 mCallbackMutex(),
85 mNextCallbackTicket(0),
86 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070087 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010088 mDequeueTimeout(-1),
89 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080090
91BufferQueueProducer::~BufferQueueProducer() {}
92
93status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
94 ATRACE_CALL();
95 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020096 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080097
98 if (mCore->mIsAbandoned) {
99 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
100 return NO_INIT;
101 }
102
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700103 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
104 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
105 return NO_INIT;
106 }
107
Dan Stoza3e96f192014-03-03 10:16:19 -0800108 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800109 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800110 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800111 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800113 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700114 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800115 return BAD_VALUE;
116 }
117
118 mSlots[slot].mRequestBufferCalled = true;
119 *buf = mSlots[slot].mGraphicBuffer;
120 return NO_ERROR;
121}
122
Pablo Ceballosfa455352015-08-12 17:47:47 -0700123status_t BufferQueueProducer::setMaxDequeuedBufferCount(
124 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700125 int maxBufferCount;
126 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
127}
128
129status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
130 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700131 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
133 maxDequeuedBuffers);
134
Pablo Ceballos981066c2016-02-18 12:54:37 -0800135 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700136 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200137 std::unique_lock<std::mutex> lock(mCore->mMutex);
138 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700139
140 if (mCore->mIsAbandoned) {
141 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
142 "abandoned");
143 return NO_INIT;
144 }
145
Brian Lindahlc794b692023-01-31 15:42:47 -0700146 *maxBufferCount = mCore->getMaxBufferCountLocked();
147
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700148 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
149 return NO_ERROR;
150 }
151
Pablo Ceballos72daab62015-12-07 16:38:43 -0800152 // The new maxDequeuedBuffer count should not be violated by the number
153 // of currently dequeued buffers
154 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800155 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700158 }
159 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800160 if (dequeuedCount > maxDequeuedBuffers) {
161 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
162 "count (%d) exceeds the current dequeued buffer count (%d)",
163 maxDequeuedBuffers, dequeuedCount);
164 return BAD_VALUE;
165 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700167 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700168 bufferCount += maxDequeuedBuffers;
169
170 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
171 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
172 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
173 return BAD_VALUE;
174 }
175
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700176 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700177 if (bufferCount < minBufferSlots) {
178 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
179 "less than minimum %d", bufferCount, minBufferSlots);
180 return BAD_VALUE;
181 }
182
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700183 if (bufferCount > mCore->mMaxBufferCount) {
184 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700185 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
186 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
187 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
188 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700189 return BAD_VALUE;
190 }
191
Pablo Ceballos72daab62015-12-07 16:38:43 -0800192 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800193 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800194 return BAD_VALUE;
195 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700196 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700197 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800198 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800199 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800200 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800201 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200202 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700203 } // Autolock scope
204
205 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700206 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800207 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700208 }
209
210 return NO_ERROR;
211}
212
213status_t BufferQueueProducer::setAsyncMode(bool async) {
214 ATRACE_CALL();
215 BQ_LOGV("setAsyncMode: async = %d", async);
216
Pablo Ceballos981066c2016-02-18 12:54:37 -0800217 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200219 std::unique_lock<std::mutex> lock(mCore->mMutex);
220 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700221
222 if (mCore->mIsAbandoned) {
223 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
224 return NO_INIT;
225 }
226
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700227 if (async == mCore->mAsyncMode) {
228 return NO_ERROR;
229 }
230
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700231 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700232 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
233 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700234 BQ_LOGE("setAsyncMode(%d): this call would cause the "
235 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700236 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
237 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
238 mCore->mMaxDequeuedBufferCount,
239 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700240 return BAD_VALUE;
241 }
242
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800243 int delta = mCore->getMaxBufferCountLocked(async,
244 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
245 - mCore->getMaxBufferCountLocked();
246
Pablo Ceballos981066c2016-02-18 12:54:37 -0800247 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800248 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
249 "available slots. Delta = %d", delta);
250 return BAD_VALUE;
251 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700252 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800253 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200254 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700255 if (delta < 0) {
256 listener = mCore->mConsumerListener;
257 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700258 } // Autolock scope
259
260 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700261 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800262 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700263 }
264 return NO_ERROR;
265}
266
Dan Stoza5ecfb682016-01-04 17:01:02 -0800267int BufferQueueProducer::getFreeBufferLocked() const {
268 if (mCore->mFreeBuffers.empty()) {
269 return BufferQueueCore::INVALID_BUFFER_SLOT;
270 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800271 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800272 mCore->mFreeBuffers.pop_front();
273 return slot;
274}
275
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800276int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800277 if (mCore->mFreeSlots.empty()) {
278 return BufferQueueCore::INVALID_BUFFER_SLOT;
279 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800280 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800281 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800282 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800283}
284
285status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200286 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800287 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
288 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800289 bool tryAgain = true;
290 while (tryAgain) {
291 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 return NO_INIT;
294 }
295
Dan Stoza9f3053d2014-03-06 15:14:33 -0800296 int dequeuedCount = 0;
297 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800298 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700299 if (mSlots[s].mBufferState.isDequeued()) {
300 ++dequeuedCount;
301 }
302 if (mSlots[s].mBufferState.isAcquired()) {
303 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800304 }
305 }
306
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700307 // Producers are not allowed to dequeue more than
308 // mMaxDequeuedBufferCount buffers.
309 // This check is only done if a buffer has already been queued
310 if (mCore->mBufferHasBeenQueued &&
311 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700312 // Supress error logs when timeout is non-negative.
313 if (mDequeueTimeout < 0) {
314 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
315 "count (%d)", callerString,
316 mCore->mMaxDequeuedBufferCount);
317 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318 return INVALID_OPERATION;
319 }
320
Dan Stoza0de7ea72015-04-23 13:20:51 -0700321 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
322
Dan Stozaae3c3682014-04-18 15:43:35 -0700323 // If we disconnect and reconnect quickly, we can be in a state where
324 // our slots are empty but we have many buffers in the queue. This can
325 // cause us to run out of memory if we outrun the consumer. Wait here if
326 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800327 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700328 bool tooManyBuffers = mCore->mQueue.size()
329 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700330 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800331 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700332 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700333 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700335 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700336 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700337 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700338 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800339 } else {
340 if (caller == FreeSlotCaller::Dequeue) {
341 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800342 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800343 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
344 *found = slot;
345 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800346 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800347 }
348 } else {
349 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800350 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800351 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
352 *found = slot;
353 } else {
354 *found = getFreeBufferLocked();
355 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700356 }
357 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700358 }
359
360 // If no buffer is found, or if the queue has too many buffers
361 // outstanding, wait for a buffer to be acquired or released, or for the
362 // max buffer count to change.
363 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
364 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365 if (tryAgain) {
366 // Return an error if we're in non-blocking mode (producer and
367 // consumer are controlled by the application).
368 // However, the consumer is allowed to briefly acquire an extra
369 // buffer (which could cause us to have to wait here), which is
370 // okay, since it is only used to implement an atomic acquire +
371 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700372 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
374 return WOULD_BLOCK;
375 }
Dan Stoza127fc632015-06-30 13:43:32 -0700376 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200377 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
378 std::chrono::nanoseconds(mDequeueTimeout));
379 if (result == std::cv_status::timeout) {
380 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700381 }
382 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200383 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700384 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 }
386 } // while (tryAgain)
387
388 return NO_ERROR;
389}
390
Ian Elliottd11b0442017-07-18 11:05:49 -0600391status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
392 uint32_t width, uint32_t height, PixelFormat format,
393 uint64_t usage, uint64_t* outBufferAge,
394 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800395 ATRACE_CALL();
396 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200397 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800398 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700399
400 if (mCore->mIsAbandoned) {
401 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
402 return NO_INIT;
403 }
404
405 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
406 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
407 return NO_INIT;
408 }
Dan Stoza289ade12014-02-28 11:17:17 -0800409 } // Autolock scope
410
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700411 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800412
413 if ((width && !height) || (!width && height)) {
414 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
415 return BAD_VALUE;
416 }
417
418 status_t returnFlags = NO_ERROR;
419 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
420 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800421 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800422
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000423 sp<IConsumerListener> listener;
424 bool callOnFrameDequeued = false;
425 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400426#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
427 std::vector<gui::AdditionalOptions> allocOptions;
428 uint32_t allocOptionsGenId = 0;
429#endif
430
Dan Stoza289ade12014-02-28 11:17:17 -0800431 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200432 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800433
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100434 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
435 // is finished such that we don't allocate in parallel.
436 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
437 mDequeueWaitingForAllocation = true;
438 mCore->waitWhileAllocatingLocked(lock);
439 mDequeueWaitingForAllocation = false;
440 mDequeueWaitingForAllocationCondition.notify_all();
441 }
Dan Stoza289ade12014-02-28 11:17:17 -0800442
443 if (format == 0) {
444 format = mCore->mDefaultBufferFormat;
445 }
446
447 // Enable the usage bits the consumer requested
448 usage |= mCore->mConsumerUsageBits;
449
Dan Stoza9de72932015-04-16 17:28:43 -0700450 const bool useDefaultSize = !width && !height;
451 if (useDefaultSize) {
452 width = mCore->mDefaultWidth;
453 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700454 if (mCore->mAutoPrerotation &&
455 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
456 std::swap(width, height);
457 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458 }
Dan Stoza289ade12014-02-28 11:17:17 -0800459
Pablo Ceballos981066c2016-02-18 12:54:37 -0800460 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700461 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200462 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700463 if (status != NO_ERROR) {
464 return status;
465 }
466
467 // This should not happen
468 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
469 BQ_LOGE("dequeueBuffer: no available buffer slots");
470 return -EBUSY;
471 }
472
473 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
474
475 // If we are not allowed to allocate new buffers,
476 // waitForFreeSlotThenRelock must have returned a slot containing a
477 // buffer. If this buffer would require reallocation to meet the
478 // requested attributes, we free it and attempt to get another one.
479 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700480 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700481 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700482 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700483 return BAD_VALUE;
484 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800485 mCore->mFreeSlots.insert(found);
486 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700487 found = BufferItem::INVALID_BUFFER_SLOT;
488 continue;
489 }
490 }
Dan Stoza289ade12014-02-28 11:17:17 -0800491 }
492
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800493 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800494
John Reckdb164ff2024-04-03 16:59:28 -0400495 bool needsReallocation = buffer == nullptr ||
496 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
497
498#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
499 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
500 mCore->mAdditionalOptionsGenerationId;
501#endif
502
503 if (mCore->mSharedBufferSlot == found && needsReallocation) {
504 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800505 return BAD_VALUE;
506 }
507
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700508 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800509 mCore->mActiveBuffers.insert(found);
510 }
Dan Stoza289ade12014-02-28 11:17:17 -0800511 *outSlot = found;
512 ATRACE_BUFFER_INDEX(found);
513
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800514 attachedByConsumer = mSlots[found].mNeedsReallocation;
515 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800516
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700517 mSlots[found].mBufferState.dequeue();
518
John Reckdb164ff2024-04-03 16:59:28 -0400519 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700520 if (CC_UNLIKELY(ATRACE_ENABLED())) {
521 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000522 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700523 } else {
524 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
525 "layerCount:%d "
526 "usage:%d requested: %dx%d format:%d layerCount:%d "
527 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000528 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700529 BQ_LAYER_COUNT, usage, buffer->getWidth(),
530 buffer->getHeight(), buffer->getPixelFormat(),
531 buffer->getLayerCount(), buffer->getUsage());
532 }
533 }
Dan Stoza289ade12014-02-28 11:17:17 -0800534 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700535 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800536 mSlots[found].mRequestBufferCalled = false;
537 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
538 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
539 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800540 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700541 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400542#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
543 allocOptions = mCore->mAdditionalOptions;
544 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
545#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800546
547 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800548 } else {
549 // We add 1 because that will be the frame number when this buffer
550 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700551 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800552 }
553
Dan Stoza800b41a2015-04-28 14:20:04 -0700554 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
555 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800556
Yi Kong48a619f2018-06-05 16:34:59 -0700557 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800558 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
559 "slot=%d w=%d h=%d format=%u",
560 found, buffer->width, buffer->height, buffer->format);
561 }
562
563 eglDisplay = mSlots[found].mEglDisplay;
564 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700565 // Don't return a fence in shared buffer mode, except for the first
566 // frame.
567 *outFence = (mCore->mSharedBufferMode &&
568 mCore->mSharedBufferSlot == found) ?
569 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800570 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
571 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700572
573 // If shared buffer mode has just been enabled, cache the slot of the
574 // first buffer that is dequeued and mark it as the shared buffer.
575 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
576 BufferQueueCore::INVALID_BUFFER_SLOT) {
577 mCore->mSharedBufferSlot = found;
578 mSlots[found].mBufferState.mShared = true;
579 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700580
581 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000582 callOnFrameDequeued = true;
583 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700584 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000585
586 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800587 } // Autolock scope
588
589 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700590 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400591
592#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
593 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
594 tempOptions.reserve(allocOptions.size());
595 for (const auto& it : allocOptions) {
596 tempOptions.emplace_back(it.name.c_str(), it.value);
597 }
598 const GraphicBufferAllocator::AllocationRequest allocRequest = {
599 .importBuffer = true,
600 .width = width,
601 .height = height,
602 .format = format,
603 .layerCount = BQ_LAYER_COUNT,
604 .usage = usage,
605 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
606 .extras = std::move(tempOptions),
607 };
608 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
609#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000610 sp<GraphicBuffer> graphicBuffer =
611 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
612 {mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400613#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700614
615 status_t error = graphicBuffer->initCheck();
616
Dan Stoza289ade12014-02-28 11:17:17 -0800617 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200618 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800619
Chia-I Wufeec3b12017-05-25 09:34:56 -0700620 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700621 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
622 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400623#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
624 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
625#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000626 callOnFrameDequeued = true;
627 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700628 }
629
630 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200631 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700632
Chia-I Wufeec3b12017-05-25 09:34:56 -0700633 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700634 mCore->mFreeSlots.insert(*outSlot);
635 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700636 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
637 return error;
638 }
639
Dan Stoza289ade12014-02-28 11:17:17 -0800640 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700641 mCore->mFreeSlots.insert(*outSlot);
642 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800643 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
644 return NO_INIT;
645 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800646
Pablo Ceballos9e314332016-01-12 13:49:19 -0800647 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800648 } // Autolock scope
649 }
650
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000651 if (listener != nullptr && callOnFrameDequeued) {
652 listener->onFrameDequeued(bufferId);
653 }
654
Dan Stoza9f3053d2014-03-06 15:14:33 -0800655 if (attachedByConsumer) {
656 returnFlags |= BUFFER_NEEDS_REALLOCATION;
657 }
658
Dan Stoza289ade12014-02-28 11:17:17 -0800659 if (eglFence != EGL_NO_SYNC_KHR) {
660 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
661 1000000000);
662 // If something goes wrong, log the error, but return the buffer without
663 // synchronizing access to it. It's too late at this point to abort the
664 // dequeue operation.
665 if (result == EGL_FALSE) {
666 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
667 eglGetError());
668 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
669 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
670 }
671 eglDestroySyncKHR(eglDisplay, eglFence);
672 }
673
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700674 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
675 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800676 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800677 mSlots[*outSlot].mGraphicBuffer != nullptr ?
678 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800679
Ian Elliottd11b0442017-07-18 11:05:49 -0600680 if (outBufferAge) {
681 *outBufferAge = mCore->mBufferAge;
682 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700683 addAndGetFrameTimestamps(nullptr, outTimestamps);
684
Dan Stoza289ade12014-02-28 11:17:17 -0800685 return returnFlags;
686}
687
Dan Stoza9f3053d2014-03-06 15:14:33 -0800688status_t BufferQueueProducer::detachBuffer(int slot) {
689 ATRACE_CALL();
690 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700691 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800692
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700693 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000694 bool callOnFrameDetached = false;
695 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700696 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200697 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700698
699 if (mCore->mIsAbandoned) {
700 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
701 return NO_INIT;
702 }
703
704 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
705 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
706 return NO_INIT;
707 }
708
709 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
710 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
711 return BAD_VALUE;
712 }
713
714 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
715 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
716 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
717 return BAD_VALUE;
718 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000719 // TODO(http://b/140581935): This message is BQ_LOGW because it
720 // often logs when no actionable errors are present. Return to
721 // using BQ_LOGE after ensuring this only logs during errors.
722 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700723 "(state = %s)", slot, mSlots[slot].mBufferState.string());
724 return BAD_VALUE;
725 } else if (!mSlots[slot].mRequestBufferCalled) {
726 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
727 slot);
728 return BAD_VALUE;
729 }
730
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700731 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700732 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000733 if (gb != nullptr) {
734 callOnFrameDetached = true;
735 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700736 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700737 mSlots[slot].mBufferState.detachProducer();
738 mCore->mActiveBuffers.erase(slot);
739 mCore->mFreeSlots.insert(slot);
740 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200741 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700742 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800743 }
744
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000745 if (listener != nullptr && callOnFrameDetached) {
746 listener->onFrameDetached(bufferId);
747 }
748
Yi Kong48a619f2018-06-05 16:34:59 -0700749 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700750 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700751 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800752
Dan Stoza9f3053d2014-03-06 15:14:33 -0800753 return NO_ERROR;
754}
755
Dan Stozad9822a32014-03-28 15:25:31 -0700756status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
757 sp<Fence>* outFence) {
758 ATRACE_CALL();
759
Yi Kong48a619f2018-06-05 16:34:59 -0700760 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700761 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
762 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700763 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700764 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
765 return BAD_VALUE;
766 }
767
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700768 sp<IConsumerListener> listener;
769 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200770 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700771
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700772 if (mCore->mIsAbandoned) {
773 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
774 return NO_INIT;
775 }
776
777 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
778 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
779 return NO_INIT;
780 }
781
782 if (mCore->mSharedBufferMode) {
783 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
784 "mode");
785 return BAD_VALUE;
786 }
787
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200788 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700789
790 if (mCore->mFreeBuffers.empty()) {
791 return NO_MEMORY;
792 }
793
794 int found = mCore->mFreeBuffers.front();
795 mCore->mFreeBuffers.remove(found);
796 mCore->mFreeSlots.insert(found);
797
798 BQ_LOGV("detachNextBuffer detached slot %d", found);
799
800 *outBuffer = mSlots[found].mGraphicBuffer;
801 *outFence = mSlots[found].mFence;
802 mCore->clearBufferSlotLocked(found);
803 VALIDATE_CONSISTENCY();
804 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700805 }
806
Yi Kong48a619f2018-06-05 16:34:59 -0700807 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700808 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700809 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800810
Dan Stozad9822a32014-03-28 15:25:31 -0700811 return NO_ERROR;
812}
813
Dan Stoza9f3053d2014-03-06 15:14:33 -0800814status_t BufferQueueProducer::attachBuffer(int* outSlot,
815 const sp<android::GraphicBuffer>& buffer) {
816 ATRACE_CALL();
817
Yi Kong48a619f2018-06-05 16:34:59 -0700818 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700819 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800820 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700821 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700822 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800823 return BAD_VALUE;
824 }
825
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200826 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700827
828 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700829 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700830 return NO_INIT;
831 }
832
833 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700834 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700835 return NO_INIT;
836 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800837
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700838 if (mCore->mSharedBufferMode) {
839 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700840 return BAD_VALUE;
841 }
842
Dan Stoza812ed062015-06-02 15:45:22 -0700843 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
844 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
845 "[queue %u]", buffer->getGenerationNumber(),
846 mCore->mGenerationNumber);
847 return BAD_VALUE;
848 }
849
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200850 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700851
Dan Stoza9f3053d2014-03-06 15:14:33 -0800852 status_t returnFlags = NO_ERROR;
853 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200854 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800855 if (status != NO_ERROR) {
856 return status;
857 }
858
859 // This should not happen
860 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700861 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800862 return -EBUSY;
863 }
864
865 *outSlot = found;
866 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700867 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800868 *outSlot, returnFlags);
869
870 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800872 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
873 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700874 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800875 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800876 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800877 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800878 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700879
Dan Stoza9f3053d2014-03-06 15:14:33 -0800880 return returnFlags;
881}
882
Dan Stoza289ade12014-02-28 11:17:17 -0800883status_t BufferQueueProducer::queueBuffer(int slot,
884 const QueueBufferInput &input, QueueBufferOutput *output) {
885 ATRACE_CALL();
886 ATRACE_BUFFER_INDEX(slot);
887
Brian Andersond6927fb2016-07-23 23:37:30 -0700888 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800889 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800890 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700891 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800892 int scalingMode;
893 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700894 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700895 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700896 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700897 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700898 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
899 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700900 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700901 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800902
Yi Kong48a619f2018-06-05 16:34:59 -0700903 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800904 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800905 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800906 }
907
Brian Anderson3d4039d2016-09-23 16:31:30 -0700908 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
909
Dan Stoza289ade12014-02-28 11:17:17 -0800910 switch (scalingMode) {
911 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
912 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
913 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
914 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
915 break;
916 default:
917 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800918 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800919 }
920
Dan Stoza8dc55392014-11-04 11:37:46 -0800921 sp<IConsumerListener> frameAvailableListener;
922 sp<IConsumerListener> frameReplacedListener;
923 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700924 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800925 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500926 int connectedApi;
927 sp<Fence> lastQueuedFence;
928
Dan Stoza289ade12014-02-28 11:17:17 -0800929 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200930 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800931
932 if (mCore->mIsAbandoned) {
933 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
934 return NO_INIT;
935 }
936
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700937 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
938 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
939 return NO_INIT;
940 }
941
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800942 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800943 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800944 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800945 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700946 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800947 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700948 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800949 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800950 } else if (!mSlots[slot].mRequestBufferCalled) {
951 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
952 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800953 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800954 }
955
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700956 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700957 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700958 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700959 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700960 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700961 mSlots[slot].mBufferState.mShared = true;
962 }
963
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800964 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700965 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
966 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
967 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700968 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800969 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800970
971 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
972 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700973 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800974 crop.intersect(bufferRect, &croppedRect);
975 if (croppedRect != crop) {
976 BQ_LOGE("queueBuffer: crop rect is not contained within the "
977 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800978 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800979 }
980
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800981 // Override UNKNOWN dataspace with consumer default
982 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
983 dataSpace = mCore->mDefaultBufferDataSpace;
984 }
985
Brian Andersond6927fb2016-07-23 23:37:30 -0700986 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700987 mSlots[slot].mBufferState.queue();
988
Brian Andersond6927fb2016-07-23 23:37:30 -0700989 // Increment the frame counter and store a local version of it
990 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800991 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700992 currentFrameNumber = mCore->mFrameCounter;
993 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800994
Dan Stoza289ade12014-02-28 11:17:17 -0800995 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
996 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
997 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800998 item.mTransform = transform &
999 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001000 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1002 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001003 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001004 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001005 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001006 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -07001007 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001008 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001009 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001010 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001011 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001012 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001013 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001014 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001015 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001016 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001017 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001018 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001019
Ruben Brunk1681d952014-06-27 15:51:55 -07001020 mStickyTransform = stickyTransform;
1021
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001022 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001023 if (mCore->mSharedBufferMode) {
1024 mCore->mSharedBufferCache.crop = crop;
1025 mCore->mSharedBufferCache.transform = transform;
1026 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001027 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001028 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001029 }
1030
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001031 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001032 if (mCore->mQueue.empty()) {
1033 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1034 // and simply queue this buffer
1035 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001036 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001037 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001038 // When the queue is not empty, we need to look at the last buffer
1039 // in the queue to see if we need to replace it
1040 const BufferItem& last = mCore->mQueue.itemAt(
1041 mCore->mQueue.size() - 1);
1042 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001043
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001044 if (!last.mIsStale) {
1045 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001046
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001047 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001048 // still be around. Mark it as no longer shared if this
1049 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001050 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001051 mSlots[last.mSlot].mBufferState.isFree()) {
1052 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001053 }
1054 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001055 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1056 mCore->mActiveBuffers.erase(last.mSlot);
1057 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001058 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001059 }
Dan Stoza289ade12014-02-28 11:17:17 -08001060 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001061
Steven Thomas862e7532019-07-10 19:21:03 -07001062 // Make sure to merge the damage rect from the frame we're about
1063 // to drop into the new frame's damage rect.
1064 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1065 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1066 item.mSurfaceDamage = Region::INVALID_REGION;
1067 } else {
1068 item.mSurfaceDamage |= last.mSurfaceDamage;
1069 }
1070
Dan Stoza289ade12014-02-28 11:17:17 -08001071 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001072 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001073 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001074 } else {
1075 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001076 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001077 }
1078 }
1079
1080 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001081 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001082 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001083
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001084 output->width = mCore->mDefaultWidth;
1085 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001086 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001087 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1088 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001089
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001090 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001091#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001092 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001093#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001094 // Take a ticket for the callback functions
1095 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001096
Pablo Ceballos9e314332016-01-12 13:49:19 -08001097 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001098
1099 connectedApi = mCore->mConnectedApi;
1100 lastQueuedFence = std::move(mLastQueueBufferFence);
1101
1102 mLastQueueBufferFence = std::move(acquireFence);
1103 mLastQueuedCrop = item.mCrop;
1104 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001105 } // Autolock scope
1106
Irvel468051e2016-06-13 16:44:44 -07001107 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1108 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1109 // there will be no Binder call
1110 if (!mConsumerIsSurfaceFlinger) {
1111 item.mGraphicBuffer.clear();
1112 }
1113
JihCheng Chiu544c5222019-04-02 20:50:58 +08001114 // Update and get FrameEventHistory.
1115 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1116 NewFrameEventsEntry newFrameEventsEntry = {
1117 currentFrameNumber,
1118 postedTime,
1119 requestedPresentTimestamp,
1120 std::move(acquireFenceTime)
1121 };
1122 addAndGetFrameTimestamps(&newFrameEventsEntry,
1123 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1124
Dan Stoza8dc55392014-11-04 11:37:46 -08001125 // Call back without the main BufferQueue lock held, but with the callback
1126 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001127
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001128 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001129 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001130 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001131 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001132 }
1133
Yi Kong48a619f2018-06-05 16:34:59 -07001134 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001135 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001136 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001137 frameReplacedListener->onFrameReplaced(item);
1138 }
1139
1140 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001141 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001142 }
1143
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001144 // Wait without lock held
1145 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1146 // Waiting here allows for two full buffers to be queued but not a
1147 // third. In the event that frames take varying time, this makes a
1148 // small trade-off in favor of latency rather than throughput.
1149 lastQueuedFence->waitForever("Throttling EGL Production");
1150 }
1151
Dan Stoza289ade12014-02-28 11:17:17 -08001152 return NO_ERROR;
1153}
1154
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001155status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001156 ATRACE_CALL();
1157 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001158
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001159 sp<IConsumerListener> listener;
1160 bool callOnFrameCancelled = false;
1161 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1162 {
1163 std::lock_guard<std::mutex> lock(mCore->mMutex);
1164
1165 if (mCore->mIsAbandoned) {
1166 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1167 return NO_INIT;
1168 }
1169
1170 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1171 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1172 return NO_INIT;
1173 }
1174
1175 if (mCore->mSharedBufferMode) {
1176 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1177 return BAD_VALUE;
1178 }
1179
1180 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1181 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1182 BufferQueueDefs::NUM_BUFFER_SLOTS);
1183 return BAD_VALUE;
1184 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1185 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1186 "(state = %s)",
1187 slot, mSlots[slot].mBufferState.string());
1188 return BAD_VALUE;
1189 } else if (fence == nullptr) {
1190 BQ_LOGE("cancelBuffer: fence is NULL");
1191 return BAD_VALUE;
1192 }
1193
1194 mSlots[slot].mBufferState.cancel();
1195
1196 // After leaving shared buffer mode, the shared buffer will still be around.
1197 // Mark it as no longer shared if this operation causes it to be free.
1198 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1199 mSlots[slot].mBufferState.mShared = false;
1200 }
1201
1202 // Don't put the shared buffer on the free list.
1203 if (!mSlots[slot].mBufferState.isShared()) {
1204 mCore->mActiveBuffers.erase(slot);
1205 mCore->mFreeBuffers.push_back(slot);
1206 }
1207
1208 auto gb = mSlots[slot].mGraphicBuffer;
1209 if (gb != nullptr) {
1210 callOnFrameCancelled = true;
1211 bufferId = gb->getId();
1212 }
1213 mSlots[slot].mFence = fence;
1214 mCore->mDequeueCondition.notify_all();
1215 listener = mCore->mConsumerListener;
1216 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001217 }
1218
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001219 if (listener != nullptr && callOnFrameCancelled) {
1220 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001221 }
1222
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001223 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001224}
1225
1226int BufferQueueProducer::query(int what, int *outValue) {
1227 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001228 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001229
Yi Kong48a619f2018-06-05 16:34:59 -07001230 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001231 BQ_LOGE("query: outValue was NULL");
1232 return BAD_VALUE;
1233 }
1234
1235 if (mCore->mIsAbandoned) {
1236 BQ_LOGE("query: BufferQueue has been abandoned");
1237 return NO_INIT;
1238 }
1239
1240 int value;
1241 switch (what) {
1242 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001243 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001244 break;
1245 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001246 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001247 break;
1248 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001249 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001250 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001251 case NATIVE_WINDOW_LAYER_COUNT:
1252 // All BufferQueue buffers have a single layer.
1253 value = BQ_LAYER_COUNT;
1254 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001255 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001256 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001257 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001258 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001259 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001260 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001261 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1262 value = (mCore->mQueue.size() > 1);
1263 break;
1264 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001265 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001266 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001267 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001268 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1269 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1270 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001271 case NATIVE_WINDOW_BUFFER_AGE:
1272 if (mCore->mBufferAge > INT32_MAX) {
1273 value = 0;
1274 } else {
1275 value = static_cast<int32_t>(mCore->mBufferAge);
1276 }
1277 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001278 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1279 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1280 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001281 default:
1282 return BAD_VALUE;
1283 }
1284
1285 BQ_LOGV("query: %d? %d", what, value);
1286 *outValue = value;
1287 return NO_ERROR;
1288}
1289
Dan Stozaf0eaf252014-03-21 13:05:51 -07001290status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001291 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1292 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001293 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001294 mConsumerName = mCore->mConsumerName;
1295 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1296 producerControlledByApp ? "true" : "false");
1297
1298 if (mCore->mIsAbandoned) {
1299 BQ_LOGE("connect: BufferQueue has been abandoned");
1300 return NO_INIT;
1301 }
1302
Yi Kong48a619f2018-06-05 16:34:59 -07001303 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001304 BQ_LOGE("connect: BufferQueue has no consumer");
1305 return NO_INIT;
1306 }
1307
Yi Kong48a619f2018-06-05 16:34:59 -07001308 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001309 BQ_LOGE("connect: output was NULL");
1310 return BAD_VALUE;
1311 }
1312
1313 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1314 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1315 mCore->mConnectedApi, api);
1316 return BAD_VALUE;
1317 }
1318
1319 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1320 mDequeueTimeout < 0 ?
1321 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1322 mCore->mMaxBufferCount) -
1323 mCore->getMaxBufferCountLocked();
1324 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1325 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1326 "slots. Delta = %d", delta);
1327 return BAD_VALUE;
1328 }
1329
Dan Stoza289ade12014-02-28 11:17:17 -08001330 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001331 switch (api) {
1332 case NATIVE_WINDOW_API_EGL:
1333 case NATIVE_WINDOW_API_CPU:
1334 case NATIVE_WINDOW_API_MEDIA:
1335 case NATIVE_WINDOW_API_CAMERA:
1336 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001337
1338 output->width = mCore->mDefaultWidth;
1339 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001340 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001341 output->numPendingBuffers =
1342 static_cast<uint32_t>(mCore->mQueue.size());
1343 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001344 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001345 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001346
Yi Kong48a619f2018-06-05 16:34:59 -07001347 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001348 // Set up a death notification so that we can disconnect
1349 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001350#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001351 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001352 status = IInterface::asBinder(listener)->linkToDeath(
1353 static_cast<IBinder::DeathRecipient*>(this));
1354 if (status != NO_ERROR) {
1355 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1356 strerror(-status), status);
1357 }
1358 mCore->mLinkedToDeath = listener;
1359 }
Chong Zhang62493092020-01-15 16:04:47 -08001360#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001361 mCore->mConnectedProducerListener = listener;
1362 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001363 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001364 break;
1365 default:
1366 BQ_LOGE("connect: unknown API %d", api);
1367 status = BAD_VALUE;
1368 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001369 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001370 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001371 mCore->mBufferHasBeenQueued = false;
1372 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001373 mCore->mQueueBufferCanDrop = false;
1374 mCore->mLegacyBufferDrop = true;
1375 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1376 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1377 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001378 }
Dan Stoza289ade12014-02-28 11:17:17 -08001379
Pablo Ceballos981066c2016-02-18 12:54:37 -08001380 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001381#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1382 mCore->mAdditionalOptions.clear();
1383#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001384 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001385 return status;
1386}
1387
Robert Carr97b9c862016-09-08 13:54:35 -07001388status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001389 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001390 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001391
1392 int status = NO_ERROR;
1393 sp<IConsumerListener> listener;
1394 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001395 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001396
1397 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001398 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001399 return NO_ERROR;
1400 }
1401 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1402 }
1403
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001404 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001405
1406 if (mCore->mIsAbandoned) {
1407 // It's not really an error to disconnect after the surface has
1408 // been abandoned; it should just be a no-op.
1409 return NO_ERROR;
1410 }
1411
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001412 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001413 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1414 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1415 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001416 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001417 // If we're asked to disconnect the currently connected api but
1418 // nobody is connected, it's not really an error.
1419 if (api == BufferQueueCore::NO_CONNECTED_API) {
1420 return NO_ERROR;
1421 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001422 }
1423
Dan Stoza289ade12014-02-28 11:17:17 -08001424 switch (api) {
1425 case NATIVE_WINDOW_API_EGL:
1426 case NATIVE_WINDOW_API_CPU:
1427 case NATIVE_WINDOW_API_MEDIA:
1428 case NATIVE_WINDOW_API_CAMERA:
1429 if (mCore->mConnectedApi == api) {
1430 mCore->freeAllBuffersLocked();
1431
Chong Zhang62493092020-01-15 16:04:47 -08001432#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001433 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001434 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001435 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001436 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001437 // This can fail if we're here because of the death
1438 // notification, but we just ignore it
1439 token->unlinkToDeath(
1440 static_cast<IBinder::DeathRecipient*>(this));
1441 }
Chong Zhang62493092020-01-15 16:04:47 -08001442#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001443 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001444 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001445 mCore->mLinkedToDeath = nullptr;
1446 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001447 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001448 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001449 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001450 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001451 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001452#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1453 mCore->mAdditionalOptions.clear();
1454#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001455 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001456 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1457 BQ_LOGE("disconnect: not connected (req=%d)", api);
1458 status = NO_INIT;
1459 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001460 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001461 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001462 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001463 }
1464 break;
1465 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001466 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001467 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001468 break;
1469 }
1470 } // Autolock scope
1471
1472 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001473 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001474 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001475 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001476 }
1477
1478 return status;
1479}
1480
Jesse Hall399184a2014-03-03 15:42:54 -08001481status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001482 sp<IConsumerListener> listener;
1483 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001484 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001485 mCore->mSidebandStream = stream;
1486 listener = mCore->mConsumerListener;
1487 } // Autolock scope
1488
Yi Kong48a619f2018-06-05 16:34:59 -07001489 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001490 listener->onSidebandStreamChanged();
1491 }
Jesse Hall399184a2014-03-03 15:42:54 -08001492 return NO_ERROR;
1493}
1494
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001495void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001496 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001497 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001498
1499 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001500 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001501 size_t newBufferCount = 0;
1502 uint32_t allocWidth = 0;
1503 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001504 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001505 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001506 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001507#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1508 std::vector<gui::AdditionalOptions> allocOptions;
1509 uint32_t allocOptionsGenId = 0;
1510#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001511 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001512 std::unique_lock<std::mutex> lock(mCore->mMutex);
1513 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001514
Dan Stoza9de72932015-04-16 17:28:43 -07001515 if (!mCore->mAllowAllocation) {
1516 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1517 "BufferQueue");
1518 return;
1519 }
1520
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001521 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1522 // both allocateBuffers and dequeueBuffer.
1523 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001524 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001525 return;
1526 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001527
Antoine Labour78014f32014-07-15 21:17:03 -07001528 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1529 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001530 if (useDefaultSize && mCore->mAutoPrerotation &&
1531 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1532 std::swap(allocWidth, allocHeight);
1533 }
1534
Antoine Labour78014f32014-07-15 21:17:03 -07001535 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1536 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001537 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001538
John Reckdb164ff2024-04-03 16:59:28 -04001539#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1540 allocOptions = mCore->mAdditionalOptions;
1541 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1542#endif
1543
Antoine Labour78014f32014-07-15 21:17:03 -07001544 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001545
Antoine Labour78014f32014-07-15 21:17:03 -07001546 } // Autolock scope
1547
John Reckdb164ff2024-04-03 16:59:28 -04001548#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1549 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1550 tempOptions.reserve(allocOptions.size());
1551 for (const auto& it : allocOptions) {
1552 tempOptions.emplace_back(it.name.c_str(), it.value);
1553 }
1554 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1555 .importBuffer = true,
1556 .width = allocWidth,
1557 .height = allocHeight,
1558 .format = allocFormat,
1559 .layerCount = BQ_LAYER_COUNT,
1560 .usage = allocUsage,
1561 .requestorName = allocName,
1562 .extras = std::move(tempOptions),
1563 };
1564#endif
1565
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001566 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001567 for (size_t i = 0; i < newBufferCount; ++i) {
John Reckdb164ff2024-04-03 16:59:28 -04001568#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1569 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
1570#else
Mathias Agopian0556d792017-03-22 15:49:32 -07001571 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001572 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001573 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001574#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001575
1576 status_t result = graphicBuffer->initCheck();
1577
Antoine Labour78014f32014-07-15 21:17:03 -07001578 if (result != NO_ERROR) {
1579 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001580 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001581 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001582 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001583 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001584 return;
1585 }
1586 buffers.push_back(graphicBuffer);
1587 }
1588
1589 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001590 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001591 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1592 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001593 if (useDefaultSize && mCore->mAutoPrerotation &&
1594 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1595 std::swap(checkWidth, checkHeight);
1596 }
1597
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001598 PixelFormat checkFormat = format != 0 ?
1599 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001600 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001601 bool allocOptionsChanged = false;
1602#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1603 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1604#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001605 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001606 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001607 // Something changed while we released the lock. Retry.
1608 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1609 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001610 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001611 continue;
1612 }
1613
Antoine Labour78014f32014-07-15 21:17:03 -07001614 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001615 if (mCore->mFreeSlots.empty()) {
1616 BQ_LOGV("allocateBuffers: a slot was occupied while "
1617 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001618 continue;
1619 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001620 auto slot = mCore->mFreeSlots.begin();
1621 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1622 mSlots[*slot].mGraphicBuffer = buffers[i];
1623 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001624#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1625 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1626#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001627
1628 // freeBufferLocked puts this slot on the free slots list. Since
1629 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001630 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001631
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001632 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1633 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001634
1635 // Make sure the erase is done after all uses of the slot
1636 // iterator since it will be invalid after this point.
1637 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001638 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001639
Antoine Labour78014f32014-07-15 21:17:03 -07001640 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001641 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001642 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001643
1644 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1645 // waiting anymore so it can use the buffer we just allocated.
1646 while (mDequeueWaitingForAllocation) {
1647 mDequeueWaitingForAllocationCondition.wait(lock);
1648 }
Antoine Labour78014f32014-07-15 21:17:03 -07001649 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001650 }
1651}
1652
Dan Stoza9de72932015-04-16 17:28:43 -07001653status_t BufferQueueProducer::allowAllocation(bool allow) {
1654 ATRACE_CALL();
1655 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1656
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001657 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001658 mCore->mAllowAllocation = allow;
1659 return NO_ERROR;
1660}
1661
Dan Stoza812ed062015-06-02 15:45:22 -07001662status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1663 ATRACE_CALL();
1664 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1665
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001666 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001667 mCore->mGenerationNumber = generationNumber;
1668 return NO_ERROR;
1669}
1670
Dan Stozac6f30bd2015-06-08 09:32:50 -07001671String8 BufferQueueProducer::getConsumerName() const {
1672 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001673 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001674 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001675 return mConsumerName;
1676}
1677
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001678status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001679 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001680 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001681
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001682 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001683 if (!sharedBufferMode) {
1684 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001685 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001686 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001687 return NO_ERROR;
1688}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001689
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001690status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1691 ATRACE_CALL();
1692 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1693
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001694 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001695
1696 mCore->mAutoRefresh = autoRefresh;
1697 return NO_ERROR;
1698}
1699
Dan Stoza127fc632015-06-30 13:43:32 -07001700status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1701 ATRACE_CALL();
1702 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1703
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001704 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001705 bool dequeueBufferCannotBlock =
1706 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1707 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001708 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1709 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1710 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1711 "available slots. Delta = %d", delta);
1712 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001713 }
1714
Pablo Ceballos981066c2016-02-18 12:54:37 -08001715 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001716 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1717 if (timeout > 0) {
1718 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001719 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001720
Pablo Ceballos981066c2016-02-18 12:54:37 -08001721 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001722 return NO_ERROR;
1723}
1724
Sungtak Lee3249fb62019-03-02 16:40:47 -08001725status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1726 ATRACE_CALL();
1727 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1728
1729 std::lock_guard<std::mutex> lock(mCore->mMutex);
1730 mCore->mLegacyBufferDrop = drop;
1731 return NO_ERROR;
1732}
1733
Dan Stoza50101d02016-04-07 16:53:23 -07001734status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001735 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001736 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001737
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001738 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001739 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1740
Dan Stoza50101d02016-04-07 16:53:23 -07001741 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1742 *outBuffer = nullptr;
1743 *outFence = Fence::NO_FENCE;
1744 return NO_ERROR;
1745 }
1746
1747 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1748 *outFence = mLastQueueBufferFence;
1749
John Reck1a61da52016-04-28 13:18:15 -07001750 // Currently only SurfaceFlinger internally ever changes
1751 // GLConsumer's filtering mode, so we just use 'true' here as
1752 // this is slightly specialized for the current client of this API,
1753 // which does want filtering.
1754 GLConsumer::computeTransformMatrix(outTransformMatrix,
1755 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1756 mLastQueuedTransform, true /* filter */);
1757
Dan Stoza50101d02016-04-07 16:53:23 -07001758 return NO_ERROR;
1759}
1760
John Reckf0f13e82021-05-18 00:42:56 -04001761status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1762 Rect* outRect, uint32_t* outTransform) {
1763 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001764
1765 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001766 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1767 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1768 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001769 *outBuffer = nullptr;
1770 *outFence = Fence::NO_FENCE;
1771 return NO_ERROR;
1772 }
1773
1774 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1775 *outFence = mLastQueueBufferFence;
1776 *outRect = mLastQueuedCrop;
1777 *outTransform = mLastQueuedTransform;
1778
1779 return NO_ERROR;
1780}
1781
Brian Anderson3890c392016-07-25 12:48:08 -07001782void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1783 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001784}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001785
Brian Anderson3890c392016-07-25 12:48:08 -07001786void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001787 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001788 FrameEventHistoryDelta* outDelta) {
1789 if (newTimestamps == nullptr && outDelta == nullptr) {
1790 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001791 }
1792
1793 ATRACE_CALL();
1794 BQ_LOGV("addAndGetFrameTimestamps");
1795 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001796 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001797 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001798 listener = mCore->mConsumerListener;
1799 }
Yi Kong48a619f2018-06-05 16:34:59 -07001800 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001801 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001802 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001803}
1804
Dan Stoza289ade12014-02-28 11:17:17 -08001805void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1806 // If we're here, it means that a producer we were connected to died.
1807 // We're guaranteed that we are still connected to it because we remove
1808 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1809 // without synchronization here.
1810 int api = mCore->mConnectedApi;
1811 disconnect(api);
1812}
1813
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001814status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1815 BQ_LOGV("getUniqueId");
1816
1817 *outId = mCore->mUniqueId;
1818 return NO_ERROR;
1819}
1820
Chia-I Wue2786ea2017-08-07 10:36:08 -07001821status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1822 BQ_LOGV("getConsumerUsage");
1823
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001824 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001825 *outUsage = mCore->mConsumerUsageBits;
1826 return NO_ERROR;
1827}
1828
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001829status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1830 ATRACE_CALL();
1831 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1832
1833 std::lock_guard<std::mutex> lock(mCore->mMutex);
1834
1835 mCore->mAutoPrerotation = autoPrerotation;
1836 return NO_ERROR;
1837}
1838
Ady Abraham107788e2023-10-17 12:31:08 -07001839#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001840status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1841 int8_t changeFrameRateStrategy) {
1842 ATRACE_CALL();
1843 BQ_LOGV("setFrameRate: %.2f", frameRate);
1844
1845 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1846 "BufferQueueProducer::setFrameRate")) {
1847 return BAD_VALUE;
1848 }
1849
1850 sp<IConsumerListener> listener;
1851 {
1852 std::lock_guard<std::mutex> lock(mCore->mMutex);
1853 listener = mCore->mConsumerListener;
1854 }
1855 if (listener != nullptr) {
1856 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1857 }
1858 return NO_ERROR;
1859}
1860#endif
1861
John Reckdb164ff2024-04-03 16:59:28 -04001862#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1863status_t BufferQueueProducer::setAdditionalOptions(
1864 const std::vector<gui::AdditionalOptions>& options) {
1865 ATRACE_CALL();
1866 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1867
1868 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1869 return INVALID_OPERATION;
1870 }
1871
1872 std::lock_guard<std::mutex> lock(mCore->mMutex);
1873
1874 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1875 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1876 return NO_INIT;
1877 }
1878
1879 if (mCore->mAdditionalOptions != options) {
1880 mCore->mAdditionalOptions = options;
1881 mCore->mAdditionalOptionsGenerationId++;
1882 }
1883 return NO_ERROR;
1884}
1885#endif
1886
Dan Stoza289ade12014-02-28 11:17:17 -08001887} // namespace android