blob: 39209f97456fb22e47445a2f036a5f8cda8f23fb [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 }
Patrick Williams078d7362024-08-27 10:20:39 -0500205#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
206 mCore->notifyBufferReleased();
207#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200208 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500209#endif
Pablo Ceballosfa455352015-08-12 17:47:47 -0700210 } // Autolock scope
211
212 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700213 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800214 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 }
216
217 return NO_ERROR;
218}
219
220status_t BufferQueueProducer::setAsyncMode(bool async) {
221 ATRACE_CALL();
222 BQ_LOGV("setAsyncMode: async = %d", async);
223
Pablo Ceballos981066c2016-02-18 12:54:37 -0800224 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700225 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200226 std::unique_lock<std::mutex> lock(mCore->mMutex);
227 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700228
229 if (mCore->mIsAbandoned) {
230 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
231 return NO_INIT;
232 }
233
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700234 if (async == mCore->mAsyncMode) {
235 return NO_ERROR;
236 }
237
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700238 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700239 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
240 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700241 BQ_LOGE("setAsyncMode(%d): this call would cause the "
242 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700243 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
244 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
245 mCore->mMaxDequeuedBufferCount,
246 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700247 return BAD_VALUE;
248 }
249
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800250 int delta = mCore->getMaxBufferCountLocked(async,
251 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
252 - mCore->getMaxBufferCountLocked();
253
Pablo Ceballos981066c2016-02-18 12:54:37 -0800254 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800255 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
256 "available slots. Delta = %d", delta);
257 return BAD_VALUE;
258 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700259 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800260 VALIDATE_CONSISTENCY();
Patrick Williams078d7362024-08-27 10:20:39 -0500261#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
262 mCore->notifyBufferReleased();
263#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200264 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500265#endif
266
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700267 if (delta < 0) {
268 listener = mCore->mConsumerListener;
269 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700270 } // Autolock scope
271
272 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700273 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800274 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700275 }
276 return NO_ERROR;
277}
278
Dan Stoza5ecfb682016-01-04 17:01:02 -0800279int BufferQueueProducer::getFreeBufferLocked() const {
280 if (mCore->mFreeBuffers.empty()) {
281 return BufferQueueCore::INVALID_BUFFER_SLOT;
282 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800283 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284 mCore->mFreeBuffers.pop_front();
285 return slot;
286}
287
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800288int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800289 if (mCore->mFreeSlots.empty()) {
290 return BufferQueueCore::INVALID_BUFFER_SLOT;
291 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800292 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800293 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800294 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800295}
296
297status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200298 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800299 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
300 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800301 bool tryAgain = true;
302 while (tryAgain) {
303 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800305 return NO_INIT;
306 }
307
Dan Stoza9f3053d2014-03-06 15:14:33 -0800308 int dequeuedCount = 0;
309 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800310 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700311 if (mSlots[s].mBufferState.isDequeued()) {
312 ++dequeuedCount;
313 }
314 if (mSlots[s].mBufferState.isAcquired()) {
315 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 }
317 }
318
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700319 // Producers are not allowed to dequeue more than
320 // mMaxDequeuedBufferCount buffers.
321 // This check is only done if a buffer has already been queued
322 if (mCore->mBufferHasBeenQueued &&
323 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700324 // Supress error logs when timeout is non-negative.
325 if (mDequeueTimeout < 0) {
326 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
327 "count (%d)", callerString,
328 mCore->mMaxDequeuedBufferCount);
329 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800330 return INVALID_OPERATION;
331 }
332
Dan Stoza0de7ea72015-04-23 13:20:51 -0700333 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
334
Dan Stozaae3c3682014-04-18 15:43:35 -0700335 // If we disconnect and reconnect quickly, we can be in a state where
336 // our slots are empty but we have many buffers in the queue. This can
337 // cause us to run out of memory if we outrun the consumer. Wait here if
338 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800339 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700340 bool tooManyBuffers = mCore->mQueue.size()
341 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700342 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800343 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700344 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700345 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700346 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700347 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700348 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700349 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700350 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800351 } else {
352 if (caller == FreeSlotCaller::Dequeue) {
353 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800354 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800355 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
356 *found = slot;
357 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800358 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800359 }
360 } else {
361 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800362 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800363 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
364 *found = slot;
365 } else {
366 *found = getFreeBufferLocked();
367 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700368 }
369 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700370 }
371
372 // If no buffer is found, or if the queue has too many buffers
373 // outstanding, wait for a buffer to be acquired or released, or for the
374 // max buffer count to change.
375 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
376 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 if (tryAgain) {
378 // Return an error if we're in non-blocking mode (producer and
379 // consumer are controlled by the application).
380 // However, the consumer is allowed to briefly acquire an extra
381 // buffer (which could cause us to have to wait here), which is
382 // okay, since it is only used to implement an atomic acquire +
383 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700384 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
386 return WOULD_BLOCK;
387 }
Patrick Williams078d7362024-08-27 10:20:39 -0500388#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
389 if (status_t status = waitForBufferRelease(lock, mDequeueTimeout);
390 status == TIMED_OUT) {
391 return TIMED_OUT;
392 }
393#else
Dan Stoza127fc632015-06-30 13:43:32 -0700394 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200395 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
396 std::chrono::nanoseconds(mDequeueTimeout));
397 if (result == std::cv_status::timeout) {
398 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700399 }
400 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200401 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700402 }
Patrick Williams078d7362024-08-27 10:20:39 -0500403#endif
Dan Stoza9f3053d2014-03-06 15:14:33 -0800404 }
405 } // while (tryAgain)
406
407 return NO_ERROR;
408}
409
Patrick Williams078d7362024-08-27 10:20:39 -0500410#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
411status_t BufferQueueProducer::waitForBufferRelease(std::unique_lock<std::mutex>& lock,
412 nsecs_t timeout) const {
413 if (mDequeueTimeout >= 0) {
414 std::cv_status result =
415 mCore->mDequeueCondition.wait_for(lock, std::chrono::nanoseconds(timeout));
416 if (result == std::cv_status::timeout) {
417 return TIMED_OUT;
418 }
419 } else {
420 mCore->mDequeueCondition.wait(lock);
421 }
422 return OK;
423}
424#endif
425
Ian Elliottd11b0442017-07-18 11:05:49 -0600426status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
427 uint32_t width, uint32_t height, PixelFormat format,
428 uint64_t usage, uint64_t* outBufferAge,
429 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800430 ATRACE_CALL();
431 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200432 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800433 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700434
435 if (mCore->mIsAbandoned) {
436 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
437 return NO_INIT;
438 }
439
440 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
441 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
442 return NO_INIT;
443 }
Dan Stoza289ade12014-02-28 11:17:17 -0800444 } // Autolock scope
445
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700446 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800447
448 if ((width && !height) || (!width && height)) {
449 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
450 return BAD_VALUE;
451 }
452
453 status_t returnFlags = NO_ERROR;
454 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
455 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800456 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800457
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000458 sp<IConsumerListener> listener;
459 bool callOnFrameDequeued = false;
460 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400461#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
462 std::vector<gui::AdditionalOptions> allocOptions;
463 uint32_t allocOptionsGenId = 0;
464#endif
465
Dan Stoza289ade12014-02-28 11:17:17 -0800466 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200467 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800468
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100469 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
470 // is finished such that we don't allocate in parallel.
471 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
472 mDequeueWaitingForAllocation = true;
473 mCore->waitWhileAllocatingLocked(lock);
474 mDequeueWaitingForAllocation = false;
475 mDequeueWaitingForAllocationCondition.notify_all();
476 }
Dan Stoza289ade12014-02-28 11:17:17 -0800477
478 if (format == 0) {
479 format = mCore->mDefaultBufferFormat;
480 }
481
482 // Enable the usage bits the consumer requested
483 usage |= mCore->mConsumerUsageBits;
484
Dan Stoza9de72932015-04-16 17:28:43 -0700485 const bool useDefaultSize = !width && !height;
486 if (useDefaultSize) {
487 width = mCore->mDefaultWidth;
488 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700489 if (mCore->mAutoPrerotation &&
490 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
491 std::swap(width, height);
492 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800493 }
Dan Stoza289ade12014-02-28 11:17:17 -0800494
Pablo Ceballos981066c2016-02-18 12:54:37 -0800495 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700496 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200497 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700498 if (status != NO_ERROR) {
499 return status;
500 }
501
502 // This should not happen
503 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
504 BQ_LOGE("dequeueBuffer: no available buffer slots");
505 return -EBUSY;
506 }
507
508 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
509
510 // If we are not allowed to allocate new buffers,
511 // waitForFreeSlotThenRelock must have returned a slot containing a
512 // buffer. If this buffer would require reallocation to meet the
513 // requested attributes, we free it and attempt to get another one.
514 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700515 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700516 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700517 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700518 return BAD_VALUE;
519 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800520 mCore->mFreeSlots.insert(found);
521 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700522 found = BufferItem::INVALID_BUFFER_SLOT;
523 continue;
524 }
525 }
Dan Stoza289ade12014-02-28 11:17:17 -0800526 }
527
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800528 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800529
John Reckdb164ff2024-04-03 16:59:28 -0400530 bool needsReallocation = buffer == nullptr ||
531 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
532
533#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
534 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
535 mCore->mAdditionalOptionsGenerationId;
536#endif
537
538 if (mCore->mSharedBufferSlot == found && needsReallocation) {
539 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800540 return BAD_VALUE;
541 }
542
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700543 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800544 mCore->mActiveBuffers.insert(found);
545 }
Dan Stoza289ade12014-02-28 11:17:17 -0800546 *outSlot = found;
547 ATRACE_BUFFER_INDEX(found);
548
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800549 attachedByConsumer = mSlots[found].mNeedsReallocation;
550 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800551
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700552 mSlots[found].mBufferState.dequeue();
553
John Reckdb164ff2024-04-03 16:59:28 -0400554 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700555 if (CC_UNLIKELY(ATRACE_ENABLED())) {
556 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000557 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700558 } else {
559 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
560 "layerCount:%d "
561 "usage:%d requested: %dx%d format:%d layerCount:%d "
562 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000563 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700564 BQ_LAYER_COUNT, usage, buffer->getWidth(),
565 buffer->getHeight(), buffer->getPixelFormat(),
566 buffer->getLayerCount(), buffer->getUsage());
567 }
568 }
Dan Stoza289ade12014-02-28 11:17:17 -0800569 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700570 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800571 mSlots[found].mRequestBufferCalled = false;
572 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
573 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
574 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800575 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700576 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400577#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
578 allocOptions = mCore->mAdditionalOptions;
579 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
580#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800581
582 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800583 } else {
584 // We add 1 because that will be the frame number when this buffer
585 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700586 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800587 }
588
Dan Stoza800b41a2015-04-28 14:20:04 -0700589 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
590 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800591
Yi Kong48a619f2018-06-05 16:34:59 -0700592 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800593 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
594 "slot=%d w=%d h=%d format=%u",
595 found, buffer->width, buffer->height, buffer->format);
596 }
597
598 eglDisplay = mSlots[found].mEglDisplay;
599 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700600 // Don't return a fence in shared buffer mode, except for the first
601 // frame.
602 *outFence = (mCore->mSharedBufferMode &&
603 mCore->mSharedBufferSlot == found) ?
604 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800605 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
606 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700607
608 // If shared buffer mode has just been enabled, cache the slot of the
609 // first buffer that is dequeued and mark it as the shared buffer.
610 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
611 BufferQueueCore::INVALID_BUFFER_SLOT) {
612 mCore->mSharedBufferSlot = found;
613 mSlots[found].mBufferState.mShared = true;
614 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700615
616 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000617 callOnFrameDequeued = true;
618 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700619 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000620
621 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800622 } // Autolock scope
623
624 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700625 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400626
627#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
628 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
629 tempOptions.reserve(allocOptions.size());
630 for (const auto& it : allocOptions) {
631 tempOptions.emplace_back(it.name.c_str(), it.value);
632 }
633 const GraphicBufferAllocator::AllocationRequest allocRequest = {
634 .importBuffer = true,
635 .width = width,
636 .height = height,
637 .format = format,
638 .layerCount = BQ_LAYER_COUNT,
639 .usage = usage,
640 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
641 .extras = std::move(tempOptions),
642 };
643 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
644#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000645 sp<GraphicBuffer> graphicBuffer =
646 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
647 {mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400648#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700649
650 status_t error = graphicBuffer->initCheck();
651
Dan Stoza289ade12014-02-28 11:17:17 -0800652 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200653 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800654
Chia-I Wufeec3b12017-05-25 09:34:56 -0700655 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700656 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
657 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400658#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
659 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
660#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000661 callOnFrameDequeued = true;
662 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700663 }
664
665 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200666 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700667
Chia-I Wufeec3b12017-05-25 09:34:56 -0700668 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700669 mCore->mFreeSlots.insert(*outSlot);
670 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700671 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
672 return error;
673 }
674
Dan Stoza289ade12014-02-28 11:17:17 -0800675 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700676 mCore->mFreeSlots.insert(*outSlot);
677 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800678 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
679 return NO_INIT;
680 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800681
Pablo Ceballos9e314332016-01-12 13:49:19 -0800682 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800683 } // Autolock scope
684 }
685
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000686 if (listener != nullptr && callOnFrameDequeued) {
687 listener->onFrameDequeued(bufferId);
688 }
689
Dan Stoza9f3053d2014-03-06 15:14:33 -0800690 if (attachedByConsumer) {
691 returnFlags |= BUFFER_NEEDS_REALLOCATION;
692 }
693
Dan Stoza289ade12014-02-28 11:17:17 -0800694 if (eglFence != EGL_NO_SYNC_KHR) {
695 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
696 1000000000);
697 // If something goes wrong, log the error, but return the buffer without
698 // synchronizing access to it. It's too late at this point to abort the
699 // dequeue operation.
700 if (result == EGL_FALSE) {
701 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
702 eglGetError());
703 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
704 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
705 }
706 eglDestroySyncKHR(eglDisplay, eglFence);
707 }
708
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700709 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
710 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800711 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800712 mSlots[*outSlot].mGraphicBuffer != nullptr ?
713 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800714
Ian Elliottd11b0442017-07-18 11:05:49 -0600715 if (outBufferAge) {
716 *outBufferAge = mCore->mBufferAge;
717 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700718 addAndGetFrameTimestamps(nullptr, outTimestamps);
719
Dan Stoza289ade12014-02-28 11:17:17 -0800720 return returnFlags;
721}
722
Dan Stoza9f3053d2014-03-06 15:14:33 -0800723status_t BufferQueueProducer::detachBuffer(int slot) {
724 ATRACE_CALL();
725 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700726 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800727
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700728 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000729 bool callOnFrameDetached = false;
730 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700731 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200732 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700733
734 if (mCore->mIsAbandoned) {
735 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
736 return NO_INIT;
737 }
738
739 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
740 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
741 return NO_INIT;
742 }
743
744 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
745 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
746 return BAD_VALUE;
747 }
748
749 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
750 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
751 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
752 return BAD_VALUE;
753 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000754 // TODO(http://b/140581935): This message is BQ_LOGW because it
755 // often logs when no actionable errors are present. Return to
756 // using BQ_LOGE after ensuring this only logs during errors.
757 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700758 "(state = %s)", slot, mSlots[slot].mBufferState.string());
759 return BAD_VALUE;
760 } else if (!mSlots[slot].mRequestBufferCalled) {
761 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
762 slot);
763 return BAD_VALUE;
764 }
765
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700766 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700767 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000768 if (gb != nullptr) {
769 callOnFrameDetached = true;
770 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700771 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700772 mSlots[slot].mBufferState.detachProducer();
773 mCore->mActiveBuffers.erase(slot);
774 mCore->mFreeSlots.insert(slot);
775 mCore->clearBufferSlotLocked(slot);
Patrick Williams078d7362024-08-27 10:20:39 -0500776#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
777 mCore->notifyBufferReleased();
778#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200779 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500780#endif
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700781 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800782 }
783
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000784 if (listener != nullptr && callOnFrameDetached) {
785 listener->onFrameDetached(bufferId);
786 }
787
Yi Kong48a619f2018-06-05 16:34:59 -0700788 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700789 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700790 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800791
Dan Stoza9f3053d2014-03-06 15:14:33 -0800792 return NO_ERROR;
793}
794
Dan Stozad9822a32014-03-28 15:25:31 -0700795status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
796 sp<Fence>* outFence) {
797 ATRACE_CALL();
798
Yi Kong48a619f2018-06-05 16:34:59 -0700799 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700800 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
801 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700802 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700803 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
804 return BAD_VALUE;
805 }
806
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700807 sp<IConsumerListener> listener;
808 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200809 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700810
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700811 if (mCore->mIsAbandoned) {
812 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
813 return NO_INIT;
814 }
815
816 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
817 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
818 return NO_INIT;
819 }
820
821 if (mCore->mSharedBufferMode) {
822 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
823 "mode");
824 return BAD_VALUE;
825 }
826
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200827 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700828
829 if (mCore->mFreeBuffers.empty()) {
830 return NO_MEMORY;
831 }
832
833 int found = mCore->mFreeBuffers.front();
834 mCore->mFreeBuffers.remove(found);
835 mCore->mFreeSlots.insert(found);
836
837 BQ_LOGV("detachNextBuffer detached slot %d", found);
838
839 *outBuffer = mSlots[found].mGraphicBuffer;
840 *outFence = mSlots[found].mFence;
841 mCore->clearBufferSlotLocked(found);
842 VALIDATE_CONSISTENCY();
843 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700844 }
845
Yi Kong48a619f2018-06-05 16:34:59 -0700846 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700847 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700848 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800849
Dan Stozad9822a32014-03-28 15:25:31 -0700850 return NO_ERROR;
851}
852
Dan Stoza9f3053d2014-03-06 15:14:33 -0800853status_t BufferQueueProducer::attachBuffer(int* outSlot,
854 const sp<android::GraphicBuffer>& buffer) {
855 ATRACE_CALL();
856
Yi Kong48a619f2018-06-05 16:34:59 -0700857 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700858 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800859 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700860 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700861 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800862 return BAD_VALUE;
863 }
864
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200865 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700866
867 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700868 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700869 return NO_INIT;
870 }
871
872 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700873 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700874 return NO_INIT;
875 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800876
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700877 if (mCore->mSharedBufferMode) {
878 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700879 return BAD_VALUE;
880 }
881
Dan Stoza812ed062015-06-02 15:45:22 -0700882 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
883 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
884 "[queue %u]", buffer->getGenerationNumber(),
885 mCore->mGenerationNumber);
886 return BAD_VALUE;
887 }
888
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200889 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700890
Dan Stoza9f3053d2014-03-06 15:14:33 -0800891 status_t returnFlags = NO_ERROR;
892 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200893 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800894 if (status != NO_ERROR) {
895 return status;
896 }
897
898 // This should not happen
899 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700900 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800901 return -EBUSY;
902 }
903
904 *outSlot = found;
905 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700906 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800907 *outSlot, returnFlags);
908
909 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800911 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
912 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700913 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800914 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800915 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800916 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800917 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700918
Dan Stoza9f3053d2014-03-06 15:14:33 -0800919 return returnFlags;
920}
921
Dan Stoza289ade12014-02-28 11:17:17 -0800922status_t BufferQueueProducer::queueBuffer(int slot,
923 const QueueBufferInput &input, QueueBufferOutput *output) {
924 ATRACE_CALL();
925 ATRACE_BUFFER_INDEX(slot);
926
Brian Andersond6927fb2016-07-23 23:37:30 -0700927 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800928 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800929 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700930 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800931 int scalingMode;
932 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700933 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700934 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700935 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700936 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700937 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
938 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700939 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700940 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Brian Lindahl628cff42024-10-30 11:50:28 -0600941 const std::optional<PictureProfileHandle>& pictureProfileHandle =
942 input.getPictureProfileHandle();
Dan Stoza289ade12014-02-28 11:17:17 -0800943
Yi Kong48a619f2018-06-05 16:34:59 -0700944 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800945 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800946 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800947 }
948
Brian Anderson3d4039d2016-09-23 16:31:30 -0700949 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
950
Dan Stoza289ade12014-02-28 11:17:17 -0800951 switch (scalingMode) {
952 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
953 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
954 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
955 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
956 break;
957 default:
958 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800959 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800960 }
961
Dan Stoza8dc55392014-11-04 11:37:46 -0800962 sp<IConsumerListener> frameAvailableListener;
963 sp<IConsumerListener> frameReplacedListener;
964 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700965 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800966 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500967 int connectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -0700968 bool enableEglCpuThrottling = true;
John Reckd2c4a4a2024-02-07 10:31:09 -0500969 sp<Fence> lastQueuedFence;
970
Dan Stoza289ade12014-02-28 11:17:17 -0800971 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200972 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800973
974 if (mCore->mIsAbandoned) {
975 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
976 return NO_INIT;
977 }
978
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700979 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
980 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
981 return NO_INIT;
982 }
983
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800984 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800985 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800986 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800987 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800989 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700990 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800991 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800992 } else if (!mSlots[slot].mRequestBufferCalled) {
993 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
994 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800995 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800996 }
997
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700998 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700999 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001000 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001001 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001002 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001003 mSlots[slot].mBufferState.mShared = true;
1004 }
1005
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001006 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001007 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
1008 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
1009 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -07001010 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001011 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -08001012
1013 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
1014 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -07001015 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -08001016 crop.intersect(bufferRect, &croppedRect);
1017 if (croppedRect != crop) {
1018 BQ_LOGE("queueBuffer: crop rect is not contained within the "
1019 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001020 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001021 }
1022
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001023 // Override UNKNOWN dataspace with consumer default
1024 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
1025 dataSpace = mCore->mDefaultBufferDataSpace;
1026 }
1027
Brian Andersond6927fb2016-07-23 23:37:30 -07001028 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001029 mSlots[slot].mBufferState.queue();
1030
Brian Andersond6927fb2016-07-23 23:37:30 -07001031 // Increment the frame counter and store a local version of it
1032 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -08001033 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -07001034 currentFrameNumber = mCore->mFrameCounter;
1035 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001036
Dan Stoza289ade12014-02-28 11:17:17 -08001037 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1038 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1039 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001040 item.mTransform = transform &
1041 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001042 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1044 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001045 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001046 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001047 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001048 item.mHdrMetadata = hdrMetadata;
Brian Lindahl628cff42024-10-30 11:50:28 -06001049 item.mPictureProfileHandle = pictureProfileHandle;
Brian Andersond6927fb2016-07-23 23:37:30 -07001050 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001051 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001052 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001053 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001054 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001055 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001056 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001057 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001058 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001059 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001060 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001061 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001062
Ruben Brunk1681d952014-06-27 15:51:55 -07001063 mStickyTransform = stickyTransform;
1064
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001065 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001066 if (mCore->mSharedBufferMode) {
1067 mCore->mSharedBufferCache.crop = crop;
1068 mCore->mSharedBufferCache.transform = transform;
1069 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001070 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001071 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001072 }
1073
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001074 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001075 if (mCore->mQueue.empty()) {
1076 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1077 // and simply queue this buffer
1078 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001079 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001080 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001081 // When the queue is not empty, we need to look at the last buffer
1082 // in the queue to see if we need to replace it
1083 const BufferItem& last = mCore->mQueue.itemAt(
1084 mCore->mQueue.size() - 1);
1085 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001086
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001087 if (!last.mIsStale) {
1088 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001089
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001090 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001091 // still be around. Mark it as no longer shared if this
1092 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001093 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001094 mSlots[last.mSlot].mBufferState.isFree()) {
1095 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001096 }
1097 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001098 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1099 mCore->mActiveBuffers.erase(last.mSlot);
1100 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001101 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001102 }
Dan Stoza289ade12014-02-28 11:17:17 -08001103 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001104
Steven Thomas862e7532019-07-10 19:21:03 -07001105 // Make sure to merge the damage rect from the frame we're about
1106 // to drop into the new frame's damage rect.
1107 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1108 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1109 item.mSurfaceDamage = Region::INVALID_REGION;
1110 } else {
1111 item.mSurfaceDamage |= last.mSurfaceDamage;
1112 }
1113
Dan Stoza289ade12014-02-28 11:17:17 -08001114 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001115 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001116 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001117 } else {
1118 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001119 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001120 }
1121 }
1122
1123 mCore->mBufferHasBeenQueued = true;
Patrick Williams078d7362024-08-27 10:20:39 -05001124#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1125 mCore->notifyBufferReleased();
1126#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001127 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001128#endif
Dan Stoza50101d02016-04-07 16:53:23 -07001129 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001130
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001131 output->width = mCore->mDefaultWidth;
1132 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001133 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001134 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1135 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001136
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001137 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001138#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001139 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001140#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001141 // Take a ticket for the callback functions
1142 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001143
Pablo Ceballos9e314332016-01-12 13:49:19 -08001144 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001145
1146 connectedApi = mCore->mConnectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001147 if (flags::bq_producer_throttles_only_async_mode()) {
1148 enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1149 }
John Reckd2c4a4a2024-02-07 10:31:09 -05001150 lastQueuedFence = std::move(mLastQueueBufferFence);
1151
1152 mLastQueueBufferFence = std::move(acquireFence);
1153 mLastQueuedCrop = item.mCrop;
1154 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001155 } // Autolock scope
1156
Irvel468051e2016-06-13 16:44:44 -07001157 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1158 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1159 // there will be no Binder call
1160 if (!mConsumerIsSurfaceFlinger) {
1161 item.mGraphicBuffer.clear();
1162 }
1163
JihCheng Chiu544c5222019-04-02 20:50:58 +08001164 // Update and get FrameEventHistory.
1165 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1166 NewFrameEventsEntry newFrameEventsEntry = {
1167 currentFrameNumber,
1168 postedTime,
1169 requestedPresentTimestamp,
1170 std::move(acquireFenceTime)
1171 };
1172 addAndGetFrameTimestamps(&newFrameEventsEntry,
1173 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1174
Dan Stoza8dc55392014-11-04 11:37:46 -08001175 // Call back without the main BufferQueue lock held, but with the callback
1176 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001177
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001178 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001179 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001180 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001181 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001182 }
1183
Yi Kong48a619f2018-06-05 16:34:59 -07001184 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001185 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001186 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001187 frameReplacedListener->onFrameReplaced(item);
1188 }
1189
1190 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001191 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001192 }
1193
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001194 // Wait without lock held
Mathias Agopianb96661f2024-09-17 11:45:38 -07001195 if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001196 // Waiting here allows for two full buffers to be queued but not a
1197 // third. In the event that frames take varying time, this makes a
1198 // small trade-off in favor of latency rather than throughput.
1199 lastQueuedFence->waitForever("Throttling EGL Production");
1200 }
1201
Dan Stoza289ade12014-02-28 11:17:17 -08001202 return NO_ERROR;
1203}
1204
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001205status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001206 ATRACE_CALL();
1207 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001208
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001209 sp<IConsumerListener> listener;
1210 bool callOnFrameCancelled = false;
1211 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1212 {
1213 std::lock_guard<std::mutex> lock(mCore->mMutex);
1214
1215 if (mCore->mIsAbandoned) {
1216 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1217 return NO_INIT;
1218 }
1219
1220 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1221 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1222 return NO_INIT;
1223 }
1224
1225 if (mCore->mSharedBufferMode) {
1226 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1227 return BAD_VALUE;
1228 }
1229
1230 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1231 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1232 BufferQueueDefs::NUM_BUFFER_SLOTS);
1233 return BAD_VALUE;
1234 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1235 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1236 "(state = %s)",
1237 slot, mSlots[slot].mBufferState.string());
1238 return BAD_VALUE;
1239 } else if (fence == nullptr) {
1240 BQ_LOGE("cancelBuffer: fence is NULL");
1241 return BAD_VALUE;
1242 }
1243
1244 mSlots[slot].mBufferState.cancel();
1245
1246 // After leaving shared buffer mode, the shared buffer will still be around.
1247 // Mark it as no longer shared if this operation causes it to be free.
1248 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1249 mSlots[slot].mBufferState.mShared = false;
1250 }
1251
1252 // Don't put the shared buffer on the free list.
1253 if (!mSlots[slot].mBufferState.isShared()) {
1254 mCore->mActiveBuffers.erase(slot);
1255 mCore->mFreeBuffers.push_back(slot);
1256 }
1257
1258 auto gb = mSlots[slot].mGraphicBuffer;
1259 if (gb != nullptr) {
1260 callOnFrameCancelled = true;
1261 bufferId = gb->getId();
1262 }
1263 mSlots[slot].mFence = fence;
Patrick Williams078d7362024-08-27 10:20:39 -05001264#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1265 mCore->notifyBufferReleased();
1266#else
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001267 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001268#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001269 listener = mCore->mConsumerListener;
1270 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001271 }
1272
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001273 if (listener != nullptr && callOnFrameCancelled) {
1274 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001275 }
1276
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001277 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001278}
1279
1280int BufferQueueProducer::query(int what, int *outValue) {
1281 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001282 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001283
Yi Kong48a619f2018-06-05 16:34:59 -07001284 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001285 BQ_LOGE("query: outValue was NULL");
1286 return BAD_VALUE;
1287 }
1288
1289 if (mCore->mIsAbandoned) {
1290 BQ_LOGE("query: BufferQueue has been abandoned");
1291 return NO_INIT;
1292 }
1293
1294 int value;
1295 switch (what) {
1296 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001297 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001298 break;
1299 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001300 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001301 break;
1302 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001303 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001304 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001305 case NATIVE_WINDOW_LAYER_COUNT:
1306 // All BufferQueue buffers have a single layer.
1307 value = BQ_LAYER_COUNT;
1308 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001309 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001310 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001311 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001312 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001313 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001314 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001315 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1316 value = (mCore->mQueue.size() > 1);
1317 break;
1318 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001319 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001320 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001321 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001322 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1323 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1324 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001325 case NATIVE_WINDOW_BUFFER_AGE:
1326 if (mCore->mBufferAge > INT32_MAX) {
1327 value = 0;
1328 } else {
1329 value = static_cast<int32_t>(mCore->mBufferAge);
1330 }
1331 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001332 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1333 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1334 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001335 default:
1336 return BAD_VALUE;
1337 }
1338
1339 BQ_LOGV("query: %d? %d", what, value);
1340 *outValue = value;
1341 return NO_ERROR;
1342}
1343
Dan Stozaf0eaf252014-03-21 13:05:51 -07001344status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001345 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1346 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001347 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001348 mConsumerName = mCore->mConsumerName;
1349 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1350 producerControlledByApp ? "true" : "false");
1351
1352 if (mCore->mIsAbandoned) {
1353 BQ_LOGE("connect: BufferQueue has been abandoned");
1354 return NO_INIT;
1355 }
1356
Yi Kong48a619f2018-06-05 16:34:59 -07001357 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001358 BQ_LOGE("connect: BufferQueue has no consumer");
1359 return NO_INIT;
1360 }
1361
Yi Kong48a619f2018-06-05 16:34:59 -07001362 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001363 BQ_LOGE("connect: output was NULL");
1364 return BAD_VALUE;
1365 }
1366
1367 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1368 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1369 mCore->mConnectedApi, api);
1370 return BAD_VALUE;
1371 }
1372
1373 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1374 mDequeueTimeout < 0 ?
1375 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1376 mCore->mMaxBufferCount) -
1377 mCore->getMaxBufferCountLocked();
1378 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1379 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1380 "slots. Delta = %d", delta);
1381 return BAD_VALUE;
1382 }
1383
Dan Stoza289ade12014-02-28 11:17:17 -08001384 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001385 switch (api) {
1386 case NATIVE_WINDOW_API_EGL:
1387 case NATIVE_WINDOW_API_CPU:
1388 case NATIVE_WINDOW_API_MEDIA:
1389 case NATIVE_WINDOW_API_CAMERA:
1390 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001391
1392 output->width = mCore->mDefaultWidth;
1393 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001394 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001395 output->numPendingBuffers =
1396 static_cast<uint32_t>(mCore->mQueue.size());
1397 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001398 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001399 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001400
Yi Kong48a619f2018-06-05 16:34:59 -07001401 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001402 // Set up a death notification so that we can disconnect
1403 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001404#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001405 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001406 status = IInterface::asBinder(listener)->linkToDeath(
1407 static_cast<IBinder::DeathRecipient*>(this));
1408 if (status != NO_ERROR) {
1409 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1410 strerror(-status), status);
1411 }
1412 mCore->mLinkedToDeath = listener;
1413 }
Chong Zhang62493092020-01-15 16:04:47 -08001414#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001415 mCore->mConnectedProducerListener = listener;
1416 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Sungtak Leecd217472024-07-19 17:17:40 +00001417#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1418 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1419#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001420 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001421 break;
1422 default:
1423 BQ_LOGE("connect: unknown API %d", api);
1424 status = BAD_VALUE;
1425 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001426 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001427 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001428 mCore->mBufferHasBeenQueued = false;
1429 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001430 mCore->mQueueBufferCanDrop = false;
1431 mCore->mLegacyBufferDrop = true;
1432 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1433 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1434 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001435 }
Dan Stoza289ade12014-02-28 11:17:17 -08001436
Pablo Ceballos981066c2016-02-18 12:54:37 -08001437 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001438#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1439 mCore->mAdditionalOptions.clear();
1440#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001441 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001442 return status;
1443}
1444
Robert Carr97b9c862016-09-08 13:54:35 -07001445status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001446 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001447 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001448
1449 int status = NO_ERROR;
1450 sp<IConsumerListener> listener;
1451 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001452 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001453
1454 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001455 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001456 return NO_ERROR;
1457 }
1458 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1459 }
1460
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001461 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001462
1463 if (mCore->mIsAbandoned) {
1464 // It's not really an error to disconnect after the surface has
1465 // been abandoned; it should just be a no-op.
1466 return NO_ERROR;
1467 }
1468
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001469 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001470 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1471 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1472 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001473 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001474 // If we're asked to disconnect the currently connected api but
1475 // nobody is connected, it's not really an error.
1476 if (api == BufferQueueCore::NO_CONNECTED_API) {
1477 return NO_ERROR;
1478 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001479 }
1480
Dan Stoza289ade12014-02-28 11:17:17 -08001481 switch (api) {
1482 case NATIVE_WINDOW_API_EGL:
1483 case NATIVE_WINDOW_API_CPU:
1484 case NATIVE_WINDOW_API_MEDIA:
1485 case NATIVE_WINDOW_API_CAMERA:
1486 if (mCore->mConnectedApi == api) {
1487 mCore->freeAllBuffersLocked();
1488
Chong Zhang62493092020-01-15 16:04:47 -08001489#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001490 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001491 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001492 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001493 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001494 // This can fail if we're here because of the death
1495 // notification, but we just ignore it
1496 token->unlinkToDeath(
1497 static_cast<IBinder::DeathRecipient*>(this));
1498 }
Chong Zhang62493092020-01-15 16:04:47 -08001499#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001500 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001501 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001502 mCore->mLinkedToDeath = nullptr;
1503 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001504 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001505 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001506 mCore->mSidebandStream.clear();
Patrick Williams078d7362024-08-27 10:20:39 -05001507#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1508 mCore->notifyBufferReleased();
1509#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001510 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001511#endif
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001512 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001513#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1514 mCore->mAdditionalOptions.clear();
1515#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001516 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001517 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1518 BQ_LOGE("disconnect: not connected (req=%d)", api);
1519 status = NO_INIT;
1520 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001521 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001522 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001523 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001524 }
1525 break;
1526 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001527 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001528 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001529 break;
1530 }
1531 } // Autolock scope
1532
1533 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001534 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001535 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001536 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001537 }
1538
1539 return status;
1540}
1541
Jesse Hall399184a2014-03-03 15:42:54 -08001542status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001543 sp<IConsumerListener> listener;
1544 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001545 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001546 mCore->mSidebandStream = stream;
1547 listener = mCore->mConsumerListener;
1548 } // Autolock scope
1549
Yi Kong48a619f2018-06-05 16:34:59 -07001550 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001551 listener->onSidebandStreamChanged();
1552 }
Jesse Hall399184a2014-03-03 15:42:54 -08001553 return NO_ERROR;
1554}
1555
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001556void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001557 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001558 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001559
1560 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001561 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001562 uint32_t allocWidth = 0;
1563 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001564 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001565 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001566 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001567#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1568 std::vector<gui::AdditionalOptions> allocOptions;
1569 uint32_t allocOptionsGenId = 0;
1570#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001571 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001572 std::unique_lock<std::mutex> lock(mCore->mMutex);
1573 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001574
Dan Stoza9de72932015-04-16 17:28:43 -07001575 if (!mCore->mAllowAllocation) {
1576 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1577 "BufferQueue");
1578 return;
1579 }
1580
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001581 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1582 // both allocateBuffers and dequeueBuffer.
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001583 if (mCore->mFreeSlots.empty()) {
1584 BQ_LOGV("allocateBuffers: a slot was occupied while "
1585 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001586 return;
1587 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001588
Antoine Labour78014f32014-07-15 21:17:03 -07001589 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1590 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001591 if (useDefaultSize && mCore->mAutoPrerotation &&
1592 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1593 std::swap(allocWidth, allocHeight);
1594 }
1595
Antoine Labour78014f32014-07-15 21:17:03 -07001596 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1597 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001598 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001599
John Reckdb164ff2024-04-03 16:59:28 -04001600#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1601 allocOptions = mCore->mAdditionalOptions;
1602 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1603#endif
1604
Antoine Labour78014f32014-07-15 21:17:03 -07001605 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001606
Antoine Labour78014f32014-07-15 21:17:03 -07001607 } // Autolock scope
1608
John Reckdb164ff2024-04-03 16:59:28 -04001609#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1610 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1611 tempOptions.reserve(allocOptions.size());
1612 for (const auto& it : allocOptions) {
1613 tempOptions.emplace_back(it.name.c_str(), it.value);
1614 }
1615 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1616 .importBuffer = true,
1617 .width = allocWidth,
1618 .height = allocHeight,
1619 .format = allocFormat,
1620 .layerCount = BQ_LAYER_COUNT,
1621 .usage = allocUsage,
1622 .requestorName = allocName,
1623 .extras = std::move(tempOptions),
1624 };
1625#endif
1626
John Reckdb164ff2024-04-03 16:59:28 -04001627#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001628 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
John Reckdb164ff2024-04-03 16:59:28 -04001629#else
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001630 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
1631 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
1632 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001633#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001634
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001635 status_t result = graphicBuffer->initCheck();
Mathias Agopian0556d792017-03-22 15:49:32 -07001636
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001637 if (result != NO_ERROR) {
1638 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1639 " %u, usage %#" PRIx64 ")", width, height, format, usage);
1640 std::lock_guard<std::mutex> lock(mCore->mMutex);
1641 mCore->mIsAllocating = false;
1642 mCore->mIsAllocatingCondition.notify_all();
1643 return;
Antoine Labour78014f32014-07-15 21:17:03 -07001644 }
1645
1646 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001647 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001648 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1649 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001650 if (useDefaultSize && mCore->mAutoPrerotation &&
1651 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1652 std::swap(checkWidth, checkHeight);
1653 }
1654
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001655 PixelFormat checkFormat = format != 0 ?
1656 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001657 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001658 bool allocOptionsChanged = false;
1659#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1660 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1661#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001662 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001663 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001664 // Something changed while we released the lock. Retry.
1665 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1666 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001667 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001668 continue;
1669 }
1670
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001671 if (mCore->mFreeSlots.empty()) {
1672 BQ_LOGV("allocateBuffers: a slot was occupied while "
1673 "allocating. Dropping allocated buffer.");
1674 } else {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001675 auto slot = mCore->mFreeSlots.begin();
1676 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001677 mSlots[*slot].mGraphicBuffer = graphicBuffer;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001678 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001679#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1680 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1681#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001682
1683 // freeBufferLocked puts this slot on the free slots list. Since
1684 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001685 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001686
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001687 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1688 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001689
1690 // Make sure the erase is done after all uses of the slot
1691 // iterator since it will be invalid after this point.
1692 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001693 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001694
Antoine Labour78014f32014-07-15 21:17:03 -07001695 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001696 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001697 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001698
1699 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1700 // waiting anymore so it can use the buffer we just allocated.
1701 while (mDequeueWaitingForAllocation) {
1702 mDequeueWaitingForAllocationCondition.wait(lock);
1703 }
Antoine Labour78014f32014-07-15 21:17:03 -07001704 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001705 }
1706}
1707
Dan Stoza9de72932015-04-16 17:28:43 -07001708status_t BufferQueueProducer::allowAllocation(bool allow) {
1709 ATRACE_CALL();
1710 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1711
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001712 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001713 mCore->mAllowAllocation = allow;
1714 return NO_ERROR;
1715}
1716
Dan Stoza812ed062015-06-02 15:45:22 -07001717status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1718 ATRACE_CALL();
1719 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1720
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001721 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001722 mCore->mGenerationNumber = generationNumber;
1723 return NO_ERROR;
1724}
1725
Dan Stozac6f30bd2015-06-08 09:32:50 -07001726String8 BufferQueueProducer::getConsumerName() const {
1727 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001728 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001729 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001730 return mConsumerName;
1731}
1732
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001733status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001734 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001735 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001736
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001737 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001738 if (!sharedBufferMode) {
1739 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001740 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001741 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001742 return NO_ERROR;
1743}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001744
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001745status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1746 ATRACE_CALL();
1747 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1748
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001749 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001750
1751 mCore->mAutoRefresh = autoRefresh;
1752 return NO_ERROR;
1753}
1754
Dan Stoza127fc632015-06-30 13:43:32 -07001755status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1756 ATRACE_CALL();
1757 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1758
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001759 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001760 bool dequeueBufferCannotBlock =
1761 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1762 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001763 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1764 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1765 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1766 "available slots. Delta = %d", delta);
1767 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001768 }
1769
Pablo Ceballos981066c2016-02-18 12:54:37 -08001770 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001771 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1772 if (timeout > 0) {
1773 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001774 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001775
Pablo Ceballos981066c2016-02-18 12:54:37 -08001776 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001777 return NO_ERROR;
1778}
1779
Sungtak Lee3249fb62019-03-02 16:40:47 -08001780status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1781 ATRACE_CALL();
1782 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1783
1784 std::lock_guard<std::mutex> lock(mCore->mMutex);
1785 mCore->mLegacyBufferDrop = drop;
1786 return NO_ERROR;
1787}
1788
Dan Stoza50101d02016-04-07 16:53:23 -07001789status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001790 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001791 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001792
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001793 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001794 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1795
Dan Stoza50101d02016-04-07 16:53:23 -07001796 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1797 *outBuffer = nullptr;
1798 *outFence = Fence::NO_FENCE;
1799 return NO_ERROR;
1800 }
1801
1802 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1803 *outFence = mLastQueueBufferFence;
1804
John Reck1a61da52016-04-28 13:18:15 -07001805 // Currently only SurfaceFlinger internally ever changes
1806 // GLConsumer's filtering mode, so we just use 'true' here as
1807 // this is slightly specialized for the current client of this API,
1808 // which does want filtering.
1809 GLConsumer::computeTransformMatrix(outTransformMatrix,
1810 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1811 mLastQueuedTransform, true /* filter */);
1812
Dan Stoza50101d02016-04-07 16:53:23 -07001813 return NO_ERROR;
1814}
1815
John Reckf0f13e82021-05-18 00:42:56 -04001816status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1817 Rect* outRect, uint32_t* outTransform) {
1818 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001819
1820 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001821 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1822 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1823 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001824 *outBuffer = nullptr;
1825 *outFence = Fence::NO_FENCE;
1826 return NO_ERROR;
1827 }
1828
1829 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1830 *outFence = mLastQueueBufferFence;
1831 *outRect = mLastQueuedCrop;
1832 *outTransform = mLastQueuedTransform;
1833
1834 return NO_ERROR;
1835}
1836
Brian Anderson3890c392016-07-25 12:48:08 -07001837void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1838 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001839}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001840
Brian Anderson3890c392016-07-25 12:48:08 -07001841void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001842 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001843 FrameEventHistoryDelta* outDelta) {
1844 if (newTimestamps == nullptr && outDelta == nullptr) {
1845 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001846 }
1847
1848 ATRACE_CALL();
1849 BQ_LOGV("addAndGetFrameTimestamps");
1850 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001851 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001852 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001853 listener = mCore->mConsumerListener;
1854 }
Yi Kong48a619f2018-06-05 16:34:59 -07001855 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001856 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001857 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001858}
1859
Dan Stoza289ade12014-02-28 11:17:17 -08001860void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1861 // If we're here, it means that a producer we were connected to died.
1862 // We're guaranteed that we are still connected to it because we remove
1863 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1864 // without synchronization here.
1865 int api = mCore->mConnectedApi;
1866 disconnect(api);
1867}
1868
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001869status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1870 BQ_LOGV("getUniqueId");
1871
1872 *outId = mCore->mUniqueId;
1873 return NO_ERROR;
1874}
1875
Chia-I Wue2786ea2017-08-07 10:36:08 -07001876status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1877 BQ_LOGV("getConsumerUsage");
1878
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001879 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001880 *outUsage = mCore->mConsumerUsageBits;
1881 return NO_ERROR;
1882}
1883
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001884status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1885 ATRACE_CALL();
1886 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1887
1888 std::lock_guard<std::mutex> lock(mCore->mMutex);
1889
1890 mCore->mAutoPrerotation = autoPrerotation;
1891 return NO_ERROR;
1892}
1893
Ady Abraham107788e2023-10-17 12:31:08 -07001894#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001895status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1896 int8_t changeFrameRateStrategy) {
1897 ATRACE_CALL();
1898 BQ_LOGV("setFrameRate: %.2f", frameRate);
1899
1900 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1901 "BufferQueueProducer::setFrameRate")) {
1902 return BAD_VALUE;
1903 }
1904
1905 sp<IConsumerListener> listener;
1906 {
1907 std::lock_guard<std::mutex> lock(mCore->mMutex);
1908 listener = mCore->mConsumerListener;
1909 }
1910 if (listener != nullptr) {
1911 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1912 }
1913 return NO_ERROR;
1914}
1915#endif
1916
John Reckdb164ff2024-04-03 16:59:28 -04001917#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1918status_t BufferQueueProducer::setAdditionalOptions(
1919 const std::vector<gui::AdditionalOptions>& options) {
1920 ATRACE_CALL();
1921 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1922
1923 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1924 return INVALID_OPERATION;
1925 }
1926
1927 std::lock_guard<std::mutex> lock(mCore->mMutex);
1928
1929 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1930 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1931 return NO_INIT;
1932 }
1933
1934 if (mCore->mAdditionalOptions != options) {
1935 mCore->mAdditionalOptions = options;
1936 mCore->mAdditionalOptionsGenerationId++;
1937 }
1938 return NO_ERROR;
1939}
1940#endif
1941
Dan Stoza289ade12014-02-28 11:17:17 -08001942} // namespace android