blob: 19693e37cfcf9efcd4687df0c735c9f33f87df04 [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
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700167 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700168 bufferCount += maxDequeuedBuffers;
169
170 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
171 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
172 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
173 return BAD_VALUE;
174 }
175
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700176 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700177 if (bufferCount < minBufferSlots) {
178 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
179 "less than minimum %d", bufferCount, minBufferSlots);
180 return BAD_VALUE;
181 }
182
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700183 if (bufferCount > mCore->mMaxBufferCount) {
184 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700185 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
186 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
187 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
188 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700189 return BAD_VALUE;
190 }
191
Pablo Ceballos72daab62015-12-07 16:38:43 -0800192 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800193 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800194 return BAD_VALUE;
195 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700196 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700197 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800198 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800199 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800200 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800201 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200202 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700203 } // Autolock scope
204
205 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700206 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800207 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700208 }
209
210 return NO_ERROR;
211}
212
213status_t BufferQueueProducer::setAsyncMode(bool async) {
214 ATRACE_CALL();
215 BQ_LOGV("setAsyncMode: async = %d", async);
216
Pablo Ceballos981066c2016-02-18 12:54:37 -0800217 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200219 std::unique_lock<std::mutex> lock(mCore->mMutex);
220 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700221
222 if (mCore->mIsAbandoned) {
223 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
224 return NO_INIT;
225 }
226
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700227 if (async == mCore->mAsyncMode) {
228 return NO_ERROR;
229 }
230
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700231 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700232 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
233 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700234 BQ_LOGE("setAsyncMode(%d): this call would cause the "
235 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700236 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
237 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
238 mCore->mMaxDequeuedBufferCount,
239 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700240 return BAD_VALUE;
241 }
242
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800243 int delta = mCore->getMaxBufferCountLocked(async,
244 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
245 - mCore->getMaxBufferCountLocked();
246
Pablo Ceballos981066c2016-02-18 12:54:37 -0800247 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800248 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
249 "available slots. Delta = %d", delta);
250 return BAD_VALUE;
251 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700252 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800253 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200254 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700255 if (delta < 0) {
256 listener = mCore->mConsumerListener;
257 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700258 } // Autolock scope
259
260 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700261 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800262 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700263 }
264 return NO_ERROR;
265}
266
Dan Stoza5ecfb682016-01-04 17:01:02 -0800267int BufferQueueProducer::getFreeBufferLocked() const {
268 if (mCore->mFreeBuffers.empty()) {
269 return BufferQueueCore::INVALID_BUFFER_SLOT;
270 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800271 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800272 mCore->mFreeBuffers.pop_front();
273 return slot;
274}
275
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800276int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800277 if (mCore->mFreeSlots.empty()) {
278 return BufferQueueCore::INVALID_BUFFER_SLOT;
279 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800280 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800281 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800282 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800283}
284
285status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200286 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800287 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
288 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800289 bool tryAgain = true;
290 while (tryAgain) {
291 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 return NO_INIT;
294 }
295
Dan Stoza9f3053d2014-03-06 15:14:33 -0800296 int dequeuedCount = 0;
297 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800298 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700299 if (mSlots[s].mBufferState.isDequeued()) {
300 ++dequeuedCount;
301 }
302 if (mSlots[s].mBufferState.isAcquired()) {
303 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800304 }
305 }
306
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700307 // Producers are not allowed to dequeue more than
308 // mMaxDequeuedBufferCount buffers.
309 // This check is only done if a buffer has already been queued
310 if (mCore->mBufferHasBeenQueued &&
311 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700312 // Supress error logs when timeout is non-negative.
313 if (mDequeueTimeout < 0) {
314 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
315 "count (%d)", callerString,
316 mCore->mMaxDequeuedBufferCount);
317 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318 return INVALID_OPERATION;
319 }
320
Dan Stoza0de7ea72015-04-23 13:20:51 -0700321 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
322
Dan Stozaae3c3682014-04-18 15:43:35 -0700323 // If we disconnect and reconnect quickly, we can be in a state where
324 // our slots are empty but we have many buffers in the queue. This can
325 // cause us to run out of memory if we outrun the consumer. Wait here if
326 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800327 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700328 bool tooManyBuffers = mCore->mQueue.size()
329 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700330 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800331 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700332 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700333 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700335 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700336 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700337 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700338 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800339 } else {
340 if (caller == FreeSlotCaller::Dequeue) {
341 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800342 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800343 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
344 *found = slot;
345 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800346 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800347 }
348 } else {
349 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800350 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800351 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
352 *found = slot;
353 } else {
354 *found = getFreeBufferLocked();
355 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700356 }
357 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700358 }
359
360 // If no buffer is found, or if the queue has too many buffers
361 // outstanding, wait for a buffer to be acquired or released, or for the
362 // max buffer count to change.
363 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
364 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800365 if (tryAgain) {
366 // Return an error if we're in non-blocking mode (producer and
367 // consumer are controlled by the application).
368 // However, the consumer is allowed to briefly acquire an extra
369 // buffer (which could cause us to have to wait here), which is
370 // okay, since it is only used to implement an atomic acquire +
371 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700372 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
374 return WOULD_BLOCK;
375 }
Dan Stoza127fc632015-06-30 13:43:32 -0700376 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200377 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
378 std::chrono::nanoseconds(mDequeueTimeout));
379 if (result == std::cv_status::timeout) {
380 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700381 }
382 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200383 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700384 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800385 }
386 } // while (tryAgain)
387
388 return NO_ERROR;
389}
390
Ian Elliottd11b0442017-07-18 11:05:49 -0600391status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
392 uint32_t width, uint32_t height, PixelFormat format,
393 uint64_t usage, uint64_t* outBufferAge,
394 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800395 ATRACE_CALL();
396 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200397 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800398 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700399
400 if (mCore->mIsAbandoned) {
401 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
402 return NO_INIT;
403 }
404
405 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
406 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
407 return NO_INIT;
408 }
Dan Stoza289ade12014-02-28 11:17:17 -0800409 } // Autolock scope
410
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700411 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800412
413 if ((width && !height) || (!width && height)) {
414 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
415 return BAD_VALUE;
416 }
417
418 status_t returnFlags = NO_ERROR;
419 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
420 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800421 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800422
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000423 sp<IConsumerListener> listener;
424 bool callOnFrameDequeued = false;
425 uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
Dan Stoza289ade12014-02-28 11:17:17 -0800426 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200427 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800428
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100429 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
430 // is finished such that we don't allocate in parallel.
431 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
432 mDequeueWaitingForAllocation = true;
433 mCore->waitWhileAllocatingLocked(lock);
434 mDequeueWaitingForAllocation = false;
435 mDequeueWaitingForAllocationCondition.notify_all();
436 }
Dan Stoza289ade12014-02-28 11:17:17 -0800437
438 if (format == 0) {
439 format = mCore->mDefaultBufferFormat;
440 }
441
442 // Enable the usage bits the consumer requested
443 usage |= mCore->mConsumerUsageBits;
444
Dan Stoza9de72932015-04-16 17:28:43 -0700445 const bool useDefaultSize = !width && !height;
446 if (useDefaultSize) {
447 width = mCore->mDefaultWidth;
448 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700449 if (mCore->mAutoPrerotation &&
450 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
451 std::swap(width, height);
452 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453 }
Dan Stoza289ade12014-02-28 11:17:17 -0800454
Pablo Ceballos981066c2016-02-18 12:54:37 -0800455 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700456 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200457 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700458 if (status != NO_ERROR) {
459 return status;
460 }
461
462 // This should not happen
463 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
464 BQ_LOGE("dequeueBuffer: no available buffer slots");
465 return -EBUSY;
466 }
467
468 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
469
470 // If we are not allowed to allocate new buffers,
471 // waitForFreeSlotThenRelock must have returned a slot containing a
472 // buffer. If this buffer would require reallocation to meet the
473 // requested attributes, we free it and attempt to get another one.
474 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700475 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700476 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700477 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700478 return BAD_VALUE;
479 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800480 mCore->mFreeSlots.insert(found);
481 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700482 found = BufferItem::INVALID_BUFFER_SLOT;
483 continue;
484 }
485 }
Dan Stoza289ade12014-02-28 11:17:17 -0800486 }
487
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800488 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700489 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700490 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800491 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
492 "buffer");
493
494 return BAD_VALUE;
495 }
496
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700497 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800498 mCore->mActiveBuffers.insert(found);
499 }
Dan Stoza289ade12014-02-28 11:17:17 -0800500 *outSlot = found;
501 ATRACE_BUFFER_INDEX(found);
502
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800503 attachedByConsumer = mSlots[found].mNeedsReallocation;
504 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800505
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700506 mSlots[found].mBufferState.dequeue();
507
Yi Kong48a619f2018-06-05 16:34:59 -0700508 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700509 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800510 {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700511 if (CC_UNLIKELY(ATRACE_ENABLED())) {
512 if (buffer == nullptr) {
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000513 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
Vishnu Nair14a3c112023-04-21 14:49:47 -0700514 } else {
515 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
516 "layerCount:%d "
517 "usage:%d requested: %dx%d format:%d layerCount:%d "
518 "usage:%d ",
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +0000519 mConsumerName.c_str(), width, height, format,
Vishnu Nair14a3c112023-04-21 14:49:47 -0700520 BQ_LAYER_COUNT, usage, buffer->getWidth(),
521 buffer->getHeight(), buffer->getPixelFormat(),
522 buffer->getLayerCount(), buffer->getUsage());
523 }
524 }
Dan Stoza289ade12014-02-28 11:17:17 -0800525 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700526 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800527 mSlots[found].mRequestBufferCalled = false;
528 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
529 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
530 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800531 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700532 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800533
534 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800535 } else {
536 // We add 1 because that will be the frame number when this buffer
537 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700538 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800539 }
540
Dan Stoza800b41a2015-04-28 14:20:04 -0700541 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
542 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800543
Yi Kong48a619f2018-06-05 16:34:59 -0700544 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800545 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
546 "slot=%d w=%d h=%d format=%u",
547 found, buffer->width, buffer->height, buffer->format);
548 }
549
550 eglDisplay = mSlots[found].mEglDisplay;
551 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700552 // Don't return a fence in shared buffer mode, except for the first
553 // frame.
554 *outFence = (mCore->mSharedBufferMode &&
555 mCore->mSharedBufferSlot == found) ?
556 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800557 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
558 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700559
560 // If shared buffer mode has just been enabled, cache the slot of the
561 // first buffer that is dequeued and mark it as the shared buffer.
562 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
563 BufferQueueCore::INVALID_BUFFER_SLOT) {
564 mCore->mSharedBufferSlot = found;
565 mSlots[found].mBufferState.mShared = true;
566 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700567
568 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000569 callOnFrameDequeued = true;
570 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Adithya Srinivasana820af92019-11-01 13:55:17 -0700571 }
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000572
573 listener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800574 } // Autolock scope
575
576 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700577 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000578 sp<GraphicBuffer> graphicBuffer =
579 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
580 {mConsumerName.c_str(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -0700581
582 status_t error = graphicBuffer->initCheck();
583
Dan Stoza289ade12014-02-28 11:17:17 -0800584 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200585 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800586
Chia-I Wufeec3b12017-05-25 09:34:56 -0700587 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
589 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000590 callOnFrameDequeued = true;
591 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700592 }
593
594 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200595 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700596
Chia-I Wufeec3b12017-05-25 09:34:56 -0700597 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700598 mCore->mFreeSlots.insert(*outSlot);
599 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700600 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
601 return error;
602 }
603
Dan Stoza289ade12014-02-28 11:17:17 -0800604 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700605 mCore->mFreeSlots.insert(*outSlot);
606 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800607 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
608 return NO_INIT;
609 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800610
Pablo Ceballos9e314332016-01-12 13:49:19 -0800611 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800612 } // Autolock scope
613 }
614
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000615 if (listener != nullptr && callOnFrameDequeued) {
616 listener->onFrameDequeued(bufferId);
617 }
618
Dan Stoza9f3053d2014-03-06 15:14:33 -0800619 if (attachedByConsumer) {
620 returnFlags |= BUFFER_NEEDS_REALLOCATION;
621 }
622
Dan Stoza289ade12014-02-28 11:17:17 -0800623 if (eglFence != EGL_NO_SYNC_KHR) {
624 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
625 1000000000);
626 // If something goes wrong, log the error, but return the buffer without
627 // synchronizing access to it. It's too late at this point to abort the
628 // dequeue operation.
629 if (result == EGL_FALSE) {
630 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
631 eglGetError());
632 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
633 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
634 }
635 eglDestroySyncKHR(eglDisplay, eglFence);
636 }
637
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700638 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
639 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800640 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800641 mSlots[*outSlot].mGraphicBuffer != nullptr ?
642 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800643
Ian Elliottd11b0442017-07-18 11:05:49 -0600644 if (outBufferAge) {
645 *outBufferAge = mCore->mBufferAge;
646 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700647 addAndGetFrameTimestamps(nullptr, outTimestamps);
648
Dan Stoza289ade12014-02-28 11:17:17 -0800649 return returnFlags;
650}
651
Dan Stoza9f3053d2014-03-06 15:14:33 -0800652status_t BufferQueueProducer::detachBuffer(int slot) {
653 ATRACE_CALL();
654 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700655 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800656
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700657 sp<IConsumerListener> listener;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000658 bool callOnFrameDetached = false;
659 uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700660 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200661 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700662
663 if (mCore->mIsAbandoned) {
664 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
665 return NO_INIT;
666 }
667
668 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
669 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
670 return NO_INIT;
671 }
672
673 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
674 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
675 return BAD_VALUE;
676 }
677
678 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
679 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
680 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
681 return BAD_VALUE;
682 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000683 // TODO(http://b/140581935): This message is BQ_LOGW because it
684 // often logs when no actionable errors are present. Return to
685 // using BQ_LOGE after ensuring this only logs during errors.
686 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700687 "(state = %s)", slot, mSlots[slot].mBufferState.string());
688 return BAD_VALUE;
689 } else if (!mSlots[slot].mRequestBufferCalled) {
690 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
691 slot);
692 return BAD_VALUE;
693 }
694
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700695 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700696 auto gb = mSlots[slot].mGraphicBuffer;
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000697 if (gb != nullptr) {
698 callOnFrameDetached = true;
699 bufferId = gb->getId();
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700700 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700701 mSlots[slot].mBufferState.detachProducer();
702 mCore->mActiveBuffers.erase(slot);
703 mCore->mFreeSlots.insert(slot);
704 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200705 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700706 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800707 }
708
Shuzhen Wang266f31a2023-07-24 22:45:44 +0000709 if (listener != nullptr && callOnFrameDetached) {
710 listener->onFrameDetached(bufferId);
711 }
712
Yi Kong48a619f2018-06-05 16:34:59 -0700713 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700714 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700715 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800716
Dan Stoza9f3053d2014-03-06 15:14:33 -0800717 return NO_ERROR;
718}
719
Dan Stozad9822a32014-03-28 15:25:31 -0700720status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
721 sp<Fence>* outFence) {
722 ATRACE_CALL();
723
Yi Kong48a619f2018-06-05 16:34:59 -0700724 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700725 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
726 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700727 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700728 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
729 return BAD_VALUE;
730 }
731
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700732 sp<IConsumerListener> listener;
733 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200734 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700735
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700736 if (mCore->mIsAbandoned) {
737 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
738 return NO_INIT;
739 }
740
741 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
742 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
743 return NO_INIT;
744 }
745
746 if (mCore->mSharedBufferMode) {
747 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
748 "mode");
749 return BAD_VALUE;
750 }
751
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200752 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700753
754 if (mCore->mFreeBuffers.empty()) {
755 return NO_MEMORY;
756 }
757
758 int found = mCore->mFreeBuffers.front();
759 mCore->mFreeBuffers.remove(found);
760 mCore->mFreeSlots.insert(found);
761
762 BQ_LOGV("detachNextBuffer detached slot %d", found);
763
764 *outBuffer = mSlots[found].mGraphicBuffer;
765 *outFence = mSlots[found].mFence;
766 mCore->clearBufferSlotLocked(found);
767 VALIDATE_CONSISTENCY();
768 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700769 }
770
Yi Kong48a619f2018-06-05 16:34:59 -0700771 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700772 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700773 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800774
Dan Stozad9822a32014-03-28 15:25:31 -0700775 return NO_ERROR;
776}
777
Dan Stoza9f3053d2014-03-06 15:14:33 -0800778status_t BufferQueueProducer::attachBuffer(int* outSlot,
779 const sp<android::GraphicBuffer>& buffer) {
780 ATRACE_CALL();
781
Yi Kong48a619f2018-06-05 16:34:59 -0700782 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700783 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700785 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700786 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800787 return BAD_VALUE;
788 }
789
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200790 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700791
792 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700793 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700794 return NO_INIT;
795 }
796
797 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700798 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700799 return NO_INIT;
800 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700802 if (mCore->mSharedBufferMode) {
803 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700804 return BAD_VALUE;
805 }
806
Dan Stoza812ed062015-06-02 15:45:22 -0700807 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
808 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
809 "[queue %u]", buffer->getGenerationNumber(),
810 mCore->mGenerationNumber);
811 return BAD_VALUE;
812 }
813
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200814 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700815
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 status_t returnFlags = NO_ERROR;
817 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200818 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800819 if (status != NO_ERROR) {
820 return status;
821 }
822
823 // This should not happen
824 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700825 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800826 return -EBUSY;
827 }
828
829 *outSlot = found;
830 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700831 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800832 *outSlot, returnFlags);
833
834 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700835 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800836 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
837 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700838 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800839 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800840 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800841 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800842 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700843
Dan Stoza9f3053d2014-03-06 15:14:33 -0800844 return returnFlags;
845}
846
Dan Stoza289ade12014-02-28 11:17:17 -0800847status_t BufferQueueProducer::queueBuffer(int slot,
848 const QueueBufferInput &input, QueueBufferOutput *output) {
849 ATRACE_CALL();
850 ATRACE_BUFFER_INDEX(slot);
851
Brian Andersond6927fb2016-07-23 23:37:30 -0700852 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800853 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800854 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700855 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800856 int scalingMode;
857 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700858 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700859 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700860 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700861 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700862 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
863 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700864 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700865 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800866
Yi Kong48a619f2018-06-05 16:34:59 -0700867 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800868 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800869 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800870 }
871
Brian Anderson3d4039d2016-09-23 16:31:30 -0700872 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
873
Dan Stoza289ade12014-02-28 11:17:17 -0800874 switch (scalingMode) {
875 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
876 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
877 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
878 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
879 break;
880 default:
881 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800882 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800883 }
884
Dan Stoza8dc55392014-11-04 11:37:46 -0800885 sp<IConsumerListener> frameAvailableListener;
886 sp<IConsumerListener> frameReplacedListener;
887 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700888 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800889 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800890 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200891 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800892
893 if (mCore->mIsAbandoned) {
894 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
895 return NO_INIT;
896 }
897
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700898 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
899 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
900 return NO_INIT;
901 }
902
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800903 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800904 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800905 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800906 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700907 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800908 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700909 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800910 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800911 } else if (!mSlots[slot].mRequestBufferCalled) {
912 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
913 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800914 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800915 }
916
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700917 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700918 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700919 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700920 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700921 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700922 mSlots[slot].mBufferState.mShared = true;
923 }
924
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800925 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700926 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
927 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
928 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700929 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800930 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800931
932 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
933 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700934 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800935 crop.intersect(bufferRect, &croppedRect);
936 if (croppedRect != crop) {
937 BQ_LOGE("queueBuffer: crop rect is not contained within the "
938 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800939 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800940 }
941
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800942 // Override UNKNOWN dataspace with consumer default
943 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
944 dataSpace = mCore->mDefaultBufferDataSpace;
945 }
946
Brian Andersond6927fb2016-07-23 23:37:30 -0700947 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700948 mSlots[slot].mBufferState.queue();
949
Brian Andersond6927fb2016-07-23 23:37:30 -0700950 // Increment the frame counter and store a local version of it
951 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800952 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700953 currentFrameNumber = mCore->mFrameCounter;
954 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800955
Dan Stoza289ade12014-02-28 11:17:17 -0800956 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
957 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
958 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800959 item.mTransform = transform &
960 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800961 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800962 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
963 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700964 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800965 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800966 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700967 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700968 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800969 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700970 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700971 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700972 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700973 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800974 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700975 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700976 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700977 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700978 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800979 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800980
Ruben Brunk1681d952014-06-27 15:51:55 -0700981 mStickyTransform = stickyTransform;
982
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700983 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700984 if (mCore->mSharedBufferMode) {
985 mCore->mSharedBufferCache.crop = crop;
986 mCore->mSharedBufferCache.transform = transform;
987 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700989 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700990 }
991
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800992 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800993 if (mCore->mQueue.empty()) {
994 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
995 // and simply queue this buffer
996 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800997 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800998 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700999 // When the queue is not empty, we need to look at the last buffer
1000 // in the queue to see if we need to replace it
1001 const BufferItem& last = mCore->mQueue.itemAt(
1002 mCore->mQueue.size() - 1);
1003 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001004
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001005 if (!last.mIsStale) {
1006 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001008 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001009 // still be around. Mark it as no longer shared if this
1010 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001011 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001012 mSlots[last.mSlot].mBufferState.isFree()) {
1013 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001014 }
1015 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001016 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1017 mCore->mActiveBuffers.erase(last.mSlot);
1018 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001019 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001020 }
Dan Stoza289ade12014-02-28 11:17:17 -08001021 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001022
Steven Thomas862e7532019-07-10 19:21:03 -07001023 // Make sure to merge the damage rect from the frame we're about
1024 // to drop into the new frame's damage rect.
1025 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1026 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1027 item.mSurfaceDamage = Region::INVALID_REGION;
1028 } else {
1029 item.mSurfaceDamage |= last.mSurfaceDamage;
1030 }
1031
Dan Stoza289ade12014-02-28 11:17:17 -08001032 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001033 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001034 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001035 } else {
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 }
1039 }
1040
1041 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001042 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001043 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001044
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001045 output->width = mCore->mDefaultWidth;
1046 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001047 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001048 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1049 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001050
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001051 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001052#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001053 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001054#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001055 // Take a ticket for the callback functions
1056 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001057
Pablo Ceballos9e314332016-01-12 13:49:19 -08001058 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001059 } // Autolock scope
1060
Irvel468051e2016-06-13 16:44:44 -07001061 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1062 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1063 // there will be no Binder call
1064 if (!mConsumerIsSurfaceFlinger) {
1065 item.mGraphicBuffer.clear();
1066 }
1067
JihCheng Chiu544c5222019-04-02 20:50:58 +08001068 // Update and get FrameEventHistory.
1069 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1070 NewFrameEventsEntry newFrameEventsEntry = {
1071 currentFrameNumber,
1072 postedTime,
1073 requestedPresentTimestamp,
1074 std::move(acquireFenceTime)
1075 };
1076 addAndGetFrameTimestamps(&newFrameEventsEntry,
1077 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1078
Dan Stoza8dc55392014-11-04 11:37:46 -08001079 // Call back without the main BufferQueue lock held, but with the callback
1080 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001081
1082 int connectedApi;
1083 sp<Fence> lastQueuedFence;
1084
1085 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001086 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001087 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001088 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001089 }
1090
Yi Kong48a619f2018-06-05 16:34:59 -07001091 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001092 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001093 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001094 frameReplacedListener->onFrameReplaced(item);
1095 }
1096
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001097 connectedApi = mCore->mConnectedApi;
1098 lastQueuedFence = std::move(mLastQueueBufferFence);
1099
1100 mLastQueueBufferFence = std::move(acquireFence);
1101 mLastQueuedCrop = item.mCrop;
1102 mLastQueuedTransform = item.mTransform;
1103
Dan Stoza8dc55392014-11-04 11:37:46 -08001104 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001105 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001106 }
1107
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001108 // Wait without lock held
1109 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1110 // Waiting here allows for two full buffers to be queued but not a
1111 // third. In the event that frames take varying time, this makes a
1112 // small trade-off in favor of latency rather than throughput.
1113 lastQueuedFence->waitForever("Throttling EGL Production");
1114 }
1115
Dan Stoza289ade12014-02-28 11:17:17 -08001116 return NO_ERROR;
1117}
1118
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001119status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001120 ATRACE_CALL();
1121 BQ_LOGV("cancelBuffer: slot %d", slot);
Dan Stoza289ade12014-02-28 11:17:17 -08001122
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001123 sp<IConsumerListener> listener;
1124 bool callOnFrameCancelled = false;
1125 uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1126 {
1127 std::lock_guard<std::mutex> lock(mCore->mMutex);
1128
1129 if (mCore->mIsAbandoned) {
1130 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1131 return NO_INIT;
1132 }
1133
1134 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1135 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1136 return NO_INIT;
1137 }
1138
1139 if (mCore->mSharedBufferMode) {
1140 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1141 return BAD_VALUE;
1142 }
1143
1144 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
1145 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot,
1146 BufferQueueDefs::NUM_BUFFER_SLOTS);
1147 return BAD_VALUE;
1148 } else if (!mSlots[slot].mBufferState.isDequeued()) {
1149 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1150 "(state = %s)",
1151 slot, mSlots[slot].mBufferState.string());
1152 return BAD_VALUE;
1153 } else if (fence == nullptr) {
1154 BQ_LOGE("cancelBuffer: fence is NULL");
1155 return BAD_VALUE;
1156 }
1157
1158 mSlots[slot].mBufferState.cancel();
1159
1160 // After leaving shared buffer mode, the shared buffer will still be around.
1161 // Mark it as no longer shared if this operation causes it to be free.
1162 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1163 mSlots[slot].mBufferState.mShared = false;
1164 }
1165
1166 // Don't put the shared buffer on the free list.
1167 if (!mSlots[slot].mBufferState.isShared()) {
1168 mCore->mActiveBuffers.erase(slot);
1169 mCore->mFreeBuffers.push_back(slot);
1170 }
1171
1172 auto gb = mSlots[slot].mGraphicBuffer;
1173 if (gb != nullptr) {
1174 callOnFrameCancelled = true;
1175 bufferId = gb->getId();
1176 }
1177 mSlots[slot].mFence = fence;
1178 mCore->mDequeueCondition.notify_all();
1179 listener = mCore->mConsumerListener;
1180 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001181 }
1182
Shuzhen Wang266f31a2023-07-24 22:45:44 +00001183 if (listener != nullptr && callOnFrameCancelled) {
1184 listener->onFrameCancelled(bufferId);
Dan Stoza289ade12014-02-28 11:17:17 -08001185 }
1186
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001187 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001188}
1189
1190int BufferQueueProducer::query(int what, int *outValue) {
1191 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001192 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001193
Yi Kong48a619f2018-06-05 16:34:59 -07001194 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001195 BQ_LOGE("query: outValue was NULL");
1196 return BAD_VALUE;
1197 }
1198
1199 if (mCore->mIsAbandoned) {
1200 BQ_LOGE("query: BufferQueue has been abandoned");
1201 return NO_INIT;
1202 }
1203
1204 int value;
1205 switch (what) {
1206 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001207 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001208 break;
1209 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001210 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001211 break;
1212 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001213 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001214 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001215 case NATIVE_WINDOW_LAYER_COUNT:
1216 // All BufferQueue buffers have a single layer.
1217 value = BQ_LAYER_COUNT;
1218 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001219 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001220 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001221 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001222 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001223 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001224 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001225 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1226 value = (mCore->mQueue.size() > 1);
1227 break;
1228 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001229 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001230 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001231 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001232 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1233 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1234 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001235 case NATIVE_WINDOW_BUFFER_AGE:
1236 if (mCore->mBufferAge > INT32_MAX) {
1237 value = 0;
1238 } else {
1239 value = static_cast<int32_t>(mCore->mBufferAge);
1240 }
1241 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001242 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1243 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1244 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001245 default:
1246 return BAD_VALUE;
1247 }
1248
1249 BQ_LOGV("query: %d? %d", what, value);
1250 *outValue = value;
1251 return NO_ERROR;
1252}
1253
Dan Stozaf0eaf252014-03-21 13:05:51 -07001254status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001255 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1256 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001257 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001258 mConsumerName = mCore->mConsumerName;
1259 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1260 producerControlledByApp ? "true" : "false");
1261
1262 if (mCore->mIsAbandoned) {
1263 BQ_LOGE("connect: BufferQueue has been abandoned");
1264 return NO_INIT;
1265 }
1266
Yi Kong48a619f2018-06-05 16:34:59 -07001267 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001268 BQ_LOGE("connect: BufferQueue has no consumer");
1269 return NO_INIT;
1270 }
1271
Yi Kong48a619f2018-06-05 16:34:59 -07001272 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001273 BQ_LOGE("connect: output was NULL");
1274 return BAD_VALUE;
1275 }
1276
1277 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1278 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1279 mCore->mConnectedApi, api);
1280 return BAD_VALUE;
1281 }
1282
1283 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1284 mDequeueTimeout < 0 ?
1285 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1286 mCore->mMaxBufferCount) -
1287 mCore->getMaxBufferCountLocked();
1288 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1289 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1290 "slots. Delta = %d", delta);
1291 return BAD_VALUE;
1292 }
1293
Dan Stoza289ade12014-02-28 11:17:17 -08001294 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001295 switch (api) {
1296 case NATIVE_WINDOW_API_EGL:
1297 case NATIVE_WINDOW_API_CPU:
1298 case NATIVE_WINDOW_API_MEDIA:
1299 case NATIVE_WINDOW_API_CAMERA:
1300 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001301
1302 output->width = mCore->mDefaultWidth;
1303 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001304 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001305 output->numPendingBuffers =
1306 static_cast<uint32_t>(mCore->mQueue.size());
1307 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001308 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001309 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001310
Yi Kong48a619f2018-06-05 16:34:59 -07001311 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001312 // Set up a death notification so that we can disconnect
1313 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001314#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001315 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001316 status = IInterface::asBinder(listener)->linkToDeath(
1317 static_cast<IBinder::DeathRecipient*>(this));
1318 if (status != NO_ERROR) {
1319 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1320 strerror(-status), status);
1321 }
1322 mCore->mLinkedToDeath = listener;
1323 }
Chong Zhang62493092020-01-15 16:04:47 -08001324#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001325 mCore->mConnectedProducerListener = listener;
1326 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001327 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001328 break;
1329 default:
1330 BQ_LOGE("connect: unknown API %d", api);
1331 status = BAD_VALUE;
1332 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001333 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001334 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001335 mCore->mBufferHasBeenQueued = false;
1336 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001337 mCore->mQueueBufferCanDrop = false;
1338 mCore->mLegacyBufferDrop = true;
1339 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1340 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1341 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001342 }
Dan Stoza289ade12014-02-28 11:17:17 -08001343
Pablo Ceballos981066c2016-02-18 12:54:37 -08001344 mCore->mAllowAllocation = true;
1345 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001346 return status;
1347}
1348
Robert Carr97b9c862016-09-08 13:54:35 -07001349status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001350 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001351 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001352
1353 int status = NO_ERROR;
1354 sp<IConsumerListener> listener;
1355 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001356 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001357
1358 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001359 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001360 return NO_ERROR;
1361 }
1362 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1363 }
1364
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001365 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001366
1367 if (mCore->mIsAbandoned) {
1368 // It's not really an error to disconnect after the surface has
1369 // been abandoned; it should just be a no-op.
1370 return NO_ERROR;
1371 }
1372
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001373 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001374 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1375 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1376 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001377 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001378 // If we're asked to disconnect the currently connected api but
1379 // nobody is connected, it's not really an error.
1380 if (api == BufferQueueCore::NO_CONNECTED_API) {
1381 return NO_ERROR;
1382 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001383 }
1384
Dan Stoza289ade12014-02-28 11:17:17 -08001385 switch (api) {
1386 case NATIVE_WINDOW_API_EGL:
1387 case NATIVE_WINDOW_API_CPU:
1388 case NATIVE_WINDOW_API_MEDIA:
1389 case NATIVE_WINDOW_API_CAMERA:
1390 if (mCore->mConnectedApi == api) {
1391 mCore->freeAllBuffersLocked();
1392
Chong Zhang62493092020-01-15 16:04:47 -08001393#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001394 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001395 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001396 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001397 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001398 // This can fail if we're here because of the death
1399 // notification, but we just ignore it
1400 token->unlinkToDeath(
1401 static_cast<IBinder::DeathRecipient*>(this));
1402 }
Chong Zhang62493092020-01-15 16:04:47 -08001403#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001404 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001405 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001406 mCore->mLinkedToDeath = nullptr;
1407 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001408 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001409 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001410 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001411 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001412 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001413 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001414 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1415 BQ_LOGE("disconnect: not connected (req=%d)", api);
1416 status = NO_INIT;
1417 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001418 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001419 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001420 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001421 }
1422 break;
1423 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001424 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001425 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001426 break;
1427 }
1428 } // Autolock scope
1429
1430 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001431 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001432 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001433 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001434 }
1435
1436 return status;
1437}
1438
Jesse Hall399184a2014-03-03 15:42:54 -08001439status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001440 sp<IConsumerListener> listener;
1441 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001442 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001443 mCore->mSidebandStream = stream;
1444 listener = mCore->mConsumerListener;
1445 } // Autolock scope
1446
Yi Kong48a619f2018-06-05 16:34:59 -07001447 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001448 listener->onSidebandStreamChanged();
1449 }
Jesse Hall399184a2014-03-03 15:42:54 -08001450 return NO_ERROR;
1451}
1452
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001453void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001454 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001455 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001456
1457 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001458 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001459 size_t newBufferCount = 0;
1460 uint32_t allocWidth = 0;
1461 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001462 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001463 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001464 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001465 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001466 std::unique_lock<std::mutex> lock(mCore->mMutex);
1467 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001468
Dan Stoza9de72932015-04-16 17:28:43 -07001469 if (!mCore->mAllowAllocation) {
1470 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1471 "BufferQueue");
1472 return;
1473 }
1474
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001475 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1476 // both allocateBuffers and dequeueBuffer.
1477 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001478 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001479 return;
1480 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001481
Antoine Labour78014f32014-07-15 21:17:03 -07001482 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1483 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001484 if (useDefaultSize && mCore->mAutoPrerotation &&
1485 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1486 std::swap(allocWidth, allocHeight);
1487 }
1488
Antoine Labour78014f32014-07-15 21:17:03 -07001489 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1490 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001491 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001492
1493 mCore->mIsAllocating = true;
1494 } // Autolock scope
1495
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001496 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001497 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001498 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001499 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001500 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001501
1502 status_t result = graphicBuffer->initCheck();
1503
Antoine Labour78014f32014-07-15 21:17:03 -07001504 if (result != NO_ERROR) {
1505 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001506 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001507 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001508 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001509 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001510 return;
1511 }
1512 buffers.push_back(graphicBuffer);
1513 }
1514
1515 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001516 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001517 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1518 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001519 if (useDefaultSize && mCore->mAutoPrerotation &&
1520 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1521 std::swap(checkWidth, checkHeight);
1522 }
1523
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001524 PixelFormat checkFormat = format != 0 ?
1525 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001526 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001527 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1528 checkFormat != allocFormat || checkUsage != allocUsage) {
1529 // Something changed while we released the lock. Retry.
1530 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1531 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001532 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001533 continue;
1534 }
1535
Antoine Labour78014f32014-07-15 21:17:03 -07001536 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001537 if (mCore->mFreeSlots.empty()) {
1538 BQ_LOGV("allocateBuffers: a slot was occupied while "
1539 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001540 continue;
1541 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001542 auto slot = mCore->mFreeSlots.begin();
1543 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1544 mSlots[*slot].mGraphicBuffer = buffers[i];
1545 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001546
1547 // freeBufferLocked puts this slot on the free slots list. Since
1548 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001549 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001550
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001551 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1552 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001553
1554 // Make sure the erase is done after all uses of the slot
1555 // iterator since it will be invalid after this point.
1556 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001557 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001558
Antoine Labour78014f32014-07-15 21:17:03 -07001559 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001560 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001561 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001562
1563 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1564 // waiting anymore so it can use the buffer we just allocated.
1565 while (mDequeueWaitingForAllocation) {
1566 mDequeueWaitingForAllocationCondition.wait(lock);
1567 }
Antoine Labour78014f32014-07-15 21:17:03 -07001568 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001569 }
1570}
1571
Dan Stoza9de72932015-04-16 17:28:43 -07001572status_t BufferQueueProducer::allowAllocation(bool allow) {
1573 ATRACE_CALL();
1574 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1575
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001576 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001577 mCore->mAllowAllocation = allow;
1578 return NO_ERROR;
1579}
1580
Dan Stoza812ed062015-06-02 15:45:22 -07001581status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1582 ATRACE_CALL();
1583 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1584
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001585 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001586 mCore->mGenerationNumber = generationNumber;
1587 return NO_ERROR;
1588}
1589
Dan Stozac6f30bd2015-06-08 09:32:50 -07001590String8 BufferQueueProducer::getConsumerName() const {
1591 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001592 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001593 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001594 return mConsumerName;
1595}
1596
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001597status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001598 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001599 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001600
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001601 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001602 if (!sharedBufferMode) {
1603 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001604 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001605 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001606 return NO_ERROR;
1607}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001608
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001609status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1610 ATRACE_CALL();
1611 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1612
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001613 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001614
1615 mCore->mAutoRefresh = autoRefresh;
1616 return NO_ERROR;
1617}
1618
Dan Stoza127fc632015-06-30 13:43:32 -07001619status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1620 ATRACE_CALL();
1621 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1622
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001623 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001624 bool dequeueBufferCannotBlock =
1625 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1626 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001627 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1628 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1629 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1630 "available slots. Delta = %d", delta);
1631 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001632 }
1633
Pablo Ceballos981066c2016-02-18 12:54:37 -08001634 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001635 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1636 if (timeout > 0) {
1637 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001638 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001639
Pablo Ceballos981066c2016-02-18 12:54:37 -08001640 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001641 return NO_ERROR;
1642}
1643
Sungtak Lee3249fb62019-03-02 16:40:47 -08001644status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1645 ATRACE_CALL();
1646 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1647
1648 std::lock_guard<std::mutex> lock(mCore->mMutex);
1649 mCore->mLegacyBufferDrop = drop;
1650 return NO_ERROR;
1651}
1652
Dan Stoza50101d02016-04-07 16:53:23 -07001653status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001654 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001655 ATRACE_CALL();
1656 BQ_LOGV("getLastQueuedBuffer");
1657
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001658 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001659 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1660 *outBuffer = nullptr;
1661 *outFence = Fence::NO_FENCE;
1662 return NO_ERROR;
1663 }
1664
1665 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1666 *outFence = mLastQueueBufferFence;
1667
John Reck1a61da52016-04-28 13:18:15 -07001668 // Currently only SurfaceFlinger internally ever changes
1669 // GLConsumer's filtering mode, so we just use 'true' here as
1670 // this is slightly specialized for the current client of this API,
1671 // which does want filtering.
1672 GLConsumer::computeTransformMatrix(outTransformMatrix,
1673 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1674 mLastQueuedTransform, true /* filter */);
1675
Dan Stoza50101d02016-04-07 16:53:23 -07001676 return NO_ERROR;
1677}
1678
John Reckf0f13e82021-05-18 00:42:56 -04001679status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1680 Rect* outRect, uint32_t* outTransform) {
1681 ATRACE_CALL();
1682 BQ_LOGV("getLastQueuedBuffer");
1683
1684 std::lock_guard<std::mutex> lock(mCore->mMutex);
1685 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1686 *outBuffer = nullptr;
1687 *outFence = Fence::NO_FENCE;
1688 return NO_ERROR;
1689 }
1690
1691 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1692 *outFence = mLastQueueBufferFence;
1693 *outRect = mLastQueuedCrop;
1694 *outTransform = mLastQueuedTransform;
1695
1696 return NO_ERROR;
1697}
1698
Brian Anderson3890c392016-07-25 12:48:08 -07001699void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1700 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001701}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001702
Brian Anderson3890c392016-07-25 12:48:08 -07001703void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001704 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001705 FrameEventHistoryDelta* outDelta) {
1706 if (newTimestamps == nullptr && outDelta == nullptr) {
1707 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001708 }
1709
1710 ATRACE_CALL();
1711 BQ_LOGV("addAndGetFrameTimestamps");
1712 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001713 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001714 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001715 listener = mCore->mConsumerListener;
1716 }
Yi Kong48a619f2018-06-05 16:34:59 -07001717 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001718 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001719 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001720}
1721
Dan Stoza289ade12014-02-28 11:17:17 -08001722void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1723 // If we're here, it means that a producer we were connected to died.
1724 // We're guaranteed that we are still connected to it because we remove
1725 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1726 // without synchronization here.
1727 int api = mCore->mConnectedApi;
1728 disconnect(api);
1729}
1730
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001731status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1732 BQ_LOGV("getUniqueId");
1733
1734 *outId = mCore->mUniqueId;
1735 return NO_ERROR;
1736}
1737
Chia-I Wue2786ea2017-08-07 10:36:08 -07001738status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1739 BQ_LOGV("getConsumerUsage");
1740
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001741 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001742 *outUsage = mCore->mConsumerUsageBits;
1743 return NO_ERROR;
1744}
1745
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001746status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1747 ATRACE_CALL();
1748 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1749
1750 std::lock_guard<std::mutex> lock(mCore->mMutex);
1751
1752 mCore->mAutoPrerotation = autoPrerotation;
1753 return NO_ERROR;
1754}
1755
Ady Abraham107788e2023-10-17 12:31:08 -07001756#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001757status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1758 int8_t changeFrameRateStrategy) {
1759 ATRACE_CALL();
1760 BQ_LOGV("setFrameRate: %.2f", frameRate);
1761
1762 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1763 "BufferQueueProducer::setFrameRate")) {
1764 return BAD_VALUE;
1765 }
1766
1767 sp<IConsumerListener> listener;
1768 {
1769 std::lock_guard<std::mutex> lock(mCore->mMutex);
1770 listener = mCore->mConsumerListener;
1771 }
1772 if (listener != nullptr) {
1773 listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1774 }
1775 return NO_ERROR;
1776}
1777#endif
1778
Dan Stoza289ade12014-02-28 11:17:17 -08001779} // namespace android