blob: 91ba18db7f38b160c5b0bd836eff352311eda68f [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
31#include <gui/BufferItem.h>
32#include <gui/BufferQueueCore.h>
33#include <gui/BufferQueueProducer.h>
34#include <gui/IConsumerListener.h>
35#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070036#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
38#include <utils/Log.h>
39#include <utils/Trace.h>
40
41namespace android {
42
43BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
44 mCore(core),
45 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070046 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070047 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080048 mLastQueueBufferFence(Fence::NO_FENCE),
49 mCallbackMutex(),
50 mNextCallbackTicket(0),
51 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070052 mCallbackCondition(),
53 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080054
55BufferQueueProducer::~BufferQueueProducer() {}
56
57status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
58 ATRACE_CALL();
59 BQ_LOGV("requestBuffer: slot %d", slot);
60 Mutex::Autolock lock(mCore->mMutex);
61
62 if (mCore->mIsAbandoned) {
63 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
64 return NO_INIT;
65 }
66
Pablo Ceballos583b1b32015-09-03 18:23:52 -070067 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
68 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
69 return NO_INIT;
70 }
71
Dan Stoza3e96f192014-03-03 10:16:19 -080072 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080073 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080074 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080075 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070076 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080077 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070078 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080079 return BAD_VALUE;
80 }
81
82 mSlots[slot].mRequestBufferCalled = true;
83 *buf = mSlots[slot].mGraphicBuffer;
84 return NO_ERROR;
85}
86
Pablo Ceballosfa455352015-08-12 17:47:47 -070087status_t BufferQueueProducer::setMaxDequeuedBufferCount(
88 int maxDequeuedBuffers) {
89 ATRACE_CALL();
90 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
91 maxDequeuedBuffers);
92
Pablo Ceballos981066c2016-02-18 12:54:37 -080093 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070094 { // Autolock scope
95 Mutex::Autolock lock(mCore->mMutex);
96 mCore->waitWhileAllocatingLocked();
97
98 if (mCore->mIsAbandoned) {
99 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
100 "abandoned");
101 return NO_INIT;
102 }
103
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700104 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
105 return NO_ERROR;
106 }
107
Pablo Ceballos72daab62015-12-07 16:38:43 -0800108 // The new maxDequeuedBuffer count should not be violated by the number
109 // of currently dequeued buffers
110 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800111 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800113 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700114 }
115 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800116 if (dequeuedCount > maxDequeuedBuffers) {
117 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
118 "count (%d) exceeds the current dequeued buffer count (%d)",
119 maxDequeuedBuffers, dequeuedCount);
120 return BAD_VALUE;
121 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700122
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700123 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700124 bufferCount += maxDequeuedBuffers;
125
126 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
127 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
128 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
129 return BAD_VALUE;
130 }
131
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700132 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700133 if (bufferCount < minBufferSlots) {
134 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
135 "less than minimum %d", bufferCount, minBufferSlots);
136 return BAD_VALUE;
137 }
138
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700139 if (bufferCount > mCore->mMaxBufferCount) {
140 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700141 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
142 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
143 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
144 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700145 return BAD_VALUE;
146 }
147
Pablo Ceballos72daab62015-12-07 16:38:43 -0800148 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800149 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800150 return BAD_VALUE;
151 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700152 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800153 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800154 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800155 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800156 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700157 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700158 } // Autolock scope
159
160 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800161 if (listener != NULL) {
162 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700163 }
164
165 return NO_ERROR;
166}
167
168status_t BufferQueueProducer::setAsyncMode(bool async) {
169 ATRACE_CALL();
170 BQ_LOGV("setAsyncMode: async = %d", async);
171
Pablo Ceballos981066c2016-02-18 12:54:37 -0800172 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700173 { // Autolock scope
174 Mutex::Autolock lock(mCore->mMutex);
175 mCore->waitWhileAllocatingLocked();
176
177 if (mCore->mIsAbandoned) {
178 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
179 return NO_INIT;
180 }
181
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700182 if (async == mCore->mAsyncMode) {
183 return NO_ERROR;
184 }
185
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700186 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700187 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
188 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700189 BQ_LOGE("setAsyncMode(%d): this call would cause the "
190 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700191 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
192 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
193 mCore->mMaxDequeuedBufferCount,
194 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700195 return BAD_VALUE;
196 }
197
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800198 int delta = mCore->getMaxBufferCountLocked(async,
199 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
200 - mCore->getMaxBufferCountLocked();
201
Pablo Ceballos981066c2016-02-18 12:54:37 -0800202 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800203 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
204 "available slots. Delta = %d", delta);
205 return BAD_VALUE;
206 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700207 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800208 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700209 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700210 if (delta < 0) {
211 listener = mCore->mConsumerListener;
212 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700213 } // Autolock scope
214
215 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800216 if (listener != NULL) {
217 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700218 }
219 return NO_ERROR;
220}
221
Dan Stoza5ecfb682016-01-04 17:01:02 -0800222int BufferQueueProducer::getFreeBufferLocked() const {
223 if (mCore->mFreeBuffers.empty()) {
224 return BufferQueueCore::INVALID_BUFFER_SLOT;
225 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800226 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800227 mCore->mFreeBuffers.pop_front();
228 return slot;
229}
230
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800231int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800232 if (mCore->mFreeSlots.empty()) {
233 return BufferQueueCore::INVALID_BUFFER_SLOT;
234 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800235 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800237 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800238}
239
240status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800242 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
243 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800244 bool tryAgain = true;
245 while (tryAgain) {
246 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800247 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800248 return NO_INIT;
249 }
250
Dan Stoza9f3053d2014-03-06 15:14:33 -0800251 int dequeuedCount = 0;
252 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800253 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700254 if (mSlots[s].mBufferState.isDequeued()) {
255 ++dequeuedCount;
256 }
257 if (mSlots[s].mBufferState.isAcquired()) {
258 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800259 }
260 }
261
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700262 // Producers are not allowed to dequeue more than
263 // mMaxDequeuedBufferCount buffers.
264 // This check is only done if a buffer has already been queued
265 if (mCore->mBufferHasBeenQueued &&
266 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
267 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800268 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800269 return INVALID_OPERATION;
270 }
271
Dan Stoza0de7ea72015-04-23 13:20:51 -0700272 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
273
Dan Stozaae3c3682014-04-18 15:43:35 -0700274 // If we disconnect and reconnect quickly, we can be in a state where
275 // our slots are empty but we have many buffers in the queue. This can
276 // cause us to run out of memory if we outrun the consumer. Wait here if
277 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800278 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700279 bool tooManyBuffers = mCore->mQueue.size()
280 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700281 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800282 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700283 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700284 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700285 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700286 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700287 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700288 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700289 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 } else {
291 if (caller == FreeSlotCaller::Dequeue) {
292 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800293 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800294 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
295 *found = slot;
296 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800297 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800298 }
299 } else {
300 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800302 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
303 *found = slot;
304 } else {
305 *found = getFreeBufferLocked();
306 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700307 }
308 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700309 }
310
311 // If no buffer is found, or if the queue has too many buffers
312 // outstanding, wait for a buffer to be acquired or released, or for the
313 // max buffer count to change.
314 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
315 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 if (tryAgain) {
317 // Return an error if we're in non-blocking mode (producer and
318 // consumer are controlled by the application).
319 // However, the consumer is allowed to briefly acquire an extra
320 // buffer (which could cause us to have to wait here), which is
321 // okay, since it is only used to implement an atomic acquire +
322 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700323 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
325 return WOULD_BLOCK;
326 }
Dan Stoza127fc632015-06-30 13:43:32 -0700327 if (mDequeueTimeout >= 0) {
328 status_t result = mCore->mDequeueCondition.waitRelative(
329 mCore->mMutex, mDequeueTimeout);
330 if (result == TIMED_OUT) {
331 return result;
332 }
333 } else {
334 mCore->mDequeueCondition.wait(mCore->mMutex);
335 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800336 }
337 } // while (tryAgain)
338
339 return NO_ERROR;
340}
341
Dan Stoza289ade12014-02-28 11:17:17 -0800342status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700343 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
344 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800345 ATRACE_CALL();
346 { // Autolock scope
347 Mutex::Autolock lock(mCore->mMutex);
348 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700349
350 if (mCore->mIsAbandoned) {
351 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
352 return NO_INIT;
353 }
354
355 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
356 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
357 return NO_INIT;
358 }
Dan Stoza289ade12014-02-28 11:17:17 -0800359 } // Autolock scope
360
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700361 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
362 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800363
364 if ((width && !height) || (!width && height)) {
365 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
366 return BAD_VALUE;
367 }
368
369 status_t returnFlags = NO_ERROR;
370 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
371 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800373
374 { // Autolock scope
375 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700376 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800377
378 if (format == 0) {
379 format = mCore->mDefaultBufferFormat;
380 }
381
382 // Enable the usage bits the consumer requested
383 usage |= mCore->mConsumerUsageBits;
384
Dan Stoza9de72932015-04-16 17:28:43 -0700385 const bool useDefaultSize = !width && !height;
386 if (useDefaultSize) {
387 width = mCore->mDefaultWidth;
388 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800389 }
Dan Stoza289ade12014-02-28 11:17:17 -0800390
Pablo Ceballos981066c2016-02-18 12:54:37 -0800391 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700392 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800393 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800394 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700395 if (status != NO_ERROR) {
396 return status;
397 }
398
399 // This should not happen
400 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
401 BQ_LOGE("dequeueBuffer: no available buffer slots");
402 return -EBUSY;
403 }
404
405 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
406
407 // If we are not allowed to allocate new buffers,
408 // waitForFreeSlotThenRelock must have returned a slot containing a
409 // buffer. If this buffer would require reallocation to meet the
410 // requested attributes, we free it and attempt to get another one.
411 if (!mCore->mAllowAllocation) {
412 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700413 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700414 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
415 "buffer");
416 return BAD_VALUE;
417 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800418 mCore->mFreeSlots.insert(found);
419 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700420 found = BufferItem::INVALID_BUFFER_SLOT;
421 continue;
422 }
423 }
Dan Stoza289ade12014-02-28 11:17:17 -0800424 }
425
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800426 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700427 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800428 buffer->needsReallocation(width, height, format, usage)) {
429 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
430 "buffer");
431
432 return BAD_VALUE;
433 }
434
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700435 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800436 mCore->mActiveBuffers.insert(found);
437 }
Dan Stoza289ade12014-02-28 11:17:17 -0800438 *outSlot = found;
439 ATRACE_BUFFER_INDEX(found);
440
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800441 attachedByConsumer = mSlots[found].mNeedsReallocation;
442 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800443
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700444 mSlots[found].mBufferState.dequeue();
445
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700446 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700447 // first buffer that is dequeued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700448 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700449 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700450 mCore->mSharedBufferSlot = found;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700451 mSlots[found].mBufferState.mShared = true;
452 }
Dan Stoza289ade12014-02-28 11:17:17 -0800453
Dan Stoza289ade12014-02-28 11:17:17 -0800454 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700455 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800456 {
457 mSlots[found].mAcquireCalled = false;
458 mSlots[found].mGraphicBuffer = NULL;
459 mSlots[found].mRequestBufferCalled = false;
460 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
461 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
462 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800463 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700464 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800465
466 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800467 } else {
468 // We add 1 because that will be the frame number when this buffer
469 // is queued
470 mCore->mBufferAge =
471 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800472 }
473
Dan Stoza800b41a2015-04-28 14:20:04 -0700474 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
475 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800476
Dan Stoza289ade12014-02-28 11:17:17 -0800477 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
478 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
479 "slot=%d w=%d h=%d format=%u",
480 found, buffer->width, buffer->height, buffer->format);
481 }
482
483 eglDisplay = mSlots[found].mEglDisplay;
484 eglFence = mSlots[found].mEglFence;
485 *outFence = mSlots[found].mFence;
486 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
487 mSlots[found].mFence = Fence::NO_FENCE;
488 } // Autolock scope
489
490 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
491 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700492 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800493 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800494 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800495 { // Autolock scope
496 Mutex::Autolock lock(mCore->mMutex);
497
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700498 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
499 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
500 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
501 }
502
503 mCore->mIsAllocating = false;
504 mCore->mIsAllocatingCondition.broadcast();
505
506 if (graphicBuffer == NULL) {
507 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
508 return error;
509 }
510
Dan Stoza289ade12014-02-28 11:17:17 -0800511 if (mCore->mIsAbandoned) {
512 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
513 return NO_INIT;
514 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800515
Pablo Ceballos9e314332016-01-12 13:49:19 -0800516 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800517 } // Autolock scope
518 }
519
Dan Stoza9f3053d2014-03-06 15:14:33 -0800520 if (attachedByConsumer) {
521 returnFlags |= BUFFER_NEEDS_REALLOCATION;
522 }
523
Dan Stoza289ade12014-02-28 11:17:17 -0800524 if (eglFence != EGL_NO_SYNC_KHR) {
525 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
526 1000000000);
527 // If something goes wrong, log the error, but return the buffer without
528 // synchronizing access to it. It's too late at this point to abort the
529 // dequeue operation.
530 if (result == EGL_FALSE) {
531 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
532 eglGetError());
533 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
534 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
535 }
536 eglDestroySyncKHR(eglDisplay, eglFence);
537 }
538
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700539 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
540 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800541 mSlots[*outSlot].mFrameNumber,
542 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
543
544 return returnFlags;
545}
546
Dan Stoza9f3053d2014-03-06 15:14:33 -0800547status_t BufferQueueProducer::detachBuffer(int slot) {
548 ATRACE_CALL();
549 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700550 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos981066c2016-02-18 12:54:37 -0800551 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800552
Pablo Ceballos981066c2016-02-18 12:54:37 -0800553 if (mCore->mIsAbandoned) {
554 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
555 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800556 }
557
Pablo Ceballos981066c2016-02-18 12:54:37 -0800558 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
559 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
560 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700561 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800562
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700563 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
564 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800565 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700566 }
567
Pablo Ceballos981066c2016-02-18 12:54:37 -0800568 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
569 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
570 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
571 return BAD_VALUE;
572 } else if (!mSlots[slot].mBufferState.isDequeued()) {
573 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
574 "(state = %s)", slot, mSlots[slot].mBufferState.string());
575 return BAD_VALUE;
576 } else if (!mSlots[slot].mRequestBufferCalled) {
577 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
578 slot);
579 return BAD_VALUE;
580 }
581
582 mSlots[slot].mBufferState.detachProducer();
583 mCore->mActiveBuffers.erase(slot);
584 mCore->mFreeSlots.insert(slot);
585 mCore->clearBufferSlotLocked(slot);
586 mCore->mDequeueCondition.broadcast();
587 VALIDATE_CONSISTENCY();
588
Dan Stoza9f3053d2014-03-06 15:14:33 -0800589 return NO_ERROR;
590}
591
Dan Stozad9822a32014-03-28 15:25:31 -0700592status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
593 sp<Fence>* outFence) {
594 ATRACE_CALL();
595
596 if (outBuffer == NULL) {
597 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
598 return BAD_VALUE;
599 } else if (outFence == NULL) {
600 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
601 return BAD_VALUE;
602 }
603
Pablo Ceballos981066c2016-02-18 12:54:37 -0800604 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700605
Pablo Ceballos981066c2016-02-18 12:54:37 -0800606 if (mCore->mIsAbandoned) {
607 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
608 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700609 }
610
Pablo Ceballos981066c2016-02-18 12:54:37 -0800611 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
612 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
613 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700614 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800615
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700616 if (mCore->mSharedBufferMode) {
617 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
618 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800619 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700620 }
621
Pablo Ceballos981066c2016-02-18 12:54:37 -0800622 mCore->waitWhileAllocatingLocked();
623
624 if (mCore->mFreeBuffers.empty()) {
625 return NO_MEMORY;
626 }
627
628 int found = mCore->mFreeBuffers.front();
629 mCore->mFreeBuffers.remove(found);
630 mCore->mFreeSlots.insert(found);
631
632 BQ_LOGV("detachNextBuffer detached slot %d", found);
633
634 *outBuffer = mSlots[found].mGraphicBuffer;
635 *outFence = mSlots[found].mFence;
636 mCore->clearBufferSlotLocked(found);
637 VALIDATE_CONSISTENCY();
638
Dan Stozad9822a32014-03-28 15:25:31 -0700639 return NO_ERROR;
640}
641
Dan Stoza9f3053d2014-03-06 15:14:33 -0800642status_t BufferQueueProducer::attachBuffer(int* outSlot,
643 const sp<android::GraphicBuffer>& buffer) {
644 ATRACE_CALL();
645
646 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700647 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800648 return BAD_VALUE;
649 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700650 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800651 return BAD_VALUE;
652 }
653
654 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700655
656 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700657 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700658 return NO_INIT;
659 }
660
661 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700662 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700663 return NO_INIT;
664 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800665
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700666 if (mCore->mSharedBufferMode) {
667 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700668 return BAD_VALUE;
669 }
670
Dan Stoza812ed062015-06-02 15:45:22 -0700671 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
672 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
673 "[queue %u]", buffer->getGenerationNumber(),
674 mCore->mGenerationNumber);
675 return BAD_VALUE;
676 }
677
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700678 mCore->waitWhileAllocatingLocked();
679
Dan Stoza9f3053d2014-03-06 15:14:33 -0800680 status_t returnFlags = NO_ERROR;
681 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800682 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800683 if (status != NO_ERROR) {
684 return status;
685 }
686
687 // This should not happen
688 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700689 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800690 return -EBUSY;
691 }
692
693 *outSlot = found;
694 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696 *outSlot, returnFlags);
697
698 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700699 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800700 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
701 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700702 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800703 mSlots[*outSlot].mAcquireCalled = false;
704 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800705 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700706
Dan Stoza9f3053d2014-03-06 15:14:33 -0800707 return returnFlags;
708}
709
Dan Stoza289ade12014-02-28 11:17:17 -0800710status_t BufferQueueProducer::queueBuffer(int slot,
711 const QueueBufferInput &input, QueueBufferOutput *output) {
712 ATRACE_CALL();
713 ATRACE_BUFFER_INDEX(slot);
714
715 int64_t timestamp;
716 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800717 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700718 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800719 int scalingMode;
720 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700721 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800722 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800723 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700724 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700725 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800726
727 if (fence == NULL) {
728 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800729 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800730 }
731
732 switch (scalingMode) {
733 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
734 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
735 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
736 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
737 break;
738 default:
739 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800740 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800741 }
742
Dan Stoza8dc55392014-11-04 11:37:46 -0800743 sp<IConsumerListener> frameAvailableListener;
744 sp<IConsumerListener> frameReplacedListener;
745 int callbackTicket = 0;
746 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800747 { // Autolock scope
748 Mutex::Autolock lock(mCore->mMutex);
749
750 if (mCore->mIsAbandoned) {
751 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
752 return NO_INIT;
753 }
754
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700755 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
756 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
757 return NO_INIT;
758 }
759
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800760 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800761 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800762 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800763 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700764 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800765 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700766 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800767 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800768 } else if (!mSlots[slot].mRequestBufferCalled) {
769 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
770 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800771 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 }
773
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700774 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700775 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700776 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700777 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700778 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700779 mSlots[slot].mBufferState.mShared = true;
780 }
781
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800782 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700783 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800784 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800785 crop.left, crop.top, crop.right, crop.bottom, transform,
786 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800787
788 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
789 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700790 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800791 crop.intersect(bufferRect, &croppedRect);
792 if (croppedRect != crop) {
793 BQ_LOGE("queueBuffer: crop rect is not contained within the "
794 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800795 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800796 }
797
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800798 // Override UNKNOWN dataspace with consumer default
799 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
800 dataSpace = mCore->mDefaultBufferDataSpace;
801 }
802
Dan Stoza289ade12014-02-28 11:17:17 -0800803 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700804 mSlots[slot].mBufferState.queue();
805
Dan Stoza289ade12014-02-28 11:17:17 -0800806 ++mCore->mFrameCounter;
807 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
808
Dan Stoza289ade12014-02-28 11:17:17 -0800809 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
810 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
811 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800812 item.mTransform = transform &
813 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800814 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800815 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
816 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800817 item.mTimestamp = timestamp;
818 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800819 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800820 item.mFrameNumber = mCore->mFrameCounter;
821 item.mSlot = slot;
822 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700823 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700824 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700825 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700826 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700827 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700828 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800829
Ruben Brunk1681d952014-06-27 15:51:55 -0700830 mStickyTransform = stickyTransform;
831
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700832 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700833 if (mCore->mSharedBufferMode) {
834 mCore->mSharedBufferCache.crop = crop;
835 mCore->mSharedBufferCache.transform = transform;
836 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700837 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700838 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700839 }
840
Dan Stoza289ade12014-02-28 11:17:17 -0800841 if (mCore->mQueue.empty()) {
842 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
843 // and simply queue this buffer
844 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800845 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800846 } else {
847 // When the queue is not empty, we need to look at the front buffer
848 // state to see if we need to replace it
849 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
850 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800851
852 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700853 mSlots[front->mSlot].mBufferState.freeQueued();
854
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700855 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700856 // still be around. Mark it as no longer shared if this
857 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700858 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700859 mSlots[front->mSlot].mBufferState.isFree()) {
860 mSlots[front->mSlot].mBufferState.mShared = false;
861 }
862 // Don't put the shared buffer on the free list.
863 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800864 mCore->mActiveBuffers.erase(front->mSlot);
865 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700866 }
Dan Stoza289ade12014-02-28 11:17:17 -0800867 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800868
Dan Stoza289ade12014-02-28 11:17:17 -0800869 // Overwrite the droppable buffer with the incoming one
870 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800871 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800872 } else {
873 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800874 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800875 }
876 }
877
878 mCore->mBufferHasBeenQueued = true;
879 mCore->mDequeueCondition.broadcast();
880
881 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800882 mCore->mTransformHint,
883 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800884
885 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800886
887 // Take a ticket for the callback functions
888 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700889
Pablo Ceballos9e314332016-01-12 13:49:19 -0800890 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800891 } // Autolock scope
892
Dan Stoza8dc55392014-11-04 11:37:46 -0800893 // Don't send the GraphicBuffer through the callback, and don't send
894 // the slot number, since the consumer shouldn't need it
895 item.mGraphicBuffer.clear();
896 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
897
898 // Call back without the main BufferQueue lock held, but with the callback
899 // lock held so we can ensure that callbacks occur in order
900 {
901 Mutex::Autolock lock(mCallbackMutex);
902 while (callbackTicket != mCurrentCallbackTicket) {
903 mCallbackCondition.wait(mCallbackMutex);
904 }
905
906 if (frameAvailableListener != NULL) {
907 frameAvailableListener->onFrameAvailable(item);
908 } else if (frameReplacedListener != NULL) {
909 frameReplacedListener->onFrameReplaced(item);
910 }
911
912 ++mCurrentCallbackTicket;
913 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800914 }
915
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000916 // Wait without lock held
917 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
918 // Waiting here allows for two full buffers to be queued but not a
919 // third. In the event that frames take varying time, this makes a
920 // small trade-off in favor of latency rather than throughput.
921 mLastQueueBufferFence->waitForever("Throttling EGL Production");
922 mLastQueueBufferFence = fence;
923 }
924
Dan Stoza289ade12014-02-28 11:17:17 -0800925 return NO_ERROR;
926}
927
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700928status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800929 ATRACE_CALL();
930 BQ_LOGV("cancelBuffer: slot %d", slot);
931 Mutex::Autolock lock(mCore->mMutex);
932
933 if (mCore->mIsAbandoned) {
934 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700935 return NO_INIT;
936 }
937
938 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
939 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
940 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800941 }
942
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700943 if (mCore->mSharedBufferMode) {
944 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700945 return BAD_VALUE;
946 }
947
Dan Stoza3e96f192014-03-03 10:16:19 -0800948 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800949 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800950 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700951 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700952 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800953 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700954 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700955 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800956 } else if (fence == NULL) {
957 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700958 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800959 }
960
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700961 mSlots[slot].mBufferState.cancel();
962
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700963 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700964 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700965 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700966 mSlots[slot].mBufferState.mShared = false;
967 }
968
969 // Don't put the shared buffer on the free list.
970 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800971 mCore->mActiveBuffers.erase(slot);
972 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700973 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800974
Dan Stoza289ade12014-02-28 11:17:17 -0800975 mSlots[slot].mFence = fence;
976 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800977 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700978
979 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800980}
981
982int BufferQueueProducer::query(int what, int *outValue) {
983 ATRACE_CALL();
984 Mutex::Autolock lock(mCore->mMutex);
985
986 if (outValue == NULL) {
987 BQ_LOGE("query: outValue was NULL");
988 return BAD_VALUE;
989 }
990
991 if (mCore->mIsAbandoned) {
992 BQ_LOGE("query: BufferQueue has been abandoned");
993 return NO_INIT;
994 }
995
996 int value;
997 switch (what) {
998 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800999 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001000 break;
1001 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001002 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001003 break;
1004 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001005 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001006 break;
1007 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001008 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001009 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001010 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001011 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001012 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001013 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1014 value = (mCore->mQueue.size() > 1);
1015 break;
1016 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001017 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001018 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001019 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1020 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1021 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001022 case NATIVE_WINDOW_BUFFER_AGE:
1023 if (mCore->mBufferAge > INT32_MAX) {
1024 value = 0;
1025 } else {
1026 value = static_cast<int32_t>(mCore->mBufferAge);
1027 }
1028 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001029 default:
1030 return BAD_VALUE;
1031 }
1032
1033 BQ_LOGV("query: %d? %d", what, value);
1034 *outValue = value;
1035 return NO_ERROR;
1036}
1037
Dan Stozaf0eaf252014-03-21 13:05:51 -07001038status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001039 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1040 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001041 Mutex::Autolock lock(mCore->mMutex);
1042 mConsumerName = mCore->mConsumerName;
1043 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1044 producerControlledByApp ? "true" : "false");
1045
1046 if (mCore->mIsAbandoned) {
1047 BQ_LOGE("connect: BufferQueue has been abandoned");
1048 return NO_INIT;
1049 }
1050
1051 if (mCore->mConsumerListener == NULL) {
1052 BQ_LOGE("connect: BufferQueue has no consumer");
1053 return NO_INIT;
1054 }
1055
1056 if (output == NULL) {
1057 BQ_LOGE("connect: output was NULL");
1058 return BAD_VALUE;
1059 }
1060
1061 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1062 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1063 mCore->mConnectedApi, api);
1064 return BAD_VALUE;
1065 }
1066
1067 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1068 mDequeueTimeout < 0 ?
1069 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1070 mCore->mMaxBufferCount) -
1071 mCore->getMaxBufferCountLocked();
1072 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1073 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1074 "slots. Delta = %d", delta);
1075 return BAD_VALUE;
1076 }
1077
Dan Stoza289ade12014-02-28 11:17:17 -08001078 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001079 switch (api) {
1080 case NATIVE_WINDOW_API_EGL:
1081 case NATIVE_WINDOW_API_CPU:
1082 case NATIVE_WINDOW_API_MEDIA:
1083 case NATIVE_WINDOW_API_CAMERA:
1084 mCore->mConnectedApi = api;
1085 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1086 mCore->mTransformHint,
1087 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001088
Pablo Ceballos981066c2016-02-18 12:54:37 -08001089 // Set up a death notification so that we can disconnect
1090 // automatically if the remote producer dies
1091 if (listener != NULL &&
1092 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1093 status = IInterface::asBinder(listener)->linkToDeath(
1094 static_cast<IBinder::DeathRecipient*>(this));
1095 if (status != NO_ERROR) {
1096 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1097 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001098 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001099 }
1100 mCore->mConnectedProducerListener = listener;
1101 break;
1102 default:
1103 BQ_LOGE("connect: unknown API %d", api);
1104 status = BAD_VALUE;
1105 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001106 }
1107
Pablo Ceballos981066c2016-02-18 12:54:37 -08001108 mCore->mBufferHasBeenQueued = false;
1109 mCore->mDequeueBufferCannotBlock = false;
1110 if (mDequeueTimeout < 0) {
1111 mCore->mDequeueBufferCannotBlock =
1112 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001113 }
Dan Stoza289ade12014-02-28 11:17:17 -08001114
Pablo Ceballos981066c2016-02-18 12:54:37 -08001115 mCore->mAllowAllocation = true;
1116 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001117 return status;
1118}
1119
1120status_t BufferQueueProducer::disconnect(int api) {
1121 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001122 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001123
1124 int status = NO_ERROR;
1125 sp<IConsumerListener> listener;
1126 { // Autolock scope
1127 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001128 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001129
1130 if (mCore->mIsAbandoned) {
1131 // It's not really an error to disconnect after the surface has
1132 // been abandoned; it should just be a no-op.
1133 return NO_ERROR;
1134 }
1135
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001136 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1137 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001138 // If we're asked to disconnect the currently connected api but
1139 // nobody is connected, it's not really an error.
1140 if (api == BufferQueueCore::NO_CONNECTED_API) {
1141 return NO_ERROR;
1142 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001143 }
1144
Dan Stoza289ade12014-02-28 11:17:17 -08001145 switch (api) {
1146 case NATIVE_WINDOW_API_EGL:
1147 case NATIVE_WINDOW_API_CPU:
1148 case NATIVE_WINDOW_API_MEDIA:
1149 case NATIVE_WINDOW_API_CAMERA:
1150 if (mCore->mConnectedApi == api) {
1151 mCore->freeAllBuffersLocked();
1152
1153 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001154 if (mCore->mConnectedProducerListener != NULL) {
1155 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001156 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001157 // This can fail if we're here because of the death
1158 // notification, but we just ignore it
1159 token->unlinkToDeath(
1160 static_cast<IBinder::DeathRecipient*>(this));
1161 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001162 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001163 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001164 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001165 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001166 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001167 mCore->mDequeueCondition.broadcast();
1168 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001169 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001170 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001171 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001172 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001173 }
1174 break;
1175 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001176 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001177 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001178 break;
1179 }
1180 } // Autolock scope
1181
1182 // Call back without lock held
1183 if (listener != NULL) {
1184 listener->onBuffersReleased();
1185 }
1186
1187 return status;
1188}
1189
Jesse Hall399184a2014-03-03 15:42:54 -08001190status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001191 sp<IConsumerListener> listener;
1192 { // Autolock scope
1193 Mutex::Autolock _l(mCore->mMutex);
1194 mCore->mSidebandStream = stream;
1195 listener = mCore->mConsumerListener;
1196 } // Autolock scope
1197
1198 if (listener != NULL) {
1199 listener->onSidebandStreamChanged();
1200 }
Jesse Hall399184a2014-03-03 15:42:54 -08001201 return NO_ERROR;
1202}
1203
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001204void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1205 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001206 ATRACE_CALL();
1207 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001208 size_t newBufferCount = 0;
1209 uint32_t allocWidth = 0;
1210 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001211 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001212 uint32_t allocUsage = 0;
1213 { // Autolock scope
1214 Mutex::Autolock lock(mCore->mMutex);
1215 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001216
Dan Stoza9de72932015-04-16 17:28:43 -07001217 if (!mCore->mAllowAllocation) {
1218 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1219 "BufferQueue");
1220 return;
1221 }
1222
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001223 newBufferCount = mCore->mFreeSlots.size();
1224 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001225 return;
1226 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001227
Antoine Labour78014f32014-07-15 21:17:03 -07001228 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1229 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1230 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1231 allocUsage = usage | mCore->mConsumerUsageBits;
1232
1233 mCore->mIsAllocating = true;
1234 } // Autolock scope
1235
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001236 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001237 for (size_t i = 0; i < newBufferCount; ++i) {
1238 status_t result = NO_ERROR;
1239 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1240 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1241 if (result != NO_ERROR) {
1242 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1243 " %u, usage %u)", width, height, format, usage);
1244 Mutex::Autolock lock(mCore->mMutex);
1245 mCore->mIsAllocating = false;
1246 mCore->mIsAllocatingCondition.broadcast();
1247 return;
1248 }
1249 buffers.push_back(graphicBuffer);
1250 }
1251
1252 { // Autolock scope
1253 Mutex::Autolock lock(mCore->mMutex);
1254 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1255 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001256 PixelFormat checkFormat = format != 0 ?
1257 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001258 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1259 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1260 checkFormat != allocFormat || checkUsage != allocUsage) {
1261 // Something changed while we released the lock. Retry.
1262 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1263 mCore->mIsAllocating = false;
1264 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001265 continue;
1266 }
1267
Antoine Labour78014f32014-07-15 21:17:03 -07001268 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001269 if (mCore->mFreeSlots.empty()) {
1270 BQ_LOGV("allocateBuffers: a slot was occupied while "
1271 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001272 continue;
1273 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001274 auto slot = mCore->mFreeSlots.begin();
1275 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1276 mSlots[*slot].mGraphicBuffer = buffers[i];
1277 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001278
1279 // freeBufferLocked puts this slot on the free slots list. Since
1280 // we then attached a buffer, move the slot to free buffer list.
1281 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001282 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001283
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001284 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1285 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001286 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001287
Antoine Labour78014f32014-07-15 21:17:03 -07001288 mCore->mIsAllocating = false;
1289 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001290 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001291 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001292 }
1293}
1294
Dan Stoza9de72932015-04-16 17:28:43 -07001295status_t BufferQueueProducer::allowAllocation(bool allow) {
1296 ATRACE_CALL();
1297 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1298
1299 Mutex::Autolock lock(mCore->mMutex);
1300 mCore->mAllowAllocation = allow;
1301 return NO_ERROR;
1302}
1303
Dan Stoza812ed062015-06-02 15:45:22 -07001304status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1305 ATRACE_CALL();
1306 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1307
1308 Mutex::Autolock lock(mCore->mMutex);
1309 mCore->mGenerationNumber = generationNumber;
1310 return NO_ERROR;
1311}
1312
Dan Stozac6f30bd2015-06-08 09:32:50 -07001313String8 BufferQueueProducer::getConsumerName() const {
1314 ATRACE_CALL();
1315 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1316 return mConsumerName;
1317}
1318
Dan Stoza7dde5992015-05-22 09:51:44 -07001319uint64_t BufferQueueProducer::getNextFrameNumber() const {
1320 ATRACE_CALL();
1321
1322 Mutex::Autolock lock(mCore->mMutex);
1323 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1324 return nextFrameNumber;
1325}
1326
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001327status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001328 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001329 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001330
1331 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001332 if (!sharedBufferMode) {
1333 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001334 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001335 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001336 return NO_ERROR;
1337}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001338
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001339status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1340 ATRACE_CALL();
1341 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1342
1343 Mutex::Autolock lock(mCore->mMutex);
1344
1345 mCore->mAutoRefresh = autoRefresh;
1346 return NO_ERROR;
1347}
1348
Dan Stoza127fc632015-06-30 13:43:32 -07001349status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1350 ATRACE_CALL();
1351 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1352
Pablo Ceballos981066c2016-02-18 12:54:37 -08001353 Mutex::Autolock lock(mCore->mMutex);
1354 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1355 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1356 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1357 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1358 "available slots. Delta = %d", delta);
1359 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001360 }
1361
Pablo Ceballos981066c2016-02-18 12:54:37 -08001362 mDequeueTimeout = timeout;
1363 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001364
Pablo Ceballos981066c2016-02-18 12:54:37 -08001365 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001366 return NO_ERROR;
1367}
1368
Dan Stoza289ade12014-02-28 11:17:17 -08001369void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1370 // If we're here, it means that a producer we were connected to died.
1371 // We're guaranteed that we are still connected to it because we remove
1372 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1373 // without synchronization here.
1374 int api = mCore->mConnectedApi;
1375 disconnect(api);
1376}
1377
1378} // namespace android