blob: 9eb1a9f5260dd7b460433e45feef6f33bea89b9e [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>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Dan Stoza289ade12014-02-28 11:17:17 -080045namespace android {
46
Iris Chang430193f2019-12-04 16:25:46 +080047// Macros for include BufferQueueCore information in log messages
48#define BQ_LOGV(x, ...) \
49 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
50 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
51 ##__VA_ARGS__)
52#define BQ_LOGD(x, ...) \
53 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
54 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
55 ##__VA_ARGS__)
56#define BQ_LOGI(x, ...) \
57 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
58 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
59 ##__VA_ARGS__)
60#define BQ_LOGW(x, ...) \
61 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
62 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
63 ##__VA_ARGS__)
64#define BQ_LOGE(x, ...) \
65 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
66 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
67 ##__VA_ARGS__)
68
Craig Donner6ebc46a2016-10-21 15:23:44 -070069static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080070ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070071
Irvel468051e2016-06-13 16:44:44 -070072BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
73 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080074 mCore(core),
75 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070076 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070077 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070078 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080079 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070080 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080081 mCallbackMutex(),
82 mNextCallbackTicket(0),
83 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070084 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010085 mDequeueTimeout(-1),
86 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080087
88BufferQueueProducer::~BufferQueueProducer() {}
89
90status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
91 ATRACE_CALL();
92 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020093 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080094
95 if (mCore->mIsAbandoned) {
96 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
97 return NO_INIT;
98 }
99
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700100 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
101 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
102 return NO_INIT;
103 }
104
Dan Stoza3e96f192014-03-03 10:16:19 -0800105 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800106 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800107 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800108 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700109 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800110 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700111 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800112 return BAD_VALUE;
113 }
114
115 mSlots[slot].mRequestBufferCalled = true;
116 *buf = mSlots[slot].mGraphicBuffer;
117 return NO_ERROR;
118}
119
Pablo Ceballosfa455352015-08-12 17:47:47 -0700120status_t BufferQueueProducer::setMaxDequeuedBufferCount(
121 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700122 int maxBufferCount;
123 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
124}
125
126status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
127 int* maxBufferCount) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700128 ATRACE_CALL();
129 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
130 maxDequeuedBuffers);
131
Pablo Ceballos981066c2016-02-18 12:54:37 -0800132 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700133 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200134 std::unique_lock<std::mutex> lock(mCore->mMutex);
135 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700136
137 if (mCore->mIsAbandoned) {
138 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
139 "abandoned");
140 return NO_INIT;
141 }
142
Brian Lindahlc794b692023-01-31 15:42:47 -0700143 *maxBufferCount = mCore->getMaxBufferCountLocked();
144
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700145 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
146 return NO_ERROR;
147 }
148
Pablo Ceballos72daab62015-12-07 16:38:43 -0800149 // The new maxDequeuedBuffer count should not be violated by the number
150 // of currently dequeued buffers
151 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800152 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700153 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800154 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700155 }
156 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 if (dequeuedCount > maxDequeuedBuffers) {
158 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
159 "count (%d) exceeds the current dequeued buffer count (%d)",
160 maxDequeuedBuffers, dequeuedCount);
161 return BAD_VALUE;
162 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700163
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700164 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 bufferCount += maxDequeuedBuffers;
166
167 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
168 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
169 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
170 return BAD_VALUE;
171 }
172
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700173 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700174 if (bufferCount < minBufferSlots) {
175 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
176 "less than minimum %d", bufferCount, minBufferSlots);
177 return BAD_VALUE;
178 }
179
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700180 if (bufferCount > mCore->mMaxBufferCount) {
181 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700182 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
183 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
184 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
185 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700186 return BAD_VALUE;
187 }
188
Pablo Ceballos72daab62015-12-07 16:38:43 -0800189 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800190 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800191 return BAD_VALUE;
192 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700193 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700194 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800195 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800196 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800197 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800198 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200199 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700200 } // Autolock scope
201
202 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700203 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800204 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700205 }
206
207 return NO_ERROR;
208}
209
210status_t BufferQueueProducer::setAsyncMode(bool async) {
211 ATRACE_CALL();
212 BQ_LOGV("setAsyncMode: async = %d", async);
213
Pablo Ceballos981066c2016-02-18 12:54:37 -0800214 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200216 std::unique_lock<std::mutex> lock(mCore->mMutex);
217 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218
219 if (mCore->mIsAbandoned) {
220 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
221 return NO_INIT;
222 }
223
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700224 if (async == mCore->mAsyncMode) {
225 return NO_ERROR;
226 }
227
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700228 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700229 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
230 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700231 BQ_LOGE("setAsyncMode(%d): this call would cause the "
232 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700233 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
234 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
235 mCore->mMaxDequeuedBufferCount,
236 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700237 return BAD_VALUE;
238 }
239
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800240 int delta = mCore->getMaxBufferCountLocked(async,
241 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
242 - mCore->getMaxBufferCountLocked();
243
Pablo Ceballos981066c2016-02-18 12:54:37 -0800244 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800245 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
246 "available slots. Delta = %d", delta);
247 return BAD_VALUE;
248 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700249 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800250 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200251 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700252 if (delta < 0) {
253 listener = mCore->mConsumerListener;
254 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700255 } // Autolock scope
256
257 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700258 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800259 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700260 }
261 return NO_ERROR;
262}
263
Dan Stoza5ecfb682016-01-04 17:01:02 -0800264int BufferQueueProducer::getFreeBufferLocked() const {
265 if (mCore->mFreeBuffers.empty()) {
266 return BufferQueueCore::INVALID_BUFFER_SLOT;
267 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800268 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800269 mCore->mFreeBuffers.pop_front();
270 return slot;
271}
272
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800273int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800274 if (mCore->mFreeSlots.empty()) {
275 return BufferQueueCore::INVALID_BUFFER_SLOT;
276 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800277 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800278 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800279 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800280}
281
282status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200283 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
285 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800286 bool tryAgain = true;
287 while (tryAgain) {
288 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800289 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800290 return NO_INIT;
291 }
292
Dan Stoza9f3053d2014-03-06 15:14:33 -0800293 int dequeuedCount = 0;
294 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800295 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 if (mSlots[s].mBufferState.isDequeued()) {
297 ++dequeuedCount;
298 }
299 if (mSlots[s].mBufferState.isAcquired()) {
300 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800301 }
302 }
303
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700304 // Producers are not allowed to dequeue more than
305 // mMaxDequeuedBufferCount buffers.
306 // This check is only done if a buffer has already been queued
307 if (mCore->mBufferHasBeenQueued &&
308 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700309 // Supress error logs when timeout is non-negative.
310 if (mDequeueTimeout < 0) {
311 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
312 "count (%d)", callerString,
313 mCore->mMaxDequeuedBufferCount);
314 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800315 return INVALID_OPERATION;
316 }
317
Dan Stoza0de7ea72015-04-23 13:20:51 -0700318 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
319
Dan Stozaae3c3682014-04-18 15:43:35 -0700320 // If we disconnect and reconnect quickly, we can be in a state where
321 // our slots are empty but we have many buffers in the queue. This can
322 // cause us to run out of memory if we outrun the consumer. Wait here if
323 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800324 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700325 bool tooManyBuffers = mCore->mQueue.size()
326 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700327 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800328 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700329 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700330 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700331 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700332 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700333 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700334 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700335 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800336 } else {
337 if (caller == FreeSlotCaller::Dequeue) {
338 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800339 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800340 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
341 *found = slot;
342 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800343 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800344 }
345 } else {
346 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800347 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800348 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
349 *found = slot;
350 } else {
351 *found = getFreeBufferLocked();
352 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700353 }
354 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700355 }
356
357 // If no buffer is found, or if the queue has too many buffers
358 // outstanding, wait for a buffer to be acquired or released, or for the
359 // max buffer count to change.
360 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
361 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800362 if (tryAgain) {
363 // Return an error if we're in non-blocking mode (producer and
364 // consumer are controlled by the application).
365 // However, the consumer is allowed to briefly acquire an extra
366 // buffer (which could cause us to have to wait here), which is
367 // okay, since it is only used to implement an atomic acquire +
368 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700369 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800370 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
371 return WOULD_BLOCK;
372 }
Dan Stoza127fc632015-06-30 13:43:32 -0700373 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200374 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
375 std::chrono::nanoseconds(mDequeueTimeout));
376 if (result == std::cv_status::timeout) {
377 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700378 }
379 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200380 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700381 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 }
383 } // while (tryAgain)
384
385 return NO_ERROR;
386}
387
Ian Elliottd11b0442017-07-18 11:05:49 -0600388status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
389 uint32_t width, uint32_t height, PixelFormat format,
390 uint64_t usage, uint64_t* outBufferAge,
391 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800392 ATRACE_CALL();
393 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200394 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800395 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700396
397 if (mCore->mIsAbandoned) {
398 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
399 return NO_INIT;
400 }
401
402 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
403 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
404 return NO_INIT;
405 }
Dan Stoza289ade12014-02-28 11:17:17 -0800406 } // Autolock scope
407
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800409
410 if ((width && !height) || (!width && height)) {
411 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
412 return BAD_VALUE;
413 }
414
415 status_t returnFlags = NO_ERROR;
416 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
417 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800419
420 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200421 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800422
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100423 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
424 // is finished such that we don't allocate in parallel.
425 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
426 mDequeueWaitingForAllocation = true;
427 mCore->waitWhileAllocatingLocked(lock);
428 mDequeueWaitingForAllocation = false;
429 mDequeueWaitingForAllocationCondition.notify_all();
430 }
Dan Stoza289ade12014-02-28 11:17:17 -0800431
432 if (format == 0) {
433 format = mCore->mDefaultBufferFormat;
434 }
435
436 // Enable the usage bits the consumer requested
437 usage |= mCore->mConsumerUsageBits;
438
Dan Stoza9de72932015-04-16 17:28:43 -0700439 const bool useDefaultSize = !width && !height;
440 if (useDefaultSize) {
441 width = mCore->mDefaultWidth;
442 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700443 if (mCore->mAutoPrerotation &&
444 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
445 std::swap(width, height);
446 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447 }
Dan Stoza289ade12014-02-28 11:17:17 -0800448
Pablo Ceballos981066c2016-02-18 12:54:37 -0800449 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700450 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200451 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700452 if (status != NO_ERROR) {
453 return status;
454 }
455
456 // This should not happen
457 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
458 BQ_LOGE("dequeueBuffer: no available buffer slots");
459 return -EBUSY;
460 }
461
462 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
463
464 // If we are not allowed to allocate new buffers,
465 // waitForFreeSlotThenRelock must have returned a slot containing a
466 // buffer. If this buffer would require reallocation to meet the
467 // requested attributes, we free it and attempt to get another one.
468 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700469 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700470 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700471 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700472 return BAD_VALUE;
473 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800474 mCore->mFreeSlots.insert(found);
475 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700476 found = BufferItem::INVALID_BUFFER_SLOT;
477 continue;
478 }
479 }
Dan Stoza289ade12014-02-28 11:17:17 -0800480 }
481
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800482 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700483 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800485 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
486 "buffer");
487
488 return BAD_VALUE;
489 }
490
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700491 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800492 mCore->mActiveBuffers.insert(found);
493 }
Dan Stoza289ade12014-02-28 11:17:17 -0800494 *outSlot = found;
495 ATRACE_BUFFER_INDEX(found);
496
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800497 attachedByConsumer = mSlots[found].mNeedsReallocation;
498 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800499
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700500 mSlots[found].mBufferState.dequeue();
501
Yi Kong48a619f2018-06-05 16:34:59 -0700502 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700503 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800504 {
505 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700506 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800507 mSlots[found].mRequestBufferCalled = false;
508 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
509 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
510 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800511 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700512 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800513
514 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800515 } else {
516 // We add 1 because that will be the frame number when this buffer
517 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700518 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800519 }
520
Dan Stoza800b41a2015-04-28 14:20:04 -0700521 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
522 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800523
Yi Kong48a619f2018-06-05 16:34:59 -0700524 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800525 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
526 "slot=%d w=%d h=%d format=%u",
527 found, buffer->width, buffer->height, buffer->format);
528 }
529
530 eglDisplay = mSlots[found].mEglDisplay;
531 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700532 // Don't return a fence in shared buffer mode, except for the first
533 // frame.
534 *outFence = (mCore->mSharedBufferMode &&
535 mCore->mSharedBufferSlot == found) ?
536 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800537 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
538 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700539
540 // If shared buffer mode has just been enabled, cache the slot of the
541 // first buffer that is dequeued and mark it as the shared buffer.
542 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
543 BufferQueueCore::INVALID_BUFFER_SLOT) {
544 mCore->mSharedBufferSlot = found;
545 mSlots[found].mBufferState.mShared = true;
546 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700547
548 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
549 if (mCore->mConsumerListener != nullptr) {
550 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
551 }
552 }
Dan Stoza289ade12014-02-28 11:17:17 -0800553 } // Autolock scope
554
555 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700556 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700557 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700558 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700559 {mConsumerName.string(), mConsumerName.size()});
560
561 status_t error = graphicBuffer->initCheck();
562
Dan Stoza289ade12014-02-28 11:17:17 -0800563 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200564 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800565
Chia-I Wufeec3b12017-05-25 09:34:56 -0700566 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700567 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
568 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Adithya Srinivasana820af92019-11-01 13:55:17 -0700569 if (mCore->mConsumerListener != nullptr) {
570 mCore->mConsumerListener->onFrameDequeued(
571 mSlots[*outSlot].mGraphicBuffer->getId());
572 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700573 }
574
575 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200576 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700577
Chia-I Wufeec3b12017-05-25 09:34:56 -0700578 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700579 mCore->mFreeSlots.insert(*outSlot);
580 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700581 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
582 return error;
583 }
584
Dan Stoza289ade12014-02-28 11:17:17 -0800585 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700586 mCore->mFreeSlots.insert(*outSlot);
587 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800588 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
589 return NO_INIT;
590 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800591
Pablo Ceballos9e314332016-01-12 13:49:19 -0800592 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800593 } // Autolock scope
594 }
595
Dan Stoza9f3053d2014-03-06 15:14:33 -0800596 if (attachedByConsumer) {
597 returnFlags |= BUFFER_NEEDS_REALLOCATION;
598 }
599
Dan Stoza289ade12014-02-28 11:17:17 -0800600 if (eglFence != EGL_NO_SYNC_KHR) {
601 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
602 1000000000);
603 // If something goes wrong, log the error, but return the buffer without
604 // synchronizing access to it. It's too late at this point to abort the
605 // dequeue operation.
606 if (result == EGL_FALSE) {
607 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
608 eglGetError());
609 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
610 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
611 }
612 eglDestroySyncKHR(eglDisplay, eglFence);
613 }
614
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700615 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
616 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800617 mSlots[*outSlot].mFrameNumber,
618 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
619
Ian Elliottd11b0442017-07-18 11:05:49 -0600620 if (outBufferAge) {
621 *outBufferAge = mCore->mBufferAge;
622 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700623 addAndGetFrameTimestamps(nullptr, outTimestamps);
624
Dan Stoza289ade12014-02-28 11:17:17 -0800625 return returnFlags;
626}
627
Dan Stoza9f3053d2014-03-06 15:14:33 -0800628status_t BufferQueueProducer::detachBuffer(int slot) {
629 ATRACE_CALL();
630 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700631 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800632
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700633 sp<IConsumerListener> listener;
634 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200635 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700636
637 if (mCore->mIsAbandoned) {
638 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
639 return NO_INIT;
640 }
641
642 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
643 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
644 return NO_INIT;
645 }
646
647 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
648 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
649 return BAD_VALUE;
650 }
651
652 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
653 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
654 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
655 return BAD_VALUE;
656 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000657 // TODO(http://b/140581935): This message is BQ_LOGW because it
658 // often logs when no actionable errors are present. Return to
659 // using BQ_LOGE after ensuring this only logs during errors.
660 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700661 "(state = %s)", slot, mSlots[slot].mBufferState.string());
662 return BAD_VALUE;
663 } else if (!mSlots[slot].mRequestBufferCalled) {
664 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
665 slot);
666 return BAD_VALUE;
667 }
668
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700669 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700670 auto gb = mSlots[slot].mGraphicBuffer;
671 if (listener != nullptr && gb != nullptr) {
672 listener->onFrameDetached(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700673 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700674 mSlots[slot].mBufferState.detachProducer();
675 mCore->mActiveBuffers.erase(slot);
676 mCore->mFreeSlots.insert(slot);
677 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200678 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700679 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800680 }
681
Yi Kong48a619f2018-06-05 16:34:59 -0700682 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700683 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700684 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800685
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686 return NO_ERROR;
687}
688
Dan Stozad9822a32014-03-28 15:25:31 -0700689status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
690 sp<Fence>* outFence) {
691 ATRACE_CALL();
692
Yi Kong48a619f2018-06-05 16:34:59 -0700693 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700694 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
695 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700696 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700697 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
698 return BAD_VALUE;
699 }
700
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700701 sp<IConsumerListener> listener;
702 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200703 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700704
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700705 if (mCore->mIsAbandoned) {
706 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
707 return NO_INIT;
708 }
709
710 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
711 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
712 return NO_INIT;
713 }
714
715 if (mCore->mSharedBufferMode) {
716 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
717 "mode");
718 return BAD_VALUE;
719 }
720
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200721 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700722
723 if (mCore->mFreeBuffers.empty()) {
724 return NO_MEMORY;
725 }
726
727 int found = mCore->mFreeBuffers.front();
728 mCore->mFreeBuffers.remove(found);
729 mCore->mFreeSlots.insert(found);
730
731 BQ_LOGV("detachNextBuffer detached slot %d", found);
732
733 *outBuffer = mSlots[found].mGraphicBuffer;
734 *outFence = mSlots[found].mFence;
735 mCore->clearBufferSlotLocked(found);
736 VALIDATE_CONSISTENCY();
737 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700738 }
739
Yi Kong48a619f2018-06-05 16:34:59 -0700740 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700741 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700742 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800743
Dan Stozad9822a32014-03-28 15:25:31 -0700744 return NO_ERROR;
745}
746
Dan Stoza9f3053d2014-03-06 15:14:33 -0800747status_t BufferQueueProducer::attachBuffer(int* outSlot,
748 const sp<android::GraphicBuffer>& buffer) {
749 ATRACE_CALL();
750
Yi Kong48a619f2018-06-05 16:34:59 -0700751 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700752 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800753 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700754 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700755 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800756 return BAD_VALUE;
757 }
758
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200759 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700760
761 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700762 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700763 return NO_INIT;
764 }
765
766 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700767 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700768 return NO_INIT;
769 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800770
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700771 if (mCore->mSharedBufferMode) {
772 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700773 return BAD_VALUE;
774 }
775
Dan Stoza812ed062015-06-02 15:45:22 -0700776 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
777 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
778 "[queue %u]", buffer->getGenerationNumber(),
779 mCore->mGenerationNumber);
780 return BAD_VALUE;
781 }
782
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200783 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700784
Dan Stoza9f3053d2014-03-06 15:14:33 -0800785 status_t returnFlags = NO_ERROR;
786 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200787 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800788 if (status != NO_ERROR) {
789 return status;
790 }
791
792 // This should not happen
793 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700794 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800795 return -EBUSY;
796 }
797
798 *outSlot = found;
799 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700800 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801 *outSlot, returnFlags);
802
803 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700804 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800805 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
806 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700807 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800808 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800809 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800810 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800811 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700812
Dan Stoza9f3053d2014-03-06 15:14:33 -0800813 return returnFlags;
814}
815
Dan Stoza289ade12014-02-28 11:17:17 -0800816status_t BufferQueueProducer::queueBuffer(int slot,
817 const QueueBufferInput &input, QueueBufferOutput *output) {
818 ATRACE_CALL();
819 ATRACE_BUFFER_INDEX(slot);
820
Brian Andersond6927fb2016-07-23 23:37:30 -0700821 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800822 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800823 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700824 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800825 int scalingMode;
826 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700827 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700828 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700829 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700830 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700831 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
832 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700833 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700834 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800835
Yi Kong48a619f2018-06-05 16:34:59 -0700836 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800837 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800838 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800839 }
840
Brian Anderson3d4039d2016-09-23 16:31:30 -0700841 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
842
Dan Stoza289ade12014-02-28 11:17:17 -0800843 switch (scalingMode) {
844 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
845 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
846 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
847 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
848 break;
849 default:
850 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800851 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800852 }
853
Dan Stoza8dc55392014-11-04 11:37:46 -0800854 sp<IConsumerListener> frameAvailableListener;
855 sp<IConsumerListener> frameReplacedListener;
856 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700857 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800858 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800859 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200860 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800861
862 if (mCore->mIsAbandoned) {
863 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
864 return NO_INIT;
865 }
866
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700867 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
868 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
869 return NO_INIT;
870 }
871
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800872 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800873 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800874 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800875 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800877 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800879 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800880 } else if (!mSlots[slot].mRequestBufferCalled) {
881 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
882 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800883 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800884 }
885
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700886 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700887 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700888 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700889 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700890 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700891 mSlots[slot].mBufferState.mShared = true;
892 }
893
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800894 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700895 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
896 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
897 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700898 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800899 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800900
901 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
902 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700903 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800904 crop.intersect(bufferRect, &croppedRect);
905 if (croppedRect != crop) {
906 BQ_LOGE("queueBuffer: crop rect is not contained within the "
907 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800908 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800909 }
910
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800911 // Override UNKNOWN dataspace with consumer default
912 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
913 dataSpace = mCore->mDefaultBufferDataSpace;
914 }
915
Brian Andersond6927fb2016-07-23 23:37:30 -0700916 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700917 mSlots[slot].mBufferState.queue();
918
Brian Andersond6927fb2016-07-23 23:37:30 -0700919 // Increment the frame counter and store a local version of it
920 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800921 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700922 currentFrameNumber = mCore->mFrameCounter;
923 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800924
Dan Stoza289ade12014-02-28 11:17:17 -0800925 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
926 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
927 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800928 item.mTransform = transform &
929 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800930 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800931 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
932 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700933 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800934 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800935 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700936 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700937 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700939 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700940 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700941 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700942 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800943 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700944 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700945 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700946 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700947 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800948 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800949
Ruben Brunk1681d952014-06-27 15:51:55 -0700950 mStickyTransform = stickyTransform;
951
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700952 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700953 if (mCore->mSharedBufferMode) {
954 mCore->mSharedBufferCache.crop = crop;
955 mCore->mSharedBufferCache.transform = transform;
956 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700957 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700958 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700959 }
960
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800961 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800962 if (mCore->mQueue.empty()) {
963 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
964 // and simply queue this buffer
965 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800966 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800967 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700968 // When the queue is not empty, we need to look at the last buffer
969 // in the queue to see if we need to replace it
970 const BufferItem& last = mCore->mQueue.itemAt(
971 mCore->mQueue.size() - 1);
972 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800973
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700974 if (!last.mIsStale) {
975 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700976
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700977 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700978 // still be around. Mark it as no longer shared if this
979 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700980 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700981 mSlots[last.mSlot].mBufferState.isFree()) {
982 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700983 }
984 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700985 if (!mSlots[last.mSlot].mBufferState.isShared()) {
986 mCore->mActiveBuffers.erase(last.mSlot);
987 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800988 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700989 }
Dan Stoza289ade12014-02-28 11:17:17 -0800990 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800991
Steven Thomas862e7532019-07-10 19:21:03 -0700992 // Make sure to merge the damage rect from the frame we're about
993 // to drop into the new frame's damage rect.
994 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
995 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
996 item.mSurfaceDamage = Region::INVALID_REGION;
997 } else {
998 item.mSurfaceDamage |= last.mSurfaceDamage;
999 }
1000
Dan Stoza289ade12014-02-28 11:17:17 -08001001 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001002 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001003 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001004 } else {
1005 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001006 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001007 }
1008 }
1009
1010 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001011 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001012 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001013
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001014 output->width = mCore->mDefaultWidth;
1015 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001016 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001017 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1018 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001019
Colin Cross152c3b72016-09-27 14:08:19 -07001020 ATRACE_INT(mCore->mConsumerName.string(),
1021 static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001022#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001023 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001024#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001025 // Take a ticket for the callback functions
1026 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001027
Pablo Ceballos9e314332016-01-12 13:49:19 -08001028 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001029 } // Autolock scope
1030
Irvel468051e2016-06-13 16:44:44 -07001031 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1032 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1033 // there will be no Binder call
1034 if (!mConsumerIsSurfaceFlinger) {
1035 item.mGraphicBuffer.clear();
1036 }
1037
JihCheng Chiu544c5222019-04-02 20:50:58 +08001038 // Update and get FrameEventHistory.
1039 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1040 NewFrameEventsEntry newFrameEventsEntry = {
1041 currentFrameNumber,
1042 postedTime,
1043 requestedPresentTimestamp,
1044 std::move(acquireFenceTime)
1045 };
1046 addAndGetFrameTimestamps(&newFrameEventsEntry,
1047 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1048
Dan Stoza8dc55392014-11-04 11:37:46 -08001049 // Call back without the main BufferQueue lock held, but with the callback
1050 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001051
1052 int connectedApi;
1053 sp<Fence> lastQueuedFence;
1054
1055 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001056 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001057 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001058 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001059 }
1060
Yi Kong48a619f2018-06-05 16:34:59 -07001061 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001062 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001063 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001064 frameReplacedListener->onFrameReplaced(item);
1065 }
1066
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001067 connectedApi = mCore->mConnectedApi;
1068 lastQueuedFence = std::move(mLastQueueBufferFence);
1069
1070 mLastQueueBufferFence = std::move(acquireFence);
1071 mLastQueuedCrop = item.mCrop;
1072 mLastQueuedTransform = item.mTransform;
1073
Dan Stoza8dc55392014-11-04 11:37:46 -08001074 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001075 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001076 }
1077
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001078 // Wait without lock held
1079 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1080 // Waiting here allows for two full buffers to be queued but not a
1081 // third. In the event that frames take varying time, this makes a
1082 // small trade-off in favor of latency rather than throughput.
1083 lastQueuedFence->waitForever("Throttling EGL Production");
1084 }
1085
Dan Stoza289ade12014-02-28 11:17:17 -08001086 return NO_ERROR;
1087}
1088
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001089status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001090 ATRACE_CALL();
1091 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001092 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001093
1094 if (mCore->mIsAbandoned) {
1095 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001096 return NO_INIT;
1097 }
1098
1099 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1100 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1101 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001102 }
1103
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001104 if (mCore->mSharedBufferMode) {
1105 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001106 return BAD_VALUE;
1107 }
1108
Dan Stoza3e96f192014-03-03 10:16:19 -08001109 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001110 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001111 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001112 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001113 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001114 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001115 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001116 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001117 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001118 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001119 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001120 }
1121
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001122 mSlots[slot].mBufferState.cancel();
1123
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001124 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001125 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001126 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001127 mSlots[slot].mBufferState.mShared = false;
1128 }
1129
1130 // Don't put the shared buffer on the free list.
1131 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001132 mCore->mActiveBuffers.erase(slot);
1133 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001134 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001135
Robert Carra0aeedc2020-04-20 15:09:35 -07001136 auto gb = mSlots[slot].mGraphicBuffer;
1137 if (mCore->mConsumerListener != nullptr && gb != nullptr) {
1138 mCore->mConsumerListener->onFrameCancelled(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001139 }
Dan Stoza289ade12014-02-28 11:17:17 -08001140 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001141 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001142 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001143
1144 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001145}
1146
1147int BufferQueueProducer::query(int what, int *outValue) {
1148 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001149 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001150
Yi Kong48a619f2018-06-05 16:34:59 -07001151 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001152 BQ_LOGE("query: outValue was NULL");
1153 return BAD_VALUE;
1154 }
1155
1156 if (mCore->mIsAbandoned) {
1157 BQ_LOGE("query: BufferQueue has been abandoned");
1158 return NO_INIT;
1159 }
1160
1161 int value;
1162 switch (what) {
1163 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001164 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001165 break;
1166 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001167 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001168 break;
1169 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001170 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001171 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001172 case NATIVE_WINDOW_LAYER_COUNT:
1173 // All BufferQueue buffers have a single layer.
1174 value = BQ_LAYER_COUNT;
1175 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001176 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001177 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001178 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001179 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001180 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001181 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001182 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1183 value = (mCore->mQueue.size() > 1);
1184 break;
1185 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001186 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001187 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001188 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001189 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1190 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1191 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001192 case NATIVE_WINDOW_BUFFER_AGE:
1193 if (mCore->mBufferAge > INT32_MAX) {
1194 value = 0;
1195 } else {
1196 value = static_cast<int32_t>(mCore->mBufferAge);
1197 }
1198 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001199 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1200 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1201 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001202 default:
1203 return BAD_VALUE;
1204 }
1205
1206 BQ_LOGV("query: %d? %d", what, value);
1207 *outValue = value;
1208 return NO_ERROR;
1209}
1210
Dan Stozaf0eaf252014-03-21 13:05:51 -07001211status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001212 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1213 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001214 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001215 mConsumerName = mCore->mConsumerName;
1216 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1217 producerControlledByApp ? "true" : "false");
1218
1219 if (mCore->mIsAbandoned) {
1220 BQ_LOGE("connect: BufferQueue has been abandoned");
1221 return NO_INIT;
1222 }
1223
Yi Kong48a619f2018-06-05 16:34:59 -07001224 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001225 BQ_LOGE("connect: BufferQueue has no consumer");
1226 return NO_INIT;
1227 }
1228
Yi Kong48a619f2018-06-05 16:34:59 -07001229 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001230 BQ_LOGE("connect: output was NULL");
1231 return BAD_VALUE;
1232 }
1233
1234 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1235 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1236 mCore->mConnectedApi, api);
1237 return BAD_VALUE;
1238 }
1239
1240 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1241 mDequeueTimeout < 0 ?
1242 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1243 mCore->mMaxBufferCount) -
1244 mCore->getMaxBufferCountLocked();
1245 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1246 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1247 "slots. Delta = %d", delta);
1248 return BAD_VALUE;
1249 }
1250
Dan Stoza289ade12014-02-28 11:17:17 -08001251 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001252 switch (api) {
1253 case NATIVE_WINDOW_API_EGL:
1254 case NATIVE_WINDOW_API_CPU:
1255 case NATIVE_WINDOW_API_MEDIA:
1256 case NATIVE_WINDOW_API_CAMERA:
1257 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001258
1259 output->width = mCore->mDefaultWidth;
1260 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001261 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001262 output->numPendingBuffers =
1263 static_cast<uint32_t>(mCore->mQueue.size());
1264 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001265 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001266 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001267
Yi Kong48a619f2018-06-05 16:34:59 -07001268 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001269 // Set up a death notification so that we can disconnect
1270 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001271#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001272 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001273 status = IInterface::asBinder(listener)->linkToDeath(
1274 static_cast<IBinder::DeathRecipient*>(this));
1275 if (status != NO_ERROR) {
1276 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1277 strerror(-status), status);
1278 }
1279 mCore->mLinkedToDeath = listener;
1280 }
Chong Zhang62493092020-01-15 16:04:47 -08001281#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001282 mCore->mConnectedProducerListener = listener;
1283 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001284 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001285 break;
1286 default:
1287 BQ_LOGE("connect: unknown API %d", api);
1288 status = BAD_VALUE;
1289 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001290 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001291 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001292 mCore->mBufferHasBeenQueued = false;
1293 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001294 mCore->mQueueBufferCanDrop = false;
1295 mCore->mLegacyBufferDrop = true;
1296 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1297 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1298 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001299 }
Dan Stoza289ade12014-02-28 11:17:17 -08001300
Pablo Ceballos981066c2016-02-18 12:54:37 -08001301 mCore->mAllowAllocation = true;
1302 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001303 return status;
1304}
1305
Robert Carr97b9c862016-09-08 13:54:35 -07001306status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001307 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001308 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001309
1310 int status = NO_ERROR;
1311 sp<IConsumerListener> listener;
1312 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001313 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001314
1315 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001316 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001317 return NO_ERROR;
1318 }
1319 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1320 }
1321
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001322 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001323
1324 if (mCore->mIsAbandoned) {
1325 // It's not really an error to disconnect after the surface has
1326 // been abandoned; it should just be a no-op.
1327 return NO_ERROR;
1328 }
1329
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001330 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001331 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1332 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1333 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001334 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001335 // If we're asked to disconnect the currently connected api but
1336 // nobody is connected, it's not really an error.
1337 if (api == BufferQueueCore::NO_CONNECTED_API) {
1338 return NO_ERROR;
1339 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001340 }
1341
Dan Stoza289ade12014-02-28 11:17:17 -08001342 switch (api) {
1343 case NATIVE_WINDOW_API_EGL:
1344 case NATIVE_WINDOW_API_CPU:
1345 case NATIVE_WINDOW_API_MEDIA:
1346 case NATIVE_WINDOW_API_CAMERA:
1347 if (mCore->mConnectedApi == api) {
1348 mCore->freeAllBuffersLocked();
1349
Chong Zhang62493092020-01-15 16:04:47 -08001350#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001351 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001352 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001353 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001354 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001355 // This can fail if we're here because of the death
1356 // notification, but we just ignore it
1357 token->unlinkToDeath(
1358 static_cast<IBinder::DeathRecipient*>(this));
1359 }
Chong Zhang62493092020-01-15 16:04:47 -08001360#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001361 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001362 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001363 mCore->mLinkedToDeath = nullptr;
1364 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001365 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001366 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001367 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001368 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001369 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001370 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001371 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1372 BQ_LOGE("disconnect: not connected (req=%d)", api);
1373 status = NO_INIT;
1374 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001375 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001376 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001377 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001378 }
1379 break;
1380 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001381 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001382 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001383 break;
1384 }
1385 } // Autolock scope
1386
1387 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001388 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001389 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001390 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001391 }
1392
1393 return status;
1394}
1395
Jesse Hall399184a2014-03-03 15:42:54 -08001396status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001397 sp<IConsumerListener> listener;
1398 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001399 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001400 mCore->mSidebandStream = stream;
1401 listener = mCore->mConsumerListener;
1402 } // Autolock scope
1403
Yi Kong48a619f2018-06-05 16:34:59 -07001404 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001405 listener->onSidebandStreamChanged();
1406 }
Jesse Hall399184a2014-03-03 15:42:54 -08001407 return NO_ERROR;
1408}
1409
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001410void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001411 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001412 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001413
1414 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001415 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001416 size_t newBufferCount = 0;
1417 uint32_t allocWidth = 0;
1418 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001419 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001420 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001421 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001422 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001423 std::unique_lock<std::mutex> lock(mCore->mMutex);
1424 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001425
Dan Stoza9de72932015-04-16 17:28:43 -07001426 if (!mCore->mAllowAllocation) {
1427 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1428 "BufferQueue");
1429 return;
1430 }
1431
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001432 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1433 // both allocateBuffers and dequeueBuffer.
1434 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001435 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001436 return;
1437 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001438
Antoine Labour78014f32014-07-15 21:17:03 -07001439 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1440 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001441 if (useDefaultSize && mCore->mAutoPrerotation &&
1442 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1443 std::swap(allocWidth, allocHeight);
1444 }
1445
Antoine Labour78014f32014-07-15 21:17:03 -07001446 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1447 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001448 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001449
1450 mCore->mIsAllocating = true;
1451 } // Autolock scope
1452
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001453 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001454 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001455 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001456 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001457 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001458
1459 status_t result = graphicBuffer->initCheck();
1460
Antoine Labour78014f32014-07-15 21:17:03 -07001461 if (result != NO_ERROR) {
1462 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001463 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001464 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001465 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001466 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001467 return;
1468 }
1469 buffers.push_back(graphicBuffer);
1470 }
1471
1472 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001473 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001474 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1475 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001476 if (useDefaultSize && mCore->mAutoPrerotation &&
1477 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1478 std::swap(checkWidth, checkHeight);
1479 }
1480
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001481 PixelFormat checkFormat = format != 0 ?
1482 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001483 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001484 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1485 checkFormat != allocFormat || checkUsage != allocUsage) {
1486 // Something changed while we released the lock. Retry.
1487 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1488 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001489 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001490 continue;
1491 }
1492
Antoine Labour78014f32014-07-15 21:17:03 -07001493 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001494 if (mCore->mFreeSlots.empty()) {
1495 BQ_LOGV("allocateBuffers: a slot was occupied while "
1496 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001497 continue;
1498 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001499 auto slot = mCore->mFreeSlots.begin();
1500 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1501 mSlots[*slot].mGraphicBuffer = buffers[i];
1502 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001503
1504 // freeBufferLocked puts this slot on the free slots list. Since
1505 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001506 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001507
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001508 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1509 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001510
1511 // Make sure the erase is done after all uses of the slot
1512 // iterator since it will be invalid after this point.
1513 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001514 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001515
Antoine Labour78014f32014-07-15 21:17:03 -07001516 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001517 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001518 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001519
1520 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1521 // waiting anymore so it can use the buffer we just allocated.
1522 while (mDequeueWaitingForAllocation) {
1523 mDequeueWaitingForAllocationCondition.wait(lock);
1524 }
Antoine Labour78014f32014-07-15 21:17:03 -07001525 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001526 }
1527}
1528
Dan Stoza9de72932015-04-16 17:28:43 -07001529status_t BufferQueueProducer::allowAllocation(bool allow) {
1530 ATRACE_CALL();
1531 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1532
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001533 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001534 mCore->mAllowAllocation = allow;
1535 return NO_ERROR;
1536}
1537
Dan Stoza812ed062015-06-02 15:45:22 -07001538status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1539 ATRACE_CALL();
1540 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1541
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001542 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001543 mCore->mGenerationNumber = generationNumber;
1544 return NO_ERROR;
1545}
1546
Dan Stozac6f30bd2015-06-08 09:32:50 -07001547String8 BufferQueueProducer::getConsumerName() const {
1548 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001549 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001550 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1551 return mConsumerName;
1552}
1553
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001554status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001555 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001556 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001557
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001558 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001559 if (!sharedBufferMode) {
1560 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001561 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001562 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001563 return NO_ERROR;
1564}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001565
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001566status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1567 ATRACE_CALL();
1568 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1569
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001570 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001571
1572 mCore->mAutoRefresh = autoRefresh;
1573 return NO_ERROR;
1574}
1575
Dan Stoza127fc632015-06-30 13:43:32 -07001576status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1577 ATRACE_CALL();
1578 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1579
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001580 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001581 bool dequeueBufferCannotBlock =
1582 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1583 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001584 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1585 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1586 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1587 "available slots. Delta = %d", delta);
1588 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001589 }
1590
Pablo Ceballos981066c2016-02-18 12:54:37 -08001591 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001592 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1593 if (timeout > 0) {
1594 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001595 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001596
Pablo Ceballos981066c2016-02-18 12:54:37 -08001597 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001598 return NO_ERROR;
1599}
1600
Sungtak Lee3249fb62019-03-02 16:40:47 -08001601status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1602 ATRACE_CALL();
1603 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1604
1605 std::lock_guard<std::mutex> lock(mCore->mMutex);
1606 mCore->mLegacyBufferDrop = drop;
1607 return NO_ERROR;
1608}
1609
Dan Stoza50101d02016-04-07 16:53:23 -07001610status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001611 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001612 ATRACE_CALL();
1613 BQ_LOGV("getLastQueuedBuffer");
1614
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001615 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001616 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1617 *outBuffer = nullptr;
1618 *outFence = Fence::NO_FENCE;
1619 return NO_ERROR;
1620 }
1621
1622 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1623 *outFence = mLastQueueBufferFence;
1624
John Reck1a61da52016-04-28 13:18:15 -07001625 // Currently only SurfaceFlinger internally ever changes
1626 // GLConsumer's filtering mode, so we just use 'true' here as
1627 // this is slightly specialized for the current client of this API,
1628 // which does want filtering.
1629 GLConsumer::computeTransformMatrix(outTransformMatrix,
1630 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1631 mLastQueuedTransform, true /* filter */);
1632
Dan Stoza50101d02016-04-07 16:53:23 -07001633 return NO_ERROR;
1634}
1635
John Reckf0f13e82021-05-18 00:42:56 -04001636status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1637 Rect* outRect, uint32_t* outTransform) {
1638 ATRACE_CALL();
1639 BQ_LOGV("getLastQueuedBuffer");
1640
1641 std::lock_guard<std::mutex> lock(mCore->mMutex);
1642 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1643 *outBuffer = nullptr;
1644 *outFence = Fence::NO_FENCE;
1645 return NO_ERROR;
1646 }
1647
1648 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1649 *outFence = mLastQueueBufferFence;
1650 *outRect = mLastQueuedCrop;
1651 *outTransform = mLastQueuedTransform;
1652
1653 return NO_ERROR;
1654}
1655
Brian Anderson3890c392016-07-25 12:48:08 -07001656void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1657 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001658}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001659
Brian Anderson3890c392016-07-25 12:48:08 -07001660void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001661 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001662 FrameEventHistoryDelta* outDelta) {
1663 if (newTimestamps == nullptr && outDelta == nullptr) {
1664 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001665 }
1666
1667 ATRACE_CALL();
1668 BQ_LOGV("addAndGetFrameTimestamps");
1669 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001670 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001671 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001672 listener = mCore->mConsumerListener;
1673 }
Yi Kong48a619f2018-06-05 16:34:59 -07001674 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001675 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001676 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001677}
1678
Dan Stoza289ade12014-02-28 11:17:17 -08001679void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1680 // If we're here, it means that a producer we were connected to died.
1681 // We're guaranteed that we are still connected to it because we remove
1682 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1683 // without synchronization here.
1684 int api = mCore->mConnectedApi;
1685 disconnect(api);
1686}
1687
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001688status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1689 BQ_LOGV("getUniqueId");
1690
1691 *outId = mCore->mUniqueId;
1692 return NO_ERROR;
1693}
1694
Chia-I Wue2786ea2017-08-07 10:36:08 -07001695status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1696 BQ_LOGV("getConsumerUsage");
1697
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001698 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001699 *outUsage = mCore->mConsumerUsageBits;
1700 return NO_ERROR;
1701}
1702
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001703status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1704 ATRACE_CALL();
1705 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1706
1707 std::lock_guard<std::mutex> lock(mCore->mMutex);
1708
1709 mCore->mAutoPrerotation = autoPrerotation;
1710 return NO_ERROR;
1711}
1712
Dan Stoza289ade12014-02-28 11:17:17 -08001713} // namespace android