blob: 09c74deee434877f426ededc76bd6b70c2b2b697 [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 }
Dan Stoza289ade12014-02-28 11:17:17 -0800515 } // Autolock scope
516
517 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700518 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700519 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700520 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700521 {mConsumerName.string(), mConsumerName.size()});
522
523 status_t error = graphicBuffer->initCheck();
524
Dan Stoza289ade12014-02-28 11:17:17 -0800525 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200526 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800527
Chia-I Wufeec3b12017-05-25 09:34:56 -0700528 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700529 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
530 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
531 }
532
533 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200534 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700535
Chia-I Wufeec3b12017-05-25 09:34:56 -0700536 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700537 mCore->mFreeSlots.insert(*outSlot);
538 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700539 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
540 return error;
541 }
542
Dan Stoza289ade12014-02-28 11:17:17 -0800543 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700544 mCore->mFreeSlots.insert(*outSlot);
545 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800546 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
547 return NO_INIT;
548 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800549
Pablo Ceballos9e314332016-01-12 13:49:19 -0800550 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800551 } // Autolock scope
552 }
553
Dan Stoza9f3053d2014-03-06 15:14:33 -0800554 if (attachedByConsumer) {
555 returnFlags |= BUFFER_NEEDS_REALLOCATION;
556 }
557
Dan Stoza289ade12014-02-28 11:17:17 -0800558 if (eglFence != EGL_NO_SYNC_KHR) {
559 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
560 1000000000);
561 // If something goes wrong, log the error, but return the buffer without
562 // synchronizing access to it. It's too late at this point to abort the
563 // dequeue operation.
564 if (result == EGL_FALSE) {
565 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
566 eglGetError());
567 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
568 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
569 }
570 eglDestroySyncKHR(eglDisplay, eglFence);
571 }
572
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700573 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
574 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800575 mSlots[*outSlot].mFrameNumber,
576 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
577
Ian Elliottd11b0442017-07-18 11:05:49 -0600578 if (outBufferAge) {
579 *outBufferAge = mCore->mBufferAge;
580 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700581 addAndGetFrameTimestamps(nullptr, outTimestamps);
582
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700583 { // Autolock scope
584 std::lock_guard<std::mutex> lock(mCore->mMutex);
585 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
586 }
587
Dan Stoza289ade12014-02-28 11:17:17 -0800588 return returnFlags;
589}
590
Dan Stoza9f3053d2014-03-06 15:14:33 -0800591status_t BufferQueueProducer::detachBuffer(int slot) {
592 ATRACE_CALL();
593 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700594 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800595
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700596 sp<IConsumerListener> listener;
597 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200598 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700599
600 if (mCore->mIsAbandoned) {
601 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
602 return NO_INIT;
603 }
604
605 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
606 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
607 return NO_INIT;
608 }
609
610 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
611 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
612 return BAD_VALUE;
613 }
614
615 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
616 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
617 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
618 return BAD_VALUE;
619 } else if (!mSlots[slot].mBufferState.isDequeued()) {
620 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
621 "(state = %s)", slot, mSlots[slot].mBufferState.string());
622 return BAD_VALUE;
623 } else if (!mSlots[slot].mRequestBufferCalled) {
624 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
625 slot);
626 return BAD_VALUE;
627 }
628
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700629 listener = mCore->mConsumerListener;
630 if (listener != nullptr) {
631 listener->onFrameDetached(mSlots[slot].mGraphicBuffer->getId());
632 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700633 mSlots[slot].mBufferState.detachProducer();
634 mCore->mActiveBuffers.erase(slot);
635 mCore->mFreeSlots.insert(slot);
636 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200637 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700638 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800639 }
640
Yi Kong48a619f2018-06-05 16:34:59 -0700641 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700642 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700643 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800644
Dan Stoza9f3053d2014-03-06 15:14:33 -0800645 return NO_ERROR;
646}
647
Dan Stozad9822a32014-03-28 15:25:31 -0700648status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
649 sp<Fence>* outFence) {
650 ATRACE_CALL();
651
Yi Kong48a619f2018-06-05 16:34:59 -0700652 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700653 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
654 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700655 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700656 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
657 return BAD_VALUE;
658 }
659
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700660 sp<IConsumerListener> listener;
661 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200662 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700663
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700664 if (mCore->mIsAbandoned) {
665 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
666 return NO_INIT;
667 }
668
669 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
670 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
671 return NO_INIT;
672 }
673
674 if (mCore->mSharedBufferMode) {
675 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
676 "mode");
677 return BAD_VALUE;
678 }
679
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200680 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700681
682 if (mCore->mFreeBuffers.empty()) {
683 return NO_MEMORY;
684 }
685
686 int found = mCore->mFreeBuffers.front();
687 mCore->mFreeBuffers.remove(found);
688 mCore->mFreeSlots.insert(found);
689
690 BQ_LOGV("detachNextBuffer detached slot %d", found);
691
692 *outBuffer = mSlots[found].mGraphicBuffer;
693 *outFence = mSlots[found].mFence;
694 mCore->clearBufferSlotLocked(found);
695 VALIDATE_CONSISTENCY();
696 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700697 }
698
Yi Kong48a619f2018-06-05 16:34:59 -0700699 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700700 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700701 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800702
Dan Stozad9822a32014-03-28 15:25:31 -0700703 return NO_ERROR;
704}
705
Dan Stoza9f3053d2014-03-06 15:14:33 -0800706status_t BufferQueueProducer::attachBuffer(int* outSlot,
707 const sp<android::GraphicBuffer>& buffer) {
708 ATRACE_CALL();
709
Yi Kong48a619f2018-06-05 16:34:59 -0700710 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700711 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800712 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700713 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700714 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800715 return BAD_VALUE;
716 }
717
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200718 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700719
720 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700721 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700722 return NO_INIT;
723 }
724
725 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700726 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700727 return NO_INIT;
728 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800729
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700730 if (mCore->mSharedBufferMode) {
731 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700732 return BAD_VALUE;
733 }
734
Dan Stoza812ed062015-06-02 15:45:22 -0700735 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
736 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
737 "[queue %u]", buffer->getGenerationNumber(),
738 mCore->mGenerationNumber);
739 return BAD_VALUE;
740 }
741
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200742 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700743
Dan Stoza9f3053d2014-03-06 15:14:33 -0800744 status_t returnFlags = NO_ERROR;
745 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200746 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800747 if (status != NO_ERROR) {
748 return status;
749 }
750
751 // This should not happen
752 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700753 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 return -EBUSY;
755 }
756
757 *outSlot = found;
758 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700759 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800760 *outSlot, returnFlags);
761
762 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700763 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800764 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
765 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700766 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800767 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800768 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800769 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800770 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700771
Dan Stoza9f3053d2014-03-06 15:14:33 -0800772 return returnFlags;
773}
774
Dan Stoza289ade12014-02-28 11:17:17 -0800775status_t BufferQueueProducer::queueBuffer(int slot,
776 const QueueBufferInput &input, QueueBufferOutput *output) {
777 ATRACE_CALL();
778 ATRACE_BUFFER_INDEX(slot);
779
Brian Andersond6927fb2016-07-23 23:37:30 -0700780 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800781 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800782 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700783 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800784 int scalingMode;
785 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700786 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700787 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700788 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700789 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700790 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
791 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700792 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700793 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800794
Yi Kong48a619f2018-06-05 16:34:59 -0700795 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800796 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800797 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800798 }
799
Brian Anderson3d4039d2016-09-23 16:31:30 -0700800 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
801
Dan Stoza289ade12014-02-28 11:17:17 -0800802 switch (scalingMode) {
803 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
804 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
805 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
806 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
807 break;
808 default:
809 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800810 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800811 }
812
Dan Stoza8dc55392014-11-04 11:37:46 -0800813 sp<IConsumerListener> frameAvailableListener;
814 sp<IConsumerListener> frameReplacedListener;
815 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700816 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800817 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800818 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200819 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800820
821 if (mCore->mIsAbandoned) {
822 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
823 return NO_INIT;
824 }
825
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700826 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
827 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
828 return NO_INIT;
829 }
830
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800831 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800832 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800833 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800834 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700835 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800836 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700837 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800838 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800839 } else if (!mSlots[slot].mRequestBufferCalled) {
840 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
841 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800842 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800843 }
844
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700845 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700846 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700847 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700848 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700849 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700850 mSlots[slot].mBufferState.mShared = true;
851 }
852
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800853 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700854 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
855 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
856 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700857 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800859
860 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
861 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700862 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800863 crop.intersect(bufferRect, &croppedRect);
864 if (croppedRect != crop) {
865 BQ_LOGE("queueBuffer: crop rect is not contained within the "
866 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800867 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800868 }
869
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800870 // Override UNKNOWN dataspace with consumer default
871 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
872 dataSpace = mCore->mDefaultBufferDataSpace;
873 }
874
Brian Andersond6927fb2016-07-23 23:37:30 -0700875 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876 mSlots[slot].mBufferState.queue();
877
Brian Andersond6927fb2016-07-23 23:37:30 -0700878 // Increment the frame counter and store a local version of it
879 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800880 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700881 currentFrameNumber = mCore->mFrameCounter;
882 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800883
Dan Stoza289ade12014-02-28 11:17:17 -0800884 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
885 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
886 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800887 item.mTransform = transform &
888 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800889 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800890 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
891 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700892 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800893 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800894 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700895 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700896 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800897 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700898 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700899 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700900 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700901 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800902 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700903 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700904 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700905 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700906 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800907 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800908
Ruben Brunk1681d952014-06-27 15:51:55 -0700909 mStickyTransform = stickyTransform;
910
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700911 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700912 if (mCore->mSharedBufferMode) {
913 mCore->mSharedBufferCache.crop = crop;
914 mCore->mSharedBufferCache.transform = transform;
915 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700916 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700917 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700918 }
919
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800920 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800921 if (mCore->mQueue.empty()) {
922 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
923 // and simply queue this buffer
924 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800925 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800926 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700927 // When the queue is not empty, we need to look at the last buffer
928 // in the queue to see if we need to replace it
929 const BufferItem& last = mCore->mQueue.itemAt(
930 mCore->mQueue.size() - 1);
931 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800932
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700933 if (!last.mIsStale) {
934 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700935
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700936 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700937 // still be around. Mark it as no longer shared if this
938 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700939 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700940 mSlots[last.mSlot].mBufferState.isFree()) {
941 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700942 }
943 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700944 if (!mSlots[last.mSlot].mBufferState.isShared()) {
945 mCore->mActiveBuffers.erase(last.mSlot);
946 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800947 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700948 }
Dan Stoza289ade12014-02-28 11:17:17 -0800949 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800950
Steven Thomas862e7532019-07-10 19:21:03 -0700951 // Make sure to merge the damage rect from the frame we're about
952 // to drop into the new frame's damage rect.
953 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
954 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
955 item.mSurfaceDamage = Region::INVALID_REGION;
956 } else {
957 item.mSurfaceDamage |= last.mSurfaceDamage;
958 }
959
Dan Stoza289ade12014-02-28 11:17:17 -0800960 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700961 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800962 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800963 } else {
964 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800965 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800966 }
967 }
968
969 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200970 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700971 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800972
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700973 output->width = mCore->mDefaultWidth;
974 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700975 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700976 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
977 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800978
Colin Cross152c3b72016-09-27 14:08:19 -0700979 ATRACE_INT(mCore->mConsumerName.string(),
980 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700981 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800982
983 // Take a ticket for the callback functions
984 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700985
Pablo Ceballos9e314332016-01-12 13:49:19 -0800986 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800987 } // Autolock scope
988
Irvel468051e2016-06-13 16:44:44 -0700989 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
990 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
991 // there will be no Binder call
992 if (!mConsumerIsSurfaceFlinger) {
993 item.mGraphicBuffer.clear();
994 }
995
Dan Stoza8dc55392014-11-04 11:37:46 -0800996 // Call back without the main BufferQueue lock held, but with the callback
997 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700998
999 int connectedApi;
1000 sp<Fence> lastQueuedFence;
1001
1002 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001003 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001004 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001005 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001006 }
1007
Yi Kong48a619f2018-06-05 16:34:59 -07001008 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001009 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001010 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001011 frameReplacedListener->onFrameReplaced(item);
1012 }
1013
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001014 connectedApi = mCore->mConnectedApi;
1015 lastQueuedFence = std::move(mLastQueueBufferFence);
1016
1017 mLastQueueBufferFence = std::move(acquireFence);
1018 mLastQueuedCrop = item.mCrop;
1019 mLastQueuedTransform = item.mTransform;
1020
Dan Stoza8dc55392014-11-04 11:37:46 -08001021 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001022 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001023 }
1024
Brian Andersond6927fb2016-07-23 23:37:30 -07001025 // Update and get FrameEventHistory.
1026 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1027 NewFrameEventsEntry newFrameEventsEntry = {
1028 currentFrameNumber,
1029 postedTime,
1030 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001031 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001032 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001033 addAndGetFrameTimestamps(&newFrameEventsEntry,
1034 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001035
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001036 // Wait without lock held
1037 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1038 // Waiting here allows for two full buffers to be queued but not a
1039 // third. In the event that frames take varying time, this makes a
1040 // small trade-off in favor of latency rather than throughput.
1041 lastQueuedFence->waitForever("Throttling EGL Production");
1042 }
1043
Dan Stoza289ade12014-02-28 11:17:17 -08001044 return NO_ERROR;
1045}
1046
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001047status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001048 ATRACE_CALL();
1049 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001050 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001051
1052 if (mCore->mIsAbandoned) {
1053 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001054 return NO_INIT;
1055 }
1056
1057 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1058 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1059 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001060 }
1061
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001062 if (mCore->mSharedBufferMode) {
1063 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001064 return BAD_VALUE;
1065 }
1066
Dan Stoza3e96f192014-03-03 10:16:19 -08001067 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001068 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001069 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001070 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001071 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001072 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001073 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001074 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001075 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001076 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001077 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001078 }
1079
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001080 mSlots[slot].mBufferState.cancel();
1081
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001082 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001083 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001084 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001085 mSlots[slot].mBufferState.mShared = false;
1086 }
1087
1088 // Don't put the shared buffer on the free list.
1089 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001090 mCore->mActiveBuffers.erase(slot);
1091 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001092 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001093
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001094 if (mCore->mConsumerListener != nullptr) {
1095 mCore->mConsumerListener->onFrameCancelled(mSlots[slot].mGraphicBuffer->getId());
1096 }
Dan Stoza289ade12014-02-28 11:17:17 -08001097 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001098 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001099 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001100
1101 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001102}
1103
1104int BufferQueueProducer::query(int what, int *outValue) {
1105 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001106 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001107
Yi Kong48a619f2018-06-05 16:34:59 -07001108 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001109 BQ_LOGE("query: outValue was NULL");
1110 return BAD_VALUE;
1111 }
1112
1113 if (mCore->mIsAbandoned) {
1114 BQ_LOGE("query: BufferQueue has been abandoned");
1115 return NO_INIT;
1116 }
1117
1118 int value;
1119 switch (what) {
1120 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001121 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001122 break;
1123 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001124 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001125 break;
1126 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001127 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001128 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001129 case NATIVE_WINDOW_LAYER_COUNT:
1130 // All BufferQueue buffers have a single layer.
1131 value = BQ_LAYER_COUNT;
1132 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001133 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001134 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001135 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001136 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001137 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001138 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001139 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1140 value = (mCore->mQueue.size() > 1);
1141 break;
1142 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001143 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001144 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001145 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001146 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1147 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1148 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001149 case NATIVE_WINDOW_BUFFER_AGE:
1150 if (mCore->mBufferAge > INT32_MAX) {
1151 value = 0;
1152 } else {
1153 value = static_cast<int32_t>(mCore->mBufferAge);
1154 }
1155 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001156 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1157 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1158 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001159 default:
1160 return BAD_VALUE;
1161 }
1162
1163 BQ_LOGV("query: %d? %d", what, value);
1164 *outValue = value;
1165 return NO_ERROR;
1166}
1167
Dan Stozaf0eaf252014-03-21 13:05:51 -07001168status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001169 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1170 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001171 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001172 mConsumerName = mCore->mConsumerName;
1173 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1174 producerControlledByApp ? "true" : "false");
1175
1176 if (mCore->mIsAbandoned) {
1177 BQ_LOGE("connect: BufferQueue has been abandoned");
1178 return NO_INIT;
1179 }
1180
Yi Kong48a619f2018-06-05 16:34:59 -07001181 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001182 BQ_LOGE("connect: BufferQueue has no consumer");
1183 return NO_INIT;
1184 }
1185
Yi Kong48a619f2018-06-05 16:34:59 -07001186 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001187 BQ_LOGE("connect: output was NULL");
1188 return BAD_VALUE;
1189 }
1190
1191 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1192 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1193 mCore->mConnectedApi, api);
1194 return BAD_VALUE;
1195 }
1196
1197 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1198 mDequeueTimeout < 0 ?
1199 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1200 mCore->mMaxBufferCount) -
1201 mCore->getMaxBufferCountLocked();
1202 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1203 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1204 "slots. Delta = %d", delta);
1205 return BAD_VALUE;
1206 }
1207
Dan Stoza289ade12014-02-28 11:17:17 -08001208 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001209 switch (api) {
1210 case NATIVE_WINDOW_API_EGL:
1211 case NATIVE_WINDOW_API_CPU:
1212 case NATIVE_WINDOW_API_MEDIA:
1213 case NATIVE_WINDOW_API_CAMERA:
1214 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001215
1216 output->width = mCore->mDefaultWidth;
1217 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001218 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001219 output->numPendingBuffers =
1220 static_cast<uint32_t>(mCore->mQueue.size());
1221 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001222 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001223 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001224
Yi Kong48a619f2018-06-05 16:34:59 -07001225 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001226 // Set up a death notification so that we can disconnect
1227 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001228 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001229 status = IInterface::asBinder(listener)->linkToDeath(
1230 static_cast<IBinder::DeathRecipient*>(this));
1231 if (status != NO_ERROR) {
1232 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1233 strerror(-status), status);
1234 }
1235 mCore->mLinkedToDeath = listener;
1236 }
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001237 mCore->mConnectedProducerListener = listener;
1238 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001239 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001240 break;
1241 default:
1242 BQ_LOGE("connect: unknown API %d", api);
1243 status = BAD_VALUE;
1244 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001245 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001246 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001247 mCore->mBufferHasBeenQueued = false;
1248 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001249 mCore->mQueueBufferCanDrop = false;
1250 mCore->mLegacyBufferDrop = true;
1251 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1252 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1253 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001254 }
Dan Stoza289ade12014-02-28 11:17:17 -08001255
Pablo Ceballos981066c2016-02-18 12:54:37 -08001256 mCore->mAllowAllocation = true;
1257 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001258 return status;
1259}
1260
Robert Carr97b9c862016-09-08 13:54:35 -07001261status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001262 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001263 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001264
1265 int status = NO_ERROR;
1266 sp<IConsumerListener> listener;
1267 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001268 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001269
1270 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001271 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001272 return NO_ERROR;
1273 }
1274 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1275 }
1276
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001277 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001278
1279 if (mCore->mIsAbandoned) {
1280 // It's not really an error to disconnect after the surface has
1281 // been abandoned; it should just be a no-op.
1282 return NO_ERROR;
1283 }
1284
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001285 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001286 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1287 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1288 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001289 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001290 // If we're asked to disconnect the currently connected api but
1291 // nobody is connected, it's not really an error.
1292 if (api == BufferQueueCore::NO_CONNECTED_API) {
1293 return NO_ERROR;
1294 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001295 }
1296
Dan Stoza289ade12014-02-28 11:17:17 -08001297 switch (api) {
1298 case NATIVE_WINDOW_API_EGL:
1299 case NATIVE_WINDOW_API_CPU:
1300 case NATIVE_WINDOW_API_MEDIA:
1301 case NATIVE_WINDOW_API_CAMERA:
1302 if (mCore->mConnectedApi == api) {
1303 mCore->freeAllBuffersLocked();
1304
1305 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001306 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001307 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001308 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001309 // This can fail if we're here because of the death
1310 // notification, but we just ignore it
1311 token->unlinkToDeath(
1312 static_cast<IBinder::DeathRecipient*>(this));
1313 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001314 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001315 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001316 mCore->mLinkedToDeath = nullptr;
1317 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001318 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001319 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001320 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001321 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001322 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001323 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001324 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1325 BQ_LOGE("disconnect: not connected (req=%d)", api);
1326 status = NO_INIT;
1327 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001328 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001329 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001330 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001331 }
1332 break;
1333 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001334 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001335 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001336 break;
1337 }
1338 } // Autolock scope
1339
1340 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001341 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001342 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001343 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001344 }
1345
1346 return status;
1347}
1348
Jesse Hall399184a2014-03-03 15:42:54 -08001349status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001350 sp<IConsumerListener> listener;
1351 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001352 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001353 mCore->mSidebandStream = stream;
1354 listener = mCore->mConsumerListener;
1355 } // Autolock scope
1356
Yi Kong48a619f2018-06-05 16:34:59 -07001357 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001358 listener->onSidebandStreamChanged();
1359 }
Jesse Hall399184a2014-03-03 15:42:54 -08001360 return NO_ERROR;
1361}
1362
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001363void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001364 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001365 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001366
1367 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001368 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001369 size_t newBufferCount = 0;
1370 uint32_t allocWidth = 0;
1371 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001372 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001373 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001374 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001375 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001376 std::unique_lock<std::mutex> lock(mCore->mMutex);
1377 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001378
Dan Stoza9de72932015-04-16 17:28:43 -07001379 if (!mCore->mAllowAllocation) {
1380 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1381 "BufferQueue");
1382 return;
1383 }
1384
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001385 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1386 // both allocateBuffers and dequeueBuffer.
1387 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001388 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001389 return;
1390 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001391
Antoine Labour78014f32014-07-15 21:17:03 -07001392 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1393 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001394 if (useDefaultSize && mCore->mAutoPrerotation &&
1395 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1396 std::swap(allocWidth, allocHeight);
1397 }
1398
Antoine Labour78014f32014-07-15 21:17:03 -07001399 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1400 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001401 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001402
1403 mCore->mIsAllocating = true;
1404 } // Autolock scope
1405
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001406 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001407 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001408 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001409 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001410 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001411
1412 status_t result = graphicBuffer->initCheck();
1413
Antoine Labour78014f32014-07-15 21:17:03 -07001414 if (result != NO_ERROR) {
1415 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001416 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001417 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001418 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001419 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001420 return;
1421 }
1422 buffers.push_back(graphicBuffer);
1423 }
1424
1425 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001426 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001427 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1428 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001429 if (useDefaultSize && mCore->mAutoPrerotation &&
1430 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1431 std::swap(checkWidth, checkHeight);
1432 }
1433
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001434 PixelFormat checkFormat = format != 0 ?
1435 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001436 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001437 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1438 checkFormat != allocFormat || checkUsage != allocUsage) {
1439 // Something changed while we released the lock. Retry.
1440 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1441 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001442 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001443 continue;
1444 }
1445
Antoine Labour78014f32014-07-15 21:17:03 -07001446 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001447 if (mCore->mFreeSlots.empty()) {
1448 BQ_LOGV("allocateBuffers: a slot was occupied while "
1449 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001450 continue;
1451 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001452 auto slot = mCore->mFreeSlots.begin();
1453 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1454 mSlots[*slot].mGraphicBuffer = buffers[i];
1455 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001456
1457 // freeBufferLocked puts this slot on the free slots list. Since
1458 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001459 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001460
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001461 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1462 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001463
1464 // Make sure the erase is done after all uses of the slot
1465 // iterator since it will be invalid after this point.
1466 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001467 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001468
Antoine Labour78014f32014-07-15 21:17:03 -07001469 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001470 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001471 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001472
1473 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1474 // waiting anymore so it can use the buffer we just allocated.
1475 while (mDequeueWaitingForAllocation) {
1476 mDequeueWaitingForAllocationCondition.wait(lock);
1477 }
Antoine Labour78014f32014-07-15 21:17:03 -07001478 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001479 }
1480}
1481
Dan Stoza9de72932015-04-16 17:28:43 -07001482status_t BufferQueueProducer::allowAllocation(bool allow) {
1483 ATRACE_CALL();
1484 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1485
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001486 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001487 mCore->mAllowAllocation = allow;
1488 return NO_ERROR;
1489}
1490
Dan Stoza812ed062015-06-02 15:45:22 -07001491status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1492 ATRACE_CALL();
1493 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1494
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001495 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001496 mCore->mGenerationNumber = generationNumber;
1497 return NO_ERROR;
1498}
1499
Dan Stozac6f30bd2015-06-08 09:32:50 -07001500String8 BufferQueueProducer::getConsumerName() const {
1501 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001502 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001503 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1504 return mConsumerName;
1505}
1506
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001507status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001508 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001509 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001510
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001511 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001512 if (!sharedBufferMode) {
1513 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001514 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001515 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001516 return NO_ERROR;
1517}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001518
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001519status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1520 ATRACE_CALL();
1521 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1522
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001523 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001524
1525 mCore->mAutoRefresh = autoRefresh;
1526 return NO_ERROR;
1527}
1528
Dan Stoza127fc632015-06-30 13:43:32 -07001529status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1530 ATRACE_CALL();
1531 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1532
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001533 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001534 bool dequeueBufferCannotBlock =
1535 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1536 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001537 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1538 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1539 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1540 "available slots. Delta = %d", delta);
1541 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001542 }
1543
Pablo Ceballos981066c2016-02-18 12:54:37 -08001544 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001545 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1546 if (timeout > 0) {
1547 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001548 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001549
Pablo Ceballos981066c2016-02-18 12:54:37 -08001550 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001551 return NO_ERROR;
1552}
1553
Sungtak Lee3249fb62019-03-02 16:40:47 -08001554status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1555 ATRACE_CALL();
1556 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1557
1558 std::lock_guard<std::mutex> lock(mCore->mMutex);
1559 mCore->mLegacyBufferDrop = drop;
1560 return NO_ERROR;
1561}
1562
Dan Stoza50101d02016-04-07 16:53:23 -07001563status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001564 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001565 ATRACE_CALL();
1566 BQ_LOGV("getLastQueuedBuffer");
1567
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001568 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001569 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1570 *outBuffer = nullptr;
1571 *outFence = Fence::NO_FENCE;
1572 return NO_ERROR;
1573 }
1574
1575 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1576 *outFence = mLastQueueBufferFence;
1577
John Reck1a61da52016-04-28 13:18:15 -07001578 // Currently only SurfaceFlinger internally ever changes
1579 // GLConsumer's filtering mode, so we just use 'true' here as
1580 // this is slightly specialized for the current client of this API,
1581 // which does want filtering.
1582 GLConsumer::computeTransformMatrix(outTransformMatrix,
1583 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1584 mLastQueuedTransform, true /* filter */);
1585
Dan Stoza50101d02016-04-07 16:53:23 -07001586 return NO_ERROR;
1587}
1588
Brian Anderson3890c392016-07-25 12:48:08 -07001589void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1590 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001591}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001592
Brian Anderson3890c392016-07-25 12:48:08 -07001593void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001594 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001595 FrameEventHistoryDelta* outDelta) {
1596 if (newTimestamps == nullptr && outDelta == nullptr) {
1597 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001598 }
1599
1600 ATRACE_CALL();
1601 BQ_LOGV("addAndGetFrameTimestamps");
1602 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001603 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001604 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001605 listener = mCore->mConsumerListener;
1606 }
Yi Kong48a619f2018-06-05 16:34:59 -07001607 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001608 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001609 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001610}
1611
Dan Stoza289ade12014-02-28 11:17:17 -08001612void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1613 // If we're here, it means that a producer we were connected to died.
1614 // We're guaranteed that we are still connected to it because we remove
1615 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1616 // without synchronization here.
1617 int api = mCore->mConnectedApi;
1618 disconnect(api);
1619}
1620
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001621status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1622 BQ_LOGV("getUniqueId");
1623
1624 *outId = mCore->mUniqueId;
1625 return NO_ERROR;
1626}
1627
Chia-I Wue2786ea2017-08-07 10:36:08 -07001628status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1629 BQ_LOGV("getConsumerUsage");
1630
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001631 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001632 *outUsage = mCore->mConsumerUsageBits;
1633 return NO_ERROR;
1634}
1635
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001636status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1637 ATRACE_CALL();
1638 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1639
1640 std::lock_guard<std::mutex> lock(mCore->mMutex);
1641
1642 mCore->mAutoPrerotation = autoPrerotation;
1643 return NO_ERROR;
1644}
1645
Dan Stoza289ade12014-02-28 11:17:17 -08001646} // namespace android