blob: e05667e89552fcbb183b191c7ec9371ef6cebe25 [file] [log] [blame]
Dan Stoza289ade12014-02-28 19:17:171/*
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 21:32:0417#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 18:16:1919#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 21:49:1923#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 19:17:1729#define EGL_EGLEXT_PROTOTYPES
30
Robert Carr97b9c862016-09-08 20:54:3531#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 19:17:1732#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 20:18:1535#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 19:17:1736#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 20:05:5137#include <gui/IProducerListener.h>
Jayant Chowdharyad9fe272019-03-08 06:36:0638#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 19:17:1739
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
Mathias Agopian6a3c05b2017-04-28 03:06:5543#include <system/window.h>
44
Dan Stoza289ade12014-02-28 19:17:1745namespace android {
46
Craig Donner6ebc46a2016-10-21 22:23:4447static constexpr uint32_t BQ_LAYER_COUNT = 1;
48
Irvel468051e2016-06-13 23:44:4449BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
50 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 19:17:1751 mCore(core),
52 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 22:51:5553 mConsumerName(),
Eric Penner99a0afb2014-09-30 18:28:3054 mStickyTransform(0),
Irvel468051e2016-06-13 23:44:4455 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 19:37:4656 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-21 00:40:3457 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 19:37:4658 mCallbackMutex(),
59 mNextCallbackTicket(0),
60 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 20:43:3261 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-27 23:44:0362 mDequeueTimeout(-1),
63 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 19:17:1764
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 00:27:4470 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:1771
72 if (mCore->mIsAbandoned) {
73 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
74 return NO_INIT;
75 }
76
Pablo Ceballos583b1b32015-09-04 01:23:5277 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 18:16:1982 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:1783 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 18:16:1984 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 19:17:1785 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:4586 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:1787 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:4588 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 19:17:1789 return BAD_VALUE;
90 }
91
92 mSlots[slot].mRequestBufferCalled = true;
93 *buf = mSlots[slot].mGraphicBuffer;
94 return NO_ERROR;
95}
96
Pablo Ceballosfa455352015-08-13 00:47:4797status_t BufferQueueProducer::setMaxDequeuedBufferCount(
98 int maxDequeuedBuffers) {
99 ATRACE_CALL();
100 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
101 maxDequeuedBuffers);
102
Pablo Ceballos981066c2016-02-18 20:54:37103 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-13 00:47:47104 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44105 std::unique_lock<std::mutex> lock(mCore->mMutex);
106 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-13 00:47:47107
108 if (mCore->mIsAbandoned) {
109 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
110 "abandoned");
111 return NO_INIT;
112 }
113
Pablo Ceballos245cc5b2016-04-19 18:33:00114 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
115 return NO_ERROR;
116 }
117
Pablo Ceballos72daab62015-12-08 00:38:43118 // The new maxDequeuedBuffer count should not be violated by the number
119 // of currently dequeued buffers
120 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 20:15:22121 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 22:05:45122 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-08 00:38:43123 dequeuedCount++;
Pablo Ceballosfa455352015-08-13 00:47:47124 }
125 }
Pablo Ceballos72daab62015-12-08 00:38:43126 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-13 00:47:47132
Pablo Ceballos567dbbb2015-08-27 01:59:08133 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-13 00:47:47134 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-27 01:59:08142 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-13 00:47:47143 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 23:16:06149 if (bufferCount > mCore->mMaxBufferCount) {
150 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-27 01:59:08151 "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 23:16:06155 return BAD_VALUE;
156 }
157
Pablo Ceballos72daab62015-12-08 00:38:43158 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 20:54:37159 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 20:15:22160 return BAD_VALUE;
161 }
Pablo Ceballosfa455352015-08-13 00:47:47162 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 21:49:19163 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-08 00:38:43164 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 20:54:37165 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-08 00:38:43166 }
Jorim Jaggi6ae55032019-04-02 00:27:44167 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-13 00:47:47168 } // Autolock scope
169
170 // Call back without lock held
Yi Kong48a619f2018-06-05 23:34:59171 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 20:54:37172 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-13 00:47:47173 }
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 20:54:37182 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-13 00:47:47183 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44184 std::unique_lock<std::mutex> lock(mCore->mMutex);
185 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-13 00:47:47186
187 if (mCore->mIsAbandoned) {
188 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
189 return NO_INIT;
190 }
191
Pablo Ceballos245cc5b2016-04-19 18:33:00192 if (async == mCore->mAsyncMode) {
193 return NO_ERROR;
194 }
195
Pablo Ceballos19e3e062015-08-19 23:16:06196 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-27 01:59:08197 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
198 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 23:16:06199 BQ_LOGE("setAsyncMode(%d): this call would cause the "
200 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-27 01:59:08201 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
202 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
203 mCore->mMaxDequeuedBufferCount,
204 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 23:16:06205 return BAD_VALUE;
206 }
207
Pablo Ceballos23b4abe2016-01-08 20:15:22208 int delta = mCore->getMaxBufferCountLocked(async,
209 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
210 - mCore->getMaxBufferCountLocked();
211
Pablo Ceballos981066c2016-02-18 20:54:37212 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 20:15:22213 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
214 "available slots. Delta = %d", delta);
215 return BAD_VALUE;
216 }
Pablo Ceballosfa455352015-08-13 00:47:47217 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 21:49:19218 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 00:27:44219 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 18:33:00220 if (delta < 0) {
221 listener = mCore->mConsumerListener;
222 }
Pablo Ceballosfa455352015-08-13 00:47:47223 } // Autolock scope
224
225 // Call back without lock held
Yi Kong48a619f2018-06-05 23:34:59226 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 20:54:37227 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-13 00:47:47228 }
229 return NO_ERROR;
230}
231
Dan Stoza5ecfb682016-01-05 01:01:02232int BufferQueueProducer::getFreeBufferLocked() const {
233 if (mCore->mFreeBuffers.empty()) {
234 return BufferQueueCore::INVALID_BUFFER_SLOT;
235 }
Pablo Ceballos23b4abe2016-01-08 20:15:22236 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-05 01:01:02237 mCore->mFreeBuffers.pop_front();
238 return slot;
239}
240
Pablo Ceballos23b4abe2016-01-08 20:15:22241int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-05 01:01:02242 if (mCore->mFreeSlots.empty()) {
243 return BufferQueueCore::INVALID_BUFFER_SLOT;
244 }
Pablo Ceballosdce5c552016-02-10 23:43:22245 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 20:15:22246 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 23:43:22247 return slot;
Dan Stoza5ecfb682016-01-05 01:01:02248}
249
250status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 00:27:44251 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-05 01:01:02252 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
253 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 23:14:33254 bool tryAgain = true;
255 while (tryAgain) {
256 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-05 01:01:02257 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 23:14:33258 return NO_INIT;
259 }
260
Dan Stoza9f3053d2014-03-06 23:14:33261 int dequeuedCount = 0;
262 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 20:15:22263 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 22:05:45264 if (mSlots[s].mBufferState.isDequeued()) {
265 ++dequeuedCount;
266 }
267 if (mSlots[s].mBufferState.isAcquired()) {
268 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 23:14:33269 }
270 }
271
Pablo Ceballose5b755a2015-08-13 23:18:19272 // 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 23:36:44277 // 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 23:14:33283 return INVALID_OPERATION;
284 }
285
Dan Stoza0de7ea72015-04-23 20:20:51286 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
287
Dan Stozaae3c3682014-04-18 22:43:35288 // 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 20:15:22292 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 21:32:04293 bool tooManyBuffers = mCore->mQueue.size()
294 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 22:43:35295 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-05 01:01:02296 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 22:43:35297 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 20:20:51298 } else {
Pablo Ceballos3559fbf2016-03-17 22:50:23299 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 22:05:45300 // return it.
Pablo Ceballos3559fbf2016-03-17 22:50:23301 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 22:05:45302 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 22:50:23303 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-05 01:01:02304 } else {
305 if (caller == FreeSlotCaller::Dequeue) {
306 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 20:15:22307 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-05 01:01:02308 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
309 *found = slot;
310 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 20:15:22311 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-05 01:01:02312 }
313 } else {
314 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 20:15:22315 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-05 01:01:02316 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
317 *found = slot;
318 } else {
319 *found = getFreeBufferLocked();
320 }
Dan Stoza0de7ea72015-04-23 20:20:51321 }
322 }
Dan Stozaae3c3682014-04-18 22:43:35323 }
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 23:14:33330 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-13 00:47:47337 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 23:14:33338 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
339 return WOULD_BLOCK;
340 }
Dan Stoza127fc632015-06-30 20:43:32341 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 00:27:44342 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 20:43:32346 }
347 } else {
Jorim Jaggi6ae55032019-04-02 00:27:44348 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 20:43:32349 }
Dan Stoza9f3053d2014-03-06 23:14:33350 }
351 } // while (tryAgain)
352
353 return NO_ERROR;
354}
355
Ian Elliottd11b0442017-07-18 17:05:49356status_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 19:17:17360 ATRACE_CALL();
361 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44362 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:17363 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-04 01:23:52364
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 19:17:17374 } // Autolock scope
375
Mathias Agopiancb496ac2017-05-22 21:21:00376 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 19:17:17377
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 23:14:33386 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 19:17:17387
388 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44389 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:17390
Jorim Jaggi35b4e382019-03-27 23:44:03391 // 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 19:17:17399
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-17 00:28:43407 const bool useDefaultSize = !width && !height;
408 if (useDefaultSize) {
409 width = mCore->mDefaultWidth;
410 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-25 02:35:03411 if (mCore->mAutoPrerotation &&
412 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
413 std::swap(width, height);
414 }
Dan Stoza9f3053d2014-03-06 23:14:33415 }
Dan Stoza289ade12014-02-28 19:17:17416
Pablo Ceballos981066c2016-02-18 20:54:37417 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-17 00:28:43418 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 00:27:44419 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-17 00:28:43420 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 21:21:00437 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 22:50:23438 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 21:21:00439 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 22:05:45440 return BAD_VALUE;
441 }
Pablo Ceballos23b4abe2016-01-08 20:15:22442 mCore->mFreeSlots.insert(found);
443 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-17 00:28:43444 found = BufferItem::INVALID_BUFFER_SLOT;
445 continue;
446 }
447 }
Dan Stoza289ade12014-02-28 19:17:17448 }
449
Pablo Ceballos23b4abe2016-01-08 20:15:22450 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 22:50:23451 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 21:21:00452 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 20:15:22453 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
454 "buffer");
455
456 return BAD_VALUE;
457 }
458
Pablo Ceballos3559fbf2016-03-17 22:50:23459 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 20:15:22460 mCore->mActiveBuffers.insert(found);
461 }
Dan Stoza289ade12014-02-28 19:17:17462 *outSlot = found;
463 ATRACE_BUFFER_INDEX(found);
464
Pablo Ceballos23b4abe2016-01-08 20:15:22465 attachedByConsumer = mSlots[found].mNeedsReallocation;
466 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 23:14:33467
Pablo Ceballosccdfd602015-10-07 22:05:45468 mSlots[found].mBufferState.dequeue();
469
Yi Kong48a619f2018-06-05 23:34:59470 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 22:49:32471 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 19:17:17472 {
473 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 23:34:59474 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 19:17:17475 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-26 00:49:08479 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 22:05:45480 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 19:17:17481
482 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-26 00:49:08483 } else {
484 // We add 1 because that will be the frame number when this buffer
485 // is queued
Mathias Agopiancb496ac2017-05-22 21:21:00486 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 19:17:17487 }
488
Dan Stoza800b41a2015-04-28 21:20:04489 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
490 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-26 00:49:08491
Yi Kong48a619f2018-06-05 23:34:59492 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 19:17:17493 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 22:03:21500 // 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 19:17:17505 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
506 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 22:03:21507
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 19:17:17515 } // Autolock scope
516
517 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 20:13:57518 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 22:49:32519 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 19:43:28520 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 22:49:32521 {mConsumerName.string(), mConsumerName.size()});
522
523 status_t error = graphicBuffer->initCheck();
524
Dan Stoza289ade12014-02-28 19:17:17525 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44526 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:17527
Chia-I Wufeec3b12017-05-25 16:34:56528 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 22:05:45529 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
530 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
531 }
532
533 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 00:27:44534 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 22:05:45535
Chia-I Wufeec3b12017-05-25 16:34:56536 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 22:08:33537 mCore->mFreeSlots.insert(*outSlot);
538 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 22:05:45539 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
540 return error;
541 }
542
Dan Stoza289ade12014-02-28 19:17:17543 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 22:08:33544 mCore->mFreeSlots.insert(*outSlot);
545 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 19:17:17546 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
547 return NO_INIT;
548 }
Pablo Ceballos23b4abe2016-01-08 20:15:22549
Pablo Ceballos9e314332016-01-12 21:49:19550 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:17551 } // Autolock scope
552 }
553
Dan Stoza9f3053d2014-03-06 23:14:33554 if (attachedByConsumer) {
555 returnFlags |= BUFFER_NEEDS_REALLOCATION;
556 }
557
Dan Stoza289ade12014-02-28 19:17:17558 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 21:32:04573 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
574 *outSlot,
Dan Stoza289ade12014-02-28 19:17:17575 mSlots[*outSlot].mFrameNumber,
576 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
577
Ian Elliottd11b0442017-07-18 17:05:49578 if (outBufferAge) {
579 *outBufferAge = mCore->mBufferAge;
580 }
Brian Anderson7c3ba8a2016-07-25 19:48:08581 addAndGetFrameTimestamps(nullptr, outTimestamps);
582
Dan Stoza289ade12014-02-28 19:17:17583 return returnFlags;
584}
585
Dan Stoza9f3053d2014-03-06 23:14:33586status_t BufferQueueProducer::detachBuffer(int slot) {
587 ATRACE_CALL();
588 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 22:05:45589 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 23:14:33590
Eino-Ville Talvala93dd0512016-06-10 21:21:02591 sp<IConsumerListener> listener;
592 {
Jorim Jaggi6ae55032019-04-02 00:27:44593 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 21:21:02594
595 if (mCore->mIsAbandoned) {
596 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
597 return NO_INIT;
598 }
599
600 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
601 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
602 return NO_INIT;
603 }
604
605 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
606 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
607 return BAD_VALUE;
608 }
609
610 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
611 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
612 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
613 return BAD_VALUE;
614 } else if (!mSlots[slot].mBufferState.isDequeued()) {
615 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
616 "(state = %s)", slot, mSlots[slot].mBufferState.string());
617 return BAD_VALUE;
618 } else if (!mSlots[slot].mRequestBufferCalled) {
619 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
620 slot);
621 return BAD_VALUE;
622 }
623
624 mSlots[slot].mBufferState.detachProducer();
625 mCore->mActiveBuffers.erase(slot);
626 mCore->mFreeSlots.insert(slot);
627 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 00:27:44628 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 21:21:02629 VALIDATE_CONSISTENCY();
630 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 23:14:33631 }
632
Yi Kong48a619f2018-06-05 23:34:59633 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 21:21:02634 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-04 01:23:52635 }
Pablo Ceballos981066c2016-02-18 20:54:37636
Dan Stoza9f3053d2014-03-06 23:14:33637 return NO_ERROR;
638}
639
Dan Stozad9822a32014-03-28 22:25:31640status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
641 sp<Fence>* outFence) {
642 ATRACE_CALL();
643
Yi Kong48a619f2018-06-05 23:34:59644 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 22:25:31645 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
646 return BAD_VALUE;
Yi Kong48a619f2018-06-05 23:34:59647 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 22:25:31648 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
649 return BAD_VALUE;
650 }
651
Eino-Ville Talvala2672dec2017-06-13 23:39:11652 sp<IConsumerListener> listener;
653 {
Jorim Jaggi6ae55032019-04-02 00:27:44654 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 22:25:31655
Eino-Ville Talvala2672dec2017-06-13 23:39:11656 if (mCore->mIsAbandoned) {
657 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
658 return NO_INIT;
659 }
660
661 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
662 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
663 return NO_INIT;
664 }
665
666 if (mCore->mSharedBufferMode) {
667 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
668 "mode");
669 return BAD_VALUE;
670 }
671
Jorim Jaggi6ae55032019-04-02 00:27:44672 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 23:39:11673
674 if (mCore->mFreeBuffers.empty()) {
675 return NO_MEMORY;
676 }
677
678 int found = mCore->mFreeBuffers.front();
679 mCore->mFreeBuffers.remove(found);
680 mCore->mFreeSlots.insert(found);
681
682 BQ_LOGV("detachNextBuffer detached slot %d", found);
683
684 *outBuffer = mSlots[found].mGraphicBuffer;
685 *outFence = mSlots[found].mFence;
686 mCore->clearBufferSlotLocked(found);
687 VALIDATE_CONSISTENCY();
688 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 22:25:31689 }
690
Yi Kong48a619f2018-06-05 23:34:59691 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 23:39:11692 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-04 01:23:52693 }
Pablo Ceballos981066c2016-02-18 20:54:37694
Dan Stozad9822a32014-03-28 22:25:31695 return NO_ERROR;
696}
697
Dan Stoza9f3053d2014-03-06 23:14:33698status_t BufferQueueProducer::attachBuffer(int* outSlot,
699 const sp<android::GraphicBuffer>& buffer) {
700 ATRACE_CALL();
701
Yi Kong48a619f2018-06-05 23:34:59702 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 22:05:45703 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 23:14:33704 return BAD_VALUE;
Yi Kong48a619f2018-06-05 23:34:59705 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 22:05:45706 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 23:14:33707 return BAD_VALUE;
708 }
709
Jorim Jaggi6ae55032019-04-02 00:27:44710 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-04 01:23:52711
712 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 22:05:45713 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-04 01:23:52714 return NO_INIT;
715 }
716
717 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 22:05:45718 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-04 01:23:52719 return NO_INIT;
720 }
Dan Stoza9f3053d2014-03-06 23:14:33721
Pablo Ceballos3559fbf2016-03-17 22:50:23722 if (mCore->mSharedBufferMode) {
723 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 22:05:45724 return BAD_VALUE;
725 }
726
Dan Stoza812ed062015-06-02 22:45:22727 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
728 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
729 "[queue %u]", buffer->getGenerationNumber(),
730 mCore->mGenerationNumber);
731 return BAD_VALUE;
732 }
733
Jorim Jaggi6ae55032019-04-02 00:27:44734 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-04 01:23:52735
Dan Stoza9f3053d2014-03-06 23:14:33736 status_t returnFlags = NO_ERROR;
737 int found;
Jorim Jaggi6ae55032019-04-02 00:27:44738 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 23:14:33739 if (status != NO_ERROR) {
740 return status;
741 }
742
743 // This should not happen
744 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 22:05:45745 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 23:14:33746 return -EBUSY;
747 }
748
749 *outSlot = found;
750 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 22:05:45751 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 23:14:33752 *outSlot, returnFlags);
753
754 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 22:05:45755 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 23:14:33756 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
757 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 22:03:46758 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 20:15:22759 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-23 00:41:38760 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 20:15:22761 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 21:49:19762 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 20:20:51763
Dan Stoza9f3053d2014-03-06 23:14:33764 return returnFlags;
765}
766
Dan Stoza289ade12014-02-28 19:17:17767status_t BufferQueueProducer::queueBuffer(int slot,
768 const QueueBufferInput &input, QueueBufferOutput *output) {
769 ATRACE_CALL();
770 ATRACE_BUFFER_INDEX(slot);
771
Brian Andersond6927fb2016-07-24 06:37:30772 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 19:17:17773 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43774 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 21:47:20775 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 19:17:17776 int scalingMode;
777 uint32_t transform;
Ruben Brunk1681d952014-06-27 22:51:55778 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-24 06:37:30779 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 19:48:08780 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-24 06:37:30781 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 19:48:08782 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
783 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 22:48:25784 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 19:34:34785 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 19:17:17786
Yi Kong48a619f2018-06-05 23:34:59787 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 19:17:17788 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 16:30:48789 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17790 }
791
Brian Anderson3d4039d2016-09-23 23:31:30792 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
793
Dan Stoza289ade12014-02-28 19:17:17794 switch (scalingMode) {
795 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
796 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
797 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
798 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
799 break;
800 default:
801 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 23:14:33802 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17803 }
804
Dan Stoza8dc55392014-11-04 19:37:46805 sp<IConsumerListener> frameAvailableListener;
806 sp<IConsumerListener> frameReplacedListener;
807 int callbackTicket = 0;
Brian Andersond6927fb2016-07-24 06:37:30808 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 19:37:46809 BufferItem item;
Dan Stoza289ade12014-02-28 19:17:17810 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:44811 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:17812
813 if (mCore->mIsAbandoned) {
814 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
815 return NO_INIT;
816 }
817
Pablo Ceballos583b1b32015-09-04 01:23:52818 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
819 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
820 return NO_INIT;
821 }
822
Pablo Ceballos23b4abe2016-01-08 20:15:22823 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:17824 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 20:15:22825 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 23:14:33826 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:45827 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:17828 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:45829 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 23:14:33830 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17831 } else if (!mSlots[slot].mRequestBufferCalled) {
832 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
833 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 23:14:33834 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17835 }
836
Pablo Ceballos3559fbf2016-03-17 22:50:23837 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 23:02:19838 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 22:50:23839 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 23:02:19840 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 22:50:23841 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 23:02:19842 mSlots[slot].mBufferState.mShared = true;
843 }
844
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43845 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 19:34:34846 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
847 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
848 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-24 06:37:30849 transform,
Dan Stoza3be1c6b2014-11-18 18:24:03850 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 19:17:17851
852 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
853 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 21:47:20854 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 19:17:17855 crop.intersect(bufferRect, &croppedRect);
856 if (croppedRect != crop) {
857 BQ_LOGE("queueBuffer: crop rect is not contained within the "
858 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 23:14:33859 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17860 }
861
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43862 // Override UNKNOWN dataspace with consumer default
863 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
864 dataSpace = mCore->mDefaultBufferDataSpace;
865 }
866
Brian Andersond6927fb2016-07-24 06:37:30867 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 22:05:45868 mSlots[slot].mBufferState.queue();
869
Brian Andersond6927fb2016-07-24 06:37:30870 // Increment the frame counter and store a local version of it
871 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 19:17:17872 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-24 06:37:30873 currentFrameNumber = mCore->mFrameCounter;
874 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 19:17:17875
Dan Stoza289ade12014-02-28 19:17:17876 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
877 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
878 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 18:24:03879 item.mTransform = transform &
880 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 19:17:17881 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 18:24:03882 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
883 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-24 06:37:30884 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 19:17:17885 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43886 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 19:34:34887 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-24 06:37:30888 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 19:17:17889 item.mSlot = slot;
Brian Andersond6927fb2016-07-24 06:37:30890 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 23:31:30891 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-27 01:59:08892 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 20:38:23893 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-03 00:40:47894 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 22:50:23895 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 23:23:42896 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 23:32:12897 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 22:50:23898 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 16:54:38899 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 19:17:17900
Ruben Brunk1681d952014-06-27 22:51:55901 mStickyTransform = stickyTransform;
902
Pablo Ceballosccdfd602015-10-07 22:05:45903 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 22:50:23904 if (mCore->mSharedBufferMode) {
905 mCore->mSharedBufferCache.crop = crop;
906 mCore->mSharedBufferCache.transform = transform;
907 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 22:05:45908 scalingMode);
Pablo Ceballos3559fbf2016-03-17 22:50:23909 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 22:05:45910 }
911
Shuzhen Wang22f842b2017-01-19 07:02:36912 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 19:17:17913 if (mCore->mQueue.empty()) {
914 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
915 // and simply queue this buffer
916 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 19:37:46917 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17918 } else {
Pablo Ceballos4d85da42016-04-20 03:11:56919 // When the queue is not empty, we need to look at the last buffer
920 // in the queue to see if we need to replace it
921 const BufferItem& last = mCore->mQueue.itemAt(
922 mCore->mQueue.size() - 1);
923 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 20:15:22924
Pablo Ceballos4d85da42016-04-20 03:11:56925 if (!last.mIsStale) {
926 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 22:05:45927
Pablo Ceballos3559fbf2016-03-17 22:50:23928 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 22:05:45929 // still be around. Mark it as no longer shared if this
930 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 22:50:23931 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-20 03:11:56932 mSlots[last.mSlot].mBufferState.isFree()) {
933 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 22:05:45934 }
935 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-20 03:11:56936 if (!mSlots[last.mSlot].mBufferState.isShared()) {
937 mCore->mActiveBuffers.erase(last.mSlot);
938 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-19 07:02:36939 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 22:05:45940 }
Dan Stoza289ade12014-02-28 19:17:17941 }
Pablo Ceballos23b4abe2016-01-08 20:15:22942
Dan Stoza289ade12014-02-28 19:17:17943 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-20 03:11:56944 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 19:37:46945 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17946 } else {
947 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 19:37:46948 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17949 }
950 }
951
952 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 00:27:44953 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 23:53:23954 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 19:17:17955
Brian Anderson7c3ba8a2016-07-25 19:48:08956 output->width = mCore->mDefaultWidth;
957 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-25 02:35:03958 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 19:48:08959 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
960 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 19:17:17961
Colin Cross152c3b72016-09-27 21:08:19962 ATRACE_INT(mCore->mConsumerName.string(),
963 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 18:37:28964 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 19:37:46965
966 // Take a ticket for the callback functions
967 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 20:20:51968
Pablo Ceballos9e314332016-01-12 21:49:19969 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:17970 } // Autolock scope
971
Irvel468051e2016-06-13 23:44:44972 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
973 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
974 // there will be no Binder call
975 if (!mConsumerIsSurfaceFlinger) {
976 item.mGraphicBuffer.clear();
977 }
978
Dan Stoza8dc55392014-11-04 19:37:46979 // Call back without the main BufferQueue lock held, but with the callback
980 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-04 00:14:31981
982 int connectedApi;
983 sp<Fence> lastQueuedFence;
984
985 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 00:27:44986 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 19:37:46987 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 00:27:44988 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 19:37:46989 }
990
Yi Kong48a619f2018-06-05 23:34:59991 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 19:37:46992 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 23:34:59993 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 19:37:46994 frameReplacedListener->onFrameReplaced(item);
995 }
996
Mathias Agopian4f6ce7c2017-04-04 00:14:31997 connectedApi = mCore->mConnectedApi;
998 lastQueuedFence = std::move(mLastQueueBufferFence);
999
1000 mLastQueueBufferFence = std::move(acquireFence);
1001 mLastQueuedCrop = item.mCrop;
1002 mLastQueuedTransform = item.mTransform;
1003
Dan Stoza8dc55392014-11-04 19:37:461004 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 00:27:441005 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 19:17:171006 }
1007
Brian Andersond6927fb2016-07-24 06:37:301008 // Update and get FrameEventHistory.
1009 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1010 NewFrameEventsEntry newFrameEventsEntry = {
1011 currentFrameNumber,
1012 postedTime,
1013 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 23:31:301014 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-24 06:37:301015 };
Brian Anderson7c3ba8a2016-07-25 19:48:081016 addAndGetFrameTimestamps(&newFrameEventsEntry,
1017 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-24 06:37:301018
Yiwei Zhangcb7dd002019-04-16 18:03:011019 // Wait without lock held
1020 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1021 // Waiting here allows for two full buffers to be queued but not a
1022 // third. In the event that frames take varying time, this makes a
1023 // small trade-off in favor of latency rather than throughput.
1024 lastQueuedFence->waitForever("Throttling EGL Production");
1025 }
1026
Dan Stoza289ade12014-02-28 19:17:171027 return NO_ERROR;
1028}
1029
Pablo Ceballos583b1b32015-09-04 01:23:521030status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 19:17:171031 ATRACE_CALL();
1032 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 00:27:441033 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:171034
1035 if (mCore->mIsAbandoned) {
1036 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-04 01:23:521037 return NO_INIT;
1038 }
1039
1040 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1041 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1042 return NO_INIT;
Dan Stoza289ade12014-02-28 19:17:171043 }
1044
Pablo Ceballos3559fbf2016-03-17 22:50:231045 if (mCore->mSharedBufferMode) {
1046 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 22:05:451047 return BAD_VALUE;
1048 }
1049
Dan Stoza3e96f192014-03-03 18:16:191050 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:171051 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 18:16:191052 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-04 01:23:521053 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:451054 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:171055 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:451056 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-04 01:23:521057 return BAD_VALUE;
Yi Kong48a619f2018-06-05 23:34:591058 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 19:17:171059 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-04 01:23:521060 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:171061 }
1062
Pablo Ceballosccdfd602015-10-07 22:05:451063 mSlots[slot].mBufferState.cancel();
1064
Pablo Ceballos3559fbf2016-03-17 22:50:231065 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 22:05:451066 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 22:50:231067 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 22:05:451068 mSlots[slot].mBufferState.mShared = false;
1069 }
1070
1071 // Don't put the shared buffer on the free list.
1072 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 20:15:221073 mCore->mActiveBuffers.erase(slot);
1074 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 22:05:451075 }
Pablo Ceballos23b4abe2016-01-08 20:15:221076
Dan Stoza289ade12014-02-28 19:17:171077 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 00:27:441078 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 21:49:191079 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-04 01:23:521080
1081 return NO_ERROR;
Dan Stoza289ade12014-02-28 19:17:171082}
1083
1084int BufferQueueProducer::query(int what, int *outValue) {
1085 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 00:27:441086 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 19:17:171087
Yi Kong48a619f2018-06-05 23:34:591088 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 19:17:171089 BQ_LOGE("query: outValue was NULL");
1090 return BAD_VALUE;
1091 }
1092
1093 if (mCore->mIsAbandoned) {
1094 BQ_LOGE("query: BufferQueue has been abandoned");
1095 return NO_INIT;
1096 }
1097
1098 int value;
1099 switch (what) {
1100 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 18:24:031101 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 19:17:171102 break;
1103 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 18:24:031104 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 19:17:171105 break;
1106 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 18:24:031107 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 19:17:171108 break;
Craig Donner6ebc46a2016-10-21 22:23:441109 case NATIVE_WINDOW_LAYER_COUNT:
1110 // All BufferQueue buffers have a single layer.
1111 value = BQ_LAYER_COUNT;
1112 break;
Dan Stoza289ade12014-02-28 19:17:171113 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-27 01:59:081114 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 19:17:171115 break;
Ruben Brunk1681d952014-06-27 22:51:551116 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 18:24:031117 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 22:51:551118 break;
Dan Stoza289ade12014-02-28 19:17:171119 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1120 value = (mCore->mQueue.size() > 1);
1121 break;
1122 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 17:36:081123 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 18:24:031124 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 19:17:171125 break;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:431126 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1127 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1128 break;
Dan Stoza4afd8b62015-02-26 00:49:081129 case NATIVE_WINDOW_BUFFER_AGE:
1130 if (mCore->mBufferAge > INT32_MAX) {
1131 value = 0;
1132 } else {
1133 value = static_cast<int32_t>(mCore->mBufferAge);
1134 }
1135 break;
Jiwen 'Steve' Cai20419132017-04-22 01:49:531136 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1137 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1138 break;
Yiwei Zhangdbd96152018-02-08 22:22:531139 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1140 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1141 break;
Dan Stoza289ade12014-02-28 19:17:171142 default:
1143 return BAD_VALUE;
1144 }
1145
1146 BQ_LOGV("query: %d? %d", what, value);
1147 *outValue = value;
1148 return NO_ERROR;
1149}
1150
Dan Stozaf0eaf252014-03-21 20:05:511151status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 19:17:171152 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1153 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 00:27:441154 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 20:54:371155 mConsumerName = mCore->mConsumerName;
1156 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1157 producerControlledByApp ? "true" : "false");
1158
1159 if (mCore->mIsAbandoned) {
1160 BQ_LOGE("connect: BufferQueue has been abandoned");
1161 return NO_INIT;
1162 }
1163
Yi Kong48a619f2018-06-05 23:34:591164 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 20:54:371165 BQ_LOGE("connect: BufferQueue has no consumer");
1166 return NO_INIT;
1167 }
1168
Yi Kong48a619f2018-06-05 23:34:591169 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 20:54:371170 BQ_LOGE("connect: output was NULL");
1171 return BAD_VALUE;
1172 }
1173
1174 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1175 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1176 mCore->mConnectedApi, api);
1177 return BAD_VALUE;
1178 }
1179
1180 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1181 mDequeueTimeout < 0 ?
1182 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1183 mCore->mMaxBufferCount) -
1184 mCore->getMaxBufferCountLocked();
1185 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1186 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1187 "slots. Delta = %d", delta);
1188 return BAD_VALUE;
1189 }
1190
Dan Stoza289ade12014-02-28 19:17:171191 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 20:54:371192 switch (api) {
1193 case NATIVE_WINDOW_API_EGL:
1194 case NATIVE_WINDOW_API_CPU:
1195 case NATIVE_WINDOW_API_MEDIA:
1196 case NATIVE_WINDOW_API_CAMERA:
1197 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 19:48:081198
1199 output->width = mCore->mDefaultWidth;
1200 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-25 02:35:031201 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 19:48:081202 output->numPendingBuffers =
1203 static_cast<uint32_t>(mCore->mQueue.size());
1204 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-19 07:02:361205 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 19:17:171206
Yi Kong48a619f2018-06-05 23:34:591207 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 23:24:261208 // Set up a death notification so that we can disconnect
1209 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 23:34:591210 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 23:24:261211 status = IInterface::asBinder(listener)->linkToDeath(
1212 static_cast<IBinder::DeathRecipient*>(this));
1213 if (status != NO_ERROR) {
1214 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1215 strerror(-status), status);
1216 }
1217 mCore->mLinkedToDeath = listener;
1218 }
1219 if (listener->needsReleaseNotify()) {
1220 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 19:17:171221 }
Pablo Ceballos981066c2016-02-18 20:54:371222 }
Pablo Ceballos981066c2016-02-18 20:54:371223 break;
1224 default:
1225 BQ_LOGE("connect: unknown API %d", api);
1226 status = BAD_VALUE;
1227 break;
Dan Stoza289ade12014-02-28 19:17:171228 }
Jayant Chowdharyad9fe272019-03-08 06:36:061229 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 20:54:371230 mCore->mBufferHasBeenQueued = false;
1231 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-03 00:40:471232 mCore->mQueueBufferCanDrop = false;
1233 mCore->mLegacyBufferDrop = true;
1234 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1235 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1236 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 20:43:321237 }
Dan Stoza289ade12014-02-28 19:17:171238
Pablo Ceballos981066c2016-02-18 20:54:371239 mCore->mAllowAllocation = true;
1240 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:171241 return status;
1242}
1243
Robert Carr97b9c862016-09-08 20:54:351244status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 19:17:171245 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 22:05:451246 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 19:17:171247
1248 int status = NO_ERROR;
1249 sp<IConsumerListener> listener;
1250 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:441251 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 20:54:351252
1253 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-08 06:36:061254 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 20:54:351255 return NO_ERROR;
1256 }
1257 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1258 }
1259
Jorim Jaggi6ae55032019-04-02 00:27:441260 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 19:17:171261
1262 if (mCore->mIsAbandoned) {
1263 // It's not really an error to disconnect after the surface has
1264 // been abandoned; it should just be a no-op.
1265 return NO_ERROR;
1266 }
1267
Chong Zhang1b3a9ac2016-03-01 00:47:471268 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 23:52:331269 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1270 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1271 }
Chong Zhang1b3a9ac2016-03-01 00:47:471272 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 20:08:331273 // If we're asked to disconnect the currently connected api but
1274 // nobody is connected, it's not really an error.
1275 if (api == BufferQueueCore::NO_CONNECTED_API) {
1276 return NO_ERROR;
1277 }
Chong Zhang1b3a9ac2016-03-01 00:47:471278 }
1279
Dan Stoza289ade12014-02-28 19:17:171280 switch (api) {
1281 case NATIVE_WINDOW_API_EGL:
1282 case NATIVE_WINDOW_API_CPU:
1283 case NATIVE_WINDOW_API_MEDIA:
1284 case NATIVE_WINDOW_API_CAMERA:
1285 if (mCore->mConnectedApi == api) {
1286 mCore->freeAllBuffersLocked();
1287
1288 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 23:34:591289 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 20:05:511290 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 23:24:261291 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 19:17:171292 // This can fail if we're here because of the death
1293 // notification, but we just ignore it
1294 token->unlinkToDeath(
1295 static_cast<IBinder::DeathRecipient*>(this));
1296 }
Pablo Ceballos3559fbf2016-03-17 22:50:231297 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 20:15:221298 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 23:34:591299 mCore->mLinkedToDeath = nullptr;
1300 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 19:17:171301 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 20:54:351302 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 23:42:541303 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 00:27:441304 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-25 02:35:031305 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 19:17:171306 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 22:43:161307 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1308 BQ_LOGE("disconnect: not connected (req=%d)", api);
1309 status = NO_INIT;
1310 } else {
Pablo Ceballosccdfd602015-10-07 22:05:451311 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 19:17:171312 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 23:14:331313 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:171314 }
1315 break;
1316 default:
Pablo Ceballosccdfd602015-10-07 22:05:451317 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 23:14:331318 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:171319 break;
1320 }
1321 } // Autolock scope
1322
1323 // Call back without lock held
Yi Kong48a619f2018-06-05 23:34:591324 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 19:17:171325 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-02 00:54:331326 listener->onDisconnect();
Dan Stoza289ade12014-02-28 19:17:171327 }
1328
1329 return status;
1330}
1331
Jesse Hall399184a2014-03-03 23:42:541332status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 14:16:081333 sp<IConsumerListener> listener;
1334 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:441335 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 14:16:081336 mCore->mSidebandStream = stream;
1337 listener = mCore->mConsumerListener;
1338 } // Autolock scope
1339
Yi Kong48a619f2018-06-05 23:34:591340 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 14:16:081341 listener->onSidebandStreamChanged();
1342 }
Jesse Hall399184a2014-03-03 23:42:541343 return NO_ERROR;
1344}
1345
Pablo Ceballos567dbbb2015-08-27 01:59:081346void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 21:21:001347 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-16 04:17:031348 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-25 02:35:031349
1350 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-16 04:17:031351 while (true) {
Antoine Labour78014f32014-07-16 04:17:031352 size_t newBufferCount = 0;
1353 uint32_t allocWidth = 0;
1354 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 18:24:031355 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 21:21:001356 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 22:40:081357 std::string allocName;
Antoine Labour78014f32014-07-16 04:17:031358 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:441359 std::unique_lock<std::mutex> lock(mCore->mMutex);
1360 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 20:13:571361
Dan Stoza9de72932015-04-17 00:28:431362 if (!mCore->mAllowAllocation) {
1363 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1364 "BufferQueue");
1365 return;
1366 }
1367
Jorim Jaggi0a3e7842018-07-17 11:48:331368 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1369 // both allocateBuffers and dequeueBuffer.
1370 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 20:15:221371 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-16 04:17:031372 return;
1373 }
Pablo Ceballos23b4abe2016-01-08 20:15:221374
Antoine Labour78014f32014-07-16 04:17:031375 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1376 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-25 02:35:031377 if (useDefaultSize && mCore->mAutoPrerotation &&
1378 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1379 std::swap(allocWidth, allocHeight);
1380 }
1381
Antoine Labour78014f32014-07-16 04:17:031382 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1383 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 22:40:081384 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-16 04:17:031385
1386 mCore->mIsAllocating = true;
1387 } // Autolock scope
1388
Dan Stoza3be1c6b2014-11-18 18:24:031389 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 11:48:331390 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 22:49:321391 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 22:23:441392 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 22:40:081393 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 22:49:321394
1395 status_t result = graphicBuffer->initCheck();
1396
Antoine Labour78014f32014-07-16 04:17:031397 if (result != NO_ERROR) {
1398 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 21:21:001399 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 00:27:441400 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-16 04:17:031401 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 00:27:441402 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-16 04:17:031403 return;
1404 }
1405 buffers.push_back(graphicBuffer);
1406 }
1407
1408 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 00:27:441409 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-16 04:17:031410 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1411 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-25 02:35:031412 if (useDefaultSize && mCore->mAutoPrerotation &&
1413 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1414 std::swap(checkWidth, checkHeight);
1415 }
1416
Dan Stoza3be1c6b2014-11-18 18:24:031417 PixelFormat checkFormat = format != 0 ?
1418 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 21:21:001419 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-16 04:17:031420 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1421 checkFormat != allocFormat || checkUsage != allocUsage) {
1422 // Something changed while we released the lock. Retry.
1423 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1424 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 00:27:441425 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 20:13:571426 continue;
1427 }
1428
Antoine Labour78014f32014-07-16 04:17:031429 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 20:15:221430 if (mCore->mFreeSlots.empty()) {
1431 BQ_LOGV("allocateBuffers: a slot was occupied while "
1432 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-16 04:17:031433 continue;
1434 }
Pablo Ceballos23b4abe2016-01-08 20:15:221435 auto slot = mCore->mFreeSlots.begin();
1436 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1437 mSlots[*slot].mGraphicBuffer = buffers[i];
1438 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 20:20:511439
1440 // freeBufferLocked puts this slot on the free slots list. Since
1441 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 20:15:221442 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 20:20:511443
Pablo Ceballos23b4abe2016-01-08 20:15:221444 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1445 *slot);
Christopher Ferris87e94cd2016-04-26 18:29:081446
1447 // Make sure the erase is done after all uses of the slot
1448 // iterator since it will be invalid after this point.
1449 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-16 04:17:031450 }
Dan Stoza29a3e902014-06-20 20:13:571451
Antoine Labour78014f32014-07-16 04:17:031452 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 00:27:441453 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 21:49:191454 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-27 23:44:031455
1456 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1457 // waiting anymore so it can use the buffer we just allocated.
1458 while (mDequeueWaitingForAllocation) {
1459 mDequeueWaitingForAllocationCondition.wait(lock);
1460 }
Antoine Labour78014f32014-07-16 04:17:031461 } // Autolock scope
Dan Stoza29a3e902014-06-20 20:13:571462 }
1463}
1464
Dan Stoza9de72932015-04-17 00:28:431465status_t BufferQueueProducer::allowAllocation(bool allow) {
1466 ATRACE_CALL();
1467 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1468
Jorim Jaggi6ae55032019-04-02 00:27:441469 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-17 00:28:431470 mCore->mAllowAllocation = allow;
1471 return NO_ERROR;
1472}
1473
Dan Stoza812ed062015-06-02 22:45:221474status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1475 ATRACE_CALL();
1476 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1477
Jorim Jaggi6ae55032019-04-02 00:27:441478 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 22:45:221479 mCore->mGenerationNumber = generationNumber;
1480 return NO_ERROR;
1481}
1482
Dan Stozac6f30bd2015-06-08 16:32:501483String8 BufferQueueProducer::getConsumerName() const {
1484 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 00:27:441485 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 16:32:501486 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1487 return mConsumerName;
1488}
1489
Pablo Ceballos3559fbf2016-03-17 22:50:231490status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 22:05:451491 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 22:50:231492 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 22:05:451493
Jorim Jaggi6ae55032019-04-02 00:27:441494 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 22:50:231495 if (!sharedBufferMode) {
1496 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 22:05:451497 }
Pablo Ceballos3559fbf2016-03-17 22:50:231498 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 20:43:321499 return NO_ERROR;
1500}
Pablo Ceballosccdfd602015-10-07 22:05:451501
Pablo Ceballosff95aab2016-01-14 01:09:581502status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1503 ATRACE_CALL();
1504 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1505
Jorim Jaggi6ae55032019-04-02 00:27:441506 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-14 01:09:581507
1508 mCore->mAutoRefresh = autoRefresh;
1509 return NO_ERROR;
1510}
1511
Dan Stoza127fc632015-06-30 20:43:321512status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1513 ATRACE_CALL();
1514 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1515
Jorim Jaggi6ae55032019-04-02 00:27:441516 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 21:27:141517 bool dequeueBufferCannotBlock =
1518 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1519 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 20:54:371520 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1521 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1522 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1523 "available slots. Delta = %d", delta);
1524 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 20:15:221525 }
1526
Pablo Ceballos981066c2016-02-18 20:54:371527 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 21:27:141528 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1529 if (timeout > 0) {
1530 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-03 00:40:471531 }
Pablo Ceballos9e314332016-01-12 21:49:191532
Pablo Ceballos981066c2016-02-18 20:54:371533 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 22:05:451534 return NO_ERROR;
1535}
1536
Sungtak Lee3249fb62019-03-03 00:40:471537status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1538 ATRACE_CALL();
1539 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1540
1541 std::lock_guard<std::mutex> lock(mCore->mMutex);
1542 mCore->mLegacyBufferDrop = drop;
1543 return NO_ERROR;
1544}
1545
Dan Stoza50101d02016-04-07 23:53:231546status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 20:18:151547 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 23:53:231548 ATRACE_CALL();
1549 BQ_LOGV("getLastQueuedBuffer");
1550
Jorim Jaggi6ae55032019-04-02 00:27:441551 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 23:53:231552 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1553 *outBuffer = nullptr;
1554 *outFence = Fence::NO_FENCE;
1555 return NO_ERROR;
1556 }
1557
1558 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1559 *outFence = mLastQueueBufferFence;
1560
John Reck1a61da52016-04-28 20:18:151561 // Currently only SurfaceFlinger internally ever changes
1562 // GLConsumer's filtering mode, so we just use 'true' here as
1563 // this is slightly specialized for the current client of this API,
1564 // which does want filtering.
1565 GLConsumer::computeTransformMatrix(outTransformMatrix,
1566 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1567 mLastQueuedTransform, true /* filter */);
1568
Dan Stoza50101d02016-04-07 23:53:231569 return NO_ERROR;
1570}
1571
Brian Anderson3890c392016-07-25 19:48:081572void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1573 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-24 06:37:301574}
Pablo Ceballosce796e72016-02-05 03:10:511575
Brian Anderson3890c392016-07-25 19:48:081576void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-24 06:37:301577 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 19:48:081578 FrameEventHistoryDelta* outDelta) {
1579 if (newTimestamps == nullptr && outDelta == nullptr) {
1580 return;
Brian Andersond6927fb2016-07-24 06:37:301581 }
1582
1583 ATRACE_CALL();
1584 BQ_LOGV("addAndGetFrameTimestamps");
1585 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-05 03:10:511586 {
Jorim Jaggi6ae55032019-04-02 00:27:441587 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-05 03:10:511588 listener = mCore->mConsumerListener;
1589 }
Yi Kong48a619f2018-06-05 23:34:591590 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 19:48:081591 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-05 03:10:511592 }
Pablo Ceballosce796e72016-02-05 03:10:511593}
1594
Dan Stoza289ade12014-02-28 19:17:171595void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1596 // If we're here, it means that a producer we were connected to died.
1597 // We're guaranteed that we are still connected to it because we remove
1598 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1599 // without synchronization here.
1600 int api = mCore->mConnectedApi;
1601 disconnect(api);
1602}
1603
Pablo Ceballos8e3e92b2016-06-28 00:56:531604status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1605 BQ_LOGV("getUniqueId");
1606
1607 *outId = mCore->mUniqueId;
1608 return NO_ERROR;
1609}
1610
Chia-I Wue2786ea2017-08-07 17:36:081611status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1612 BQ_LOGV("getConsumerUsage");
1613
Jorim Jaggi6ae55032019-04-02 00:27:441614 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 17:36:081615 *outUsage = mCore->mConsumerUsageBits;
1616 return NO_ERROR;
1617}
1618
Yiwei Zhang538cedc2019-06-25 02:35:031619status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1620 ATRACE_CALL();
1621 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1622
1623 std::lock_guard<std::mutex> lock(mCore->mMutex);
1624
1625 mCore->mAutoPrerotation = autoPrerotation;
1626 return NO_ERROR;
1627}
1628
Dan Stoza289ade12014-02-28 19:17:171629} // namespace android