blob: c17a525cb2b2a918e2705b5310c9e907d303cc25 [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
Craig Donner6ebc46a2016-10-21 15:23:44 -070047static constexpr uint32_t BQ_LAYER_COUNT = 1;
48
Irvel468051e2016-06-13 16:44:44 -070049BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
50 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080051 mCore(core),
52 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070053 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070054 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070055 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080056 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070057 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080058 mCallbackMutex(),
59 mNextCallbackTicket(0),
60 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070061 mCallbackCondition(),
62 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080063
64BufferQueueProducer::~BufferQueueProducer() {}
65
66status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
67 ATRACE_CALL();
68 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020069 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080070
71 if (mCore->mIsAbandoned) {
72 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
73 return NO_INIT;
74 }
75
Pablo Ceballos583b1b32015-09-03 18:23:52 -070076 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
77 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
78 return NO_INIT;
79 }
80
Dan Stoza3e96f192014-03-03 10:16:19 -080081 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080082 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080083 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080084 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070085 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080086 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070087 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080088 return BAD_VALUE;
89 }
90
91 mSlots[slot].mRequestBufferCalled = true;
92 *buf = mSlots[slot].mGraphicBuffer;
93 return NO_ERROR;
94}
95
Pablo Ceballosfa455352015-08-12 17:47:47 -070096status_t BufferQueueProducer::setMaxDequeuedBufferCount(
97 int maxDequeuedBuffers) {
98 ATRACE_CALL();
99 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
100 maxDequeuedBuffers);
101
Pablo Ceballos981066c2016-02-18 12:54:37 -0800102 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700103 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200104 std::unique_lock<std::mutex> lock(mCore->mMutex);
105 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700106
107 if (mCore->mIsAbandoned) {
108 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
109 "abandoned");
110 return NO_INIT;
111 }
112
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700113 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
114 return NO_ERROR;
115 }
116
Pablo Ceballos72daab62015-12-07 16:38:43 -0800117 // The new maxDequeuedBuffer count should not be violated by the number
118 // of currently dequeued buffers
119 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800120 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700121 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800122 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700123 }
124 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800125 if (dequeuedCount > maxDequeuedBuffers) {
126 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
127 "count (%d) exceeds the current dequeued buffer count (%d)",
128 maxDequeuedBuffers, dequeuedCount);
129 return BAD_VALUE;
130 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700132 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700133 bufferCount += maxDequeuedBuffers;
134
135 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
136 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
137 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
138 return BAD_VALUE;
139 }
140
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700141 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700142 if (bufferCount < minBufferSlots) {
143 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
144 "less than minimum %d", bufferCount, minBufferSlots);
145 return BAD_VALUE;
146 }
147
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700148 if (bufferCount > mCore->mMaxBufferCount) {
149 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700150 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
151 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
152 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
153 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700154 return BAD_VALUE;
155 }
156
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800158 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800159 return BAD_VALUE;
160 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700161 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800162 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800163 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800164 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800165 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200166 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700167 } // Autolock scope
168
169 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700170 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800171 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700172 }
173
174 return NO_ERROR;
175}
176
177status_t BufferQueueProducer::setAsyncMode(bool async) {
178 ATRACE_CALL();
179 BQ_LOGV("setAsyncMode: async = %d", async);
180
Pablo Ceballos981066c2016-02-18 12:54:37 -0800181 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700182 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200183 std::unique_lock<std::mutex> lock(mCore->mMutex);
184 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700185
186 if (mCore->mIsAbandoned) {
187 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
188 return NO_INIT;
189 }
190
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700191 if (async == mCore->mAsyncMode) {
192 return NO_ERROR;
193 }
194
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700195 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700196 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
197 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700198 BQ_LOGE("setAsyncMode(%d): this call would cause the "
199 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700200 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
201 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
202 mCore->mMaxDequeuedBufferCount,
203 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700204 return BAD_VALUE;
205 }
206
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800207 int delta = mCore->getMaxBufferCountLocked(async,
208 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
209 - mCore->getMaxBufferCountLocked();
210
Pablo Ceballos981066c2016-02-18 12:54:37 -0800211 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800212 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
213 "available slots. Delta = %d", delta);
214 return BAD_VALUE;
215 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800217 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200218 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700219 if (delta < 0) {
220 listener = mCore->mConsumerListener;
221 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700222 } // Autolock scope
223
224 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700225 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800226 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700227 }
228 return NO_ERROR;
229}
230
Dan Stoza5ecfb682016-01-04 17:01:02 -0800231int BufferQueueProducer::getFreeBufferLocked() const {
232 if (mCore->mFreeBuffers.empty()) {
233 return BufferQueueCore::INVALID_BUFFER_SLOT;
234 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800236 mCore->mFreeBuffers.pop_front();
237 return slot;
238}
239
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800240int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800241 if (mCore->mFreeSlots.empty()) {
242 return BufferQueueCore::INVALID_BUFFER_SLOT;
243 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800244 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800245 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800246 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800247}
248
249status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200250 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800251 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
252 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800253 bool tryAgain = true;
254 while (tryAgain) {
255 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800256 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800257 return NO_INIT;
258 }
259
Dan Stoza9f3053d2014-03-06 15:14:33 -0800260 int dequeuedCount = 0;
261 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800262 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 if (mSlots[s].mBufferState.isDequeued()) {
264 ++dequeuedCount;
265 }
266 if (mSlots[s].mBufferState.isAcquired()) {
267 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800268 }
269 }
270
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700271 // Producers are not allowed to dequeue more than
272 // mMaxDequeuedBufferCount buffers.
273 // This check is only done if a buffer has already been queued
274 if (mCore->mBufferHasBeenQueued &&
275 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
276 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800277 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800278 return INVALID_OPERATION;
279 }
280
Dan Stoza0de7ea72015-04-23 13:20:51 -0700281 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
282
Dan Stozaae3c3682014-04-18 15:43:35 -0700283 // If we disconnect and reconnect quickly, we can be in a state where
284 // our slots are empty but we have many buffers in the queue. This can
285 // cause us to run out of memory if we outrun the consumer. Wait here if
286 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800287 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700288 bool tooManyBuffers = mCore->mQueue.size()
289 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700290 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800291 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700292 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700293 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700294 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700295 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700296 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700297 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700298 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800299 } else {
300 if (caller == FreeSlotCaller::Dequeue) {
301 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800303 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
304 *found = slot;
305 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800306 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800307 }
308 } else {
309 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800310 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800311 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
312 *found = slot;
313 } else {
314 *found = getFreeBufferLocked();
315 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700316 }
317 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700318 }
319
320 // If no buffer is found, or if the queue has too many buffers
321 // outstanding, wait for a buffer to be acquired or released, or for the
322 // max buffer count to change.
323 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
324 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 if (tryAgain) {
326 // Return an error if we're in non-blocking mode (producer and
327 // consumer are controlled by the application).
328 // However, the consumer is allowed to briefly acquire an extra
329 // buffer (which could cause us to have to wait here), which is
330 // okay, since it is only used to implement an atomic acquire +
331 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700332 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800333 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
334 return WOULD_BLOCK;
335 }
Dan Stoza127fc632015-06-30 13:43:32 -0700336 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200337 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
338 std::chrono::nanoseconds(mDequeueTimeout));
339 if (result == std::cv_status::timeout) {
340 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700341 }
342 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200343 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700344 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800345 }
346 } // while (tryAgain)
347
348 return NO_ERROR;
349}
350
Ian Elliottd11b0442017-07-18 11:05:49 -0600351status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
352 uint32_t width, uint32_t height, PixelFormat format,
353 uint64_t usage, uint64_t* outBufferAge,
354 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800355 ATRACE_CALL();
356 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200357 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800358 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700359
360 if (mCore->mIsAbandoned) {
361 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
362 return NO_INIT;
363 }
364
365 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
366 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
367 return NO_INIT;
368 }
Dan Stoza289ade12014-02-28 11:17:17 -0800369 } // Autolock scope
370
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700371 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800372
373 if ((width && !height) || (!width && height)) {
374 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
375 return BAD_VALUE;
376 }
377
378 status_t returnFlags = NO_ERROR;
379 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
380 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800382
383 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200384 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800385
386 if (format == 0) {
387 format = mCore->mDefaultBufferFormat;
388 }
389
390 // Enable the usage bits the consumer requested
391 usage |= mCore->mConsumerUsageBits;
392
Dan Stoza9de72932015-04-16 17:28:43 -0700393 const bool useDefaultSize = !width && !height;
394 if (useDefaultSize) {
395 width = mCore->mDefaultWidth;
396 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800397 }
Dan Stoza289ade12014-02-28 11:17:17 -0800398
Pablo Ceballos981066c2016-02-18 12:54:37 -0800399 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700400 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200401 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700402 if (status != NO_ERROR) {
403 return status;
404 }
405
406 // This should not happen
407 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
408 BQ_LOGE("dequeueBuffer: no available buffer slots");
409 return -EBUSY;
410 }
411
412 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
413
414 // If we are not allowed to allocate new buffers,
415 // waitForFreeSlotThenRelock must have returned a slot containing a
416 // buffer. If this buffer would require reallocation to meet the
417 // requested attributes, we free it and attempt to get another one.
418 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700419 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700420 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700421 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700422 return BAD_VALUE;
423 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800424 mCore->mFreeSlots.insert(found);
425 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700426 found = BufferItem::INVALID_BUFFER_SLOT;
427 continue;
428 }
429 }
Dan Stoza289ade12014-02-28 11:17:17 -0800430 }
431
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800432 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700433 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700434 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800435 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
436 "buffer");
437
438 return BAD_VALUE;
439 }
440
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700441 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 mCore->mActiveBuffers.insert(found);
443 }
Dan Stoza289ade12014-02-28 11:17:17 -0800444 *outSlot = found;
445 ATRACE_BUFFER_INDEX(found);
446
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800447 attachedByConsumer = mSlots[found].mNeedsReallocation;
448 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800449
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700450 mSlots[found].mBufferState.dequeue();
451
Yi Kong48a619f2018-06-05 16:34:59 -0700452 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700453 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800454 {
455 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700456 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800457 mSlots[found].mRequestBufferCalled = false;
458 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
459 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
460 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800461 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700462 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800463
464 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800465 } else {
466 // We add 1 because that will be the frame number when this buffer
467 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700468 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800469 }
470
Dan Stoza800b41a2015-04-28 14:20:04 -0700471 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
472 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800473
Yi Kong48a619f2018-06-05 16:34:59 -0700474 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800475 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
476 "slot=%d w=%d h=%d format=%u",
477 found, buffer->width, buffer->height, buffer->format);
478 }
479
480 eglDisplay = mSlots[found].mEglDisplay;
481 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700482 // Don't return a fence in shared buffer mode, except for the first
483 // frame.
484 *outFence = (mCore->mSharedBufferMode &&
485 mCore->mSharedBufferSlot == found) ?
486 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800487 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
488 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700489
490 // If shared buffer mode has just been enabled, cache the slot of the
491 // first buffer that is dequeued and mark it as the shared buffer.
492 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
493 BufferQueueCore::INVALID_BUFFER_SLOT) {
494 mCore->mSharedBufferSlot = found;
495 mSlots[found].mBufferState.mShared = true;
496 }
Dan Stoza289ade12014-02-28 11:17:17 -0800497 } // Autolock scope
498
499 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700500 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700501 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700502 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700503 {mConsumerName.string(), mConsumerName.size()});
504
505 status_t error = graphicBuffer->initCheck();
506
Dan Stoza289ade12014-02-28 11:17:17 -0800507 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200508 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800509
Chia-I Wufeec3b12017-05-25 09:34:56 -0700510 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700511 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
512 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
513 }
514
515 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200516 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700517
Chia-I Wufeec3b12017-05-25 09:34:56 -0700518 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700519 mCore->mFreeSlots.insert(*outSlot);
520 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
522 return error;
523 }
524
Dan Stoza289ade12014-02-28 11:17:17 -0800525 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700526 mCore->mFreeSlots.insert(*outSlot);
527 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800528 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
529 return NO_INIT;
530 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800531
Pablo Ceballos9e314332016-01-12 13:49:19 -0800532 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800533 } // Autolock scope
534 }
535
Dan Stoza9f3053d2014-03-06 15:14:33 -0800536 if (attachedByConsumer) {
537 returnFlags |= BUFFER_NEEDS_REALLOCATION;
538 }
539
Dan Stoza289ade12014-02-28 11:17:17 -0800540 if (eglFence != EGL_NO_SYNC_KHR) {
541 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
542 1000000000);
543 // If something goes wrong, log the error, but return the buffer without
544 // synchronizing access to it. It's too late at this point to abort the
545 // dequeue operation.
546 if (result == EGL_FALSE) {
547 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
548 eglGetError());
549 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
550 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
551 }
552 eglDestroySyncKHR(eglDisplay, eglFence);
553 }
554
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700555 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
556 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800557 mSlots[*outSlot].mFrameNumber,
558 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
559
Ian Elliottd11b0442017-07-18 11:05:49 -0600560 if (outBufferAge) {
561 *outBufferAge = mCore->mBufferAge;
562 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700563 addAndGetFrameTimestamps(nullptr, outTimestamps);
564
Dan Stoza289ade12014-02-28 11:17:17 -0800565 return returnFlags;
566}
567
Dan Stoza9f3053d2014-03-06 15:14:33 -0800568status_t BufferQueueProducer::detachBuffer(int slot) {
569 ATRACE_CALL();
570 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700571 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800572
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700573 sp<IConsumerListener> listener;
574 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200575 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700576
577 if (mCore->mIsAbandoned) {
578 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
579 return NO_INIT;
580 }
581
582 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
583 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
584 return NO_INIT;
585 }
586
587 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
588 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
589 return BAD_VALUE;
590 }
591
592 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
593 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
594 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
595 return BAD_VALUE;
596 } else if (!mSlots[slot].mBufferState.isDequeued()) {
597 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
598 "(state = %s)", slot, mSlots[slot].mBufferState.string());
599 return BAD_VALUE;
600 } else if (!mSlots[slot].mRequestBufferCalled) {
601 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
602 slot);
603 return BAD_VALUE;
604 }
605
606 mSlots[slot].mBufferState.detachProducer();
607 mCore->mActiveBuffers.erase(slot);
608 mCore->mFreeSlots.insert(slot);
609 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200610 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700611 VALIDATE_CONSISTENCY();
612 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800613 }
614
Yi Kong48a619f2018-06-05 16:34:59 -0700615 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700616 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700617 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800618
Dan Stoza9f3053d2014-03-06 15:14:33 -0800619 return NO_ERROR;
620}
621
Dan Stozad9822a32014-03-28 15:25:31 -0700622status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
623 sp<Fence>* outFence) {
624 ATRACE_CALL();
625
Yi Kong48a619f2018-06-05 16:34:59 -0700626 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700627 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
628 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700629 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700630 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
631 return BAD_VALUE;
632 }
633
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700634 sp<IConsumerListener> listener;
635 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200636 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700637
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700638 if (mCore->mIsAbandoned) {
639 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
640 return NO_INIT;
641 }
642
643 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
644 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
645 return NO_INIT;
646 }
647
648 if (mCore->mSharedBufferMode) {
649 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
650 "mode");
651 return BAD_VALUE;
652 }
653
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200654 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700655
656 if (mCore->mFreeBuffers.empty()) {
657 return NO_MEMORY;
658 }
659
660 int found = mCore->mFreeBuffers.front();
661 mCore->mFreeBuffers.remove(found);
662 mCore->mFreeSlots.insert(found);
663
664 BQ_LOGV("detachNextBuffer detached slot %d", found);
665
666 *outBuffer = mSlots[found].mGraphicBuffer;
667 *outFence = mSlots[found].mFence;
668 mCore->clearBufferSlotLocked(found);
669 VALIDATE_CONSISTENCY();
670 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700671 }
672
Yi Kong48a619f2018-06-05 16:34:59 -0700673 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700674 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700675 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800676
Dan Stozad9822a32014-03-28 15:25:31 -0700677 return NO_ERROR;
678}
679
Dan Stoza9f3053d2014-03-06 15:14:33 -0800680status_t BufferQueueProducer::attachBuffer(int* outSlot,
681 const sp<android::GraphicBuffer>& buffer) {
682 ATRACE_CALL();
683
Yi Kong48a619f2018-06-05 16:34:59 -0700684 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700685 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700687 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700688 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800689 return BAD_VALUE;
690 }
691
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200692 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700693
694 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700696 return NO_INIT;
697 }
698
699 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700700 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700701 return NO_INIT;
702 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700704 if (mCore->mSharedBufferMode) {
705 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700706 return BAD_VALUE;
707 }
708
Dan Stoza812ed062015-06-02 15:45:22 -0700709 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
710 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
711 "[queue %u]", buffer->getGenerationNumber(),
712 mCore->mGenerationNumber);
713 return BAD_VALUE;
714 }
715
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200716 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700717
Dan Stoza9f3053d2014-03-06 15:14:33 -0800718 status_t returnFlags = NO_ERROR;
719 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200720 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800721 if (status != NO_ERROR) {
722 return status;
723 }
724
725 // This should not happen
726 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700727 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 return -EBUSY;
729 }
730
731 *outSlot = found;
732 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700733 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800734 *outSlot, returnFlags);
735
736 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700737 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
739 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700740 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800741 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800742 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800743 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800744 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700745
Dan Stoza9f3053d2014-03-06 15:14:33 -0800746 return returnFlags;
747}
748
Dan Stoza289ade12014-02-28 11:17:17 -0800749status_t BufferQueueProducer::queueBuffer(int slot,
750 const QueueBufferInput &input, QueueBufferOutput *output) {
751 ATRACE_CALL();
752 ATRACE_BUFFER_INDEX(slot);
753
Brian Andersond6927fb2016-07-23 23:37:30 -0700754 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800755 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800756 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700757 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800758 int scalingMode;
759 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700760 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700761 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700763 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700764 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
765 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700766 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700767 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800768
Yi Kong48a619f2018-06-05 16:34:59 -0700769 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800770 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800771 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 }
773
Brian Anderson3d4039d2016-09-23 16:31:30 -0700774 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
775
Dan Stoza289ade12014-02-28 11:17:17 -0800776 switch (scalingMode) {
777 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
778 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
779 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
780 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
781 break;
782 default:
783 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800785 }
786
Dan Stoza8dc55392014-11-04 11:37:46 -0800787 sp<IConsumerListener> frameAvailableListener;
788 sp<IConsumerListener> frameReplacedListener;
789 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700790 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800791 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800792 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200793 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800794
795 if (mCore->mIsAbandoned) {
796 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
797 return NO_INIT;
798 }
799
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700800 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
801 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
802 return NO_INIT;
803 }
804
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800805 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800806 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800807 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800808 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700809 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800810 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700811 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800812 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800813 } else if (!mSlots[slot].mRequestBufferCalled) {
814 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
815 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800817 }
818
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700819 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700820 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700821 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700822 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700823 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700824 mSlots[slot].mBufferState.mShared = true;
825 }
826
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800827 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700828 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
829 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
830 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700831 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800832 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800833
834 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
835 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700836 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800837 crop.intersect(bufferRect, &croppedRect);
838 if (croppedRect != crop) {
839 BQ_LOGE("queueBuffer: crop rect is not contained within the "
840 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800842 }
843
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800844 // Override UNKNOWN dataspace with consumer default
845 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
846 dataSpace = mCore->mDefaultBufferDataSpace;
847 }
848
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700850 mSlots[slot].mBufferState.queue();
851
Brian Andersond6927fb2016-07-23 23:37:30 -0700852 // Increment the frame counter and store a local version of it
853 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800854 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700855 currentFrameNumber = mCore->mFrameCounter;
856 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800857
Dan Stoza289ade12014-02-28 11:17:17 -0800858 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
859 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
860 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800861 item.mTransform = transform &
862 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800863 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
865 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700866 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800868 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700869 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700870 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800871 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700872 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700873 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700874 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700875 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700877 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700878 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700879 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800880 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800881
Ruben Brunk1681d952014-06-27 15:51:55 -0700882 mStickyTransform = stickyTransform;
883
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700884 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700885 if (mCore->mSharedBufferMode) {
886 mCore->mSharedBufferCache.crop = crop;
887 mCore->mSharedBufferCache.transform = transform;
888 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700889 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700890 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700891 }
892
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800893 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800894 if (mCore->mQueue.empty()) {
895 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
896 // and simply queue this buffer
897 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800898 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800899 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700900 // When the queue is not empty, we need to look at the last buffer
901 // in the queue to see if we need to replace it
902 const BufferItem& last = mCore->mQueue.itemAt(
903 mCore->mQueue.size() - 1);
904 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800905
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700906 if (!last.mIsStale) {
907 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700908
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700909 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 // still be around. Mark it as no longer shared if this
911 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700912 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700913 mSlots[last.mSlot].mBufferState.isFree()) {
914 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700915 }
916 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700917 if (!mSlots[last.mSlot].mBufferState.isShared()) {
918 mCore->mActiveBuffers.erase(last.mSlot);
919 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800920 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700921 }
Dan Stoza289ade12014-02-28 11:17:17 -0800922 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800923
Dan Stoza289ade12014-02-28 11:17:17 -0800924 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700925 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800926 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800927 } else {
928 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800929 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 }
931 }
932
933 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200934 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700935 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800936
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700937 output->width = mCore->mDefaultWidth;
938 output->height = mCore->mDefaultHeight;
939 output->transformHint = mCore->mTransformHint;
940 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
941 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800942
Colin Cross152c3b72016-09-27 14:08:19 -0700943 ATRACE_INT(mCore->mConsumerName.string(),
944 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700945 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800946
947 // Take a ticket for the callback functions
948 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700949
Pablo Ceballos9e314332016-01-12 13:49:19 -0800950 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800951 } // Autolock scope
952
Irvel468051e2016-06-13 16:44:44 -0700953 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
954 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
955 // there will be no Binder call
956 if (!mConsumerIsSurfaceFlinger) {
957 item.mGraphicBuffer.clear();
958 }
959
960 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800961 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
962
963 // Call back without the main BufferQueue lock held, but with the callback
964 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700965
966 int connectedApi;
967 sp<Fence> lastQueuedFence;
968
969 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200970 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -0800971 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200972 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -0800973 }
974
Yi Kong48a619f2018-06-05 16:34:59 -0700975 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800976 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -0700977 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800978 frameReplacedListener->onFrameReplaced(item);
979 }
980
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700981 connectedApi = mCore->mConnectedApi;
982 lastQueuedFence = std::move(mLastQueueBufferFence);
983
984 mLastQueueBufferFence = std::move(acquireFence);
985 mLastQueuedCrop = item.mCrop;
986 mLastQueuedTransform = item.mTransform;
987
Dan Stoza8dc55392014-11-04 11:37:46 -0800988 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200989 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -0800990 }
991
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000992 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700993 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000994 // Waiting here allows for two full buffers to be queued but not a
995 // third. In the event that frames take varying time, this makes a
996 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700997 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000998 }
999
Brian Andersond6927fb2016-07-23 23:37:30 -07001000 // Update and get FrameEventHistory.
1001 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1002 NewFrameEventsEntry newFrameEventsEntry = {
1003 currentFrameNumber,
1004 postedTime,
1005 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001006 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001007 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001008 addAndGetFrameTimestamps(&newFrameEventsEntry,
1009 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001010
Dan Stoza289ade12014-02-28 11:17:17 -08001011 return NO_ERROR;
1012}
1013
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001014status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001015 ATRACE_CALL();
1016 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001017 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001018
1019 if (mCore->mIsAbandoned) {
1020 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001021 return NO_INIT;
1022 }
1023
1024 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1025 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1026 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001027 }
1028
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001029 if (mCore->mSharedBufferMode) {
1030 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001031 return BAD_VALUE;
1032 }
1033
Dan Stoza3e96f192014-03-03 10:16:19 -08001034 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001035 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001036 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001037 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001038 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001039 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001040 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001041 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001042 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001043 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001044 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001045 }
1046
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001047 mSlots[slot].mBufferState.cancel();
1048
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001049 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001050 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001051 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001052 mSlots[slot].mBufferState.mShared = false;
1053 }
1054
1055 // Don't put the shared buffer on the free list.
1056 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001057 mCore->mActiveBuffers.erase(slot);
1058 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001059 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001060
Dan Stoza289ade12014-02-28 11:17:17 -08001061 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001062 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001063 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001064
1065 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001066}
1067
1068int BufferQueueProducer::query(int what, int *outValue) {
1069 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001070 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001071
Yi Kong48a619f2018-06-05 16:34:59 -07001072 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001073 BQ_LOGE("query: outValue was NULL");
1074 return BAD_VALUE;
1075 }
1076
1077 if (mCore->mIsAbandoned) {
1078 BQ_LOGE("query: BufferQueue has been abandoned");
1079 return NO_INIT;
1080 }
1081
1082 int value;
1083 switch (what) {
1084 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001085 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001086 break;
1087 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001088 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001089 break;
1090 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001091 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001092 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001093 case NATIVE_WINDOW_LAYER_COUNT:
1094 // All BufferQueue buffers have a single layer.
1095 value = BQ_LAYER_COUNT;
1096 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001097 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001098 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001099 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001100 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001101 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001102 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001103 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1104 value = (mCore->mQueue.size() > 1);
1105 break;
1106 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001107 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001108 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001109 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001110 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1111 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1112 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001113 case NATIVE_WINDOW_BUFFER_AGE:
1114 if (mCore->mBufferAge > INT32_MAX) {
1115 value = 0;
1116 } else {
1117 value = static_cast<int32_t>(mCore->mBufferAge);
1118 }
1119 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001120 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1121 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1122 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001123 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1124 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1125 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001126 default:
1127 return BAD_VALUE;
1128 }
1129
1130 BQ_LOGV("query: %d? %d", what, value);
1131 *outValue = value;
1132 return NO_ERROR;
1133}
1134
Dan Stozaf0eaf252014-03-21 13:05:51 -07001135status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001136 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1137 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001138 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001139 mConsumerName = mCore->mConsumerName;
1140 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1141 producerControlledByApp ? "true" : "false");
1142
1143 if (mCore->mIsAbandoned) {
1144 BQ_LOGE("connect: BufferQueue has been abandoned");
1145 return NO_INIT;
1146 }
1147
Yi Kong48a619f2018-06-05 16:34:59 -07001148 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001149 BQ_LOGE("connect: BufferQueue has no consumer");
1150 return NO_INIT;
1151 }
1152
Yi Kong48a619f2018-06-05 16:34:59 -07001153 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001154 BQ_LOGE("connect: output was NULL");
1155 return BAD_VALUE;
1156 }
1157
1158 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1159 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1160 mCore->mConnectedApi, api);
1161 return BAD_VALUE;
1162 }
1163
1164 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1165 mDequeueTimeout < 0 ?
1166 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1167 mCore->mMaxBufferCount) -
1168 mCore->getMaxBufferCountLocked();
1169 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1170 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1171 "slots. Delta = %d", delta);
1172 return BAD_VALUE;
1173 }
1174
Dan Stoza289ade12014-02-28 11:17:17 -08001175 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001176 switch (api) {
1177 case NATIVE_WINDOW_API_EGL:
1178 case NATIVE_WINDOW_API_CPU:
1179 case NATIVE_WINDOW_API_MEDIA:
1180 case NATIVE_WINDOW_API_CAMERA:
1181 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001182
1183 output->width = mCore->mDefaultWidth;
1184 output->height = mCore->mDefaultHeight;
1185 output->transformHint = mCore->mTransformHint;
1186 output->numPendingBuffers =
1187 static_cast<uint32_t>(mCore->mQueue.size());
1188 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001189 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001190
Yi Kong48a619f2018-06-05 16:34:59 -07001191 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001192 // Set up a death notification so that we can disconnect
1193 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001194 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001195 status = IInterface::asBinder(listener)->linkToDeath(
1196 static_cast<IBinder::DeathRecipient*>(this));
1197 if (status != NO_ERROR) {
1198 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1199 strerror(-status), status);
1200 }
1201 mCore->mLinkedToDeath = listener;
1202 }
1203 if (listener->needsReleaseNotify()) {
1204 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001205 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001206 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001207 break;
1208 default:
1209 BQ_LOGE("connect: unknown API %d", api);
1210 status = BAD_VALUE;
1211 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001212 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001213 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001214 mCore->mBufferHasBeenQueued = false;
1215 mCore->mDequeueBufferCannotBlock = false;
1216 if (mDequeueTimeout < 0) {
1217 mCore->mDequeueBufferCannotBlock =
1218 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001219 }
Dan Stoza289ade12014-02-28 11:17:17 -08001220
Pablo Ceballos981066c2016-02-18 12:54:37 -08001221 mCore->mAllowAllocation = true;
1222 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001223 return status;
1224}
1225
Robert Carr97b9c862016-09-08 13:54:35 -07001226status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001227 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001228 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001229
1230 int status = NO_ERROR;
1231 sp<IConsumerListener> listener;
1232 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001233 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001234
1235 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001236 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001237 return NO_ERROR;
1238 }
1239 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1240 }
1241
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001242 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001243
1244 if (mCore->mIsAbandoned) {
1245 // It's not really an error to disconnect after the surface has
1246 // been abandoned; it should just be a no-op.
1247 return NO_ERROR;
1248 }
1249
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001250 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001251 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1252 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1253 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001254 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001255 // If we're asked to disconnect the currently connected api but
1256 // nobody is connected, it's not really an error.
1257 if (api == BufferQueueCore::NO_CONNECTED_API) {
1258 return NO_ERROR;
1259 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001260 }
1261
Dan Stoza289ade12014-02-28 11:17:17 -08001262 switch (api) {
1263 case NATIVE_WINDOW_API_EGL:
1264 case NATIVE_WINDOW_API_CPU:
1265 case NATIVE_WINDOW_API_MEDIA:
1266 case NATIVE_WINDOW_API_CAMERA:
1267 if (mCore->mConnectedApi == api) {
1268 mCore->freeAllBuffersLocked();
1269
1270 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001271 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001272 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001273 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001274 // This can fail if we're here because of the death
1275 // notification, but we just ignore it
1276 token->unlinkToDeath(
1277 static_cast<IBinder::DeathRecipient*>(this));
1278 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001279 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001280 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001281 mCore->mLinkedToDeath = nullptr;
1282 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001283 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001284 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001285 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001286 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001287 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001288 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1289 BQ_LOGE("disconnect: not connected (req=%d)", api);
1290 status = NO_INIT;
1291 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001292 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001293 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001294 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001295 }
1296 break;
1297 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001298 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001299 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001300 break;
1301 }
1302 } // Autolock scope
1303
1304 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001305 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001306 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001307 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001308 }
1309
1310 return status;
1311}
1312
Jesse Hall399184a2014-03-03 15:42:54 -08001313status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001314 sp<IConsumerListener> listener;
1315 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001316 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001317 mCore->mSidebandStream = stream;
1318 listener = mCore->mConsumerListener;
1319 } // Autolock scope
1320
Yi Kong48a619f2018-06-05 16:34:59 -07001321 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001322 listener->onSidebandStreamChanged();
1323 }
Jesse Hall399184a2014-03-03 15:42:54 -08001324 return NO_ERROR;
1325}
1326
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001327void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001328 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001329 ATRACE_CALL();
1330 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001331 size_t newBufferCount = 0;
1332 uint32_t allocWidth = 0;
1333 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001334 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001335 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001336 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001337 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001338 std::unique_lock<std::mutex> lock(mCore->mMutex);
1339 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001340
Dan Stoza9de72932015-04-16 17:28:43 -07001341 if (!mCore->mAllowAllocation) {
1342 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1343 "BufferQueue");
1344 return;
1345 }
1346
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001347 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1348 // both allocateBuffers and dequeueBuffer.
1349 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001350 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001351 return;
1352 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001353
Antoine Labour78014f32014-07-15 21:17:03 -07001354 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1355 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1356 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1357 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001358 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001359
1360 mCore->mIsAllocating = true;
1361 } // Autolock scope
1362
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001363 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001364 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001365 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001366 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001367 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001368
1369 status_t result = graphicBuffer->initCheck();
1370
Antoine Labour78014f32014-07-15 21:17:03 -07001371 if (result != NO_ERROR) {
1372 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001373 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001374 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001375 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001376 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001377 return;
1378 }
1379 buffers.push_back(graphicBuffer);
1380 }
1381
1382 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001383 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001384 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1385 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001386 PixelFormat checkFormat = format != 0 ?
1387 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001388 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001389 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1390 checkFormat != allocFormat || checkUsage != allocUsage) {
1391 // Something changed while we released the lock. Retry.
1392 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1393 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001394 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001395 continue;
1396 }
1397
Antoine Labour78014f32014-07-15 21:17:03 -07001398 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001399 if (mCore->mFreeSlots.empty()) {
1400 BQ_LOGV("allocateBuffers: a slot was occupied while "
1401 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001402 continue;
1403 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001404 auto slot = mCore->mFreeSlots.begin();
1405 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1406 mSlots[*slot].mGraphicBuffer = buffers[i];
1407 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001408
1409 // freeBufferLocked puts this slot on the free slots list. Since
1410 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001411 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001412
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001413 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1414 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001415
1416 // Make sure the erase is done after all uses of the slot
1417 // iterator since it will be invalid after this point.
1418 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001419 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001420
Antoine Labour78014f32014-07-15 21:17:03 -07001421 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001422 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001423 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001424 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001425 }
1426}
1427
Dan Stoza9de72932015-04-16 17:28:43 -07001428status_t BufferQueueProducer::allowAllocation(bool allow) {
1429 ATRACE_CALL();
1430 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1431
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001432 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001433 mCore->mAllowAllocation = allow;
1434 return NO_ERROR;
1435}
1436
Dan Stoza812ed062015-06-02 15:45:22 -07001437status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1438 ATRACE_CALL();
1439 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1440
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001441 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001442 mCore->mGenerationNumber = generationNumber;
1443 return NO_ERROR;
1444}
1445
Dan Stozac6f30bd2015-06-08 09:32:50 -07001446String8 BufferQueueProducer::getConsumerName() const {
1447 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001448 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001449 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1450 return mConsumerName;
1451}
1452
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001453status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001454 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001455 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001456
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001457 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001458 if (!sharedBufferMode) {
1459 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001460 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001461 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001462 return NO_ERROR;
1463}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001464
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001465status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1466 ATRACE_CALL();
1467 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1468
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001469 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001470
1471 mCore->mAutoRefresh = autoRefresh;
1472 return NO_ERROR;
1473}
1474
Dan Stoza127fc632015-06-30 13:43:32 -07001475status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1476 ATRACE_CALL();
1477 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1478
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001479 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001480 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1481 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1482 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1483 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1484 "available slots. Delta = %d", delta);
1485 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001486 }
1487
Pablo Ceballos981066c2016-02-18 12:54:37 -08001488 mDequeueTimeout = timeout;
1489 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001490
Pablo Ceballos981066c2016-02-18 12:54:37 -08001491 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001492 return NO_ERROR;
1493}
1494
Dan Stoza50101d02016-04-07 16:53:23 -07001495status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001496 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001497 ATRACE_CALL();
1498 BQ_LOGV("getLastQueuedBuffer");
1499
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001500 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001501 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1502 *outBuffer = nullptr;
1503 *outFence = Fence::NO_FENCE;
1504 return NO_ERROR;
1505 }
1506
1507 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1508 *outFence = mLastQueueBufferFence;
1509
John Reck1a61da52016-04-28 13:18:15 -07001510 // Currently only SurfaceFlinger internally ever changes
1511 // GLConsumer's filtering mode, so we just use 'true' here as
1512 // this is slightly specialized for the current client of this API,
1513 // which does want filtering.
1514 GLConsumer::computeTransformMatrix(outTransformMatrix,
1515 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1516 mLastQueuedTransform, true /* filter */);
1517
Dan Stoza50101d02016-04-07 16:53:23 -07001518 return NO_ERROR;
1519}
1520
Brian Anderson3890c392016-07-25 12:48:08 -07001521void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1522 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001523}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001524
Brian Anderson3890c392016-07-25 12:48:08 -07001525void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001527 FrameEventHistoryDelta* outDelta) {
1528 if (newTimestamps == nullptr && outDelta == nullptr) {
1529 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001530 }
1531
1532 ATRACE_CALL();
1533 BQ_LOGV("addAndGetFrameTimestamps");
1534 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001535 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001536 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001537 listener = mCore->mConsumerListener;
1538 }
Yi Kong48a619f2018-06-05 16:34:59 -07001539 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001540 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001541 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001542}
1543
Dan Stoza289ade12014-02-28 11:17:17 -08001544void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1545 // If we're here, it means that a producer we were connected to died.
1546 // We're guaranteed that we are still connected to it because we remove
1547 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1548 // without synchronization here.
1549 int api = mCore->mConnectedApi;
1550 disconnect(api);
1551}
1552
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001553status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1554 BQ_LOGV("getUniqueId");
1555
1556 *outId = mCore->mUniqueId;
1557 return NO_ERROR;
1558}
1559
Chia-I Wue2786ea2017-08-07 10:36:08 -07001560status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1561 BQ_LOGV("getConsumerUsage");
1562
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001563 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001564 *outUsage = mCore->mConsumerUsageBits;
1565 return NO_ERROR;
1566}
1567
Dan Stoza289ade12014-02-28 11:17:17 -08001568} // namespace android