blob: 73f61c5e98690db908083523b0b00f22830a9973 [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 {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700847 // When the queue is not empty, we need to look at the last buffer
848 // in the queue to see if we need to replace it
849 const BufferItem& last = mCore->mQueue.itemAt(
850 mCore->mQueue.size() - 1);
851 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800852
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700853 if (!last.mIsStale) {
854 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700855
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700856 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700857 // still be around. Mark it as no longer shared if this
858 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700859 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700860 mSlots[last.mSlot].mBufferState.isFree()) {
861 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 }
863 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700864 if (!mSlots[last.mSlot].mBufferState.isShared()) {
865 mCore->mActiveBuffers.erase(last.mSlot);
866 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700867 }
Dan Stoza289ade12014-02-28 11:17:17 -0800868 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800869
Dan Stoza289ade12014-02-28 11:17:17 -0800870 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700871 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800872 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800873 } else {
874 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800875 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800876 }
877 }
878
879 mCore->mBufferHasBeenQueued = true;
880 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700881 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800882
883 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800884 mCore->mTransformHint,
885 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800886
887 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800888
889 // Take a ticket for the callback functions
890 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700891
Pablo Ceballos9e314332016-01-12 13:49:19 -0800892 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800893 } // Autolock scope
894
Dan Stoza8dc55392014-11-04 11:37:46 -0800895 // Don't send the GraphicBuffer through the callback, and don't send
896 // the slot number, since the consumer shouldn't need it
897 item.mGraphicBuffer.clear();
898 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
899
900 // Call back without the main BufferQueue lock held, but with the callback
901 // lock held so we can ensure that callbacks occur in order
902 {
903 Mutex::Autolock lock(mCallbackMutex);
904 while (callbackTicket != mCurrentCallbackTicket) {
905 mCallbackCondition.wait(mCallbackMutex);
906 }
907
908 if (frameAvailableListener != NULL) {
909 frameAvailableListener->onFrameAvailable(item);
910 } else if (frameReplacedListener != NULL) {
911 frameReplacedListener->onFrameReplaced(item);
912 }
913
914 ++mCurrentCallbackTicket;
915 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800916 }
917
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000918 // Wait without lock held
919 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
920 // Waiting here allows for two full buffers to be queued but not a
921 // third. In the event that frames take varying time, this makes a
922 // small trade-off in favor of latency rather than throughput.
923 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000924 }
Dan Stoza50101d02016-04-07 16:53:23 -0700925 mLastQueueBufferFence = fence;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000926
Dan Stoza289ade12014-02-28 11:17:17 -0800927 return NO_ERROR;
928}
929
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700930status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800931 ATRACE_CALL();
932 BQ_LOGV("cancelBuffer: slot %d", slot);
933 Mutex::Autolock lock(mCore->mMutex);
934
935 if (mCore->mIsAbandoned) {
936 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700937 return NO_INIT;
938 }
939
940 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
941 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
942 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800943 }
944
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700945 if (mCore->mSharedBufferMode) {
946 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700947 return BAD_VALUE;
948 }
949
Dan Stoza3e96f192014-03-03 10:16:19 -0800950 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800951 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800952 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700953 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700954 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800955 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700956 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700957 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800958 } else if (fence == NULL) {
959 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700960 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800961 }
962
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700963 mSlots[slot].mBufferState.cancel();
964
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700965 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700966 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700967 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700968 mSlots[slot].mBufferState.mShared = false;
969 }
970
971 // Don't put the shared buffer on the free list.
972 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800973 mCore->mActiveBuffers.erase(slot);
974 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700975 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800976
Dan Stoza289ade12014-02-28 11:17:17 -0800977 mSlots[slot].mFence = fence;
978 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800979 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700980
981 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800982}
983
984int BufferQueueProducer::query(int what, int *outValue) {
985 ATRACE_CALL();
986 Mutex::Autolock lock(mCore->mMutex);
987
988 if (outValue == NULL) {
989 BQ_LOGE("query: outValue was NULL");
990 return BAD_VALUE;
991 }
992
993 if (mCore->mIsAbandoned) {
994 BQ_LOGE("query: BufferQueue has been abandoned");
995 return NO_INIT;
996 }
997
998 int value;
999 switch (what) {
1000 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001002 break;
1003 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001004 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001005 break;
1006 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001007 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001008 break;
1009 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001010 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001011 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001012 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001013 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001014 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001015 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1016 value = (mCore->mQueue.size() > 1);
1017 break;
1018 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001019 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001020 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001021 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1022 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1023 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001024 case NATIVE_WINDOW_BUFFER_AGE:
1025 if (mCore->mBufferAge > INT32_MAX) {
1026 value = 0;
1027 } else {
1028 value = static_cast<int32_t>(mCore->mBufferAge);
1029 }
1030 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001031 default:
1032 return BAD_VALUE;
1033 }
1034
1035 BQ_LOGV("query: %d? %d", what, value);
1036 *outValue = value;
1037 return NO_ERROR;
1038}
1039
Dan Stozaf0eaf252014-03-21 13:05:51 -07001040status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001041 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1042 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001043 Mutex::Autolock lock(mCore->mMutex);
1044 mConsumerName = mCore->mConsumerName;
1045 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1046 producerControlledByApp ? "true" : "false");
1047
1048 if (mCore->mIsAbandoned) {
1049 BQ_LOGE("connect: BufferQueue has been abandoned");
1050 return NO_INIT;
1051 }
1052
1053 if (mCore->mConsumerListener == NULL) {
1054 BQ_LOGE("connect: BufferQueue has no consumer");
1055 return NO_INIT;
1056 }
1057
1058 if (output == NULL) {
1059 BQ_LOGE("connect: output was NULL");
1060 return BAD_VALUE;
1061 }
1062
1063 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1064 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1065 mCore->mConnectedApi, api);
1066 return BAD_VALUE;
1067 }
1068
1069 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1070 mDequeueTimeout < 0 ?
1071 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1072 mCore->mMaxBufferCount) -
1073 mCore->getMaxBufferCountLocked();
1074 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1075 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1076 "slots. Delta = %d", delta);
1077 return BAD_VALUE;
1078 }
1079
Dan Stoza289ade12014-02-28 11:17:17 -08001080 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001081 switch (api) {
1082 case NATIVE_WINDOW_API_EGL:
1083 case NATIVE_WINDOW_API_CPU:
1084 case NATIVE_WINDOW_API_MEDIA:
1085 case NATIVE_WINDOW_API_CAMERA:
1086 mCore->mConnectedApi = api;
1087 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1088 mCore->mTransformHint,
1089 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001090
Pablo Ceballos981066c2016-02-18 12:54:37 -08001091 // Set up a death notification so that we can disconnect
1092 // automatically if the remote producer dies
1093 if (listener != NULL &&
1094 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1095 status = IInterface::asBinder(listener)->linkToDeath(
1096 static_cast<IBinder::DeathRecipient*>(this));
1097 if (status != NO_ERROR) {
1098 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1099 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001100 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001101 }
1102 mCore->mConnectedProducerListener = listener;
1103 break;
1104 default:
1105 BQ_LOGE("connect: unknown API %d", api);
1106 status = BAD_VALUE;
1107 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001108 }
1109
Pablo Ceballos981066c2016-02-18 12:54:37 -08001110 mCore->mBufferHasBeenQueued = false;
1111 mCore->mDequeueBufferCannotBlock = false;
1112 if (mDequeueTimeout < 0) {
1113 mCore->mDequeueBufferCannotBlock =
1114 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001115 }
Dan Stoza289ade12014-02-28 11:17:17 -08001116
Pablo Ceballos981066c2016-02-18 12:54:37 -08001117 mCore->mAllowAllocation = true;
1118 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001119 return status;
1120}
1121
1122status_t BufferQueueProducer::disconnect(int api) {
1123 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001124 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001125
1126 int status = NO_ERROR;
1127 sp<IConsumerListener> listener;
1128 { // Autolock scope
1129 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001130 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001131
1132 if (mCore->mIsAbandoned) {
1133 // It's not really an error to disconnect after the surface has
1134 // been abandoned; it should just be a no-op.
1135 return NO_ERROR;
1136 }
1137
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001138 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1139 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001140 // If we're asked to disconnect the currently connected api but
1141 // nobody is connected, it's not really an error.
1142 if (api == BufferQueueCore::NO_CONNECTED_API) {
1143 return NO_ERROR;
1144 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001145 }
1146
Dan Stoza289ade12014-02-28 11:17:17 -08001147 switch (api) {
1148 case NATIVE_WINDOW_API_EGL:
1149 case NATIVE_WINDOW_API_CPU:
1150 case NATIVE_WINDOW_API_MEDIA:
1151 case NATIVE_WINDOW_API_CAMERA:
1152 if (mCore->mConnectedApi == api) {
1153 mCore->freeAllBuffersLocked();
1154
1155 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001156 if (mCore->mConnectedProducerListener != NULL) {
1157 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001158 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001159 // This can fail if we're here because of the death
1160 // notification, but we just ignore it
1161 token->unlinkToDeath(
1162 static_cast<IBinder::DeathRecipient*>(this));
1163 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001164 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001165 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001166 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001167 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001168 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001169 mCore->mDequeueCondition.broadcast();
1170 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001171 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001172 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001173 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001174 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001175 }
1176 break;
1177 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001178 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001179 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001180 break;
1181 }
1182 } // Autolock scope
1183
1184 // Call back without lock held
1185 if (listener != NULL) {
1186 listener->onBuffersReleased();
1187 }
1188
1189 return status;
1190}
1191
Jesse Hall399184a2014-03-03 15:42:54 -08001192status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001193 sp<IConsumerListener> listener;
1194 { // Autolock scope
1195 Mutex::Autolock _l(mCore->mMutex);
1196 mCore->mSidebandStream = stream;
1197 listener = mCore->mConsumerListener;
1198 } // Autolock scope
1199
1200 if (listener != NULL) {
1201 listener->onSidebandStreamChanged();
1202 }
Jesse Hall399184a2014-03-03 15:42:54 -08001203 return NO_ERROR;
1204}
1205
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001206void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1207 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001208 ATRACE_CALL();
1209 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001210 size_t newBufferCount = 0;
1211 uint32_t allocWidth = 0;
1212 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001213 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001214 uint32_t allocUsage = 0;
1215 { // Autolock scope
1216 Mutex::Autolock lock(mCore->mMutex);
1217 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001218
Dan Stoza9de72932015-04-16 17:28:43 -07001219 if (!mCore->mAllowAllocation) {
1220 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1221 "BufferQueue");
1222 return;
1223 }
1224
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001225 newBufferCount = mCore->mFreeSlots.size();
1226 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001227 return;
1228 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001229
Antoine Labour78014f32014-07-15 21:17:03 -07001230 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1231 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1232 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1233 allocUsage = usage | mCore->mConsumerUsageBits;
1234
1235 mCore->mIsAllocating = true;
1236 } // Autolock scope
1237
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001238 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001239 for (size_t i = 0; i < newBufferCount; ++i) {
1240 status_t result = NO_ERROR;
1241 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1242 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1243 if (result != NO_ERROR) {
1244 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1245 " %u, usage %u)", width, height, format, usage);
1246 Mutex::Autolock lock(mCore->mMutex);
1247 mCore->mIsAllocating = false;
1248 mCore->mIsAllocatingCondition.broadcast();
1249 return;
1250 }
1251 buffers.push_back(graphicBuffer);
1252 }
1253
1254 { // Autolock scope
1255 Mutex::Autolock lock(mCore->mMutex);
1256 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1257 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001258 PixelFormat checkFormat = format != 0 ?
1259 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001260 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1261 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1262 checkFormat != allocFormat || checkUsage != allocUsage) {
1263 // Something changed while we released the lock. Retry.
1264 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1265 mCore->mIsAllocating = false;
1266 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001267 continue;
1268 }
1269
Antoine Labour78014f32014-07-15 21:17:03 -07001270 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001271 if (mCore->mFreeSlots.empty()) {
1272 BQ_LOGV("allocateBuffers: a slot was occupied while "
1273 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001274 continue;
1275 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001276 auto slot = mCore->mFreeSlots.begin();
1277 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1278 mSlots[*slot].mGraphicBuffer = buffers[i];
1279 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001280
1281 // freeBufferLocked puts this slot on the free slots list. Since
1282 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001283 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001284
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001285 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1286 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001287
1288 // Make sure the erase is done after all uses of the slot
1289 // iterator since it will be invalid after this point.
1290 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001291 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001292
Antoine Labour78014f32014-07-15 21:17:03 -07001293 mCore->mIsAllocating = false;
1294 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001295 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001296 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001297 }
1298}
1299
Dan Stoza9de72932015-04-16 17:28:43 -07001300status_t BufferQueueProducer::allowAllocation(bool allow) {
1301 ATRACE_CALL();
1302 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1303
1304 Mutex::Autolock lock(mCore->mMutex);
1305 mCore->mAllowAllocation = allow;
1306 return NO_ERROR;
1307}
1308
Dan Stoza812ed062015-06-02 15:45:22 -07001309status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1310 ATRACE_CALL();
1311 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1312
1313 Mutex::Autolock lock(mCore->mMutex);
1314 mCore->mGenerationNumber = generationNumber;
1315 return NO_ERROR;
1316}
1317
Dan Stozac6f30bd2015-06-08 09:32:50 -07001318String8 BufferQueueProducer::getConsumerName() const {
1319 ATRACE_CALL();
1320 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1321 return mConsumerName;
1322}
1323
Dan Stoza7dde5992015-05-22 09:51:44 -07001324uint64_t BufferQueueProducer::getNextFrameNumber() const {
1325 ATRACE_CALL();
1326
1327 Mutex::Autolock lock(mCore->mMutex);
1328 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1329 return nextFrameNumber;
1330}
1331
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001332status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001333 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001334 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001335
1336 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001337 if (!sharedBufferMode) {
1338 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001339 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001340 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001341 return NO_ERROR;
1342}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001343
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001344status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1345 ATRACE_CALL();
1346 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1347
1348 Mutex::Autolock lock(mCore->mMutex);
1349
1350 mCore->mAutoRefresh = autoRefresh;
1351 return NO_ERROR;
1352}
1353
Dan Stoza127fc632015-06-30 13:43:32 -07001354status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1355 ATRACE_CALL();
1356 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1357
Pablo Ceballos981066c2016-02-18 12:54:37 -08001358 Mutex::Autolock lock(mCore->mMutex);
1359 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1360 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1361 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1362 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1363 "available slots. Delta = %d", delta);
1364 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001365 }
1366
Pablo Ceballos981066c2016-02-18 12:54:37 -08001367 mDequeueTimeout = timeout;
1368 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001369
Pablo Ceballos981066c2016-02-18 12:54:37 -08001370 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001371 return NO_ERROR;
1372}
1373
Dan Stoza50101d02016-04-07 16:53:23 -07001374status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
1375 sp<Fence>* outFence) {
1376 ATRACE_CALL();
1377 BQ_LOGV("getLastQueuedBuffer");
1378
1379 Mutex::Autolock lock(mCore->mMutex);
1380 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1381 *outBuffer = nullptr;
1382 *outFence = Fence::NO_FENCE;
1383 return NO_ERROR;
1384 }
1385
1386 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1387 *outFence = mLastQueueBufferFence;
1388
1389 return NO_ERROR;
1390}
1391
Dan Stoza289ade12014-02-28 11:17:17 -08001392void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1393 // If we're here, it means that a producer we were connected to died.
1394 // We're guaranteed that we are still connected to it because we remove
1395 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1396 // without synchronization here.
1397 int api = mCore->mConnectedApi;
1398 disconnect(api);
1399}
1400
1401} // namespace android