blob: da74e9cde686740ddf50703255f4b9531f8d5405 [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
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700170 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700171 bufferCount += maxDequeuedBuffers;
172
173 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
174 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
175 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
176 return BAD_VALUE;
177 }
178
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700179 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700180 if (bufferCount < minBufferSlots) {
181 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
182 "less than minimum %d", bufferCount, minBufferSlots);
183 return BAD_VALUE;
184 }
185
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700186 if (bufferCount > mCore->mMaxBufferCount) {
187 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700188 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
189 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
190 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
191 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700192 return BAD_VALUE;
193 }
194
Pablo Ceballos72daab62015-12-07 16:38:43 -0800195 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800196 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800197 return BAD_VALUE;
198 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700199 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700200 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800201 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800202 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800203 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800204 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200205 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700206 } // Autolock scope
207
208 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700209 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800210 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700211 }
212
213 return NO_ERROR;
214}
215
216status_t BufferQueueProducer::setAsyncMode(bool async) {
217 ATRACE_CALL();
218 BQ_LOGV("setAsyncMode: async = %d", async);
219
Pablo Ceballos981066c2016-02-18 12:54:37 -0800220 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700221 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200222 std::unique_lock<std::mutex> lock(mCore->mMutex);
223 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700224
225 if (mCore->mIsAbandoned) {
226 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
227 return NO_INIT;
228 }
229
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700230 if (async == mCore->mAsyncMode) {
231 return NO_ERROR;
232 }
233
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700234 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700235 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
236 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700237 BQ_LOGE("setAsyncMode(%d): this call would cause the "
238 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700239 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
240 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
241 mCore->mMaxDequeuedBufferCount,
242 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700243 return BAD_VALUE;
244 }
245
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 int delta = mCore->getMaxBufferCountLocked(async,
247 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
248 - mCore->getMaxBufferCountLocked();
249
Pablo Ceballos981066c2016-02-18 12:54:37 -0800250 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800251 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
252 "available slots. Delta = %d", delta);
253 return BAD_VALUE;
254 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700255 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800256 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200257 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700258 if (delta < 0) {
259 listener = mCore->mConsumerListener;
260 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700261 } // Autolock scope
262
263 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700264 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800265 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700266 }
267 return NO_ERROR;
268}
269
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270int BufferQueueProducer::getFreeBufferLocked() const {
271 if (mCore->mFreeBuffers.empty()) {
272 return BufferQueueCore::INVALID_BUFFER_SLOT;
273 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800274 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800275 mCore->mFreeBuffers.pop_front();
276 return slot;
277}
278
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800280 if (mCore->mFreeSlots.empty()) {
281 return BufferQueueCore::INVALID_BUFFER_SLOT;
282 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800283 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800284 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800285 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800286}
287
288status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200289 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
291 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800292 bool tryAgain = true;
293 while (tryAgain) {
294 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800295 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800296 return NO_INIT;
297 }
298
Dan Stoza9f3053d2014-03-06 15:14:33 -0800299 int dequeuedCount = 0;
300 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700302 if (mSlots[s].mBufferState.isDequeued()) {
303 ++dequeuedCount;
304 }
305 if (mSlots[s].mBufferState.isAcquired()) {
306 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800307 }
308 }
309
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700310 // Producers are not allowed to dequeue more than
311 // mMaxDequeuedBufferCount buffers.
312 // This check is only done if a buffer has already been queued
313 if (mCore->mBufferHasBeenQueued &&
314 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700315 // Supress error logs when timeout is non-negative.
316 if (mDequeueTimeout < 0) {
317 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
318 "count (%d)", callerString,
319 mCore->mMaxDequeuedBufferCount);
320 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800321 return INVALID_OPERATION;
322 }
323
Dan Stoza0de7ea72015-04-23 13:20:51 -0700324 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
325
Dan Stozaae3c3682014-04-18 15:43:35 -0700326 // If we disconnect and reconnect quickly, we can be in a state where
327 // our slots are empty but we have many buffers in the queue. This can
328 // cause us to run out of memory if we outrun the consumer. Wait here if
329 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800330 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700331 bool tooManyBuffers = mCore->mQueue.size()
332 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700333 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800334 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700335 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700336 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700337 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700338 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700339 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700340 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700341 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800342 } else {
343 if (caller == FreeSlotCaller::Dequeue) {
344 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800345 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800346 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
347 *found = slot;
348 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800349 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800350 }
351 } else {
352 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800353 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800354 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
355 *found = slot;
356 } else {
357 *found = getFreeBufferLocked();
358 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700359 }
360 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700361 }
362
363 // If no buffer is found, or if the queue has too many buffers
364 // outstanding, wait for a buffer to be acquired or released, or for the
365 // max buffer count to change.
366 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
367 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 if (tryAgain) {
369 // Return an error if we're in non-blocking mode (producer and
370 // consumer are controlled by the application).
371 // However, the consumer is allowed to briefly acquire an extra
372 // buffer (which could cause us to have to wait here), which is
373 // okay, since it is only used to implement an atomic acquire +
374 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700375 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800376 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
377 return WOULD_BLOCK;
378 }
Dan Stoza127fc632015-06-30 13:43:32 -0700379 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200380 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
381 std::chrono::nanoseconds(mDequeueTimeout));
382 if (result == std::cv_status::timeout) {
383 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700384 }
385 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200386 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700387 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800388 }
389 } // while (tryAgain)
390
391 return NO_ERROR;
392}
393
Ian Elliottd11b0442017-07-18 11:05:49 -0600394status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
395 uint32_t width, uint32_t height, PixelFormat format,
396 uint64_t usage, uint64_t* outBufferAge,
397 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800398 ATRACE_CALL();
399 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200400 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800401 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700402
403 if (mCore->mIsAbandoned) {
404 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
405 return NO_INIT;
406 }
407
408 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
409 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
410 return NO_INIT;
411 }
Dan Stoza289ade12014-02-28 11:17:17 -0800412 } // Autolock scope
413
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700414 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800415
416 if ((width && !height) || (!width && height)) {
417 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
418 return BAD_VALUE;
419 }
420
421 status_t returnFlags = NO_ERROR;
422 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
423 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800424 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800425
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000426 sp<IConsumerListener> listener;
427 bool callOnFrameDequeued = false;
428 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400429#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
430 std::vector<gui::AdditionalOptions> allocOptions;
431 uint32_t allocOptionsGenId = 0;
432#endif
433
Dan Stoza289ade12014-02-28 11:17:17 -0800434 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200435 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800436
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100437 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
438 // is finished such that we don't allocate in parallel.
439 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
440 mDequeueWaitingForAllocation = true;
441 mCore->waitWhileAllocatingLocked(lock);
442 mDequeueWaitingForAllocation = false;
443 mDequeueWaitingForAllocationCondition.notify_all();
444 }
Dan Stoza289ade12014-02-28 11:17:17 -0800445
446 if (format == 0) {
447 format = mCore->mDefaultBufferFormat;
448 }
449
450 // Enable the usage bits the consumer requested
451 usage |= mCore->mConsumerUsageBits;
452
Dan Stoza9de72932015-04-16 17:28:43 -0700453 const bool useDefaultSize = !width && !height;
454 if (useDefaultSize) {
455 width = mCore->mDefaultWidth;
456 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700457 if (mCore->mAutoPrerotation &&
458 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
459 std::swap(width, height);
460 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800461 }
Dan Stoza289ade12014-02-28 11:17:17 -0800462
Pablo Ceballos981066c2016-02-18 12:54:37 -0800463 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700464 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200465 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700466 if (status != NO_ERROR) {
467 return status;
468 }
469
470 // This should not happen
471 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
472 BQ_LOGE("dequeueBuffer: no available buffer slots");
473 return -EBUSY;
474 }
475
476 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
477
478 // If we are not allowed to allocate new buffers,
479 // waitForFreeSlotThenRelock must have returned a slot containing a
480 // buffer. If this buffer would require reallocation to meet the
481 // requested attributes, we free it and attempt to get another one.
482 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700483 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700484 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700485 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700486 return BAD_VALUE;
487 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800488 mCore->mFreeSlots.insert(found);
489 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700490 found = BufferItem::INVALID_BUFFER_SLOT;
491 continue;
492 }
493 }
Dan Stoza289ade12014-02-28 11:17:17 -0800494 }
495
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800496 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800497
John Reckdb164ff2024-04-03 16:59:28 -0400498 bool needsReallocation = buffer == nullptr ||
499 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
500
501#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
502 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
503 mCore->mAdditionalOptionsGenerationId;
504#endif
505
506 if (mCore->mSharedBufferSlot == found && needsReallocation) {
507 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800508 return BAD_VALUE;
509 }
510
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700511 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800512 mCore->mActiveBuffers.insert(found);
513 }
Dan Stoza289ade12014-02-28 11:17:17 -0800514 *outSlot = found;
515 ATRACE_BUFFER_INDEX(found);
516
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800517 attachedByConsumer = mSlots[found].mNeedsReallocation;
518 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800519
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700520 mSlots[found].mBufferState.dequeue();
521
John Reckdb164ff2024-04-03 16:59:28 -0400522 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700523 if (CC_UNLIKELY(ATRACE_ENABLED())) {
524 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000525 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700526 } else {
527 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
528 "layerCount:%d "
529 "usage:%d requested: %dx%d format:%d layerCount:%d "
530 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000531 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700532 BQ_LAYER_COUNT, usage, buffer->getWidth(),
533 buffer->getHeight(), buffer->getPixelFormat(),
534 buffer->getLayerCount(), buffer->getUsage());
535 }
536 }
Dan Stoza289ade12014-02-28 11:17:17 -0800537 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700538 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800539 mSlots[found].mRequestBufferCalled = false;
540 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
541 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
542 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800543 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700544 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400545#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
546 allocOptions = mCore->mAdditionalOptions;
547 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
548#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800549
550 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800551 } else {
552 // We add 1 because that will be the frame number when this buffer
553 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700554 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800555 }
556
Dan Stoza800b41a2015-04-28 14:20:04 -0700557 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
558 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800559
Yi Kong48a619f2018-06-05 16:34:59 -0700560 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800561 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
562 "slot=%d w=%d h=%d format=%u",
563 found, buffer->width, buffer->height, buffer->format);
564 }
565
566 eglDisplay = mSlots[found].mEglDisplay;
567 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700568 // Don't return a fence in shared buffer mode, except for the first
569 // frame.
570 *outFence = (mCore->mSharedBufferMode &&
571 mCore->mSharedBufferSlot == found) ?
572 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800573 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
574 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700575
576 // If shared buffer mode has just been enabled, cache the slot of the
577 // first buffer that is dequeued and mark it as the shared buffer.
578 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
579 BufferQueueCore::INVALID_BUFFER_SLOT) {
580 mCore->mSharedBufferSlot = found;
581 mSlots[found].mBufferState.mShared = true;
582 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700583
584 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000585 callOnFrameDequeued = true;
586 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700587 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000588
589 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800590 } // Autolock scope
591
592 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700593 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400594
595#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
596 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
597 tempOptions.reserve(allocOptions.size());
598 for (const auto& it : allocOptions) {
599 tempOptions.emplace_back(it.name.c_str(), it.value);
600 }
601 const GraphicBufferAllocator::AllocationRequest allocRequest = {
602 .importBuffer = true,
603 .width = width,
604 .height = height,
605 .format = format,
606 .layerCount = BQ_LAYER_COUNT,
607 .usage = usage,
608 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
609 .extras = std::move(tempOptions),
610 };
611 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
612#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000613 sp<GraphicBuffer> graphicBuffer =
614 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
615 {mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400616#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700617
618 status_t error = graphicBuffer->initCheck();
619
Dan Stoza289ade12014-02-28 11:17:17 -0800620 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200621 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800622
Chia-I Wufeec3b12017-05-25 09:34:56 -0700623 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700624 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
625 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400626#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
627 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
628#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000629 callOnFrameDequeued = true;
630 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700631 }
632
633 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200634 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700635
Chia-I Wufeec3b12017-05-25 09:34:56 -0700636 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700637 mCore->mFreeSlots.insert(*outSlot);
638 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700639 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
640 return error;
641 }
642
Dan Stoza289ade12014-02-28 11:17:17 -0800643 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700644 mCore->mFreeSlots.insert(*outSlot);
645 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800646 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
647 return NO_INIT;
648 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800649
Pablo Ceballos9e314332016-01-12 13:49:19 -0800650 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800651 } // Autolock scope
652 }
653
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000654 if (listener != nullptr && callOnFrameDequeued) {
655 listener->onFrameDequeued(bufferId);
656 }
657
Dan Stoza9f3053d2014-03-06 15:14:33 -0800658 if (attachedByConsumer) {
659 returnFlags |= BUFFER_NEEDS_REALLOCATION;
660 }
661
Dan Stoza289ade12014-02-28 11:17:17 -0800662 if (eglFence != EGL_NO_SYNC_KHR) {
663 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
664 1000000000);
665 // If something goes wrong, log the error, but return the buffer without
666 // synchronizing access to it. It's too late at this point to abort the
667 // dequeue operation.
668 if (result == EGL_FALSE) {
669 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
670 eglGetError());
671 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
672 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
673 }
674 eglDestroySyncKHR(eglDisplay, eglFence);
675 }
676
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700677 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
678 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800679 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800680 mSlots[*outSlot].mGraphicBuffer != nullptr ?
681 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800682
Ian Elliottd11b0442017-07-18 11:05:49 -0600683 if (outBufferAge) {
684 *outBufferAge = mCore->mBufferAge;
685 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700686 addAndGetFrameTimestamps(nullptr, outTimestamps);
687
Dan Stoza289ade12014-02-28 11:17:17 -0800688 return returnFlags;
689}
690
Dan Stoza9f3053d2014-03-06 15:14:33 -0800691status_t BufferQueueProducer::detachBuffer(int slot) {
692 ATRACE_CALL();
693 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700694 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800695
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700696 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000697 bool callOnFrameDetached = false;
698 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700699 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200700 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700701
702 if (mCore->mIsAbandoned) {
703 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
704 return NO_INIT;
705 }
706
707 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
708 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
709 return NO_INIT;
710 }
711
712 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
713 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
714 return BAD_VALUE;
715 }
716
717 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
718 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
719 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
720 return BAD_VALUE;
721 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000722 // TODO(http://b/140581935): This message is BQ_LOGW because it
723 // often logs when no actionable errors are present. Return to
724 // using BQ_LOGE after ensuring this only logs during errors.
725 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700726 "(state = %s)", slot, mSlots[slot].mBufferState.string());
727 return BAD_VALUE;
728 } else if (!mSlots[slot].mRequestBufferCalled) {
729 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
730 slot);
731 return BAD_VALUE;
732 }
733
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700734 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700735 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000736 if (gb != nullptr) {
737 callOnFrameDetached = true;
738 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700739 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700740 mSlots[slot].mBufferState.detachProducer();
741 mCore->mActiveBuffers.erase(slot);
742 mCore->mFreeSlots.insert(slot);
743 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200744 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700745 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800746 }
747
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000748 if (listener != nullptr && callOnFrameDetached) {
749 listener->onFrameDetached(bufferId);
750 }
751
Yi Kong48a619f2018-06-05 16:34:59 -0700752 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700753 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700754 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800755
Dan Stoza9f3053d2014-03-06 15:14:33 -0800756 return NO_ERROR;
757}
758
Dan Stozad9822a32014-03-28 15:25:31 -0700759status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
760 sp<Fence>* outFence) {
761 ATRACE_CALL();
762
Yi Kong48a619f2018-06-05 16:34:59 -0700763 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700764 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
765 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700766 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700767 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
768 return BAD_VALUE;
769 }
770
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700771 sp<IConsumerListener> listener;
772 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200773 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700774
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700775 if (mCore->mIsAbandoned) {
776 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
777 return NO_INIT;
778 }
779
780 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
781 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
782 return NO_INIT;
783 }
784
785 if (mCore->mSharedBufferMode) {
786 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
787 "mode");
788 return BAD_VALUE;
789 }
790
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200791 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700792
793 if (mCore->mFreeBuffers.empty()) {
794 return NO_MEMORY;
795 }
796
797 int found = mCore->mFreeBuffers.front();
798 mCore->mFreeBuffers.remove(found);
799 mCore->mFreeSlots.insert(found);
800
801 BQ_LOGV("detachNextBuffer detached slot %d", found);
802
803 *outBuffer = mSlots[found].mGraphicBuffer;
804 *outFence = mSlots[found].mFence;
805 mCore->clearBufferSlotLocked(found);
806 VALIDATE_CONSISTENCY();
807 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700808 }
809
Yi Kong48a619f2018-06-05 16:34:59 -0700810 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700811 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700812 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800813
Dan Stozad9822a32014-03-28 15:25:31 -0700814 return NO_ERROR;
815}
816
Dan Stoza9f3053d2014-03-06 15:14:33 -0800817status_t BufferQueueProducer::attachBuffer(int* outSlot,
818 const sp<android::GraphicBuffer>& buffer) {
819 ATRACE_CALL();
820
Yi Kong48a619f2018-06-05 16:34:59 -0700821 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700822 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800823 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700824 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700825 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800826 return BAD_VALUE;
827 }
828
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200829 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700830
831 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700832 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700833 return NO_INIT;
834 }
835
836 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700837 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700838 return NO_INIT;
839 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800840
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700841 if (mCore->mSharedBufferMode) {
842 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700843 return BAD_VALUE;
844 }
845
Dan Stoza812ed062015-06-02 15:45:22 -0700846 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
847 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
848 "[queue %u]", buffer->getGenerationNumber(),
849 mCore->mGenerationNumber);
850 return BAD_VALUE;
851 }
852
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200853 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700854
Dan Stoza9f3053d2014-03-06 15:14:33 -0800855 status_t returnFlags = NO_ERROR;
856 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200857 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800858 if (status != NO_ERROR) {
859 return status;
860 }
861
862 // This should not happen
863 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700864 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800865 return -EBUSY;
866 }
867
868 *outSlot = found;
869 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700870 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800871 *outSlot, returnFlags);
872
873 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700874 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800875 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
876 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700877 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800878 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800879 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800880 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800881 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700882
Dan Stoza9f3053d2014-03-06 15:14:33 -0800883 return returnFlags;
884}
885
Dan Stoza289ade12014-02-28 11:17:17 -0800886status_t BufferQueueProducer::queueBuffer(int slot,
887 const QueueBufferInput &input, QueueBufferOutput *output) {
888 ATRACE_CALL();
889 ATRACE_BUFFER_INDEX(slot);
890
Brian Andersond6927fb2016-07-23 23:37:30 -0700891 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800892 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800893 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700894 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800895 int scalingMode;
896 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700897 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700898 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700899 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700900 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700901 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
902 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700903 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700904 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800905
Yi Kong48a619f2018-06-05 16:34:59 -0700906 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800907 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800908 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800909 }
910
Brian Anderson3d4039d2016-09-23 16:31:30 -0700911 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
912
Dan Stoza289ade12014-02-28 11:17:17 -0800913 switch (scalingMode) {
914 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
915 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
916 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
917 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
918 break;
919 default:
920 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800921 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800922 }
923
Dan Stoza8dc55392014-11-04 11:37:46 -0800924 sp<IConsumerListener> frameAvailableListener;
925 sp<IConsumerListener> frameReplacedListener;
926 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700927 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800928 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500929 int connectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -0700930 bool enableEglCpuThrottling = true;
John Reckd2c4a4a2024-02-07 10:31:09 -0500931 sp<Fence> lastQueuedFence;
932
Dan Stoza289ade12014-02-28 11:17:17 -0800933 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200934 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800935
936 if (mCore->mIsAbandoned) {
937 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
938 return NO_INIT;
939 }
940
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700941 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
942 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
943 return NO_INIT;
944 }
945
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800946 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800947 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800948 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800949 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700950 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800951 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700952 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800953 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800954 } else if (!mSlots[slot].mRequestBufferCalled) {
955 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
956 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800957 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800958 }
959
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700960 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700961 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700962 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700963 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700964 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700965 mSlots[slot].mBufferState.mShared = true;
966 }
967
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800968 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700969 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
970 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
971 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700972 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800973 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800974
975 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
976 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700977 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800978 crop.intersect(bufferRect, &croppedRect);
979 if (croppedRect != crop) {
980 BQ_LOGE("queueBuffer: crop rect is not contained within the "
981 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800982 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800983 }
984
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800985 // Override UNKNOWN dataspace with consumer default
986 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
987 dataSpace = mCore->mDefaultBufferDataSpace;
988 }
989
Brian Andersond6927fb2016-07-23 23:37:30 -0700990 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700991 mSlots[slot].mBufferState.queue();
992
Brian Andersond6927fb2016-07-23 23:37:30 -0700993 // Increment the frame counter and store a local version of it
994 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800995 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700996 currentFrameNumber = mCore->mFrameCounter;
997 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800998
Dan Stoza289ade12014-02-28 11:17:17 -0800999 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1000 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1001 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001002 item.mTransform = transform &
1003 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001004 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001005 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1006 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001007 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001008 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001009 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001010 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -07001011 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001012 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001013 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001014 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001015 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001016 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001017 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001018 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001019 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001020 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001021 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001022 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001023
Ruben Brunk1681d952014-06-27 15:51:55 -07001024 mStickyTransform = stickyTransform;
1025
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001026 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001027 if (mCore->mSharedBufferMode) {
1028 mCore->mSharedBufferCache.crop = crop;
1029 mCore->mSharedBufferCache.transform = transform;
1030 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001031 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001032 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001033 }
1034
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001035 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001036 if (mCore->mQueue.empty()) {
1037 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1038 // and simply queue this buffer
1039 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001040 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001041 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001042 // When the queue is not empty, we need to look at the last buffer
1043 // in the queue to see if we need to replace it
1044 const BufferItem& last = mCore->mQueue.itemAt(
1045 mCore->mQueue.size() - 1);
1046 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001047
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001048 if (!last.mIsStale) {
1049 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001050
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001051 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001052 // still be around. Mark it as no longer shared if this
1053 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001054 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001055 mSlots[last.mSlot].mBufferState.isFree()) {
1056 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001057 }
1058 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001059 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1060 mCore->mActiveBuffers.erase(last.mSlot);
1061 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001062 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001063 }
Dan Stoza289ade12014-02-28 11:17:17 -08001064 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001065
Steven Thomas862e7532019-07-10 19:21:03 -07001066 // Make sure to merge the damage rect from the frame we're about
1067 // to drop into the new frame's damage rect.
1068 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1069 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1070 item.mSurfaceDamage = Region::INVALID_REGION;
1071 } else {
1072 item.mSurfaceDamage |= last.mSurfaceDamage;
1073 }
1074
Dan Stoza289ade12014-02-28 11:17:17 -08001075 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001076 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001077 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001078 } else {
1079 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001080 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001081 }
1082 }
1083
1084 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001085 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001086 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001087
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001088 output->width = mCore->mDefaultWidth;
1089 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001090 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001091 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1092 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001093
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001094 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001095#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001096 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001097#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001098 // Take a ticket for the callback functions
1099 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001100
Pablo Ceballos9e314332016-01-12 13:49:19 -08001101 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001102
1103 connectedApi = mCore->mConnectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001104 if (flags::bq_producer_throttles_only_async_mode()) {
1105 enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1106 }
John Reckd2c4a4a2024-02-07 10:31:09 -05001107 lastQueuedFence = std::move(mLastQueueBufferFence);
1108
1109 mLastQueueBufferFence = std::move(acquireFence);
1110 mLastQueuedCrop = item.mCrop;
1111 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001112 } // Autolock scope
1113
Irvel468051e2016-06-13 16:44:44 -07001114 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1115 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1116 // there will be no Binder call
1117 if (!mConsumerIsSurfaceFlinger) {
1118 item.mGraphicBuffer.clear();
1119 }
1120
JihCheng Chiu544c5222019-04-02 20:50:58 +08001121 // Update and get FrameEventHistory.
1122 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1123 NewFrameEventsEntry newFrameEventsEntry = {
1124 currentFrameNumber,
1125 postedTime,
1126 requestedPresentTimestamp,
1127 std::move(acquireFenceTime)
1128 };
1129 addAndGetFrameTimestamps(&newFrameEventsEntry,
1130 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1131
Dan Stoza8dc55392014-11-04 11:37:46 -08001132 // Call back without the main BufferQueue lock held, but with the callback
1133 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001134
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001135 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001136 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001137 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001138 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001139 }
1140
Yi Kong48a619f2018-06-05 16:34:59 -07001141 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001142 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001143 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001144 frameReplacedListener->onFrameReplaced(item);
1145 }
1146
1147 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001148 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001149 }
1150
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001151 // Wait without lock held
Mathias Agopianb96661f2024-09-17 11:45:38 -07001152 if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001153 // Waiting here allows for two full buffers to be queued but not a
1154 // third. In the event that frames take varying time, this makes a
1155 // small trade-off in favor of latency rather than throughput.
1156 lastQueuedFence->waitForever("Throttling EGL Production");
1157 }
1158
Dan Stoza289ade12014-02-28 11:17:17 -08001159 return NO_ERROR;
1160}
1161
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001162status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001163 ATRACE_CALL();
1164 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001165
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001166 sp<IConsumerListener> listener;
1167 bool callOnFrameCancelled = false;
1168 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1169 {
1170 std::lock_guard<std::mutex> lock(mCore->mMutex);
1171
1172 if (mCore->mIsAbandoned) {
1173 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1174 return NO_INIT;
1175 }
1176
1177 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1178 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1179 return NO_INIT;
1180 }
1181
1182 if (mCore->mSharedBufferMode) {
1183 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1184 return BAD_VALUE;
1185 }
1186
1187 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1188 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1189 BufferQueueDefs::NUM_BUFFER_SLOTS);
1190 return BAD_VALUE;
1191 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1192 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1193 "(state = %s)",
1194 slot, mSlots[slot].mBufferState.string());
1195 return BAD_VALUE;
1196 } else if (fence == nullptr) {
1197 BQ_LOGE("cancelBuffer: fence is NULL");
1198 return BAD_VALUE;
1199 }
1200
1201 mSlots[slot].mBufferState.cancel();
1202
1203 // After leaving shared buffer mode, the shared buffer will still be around.
1204 // Mark it as no longer shared if this operation causes it to be free.
1205 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1206 mSlots[slot].mBufferState.mShared = false;
1207 }
1208
1209 // Don't put the shared buffer on the free list.
1210 if (!mSlots[slot].mBufferState.isShared()) {
1211 mCore->mActiveBuffers.erase(slot);
1212 mCore->mFreeBuffers.push_back(slot);
1213 }
1214
1215 auto gb = mSlots[slot].mGraphicBuffer;
1216 if (gb != nullptr) {
1217 callOnFrameCancelled = true;
1218 bufferId = gb->getId();
1219 }
1220 mSlots[slot].mFence = fence;
1221 mCore->mDequeueCondition.notify_all();
1222 listener = mCore->mConsumerListener;
1223 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001224 }
1225
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001226 if (listener != nullptr && callOnFrameCancelled) {
1227 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001228 }
1229
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001230 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001231}
1232
1233int BufferQueueProducer::query(int what, int *outValue) {
1234 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001235 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001236
Yi Kong48a619f2018-06-05 16:34:59 -07001237 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001238 BQ_LOGE("query: outValue was NULL");
1239 return BAD_VALUE;
1240 }
1241
1242 if (mCore->mIsAbandoned) {
1243 BQ_LOGE("query: BufferQueue has been abandoned");
1244 return NO_INIT;
1245 }
1246
1247 int value;
1248 switch (what) {
1249 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001250 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001251 break;
1252 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001253 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001254 break;
1255 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001256 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001257 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001258 case NATIVE_WINDOW_LAYER_COUNT:
1259 // All BufferQueue buffers have a single layer.
1260 value = BQ_LAYER_COUNT;
1261 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001262 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001263 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001264 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001265 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001266 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001267 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001268 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1269 value = (mCore->mQueue.size() > 1);
1270 break;
1271 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001272 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001273 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001274 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001275 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1276 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1277 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001278 case NATIVE_WINDOW_BUFFER_AGE:
1279 if (mCore->mBufferAge > INT32_MAX) {
1280 value = 0;
1281 } else {
1282 value = static_cast<int32_t>(mCore->mBufferAge);
1283 }
1284 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001285 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1286 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1287 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001288 default:
1289 return BAD_VALUE;
1290 }
1291
1292 BQ_LOGV("query: %d? %d", what, value);
1293 *outValue = value;
1294 return NO_ERROR;
1295}
1296
Dan Stozaf0eaf252014-03-21 13:05:51 -07001297status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001298 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1299 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001300 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001301 mConsumerName = mCore->mConsumerName;
1302 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1303 producerControlledByApp ? "true" : "false");
1304
1305 if (mCore->mIsAbandoned) {
1306 BQ_LOGE("connect: BufferQueue has been abandoned");
1307 return NO_INIT;
1308 }
1309
Yi Kong48a619f2018-06-05 16:34:59 -07001310 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001311 BQ_LOGE("connect: BufferQueue has no consumer");
1312 return NO_INIT;
1313 }
1314
Yi Kong48a619f2018-06-05 16:34:59 -07001315 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001316 BQ_LOGE("connect: output was NULL");
1317 return BAD_VALUE;
1318 }
1319
1320 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1321 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1322 mCore->mConnectedApi, api);
1323 return BAD_VALUE;
1324 }
1325
1326 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1327 mDequeueTimeout < 0 ?
1328 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1329 mCore->mMaxBufferCount) -
1330 mCore->getMaxBufferCountLocked();
1331 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1332 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1333 "slots. Delta = %d", delta);
1334 return BAD_VALUE;
1335 }
1336
Dan Stoza289ade12014-02-28 11:17:17 -08001337 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001338 switch (api) {
1339 case NATIVE_WINDOW_API_EGL:
1340 case NATIVE_WINDOW_API_CPU:
1341 case NATIVE_WINDOW_API_MEDIA:
1342 case NATIVE_WINDOW_API_CAMERA:
1343 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001344
1345 output->width = mCore->mDefaultWidth;
1346 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001347 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001348 output->numPendingBuffers =
1349 static_cast<uint32_t>(mCore->mQueue.size());
1350 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001351 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001352 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001353
Yi Kong48a619f2018-06-05 16:34:59 -07001354 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001355 // Set up a death notification so that we can disconnect
1356 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001357#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001358 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001359 status = IInterface::asBinder(listener)->linkToDeath(
1360 static_cast<IBinder::DeathRecipient*>(this));
1361 if (status != NO_ERROR) {
1362 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1363 strerror(-status), status);
1364 }
1365 mCore->mLinkedToDeath = listener;
1366 }
Chong Zhang62493092020-01-15 16:04:47 -08001367#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001368 mCore->mConnectedProducerListener = listener;
1369 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Sungtak Leecd217472024-07-19 17:17:40 +00001370#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1371 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1372#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001373 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001374 break;
1375 default:
1376 BQ_LOGE("connect: unknown API %d", api);
1377 status = BAD_VALUE;
1378 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001379 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001380 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001381 mCore->mBufferHasBeenQueued = false;
1382 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001383 mCore->mQueueBufferCanDrop = false;
1384 mCore->mLegacyBufferDrop = true;
1385 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1386 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1387 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001388 }
Dan Stoza289ade12014-02-28 11:17:17 -08001389
Pablo Ceballos981066c2016-02-18 12:54:37 -08001390 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001391#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1392 mCore->mAdditionalOptions.clear();
1393#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001394 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001395 return status;
1396}
1397
Robert Carr97b9c862016-09-08 13:54:35 -07001398status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001399 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001400 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001401
1402 int status = NO_ERROR;
1403 sp<IConsumerListener> listener;
1404 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001405 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001406
1407 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001408 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001409 return NO_ERROR;
1410 }
1411 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1412 }
1413
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001414 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001415
1416 if (mCore->mIsAbandoned) {
1417 // It's not really an error to disconnect after the surface has
1418 // been abandoned; it should just be a no-op.
1419 return NO_ERROR;
1420 }
1421
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001422 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001423 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1424 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1425 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001426 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001427 // If we're asked to disconnect the currently connected api but
1428 // nobody is connected, it's not really an error.
1429 if (api == BufferQueueCore::NO_CONNECTED_API) {
1430 return NO_ERROR;
1431 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001432 }
1433
Dan Stoza289ade12014-02-28 11:17:17 -08001434 switch (api) {
1435 case NATIVE_WINDOW_API_EGL:
1436 case NATIVE_WINDOW_API_CPU:
1437 case NATIVE_WINDOW_API_MEDIA:
1438 case NATIVE_WINDOW_API_CAMERA:
1439 if (mCore->mConnectedApi == api) {
1440 mCore->freeAllBuffersLocked();
1441
Chong Zhang62493092020-01-15 16:04:47 -08001442#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001443 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001444 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001445 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001446 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001447 // This can fail if we're here because of the death
1448 // notification, but we just ignore it
1449 token->unlinkToDeath(
1450 static_cast<IBinder::DeathRecipient*>(this));
1451 }
Chong Zhang62493092020-01-15 16:04:47 -08001452#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001453 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001454 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001455 mCore->mLinkedToDeath = nullptr;
1456 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001457 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001458 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001459 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001460 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001461 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001462#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1463 mCore->mAdditionalOptions.clear();
1464#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001465 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001466 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1467 BQ_LOGE("disconnect: not connected (req=%d)", api);
1468 status = NO_INIT;
1469 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001470 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001471 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001472 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001473 }
1474 break;
1475 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001476 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001477 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001478 break;
1479 }
1480 } // Autolock scope
1481
1482 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001483 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001484 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001485 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001486 }
1487
1488 return status;
1489}
1490
Jesse Hall399184a2014-03-03 15:42:54 -08001491status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001492 sp<IConsumerListener> listener;
1493 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001494 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001495 mCore->mSidebandStream = stream;
1496 listener = mCore->mConsumerListener;
1497 } // Autolock scope
1498
Yi Kong48a619f2018-06-05 16:34:59 -07001499 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001500 listener->onSidebandStreamChanged();
1501 }
Jesse Hall399184a2014-03-03 15:42:54 -08001502 return NO_ERROR;
1503}
1504
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001505void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001506 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001507 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001508
1509 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001510 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001511 size_t newBufferCount = 0;
1512 uint32_t allocWidth = 0;
1513 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001514 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001515 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001516 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001517#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1518 std::vector<gui::AdditionalOptions> allocOptions;
1519 uint32_t allocOptionsGenId = 0;
1520#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001521 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001522 std::unique_lock<std::mutex> lock(mCore->mMutex);
1523 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001524
Dan Stoza9de72932015-04-16 17:28:43 -07001525 if (!mCore->mAllowAllocation) {
1526 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1527 "BufferQueue");
1528 return;
1529 }
1530
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001531 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1532 // both allocateBuffers and dequeueBuffer.
1533 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001534 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001535 return;
1536 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001537
Antoine Labour78014f32014-07-15 21:17:03 -07001538 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1539 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001540 if (useDefaultSize && mCore->mAutoPrerotation &&
1541 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1542 std::swap(allocWidth, allocHeight);
1543 }
1544
Antoine Labour78014f32014-07-15 21:17:03 -07001545 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1546 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001547 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001548
John Reckdb164ff2024-04-03 16:59:28 -04001549#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1550 allocOptions = mCore->mAdditionalOptions;
1551 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1552#endif
1553
Antoine Labour78014f32014-07-15 21:17:03 -07001554 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001555
Antoine Labour78014f32014-07-15 21:17:03 -07001556 } // Autolock scope
1557
John Reckdb164ff2024-04-03 16:59:28 -04001558#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1559 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1560 tempOptions.reserve(allocOptions.size());
1561 for (const auto& it : allocOptions) {
1562 tempOptions.emplace_back(it.name.c_str(), it.value);
1563 }
1564 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1565 .importBuffer = true,
1566 .width = allocWidth,
1567 .height = allocHeight,
1568 .format = allocFormat,
1569 .layerCount = BQ_LAYER_COUNT,
1570 .usage = allocUsage,
1571 .requestorName = allocName,
1572 .extras = std::move(tempOptions),
1573 };
1574#endif
1575
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001576 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001577 for (size_t i = 0; i < newBufferCount; ++i) {
John Reckdb164ff2024-04-03 16:59:28 -04001578#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1579 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
1580#else
Mathias Agopian0556d792017-03-22 15:49:32 -07001581 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001582 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001583 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001584#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001585
1586 status_t result = graphicBuffer->initCheck();
1587
Antoine Labour78014f32014-07-15 21:17:03 -07001588 if (result != NO_ERROR) {
1589 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001590 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001591 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001592 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001593 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001594 return;
1595 }
1596 buffers.push_back(graphicBuffer);
1597 }
1598
1599 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001600 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001601 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1602 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001603 if (useDefaultSize && mCore->mAutoPrerotation &&
1604 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1605 std::swap(checkWidth, checkHeight);
1606 }
1607
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001608 PixelFormat checkFormat = format != 0 ?
1609 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001610 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001611 bool allocOptionsChanged = false;
1612#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1613 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1614#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001615 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001616 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001617 // Something changed while we released the lock. Retry.
1618 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1619 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001620 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001621 continue;
1622 }
1623
Antoine Labour78014f32014-07-15 21:17:03 -07001624 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001625 if (mCore->mFreeSlots.empty()) {
1626 BQ_LOGV("allocateBuffers: a slot was occupied while "
1627 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001628 continue;
1629 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001630 auto slot = mCore->mFreeSlots.begin();
1631 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1632 mSlots[*slot].mGraphicBuffer = buffers[i];
1633 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001634#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1635 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1636#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001637
1638 // freeBufferLocked puts this slot on the free slots list. Since
1639 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001640 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001641
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001642 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1643 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001644
1645 // Make sure the erase is done after all uses of the slot
1646 // iterator since it will be invalid after this point.
1647 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001648 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001649
Antoine Labour78014f32014-07-15 21:17:03 -07001650 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001651 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001652 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001653
1654 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1655 // waiting anymore so it can use the buffer we just allocated.
1656 while (mDequeueWaitingForAllocation) {
1657 mDequeueWaitingForAllocationCondition.wait(lock);
1658 }
Antoine Labour78014f32014-07-15 21:17:03 -07001659 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001660 }
1661}
1662
Dan Stoza9de72932015-04-16 17:28:43 -07001663status_t BufferQueueProducer::allowAllocation(bool allow) {
1664 ATRACE_CALL();
1665 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1666
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001667 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001668 mCore->mAllowAllocation = allow;
1669 return NO_ERROR;
1670}
1671
Dan Stoza812ed062015-06-02 15:45:22 -07001672status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1673 ATRACE_CALL();
1674 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1675
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001676 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001677 mCore->mGenerationNumber = generationNumber;
1678 return NO_ERROR;
1679}
1680
Dan Stozac6f30bd2015-06-08 09:32:50 -07001681String8 BufferQueueProducer::getConsumerName() const {
1682 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001683 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001684 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001685 return mConsumerName;
1686}
1687
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001688status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001689 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001690 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001691
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001692 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001693 if (!sharedBufferMode) {
1694 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001695 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001696 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001697 return NO_ERROR;
1698}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001699
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001700status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1701 ATRACE_CALL();
1702 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1703
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001704 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001705
1706 mCore->mAutoRefresh = autoRefresh;
1707 return NO_ERROR;
1708}
1709
Dan Stoza127fc632015-06-30 13:43:32 -07001710status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1711 ATRACE_CALL();
1712 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1713
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001714 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001715 bool dequeueBufferCannotBlock =
1716 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1717 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001718 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1719 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1720 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1721 "available slots. Delta = %d", delta);
1722 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001723 }
1724
Pablo Ceballos981066c2016-02-18 12:54:37 -08001725 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001726 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1727 if (timeout > 0) {
1728 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001729 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001730
Pablo Ceballos981066c2016-02-18 12:54:37 -08001731 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001732 return NO_ERROR;
1733}
1734
Sungtak Lee3249fb62019-03-02 16:40:47 -08001735status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1736 ATRACE_CALL();
1737 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1738
1739 std::lock_guard<std::mutex> lock(mCore->mMutex);
1740 mCore->mLegacyBufferDrop = drop;
1741 return NO_ERROR;
1742}
1743
Dan Stoza50101d02016-04-07 16:53:23 -07001744status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001745 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001746 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001747
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001748 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001749 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1750
Dan Stoza50101d02016-04-07 16:53:23 -07001751 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1752 *outBuffer = nullptr;
1753 *outFence = Fence::NO_FENCE;
1754 return NO_ERROR;
1755 }
1756
1757 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1758 *outFence = mLastQueueBufferFence;
1759
John Reck1a61da52016-04-28 13:18:15 -07001760 // Currently only SurfaceFlinger internally ever changes
1761 // GLConsumer's filtering mode, so we just use 'true' here as
1762 // this is slightly specialized for the current client of this API,
1763 // which does want filtering.
1764 GLConsumer::computeTransformMatrix(outTransformMatrix,
1765 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1766 mLastQueuedTransform, true /* filter */);
1767
Dan Stoza50101d02016-04-07 16:53:23 -07001768 return NO_ERROR;
1769}
1770
John Reckf0f13e82021-05-18 00:42:56 -04001771status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1772 Rect* outRect, uint32_t* outTransform) {
1773 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001774
1775 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001776 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1777 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1778 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001779 *outBuffer = nullptr;
1780 *outFence = Fence::NO_FENCE;
1781 return NO_ERROR;
1782 }
1783
1784 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1785 *outFence = mLastQueueBufferFence;
1786 *outRect = mLastQueuedCrop;
1787 *outTransform = mLastQueuedTransform;
1788
1789 return NO_ERROR;
1790}
1791
Brian Anderson3890c392016-07-25 12:48:08 -07001792void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1793 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001794}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001795
Brian Anderson3890c392016-07-25 12:48:08 -07001796void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001797 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001798 FrameEventHistoryDelta* outDelta) {
1799 if (newTimestamps == nullptr && outDelta == nullptr) {
1800 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001801 }
1802
1803 ATRACE_CALL();
1804 BQ_LOGV("addAndGetFrameTimestamps");
1805 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001806 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001807 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001808 listener = mCore->mConsumerListener;
1809 }
Yi Kong48a619f2018-06-05 16:34:59 -07001810 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001811 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001812 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001813}
1814
Dan Stoza289ade12014-02-28 11:17:17 -08001815void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1816 // If we're here, it means that a producer we were connected to died.
1817 // We're guaranteed that we are still connected to it because we remove
1818 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1819 // without synchronization here.
1820 int api = mCore->mConnectedApi;
1821 disconnect(api);
1822}
1823
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001824status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1825 BQ_LOGV("getUniqueId");
1826
1827 *outId = mCore->mUniqueId;
1828 return NO_ERROR;
1829}
1830
Chia-I Wue2786ea2017-08-07 10:36:08 -07001831status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1832 BQ_LOGV("getConsumerUsage");
1833
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001834 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001835 *outUsage = mCore->mConsumerUsageBits;
1836 return NO_ERROR;
1837}
1838
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001839status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1840 ATRACE_CALL();
1841 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1842
1843 std::lock_guard<std::mutex> lock(mCore->mMutex);
1844
1845 mCore->mAutoPrerotation = autoPrerotation;
1846 return NO_ERROR;
1847}
1848
Ady Abraham107788e2023-10-17 12:31:08 -07001849#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001850status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1851 int8_t changeFrameRateStrategy) {
1852 ATRACE_CALL();
1853 BQ_LOGV("setFrameRate: %.2f", frameRate);
1854
1855 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1856 "BufferQueueProducer::setFrameRate")) {
1857 return BAD_VALUE;
1858 }
1859
1860 sp<IConsumerListener> listener;
1861 {
1862 std::lock_guard<std::mutex> lock(mCore->mMutex);
1863 listener = mCore->mConsumerListener;
1864 }
1865 if (listener != nullptr) {
1866 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1867 }
1868 return NO_ERROR;
1869}
1870#endif
1871
John Reckdb164ff2024-04-03 16:59:28 -04001872#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1873status_t BufferQueueProducer::setAdditionalOptions(
1874 const std::vector<gui::AdditionalOptions>& options) {
1875 ATRACE_CALL();
1876 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1877
1878 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1879 return INVALID_OPERATION;
1880 }
1881
1882 std::lock_guard<std::mutex> lock(mCore->mMutex);
1883
1884 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1885 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1886 return NO_INIT;
1887 }
1888
1889 if (mCore->mAdditionalOptions != options) {
1890 mCore->mAdditionalOptions = options;
1891 mCore->mAdditionalOptionsGenerationId++;
1892 }
1893 return NO_ERROR;
1894}
1895#endif
1896
Dan Stoza289ade12014-02-28 11:17:17 -08001897} // namespace android