blob: 69a408c075052b769f1afb52de6fc7cc6da7e990 [file] [log] [blame]
Dan Stoza289ade12014-02-28 19:17:171/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 21:32:0417#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 18:16:1919#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 21:49:1923#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 19:17:1729#define EGL_EGLEXT_PROTOTYPES
30
31#include <gui/BufferItem.h>
32#include <gui/BufferQueueCore.h>
33#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 20:18:1534#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 19:17:1735#include <gui/IConsumerListener.h>
36#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 20:05:5137#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 19:17:1738
39#include <utils/Log.h>
40#include <utils/Trace.h>
41
42namespace android {
43
44BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
45 mCore(core),
46 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 22:51:5547 mConsumerName(),
Eric Penner99a0afb2014-09-30 18:28:3048 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 19:37:4649 mLastQueueBufferFence(Fence::NO_FENCE),
50 mCallbackMutex(),
51 mNextCallbackTicket(0),
52 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 20:43:3253 mCallbackCondition(),
54 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 19:17:1755
56BufferQueueProducer::~BufferQueueProducer() {}
57
58status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
59 ATRACE_CALL();
60 BQ_LOGV("requestBuffer: slot %d", slot);
61 Mutex::Autolock lock(mCore->mMutex);
62
63 if (mCore->mIsAbandoned) {
64 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
65 return NO_INIT;
66 }
67
Pablo Ceballos583b1b32015-09-04 01:23:5268 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
69 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
70 return NO_INIT;
71 }
72
Dan Stoza3e96f192014-03-03 18:16:1973 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:1774 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 18:16:1975 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 19:17:1776 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:4577 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:1778 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:4579 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 19:17:1780 return BAD_VALUE;
81 }
82
83 mSlots[slot].mRequestBufferCalled = true;
84 *buf = mSlots[slot].mGraphicBuffer;
85 return NO_ERROR;
86}
87
Pablo Ceballosfa455352015-08-13 00:47:4788status_t BufferQueueProducer::setMaxDequeuedBufferCount(
89 int maxDequeuedBuffers) {
90 ATRACE_CALL();
91 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
92 maxDequeuedBuffers);
93
Pablo Ceballos981066c2016-02-18 20:54:3794 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-13 00:47:4795 { // Autolock scope
96 Mutex::Autolock lock(mCore->mMutex);
97 mCore->waitWhileAllocatingLocked();
98
99 if (mCore->mIsAbandoned) {
100 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
101 "abandoned");
102 return NO_INIT;
103 }
104
Pablo Ceballos245cc5b2016-04-19 18:33:00105 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
106 return NO_ERROR;
107 }
108
Pablo Ceballos72daab62015-12-08 00:38:43109 // The new maxDequeuedBuffer count should not be violated by the number
110 // of currently dequeued buffers
111 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 20:15:22112 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 22:05:45113 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-08 00:38:43114 dequeuedCount++;
Pablo Ceballosfa455352015-08-13 00:47:47115 }
116 }
Pablo Ceballos72daab62015-12-08 00:38:43117 if (dequeuedCount > maxDequeuedBuffers) {
118 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
119 "count (%d) exceeds the current dequeued buffer count (%d)",
120 maxDequeuedBuffers, dequeuedCount);
121 return BAD_VALUE;
122 }
Pablo Ceballosfa455352015-08-13 00:47:47123
Pablo Ceballos567dbbb2015-08-27 01:59:08124 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-13 00:47:47125 bufferCount += maxDequeuedBuffers;
126
127 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
128 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
129 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
130 return BAD_VALUE;
131 }
132
Pablo Ceballos567dbbb2015-08-27 01:59:08133 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-13 00:47:47134 if (bufferCount < minBufferSlots) {
135 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
136 "less than minimum %d", bufferCount, minBufferSlots);
137 return BAD_VALUE;
138 }
139
Pablo Ceballos19e3e062015-08-19 23:16:06140 if (bufferCount > mCore->mMaxBufferCount) {
141 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-27 01:59:08142 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
143 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
144 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
145 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 23:16:06146 return BAD_VALUE;
147 }
148
Pablo Ceballos72daab62015-12-08 00:38:43149 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 20:54:37150 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 20:15:22151 return BAD_VALUE;
152 }
Pablo Ceballosfa455352015-08-13 00:47:47153 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 21:49:19154 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-08 00:38:43155 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 20:54:37156 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-08 00:38:43157 }
Pablo Ceballosfa455352015-08-13 00:47:47158 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-13 00:47:47159 } // Autolock scope
160
161 // Call back without lock held
Pablo Ceballos981066c2016-02-18 20:54:37162 if (listener != NULL) {
163 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-13 00:47:47164 }
165
166 return NO_ERROR;
167}
168
169status_t BufferQueueProducer::setAsyncMode(bool async) {
170 ATRACE_CALL();
171 BQ_LOGV("setAsyncMode: async = %d", async);
172
Pablo Ceballos981066c2016-02-18 20:54:37173 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-13 00:47:47174 { // Autolock scope
175 Mutex::Autolock lock(mCore->mMutex);
176 mCore->waitWhileAllocatingLocked();
177
178 if (mCore->mIsAbandoned) {
179 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
180 return NO_INIT;
181 }
182
Pablo Ceballos245cc5b2016-04-19 18:33:00183 if (async == mCore->mAsyncMode) {
184 return NO_ERROR;
185 }
186
Pablo Ceballos19e3e062015-08-19 23:16:06187 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-27 01:59:08188 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
189 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 23:16:06190 BQ_LOGE("setAsyncMode(%d): this call would cause the "
191 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-27 01:59:08192 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
193 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
194 mCore->mMaxDequeuedBufferCount,
195 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 23:16:06196 return BAD_VALUE;
197 }
198
Pablo Ceballos23b4abe2016-01-08 20:15:22199 int delta = mCore->getMaxBufferCountLocked(async,
200 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
201 - mCore->getMaxBufferCountLocked();
202
Pablo Ceballos981066c2016-02-18 20:54:37203 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 20:15:22204 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
205 "available slots. Delta = %d", delta);
206 return BAD_VALUE;
207 }
Pablo Ceballosfa455352015-08-13 00:47:47208 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 21:49:19209 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-13 00:47:47210 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 18:33:00211 if (delta < 0) {
212 listener = mCore->mConsumerListener;
213 }
Pablo Ceballosfa455352015-08-13 00:47:47214 } // Autolock scope
215
216 // Call back without lock held
Pablo Ceballos981066c2016-02-18 20:54:37217 if (listener != NULL) {
218 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-13 00:47:47219 }
220 return NO_ERROR;
221}
222
Dan Stoza5ecfb682016-01-05 01:01:02223int BufferQueueProducer::getFreeBufferLocked() const {
224 if (mCore->mFreeBuffers.empty()) {
225 return BufferQueueCore::INVALID_BUFFER_SLOT;
226 }
Pablo Ceballos23b4abe2016-01-08 20:15:22227 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-05 01:01:02228 mCore->mFreeBuffers.pop_front();
229 return slot;
230}
231
Pablo Ceballos23b4abe2016-01-08 20:15:22232int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-05 01:01:02233 if (mCore->mFreeSlots.empty()) {
234 return BufferQueueCore::INVALID_BUFFER_SLOT;
235 }
Pablo Ceballosdce5c552016-02-10 23:43:22236 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 20:15:22237 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 23:43:22238 return slot;
Dan Stoza5ecfb682016-01-05 01:01:02239}
240
241status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 20:15:22242 int* found) const {
Dan Stoza5ecfb682016-01-05 01:01:02243 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
244 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 23:14:33245 bool tryAgain = true;
246 while (tryAgain) {
247 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-05 01:01:02248 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 23:14:33249 return NO_INIT;
250 }
251
Dan Stoza9f3053d2014-03-06 23:14:33252 int dequeuedCount = 0;
253 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 20:15:22254 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 22:05:45255 if (mSlots[s].mBufferState.isDequeued()) {
256 ++dequeuedCount;
257 }
258 if (mSlots[s].mBufferState.isAcquired()) {
259 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 23:14:33260 }
261 }
262
Pablo Ceballose5b755a2015-08-13 23:18:19263 // Producers are not allowed to dequeue more than
264 // mMaxDequeuedBufferCount buffers.
265 // This check is only done if a buffer has already been queued
266 if (mCore->mBufferHasBeenQueued &&
267 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
268 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-05 01:01:02269 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 23:14:33270 return INVALID_OPERATION;
271 }
272
Dan Stoza0de7ea72015-04-23 20:20:51273 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
274
Dan Stozaae3c3682014-04-18 22:43:35275 // If we disconnect and reconnect quickly, we can be in a state where
276 // our slots are empty but we have many buffers in the queue. This can
277 // cause us to run out of memory if we outrun the consumer. Wait here if
278 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 20:15:22279 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 21:32:04280 bool tooManyBuffers = mCore->mQueue.size()
281 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 22:43:35282 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-05 01:01:02283 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 22:43:35284 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 20:20:51285 } else {
Pablo Ceballos3559fbf2016-03-17 22:50:23286 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 22:05:45287 // return it.
Pablo Ceballos3559fbf2016-03-17 22:50:23288 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 22:05:45289 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 22:50:23290 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-05 01:01:02291 } else {
292 if (caller == FreeSlotCaller::Dequeue) {
293 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 20:15:22294 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-05 01:01:02295 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
296 *found = slot;
297 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 20:15:22298 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-05 01:01:02299 }
300 } else {
301 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 20:15:22302 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-05 01:01:02303 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
304 *found = slot;
305 } else {
306 *found = getFreeBufferLocked();
307 }
Dan Stoza0de7ea72015-04-23 20:20:51308 }
309 }
Dan Stozaae3c3682014-04-18 22:43:35310 }
311
312 // If no buffer is found, or if the queue has too many buffers
313 // outstanding, wait for a buffer to be acquired or released, or for the
314 // max buffer count to change.
315 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
316 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 23:14:33317 if (tryAgain) {
318 // Return an error if we're in non-blocking mode (producer and
319 // consumer are controlled by the application).
320 // However, the consumer is allowed to briefly acquire an extra
321 // buffer (which could cause us to have to wait here), which is
322 // okay, since it is only used to implement an atomic acquire +
323 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-13 00:47:47324 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 23:14:33325 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
326 return WOULD_BLOCK;
327 }
Dan Stoza127fc632015-06-30 20:43:32328 if (mDequeueTimeout >= 0) {
329 status_t result = mCore->mDequeueCondition.waitRelative(
330 mCore->mMutex, mDequeueTimeout);
331 if (result == TIMED_OUT) {
332 return result;
333 }
334 } else {
335 mCore->mDequeueCondition.wait(mCore->mMutex);
336 }
Dan Stoza9f3053d2014-03-06 23:14:33337 }
338 } // while (tryAgain)
339
340 return NO_ERROR;
341}
342
Dan Stoza289ade12014-02-28 19:17:17343status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-27 01:59:08344 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
345 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 19:17:17346 ATRACE_CALL();
347 { // Autolock scope
348 Mutex::Autolock lock(mCore->mMutex);
349 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-04 01:23:52350
351 if (mCore->mIsAbandoned) {
352 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
353 return NO_INIT;
354 }
355
356 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
357 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
358 return NO_INIT;
359 }
Dan Stoza289ade12014-02-28 19:17:17360 } // Autolock scope
361
Pablo Ceballos567dbbb2015-08-27 01:59:08362 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
363 format, usage);
Dan Stoza289ade12014-02-28 19:17:17364
365 if ((width && !height) || (!width && height)) {
366 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
367 return BAD_VALUE;
368 }
369
370 status_t returnFlags = NO_ERROR;
371 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
372 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 23:14:33373 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 19:17:17374
375 { // Autolock scope
376 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-16 04:17:03377 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 19:17:17378
379 if (format == 0) {
380 format = mCore->mDefaultBufferFormat;
381 }
382
383 // Enable the usage bits the consumer requested
384 usage |= mCore->mConsumerUsageBits;
385
Dan Stoza9de72932015-04-17 00:28:43386 const bool useDefaultSize = !width && !height;
387 if (useDefaultSize) {
388 width = mCore->mDefaultWidth;
389 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 23:14:33390 }
Dan Stoza289ade12014-02-28 19:17:17391
Pablo Ceballos981066c2016-02-18 20:54:37392 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-17 00:28:43393 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-05 01:01:02394 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 20:15:22395 &found);
Dan Stoza9de72932015-04-17 00:28:43396 if (status != NO_ERROR) {
397 return status;
398 }
399
400 // This should not happen
401 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
402 BQ_LOGE("dequeueBuffer: no available buffer slots");
403 return -EBUSY;
404 }
405
406 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
407
408 // If we are not allowed to allocate new buffers,
409 // waitForFreeSlotThenRelock must have returned a slot containing a
410 // buffer. If this buffer would require reallocation to meet the
411 // requested attributes, we free it and attempt to get another one.
412 if (!mCore->mAllowAllocation) {
413 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 22:50:23414 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 22:05:45415 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
416 "buffer");
417 return BAD_VALUE;
418 }
Pablo Ceballos23b4abe2016-01-08 20:15:22419 mCore->mFreeSlots.insert(found);
420 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-17 00:28:43421 found = BufferItem::INVALID_BUFFER_SLOT;
422 continue;
423 }
424 }
Dan Stoza289ade12014-02-28 19:17:17425 }
426
Pablo Ceballos23b4abe2016-01-08 20:15:22427 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 22:50:23428 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 20:15:22429 buffer->needsReallocation(width, height, format, usage)) {
430 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
431 "buffer");
432
433 return BAD_VALUE;
434 }
435
Pablo Ceballos3559fbf2016-03-17 22:50:23436 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 20:15:22437 mCore->mActiveBuffers.insert(found);
438 }
Dan Stoza289ade12014-02-28 19:17:17439 *outSlot = found;
440 ATRACE_BUFFER_INDEX(found);
441
Pablo Ceballos23b4abe2016-01-08 20:15:22442 attachedByConsumer = mSlots[found].mNeedsReallocation;
443 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 23:14:33444
Pablo Ceballosccdfd602015-10-07 22:05:45445 mSlots[found].mBufferState.dequeue();
446
Dan Stoza289ade12014-02-28 19:17:17447 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-17 00:28:43448 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 19:17:17449 {
450 mSlots[found].mAcquireCalled = false;
451 mSlots[found].mGraphicBuffer = NULL;
452 mSlots[found].mRequestBufferCalled = false;
453 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
454 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
455 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-26 00:49:08456 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 22:05:45457 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 19:17:17458
459 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-26 00:49:08460 } else {
461 // We add 1 because that will be the frame number when this buffer
462 // is queued
463 mCore->mBufferAge =
464 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 19:17:17465 }
466
Dan Stoza800b41a2015-04-28 21:20:04467 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
468 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-26 00:49:08469
Dan Stoza289ade12014-02-28 19:17:17470 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
471 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
472 "slot=%d w=%d h=%d format=%u",
473 found, buffer->width, buffer->height, buffer->format);
474 }
475
476 eglDisplay = mSlots[found].mEglDisplay;
477 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 22:03:21478 // Don't return a fence in shared buffer mode, except for the first
479 // frame.
480 *outFence = (mCore->mSharedBufferMode &&
481 mCore->mSharedBufferSlot == found) ?
482 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 19:17:17483 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
484 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 22:03:21485
486 // If shared buffer mode has just been enabled, cache the slot of the
487 // first buffer that is dequeued and mark it as the shared buffer.
488 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
489 BufferQueueCore::INVALID_BUFFER_SLOT) {
490 mCore->mSharedBufferSlot = found;
491 mSlots[found].mBufferState.mShared = true;
492 }
Dan Stoza289ade12014-02-28 19:17:17493 } // Autolock scope
494
495 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
496 status_t error;
Dan Stoza29a3e902014-06-20 20:13:57497 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 19:17:17498 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 18:24:03499 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 19:17:17500 { // Autolock scope
501 Mutex::Autolock lock(mCore->mMutex);
502
Pablo Ceballosccdfd602015-10-07 22:05:45503 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
504 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
505 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
506 }
507
508 mCore->mIsAllocating = false;
509 mCore->mIsAllocatingCondition.broadcast();
510
511 if (graphicBuffer == NULL) {
512 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
513 return error;
514 }
515
Dan Stoza289ade12014-02-28 19:17:17516 if (mCore->mIsAbandoned) {
517 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
518 return NO_INIT;
519 }
Pablo Ceballos23b4abe2016-01-08 20:15:22520
Pablo Ceballos9e314332016-01-12 21:49:19521 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:17522 } // Autolock scope
523 }
524
Dan Stoza9f3053d2014-03-06 23:14:33525 if (attachedByConsumer) {
526 returnFlags |= BUFFER_NEEDS_REALLOCATION;
527 }
528
Dan Stoza289ade12014-02-28 19:17:17529 if (eglFence != EGL_NO_SYNC_KHR) {
530 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
531 1000000000);
532 // If something goes wrong, log the error, but return the buffer without
533 // synchronizing access to it. It's too late at this point to abort the
534 // dequeue operation.
535 if (result == EGL_FALSE) {
536 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
537 eglGetError());
538 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
539 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
540 }
541 eglDestroySyncKHR(eglDisplay, eglFence);
542 }
543
Mark Salyzyn8f515ce2014-06-09 21:32:04544 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
545 *outSlot,
Dan Stoza289ade12014-02-28 19:17:17546 mSlots[*outSlot].mFrameNumber,
547 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
548
549 return returnFlags;
550}
551
Dan Stoza9f3053d2014-03-06 23:14:33552status_t BufferQueueProducer::detachBuffer(int slot) {
553 ATRACE_CALL();
554 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 22:05:45555 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos981066c2016-02-18 20:54:37556 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 23:14:33557
Pablo Ceballos981066c2016-02-18 20:54:37558 if (mCore->mIsAbandoned) {
559 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
560 return NO_INIT;
Dan Stoza9f3053d2014-03-06 23:14:33561 }
562
Pablo Ceballos981066c2016-02-18 20:54:37563 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
564 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
565 return NO_INIT;
Pablo Ceballos583b1b32015-09-04 01:23:52566 }
Pablo Ceballos981066c2016-02-18 20:54:37567
Pablo Ceballos3559fbf2016-03-17 22:50:23568 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
569 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
Pablo Ceballos981066c2016-02-18 20:54:37570 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:45571 }
572
Pablo Ceballos981066c2016-02-18 20:54:37573 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
574 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
575 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
576 return BAD_VALUE;
577 } else if (!mSlots[slot].mBufferState.isDequeued()) {
578 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
579 "(state = %s)", slot, mSlots[slot].mBufferState.string());
580 return BAD_VALUE;
581 } else if (!mSlots[slot].mRequestBufferCalled) {
582 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
583 slot);
584 return BAD_VALUE;
585 }
586
587 mSlots[slot].mBufferState.detachProducer();
588 mCore->mActiveBuffers.erase(slot);
589 mCore->mFreeSlots.insert(slot);
590 mCore->clearBufferSlotLocked(slot);
591 mCore->mDequeueCondition.broadcast();
592 VALIDATE_CONSISTENCY();
593
Dan Stoza9f3053d2014-03-06 23:14:33594 return NO_ERROR;
595}
596
Dan Stozad9822a32014-03-28 22:25:31597status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
598 sp<Fence>* outFence) {
599 ATRACE_CALL();
600
601 if (outBuffer == NULL) {
602 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
603 return BAD_VALUE;
604 } else if (outFence == NULL) {
605 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
606 return BAD_VALUE;
607 }
608
Pablo Ceballos981066c2016-02-18 20:54:37609 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 22:25:31610
Pablo Ceballos981066c2016-02-18 20:54:37611 if (mCore->mIsAbandoned) {
612 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
613 return NO_INIT;
Dan Stozad9822a32014-03-28 22:25:31614 }
615
Pablo Ceballos981066c2016-02-18 20:54:37616 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
617 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
618 return NO_INIT;
Pablo Ceballos583b1b32015-09-04 01:23:52619 }
Pablo Ceballos981066c2016-02-18 20:54:37620
Pablo Ceballos3559fbf2016-03-17 22:50:23621 if (mCore->mSharedBufferMode) {
622 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
623 "mode");
Pablo Ceballos981066c2016-02-18 20:54:37624 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:45625 }
626
Pablo Ceballos981066c2016-02-18 20:54:37627 mCore->waitWhileAllocatingLocked();
628
629 if (mCore->mFreeBuffers.empty()) {
630 return NO_MEMORY;
631 }
632
633 int found = mCore->mFreeBuffers.front();
634 mCore->mFreeBuffers.remove(found);
635 mCore->mFreeSlots.insert(found);
636
637 BQ_LOGV("detachNextBuffer detached slot %d", found);
638
639 *outBuffer = mSlots[found].mGraphicBuffer;
640 *outFence = mSlots[found].mFence;
641 mCore->clearBufferSlotLocked(found);
642 VALIDATE_CONSISTENCY();
643
Dan Stozad9822a32014-03-28 22:25:31644 return NO_ERROR;
645}
646
Dan Stoza9f3053d2014-03-06 23:14:33647status_t BufferQueueProducer::attachBuffer(int* outSlot,
648 const sp<android::GraphicBuffer>& buffer) {
649 ATRACE_CALL();
650
651 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 22:05:45652 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 23:14:33653 return BAD_VALUE;
654 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 22:05:45655 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 23:14:33656 return BAD_VALUE;
657 }
658
659 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-04 01:23:52660
661 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 22:05:45662 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-04 01:23:52663 return NO_INIT;
664 }
665
666 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 22:05:45667 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-04 01:23:52668 return NO_INIT;
669 }
Dan Stoza9f3053d2014-03-06 23:14:33670
Pablo Ceballos3559fbf2016-03-17 22:50:23671 if (mCore->mSharedBufferMode) {
672 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 22:05:45673 return BAD_VALUE;
674 }
675
Dan Stoza812ed062015-06-02 22:45:22676 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
677 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
678 "[queue %u]", buffer->getGenerationNumber(),
679 mCore->mGenerationNumber);
680 return BAD_VALUE;
681 }
682
Pablo Ceballos583b1b32015-09-04 01:23:52683 mCore->waitWhileAllocatingLocked();
684
Dan Stoza9f3053d2014-03-06 23:14:33685 status_t returnFlags = NO_ERROR;
686 int found;
Pablo Ceballos23b4abe2016-01-08 20:15:22687 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 23:14:33688 if (status != NO_ERROR) {
689 return status;
690 }
691
692 // This should not happen
693 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 22:05:45694 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 23:14:33695 return -EBUSY;
696 }
697
698 *outSlot = found;
699 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 22:05:45700 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 23:14:33701 *outSlot, returnFlags);
702
703 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 22:05:45704 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 23:14:33705 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
706 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 22:03:46707 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 20:15:22708 mSlots[*outSlot].mAcquireCalled = false;
709 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 21:49:19710 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 20:20:51711
Dan Stoza9f3053d2014-03-06 23:14:33712 return returnFlags;
713}
714
Dan Stoza289ade12014-02-28 19:17:17715status_t BufferQueueProducer::queueBuffer(int slot,
716 const QueueBufferInput &input, QueueBufferOutput *output) {
717 ATRACE_CALL();
718 ATRACE_BUFFER_INDEX(slot);
719
720 int64_t timestamp;
721 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43722 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 21:47:20723 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 19:17:17724 int scalingMode;
725 uint32_t transform;
Ruben Brunk1681d952014-06-27 22:51:55726 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 19:17:17727 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43728 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-27 01:59:08729 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 23:23:42730 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 19:17:17731
732 if (fence == NULL) {
733 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 16:30:48734 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17735 }
736
737 switch (scalingMode) {
738 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
739 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
740 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
741 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
742 break;
743 default:
744 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 23:14:33745 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17746 }
747
Dan Stoza8dc55392014-11-04 19:37:46748 sp<IConsumerListener> frameAvailableListener;
749 sp<IConsumerListener> frameReplacedListener;
750 int callbackTicket = 0;
751 BufferItem item;
Dan Stoza289ade12014-02-28 19:17:17752 { // Autolock scope
753 Mutex::Autolock lock(mCore->mMutex);
754
755 if (mCore->mIsAbandoned) {
756 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
757 return NO_INIT;
758 }
759
Pablo Ceballos583b1b32015-09-04 01:23:52760 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
761 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
762 return NO_INIT;
763 }
764
Pablo Ceballos23b4abe2016-01-08 20:15:22765 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:17766 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 20:15:22767 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 23:14:33768 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:45769 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:17770 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:45771 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 23:14:33772 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17773 } else if (!mSlots[slot].mRequestBufferCalled) {
774 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
775 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 23:14:33776 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17777 }
778
Pablo Ceballos3559fbf2016-03-17 22:50:23779 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 23:02:19780 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 22:50:23781 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 23:02:19782 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 22:50:23783 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 23:02:19784 mSlots[slot].mBufferState.mShared = true;
785 }
786
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43787 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 21:32:04788 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43789 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 18:24:03790 crop.left, crop.top, crop.right, crop.bottom, transform,
791 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 19:17:17792
793 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
794 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 21:47:20795 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 19:17:17796 crop.intersect(bufferRect, &croppedRect);
797 if (croppedRect != crop) {
798 BQ_LOGE("queueBuffer: crop rect is not contained within the "
799 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 23:14:33800 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17801 }
802
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43803 // Override UNKNOWN dataspace with consumer default
804 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
805 dataSpace = mCore->mDefaultBufferDataSpace;
806 }
807
Dan Stoza289ade12014-02-28 19:17:17808 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 22:05:45809 mSlots[slot].mBufferState.queue();
810
Dan Stoza289ade12014-02-28 19:17:17811 ++mCore->mFrameCounter;
812 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
813
Dan Stoza289ade12014-02-28 19:17:17814 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
815 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
816 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 18:24:03817 item.mTransform = transform &
818 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 19:17:17819 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 18:24:03820 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
821 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 19:17:17822 item.mTimestamp = timestamp;
823 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:43824 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 19:17:17825 item.mFrameNumber = mCore->mFrameCounter;
826 item.mSlot = slot;
827 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-27 01:59:08828 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 22:05:45829 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 22:50:23830 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 23:23:42831 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 23:32:12832 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 22:50:23833 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 19:17:17834
Ruben Brunk1681d952014-06-27 22:51:55835 mStickyTransform = stickyTransform;
836
Pablo Ceballosccdfd602015-10-07 22:05:45837 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 22:50:23838 if (mCore->mSharedBufferMode) {
839 mCore->mSharedBufferCache.crop = crop;
840 mCore->mSharedBufferCache.transform = transform;
841 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 22:05:45842 scalingMode);
Pablo Ceballos3559fbf2016-03-17 22:50:23843 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 22:05:45844 }
845
Dan Stoza289ade12014-02-28 19:17:17846 if (mCore->mQueue.empty()) {
847 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
848 // and simply queue this buffer
849 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 19:37:46850 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17851 } else {
Pablo Ceballos4d85da42016-04-20 03:11:56852 // When the queue is not empty, we need to look at the last buffer
853 // in the queue to see if we need to replace it
854 const BufferItem& last = mCore->mQueue.itemAt(
855 mCore->mQueue.size() - 1);
856 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 20:15:22857
Pablo Ceballos4d85da42016-04-20 03:11:56858 if (!last.mIsStale) {
859 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 22:05:45860
Pablo Ceballos3559fbf2016-03-17 22:50:23861 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 22:05:45862 // still be around. Mark it as no longer shared if this
863 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 22:50:23864 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-20 03:11:56865 mSlots[last.mSlot].mBufferState.isFree()) {
866 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 22:05:45867 }
868 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-20 03:11:56869 if (!mSlots[last.mSlot].mBufferState.isShared()) {
870 mCore->mActiveBuffers.erase(last.mSlot);
871 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 22:05:45872 }
Dan Stoza289ade12014-02-28 19:17:17873 }
Pablo Ceballos23b4abe2016-01-08 20:15:22874
Dan Stoza289ade12014-02-28 19:17:17875 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-20 03:11:56876 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 19:37:46877 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17878 } else {
879 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 19:37:46880 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 19:17:17881 }
882 }
883
884 mCore->mBufferHasBeenQueued = true;
885 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 23:53:23886 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 19:17:17887
888 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 18:24:03889 mCore->mTransformHint,
890 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 19:17:17891
892 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 19:37:46893
894 // Take a ticket for the callback functions
895 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 20:20:51896
Pablo Ceballos9e314332016-01-12 21:49:19897 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:17898 } // Autolock scope
899
Dan Stoza8dc55392014-11-04 19:37:46900 // Don't send the GraphicBuffer through the callback, and don't send
901 // the slot number, since the consumer shouldn't need it
902 item.mGraphicBuffer.clear();
903 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
904
905 // Call back without the main BufferQueue lock held, but with the callback
906 // lock held so we can ensure that callbacks occur in order
907 {
908 Mutex::Autolock lock(mCallbackMutex);
909 while (callbackTicket != mCurrentCallbackTicket) {
910 mCallbackCondition.wait(mCallbackMutex);
911 }
912
913 if (frameAvailableListener != NULL) {
914 frameAvailableListener->onFrameAvailable(item);
915 } else if (frameReplacedListener != NULL) {
916 frameReplacedListener->onFrameReplaced(item);
917 }
918
919 ++mCurrentCallbackTicket;
920 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 19:17:17921 }
922
Christian Poetzsch82fbb122015-12-07 13:36:22923 // Wait without lock held
924 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
925 // Waiting here allows for two full buffers to be queued but not a
926 // third. In the event that frames take varying time, this makes a
927 // small trade-off in favor of latency rather than throughput.
928 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22929 }
Dan Stoza50101d02016-04-07 23:53:23930 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 20:18:15931 mLastQueuedCrop = item.mCrop;
932 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22933
Dan Stoza289ade12014-02-28 19:17:17934 return NO_ERROR;
935}
936
Pablo Ceballos583b1b32015-09-04 01:23:52937status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 19:17:17938 ATRACE_CALL();
939 BQ_LOGV("cancelBuffer: slot %d", slot);
940 Mutex::Autolock lock(mCore->mMutex);
941
942 if (mCore->mIsAbandoned) {
943 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-04 01:23:52944 return NO_INIT;
945 }
946
947 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
948 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
949 return NO_INIT;
Dan Stoza289ade12014-02-28 19:17:17950 }
951
Pablo Ceballos3559fbf2016-03-17 22:50:23952 if (mCore->mSharedBufferMode) {
953 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 22:05:45954 return BAD_VALUE;
955 }
956
Dan Stoza3e96f192014-03-03 18:16:19957 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 19:17:17958 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 18:16:19959 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-04 01:23:52960 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 22:05:45961 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 19:17:17962 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 22:05:45963 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-04 01:23:52964 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17965 } else if (fence == NULL) {
966 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-04 01:23:52967 return BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:17968 }
969
Pablo Ceballosccdfd602015-10-07 22:05:45970 mSlots[slot].mBufferState.cancel();
971
Pablo Ceballos3559fbf2016-03-17 22:50:23972 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 22:05:45973 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 22:50:23974 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 22:05:45975 mSlots[slot].mBufferState.mShared = false;
976 }
977
978 // Don't put the shared buffer on the free list.
979 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 20:15:22980 mCore->mActiveBuffers.erase(slot);
981 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 22:05:45982 }
Pablo Ceballos23b4abe2016-01-08 20:15:22983
Dan Stoza289ade12014-02-28 19:17:17984 mSlots[slot].mFence = fence;
985 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 21:49:19986 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-04 01:23:52987
988 return NO_ERROR;
Dan Stoza289ade12014-02-28 19:17:17989}
990
991int BufferQueueProducer::query(int what, int *outValue) {
992 ATRACE_CALL();
993 Mutex::Autolock lock(mCore->mMutex);
994
995 if (outValue == NULL) {
996 BQ_LOGE("query: outValue was NULL");
997 return BAD_VALUE;
998 }
999
1000 if (mCore->mIsAbandoned) {
1001 BQ_LOGE("query: BufferQueue has been abandoned");
1002 return NO_INIT;
1003 }
1004
1005 int value;
1006 switch (what) {
1007 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 18:24:031008 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 19:17:171009 break;
1010 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 18:24:031011 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 19:17:171012 break;
1013 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 18:24:031014 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 19:17:171015 break;
1016 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-27 01:59:081017 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 19:17:171018 break;
Ruben Brunk1681d952014-06-27 22:51:551019 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 18:24:031020 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 22:51:551021 break;
Dan Stoza289ade12014-02-28 19:17:171022 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1023 value = (mCore->mQueue.size() > 1);
1024 break;
1025 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 18:24:031026 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 19:17:171027 break;
Eino-Ville Talvala82c6bcc2015-02-20 00:10:431028 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1029 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1030 break;
Dan Stoza4afd8b62015-02-26 00:49:081031 case NATIVE_WINDOW_BUFFER_AGE:
1032 if (mCore->mBufferAge > INT32_MAX) {
1033 value = 0;
1034 } else {
1035 value = static_cast<int32_t>(mCore->mBufferAge);
1036 }
1037 break;
Dan Stoza289ade12014-02-28 19:17:171038 default:
1039 return BAD_VALUE;
1040 }
1041
1042 BQ_LOGV("query: %d? %d", what, value);
1043 *outValue = value;
1044 return NO_ERROR;
1045}
1046
Dan Stozaf0eaf252014-03-21 20:05:511047status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 19:17:171048 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1049 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 20:54:371050 Mutex::Autolock lock(mCore->mMutex);
1051 mConsumerName = mCore->mConsumerName;
1052 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1053 producerControlledByApp ? "true" : "false");
1054
1055 if (mCore->mIsAbandoned) {
1056 BQ_LOGE("connect: BufferQueue has been abandoned");
1057 return NO_INIT;
1058 }
1059
1060 if (mCore->mConsumerListener == NULL) {
1061 BQ_LOGE("connect: BufferQueue has no consumer");
1062 return NO_INIT;
1063 }
1064
1065 if (output == NULL) {
1066 BQ_LOGE("connect: output was NULL");
1067 return BAD_VALUE;
1068 }
1069
1070 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1071 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1072 mCore->mConnectedApi, api);
1073 return BAD_VALUE;
1074 }
1075
1076 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1077 mDequeueTimeout < 0 ?
1078 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1079 mCore->mMaxBufferCount) -
1080 mCore->getMaxBufferCountLocked();
1081 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1082 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1083 "slots. Delta = %d", delta);
1084 return BAD_VALUE;
1085 }
1086
Dan Stoza289ade12014-02-28 19:17:171087 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 20:54:371088 switch (api) {
1089 case NATIVE_WINDOW_API_EGL:
1090 case NATIVE_WINDOW_API_CPU:
1091 case NATIVE_WINDOW_API_MEDIA:
1092 case NATIVE_WINDOW_API_CAMERA:
1093 mCore->mConnectedApi = api;
1094 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1095 mCore->mTransformHint,
1096 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 19:17:171097
Pablo Ceballos981066c2016-02-18 20:54:371098 // Set up a death notification so that we can disconnect
1099 // automatically if the remote producer dies
1100 if (listener != NULL &&
1101 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1102 status = IInterface::asBinder(listener)->linkToDeath(
1103 static_cast<IBinder::DeathRecipient*>(this));
1104 if (status != NO_ERROR) {
1105 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1106 strerror(-status), status);
Dan Stoza289ade12014-02-28 19:17:171107 }
Pablo Ceballos981066c2016-02-18 20:54:371108 }
1109 mCore->mConnectedProducerListener = listener;
1110 break;
1111 default:
1112 BQ_LOGE("connect: unknown API %d", api);
1113 status = BAD_VALUE;
1114 break;
Dan Stoza289ade12014-02-28 19:17:171115 }
1116
Pablo Ceballos981066c2016-02-18 20:54:371117 mCore->mBufferHasBeenQueued = false;
1118 mCore->mDequeueBufferCannotBlock = false;
1119 if (mDequeueTimeout < 0) {
1120 mCore->mDequeueBufferCannotBlock =
1121 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 20:43:321122 }
Dan Stoza289ade12014-02-28 19:17:171123
Pablo Ceballos981066c2016-02-18 20:54:371124 mCore->mAllowAllocation = true;
1125 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 19:17:171126 return status;
1127}
1128
1129status_t BufferQueueProducer::disconnect(int api) {
1130 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 22:05:451131 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 19:17:171132
1133 int status = NO_ERROR;
1134 sp<IConsumerListener> listener;
1135 { // Autolock scope
1136 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-16 04:17:031137 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 19:17:171138
1139 if (mCore->mIsAbandoned) {
1140 // It's not really an error to disconnect after the surface has
1141 // been abandoned; it should just be a no-op.
1142 return NO_ERROR;
1143 }
1144
Chong Zhang1b3a9ac2016-03-01 00:47:471145 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1146 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 20:08:331147 // If we're asked to disconnect the currently connected api but
1148 // nobody is connected, it's not really an error.
1149 if (api == BufferQueueCore::NO_CONNECTED_API) {
1150 return NO_ERROR;
1151 }
Chong Zhang1b3a9ac2016-03-01 00:47:471152 }
1153
Dan Stoza289ade12014-02-28 19:17:171154 switch (api) {
1155 case NATIVE_WINDOW_API_EGL:
1156 case NATIVE_WINDOW_API_CPU:
1157 case NATIVE_WINDOW_API_MEDIA:
1158 case NATIVE_WINDOW_API_CAMERA:
1159 if (mCore->mConnectedApi == api) {
1160 mCore->freeAllBuffersLocked();
1161
1162 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 20:05:511163 if (mCore->mConnectedProducerListener != NULL) {
1164 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 16:01:011165 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 19:17:171166 // This can fail if we're here because of the death
1167 // notification, but we just ignore it
1168 token->unlinkToDeath(
1169 static_cast<IBinder::DeathRecipient*>(this));
1170 }
Pablo Ceballos3559fbf2016-03-17 22:50:231171 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 20:15:221172 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 20:05:511173 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 19:17:171174 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 23:42:541175 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 19:17:171176 mCore->mDequeueCondition.broadcast();
1177 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-07-01 05:54:161178 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 22:05:451179 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 19:17:171180 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 23:14:331181 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:171182 }
1183 break;
1184 default:
Pablo Ceballosccdfd602015-10-07 22:05:451185 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 23:14:331186 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 19:17:171187 break;
1188 }
1189 } // Autolock scope
1190
1191 // Call back without lock held
1192 if (listener != NULL) {
1193 listener->onBuffersReleased();
1194 }
1195
1196 return status;
1197}
1198
Jesse Hall399184a2014-03-03 23:42:541199status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 14:16:081200 sp<IConsumerListener> listener;
1201 { // Autolock scope
1202 Mutex::Autolock _l(mCore->mMutex);
1203 mCore->mSidebandStream = stream;
1204 listener = mCore->mConsumerListener;
1205 } // Autolock scope
1206
1207 if (listener != NULL) {
1208 listener->onSidebandStreamChanged();
1209 }
Jesse Hall399184a2014-03-03 23:42:541210 return NO_ERROR;
1211}
1212
Pablo Ceballos567dbbb2015-08-27 01:59:081213void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1214 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-16 04:17:031215 ATRACE_CALL();
1216 while (true) {
Antoine Labour78014f32014-07-16 04:17:031217 size_t newBufferCount = 0;
1218 uint32_t allocWidth = 0;
1219 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 18:24:031220 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-16 04:17:031221 uint32_t allocUsage = 0;
1222 { // Autolock scope
1223 Mutex::Autolock lock(mCore->mMutex);
1224 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 20:13:571225
Dan Stoza9de72932015-04-17 00:28:431226 if (!mCore->mAllowAllocation) {
1227 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1228 "BufferQueue");
1229 return;
1230 }
1231
Pablo Ceballos23b4abe2016-01-08 20:15:221232 newBufferCount = mCore->mFreeSlots.size();
1233 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-16 04:17:031234 return;
1235 }
Pablo Ceballos23b4abe2016-01-08 20:15:221236
Antoine Labour78014f32014-07-16 04:17:031237 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1238 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1239 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1240 allocUsage = usage | mCore->mConsumerUsageBits;
1241
1242 mCore->mIsAllocating = true;
1243 } // Autolock scope
1244
Dan Stoza3be1c6b2014-11-18 18:24:031245 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-16 04:17:031246 for (size_t i = 0; i < newBufferCount; ++i) {
1247 status_t result = NO_ERROR;
1248 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1249 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1250 if (result != NO_ERROR) {
1251 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1252 " %u, usage %u)", width, height, format, usage);
1253 Mutex::Autolock lock(mCore->mMutex);
1254 mCore->mIsAllocating = false;
1255 mCore->mIsAllocatingCondition.broadcast();
1256 return;
1257 }
1258 buffers.push_back(graphicBuffer);
1259 }
1260
1261 { // Autolock scope
1262 Mutex::Autolock lock(mCore->mMutex);
1263 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1264 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 18:24:031265 PixelFormat checkFormat = format != 0 ?
1266 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-16 04:17:031267 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1268 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1269 checkFormat != allocFormat || checkUsage != allocUsage) {
1270 // Something changed while we released the lock. Retry.
1271 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1272 mCore->mIsAllocating = false;
1273 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 20:13:571274 continue;
1275 }
1276
Antoine Labour78014f32014-07-16 04:17:031277 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 20:15:221278 if (mCore->mFreeSlots.empty()) {
1279 BQ_LOGV("allocateBuffers: a slot was occupied while "
1280 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-16 04:17:031281 continue;
1282 }
Pablo Ceballos23b4abe2016-01-08 20:15:221283 auto slot = mCore->mFreeSlots.begin();
1284 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1285 mSlots[*slot].mGraphicBuffer = buffers[i];
1286 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 20:20:511287
1288 // freeBufferLocked puts this slot on the free slots list. Since
1289 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 20:15:221290 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 20:20:511291
Pablo Ceballos23b4abe2016-01-08 20:15:221292 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1293 *slot);
Christopher Ferris87e94cd2016-04-26 18:29:081294
1295 // Make sure the erase is done after all uses of the slot
1296 // iterator since it will be invalid after this point.
1297 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-16 04:17:031298 }
Dan Stoza29a3e902014-06-20 20:13:571299
Antoine Labour78014f32014-07-16 04:17:031300 mCore->mIsAllocating = false;
1301 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 21:49:191302 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-16 04:17:031303 } // Autolock scope
Dan Stoza29a3e902014-06-20 20:13:571304 }
1305}
1306
Dan Stoza9de72932015-04-17 00:28:431307status_t BufferQueueProducer::allowAllocation(bool allow) {
1308 ATRACE_CALL();
1309 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1310
1311 Mutex::Autolock lock(mCore->mMutex);
1312 mCore->mAllowAllocation = allow;
1313 return NO_ERROR;
1314}
1315
Dan Stoza812ed062015-06-02 22:45:221316status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1317 ATRACE_CALL();
1318 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1319
1320 Mutex::Autolock lock(mCore->mMutex);
1321 mCore->mGenerationNumber = generationNumber;
1322 return NO_ERROR;
1323}
1324
Dan Stozac6f30bd2015-06-08 16:32:501325String8 BufferQueueProducer::getConsumerName() const {
1326 ATRACE_CALL();
1327 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1328 return mConsumerName;
1329}
1330
Dan Stoza7dde5992015-05-22 16:51:441331uint64_t BufferQueueProducer::getNextFrameNumber() const {
1332 ATRACE_CALL();
1333
1334 Mutex::Autolock lock(mCore->mMutex);
1335 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1336 return nextFrameNumber;
1337}
1338
Pablo Ceballos3559fbf2016-03-17 22:50:231339status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 22:05:451340 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 22:50:231341 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 22:05:451342
1343 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 22:50:231344 if (!sharedBufferMode) {
1345 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 22:05:451346 }
Pablo Ceballos3559fbf2016-03-17 22:50:231347 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 20:43:321348 return NO_ERROR;
1349}
Pablo Ceballosccdfd602015-10-07 22:05:451350
Pablo Ceballosff95aab2016-01-14 01:09:581351status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1352 ATRACE_CALL();
1353 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1354
1355 Mutex::Autolock lock(mCore->mMutex);
1356
1357 mCore->mAutoRefresh = autoRefresh;
1358 return NO_ERROR;
1359}
1360
Dan Stoza127fc632015-06-30 20:43:321361status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1362 ATRACE_CALL();
1363 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1364
Pablo Ceballos981066c2016-02-18 20:54:371365 Mutex::Autolock lock(mCore->mMutex);
1366 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1367 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1368 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1369 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1370 "available slots. Delta = %d", delta);
1371 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 20:15:221372 }
1373
Pablo Ceballos981066c2016-02-18 20:54:371374 mDequeueTimeout = timeout;
1375 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 21:49:191376
Pablo Ceballos981066c2016-02-18 20:54:371377 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 22:05:451378 return NO_ERROR;
1379}
1380
Dan Stoza50101d02016-04-07 23:53:231381status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 20:18:151382 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 23:53:231383 ATRACE_CALL();
1384 BQ_LOGV("getLastQueuedBuffer");
1385
1386 Mutex::Autolock lock(mCore->mMutex);
1387 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1388 *outBuffer = nullptr;
1389 *outFence = Fence::NO_FENCE;
1390 return NO_ERROR;
1391 }
1392
1393 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1394 *outFence = mLastQueueBufferFence;
1395
John Reck1a61da52016-04-28 20:18:151396 // Currently only SurfaceFlinger internally ever changes
1397 // GLConsumer's filtering mode, so we just use 'true' here as
1398 // this is slightly specialized for the current client of this API,
1399 // which does want filtering.
1400 GLConsumer::computeTransformMatrix(outTransformMatrix,
1401 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1402 mLastQueuedTransform, true /* filter */);
1403
Dan Stoza50101d02016-04-07 23:53:231404 return NO_ERROR;
1405}
1406
Dan Stoza289ade12014-02-28 19:17:171407void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1408 // If we're here, it means that a producer we were connected to died.
1409 // We're guaranteed that we are still connected to it because we remove
1410 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1411 // without synchronization here.
1412 int api = mCore->mConnectedApi;
1413 disconnect(api);
1414}
1415
1416} // namespace android