blob: e657488969c1bcc1a135afd7bfd9dfc67f578974 [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(),
62 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080063
64BufferQueueProducer::~BufferQueueProducer() {}
65
66status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
67 ATRACE_CALL();
68 BQ_LOGV("requestBuffer: slot %d", slot);
69 Mutex::Autolock lock(mCore->mMutex);
70
71 if (mCore->mIsAbandoned) {
72 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
73 return NO_INIT;
74 }
75
Pablo Ceballos583b1b32015-09-03 18:23:52 -070076 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
77 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
78 return NO_INIT;
79 }
80
Dan Stoza3e96f192014-03-03 10:16:19 -080081 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080082 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080083 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080084 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070085 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080086 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070087 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080088 return BAD_VALUE;
89 }
90
91 mSlots[slot].mRequestBufferCalled = true;
92 *buf = mSlots[slot].mGraphicBuffer;
93 return NO_ERROR;
94}
95
Pablo Ceballosfa455352015-08-12 17:47:47 -070096status_t BufferQueueProducer::setMaxDequeuedBufferCount(
97 int maxDequeuedBuffers) {
98 ATRACE_CALL();
99 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
100 maxDequeuedBuffers);
101
Pablo Ceballos981066c2016-02-18 12:54:37 -0800102 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700103 { // Autolock scope
104 Mutex::Autolock lock(mCore->mMutex);
105 mCore->waitWhileAllocatingLocked();
106
107 if (mCore->mIsAbandoned) {
108 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
109 "abandoned");
110 return NO_INIT;
111 }
112
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700113 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
114 return NO_ERROR;
115 }
116
Pablo Ceballos72daab62015-12-07 16:38:43 -0800117 // The new maxDequeuedBuffer count should not be violated by the number
118 // of currently dequeued buffers
119 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800120 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700121 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800122 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700123 }
124 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800125 if (dequeuedCount > maxDequeuedBuffers) {
126 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
127 "count (%d) exceeds the current dequeued buffer count (%d)",
128 maxDequeuedBuffers, dequeuedCount);
129 return BAD_VALUE;
130 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700132 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700133 bufferCount += maxDequeuedBuffers;
134
135 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
136 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
137 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
138 return BAD_VALUE;
139 }
140
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700141 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700142 if (bufferCount < minBufferSlots) {
143 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
144 "less than minimum %d", bufferCount, minBufferSlots);
145 return BAD_VALUE;
146 }
147
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700148 if (bufferCount > mCore->mMaxBufferCount) {
149 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700150 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
151 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
152 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
153 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700154 return BAD_VALUE;
155 }
156
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800158 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800159 return BAD_VALUE;
160 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700161 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800162 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800163 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800164 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800165 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700167 } // Autolock scope
168
169 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700170 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800171 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700172 }
173
174 return NO_ERROR;
175}
176
177status_t BufferQueueProducer::setAsyncMode(bool async) {
178 ATRACE_CALL();
179 BQ_LOGV("setAsyncMode: async = %d", async);
180
Pablo Ceballos981066c2016-02-18 12:54:37 -0800181 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700182 { // Autolock scope
183 Mutex::Autolock lock(mCore->mMutex);
184 mCore->waitWhileAllocatingLocked();
185
186 if (mCore->mIsAbandoned) {
187 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
188 return NO_INIT;
189 }
190
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700191 if (async == mCore->mAsyncMode) {
192 return NO_ERROR;
193 }
194
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700195 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700196 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
197 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700198 BQ_LOGE("setAsyncMode(%d): this call would cause the "
199 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700200 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
201 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
202 mCore->mMaxDequeuedBufferCount,
203 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700204 return BAD_VALUE;
205 }
206
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800207 int delta = mCore->getMaxBufferCountLocked(async,
208 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
209 - mCore->getMaxBufferCountLocked();
210
Pablo Ceballos981066c2016-02-18 12:54:37 -0800211 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800212 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
213 "available slots. Delta = %d", delta);
214 return BAD_VALUE;
215 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800217 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700219 if (delta < 0) {
220 listener = mCore->mConsumerListener;
221 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700222 } // Autolock scope
223
224 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700225 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800226 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700227 }
228 return NO_ERROR;
229}
230
Dan Stoza5ecfb682016-01-04 17:01:02 -0800231int BufferQueueProducer::getFreeBufferLocked() const {
232 if (mCore->mFreeBuffers.empty()) {
233 return BufferQueueCore::INVALID_BUFFER_SLOT;
234 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800236 mCore->mFreeBuffers.pop_front();
237 return slot;
238}
239
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800240int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800241 if (mCore->mFreeSlots.empty()) {
242 return BufferQueueCore::INVALID_BUFFER_SLOT;
243 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800244 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800245 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800246 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800247}
248
249status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800250 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800251 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
252 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800253 bool tryAgain = true;
254 while (tryAgain) {
255 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800256 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800257 return NO_INIT;
258 }
259
Dan Stoza9f3053d2014-03-06 15:14:33 -0800260 int dequeuedCount = 0;
261 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800262 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 if (mSlots[s].mBufferState.isDequeued()) {
264 ++dequeuedCount;
265 }
266 if (mSlots[s].mBufferState.isAcquired()) {
267 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800268 }
269 }
270
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700271 // Producers are not allowed to dequeue more than
272 // mMaxDequeuedBufferCount buffers.
273 // This check is only done if a buffer has already been queued
274 if (mCore->mBufferHasBeenQueued &&
275 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
276 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800277 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800278 return INVALID_OPERATION;
279 }
280
Dan Stoza0de7ea72015-04-23 13:20:51 -0700281 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
282
Dan Stozaae3c3682014-04-18 15:43:35 -0700283 // If we disconnect and reconnect quickly, we can be in a state where
284 // our slots are empty but we have many buffers in the queue. This can
285 // cause us to run out of memory if we outrun the consumer. Wait here if
286 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800287 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700288 bool tooManyBuffers = mCore->mQueue.size()
289 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700290 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800291 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700292 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700293 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700294 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700295 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700296 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700297 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700298 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800299 } else {
300 if (caller == FreeSlotCaller::Dequeue) {
301 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800303 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
304 *found = slot;
305 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800306 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800307 }
308 } else {
309 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800310 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800311 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
312 *found = slot;
313 } else {
314 *found = getFreeBufferLocked();
315 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700316 }
317 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700318 }
319
320 // If no buffer is found, or if the queue has too many buffers
321 // outstanding, wait for a buffer to be acquired or released, or for the
322 // max buffer count to change.
323 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
324 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 if (tryAgain) {
326 // Return an error if we're in non-blocking mode (producer and
327 // consumer are controlled by the application).
328 // However, the consumer is allowed to briefly acquire an extra
329 // buffer (which could cause us to have to wait here), which is
330 // okay, since it is only used to implement an atomic acquire +
331 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700332 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800333 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
334 return WOULD_BLOCK;
335 }
Dan Stoza127fc632015-06-30 13:43:32 -0700336 if (mDequeueTimeout >= 0) {
337 status_t result = mCore->mDequeueCondition.waitRelative(
338 mCore->mMutex, mDequeueTimeout);
339 if (result == TIMED_OUT) {
340 return result;
341 }
342 } else {
343 mCore->mDequeueCondition.wait(mCore->mMutex);
344 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800345 }
346 } // while (tryAgain)
347
348 return NO_ERROR;
349}
350
Ian Elliottd11b0442017-07-18 11:05:49 -0600351status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
352 uint32_t width, uint32_t height, PixelFormat format,
353 uint64_t usage, uint64_t* outBufferAge,
354 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800355 ATRACE_CALL();
356 { // Autolock scope
357 Mutex::Autolock lock(mCore->mMutex);
358 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700359
360 if (mCore->mIsAbandoned) {
361 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
362 return NO_INIT;
363 }
364
365 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
366 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
367 return NO_INIT;
368 }
Dan Stoza289ade12014-02-28 11:17:17 -0800369 } // Autolock scope
370
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700371 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800372
373 if ((width && !height) || (!width && height)) {
374 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
375 return BAD_VALUE;
376 }
377
378 status_t returnFlags = NO_ERROR;
379 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
380 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800382
383 { // Autolock scope
384 Mutex::Autolock lock(mCore->mMutex);
385
386 if (format == 0) {
387 format = mCore->mDefaultBufferFormat;
388 }
389
390 // Enable the usage bits the consumer requested
391 usage |= mCore->mConsumerUsageBits;
392
Dan Stoza9de72932015-04-16 17:28:43 -0700393 const bool useDefaultSize = !width && !height;
394 if (useDefaultSize) {
395 width = mCore->mDefaultWidth;
396 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800397 }
Dan Stoza289ade12014-02-28 11:17:17 -0800398
Pablo Ceballos981066c2016-02-18 12:54:37 -0800399 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700400 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800401 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800402 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700403 if (status != NO_ERROR) {
404 return status;
405 }
406
407 // This should not happen
408 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
409 BQ_LOGE("dequeueBuffer: no available buffer slots");
410 return -EBUSY;
411 }
412
413 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
414
415 // If we are not allowed to allocate new buffers,
416 // waitForFreeSlotThenRelock must have returned a slot containing a
417 // buffer. If this buffer would require reallocation to meet the
418 // requested attributes, we free it and attempt to get another one.
419 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700420 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700421 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700422 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 return BAD_VALUE;
424 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800425 mCore->mFreeSlots.insert(found);
426 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700427 found = BufferItem::INVALID_BUFFER_SLOT;
428 continue;
429 }
430 }
Dan Stoza289ade12014-02-28 11:17:17 -0800431 }
432
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800433 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700434 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700435 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800436 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
437 "buffer");
438
439 return BAD_VALUE;
440 }
441
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700442 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800443 mCore->mActiveBuffers.insert(found);
444 }
Dan Stoza289ade12014-02-28 11:17:17 -0800445 *outSlot = found;
446 ATRACE_BUFFER_INDEX(found);
447
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800448 attachedByConsumer = mSlots[found].mNeedsReallocation;
449 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800450
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700451 mSlots[found].mBufferState.dequeue();
452
Yi Kong48a619f2018-06-05 16:34:59 -0700453 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700454 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800455 {
456 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700457 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800458 mSlots[found].mRequestBufferCalled = false;
459 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
460 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
461 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800462 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700463 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800464
465 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800466 } else {
467 // We add 1 because that will be the frame number when this buffer
468 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700469 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800470 }
471
Dan Stoza800b41a2015-04-28 14:20:04 -0700472 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
473 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800474
Yi Kong48a619f2018-06-05 16:34:59 -0700475 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800476 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
477 "slot=%d w=%d h=%d format=%u",
478 found, buffer->width, buffer->height, buffer->format);
479 }
480
481 eglDisplay = mSlots[found].mEglDisplay;
482 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700483 // Don't return a fence in shared buffer mode, except for the first
484 // frame.
485 *outFence = (mCore->mSharedBufferMode &&
486 mCore->mSharedBufferSlot == found) ?
487 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800488 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
489 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700490
491 // If shared buffer mode has just been enabled, cache the slot of the
492 // first buffer that is dequeued and mark it as the shared buffer.
493 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
494 BufferQueueCore::INVALID_BUFFER_SLOT) {
495 mCore->mSharedBufferSlot = found;
496 mSlots[found].mBufferState.mShared = true;
497 }
Dan Stoza289ade12014-02-28 11:17:17 -0800498 } // Autolock scope
499
500 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700501 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700502 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700503 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700504 {mConsumerName.string(), mConsumerName.size()});
505
506 status_t error = graphicBuffer->initCheck();
507
Dan Stoza289ade12014-02-28 11:17:17 -0800508 { // Autolock scope
509 Mutex::Autolock lock(mCore->mMutex);
510
Chia-I Wufeec3b12017-05-25 09:34:56 -0700511 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700512 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
513 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
514 }
515
516 mCore->mIsAllocating = false;
517 mCore->mIsAllocatingCondition.broadcast();
518
Chia-I Wufeec3b12017-05-25 09:34:56 -0700519 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700520 mCore->mFreeSlots.insert(*outSlot);
521 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700522 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
523 return error;
524 }
525
Dan Stoza289ade12014-02-28 11:17:17 -0800526 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700527 mCore->mFreeSlots.insert(*outSlot);
528 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800529 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
530 return NO_INIT;
531 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800532
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700533 if (mCore->mConsumerListener != nullptr) {
534 BufferItem item;
535 item.mGraphicBuffer = graphicBuffer;
536 item.mSlot = *outSlot;
537 mCore->mConsumerListener->onBufferAllocated(item);
538 }
539
Pablo Ceballos9e314332016-01-12 13:49:19 -0800540 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800541 } // Autolock scope
542 }
543
Dan Stoza9f3053d2014-03-06 15:14:33 -0800544 if (attachedByConsumer) {
545 returnFlags |= BUFFER_NEEDS_REALLOCATION;
546 }
547
Dan Stoza289ade12014-02-28 11:17:17 -0800548 if (eglFence != EGL_NO_SYNC_KHR) {
549 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
550 1000000000);
551 // If something goes wrong, log the error, but return the buffer without
552 // synchronizing access to it. It's too late at this point to abort the
553 // dequeue operation.
554 if (result == EGL_FALSE) {
555 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
556 eglGetError());
557 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
558 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
559 }
560 eglDestroySyncKHR(eglDisplay, eglFence);
561 }
562
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700563 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
564 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800565 mSlots[*outSlot].mFrameNumber,
566 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
567
Ian Elliottd11b0442017-07-18 11:05:49 -0600568 if (outBufferAge) {
569 *outBufferAge = mCore->mBufferAge;
570 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700571 addAndGetFrameTimestamps(nullptr, outTimestamps);
572
Dan Stoza289ade12014-02-28 11:17:17 -0800573 return returnFlags;
574}
575
Dan Stoza9f3053d2014-03-06 15:14:33 -0800576status_t BufferQueueProducer::detachBuffer(int slot) {
577 ATRACE_CALL();
578 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700579 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800580
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700581 sp<IConsumerListener> listener;
582 {
583 Mutex::Autolock lock(mCore->mMutex);
584
585 if (mCore->mIsAbandoned) {
586 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
587 return NO_INIT;
588 }
589
590 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
591 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
592 return NO_INIT;
593 }
594
595 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
596 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
597 return BAD_VALUE;
598 }
599
600 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
601 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
602 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
603 return BAD_VALUE;
604 } else if (!mSlots[slot].mBufferState.isDequeued()) {
605 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
606 "(state = %s)", slot, mSlots[slot].mBufferState.string());
607 return BAD_VALUE;
608 } else if (!mSlots[slot].mRequestBufferCalled) {
609 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
610 slot);
611 return BAD_VALUE;
612 }
613
614 mSlots[slot].mBufferState.detachProducer();
615 mCore->mActiveBuffers.erase(slot);
616 mCore->mFreeSlots.insert(slot);
617 mCore->clearBufferSlotLocked(slot);
618 mCore->mDequeueCondition.broadcast();
619 VALIDATE_CONSISTENCY();
620 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800621 }
622
Yi Kong48a619f2018-06-05 16:34:59 -0700623 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700624 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700625 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800626
Dan Stoza9f3053d2014-03-06 15:14:33 -0800627 return NO_ERROR;
628}
629
Dan Stozad9822a32014-03-28 15:25:31 -0700630status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
631 sp<Fence>* outFence) {
632 ATRACE_CALL();
633
Yi Kong48a619f2018-06-05 16:34:59 -0700634 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700635 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
636 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700637 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700638 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
639 return BAD_VALUE;
640 }
641
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700642 sp<IConsumerListener> listener;
643 {
644 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700645
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700646 if (mCore->mIsAbandoned) {
647 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
648 return NO_INIT;
649 }
650
651 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
652 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
653 return NO_INIT;
654 }
655
656 if (mCore->mSharedBufferMode) {
657 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
658 "mode");
659 return BAD_VALUE;
660 }
661
662 mCore->waitWhileAllocatingLocked();
663
664 if (mCore->mFreeBuffers.empty()) {
665 return NO_MEMORY;
666 }
667
668 int found = mCore->mFreeBuffers.front();
669 mCore->mFreeBuffers.remove(found);
670 mCore->mFreeSlots.insert(found);
671
672 BQ_LOGV("detachNextBuffer detached slot %d", found);
673
674 *outBuffer = mSlots[found].mGraphicBuffer;
675 *outFence = mSlots[found].mFence;
676 mCore->clearBufferSlotLocked(found);
677 VALIDATE_CONSISTENCY();
678 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700679 }
680
Yi Kong48a619f2018-06-05 16:34:59 -0700681 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700682 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700683 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800684
Dan Stozad9822a32014-03-28 15:25:31 -0700685 return NO_ERROR;
686}
687
Dan Stoza9f3053d2014-03-06 15:14:33 -0800688status_t BufferQueueProducer::attachBuffer(int* outSlot,
689 const sp<android::GraphicBuffer>& buffer) {
690 ATRACE_CALL();
691
Yi Kong48a619f2018-06-05 16:34:59 -0700692 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700693 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800694 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700695 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700696 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697 return BAD_VALUE;
698 }
699
700 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700701
702 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700703 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700704 return NO_INIT;
705 }
706
707 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700708 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700709 return NO_INIT;
710 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800711
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700712 if (mCore->mSharedBufferMode) {
713 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700714 return BAD_VALUE;
715 }
716
Dan Stoza812ed062015-06-02 15:45:22 -0700717 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
718 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
719 "[queue %u]", buffer->getGenerationNumber(),
720 mCore->mGenerationNumber);
721 return BAD_VALUE;
722 }
723
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700724 mCore->waitWhileAllocatingLocked();
725
Dan Stoza9f3053d2014-03-06 15:14:33 -0800726 status_t returnFlags = NO_ERROR;
727 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800728 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800729 if (status != NO_ERROR) {
730 return status;
731 }
732
733 // This should not happen
734 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700735 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800736 return -EBUSY;
737 }
738
739 *outSlot = found;
740 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700741 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800742 *outSlot, returnFlags);
743
744 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700745 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800746 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
747 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700748 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800749 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800750 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800751 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800752 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700753
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 return returnFlags;
755}
756
Dan Stoza289ade12014-02-28 11:17:17 -0800757status_t BufferQueueProducer::queueBuffer(int slot,
758 const QueueBufferInput &input, QueueBufferOutput *output) {
759 ATRACE_CALL();
760 ATRACE_BUFFER_INDEX(slot);
761
Brian Andersond6927fb2016-07-23 23:37:30 -0700762 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800763 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800764 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700765 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800766 int scalingMode;
767 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700768 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700769 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700770 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700771 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700772 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
773 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700774 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700775 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800776
Yi Kong48a619f2018-06-05 16:34:59 -0700777 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800778 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800779 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800780 }
781
Brian Anderson3d4039d2016-09-23 16:31:30 -0700782 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
783
Dan Stoza289ade12014-02-28 11:17:17 -0800784 switch (scalingMode) {
785 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
786 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
787 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
788 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
789 break;
790 default:
791 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800792 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800793 }
794
Dan Stoza8dc55392014-11-04 11:37:46 -0800795 sp<IConsumerListener> frameAvailableListener;
796 sp<IConsumerListener> frameReplacedListener;
797 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700798 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800799 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800800 { // Autolock scope
801 Mutex::Autolock lock(mCore->mMutex);
802
803 if (mCore->mIsAbandoned) {
804 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
805 return NO_INIT;
806 }
807
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700808 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
809 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
810 return NO_INIT;
811 }
812
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800813 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800814 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800815 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700817 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800818 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700819 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800820 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800821 } else if (!mSlots[slot].mRequestBufferCalled) {
822 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
823 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800824 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800825 }
826
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700827 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700828 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700829 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700830 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700831 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700832 mSlots[slot].mBufferState.mShared = true;
833 }
834
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800835 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700836 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
837 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
838 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700839 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800840 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800841
842 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
843 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700844 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800845 crop.intersect(bufferRect, &croppedRect);
846 if (croppedRect != crop) {
847 BQ_LOGE("queueBuffer: crop rect is not contained within the "
848 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800849 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800850 }
851
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800852 // Override UNKNOWN dataspace with consumer default
853 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
854 dataSpace = mCore->mDefaultBufferDataSpace;
855 }
856
Brian Andersond6927fb2016-07-23 23:37:30 -0700857 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700858 mSlots[slot].mBufferState.queue();
859
Brian Andersond6927fb2016-07-23 23:37:30 -0700860 // Increment the frame counter and store a local version of it
861 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800862 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700863 currentFrameNumber = mCore->mFrameCounter;
864 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800865
Dan Stoza289ade12014-02-28 11:17:17 -0800866 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
867 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
868 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800869 item.mTransform = transform &
870 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800871 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800872 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
873 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700874 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800875 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800876 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700877 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700878 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800879 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700880 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700881 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700882 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700883 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700884 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700885 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700886 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700887 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800888 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800889
Ruben Brunk1681d952014-06-27 15:51:55 -0700890 mStickyTransform = stickyTransform;
891
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700892 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700893 if (mCore->mSharedBufferMode) {
894 mCore->mSharedBufferCache.crop = crop;
895 mCore->mSharedBufferCache.transform = transform;
896 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700897 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700898 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700899 }
900
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800901 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800902 if (mCore->mQueue.empty()) {
903 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
904 // and simply queue this buffer
905 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800906 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800907 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700908 // When the queue is not empty, we need to look at the last buffer
909 // in the queue to see if we need to replace it
910 const BufferItem& last = mCore->mQueue.itemAt(
911 mCore->mQueue.size() - 1);
912 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800913
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700914 if (!last.mIsStale) {
915 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700916
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700917 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700918 // still be around. Mark it as no longer shared if this
919 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700920 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700921 mSlots[last.mSlot].mBufferState.isFree()) {
922 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700923 }
924 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700925 if (!mSlots[last.mSlot].mBufferState.isShared()) {
926 mCore->mActiveBuffers.erase(last.mSlot);
927 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800928 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700929 }
Dan Stoza289ade12014-02-28 11:17:17 -0800930 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800931
Dan Stoza289ade12014-02-28 11:17:17 -0800932 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700933 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800934 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800935 } else {
936 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800937 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 }
939 }
940
941 mCore->mBufferHasBeenQueued = true;
942 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700943 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800944
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700945 output->width = mCore->mDefaultWidth;
946 output->height = mCore->mDefaultHeight;
947 output->transformHint = mCore->mTransformHint;
948 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
949 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800950
Colin Cross152c3b72016-09-27 14:08:19 -0700951 ATRACE_INT(mCore->mConsumerName.string(),
952 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700953 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800954
955 // Take a ticket for the callback functions
956 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700957
Pablo Ceballos9e314332016-01-12 13:49:19 -0800958 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800959 } // Autolock scope
960
Irvel468051e2016-06-13 16:44:44 -0700961 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
962 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
963 // there will be no Binder call
964 if (!mConsumerIsSurfaceFlinger) {
965 item.mGraphicBuffer.clear();
966 }
967
968 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800969 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
970
971 // Call back without the main BufferQueue lock held, but with the callback
972 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700973
974 int connectedApi;
975 sp<Fence> lastQueuedFence;
976
977 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800978 Mutex::Autolock lock(mCallbackMutex);
979 while (callbackTicket != mCurrentCallbackTicket) {
980 mCallbackCondition.wait(mCallbackMutex);
981 }
982
Yi Kong48a619f2018-06-05 16:34:59 -0700983 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800984 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -0700985 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -0800986 frameReplacedListener->onFrameReplaced(item);
987 }
988
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700989 connectedApi = mCore->mConnectedApi;
990 lastQueuedFence = std::move(mLastQueueBufferFence);
991
992 mLastQueueBufferFence = std::move(acquireFence);
993 mLastQueuedCrop = item.mCrop;
994 mLastQueuedTransform = item.mTransform;
995
Dan Stoza8dc55392014-11-04 11:37:46 -0800996 ++mCurrentCallbackTicket;
997 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800998 }
999
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001000 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001001 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001002 // Waiting here allows for two full buffers to be queued but not a
1003 // third. In the event that frames take varying time, this makes a
1004 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001005 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +00001006 }
1007
Brian Andersond6927fb2016-07-23 23:37:30 -07001008 // 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 16:31:30 -07001014 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001015 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001016 addAndGetFrameTimestamps(&newFrameEventsEntry,
1017 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001018
Dan Stoza289ade12014-02-28 11:17:17 -08001019 return NO_ERROR;
1020}
1021
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001022status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001023 ATRACE_CALL();
1024 BQ_LOGV("cancelBuffer: slot %d", slot);
1025 Mutex::Autolock lock(mCore->mMutex);
1026
1027 if (mCore->mIsAbandoned) {
1028 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001029 return NO_INIT;
1030 }
1031
1032 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1033 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1034 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001035 }
1036
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001037 if (mCore->mSharedBufferMode) {
1038 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001039 return BAD_VALUE;
1040 }
1041
Dan Stoza3e96f192014-03-03 10:16:19 -08001042 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001043 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001044 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001045 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001046 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001047 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001048 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001049 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001050 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001051 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001052 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001053 }
1054
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001055 mSlots[slot].mBufferState.cancel();
1056
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001057 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001058 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001059 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001060 mSlots[slot].mBufferState.mShared = false;
1061 }
1062
1063 // Don't put the shared buffer on the free list.
1064 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001065 mCore->mActiveBuffers.erase(slot);
1066 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001067 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001068
Dan Stoza289ade12014-02-28 11:17:17 -08001069 mSlots[slot].mFence = fence;
1070 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001071 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001072
1073 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001074}
1075
1076int BufferQueueProducer::query(int what, int *outValue) {
1077 ATRACE_CALL();
1078 Mutex::Autolock lock(mCore->mMutex);
1079
Yi Kong48a619f2018-06-05 16:34:59 -07001080 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001081 BQ_LOGE("query: outValue was NULL");
1082 return BAD_VALUE;
1083 }
1084
1085 if (mCore->mIsAbandoned) {
1086 BQ_LOGE("query: BufferQueue has been abandoned");
1087 return NO_INIT;
1088 }
1089
1090 int value;
1091 switch (what) {
1092 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001093 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001094 break;
1095 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001096 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001097 break;
1098 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001099 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001100 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001101 case NATIVE_WINDOW_LAYER_COUNT:
1102 // All BufferQueue buffers have a single layer.
1103 value = BQ_LAYER_COUNT;
1104 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001105 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001106 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001107 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001108 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001109 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001110 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001111 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1112 value = (mCore->mQueue.size() > 1);
1113 break;
1114 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001115 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001116 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001117 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001118 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1119 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1120 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001121 case NATIVE_WINDOW_BUFFER_AGE:
1122 if (mCore->mBufferAge > INT32_MAX) {
1123 value = 0;
1124 } else {
1125 value = static_cast<int32_t>(mCore->mBufferAge);
1126 }
1127 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001128 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1129 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1130 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001131 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1132 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1133 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001134 default:
1135 return BAD_VALUE;
1136 }
1137
1138 BQ_LOGV("query: %d? %d", what, value);
1139 *outValue = value;
1140 return NO_ERROR;
1141}
1142
Dan Stozaf0eaf252014-03-21 13:05:51 -07001143status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001144 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1145 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001146 Mutex::Autolock lock(mCore->mMutex);
1147 mConsumerName = mCore->mConsumerName;
1148 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1149 producerControlledByApp ? "true" : "false");
1150
1151 if (mCore->mIsAbandoned) {
1152 BQ_LOGE("connect: BufferQueue has been abandoned");
1153 return NO_INIT;
1154 }
1155
Yi Kong48a619f2018-06-05 16:34:59 -07001156 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001157 BQ_LOGE("connect: BufferQueue has no consumer");
1158 return NO_INIT;
1159 }
1160
Yi Kong48a619f2018-06-05 16:34:59 -07001161 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001162 BQ_LOGE("connect: output was NULL");
1163 return BAD_VALUE;
1164 }
1165
1166 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1167 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1168 mCore->mConnectedApi, api);
1169 return BAD_VALUE;
1170 }
1171
1172 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1173 mDequeueTimeout < 0 ?
1174 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1175 mCore->mMaxBufferCount) -
1176 mCore->getMaxBufferCountLocked();
1177 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1178 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1179 "slots. Delta = %d", delta);
1180 return BAD_VALUE;
1181 }
1182
Dan Stoza289ade12014-02-28 11:17:17 -08001183 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001184 switch (api) {
1185 case NATIVE_WINDOW_API_EGL:
1186 case NATIVE_WINDOW_API_CPU:
1187 case NATIVE_WINDOW_API_MEDIA:
1188 case NATIVE_WINDOW_API_CAMERA:
1189 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001190
1191 output->width = mCore->mDefaultWidth;
1192 output->height = mCore->mDefaultHeight;
1193 output->transformHint = mCore->mTransformHint;
1194 output->numPendingBuffers =
1195 static_cast<uint32_t>(mCore->mQueue.size());
1196 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001197 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001198
Yi Kong48a619f2018-06-05 16:34:59 -07001199 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001200 // Set up a death notification so that we can disconnect
1201 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001202 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001203 status = IInterface::asBinder(listener)->linkToDeath(
1204 static_cast<IBinder::DeathRecipient*>(this));
1205 if (status != NO_ERROR) {
1206 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1207 strerror(-status), status);
1208 }
1209 mCore->mLinkedToDeath = listener;
1210 }
1211 if (listener->needsReleaseNotify()) {
1212 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001213 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001214 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001215 break;
1216 default:
1217 BQ_LOGE("connect: unknown API %d", api);
1218 status = BAD_VALUE;
1219 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001220 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001221 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001222 mCore->mBufferHasBeenQueued = false;
1223 mCore->mDequeueBufferCannotBlock = false;
1224 if (mDequeueTimeout < 0) {
1225 mCore->mDequeueBufferCannotBlock =
1226 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001227 }
Dan Stoza289ade12014-02-28 11:17:17 -08001228
Pablo Ceballos981066c2016-02-18 12:54:37 -08001229 mCore->mAllowAllocation = true;
1230 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001231 return status;
1232}
1233
Robert Carr97b9c862016-09-08 13:54:35 -07001234status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001235 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001236 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001237
1238 int status = NO_ERROR;
1239 sp<IConsumerListener> listener;
1240 { // Autolock scope
1241 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001242
1243 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001244 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001245 return NO_ERROR;
1246 }
1247 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1248 }
1249
Antoine Labour78014f32014-07-15 21:17:03 -07001250 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001251
1252 if (mCore->mIsAbandoned) {
1253 // It's not really an error to disconnect after the surface has
1254 // been abandoned; it should just be a no-op.
1255 return NO_ERROR;
1256 }
1257
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001258 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001259 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1260 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1261 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001262 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001263 // If we're asked to disconnect the currently connected api but
1264 // nobody is connected, it's not really an error.
1265 if (api == BufferQueueCore::NO_CONNECTED_API) {
1266 return NO_ERROR;
1267 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001268 }
1269
Dan Stoza289ade12014-02-28 11:17:17 -08001270 switch (api) {
1271 case NATIVE_WINDOW_API_EGL:
1272 case NATIVE_WINDOW_API_CPU:
1273 case NATIVE_WINDOW_API_MEDIA:
1274 case NATIVE_WINDOW_API_CAMERA:
1275 if (mCore->mConnectedApi == api) {
1276 mCore->freeAllBuffersLocked();
1277
1278 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001279 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001280 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001281 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001282 // This can fail if we're here because of the death
1283 // notification, but we just ignore it
1284 token->unlinkToDeath(
1285 static_cast<IBinder::DeathRecipient*>(this));
1286 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001287 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001288 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001289 mCore->mLinkedToDeath = nullptr;
1290 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001291 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001292 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001293 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001294 mCore->mDequeueCondition.broadcast();
1295 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001296 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1297 BQ_LOGE("disconnect: not connected (req=%d)", api);
1298 status = NO_INIT;
1299 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001300 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001301 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001302 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001303 }
1304 break;
1305 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001306 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001307 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001308 break;
1309 }
1310 } // Autolock scope
1311
1312 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001313 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001314 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001315 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001316 }
1317
1318 return status;
1319}
1320
Jesse Hall399184a2014-03-03 15:42:54 -08001321status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001322 sp<IConsumerListener> listener;
1323 { // Autolock scope
1324 Mutex::Autolock _l(mCore->mMutex);
1325 mCore->mSidebandStream = stream;
1326 listener = mCore->mConsumerListener;
1327 } // Autolock scope
1328
Yi Kong48a619f2018-06-05 16:34:59 -07001329 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001330 listener->onSidebandStreamChanged();
1331 }
Jesse Hall399184a2014-03-03 15:42:54 -08001332 return NO_ERROR;
1333}
1334
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001335void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001336 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001337 ATRACE_CALL();
1338 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001339 size_t newBufferCount = 0;
1340 uint32_t allocWidth = 0;
1341 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001342 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001343 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001344 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001345 { // Autolock scope
1346 Mutex::Autolock lock(mCore->mMutex);
1347 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001348
Dan Stoza9de72932015-04-16 17:28:43 -07001349 if (!mCore->mAllowAllocation) {
1350 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1351 "BufferQueue");
1352 return;
1353 }
1354
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001355 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1356 // both allocateBuffers and dequeueBuffer.
1357 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001358 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001359 return;
1360 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001361
Antoine Labour78014f32014-07-15 21:17:03 -07001362 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1363 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1364 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1365 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001366 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001367
1368 mCore->mIsAllocating = true;
1369 } // Autolock scope
1370
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001371 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001372 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001373 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001374 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001375 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001376
1377 status_t result = graphicBuffer->initCheck();
1378
Antoine Labour78014f32014-07-15 21:17:03 -07001379 if (result != NO_ERROR) {
1380 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001381 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Antoine Labour78014f32014-07-15 21:17:03 -07001382 Mutex::Autolock lock(mCore->mMutex);
1383 mCore->mIsAllocating = false;
1384 mCore->mIsAllocatingCondition.broadcast();
1385 return;
1386 }
1387 buffers.push_back(graphicBuffer);
1388 }
1389
1390 { // Autolock scope
1391 Mutex::Autolock lock(mCore->mMutex);
1392 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1393 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001394 PixelFormat checkFormat = format != 0 ?
1395 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001396 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001397 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1398 checkFormat != allocFormat || checkUsage != allocUsage) {
1399 // Something changed while we released the lock. Retry.
1400 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1401 mCore->mIsAllocating = false;
1402 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001403 continue;
1404 }
1405
Antoine Labour78014f32014-07-15 21:17:03 -07001406 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001407 if (mCore->mFreeSlots.empty()) {
1408 BQ_LOGV("allocateBuffers: a slot was occupied while "
1409 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001410 continue;
1411 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001412 auto slot = mCore->mFreeSlots.begin();
1413 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1414 mSlots[*slot].mGraphicBuffer = buffers[i];
1415 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001416
1417 // freeBufferLocked puts this slot on the free slots list. Since
1418 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001419 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001420
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001421 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1422 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001423
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001424 if (mCore->mConsumerListener != nullptr) {
1425 BufferItem item;
1426 item.mGraphicBuffer = buffers[i];
1427 item.mSlot = *slot;
1428 mCore->mConsumerListener->onBufferAllocated(item);
1429 }
1430
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001431 // Make sure the erase is done after all uses of the slot
1432 // iterator since it will be invalid after this point.
1433 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001434 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001435
Antoine Labour78014f32014-07-15 21:17:03 -07001436 mCore->mIsAllocating = false;
1437 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001438 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001439 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001440 }
1441}
1442
Dan Stoza9de72932015-04-16 17:28:43 -07001443status_t BufferQueueProducer::allowAllocation(bool allow) {
1444 ATRACE_CALL();
1445 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1446
1447 Mutex::Autolock lock(mCore->mMutex);
1448 mCore->mAllowAllocation = allow;
1449 return NO_ERROR;
1450}
1451
Dan Stoza812ed062015-06-02 15:45:22 -07001452status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1453 ATRACE_CALL();
1454 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1455
1456 Mutex::Autolock lock(mCore->mMutex);
1457 mCore->mGenerationNumber = generationNumber;
1458 return NO_ERROR;
1459}
1460
Dan Stozac6f30bd2015-06-08 09:32:50 -07001461String8 BufferQueueProducer::getConsumerName() const {
1462 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001463 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001464 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1465 return mConsumerName;
1466}
1467
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001468status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001469 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001470 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001471
1472 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001473 if (!sharedBufferMode) {
1474 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001475 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001476 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001477 return NO_ERROR;
1478}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001479
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001480status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1481 ATRACE_CALL();
1482 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1483
1484 Mutex::Autolock lock(mCore->mMutex);
1485
1486 mCore->mAutoRefresh = autoRefresh;
1487 return NO_ERROR;
1488}
1489
Dan Stoza127fc632015-06-30 13:43:32 -07001490status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1491 ATRACE_CALL();
1492 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1493
Pablo Ceballos981066c2016-02-18 12:54:37 -08001494 Mutex::Autolock lock(mCore->mMutex);
1495 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1496 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1497 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1498 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1499 "available slots. Delta = %d", delta);
1500 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001501 }
1502
Pablo Ceballos981066c2016-02-18 12:54:37 -08001503 mDequeueTimeout = timeout;
1504 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001505
Pablo Ceballos981066c2016-02-18 12:54:37 -08001506 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001507 return NO_ERROR;
1508}
1509
Dan Stoza50101d02016-04-07 16:53:23 -07001510status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001511 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001512 ATRACE_CALL();
1513 BQ_LOGV("getLastQueuedBuffer");
1514
1515 Mutex::Autolock lock(mCore->mMutex);
1516 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1517 *outBuffer = nullptr;
1518 *outFence = Fence::NO_FENCE;
1519 return NO_ERROR;
1520 }
1521
1522 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1523 *outFence = mLastQueueBufferFence;
1524
John Reck1a61da52016-04-28 13:18:15 -07001525 // Currently only SurfaceFlinger internally ever changes
1526 // GLConsumer's filtering mode, so we just use 'true' here as
1527 // this is slightly specialized for the current client of this API,
1528 // which does want filtering.
1529 GLConsumer::computeTransformMatrix(outTransformMatrix,
1530 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1531 mLastQueuedTransform, true /* filter */);
1532
Dan Stoza50101d02016-04-07 16:53:23 -07001533 return NO_ERROR;
1534}
1535
Brian Anderson3890c392016-07-25 12:48:08 -07001536void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1537 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001538}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001539
Brian Anderson3890c392016-07-25 12:48:08 -07001540void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001541 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001542 FrameEventHistoryDelta* outDelta) {
1543 if (newTimestamps == nullptr && outDelta == nullptr) {
1544 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001545 }
1546
1547 ATRACE_CALL();
1548 BQ_LOGV("addAndGetFrameTimestamps");
1549 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001550 {
1551 Mutex::Autolock lock(mCore->mMutex);
1552 listener = mCore->mConsumerListener;
1553 }
Yi Kong48a619f2018-06-05 16:34:59 -07001554 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001555 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001556 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001557}
1558
Dan Stoza289ade12014-02-28 11:17:17 -08001559void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1560 // If we're here, it means that a producer we were connected to died.
1561 // We're guaranteed that we are still connected to it because we remove
1562 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1563 // without synchronization here.
1564 int api = mCore->mConnectedApi;
1565 disconnect(api);
1566}
1567
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001568status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1569 BQ_LOGV("getUniqueId");
1570
1571 *outId = mCore->mUniqueId;
1572 return NO_ERROR;
1573}
1574
Chia-I Wue2786ea2017-08-07 10:36:08 -07001575status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1576 BQ_LOGV("getConsumerUsage");
1577
1578 Mutex::Autolock lock(mCore->mMutex);
1579 *outUsage = mCore->mConsumerUsageBits;
1580 return NO_ERROR;
1581}
1582
Dan Stoza289ade12014-02-28 11:17:17 -08001583} // namespace android