blob: c08552ef7f59c5011c6ca4ab481d2fb164872c37 [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
Jim Shargo2e614a42024-10-02 19:31:53 +000043#include <utils/Errors.h>
Dan Stoza289ade12014-02-28 11:17:17 -080044#include <utils/Log.h>
45#include <utils/Trace.h>
46
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070047#include <system/window.h>
48
Mathias Agopianb96661f2024-09-17 11:45:38 -070049#include <com_android_graphics_libgui_flags.h>
50
Dan Stoza289ade12014-02-28 11:17:17 -080051namespace android {
Mathias Agopianb96661f2024-09-17 11:45:38 -070052using namespace com::android::graphics::libgui;
Dan Stoza289ade12014-02-28 11:17:17 -080053
Iris Chang430193f2019-12-04 16:25:46 +080054// Macros for include BufferQueueCore information in log messages
55#define BQ_LOGV(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000056 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080057 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
58 ##__VA_ARGS__)
59#define BQ_LOGD(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000060 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080061 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
62 ##__VA_ARGS__)
63#define BQ_LOGI(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000064 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080065 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
66 ##__VA_ARGS__)
67#define BQ_LOGW(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000068 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080069 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
70 ##__VA_ARGS__)
71#define BQ_LOGE(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000072 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080073 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
74 ##__VA_ARGS__)
75
Craig Donner6ebc46a2016-10-21 15:23:44 -070076static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080077ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070078
Irvel468051e2016-06-13 16:44:44 -070079BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
80 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080081 mCore(core),
82 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070083 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070084 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070085 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080086 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070087 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080088 mCallbackMutex(),
89 mNextCallbackTicket(0),
90 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070091 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010092 mDequeueTimeout(-1),
93 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080094
95BufferQueueProducer::~BufferQueueProducer() {}
96
97status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
98 ATRACE_CALL();
99 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200100 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800101
102 if (mCore->mIsAbandoned) {
103 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
104 return NO_INIT;
105 }
106
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700107 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
108 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
109 return NO_INIT;
110 }
111
Jim Shargo2e614a42024-10-02 19:31:53 +0000112 int maxSlot = mCore->getTotalSlotCountLocked();
113 if (slot < 0 || slot >= maxSlot) {
114 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", slot, maxSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800115 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700116 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800117 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700118 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800119 return BAD_VALUE;
120 }
121
122 mSlots[slot].mRequestBufferCalled = true;
123 *buf = mSlots[slot].mGraphicBuffer;
124 return NO_ERROR;
125}
126
Jim Shargo2e614a42024-10-02 19:31:53 +0000127#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
128status_t BufferQueueProducer::extendSlotCount(int size) {
129 ATRACE_CALL();
130
131 sp<IConsumerListener> listener;
132 {
133 std::lock_guard<std::mutex> lock(mCore->mMutex);
134 BQ_LOGV("extendSlotCount: size %d", size);
135
136 if (mCore->mIsAbandoned) {
137 BQ_LOGE("extendSlotCount: BufferQueue has been abandoned");
138 return NO_INIT;
139 }
140
141 if (!mCore->mAllowExtendedSlotCount) {
142 BQ_LOGE("extendSlotCount: Consumer did not allow unlimited slots");
143 return INVALID_OPERATION;
144 }
145
146 int maxBeforeExtension = mCore->mMaxBufferCount;
147
148 if (size == maxBeforeExtension) {
149 return NO_ERROR;
150 }
151
152 if (size < maxBeforeExtension) {
153 return BAD_VALUE;
154 }
155
156 if (status_t ret = mCore->extendSlotCountLocked(size); ret != OK) {
157 return ret;
158 }
159 listener = mCore->mConsumerListener;
160 }
161
162 if (listener) {
163 listener->onSlotCountChanged(size);
164 }
165
166 return NO_ERROR;
167}
168#endif
169
Pablo Ceballosfa455352015-08-12 17:47:47 -0700170status_t BufferQueueProducer::setMaxDequeuedBufferCount(
171 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700172 int maxBufferCount;
173 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
174}
175
176status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
177 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700178 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700179 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
180 maxDequeuedBuffers);
181
Pablo Ceballos981066c2016-02-18 12:54:37 -0800182 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700183 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200184 std::unique_lock<std::mutex> lock(mCore->mMutex);
185 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700186
187 if (mCore->mIsAbandoned) {
188 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
189 "abandoned");
190 return NO_INIT;
191 }
192
Brian Lindahlc794b692023-01-31 15:42:47 -0700193 *maxBufferCount = mCore->getMaxBufferCountLocked();
194
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700195 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
196 return NO_ERROR;
197 }
198
Pablo Ceballos72daab62015-12-07 16:38:43 -0800199 // The new maxDequeuedBuffer count should not be violated by the number
200 // of currently dequeued buffers
201 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700203 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800204 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700205 }
206 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800207 if (dequeuedCount > maxDequeuedBuffers) {
208 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
209 "count (%d) exceeds the current dequeued buffer count (%d)",
210 maxDequeuedBuffers, dequeuedCount);
211 return BAD_VALUE;
212 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700213
Rajat Yadavd108d682021-08-31 18:58:23 +0800214 int minUndequedBufferCount = mCore->getMinUndequeuedBufferCountLocked();
215 int bufferCount = minUndequedBufferCount + maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216
Jim Shargo2e614a42024-10-02 19:31:53 +0000217 if (bufferCount > mCore->getTotalSlotCountLocked()) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
Jim Shargo2e614a42024-10-02 19:31:53 +0000219 "(max %d)",
220 bufferCount, mCore->getTotalSlotCountLocked());
micky3878859b132025-06-17 19:05:01 -0400221 bufferCount = mCore->getTotalSlotCountLocked();
222 maxDequeuedBuffers = bufferCount - minUndequedBufferCount;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700223 return BAD_VALUE;
224 }
225
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700226 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700227 if (bufferCount < minBufferSlots) {
228 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
229 "less than minimum %d", bufferCount, minBufferSlots);
230 return BAD_VALUE;
231 }
232
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700233 if (bufferCount > mCore->mMaxBufferCount) {
234 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700235 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
236 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
237 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
238 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700239 return BAD_VALUE;
240 }
241
Pablo Ceballos72daab62015-12-07 16:38:43 -0800242 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800243 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800244 return BAD_VALUE;
245 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700246 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700247 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800248 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800249 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800250 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800251 }
Patrick Williams078d7362024-08-27 10:20:39 -0500252#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
253 mCore->notifyBufferReleased();
254#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200255 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500256#endif
Pablo Ceballosfa455352015-08-12 17:47:47 -0700257 } // Autolock scope
258
259 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700260 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800261 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700262 }
263
264 return NO_ERROR;
265}
266
267status_t BufferQueueProducer::setAsyncMode(bool async) {
268 ATRACE_CALL();
269 BQ_LOGV("setAsyncMode: async = %d", async);
270
Pablo Ceballos981066c2016-02-18 12:54:37 -0800271 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700272 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200273 std::unique_lock<std::mutex> lock(mCore->mMutex);
274 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700275
276 if (mCore->mIsAbandoned) {
277 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
278 return NO_INIT;
279 }
280
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700281 if (async == mCore->mAsyncMode) {
282 return NO_ERROR;
283 }
284
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700285 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700286 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
287 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700288 BQ_LOGE("setAsyncMode(%d): this call would cause the "
289 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700290 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
291 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
292 mCore->mMaxDequeuedBufferCount,
293 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700294 return BAD_VALUE;
295 }
296
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800297 int delta = mCore->getMaxBufferCountLocked(async,
298 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
299 - mCore->getMaxBufferCountLocked();
300
Pablo Ceballos981066c2016-02-18 12:54:37 -0800301 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
303 "available slots. Delta = %d", delta);
304 return BAD_VALUE;
305 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700306 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800307 VALIDATE_CONSISTENCY();
Patrick Williams078d7362024-08-27 10:20:39 -0500308#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
309 mCore->notifyBufferReleased();
310#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200311 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500312#endif
313
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700314 if (delta < 0) {
315 listener = mCore->mConsumerListener;
316 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700317 } // Autolock scope
318
319 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700320 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800321 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700322 }
323 return NO_ERROR;
324}
325
Dan Stoza5ecfb682016-01-04 17:01:02 -0800326int BufferQueueProducer::getFreeBufferLocked() const {
327 if (mCore->mFreeBuffers.empty()) {
328 return BufferQueueCore::INVALID_BUFFER_SLOT;
329 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800330 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800331 mCore->mFreeBuffers.pop_front();
332 return slot;
333}
334
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800335int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800336 if (mCore->mFreeSlots.empty()) {
337 return BufferQueueCore::INVALID_BUFFER_SLOT;
338 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800339 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800340 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800341 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800342}
343
344status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200345 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800346 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
347 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800348 bool tryAgain = true;
349 while (tryAgain) {
350 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800351 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800352 return NO_INIT;
353 }
354
Dan Stoza9f3053d2014-03-06 15:14:33 -0800355 int dequeuedCount = 0;
356 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800357 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700358 if (mSlots[s].mBufferState.isDequeued()) {
359 ++dequeuedCount;
360 }
361 if (mSlots[s].mBufferState.isAcquired()) {
362 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800363 }
364 }
365
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700366 // Producers are not allowed to dequeue more than
367 // mMaxDequeuedBufferCount buffers.
368 // This check is only done if a buffer has already been queued
Jim Shargoa62f0512025-02-27 22:58:38 +0000369 using namespace com::android::graphics::libgui::flags;
370 bool flagGatedBufferHasBeenQueued =
371 bq_always_use_max_dequeued_buffer_count() || mCore->mBufferHasBeenQueued;
372 if (flagGatedBufferHasBeenQueued && dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700373 // Supress error logs when timeout is non-negative.
374 if (mDequeueTimeout < 0) {
375 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
376 "count (%d)", callerString,
377 mCore->mMaxDequeuedBufferCount);
378 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800379 return INVALID_OPERATION;
380 }
381
Dan Stoza0de7ea72015-04-23 13:20:51 -0700382 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
383
Dan Stozaae3c3682014-04-18 15:43:35 -0700384 // If we disconnect and reconnect quickly, we can be in a state where
385 // our slots are empty but we have many buffers in the queue. This can
386 // cause us to run out of memory if we outrun the consumer. Wait here if
387 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800388 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700389 bool tooManyBuffers = mCore->mQueue.size()
390 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700391 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800392 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700393 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700394 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700395 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700396 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700397 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700398 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700399 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800400 } else {
401 if (caller == FreeSlotCaller::Dequeue) {
402 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800403 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800404 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
405 *found = slot;
406 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800407 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800408 }
409 } else {
410 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800411 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800412 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
413 *found = slot;
414 } else {
415 *found = getFreeBufferLocked();
416 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700417 }
418 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700419 }
420
421 // If no buffer is found, or if the queue has too many buffers
422 // outstanding, wait for a buffer to be acquired or released, or for the
423 // max buffer count to change.
424 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
425 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800426 if (tryAgain) {
427 // Return an error if we're in non-blocking mode (producer and
428 // consumer are controlled by the application).
429 // However, the consumer is allowed to briefly acquire an extra
430 // buffer (which could cause us to have to wait here), which is
431 // okay, since it is only used to implement an atomic acquire +
432 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700433 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800434 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
435 return WOULD_BLOCK;
436 }
Patrick Williams078d7362024-08-27 10:20:39 -0500437#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
438 if (status_t status = waitForBufferRelease(lock, mDequeueTimeout);
439 status == TIMED_OUT) {
440 return TIMED_OUT;
441 }
442#else
Dan Stoza127fc632015-06-30 13:43:32 -0700443 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200444 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
445 std::chrono::nanoseconds(mDequeueTimeout));
446 if (result == std::cv_status::timeout) {
447 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700448 }
449 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200450 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700451 }
Patrick Williams078d7362024-08-27 10:20:39 -0500452#endif
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453 }
454 } // while (tryAgain)
455
456 return NO_ERROR;
457}
458
Patrick Williams078d7362024-08-27 10:20:39 -0500459#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
460status_t BufferQueueProducer::waitForBufferRelease(std::unique_lock<std::mutex>& lock,
461 nsecs_t timeout) const {
462 if (mDequeueTimeout >= 0) {
463 std::cv_status result =
464 mCore->mDequeueCondition.wait_for(lock, std::chrono::nanoseconds(timeout));
465 if (result == std::cv_status::timeout) {
466 return TIMED_OUT;
467 }
468 } else {
469 mCore->mDequeueCondition.wait(lock);
470 }
471 return OK;
472}
473#endif
474
Ian Elliottd11b0442017-07-18 11:05:49 -0600475status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
476 uint32_t width, uint32_t height, PixelFormat format,
477 uint64_t usage, uint64_t* outBufferAge,
478 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800479 ATRACE_CALL();
480 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200481 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800482 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700483
484 if (mCore->mIsAbandoned) {
485 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
486 return NO_INIT;
487 }
488
489 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
490 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
491 return NO_INIT;
492 }
Dan Stoza289ade12014-02-28 11:17:17 -0800493 } // Autolock scope
494
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700495 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800496
497 if ((width && !height) || (!width && height)) {
498 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
499 return BAD_VALUE;
500 }
501
502 status_t returnFlags = NO_ERROR;
Jim Shargo90842182024-11-14 00:49:27 +0000503#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza289ade12014-02-28 11:17:17 -0800504 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
505 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Jim Shargo90842182024-11-14 00:49:27 +0000506#endif
Dan Stoza9f3053d2014-03-06 15:14:33 -0800507 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800508
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000509 sp<IConsumerListener> listener;
510 bool callOnFrameDequeued = false;
511 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400512#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
513 std::vector<gui::AdditionalOptions> allocOptions;
514 uint32_t allocOptionsGenId = 0;
515#endif
516
Dan Stoza289ade12014-02-28 11:17:17 -0800517 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200518 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800519
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100520 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
521 // is finished such that we don't allocate in parallel.
522 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
523 mDequeueWaitingForAllocation = true;
524 mCore->waitWhileAllocatingLocked(lock);
525 mDequeueWaitingForAllocation = false;
526 mDequeueWaitingForAllocationCondition.notify_all();
527 }
Dan Stoza289ade12014-02-28 11:17:17 -0800528
529 if (format == 0) {
530 format = mCore->mDefaultBufferFormat;
531 }
532
533 // Enable the usage bits the consumer requested
534 usage |= mCore->mConsumerUsageBits;
535
Dan Stoza9de72932015-04-16 17:28:43 -0700536 const bool useDefaultSize = !width && !height;
537 if (useDefaultSize) {
538 width = mCore->mDefaultWidth;
539 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700540 if (mCore->mAutoPrerotation &&
541 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
542 std::swap(width, height);
543 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800544 }
Dan Stoza289ade12014-02-28 11:17:17 -0800545
Pablo Ceballos981066c2016-02-18 12:54:37 -0800546 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700547 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200548 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700549 if (status != NO_ERROR) {
550 return status;
551 }
552
553 // This should not happen
554 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
555 BQ_LOGE("dequeueBuffer: no available buffer slots");
556 return -EBUSY;
557 }
558
559 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
560
561 // If we are not allowed to allocate new buffers,
562 // waitForFreeSlotThenRelock must have returned a slot containing a
563 // buffer. If this buffer would require reallocation to meet the
564 // requested attributes, we free it and attempt to get another one.
565 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700566 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700567 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700568 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700569 return BAD_VALUE;
570 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800571 mCore->mFreeSlots.insert(found);
572 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700573 found = BufferItem::INVALID_BUFFER_SLOT;
574 continue;
575 }
576 }
Dan Stoza289ade12014-02-28 11:17:17 -0800577 }
578
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800579 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800580
John Reckdb164ff2024-04-03 16:59:28 -0400581 bool needsReallocation = buffer == nullptr ||
582 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
583
584#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
585 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
586 mCore->mAdditionalOptionsGenerationId;
587#endif
588
589 if (mCore->mSharedBufferSlot == found && needsReallocation) {
590 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800591 return BAD_VALUE;
592 }
593
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700594 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800595 mCore->mActiveBuffers.insert(found);
596 }
Dan Stoza289ade12014-02-28 11:17:17 -0800597 *outSlot = found;
598 ATRACE_BUFFER_INDEX(found);
599
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800600 attachedByConsumer = mSlots[found].mNeedsReallocation;
601 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800602
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700603 mSlots[found].mBufferState.dequeue();
604
John Reckdb164ff2024-04-03 16:59:28 -0400605 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700606 if (CC_UNLIKELY(ATRACE_ENABLED())) {
607 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000608 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700609 } else {
610 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
611 "layerCount:%d "
612 "usage:%d requested: %dx%d format:%d layerCount:%d "
613 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000614 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700615 BQ_LAYER_COUNT, usage, buffer->getWidth(),
616 buffer->getHeight(), buffer->getPixelFormat(),
617 buffer->getLayerCount(), buffer->getUsage());
618 }
619 }
Dan Stoza289ade12014-02-28 11:17:17 -0800620 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700621 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800622 mSlots[found].mRequestBufferCalled = false;
Jim Shargo90842182024-11-14 00:49:27 +0000623#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza289ade12014-02-28 11:17:17 -0800624 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
625 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
Jim Shargo90842182024-11-14 00:49:27 +0000626#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800627 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800628 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700629 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400630#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
631 allocOptions = mCore->mAdditionalOptions;
632 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
633#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800634
635 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800636 } else {
637 // We add 1 because that will be the frame number when this buffer
638 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700639 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800640 }
641
Dan Stoza800b41a2015-04-28 14:20:04 -0700642 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
643 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800644
Yi Kong48a619f2018-06-05 16:34:59 -0700645 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800646 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
647 "slot=%d w=%d h=%d format=%u",
648 found, buffer->width, buffer->height, buffer->format);
649 }
650
Jim Shargo90842182024-11-14 00:49:27 +0000651#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza289ade12014-02-28 11:17:17 -0800652 eglDisplay = mSlots[found].mEglDisplay;
653 eglFence = mSlots[found].mEglFence;
Jim Shargo90842182024-11-14 00:49:27 +0000654#endif
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700655 // Don't return a fence in shared buffer mode, except for the first
656 // frame.
657 *outFence = (mCore->mSharedBufferMode &&
658 mCore->mSharedBufferSlot == found) ?
659 Fence::NO_FENCE : mSlots[found].mFence;
Jim Shargo90842182024-11-14 00:49:27 +0000660#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza289ade12014-02-28 11:17:17 -0800661 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
Jim Shargo90842182024-11-14 00:49:27 +0000662#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800663 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700664
665 // If shared buffer mode has just been enabled, cache the slot of the
666 // first buffer that is dequeued and mark it as the shared buffer.
667 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
668 BufferQueueCore::INVALID_BUFFER_SLOT) {
669 mCore->mSharedBufferSlot = found;
670 mSlots[found].mBufferState.mShared = true;
671 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700672
673 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000674 callOnFrameDequeued = true;
675 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700676 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000677
678 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800679 } // Autolock scope
680
681 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700682 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400683
684#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
685 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
686 tempOptions.reserve(allocOptions.size());
687 for (const auto& it : allocOptions) {
688 tempOptions.emplace_back(it.name.c_str(), it.value);
689 }
690 const GraphicBufferAllocator::AllocationRequest allocRequest = {
691 .importBuffer = true,
692 .width = width,
693 .height = height,
694 .format = format,
695 .layerCount = BQ_LAYER_COUNT,
696 .usage = usage,
697 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
698 .extras = std::move(tempOptions),
699 };
Anton Ivanov8ed86592025-02-20 11:52:50 -0800700 sp<GraphicBuffer> graphicBuffer = sp<GraphicBuffer>::make(allocRequest);
John Reckdb164ff2024-04-03 16:59:28 -0400701#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000702 sp<GraphicBuffer> graphicBuffer =
Anton Ivanov8ed86592025-02-20 11:52:50 -0800703 sp<GraphicBuffer>::make(width, height, format, BQ_LAYER_COUNT, usage,
704 std::string{mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400705#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700706
707 status_t error = graphicBuffer->initCheck();
708
Dan Stoza289ade12014-02-28 11:17:17 -0800709 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200710 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800711
Chia-I Wufeec3b12017-05-25 09:34:56 -0700712 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700713 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
714 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400715#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
716 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
717#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000718 callOnFrameDequeued = true;
719 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700720 }
721
722 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200723 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700724
Chia-I Wufeec3b12017-05-25 09:34:56 -0700725 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700726 mCore->mFreeSlots.insert(*outSlot);
727 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700728 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
729 return error;
730 }
731
Dan Stoza289ade12014-02-28 11:17:17 -0800732 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700733 mCore->mFreeSlots.insert(*outSlot);
734 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800735 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
736 return NO_INIT;
737 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800738
Pablo Ceballos9e314332016-01-12 13:49:19 -0800739 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800740 } // Autolock scope
741 }
742
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000743 if (listener != nullptr && callOnFrameDequeued) {
744 listener->onFrameDequeued(bufferId);
745 }
746
Dan Stoza9f3053d2014-03-06 15:14:33 -0800747 if (attachedByConsumer) {
748 returnFlags |= BUFFER_NEEDS_REALLOCATION;
749 }
750
Jim Shargo90842182024-11-14 00:49:27 +0000751#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza289ade12014-02-28 11:17:17 -0800752 if (eglFence != EGL_NO_SYNC_KHR) {
753 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
754 1000000000);
755 // If something goes wrong, log the error, but return the buffer without
756 // synchronizing access to it. It's too late at this point to abort the
757 // dequeue operation.
758 if (result == EGL_FALSE) {
759 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
760 eglGetError());
761 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
762 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
763 }
764 eglDestroySyncKHR(eglDisplay, eglFence);
765 }
Jim Shargo90842182024-11-14 00:49:27 +0000766#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800767
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700768 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
769 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800770 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800771 mSlots[*outSlot].mGraphicBuffer != nullptr ?
772 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800773
Ian Elliottd11b0442017-07-18 11:05:49 -0600774 if (outBufferAge) {
775 *outBufferAge = mCore->mBufferAge;
776 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700777 addAndGetFrameTimestamps(nullptr, outTimestamps);
778
Dan Stoza289ade12014-02-28 11:17:17 -0800779 return returnFlags;
780}
781
Dan Stoza9f3053d2014-03-06 15:14:33 -0800782status_t BufferQueueProducer::detachBuffer(int slot) {
783 ATRACE_CALL();
784 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700785 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800786
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700787 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000788 bool callOnFrameDetached = false;
789 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700790 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200791 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700792
793 if (mCore->mIsAbandoned) {
794 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
795 return NO_INIT;
796 }
797
798 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
799 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
800 return NO_INIT;
801 }
802
803 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
804 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
805 return BAD_VALUE;
806 }
807
Jim Shargo2e614a42024-10-02 19:31:53 +0000808 const int totalSlotCount = mCore->getTotalSlotCountLocked();
809 if (slot < 0 || slot >= totalSlotCount) {
810 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700811 return BAD_VALUE;
812 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000813 // TODO(http://b/140581935): This message is BQ_LOGW because it
814 // often logs when no actionable errors are present. Return to
815 // using BQ_LOGE after ensuring this only logs during errors.
816 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700817 "(state = %s)", slot, mSlots[slot].mBufferState.string());
818 return BAD_VALUE;
819 } else if (!mSlots[slot].mRequestBufferCalled) {
820 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
821 slot);
822 return BAD_VALUE;
823 }
824
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700825 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700826 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000827 if (gb != nullptr) {
828 callOnFrameDetached = true;
829 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700830 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700831 mSlots[slot].mBufferState.detachProducer();
832 mCore->mActiveBuffers.erase(slot);
833 mCore->mFreeSlots.insert(slot);
834 mCore->clearBufferSlotLocked(slot);
Patrick Williams078d7362024-08-27 10:20:39 -0500835#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
836 mCore->notifyBufferReleased();
837#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200838 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -0500839#endif
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700840 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841 }
842
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000843 if (listener != nullptr && callOnFrameDetached) {
844 listener->onFrameDetached(bufferId);
845 }
846
Yi Kong48a619f2018-06-05 16:34:59 -0700847 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700848 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700849 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800850
Dan Stoza9f3053d2014-03-06 15:14:33 -0800851 return NO_ERROR;
852}
853
Dan Stozad9822a32014-03-28 15:25:31 -0700854status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
855 sp<Fence>* outFence) {
856 ATRACE_CALL();
857
Yi Kong48a619f2018-06-05 16:34:59 -0700858 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700859 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
860 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700861 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700862 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
863 return BAD_VALUE;
864 }
865
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700866 sp<IConsumerListener> listener;
867 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200868 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700869
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700870 if (mCore->mIsAbandoned) {
871 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
872 return NO_INIT;
873 }
874
875 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
876 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
877 return NO_INIT;
878 }
879
880 if (mCore->mSharedBufferMode) {
881 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
882 "mode");
883 return BAD_VALUE;
884 }
885
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200886 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700887
888 if (mCore->mFreeBuffers.empty()) {
889 return NO_MEMORY;
890 }
891
892 int found = mCore->mFreeBuffers.front();
893 mCore->mFreeBuffers.remove(found);
894 mCore->mFreeSlots.insert(found);
895
896 BQ_LOGV("detachNextBuffer detached slot %d", found);
897
898 *outBuffer = mSlots[found].mGraphicBuffer;
899 *outFence = mSlots[found].mFence;
900 mCore->clearBufferSlotLocked(found);
901 VALIDATE_CONSISTENCY();
902 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700903 }
904
Yi Kong48a619f2018-06-05 16:34:59 -0700905 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700906 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700907 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800908
Dan Stozad9822a32014-03-28 15:25:31 -0700909 return NO_ERROR;
910}
911
Dan Stoza9f3053d2014-03-06 15:14:33 -0800912status_t BufferQueueProducer::attachBuffer(int* outSlot,
913 const sp<android::GraphicBuffer>& buffer) {
914 ATRACE_CALL();
915
Yi Kong48a619f2018-06-05 16:34:59 -0700916 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700917 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800918 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700919 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700920 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800921 return BAD_VALUE;
922 }
923
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200924 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700925
926 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700927 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700928 return NO_INIT;
929 }
930
931 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700932 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700933 return NO_INIT;
934 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800935
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700936 if (mCore->mSharedBufferMode) {
937 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700938 return BAD_VALUE;
939 }
940
Dan Stoza812ed062015-06-02 15:45:22 -0700941 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
942 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
943 "[queue %u]", buffer->getGenerationNumber(),
944 mCore->mGenerationNumber);
945 return BAD_VALUE;
946 }
947
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200948 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700949
Dan Stoza9f3053d2014-03-06 15:14:33 -0800950 status_t returnFlags = NO_ERROR;
951 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200952 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800953 if (status != NO_ERROR) {
954 return status;
955 }
956
957 // This should not happen
958 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700959 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800960 return -EBUSY;
961 }
962
963 *outSlot = found;
964 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700965 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800966 *outSlot, returnFlags);
967
968 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700969 mSlots[*outSlot].mBufferState.attachProducer();
Jim Shargo90842182024-11-14 00:49:27 +0000970#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
Dan Stoza9f3053d2014-03-06 15:14:33 -0800971 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
Jim Shargo90842182024-11-14 00:49:27 +0000972#endif
Dan Stoza9f3053d2014-03-06 15:14:33 -0800973 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700974 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800975 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800976 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800977 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800978 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700979
Dan Stoza9f3053d2014-03-06 15:14:33 -0800980 return returnFlags;
981}
982
Dan Stoza289ade12014-02-28 11:17:17 -0800983status_t BufferQueueProducer::queueBuffer(int slot,
984 const QueueBufferInput &input, QueueBufferOutput *output) {
985 ATRACE_CALL();
986 ATRACE_BUFFER_INDEX(slot);
987
Brian Andersond6927fb2016-07-23 23:37:30 -0700988 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800989 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800990 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700991 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800992 int scalingMode;
993 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700994 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700995 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700996 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700997 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700998 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
999 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -07001000 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001001 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Brian Lindahl628cff42024-10-30 11:50:28 -06001002 const std::optional<PictureProfileHandle>& pictureProfileHandle =
1003 input.getPictureProfileHandle();
Dan Stoza289ade12014-02-28 11:17:17 -08001004
Yi Kong48a619f2018-06-05 16:34:59 -07001005 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001006 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -08001007 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001008 }
1009
Brian Anderson3d4039d2016-09-23 16:31:30 -07001010 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
1011
Dan Stoza289ade12014-02-28 11:17:17 -08001012 switch (scalingMode) {
1013 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1014 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1015 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1016 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1017 break;
1018 default:
1019 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001020 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001021 }
1022
Dan Stoza8dc55392014-11-04 11:37:46 -08001023 sp<IConsumerListener> frameAvailableListener;
1024 sp<IConsumerListener> frameReplacedListener;
1025 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -07001026 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -08001027 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -05001028 int connectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001029 bool enableEglCpuThrottling = true;
John Reckd2c4a4a2024-02-07 10:31:09 -05001030 sp<Fence> lastQueuedFence;
1031
Dan Stoza289ade12014-02-28 11:17:17 -08001032 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001033 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001034
1035 if (mCore->mIsAbandoned) {
1036 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
1037 return NO_INIT;
1038 }
1039
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001040 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1041 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
1042 return NO_INIT;
1043 }
1044
Jim Shargo2e614a42024-10-02 19:31:53 +00001045 const int totalSlotCount = mCore->getTotalSlotCountLocked();
1046 if (slot < 0 || slot >= totalSlotCount) {
1047 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001048 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001049 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001050 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001051 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -08001052 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001053 } else if (!mSlots[slot].mRequestBufferCalled) {
1054 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
1055 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001056 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001057 }
1058
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001059 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001060 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001061 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001062 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001063 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -07001064 mSlots[slot].mBufferState.mShared = true;
1065 }
1066
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001067 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001068 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
1069 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
1070 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -07001071 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001072 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -08001073
1074 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
1075 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -07001076 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -08001077 crop.intersect(bufferRect, &croppedRect);
1078 if (croppedRect != crop) {
1079 BQ_LOGE("queueBuffer: crop rect is not contained within the "
1080 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001081 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001082 }
1083
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001084 // Override UNKNOWN dataspace with consumer default
1085 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
1086 dataSpace = mCore->mDefaultBufferDataSpace;
1087 }
1088
Brian Andersond6927fb2016-07-23 23:37:30 -07001089 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001090 mSlots[slot].mBufferState.queue();
1091
Brian Andersond6927fb2016-07-23 23:37:30 -07001092 // Increment the frame counter and store a local version of it
1093 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -08001094 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -07001095 currentFrameNumber = mCore->mFrameCounter;
1096 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001097
Dan Stoza289ade12014-02-28 11:17:17 -08001098 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1099 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1100 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001101 item.mTransform = transform &
1102 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001103 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001104 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1105 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001106 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001107 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001108 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001109 item.mHdrMetadata = hdrMetadata;
Brian Lindahl628cff42024-10-30 11:50:28 -06001110 item.mPictureProfileHandle = pictureProfileHandle;
Brian Andersond6927fb2016-07-23 23:37:30 -07001111 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001112 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001113 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001114 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001115 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001116 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001117 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001118 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001119 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001120 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001121 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001122 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001123
Ruben Brunk1681d952014-06-27 15:51:55 -07001124 mStickyTransform = stickyTransform;
1125
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001126 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001127 if (mCore->mSharedBufferMode) {
1128 mCore->mSharedBufferCache.crop = crop;
1129 mCore->mSharedBufferCache.transform = transform;
1130 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001131 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001132 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001133 }
1134
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001135 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001136 if (mCore->mQueue.empty()) {
1137 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1138 // and simply queue this buffer
1139 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001140 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001141 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001142 // When the queue is not empty, we need to look at the last buffer
1143 // in the queue to see if we need to replace it
1144 const BufferItem& last = mCore->mQueue.itemAt(
1145 mCore->mQueue.size() - 1);
1146 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001147
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001148 if (!last.mIsStale) {
1149 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001150
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001151 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001152 // still be around. Mark it as no longer shared if this
1153 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001154 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001155 mSlots[last.mSlot].mBufferState.isFree()) {
1156 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001157 }
1158 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001159 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1160 mCore->mActiveBuffers.erase(last.mSlot);
1161 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001162 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001163 }
Dan Stoza289ade12014-02-28 11:17:17 -08001164 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001165
Steven Thomas862e7532019-07-10 19:21:03 -07001166 // Make sure to merge the damage rect from the frame we're about
1167 // to drop into the new frame's damage rect.
1168 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1169 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1170 item.mSurfaceDamage = Region::INVALID_REGION;
1171 } else {
1172 item.mSurfaceDamage |= last.mSurfaceDamage;
1173 }
1174
Dan Stoza289ade12014-02-28 11:17:17 -08001175 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001176 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001177 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001178 } else {
1179 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001180 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001181 }
1182 }
1183
1184 mCore->mBufferHasBeenQueued = true;
Patrick Williams078d7362024-08-27 10:20:39 -05001185#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1186 mCore->notifyBufferReleased();
1187#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001188 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001189#endif
Dan Stoza50101d02016-04-07 16:53:23 -07001190 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001191
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001192 output->width = mCore->mDefaultWidth;
1193 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001194 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001195 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1196 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001197
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001198 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001199#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001200 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001201#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001202 // Take a ticket for the callback functions
1203 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001204
Pablo Ceballos9e314332016-01-12 13:49:19 -08001205 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001206
1207 connectedApi = mCore->mConnectedApi;
Mathias Agopianb96661f2024-09-17 11:45:38 -07001208 if (flags::bq_producer_throttles_only_async_mode()) {
1209 enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1210 }
John Reckd2c4a4a2024-02-07 10:31:09 -05001211 lastQueuedFence = std::move(mLastQueueBufferFence);
1212
1213 mLastQueueBufferFence = std::move(acquireFence);
1214 mLastQueuedCrop = item.mCrop;
1215 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001216 } // Autolock scope
1217
Irvel468051e2016-06-13 16:44:44 -07001218 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1219 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1220 // there will be no Binder call
1221 if (!mConsumerIsSurfaceFlinger) {
1222 item.mGraphicBuffer.clear();
1223 }
1224
JihCheng Chiu544c5222019-04-02 20:50:58 +08001225 // Update and get FrameEventHistory.
1226 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1227 NewFrameEventsEntry newFrameEventsEntry = {
1228 currentFrameNumber,
1229 postedTime,
1230 requestedPresentTimestamp,
1231 std::move(acquireFenceTime)
1232 };
1233 addAndGetFrameTimestamps(&newFrameEventsEntry,
1234 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1235
Dan Stoza8dc55392014-11-04 11:37:46 -08001236 // Call back without the main BufferQueue lock held, but with the callback
1237 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001238
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001239 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001240 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001241 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001242 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001243 }
1244
Yi Kong48a619f2018-06-05 16:34:59 -07001245 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001246 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001247 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001248 frameReplacedListener->onFrameReplaced(item);
1249 }
1250
1251 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001252 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001253 }
1254
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001255 // Wait without lock held
Mathias Agopianb96661f2024-09-17 11:45:38 -07001256 if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001257 // Waiting here allows for two full buffers to be queued but not a
1258 // third. In the event that frames take varying time, this makes a
1259 // small trade-off in favor of latency rather than throughput.
1260 lastQueuedFence->waitForever("Throttling EGL Production");
1261 }
1262
Dan Stoza289ade12014-02-28 11:17:17 -08001263 return NO_ERROR;
1264}
1265
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001266status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001267 ATRACE_CALL();
1268 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001269
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001270 sp<IConsumerListener> listener;
1271 bool callOnFrameCancelled = false;
1272 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1273 {
1274 std::lock_guard<std::mutex> lock(mCore->mMutex);
1275
1276 if (mCore->mIsAbandoned) {
1277 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1278 return NO_INIT;
1279 }
1280
1281 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1282 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1283 return NO_INIT;
1284 }
1285
1286 if (mCore->mSharedBufferMode) {
1287 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1288 return BAD_VALUE;
1289 }
1290
Jim Shargo2e614a42024-10-02 19:31:53 +00001291 const int totalSlotCount = mCore->getTotalSlotCountLocked();
1292 if (slot < 0 || slot >= totalSlotCount) {
1293 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001294 return BAD_VALUE;
1295 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1296 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1297 "(state = %s)",
1298 slot, mSlots[slot].mBufferState.string());
1299 return BAD_VALUE;
1300 } else if (fence == nullptr) {
1301 BQ_LOGE("cancelBuffer: fence is NULL");
1302 return BAD_VALUE;
1303 }
1304
1305 mSlots[slot].mBufferState.cancel();
1306
1307 // After leaving shared buffer mode, the shared buffer will still be around.
1308 // Mark it as no longer shared if this operation causes it to be free.
1309 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1310 mSlots[slot].mBufferState.mShared = false;
1311 }
1312
1313 // Don't put the shared buffer on the free list.
1314 if (!mSlots[slot].mBufferState.isShared()) {
1315 mCore->mActiveBuffers.erase(slot);
1316 mCore->mFreeBuffers.push_back(slot);
1317 }
1318
1319 auto gb = mSlots[slot].mGraphicBuffer;
1320 if (gb != nullptr) {
1321 callOnFrameCancelled = true;
1322 bufferId = gb->getId();
1323 }
1324 mSlots[slot].mFence = fence;
Patrick Williams078d7362024-08-27 10:20:39 -05001325#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1326 mCore->notifyBufferReleased();
1327#else
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001328 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001329#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001330 listener = mCore->mConsumerListener;
1331 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001332 }
1333
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001334 if (listener != nullptr && callOnFrameCancelled) {
1335 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001336 }
1337
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001338 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001339}
1340
1341int BufferQueueProducer::query(int what, int *outValue) {
1342 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001343 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001344
Yi Kong48a619f2018-06-05 16:34:59 -07001345 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001346 BQ_LOGE("query: outValue was NULL");
1347 return BAD_VALUE;
1348 }
1349
1350 if (mCore->mIsAbandoned) {
1351 BQ_LOGE("query: BufferQueue has been abandoned");
1352 return NO_INIT;
1353 }
1354
1355 int value;
1356 switch (what) {
1357 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001358 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001359 break;
1360 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001361 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001362 break;
1363 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001364 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001365 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001366 case NATIVE_WINDOW_LAYER_COUNT:
1367 // All BufferQueue buffers have a single layer.
1368 value = BQ_LAYER_COUNT;
1369 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001370 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001371 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001372 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001373 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001374 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001375 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001376 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1377 value = (mCore->mQueue.size() > 1);
1378 break;
1379 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001380 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001381 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001382 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001383 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1384 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1385 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001386 case NATIVE_WINDOW_BUFFER_AGE:
1387 if (mCore->mBufferAge > INT32_MAX) {
1388 value = 0;
1389 } else {
1390 value = static_cast<int32_t>(mCore->mBufferAge);
1391 }
1392 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001393 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1394 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1395 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001396 default:
1397 return BAD_VALUE;
1398 }
1399
1400 BQ_LOGV("query: %d? %d", what, value);
1401 *outValue = value;
1402 return NO_ERROR;
1403}
1404
Dan Stozaf0eaf252014-03-21 13:05:51 -07001405status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001406 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1407 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001408 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001409 mConsumerName = mCore->mConsumerName;
1410 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1411 producerControlledByApp ? "true" : "false");
1412
1413 if (mCore->mIsAbandoned) {
1414 BQ_LOGE("connect: BufferQueue has been abandoned");
1415 return NO_INIT;
1416 }
1417
Yi Kong48a619f2018-06-05 16:34:59 -07001418 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001419 BQ_LOGE("connect: BufferQueue has no consumer");
1420 return NO_INIT;
1421 }
1422
Yi Kong48a619f2018-06-05 16:34:59 -07001423 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001424 BQ_LOGE("connect: output was NULL");
1425 return BAD_VALUE;
1426 }
1427
1428 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1429 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1430 mCore->mConnectedApi, api);
1431 return BAD_VALUE;
1432 }
1433
1434 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1435 mDequeueTimeout < 0 ?
1436 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1437 mCore->mMaxBufferCount) -
1438 mCore->getMaxBufferCountLocked();
1439 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1440 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1441 "slots. Delta = %d", delta);
1442 return BAD_VALUE;
1443 }
1444
Dan Stoza289ade12014-02-28 11:17:17 -08001445 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001446 switch (api) {
1447 case NATIVE_WINDOW_API_EGL:
1448 case NATIVE_WINDOW_API_CPU:
1449 case NATIVE_WINDOW_API_MEDIA:
1450 case NATIVE_WINDOW_API_CAMERA:
1451 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001452
1453 output->width = mCore->mDefaultWidth;
1454 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001455 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001456 output->numPendingBuffers =
1457 static_cast<uint32_t>(mCore->mQueue.size());
1458 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001459 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001460 output->maxBufferCount = mCore->mMaxBufferCount;
Jim Shargo2e614a42024-10-02 19:31:53 +00001461#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
1462 output->isSlotExpansionAllowed = mCore->mAllowExtendedSlotCount;
1463#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001464
Yi Kong48a619f2018-06-05 16:34:59 -07001465 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001466 // Set up a death notification so that we can disconnect
1467 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001468#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001469 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001470 status = IInterface::asBinder(listener)->linkToDeath(
Anton Ivanov8ed86592025-02-20 11:52:50 -08001471 sp<IBinder::DeathRecipient>::fromExisting(this));
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001472 if (status != NO_ERROR) {
1473 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1474 strerror(-status), status);
1475 }
1476 mCore->mLinkedToDeath = listener;
1477 }
Chong Zhang62493092020-01-15 16:04:47 -08001478#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001479 mCore->mConnectedProducerListener = listener;
1480 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Sungtak Leecd217472024-07-19 17:17:40 +00001481#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1482 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1483#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001484 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001485 break;
1486 default:
1487 BQ_LOGE("connect: unknown API %d", api);
1488 status = BAD_VALUE;
1489 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001490 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001491 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001492 mCore->mBufferHasBeenQueued = false;
1493 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001494 mCore->mQueueBufferCanDrop = false;
1495 mCore->mLegacyBufferDrop = true;
1496 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1497 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1498 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001499 }
Dan Stoza289ade12014-02-28 11:17:17 -08001500
Pablo Ceballos981066c2016-02-18 12:54:37 -08001501 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001502#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1503 mCore->mAdditionalOptions.clear();
1504#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001505 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001506 return status;
1507}
1508
Robert Carr97b9c862016-09-08 13:54:35 -07001509status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001510 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001511 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001512
1513 int status = NO_ERROR;
1514 sp<IConsumerListener> listener;
1515 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001516 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001517
1518 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001519 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001520 return NO_ERROR;
1521 }
1522 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1523 }
1524
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001525 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001526
1527 if (mCore->mIsAbandoned) {
1528 // It's not really an error to disconnect after the surface has
1529 // been abandoned; it should just be a no-op.
1530 return NO_ERROR;
1531 }
1532
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001533 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001534 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1535 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1536 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001537 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001538 // If we're asked to disconnect the currently connected api but
1539 // nobody is connected, it's not really an error.
1540 if (api == BufferQueueCore::NO_CONNECTED_API) {
1541 return NO_ERROR;
1542 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001543 }
1544
Dan Stoza289ade12014-02-28 11:17:17 -08001545 switch (api) {
1546 case NATIVE_WINDOW_API_EGL:
1547 case NATIVE_WINDOW_API_CPU:
1548 case NATIVE_WINDOW_API_MEDIA:
1549 case NATIVE_WINDOW_API_CAMERA:
1550 if (mCore->mConnectedApi == api) {
1551 mCore->freeAllBuffersLocked();
1552
Chong Zhang62493092020-01-15 16:04:47 -08001553#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001554 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001555 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001556 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001557 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001558 // This can fail if we're here because of the death
1559 // notification, but we just ignore it
Anton Ivanov8ed86592025-02-20 11:52:50 -08001560 token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Dan Stoza289ade12014-02-28 11:17:17 -08001561 }
Chong Zhang62493092020-01-15 16:04:47 -08001562#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001563 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001564 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001565 mCore->mLinkedToDeath = nullptr;
1566 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001567 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001568 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001569 mCore->mSidebandStream.clear();
Patrick Williams078d7362024-08-27 10:20:39 -05001570#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1571 mCore->notifyBufferReleased();
1572#else
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001573 mCore->mDequeueCondition.notify_all();
Patrick Williams078d7362024-08-27 10:20:39 -05001574#endif
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001575 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001576#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1577 mCore->mAdditionalOptions.clear();
1578#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001579 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001580 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1581 BQ_LOGE("disconnect: not connected (req=%d)", api);
1582 status = NO_INIT;
1583 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001584 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001585 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001586 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001587 }
1588 break;
1589 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001590 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001591 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001592 break;
1593 }
1594 } // Autolock scope
1595
1596 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001597 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001598 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001599 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001600 }
1601
1602 return status;
1603}
1604
Jesse Hall399184a2014-03-03 15:42:54 -08001605status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001606 sp<IConsumerListener> listener;
1607 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001608 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001609 mCore->mSidebandStream = stream;
1610 listener = mCore->mConsumerListener;
1611 } // Autolock scope
1612
Yi Kong48a619f2018-06-05 16:34:59 -07001613 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001614 listener->onSidebandStreamChanged();
1615 }
Jesse Hall399184a2014-03-03 15:42:54 -08001616 return NO_ERROR;
1617}
1618
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001619void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001620 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001621 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001622
1623 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001624 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001625 uint32_t allocWidth = 0;
1626 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001627 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001628 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001629 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001630#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1631 std::vector<gui::AdditionalOptions> allocOptions;
1632 uint32_t allocOptionsGenId = 0;
1633#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001634 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001635 std::unique_lock<std::mutex> lock(mCore->mMutex);
1636 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001637
Dan Stoza9de72932015-04-16 17:28:43 -07001638 if (!mCore->mAllowAllocation) {
1639 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1640 "BufferQueue");
1641 return;
1642 }
1643
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001644 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1645 // both allocateBuffers and dequeueBuffer.
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001646 if (mCore->mFreeSlots.empty()) {
1647 BQ_LOGV("allocateBuffers: a slot was occupied while "
1648 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001649 return;
1650 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001651
Antoine Labour78014f32014-07-15 21:17:03 -07001652 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1653 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001654 if (useDefaultSize && mCore->mAutoPrerotation &&
1655 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1656 std::swap(allocWidth, allocHeight);
1657 }
1658
Antoine Labour78014f32014-07-15 21:17:03 -07001659 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1660 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001661 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001662
John Reckdb164ff2024-04-03 16:59:28 -04001663#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1664 allocOptions = mCore->mAdditionalOptions;
1665 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1666#endif
1667
Antoine Labour78014f32014-07-15 21:17:03 -07001668 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001669
Antoine Labour78014f32014-07-15 21:17:03 -07001670 } // Autolock scope
1671
John Reckdb164ff2024-04-03 16:59:28 -04001672#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1673 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1674 tempOptions.reserve(allocOptions.size());
1675 for (const auto& it : allocOptions) {
1676 tempOptions.emplace_back(it.name.c_str(), it.value);
1677 }
1678 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1679 .importBuffer = true,
1680 .width = allocWidth,
1681 .height = allocHeight,
1682 .format = allocFormat,
1683 .layerCount = BQ_LAYER_COUNT,
1684 .usage = allocUsage,
1685 .requestorName = allocName,
1686 .extras = std::move(tempOptions),
1687 };
1688#endif
1689
John Reckdb164ff2024-04-03 16:59:28 -04001690#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
Anton Ivanov8ed86592025-02-20 11:52:50 -08001691 sp<GraphicBuffer> graphicBuffer = sp<GraphicBuffer>::make(allocRequest);
John Reckdb164ff2024-04-03 16:59:28 -04001692#else
Anton Ivanov8ed86592025-02-20 11:52:50 -08001693 sp<GraphicBuffer> graphicBuffer =
1694 sp<GraphicBuffer>::make(allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
1695 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001696#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001697
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001698 status_t result = graphicBuffer->initCheck();
Mathias Agopian0556d792017-03-22 15:49:32 -07001699
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001700 if (result != NO_ERROR) {
1701 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1702 " %u, usage %#" PRIx64 ")", width, height, format, usage);
1703 std::lock_guard<std::mutex> lock(mCore->mMutex);
1704 mCore->mIsAllocating = false;
1705 mCore->mIsAllocatingCondition.notify_all();
1706 return;
Antoine Labour78014f32014-07-15 21:17:03 -07001707 }
1708
1709 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001710 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001711 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1712 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001713 if (useDefaultSize && mCore->mAutoPrerotation &&
1714 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1715 std::swap(checkWidth, checkHeight);
1716 }
1717
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001718 PixelFormat checkFormat = format != 0 ?
1719 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001720 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001721 bool allocOptionsChanged = false;
1722#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1723 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1724#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001725 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001726 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001727 // Something changed while we released the lock. Retry.
1728 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1729 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001730 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001731 continue;
1732 }
1733
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001734 if (mCore->mFreeSlots.empty()) {
1735 BQ_LOGV("allocateBuffers: a slot was occupied while "
1736 "allocating. Dropping allocated buffer.");
1737 } else {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001738 auto slot = mCore->mFreeSlots.begin();
1739 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
Shuangxi Xiang045a30e2024-10-14 09:38:32 +00001740 mSlots[*slot].mGraphicBuffer = graphicBuffer;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001741 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001742#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1743 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1744#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001745
1746 // freeBufferLocked puts this slot on the free slots list. Since
1747 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001748 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001749
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001750 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1751 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001752
1753 // Make sure the erase is done after all uses of the slot
1754 // iterator since it will be invalid after this point.
1755 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001756 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001757
Antoine Labour78014f32014-07-15 21:17:03 -07001758 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001759 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001760 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001761
1762 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1763 // waiting anymore so it can use the buffer we just allocated.
1764 while (mDequeueWaitingForAllocation) {
1765 mDequeueWaitingForAllocationCondition.wait(lock);
1766 }
Antoine Labour78014f32014-07-15 21:17:03 -07001767 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001768 }
1769}
1770
Dan Stoza9de72932015-04-16 17:28:43 -07001771status_t BufferQueueProducer::allowAllocation(bool allow) {
1772 ATRACE_CALL();
1773 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1774
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001775 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001776 mCore->mAllowAllocation = allow;
1777 return NO_ERROR;
1778}
1779
Dan Stoza812ed062015-06-02 15:45:22 -07001780status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1781 ATRACE_CALL();
1782 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1783
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001784 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001785 mCore->mGenerationNumber = generationNumber;
1786 return NO_ERROR;
1787}
1788
Dan Stozac6f30bd2015-06-08 09:32:50 -07001789String8 BufferQueueProducer::getConsumerName() const {
1790 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001791 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001792 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001793 return mConsumerName;
1794}
1795
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001796status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001797 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001798 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001799
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001800 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001801 if (!sharedBufferMode) {
1802 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001803 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001804 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001805 return NO_ERROR;
1806}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001807
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001808status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1809 ATRACE_CALL();
1810 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1811
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001812 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001813
1814 mCore->mAutoRefresh = autoRefresh;
1815 return NO_ERROR;
1816}
1817
Dan Stoza127fc632015-06-30 13:43:32 -07001818status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1819 ATRACE_CALL();
1820 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1821
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001822 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001823 bool dequeueBufferCannotBlock =
1824 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1825 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001826 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1827 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1828 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1829 "available slots. Delta = %d", delta);
1830 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001831 }
1832
Pablo Ceballos981066c2016-02-18 12:54:37 -08001833 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001834 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1835 if (timeout > 0) {
1836 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001837 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001838
Pablo Ceballos981066c2016-02-18 12:54:37 -08001839 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001840 return NO_ERROR;
1841}
1842
Sungtak Lee3249fb62019-03-02 16:40:47 -08001843status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1844 ATRACE_CALL();
1845 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1846
1847 std::lock_guard<std::mutex> lock(mCore->mMutex);
1848 mCore->mLegacyBufferDrop = drop;
1849 return NO_ERROR;
1850}
1851
Dan Stoza50101d02016-04-07 16:53:23 -07001852status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001853 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001854 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001855
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001856 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001857 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1858
Dan Stoza50101d02016-04-07 16:53:23 -07001859 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1860 *outBuffer = nullptr;
1861 *outFence = Fence::NO_FENCE;
1862 return NO_ERROR;
1863 }
1864
1865 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1866 *outFence = mLastQueueBufferFence;
1867
John Reck1a61da52016-04-28 13:18:15 -07001868 // Currently only SurfaceFlinger internally ever changes
1869 // GLConsumer's filtering mode, so we just use 'true' here as
1870 // this is slightly specialized for the current client of this API,
1871 // which does want filtering.
1872 GLConsumer::computeTransformMatrix(outTransformMatrix,
1873 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1874 mLastQueuedTransform, true /* filter */);
1875
Dan Stoza50101d02016-04-07 16:53:23 -07001876 return NO_ERROR;
1877}
1878
John Reckf0f13e82021-05-18 00:42:56 -04001879status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1880 Rect* outRect, uint32_t* outTransform) {
1881 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001882
1883 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001884 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1885 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1886 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001887 *outBuffer = nullptr;
1888 *outFence = Fence::NO_FENCE;
1889 return NO_ERROR;
1890 }
1891
1892 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1893 *outFence = mLastQueueBufferFence;
1894 *outRect = mLastQueuedCrop;
1895 *outTransform = mLastQueuedTransform;
1896
1897 return NO_ERROR;
1898}
1899
Brian Anderson3890c392016-07-25 12:48:08 -07001900void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1901 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001902}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001903
Brian Anderson3890c392016-07-25 12:48:08 -07001904void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001905 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001906 FrameEventHistoryDelta* outDelta) {
1907 if (newTimestamps == nullptr && outDelta == nullptr) {
1908 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001909 }
1910
1911 ATRACE_CALL();
1912 BQ_LOGV("addAndGetFrameTimestamps");
1913 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001914 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001915 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001916 listener = mCore->mConsumerListener;
1917 }
Yi Kong48a619f2018-06-05 16:34:59 -07001918 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001919 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001920 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001921}
1922
Dan Stoza289ade12014-02-28 11:17:17 -08001923void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1924 // If we're here, it means that a producer we were connected to died.
1925 // We're guaranteed that we are still connected to it because we remove
1926 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1927 // without synchronization here.
1928 int api = mCore->mConnectedApi;
1929 disconnect(api);
1930}
1931
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001932status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1933 BQ_LOGV("getUniqueId");
1934
1935 *outId = mCore->mUniqueId;
1936 return NO_ERROR;
1937}
1938
Chia-I Wue2786ea2017-08-07 10:36:08 -07001939status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1940 BQ_LOGV("getConsumerUsage");
1941
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001942 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001943 *outUsage = mCore->mConsumerUsageBits;
1944 return NO_ERROR;
1945}
1946
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001947status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1948 ATRACE_CALL();
1949 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1950
1951 std::lock_guard<std::mutex> lock(mCore->mMutex);
1952
1953 mCore->mAutoPrerotation = autoPrerotation;
1954 return NO_ERROR;
1955}
1956
Ady Abraham107788e2023-10-17 12:31:08 -07001957#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001958status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1959 int8_t changeFrameRateStrategy) {
1960 ATRACE_CALL();
1961 BQ_LOGV("setFrameRate: %.2f", frameRate);
1962
1963 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1964 "BufferQueueProducer::setFrameRate")) {
1965 return BAD_VALUE;
1966 }
1967
1968 sp<IConsumerListener> listener;
1969 {
1970 std::lock_guard<std::mutex> lock(mCore->mMutex);
1971 listener = mCore->mConsumerListener;
1972 }
1973 if (listener != nullptr) {
1974 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1975 }
1976 return NO_ERROR;
1977}
1978#endif
1979
John Reckdb164ff2024-04-03 16:59:28 -04001980#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1981status_t BufferQueueProducer::setAdditionalOptions(
1982 const std::vector<gui::AdditionalOptions>& options) {
1983 ATRACE_CALL();
1984 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1985
1986 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1987 return INVALID_OPERATION;
1988 }
1989
1990 std::lock_guard<std::mutex> lock(mCore->mMutex);
1991
1992 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1993 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1994 return NO_INIT;
1995 }
1996
1997 if (mCore->mAdditionalOptions != options) {
1998 mCore->mAdditionalOptions = options;
1999 mCore->mAdditionalOptionsGenerationId++;
2000 }
2001 return NO_ERROR;
2002}
2003#endif
2004
Dan Stoza289ade12014-02-28 11:17:17 -08002005} // namespace android