blob: 473a374a595e58f9808affbd457801ec041da8a9 [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();
Dan Stoza289ade12014-02-28 11:17:17 -0800941
Yi Kong48a619f2018-06-05 16:34:59 -0700942 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800943 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800944 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800945 }
946
Brian Anderson3d4039d2016-09-23 16:31:30 -0700947 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
948
Dan Stoza289ade12014-02-28 11:17:17 -0800949 switch (scalingMode) {
950 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
951 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
952 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
953 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
954 break;
955 default:
956 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800957 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800958 }
959
Dan Stoza8dc55392014-11-04 11:37:46 -0800960 sp<IConsumerListener> frameAvailableListener;
961 sp<IConsumerListener> frameReplacedListener;
962 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700963 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800964 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500965 int connectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -0700966 bool enableEglCpuThrottling = true;
John Reckd2c4a4a2024-02-07 10:31:09 -0500967 sp<Fence> lastQueuedFence;
968
Dan Stoza289ade12014-02-28 11:17:17 -0800969 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200970 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800971
972 if (mCore->mIsAbandoned) {
973 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
974 return NO_INIT;
975 }
976
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700977 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
978 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
979 return NO_INIT;
980 }
981
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800982 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800983 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800984 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800985 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700986 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800987 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800989 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800990 } else if (!mSlots[slot].mRequestBufferCalled) {
991 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
992 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800993 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800994 }
995
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700996 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700997 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700998 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700999 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001000 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001001 mSlots[slot].mBufferState.mShared = true;
1002 }
1003
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001004 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001005 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
1006 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
1007 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -07001008 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001009 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -08001010
1011 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
1012 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -07001013 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -08001014 crop.intersect(bufferRect, &croppedRect);
1015 if (croppedRect != crop) {
1016 BQ_LOGE("queueBuffer: crop rect is not contained within the "
1017 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001018 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001019 }
1020
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001021 // Override UNKNOWN dataspace with consumer default
1022 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
1023 dataSpace = mCore->mDefaultBufferDataSpace;
1024 }
1025
Brian Andersond6927fb2016-07-23 23:37:30 -07001026 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001027 mSlots[slot].mBufferState.queue();
1028
Brian Andersond6927fb2016-07-23 23:37:30 -07001029 // Increment the frame counter and store a local version of it
1030 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -08001031 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -07001032 currentFrameNumber = mCore->mFrameCounter;
1033 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001034
Dan Stoza289ade12014-02-28 11:17:17 -08001035 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1036 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1037 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001038 item.mTransform = transform &
1039 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001040 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001041 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1042 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001043 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001044 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001045 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001046 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -07001047 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001048 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001049 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001050 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001051 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001052 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001053 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001054 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001055 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001056 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001057 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001058 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001059
Ruben Brunk1681d952014-06-27 15:51:55 -07001060 mStickyTransform = stickyTransform;
1061
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001062 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001063 if (mCore->mSharedBufferMode) {
1064 mCore->mSharedBufferCache.crop = crop;
1065 mCore->mSharedBufferCache.transform = transform;
1066 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001067 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001068 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001069 }
1070
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001071 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001072 if (mCore->mQueue.empty()) {
1073 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1074 // and simply queue this buffer
1075 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001076 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001077 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001078 // When the queue is not empty, we need to look at the last buffer
1079 // in the queue to see if we need to replace it
1080 const BufferItem& last = mCore->mQueue.itemAt(
1081 mCore->mQueue.size() - 1);
1082 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001083
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001084 if (!last.mIsStale) {
1085 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001086
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001087 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001088 // still be around. Mark it as no longer shared if this
1089 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001090 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001091 mSlots[last.mSlot].mBufferState.isFree()) {
1092 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001093 }
1094 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001095 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1096 mCore->mActiveBuffers.erase(last.mSlot);
1097 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001098 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001099 }
Dan Stoza289ade12014-02-28 11:17:17 -08001100 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001101
Steven Thomas862e7532019-07-10 19:21:03 -07001102 // Make sure to merge the damage rect from the frame we're about
1103 // to drop into the new frame's damage rect.
1104 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1105 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1106 item.mSurfaceDamage = Region::INVALID_REGION;
1107 } else {
1108 item.mSurfaceDamage |= last.mSurfaceDamage;
1109 }
1110
Dan Stoza289ade12014-02-28 11:17:17 -08001111 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001112 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001113 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001114 } else {
1115 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001116 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001117 }
1118 }
1119
1120 mCore->mBufferHasBeenQueued = true;
Patrick Williams078d7362024-08-27 10:20:39 -05001121#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1122 mCore->notifyBufferReleased();
1123#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001124 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001125#endif
Dan Stoza50101d02016-04-07 16:53:23 -07001126 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001127
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001128 output->width = mCore->mDefaultWidth;
1129 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001130 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001131 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1132 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001133
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001134 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001135#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001136 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001137#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001138 // Take a ticket for the callback functions
1139 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001140
Pablo Ceballos9e314332016-01-12 13:49:19 -08001141 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001142
1143 connectedApi = mCore->mConnectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001144 if (flags::bq_producer_throttles_only_async_mode()) {
1145 enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1146 }
John Reckd2c4a4a2024-02-07 10:31:09 -05001147 lastQueuedFence = std::move(mLastQueueBufferFence);
1148
1149 mLastQueueBufferFence = std::move(acquireFence);
1150 mLastQueuedCrop = item.mCrop;
1151 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001152 } // Autolock scope
1153
Irvel468051e2016-06-13 16:44:44 -07001154 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1155 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1156 // there will be no Binder call
1157 if (!mConsumerIsSurfaceFlinger) {
1158 item.mGraphicBuffer.clear();
1159 }
1160
JihCheng Chiu544c5222019-04-02 20:50:58 +08001161 // Update and get FrameEventHistory.
1162 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1163 NewFrameEventsEntry newFrameEventsEntry = {
1164 currentFrameNumber,
1165 postedTime,
1166 requestedPresentTimestamp,
1167 std::move(acquireFenceTime)
1168 };
1169 addAndGetFrameTimestamps(&newFrameEventsEntry,
1170 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1171
Dan Stoza8dc55392014-11-04 11:37:46 -08001172 // Call back without the main BufferQueue lock held, but with the callback
1173 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001174
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001175 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001176 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001177 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001178 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001179 }
1180
Yi Kong48a619f2018-06-05 16:34:59 -07001181 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001182 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001183 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001184 frameReplacedListener->onFrameReplaced(item);
1185 }
1186
1187 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001188 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001189 }
1190
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001191 // Wait without lock held
Mathias Agopianb96661f2024-09-17 11:45:38 -07001192 if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001193 // Waiting here allows for two full buffers to be queued but not a
1194 // third. In the event that frames take varying time, this makes a
1195 // small trade-off in favor of latency rather than throughput.
1196 lastQueuedFence->waitForever("Throttling EGL Production");
1197 }
1198
Dan Stoza289ade12014-02-28 11:17:17 -08001199 return NO_ERROR;
1200}
1201
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001202status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001203 ATRACE_CALL();
1204 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001205
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001206 sp<IConsumerListener> listener;
1207 bool callOnFrameCancelled = false;
1208 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1209 {
1210 std::lock_guard<std::mutex> lock(mCore->mMutex);
1211
1212 if (mCore->mIsAbandoned) {
1213 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1214 return NO_INIT;
1215 }
1216
1217 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1218 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1219 return NO_INIT;
1220 }
1221
1222 if (mCore->mSharedBufferMode) {
1223 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1224 return BAD_VALUE;
1225 }
1226
1227 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1228 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1229 BufferQueueDefs::NUM_BUFFER_SLOTS);
1230 return BAD_VALUE;
1231 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1232 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1233 "(state = %s)",
1234 slot, mSlots[slot].mBufferState.string());
1235 return BAD_VALUE;
1236 } else if (fence == nullptr) {
1237 BQ_LOGE("cancelBuffer: fence is NULL");
1238 return BAD_VALUE;
1239 }
1240
1241 mSlots[slot].mBufferState.cancel();
1242
1243 // After leaving shared buffer mode, the shared buffer will still be around.
1244 // Mark it as no longer shared if this operation causes it to be free.
1245 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1246 mSlots[slot].mBufferState.mShared = false;
1247 }
1248
1249 // Don't put the shared buffer on the free list.
1250 if (!mSlots[slot].mBufferState.isShared()) {
1251 mCore->mActiveBuffers.erase(slot);
1252 mCore->mFreeBuffers.push_back(slot);
1253 }
1254
1255 auto gb = mSlots[slot].mGraphicBuffer;
1256 if (gb != nullptr) {
1257 callOnFrameCancelled = true;
1258 bufferId = gb->getId();
1259 }
1260 mSlots[slot].mFence = fence;
Patrick Williams078d7362024-08-27 10:20:39 -05001261#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1262 mCore->notifyBufferReleased();
1263#else
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001264 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001265#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001266 listener = mCore->mConsumerListener;
1267 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001268 }
1269
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001270 if (listener != nullptr && callOnFrameCancelled) {
1271 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001272 }
1273
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001274 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001275}
1276
1277int BufferQueueProducer::query(int what, int *outValue) {
1278 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001279 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001280
Yi Kong48a619f2018-06-05 16:34:59 -07001281 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001282 BQ_LOGE("query: outValue was NULL");
1283 return BAD_VALUE;
1284 }
1285
1286 if (mCore->mIsAbandoned) {
1287 BQ_LOGE("query: BufferQueue has been abandoned");
1288 return NO_INIT;
1289 }
1290
1291 int value;
1292 switch (what) {
1293 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001294 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001295 break;
1296 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001297 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001298 break;
1299 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001300 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001301 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001302 case NATIVE_WINDOW_LAYER_COUNT:
1303 // All BufferQueue buffers have a single layer.
1304 value = BQ_LAYER_COUNT;
1305 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001306 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001307 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001308 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001309 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001310 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001311 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001312 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1313 value = (mCore->mQueue.size() > 1);
1314 break;
1315 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001316 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001317 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001318 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001319 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1320 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1321 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001322 case NATIVE_WINDOW_BUFFER_AGE:
1323 if (mCore->mBufferAge > INT32_MAX) {
1324 value = 0;
1325 } else {
1326 value = static_cast<int32_t>(mCore->mBufferAge);
1327 }
1328 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001329 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1330 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1331 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001332 default:
1333 return BAD_VALUE;
1334 }
1335
1336 BQ_LOGV("query: %d? %d", what, value);
1337 *outValue = value;
1338 return NO_ERROR;
1339}
1340
Dan Stozaf0eaf252014-03-21 13:05:51 -07001341status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001342 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1343 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001344 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001345 mConsumerName = mCore->mConsumerName;
1346 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1347 producerControlledByApp ? "true" : "false");
1348
1349 if (mCore->mIsAbandoned) {
1350 BQ_LOGE("connect: BufferQueue has been abandoned");
1351 return NO_INIT;
1352 }
1353
Yi Kong48a619f2018-06-05 16:34:59 -07001354 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001355 BQ_LOGE("connect: BufferQueue has no consumer");
1356 return NO_INIT;
1357 }
1358
Yi Kong48a619f2018-06-05 16:34:59 -07001359 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001360 BQ_LOGE("connect: output was NULL");
1361 return BAD_VALUE;
1362 }
1363
1364 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1365 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1366 mCore->mConnectedApi, api);
1367 return BAD_VALUE;
1368 }
1369
1370 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1371 mDequeueTimeout < 0 ?
1372 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1373 mCore->mMaxBufferCount) -
1374 mCore->getMaxBufferCountLocked();
1375 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1376 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1377 "slots. Delta = %d", delta);
1378 return BAD_VALUE;
1379 }
1380
Dan Stoza289ade12014-02-28 11:17:17 -08001381 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001382 switch (api) {
1383 case NATIVE_WINDOW_API_EGL:
1384 case NATIVE_WINDOW_API_CPU:
1385 case NATIVE_WINDOW_API_MEDIA:
1386 case NATIVE_WINDOW_API_CAMERA:
1387 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001388
1389 output->width = mCore->mDefaultWidth;
1390 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001391 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001392 output->numPendingBuffers =
1393 static_cast<uint32_t>(mCore->mQueue.size());
1394 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001395 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001396 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001397
Yi Kong48a619f2018-06-05 16:34:59 -07001398 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001399 // Set up a death notification so that we can disconnect
1400 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001401#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001402 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001403 status = IInterface::asBinder(listener)->linkToDeath(
1404 static_cast<IBinder::DeathRecipient*>(this));
1405 if (status != NO_ERROR) {
1406 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1407 strerror(-status), status);
1408 }
1409 mCore->mLinkedToDeath = listener;
1410 }
Chong Zhang62493092020-01-15 16:04:47 -08001411#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001412 mCore->mConnectedProducerListener = listener;
1413 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Sungtak Leecd217472024-07-19 17:17:40 +00001414#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1415 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1416#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001417 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001418 break;
1419 default:
1420 BQ_LOGE("connect: unknown API %d", api);
1421 status = BAD_VALUE;
1422 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001423 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001424 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001425 mCore->mBufferHasBeenQueued = false;
1426 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001427 mCore->mQueueBufferCanDrop = false;
1428 mCore->mLegacyBufferDrop = true;
1429 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1430 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1431 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001432 }
Dan Stoza289ade12014-02-28 11:17:17 -08001433
Pablo Ceballos981066c2016-02-18 12:54:37 -08001434 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001435#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1436 mCore->mAdditionalOptions.clear();
1437#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001438 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001439 return status;
1440}
1441
Robert Carr97b9c862016-09-08 13:54:35 -07001442status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001443 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001444 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001445
1446 int status = NO_ERROR;
1447 sp<IConsumerListener> listener;
1448 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001449 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001450
1451 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001452 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001453 return NO_ERROR;
1454 }
1455 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1456 }
1457
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001458 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001459
1460 if (mCore->mIsAbandoned) {
1461 // It's not really an error to disconnect after the surface has
1462 // been abandoned; it should just be a no-op.
1463 return NO_ERROR;
1464 }
1465
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001466 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001467 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1468 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1469 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001470 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001471 // If we're asked to disconnect the currently connected api but
1472 // nobody is connected, it's not really an error.
1473 if (api == BufferQueueCore::NO_CONNECTED_API) {
1474 return NO_ERROR;
1475 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001476 }
1477
Dan Stoza289ade12014-02-28 11:17:17 -08001478 switch (api) {
1479 case NATIVE_WINDOW_API_EGL:
1480 case NATIVE_WINDOW_API_CPU:
1481 case NATIVE_WINDOW_API_MEDIA:
1482 case NATIVE_WINDOW_API_CAMERA:
1483 if (mCore->mConnectedApi == api) {
1484 mCore->freeAllBuffersLocked();
1485
Chong Zhang62493092020-01-15 16:04:47 -08001486#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001487 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001488 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001489 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001490 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001491 // This can fail if we're here because of the death
1492 // notification, but we just ignore it
1493 token->unlinkToDeath(
1494 static_cast<IBinder::DeathRecipient*>(this));
1495 }
Chong Zhang62493092020-01-15 16:04:47 -08001496#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001497 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001498 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001499 mCore->mLinkedToDeath = nullptr;
1500 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001501 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001502 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001503 mCore->mSidebandStream.clear();
Patrick Williams078d7362024-08-27 10:20:39 -05001504#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1505 mCore->notifyBufferReleased();
1506#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001507 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001508#endif
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001509 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001510#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1511 mCore->mAdditionalOptions.clear();
1512#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001513 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001514 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1515 BQ_LOGE("disconnect: not connected (req=%d)", api);
1516 status = NO_INIT;
1517 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001518 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001519 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001520 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001521 }
1522 break;
1523 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001524 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001525 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001526 break;
1527 }
1528 } // Autolock scope
1529
1530 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001531 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001532 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001533 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001534 }
1535
1536 return status;
1537}
1538
Jesse Hall399184a2014-03-03 15:42:54 -08001539status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001540 sp<IConsumerListener> listener;
1541 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001542 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001543 mCore->mSidebandStream = stream;
1544 listener = mCore->mConsumerListener;
1545 } // Autolock scope
1546
Yi Kong48a619f2018-06-05 16:34:59 -07001547 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001548 listener->onSidebandStreamChanged();
1549 }
Jesse Hall399184a2014-03-03 15:42:54 -08001550 return NO_ERROR;
1551}
1552
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001553void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001554 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001555 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001556
1557 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001558 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001559 uint32_t allocWidth = 0;
1560 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001561 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001562 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001563 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001564#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1565 std::vector<gui::AdditionalOptions> allocOptions;
1566 uint32_t allocOptionsGenId = 0;
1567#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001568 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001569 std::unique_lock<std::mutex> lock(mCore->mMutex);
1570 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001571
Dan Stoza9de72932015-04-16 17:28:43 -07001572 if (!mCore->mAllowAllocation) {
1573 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1574 "BufferQueue");
1575 return;
1576 }
1577
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001578 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1579 // both allocateBuffers and dequeueBuffer.
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001580 if (mCore->mFreeSlots.empty()) {
1581 BQ_LOGV("allocateBuffers: a slot was occupied while "
1582 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001583 return;
1584 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001585
Antoine Labour78014f32014-07-15 21:17:03 -07001586 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1587 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001588 if (useDefaultSize && mCore->mAutoPrerotation &&
1589 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1590 std::swap(allocWidth, allocHeight);
1591 }
1592
Antoine Labour78014f32014-07-15 21:17:03 -07001593 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1594 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001595 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001596
John Reckdb164ff2024-04-03 16:59:28 -04001597#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1598 allocOptions = mCore->mAdditionalOptions;
1599 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1600#endif
1601
Antoine Labour78014f32014-07-15 21:17:03 -07001602 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001603
Antoine Labour78014f32014-07-15 21:17:03 -07001604 } // Autolock scope
1605
John Reckdb164ff2024-04-03 16:59:28 -04001606#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1607 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1608 tempOptions.reserve(allocOptions.size());
1609 for (const auto& it : allocOptions) {
1610 tempOptions.emplace_back(it.name.c_str(), it.value);
1611 }
1612 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1613 .importBuffer = true,
1614 .width = allocWidth,
1615 .height = allocHeight,
1616 .format = allocFormat,
1617 .layerCount = BQ_LAYER_COUNT,
1618 .usage = allocUsage,
1619 .requestorName = allocName,
1620 .extras = std::move(tempOptions),
1621 };
1622#endif
1623
John Reckdb164ff2024-04-03 16:59:28 -04001624#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001625 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
John Reckdb164ff2024-04-03 16:59:28 -04001626#else
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001627 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
1628 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
1629 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001630#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001631
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001632 status_t result = graphicBuffer->initCheck();
Mathias Agopian0556d792017-03-22 15:49:32 -07001633
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001634 if (result != NO_ERROR) {
1635 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1636 " %u, usage %#" PRIx64 ")", width, height, format, usage);
1637 std::lock_guard<std::mutex> lock(mCore->mMutex);
1638 mCore->mIsAllocating = false;
1639 mCore->mIsAllocatingCondition.notify_all();
1640 return;
Antoine Labour78014f32014-07-15 21:17:03 -07001641 }
1642
1643 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001644 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001645 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1646 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001647 if (useDefaultSize && mCore->mAutoPrerotation &&
1648 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1649 std::swap(checkWidth, checkHeight);
1650 }
1651
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001652 PixelFormat checkFormat = format != 0 ?
1653 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001654 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001655 bool allocOptionsChanged = false;
1656#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1657 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1658#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001659 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001660 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001661 // Something changed while we released the lock. Retry.
1662 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1663 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001664 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001665 continue;
1666 }
1667
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001668 if (mCore->mFreeSlots.empty()) {
1669 BQ_LOGV("allocateBuffers: a slot was occupied while "
1670 "allocating. Dropping allocated buffer.");
1671 } else {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001672 auto slot = mCore->mFreeSlots.begin();
1673 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001674 mSlots[*slot].mGraphicBuffer = graphicBuffer;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001675 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001676#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1677 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1678#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001679
1680 // freeBufferLocked puts this slot on the free slots list. Since
1681 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001682 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001683
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001684 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1685 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001686
1687 // Make sure the erase is done after all uses of the slot
1688 // iterator since it will be invalid after this point.
1689 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001690 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001691
Antoine Labour78014f32014-07-15 21:17:03 -07001692 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001693 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001694 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001695
1696 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1697 // waiting anymore so it can use the buffer we just allocated.
1698 while (mDequeueWaitingForAllocation) {
1699 mDequeueWaitingForAllocationCondition.wait(lock);
1700 }
Antoine Labour78014f32014-07-15 21:17:03 -07001701 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001702 }
1703}
1704
Dan Stoza9de72932015-04-16 17:28:43 -07001705status_t BufferQueueProducer::allowAllocation(bool allow) {
1706 ATRACE_CALL();
1707 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1708
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001709 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001710 mCore->mAllowAllocation = allow;
1711 return NO_ERROR;
1712}
1713
Dan Stoza812ed062015-06-02 15:45:22 -07001714status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1715 ATRACE_CALL();
1716 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1717
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001718 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001719 mCore->mGenerationNumber = generationNumber;
1720 return NO_ERROR;
1721}
1722
Dan Stozac6f30bd2015-06-08 09:32:50 -07001723String8 BufferQueueProducer::getConsumerName() const {
1724 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001725 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001726 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001727 return mConsumerName;
1728}
1729
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001730status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001731 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001732 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001733
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001734 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001735 if (!sharedBufferMode) {
1736 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001737 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001738 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001739 return NO_ERROR;
1740}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001741
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001742status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1743 ATRACE_CALL();
1744 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1745
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001746 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001747
1748 mCore->mAutoRefresh = autoRefresh;
1749 return NO_ERROR;
1750}
1751
Dan Stoza127fc632015-06-30 13:43:32 -07001752status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1753 ATRACE_CALL();
1754 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1755
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001756 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001757 bool dequeueBufferCannotBlock =
1758 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1759 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001760 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1761 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1762 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1763 "available slots. Delta = %d", delta);
1764 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001765 }
1766
Pablo Ceballos981066c2016-02-18 12:54:37 -08001767 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001768 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1769 if (timeout > 0) {
1770 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001771 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001772
Pablo Ceballos981066c2016-02-18 12:54:37 -08001773 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001774 return NO_ERROR;
1775}
1776
Sungtak Lee3249fb62019-03-02 16:40:47 -08001777status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1778 ATRACE_CALL();
1779 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1780
1781 std::lock_guard<std::mutex> lock(mCore->mMutex);
1782 mCore->mLegacyBufferDrop = drop;
1783 return NO_ERROR;
1784}
1785
Dan Stoza50101d02016-04-07 16:53:23 -07001786status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001787 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001788 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001789
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001790 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001791 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1792
Dan Stoza50101d02016-04-07 16:53:23 -07001793 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1794 *outBuffer = nullptr;
1795 *outFence = Fence::NO_FENCE;
1796 return NO_ERROR;
1797 }
1798
1799 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1800 *outFence = mLastQueueBufferFence;
1801
John Reck1a61da52016-04-28 13:18:15 -07001802 // Currently only SurfaceFlinger internally ever changes
1803 // GLConsumer's filtering mode, so we just use 'true' here as
1804 // this is slightly specialized for the current client of this API,
1805 // which does want filtering.
1806 GLConsumer::computeTransformMatrix(outTransformMatrix,
1807 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1808 mLastQueuedTransform, true /* filter */);
1809
Dan Stoza50101d02016-04-07 16:53:23 -07001810 return NO_ERROR;
1811}
1812
John Reckf0f13e82021-05-18 00:42:56 -04001813status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1814 Rect* outRect, uint32_t* outTransform) {
1815 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001816
1817 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001818 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1819 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1820 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001821 *outBuffer = nullptr;
1822 *outFence = Fence::NO_FENCE;
1823 return NO_ERROR;
1824 }
1825
1826 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1827 *outFence = mLastQueueBufferFence;
1828 *outRect = mLastQueuedCrop;
1829 *outTransform = mLastQueuedTransform;
1830
1831 return NO_ERROR;
1832}
1833
Brian Anderson3890c392016-07-25 12:48:08 -07001834void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1835 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001836}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001837
Brian Anderson3890c392016-07-25 12:48:08 -07001838void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001839 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001840 FrameEventHistoryDelta* outDelta) {
1841 if (newTimestamps == nullptr && outDelta == nullptr) {
1842 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001843 }
1844
1845 ATRACE_CALL();
1846 BQ_LOGV("addAndGetFrameTimestamps");
1847 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001848 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001849 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001850 listener = mCore->mConsumerListener;
1851 }
Yi Kong48a619f2018-06-05 16:34:59 -07001852 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001853 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001854 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001855}
1856
Dan Stoza289ade12014-02-28 11:17:17 -08001857void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1858 // If we're here, it means that a producer we were connected to died.
1859 // We're guaranteed that we are still connected to it because we remove
1860 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1861 // without synchronization here.
1862 int api = mCore->mConnectedApi;
1863 disconnect(api);
1864}
1865
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001866status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1867 BQ_LOGV("getUniqueId");
1868
1869 *outId = mCore->mUniqueId;
1870 return NO_ERROR;
1871}
1872
Chia-I Wue2786ea2017-08-07 10:36:08 -07001873status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1874 BQ_LOGV("getConsumerUsage");
1875
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001876 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001877 *outUsage = mCore->mConsumerUsageBits;
1878 return NO_ERROR;
1879}
1880
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001881status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1882 ATRACE_CALL();
1883 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1884
1885 std::lock_guard<std::mutex> lock(mCore->mMutex);
1886
1887 mCore->mAutoPrerotation = autoPrerotation;
1888 return NO_ERROR;
1889}
1890
Ady Abraham107788e2023-10-17 12:31:08 -07001891#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001892status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1893 int8_t changeFrameRateStrategy) {
1894 ATRACE_CALL();
1895 BQ_LOGV("setFrameRate: %.2f", frameRate);
1896
1897 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1898 "BufferQueueProducer::setFrameRate")) {
1899 return BAD_VALUE;
1900 }
1901
1902 sp<IConsumerListener> listener;
1903 {
1904 std::lock_guard<std::mutex> lock(mCore->mMutex);
1905 listener = mCore->mConsumerListener;
1906 }
1907 if (listener != nullptr) {
1908 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1909 }
1910 return NO_ERROR;
1911}
1912#endif
1913
John Reckdb164ff2024-04-03 16:59:28 -04001914#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1915status_t BufferQueueProducer::setAdditionalOptions(
1916 const std::vector<gui::AdditionalOptions>& options) {
1917 ATRACE_CALL();
1918 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1919
1920 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1921 return INVALID_OPERATION;
1922 }
1923
1924 std::lock_guard<std::mutex> lock(mCore->mMutex);
1925
1926 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1927 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1928 return NO_INIT;
1929 }
1930
1931 if (mCore->mAdditionalOptions != options) {
1932 mCore->mAdditionalOptions = options;
1933 mCore->mAdditionalOptionsGenerationId++;
1934 }
1935 return NO_ERROR;
1936}
1937#endif
1938
Dan Stoza289ade12014-02-28 11:17:17 -08001939} // namespace android