blob: 2d56dd37c2179f2c98fc193dc2fbe2a55061d44b [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) {
122 ATRACE_CALL();
123 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
124 maxDequeuedBuffers);
125
Pablo Ceballos981066c2016-02-18 12:54:37 -0800126 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700127 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200128 std::unique_lock<std::mutex> lock(mCore->mMutex);
129 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130
131 if (mCore->mIsAbandoned) {
132 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
133 "abandoned");
134 return NO_INIT;
135 }
136
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700137 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
138 return NO_ERROR;
139 }
140
Pablo Ceballos72daab62015-12-07 16:38:43 -0800141 // The new maxDequeuedBuffer count should not be violated by the number
142 // of currently dequeued buffers
143 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800144 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700145 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800146 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700147 }
148 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800149 if (dequeuedCount > maxDequeuedBuffers) {
150 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
151 "count (%d) exceeds the current dequeued buffer count (%d)",
152 maxDequeuedBuffers, dequeuedCount);
153 return BAD_VALUE;
154 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700155
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700156 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700157 bufferCount += maxDequeuedBuffers;
158
159 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
160 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
161 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
162 return BAD_VALUE;
163 }
164
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700165 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 if (bufferCount < minBufferSlots) {
167 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
168 "less than minimum %d", bufferCount, minBufferSlots);
169 return BAD_VALUE;
170 }
171
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700172 if (bufferCount > mCore->mMaxBufferCount) {
173 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700174 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
175 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
176 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
177 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700178 return BAD_VALUE;
179 }
180
Pablo Ceballos72daab62015-12-07 16:38:43 -0800181 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800182 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800183 return BAD_VALUE;
184 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700185 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800186 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800187 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800188 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800189 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200190 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700191 } // Autolock scope
192
193 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700194 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800195 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700196 }
197
198 return NO_ERROR;
199}
200
201status_t BufferQueueProducer::setAsyncMode(bool async) {
202 ATRACE_CALL();
203 BQ_LOGV("setAsyncMode: async = %d", async);
204
Pablo Ceballos981066c2016-02-18 12:54:37 -0800205 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700206 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200207 std::unique_lock<std::mutex> lock(mCore->mMutex);
208 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700209
210 if (mCore->mIsAbandoned) {
211 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
212 return NO_INIT;
213 }
214
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700215 if (async == mCore->mAsyncMode) {
216 return NO_ERROR;
217 }
218
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700219 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700220 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
221 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700222 BQ_LOGE("setAsyncMode(%d): this call would cause the "
223 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700224 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
225 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
226 mCore->mMaxDequeuedBufferCount,
227 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700228 return BAD_VALUE;
229 }
230
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800231 int delta = mCore->getMaxBufferCountLocked(async,
232 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
233 - mCore->getMaxBufferCountLocked();
234
Pablo Ceballos981066c2016-02-18 12:54:37 -0800235 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
237 "available slots. Delta = %d", delta);
238 return BAD_VALUE;
239 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700240 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800241 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200242 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700243 if (delta < 0) {
244 listener = mCore->mConsumerListener;
245 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700246 } // Autolock scope
247
248 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700249 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800250 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700251 }
252 return NO_ERROR;
253}
254
Dan Stoza5ecfb682016-01-04 17:01:02 -0800255int BufferQueueProducer::getFreeBufferLocked() const {
256 if (mCore->mFreeBuffers.empty()) {
257 return BufferQueueCore::INVALID_BUFFER_SLOT;
258 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800259 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800260 mCore->mFreeBuffers.pop_front();
261 return slot;
262}
263
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800264int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800265 if (mCore->mFreeSlots.empty()) {
266 return BufferQueueCore::INVALID_BUFFER_SLOT;
267 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800268 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800269 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800270 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800271}
272
273status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200274 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800275 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
276 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 bool tryAgain = true;
278 while (tryAgain) {
279 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800280 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800281 return NO_INIT;
282 }
283
Dan Stoza9f3053d2014-03-06 15:14:33 -0800284 int dequeuedCount = 0;
285 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800286 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700287 if (mSlots[s].mBufferState.isDequeued()) {
288 ++dequeuedCount;
289 }
290 if (mSlots[s].mBufferState.isAcquired()) {
291 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800292 }
293 }
294
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700295 // Producers are not allowed to dequeue more than
296 // mMaxDequeuedBufferCount buffers.
297 // This check is only done if a buffer has already been queued
298 if (mCore->mBufferHasBeenQueued &&
299 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700300 // Supress error logs when timeout is non-negative.
301 if (mDequeueTimeout < 0) {
302 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
303 "count (%d)", callerString,
304 mCore->mMaxDequeuedBufferCount);
305 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800306 return INVALID_OPERATION;
307 }
308
Dan Stoza0de7ea72015-04-23 13:20:51 -0700309 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
310
Dan Stozaae3c3682014-04-18 15:43:35 -0700311 // If we disconnect and reconnect quickly, we can be in a state where
312 // our slots are empty but we have many buffers in the queue. This can
313 // cause us to run out of memory if we outrun the consumer. Wait here if
314 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800315 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700316 bool tooManyBuffers = mCore->mQueue.size()
317 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700318 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800319 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700320 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700321 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700322 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700323 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700324 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700325 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700326 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800327 } else {
328 if (caller == FreeSlotCaller::Dequeue) {
329 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800330 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800331 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
332 *found = slot;
333 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800334 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800335 }
336 } else {
337 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800338 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800339 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
340 *found = slot;
341 } else {
342 *found = getFreeBufferLocked();
343 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700344 }
345 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700346 }
347
348 // If no buffer is found, or if the queue has too many buffers
349 // outstanding, wait for a buffer to be acquired or released, or for the
350 // max buffer count to change.
351 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
352 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800353 if (tryAgain) {
354 // Return an error if we're in non-blocking mode (producer and
355 // consumer are controlled by the application).
356 // However, the consumer is allowed to briefly acquire an extra
357 // buffer (which could cause us to have to wait here), which is
358 // okay, since it is only used to implement an atomic acquire +
359 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700360 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800361 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
362 return WOULD_BLOCK;
363 }
Dan Stoza127fc632015-06-30 13:43:32 -0700364 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200365 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
366 std::chrono::nanoseconds(mDequeueTimeout));
367 if (result == std::cv_status::timeout) {
368 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700369 }
370 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200371 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700372 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 }
374 } // while (tryAgain)
375
376 return NO_ERROR;
377}
378
Ian Elliottd11b0442017-07-18 11:05:49 -0600379status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
380 uint32_t width, uint32_t height, PixelFormat format,
381 uint64_t usage, uint64_t* outBufferAge,
382 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800383 ATRACE_CALL();
384 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200385 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800386 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700387
388 if (mCore->mIsAbandoned) {
389 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
390 return NO_INIT;
391 }
392
393 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
394 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
395 return NO_INIT;
396 }
Dan Stoza289ade12014-02-28 11:17:17 -0800397 } // Autolock scope
398
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700399 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800400
401 if ((width && !height) || (!width && height)) {
402 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
403 return BAD_VALUE;
404 }
405
406 status_t returnFlags = NO_ERROR;
407 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
408 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800409 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800410
411 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200412 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800413
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100414 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
415 // is finished such that we don't allocate in parallel.
416 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
417 mDequeueWaitingForAllocation = true;
418 mCore->waitWhileAllocatingLocked(lock);
419 mDequeueWaitingForAllocation = false;
420 mDequeueWaitingForAllocationCondition.notify_all();
421 }
Dan Stoza289ade12014-02-28 11:17:17 -0800422
423 if (format == 0) {
424 format = mCore->mDefaultBufferFormat;
425 }
426
427 // Enable the usage bits the consumer requested
428 usage |= mCore->mConsumerUsageBits;
429
Dan Stoza9de72932015-04-16 17:28:43 -0700430 const bool useDefaultSize = !width && !height;
431 if (useDefaultSize) {
432 width = mCore->mDefaultWidth;
433 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700434 if (mCore->mAutoPrerotation &&
435 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
436 std::swap(width, height);
437 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800438 }
Dan Stoza289ade12014-02-28 11:17:17 -0800439
Pablo Ceballos981066c2016-02-18 12:54:37 -0800440 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700441 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200442 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700443 if (status != NO_ERROR) {
444 return status;
445 }
446
447 // This should not happen
448 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
449 BQ_LOGE("dequeueBuffer: no available buffer slots");
450 return -EBUSY;
451 }
452
453 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
454
455 // If we are not allowed to allocate new buffers,
456 // waitForFreeSlotThenRelock must have returned a slot containing a
457 // buffer. If this buffer would require reallocation to meet the
458 // requested attributes, we free it and attempt to get another one.
459 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700460 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700461 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700462 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700463 return BAD_VALUE;
464 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800465 mCore->mFreeSlots.insert(found);
466 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700467 found = BufferItem::INVALID_BUFFER_SLOT;
468 continue;
469 }
470 }
Dan Stoza289ade12014-02-28 11:17:17 -0800471 }
472
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800473 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700474 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700475 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800476 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
477 "buffer");
478
479 return BAD_VALUE;
480 }
481
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700482 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800483 mCore->mActiveBuffers.insert(found);
484 }
Dan Stoza289ade12014-02-28 11:17:17 -0800485 *outSlot = found;
486 ATRACE_BUFFER_INDEX(found);
487
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800488 attachedByConsumer = mSlots[found].mNeedsReallocation;
489 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800490
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700491 mSlots[found].mBufferState.dequeue();
492
Yi Kong48a619f2018-06-05 16:34:59 -0700493 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700494 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800495 {
496 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700497 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800498 mSlots[found].mRequestBufferCalled = false;
499 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
500 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
501 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800502 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700503 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800504
505 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800506 } else {
507 // We add 1 because that will be the frame number when this buffer
508 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700509 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800510 }
511
Dan Stoza800b41a2015-04-28 14:20:04 -0700512 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
513 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800514
Yi Kong48a619f2018-06-05 16:34:59 -0700515 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800516 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
517 "slot=%d w=%d h=%d format=%u",
518 found, buffer->width, buffer->height, buffer->format);
519 }
520
521 eglDisplay = mSlots[found].mEglDisplay;
522 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700523 // Don't return a fence in shared buffer mode, except for the first
524 // frame.
525 *outFence = (mCore->mSharedBufferMode &&
526 mCore->mSharedBufferSlot == found) ?
527 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800528 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
529 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700530
531 // If shared buffer mode has just been enabled, cache the slot of the
532 // first buffer that is dequeued and mark it as the shared buffer.
533 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
534 BufferQueueCore::INVALID_BUFFER_SLOT) {
535 mCore->mSharedBufferSlot = found;
536 mSlots[found].mBufferState.mShared = true;
537 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700538
539 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
540 if (mCore->mConsumerListener != nullptr) {
541 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
542 }
543 }
Dan Stoza289ade12014-02-28 11:17:17 -0800544 } // Autolock scope
545
546 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700547 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700548 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700549 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700550 {mConsumerName.string(), mConsumerName.size()});
551
552 status_t error = graphicBuffer->initCheck();
553
Dan Stoza289ade12014-02-28 11:17:17 -0800554 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200555 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800556
Chia-I Wufeec3b12017-05-25 09:34:56 -0700557 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700558 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
559 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Adithya Srinivasana820af92019-11-01 13:55:17 -0700560 if (mCore->mConsumerListener != nullptr) {
561 mCore->mConsumerListener->onFrameDequeued(
562 mSlots[*outSlot].mGraphicBuffer->getId());
563 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700564 }
565
566 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200567 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700568
Chia-I Wufeec3b12017-05-25 09:34:56 -0700569 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700570 mCore->mFreeSlots.insert(*outSlot);
571 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700572 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
573 return error;
574 }
575
Dan Stoza289ade12014-02-28 11:17:17 -0800576 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700577 mCore->mFreeSlots.insert(*outSlot);
578 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800579 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
580 return NO_INIT;
581 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800582
Pablo Ceballos9e314332016-01-12 13:49:19 -0800583 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800584 } // Autolock scope
585 }
586
Dan Stoza9f3053d2014-03-06 15:14:33 -0800587 if (attachedByConsumer) {
588 returnFlags |= BUFFER_NEEDS_REALLOCATION;
589 }
590
Dan Stoza289ade12014-02-28 11:17:17 -0800591 if (eglFence != EGL_NO_SYNC_KHR) {
592 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
593 1000000000);
594 // If something goes wrong, log the error, but return the buffer without
595 // synchronizing access to it. It's too late at this point to abort the
596 // dequeue operation.
597 if (result == EGL_FALSE) {
598 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
599 eglGetError());
600 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
601 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
602 }
603 eglDestroySyncKHR(eglDisplay, eglFence);
604 }
605
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700606 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
607 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800608 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800609 mSlots[*outSlot].mGraphicBuffer != nullptr ?
610 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800611
Ian Elliottd11b0442017-07-18 11:05:49 -0600612 if (outBufferAge) {
613 *outBufferAge = mCore->mBufferAge;
614 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700615 addAndGetFrameTimestamps(nullptr, outTimestamps);
616
Dan Stoza289ade12014-02-28 11:17:17 -0800617 return returnFlags;
618}
619
Dan Stoza9f3053d2014-03-06 15:14:33 -0800620status_t BufferQueueProducer::detachBuffer(int slot) {
621 ATRACE_CALL();
622 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700623 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800624
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700625 sp<IConsumerListener> listener;
626 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200627 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700628
629 if (mCore->mIsAbandoned) {
630 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
631 return NO_INIT;
632 }
633
634 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
635 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
636 return NO_INIT;
637 }
638
639 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
640 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
641 return BAD_VALUE;
642 }
643
644 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
645 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
646 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
647 return BAD_VALUE;
648 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000649 // TODO(http://b/140581935): This message is BQ_LOGW because it
650 // often logs when no actionable errors are present. Return to
651 // using BQ_LOGE after ensuring this only logs during errors.
652 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700653 "(state = %s)", slot, mSlots[slot].mBufferState.string());
654 return BAD_VALUE;
655 } else if (!mSlots[slot].mRequestBufferCalled) {
656 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
657 slot);
658 return BAD_VALUE;
659 }
660
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700661 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700662 auto gb = mSlots[slot].mGraphicBuffer;
663 if (listener != nullptr && gb != nullptr) {
664 listener->onFrameDetached(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700665 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700666 mSlots[slot].mBufferState.detachProducer();
667 mCore->mActiveBuffers.erase(slot);
668 mCore->mFreeSlots.insert(slot);
669 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200670 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700671 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800672 }
673
Yi Kong48a619f2018-06-05 16:34:59 -0700674 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700675 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700676 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800677
Dan Stoza9f3053d2014-03-06 15:14:33 -0800678 return NO_ERROR;
679}
680
Dan Stozad9822a32014-03-28 15:25:31 -0700681status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
682 sp<Fence>* outFence) {
683 ATRACE_CALL();
684
Yi Kong48a619f2018-06-05 16:34:59 -0700685 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700686 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
687 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700688 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700689 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
690 return BAD_VALUE;
691 }
692
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700693 sp<IConsumerListener> listener;
694 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200695 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700696
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700697 if (mCore->mIsAbandoned) {
698 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
699 return NO_INIT;
700 }
701
702 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
703 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
704 return NO_INIT;
705 }
706
707 if (mCore->mSharedBufferMode) {
708 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
709 "mode");
710 return BAD_VALUE;
711 }
712
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200713 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700714
715 if (mCore->mFreeBuffers.empty()) {
716 return NO_MEMORY;
717 }
718
719 int found = mCore->mFreeBuffers.front();
720 mCore->mFreeBuffers.remove(found);
721 mCore->mFreeSlots.insert(found);
722
723 BQ_LOGV("detachNextBuffer detached slot %d", found);
724
725 *outBuffer = mSlots[found].mGraphicBuffer;
726 *outFence = mSlots[found].mFence;
727 mCore->clearBufferSlotLocked(found);
728 VALIDATE_CONSISTENCY();
729 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700730 }
731
Yi Kong48a619f2018-06-05 16:34:59 -0700732 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700733 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700734 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800735
Dan Stozad9822a32014-03-28 15:25:31 -0700736 return NO_ERROR;
737}
738
Dan Stoza9f3053d2014-03-06 15:14:33 -0800739status_t BufferQueueProducer::attachBuffer(int* outSlot,
740 const sp<android::GraphicBuffer>& buffer) {
741 ATRACE_CALL();
742
Yi Kong48a619f2018-06-05 16:34:59 -0700743 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700744 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800745 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700746 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700747 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800748 return BAD_VALUE;
749 }
750
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200751 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700752
753 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700754 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700755 return NO_INIT;
756 }
757
758 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700759 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700760 return NO_INIT;
761 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800762
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700763 if (mCore->mSharedBufferMode) {
764 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700765 return BAD_VALUE;
766 }
767
Dan Stoza812ed062015-06-02 15:45:22 -0700768 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
769 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
770 "[queue %u]", buffer->getGenerationNumber(),
771 mCore->mGenerationNumber);
772 return BAD_VALUE;
773 }
774
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200775 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700776
Dan Stoza9f3053d2014-03-06 15:14:33 -0800777 status_t returnFlags = NO_ERROR;
778 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200779 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800780 if (status != NO_ERROR) {
781 return status;
782 }
783
784 // This should not happen
785 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700786 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800787 return -EBUSY;
788 }
789
790 *outSlot = found;
791 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700792 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800793 *outSlot, returnFlags);
794
795 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700796 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800797 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
798 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700799 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800800 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800801 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800802 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800803 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700804
Dan Stoza9f3053d2014-03-06 15:14:33 -0800805 return returnFlags;
806}
807
Dan Stoza289ade12014-02-28 11:17:17 -0800808status_t BufferQueueProducer::queueBuffer(int slot,
809 const QueueBufferInput &input, QueueBufferOutput *output) {
810 ATRACE_CALL();
811 ATRACE_BUFFER_INDEX(slot);
812
Brian Andersond6927fb2016-07-23 23:37:30 -0700813 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800814 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800815 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700816 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800817 int scalingMode;
818 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700819 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700820 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700821 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700822 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700823 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
824 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700825 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700826 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800827
Yi Kong48a619f2018-06-05 16:34:59 -0700828 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800829 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800830 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800831 }
832
Brian Anderson3d4039d2016-09-23 16:31:30 -0700833 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
834
Dan Stoza289ade12014-02-28 11:17:17 -0800835 switch (scalingMode) {
836 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
837 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
838 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
839 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
840 break;
841 default:
842 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800843 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800844 }
845
Dan Stoza8dc55392014-11-04 11:37:46 -0800846 sp<IConsumerListener> frameAvailableListener;
847 sp<IConsumerListener> frameReplacedListener;
848 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800850 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800851 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200852 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800853
854 if (mCore->mIsAbandoned) {
855 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
856 return NO_INIT;
857 }
858
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700859 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
860 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
861 return NO_INIT;
862 }
863
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800864 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800865 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800866 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800867 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700868 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800869 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700870 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800871 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800872 } else if (!mSlots[slot].mRequestBufferCalled) {
873 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
874 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800875 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800876 }
877
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700878 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700879 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700880 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700881 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700882 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700883 mSlots[slot].mBufferState.mShared = true;
884 }
885
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800886 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700887 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
888 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
889 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700890 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800891 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800892
893 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
894 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700895 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800896 crop.intersect(bufferRect, &croppedRect);
897 if (croppedRect != crop) {
898 BQ_LOGE("queueBuffer: crop rect is not contained within the "
899 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800900 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800901 }
902
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800903 // Override UNKNOWN dataspace with consumer default
904 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
905 dataSpace = mCore->mDefaultBufferDataSpace;
906 }
907
Brian Andersond6927fb2016-07-23 23:37:30 -0700908 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700909 mSlots[slot].mBufferState.queue();
910
Brian Andersond6927fb2016-07-23 23:37:30 -0700911 // Increment the frame counter and store a local version of it
912 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800913 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700914 currentFrameNumber = mCore->mFrameCounter;
915 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800916
Dan Stoza289ade12014-02-28 11:17:17 -0800917 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
918 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
919 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800920 item.mTransform = transform &
921 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800922 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800923 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
924 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700925 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800926 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800927 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700928 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700929 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700931 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700932 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700933 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700934 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800935 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700936 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700937 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700938 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700939 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800940 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800941
Ruben Brunk1681d952014-06-27 15:51:55 -0700942 mStickyTransform = stickyTransform;
943
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700944 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700945 if (mCore->mSharedBufferMode) {
946 mCore->mSharedBufferCache.crop = crop;
947 mCore->mSharedBufferCache.transform = transform;
948 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700949 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700950 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700951 }
952
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800953 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800954 if (mCore->mQueue.empty()) {
955 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
956 // and simply queue this buffer
957 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800958 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800959 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700960 // When the queue is not empty, we need to look at the last buffer
961 // in the queue to see if we need to replace it
962 const BufferItem& last = mCore->mQueue.itemAt(
963 mCore->mQueue.size() - 1);
964 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800965
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700966 if (!last.mIsStale) {
967 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700968
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700969 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700970 // still be around. Mark it as no longer shared if this
971 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700972 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700973 mSlots[last.mSlot].mBufferState.isFree()) {
974 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700975 }
976 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700977 if (!mSlots[last.mSlot].mBufferState.isShared()) {
978 mCore->mActiveBuffers.erase(last.mSlot);
979 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800980 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700981 }
Dan Stoza289ade12014-02-28 11:17:17 -0800982 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800983
Steven Thomas862e7532019-07-10 19:21:03 -0700984 // Make sure to merge the damage rect from the frame we're about
985 // to drop into the new frame's damage rect.
986 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
987 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
988 item.mSurfaceDamage = Region::INVALID_REGION;
989 } else {
990 item.mSurfaceDamage |= last.mSurfaceDamage;
991 }
992
Dan Stoza289ade12014-02-28 11:17:17 -0800993 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700994 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800995 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800996 } else {
997 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800998 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800999 }
1000 }
1001
1002 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001003 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001004 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001005
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001006 output->width = mCore->mDefaultWidth;
1007 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001008 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001009 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1010 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001011
Colin Cross152c3b72016-09-27 14:08:19 -07001012 ATRACE_INT(mCore->mConsumerName.string(),
1013 static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001014#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001015 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001016#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001017 // Take a ticket for the callback functions
1018 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001019
Pablo Ceballos9e314332016-01-12 13:49:19 -08001020 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001021 } // Autolock scope
1022
Irvel468051e2016-06-13 16:44:44 -07001023 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1024 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1025 // there will be no Binder call
1026 if (!mConsumerIsSurfaceFlinger) {
1027 item.mGraphicBuffer.clear();
1028 }
1029
JihCheng Chiu544c5222019-04-02 20:50:58 +08001030 // Update and get FrameEventHistory.
1031 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1032 NewFrameEventsEntry newFrameEventsEntry = {
1033 currentFrameNumber,
1034 postedTime,
1035 requestedPresentTimestamp,
1036 std::move(acquireFenceTime)
1037 };
1038 addAndGetFrameTimestamps(&newFrameEventsEntry,
1039 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1040
Dan Stoza8dc55392014-11-04 11:37:46 -08001041 // Call back without the main BufferQueue lock held, but with the callback
1042 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001043
1044 int connectedApi;
1045 sp<Fence> lastQueuedFence;
1046
1047 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001048 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001049 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001050 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001051 }
1052
Yi Kong48a619f2018-06-05 16:34:59 -07001053 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001054 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001055 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001056 frameReplacedListener->onFrameReplaced(item);
1057 }
1058
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001059 connectedApi = mCore->mConnectedApi;
1060 lastQueuedFence = std::move(mLastQueueBufferFence);
1061
1062 mLastQueueBufferFence = std::move(acquireFence);
1063 mLastQueuedCrop = item.mCrop;
1064 mLastQueuedTransform = item.mTransform;
1065
Dan Stoza8dc55392014-11-04 11:37:46 -08001066 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001067 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001068 }
1069
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001070 // Wait without lock held
1071 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1072 // Waiting here allows for two full buffers to be queued but not a
1073 // third. In the event that frames take varying time, this makes a
1074 // small trade-off in favor of latency rather than throughput.
1075 lastQueuedFence->waitForever("Throttling EGL Production");
1076 }
1077
Dan Stoza289ade12014-02-28 11:17:17 -08001078 return NO_ERROR;
1079}
1080
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001081status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001082 ATRACE_CALL();
1083 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001084 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001085
1086 if (mCore->mIsAbandoned) {
1087 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001088 return NO_INIT;
1089 }
1090
1091 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1092 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1093 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001094 }
1095
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001096 if (mCore->mSharedBufferMode) {
1097 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001098 return BAD_VALUE;
1099 }
1100
Dan Stoza3e96f192014-03-03 10:16:19 -08001101 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001102 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001103 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001104 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001105 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001106 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001107 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001108 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001109 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001110 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001111 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001112 }
1113
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001114 mSlots[slot].mBufferState.cancel();
1115
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001116 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001117 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001118 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001119 mSlots[slot].mBufferState.mShared = false;
1120 }
1121
1122 // Don't put the shared buffer on the free list.
1123 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001124 mCore->mActiveBuffers.erase(slot);
1125 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001126 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001127
Robert Carra0aeedc2020-04-20 15:09:35 -07001128 auto gb = mSlots[slot].mGraphicBuffer;
1129 if (mCore->mConsumerListener != nullptr && gb != nullptr) {
1130 mCore->mConsumerListener->onFrameCancelled(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001131 }
Dan Stoza289ade12014-02-28 11:17:17 -08001132 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001133 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001134 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001135
1136 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001137}
1138
1139int BufferQueueProducer::query(int what, int *outValue) {
1140 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001141 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001142
Yi Kong48a619f2018-06-05 16:34:59 -07001143 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001144 BQ_LOGE("query: outValue was NULL");
1145 return BAD_VALUE;
1146 }
1147
1148 if (mCore->mIsAbandoned) {
1149 BQ_LOGE("query: BufferQueue has been abandoned");
1150 return NO_INIT;
1151 }
1152
1153 int value;
1154 switch (what) {
1155 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001156 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001157 break;
1158 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001159 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001160 break;
1161 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001162 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001163 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001164 case NATIVE_WINDOW_LAYER_COUNT:
1165 // All BufferQueue buffers have a single layer.
1166 value = BQ_LAYER_COUNT;
1167 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001168 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001169 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001170 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001171 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001172 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001173 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001174 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1175 value = (mCore->mQueue.size() > 1);
1176 break;
1177 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001178 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001179 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001180 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001181 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1182 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1183 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001184 case NATIVE_WINDOW_BUFFER_AGE:
1185 if (mCore->mBufferAge > INT32_MAX) {
1186 value = 0;
1187 } else {
1188 value = static_cast<int32_t>(mCore->mBufferAge);
1189 }
1190 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001191 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1192 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1193 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001194 default:
1195 return BAD_VALUE;
1196 }
1197
1198 BQ_LOGV("query: %d? %d", what, value);
1199 *outValue = value;
1200 return NO_ERROR;
1201}
1202
Dan Stozaf0eaf252014-03-21 13:05:51 -07001203status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001204 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1205 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001206 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001207 mConsumerName = mCore->mConsumerName;
1208 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1209 producerControlledByApp ? "true" : "false");
1210
1211 if (mCore->mIsAbandoned) {
1212 BQ_LOGE("connect: BufferQueue has been abandoned");
1213 return NO_INIT;
1214 }
1215
Yi Kong48a619f2018-06-05 16:34:59 -07001216 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001217 BQ_LOGE("connect: BufferQueue has no consumer");
1218 return NO_INIT;
1219 }
1220
Yi Kong48a619f2018-06-05 16:34:59 -07001221 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001222 BQ_LOGE("connect: output was NULL");
1223 return BAD_VALUE;
1224 }
1225
1226 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1227 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1228 mCore->mConnectedApi, api);
1229 return BAD_VALUE;
1230 }
1231
1232 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1233 mDequeueTimeout < 0 ?
1234 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1235 mCore->mMaxBufferCount) -
1236 mCore->getMaxBufferCountLocked();
1237 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1238 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1239 "slots. Delta = %d", delta);
1240 return BAD_VALUE;
1241 }
1242
Dan Stoza289ade12014-02-28 11:17:17 -08001243 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001244 switch (api) {
1245 case NATIVE_WINDOW_API_EGL:
1246 case NATIVE_WINDOW_API_CPU:
1247 case NATIVE_WINDOW_API_MEDIA:
1248 case NATIVE_WINDOW_API_CAMERA:
1249 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001250
1251 output->width = mCore->mDefaultWidth;
1252 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001253 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001254 output->numPendingBuffers =
1255 static_cast<uint32_t>(mCore->mQueue.size());
1256 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001257 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001258 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001259
Yi Kong48a619f2018-06-05 16:34:59 -07001260 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001261 // Set up a death notification so that we can disconnect
1262 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001263#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001264 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001265 status = IInterface::asBinder(listener)->linkToDeath(
1266 static_cast<IBinder::DeathRecipient*>(this));
1267 if (status != NO_ERROR) {
1268 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1269 strerror(-status), status);
1270 }
1271 mCore->mLinkedToDeath = listener;
1272 }
Chong Zhang62493092020-01-15 16:04:47 -08001273#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001274 mCore->mConnectedProducerListener = listener;
1275 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001276 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001277 break;
1278 default:
1279 BQ_LOGE("connect: unknown API %d", api);
1280 status = BAD_VALUE;
1281 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001282 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001283 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001284 mCore->mBufferHasBeenQueued = false;
1285 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001286 mCore->mQueueBufferCanDrop = false;
1287 mCore->mLegacyBufferDrop = true;
1288 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1289 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1290 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001291 }
Dan Stoza289ade12014-02-28 11:17:17 -08001292
Pablo Ceballos981066c2016-02-18 12:54:37 -08001293 mCore->mAllowAllocation = true;
1294 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001295 return status;
1296}
1297
Robert Carr97b9c862016-09-08 13:54:35 -07001298status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001299 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001300 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001301
1302 int status = NO_ERROR;
1303 sp<IConsumerListener> listener;
1304 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001305 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001306
1307 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001308 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001309 return NO_ERROR;
1310 }
1311 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1312 }
1313
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001314 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001315
1316 if (mCore->mIsAbandoned) {
1317 // It's not really an error to disconnect after the surface has
1318 // been abandoned; it should just be a no-op.
1319 return NO_ERROR;
1320 }
1321
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001322 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001323 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1324 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1325 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001326 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001327 // If we're asked to disconnect the currently connected api but
1328 // nobody is connected, it's not really an error.
1329 if (api == BufferQueueCore::NO_CONNECTED_API) {
1330 return NO_ERROR;
1331 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001332 }
1333
Dan Stoza289ade12014-02-28 11:17:17 -08001334 switch (api) {
1335 case NATIVE_WINDOW_API_EGL:
1336 case NATIVE_WINDOW_API_CPU:
1337 case NATIVE_WINDOW_API_MEDIA:
1338 case NATIVE_WINDOW_API_CAMERA:
1339 if (mCore->mConnectedApi == api) {
1340 mCore->freeAllBuffersLocked();
1341
Chong Zhang62493092020-01-15 16:04:47 -08001342#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001343 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001344 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001345 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001346 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001347 // This can fail if we're here because of the death
1348 // notification, but we just ignore it
1349 token->unlinkToDeath(
1350 static_cast<IBinder::DeathRecipient*>(this));
1351 }
Chong Zhang62493092020-01-15 16:04:47 -08001352#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001353 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001354 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001355 mCore->mLinkedToDeath = nullptr;
1356 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001357 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001358 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001359 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001360 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001361 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001362 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001363 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1364 BQ_LOGE("disconnect: not connected (req=%d)", api);
1365 status = NO_INIT;
1366 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001367 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001368 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001369 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001370 }
1371 break;
1372 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001373 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001374 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001375 break;
1376 }
1377 } // Autolock scope
1378
1379 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001380 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001381 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001382 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001383 }
1384
1385 return status;
1386}
1387
Jesse Hall399184a2014-03-03 15:42:54 -08001388status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001389 sp<IConsumerListener> listener;
1390 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001391 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001392 mCore->mSidebandStream = stream;
1393 listener = mCore->mConsumerListener;
1394 } // Autolock scope
1395
Yi Kong48a619f2018-06-05 16:34:59 -07001396 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001397 listener->onSidebandStreamChanged();
1398 }
Jesse Hall399184a2014-03-03 15:42:54 -08001399 return NO_ERROR;
1400}
1401
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001402void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001403 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001404 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001405
1406 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001407 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001408 size_t newBufferCount = 0;
1409 uint32_t allocWidth = 0;
1410 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001411 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001412 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001413 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001414 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001415 std::unique_lock<std::mutex> lock(mCore->mMutex);
1416 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001417
Dan Stoza9de72932015-04-16 17:28:43 -07001418 if (!mCore->mAllowAllocation) {
1419 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1420 "BufferQueue");
1421 return;
1422 }
1423
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001424 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1425 // both allocateBuffers and dequeueBuffer.
1426 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001427 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001428 return;
1429 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001430
Antoine Labour78014f32014-07-15 21:17:03 -07001431 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1432 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001433 if (useDefaultSize && mCore->mAutoPrerotation &&
1434 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1435 std::swap(allocWidth, allocHeight);
1436 }
1437
Antoine Labour78014f32014-07-15 21:17:03 -07001438 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1439 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001440 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001441
1442 mCore->mIsAllocating = true;
1443 } // Autolock scope
1444
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001445 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001446 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001447 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001448 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001449 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001450
1451 status_t result = graphicBuffer->initCheck();
1452
Antoine Labour78014f32014-07-15 21:17:03 -07001453 if (result != NO_ERROR) {
1454 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001455 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001456 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001457 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001458 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001459 return;
1460 }
1461 buffers.push_back(graphicBuffer);
1462 }
1463
1464 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001465 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001466 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1467 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001468 if (useDefaultSize && mCore->mAutoPrerotation &&
1469 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1470 std::swap(checkWidth, checkHeight);
1471 }
1472
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001473 PixelFormat checkFormat = format != 0 ?
1474 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001475 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001476 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1477 checkFormat != allocFormat || checkUsage != allocUsage) {
1478 // Something changed while we released the lock. Retry.
1479 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1480 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001481 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001482 continue;
1483 }
1484
Antoine Labour78014f32014-07-15 21:17:03 -07001485 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001486 if (mCore->mFreeSlots.empty()) {
1487 BQ_LOGV("allocateBuffers: a slot was occupied while "
1488 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001489 continue;
1490 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001491 auto slot = mCore->mFreeSlots.begin();
1492 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1493 mSlots[*slot].mGraphicBuffer = buffers[i];
1494 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001495
1496 // freeBufferLocked puts this slot on the free slots list. Since
1497 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001498 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001499
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001500 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1501 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001502
1503 // Make sure the erase is done after all uses of the slot
1504 // iterator since it will be invalid after this point.
1505 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001506 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001507
Antoine Labour78014f32014-07-15 21:17:03 -07001508 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001509 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001510 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001511
1512 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1513 // waiting anymore so it can use the buffer we just allocated.
1514 while (mDequeueWaitingForAllocation) {
1515 mDequeueWaitingForAllocationCondition.wait(lock);
1516 }
Antoine Labour78014f32014-07-15 21:17:03 -07001517 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001518 }
1519}
1520
Dan Stoza9de72932015-04-16 17:28:43 -07001521status_t BufferQueueProducer::allowAllocation(bool allow) {
1522 ATRACE_CALL();
1523 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1524
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001525 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001526 mCore->mAllowAllocation = allow;
1527 return NO_ERROR;
1528}
1529
Dan Stoza812ed062015-06-02 15:45:22 -07001530status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1531 ATRACE_CALL();
1532 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1533
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001534 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001535 mCore->mGenerationNumber = generationNumber;
1536 return NO_ERROR;
1537}
1538
Dan Stozac6f30bd2015-06-08 09:32:50 -07001539String8 BufferQueueProducer::getConsumerName() const {
1540 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001541 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001542 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1543 return mConsumerName;
1544}
1545
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001546status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001547 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001548 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001549
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001550 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001551 if (!sharedBufferMode) {
1552 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001553 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001554 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001555 return NO_ERROR;
1556}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001557
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001558status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1559 ATRACE_CALL();
1560 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1561
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001562 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001563
1564 mCore->mAutoRefresh = autoRefresh;
1565 return NO_ERROR;
1566}
1567
Dan Stoza127fc632015-06-30 13:43:32 -07001568status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1569 ATRACE_CALL();
1570 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1571
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001572 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001573 bool dequeueBufferCannotBlock =
1574 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1575 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001576 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1577 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1578 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1579 "available slots. Delta = %d", delta);
1580 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001581 }
1582
Pablo Ceballos981066c2016-02-18 12:54:37 -08001583 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001584 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1585 if (timeout > 0) {
1586 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001587 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001588
Pablo Ceballos981066c2016-02-18 12:54:37 -08001589 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001590 return NO_ERROR;
1591}
1592
Sungtak Lee3249fb62019-03-02 16:40:47 -08001593status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1594 ATRACE_CALL();
1595 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1596
1597 std::lock_guard<std::mutex> lock(mCore->mMutex);
1598 mCore->mLegacyBufferDrop = drop;
1599 return NO_ERROR;
1600}
1601
Dan Stoza50101d02016-04-07 16:53:23 -07001602status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001603 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001604 ATRACE_CALL();
1605 BQ_LOGV("getLastQueuedBuffer");
1606
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001607 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001608 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1609 *outBuffer = nullptr;
1610 *outFence = Fence::NO_FENCE;
1611 return NO_ERROR;
1612 }
1613
1614 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1615 *outFence = mLastQueueBufferFence;
1616
John Reck1a61da52016-04-28 13:18:15 -07001617 // Currently only SurfaceFlinger internally ever changes
1618 // GLConsumer's filtering mode, so we just use 'true' here as
1619 // this is slightly specialized for the current client of this API,
1620 // which does want filtering.
1621 GLConsumer::computeTransformMatrix(outTransformMatrix,
1622 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1623 mLastQueuedTransform, true /* filter */);
1624
Dan Stoza50101d02016-04-07 16:53:23 -07001625 return NO_ERROR;
1626}
1627
John Reckf0f13e82021-05-18 00:42:56 -04001628status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1629 Rect* outRect, uint32_t* outTransform) {
1630 ATRACE_CALL();
1631 BQ_LOGV("getLastQueuedBuffer");
1632
1633 std::lock_guard<std::mutex> lock(mCore->mMutex);
1634 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1635 *outBuffer = nullptr;
1636 *outFence = Fence::NO_FENCE;
1637 return NO_ERROR;
1638 }
1639
1640 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1641 *outFence = mLastQueueBufferFence;
1642 *outRect = mLastQueuedCrop;
1643 *outTransform = mLastQueuedTransform;
1644
1645 return NO_ERROR;
1646}
1647
Brian Anderson3890c392016-07-25 12:48:08 -07001648void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1649 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001650}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001651
Brian Anderson3890c392016-07-25 12:48:08 -07001652void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001653 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001654 FrameEventHistoryDelta* outDelta) {
1655 if (newTimestamps == nullptr && outDelta == nullptr) {
1656 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001657 }
1658
1659 ATRACE_CALL();
1660 BQ_LOGV("addAndGetFrameTimestamps");
1661 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001662 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001663 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001664 listener = mCore->mConsumerListener;
1665 }
Yi Kong48a619f2018-06-05 16:34:59 -07001666 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001667 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001668 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001669}
1670
Dan Stoza289ade12014-02-28 11:17:17 -08001671void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1672 // If we're here, it means that a producer we were connected to died.
1673 // We're guaranteed that we are still connected to it because we remove
1674 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1675 // without synchronization here.
1676 int api = mCore->mConnectedApi;
1677 disconnect(api);
1678}
1679
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001680status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1681 BQ_LOGV("getUniqueId");
1682
1683 *outId = mCore->mUniqueId;
1684 return NO_ERROR;
1685}
1686
Chia-I Wue2786ea2017-08-07 10:36:08 -07001687status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1688 BQ_LOGV("getConsumerUsage");
1689
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001690 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001691 *outUsage = mCore->mConsumerUsageBits;
1692 return NO_ERROR;
1693}
1694
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001695status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1696 ATRACE_CALL();
1697 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1698
1699 std::lock_guard<std::mutex> lock(mCore->mMutex);
1700
1701 mCore->mAutoPrerotation = autoPrerotation;
1702 return NO_ERROR;
1703}
1704
Dan Stoza289ade12014-02-28 11:17:17 -08001705} // namespace android