blob: f1e5eb7c896fe97021c38466c6d7d534c2597863 [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
Mathias Agopianb96661f2024-09-17 11:45:38 -070048#include <com_android_graphics_libgui_flags.h>
49
Dan Stoza289ade12014-02-28 11:17:17 -080050namespace android {
Mathias Agopianb96661f2024-09-17 11:45:38 -070051using namespace com::android::graphics::libgui;
Dan Stoza289ade12014-02-28 11:17:17 -080052
Iris Chang430193f2019-12-04 16:25:46 +080053// Macros for include BufferQueueCore information in log messages
54#define BQ_LOGV(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000055 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080056 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
57 ##__VA_ARGS__)
58#define BQ_LOGD(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000059 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080060 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
61 ##__VA_ARGS__)
62#define BQ_LOGI(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000063 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080064 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
65 ##__VA_ARGS__)
66#define BQ_LOGW(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000067 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080068 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
69 ##__VA_ARGS__)
70#define BQ_LOGE(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000071 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080072 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
73 ##__VA_ARGS__)
74
Craig Donner6ebc46a2016-10-21 15:23:44 -070075static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080076ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070077
Irvel468051e2016-06-13 16:44:44 -070078BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
79 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080080 mCore(core),
81 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070082 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070083 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070084 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080085 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070086 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080087 mCallbackMutex(),
88 mNextCallbackTicket(0),
89 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070090 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010091 mDequeueTimeout(-1),
92 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080093
94BufferQueueProducer::~BufferQueueProducer() {}
95
96status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
97 ATRACE_CALL();
98 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020099 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800100
101 if (mCore->mIsAbandoned) {
102 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
103 return NO_INIT;
104 }
105
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700106 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
107 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
108 return NO_INIT;
109 }
110
Dan Stoza3e96f192014-03-03 10:16:19 -0800111 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800112 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800113 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800114 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700115 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800116 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700117 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800118 return BAD_VALUE;
119 }
120
121 mSlots[slot].mRequestBufferCalled = true;
122 *buf = mSlots[slot].mGraphicBuffer;
123 return NO_ERROR;
124}
125
Pablo Ceballosfa455352015-08-12 17:47:47 -0700126status_t BufferQueueProducer::setMaxDequeuedBufferCount(
127 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700128 int maxBufferCount;
129 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
130}
131
132status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
133 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700134 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700135 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
136 maxDequeuedBuffers);
137
Pablo Ceballos981066c2016-02-18 12:54:37 -0800138 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700139 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200140 std::unique_lock<std::mutex> lock(mCore->mMutex);
141 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700142
143 if (mCore->mIsAbandoned) {
144 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
145 "abandoned");
146 return NO_INIT;
147 }
148
Brian Lindahlc794b692023-01-31 15:42:47 -0700149 *maxBufferCount = mCore->getMaxBufferCountLocked();
150
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700151 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
152 return NO_ERROR;
153 }
154
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 // The new maxDequeuedBuffer count should not be violated by the number
156 // of currently dequeued buffers
157 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800158 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700159 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800160 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700161 }
162 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800163 if (dequeuedCount > maxDequeuedBuffers) {
164 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
165 "count (%d) exceeds the current dequeued buffer count (%d)",
166 maxDequeuedBuffers, dequeuedCount);
167 return BAD_VALUE;
168 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700169
Rajat Yadavd108d682021-08-31 18:58:23 +0800170 int minUndequedBufferCount = mCore->getMinUndequeuedBufferCountLocked();
171 int bufferCount = minUndequedBufferCount + maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700172
173 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
174 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
175 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Rajat Yadavd108d682021-08-31 18:58:23 +0800176 bufferCount = BufferQueueDefs::NUM_BUFFER_SLOTS;
177 maxDequeuedBuffers = bufferCount - minUndequedBufferCount;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700178 }
179
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700180 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700181 if (bufferCount < minBufferSlots) {
182 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
183 "less than minimum %d", bufferCount, minBufferSlots);
184 return BAD_VALUE;
185 }
186
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 if (bufferCount > mCore->mMaxBufferCount) {
188 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700189 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
190 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
191 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
192 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700193 return BAD_VALUE;
194 }
195
Pablo Ceballos72daab62015-12-07 16:38:43 -0800196 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800197 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800198 return BAD_VALUE;
199 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700200 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700201 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800202 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800203 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800204 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800205 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200206 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700207 } // Autolock scope
208
209 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700210 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800211 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700212 }
213
214 return NO_ERROR;
215}
216
217status_t BufferQueueProducer::setAsyncMode(bool async) {
218 ATRACE_CALL();
219 BQ_LOGV("setAsyncMode: async = %d", async);
220
Pablo Ceballos981066c2016-02-18 12:54:37 -0800221 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700222 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200223 std::unique_lock<std::mutex> lock(mCore->mMutex);
224 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700225
226 if (mCore->mIsAbandoned) {
227 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
228 return NO_INIT;
229 }
230
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700231 if (async == mCore->mAsyncMode) {
232 return NO_ERROR;
233 }
234
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700235 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700236 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
237 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700238 BQ_LOGE("setAsyncMode(%d): this call would cause the "
239 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700240 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
241 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
242 mCore->mMaxDequeuedBufferCount,
243 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700244 return BAD_VALUE;
245 }
246
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800247 int delta = mCore->getMaxBufferCountLocked(async,
248 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
249 - mCore->getMaxBufferCountLocked();
250
Pablo Ceballos981066c2016-02-18 12:54:37 -0800251 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800252 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
253 "available slots. Delta = %d", delta);
254 return BAD_VALUE;
255 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700256 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800257 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200258 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700259 if (delta < 0) {
260 listener = mCore->mConsumerListener;
261 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700262 } // Autolock scope
263
264 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700265 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800266 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700267 }
268 return NO_ERROR;
269}
270
Dan Stoza5ecfb682016-01-04 17:01:02 -0800271int BufferQueueProducer::getFreeBufferLocked() const {
272 if (mCore->mFreeBuffers.empty()) {
273 return BufferQueueCore::INVALID_BUFFER_SLOT;
274 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800275 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800276 mCore->mFreeBuffers.pop_front();
277 return slot;
278}
279
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800280int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800281 if (mCore->mFreeSlots.empty()) {
282 return BufferQueueCore::INVALID_BUFFER_SLOT;
283 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800284 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800285 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800286 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800287}
288
289status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200290 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800291 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
292 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 bool tryAgain = true;
294 while (tryAgain) {
295 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800296 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800297 return NO_INIT;
298 }
299
Dan Stoza9f3053d2014-03-06 15:14:33 -0800300 int dequeuedCount = 0;
301 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700303 if (mSlots[s].mBufferState.isDequeued()) {
304 ++dequeuedCount;
305 }
306 if (mSlots[s].mBufferState.isAcquired()) {
307 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800308 }
309 }
310
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700311 // Producers are not allowed to dequeue more than
312 // mMaxDequeuedBufferCount buffers.
313 // This check is only done if a buffer has already been queued
314 if (mCore->mBufferHasBeenQueued &&
315 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700316 // Supress error logs when timeout is non-negative.
317 if (mDequeueTimeout < 0) {
318 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
319 "count (%d)", callerString,
320 mCore->mMaxDequeuedBufferCount);
321 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322 return INVALID_OPERATION;
323 }
324
Dan Stoza0de7ea72015-04-23 13:20:51 -0700325 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
326
Dan Stozaae3c3682014-04-18 15:43:35 -0700327 // If we disconnect and reconnect quickly, we can be in a state where
328 // our slots are empty but we have many buffers in the queue. This can
329 // cause us to run out of memory if we outrun the consumer. Wait here if
330 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800331 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700332 bool tooManyBuffers = mCore->mQueue.size()
333 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700334 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800335 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700336 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700337 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700338 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700339 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700340 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700341 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700342 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800343 } else {
344 if (caller == FreeSlotCaller::Dequeue) {
345 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800346 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800347 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
348 *found = slot;
349 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800350 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800351 }
352 } else {
353 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800354 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800355 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
356 *found = slot;
357 } else {
358 *found = getFreeBufferLocked();
359 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700360 }
361 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700362 }
363
364 // If no buffer is found, or if the queue has too many buffers
365 // outstanding, wait for a buffer to be acquired or released, or for the
366 // max buffer count to change.
367 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
368 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 if (tryAgain) {
370 // Return an error if we're in non-blocking mode (producer and
371 // consumer are controlled by the application).
372 // However, the consumer is allowed to briefly acquire an extra
373 // buffer (which could cause us to have to wait here), which is
374 // okay, since it is only used to implement an atomic acquire +
375 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700376 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
378 return WOULD_BLOCK;
379 }
Dan Stoza127fc632015-06-30 13:43:32 -0700380 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200381 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
382 std::chrono::nanoseconds(mDequeueTimeout));
383 if (result == std::cv_status::timeout) {
384 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700385 }
386 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200387 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700388 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800389 }
390 } // while (tryAgain)
391
392 return NO_ERROR;
393}
394
Ian Elliottd11b0442017-07-18 11:05:49 -0600395status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
396 uint32_t width, uint32_t height, PixelFormat format,
397 uint64_t usage, uint64_t* outBufferAge,
398 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800399 ATRACE_CALL();
400 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200401 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800402 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700403
404 if (mCore->mIsAbandoned) {
405 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
406 return NO_INIT;
407 }
408
409 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
410 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
411 return NO_INIT;
412 }
Dan Stoza289ade12014-02-28 11:17:17 -0800413 } // Autolock scope
414
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700415 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800416
417 if ((width && !height) || (!width && height)) {
418 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
419 return BAD_VALUE;
420 }
421
422 status_t returnFlags = NO_ERROR;
423 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
424 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800426
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000427 sp<IConsumerListener> listener;
428 bool callOnFrameDequeued = false;
429 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400430#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
431 std::vector<gui::AdditionalOptions> allocOptions;
432 uint32_t allocOptionsGenId = 0;
433#endif
434
Dan Stoza289ade12014-02-28 11:17:17 -0800435 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200436 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800437
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100438 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
439 // is finished such that we don't allocate in parallel.
440 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
441 mDequeueWaitingForAllocation = true;
442 mCore->waitWhileAllocatingLocked(lock);
443 mDequeueWaitingForAllocation = false;
444 mDequeueWaitingForAllocationCondition.notify_all();
445 }
Dan Stoza289ade12014-02-28 11:17:17 -0800446
447 if (format == 0) {
448 format = mCore->mDefaultBufferFormat;
449 }
450
451 // Enable the usage bits the consumer requested
452 usage |= mCore->mConsumerUsageBits;
453
Dan Stoza9de72932015-04-16 17:28:43 -0700454 const bool useDefaultSize = !width && !height;
455 if (useDefaultSize) {
456 width = mCore->mDefaultWidth;
457 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700458 if (mCore->mAutoPrerotation &&
459 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
460 std::swap(width, height);
461 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800462 }
Dan Stoza289ade12014-02-28 11:17:17 -0800463
Pablo Ceballos981066c2016-02-18 12:54:37 -0800464 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700465 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200466 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700467 if (status != NO_ERROR) {
468 return status;
469 }
470
471 // This should not happen
472 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
473 BQ_LOGE("dequeueBuffer: no available buffer slots");
474 return -EBUSY;
475 }
476
477 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
478
479 // If we are not allowed to allocate new buffers,
480 // waitForFreeSlotThenRelock must have returned a slot containing a
481 // buffer. If this buffer would require reallocation to meet the
482 // requested attributes, we free it and attempt to get another one.
483 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700485 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700486 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700487 return BAD_VALUE;
488 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800489 mCore->mFreeSlots.insert(found);
490 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700491 found = BufferItem::INVALID_BUFFER_SLOT;
492 continue;
493 }
494 }
Dan Stoza289ade12014-02-28 11:17:17 -0800495 }
496
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800497 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800498
John Reckdb164ff2024-04-03 16:59:28 -0400499 bool needsReallocation = buffer == nullptr ||
500 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
501
502#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
503 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
504 mCore->mAdditionalOptionsGenerationId;
505#endif
506
507 if (mCore->mSharedBufferSlot == found && needsReallocation) {
508 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800509 return BAD_VALUE;
510 }
511
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700512 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800513 mCore->mActiveBuffers.insert(found);
514 }
Dan Stoza289ade12014-02-28 11:17:17 -0800515 *outSlot = found;
516 ATRACE_BUFFER_INDEX(found);
517
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800518 attachedByConsumer = mSlots[found].mNeedsReallocation;
519 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800520
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 mSlots[found].mBufferState.dequeue();
522
John Reckdb164ff2024-04-03 16:59:28 -0400523 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700524 if (CC_UNLIKELY(ATRACE_ENABLED())) {
525 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000526 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700527 } else {
528 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
529 "layerCount:%d "
530 "usage:%d requested: %dx%d format:%d layerCount:%d "
531 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000532 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700533 BQ_LAYER_COUNT, usage, buffer->getWidth(),
534 buffer->getHeight(), buffer->getPixelFormat(),
535 buffer->getLayerCount(), buffer->getUsage());
536 }
537 }
Dan Stoza289ade12014-02-28 11:17:17 -0800538 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700539 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800540 mSlots[found].mRequestBufferCalled = false;
541 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
542 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
543 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800544 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700545 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400546#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
547 allocOptions = mCore->mAdditionalOptions;
548 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
549#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800550
551 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800552 } else {
553 // We add 1 because that will be the frame number when this buffer
554 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700555 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800556 }
557
Dan Stoza800b41a2015-04-28 14:20:04 -0700558 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
559 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800560
Yi Kong48a619f2018-06-05 16:34:59 -0700561 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800562 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
563 "slot=%d w=%d h=%d format=%u",
564 found, buffer->width, buffer->height, buffer->format);
565 }
566
567 eglDisplay = mSlots[found].mEglDisplay;
568 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700569 // Don't return a fence in shared buffer mode, except for the first
570 // frame.
571 *outFence = (mCore->mSharedBufferMode &&
572 mCore->mSharedBufferSlot == found) ?
573 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800574 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
575 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700576
577 // If shared buffer mode has just been enabled, cache the slot of the
578 // first buffer that is dequeued and mark it as the shared buffer.
579 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
580 BufferQueueCore::INVALID_BUFFER_SLOT) {
581 mCore->mSharedBufferSlot = found;
582 mSlots[found].mBufferState.mShared = true;
583 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700584
585 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000586 callOnFrameDequeued = true;
587 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700588 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000589
590 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800591 } // Autolock scope
592
593 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700594 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400595
596#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
597 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
598 tempOptions.reserve(allocOptions.size());
599 for (const auto& it : allocOptions) {
600 tempOptions.emplace_back(it.name.c_str(), it.value);
601 }
602 const GraphicBufferAllocator::AllocationRequest allocRequest = {
603 .importBuffer = true,
604 .width = width,
605 .height = height,
606 .format = format,
607 .layerCount = BQ_LAYER_COUNT,
608 .usage = usage,
609 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
610 .extras = std::move(tempOptions),
611 };
612 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
613#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000614 sp<GraphicBuffer> graphicBuffer =
615 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
616 {mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400617#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700618
619 status_t error = graphicBuffer->initCheck();
620
Dan Stoza289ade12014-02-28 11:17:17 -0800621 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200622 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800623
Chia-I Wufeec3b12017-05-25 09:34:56 -0700624 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700625 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
626 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400627#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
628 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
629#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000630 callOnFrameDequeued = true;
631 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700632 }
633
634 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200635 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700636
Chia-I Wufeec3b12017-05-25 09:34:56 -0700637 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700638 mCore->mFreeSlots.insert(*outSlot);
639 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700640 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
641 return error;
642 }
643
Dan Stoza289ade12014-02-28 11:17:17 -0800644 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700645 mCore->mFreeSlots.insert(*outSlot);
646 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800647 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
648 return NO_INIT;
649 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800650
Pablo Ceballos9e314332016-01-12 13:49:19 -0800651 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800652 } // Autolock scope
653 }
654
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000655 if (listener != nullptr && callOnFrameDequeued) {
656 listener->onFrameDequeued(bufferId);
657 }
658
Dan Stoza9f3053d2014-03-06 15:14:33 -0800659 if (attachedByConsumer) {
660 returnFlags |= BUFFER_NEEDS_REALLOCATION;
661 }
662
Dan Stoza289ade12014-02-28 11:17:17 -0800663 if (eglFence != EGL_NO_SYNC_KHR) {
664 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
665 1000000000);
666 // If something goes wrong, log the error, but return the buffer without
667 // synchronizing access to it. It's too late at this point to abort the
668 // dequeue operation.
669 if (result == EGL_FALSE) {
670 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
671 eglGetError());
672 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
673 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
674 }
675 eglDestroySyncKHR(eglDisplay, eglFence);
676 }
677
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700678 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
679 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800680 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800681 mSlots[*outSlot].mGraphicBuffer != nullptr ?
682 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800683
Ian Elliottd11b0442017-07-18 11:05:49 -0600684 if (outBufferAge) {
685 *outBufferAge = mCore->mBufferAge;
686 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700687 addAndGetFrameTimestamps(nullptr, outTimestamps);
688
Dan Stoza289ade12014-02-28 11:17:17 -0800689 return returnFlags;
690}
691
Dan Stoza9f3053d2014-03-06 15:14:33 -0800692status_t BufferQueueProducer::detachBuffer(int slot) {
693 ATRACE_CALL();
694 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700697 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000698 bool callOnFrameDetached = false;
699 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700700 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200701 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700702
703 if (mCore->mIsAbandoned) {
704 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
705 return NO_INIT;
706 }
707
708 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
709 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
710 return NO_INIT;
711 }
712
713 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
714 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
715 return BAD_VALUE;
716 }
717
718 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
719 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
720 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
721 return BAD_VALUE;
722 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000723 // TODO(http://b/140581935): This message is BQ_LOGW because it
724 // often logs when no actionable errors are present. Return to
725 // using BQ_LOGE after ensuring this only logs during errors.
726 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700727 "(state = %s)", slot, mSlots[slot].mBufferState.string());
728 return BAD_VALUE;
729 } else if (!mSlots[slot].mRequestBufferCalled) {
730 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
731 slot);
732 return BAD_VALUE;
733 }
734
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700735 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700736 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000737 if (gb != nullptr) {
738 callOnFrameDetached = true;
739 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700740 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700741 mSlots[slot].mBufferState.detachProducer();
742 mCore->mActiveBuffers.erase(slot);
743 mCore->mFreeSlots.insert(slot);
744 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200745 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700746 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800747 }
748
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000749 if (listener != nullptr && callOnFrameDetached) {
750 listener->onFrameDetached(bufferId);
751 }
752
Yi Kong48a619f2018-06-05 16:34:59 -0700753 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700754 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700755 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800756
Dan Stoza9f3053d2014-03-06 15:14:33 -0800757 return NO_ERROR;
758}
759
Dan Stozad9822a32014-03-28 15:25:31 -0700760status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
761 sp<Fence>* outFence) {
762 ATRACE_CALL();
763
Yi Kong48a619f2018-06-05 16:34:59 -0700764 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700765 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
766 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700767 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700768 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
769 return BAD_VALUE;
770 }
771
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700772 sp<IConsumerListener> listener;
773 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200774 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700775
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700776 if (mCore->mIsAbandoned) {
777 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
778 return NO_INIT;
779 }
780
781 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
782 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
783 return NO_INIT;
784 }
785
786 if (mCore->mSharedBufferMode) {
787 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
788 "mode");
789 return BAD_VALUE;
790 }
791
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200792 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700793
794 if (mCore->mFreeBuffers.empty()) {
795 return NO_MEMORY;
796 }
797
798 int found = mCore->mFreeBuffers.front();
799 mCore->mFreeBuffers.remove(found);
800 mCore->mFreeSlots.insert(found);
801
802 BQ_LOGV("detachNextBuffer detached slot %d", found);
803
804 *outBuffer = mSlots[found].mGraphicBuffer;
805 *outFence = mSlots[found].mFence;
806 mCore->clearBufferSlotLocked(found);
807 VALIDATE_CONSISTENCY();
808 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700809 }
810
Yi Kong48a619f2018-06-05 16:34:59 -0700811 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700812 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700813 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800814
Dan Stozad9822a32014-03-28 15:25:31 -0700815 return NO_ERROR;
816}
817
Dan Stoza9f3053d2014-03-06 15:14:33 -0800818status_t BufferQueueProducer::attachBuffer(int* outSlot,
819 const sp<android::GraphicBuffer>& buffer) {
820 ATRACE_CALL();
821
Yi Kong48a619f2018-06-05 16:34:59 -0700822 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700823 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800824 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700825 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700826 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800827 return BAD_VALUE;
828 }
829
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200830 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700831
832 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700833 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700834 return NO_INIT;
835 }
836
837 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700838 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700839 return NO_INIT;
840 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700842 if (mCore->mSharedBufferMode) {
843 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700844 return BAD_VALUE;
845 }
846
Dan Stoza812ed062015-06-02 15:45:22 -0700847 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
848 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
849 "[queue %u]", buffer->getGenerationNumber(),
850 mCore->mGenerationNumber);
851 return BAD_VALUE;
852 }
853
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200854 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700855
Dan Stoza9f3053d2014-03-06 15:14:33 -0800856 status_t returnFlags = NO_ERROR;
857 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200858 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800859 if (status != NO_ERROR) {
860 return status;
861 }
862
863 // This should not happen
864 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700865 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800866 return -EBUSY;
867 }
868
869 *outSlot = found;
870 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800872 *outSlot, returnFlags);
873
874 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700875 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800876 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
877 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700878 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800879 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800880 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800881 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800882 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700883
Dan Stoza9f3053d2014-03-06 15:14:33 -0800884 return returnFlags;
885}
886
Dan Stoza289ade12014-02-28 11:17:17 -0800887status_t BufferQueueProducer::queueBuffer(int slot,
888 const QueueBufferInput &input, QueueBufferOutput *output) {
889 ATRACE_CALL();
890 ATRACE_BUFFER_INDEX(slot);
891
Brian Andersond6927fb2016-07-23 23:37:30 -0700892 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800893 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800894 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700895 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800896 int scalingMode;
897 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700898 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700899 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700900 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700901 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700902 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
903 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700904 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700905 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800906
Yi Kong48a619f2018-06-05 16:34:59 -0700907 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800908 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800909 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800910 }
911
Brian Anderson3d4039d2016-09-23 16:31:30 -0700912 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
913
Dan Stoza289ade12014-02-28 11:17:17 -0800914 switch (scalingMode) {
915 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
916 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
917 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
918 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
919 break;
920 default:
921 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800922 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800923 }
924
Dan Stoza8dc55392014-11-04 11:37:46 -0800925 sp<IConsumerListener> frameAvailableListener;
926 sp<IConsumerListener> frameReplacedListener;
927 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700928 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800929 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500930 int connectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -0700931 bool enableEglCpuThrottling = true;
John Reckd2c4a4a2024-02-07 10:31:09 -0500932 sp<Fence> lastQueuedFence;
933
Dan Stoza289ade12014-02-28 11:17:17 -0800934 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200935 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800936
937 if (mCore->mIsAbandoned) {
938 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
939 return NO_INIT;
940 }
941
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700942 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
943 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
944 return NO_INIT;
945 }
946
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800947 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800948 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800949 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800950 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700951 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800952 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700953 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800954 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800955 } else if (!mSlots[slot].mRequestBufferCalled) {
956 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
957 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800958 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800959 }
960
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700961 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700962 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700963 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700964 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700965 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700966 mSlots[slot].mBufferState.mShared = true;
967 }
968
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800969 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700970 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
971 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
972 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700973 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800974 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800975
976 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
977 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700978 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800979 crop.intersect(bufferRect, &croppedRect);
980 if (croppedRect != crop) {
981 BQ_LOGE("queueBuffer: crop rect is not contained within the "
982 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800983 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800984 }
985
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800986 // Override UNKNOWN dataspace with consumer default
987 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
988 dataSpace = mCore->mDefaultBufferDataSpace;
989 }
990
Brian Andersond6927fb2016-07-23 23:37:30 -0700991 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700992 mSlots[slot].mBufferState.queue();
993
Brian Andersond6927fb2016-07-23 23:37:30 -0700994 // Increment the frame counter and store a local version of it
995 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800996 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700997 currentFrameNumber = mCore->mFrameCounter;
998 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800999
Dan Stoza289ade12014-02-28 11:17:17 -08001000 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1001 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1002 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001003 item.mTransform = transform &
1004 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001005 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001006 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1007 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001008 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001009 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001010 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001011 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -07001012 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001013 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001014 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001015 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001016 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001017 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001018 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001019 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001020 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001021 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001022 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001023 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001024
Ruben Brunk1681d952014-06-27 15:51:55 -07001025 mStickyTransform = stickyTransform;
1026
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001027 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001028 if (mCore->mSharedBufferMode) {
1029 mCore->mSharedBufferCache.crop = crop;
1030 mCore->mSharedBufferCache.transform = transform;
1031 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001032 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001033 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001034 }
1035
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001036 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001037 if (mCore->mQueue.empty()) {
1038 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1039 // and simply queue this buffer
1040 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001041 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001042 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001043 // When the queue is not empty, we need to look at the last buffer
1044 // in the queue to see if we need to replace it
1045 const BufferItem& last = mCore->mQueue.itemAt(
1046 mCore->mQueue.size() - 1);
1047 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001048
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001049 if (!last.mIsStale) {
1050 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001051
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001052 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001053 // still be around. Mark it as no longer shared if this
1054 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001055 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001056 mSlots[last.mSlot].mBufferState.isFree()) {
1057 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001058 }
1059 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001060 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1061 mCore->mActiveBuffers.erase(last.mSlot);
1062 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001063 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001064 }
Dan Stoza289ade12014-02-28 11:17:17 -08001065 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001066
Steven Thomas862e7532019-07-10 19:21:03 -07001067 // Make sure to merge the damage rect from the frame we're about
1068 // to drop into the new frame's damage rect.
1069 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1070 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1071 item.mSurfaceDamage = Region::INVALID_REGION;
1072 } else {
1073 item.mSurfaceDamage |= last.mSurfaceDamage;
1074 }
1075
Dan Stoza289ade12014-02-28 11:17:17 -08001076 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001077 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001078 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001079 } else {
1080 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001081 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001082 }
1083 }
1084
1085 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001086 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001087 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001088
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001089 output->width = mCore->mDefaultWidth;
1090 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001091 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001092 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1093 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001094
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001095 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001096#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001097 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001098#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001099 // Take a ticket for the callback functions
1100 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001101
Pablo Ceballos9e314332016-01-12 13:49:19 -08001102 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001103
1104 connectedApi = mCore->mConnectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001105 if (flags::bq_producer_throttles_only_async_mode()) {
1106 enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1107 }
John Reckd2c4a4a2024-02-07 10:31:09 -05001108 lastQueuedFence = std::move(mLastQueueBufferFence);
1109
1110 mLastQueueBufferFence = std::move(acquireFence);
1111 mLastQueuedCrop = item.mCrop;
1112 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001113 } // Autolock scope
1114
Irvel468051e2016-06-13 16:44:44 -07001115 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1116 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1117 // there will be no Binder call
1118 if (!mConsumerIsSurfaceFlinger) {
1119 item.mGraphicBuffer.clear();
1120 }
1121
JihCheng Chiu544c5222019-04-02 20:50:58 +08001122 // Update and get FrameEventHistory.
1123 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1124 NewFrameEventsEntry newFrameEventsEntry = {
1125 currentFrameNumber,
1126 postedTime,
1127 requestedPresentTimestamp,
1128 std::move(acquireFenceTime)
1129 };
1130 addAndGetFrameTimestamps(&newFrameEventsEntry,
1131 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1132
Dan Stoza8dc55392014-11-04 11:37:46 -08001133 // Call back without the main BufferQueue lock held, but with the callback
1134 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001135
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001136 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001137 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001138 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001139 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001140 }
1141
Yi Kong48a619f2018-06-05 16:34:59 -07001142 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001143 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001144 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001145 frameReplacedListener->onFrameReplaced(item);
1146 }
1147
1148 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001149 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001150 }
1151
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001152 // Wait without lock held
Mathias Agopianb96661f2024-09-17 11:45:38 -07001153 if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001154 // Waiting here allows for two full buffers to be queued but not a
1155 // third. In the event that frames take varying time, this makes a
1156 // small trade-off in favor of latency rather than throughput.
1157 lastQueuedFence->waitForever("Throttling EGL Production");
1158 }
1159
Dan Stoza289ade12014-02-28 11:17:17 -08001160 return NO_ERROR;
1161}
1162
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001163status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001164 ATRACE_CALL();
1165 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001166
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001167 sp<IConsumerListener> listener;
1168 bool callOnFrameCancelled = false;
1169 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1170 {
1171 std::lock_guard<std::mutex> lock(mCore->mMutex);
1172
1173 if (mCore->mIsAbandoned) {
1174 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1175 return NO_INIT;
1176 }
1177
1178 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1179 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1180 return NO_INIT;
1181 }
1182
1183 if (mCore->mSharedBufferMode) {
1184 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1185 return BAD_VALUE;
1186 }
1187
1188 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1189 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1190 BufferQueueDefs::NUM_BUFFER_SLOTS);
1191 return BAD_VALUE;
1192 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1193 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1194 "(state = %s)",
1195 slot, mSlots[slot].mBufferState.string());
1196 return BAD_VALUE;
1197 } else if (fence == nullptr) {
1198 BQ_LOGE("cancelBuffer: fence is NULL");
1199 return BAD_VALUE;
1200 }
1201
1202 mSlots[slot].mBufferState.cancel();
1203
1204 // After leaving shared buffer mode, the shared buffer will still be around.
1205 // Mark it as no longer shared if this operation causes it to be free.
1206 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1207 mSlots[slot].mBufferState.mShared = false;
1208 }
1209
1210 // Don't put the shared buffer on the free list.
1211 if (!mSlots[slot].mBufferState.isShared()) {
1212 mCore->mActiveBuffers.erase(slot);
1213 mCore->mFreeBuffers.push_back(slot);
1214 }
1215
1216 auto gb = mSlots[slot].mGraphicBuffer;
1217 if (gb != nullptr) {
1218 callOnFrameCancelled = true;
1219 bufferId = gb->getId();
1220 }
1221 mSlots[slot].mFence = fence;
1222 mCore->mDequeueCondition.notify_all();
1223 listener = mCore->mConsumerListener;
1224 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001225 }
1226
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001227 if (listener != nullptr && callOnFrameCancelled) {
1228 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001229 }
1230
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001231 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001232}
1233
1234int BufferQueueProducer::query(int what, int *outValue) {
1235 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001236 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001237
Yi Kong48a619f2018-06-05 16:34:59 -07001238 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001239 BQ_LOGE("query: outValue was NULL");
1240 return BAD_VALUE;
1241 }
1242
1243 if (mCore->mIsAbandoned) {
1244 BQ_LOGE("query: BufferQueue has been abandoned");
1245 return NO_INIT;
1246 }
1247
1248 int value;
1249 switch (what) {
1250 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001251 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001252 break;
1253 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001254 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001255 break;
1256 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001257 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001258 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001259 case NATIVE_WINDOW_LAYER_COUNT:
1260 // All BufferQueue buffers have a single layer.
1261 value = BQ_LAYER_COUNT;
1262 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001263 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001264 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001265 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001266 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001267 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001268 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001269 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1270 value = (mCore->mQueue.size() > 1);
1271 break;
1272 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001273 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001274 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001275 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001276 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1277 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1278 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001279 case NATIVE_WINDOW_BUFFER_AGE:
1280 if (mCore->mBufferAge > INT32_MAX) {
1281 value = 0;
1282 } else {
1283 value = static_cast<int32_t>(mCore->mBufferAge);
1284 }
1285 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001286 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1287 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1288 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001289 default:
1290 return BAD_VALUE;
1291 }
1292
1293 BQ_LOGV("query: %d? %d", what, value);
1294 *outValue = value;
1295 return NO_ERROR;
1296}
1297
Dan Stozaf0eaf252014-03-21 13:05:51 -07001298status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001299 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1300 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001301 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001302 mConsumerName = mCore->mConsumerName;
1303 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1304 producerControlledByApp ? "true" : "false");
1305
1306 if (mCore->mIsAbandoned) {
1307 BQ_LOGE("connect: BufferQueue has been abandoned");
1308 return NO_INIT;
1309 }
1310
Yi Kong48a619f2018-06-05 16:34:59 -07001311 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001312 BQ_LOGE("connect: BufferQueue has no consumer");
1313 return NO_INIT;
1314 }
1315
Yi Kong48a619f2018-06-05 16:34:59 -07001316 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001317 BQ_LOGE("connect: output was NULL");
1318 return BAD_VALUE;
1319 }
1320
1321 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1322 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1323 mCore->mConnectedApi, api);
1324 return BAD_VALUE;
1325 }
1326
1327 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1328 mDequeueTimeout < 0 ?
1329 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1330 mCore->mMaxBufferCount) -
1331 mCore->getMaxBufferCountLocked();
1332 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1333 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1334 "slots. Delta = %d", delta);
1335 return BAD_VALUE;
1336 }
1337
Dan Stoza289ade12014-02-28 11:17:17 -08001338 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001339 switch (api) {
1340 case NATIVE_WINDOW_API_EGL:
1341 case NATIVE_WINDOW_API_CPU:
1342 case NATIVE_WINDOW_API_MEDIA:
1343 case NATIVE_WINDOW_API_CAMERA:
1344 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001345
1346 output->width = mCore->mDefaultWidth;
1347 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001348 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001349 output->numPendingBuffers =
1350 static_cast<uint32_t>(mCore->mQueue.size());
1351 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001352 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001353 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001354
Yi Kong48a619f2018-06-05 16:34:59 -07001355 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001356 // Set up a death notification so that we can disconnect
1357 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001358#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001359 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001360 status = IInterface::asBinder(listener)->linkToDeath(
1361 static_cast<IBinder::DeathRecipient*>(this));
1362 if (status != NO_ERROR) {
1363 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1364 strerror(-status), status);
1365 }
1366 mCore->mLinkedToDeath = listener;
1367 }
Chong Zhang62493092020-01-15 16:04:47 -08001368#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001369 mCore->mConnectedProducerListener = listener;
1370 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Sungtak Leecd217472024-07-19 17:17:40 +00001371#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1372 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1373#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001374 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001375 break;
1376 default:
1377 BQ_LOGE("connect: unknown API %d", api);
1378 status = BAD_VALUE;
1379 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001380 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001381 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001382 mCore->mBufferHasBeenQueued = false;
1383 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001384 mCore->mQueueBufferCanDrop = false;
1385 mCore->mLegacyBufferDrop = true;
1386 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1387 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1388 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001389 }
Dan Stoza289ade12014-02-28 11:17:17 -08001390
Pablo Ceballos981066c2016-02-18 12:54:37 -08001391 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001392#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1393 mCore->mAdditionalOptions.clear();
1394#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001395 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001396 return status;
1397}
1398
Robert Carr97b9c862016-09-08 13:54:35 -07001399status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001400 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001401 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001402
1403 int status = NO_ERROR;
1404 sp<IConsumerListener> listener;
1405 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001406 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001407
1408 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001409 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001410 return NO_ERROR;
1411 }
1412 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1413 }
1414
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001415 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001416
1417 if (mCore->mIsAbandoned) {
1418 // It's not really an error to disconnect after the surface has
1419 // been abandoned; it should just be a no-op.
1420 return NO_ERROR;
1421 }
1422
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001423 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001424 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1425 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1426 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001427 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001428 // If we're asked to disconnect the currently connected api but
1429 // nobody is connected, it's not really an error.
1430 if (api == BufferQueueCore::NO_CONNECTED_API) {
1431 return NO_ERROR;
1432 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001433 }
1434
Dan Stoza289ade12014-02-28 11:17:17 -08001435 switch (api) {
1436 case NATIVE_WINDOW_API_EGL:
1437 case NATIVE_WINDOW_API_CPU:
1438 case NATIVE_WINDOW_API_MEDIA:
1439 case NATIVE_WINDOW_API_CAMERA:
1440 if (mCore->mConnectedApi == api) {
1441 mCore->freeAllBuffersLocked();
1442
Chong Zhang62493092020-01-15 16:04:47 -08001443#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001444 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001445 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001446 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001447 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001448 // This can fail if we're here because of the death
1449 // notification, but we just ignore it
1450 token->unlinkToDeath(
1451 static_cast<IBinder::DeathRecipient*>(this));
1452 }
Chong Zhang62493092020-01-15 16:04:47 -08001453#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001454 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001455 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001456 mCore->mLinkedToDeath = nullptr;
1457 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001458 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001459 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001460 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001461 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001462 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001463#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1464 mCore->mAdditionalOptions.clear();
1465#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001466 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001467 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1468 BQ_LOGE("disconnect: not connected (req=%d)", api);
1469 status = NO_INIT;
1470 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001471 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001472 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001473 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001474 }
1475 break;
1476 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001477 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001478 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001479 break;
1480 }
1481 } // Autolock scope
1482
1483 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001484 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001485 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001486 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001487 }
1488
1489 return status;
1490}
1491
Jesse Hall399184a2014-03-03 15:42:54 -08001492status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001493 sp<IConsumerListener> listener;
1494 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001495 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001496 mCore->mSidebandStream = stream;
1497 listener = mCore->mConsumerListener;
1498 } // Autolock scope
1499
Yi Kong48a619f2018-06-05 16:34:59 -07001500 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001501 listener->onSidebandStreamChanged();
1502 }
Jesse Hall399184a2014-03-03 15:42:54 -08001503 return NO_ERROR;
1504}
1505
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001506void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001507 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001508 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001509
1510 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001511 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001512 size_t newBufferCount = 0;
1513 uint32_t allocWidth = 0;
1514 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001515 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001516 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001517 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001518#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1519 std::vector<gui::AdditionalOptions> allocOptions;
1520 uint32_t allocOptionsGenId = 0;
1521#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001522 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001523 std::unique_lock<std::mutex> lock(mCore->mMutex);
1524 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001525
Dan Stoza9de72932015-04-16 17:28:43 -07001526 if (!mCore->mAllowAllocation) {
1527 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1528 "BufferQueue");
1529 return;
1530 }
1531
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001532 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1533 // both allocateBuffers and dequeueBuffer.
1534 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001535 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001536 return;
1537 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001538
Antoine Labour78014f32014-07-15 21:17:03 -07001539 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1540 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001541 if (useDefaultSize && mCore->mAutoPrerotation &&
1542 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1543 std::swap(allocWidth, allocHeight);
1544 }
1545
Antoine Labour78014f32014-07-15 21:17:03 -07001546 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1547 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001548 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001549
John Reckdb164ff2024-04-03 16:59:28 -04001550#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1551 allocOptions = mCore->mAdditionalOptions;
1552 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1553#endif
1554
Antoine Labour78014f32014-07-15 21:17:03 -07001555 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001556
Antoine Labour78014f32014-07-15 21:17:03 -07001557 } // Autolock scope
1558
John Reckdb164ff2024-04-03 16:59:28 -04001559#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1560 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1561 tempOptions.reserve(allocOptions.size());
1562 for (const auto& it : allocOptions) {
1563 tempOptions.emplace_back(it.name.c_str(), it.value);
1564 }
1565 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1566 .importBuffer = true,
1567 .width = allocWidth,
1568 .height = allocHeight,
1569 .format = allocFormat,
1570 .layerCount = BQ_LAYER_COUNT,
1571 .usage = allocUsage,
1572 .requestorName = allocName,
1573 .extras = std::move(tempOptions),
1574 };
1575#endif
1576
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001577 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001578 for (size_t i = 0; i < newBufferCount; ++i) {
John Reckdb164ff2024-04-03 16:59:28 -04001579#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1580 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
1581#else
Mathias Agopian0556d792017-03-22 15:49:32 -07001582 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001583 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001584 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001585#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001586
1587 status_t result = graphicBuffer->initCheck();
1588
Antoine Labour78014f32014-07-15 21:17:03 -07001589 if (result != NO_ERROR) {
1590 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001591 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001592 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001593 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001594 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001595 return;
1596 }
1597 buffers.push_back(graphicBuffer);
1598 }
1599
1600 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001601 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001602 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1603 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001604 if (useDefaultSize && mCore->mAutoPrerotation &&
1605 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1606 std::swap(checkWidth, checkHeight);
1607 }
1608
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001609 PixelFormat checkFormat = format != 0 ?
1610 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001611 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001612 bool allocOptionsChanged = false;
1613#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1614 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1615#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001616 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001617 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001618 // Something changed while we released the lock. Retry.
1619 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1620 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001621 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001622 continue;
1623 }
1624
Antoine Labour78014f32014-07-15 21:17:03 -07001625 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001626 if (mCore->mFreeSlots.empty()) {
1627 BQ_LOGV("allocateBuffers: a slot was occupied while "
1628 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001629 continue;
1630 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001631 auto slot = mCore->mFreeSlots.begin();
1632 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1633 mSlots[*slot].mGraphicBuffer = buffers[i];
1634 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001635#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1636 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1637#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001638
1639 // freeBufferLocked puts this slot on the free slots list. Since
1640 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001641 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001642
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001643 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1644 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001645
1646 // Make sure the erase is done after all uses of the slot
1647 // iterator since it will be invalid after this point.
1648 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001649 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001650
Antoine Labour78014f32014-07-15 21:17:03 -07001651 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001652 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001653 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001654
1655 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1656 // waiting anymore so it can use the buffer we just allocated.
1657 while (mDequeueWaitingForAllocation) {
1658 mDequeueWaitingForAllocationCondition.wait(lock);
1659 }
Antoine Labour78014f32014-07-15 21:17:03 -07001660 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001661 }
1662}
1663
Dan Stoza9de72932015-04-16 17:28:43 -07001664status_t BufferQueueProducer::allowAllocation(bool allow) {
1665 ATRACE_CALL();
1666 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1667
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001668 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001669 mCore->mAllowAllocation = allow;
1670 return NO_ERROR;
1671}
1672
Dan Stoza812ed062015-06-02 15:45:22 -07001673status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1674 ATRACE_CALL();
1675 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1676
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001677 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001678 mCore->mGenerationNumber = generationNumber;
1679 return NO_ERROR;
1680}
1681
Dan Stozac6f30bd2015-06-08 09:32:50 -07001682String8 BufferQueueProducer::getConsumerName() const {
1683 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001684 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001685 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001686 return mConsumerName;
1687}
1688
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001689status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001690 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001691 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001692
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001693 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001694 if (!sharedBufferMode) {
1695 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001696 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001697 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001698 return NO_ERROR;
1699}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001700
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001701status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1702 ATRACE_CALL();
1703 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1704
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001705 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001706
1707 mCore->mAutoRefresh = autoRefresh;
1708 return NO_ERROR;
1709}
1710
Dan Stoza127fc632015-06-30 13:43:32 -07001711status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1712 ATRACE_CALL();
1713 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1714
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001715 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001716 bool dequeueBufferCannotBlock =
1717 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1718 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001719 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1720 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1721 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1722 "available slots. Delta = %d", delta);
1723 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001724 }
1725
Pablo Ceballos981066c2016-02-18 12:54:37 -08001726 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001727 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1728 if (timeout > 0) {
1729 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001730 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001731
Pablo Ceballos981066c2016-02-18 12:54:37 -08001732 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001733 return NO_ERROR;
1734}
1735
Sungtak Lee3249fb62019-03-02 16:40:47 -08001736status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1737 ATRACE_CALL();
1738 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1739
1740 std::lock_guard<std::mutex> lock(mCore->mMutex);
1741 mCore->mLegacyBufferDrop = drop;
1742 return NO_ERROR;
1743}
1744
Dan Stoza50101d02016-04-07 16:53:23 -07001745status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001746 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001747 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001748
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001749 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001750 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1751
Dan Stoza50101d02016-04-07 16:53:23 -07001752 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1753 *outBuffer = nullptr;
1754 *outFence = Fence::NO_FENCE;
1755 return NO_ERROR;
1756 }
1757
1758 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1759 *outFence = mLastQueueBufferFence;
1760
John Reck1a61da52016-04-28 13:18:15 -07001761 // Currently only SurfaceFlinger internally ever changes
1762 // GLConsumer's filtering mode, so we just use 'true' here as
1763 // this is slightly specialized for the current client of this API,
1764 // which does want filtering.
1765 GLConsumer::computeTransformMatrix(outTransformMatrix,
1766 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1767 mLastQueuedTransform, true /* filter */);
1768
Dan Stoza50101d02016-04-07 16:53:23 -07001769 return NO_ERROR;
1770}
1771
John Reckf0f13e82021-05-18 00:42:56 -04001772status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1773 Rect* outRect, uint32_t* outTransform) {
1774 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001775
1776 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001777 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1778 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1779 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001780 *outBuffer = nullptr;
1781 *outFence = Fence::NO_FENCE;
1782 return NO_ERROR;
1783 }
1784
1785 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1786 *outFence = mLastQueueBufferFence;
1787 *outRect = mLastQueuedCrop;
1788 *outTransform = mLastQueuedTransform;
1789
1790 return NO_ERROR;
1791}
1792
Brian Anderson3890c392016-07-25 12:48:08 -07001793void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1794 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001795}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001796
Brian Anderson3890c392016-07-25 12:48:08 -07001797void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001798 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001799 FrameEventHistoryDelta* outDelta) {
1800 if (newTimestamps == nullptr && outDelta == nullptr) {
1801 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001802 }
1803
1804 ATRACE_CALL();
1805 BQ_LOGV("addAndGetFrameTimestamps");
1806 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001807 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001808 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001809 listener = mCore->mConsumerListener;
1810 }
Yi Kong48a619f2018-06-05 16:34:59 -07001811 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001812 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001813 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001814}
1815
Dan Stoza289ade12014-02-28 11:17:17 -08001816void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1817 // If we're here, it means that a producer we were connected to died.
1818 // We're guaranteed that we are still connected to it because we remove
1819 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1820 // without synchronization here.
1821 int api = mCore->mConnectedApi;
1822 disconnect(api);
1823}
1824
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001825status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1826 BQ_LOGV("getUniqueId");
1827
1828 *outId = mCore->mUniqueId;
1829 return NO_ERROR;
1830}
1831
Chia-I Wue2786ea2017-08-07 10:36:08 -07001832status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1833 BQ_LOGV("getConsumerUsage");
1834
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001835 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001836 *outUsage = mCore->mConsumerUsageBits;
1837 return NO_ERROR;
1838}
1839
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001840status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1841 ATRACE_CALL();
1842 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1843
1844 std::lock_guard<std::mutex> lock(mCore->mMutex);
1845
1846 mCore->mAutoPrerotation = autoPrerotation;
1847 return NO_ERROR;
1848}
1849
Ady Abraham107788e2023-10-17 12:31:08 -07001850#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001851status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1852 int8_t changeFrameRateStrategy) {
1853 ATRACE_CALL();
1854 BQ_LOGV("setFrameRate: %.2f", frameRate);
1855
1856 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1857 "BufferQueueProducer::setFrameRate")) {
1858 return BAD_VALUE;
1859 }
1860
1861 sp<IConsumerListener> listener;
1862 {
1863 std::lock_guard<std::mutex> lock(mCore->mMutex);
1864 listener = mCore->mConsumerListener;
1865 }
1866 if (listener != nullptr) {
1867 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1868 }
1869 return NO_ERROR;
1870}
1871#endif
1872
John Reckdb164ff2024-04-03 16:59:28 -04001873#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1874status_t BufferQueueProducer::setAdditionalOptions(
1875 const std::vector<gui::AdditionalOptions>& options) {
1876 ATRACE_CALL();
1877 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1878
1879 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1880 return INVALID_OPERATION;
1881 }
1882
1883 std::lock_guard<std::mutex> lock(mCore->mMutex);
1884
1885 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1886 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1887 return NO_INIT;
1888 }
1889
1890 if (mCore->mAdditionalOptions != options) {
1891 mCore->mAdditionalOptions = options;
1892 mCore->mAdditionalOptionsGenerationId++;
1893 }
1894 return NO_ERROR;
1895}
1896#endif
1897
Dan Stoza289ade12014-02-28 11:17:17 -08001898} // namespace android