blob: 6600c2ca80b7f73e5ba6133a6bd5a6549a15224e [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
Ady Abraham107788e2023-10-17 12:31:08 -070035
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070036#include <gui/FrameRateUtils.h>
John Reck1a61da52016-04-28 13:18:15 -070037#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070039#include <gui/IProducerListener.h>
Vishnu Nair14a3c112023-04-21 14:49:47 -070040#include <gui/TraceUtils.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080041#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080042
43#include <utils/Log.h>
44#include <utils/Trace.h>
45
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070046#include <system/window.h>
47
Dan Stoza289ade12014-02-28 11:17:17 -080048namespace android {
49
Iris Chang430193f2019-12-04 16:25:46 +080050// Macros for include BufferQueueCore information in log messages
51#define BQ_LOGV(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000052 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080053 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
54 ##__VA_ARGS__)
55#define BQ_LOGD(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000056 ALOGD("[%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_LOGI(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000060 ALOGI("[%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_LOGW(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000064 ALOGW("[%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_LOGE(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000068 ALOGE("[%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
Craig Donner6ebc46a2016-10-21 15:23:44 -070072static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080073ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070074
Irvel468051e2016-06-13 16:44:44 -070075BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
76 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080077 mCore(core),
78 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070079 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070080 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070081 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080082 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070083 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080084 mCallbackMutex(),
85 mNextCallbackTicket(0),
86 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070087 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010088 mDequeueTimeout(-1),
89 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080090
91BufferQueueProducer::~BufferQueueProducer() {}
92
93status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
94 ATRACE_CALL();
95 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020096 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080097
98 if (mCore->mIsAbandoned) {
99 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
100 return NO_INIT;
101 }
102
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700103 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
104 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
105 return NO_INIT;
106 }
107
Dan Stoza3e96f192014-03-03 10:16:19 -0800108 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800109 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800110 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800111 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800113 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700114 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800115 return BAD_VALUE;
116 }
117
118 mSlots[slot].mRequestBufferCalled = true;
119 *buf = mSlots[slot].mGraphicBuffer;
120 return NO_ERROR;
121}
122
Pablo Ceballosfa455352015-08-12 17:47:47 -0700123status_t BufferQueueProducer::setMaxDequeuedBufferCount(
124 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700125 int maxBufferCount;
126 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
127}
128
129status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
130 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700131 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
133 maxDequeuedBuffers);
134
Pablo Ceballos981066c2016-02-18 12:54:37 -0800135 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700136 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200137 std::unique_lock<std::mutex> lock(mCore->mMutex);
138 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700139
140 if (mCore->mIsAbandoned) {
141 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
142 "abandoned");
143 return NO_INIT;
144 }
145
Brian Lindahlc794b692023-01-31 15:42:47 -0700146 *maxBufferCount = mCore->getMaxBufferCountLocked();
147
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700148 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
149 return NO_ERROR;
150 }
151
Pablo Ceballos72daab62015-12-07 16:38:43 -0800152 // The new maxDequeuedBuffer count should not be violated by the number
153 // of currently dequeued buffers
154 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800155 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700158 }
159 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800160 if (dequeuedCount > maxDequeuedBuffers) {
161 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
162 "count (%d) exceeds the current dequeued buffer count (%d)",
163 maxDequeuedBuffers, dequeuedCount);
164 return BAD_VALUE;
165 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166
Rajat Yadavd108d682021-08-31 18:58:23 +0800167 int minUndequedBufferCount = mCore->getMinUndequeuedBufferCountLocked();
168 int bufferCount = minUndequedBufferCount + maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700169
170 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
171 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
172 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Rajat Yadavd108d682021-08-31 18:58:23 +0800173 bufferCount = BufferQueueDefs::NUM_BUFFER_SLOTS;
174 maxDequeuedBuffers = bufferCount - minUndequedBufferCount;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700175 }
176
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700177 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700178 if (bufferCount < minBufferSlots) {
179 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
180 "less than minimum %d", bufferCount, minBufferSlots);
181 return BAD_VALUE;
182 }
183
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700184 if (bufferCount > mCore->mMaxBufferCount) {
185 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700186 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
187 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
188 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
189 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700190 return BAD_VALUE;
191 }
192
Pablo Ceballos72daab62015-12-07 16:38:43 -0800193 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800194 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195 return BAD_VALUE;
196 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700197 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700198 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800199 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800200 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800201 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800202 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200203 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700204 } // Autolock scope
205
206 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700207 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800208 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700209 }
210
211 return NO_ERROR;
212}
213
214status_t BufferQueueProducer::setAsyncMode(bool async) {
215 ATRACE_CALL();
216 BQ_LOGV("setAsyncMode: async = %d", async);
217
Pablo Ceballos981066c2016-02-18 12:54:37 -0800218 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700219 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200220 std::unique_lock<std::mutex> lock(mCore->mMutex);
221 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700222
223 if (mCore->mIsAbandoned) {
224 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
225 return NO_INIT;
226 }
227
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700228 if (async == mCore->mAsyncMode) {
229 return NO_ERROR;
230 }
231
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700232 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700233 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
234 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700235 BQ_LOGE("setAsyncMode(%d): this call would cause the "
236 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700237 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
238 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
239 mCore->mMaxDequeuedBufferCount,
240 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700241 return BAD_VALUE;
242 }
243
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800244 int delta = mCore->getMaxBufferCountLocked(async,
245 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
246 - mCore->getMaxBufferCountLocked();
247
Pablo Ceballos981066c2016-02-18 12:54:37 -0800248 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800249 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
250 "available slots. Delta = %d", delta);
251 return BAD_VALUE;
252 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700253 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800254 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200255 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700256 if (delta < 0) {
257 listener = mCore->mConsumerListener;
258 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700259 } // Autolock scope
260
261 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700262 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800263 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700264 }
265 return NO_ERROR;
266}
267
Dan Stoza5ecfb682016-01-04 17:01:02 -0800268int BufferQueueProducer::getFreeBufferLocked() const {
269 if (mCore->mFreeBuffers.empty()) {
270 return BufferQueueCore::INVALID_BUFFER_SLOT;
271 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800272 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800273 mCore->mFreeBuffers.pop_front();
274 return slot;
275}
276
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800277int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800278 if (mCore->mFreeSlots.empty()) {
279 return BufferQueueCore::INVALID_BUFFER_SLOT;
280 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800281 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800282 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800283 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284}
285
286status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200287 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800288 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
289 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800290 bool tryAgain = true;
291 while (tryAgain) {
292 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800293 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800294 return NO_INIT;
295 }
296
Dan Stoza9f3053d2014-03-06 15:14:33 -0800297 int dequeuedCount = 0;
298 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800299 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700300 if (mSlots[s].mBufferState.isDequeued()) {
301 ++dequeuedCount;
302 }
303 if (mSlots[s].mBufferState.isAcquired()) {
304 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800305 }
306 }
307
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700308 // Producers are not allowed to dequeue more than
309 // mMaxDequeuedBufferCount buffers.
310 // This check is only done if a buffer has already been queued
311 if (mCore->mBufferHasBeenQueued &&
312 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700313 // Supress error logs when timeout is non-negative.
314 if (mDequeueTimeout < 0) {
315 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
316 "count (%d)", callerString,
317 mCore->mMaxDequeuedBufferCount);
318 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800319 return INVALID_OPERATION;
320 }
321
Dan Stoza0de7ea72015-04-23 13:20:51 -0700322 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
323
Dan Stozaae3c3682014-04-18 15:43:35 -0700324 // If we disconnect and reconnect quickly, we can be in a state where
325 // our slots are empty but we have many buffers in the queue. This can
326 // cause us to run out of memory if we outrun the consumer. Wait here if
327 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800328 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700329 bool tooManyBuffers = mCore->mQueue.size()
330 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700331 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800332 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700333 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700334 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700335 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700336 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700337 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700338 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700339 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800340 } else {
341 if (caller == FreeSlotCaller::Dequeue) {
342 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800343 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800344 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
345 *found = slot;
346 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800347 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800348 }
349 } else {
350 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800351 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800352 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
353 *found = slot;
354 } else {
355 *found = getFreeBufferLocked();
356 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700357 }
358 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700359 }
360
361 // If no buffer is found, or if the queue has too many buffers
362 // outstanding, wait for a buffer to be acquired or released, or for the
363 // max buffer count to change.
364 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
365 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800366 if (tryAgain) {
367 // Return an error if we're in non-blocking mode (producer and
368 // consumer are controlled by the application).
369 // However, the consumer is allowed to briefly acquire an extra
370 // buffer (which could cause us to have to wait here), which is
371 // okay, since it is only used to implement an atomic acquire +
372 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700373 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800374 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
375 return WOULD_BLOCK;
376 }
Dan Stoza127fc632015-06-30 13:43:32 -0700377 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200378 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
379 std::chrono::nanoseconds(mDequeueTimeout));
380 if (result == std::cv_status::timeout) {
381 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700382 }
383 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200384 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700385 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 }
387 } // while (tryAgain)
388
389 return NO_ERROR;
390}
391
Ian Elliottd11b0442017-07-18 11:05:49 -0600392status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
393 uint32_t width, uint32_t height, PixelFormat format,
394 uint64_t usage, uint64_t* outBufferAge,
395 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800396 ATRACE_CALL();
397 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200398 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800399 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700400
401 if (mCore->mIsAbandoned) {
402 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
403 return NO_INIT;
404 }
405
406 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
407 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
408 return NO_INIT;
409 }
Dan Stoza289ade12014-02-28 11:17:17 -0800410 } // Autolock scope
411
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700412 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800413
414 if ((width && !height) || (!width && height)) {
415 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
416 return BAD_VALUE;
417 }
418
419 status_t returnFlags = NO_ERROR;
420 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
421 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800422 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800423
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000424 sp<IConsumerListener> listener;
425 bool callOnFrameDequeued = false;
426 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
John Reckdb164ff2024-04-03 16:59:28 -0400427#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
428 std::vector<gui::AdditionalOptions> allocOptions;
429 uint32_t allocOptionsGenId = 0;
430#endif
431
Dan Stoza289ade12014-02-28 11:17:17 -0800432 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200433 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800434
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100435 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
436 // is finished such that we don't allocate in parallel.
437 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
438 mDequeueWaitingForAllocation = true;
439 mCore->waitWhileAllocatingLocked(lock);
440 mDequeueWaitingForAllocation = false;
441 mDequeueWaitingForAllocationCondition.notify_all();
442 }
Dan Stoza289ade12014-02-28 11:17:17 -0800443
444 if (format == 0) {
445 format = mCore->mDefaultBufferFormat;
446 }
447
448 // Enable the usage bits the consumer requested
449 usage |= mCore->mConsumerUsageBits;
450
Dan Stoza9de72932015-04-16 17:28:43 -0700451 const bool useDefaultSize = !width && !height;
452 if (useDefaultSize) {
453 width = mCore->mDefaultWidth;
454 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700455 if (mCore->mAutoPrerotation &&
456 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
457 std::swap(width, height);
458 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800459 }
Dan Stoza289ade12014-02-28 11:17:17 -0800460
Pablo Ceballos981066c2016-02-18 12:54:37 -0800461 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700462 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200463 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700464 if (status != NO_ERROR) {
465 return status;
466 }
467
468 // This should not happen
469 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
470 BQ_LOGE("dequeueBuffer: no available buffer slots");
471 return -EBUSY;
472 }
473
474 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
475
476 // If we are not allowed to allocate new buffers,
477 // waitForFreeSlotThenRelock must have returned a slot containing a
478 // buffer. If this buffer would require reallocation to meet the
479 // requested attributes, we free it and attempt to get another one.
480 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700481 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700482 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700483 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700484 return BAD_VALUE;
485 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800486 mCore->mFreeSlots.insert(found);
487 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700488 found = BufferItem::INVALID_BUFFER_SLOT;
489 continue;
490 }
491 }
Dan Stoza289ade12014-02-28 11:17:17 -0800492 }
493
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800494 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800495
John Reckdb164ff2024-04-03 16:59:28 -0400496 bool needsReallocation = buffer == nullptr ||
497 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
498
499#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
500 needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
501 mCore->mAdditionalOptionsGenerationId;
502#endif
503
504 if (mCore->mSharedBufferSlot == found && needsReallocation) {
505 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800506 return BAD_VALUE;
507 }
508
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700509 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800510 mCore->mActiveBuffers.insert(found);
511 }
Dan Stoza289ade12014-02-28 11:17:17 -0800512 *outSlot = found;
513 ATRACE_BUFFER_INDEX(found);
514
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800515 attachedByConsumer = mSlots[found].mNeedsReallocation;
516 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800517
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700518 mSlots[found].mBufferState.dequeue();
519
John Reckdb164ff2024-04-03 16:59:28 -0400520 if (needsReallocation) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700521 if (CC_UNLIKELY(ATRACE_ENABLED())) {
522 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000523 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700524 } else {
525 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
526 "layerCount:%d "
527 "usage:%d requested: %dx%d format:%d layerCount:%d "
528 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000529 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700530 BQ_LAYER_COUNT, usage, buffer->getWidth(),
531 buffer->getHeight(), buffer->getPixelFormat(),
532 buffer->getLayerCount(), buffer->getUsage());
533 }
534 }
Dan Stoza289ade12014-02-28 11:17:17 -0800535 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700536 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800537 mSlots[found].mRequestBufferCalled = false;
538 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
539 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
540 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800541 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700542 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -0400543#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
544 allocOptions = mCore->mAdditionalOptions;
545 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
546#endif
Dan Stoza289ade12014-02-28 11:17:17 -0800547
548 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800549 } else {
550 // We add 1 because that will be the frame number when this buffer
551 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700552 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800553 }
554
Dan Stoza800b41a2015-04-28 14:20:04 -0700555 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
556 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800557
Yi Kong48a619f2018-06-05 16:34:59 -0700558 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800559 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
560 "slot=%d w=%d h=%d format=%u",
561 found, buffer->width, buffer->height, buffer->format);
562 }
563
564 eglDisplay = mSlots[found].mEglDisplay;
565 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700566 // Don't return a fence in shared buffer mode, except for the first
567 // frame.
568 *outFence = (mCore->mSharedBufferMode &&
569 mCore->mSharedBufferSlot == found) ?
570 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800571 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
572 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700573
574 // If shared buffer mode has just been enabled, cache the slot of the
575 // first buffer that is dequeued and mark it as the shared buffer.
576 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
577 BufferQueueCore::INVALID_BUFFER_SLOT) {
578 mCore->mSharedBufferSlot = found;
579 mSlots[found].mBufferState.mShared = true;
580 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700581
582 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000583 callOnFrameDequeued = true;
584 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700585 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000586
587 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800588 } // Autolock scope
589
590 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700591 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
John Reckdb164ff2024-04-03 16:59:28 -0400592
593#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
594 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
595 tempOptions.reserve(allocOptions.size());
596 for (const auto& it : allocOptions) {
597 tempOptions.emplace_back(it.name.c_str(), it.value);
598 }
599 const GraphicBufferAllocator::AllocationRequest allocRequest = {
600 .importBuffer = true,
601 .width = width,
602 .height = height,
603 .format = format,
604 .layerCount = BQ_LAYER_COUNT,
605 .usage = usage,
606 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
607 .extras = std::move(tempOptions),
608 };
609 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
610#else
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000611 sp<GraphicBuffer> graphicBuffer =
612 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
613 {mConsumerName.c_str(), mConsumerName.size()});
John Reckdb164ff2024-04-03 16:59:28 -0400614#endif
Mathias Agopian0556d792017-03-22 15:49:32 -0700615
616 status_t error = graphicBuffer->initCheck();
617
Dan Stoza289ade12014-02-28 11:17:17 -0800618 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200619 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800620
Chia-I Wufeec3b12017-05-25 09:34:56 -0700621 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700622 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
623 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
John Reckdb164ff2024-04-03 16:59:28 -0400624#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
625 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
626#endif
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000627 callOnFrameDequeued = true;
628 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700629 }
630
631 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200632 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700633
Chia-I Wufeec3b12017-05-25 09:34:56 -0700634 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700635 mCore->mFreeSlots.insert(*outSlot);
636 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700637 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
638 return error;
639 }
640
Dan Stoza289ade12014-02-28 11:17:17 -0800641 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700642 mCore->mFreeSlots.insert(*outSlot);
643 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800644 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
645 return NO_INIT;
646 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800647
Pablo Ceballos9e314332016-01-12 13:49:19 -0800648 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800649 } // Autolock scope
650 }
651
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000652 if (listener != nullptr && callOnFrameDequeued) {
653 listener->onFrameDequeued(bufferId);
654 }
655
Dan Stoza9f3053d2014-03-06 15:14:33 -0800656 if (attachedByConsumer) {
657 returnFlags |= BUFFER_NEEDS_REALLOCATION;
658 }
659
Dan Stoza289ade12014-02-28 11:17:17 -0800660 if (eglFence != EGL_NO_SYNC_KHR) {
661 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
662 1000000000);
663 // If something goes wrong, log the error, but return the buffer without
664 // synchronizing access to it. It's too late at this point to abort the
665 // dequeue operation.
666 if (result == EGL_FALSE) {
667 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
668 eglGetError());
669 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
670 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
671 }
672 eglDestroySyncKHR(eglDisplay, eglFence);
673 }
674
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700675 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
676 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800677 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800678 mSlots[*outSlot].mGraphicBuffer != nullptr ?
679 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800680
Ian Elliottd11b0442017-07-18 11:05:49 -0600681 if (outBufferAge) {
682 *outBufferAge = mCore->mBufferAge;
683 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700684 addAndGetFrameTimestamps(nullptr, outTimestamps);
685
Dan Stoza289ade12014-02-28 11:17:17 -0800686 return returnFlags;
687}
688
Dan Stoza9f3053d2014-03-06 15:14:33 -0800689status_t BufferQueueProducer::detachBuffer(int slot) {
690 ATRACE_CALL();
691 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700692 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800693
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700694 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000695 bool callOnFrameDetached = false;
696 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700697 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200698 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700699
700 if (mCore->mIsAbandoned) {
701 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
702 return NO_INIT;
703 }
704
705 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
706 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
707 return NO_INIT;
708 }
709
710 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
711 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
712 return BAD_VALUE;
713 }
714
715 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
716 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
717 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
718 return BAD_VALUE;
719 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000720 // TODO(http://b/140581935): This message is BQ_LOGW because it
721 // often logs when no actionable errors are present. Return to
722 // using BQ_LOGE after ensuring this only logs during errors.
723 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700724 "(state = %s)", slot, mSlots[slot].mBufferState.string());
725 return BAD_VALUE;
726 } else if (!mSlots[slot].mRequestBufferCalled) {
727 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
728 slot);
729 return BAD_VALUE;
730 }
731
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700732 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700733 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000734 if (gb != nullptr) {
735 callOnFrameDetached = true;
736 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700737 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700738 mSlots[slot].mBufferState.detachProducer();
739 mCore->mActiveBuffers.erase(slot);
740 mCore->mFreeSlots.insert(slot);
741 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200742 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700743 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800744 }
745
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000746 if (listener != nullptr && callOnFrameDetached) {
747 listener->onFrameDetached(bufferId);
748 }
749
Yi Kong48a619f2018-06-05 16:34:59 -0700750 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700751 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700752 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800753
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 return NO_ERROR;
755}
756
Dan Stozad9822a32014-03-28 15:25:31 -0700757status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
758 sp<Fence>* outFence) {
759 ATRACE_CALL();
760
Yi Kong48a619f2018-06-05 16:34:59 -0700761 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700762 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
763 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700764 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700765 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
766 return BAD_VALUE;
767 }
768
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700769 sp<IConsumerListener> listener;
770 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200771 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700772
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700773 if (mCore->mIsAbandoned) {
774 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
775 return NO_INIT;
776 }
777
778 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
779 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
780 return NO_INIT;
781 }
782
783 if (mCore->mSharedBufferMode) {
784 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
785 "mode");
786 return BAD_VALUE;
787 }
788
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200789 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700790
791 if (mCore->mFreeBuffers.empty()) {
792 return NO_MEMORY;
793 }
794
795 int found = mCore->mFreeBuffers.front();
796 mCore->mFreeBuffers.remove(found);
797 mCore->mFreeSlots.insert(found);
798
799 BQ_LOGV("detachNextBuffer detached slot %d", found);
800
801 *outBuffer = mSlots[found].mGraphicBuffer;
802 *outFence = mSlots[found].mFence;
803 mCore->clearBufferSlotLocked(found);
804 VALIDATE_CONSISTENCY();
805 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700806 }
807
Yi Kong48a619f2018-06-05 16:34:59 -0700808 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700809 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700810 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800811
Dan Stozad9822a32014-03-28 15:25:31 -0700812 return NO_ERROR;
813}
814
Dan Stoza9f3053d2014-03-06 15:14:33 -0800815status_t BufferQueueProducer::attachBuffer(int* outSlot,
816 const sp<android::GraphicBuffer>& buffer) {
817 ATRACE_CALL();
818
Yi Kong48a619f2018-06-05 16:34:59 -0700819 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700820 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800821 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700822 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700823 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800824 return BAD_VALUE;
825 }
826
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200827 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700828
829 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700830 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700831 return NO_INIT;
832 }
833
834 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700835 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700836 return NO_INIT;
837 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800838
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700839 if (mCore->mSharedBufferMode) {
840 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700841 return BAD_VALUE;
842 }
843
Dan Stoza812ed062015-06-02 15:45:22 -0700844 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
845 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
846 "[queue %u]", buffer->getGenerationNumber(),
847 mCore->mGenerationNumber);
848 return BAD_VALUE;
849 }
850
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200851 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700852
Dan Stoza9f3053d2014-03-06 15:14:33 -0800853 status_t returnFlags = NO_ERROR;
854 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200855 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800856 if (status != NO_ERROR) {
857 return status;
858 }
859
860 // This should not happen
861 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800863 return -EBUSY;
864 }
865
866 *outSlot = found;
867 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700868 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800869 *outSlot, returnFlags);
870
871 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700872 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800873 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
874 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700875 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800876 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800877 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800878 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800879 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700880
Dan Stoza9f3053d2014-03-06 15:14:33 -0800881 return returnFlags;
882}
883
Dan Stoza289ade12014-02-28 11:17:17 -0800884status_t BufferQueueProducer::queueBuffer(int slot,
885 const QueueBufferInput &input, QueueBufferOutput *output) {
886 ATRACE_CALL();
887 ATRACE_BUFFER_INDEX(slot);
888
Brian Andersond6927fb2016-07-23 23:37:30 -0700889 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800890 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800891 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700892 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800893 int scalingMode;
894 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700895 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700896 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700897 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700898 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700899 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
900 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700901 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700902 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800903
Yi Kong48a619f2018-06-05 16:34:59 -0700904 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800905 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800906 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800907 }
908
Brian Anderson3d4039d2016-09-23 16:31:30 -0700909 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
910
Dan Stoza289ade12014-02-28 11:17:17 -0800911 switch (scalingMode) {
912 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
913 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
914 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
915 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
916 break;
917 default:
918 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800919 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800920 }
921
Dan Stoza8dc55392014-11-04 11:37:46 -0800922 sp<IConsumerListener> frameAvailableListener;
923 sp<IConsumerListener> frameReplacedListener;
924 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700925 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800926 BufferItem item;
John Reckd2c4a4a2024-02-07 10:31:09 -0500927 int connectedApi;
928 sp<Fence> lastQueuedFence;
929
Dan Stoza289ade12014-02-28 11:17:17 -0800930 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200931 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800932
933 if (mCore->mIsAbandoned) {
934 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
935 return NO_INIT;
936 }
937
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700938 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
939 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
940 return NO_INIT;
941 }
942
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800943 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800944 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800945 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800946 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700947 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800948 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700949 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800950 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800951 } else if (!mSlots[slot].mRequestBufferCalled) {
952 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
953 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800954 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800955 }
956
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700957 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700958 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700959 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700960 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700961 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700962 mSlots[slot].mBufferState.mShared = true;
963 }
964
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800965 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700966 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
967 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
968 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700969 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800970 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800971
972 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
973 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700974 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800975 crop.intersect(bufferRect, &croppedRect);
976 if (croppedRect != crop) {
977 BQ_LOGE("queueBuffer: crop rect is not contained within the "
978 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800979 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800980 }
981
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800982 // Override UNKNOWN dataspace with consumer default
983 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
984 dataSpace = mCore->mDefaultBufferDataSpace;
985 }
986
Brian Andersond6927fb2016-07-23 23:37:30 -0700987 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 mSlots[slot].mBufferState.queue();
989
Brian Andersond6927fb2016-07-23 23:37:30 -0700990 // Increment the frame counter and store a local version of it
991 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800992 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700993 currentFrameNumber = mCore->mFrameCounter;
994 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800995
Dan Stoza289ade12014-02-28 11:17:17 -0800996 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
997 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
998 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800999 item.mTransform = transform &
1000 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -08001001 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001002 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1003 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -07001004 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -08001005 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001006 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001007 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -07001008 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -08001009 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -07001010 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -07001011 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001012 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -07001013 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -08001014 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001015 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -07001016 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -07001017 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001018 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -08001019 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -08001020
Ruben Brunk1681d952014-06-27 15:51:55 -07001021 mStickyTransform = stickyTransform;
1022
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001023 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001024 if (mCore->mSharedBufferMode) {
1025 mCore->mSharedBufferCache.crop = crop;
1026 mCore->mSharedBufferCache.transform = transform;
1027 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001028 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001029 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001030 }
1031
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001032 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001033 if (mCore->mQueue.empty()) {
1034 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1035 // and simply queue this buffer
1036 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001037 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001038 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001039 // When the queue is not empty, we need to look at the last buffer
1040 // in the queue to see if we need to replace it
1041 const BufferItem& last = mCore->mQueue.itemAt(
1042 mCore->mQueue.size() - 1);
1043 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001044
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001045 if (!last.mIsStale) {
1046 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001047
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001048 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001049 // still be around. Mark it as no longer shared if this
1050 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001051 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001052 mSlots[last.mSlot].mBufferState.isFree()) {
1053 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001054 }
1055 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001056 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1057 mCore->mActiveBuffers.erase(last.mSlot);
1058 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001059 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001060 }
Dan Stoza289ade12014-02-28 11:17:17 -08001061 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001062
Steven Thomas862e7532019-07-10 19:21:03 -07001063 // Make sure to merge the damage rect from the frame we're about
1064 // to drop into the new frame's damage rect.
1065 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1066 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1067 item.mSurfaceDamage = Region::INVALID_REGION;
1068 } else {
1069 item.mSurfaceDamage |= last.mSurfaceDamage;
1070 }
1071
Dan Stoza289ade12014-02-28 11:17:17 -08001072 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001073 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001074 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001075 } else {
1076 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001077 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001078 }
1079 }
1080
1081 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001082 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001083 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001084
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001085 output->width = mCore->mDefaultWidth;
1086 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001087 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001088 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1089 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001090
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001091 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001092#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001093 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001094#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001095 // Take a ticket for the callback functions
1096 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001097
Pablo Ceballos9e314332016-01-12 13:49:19 -08001098 VALIDATE_CONSISTENCY();
John Reckd2c4a4a2024-02-07 10:31:09 -05001099
1100 connectedApi = mCore->mConnectedApi;
1101 lastQueuedFence = std::move(mLastQueueBufferFence);
1102
1103 mLastQueueBufferFence = std::move(acquireFence);
1104 mLastQueuedCrop = item.mCrop;
1105 mLastQueuedTransform = item.mTransform;
Dan Stoza289ade12014-02-28 11:17:17 -08001106 } // Autolock scope
1107
Irvel468051e2016-06-13 16:44:44 -07001108 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1109 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1110 // there will be no Binder call
1111 if (!mConsumerIsSurfaceFlinger) {
1112 item.mGraphicBuffer.clear();
1113 }
1114
JihCheng Chiu544c5222019-04-02 20:50:58 +08001115 // Update and get FrameEventHistory.
1116 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1117 NewFrameEventsEntry newFrameEventsEntry = {
1118 currentFrameNumber,
1119 postedTime,
1120 requestedPresentTimestamp,
1121 std::move(acquireFenceTime)
1122 };
1123 addAndGetFrameTimestamps(&newFrameEventsEntry,
1124 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1125
Dan Stoza8dc55392014-11-04 11:37:46 -08001126 // Call back without the main BufferQueue lock held, but with the callback
1127 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001128
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001129 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001130 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001131 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001132 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001133 }
1134
Yi Kong48a619f2018-06-05 16:34:59 -07001135 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001136 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001137 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001138 frameReplacedListener->onFrameReplaced(item);
1139 }
1140
1141 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001142 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001143 }
1144
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001145 // Wait without lock held
1146 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1147 // Waiting here allows for two full buffers to be queued but not a
1148 // third. In the event that frames take varying time, this makes a
1149 // small trade-off in favor of latency rather than throughput.
1150 lastQueuedFence->waitForever("Throttling EGL Production");
1151 }
1152
Dan Stoza289ade12014-02-28 11:17:17 -08001153 return NO_ERROR;
1154}
1155
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001156status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001157 ATRACE_CALL();
1158 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001159
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001160 sp<IConsumerListener> listener;
1161 bool callOnFrameCancelled = false;
1162 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1163 {
1164 std::lock_guard<std::mutex> lock(mCore->mMutex);
1165
1166 if (mCore->mIsAbandoned) {
1167 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1168 return NO_INIT;
1169 }
1170
1171 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1172 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1173 return NO_INIT;
1174 }
1175
1176 if (mCore->mSharedBufferMode) {
1177 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1178 return BAD_VALUE;
1179 }
1180
1181 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1182 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1183 BufferQueueDefs::NUM_BUFFER_SLOTS);
1184 return BAD_VALUE;
1185 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1186 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1187 "(state = %s)",
1188 slot, mSlots[slot].mBufferState.string());
1189 return BAD_VALUE;
1190 } else if (fence == nullptr) {
1191 BQ_LOGE("cancelBuffer: fence is NULL");
1192 return BAD_VALUE;
1193 }
1194
1195 mSlots[slot].mBufferState.cancel();
1196
1197 // After leaving shared buffer mode, the shared buffer will still be around.
1198 // Mark it as no longer shared if this operation causes it to be free.
1199 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1200 mSlots[slot].mBufferState.mShared = false;
1201 }
1202
1203 // Don't put the shared buffer on the free list.
1204 if (!mSlots[slot].mBufferState.isShared()) {
1205 mCore->mActiveBuffers.erase(slot);
1206 mCore->mFreeBuffers.push_back(slot);
1207 }
1208
1209 auto gb = mSlots[slot].mGraphicBuffer;
1210 if (gb != nullptr) {
1211 callOnFrameCancelled = true;
1212 bufferId = gb->getId();
1213 }
1214 mSlots[slot].mFence = fence;
1215 mCore->mDequeueCondition.notify_all();
1216 listener = mCore->mConsumerListener;
1217 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001218 }
1219
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001220 if (listener != nullptr && callOnFrameCancelled) {
1221 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001222 }
1223
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001224 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001225}
1226
1227int BufferQueueProducer::query(int what, int *outValue) {
1228 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001229 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001230
Yi Kong48a619f2018-06-05 16:34:59 -07001231 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001232 BQ_LOGE("query: outValue was NULL");
1233 return BAD_VALUE;
1234 }
1235
1236 if (mCore->mIsAbandoned) {
1237 BQ_LOGE("query: BufferQueue has been abandoned");
1238 return NO_INIT;
1239 }
1240
1241 int value;
1242 switch (what) {
1243 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001244 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001245 break;
1246 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001247 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001248 break;
1249 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001250 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001251 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001252 case NATIVE_WINDOW_LAYER_COUNT:
1253 // All BufferQueue buffers have a single layer.
1254 value = BQ_LAYER_COUNT;
1255 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001256 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001257 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001258 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001259 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001260 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001261 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001262 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1263 value = (mCore->mQueue.size() > 1);
1264 break;
1265 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001266 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001267 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001268 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001269 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1270 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1271 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001272 case NATIVE_WINDOW_BUFFER_AGE:
1273 if (mCore->mBufferAge > INT32_MAX) {
1274 value = 0;
1275 } else {
1276 value = static_cast<int32_t>(mCore->mBufferAge);
1277 }
1278 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001279 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1280 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1281 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001282 default:
1283 return BAD_VALUE;
1284 }
1285
1286 BQ_LOGV("query: %d? %d", what, value);
1287 *outValue = value;
1288 return NO_ERROR;
1289}
1290
Dan Stozaf0eaf252014-03-21 13:05:51 -07001291status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001292 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1293 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001294 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001295 mConsumerName = mCore->mConsumerName;
1296 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1297 producerControlledByApp ? "true" : "false");
1298
1299 if (mCore->mIsAbandoned) {
1300 BQ_LOGE("connect: BufferQueue has been abandoned");
1301 return NO_INIT;
1302 }
1303
Yi Kong48a619f2018-06-05 16:34:59 -07001304 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001305 BQ_LOGE("connect: BufferQueue has no consumer");
1306 return NO_INIT;
1307 }
1308
Yi Kong48a619f2018-06-05 16:34:59 -07001309 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001310 BQ_LOGE("connect: output was NULL");
1311 return BAD_VALUE;
1312 }
1313
1314 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1315 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1316 mCore->mConnectedApi, api);
1317 return BAD_VALUE;
1318 }
1319
1320 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1321 mDequeueTimeout < 0 ?
1322 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1323 mCore->mMaxBufferCount) -
1324 mCore->getMaxBufferCountLocked();
1325 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1326 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1327 "slots. Delta = %d", delta);
1328 return BAD_VALUE;
1329 }
1330
Dan Stoza289ade12014-02-28 11:17:17 -08001331 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001332 switch (api) {
1333 case NATIVE_WINDOW_API_EGL:
1334 case NATIVE_WINDOW_API_CPU:
1335 case NATIVE_WINDOW_API_MEDIA:
1336 case NATIVE_WINDOW_API_CAMERA:
1337 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001338
1339 output->width = mCore->mDefaultWidth;
1340 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001341 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001342 output->numPendingBuffers =
1343 static_cast<uint32_t>(mCore->mQueue.size());
1344 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001345 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001346 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001347
Yi Kong48a619f2018-06-05 16:34:59 -07001348 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001349 // Set up a death notification so that we can disconnect
1350 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001351#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001352 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001353 status = IInterface::asBinder(listener)->linkToDeath(
1354 static_cast<IBinder::DeathRecipient*>(this));
1355 if (status != NO_ERROR) {
1356 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1357 strerror(-status), status);
1358 }
1359 mCore->mLinkedToDeath = listener;
1360 }
Chong Zhang62493092020-01-15 16:04:47 -08001361#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001362 mCore->mConnectedProducerListener = listener;
1363 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001364 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001365 break;
1366 default:
1367 BQ_LOGE("connect: unknown API %d", api);
1368 status = BAD_VALUE;
1369 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001370 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001371 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001372 mCore->mBufferHasBeenQueued = false;
1373 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001374 mCore->mQueueBufferCanDrop = false;
1375 mCore->mLegacyBufferDrop = true;
1376 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1377 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1378 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001379 }
Dan Stoza289ade12014-02-28 11:17:17 -08001380
Pablo Ceballos981066c2016-02-18 12:54:37 -08001381 mCore->mAllowAllocation = true;
John Reckdb164ff2024-04-03 16:59:28 -04001382#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1383 mCore->mAdditionalOptions.clear();
1384#endif
Pablo Ceballos981066c2016-02-18 12:54:37 -08001385 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001386 return status;
1387}
1388
Robert Carr97b9c862016-09-08 13:54:35 -07001389status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001390 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001391 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001392
1393 int status = NO_ERROR;
1394 sp<IConsumerListener> listener;
1395 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001396 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001397
1398 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001399 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001400 return NO_ERROR;
1401 }
1402 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1403 }
1404
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001405 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001406
1407 if (mCore->mIsAbandoned) {
1408 // It's not really an error to disconnect after the surface has
1409 // been abandoned; it should just be a no-op.
1410 return NO_ERROR;
1411 }
1412
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001413 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001414 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1415 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1416 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001417 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001418 // If we're asked to disconnect the currently connected api but
1419 // nobody is connected, it's not really an error.
1420 if (api == BufferQueueCore::NO_CONNECTED_API) {
1421 return NO_ERROR;
1422 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001423 }
1424
Dan Stoza289ade12014-02-28 11:17:17 -08001425 switch (api) {
1426 case NATIVE_WINDOW_API_EGL:
1427 case NATIVE_WINDOW_API_CPU:
1428 case NATIVE_WINDOW_API_MEDIA:
1429 case NATIVE_WINDOW_API_CAMERA:
1430 if (mCore->mConnectedApi == api) {
1431 mCore->freeAllBuffersLocked();
1432
Chong Zhang62493092020-01-15 16:04:47 -08001433#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001434 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001435 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001436 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001437 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001438 // This can fail if we're here because of the death
1439 // notification, but we just ignore it
1440 token->unlinkToDeath(
1441 static_cast<IBinder::DeathRecipient*>(this));
1442 }
Chong Zhang62493092020-01-15 16:04:47 -08001443#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001444 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001445 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001446 mCore->mLinkedToDeath = nullptr;
1447 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001448 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001449 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001450 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001451 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001452 mCore->mAutoPrerotation = false;
John Reckdb164ff2024-04-03 16:59:28 -04001453#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1454 mCore->mAdditionalOptions.clear();
1455#endif
Dan Stoza289ade12014-02-28 11:17:17 -08001456 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001457 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1458 BQ_LOGE("disconnect: not connected (req=%d)", api);
1459 status = NO_INIT;
1460 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001461 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001462 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001463 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001464 }
1465 break;
1466 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001467 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001468 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001469 break;
1470 }
1471 } // Autolock scope
1472
1473 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001474 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001475 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001476 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001477 }
1478
1479 return status;
1480}
1481
Jesse Hall399184a2014-03-03 15:42:54 -08001482status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001483 sp<IConsumerListener> listener;
1484 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001485 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001486 mCore->mSidebandStream = stream;
1487 listener = mCore->mConsumerListener;
1488 } // Autolock scope
1489
Yi Kong48a619f2018-06-05 16:34:59 -07001490 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001491 listener->onSidebandStreamChanged();
1492 }
Jesse Hall399184a2014-03-03 15:42:54 -08001493 return NO_ERROR;
1494}
1495
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001496void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001497 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001498 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001499
1500 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001501 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001502 size_t newBufferCount = 0;
1503 uint32_t allocWidth = 0;
1504 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001505 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001506 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001507 std::string allocName;
John Reckdb164ff2024-04-03 16:59:28 -04001508#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1509 std::vector<gui::AdditionalOptions> allocOptions;
1510 uint32_t allocOptionsGenId = 0;
1511#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001512 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001513 std::unique_lock<std::mutex> lock(mCore->mMutex);
1514 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001515
Dan Stoza9de72932015-04-16 17:28:43 -07001516 if (!mCore->mAllowAllocation) {
1517 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1518 "BufferQueue");
1519 return;
1520 }
1521
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001522 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1523 // both allocateBuffers and dequeueBuffer.
1524 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001525 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001526 return;
1527 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001528
Antoine Labour78014f32014-07-15 21:17:03 -07001529 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1530 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001531 if (useDefaultSize && mCore->mAutoPrerotation &&
1532 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1533 std::swap(allocWidth, allocHeight);
1534 }
1535
Antoine Labour78014f32014-07-15 21:17:03 -07001536 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1537 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001538 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001539
John Reckdb164ff2024-04-03 16:59:28 -04001540#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1541 allocOptions = mCore->mAdditionalOptions;
1542 allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1543#endif
1544
Antoine Labour78014f32014-07-15 21:17:03 -07001545 mCore->mIsAllocating = true;
John Reckdb164ff2024-04-03 16:59:28 -04001546
Antoine Labour78014f32014-07-15 21:17:03 -07001547 } // Autolock scope
1548
John Reckdb164ff2024-04-03 16:59:28 -04001549#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1550 std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1551 tempOptions.reserve(allocOptions.size());
1552 for (const auto& it : allocOptions) {
1553 tempOptions.emplace_back(it.name.c_str(), it.value);
1554 }
1555 const GraphicBufferAllocator::AllocationRequest allocRequest = {
1556 .importBuffer = true,
1557 .width = allocWidth,
1558 .height = allocHeight,
1559 .format = allocFormat,
1560 .layerCount = BQ_LAYER_COUNT,
1561 .usage = allocUsage,
1562 .requestorName = allocName,
1563 .extras = std::move(tempOptions),
1564 };
1565#endif
1566
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001567 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001568 for (size_t i = 0; i < newBufferCount; ++i) {
John Reckdb164ff2024-04-03 16:59:28 -04001569#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1570 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest);
1571#else
Mathias Agopian0556d792017-03-22 15:49:32 -07001572 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001573 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001574 allocUsage, allocName);
John Reckdb164ff2024-04-03 16:59:28 -04001575#endif
Mathias Agopian0556d792017-03-22 15:49:32 -07001576
1577 status_t result = graphicBuffer->initCheck();
1578
Antoine Labour78014f32014-07-15 21:17:03 -07001579 if (result != NO_ERROR) {
1580 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001581 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001582 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001583 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001584 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001585 return;
1586 }
1587 buffers.push_back(graphicBuffer);
1588 }
1589
1590 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001591 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001592 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1593 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001594 if (useDefaultSize && mCore->mAutoPrerotation &&
1595 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1596 std::swap(checkWidth, checkHeight);
1597 }
1598
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001599 PixelFormat checkFormat = format != 0 ?
1600 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001601 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
John Reckdb164ff2024-04-03 16:59:28 -04001602 bool allocOptionsChanged = false;
1603#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1604 allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1605#endif
Antoine Labour78014f32014-07-15 21:17:03 -07001606 if (checkWidth != allocWidth || checkHeight != allocHeight ||
John Reckdb164ff2024-04-03 16:59:28 -04001607 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
Antoine Labour78014f32014-07-15 21:17:03 -07001608 // Something changed while we released the lock. Retry.
1609 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1610 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001611 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001612 continue;
1613 }
1614
Antoine Labour78014f32014-07-15 21:17:03 -07001615 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001616 if (mCore->mFreeSlots.empty()) {
1617 BQ_LOGV("allocateBuffers: a slot was occupied while "
1618 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001619 continue;
1620 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001621 auto slot = mCore->mFreeSlots.begin();
1622 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1623 mSlots[*slot].mGraphicBuffer = buffers[i];
1624 mSlots[*slot].mFence = Fence::NO_FENCE;
John Reckdb164ff2024-04-03 16:59:28 -04001625#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1626 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1627#endif
Dan Stoza0de7ea72015-04-23 13:20:51 -07001628
1629 // freeBufferLocked puts this slot on the free slots list. Since
1630 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001631 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001632
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001633 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1634 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001635
1636 // Make sure the erase is done after all uses of the slot
1637 // iterator since it will be invalid after this point.
1638 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001639 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001640
Antoine Labour78014f32014-07-15 21:17:03 -07001641 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001642 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001643 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001644
1645 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1646 // waiting anymore so it can use the buffer we just allocated.
1647 while (mDequeueWaitingForAllocation) {
1648 mDequeueWaitingForAllocationCondition.wait(lock);
1649 }
Antoine Labour78014f32014-07-15 21:17:03 -07001650 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001651 }
1652}
1653
Dan Stoza9de72932015-04-16 17:28:43 -07001654status_t BufferQueueProducer::allowAllocation(bool allow) {
1655 ATRACE_CALL();
1656 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1657
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001658 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001659 mCore->mAllowAllocation = allow;
1660 return NO_ERROR;
1661}
1662
Dan Stoza812ed062015-06-02 15:45:22 -07001663status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1664 ATRACE_CALL();
1665 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1666
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001667 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001668 mCore->mGenerationNumber = generationNumber;
1669 return NO_ERROR;
1670}
1671
Dan Stozac6f30bd2015-06-08 09:32:50 -07001672String8 BufferQueueProducer::getConsumerName() const {
1673 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001674 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001675 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001676 return mConsumerName;
1677}
1678
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001679status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001680 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001681 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001682
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001683 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001684 if (!sharedBufferMode) {
1685 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001686 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001687 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001688 return NO_ERROR;
1689}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001690
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001691status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1692 ATRACE_CALL();
1693 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1694
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001695 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001696
1697 mCore->mAutoRefresh = autoRefresh;
1698 return NO_ERROR;
1699}
1700
Dan Stoza127fc632015-06-30 13:43:32 -07001701status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1702 ATRACE_CALL();
1703 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1704
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001705 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001706 bool dequeueBufferCannotBlock =
1707 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1708 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001709 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1710 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1711 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1712 "available slots. Delta = %d", delta);
1713 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001714 }
1715
Pablo Ceballos981066c2016-02-18 12:54:37 -08001716 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001717 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1718 if (timeout > 0) {
1719 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001720 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001721
Pablo Ceballos981066c2016-02-18 12:54:37 -08001722 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001723 return NO_ERROR;
1724}
1725
Sungtak Lee3249fb62019-03-02 16:40:47 -08001726status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1727 ATRACE_CALL();
1728 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1729
1730 std::lock_guard<std::mutex> lock(mCore->mMutex);
1731 mCore->mLegacyBufferDrop = drop;
1732 return NO_ERROR;
1733}
1734
Dan Stoza50101d02016-04-07 16:53:23 -07001735status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001736 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001737 ATRACE_CALL();
Dan Stoza50101d02016-04-07 16:53:23 -07001738
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001739 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001740 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1741
Dan Stoza50101d02016-04-07 16:53:23 -07001742 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1743 *outBuffer = nullptr;
1744 *outFence = Fence::NO_FENCE;
1745 return NO_ERROR;
1746 }
1747
1748 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1749 *outFence = mLastQueueBufferFence;
1750
John Reck1a61da52016-04-28 13:18:15 -07001751 // Currently only SurfaceFlinger internally ever changes
1752 // GLConsumer's filtering mode, so we just use 'true' here as
1753 // this is slightly specialized for the current client of this API,
1754 // which does want filtering.
1755 GLConsumer::computeTransformMatrix(outTransformMatrix,
1756 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1757 mLastQueuedTransform, true /* filter */);
1758
Dan Stoza50101d02016-04-07 16:53:23 -07001759 return NO_ERROR;
1760}
1761
John Reckf0f13e82021-05-18 00:42:56 -04001762status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1763 Rect* outRect, uint32_t* outTransform) {
1764 ATRACE_CALL();
John Reckf0f13e82021-05-18 00:42:56 -04001765
1766 std::lock_guard<std::mutex> lock(mCore->mMutex);
John Reckd2c4a4a2024-02-07 10:31:09 -05001767 BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1768 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1769 mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
John Reckf0f13e82021-05-18 00:42:56 -04001770 *outBuffer = nullptr;
1771 *outFence = Fence::NO_FENCE;
1772 return NO_ERROR;
1773 }
1774
1775 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1776 *outFence = mLastQueueBufferFence;
1777 *outRect = mLastQueuedCrop;
1778 *outTransform = mLastQueuedTransform;
1779
1780 return NO_ERROR;
1781}
1782
Brian Anderson3890c392016-07-25 12:48:08 -07001783void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1784 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001785}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001786
Brian Anderson3890c392016-07-25 12:48:08 -07001787void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001788 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001789 FrameEventHistoryDelta* outDelta) {
1790 if (newTimestamps == nullptr && outDelta == nullptr) {
1791 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001792 }
1793
1794 ATRACE_CALL();
1795 BQ_LOGV("addAndGetFrameTimestamps");
1796 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001797 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001798 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001799 listener = mCore->mConsumerListener;
1800 }
Yi Kong48a619f2018-06-05 16:34:59 -07001801 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001802 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001803 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001804}
1805
Dan Stoza289ade12014-02-28 11:17:17 -08001806void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1807 // If we're here, it means that a producer we were connected to died.
1808 // We're guaranteed that we are still connected to it because we remove
1809 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1810 // without synchronization here.
1811 int api = mCore->mConnectedApi;
1812 disconnect(api);
1813}
1814
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001815status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1816 BQ_LOGV("getUniqueId");
1817
1818 *outId = mCore->mUniqueId;
1819 return NO_ERROR;
1820}
1821
Chia-I Wue2786ea2017-08-07 10:36:08 -07001822status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1823 BQ_LOGV("getConsumerUsage");
1824
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001825 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001826 *outUsage = mCore->mConsumerUsageBits;
1827 return NO_ERROR;
1828}
1829
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001830status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1831 ATRACE_CALL();
1832 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1833
1834 std::lock_guard<std::mutex> lock(mCore->mMutex);
1835
1836 mCore->mAutoPrerotation = autoPrerotation;
1837 return NO_ERROR;
1838}
1839
Ady Abraham107788e2023-10-17 12:31:08 -07001840#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001841status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1842 int8_t changeFrameRateStrategy) {
1843 ATRACE_CALL();
1844 BQ_LOGV("setFrameRate: %.2f", frameRate);
1845
1846 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1847 "BufferQueueProducer::setFrameRate")) {
1848 return BAD_VALUE;
1849 }
1850
1851 sp<IConsumerListener> listener;
1852 {
1853 std::lock_guard<std::mutex> lock(mCore->mMutex);
1854 listener = mCore->mConsumerListener;
1855 }
1856 if (listener != nullptr) {
1857 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1858 }
1859 return NO_ERROR;
1860}
1861#endif
1862
John Reckdb164ff2024-04-03 16:59:28 -04001863#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1864status_t BufferQueueProducer::setAdditionalOptions(
1865 const std::vector<gui::AdditionalOptions>& options) {
1866 ATRACE_CALL();
1867 BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1868
1869 if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1870 return INVALID_OPERATION;
1871 }
1872
1873 std::lock_guard<std::mutex> lock(mCore->mMutex);
1874
1875 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1876 BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1877 return NO_INIT;
1878 }
1879
1880 if (mCore->mAdditionalOptions != options) {
1881 mCore->mAdditionalOptions = options;
1882 mCore->mAdditionalOptionsGenerationId++;
1883 }
1884 return NO_ERROR;
1885}
1886#endif
1887
Dan Stoza289ade12014-02-28 11:17:17 -08001888} // namespace android