blob: 4ff69c57cebe0ab9cf46851754887586e197e314 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Dan Stoza289ade12014-02-28 11:17:17 -080045namespace android {
46
Craig Donner6ebc46a2016-10-21 15:23:44 -070047static constexpr uint32_t BQ_LAYER_COUNT = 1;
48
Irvel468051e2016-06-13 16:44:44 -070049BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
50 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080051 mCore(core),
52 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070053 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070054 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070055 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080056 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070057 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080058 mCallbackMutex(),
59 mNextCallbackTicket(0),
60 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070061 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010062 mDequeueTimeout(-1),
63 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080064
65BufferQueueProducer::~BufferQueueProducer() {}
66
67status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
68 ATRACE_CALL();
69 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020070 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080071
72 if (mCore->mIsAbandoned) {
73 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
74 return NO_INIT;
75 }
76
Pablo Ceballos583b1b32015-09-03 18:23:52 -070077 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
78 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
79 return NO_INIT;
80 }
81
Dan Stoza3e96f192014-03-03 10:16:19 -080082 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080083 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080084 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080085 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070086 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080087 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070088 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080089 return BAD_VALUE;
90 }
91
92 mSlots[slot].mRequestBufferCalled = true;
93 *buf = mSlots[slot].mGraphicBuffer;
94 return NO_ERROR;
95}
96
Pablo Ceballosfa455352015-08-12 17:47:47 -070097status_t BufferQueueProducer::setMaxDequeuedBufferCount(
98 int maxDequeuedBuffers) {
99 ATRACE_CALL();
100 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
101 maxDequeuedBuffers);
102
Pablo Ceballos981066c2016-02-18 12:54:37 -0800103 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700104 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200105 std::unique_lock<std::mutex> lock(mCore->mMutex);
106 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700107
108 if (mCore->mIsAbandoned) {
109 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
110 "abandoned");
111 return NO_INIT;
112 }
113
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700114 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
115 return NO_ERROR;
116 }
117
Pablo Ceballos72daab62015-12-07 16:38:43 -0800118 // The new maxDequeuedBuffer count should not be violated by the number
119 // of currently dequeued buffers
120 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800121 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700122 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800123 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700124 }
125 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800126 if (dequeuedCount > maxDequeuedBuffers) {
127 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
128 "count (%d) exceeds the current dequeued buffer count (%d)",
129 maxDequeuedBuffers, dequeuedCount);
130 return BAD_VALUE;
131 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700133 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700134 bufferCount += maxDequeuedBuffers;
135
136 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
137 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
138 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
139 return BAD_VALUE;
140 }
141
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700142 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700143 if (bufferCount < minBufferSlots) {
144 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
145 "less than minimum %d", bufferCount, minBufferSlots);
146 return BAD_VALUE;
147 }
148
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700149 if (bufferCount > mCore->mMaxBufferCount) {
150 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700151 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
152 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
153 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
154 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700155 return BAD_VALUE;
156 }
157
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800159 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800160 return BAD_VALUE;
161 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700162 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800163 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800164 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800165 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800166 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200167 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700168 } // Autolock scope
169
170 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700171 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800172 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700173 }
174
175 return NO_ERROR;
176}
177
178status_t BufferQueueProducer::setAsyncMode(bool async) {
179 ATRACE_CALL();
180 BQ_LOGV("setAsyncMode: async = %d", async);
181
Pablo Ceballos981066c2016-02-18 12:54:37 -0800182 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700183 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200184 std::unique_lock<std::mutex> lock(mCore->mMutex);
185 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700186
187 if (mCore->mIsAbandoned) {
188 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
189 return NO_INIT;
190 }
191
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700192 if (async == mCore->mAsyncMode) {
193 return NO_ERROR;
194 }
195
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700196 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700197 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
198 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700199 BQ_LOGE("setAsyncMode(%d): this call would cause the "
200 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700201 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
202 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
203 mCore->mMaxDequeuedBufferCount,
204 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700205 return BAD_VALUE;
206 }
207
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800208 int delta = mCore->getMaxBufferCountLocked(async,
209 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
210 - mCore->getMaxBufferCountLocked();
211
Pablo Ceballos981066c2016-02-18 12:54:37 -0800212 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800213 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
214 "available slots. Delta = %d", delta);
215 return BAD_VALUE;
216 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700217 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800218 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200219 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700220 if (delta < 0) {
221 listener = mCore->mConsumerListener;
222 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700223 } // Autolock scope
224
225 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700226 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800227 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700228 }
229 return NO_ERROR;
230}
231
Dan Stoza5ecfb682016-01-04 17:01:02 -0800232int BufferQueueProducer::getFreeBufferLocked() const {
233 if (mCore->mFreeBuffers.empty()) {
234 return BufferQueueCore::INVALID_BUFFER_SLOT;
235 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800237 mCore->mFreeBuffers.pop_front();
238 return slot;
239}
240
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800242 if (mCore->mFreeSlots.empty()) {
243 return BufferQueueCore::INVALID_BUFFER_SLOT;
244 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800245 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800247 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800248}
249
250status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200251 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800252 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
253 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800254 bool tryAgain = true;
255 while (tryAgain) {
256 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800257 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800258 return NO_INIT;
259 }
260
Dan Stoza9f3053d2014-03-06 15:14:33 -0800261 int dequeuedCount = 0;
262 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800263 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700264 if (mSlots[s].mBufferState.isDequeued()) {
265 ++dequeuedCount;
266 }
267 if (mSlots[s].mBufferState.isAcquired()) {
268 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800269 }
270 }
271
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700272 // Producers are not allowed to dequeue more than
273 // mMaxDequeuedBufferCount buffers.
274 // This check is only done if a buffer has already been queued
275 if (mCore->mBufferHasBeenQueued &&
276 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
277 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800278 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800279 return INVALID_OPERATION;
280 }
281
Dan Stoza0de7ea72015-04-23 13:20:51 -0700282 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
283
Dan Stozaae3c3682014-04-18 15:43:35 -0700284 // If we disconnect and reconnect quickly, we can be in a state where
285 // our slots are empty but we have many buffers in the queue. This can
286 // cause us to run out of memory if we outrun the consumer. Wait here if
287 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800288 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700289 bool tooManyBuffers = mCore->mQueue.size()
290 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700293 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700294 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700298 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700299 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800300 } else {
301 if (caller == FreeSlotCaller::Dequeue) {
302 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800303 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
305 *found = slot;
306 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800307 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800308 }
309 } else {
310 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800311 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800312 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
313 *found = slot;
314 } else {
315 *found = getFreeBufferLocked();
316 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700317 }
318 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700319 }
320
321 // If no buffer is found, or if the queue has too many buffers
322 // outstanding, wait for a buffer to be acquired or released, or for the
323 // max buffer count to change.
324 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
325 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 if (tryAgain) {
327 // Return an error if we're in non-blocking mode (producer and
328 // consumer are controlled by the application).
329 // However, the consumer is allowed to briefly acquire an extra
330 // buffer (which could cause us to have to wait here), which is
331 // okay, since it is only used to implement an atomic acquire +
332 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700333 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
335 return WOULD_BLOCK;
336 }
Dan Stoza127fc632015-06-30 13:43:32 -0700337 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200338 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
339 std::chrono::nanoseconds(mDequeueTimeout));
340 if (result == std::cv_status::timeout) {
341 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700342 }
343 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200344 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700345 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800346 }
347 } // while (tryAgain)
348
349 return NO_ERROR;
350}
351
Ian Elliottd11b0442017-07-18 11:05:49 -0600352status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
353 uint32_t width, uint32_t height, PixelFormat format,
354 uint64_t usage, uint64_t* outBufferAge,
355 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800356 ATRACE_CALL();
357 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200358 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800359 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700360
361 if (mCore->mIsAbandoned) {
362 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
363 return NO_INIT;
364 }
365
366 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
367 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
368 return NO_INIT;
369 }
Dan Stoza289ade12014-02-28 11:17:17 -0800370 } // Autolock scope
371
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700372 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800373
374 if ((width && !height) || (!width && height)) {
375 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
376 return BAD_VALUE;
377 }
378
379 status_t returnFlags = NO_ERROR;
380 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
381 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800382 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800383
384 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200385 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800386
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100387 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
388 // is finished such that we don't allocate in parallel.
389 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
390 mDequeueWaitingForAllocation = true;
391 mCore->waitWhileAllocatingLocked(lock);
392 mDequeueWaitingForAllocation = false;
393 mDequeueWaitingForAllocationCondition.notify_all();
394 }
Dan Stoza289ade12014-02-28 11:17:17 -0800395
396 if (format == 0) {
397 format = mCore->mDefaultBufferFormat;
398 }
399
400 // Enable the usage bits the consumer requested
401 usage |= mCore->mConsumerUsageBits;
402
Dan Stoza9de72932015-04-16 17:28:43 -0700403 const bool useDefaultSize = !width && !height;
404 if (useDefaultSize) {
405 width = mCore->mDefaultWidth;
406 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800407 }
Dan Stoza289ade12014-02-28 11:17:17 -0800408
Pablo Ceballos981066c2016-02-18 12:54:37 -0800409 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700410 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200411 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700412 if (status != NO_ERROR) {
413 return status;
414 }
415
416 // This should not happen
417 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
418 BQ_LOGE("dequeueBuffer: no available buffer slots");
419 return -EBUSY;
420 }
421
422 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
423
424 // If we are not allowed to allocate new buffers,
425 // waitForFreeSlotThenRelock must have returned a slot containing a
426 // buffer. If this buffer would require reallocation to meet the
427 // requested attributes, we free it and attempt to get another one.
428 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700429 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700430 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700431 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700432 return BAD_VALUE;
433 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800434 mCore->mFreeSlots.insert(found);
435 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700436 found = BufferItem::INVALID_BUFFER_SLOT;
437 continue;
438 }
439 }
Dan Stoza289ade12014-02-28 11:17:17 -0800440 }
441
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700443 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700444 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
446 "buffer");
447
448 return BAD_VALUE;
449 }
450
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700451 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800452 mCore->mActiveBuffers.insert(found);
453 }
Dan Stoza289ade12014-02-28 11:17:17 -0800454 *outSlot = found;
455 ATRACE_BUFFER_INDEX(found);
456
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800457 attachedByConsumer = mSlots[found].mNeedsReallocation;
458 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800459
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700460 mSlots[found].mBufferState.dequeue();
461
Yi Kong48a619f2018-06-05 16:34:59 -0700462 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700463 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800464 {
465 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700466 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800467 mSlots[found].mRequestBufferCalled = false;
468 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
469 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
470 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800471 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700472 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800473
474 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800475 } else {
476 // We add 1 because that will be the frame number when this buffer
477 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800479 }
480
Dan Stoza800b41a2015-04-28 14:20:04 -0700481 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
482 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800483
Yi Kong48a619f2018-06-05 16:34:59 -0700484 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800485 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
486 "slot=%d w=%d h=%d format=%u",
487 found, buffer->width, buffer->height, buffer->format);
488 }
489
490 eglDisplay = mSlots[found].mEglDisplay;
491 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700492 // Don't return a fence in shared buffer mode, except for the first
493 // frame.
494 *outFence = (mCore->mSharedBufferMode &&
495 mCore->mSharedBufferSlot == found) ?
496 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800497 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
498 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700499
500 // If shared buffer mode has just been enabled, cache the slot of the
501 // first buffer that is dequeued and mark it as the shared buffer.
502 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
503 BufferQueueCore::INVALID_BUFFER_SLOT) {
504 mCore->mSharedBufferSlot = found;
505 mSlots[found].mBufferState.mShared = true;
506 }
Dan Stoza289ade12014-02-28 11:17:17 -0800507 } // Autolock scope
508
509 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700510 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700511 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700512 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700513 {mConsumerName.string(), mConsumerName.size()});
514
515 status_t error = graphicBuffer->initCheck();
516
Dan Stoza289ade12014-02-28 11:17:17 -0800517 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200518 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800519
Chia-I Wufeec3b12017-05-25 09:34:56 -0700520 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
522 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
523 }
524
525 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200526 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700527
Chia-I Wufeec3b12017-05-25 09:34:56 -0700528 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700529 mCore->mFreeSlots.insert(*outSlot);
530 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700531 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
532 return error;
533 }
534
Dan Stoza289ade12014-02-28 11:17:17 -0800535 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700536 mCore->mFreeSlots.insert(*outSlot);
537 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800538 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
539 return NO_INIT;
540 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800541
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700542 if (mCore->mConsumerListener != nullptr) {
543 BufferItem item;
544 item.mGraphicBuffer = graphicBuffer;
545 item.mSlot = *outSlot;
546 mCore->mConsumerListener->onBufferAllocated(item);
547 }
548
Pablo Ceballos9e314332016-01-12 13:49:19 -0800549 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800550 } // Autolock scope
551 }
552
Dan Stoza9f3053d2014-03-06 15:14:33 -0800553 if (attachedByConsumer) {
554 returnFlags |= BUFFER_NEEDS_REALLOCATION;
555 }
556
Dan Stoza289ade12014-02-28 11:17:17 -0800557 if (eglFence != EGL_NO_SYNC_KHR) {
558 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
559 1000000000);
560 // If something goes wrong, log the error, but return the buffer without
561 // synchronizing access to it. It's too late at this point to abort the
562 // dequeue operation.
563 if (result == EGL_FALSE) {
564 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
565 eglGetError());
566 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
567 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
568 }
569 eglDestroySyncKHR(eglDisplay, eglFence);
570 }
571
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700572 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
573 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800574 mSlots[*outSlot].mFrameNumber,
575 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
576
Ian Elliottd11b0442017-07-18 11:05:49 -0600577 if (outBufferAge) {
578 *outBufferAge = mCore->mBufferAge;
579 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700580 addAndGetFrameTimestamps(nullptr, outTimestamps);
581
Dan Stoza289ade12014-02-28 11:17:17 -0800582 return returnFlags;
583}
584
Dan Stoza9f3053d2014-03-06 15:14:33 -0800585status_t BufferQueueProducer::detachBuffer(int slot) {
586 ATRACE_CALL();
587 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800589
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700590 sp<IConsumerListener> listener;
591 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200592 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700593
594 if (mCore->mIsAbandoned) {
595 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
596 return NO_INIT;
597 }
598
599 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
600 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
601 return NO_INIT;
602 }
603
604 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
605 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
606 return BAD_VALUE;
607 }
608
609 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
610 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
611 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
612 return BAD_VALUE;
613 } else if (!mSlots[slot].mBufferState.isDequeued()) {
614 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
615 "(state = %s)", slot, mSlots[slot].mBufferState.string());
616 return BAD_VALUE;
617 } else if (!mSlots[slot].mRequestBufferCalled) {
618 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
619 slot);
620 return BAD_VALUE;
621 }
622
623 mSlots[slot].mBufferState.detachProducer();
624 mCore->mActiveBuffers.erase(slot);
625 mCore->mFreeSlots.insert(slot);
626 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200627 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700628 VALIDATE_CONSISTENCY();
629 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630 }
631
Yi Kong48a619f2018-06-05 16:34:59 -0700632 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700633 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700634 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800635
Dan Stoza9f3053d2014-03-06 15:14:33 -0800636 return NO_ERROR;
637}
638
Dan Stozad9822a32014-03-28 15:25:31 -0700639status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
640 sp<Fence>* outFence) {
641 ATRACE_CALL();
642
Yi Kong48a619f2018-06-05 16:34:59 -0700643 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700644 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
645 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700646 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700647 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
648 return BAD_VALUE;
649 }
650
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700651 sp<IConsumerListener> listener;
652 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200653 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700654
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700655 if (mCore->mIsAbandoned) {
656 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
657 return NO_INIT;
658 }
659
660 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
661 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
662 return NO_INIT;
663 }
664
665 if (mCore->mSharedBufferMode) {
666 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
667 "mode");
668 return BAD_VALUE;
669 }
670
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200671 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700672
673 if (mCore->mFreeBuffers.empty()) {
674 return NO_MEMORY;
675 }
676
677 int found = mCore->mFreeBuffers.front();
678 mCore->mFreeBuffers.remove(found);
679 mCore->mFreeSlots.insert(found);
680
681 BQ_LOGV("detachNextBuffer detached slot %d", found);
682
683 *outBuffer = mSlots[found].mGraphicBuffer;
684 *outFence = mSlots[found].mFence;
685 mCore->clearBufferSlotLocked(found);
686 VALIDATE_CONSISTENCY();
687 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700688 }
689
Yi Kong48a619f2018-06-05 16:34:59 -0700690 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700691 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700692 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800693
Dan Stozad9822a32014-03-28 15:25:31 -0700694 return NO_ERROR;
695}
696
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697status_t BufferQueueProducer::attachBuffer(int* outSlot,
698 const sp<android::GraphicBuffer>& buffer) {
699 ATRACE_CALL();
700
Yi Kong48a619f2018-06-05 16:34:59 -0700701 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700702 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700704 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700705 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800706 return BAD_VALUE;
707 }
708
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200709 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700710
711 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700712 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700713 return NO_INIT;
714 }
715
716 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700717 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700718 return NO_INIT;
719 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800720
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700721 if (mCore->mSharedBufferMode) {
722 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700723 return BAD_VALUE;
724 }
725
Dan Stoza812ed062015-06-02 15:45:22 -0700726 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
727 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
728 "[queue %u]", buffer->getGenerationNumber(),
729 mCore->mGenerationNumber);
730 return BAD_VALUE;
731 }
732
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200733 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700734
Dan Stoza9f3053d2014-03-06 15:14:33 -0800735 status_t returnFlags = NO_ERROR;
736 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200737 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 if (status != NO_ERROR) {
739 return status;
740 }
741
742 // This should not happen
743 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700744 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800745 return -EBUSY;
746 }
747
748 *outSlot = found;
749 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700750 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800751 *outSlot, returnFlags);
752
753 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700754 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800755 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
756 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700757 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800758 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800759 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800760 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800761 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700762
Dan Stoza9f3053d2014-03-06 15:14:33 -0800763 return returnFlags;
764}
765
Dan Stoza289ade12014-02-28 11:17:17 -0800766status_t BufferQueueProducer::queueBuffer(int slot,
767 const QueueBufferInput &input, QueueBufferOutput *output) {
768 ATRACE_CALL();
769 ATRACE_BUFFER_INDEX(slot);
770
Brian Andersond6927fb2016-07-23 23:37:30 -0700771 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800773 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700774 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800775 int scalingMode;
776 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700777 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700778 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700779 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700780 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700781 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
782 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700783 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700784 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800785
Yi Kong48a619f2018-06-05 16:34:59 -0700786 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800787 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800788 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800789 }
790
Brian Anderson3d4039d2016-09-23 16:31:30 -0700791 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
792
Dan Stoza289ade12014-02-28 11:17:17 -0800793 switch (scalingMode) {
794 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
795 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
796 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
797 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
798 break;
799 default:
800 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 }
803
Dan Stoza8dc55392014-11-04 11:37:46 -0800804 sp<IConsumerListener> frameAvailableListener;
805 sp<IConsumerListener> frameReplacedListener;
806 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700807 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800808 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800809 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200810 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800811
812 if (mCore->mIsAbandoned) {
813 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
814 return NO_INIT;
815 }
816
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700817 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
818 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
819 return NO_INIT;
820 }
821
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800822 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800823 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800824 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800825 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700826 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800827 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700828 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800829 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800830 } else if (!mSlots[slot].mRequestBufferCalled) {
831 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
832 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800833 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800834 }
835
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700836 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700837 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700838 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700839 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700840 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700841 mSlots[slot].mBufferState.mShared = true;
842 }
843
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800844 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700845 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
846 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
847 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700848 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800849 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800850
851 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
852 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700853 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800854 crop.intersect(bufferRect, &croppedRect);
855 if (croppedRect != crop) {
856 BQ_LOGE("queueBuffer: crop rect is not contained within the "
857 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800858 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800859 }
860
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800861 // Override UNKNOWN dataspace with consumer default
862 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
863 dataSpace = mCore->mDefaultBufferDataSpace;
864 }
865
Brian Andersond6927fb2016-07-23 23:37:30 -0700866 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700867 mSlots[slot].mBufferState.queue();
868
Brian Andersond6927fb2016-07-23 23:37:30 -0700869 // Increment the frame counter and store a local version of it
870 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800871 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700872 currentFrameNumber = mCore->mFrameCounter;
873 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800874
Dan Stoza289ade12014-02-28 11:17:17 -0800875 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
876 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
877 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800878 item.mTransform = transform &
879 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800880 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800881 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
882 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700883 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800884 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800885 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700886 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700887 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800888 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700889 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700890 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700891 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800892 (!mCore->mLegacyBufferDrop && mConsumerIsSurfaceFlinger) ||
893 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700894 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700895 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700896 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700897 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800898 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800899
Ruben Brunk1681d952014-06-27 15:51:55 -0700900 mStickyTransform = stickyTransform;
901
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700902 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700903 if (mCore->mSharedBufferMode) {
904 mCore->mSharedBufferCache.crop = crop;
905 mCore->mSharedBufferCache.transform = transform;
906 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700907 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700908 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700909 }
910
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800911 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800912 if (mCore->mQueue.empty()) {
913 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
914 // and simply queue this buffer
915 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800916 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800917 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700918 // When the queue is not empty, we need to look at the last buffer
919 // in the queue to see if we need to replace it
920 const BufferItem& last = mCore->mQueue.itemAt(
921 mCore->mQueue.size() - 1);
922 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800923
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700924 if (!last.mIsStale) {
925 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700926
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700927 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700928 // still be around. Mark it as no longer shared if this
929 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700930 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700931 mSlots[last.mSlot].mBufferState.isFree()) {
932 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700933 }
934 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700935 if (!mSlots[last.mSlot].mBufferState.isShared()) {
936 mCore->mActiveBuffers.erase(last.mSlot);
937 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800938 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700939 }
Dan Stoza289ade12014-02-28 11:17:17 -0800940 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800941
Dan Stoza289ade12014-02-28 11:17:17 -0800942 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700943 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800944 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800945 } else {
946 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800947 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800948 }
949 }
950
951 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200952 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700953 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800954
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700955 output->width = mCore->mDefaultWidth;
956 output->height = mCore->mDefaultHeight;
957 output->transformHint = mCore->mTransformHint;
958 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
959 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800960
Colin Cross152c3b72016-09-27 14:08:19 -0700961 ATRACE_INT(mCore->mConsumerName.string(),
962 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700963 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800964
965 // Take a ticket for the callback functions
966 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700967
Pablo Ceballos9e314332016-01-12 13:49:19 -0800968 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800969 } // Autolock scope
970
Irvel468051e2016-06-13 16:44:44 -0700971 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
972 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
973 // there will be no Binder call
974 if (!mConsumerIsSurfaceFlinger) {
975 item.mGraphicBuffer.clear();
976 }
977
978 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800979 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
980
981 // Call back without the main BufferQueue lock held, but with the callback
982 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700983
984 int connectedApi;
985 sp<Fence> lastQueuedFence;
986
987 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200988 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -0800989 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200990 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -0800991 }
992
Yi Kong48a619f2018-06-05 16:34:59 -0700993 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800994 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -0700995 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800996 frameReplacedListener->onFrameReplaced(item);
997 }
998
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700999 connectedApi = mCore->mConnectedApi;
1000 lastQueuedFence = std::move(mLastQueueBufferFence);
1001
1002 mLastQueueBufferFence = std::move(acquireFence);
1003 mLastQueuedCrop = item.mCrop;
1004 mLastQueuedTransform = item.mTransform;
1005
Dan Stoza8dc55392014-11-04 11:37:46 -08001006 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001007 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001008 }
1009
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001010 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001011 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001012 // Waiting here allows for two full buffers to be queued but not a
1013 // third. In the event that frames take varying time, this makes a
1014 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001015 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001016 }
1017
Brian Andersond6927fb2016-07-23 23:37:30 -07001018 // Update and get FrameEventHistory.
1019 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1020 NewFrameEventsEntry newFrameEventsEntry = {
1021 currentFrameNumber,
1022 postedTime,
1023 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001024 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001025 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001026 addAndGetFrameTimestamps(&newFrameEventsEntry,
1027 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001028
Dan Stoza289ade12014-02-28 11:17:17 -08001029 return NO_ERROR;
1030}
1031
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001032status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001033 ATRACE_CALL();
1034 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001035 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001036
1037 if (mCore->mIsAbandoned) {
1038 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001039 return NO_INIT;
1040 }
1041
1042 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1043 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1044 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001045 }
1046
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001047 if (mCore->mSharedBufferMode) {
1048 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001049 return BAD_VALUE;
1050 }
1051
Dan Stoza3e96f192014-03-03 10:16:19 -08001052 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001053 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001054 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001055 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001056 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001057 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001058 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001059 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001060 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001061 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001062 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001063 }
1064
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001065 mSlots[slot].mBufferState.cancel();
1066
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001067 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001068 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001069 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001070 mSlots[slot].mBufferState.mShared = false;
1071 }
1072
1073 // Don't put the shared buffer on the free list.
1074 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001075 mCore->mActiveBuffers.erase(slot);
1076 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001077 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001078
Dan Stoza289ade12014-02-28 11:17:17 -08001079 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001080 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001081 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001082
1083 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001084}
1085
1086int BufferQueueProducer::query(int what, int *outValue) {
1087 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001088 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001089
Yi Kong48a619f2018-06-05 16:34:59 -07001090 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001091 BQ_LOGE("query: outValue was NULL");
1092 return BAD_VALUE;
1093 }
1094
1095 if (mCore->mIsAbandoned) {
1096 BQ_LOGE("query: BufferQueue has been abandoned");
1097 return NO_INIT;
1098 }
1099
1100 int value;
1101 switch (what) {
1102 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001103 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001104 break;
1105 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001106 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001107 break;
1108 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001109 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001110 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001111 case NATIVE_WINDOW_LAYER_COUNT:
1112 // All BufferQueue buffers have a single layer.
1113 value = BQ_LAYER_COUNT;
1114 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001115 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001116 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001117 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001118 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001119 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001120 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001121 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1122 value = (mCore->mQueue.size() > 1);
1123 break;
1124 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001125 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001126 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001127 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001128 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1129 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1130 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001131 case NATIVE_WINDOW_BUFFER_AGE:
1132 if (mCore->mBufferAge > INT32_MAX) {
1133 value = 0;
1134 } else {
1135 value = static_cast<int32_t>(mCore->mBufferAge);
1136 }
1137 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001138 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1139 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1140 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001141 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1142 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1143 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001144 default:
1145 return BAD_VALUE;
1146 }
1147
1148 BQ_LOGV("query: %d? %d", what, value);
1149 *outValue = value;
1150 return NO_ERROR;
1151}
1152
Dan Stozaf0eaf252014-03-21 13:05:51 -07001153status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001154 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1155 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001156 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001157 mConsumerName = mCore->mConsumerName;
1158 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1159 producerControlledByApp ? "true" : "false");
1160
1161 if (mCore->mIsAbandoned) {
1162 BQ_LOGE("connect: BufferQueue has been abandoned");
1163 return NO_INIT;
1164 }
1165
Yi Kong48a619f2018-06-05 16:34:59 -07001166 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001167 BQ_LOGE("connect: BufferQueue has no consumer");
1168 return NO_INIT;
1169 }
1170
Yi Kong48a619f2018-06-05 16:34:59 -07001171 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001172 BQ_LOGE("connect: output was NULL");
1173 return BAD_VALUE;
1174 }
1175
1176 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1177 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1178 mCore->mConnectedApi, api);
1179 return BAD_VALUE;
1180 }
1181
1182 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1183 mDequeueTimeout < 0 ?
1184 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1185 mCore->mMaxBufferCount) -
1186 mCore->getMaxBufferCountLocked();
1187 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1188 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1189 "slots. Delta = %d", delta);
1190 return BAD_VALUE;
1191 }
1192
Dan Stoza289ade12014-02-28 11:17:17 -08001193 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001194 switch (api) {
1195 case NATIVE_WINDOW_API_EGL:
1196 case NATIVE_WINDOW_API_CPU:
1197 case NATIVE_WINDOW_API_MEDIA:
1198 case NATIVE_WINDOW_API_CAMERA:
1199 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001200
1201 output->width = mCore->mDefaultWidth;
1202 output->height = mCore->mDefaultHeight;
1203 output->transformHint = mCore->mTransformHint;
1204 output->numPendingBuffers =
1205 static_cast<uint32_t>(mCore->mQueue.size());
1206 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001207 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001208
Yi Kong48a619f2018-06-05 16:34:59 -07001209 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001210 // Set up a death notification so that we can disconnect
1211 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001212 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001213 status = IInterface::asBinder(listener)->linkToDeath(
1214 static_cast<IBinder::DeathRecipient*>(this));
1215 if (status != NO_ERROR) {
1216 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1217 strerror(-status), status);
1218 }
1219 mCore->mLinkedToDeath = listener;
1220 }
1221 if (listener->needsReleaseNotify()) {
1222 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001223 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001224 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001225 break;
1226 default:
1227 BQ_LOGE("connect: unknown API %d", api);
1228 status = BAD_VALUE;
1229 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001230 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001231 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001232 mCore->mBufferHasBeenQueued = false;
1233 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001234 mCore->mQueueBufferCanDrop = false;
1235 mCore->mLegacyBufferDrop = true;
1236 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1237 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1238 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001239 }
Dan Stoza289ade12014-02-28 11:17:17 -08001240
Pablo Ceballos981066c2016-02-18 12:54:37 -08001241 mCore->mAllowAllocation = true;
1242 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001243 return status;
1244}
1245
Robert Carr97b9c862016-09-08 13:54:35 -07001246status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001247 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001248 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001249
1250 int status = NO_ERROR;
1251 sp<IConsumerListener> listener;
1252 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001253 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001254
1255 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001256 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001257 return NO_ERROR;
1258 }
1259 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1260 }
1261
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001262 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001263
1264 if (mCore->mIsAbandoned) {
1265 // It's not really an error to disconnect after the surface has
1266 // been abandoned; it should just be a no-op.
1267 return NO_ERROR;
1268 }
1269
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001270 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001271 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1272 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1273 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001274 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001275 // If we're asked to disconnect the currently connected api but
1276 // nobody is connected, it's not really an error.
1277 if (api == BufferQueueCore::NO_CONNECTED_API) {
1278 return NO_ERROR;
1279 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001280 }
1281
Dan Stoza289ade12014-02-28 11:17:17 -08001282 switch (api) {
1283 case NATIVE_WINDOW_API_EGL:
1284 case NATIVE_WINDOW_API_CPU:
1285 case NATIVE_WINDOW_API_MEDIA:
1286 case NATIVE_WINDOW_API_CAMERA:
1287 if (mCore->mConnectedApi == api) {
1288 mCore->freeAllBuffersLocked();
1289
1290 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001291 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001292 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001293 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001294 // This can fail if we're here because of the death
1295 // notification, but we just ignore it
1296 token->unlinkToDeath(
1297 static_cast<IBinder::DeathRecipient*>(this));
1298 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001299 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001300 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001301 mCore->mLinkedToDeath = nullptr;
1302 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001303 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001304 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001305 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001306 mCore->mDequeueCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001307 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001308 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1309 BQ_LOGE("disconnect: not connected (req=%d)", api);
1310 status = NO_INIT;
1311 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001312 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001313 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001314 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001315 }
1316 break;
1317 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001318 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001319 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001320 break;
1321 }
1322 } // Autolock scope
1323
1324 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001325 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001326 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001327 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001328 }
1329
1330 return status;
1331}
1332
Jesse Hall399184a2014-03-03 15:42:54 -08001333status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001334 sp<IConsumerListener> listener;
1335 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001336 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001337 mCore->mSidebandStream = stream;
1338 listener = mCore->mConsumerListener;
1339 } // Autolock scope
1340
Yi Kong48a619f2018-06-05 16:34:59 -07001341 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001342 listener->onSidebandStreamChanged();
1343 }
Jesse Hall399184a2014-03-03 15:42:54 -08001344 return NO_ERROR;
1345}
1346
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001347void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001348 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001349 ATRACE_CALL();
1350 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001351 size_t newBufferCount = 0;
1352 uint32_t allocWidth = 0;
1353 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001354 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001355 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001356 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001357 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001358 std::unique_lock<std::mutex> lock(mCore->mMutex);
1359 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001360
Dan Stoza9de72932015-04-16 17:28:43 -07001361 if (!mCore->mAllowAllocation) {
1362 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1363 "BufferQueue");
1364 return;
1365 }
1366
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001367 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1368 // both allocateBuffers and dequeueBuffer.
1369 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001370 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001371 return;
1372 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001373
Antoine Labour78014f32014-07-15 21:17:03 -07001374 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1375 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1376 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1377 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001378 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001379
1380 mCore->mIsAllocating = true;
1381 } // Autolock scope
1382
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001383 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001384 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001385 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001386 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001387 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001388
1389 status_t result = graphicBuffer->initCheck();
1390
Antoine Labour78014f32014-07-15 21:17:03 -07001391 if (result != NO_ERROR) {
1392 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001393 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001394 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001395 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001396 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001397 return;
1398 }
1399 buffers.push_back(graphicBuffer);
1400 }
1401
1402 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001403 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001404 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1405 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001406 PixelFormat checkFormat = format != 0 ?
1407 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001408 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001409 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1410 checkFormat != allocFormat || checkUsage != allocUsage) {
1411 // Something changed while we released the lock. Retry.
1412 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1413 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001414 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001415 continue;
1416 }
1417
Antoine Labour78014f32014-07-15 21:17:03 -07001418 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001419 if (mCore->mFreeSlots.empty()) {
1420 BQ_LOGV("allocateBuffers: a slot was occupied while "
1421 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001422 continue;
1423 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001424 auto slot = mCore->mFreeSlots.begin();
1425 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1426 mSlots[*slot].mGraphicBuffer = buffers[i];
1427 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001428
1429 // freeBufferLocked puts this slot on the free slots list. Since
1430 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001431 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001432
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001433 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1434 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001435
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001436 if (mCore->mConsumerListener != nullptr) {
1437 BufferItem item;
1438 item.mGraphicBuffer = buffers[i];
1439 item.mSlot = *slot;
1440 mCore->mConsumerListener->onBufferAllocated(item);
1441 }
1442
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001443 // Make sure the erase is done after all uses of the slot
1444 // iterator since it will be invalid after this point.
1445 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001446 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001447
Antoine Labour78014f32014-07-15 21:17:03 -07001448 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001449 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001450 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001451
1452 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1453 // waiting anymore so it can use the buffer we just allocated.
1454 while (mDequeueWaitingForAllocation) {
1455 mDequeueWaitingForAllocationCondition.wait(lock);
1456 }
Antoine Labour78014f32014-07-15 21:17:03 -07001457 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001458 }
1459}
1460
Dan Stoza9de72932015-04-16 17:28:43 -07001461status_t BufferQueueProducer::allowAllocation(bool allow) {
1462 ATRACE_CALL();
1463 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1464
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001465 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001466 mCore->mAllowAllocation = allow;
1467 return NO_ERROR;
1468}
1469
Dan Stoza812ed062015-06-02 15:45:22 -07001470status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1471 ATRACE_CALL();
1472 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1473
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001474 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001475 mCore->mGenerationNumber = generationNumber;
1476 return NO_ERROR;
1477}
1478
Dan Stozac6f30bd2015-06-08 09:32:50 -07001479String8 BufferQueueProducer::getConsumerName() const {
1480 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001481 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001482 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1483 return mConsumerName;
1484}
1485
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001486status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001487 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001488 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001489
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001490 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001491 if (!sharedBufferMode) {
1492 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001493 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001494 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001495 return NO_ERROR;
1496}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001497
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001498status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1499 ATRACE_CALL();
1500 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1501
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001502 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001503
1504 mCore->mAutoRefresh = autoRefresh;
1505 return NO_ERROR;
1506}
1507
Dan Stoza127fc632015-06-30 13:43:32 -07001508status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1509 ATRACE_CALL();
1510 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1511
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001512 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001513 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1514 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1515 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1516 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1517 "available slots. Delta = %d", delta);
1518 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001519 }
1520
Pablo Ceballos981066c2016-02-18 12:54:37 -08001521 mDequeueTimeout = timeout;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001522 if (timeout >= 0) {
1523 mCore->mDequeueBufferCannotBlock = false;
1524 if (timeout != 0) {
1525 mCore->mQueueBufferCanDrop = false;
1526 }
1527 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001528
Pablo Ceballos981066c2016-02-18 12:54:37 -08001529 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001530 return NO_ERROR;
1531}
1532
Sungtak Lee3249fb62019-03-02 16:40:47 -08001533status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1534 ATRACE_CALL();
1535 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1536
1537 std::lock_guard<std::mutex> lock(mCore->mMutex);
1538 mCore->mLegacyBufferDrop = drop;
1539 return NO_ERROR;
1540}
1541
Dan Stoza50101d02016-04-07 16:53:23 -07001542status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001543 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001544 ATRACE_CALL();
1545 BQ_LOGV("getLastQueuedBuffer");
1546
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001547 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001548 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1549 *outBuffer = nullptr;
1550 *outFence = Fence::NO_FENCE;
1551 return NO_ERROR;
1552 }
1553
1554 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1555 *outFence = mLastQueueBufferFence;
1556
John Reck1a61da52016-04-28 13:18:15 -07001557 // Currently only SurfaceFlinger internally ever changes
1558 // GLConsumer's filtering mode, so we just use 'true' here as
1559 // this is slightly specialized for the current client of this API,
1560 // which does want filtering.
1561 GLConsumer::computeTransformMatrix(outTransformMatrix,
1562 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1563 mLastQueuedTransform, true /* filter */);
1564
Dan Stoza50101d02016-04-07 16:53:23 -07001565 return NO_ERROR;
1566}
1567
Brian Anderson3890c392016-07-25 12:48:08 -07001568void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1569 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001570}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001571
Brian Anderson3890c392016-07-25 12:48:08 -07001572void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001573 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001574 FrameEventHistoryDelta* outDelta) {
1575 if (newTimestamps == nullptr && outDelta == nullptr) {
1576 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001577 }
1578
1579 ATRACE_CALL();
1580 BQ_LOGV("addAndGetFrameTimestamps");
1581 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001582 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001583 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001584 listener = mCore->mConsumerListener;
1585 }
Yi Kong48a619f2018-06-05 16:34:59 -07001586 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001587 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001588 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001589}
1590
Dan Stoza289ade12014-02-28 11:17:17 -08001591void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1592 // If we're here, it means that a producer we were connected to died.
1593 // We're guaranteed that we are still connected to it because we remove
1594 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1595 // without synchronization here.
1596 int api = mCore->mConnectedApi;
1597 disconnect(api);
1598}
1599
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001600status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1601 BQ_LOGV("getUniqueId");
1602
1603 *outId = mCore->mUniqueId;
1604 return NO_ERROR;
1605}
1606
Chia-I Wue2786ea2017-08-07 10:36:08 -07001607status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1608 BQ_LOGV("getConsumerUsage");
1609
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001610 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001611 *outUsage = mCore->mConsumerUsageBits;
1612 return NO_ERROR;
1613}
1614
Dan Stoza289ade12014-02-28 11:17:17 -08001615} // namespace android