blob: a307d04a164ab682599cddc72473e70fb6dbbede [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) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700277 // Supress error logs when timeout is non-negative.
278 if (mDequeueTimeout < 0) {
279 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
280 "count (%d)", callerString,
281 mCore->mMaxDequeuedBufferCount);
282 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800283 return INVALID_OPERATION;
284 }
285
Dan Stoza0de7ea72015-04-23 13:20:51 -0700286 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
287
Dan Stozaae3c3682014-04-18 15:43:35 -0700288 // If we disconnect and reconnect quickly, we can be in a state where
289 // our slots are empty but we have many buffers in the queue. This can
290 // cause us to run out of memory if we outrun the consumer. Wait here if
291 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800292 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700293 bool tooManyBuffers = mCore->mQueue.size()
294 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700295 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800296 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700297 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700298 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700299 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700300 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700301 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700302 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700303 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 } else {
305 if (caller == FreeSlotCaller::Dequeue) {
306 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800307 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800308 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
309 *found = slot;
310 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800311 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800312 }
313 } else {
314 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800315 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800316 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
317 *found = slot;
318 } else {
319 *found = getFreeBufferLocked();
320 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700321 }
322 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700323 }
324
325 // If no buffer is found, or if the queue has too many buffers
326 // outstanding, wait for a buffer to be acquired or released, or for the
327 // max buffer count to change.
328 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
329 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800330 if (tryAgain) {
331 // Return an error if we're in non-blocking mode (producer and
332 // consumer are controlled by the application).
333 // However, the consumer is allowed to briefly acquire an extra
334 // buffer (which could cause us to have to wait here), which is
335 // okay, since it is only used to implement an atomic acquire +
336 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700337 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800338 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
339 return WOULD_BLOCK;
340 }
Dan Stoza127fc632015-06-30 13:43:32 -0700341 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200342 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
343 std::chrono::nanoseconds(mDequeueTimeout));
344 if (result == std::cv_status::timeout) {
345 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700346 }
347 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200348 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700349 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800350 }
351 } // while (tryAgain)
352
353 return NO_ERROR;
354}
355
Ian Elliottd11b0442017-07-18 11:05:49 -0600356status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
357 uint32_t width, uint32_t height, PixelFormat format,
358 uint64_t usage, uint64_t* outBufferAge,
359 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800360 ATRACE_CALL();
361 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200362 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800363 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700364
365 if (mCore->mIsAbandoned) {
366 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
367 return NO_INIT;
368 }
369
370 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
371 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
372 return NO_INIT;
373 }
Dan Stoza289ade12014-02-28 11:17:17 -0800374 } // Autolock scope
375
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700376 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800377
378 if ((width && !height) || (!width && height)) {
379 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
380 return BAD_VALUE;
381 }
382
383 status_t returnFlags = NO_ERROR;
384 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
385 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800387
388 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200389 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800390
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100391 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
392 // is finished such that we don't allocate in parallel.
393 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
394 mDequeueWaitingForAllocation = true;
395 mCore->waitWhileAllocatingLocked(lock);
396 mDequeueWaitingForAllocation = false;
397 mDequeueWaitingForAllocationCondition.notify_all();
398 }
Dan Stoza289ade12014-02-28 11:17:17 -0800399
400 if (format == 0) {
401 format = mCore->mDefaultBufferFormat;
402 }
403
404 // Enable the usage bits the consumer requested
405 usage |= mCore->mConsumerUsageBits;
406
Dan Stoza9de72932015-04-16 17:28:43 -0700407 const bool useDefaultSize = !width && !height;
408 if (useDefaultSize) {
409 width = mCore->mDefaultWidth;
410 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700411 if (mCore->mAutoPrerotation &&
412 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
413 std::swap(width, height);
414 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800415 }
Dan Stoza289ade12014-02-28 11:17:17 -0800416
Pablo Ceballos981066c2016-02-18 12:54:37 -0800417 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700418 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200419 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700420 if (status != NO_ERROR) {
421 return status;
422 }
423
424 // This should not happen
425 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
426 BQ_LOGE("dequeueBuffer: no available buffer slots");
427 return -EBUSY;
428 }
429
430 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
431
432 // If we are not allowed to allocate new buffers,
433 // waitForFreeSlotThenRelock must have returned a slot containing a
434 // buffer. If this buffer would require reallocation to meet the
435 // requested attributes, we free it and attempt to get another one.
436 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700437 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700438 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700439 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700440 return BAD_VALUE;
441 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 mCore->mFreeSlots.insert(found);
443 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700444 found = BufferItem::INVALID_BUFFER_SLOT;
445 continue;
446 }
447 }
Dan Stoza289ade12014-02-28 11:17:17 -0800448 }
449
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800450 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700451 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700452 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800453 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
454 "buffer");
455
456 return BAD_VALUE;
457 }
458
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700459 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800460 mCore->mActiveBuffers.insert(found);
461 }
Dan Stoza289ade12014-02-28 11:17:17 -0800462 *outSlot = found;
463 ATRACE_BUFFER_INDEX(found);
464
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800465 attachedByConsumer = mSlots[found].mNeedsReallocation;
466 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800467
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700468 mSlots[found].mBufferState.dequeue();
469
Yi Kong48a619f2018-06-05 16:34:59 -0700470 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700471 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800472 {
473 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700474 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800475 mSlots[found].mRequestBufferCalled = false;
476 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
477 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
478 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800479 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800481
482 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800483 } else {
484 // We add 1 because that will be the frame number when this buffer
485 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700486 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800487 }
488
Dan Stoza800b41a2015-04-28 14:20:04 -0700489 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
490 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800491
Yi Kong48a619f2018-06-05 16:34:59 -0700492 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800493 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
494 "slot=%d w=%d h=%d format=%u",
495 found, buffer->width, buffer->height, buffer->format);
496 }
497
498 eglDisplay = mSlots[found].mEglDisplay;
499 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700500 // Don't return a fence in shared buffer mode, except for the first
501 // frame.
502 *outFence = (mCore->mSharedBufferMode &&
503 mCore->mSharedBufferSlot == found) ?
504 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800505 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
506 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700507
508 // If shared buffer mode has just been enabled, cache the slot of the
509 // first buffer that is dequeued and mark it as the shared buffer.
510 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
511 BufferQueueCore::INVALID_BUFFER_SLOT) {
512 mCore->mSharedBufferSlot = found;
513 mSlots[found].mBufferState.mShared = true;
514 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700515
516 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
517 if (mCore->mConsumerListener != nullptr) {
518 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
519 }
520 }
Dan Stoza289ade12014-02-28 11:17:17 -0800521 } // Autolock scope
522
523 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700524 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700525 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700526 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700527 {mConsumerName.string(), mConsumerName.size()});
528
529 status_t error = graphicBuffer->initCheck();
530
Dan Stoza289ade12014-02-28 11:17:17 -0800531 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200532 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800533
Chia-I Wufeec3b12017-05-25 09:34:56 -0700534 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700535 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
536 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Adithya Srinivasana820af92019-11-01 13:55:17 -0700537 if (mCore->mConsumerListener != nullptr) {
538 mCore->mConsumerListener->onFrameDequeued(
539 mSlots[*outSlot].mGraphicBuffer->getId());
540 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700541 }
542
543 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200544 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700545
Chia-I Wufeec3b12017-05-25 09:34:56 -0700546 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700547 mCore->mFreeSlots.insert(*outSlot);
548 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700549 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
550 return error;
551 }
552
Dan Stoza289ade12014-02-28 11:17:17 -0800553 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700554 mCore->mFreeSlots.insert(*outSlot);
555 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800556 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
557 return NO_INIT;
558 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800559
Pablo Ceballos9e314332016-01-12 13:49:19 -0800560 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800561 } // Autolock scope
562 }
563
Dan Stoza9f3053d2014-03-06 15:14:33 -0800564 if (attachedByConsumer) {
565 returnFlags |= BUFFER_NEEDS_REALLOCATION;
566 }
567
Dan Stoza289ade12014-02-28 11:17:17 -0800568 if (eglFence != EGL_NO_SYNC_KHR) {
569 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
570 1000000000);
571 // If something goes wrong, log the error, but return the buffer without
572 // synchronizing access to it. It's too late at this point to abort the
573 // dequeue operation.
574 if (result == EGL_FALSE) {
575 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
576 eglGetError());
577 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
578 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
579 }
580 eglDestroySyncKHR(eglDisplay, eglFence);
581 }
582
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700583 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
584 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800585 mSlots[*outSlot].mFrameNumber,
586 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
587
Ian Elliottd11b0442017-07-18 11:05:49 -0600588 if (outBufferAge) {
589 *outBufferAge = mCore->mBufferAge;
590 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700591 addAndGetFrameTimestamps(nullptr, outTimestamps);
592
Dan Stoza289ade12014-02-28 11:17:17 -0800593 return returnFlags;
594}
595
Dan Stoza9f3053d2014-03-06 15:14:33 -0800596status_t BufferQueueProducer::detachBuffer(int slot) {
597 ATRACE_CALL();
598 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700599 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800600
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700601 sp<IConsumerListener> listener;
602 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200603 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700604
605 if (mCore->mIsAbandoned) {
606 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
607 return NO_INIT;
608 }
609
610 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
611 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
612 return NO_INIT;
613 }
614
615 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
616 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
617 return BAD_VALUE;
618 }
619
620 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
621 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
622 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
623 return BAD_VALUE;
624 } else if (!mSlots[slot].mBufferState.isDequeued()) {
625 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
626 "(state = %s)", slot, mSlots[slot].mBufferState.string());
627 return BAD_VALUE;
628 } else if (!mSlots[slot].mRequestBufferCalled) {
629 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
630 slot);
631 return BAD_VALUE;
632 }
633
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700634 listener = mCore->mConsumerListener;
635 if (listener != nullptr) {
636 listener->onFrameDetached(mSlots[slot].mGraphicBuffer->getId());
637 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700638 mSlots[slot].mBufferState.detachProducer();
639 mCore->mActiveBuffers.erase(slot);
640 mCore->mFreeSlots.insert(slot);
641 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200642 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700643 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800644 }
645
Yi Kong48a619f2018-06-05 16:34:59 -0700646 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700647 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700648 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800649
Dan Stoza9f3053d2014-03-06 15:14:33 -0800650 return NO_ERROR;
651}
652
Dan Stozad9822a32014-03-28 15:25:31 -0700653status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
654 sp<Fence>* outFence) {
655 ATRACE_CALL();
656
Yi Kong48a619f2018-06-05 16:34:59 -0700657 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700658 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
659 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700660 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700661 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
662 return BAD_VALUE;
663 }
664
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700665 sp<IConsumerListener> listener;
666 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200667 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700668
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700669 if (mCore->mIsAbandoned) {
670 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
671 return NO_INIT;
672 }
673
674 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
675 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
676 return NO_INIT;
677 }
678
679 if (mCore->mSharedBufferMode) {
680 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
681 "mode");
682 return BAD_VALUE;
683 }
684
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200685 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700686
687 if (mCore->mFreeBuffers.empty()) {
688 return NO_MEMORY;
689 }
690
691 int found = mCore->mFreeBuffers.front();
692 mCore->mFreeBuffers.remove(found);
693 mCore->mFreeSlots.insert(found);
694
695 BQ_LOGV("detachNextBuffer detached slot %d", found);
696
697 *outBuffer = mSlots[found].mGraphicBuffer;
698 *outFence = mSlots[found].mFence;
699 mCore->clearBufferSlotLocked(found);
700 VALIDATE_CONSISTENCY();
701 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700702 }
703
Yi Kong48a619f2018-06-05 16:34:59 -0700704 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700705 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700706 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800707
Dan Stozad9822a32014-03-28 15:25:31 -0700708 return NO_ERROR;
709}
710
Dan Stoza9f3053d2014-03-06 15:14:33 -0800711status_t BufferQueueProducer::attachBuffer(int* outSlot,
712 const sp<android::GraphicBuffer>& buffer) {
713 ATRACE_CALL();
714
Yi Kong48a619f2018-06-05 16:34:59 -0700715 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700716 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800717 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700718 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700719 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800720 return BAD_VALUE;
721 }
722
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200723 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700724
725 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700726 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700727 return NO_INIT;
728 }
729
730 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700731 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700732 return NO_INIT;
733 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800734
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700735 if (mCore->mSharedBufferMode) {
736 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700737 return BAD_VALUE;
738 }
739
Dan Stoza812ed062015-06-02 15:45:22 -0700740 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
741 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
742 "[queue %u]", buffer->getGenerationNumber(),
743 mCore->mGenerationNumber);
744 return BAD_VALUE;
745 }
746
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200747 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700748
Dan Stoza9f3053d2014-03-06 15:14:33 -0800749 status_t returnFlags = NO_ERROR;
750 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200751 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800752 if (status != NO_ERROR) {
753 return status;
754 }
755
756 // This should not happen
757 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700758 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800759 return -EBUSY;
760 }
761
762 *outSlot = found;
763 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700764 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800765 *outSlot, returnFlags);
766
767 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700768 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800769 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
770 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700771 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800772 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800773 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800774 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800775 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700776
Dan Stoza9f3053d2014-03-06 15:14:33 -0800777 return returnFlags;
778}
779
Dan Stoza289ade12014-02-28 11:17:17 -0800780status_t BufferQueueProducer::queueBuffer(int slot,
781 const QueueBufferInput &input, QueueBufferOutput *output) {
782 ATRACE_CALL();
783 ATRACE_BUFFER_INDEX(slot);
784
Brian Andersond6927fb2016-07-23 23:37:30 -0700785 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800786 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800787 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700788 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800789 int scalingMode;
790 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700791 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700792 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700793 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700794 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700795 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
796 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700797 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700798 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800799
Yi Kong48a619f2018-06-05 16:34:59 -0700800 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800801 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800802 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800803 }
804
Brian Anderson3d4039d2016-09-23 16:31:30 -0700805 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
806
Dan Stoza289ade12014-02-28 11:17:17 -0800807 switch (scalingMode) {
808 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
809 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
810 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
811 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
812 break;
813 default:
814 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800815 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800816 }
817
Dan Stoza8dc55392014-11-04 11:37:46 -0800818 sp<IConsumerListener> frameAvailableListener;
819 sp<IConsumerListener> frameReplacedListener;
820 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700821 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800822 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800823 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200824 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800825
826 if (mCore->mIsAbandoned) {
827 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
828 return NO_INIT;
829 }
830
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700831 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
832 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
833 return NO_INIT;
834 }
835
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800836 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800837 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800838 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800839 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700840 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800841 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700842 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800843 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800844 } else if (!mSlots[slot].mRequestBufferCalled) {
845 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
846 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800847 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800848 }
849
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700850 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700851 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700852 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700853 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700854 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700855 mSlots[slot].mBufferState.mShared = true;
856 }
857
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800858 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700859 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
860 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
861 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700862 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800863 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800864
865 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
866 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700867 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800868 crop.intersect(bufferRect, &croppedRect);
869 if (croppedRect != crop) {
870 BQ_LOGE("queueBuffer: crop rect is not contained within the "
871 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800872 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800873 }
874
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800875 // Override UNKNOWN dataspace with consumer default
876 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
877 dataSpace = mCore->mDefaultBufferDataSpace;
878 }
879
Brian Andersond6927fb2016-07-23 23:37:30 -0700880 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700881 mSlots[slot].mBufferState.queue();
882
Brian Andersond6927fb2016-07-23 23:37:30 -0700883 // Increment the frame counter and store a local version of it
884 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800885 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700886 currentFrameNumber = mCore->mFrameCounter;
887 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800888
Dan Stoza289ade12014-02-28 11:17:17 -0800889 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
890 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
891 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800892 item.mTransform = transform &
893 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800894 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800895 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
896 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700897 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800898 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800899 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700900 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700901 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800902 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700903 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700904 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700905 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700906 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800907 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700908 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700909 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700910 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700911 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800912 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800913
Ruben Brunk1681d952014-06-27 15:51:55 -0700914 mStickyTransform = stickyTransform;
915
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700916 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700917 if (mCore->mSharedBufferMode) {
918 mCore->mSharedBufferCache.crop = crop;
919 mCore->mSharedBufferCache.transform = transform;
920 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700921 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700922 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700923 }
924
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800925 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800926 if (mCore->mQueue.empty()) {
927 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
928 // and simply queue this buffer
929 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800930 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800931 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700932 // When the queue is not empty, we need to look at the last buffer
933 // in the queue to see if we need to replace it
934 const BufferItem& last = mCore->mQueue.itemAt(
935 mCore->mQueue.size() - 1);
936 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800937
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700938 if (!last.mIsStale) {
939 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700940
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700941 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700942 // still be around. Mark it as no longer shared if this
943 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700944 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700945 mSlots[last.mSlot].mBufferState.isFree()) {
946 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700947 }
948 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700949 if (!mSlots[last.mSlot].mBufferState.isShared()) {
950 mCore->mActiveBuffers.erase(last.mSlot);
951 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800952 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700953 }
Dan Stoza289ade12014-02-28 11:17:17 -0800954 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800955
Steven Thomas862e7532019-07-10 19:21:03 -0700956 // Make sure to merge the damage rect from the frame we're about
957 // to drop into the new frame's damage rect.
958 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
959 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
960 item.mSurfaceDamage = Region::INVALID_REGION;
961 } else {
962 item.mSurfaceDamage |= last.mSurfaceDamage;
963 }
964
Dan Stoza289ade12014-02-28 11:17:17 -0800965 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700966 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800967 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800968 } else {
969 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800970 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800971 }
972 }
973
974 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200975 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700976 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800977
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700978 output->width = mCore->mDefaultWidth;
979 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700980 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700981 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
982 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800983
Colin Cross152c3b72016-09-27 14:08:19 -0700984 ATRACE_INT(mCore->mConsumerName.string(),
985 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700986 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800987
988 // Take a ticket for the callback functions
989 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700990
Pablo Ceballos9e314332016-01-12 13:49:19 -0800991 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800992 } // Autolock scope
993
Irvel468051e2016-06-13 16:44:44 -0700994 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
995 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
996 // there will be no Binder call
997 if (!mConsumerIsSurfaceFlinger) {
998 item.mGraphicBuffer.clear();
999 }
1000
Dan Stoza8dc55392014-11-04 11:37:46 -08001001 // Call back without the main BufferQueue lock held, but with the callback
1002 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001003
1004 int connectedApi;
1005 sp<Fence> lastQueuedFence;
1006
1007 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001008 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001009 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001010 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001011 }
1012
Yi Kong48a619f2018-06-05 16:34:59 -07001013 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001014 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001015 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001016 frameReplacedListener->onFrameReplaced(item);
1017 }
1018
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001019 connectedApi = mCore->mConnectedApi;
1020 lastQueuedFence = std::move(mLastQueueBufferFence);
1021
1022 mLastQueueBufferFence = std::move(acquireFence);
1023 mLastQueuedCrop = item.mCrop;
1024 mLastQueuedTransform = item.mTransform;
1025
Dan Stoza8dc55392014-11-04 11:37:46 -08001026 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001027 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001028 }
1029
Brian Andersond6927fb2016-07-23 23:37:30 -07001030 // Update and get FrameEventHistory.
1031 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1032 NewFrameEventsEntry newFrameEventsEntry = {
1033 currentFrameNumber,
1034 postedTime,
1035 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001036 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001037 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001038 addAndGetFrameTimestamps(&newFrameEventsEntry,
1039 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001040
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001041 // Wait without lock held
1042 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1043 // Waiting here allows for two full buffers to be queued but not a
1044 // third. In the event that frames take varying time, this makes a
1045 // small trade-off in favor of latency rather than throughput.
1046 lastQueuedFence->waitForever("Throttling EGL Production");
1047 }
1048
Dan Stoza289ade12014-02-28 11:17:17 -08001049 return NO_ERROR;
1050}
1051
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001052status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001053 ATRACE_CALL();
1054 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001055 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001056
1057 if (mCore->mIsAbandoned) {
1058 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001059 return NO_INIT;
1060 }
1061
1062 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1063 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1064 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001065 }
1066
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001067 if (mCore->mSharedBufferMode) {
1068 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001069 return BAD_VALUE;
1070 }
1071
Dan Stoza3e96f192014-03-03 10:16:19 -08001072 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001073 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001074 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001075 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001076 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001077 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001078 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001079 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001080 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001081 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001082 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001083 }
1084
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001085 mSlots[slot].mBufferState.cancel();
1086
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001087 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001088 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001089 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001090 mSlots[slot].mBufferState.mShared = false;
1091 }
1092
1093 // Don't put the shared buffer on the free list.
1094 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001095 mCore->mActiveBuffers.erase(slot);
1096 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001097 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001098
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001099 if (mCore->mConsumerListener != nullptr) {
1100 mCore->mConsumerListener->onFrameCancelled(mSlots[slot].mGraphicBuffer->getId());
1101 }
Dan Stoza289ade12014-02-28 11:17:17 -08001102 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001103 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001104 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001105
1106 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001107}
1108
1109int BufferQueueProducer::query(int what, int *outValue) {
1110 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001111 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001112
Yi Kong48a619f2018-06-05 16:34:59 -07001113 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001114 BQ_LOGE("query: outValue was NULL");
1115 return BAD_VALUE;
1116 }
1117
1118 if (mCore->mIsAbandoned) {
1119 BQ_LOGE("query: BufferQueue has been abandoned");
1120 return NO_INIT;
1121 }
1122
1123 int value;
1124 switch (what) {
1125 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001126 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001127 break;
1128 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001129 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001130 break;
1131 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001132 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001133 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001134 case NATIVE_WINDOW_LAYER_COUNT:
1135 // All BufferQueue buffers have a single layer.
1136 value = BQ_LAYER_COUNT;
1137 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001138 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001139 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001140 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001141 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001142 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001143 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001144 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1145 value = (mCore->mQueue.size() > 1);
1146 break;
1147 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001148 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001149 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001150 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001151 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1152 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1153 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001154 case NATIVE_WINDOW_BUFFER_AGE:
1155 if (mCore->mBufferAge > INT32_MAX) {
1156 value = 0;
1157 } else {
1158 value = static_cast<int32_t>(mCore->mBufferAge);
1159 }
1160 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001161 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1162 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1163 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001164 default:
1165 return BAD_VALUE;
1166 }
1167
1168 BQ_LOGV("query: %d? %d", what, value);
1169 *outValue = value;
1170 return NO_ERROR;
1171}
1172
Dan Stozaf0eaf252014-03-21 13:05:51 -07001173status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001174 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1175 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001176 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001177 mConsumerName = mCore->mConsumerName;
1178 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1179 producerControlledByApp ? "true" : "false");
1180
1181 if (mCore->mIsAbandoned) {
1182 BQ_LOGE("connect: BufferQueue has been abandoned");
1183 return NO_INIT;
1184 }
1185
Yi Kong48a619f2018-06-05 16:34:59 -07001186 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001187 BQ_LOGE("connect: BufferQueue has no consumer");
1188 return NO_INIT;
1189 }
1190
Yi Kong48a619f2018-06-05 16:34:59 -07001191 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001192 BQ_LOGE("connect: output was NULL");
1193 return BAD_VALUE;
1194 }
1195
1196 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1197 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1198 mCore->mConnectedApi, api);
1199 return BAD_VALUE;
1200 }
1201
1202 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1203 mDequeueTimeout < 0 ?
1204 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1205 mCore->mMaxBufferCount) -
1206 mCore->getMaxBufferCountLocked();
1207 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1208 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1209 "slots. Delta = %d", delta);
1210 return BAD_VALUE;
1211 }
1212
Dan Stoza289ade12014-02-28 11:17:17 -08001213 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001214 switch (api) {
1215 case NATIVE_WINDOW_API_EGL:
1216 case NATIVE_WINDOW_API_CPU:
1217 case NATIVE_WINDOW_API_MEDIA:
1218 case NATIVE_WINDOW_API_CAMERA:
1219 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001220
1221 output->width = mCore->mDefaultWidth;
1222 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001223 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001224 output->numPendingBuffers =
1225 static_cast<uint32_t>(mCore->mQueue.size());
1226 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001227 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001228 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001229
Yi Kong48a619f2018-06-05 16:34:59 -07001230 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001231 // Set up a death notification so that we can disconnect
1232 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001233 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001234 status = IInterface::asBinder(listener)->linkToDeath(
1235 static_cast<IBinder::DeathRecipient*>(this));
1236 if (status != NO_ERROR) {
1237 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1238 strerror(-status), status);
1239 }
1240 mCore->mLinkedToDeath = listener;
1241 }
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001242 mCore->mConnectedProducerListener = listener;
1243 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001244 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001245 break;
1246 default:
1247 BQ_LOGE("connect: unknown API %d", api);
1248 status = BAD_VALUE;
1249 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001250 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001251 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001252 mCore->mBufferHasBeenQueued = false;
1253 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001254 mCore->mQueueBufferCanDrop = false;
1255 mCore->mLegacyBufferDrop = true;
1256 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1257 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1258 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001259 }
Dan Stoza289ade12014-02-28 11:17:17 -08001260
Pablo Ceballos981066c2016-02-18 12:54:37 -08001261 mCore->mAllowAllocation = true;
1262 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001263 return status;
1264}
1265
Robert Carr97b9c862016-09-08 13:54:35 -07001266status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001267 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001268 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001269
1270 int status = NO_ERROR;
1271 sp<IConsumerListener> listener;
1272 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001273 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001274
1275 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001276 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001277 return NO_ERROR;
1278 }
1279 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1280 }
1281
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001282 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001283
1284 if (mCore->mIsAbandoned) {
1285 // It's not really an error to disconnect after the surface has
1286 // been abandoned; it should just be a no-op.
1287 return NO_ERROR;
1288 }
1289
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001290 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001291 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1292 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1293 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001294 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001295 // If we're asked to disconnect the currently connected api but
1296 // nobody is connected, it's not really an error.
1297 if (api == BufferQueueCore::NO_CONNECTED_API) {
1298 return NO_ERROR;
1299 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001300 }
1301
Dan Stoza289ade12014-02-28 11:17:17 -08001302 switch (api) {
1303 case NATIVE_WINDOW_API_EGL:
1304 case NATIVE_WINDOW_API_CPU:
1305 case NATIVE_WINDOW_API_MEDIA:
1306 case NATIVE_WINDOW_API_CAMERA:
1307 if (mCore->mConnectedApi == api) {
1308 mCore->freeAllBuffersLocked();
1309
1310 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001311 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001312 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001313 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001314 // This can fail if we're here because of the death
1315 // notification, but we just ignore it
1316 token->unlinkToDeath(
1317 static_cast<IBinder::DeathRecipient*>(this));
1318 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001319 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001320 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001321 mCore->mLinkedToDeath = nullptr;
1322 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001323 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001324 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001325 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001326 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001327 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001328 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001329 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1330 BQ_LOGE("disconnect: not connected (req=%d)", api);
1331 status = NO_INIT;
1332 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001333 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001334 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001335 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001336 }
1337 break;
1338 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001339 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001340 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001341 break;
1342 }
1343 } // Autolock scope
1344
1345 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001346 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001347 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001348 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001349 }
1350
1351 return status;
1352}
1353
Jesse Hall399184a2014-03-03 15:42:54 -08001354status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001355 sp<IConsumerListener> listener;
1356 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001357 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001358 mCore->mSidebandStream = stream;
1359 listener = mCore->mConsumerListener;
1360 } // Autolock scope
1361
Yi Kong48a619f2018-06-05 16:34:59 -07001362 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001363 listener->onSidebandStreamChanged();
1364 }
Jesse Hall399184a2014-03-03 15:42:54 -08001365 return NO_ERROR;
1366}
1367
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001368void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001369 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001370 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001371
1372 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001373 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001374 size_t newBufferCount = 0;
1375 uint32_t allocWidth = 0;
1376 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001377 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001378 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001379 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001380 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001381 std::unique_lock<std::mutex> lock(mCore->mMutex);
1382 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001383
Dan Stoza9de72932015-04-16 17:28:43 -07001384 if (!mCore->mAllowAllocation) {
1385 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1386 "BufferQueue");
1387 return;
1388 }
1389
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001390 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1391 // both allocateBuffers and dequeueBuffer.
1392 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001393 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001394 return;
1395 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001396
Antoine Labour78014f32014-07-15 21:17:03 -07001397 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1398 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001399 if (useDefaultSize && mCore->mAutoPrerotation &&
1400 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1401 std::swap(allocWidth, allocHeight);
1402 }
1403
Antoine Labour78014f32014-07-15 21:17:03 -07001404 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1405 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001406 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001407
1408 mCore->mIsAllocating = true;
1409 } // Autolock scope
1410
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001411 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001412 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001413 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001414 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001415 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001416
1417 status_t result = graphicBuffer->initCheck();
1418
Antoine Labour78014f32014-07-15 21:17:03 -07001419 if (result != NO_ERROR) {
1420 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001421 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001422 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001423 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001424 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001425 return;
1426 }
1427 buffers.push_back(graphicBuffer);
1428 }
1429
1430 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001431 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001432 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1433 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001434 if (useDefaultSize && mCore->mAutoPrerotation &&
1435 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1436 std::swap(checkWidth, checkHeight);
1437 }
1438
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001439 PixelFormat checkFormat = format != 0 ?
1440 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001441 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001442 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1443 checkFormat != allocFormat || checkUsage != allocUsage) {
1444 // Something changed while we released the lock. Retry.
1445 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1446 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001447 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001448 continue;
1449 }
1450
Antoine Labour78014f32014-07-15 21:17:03 -07001451 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001452 if (mCore->mFreeSlots.empty()) {
1453 BQ_LOGV("allocateBuffers: a slot was occupied while "
1454 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001455 continue;
1456 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001457 auto slot = mCore->mFreeSlots.begin();
1458 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1459 mSlots[*slot].mGraphicBuffer = buffers[i];
1460 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001461
1462 // freeBufferLocked puts this slot on the free slots list. Since
1463 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001464 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001465
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001466 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1467 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001468
1469 // Make sure the erase is done after all uses of the slot
1470 // iterator since it will be invalid after this point.
1471 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001472 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001473
Antoine Labour78014f32014-07-15 21:17:03 -07001474 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001475 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001476 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001477
1478 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1479 // waiting anymore so it can use the buffer we just allocated.
1480 while (mDequeueWaitingForAllocation) {
1481 mDequeueWaitingForAllocationCondition.wait(lock);
1482 }
Antoine Labour78014f32014-07-15 21:17:03 -07001483 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001484 }
1485}
1486
Dan Stoza9de72932015-04-16 17:28:43 -07001487status_t BufferQueueProducer::allowAllocation(bool allow) {
1488 ATRACE_CALL();
1489 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1490
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001491 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001492 mCore->mAllowAllocation = allow;
1493 return NO_ERROR;
1494}
1495
Dan Stoza812ed062015-06-02 15:45:22 -07001496status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1497 ATRACE_CALL();
1498 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1499
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001500 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001501 mCore->mGenerationNumber = generationNumber;
1502 return NO_ERROR;
1503}
1504
Dan Stozac6f30bd2015-06-08 09:32:50 -07001505String8 BufferQueueProducer::getConsumerName() const {
1506 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001507 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001508 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1509 return mConsumerName;
1510}
1511
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001512status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001513 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001514 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001515
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001516 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001517 if (!sharedBufferMode) {
1518 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001519 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001520 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001521 return NO_ERROR;
1522}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001523
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001524status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1525 ATRACE_CALL();
1526 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1527
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001528 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001529
1530 mCore->mAutoRefresh = autoRefresh;
1531 return NO_ERROR;
1532}
1533
Dan Stoza127fc632015-06-30 13:43:32 -07001534status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1535 ATRACE_CALL();
1536 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1537
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001538 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001539 bool dequeueBufferCannotBlock =
1540 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1541 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001542 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1543 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1544 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1545 "available slots. Delta = %d", delta);
1546 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001547 }
1548
Pablo Ceballos981066c2016-02-18 12:54:37 -08001549 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001550 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1551 if (timeout > 0) {
1552 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001553 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001554
Pablo Ceballos981066c2016-02-18 12:54:37 -08001555 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001556 return NO_ERROR;
1557}
1558
Sungtak Lee3249fb62019-03-02 16:40:47 -08001559status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1560 ATRACE_CALL();
1561 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1562
1563 std::lock_guard<std::mutex> lock(mCore->mMutex);
1564 mCore->mLegacyBufferDrop = drop;
1565 return NO_ERROR;
1566}
1567
Dan Stoza50101d02016-04-07 16:53:23 -07001568status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001569 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001570 ATRACE_CALL();
1571 BQ_LOGV("getLastQueuedBuffer");
1572
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001573 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001574 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1575 *outBuffer = nullptr;
1576 *outFence = Fence::NO_FENCE;
1577 return NO_ERROR;
1578 }
1579
1580 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1581 *outFence = mLastQueueBufferFence;
1582
John Reck1a61da52016-04-28 13:18:15 -07001583 // Currently only SurfaceFlinger internally ever changes
1584 // GLConsumer's filtering mode, so we just use 'true' here as
1585 // this is slightly specialized for the current client of this API,
1586 // which does want filtering.
1587 GLConsumer::computeTransformMatrix(outTransformMatrix,
1588 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1589 mLastQueuedTransform, true /* filter */);
1590
Dan Stoza50101d02016-04-07 16:53:23 -07001591 return NO_ERROR;
1592}
1593
Brian Anderson3890c392016-07-25 12:48:08 -07001594void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1595 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001596}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001597
Brian Anderson3890c392016-07-25 12:48:08 -07001598void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001599 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001600 FrameEventHistoryDelta* outDelta) {
1601 if (newTimestamps == nullptr && outDelta == nullptr) {
1602 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001603 }
1604
1605 ATRACE_CALL();
1606 BQ_LOGV("addAndGetFrameTimestamps");
1607 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001608 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001609 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001610 listener = mCore->mConsumerListener;
1611 }
Yi Kong48a619f2018-06-05 16:34:59 -07001612 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001613 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001614 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001615}
1616
Dan Stoza289ade12014-02-28 11:17:17 -08001617void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1618 // If we're here, it means that a producer we were connected to died.
1619 // We're guaranteed that we are still connected to it because we remove
1620 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1621 // without synchronization here.
1622 int api = mCore->mConnectedApi;
1623 disconnect(api);
1624}
1625
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001626status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1627 BQ_LOGV("getUniqueId");
1628
1629 *outId = mCore->mUniqueId;
1630 return NO_ERROR;
1631}
1632
Chia-I Wue2786ea2017-08-07 10:36:08 -07001633status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1634 BQ_LOGV("getConsumerUsage");
1635
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001636 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001637 *outUsage = mCore->mConsumerUsageBits;
1638 return NO_ERROR;
1639}
1640
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001641status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1642 ATRACE_CALL();
1643 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1644
1645 std::lock_guard<std::mutex> lock(mCore->mMutex);
1646
1647 mCore->mAutoPrerotation = autoPrerotation;
1648 return NO_ERROR;
1649}
1650
Dan Stoza289ade12014-02-28 11:17:17 -08001651} // namespace android