blob: 72ae3758c8797cd11a7f9c211c98c5525fdacb73 [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 }
Dan Stoza289ade12014-02-28 11:17:17 -0800395
396 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
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700542 if (mCore->mConsumerListener != nullptr) {
543 BufferItem item;
544 item.mGraphicBuffer = graphicBuffer;
545 item.mSlot = *outSlot;
546 mCore->mConsumerListener->onBufferAllocated(item);
547 }
548
Pablo Ceballos9e314332016-01-12 13:49:19 -0800549 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800550 } // Autolock scope
551 }
552
Dan Stoza9f3053d2014-03-06 15:14:33 -0800553 if (attachedByConsumer) {
554 returnFlags |= BUFFER_NEEDS_REALLOCATION;
555 }
556
Dan Stoza289ade12014-02-28 11:17:17 -0800557 if (eglFence != EGL_NO_SYNC_KHR) {
558 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
559 1000000000);
560 // If something goes wrong, log the error, but return the buffer without
561 // synchronizing access to it. It's too late at this point to abort the
562 // dequeue operation.
563 if (result == EGL_FALSE) {
564 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
565 eglGetError());
566 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
567 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
568 }
569 eglDestroySyncKHR(eglDisplay, eglFence);
570 }
571
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700572 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
573 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800574 mSlots[*outSlot].mFrameNumber,
575 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
576
Ian Elliottd11b0442017-07-18 11:05:49 -0600577 if (outBufferAge) {
578 *outBufferAge = mCore->mBufferAge;
579 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700580 addAndGetFrameTimestamps(nullptr, outTimestamps);
581
Dan Stoza289ade12014-02-28 11:17:17 -0800582 return returnFlags;
583}
584
Dan Stoza9f3053d2014-03-06 15:14:33 -0800585status_t BufferQueueProducer::detachBuffer(int slot) {
586 ATRACE_CALL();
587 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800589
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700590 sp<IConsumerListener> listener;
591 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200592 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700593
594 if (mCore->mIsAbandoned) {
595 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
596 return NO_INIT;
597 }
598
599 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
600 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
601 return NO_INIT;
602 }
603
604 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
605 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
606 return BAD_VALUE;
607 }
608
609 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
610 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
611 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
612 return BAD_VALUE;
613 } else if (!mSlots[slot].mBufferState.isDequeued()) {
614 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
615 "(state = %s)", slot, mSlots[slot].mBufferState.string());
616 return BAD_VALUE;
617 } else if (!mSlots[slot].mRequestBufferCalled) {
618 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
619 slot);
620 return BAD_VALUE;
621 }
622
623 mSlots[slot].mBufferState.detachProducer();
624 mCore->mActiveBuffers.erase(slot);
625 mCore->mFreeSlots.insert(slot);
626 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200627 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700628 VALIDATE_CONSISTENCY();
629 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630 }
631
Yi Kong48a619f2018-06-05 16:34:59 -0700632 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700633 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700634 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800635
Dan Stoza9f3053d2014-03-06 15:14:33 -0800636 return NO_ERROR;
637}
638
Dan Stozad9822a32014-03-28 15:25:31 -0700639status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
640 sp<Fence>* outFence) {
641 ATRACE_CALL();
642
Yi Kong48a619f2018-06-05 16:34:59 -0700643 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700644 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
645 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700646 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700647 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
648 return BAD_VALUE;
649 }
650
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700651 sp<IConsumerListener> listener;
652 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200653 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700654
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700655 if (mCore->mIsAbandoned) {
656 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
657 return NO_INIT;
658 }
659
660 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
661 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
662 return NO_INIT;
663 }
664
665 if (mCore->mSharedBufferMode) {
666 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
667 "mode");
668 return BAD_VALUE;
669 }
670
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200671 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700672
673 if (mCore->mFreeBuffers.empty()) {
674 return NO_MEMORY;
675 }
676
677 int found = mCore->mFreeBuffers.front();
678 mCore->mFreeBuffers.remove(found);
679 mCore->mFreeSlots.insert(found);
680
681 BQ_LOGV("detachNextBuffer detached slot %d", found);
682
683 *outBuffer = mSlots[found].mGraphicBuffer;
684 *outFence = mSlots[found].mFence;
685 mCore->clearBufferSlotLocked(found);
686 VALIDATE_CONSISTENCY();
687 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700688 }
689
Yi Kong48a619f2018-06-05 16:34:59 -0700690 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700691 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700692 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800693
Dan Stozad9822a32014-03-28 15:25:31 -0700694 return NO_ERROR;
695}
696
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697status_t BufferQueueProducer::attachBuffer(int* outSlot,
698 const sp<android::GraphicBuffer>& buffer) {
699 ATRACE_CALL();
700
Yi Kong48a619f2018-06-05 16:34:59 -0700701 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700702 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700704 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700705 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800706 return BAD_VALUE;
707 }
708
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200709 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700710
711 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700712 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700713 return NO_INIT;
714 }
715
716 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700717 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700718 return NO_INIT;
719 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800720
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700721 if (mCore->mSharedBufferMode) {
722 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700723 return BAD_VALUE;
724 }
725
Dan Stoza812ed062015-06-02 15:45:22 -0700726 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
727 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
728 "[queue %u]", buffer->getGenerationNumber(),
729 mCore->mGenerationNumber);
730 return BAD_VALUE;
731 }
732
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200733 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700734
Dan Stoza9f3053d2014-03-06 15:14:33 -0800735 status_t returnFlags = NO_ERROR;
736 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200737 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 if (status != NO_ERROR) {
739 return status;
740 }
741
742 // This should not happen
743 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700744 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800745 return -EBUSY;
746 }
747
748 *outSlot = found;
749 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700750 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800751 *outSlot, returnFlags);
752
753 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700754 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800755 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
756 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700757 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800758 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800759 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800760 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800761 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700762
Dan Stoza9f3053d2014-03-06 15:14:33 -0800763 return returnFlags;
764}
765
Dan Stoza289ade12014-02-28 11:17:17 -0800766status_t BufferQueueProducer::queueBuffer(int slot,
767 const QueueBufferInput &input, QueueBufferOutput *output) {
768 ATRACE_CALL();
769 ATRACE_BUFFER_INDEX(slot);
770
Brian Andersond6927fb2016-07-23 23:37:30 -0700771 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800773 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700774 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800775 int scalingMode;
776 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700777 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700778 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700779 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700780 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700781 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
782 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700783 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700784 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800785
Yi Kong48a619f2018-06-05 16:34:59 -0700786 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800787 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800788 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800789 }
790
Brian Anderson3d4039d2016-09-23 16:31:30 -0700791 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
792
Dan Stoza289ade12014-02-28 11:17:17 -0800793 switch (scalingMode) {
794 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
795 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
796 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
797 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
798 break;
799 default:
800 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 }
803
Dan Stoza8dc55392014-11-04 11:37:46 -0800804 sp<IConsumerListener> frameAvailableListener;
805 sp<IConsumerListener> frameReplacedListener;
806 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700807 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800808 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800809 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200810 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800811
812 if (mCore->mIsAbandoned) {
813 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
814 return NO_INIT;
815 }
816
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700817 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
818 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
819 return NO_INIT;
820 }
821
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800822 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800823 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800824 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800825 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700826 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800827 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700828 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800829 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800830 } else if (!mSlots[slot].mRequestBufferCalled) {
831 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
832 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800833 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800834 }
835
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700836 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700837 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700838 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700839 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700840 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700841 mSlots[slot].mBufferState.mShared = true;
842 }
843
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800844 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700845 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
846 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
847 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700848 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800849 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800850
851 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
852 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700853 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800854 crop.intersect(bufferRect, &croppedRect);
855 if (croppedRect != crop) {
856 BQ_LOGE("queueBuffer: crop rect is not contained within the "
857 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800858 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800859 }
860
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800861 // Override UNKNOWN dataspace with consumer default
862 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
863 dataSpace = mCore->mDefaultBufferDataSpace;
864 }
865
Brian Andersond6927fb2016-07-23 23:37:30 -0700866 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700867 mSlots[slot].mBufferState.queue();
868
Brian Andersond6927fb2016-07-23 23:37:30 -0700869 // Increment the frame counter and store a local version of it
870 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800871 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700872 currentFrameNumber = mCore->mFrameCounter;
873 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800874
Dan Stoza289ade12014-02-28 11:17:17 -0800875 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
876 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
877 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800878 item.mTransform = transform &
879 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800880 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800881 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
882 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700883 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800884 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800885 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700886 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700887 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800888 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700889 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700890 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700891 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700892 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700893 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700894 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700895 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700896 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800897 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800898
Ruben Brunk1681d952014-06-27 15:51:55 -0700899 mStickyTransform = stickyTransform;
900
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700901 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700902 if (mCore->mSharedBufferMode) {
903 mCore->mSharedBufferCache.crop = crop;
904 mCore->mSharedBufferCache.transform = transform;
905 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700906 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700907 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700908 }
909
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800910 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800911 if (mCore->mQueue.empty()) {
912 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
913 // and simply queue this buffer
914 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800915 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800916 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700917 // When the queue is not empty, we need to look at the last buffer
918 // in the queue to see if we need to replace it
919 const BufferItem& last = mCore->mQueue.itemAt(
920 mCore->mQueue.size() - 1);
921 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800922
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700923 if (!last.mIsStale) {
924 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700925
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700926 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700927 // still be around. Mark it as no longer shared if this
928 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700929 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700930 mSlots[last.mSlot].mBufferState.isFree()) {
931 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700932 }
933 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700934 if (!mSlots[last.mSlot].mBufferState.isShared()) {
935 mCore->mActiveBuffers.erase(last.mSlot);
936 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800937 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700938 }
Dan Stoza289ade12014-02-28 11:17:17 -0800939 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800940
Dan Stoza289ade12014-02-28 11:17:17 -0800941 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700942 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800943 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800944 } else {
945 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800946 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800947 }
948 }
949
950 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200951 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700952 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800953
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700954 output->width = mCore->mDefaultWidth;
955 output->height = mCore->mDefaultHeight;
956 output->transformHint = mCore->mTransformHint;
957 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
958 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800959
Colin Cross152c3b72016-09-27 14:08:19 -0700960 ATRACE_INT(mCore->mConsumerName.string(),
961 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700962 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800963
964 // Take a ticket for the callback functions
965 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700966
Pablo Ceballos9e314332016-01-12 13:49:19 -0800967 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800968 } // Autolock scope
969
Irvel468051e2016-06-13 16:44:44 -0700970 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
971 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
972 // there will be no Binder call
973 if (!mConsumerIsSurfaceFlinger) {
974 item.mGraphicBuffer.clear();
975 }
976
977 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800978 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
979
980 // Call back without the main BufferQueue lock held, but with the callback
981 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700982
983 int connectedApi;
984 sp<Fence> lastQueuedFence;
985
986 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200987 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -0800988 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200989 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -0800990 }
991
Yi Kong48a619f2018-06-05 16:34:59 -0700992 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800993 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -0700994 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800995 frameReplacedListener->onFrameReplaced(item);
996 }
997
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700998 connectedApi = mCore->mConnectedApi;
999 lastQueuedFence = std::move(mLastQueueBufferFence);
1000
1001 mLastQueueBufferFence = std::move(acquireFence);
1002 mLastQueuedCrop = item.mCrop;
1003 mLastQueuedTransform = item.mTransform;
1004
Dan Stoza8dc55392014-11-04 11:37:46 -08001005 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001006 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001007 }
1008
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001009 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001010 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001011 // Waiting here allows for two full buffers to be queued but not a
1012 // third. In the event that frames take varying time, this makes a
1013 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001014 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001015 }
1016
Brian Andersond6927fb2016-07-23 23:37:30 -07001017 // Update and get FrameEventHistory.
1018 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1019 NewFrameEventsEntry newFrameEventsEntry = {
1020 currentFrameNumber,
1021 postedTime,
1022 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001023 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001024 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001025 addAndGetFrameTimestamps(&newFrameEventsEntry,
1026 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001027
Dan Stoza289ade12014-02-28 11:17:17 -08001028 return NO_ERROR;
1029}
1030
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001031status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001032 ATRACE_CALL();
1033 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001034 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001035
1036 if (mCore->mIsAbandoned) {
1037 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001038 return NO_INIT;
1039 }
1040
1041 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1042 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1043 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001044 }
1045
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001046 if (mCore->mSharedBufferMode) {
1047 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001048 return BAD_VALUE;
1049 }
1050
Dan Stoza3e96f192014-03-03 10:16:19 -08001051 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001052 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001053 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001054 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001055 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001056 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001057 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001058 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001059 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001060 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001061 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001062 }
1063
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001064 mSlots[slot].mBufferState.cancel();
1065
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001066 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001067 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001068 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001069 mSlots[slot].mBufferState.mShared = false;
1070 }
1071
1072 // Don't put the shared buffer on the free list.
1073 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001074 mCore->mActiveBuffers.erase(slot);
1075 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001076 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001077
Dan Stoza289ade12014-02-28 11:17:17 -08001078 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001079 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001080 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001081
1082 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001083}
1084
1085int BufferQueueProducer::query(int what, int *outValue) {
1086 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001087 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001088
Yi Kong48a619f2018-06-05 16:34:59 -07001089 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001090 BQ_LOGE("query: outValue was NULL");
1091 return BAD_VALUE;
1092 }
1093
1094 if (mCore->mIsAbandoned) {
1095 BQ_LOGE("query: BufferQueue has been abandoned");
1096 return NO_INIT;
1097 }
1098
1099 int value;
1100 switch (what) {
1101 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001102 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001103 break;
1104 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001105 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001106 break;
1107 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001108 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001109 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001110 case NATIVE_WINDOW_LAYER_COUNT:
1111 // All BufferQueue buffers have a single layer.
1112 value = BQ_LAYER_COUNT;
1113 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001114 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001115 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001116 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001117 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001118 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001119 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001120 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1121 value = (mCore->mQueue.size() > 1);
1122 break;
1123 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001124 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001125 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001126 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001127 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1128 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1129 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001130 case NATIVE_WINDOW_BUFFER_AGE:
1131 if (mCore->mBufferAge > INT32_MAX) {
1132 value = 0;
1133 } else {
1134 value = static_cast<int32_t>(mCore->mBufferAge);
1135 }
1136 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001137 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1138 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1139 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001140 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1141 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1142 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001143 default:
1144 return BAD_VALUE;
1145 }
1146
1147 BQ_LOGV("query: %d? %d", what, value);
1148 *outValue = value;
1149 return NO_ERROR;
1150}
1151
Dan Stozaf0eaf252014-03-21 13:05:51 -07001152status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001153 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1154 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001155 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001156 mConsumerName = mCore->mConsumerName;
1157 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1158 producerControlledByApp ? "true" : "false");
1159
1160 if (mCore->mIsAbandoned) {
1161 BQ_LOGE("connect: BufferQueue has been abandoned");
1162 return NO_INIT;
1163 }
1164
Yi Kong48a619f2018-06-05 16:34:59 -07001165 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001166 BQ_LOGE("connect: BufferQueue has no consumer");
1167 return NO_INIT;
1168 }
1169
Yi Kong48a619f2018-06-05 16:34:59 -07001170 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001171 BQ_LOGE("connect: output was NULL");
1172 return BAD_VALUE;
1173 }
1174
1175 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1176 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1177 mCore->mConnectedApi, api);
1178 return BAD_VALUE;
1179 }
1180
1181 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1182 mDequeueTimeout < 0 ?
1183 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1184 mCore->mMaxBufferCount) -
1185 mCore->getMaxBufferCountLocked();
1186 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1187 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1188 "slots. Delta = %d", delta);
1189 return BAD_VALUE;
1190 }
1191
Dan Stoza289ade12014-02-28 11:17:17 -08001192 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001193 switch (api) {
1194 case NATIVE_WINDOW_API_EGL:
1195 case NATIVE_WINDOW_API_CPU:
1196 case NATIVE_WINDOW_API_MEDIA:
1197 case NATIVE_WINDOW_API_CAMERA:
1198 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001199
1200 output->width = mCore->mDefaultWidth;
1201 output->height = mCore->mDefaultHeight;
1202 output->transformHint = mCore->mTransformHint;
1203 output->numPendingBuffers =
1204 static_cast<uint32_t>(mCore->mQueue.size());
1205 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001206 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001207
Yi Kong48a619f2018-06-05 16:34:59 -07001208 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001209 // Set up a death notification so that we can disconnect
1210 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001211 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001212 status = IInterface::asBinder(listener)->linkToDeath(
1213 static_cast<IBinder::DeathRecipient*>(this));
1214 if (status != NO_ERROR) {
1215 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1216 strerror(-status), status);
1217 }
1218 mCore->mLinkedToDeath = listener;
1219 }
1220 if (listener->needsReleaseNotify()) {
1221 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001222 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001223 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001224 break;
1225 default:
1226 BQ_LOGE("connect: unknown API %d", api);
1227 status = BAD_VALUE;
1228 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001229 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001230 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001231 mCore->mBufferHasBeenQueued = false;
1232 mCore->mDequeueBufferCannotBlock = false;
1233 if (mDequeueTimeout < 0) {
1234 mCore->mDequeueBufferCannotBlock =
1235 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001236 }
Dan Stoza289ade12014-02-28 11:17:17 -08001237
Pablo Ceballos981066c2016-02-18 12:54:37 -08001238 mCore->mAllowAllocation = true;
1239 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001240 return status;
1241}
1242
Robert Carr97b9c862016-09-08 13:54:35 -07001243status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001244 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001245 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001246
1247 int status = NO_ERROR;
1248 sp<IConsumerListener> listener;
1249 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001250 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001251
1252 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001253 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001254 return NO_ERROR;
1255 }
1256 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1257 }
1258
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001259 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001260
1261 if (mCore->mIsAbandoned) {
1262 // It's not really an error to disconnect after the surface has
1263 // been abandoned; it should just be a no-op.
1264 return NO_ERROR;
1265 }
1266
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001267 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001268 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1269 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1270 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001271 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001272 // If we're asked to disconnect the currently connected api but
1273 // nobody is connected, it's not really an error.
1274 if (api == BufferQueueCore::NO_CONNECTED_API) {
1275 return NO_ERROR;
1276 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001277 }
1278
Dan Stoza289ade12014-02-28 11:17:17 -08001279 switch (api) {
1280 case NATIVE_WINDOW_API_EGL:
1281 case NATIVE_WINDOW_API_CPU:
1282 case NATIVE_WINDOW_API_MEDIA:
1283 case NATIVE_WINDOW_API_CAMERA:
1284 if (mCore->mConnectedApi == api) {
1285 mCore->freeAllBuffersLocked();
1286
1287 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001288 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001289 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001290 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001291 // This can fail if we're here because of the death
1292 // notification, but we just ignore it
1293 token->unlinkToDeath(
1294 static_cast<IBinder::DeathRecipient*>(this));
1295 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001296 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001297 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001298 mCore->mLinkedToDeath = nullptr;
1299 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001300 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001301 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001302 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001303 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001304 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001305 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1306 BQ_LOGE("disconnect: not connected (req=%d)", api);
1307 status = NO_INIT;
1308 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001309 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001310 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001311 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001312 }
1313 break;
1314 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001315 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001316 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001317 break;
1318 }
1319 } // Autolock scope
1320
1321 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001322 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001323 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001324 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001325 }
1326
1327 return status;
1328}
1329
Jesse Hall399184a2014-03-03 15:42:54 -08001330status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001331 sp<IConsumerListener> listener;
1332 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001333 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001334 mCore->mSidebandStream = stream;
1335 listener = mCore->mConsumerListener;
1336 } // Autolock scope
1337
Yi Kong48a619f2018-06-05 16:34:59 -07001338 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001339 listener->onSidebandStreamChanged();
1340 }
Jesse Hall399184a2014-03-03 15:42:54 -08001341 return NO_ERROR;
1342}
1343
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001344void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001345 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001346 ATRACE_CALL();
1347 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001348 size_t newBufferCount = 0;
1349 uint32_t allocWidth = 0;
1350 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001351 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001352 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001353 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001354 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001355 std::unique_lock<std::mutex> lock(mCore->mMutex);
1356 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001357
Dan Stoza9de72932015-04-16 17:28:43 -07001358 if (!mCore->mAllowAllocation) {
1359 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1360 "BufferQueue");
1361 return;
1362 }
1363
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001364 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1365 // both allocateBuffers and dequeueBuffer.
1366 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001367 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001368 return;
1369 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001370
Antoine Labour78014f32014-07-15 21:17:03 -07001371 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1372 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1373 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1374 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001375 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001376
1377 mCore->mIsAllocating = true;
1378 } // Autolock scope
1379
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001380 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001381 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001382 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001383 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001384 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001385
1386 status_t result = graphicBuffer->initCheck();
1387
Antoine Labour78014f32014-07-15 21:17:03 -07001388 if (result != NO_ERROR) {
1389 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001390 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001391 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001392 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001393 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001394 return;
1395 }
1396 buffers.push_back(graphicBuffer);
1397 }
1398
1399 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001400 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001401 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1402 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001403 PixelFormat checkFormat = format != 0 ?
1404 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001405 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001406 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1407 checkFormat != allocFormat || checkUsage != allocUsage) {
1408 // Something changed while we released the lock. Retry.
1409 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1410 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001411 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001412 continue;
1413 }
1414
Antoine Labour78014f32014-07-15 21:17:03 -07001415 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001416 if (mCore->mFreeSlots.empty()) {
1417 BQ_LOGV("allocateBuffers: a slot was occupied while "
1418 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001419 continue;
1420 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001421 auto slot = mCore->mFreeSlots.begin();
1422 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1423 mSlots[*slot].mGraphicBuffer = buffers[i];
1424 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001425
1426 // freeBufferLocked puts this slot on the free slots list. Since
1427 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001428 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001429
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001430 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1431 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001432
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001433 if (mCore->mConsumerListener != nullptr) {
1434 BufferItem item;
1435 item.mGraphicBuffer = buffers[i];
1436 item.mSlot = *slot;
1437 mCore->mConsumerListener->onBufferAllocated(item);
1438 }
1439
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001440 // Make sure the erase is done after all uses of the slot
1441 // iterator since it will be invalid after this point.
1442 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001443 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001444
Antoine Labour78014f32014-07-15 21:17:03 -07001445 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001446 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001447 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001448
1449 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1450 // waiting anymore so it can use the buffer we just allocated.
1451 while (mDequeueWaitingForAllocation) {
1452 mDequeueWaitingForAllocationCondition.wait(lock);
1453 }
Antoine Labour78014f32014-07-15 21:17:03 -07001454 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001455 }
1456}
1457
Dan Stoza9de72932015-04-16 17:28:43 -07001458status_t BufferQueueProducer::allowAllocation(bool allow) {
1459 ATRACE_CALL();
1460 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1461
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001462 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001463 mCore->mAllowAllocation = allow;
1464 return NO_ERROR;
1465}
1466
Dan Stoza812ed062015-06-02 15:45:22 -07001467status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1468 ATRACE_CALL();
1469 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1470
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001471 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001472 mCore->mGenerationNumber = generationNumber;
1473 return NO_ERROR;
1474}
1475
Dan Stozac6f30bd2015-06-08 09:32:50 -07001476String8 BufferQueueProducer::getConsumerName() const {
1477 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001478 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001479 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1480 return mConsumerName;
1481}
1482
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001483status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001484 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001485 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001486
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001487 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001488 if (!sharedBufferMode) {
1489 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001490 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001491 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001492 return NO_ERROR;
1493}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001494
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001495status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1496 ATRACE_CALL();
1497 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1498
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001499 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001500
1501 mCore->mAutoRefresh = autoRefresh;
1502 return NO_ERROR;
1503}
1504
Dan Stoza127fc632015-06-30 13:43:32 -07001505status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1506 ATRACE_CALL();
1507 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1508
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001509 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001510 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1511 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1512 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1513 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1514 "available slots. Delta = %d", delta);
1515 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001516 }
1517
Pablo Ceballos981066c2016-02-18 12:54:37 -08001518 mDequeueTimeout = timeout;
1519 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001520
Pablo Ceballos981066c2016-02-18 12:54:37 -08001521 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001522 return NO_ERROR;
1523}
1524
Dan Stoza50101d02016-04-07 16:53:23 -07001525status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001526 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001527 ATRACE_CALL();
1528 BQ_LOGV("getLastQueuedBuffer");
1529
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001530 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001531 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1532 *outBuffer = nullptr;
1533 *outFence = Fence::NO_FENCE;
1534 return NO_ERROR;
1535 }
1536
1537 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1538 *outFence = mLastQueueBufferFence;
1539
John Reck1a61da52016-04-28 13:18:15 -07001540 // Currently only SurfaceFlinger internally ever changes
1541 // GLConsumer's filtering mode, so we just use 'true' here as
1542 // this is slightly specialized for the current client of this API,
1543 // which does want filtering.
1544 GLConsumer::computeTransformMatrix(outTransformMatrix,
1545 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1546 mLastQueuedTransform, true /* filter */);
1547
Dan Stoza50101d02016-04-07 16:53:23 -07001548 return NO_ERROR;
1549}
1550
Brian Anderson3890c392016-07-25 12:48:08 -07001551void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1552 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001553}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001554
Brian Anderson3890c392016-07-25 12:48:08 -07001555void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001556 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001557 FrameEventHistoryDelta* outDelta) {
1558 if (newTimestamps == nullptr && outDelta == nullptr) {
1559 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001560 }
1561
1562 ATRACE_CALL();
1563 BQ_LOGV("addAndGetFrameTimestamps");
1564 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001565 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001566 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001567 listener = mCore->mConsumerListener;
1568 }
Yi Kong48a619f2018-06-05 16:34:59 -07001569 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001570 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001571 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001572}
1573
Dan Stoza289ade12014-02-28 11:17:17 -08001574void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1575 // If we're here, it means that a producer we were connected to died.
1576 // We're guaranteed that we are still connected to it because we remove
1577 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1578 // without synchronization here.
1579 int api = mCore->mConnectedApi;
1580 disconnect(api);
1581}
1582
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001583status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1584 BQ_LOGV("getUniqueId");
1585
1586 *outId = mCore->mUniqueId;
1587 return NO_ERROR;
1588}
1589
Chia-I Wue2786ea2017-08-07 10:36:08 -07001590status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1591 BQ_LOGV("getConsumerUsage");
1592
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001593 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001594 *outUsage = mCore->mConsumerUsageBits;
1595 return NO_ERROR;
1596}
1597
Dan Stoza289ade12014-02-28 11:17:17 -08001598} // namespace android