blob: 90b4b9df3d58d3fd72619a0c3b6f9f011bb92060 [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>
37#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070038#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
43namespace android {
44
Irvel468051e2016-06-13 16:44:44 -070045BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
46 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080047 mCore(core),
48 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070049 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070050 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070051 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080052 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070053 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080054 mCallbackMutex(),
55 mNextCallbackTicket(0),
56 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070057 mCallbackCondition(),
58 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080059
60BufferQueueProducer::~BufferQueueProducer() {}
61
62status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
63 ATRACE_CALL();
64 BQ_LOGV("requestBuffer: slot %d", slot);
65 Mutex::Autolock lock(mCore->mMutex);
66
67 if (mCore->mIsAbandoned) {
68 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
69 return NO_INIT;
70 }
71
Pablo Ceballos583b1b32015-09-03 18:23:52 -070072 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
73 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
74 return NO_INIT;
75 }
76
Dan Stoza3e96f192014-03-03 10:16:19 -080077 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080078 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080079 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080080 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070081 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080082 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070083 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080084 return BAD_VALUE;
85 }
86
87 mSlots[slot].mRequestBufferCalled = true;
88 *buf = mSlots[slot].mGraphicBuffer;
89 return NO_ERROR;
90}
91
Pablo Ceballosfa455352015-08-12 17:47:47 -070092status_t BufferQueueProducer::setMaxDequeuedBufferCount(
93 int maxDequeuedBuffers) {
94 ATRACE_CALL();
95 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
96 maxDequeuedBuffers);
97
Pablo Ceballos981066c2016-02-18 12:54:37 -080098 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070099 { // Autolock scope
100 Mutex::Autolock lock(mCore->mMutex);
101 mCore->waitWhileAllocatingLocked();
102
103 if (mCore->mIsAbandoned) {
104 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
105 "abandoned");
106 return NO_INIT;
107 }
108
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700109 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
110 return NO_ERROR;
111 }
112
Pablo Ceballos72daab62015-12-07 16:38:43 -0800113 // The new maxDequeuedBuffer count should not be violated by the number
114 // of currently dequeued buffers
115 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800116 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700117 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800118 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700119 }
120 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800121 if (dequeuedCount > maxDequeuedBuffers) {
122 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
123 "count (%d) exceeds the current dequeued buffer count (%d)",
124 maxDequeuedBuffers, dequeuedCount);
125 return BAD_VALUE;
126 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700127
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700128 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700129 bufferCount += maxDequeuedBuffers;
130
131 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
132 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
133 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
134 return BAD_VALUE;
135 }
136
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700137 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700138 if (bufferCount < minBufferSlots) {
139 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
140 "less than minimum %d", bufferCount, minBufferSlots);
141 return BAD_VALUE;
142 }
143
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700144 if (bufferCount > mCore->mMaxBufferCount) {
145 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700146 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
147 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
148 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
149 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700150 return BAD_VALUE;
151 }
152
Pablo Ceballos72daab62015-12-07 16:38:43 -0800153 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800154 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800155 return BAD_VALUE;
156 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700157 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800158 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800159 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800160 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800161 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700162 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700163 } // Autolock scope
164
165 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800166 if (listener != NULL) {
167 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700168 }
169
170 return NO_ERROR;
171}
172
173status_t BufferQueueProducer::setAsyncMode(bool async) {
174 ATRACE_CALL();
175 BQ_LOGV("setAsyncMode: async = %d", async);
176
Pablo Ceballos981066c2016-02-18 12:54:37 -0800177 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700178 { // Autolock scope
179 Mutex::Autolock lock(mCore->mMutex);
180 mCore->waitWhileAllocatingLocked();
181
182 if (mCore->mIsAbandoned) {
183 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
184 return NO_INIT;
185 }
186
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700187 if (async == mCore->mAsyncMode) {
188 return NO_ERROR;
189 }
190
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700191 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700192 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
193 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700194 BQ_LOGE("setAsyncMode(%d): this call would cause the "
195 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700196 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
197 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
198 mCore->mMaxDequeuedBufferCount,
199 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700200 return BAD_VALUE;
201 }
202
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800203 int delta = mCore->getMaxBufferCountLocked(async,
204 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
205 - mCore->getMaxBufferCountLocked();
206
Pablo Ceballos981066c2016-02-18 12:54:37 -0800207 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800208 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
209 "available slots. Delta = %d", delta);
210 return BAD_VALUE;
211 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700212 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800213 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700214 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700215 if (delta < 0) {
216 listener = mCore->mConsumerListener;
217 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 } // Autolock scope
219
220 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800221 if (listener != NULL) {
222 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700223 }
224 return NO_ERROR;
225}
226
Dan Stoza5ecfb682016-01-04 17:01:02 -0800227int BufferQueueProducer::getFreeBufferLocked() const {
228 if (mCore->mFreeBuffers.empty()) {
229 return BufferQueueCore::INVALID_BUFFER_SLOT;
230 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800231 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800232 mCore->mFreeBuffers.pop_front();
233 return slot;
234}
235
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800237 if (mCore->mFreeSlots.empty()) {
238 return BufferQueueCore::INVALID_BUFFER_SLOT;
239 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800240 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800242 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800243}
244
245status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800247 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
248 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800249 bool tryAgain = true;
250 while (tryAgain) {
251 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800252 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800253 return NO_INIT;
254 }
255
Dan Stoza9f3053d2014-03-06 15:14:33 -0800256 int dequeuedCount = 0;
257 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800258 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700259 if (mSlots[s].mBufferState.isDequeued()) {
260 ++dequeuedCount;
261 }
262 if (mSlots[s].mBufferState.isAcquired()) {
263 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800264 }
265 }
266
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700267 // Producers are not allowed to dequeue more than
268 // mMaxDequeuedBufferCount buffers.
269 // This check is only done if a buffer has already been queued
270 if (mCore->mBufferHasBeenQueued &&
271 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
272 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800273 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800274 return INVALID_OPERATION;
275 }
276
Dan Stoza0de7ea72015-04-23 13:20:51 -0700277 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
278
Dan Stozaae3c3682014-04-18 15:43:35 -0700279 // If we disconnect and reconnect quickly, we can be in a state where
280 // our slots are empty but we have many buffers in the queue. This can
281 // cause us to run out of memory if we outrun the consumer. Wait here if
282 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800283 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700284 bool tooManyBuffers = mCore->mQueue.size()
285 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700286 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800287 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700288 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700289 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700290 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700291 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700292 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700293 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700294 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800295 } else {
296 if (caller == FreeSlotCaller::Dequeue) {
297 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800298 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800299 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
300 *found = slot;
301 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800303 }
304 } else {
305 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800306 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800307 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
308 *found = slot;
309 } else {
310 *found = getFreeBufferLocked();
311 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700312 }
313 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700314 }
315
316 // If no buffer is found, or if the queue has too many buffers
317 // outstanding, wait for a buffer to be acquired or released, or for the
318 // max buffer count to change.
319 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
320 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800321 if (tryAgain) {
322 // Return an error if we're in non-blocking mode (producer and
323 // consumer are controlled by the application).
324 // However, the consumer is allowed to briefly acquire an extra
325 // buffer (which could cause us to have to wait here), which is
326 // okay, since it is only used to implement an atomic acquire +
327 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700328 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800329 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
330 return WOULD_BLOCK;
331 }
Dan Stoza127fc632015-06-30 13:43:32 -0700332 if (mDequeueTimeout >= 0) {
333 status_t result = mCore->mDequeueCondition.waitRelative(
334 mCore->mMutex, mDequeueTimeout);
335 if (result == TIMED_OUT) {
336 return result;
337 }
338 } else {
339 mCore->mDequeueCondition.wait(mCore->mMutex);
340 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800341 }
342 } // while (tryAgain)
343
344 return NO_ERROR;
345}
346
Dan Stoza289ade12014-02-28 11:17:17 -0800347status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700348 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
349 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800350 ATRACE_CALL();
351 { // Autolock scope
352 Mutex::Autolock lock(mCore->mMutex);
353 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700354
355 if (mCore->mIsAbandoned) {
356 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
357 return NO_INIT;
358 }
359
360 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
361 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
362 return NO_INIT;
363 }
Dan Stoza289ade12014-02-28 11:17:17 -0800364 } // Autolock scope
365
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700366 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
367 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800368
369 if ((width && !height) || (!width && height)) {
370 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
371 return BAD_VALUE;
372 }
373
374 status_t returnFlags = NO_ERROR;
375 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
376 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800378
379 { // Autolock scope
380 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700381 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800382
383 if (format == 0) {
384 format = mCore->mDefaultBufferFormat;
385 }
386
387 // Enable the usage bits the consumer requested
388 usage |= mCore->mConsumerUsageBits;
389
Dan Stoza9de72932015-04-16 17:28:43 -0700390 const bool useDefaultSize = !width && !height;
391 if (useDefaultSize) {
392 width = mCore->mDefaultWidth;
393 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800394 }
Dan Stoza289ade12014-02-28 11:17:17 -0800395
Pablo Ceballos981066c2016-02-18 12:54:37 -0800396 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700397 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800398 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800399 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700400 if (status != NO_ERROR) {
401 return status;
402 }
403
404 // This should not happen
405 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
406 BQ_LOGE("dequeueBuffer: no available buffer slots");
407 return -EBUSY;
408 }
409
410 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
411
412 // If we are not allowed to allocate new buffers,
413 // waitForFreeSlotThenRelock must have returned a slot containing a
414 // buffer. If this buffer would require reallocation to meet the
415 // requested attributes, we free it and attempt to get another one.
416 if (!mCore->mAllowAllocation) {
417 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700418 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700419 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
420 "buffer");
421 return BAD_VALUE;
422 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800423 mCore->mFreeSlots.insert(found);
424 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700425 found = BufferItem::INVALID_BUFFER_SLOT;
426 continue;
427 }
428 }
Dan Stoza289ade12014-02-28 11:17:17 -0800429 }
430
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800431 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700432 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800433 buffer->needsReallocation(width, height, format, usage)) {
434 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
435 "buffer");
436
437 return BAD_VALUE;
438 }
439
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700440 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800441 mCore->mActiveBuffers.insert(found);
442 }
Dan Stoza289ade12014-02-28 11:17:17 -0800443 *outSlot = found;
444 ATRACE_BUFFER_INDEX(found);
445
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800446 attachedByConsumer = mSlots[found].mNeedsReallocation;
447 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700449 mSlots[found].mBufferState.dequeue();
450
Dan Stoza289ade12014-02-28 11:17:17 -0800451 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700452 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800453 {
454 mSlots[found].mAcquireCalled = false;
455 mSlots[found].mGraphicBuffer = NULL;
456 mSlots[found].mRequestBufferCalled = false;
457 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
458 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
459 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800460 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700461 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800462
463 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800464 } else {
465 // We add 1 because that will be the frame number when this buffer
466 // is queued
467 mCore->mBufferAge =
468 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800469 }
470
Dan Stoza800b41a2015-04-28 14:20:04 -0700471 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
472 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800473
Dan Stoza289ade12014-02-28 11:17:17 -0800474 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
475 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
476 "slot=%d w=%d h=%d format=%u",
477 found, buffer->width, buffer->height, buffer->format);
478 }
479
480 eglDisplay = mSlots[found].mEglDisplay;
481 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700482 // Don't return a fence in shared buffer mode, except for the first
483 // frame.
484 *outFence = (mCore->mSharedBufferMode &&
485 mCore->mSharedBufferSlot == found) ?
486 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800487 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
488 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700489
490 // If shared buffer mode has just been enabled, cache the slot of the
491 // first buffer that is dequeued and mark it as the shared buffer.
492 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
493 BufferQueueCore::INVALID_BUFFER_SLOT) {
494 mCore->mSharedBufferSlot = found;
495 mSlots[found].mBufferState.mShared = true;
496 }
Dan Stoza289ade12014-02-28 11:17:17 -0800497 } // Autolock scope
498
499 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
500 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700501 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800502 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza024e9312016-08-24 12:17:29 -0700503 width, height, format, usage,
504 {mConsumerName.string(), mConsumerName.size()}, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800505 { // Autolock scope
506 Mutex::Autolock lock(mCore->mMutex);
507
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700508 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
509 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
510 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
511 }
512
513 mCore->mIsAllocating = false;
514 mCore->mIsAllocatingCondition.broadcast();
515
516 if (graphicBuffer == NULL) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700517 mCore->mFreeSlots.insert(*outSlot);
518 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700519 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
520 return error;
521 }
522
Dan Stoza289ade12014-02-28 11:17:17 -0800523 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700524 mCore->mFreeSlots.insert(*outSlot);
525 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800526 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
527 return NO_INIT;
528 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800529
Pablo Ceballos9e314332016-01-12 13:49:19 -0800530 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800531 } // Autolock scope
532 }
533
Dan Stoza9f3053d2014-03-06 15:14:33 -0800534 if (attachedByConsumer) {
535 returnFlags |= BUFFER_NEEDS_REALLOCATION;
536 }
537
Dan Stoza289ade12014-02-28 11:17:17 -0800538 if (eglFence != EGL_NO_SYNC_KHR) {
539 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
540 1000000000);
541 // If something goes wrong, log the error, but return the buffer without
542 // synchronizing access to it. It's too late at this point to abort the
543 // dequeue operation.
544 if (result == EGL_FALSE) {
545 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
546 eglGetError());
547 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
548 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
549 }
550 eglDestroySyncKHR(eglDisplay, eglFence);
551 }
552
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700553 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
554 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800555 mSlots[*outSlot].mFrameNumber,
556 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
557
558 return returnFlags;
559}
560
Dan Stoza9f3053d2014-03-06 15:14:33 -0800561status_t BufferQueueProducer::detachBuffer(int slot) {
562 ATRACE_CALL();
563 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700564 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800565
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700566 sp<IConsumerListener> listener;
567 {
568 Mutex::Autolock lock(mCore->mMutex);
569
570 if (mCore->mIsAbandoned) {
571 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
572 return NO_INIT;
573 }
574
575 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
576 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
577 return NO_INIT;
578 }
579
580 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
581 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
582 return BAD_VALUE;
583 }
584
585 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
586 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
587 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
588 return BAD_VALUE;
589 } else if (!mSlots[slot].mBufferState.isDequeued()) {
590 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
591 "(state = %s)", slot, mSlots[slot].mBufferState.string());
592 return BAD_VALUE;
593 } else if (!mSlots[slot].mRequestBufferCalled) {
594 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
595 slot);
596 return BAD_VALUE;
597 }
598
599 mSlots[slot].mBufferState.detachProducer();
600 mCore->mActiveBuffers.erase(slot);
601 mCore->mFreeSlots.insert(slot);
602 mCore->clearBufferSlotLocked(slot);
603 mCore->mDequeueCondition.broadcast();
604 VALIDATE_CONSISTENCY();
605 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800606 }
607
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700608 if (listener != NULL) {
609 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700610 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800611
Dan Stoza9f3053d2014-03-06 15:14:33 -0800612 return NO_ERROR;
613}
614
Dan Stozad9822a32014-03-28 15:25:31 -0700615status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
616 sp<Fence>* outFence) {
617 ATRACE_CALL();
618
619 if (outBuffer == NULL) {
620 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
621 return BAD_VALUE;
622 } else if (outFence == NULL) {
623 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
624 return BAD_VALUE;
625 }
626
Pablo Ceballos981066c2016-02-18 12:54:37 -0800627 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700628
Pablo Ceballos981066c2016-02-18 12:54:37 -0800629 if (mCore->mIsAbandoned) {
630 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
631 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700632 }
633
Pablo Ceballos981066c2016-02-18 12:54:37 -0800634 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
635 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
636 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700637 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800638
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700639 if (mCore->mSharedBufferMode) {
640 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
641 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800642 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700643 }
644
Pablo Ceballos981066c2016-02-18 12:54:37 -0800645 mCore->waitWhileAllocatingLocked();
646
647 if (mCore->mFreeBuffers.empty()) {
648 return NO_MEMORY;
649 }
650
651 int found = mCore->mFreeBuffers.front();
652 mCore->mFreeBuffers.remove(found);
653 mCore->mFreeSlots.insert(found);
654
655 BQ_LOGV("detachNextBuffer detached slot %d", found);
656
657 *outBuffer = mSlots[found].mGraphicBuffer;
658 *outFence = mSlots[found].mFence;
659 mCore->clearBufferSlotLocked(found);
660 VALIDATE_CONSISTENCY();
661
Dan Stozad9822a32014-03-28 15:25:31 -0700662 return NO_ERROR;
663}
664
Dan Stoza9f3053d2014-03-06 15:14:33 -0800665status_t BufferQueueProducer::attachBuffer(int* outSlot,
666 const sp<android::GraphicBuffer>& buffer) {
667 ATRACE_CALL();
668
669 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700670 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800671 return BAD_VALUE;
672 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700673 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800674 return BAD_VALUE;
675 }
676
677 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700678
679 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700680 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700681 return NO_INIT;
682 }
683
684 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700685 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700686 return NO_INIT;
687 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800688
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700689 if (mCore->mSharedBufferMode) {
690 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700691 return BAD_VALUE;
692 }
693
Dan Stoza812ed062015-06-02 15:45:22 -0700694 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
695 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
696 "[queue %u]", buffer->getGenerationNumber(),
697 mCore->mGenerationNumber);
698 return BAD_VALUE;
699 }
700
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700701 mCore->waitWhileAllocatingLocked();
702
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703 status_t returnFlags = NO_ERROR;
704 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800705 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800706 if (status != NO_ERROR) {
707 return status;
708 }
709
710 // This should not happen
711 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700712 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800713 return -EBUSY;
714 }
715
716 *outSlot = found;
717 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700718 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800719 *outSlot, returnFlags);
720
721 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700722 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800723 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
724 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700725 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800726 mSlots[*outSlot].mAcquireCalled = false;
727 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800728 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700729
Dan Stoza9f3053d2014-03-06 15:14:33 -0800730 return returnFlags;
731}
732
Dan Stoza289ade12014-02-28 11:17:17 -0800733status_t BufferQueueProducer::queueBuffer(int slot,
734 const QueueBufferInput &input, QueueBufferOutput *output) {
735 ATRACE_CALL();
736 ATRACE_BUFFER_INDEX(slot);
737
738 int64_t timestamp;
739 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800740 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700741 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800742 int scalingMode;
743 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700744 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800745 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800746 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700747 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700748 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800749
750 if (fence == NULL) {
751 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800752 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800753 }
754
755 switch (scalingMode) {
756 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
757 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
758 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
759 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
760 break;
761 default:
762 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800763 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800764 }
765
Dan Stoza8dc55392014-11-04 11:37:46 -0800766 sp<IConsumerListener> frameAvailableListener;
767 sp<IConsumerListener> frameReplacedListener;
768 int callbackTicket = 0;
769 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800770 { // Autolock scope
771 Mutex::Autolock lock(mCore->mMutex);
772
773 if (mCore->mIsAbandoned) {
774 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
775 return NO_INIT;
776 }
777
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700778 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
779 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
780 return NO_INIT;
781 }
782
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800783 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800784 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800785 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800786 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700787 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800788 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700789 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800790 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800791 } else if (!mSlots[slot].mRequestBufferCalled) {
792 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
793 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800794 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800795 }
796
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700797 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700798 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700799 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700800 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700801 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700802 mSlots[slot].mBufferState.mShared = true;
803 }
804
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800805 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700806 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800807 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800808 crop.left, crop.top, crop.right, crop.bottom, transform,
809 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800810
811 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
812 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700813 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800814 crop.intersect(bufferRect, &croppedRect);
815 if (croppedRect != crop) {
816 BQ_LOGE("queueBuffer: crop rect is not contained within the "
817 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800818 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800819 }
820
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800821 // Override UNKNOWN dataspace with consumer default
822 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
823 dataSpace = mCore->mDefaultBufferDataSpace;
824 }
825
Dan Stoza289ade12014-02-28 11:17:17 -0800826 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700827 mSlots[slot].mBufferState.queue();
828
Dan Stoza289ade12014-02-28 11:17:17 -0800829 ++mCore->mFrameCounter;
830 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
831
Dan Stoza289ade12014-02-28 11:17:17 -0800832 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
833 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
834 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800835 item.mTransform = transform &
836 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800837 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800838 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
839 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800840 item.mTimestamp = timestamp;
841 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800842 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800843 item.mFrameNumber = mCore->mFrameCounter;
844 item.mSlot = slot;
845 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700846 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700847 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700848 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700849 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700850 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700851 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800852
Ruben Brunk1681d952014-06-27 15:51:55 -0700853 mStickyTransform = stickyTransform;
854
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700855 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700856 if (mCore->mSharedBufferMode) {
857 mCore->mSharedBufferCache.crop = crop;
858 mCore->mSharedBufferCache.transform = transform;
859 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700860 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700861 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 }
863
Dan Stoza289ade12014-02-28 11:17:17 -0800864 if (mCore->mQueue.empty()) {
865 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
866 // and simply queue this buffer
867 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800868 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800869 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700870 // When the queue is not empty, we need to look at the last buffer
871 // in the queue to see if we need to replace it
872 const BufferItem& last = mCore->mQueue.itemAt(
873 mCore->mQueue.size() - 1);
874 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800875
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700876 if (!last.mIsStale) {
877 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700879 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700880 // still be around. Mark it as no longer shared if this
881 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700882 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700883 mSlots[last.mSlot].mBufferState.isFree()) {
884 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700885 }
886 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700887 if (!mSlots[last.mSlot].mBufferState.isShared()) {
888 mCore->mActiveBuffers.erase(last.mSlot);
889 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700890 }
Dan Stoza289ade12014-02-28 11:17:17 -0800891 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800892
Dan Stoza289ade12014-02-28 11:17:17 -0800893 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700894 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800895 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800896 } else {
897 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800898 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800899 }
900 }
901
902 mCore->mBufferHasBeenQueued = true;
903 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700904 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800905
906 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800907 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700908 static_cast<uint32_t>(mCore->mQueue.size()),
909 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -0800910
Colin Cross152c3b72016-09-27 14:08:19 -0700911 ATRACE_INT(mCore->mConsumerName.string(),
912 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700913 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800914
915 // Take a ticket for the callback functions
916 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700917
Pablo Ceballos9e314332016-01-12 13:49:19 -0800918 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800919 } // Autolock scope
920
Irvel468051e2016-06-13 16:44:44 -0700921 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
922 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
923 // there will be no Binder call
924 if (!mConsumerIsSurfaceFlinger) {
925 item.mGraphicBuffer.clear();
926 }
927
928 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800929 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
930
931 // Call back without the main BufferQueue lock held, but with the callback
932 // lock held so we can ensure that callbacks occur in order
933 {
934 Mutex::Autolock lock(mCallbackMutex);
935 while (callbackTicket != mCurrentCallbackTicket) {
936 mCallbackCondition.wait(mCallbackMutex);
937 }
938
939 if (frameAvailableListener != NULL) {
940 frameAvailableListener->onFrameAvailable(item);
941 } else if (frameReplacedListener != NULL) {
942 frameReplacedListener->onFrameReplaced(item);
943 }
944
945 ++mCurrentCallbackTicket;
946 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800947 }
948
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000949 // Wait without lock held
950 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
951 // Waiting here allows for two full buffers to be queued but not a
952 // third. In the event that frames take varying time, this makes a
953 // small trade-off in favor of latency rather than throughput.
954 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000955 }
Dan Stoza50101d02016-04-07 16:53:23 -0700956 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700957 mLastQueuedCrop = item.mCrop;
958 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000959
Dan Stoza289ade12014-02-28 11:17:17 -0800960 return NO_ERROR;
961}
962
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700963status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800964 ATRACE_CALL();
965 BQ_LOGV("cancelBuffer: slot %d", slot);
966 Mutex::Autolock lock(mCore->mMutex);
967
968 if (mCore->mIsAbandoned) {
969 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700970 return NO_INIT;
971 }
972
973 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
974 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
975 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800976 }
977
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700978 if (mCore->mSharedBufferMode) {
979 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700980 return BAD_VALUE;
981 }
982
Dan Stoza3e96f192014-03-03 10:16:19 -0800983 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800984 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800985 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700986 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700987 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800988 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700989 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700990 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800991 } else if (fence == NULL) {
992 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700993 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800994 }
995
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700996 mSlots[slot].mBufferState.cancel();
997
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700998 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700999 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001000 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001001 mSlots[slot].mBufferState.mShared = false;
1002 }
1003
1004 // Don't put the shared buffer on the free list.
1005 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001006 mCore->mActiveBuffers.erase(slot);
1007 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001008 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001009
Dan Stoza289ade12014-02-28 11:17:17 -08001010 mSlots[slot].mFence = fence;
1011 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001012 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001013
1014 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001015}
1016
1017int BufferQueueProducer::query(int what, int *outValue) {
1018 ATRACE_CALL();
1019 Mutex::Autolock lock(mCore->mMutex);
1020
1021 if (outValue == NULL) {
1022 BQ_LOGE("query: outValue was NULL");
1023 return BAD_VALUE;
1024 }
1025
1026 if (mCore->mIsAbandoned) {
1027 BQ_LOGE("query: BufferQueue has been abandoned");
1028 return NO_INIT;
1029 }
1030
1031 int value;
1032 switch (what) {
1033 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001034 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001035 break;
1036 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001037 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001038 break;
1039 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001040 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001041 break;
1042 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001043 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001044 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001045 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001046 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001047 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001048 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1049 value = (mCore->mQueue.size() > 1);
1050 break;
1051 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001052 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001053 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001054 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1055 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1056 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001057 case NATIVE_WINDOW_BUFFER_AGE:
1058 if (mCore->mBufferAge > INT32_MAX) {
1059 value = 0;
1060 } else {
1061 value = static_cast<int32_t>(mCore->mBufferAge);
1062 }
1063 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001064 default:
1065 return BAD_VALUE;
1066 }
1067
1068 BQ_LOGV("query: %d? %d", what, value);
1069 *outValue = value;
1070 return NO_ERROR;
1071}
1072
Dan Stozaf0eaf252014-03-21 13:05:51 -07001073status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001074 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1075 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001076 Mutex::Autolock lock(mCore->mMutex);
1077 mConsumerName = mCore->mConsumerName;
1078 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1079 producerControlledByApp ? "true" : "false");
1080
1081 if (mCore->mIsAbandoned) {
1082 BQ_LOGE("connect: BufferQueue has been abandoned");
1083 return NO_INIT;
1084 }
1085
1086 if (mCore->mConsumerListener == NULL) {
1087 BQ_LOGE("connect: BufferQueue has no consumer");
1088 return NO_INIT;
1089 }
1090
1091 if (output == NULL) {
1092 BQ_LOGE("connect: output was NULL");
1093 return BAD_VALUE;
1094 }
1095
1096 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1097 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1098 mCore->mConnectedApi, api);
1099 return BAD_VALUE;
1100 }
1101
1102 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1103 mDequeueTimeout < 0 ?
1104 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1105 mCore->mMaxBufferCount) -
1106 mCore->getMaxBufferCountLocked();
1107 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1108 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1109 "slots. Delta = %d", delta);
1110 return BAD_VALUE;
1111 }
1112
Dan Stoza289ade12014-02-28 11:17:17 -08001113 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001114 switch (api) {
1115 case NATIVE_WINDOW_API_EGL:
1116 case NATIVE_WINDOW_API_CPU:
1117 case NATIVE_WINDOW_API_MEDIA:
1118 case NATIVE_WINDOW_API_CAMERA:
1119 mCore->mConnectedApi = api;
1120 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1121 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001122 static_cast<uint32_t>(mCore->mQueue.size()),
1123 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -08001124
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001125 if (listener != NULL) {
1126 // Set up a death notification so that we can disconnect
1127 // automatically if the remote producer dies
1128 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1129 status = IInterface::asBinder(listener)->linkToDeath(
1130 static_cast<IBinder::DeathRecipient*>(this));
1131 if (status != NO_ERROR) {
1132 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1133 strerror(-status), status);
1134 }
1135 mCore->mLinkedToDeath = listener;
1136 }
1137 if (listener->needsReleaseNotify()) {
1138 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001139 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001140 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001141 break;
1142 default:
1143 BQ_LOGE("connect: unknown API %d", api);
1144 status = BAD_VALUE;
1145 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001146 }
Robert Carr97b9c862016-09-08 13:54:35 -07001147 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001148 mCore->mBufferHasBeenQueued = false;
1149 mCore->mDequeueBufferCannotBlock = false;
1150 if (mDequeueTimeout < 0) {
1151 mCore->mDequeueBufferCannotBlock =
1152 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001153 }
Dan Stoza289ade12014-02-28 11:17:17 -08001154
Pablo Ceballos981066c2016-02-18 12:54:37 -08001155 mCore->mAllowAllocation = true;
1156 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001157 return status;
1158}
1159
Robert Carr97b9c862016-09-08 13:54:35 -07001160status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001161 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001162 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001163
1164 int status = NO_ERROR;
1165 sp<IConsumerListener> listener;
1166 { // Autolock scope
1167 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001168
1169 if (mode == DisconnectMode::AllLocal) {
1170 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1171 return NO_ERROR;
1172 }
1173 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1174 }
1175
Antoine Labour78014f32014-07-15 21:17:03 -07001176 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001177
1178 if (mCore->mIsAbandoned) {
1179 // It's not really an error to disconnect after the surface has
1180 // been abandoned; it should just be a no-op.
1181 return NO_ERROR;
1182 }
1183
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001184 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1185 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001186 // If we're asked to disconnect the currently connected api but
1187 // nobody is connected, it's not really an error.
1188 if (api == BufferQueueCore::NO_CONNECTED_API) {
1189 return NO_ERROR;
1190 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001191 }
1192
Dan Stoza289ade12014-02-28 11:17:17 -08001193 switch (api) {
1194 case NATIVE_WINDOW_API_EGL:
1195 case NATIVE_WINDOW_API_CPU:
1196 case NATIVE_WINDOW_API_MEDIA:
1197 case NATIVE_WINDOW_API_CAMERA:
1198 if (mCore->mConnectedApi == api) {
1199 mCore->freeAllBuffersLocked();
1200
1201 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001202 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001203 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001204 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001205 // This can fail if we're here because of the death
1206 // notification, but we just ignore it
1207 token->unlinkToDeath(
1208 static_cast<IBinder::DeathRecipient*>(this));
1209 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001210 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001211 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001212 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001213 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001214 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001215 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001216 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001217 mCore->mDequeueCondition.broadcast();
1218 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001219 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001220 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001221 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001222 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001223 }
1224 break;
1225 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001226 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001227 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001228 break;
1229 }
1230 } // Autolock scope
1231
1232 // Call back without lock held
1233 if (listener != NULL) {
1234 listener->onBuffersReleased();
1235 }
1236
1237 return status;
1238}
1239
Jesse Hall399184a2014-03-03 15:42:54 -08001240status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001241 sp<IConsumerListener> listener;
1242 { // Autolock scope
1243 Mutex::Autolock _l(mCore->mMutex);
1244 mCore->mSidebandStream = stream;
1245 listener = mCore->mConsumerListener;
1246 } // Autolock scope
1247
1248 if (listener != NULL) {
1249 listener->onSidebandStreamChanged();
1250 }
Jesse Hall399184a2014-03-03 15:42:54 -08001251 return NO_ERROR;
1252}
1253
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001254void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1255 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001256 ATRACE_CALL();
1257 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001258 size_t newBufferCount = 0;
1259 uint32_t allocWidth = 0;
1260 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001261 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001262 uint32_t allocUsage = 0;
1263 { // Autolock scope
1264 Mutex::Autolock lock(mCore->mMutex);
1265 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001266
Dan Stoza9de72932015-04-16 17:28:43 -07001267 if (!mCore->mAllowAllocation) {
1268 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1269 "BufferQueue");
1270 return;
1271 }
1272
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001273 newBufferCount = mCore->mFreeSlots.size();
1274 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001275 return;
1276 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001277
Antoine Labour78014f32014-07-15 21:17:03 -07001278 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1279 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1280 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1281 allocUsage = usage | mCore->mConsumerUsageBits;
1282
1283 mCore->mIsAllocating = true;
1284 } // Autolock scope
1285
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001286 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001287 for (size_t i = 0; i < newBufferCount; ++i) {
1288 status_t result = NO_ERROR;
1289 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza024e9312016-08-24 12:17:29 -07001290 allocWidth, allocHeight, allocFormat, allocUsage,
1291 {mConsumerName.string(), mConsumerName.size()}, &result));
Antoine Labour78014f32014-07-15 21:17:03 -07001292 if (result != NO_ERROR) {
1293 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1294 " %u, usage %u)", width, height, format, usage);
1295 Mutex::Autolock lock(mCore->mMutex);
1296 mCore->mIsAllocating = false;
1297 mCore->mIsAllocatingCondition.broadcast();
1298 return;
1299 }
1300 buffers.push_back(graphicBuffer);
1301 }
1302
1303 { // Autolock scope
1304 Mutex::Autolock lock(mCore->mMutex);
1305 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1306 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001307 PixelFormat checkFormat = format != 0 ?
1308 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001309 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1310 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1311 checkFormat != allocFormat || checkUsage != allocUsage) {
1312 // Something changed while we released the lock. Retry.
1313 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1314 mCore->mIsAllocating = false;
1315 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001316 continue;
1317 }
1318
Antoine Labour78014f32014-07-15 21:17:03 -07001319 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001320 if (mCore->mFreeSlots.empty()) {
1321 BQ_LOGV("allocateBuffers: a slot was occupied while "
1322 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001323 continue;
1324 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001325 auto slot = mCore->mFreeSlots.begin();
1326 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1327 mSlots[*slot].mGraphicBuffer = buffers[i];
1328 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001329
1330 // freeBufferLocked puts this slot on the free slots list. Since
1331 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001332 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001333
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001334 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1335 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001336
1337 // Make sure the erase is done after all uses of the slot
1338 // iterator since it will be invalid after this point.
1339 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001340 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001341
Antoine Labour78014f32014-07-15 21:17:03 -07001342 mCore->mIsAllocating = false;
1343 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001344 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001345 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001346 }
1347}
1348
Dan Stoza9de72932015-04-16 17:28:43 -07001349status_t BufferQueueProducer::allowAllocation(bool allow) {
1350 ATRACE_CALL();
1351 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1352
1353 Mutex::Autolock lock(mCore->mMutex);
1354 mCore->mAllowAllocation = allow;
1355 return NO_ERROR;
1356}
1357
Dan Stoza812ed062015-06-02 15:45:22 -07001358status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1359 ATRACE_CALL();
1360 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1361
1362 Mutex::Autolock lock(mCore->mMutex);
1363 mCore->mGenerationNumber = generationNumber;
1364 return NO_ERROR;
1365}
1366
Dan Stozac6f30bd2015-06-08 09:32:50 -07001367String8 BufferQueueProducer::getConsumerName() const {
1368 ATRACE_CALL();
1369 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1370 return mConsumerName;
1371}
1372
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001373status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001374 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001375 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001376
1377 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001378 if (!sharedBufferMode) {
1379 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001380 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001381 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001382 return NO_ERROR;
1383}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001384
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001385status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1386 ATRACE_CALL();
1387 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1388
1389 Mutex::Autolock lock(mCore->mMutex);
1390
1391 mCore->mAutoRefresh = autoRefresh;
1392 return NO_ERROR;
1393}
1394
Dan Stoza127fc632015-06-30 13:43:32 -07001395status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1396 ATRACE_CALL();
1397 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1398
Pablo Ceballos981066c2016-02-18 12:54:37 -08001399 Mutex::Autolock lock(mCore->mMutex);
1400 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1401 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1402 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1403 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1404 "available slots. Delta = %d", delta);
1405 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001406 }
1407
Pablo Ceballos981066c2016-02-18 12:54:37 -08001408 mDequeueTimeout = timeout;
1409 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001410
Pablo Ceballos981066c2016-02-18 12:54:37 -08001411 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001412 return NO_ERROR;
1413}
1414
Dan Stoza50101d02016-04-07 16:53:23 -07001415status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001416 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001417 ATRACE_CALL();
1418 BQ_LOGV("getLastQueuedBuffer");
1419
1420 Mutex::Autolock lock(mCore->mMutex);
1421 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1422 *outBuffer = nullptr;
1423 *outFence = Fence::NO_FENCE;
1424 return NO_ERROR;
1425 }
1426
1427 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1428 *outFence = mLastQueueBufferFence;
1429
John Reck1a61da52016-04-28 13:18:15 -07001430 // Currently only SurfaceFlinger internally ever changes
1431 // GLConsumer's filtering mode, so we just use 'true' here as
1432 // this is slightly specialized for the current client of this API,
1433 // which does want filtering.
1434 GLConsumer::computeTransformMatrix(outTransformMatrix,
1435 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1436 mLastQueuedTransform, true /* filter */);
1437
Dan Stoza50101d02016-04-07 16:53:23 -07001438 return NO_ERROR;
1439}
1440
Pablo Ceballosce796e72016-02-04 19:10:51 -08001441bool BufferQueueProducer::getFrameTimestamps(uint64_t frameNumber,
1442 FrameTimestamps* outTimestamps) const {
1443 ATRACE_CALL();
1444 BQ_LOGV("getFrameTimestamps, %" PRIu64, frameNumber);
1445 sp<IConsumerListener> listener;
1446
1447 {
1448 Mutex::Autolock lock(mCore->mMutex);
1449 listener = mCore->mConsumerListener;
1450 }
1451 if (listener != NULL) {
1452 return listener->getFrameTimestamps(frameNumber, outTimestamps);
1453 }
1454 return false;
1455}
1456
Dan Stoza289ade12014-02-28 11:17:17 -08001457void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1458 // If we're here, it means that a producer we were connected to died.
1459 // We're guaranteed that we are still connected to it because we remove
1460 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1461 // without synchronization here.
1462 int api = mCore->mConnectedApi;
1463 disconnect(api);
1464}
1465
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001466status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1467 BQ_LOGV("getUniqueId");
1468
1469 *outId = mCore->mUniqueId;
1470 return NO_ERROR;
1471}
1472
Dan Stoza289ade12014-02-28 11:17:17 -08001473} // namespace android