blob: e51144d79f5e5a8294bcdb0ebee3cdd0a25464f8 [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
911 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stozae77c7662016-05-13 11:37:28 -0700912 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800913
914 // Take a ticket for the callback functions
915 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700916
Pablo Ceballos9e314332016-01-12 13:49:19 -0800917 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800918 } // Autolock scope
919
Irvel468051e2016-06-13 16:44:44 -0700920 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
921 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
922 // there will be no Binder call
923 if (!mConsumerIsSurfaceFlinger) {
924 item.mGraphicBuffer.clear();
925 }
926
927 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800928 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
929
930 // Call back without the main BufferQueue lock held, but with the callback
931 // lock held so we can ensure that callbacks occur in order
932 {
933 Mutex::Autolock lock(mCallbackMutex);
934 while (callbackTicket != mCurrentCallbackTicket) {
935 mCallbackCondition.wait(mCallbackMutex);
936 }
937
938 if (frameAvailableListener != NULL) {
939 frameAvailableListener->onFrameAvailable(item);
940 } else if (frameReplacedListener != NULL) {
941 frameReplacedListener->onFrameReplaced(item);
942 }
943
944 ++mCurrentCallbackTicket;
945 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800946 }
947
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000948 // Wait without lock held
949 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
950 // Waiting here allows for two full buffers to be queued but not a
951 // third. In the event that frames take varying time, this makes a
952 // small trade-off in favor of latency rather than throughput.
953 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000954 }
Dan Stoza50101d02016-04-07 16:53:23 -0700955 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700956 mLastQueuedCrop = item.mCrop;
957 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000958
Dan Stoza289ade12014-02-28 11:17:17 -0800959 return NO_ERROR;
960}
961
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700962status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800963 ATRACE_CALL();
964 BQ_LOGV("cancelBuffer: slot %d", slot);
965 Mutex::Autolock lock(mCore->mMutex);
966
967 if (mCore->mIsAbandoned) {
968 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700969 return NO_INIT;
970 }
971
972 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
973 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
974 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800975 }
976
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700977 if (mCore->mSharedBufferMode) {
978 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700979 return BAD_VALUE;
980 }
981
Dan Stoza3e96f192014-03-03 10:16:19 -0800982 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800983 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800984 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700985 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700986 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800987 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700988 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700989 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800990 } else if (fence == NULL) {
991 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700992 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800993 }
994
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700995 mSlots[slot].mBufferState.cancel();
996
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700997 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700998 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700999 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001000 mSlots[slot].mBufferState.mShared = false;
1001 }
1002
1003 // Don't put the shared buffer on the free list.
1004 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001005 mCore->mActiveBuffers.erase(slot);
1006 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001008
Dan Stoza289ade12014-02-28 11:17:17 -08001009 mSlots[slot].mFence = fence;
1010 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001011 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001012
1013 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001014}
1015
1016int BufferQueueProducer::query(int what, int *outValue) {
1017 ATRACE_CALL();
1018 Mutex::Autolock lock(mCore->mMutex);
1019
1020 if (outValue == NULL) {
1021 BQ_LOGE("query: outValue was NULL");
1022 return BAD_VALUE;
1023 }
1024
1025 if (mCore->mIsAbandoned) {
1026 BQ_LOGE("query: BufferQueue has been abandoned");
1027 return NO_INIT;
1028 }
1029
1030 int value;
1031 switch (what) {
1032 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001033 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001034 break;
1035 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001036 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001037 break;
1038 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001039 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001040 break;
1041 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001042 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001043 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001044 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001045 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001046 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001047 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1048 value = (mCore->mQueue.size() > 1);
1049 break;
1050 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001051 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001052 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001053 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1054 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1055 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001056 case NATIVE_WINDOW_BUFFER_AGE:
1057 if (mCore->mBufferAge > INT32_MAX) {
1058 value = 0;
1059 } else {
1060 value = static_cast<int32_t>(mCore->mBufferAge);
1061 }
1062 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001063 default:
1064 return BAD_VALUE;
1065 }
1066
1067 BQ_LOGV("query: %d? %d", what, value);
1068 *outValue = value;
1069 return NO_ERROR;
1070}
1071
Dan Stozaf0eaf252014-03-21 13:05:51 -07001072status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001073 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1074 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001075 Mutex::Autolock lock(mCore->mMutex);
1076 mConsumerName = mCore->mConsumerName;
1077 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1078 producerControlledByApp ? "true" : "false");
1079
1080 if (mCore->mIsAbandoned) {
1081 BQ_LOGE("connect: BufferQueue has been abandoned");
1082 return NO_INIT;
1083 }
1084
1085 if (mCore->mConsumerListener == NULL) {
1086 BQ_LOGE("connect: BufferQueue has no consumer");
1087 return NO_INIT;
1088 }
1089
1090 if (output == NULL) {
1091 BQ_LOGE("connect: output was NULL");
1092 return BAD_VALUE;
1093 }
1094
1095 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1096 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1097 mCore->mConnectedApi, api);
1098 return BAD_VALUE;
1099 }
1100
1101 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1102 mDequeueTimeout < 0 ?
1103 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1104 mCore->mMaxBufferCount) -
1105 mCore->getMaxBufferCountLocked();
1106 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1107 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1108 "slots. Delta = %d", delta);
1109 return BAD_VALUE;
1110 }
1111
Dan Stoza289ade12014-02-28 11:17:17 -08001112 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001113 switch (api) {
1114 case NATIVE_WINDOW_API_EGL:
1115 case NATIVE_WINDOW_API_CPU:
1116 case NATIVE_WINDOW_API_MEDIA:
1117 case NATIVE_WINDOW_API_CAMERA:
1118 mCore->mConnectedApi = api;
1119 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1120 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001121 static_cast<uint32_t>(mCore->mQueue.size()),
1122 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -08001123
Pablo Ceballos981066c2016-02-18 12:54:37 -08001124 // Set up a death notification so that we can disconnect
1125 // automatically if the remote producer dies
1126 if (listener != NULL &&
1127 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1128 status = IInterface::asBinder(listener)->linkToDeath(
1129 static_cast<IBinder::DeathRecipient*>(this));
1130 if (status != NO_ERROR) {
1131 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1132 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001133 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001134 }
1135 mCore->mConnectedProducerListener = listener;
1136 break;
1137 default:
1138 BQ_LOGE("connect: unknown API %d", api);
1139 status = BAD_VALUE;
1140 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001141 }
Robert Carr97b9c862016-09-08 13:54:35 -07001142 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001143 mCore->mBufferHasBeenQueued = false;
1144 mCore->mDequeueBufferCannotBlock = false;
1145 if (mDequeueTimeout < 0) {
1146 mCore->mDequeueBufferCannotBlock =
1147 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001148 }
Dan Stoza289ade12014-02-28 11:17:17 -08001149
Pablo Ceballos981066c2016-02-18 12:54:37 -08001150 mCore->mAllowAllocation = true;
1151 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001152 return status;
1153}
1154
Robert Carr97b9c862016-09-08 13:54:35 -07001155status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001156 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001157 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001158
1159 int status = NO_ERROR;
1160 sp<IConsumerListener> listener;
1161 { // Autolock scope
1162 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001163
1164 if (mode == DisconnectMode::AllLocal) {
1165 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1166 return NO_ERROR;
1167 }
1168 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1169 }
1170
Antoine Labour78014f32014-07-15 21:17:03 -07001171 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001172
1173 if (mCore->mIsAbandoned) {
1174 // It's not really an error to disconnect after the surface has
1175 // been abandoned; it should just be a no-op.
1176 return NO_ERROR;
1177 }
1178
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001179 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1180 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001181 // If we're asked to disconnect the currently connected api but
1182 // nobody is connected, it's not really an error.
1183 if (api == BufferQueueCore::NO_CONNECTED_API) {
1184 return NO_ERROR;
1185 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001186 }
1187
Dan Stoza289ade12014-02-28 11:17:17 -08001188 switch (api) {
1189 case NATIVE_WINDOW_API_EGL:
1190 case NATIVE_WINDOW_API_CPU:
1191 case NATIVE_WINDOW_API_MEDIA:
1192 case NATIVE_WINDOW_API_CAMERA:
1193 if (mCore->mConnectedApi == api) {
1194 mCore->freeAllBuffersLocked();
1195
1196 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001197 if (mCore->mConnectedProducerListener != NULL) {
1198 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001199 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001200 // This can fail if we're here because of the death
1201 // notification, but we just ignore it
1202 token->unlinkToDeath(
1203 static_cast<IBinder::DeathRecipient*>(this));
1204 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001205 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001206 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001207 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001208 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001209 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001210 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001211 mCore->mDequeueCondition.broadcast();
1212 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001213 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001214 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001215 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001216 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001217 }
1218 break;
1219 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001220 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001221 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001222 break;
1223 }
1224 } // Autolock scope
1225
1226 // Call back without lock held
1227 if (listener != NULL) {
1228 listener->onBuffersReleased();
1229 }
1230
1231 return status;
1232}
1233
Jesse Hall399184a2014-03-03 15:42:54 -08001234status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001235 sp<IConsumerListener> listener;
1236 { // Autolock scope
1237 Mutex::Autolock _l(mCore->mMutex);
1238 mCore->mSidebandStream = stream;
1239 listener = mCore->mConsumerListener;
1240 } // Autolock scope
1241
1242 if (listener != NULL) {
1243 listener->onSidebandStreamChanged();
1244 }
Jesse Hall399184a2014-03-03 15:42:54 -08001245 return NO_ERROR;
1246}
1247
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001248void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1249 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001250 ATRACE_CALL();
1251 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001252 size_t newBufferCount = 0;
1253 uint32_t allocWidth = 0;
1254 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001255 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001256 uint32_t allocUsage = 0;
1257 { // Autolock scope
1258 Mutex::Autolock lock(mCore->mMutex);
1259 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001260
Dan Stoza9de72932015-04-16 17:28:43 -07001261 if (!mCore->mAllowAllocation) {
1262 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1263 "BufferQueue");
1264 return;
1265 }
1266
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001267 newBufferCount = mCore->mFreeSlots.size();
1268 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001269 return;
1270 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001271
Antoine Labour78014f32014-07-15 21:17:03 -07001272 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1273 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1274 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1275 allocUsage = usage | mCore->mConsumerUsageBits;
1276
1277 mCore->mIsAllocating = true;
1278 } // Autolock scope
1279
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001280 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001281 for (size_t i = 0; i < newBufferCount; ++i) {
1282 status_t result = NO_ERROR;
1283 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza024e9312016-08-24 12:17:29 -07001284 allocWidth, allocHeight, allocFormat, allocUsage,
1285 {mConsumerName.string(), mConsumerName.size()}, &result));
Antoine Labour78014f32014-07-15 21:17:03 -07001286 if (result != NO_ERROR) {
1287 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1288 " %u, usage %u)", width, height, format, usage);
1289 Mutex::Autolock lock(mCore->mMutex);
1290 mCore->mIsAllocating = false;
1291 mCore->mIsAllocatingCondition.broadcast();
1292 return;
1293 }
1294 buffers.push_back(graphicBuffer);
1295 }
1296
1297 { // Autolock scope
1298 Mutex::Autolock lock(mCore->mMutex);
1299 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1300 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001301 PixelFormat checkFormat = format != 0 ?
1302 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001303 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1304 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1305 checkFormat != allocFormat || checkUsage != allocUsage) {
1306 // Something changed while we released the lock. Retry.
1307 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1308 mCore->mIsAllocating = false;
1309 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001310 continue;
1311 }
1312
Antoine Labour78014f32014-07-15 21:17:03 -07001313 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001314 if (mCore->mFreeSlots.empty()) {
1315 BQ_LOGV("allocateBuffers: a slot was occupied while "
1316 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001317 continue;
1318 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001319 auto slot = mCore->mFreeSlots.begin();
1320 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1321 mSlots[*slot].mGraphicBuffer = buffers[i];
1322 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001323
1324 // freeBufferLocked puts this slot on the free slots list. Since
1325 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001326 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001327
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001328 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1329 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001330
1331 // Make sure the erase is done after all uses of the slot
1332 // iterator since it will be invalid after this point.
1333 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001334 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001335
Antoine Labour78014f32014-07-15 21:17:03 -07001336 mCore->mIsAllocating = false;
1337 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001338 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001339 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001340 }
1341}
1342
Dan Stoza9de72932015-04-16 17:28:43 -07001343status_t BufferQueueProducer::allowAllocation(bool allow) {
1344 ATRACE_CALL();
1345 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1346
1347 Mutex::Autolock lock(mCore->mMutex);
1348 mCore->mAllowAllocation = allow;
1349 return NO_ERROR;
1350}
1351
Dan Stoza812ed062015-06-02 15:45:22 -07001352status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1353 ATRACE_CALL();
1354 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1355
1356 Mutex::Autolock lock(mCore->mMutex);
1357 mCore->mGenerationNumber = generationNumber;
1358 return NO_ERROR;
1359}
1360
Dan Stozac6f30bd2015-06-08 09:32:50 -07001361String8 BufferQueueProducer::getConsumerName() const {
1362 ATRACE_CALL();
1363 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1364 return mConsumerName;
1365}
1366
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001367status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001368 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001369 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001370
1371 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001372 if (!sharedBufferMode) {
1373 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001374 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001375 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001376 return NO_ERROR;
1377}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001378
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001379status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1380 ATRACE_CALL();
1381 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1382
1383 Mutex::Autolock lock(mCore->mMutex);
1384
1385 mCore->mAutoRefresh = autoRefresh;
1386 return NO_ERROR;
1387}
1388
Dan Stoza127fc632015-06-30 13:43:32 -07001389status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1390 ATRACE_CALL();
1391 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1392
Pablo Ceballos981066c2016-02-18 12:54:37 -08001393 Mutex::Autolock lock(mCore->mMutex);
1394 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1395 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1396 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1397 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1398 "available slots. Delta = %d", delta);
1399 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001400 }
1401
Pablo Ceballos981066c2016-02-18 12:54:37 -08001402 mDequeueTimeout = timeout;
1403 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001404
Pablo Ceballos981066c2016-02-18 12:54:37 -08001405 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001406 return NO_ERROR;
1407}
1408
Dan Stoza50101d02016-04-07 16:53:23 -07001409status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001410 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001411 ATRACE_CALL();
1412 BQ_LOGV("getLastQueuedBuffer");
1413
1414 Mutex::Autolock lock(mCore->mMutex);
1415 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1416 *outBuffer = nullptr;
1417 *outFence = Fence::NO_FENCE;
1418 return NO_ERROR;
1419 }
1420
1421 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1422 *outFence = mLastQueueBufferFence;
1423
John Reck1a61da52016-04-28 13:18:15 -07001424 // Currently only SurfaceFlinger internally ever changes
1425 // GLConsumer's filtering mode, so we just use 'true' here as
1426 // this is slightly specialized for the current client of this API,
1427 // which does want filtering.
1428 GLConsumer::computeTransformMatrix(outTransformMatrix,
1429 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1430 mLastQueuedTransform, true /* filter */);
1431
Dan Stoza50101d02016-04-07 16:53:23 -07001432 return NO_ERROR;
1433}
1434
Pablo Ceballosce796e72016-02-04 19:10:51 -08001435bool BufferQueueProducer::getFrameTimestamps(uint64_t frameNumber,
1436 FrameTimestamps* outTimestamps) const {
1437 ATRACE_CALL();
1438 BQ_LOGV("getFrameTimestamps, %" PRIu64, frameNumber);
1439 sp<IConsumerListener> listener;
1440
1441 {
1442 Mutex::Autolock lock(mCore->mMutex);
1443 listener = mCore->mConsumerListener;
1444 }
1445 if (listener != NULL) {
1446 return listener->getFrameTimestamps(frameNumber, outTimestamps);
1447 }
1448 return false;
1449}
1450
Dan Stoza289ade12014-02-28 11:17:17 -08001451void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1452 // If we're here, it means that a producer we were connected to died.
1453 // We're guaranteed that we are still connected to it because we remove
1454 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1455 // without synchronization here.
1456 int api = mCore->mConnectedApi;
1457 disconnect(api);
1458}
1459
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001460status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1461 BQ_LOGV("getUniqueId");
1462
1463 *outId = mCore->mUniqueId;
1464 return NO_ERROR;
1465}
1466
Dan Stoza289ade12014-02-28 11:17:17 -08001467} // namespace android