blob: b353ffef92250cc9362ae8aedda75a74dde670a8 [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(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010062 mDequeueTimeout(-1),
63 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080064
65BufferQueueProducer::~BufferQueueProducer() {}
66
67status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
68 ATRACE_CALL();
69 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020070 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080071
72 if (mCore->mIsAbandoned) {
73 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
74 return NO_INIT;
75 }
76
Pablo Ceballos583b1b32015-09-03 18:23:52 -070077 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
78 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
79 return NO_INIT;
80 }
81
Dan Stoza3e96f192014-03-03 10:16:19 -080082 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080083 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080084 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080085 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070086 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080087 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070088 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080089 return BAD_VALUE;
90 }
91
92 mSlots[slot].mRequestBufferCalled = true;
93 *buf = mSlots[slot].mGraphicBuffer;
94 return NO_ERROR;
95}
96
Pablo Ceballosfa455352015-08-12 17:47:47 -070097status_t BufferQueueProducer::setMaxDequeuedBufferCount(
98 int maxDequeuedBuffers) {
99 ATRACE_CALL();
100 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
101 maxDequeuedBuffers);
102
Pablo Ceballos981066c2016-02-18 12:54:37 -0800103 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700104 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200105 std::unique_lock<std::mutex> lock(mCore->mMutex);
106 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700107
108 if (mCore->mIsAbandoned) {
109 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
110 "abandoned");
111 return NO_INIT;
112 }
113
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700114 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
115 return NO_ERROR;
116 }
117
Pablo Ceballos72daab62015-12-07 16:38:43 -0800118 // The new maxDequeuedBuffer count should not be violated by the number
119 // of currently dequeued buffers
120 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800121 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700122 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800123 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700124 }
125 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800126 if (dequeuedCount > maxDequeuedBuffers) {
127 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
128 "count (%d) exceeds the current dequeued buffer count (%d)",
129 maxDequeuedBuffers, dequeuedCount);
130 return BAD_VALUE;
131 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700133 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700134 bufferCount += maxDequeuedBuffers;
135
136 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
137 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
138 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
139 return BAD_VALUE;
140 }
141
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700142 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700143 if (bufferCount < minBufferSlots) {
144 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
145 "less than minimum %d", bufferCount, minBufferSlots);
146 return BAD_VALUE;
147 }
148
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700149 if (bufferCount > mCore->mMaxBufferCount) {
150 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700151 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
152 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
153 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
154 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700155 return BAD_VALUE;
156 }
157
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800159 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800160 return BAD_VALUE;
161 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700162 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800163 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800164 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800165 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800166 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200167 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700168 } // Autolock scope
169
170 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700171 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800172 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700173 }
174
175 return NO_ERROR;
176}
177
178status_t BufferQueueProducer::setAsyncMode(bool async) {
179 ATRACE_CALL();
180 BQ_LOGV("setAsyncMode: async = %d", async);
181
Pablo Ceballos981066c2016-02-18 12:54:37 -0800182 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700183 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200184 std::unique_lock<std::mutex> lock(mCore->mMutex);
185 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700186
187 if (mCore->mIsAbandoned) {
188 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
189 return NO_INIT;
190 }
191
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700192 if (async == mCore->mAsyncMode) {
193 return NO_ERROR;
194 }
195
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700196 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700197 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
198 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700199 BQ_LOGE("setAsyncMode(%d): this call would cause the "
200 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700201 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
202 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
203 mCore->mMaxDequeuedBufferCount,
204 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700205 return BAD_VALUE;
206 }
207
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800208 int delta = mCore->getMaxBufferCountLocked(async,
209 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
210 - mCore->getMaxBufferCountLocked();
211
Pablo Ceballos981066c2016-02-18 12:54:37 -0800212 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800213 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
214 "available slots. Delta = %d", delta);
215 return BAD_VALUE;
216 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700217 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800218 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200219 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700220 if (delta < 0) {
221 listener = mCore->mConsumerListener;
222 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700223 } // Autolock scope
224
225 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700226 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800227 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700228 }
229 return NO_ERROR;
230}
231
Dan Stoza5ecfb682016-01-04 17:01:02 -0800232int BufferQueueProducer::getFreeBufferLocked() const {
233 if (mCore->mFreeBuffers.empty()) {
234 return BufferQueueCore::INVALID_BUFFER_SLOT;
235 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800237 mCore->mFreeBuffers.pop_front();
238 return slot;
239}
240
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800242 if (mCore->mFreeSlots.empty()) {
243 return BufferQueueCore::INVALID_BUFFER_SLOT;
244 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800245 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800247 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800248}
249
250status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200251 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800252 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
253 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800254 bool tryAgain = true;
255 while (tryAgain) {
256 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800257 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800258 return NO_INIT;
259 }
260
Dan Stoza9f3053d2014-03-06 15:14:33 -0800261 int dequeuedCount = 0;
262 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800263 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700264 if (mSlots[s].mBufferState.isDequeued()) {
265 ++dequeuedCount;
266 }
267 if (mSlots[s].mBufferState.isAcquired()) {
268 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800269 }
270 }
271
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700272 // Producers are not allowed to dequeue more than
273 // mMaxDequeuedBufferCount buffers.
274 // This check is only done if a buffer has already been queued
275 if (mCore->mBufferHasBeenQueued &&
276 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
277 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800278 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800279 return INVALID_OPERATION;
280 }
281
Dan Stoza0de7ea72015-04-23 13:20:51 -0700282 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
283
Dan Stozaae3c3682014-04-18 15:43:35 -0700284 // If we disconnect and reconnect quickly, we can be in a state where
285 // our slots are empty but we have many buffers in the queue. This can
286 // cause us to run out of memory if we outrun the consumer. Wait here if
287 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800288 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700289 bool tooManyBuffers = mCore->mQueue.size()
290 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700293 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700294 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700298 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700299 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800300 } else {
301 if (caller == FreeSlotCaller::Dequeue) {
302 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800303 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
305 *found = slot;
306 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800307 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800308 }
309 } else {
310 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800311 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800312 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
313 *found = slot;
314 } else {
315 *found = getFreeBufferLocked();
316 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700317 }
318 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700319 }
320
321 // If no buffer is found, or if the queue has too many buffers
322 // outstanding, wait for a buffer to be acquired or released, or for the
323 // max buffer count to change.
324 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
325 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 if (tryAgain) {
327 // Return an error if we're in non-blocking mode (producer and
328 // consumer are controlled by the application).
329 // However, the consumer is allowed to briefly acquire an extra
330 // buffer (which could cause us to have to wait here), which is
331 // okay, since it is only used to implement an atomic acquire +
332 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700333 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
335 return WOULD_BLOCK;
336 }
Dan Stoza127fc632015-06-30 13:43:32 -0700337 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200338 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
339 std::chrono::nanoseconds(mDequeueTimeout));
340 if (result == std::cv_status::timeout) {
341 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700342 }
343 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200344 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700345 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800346 }
347 } // while (tryAgain)
348
349 return NO_ERROR;
350}
351
Ian Elliottd11b0442017-07-18 11:05:49 -0600352status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
353 uint32_t width, uint32_t height, PixelFormat format,
354 uint64_t usage, uint64_t* outBufferAge,
355 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800356 ATRACE_CALL();
357 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200358 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800359 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700360
361 if (mCore->mIsAbandoned) {
362 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
363 return NO_INIT;
364 }
365
366 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
367 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
368 return NO_INIT;
369 }
Dan Stoza289ade12014-02-28 11:17:17 -0800370 } // Autolock scope
371
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700372 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800373
374 if ((width && !height) || (!width && height)) {
375 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
376 return BAD_VALUE;
377 }
378
379 status_t returnFlags = NO_ERROR;
380 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
381 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800383
384 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200385 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800386
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100387 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
388 // is finished such that we don't allocate in parallel.
389 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
390 mDequeueWaitingForAllocation = true;
391 mCore->waitWhileAllocatingLocked(lock);
392 mDequeueWaitingForAllocation = false;
393 mDequeueWaitingForAllocationCondition.notify_all();
394 }
395
Dan Stoza289ade12014-02-28 11:17:17 -0800396 if (format == 0) {
397 format = mCore->mDefaultBufferFormat;
398 }
399
400 // Enable the usage bits the consumer requested
401 usage |= mCore->mConsumerUsageBits;
402
Dan Stoza9de72932015-04-16 17:28:43 -0700403 const bool useDefaultSize = !width && !height;
404 if (useDefaultSize) {
405 width = mCore->mDefaultWidth;
406 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800407 }
Dan Stoza289ade12014-02-28 11:17:17 -0800408
Pablo Ceballos981066c2016-02-18 12:54:37 -0800409 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700410 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200411 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700412 if (status != NO_ERROR) {
413 return status;
414 }
415
416 // This should not happen
417 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
418 BQ_LOGE("dequeueBuffer: no available buffer slots");
419 return -EBUSY;
420 }
421
422 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
423
424 // If we are not allowed to allocate new buffers,
425 // waitForFreeSlotThenRelock must have returned a slot containing a
426 // buffer. If this buffer would require reallocation to meet the
427 // requested attributes, we free it and attempt to get another one.
428 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700429 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700430 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700431 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700432 return BAD_VALUE;
433 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800434 mCore->mFreeSlots.insert(found);
435 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700436 found = BufferItem::INVALID_BUFFER_SLOT;
437 continue;
438 }
439 }
Dan Stoza289ade12014-02-28 11:17:17 -0800440 }
441
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700443 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700444 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
446 "buffer");
447
448 return BAD_VALUE;
449 }
450
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700451 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800452 mCore->mActiveBuffers.insert(found);
453 }
Dan Stoza289ade12014-02-28 11:17:17 -0800454 *outSlot = found;
455 ATRACE_BUFFER_INDEX(found);
456
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800457 attachedByConsumer = mSlots[found].mNeedsReallocation;
458 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800459
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700460 mSlots[found].mBufferState.dequeue();
461
Yi Kong48a619f2018-06-05 16:34:59 -0700462 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700463 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800464 {
465 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700466 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800467 mSlots[found].mRequestBufferCalled = false;
468 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
469 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
470 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800471 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700472 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800473
474 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800475 } else {
476 // We add 1 because that will be the frame number when this buffer
477 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800479 }
480
Dan Stoza800b41a2015-04-28 14:20:04 -0700481 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
482 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800483
Yi Kong48a619f2018-06-05 16:34:59 -0700484 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800485 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
486 "slot=%d w=%d h=%d format=%u",
487 found, buffer->width, buffer->height, buffer->format);
488 }
489
490 eglDisplay = mSlots[found].mEglDisplay;
491 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700492 // Don't return a fence in shared buffer mode, except for the first
493 // frame.
494 *outFence = (mCore->mSharedBufferMode &&
495 mCore->mSharedBufferSlot == found) ?
496 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800497 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
498 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700499
500 // If shared buffer mode has just been enabled, cache the slot of the
501 // first buffer that is dequeued and mark it as the shared buffer.
502 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
503 BufferQueueCore::INVALID_BUFFER_SLOT) {
504 mCore->mSharedBufferSlot = found;
505 mSlots[found].mBufferState.mShared = true;
506 }
Dan Stoza289ade12014-02-28 11:17:17 -0800507 } // Autolock scope
508
509 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700510 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700511 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700512 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700513 {mConsumerName.string(), mConsumerName.size()});
514
515 status_t error = graphicBuffer->initCheck();
516
Dan Stoza289ade12014-02-28 11:17:17 -0800517 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200518 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800519
Chia-I Wufeec3b12017-05-25 09:34:56 -0700520 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
522 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
523 }
524
525 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200526 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700527
Chia-I Wufeec3b12017-05-25 09:34:56 -0700528 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700529 mCore->mFreeSlots.insert(*outSlot);
530 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700531 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
532 return error;
533 }
534
Dan Stoza289ade12014-02-28 11:17:17 -0800535 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700536 mCore->mFreeSlots.insert(*outSlot);
537 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800538 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
539 return NO_INIT;
540 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800541
Pablo Ceballos9e314332016-01-12 13:49:19 -0800542 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800543 } // Autolock scope
544 }
545
Dan Stoza9f3053d2014-03-06 15:14:33 -0800546 if (attachedByConsumer) {
547 returnFlags |= BUFFER_NEEDS_REALLOCATION;
548 }
549
Dan Stoza289ade12014-02-28 11:17:17 -0800550 if (eglFence != EGL_NO_SYNC_KHR) {
551 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
552 1000000000);
553 // If something goes wrong, log the error, but return the buffer without
554 // synchronizing access to it. It's too late at this point to abort the
555 // dequeue operation.
556 if (result == EGL_FALSE) {
557 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
558 eglGetError());
559 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
560 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
561 }
562 eglDestroySyncKHR(eglDisplay, eglFence);
563 }
564
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700565 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
566 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800567 mSlots[*outSlot].mFrameNumber,
568 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
569
Ian Elliottd11b0442017-07-18 11:05:49 -0600570 if (outBufferAge) {
571 *outBufferAge = mCore->mBufferAge;
572 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700573 addAndGetFrameTimestamps(nullptr, outTimestamps);
574
Dan Stoza289ade12014-02-28 11:17:17 -0800575 return returnFlags;
576}
577
Dan Stoza9f3053d2014-03-06 15:14:33 -0800578status_t BufferQueueProducer::detachBuffer(int slot) {
579 ATRACE_CALL();
580 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700581 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800582
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700583 sp<IConsumerListener> listener;
584 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200585 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700586
587 if (mCore->mIsAbandoned) {
588 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
589 return NO_INIT;
590 }
591
592 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
593 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
594 return NO_INIT;
595 }
596
597 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
598 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
599 return BAD_VALUE;
600 }
601
602 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
603 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
604 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
605 return BAD_VALUE;
606 } else if (!mSlots[slot].mBufferState.isDequeued()) {
607 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
608 "(state = %s)", slot, mSlots[slot].mBufferState.string());
609 return BAD_VALUE;
610 } else if (!mSlots[slot].mRequestBufferCalled) {
611 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
612 slot);
613 return BAD_VALUE;
614 }
615
616 mSlots[slot].mBufferState.detachProducer();
617 mCore->mActiveBuffers.erase(slot);
618 mCore->mFreeSlots.insert(slot);
619 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200620 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700621 VALIDATE_CONSISTENCY();
622 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800623 }
624
Yi Kong48a619f2018-06-05 16:34:59 -0700625 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700626 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700627 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800628
Dan Stoza9f3053d2014-03-06 15:14:33 -0800629 return NO_ERROR;
630}
631
Dan Stozad9822a32014-03-28 15:25:31 -0700632status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
633 sp<Fence>* outFence) {
634 ATRACE_CALL();
635
Yi Kong48a619f2018-06-05 16:34:59 -0700636 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700637 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
638 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700639 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700640 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
641 return BAD_VALUE;
642 }
643
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700644 sp<IConsumerListener> listener;
645 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200646 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700647
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700648 if (mCore->mIsAbandoned) {
649 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
650 return NO_INIT;
651 }
652
653 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
654 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
655 return NO_INIT;
656 }
657
658 if (mCore->mSharedBufferMode) {
659 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
660 "mode");
661 return BAD_VALUE;
662 }
663
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200664 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700665
666 if (mCore->mFreeBuffers.empty()) {
667 return NO_MEMORY;
668 }
669
670 int found = mCore->mFreeBuffers.front();
671 mCore->mFreeBuffers.remove(found);
672 mCore->mFreeSlots.insert(found);
673
674 BQ_LOGV("detachNextBuffer detached slot %d", found);
675
676 *outBuffer = mSlots[found].mGraphicBuffer;
677 *outFence = mSlots[found].mFence;
678 mCore->clearBufferSlotLocked(found);
679 VALIDATE_CONSISTENCY();
680 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700681 }
682
Yi Kong48a619f2018-06-05 16:34:59 -0700683 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700684 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700685 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800686
Dan Stozad9822a32014-03-28 15:25:31 -0700687 return NO_ERROR;
688}
689
Dan Stoza9f3053d2014-03-06 15:14:33 -0800690status_t BufferQueueProducer::attachBuffer(int* outSlot,
691 const sp<android::GraphicBuffer>& buffer) {
692 ATRACE_CALL();
693
Yi Kong48a619f2018-06-05 16:34:59 -0700694 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700697 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700698 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800699 return BAD_VALUE;
700 }
701
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200702 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700703
704 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700705 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700706 return NO_INIT;
707 }
708
709 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700710 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700711 return NO_INIT;
712 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800713
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700714 if (mCore->mSharedBufferMode) {
715 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700716 return BAD_VALUE;
717 }
718
Dan Stoza812ed062015-06-02 15:45:22 -0700719 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
720 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
721 "[queue %u]", buffer->getGenerationNumber(),
722 mCore->mGenerationNumber);
723 return BAD_VALUE;
724 }
725
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200726 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700727
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 status_t returnFlags = NO_ERROR;
729 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200730 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800731 if (status != NO_ERROR) {
732 return status;
733 }
734
735 // This should not happen
736 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700737 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 return -EBUSY;
739 }
740
741 *outSlot = found;
742 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700743 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800744 *outSlot, returnFlags);
745
746 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700747 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800748 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
749 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700750 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800751 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800752 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800753 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800754 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700755
Dan Stoza9f3053d2014-03-06 15:14:33 -0800756 return returnFlags;
757}
758
Dan Stoza289ade12014-02-28 11:17:17 -0800759status_t BufferQueueProducer::queueBuffer(int slot,
760 const QueueBufferInput &input, QueueBufferOutput *output) {
761 ATRACE_CALL();
762 ATRACE_BUFFER_INDEX(slot);
763
Brian Andersond6927fb2016-07-23 23:37:30 -0700764 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800765 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800766 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700767 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800768 int scalingMode;
769 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700770 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700771 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700772 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700773 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700774 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
775 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700776 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700777 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800778
Yi Kong48a619f2018-06-05 16:34:59 -0700779 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800780 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800781 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800782 }
783
Brian Anderson3d4039d2016-09-23 16:31:30 -0700784 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
785
Dan Stoza289ade12014-02-28 11:17:17 -0800786 switch (scalingMode) {
787 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
788 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
789 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
790 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
791 break;
792 default:
793 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800794 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800795 }
796
Dan Stoza8dc55392014-11-04 11:37:46 -0800797 sp<IConsumerListener> frameAvailableListener;
798 sp<IConsumerListener> frameReplacedListener;
799 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700800 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800801 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200803 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800804
805 if (mCore->mIsAbandoned) {
806 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
807 return NO_INIT;
808 }
809
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700810 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
811 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
812 return NO_INIT;
813 }
814
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800815 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800816 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800817 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800818 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700819 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800820 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700821 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800822 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800823 } else if (!mSlots[slot].mRequestBufferCalled) {
824 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
825 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800826 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800827 }
828
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700829 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700830 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700831 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700832 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700833 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700834 mSlots[slot].mBufferState.mShared = true;
835 }
836
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800837 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700838 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
839 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
840 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700841 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800842 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800843
844 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
845 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700846 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800847 crop.intersect(bufferRect, &croppedRect);
848 if (croppedRect != crop) {
849 BQ_LOGE("queueBuffer: crop rect is not contained within the "
850 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800851 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800852 }
853
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800854 // Override UNKNOWN dataspace with consumer default
855 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
856 dataSpace = mCore->mDefaultBufferDataSpace;
857 }
858
Brian Andersond6927fb2016-07-23 23:37:30 -0700859 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700860 mSlots[slot].mBufferState.queue();
861
Brian Andersond6927fb2016-07-23 23:37:30 -0700862 // Increment the frame counter and store a local version of it
863 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800864 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700865 currentFrameNumber = mCore->mFrameCounter;
866 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800867
Dan Stoza289ade12014-02-28 11:17:17 -0800868 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
869 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
870 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800871 item.mTransform = transform &
872 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800873 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800874 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
875 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700876 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800877 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800878 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700879 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700880 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800881 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700882 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700883 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700884 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700885 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700886 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700887 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700888 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700889 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800890 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800891
Ruben Brunk1681d952014-06-27 15:51:55 -0700892 mStickyTransform = stickyTransform;
893
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700894 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700895 if (mCore->mSharedBufferMode) {
896 mCore->mSharedBufferCache.crop = crop;
897 mCore->mSharedBufferCache.transform = transform;
898 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700899 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700900 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700901 }
902
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800903 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800904 if (mCore->mQueue.empty()) {
905 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
906 // and simply queue this buffer
907 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800908 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800909 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700910 // When the queue is not empty, we need to look at the last buffer
911 // in the queue to see if we need to replace it
912 const BufferItem& last = mCore->mQueue.itemAt(
913 mCore->mQueue.size() - 1);
914 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800915
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700916 if (!last.mIsStale) {
917 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700918
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700919 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700920 // still be around. Mark it as no longer shared if this
921 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700922 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700923 mSlots[last.mSlot].mBufferState.isFree()) {
924 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700925 }
926 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700927 if (!mSlots[last.mSlot].mBufferState.isShared()) {
928 mCore->mActiveBuffers.erase(last.mSlot);
929 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800930 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700931 }
Dan Stoza289ade12014-02-28 11:17:17 -0800932 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800933
Dan Stoza289ade12014-02-28 11:17:17 -0800934 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700935 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800936 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800937 } else {
938 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800939 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800940 }
941 }
942
943 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200944 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700945 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800946
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700947 output->width = mCore->mDefaultWidth;
948 output->height = mCore->mDefaultHeight;
949 output->transformHint = mCore->mTransformHint;
950 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
951 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800952
Colin Cross152c3b72016-09-27 14:08:19 -0700953 ATRACE_INT(mCore->mConsumerName.string(),
954 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700955 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800956
957 // Take a ticket for the callback functions
958 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700959
Pablo Ceballos9e314332016-01-12 13:49:19 -0800960 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800961 } // Autolock scope
962
Irvel468051e2016-06-13 16:44:44 -0700963 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
964 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
965 // there will be no Binder call
966 if (!mConsumerIsSurfaceFlinger) {
967 item.mGraphicBuffer.clear();
968 }
969
970 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800971 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
972
973 // Call back without the main BufferQueue lock held, but with the callback
974 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700975
976 int connectedApi;
977 sp<Fence> lastQueuedFence;
978
979 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200980 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -0800981 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200982 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -0800983 }
984
Yi Kong48a619f2018-06-05 16:34:59 -0700985 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800986 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -0700987 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800988 frameReplacedListener->onFrameReplaced(item);
989 }
990
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700991 connectedApi = mCore->mConnectedApi;
992 lastQueuedFence = std::move(mLastQueueBufferFence);
993
994 mLastQueueBufferFence = std::move(acquireFence);
995 mLastQueuedCrop = item.mCrop;
996 mLastQueuedTransform = item.mTransform;
997
Dan Stoza8dc55392014-11-04 11:37:46 -0800998 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200999 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001000 }
1001
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001002 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001003 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001004 // Waiting here allows for two full buffers to be queued but not a
1005 // third. In the event that frames take varying time, this makes a
1006 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001007 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001008 }
1009
Brian Andersond6927fb2016-07-23 23:37:30 -07001010 // Update and get FrameEventHistory.
1011 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1012 NewFrameEventsEntry newFrameEventsEntry = {
1013 currentFrameNumber,
1014 postedTime,
1015 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001016 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001017 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001018 addAndGetFrameTimestamps(&newFrameEventsEntry,
1019 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001020
Dan Stoza289ade12014-02-28 11:17:17 -08001021 return NO_ERROR;
1022}
1023
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001024status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001025 ATRACE_CALL();
1026 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001027 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001028
1029 if (mCore->mIsAbandoned) {
1030 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001031 return NO_INIT;
1032 }
1033
1034 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1035 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1036 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001037 }
1038
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001039 if (mCore->mSharedBufferMode) {
1040 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001041 return BAD_VALUE;
1042 }
1043
Dan Stoza3e96f192014-03-03 10:16:19 -08001044 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001045 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001046 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001047 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001048 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001049 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001050 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001051 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001052 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001053 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001054 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001055 }
1056
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001057 mSlots[slot].mBufferState.cancel();
1058
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001059 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001060 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001061 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001062 mSlots[slot].mBufferState.mShared = false;
1063 }
1064
1065 // Don't put the shared buffer on the free list.
1066 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001067 mCore->mActiveBuffers.erase(slot);
1068 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001069 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001070
Dan Stoza289ade12014-02-28 11:17:17 -08001071 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001072 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001073 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001074
1075 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001076}
1077
1078int BufferQueueProducer::query(int what, int *outValue) {
1079 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001080 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001081
Yi Kong48a619f2018-06-05 16:34:59 -07001082 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001083 BQ_LOGE("query: outValue was NULL");
1084 return BAD_VALUE;
1085 }
1086
1087 if (mCore->mIsAbandoned) {
1088 BQ_LOGE("query: BufferQueue has been abandoned");
1089 return NO_INIT;
1090 }
1091
1092 int value;
1093 switch (what) {
1094 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001095 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001096 break;
1097 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001098 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001099 break;
1100 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001101 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001102 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001103 case NATIVE_WINDOW_LAYER_COUNT:
1104 // All BufferQueue buffers have a single layer.
1105 value = BQ_LAYER_COUNT;
1106 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001107 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001108 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001109 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001110 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001111 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001112 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001113 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1114 value = (mCore->mQueue.size() > 1);
1115 break;
1116 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001117 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001118 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001119 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001120 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1121 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1122 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001123 case NATIVE_WINDOW_BUFFER_AGE:
1124 if (mCore->mBufferAge > INT32_MAX) {
1125 value = 0;
1126 } else {
1127 value = static_cast<int32_t>(mCore->mBufferAge);
1128 }
1129 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001130 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1131 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1132 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001133 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1134 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1135 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001136 default:
1137 return BAD_VALUE;
1138 }
1139
1140 BQ_LOGV("query: %d? %d", what, value);
1141 *outValue = value;
1142 return NO_ERROR;
1143}
1144
Dan Stozaf0eaf252014-03-21 13:05:51 -07001145status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001146 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1147 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001148 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001149 mConsumerName = mCore->mConsumerName;
1150 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1151 producerControlledByApp ? "true" : "false");
1152
1153 if (mCore->mIsAbandoned) {
1154 BQ_LOGE("connect: BufferQueue has been abandoned");
1155 return NO_INIT;
1156 }
1157
Yi Kong48a619f2018-06-05 16:34:59 -07001158 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001159 BQ_LOGE("connect: BufferQueue has no consumer");
1160 return NO_INIT;
1161 }
1162
Yi Kong48a619f2018-06-05 16:34:59 -07001163 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001164 BQ_LOGE("connect: output was NULL");
1165 return BAD_VALUE;
1166 }
1167
1168 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1169 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1170 mCore->mConnectedApi, api);
1171 return BAD_VALUE;
1172 }
1173
1174 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1175 mDequeueTimeout < 0 ?
1176 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1177 mCore->mMaxBufferCount) -
1178 mCore->getMaxBufferCountLocked();
1179 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1180 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1181 "slots. Delta = %d", delta);
1182 return BAD_VALUE;
1183 }
1184
Dan Stoza289ade12014-02-28 11:17:17 -08001185 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001186 switch (api) {
1187 case NATIVE_WINDOW_API_EGL:
1188 case NATIVE_WINDOW_API_CPU:
1189 case NATIVE_WINDOW_API_MEDIA:
1190 case NATIVE_WINDOW_API_CAMERA:
1191 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001192
1193 output->width = mCore->mDefaultWidth;
1194 output->height = mCore->mDefaultHeight;
1195 output->transformHint = mCore->mTransformHint;
1196 output->numPendingBuffers =
1197 static_cast<uint32_t>(mCore->mQueue.size());
1198 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001199 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001200
Yi Kong48a619f2018-06-05 16:34:59 -07001201 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001202 // Set up a death notification so that we can disconnect
1203 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001204 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001205 status = IInterface::asBinder(listener)->linkToDeath(
1206 static_cast<IBinder::DeathRecipient*>(this));
1207 if (status != NO_ERROR) {
1208 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1209 strerror(-status), status);
1210 }
1211 mCore->mLinkedToDeath = listener;
1212 }
1213 if (listener->needsReleaseNotify()) {
1214 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001215 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001216 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001217 break;
1218 default:
1219 BQ_LOGE("connect: unknown API %d", api);
1220 status = BAD_VALUE;
1221 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001222 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001223 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001224 mCore->mBufferHasBeenQueued = false;
1225 mCore->mDequeueBufferCannotBlock = false;
1226 if (mDequeueTimeout < 0) {
1227 mCore->mDequeueBufferCannotBlock =
1228 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001229 }
Dan Stoza289ade12014-02-28 11:17:17 -08001230
Pablo Ceballos981066c2016-02-18 12:54:37 -08001231 mCore->mAllowAllocation = true;
1232 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001233 return status;
1234}
1235
Robert Carr97b9c862016-09-08 13:54:35 -07001236status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001237 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001238 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001239
1240 int status = NO_ERROR;
1241 sp<IConsumerListener> listener;
1242 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001243 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001244
1245 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001246 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001247 return NO_ERROR;
1248 }
1249 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1250 }
1251
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001252 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001253
1254 if (mCore->mIsAbandoned) {
1255 // It's not really an error to disconnect after the surface has
1256 // been abandoned; it should just be a no-op.
1257 return NO_ERROR;
1258 }
1259
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001260 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001261 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1262 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1263 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001264 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001265 // If we're asked to disconnect the currently connected api but
1266 // nobody is connected, it's not really an error.
1267 if (api == BufferQueueCore::NO_CONNECTED_API) {
1268 return NO_ERROR;
1269 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001270 }
1271
Dan Stoza289ade12014-02-28 11:17:17 -08001272 switch (api) {
1273 case NATIVE_WINDOW_API_EGL:
1274 case NATIVE_WINDOW_API_CPU:
1275 case NATIVE_WINDOW_API_MEDIA:
1276 case NATIVE_WINDOW_API_CAMERA:
1277 if (mCore->mConnectedApi == api) {
1278 mCore->freeAllBuffersLocked();
1279
1280 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001281 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001282 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001283 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001284 // This can fail if we're here because of the death
1285 // notification, but we just ignore it
1286 token->unlinkToDeath(
1287 static_cast<IBinder::DeathRecipient*>(this));
1288 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001289 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001290 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001291 mCore->mLinkedToDeath = nullptr;
1292 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001293 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001294 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001295 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001296 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001297 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001298 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1299 BQ_LOGE("disconnect: not connected (req=%d)", api);
1300 status = NO_INIT;
1301 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001302 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001303 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001304 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001305 }
1306 break;
1307 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001308 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001309 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001310 break;
1311 }
1312 } // Autolock scope
1313
1314 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001315 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001316 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001317 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001318 }
1319
1320 return status;
1321}
1322
Jesse Hall399184a2014-03-03 15:42:54 -08001323status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001324 sp<IConsumerListener> listener;
1325 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001326 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001327 mCore->mSidebandStream = stream;
1328 listener = mCore->mConsumerListener;
1329 } // Autolock scope
1330
Yi Kong48a619f2018-06-05 16:34:59 -07001331 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001332 listener->onSidebandStreamChanged();
1333 }
Jesse Hall399184a2014-03-03 15:42:54 -08001334 return NO_ERROR;
1335}
1336
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001337void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001338 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001339 ATRACE_CALL();
1340 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001341 size_t newBufferCount = 0;
1342 uint32_t allocWidth = 0;
1343 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001344 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001345 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001346 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001347 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001348 std::unique_lock<std::mutex> lock(mCore->mMutex);
1349 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001350
Dan Stoza9de72932015-04-16 17:28:43 -07001351 if (!mCore->mAllowAllocation) {
1352 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1353 "BufferQueue");
1354 return;
1355 }
1356
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001357 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1358 // both allocateBuffers and dequeueBuffer.
1359 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001360 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001361 return;
1362 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001363
Antoine Labour78014f32014-07-15 21:17:03 -07001364 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1365 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1366 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1367 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001368 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001369
1370 mCore->mIsAllocating = true;
1371 } // Autolock scope
1372
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001373 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001374 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001375 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001376 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001377 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001378
1379 status_t result = graphicBuffer->initCheck();
1380
Antoine Labour78014f32014-07-15 21:17:03 -07001381 if (result != NO_ERROR) {
1382 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001383 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001384 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001385 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001386 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001387 return;
1388 }
1389 buffers.push_back(graphicBuffer);
1390 }
1391
1392 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001393 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001394 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1395 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001396 PixelFormat checkFormat = format != 0 ?
1397 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001398 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001399 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1400 checkFormat != allocFormat || checkUsage != allocUsage) {
1401 // Something changed while we released the lock. Retry.
1402 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1403 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001404 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001405 continue;
1406 }
1407
Antoine Labour78014f32014-07-15 21:17:03 -07001408 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001409 if (mCore->mFreeSlots.empty()) {
1410 BQ_LOGV("allocateBuffers: a slot was occupied while "
1411 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001412 continue;
1413 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001414 auto slot = mCore->mFreeSlots.begin();
1415 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1416 mSlots[*slot].mGraphicBuffer = buffers[i];
1417 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001418
1419 // freeBufferLocked puts this slot on the free slots list. Since
1420 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001421 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001422
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001423 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1424 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001425
1426 // Make sure the erase is done after all uses of the slot
1427 // iterator since it will be invalid after this point.
1428 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001429 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001430
Antoine Labour78014f32014-07-15 21:17:03 -07001431 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001432 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001433 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001434
1435 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1436 // waiting anymore so it can use the buffer we just allocated.
1437 while (mDequeueWaitingForAllocation) {
1438 mDequeueWaitingForAllocationCondition.wait(lock);
1439 }
Antoine Labour78014f32014-07-15 21:17:03 -07001440 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001441 }
1442}
1443
Dan Stoza9de72932015-04-16 17:28:43 -07001444status_t BufferQueueProducer::allowAllocation(bool allow) {
1445 ATRACE_CALL();
1446 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1447
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001448 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001449 mCore->mAllowAllocation = allow;
1450 return NO_ERROR;
1451}
1452
Dan Stoza812ed062015-06-02 15:45:22 -07001453status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1454 ATRACE_CALL();
1455 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1456
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001457 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001458 mCore->mGenerationNumber = generationNumber;
1459 return NO_ERROR;
1460}
1461
Dan Stozac6f30bd2015-06-08 09:32:50 -07001462String8 BufferQueueProducer::getConsumerName() const {
1463 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001464 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001465 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1466 return mConsumerName;
1467}
1468
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001469status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001470 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001471 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001472
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001473 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001474 if (!sharedBufferMode) {
1475 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001476 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001477 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001478 return NO_ERROR;
1479}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001480
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001481status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1482 ATRACE_CALL();
1483 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1484
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001485 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001486
1487 mCore->mAutoRefresh = autoRefresh;
1488 return NO_ERROR;
1489}
1490
Dan Stoza127fc632015-06-30 13:43:32 -07001491status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1492 ATRACE_CALL();
1493 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1494
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001495 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001496 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1497 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1498 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1499 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1500 "available slots. Delta = %d", delta);
1501 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001502 }
1503
Pablo Ceballos981066c2016-02-18 12:54:37 -08001504 mDequeueTimeout = timeout;
1505 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001506
Pablo Ceballos981066c2016-02-18 12:54:37 -08001507 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001508 return NO_ERROR;
1509}
1510
Dan Stoza50101d02016-04-07 16:53:23 -07001511status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001512 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001513 ATRACE_CALL();
1514 BQ_LOGV("getLastQueuedBuffer");
1515
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001516 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001517 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1518 *outBuffer = nullptr;
1519 *outFence = Fence::NO_FENCE;
1520 return NO_ERROR;
1521 }
1522
1523 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1524 *outFence = mLastQueueBufferFence;
1525
John Reck1a61da52016-04-28 13:18:15 -07001526 // Currently only SurfaceFlinger internally ever changes
1527 // GLConsumer's filtering mode, so we just use 'true' here as
1528 // this is slightly specialized for the current client of this API,
1529 // which does want filtering.
1530 GLConsumer::computeTransformMatrix(outTransformMatrix,
1531 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1532 mLastQueuedTransform, true /* filter */);
1533
Dan Stoza50101d02016-04-07 16:53:23 -07001534 return NO_ERROR;
1535}
1536
Brian Anderson3890c392016-07-25 12:48:08 -07001537void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1538 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001539}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001540
Brian Anderson3890c392016-07-25 12:48:08 -07001541void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001542 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001543 FrameEventHistoryDelta* outDelta) {
1544 if (newTimestamps == nullptr && outDelta == nullptr) {
1545 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001546 }
1547
1548 ATRACE_CALL();
1549 BQ_LOGV("addAndGetFrameTimestamps");
1550 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001551 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001552 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001553 listener = mCore->mConsumerListener;
1554 }
Yi Kong48a619f2018-06-05 16:34:59 -07001555 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001556 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001557 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001558}
1559
Dan Stoza289ade12014-02-28 11:17:17 -08001560void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1561 // If we're here, it means that a producer we were connected to died.
1562 // We're guaranteed that we are still connected to it because we remove
1563 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1564 // without synchronization here.
1565 int api = mCore->mConnectedApi;
1566 disconnect(api);
1567}
1568
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001569status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1570 BQ_LOGV("getUniqueId");
1571
1572 *outId = mCore->mUniqueId;
1573 return NO_ERROR;
1574}
1575
Chia-I Wue2786ea2017-08-07 10:36:08 -07001576status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1577 BQ_LOGV("getConsumerUsage");
1578
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001579 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001580 *outUsage = mCore->mConsumerUsageBits;
1581 return NO_ERROR;
1582}
1583
Dan Stoza289ade12014-02-28 11:17:17 -08001584} // namespace android