blob: 3e26e058fbc80c3366d28e4a8bceb75b9d8fdc1a [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>
John Reck1a61da52016-04-28 13:18:15 -070034#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080035#include <gui/IConsumerListener.h>
36#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038
39#include <utils/Log.h>
40#include <utils/Trace.h>
41
42namespace android {
43
44BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
45 mCore(core),
46 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070047 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070048 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080049 mLastQueueBufferFence(Fence::NO_FENCE),
50 mCallbackMutex(),
51 mNextCallbackTicket(0),
52 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070053 mCallbackCondition(),
54 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080055
56BufferQueueProducer::~BufferQueueProducer() {}
57
58status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
59 ATRACE_CALL();
60 BQ_LOGV("requestBuffer: slot %d", slot);
61 Mutex::Autolock lock(mCore->mMutex);
62
63 if (mCore->mIsAbandoned) {
64 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
65 return NO_INIT;
66 }
67
Pablo Ceballos583b1b32015-09-03 18:23:52 -070068 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
69 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
70 return NO_INIT;
71 }
72
Dan Stoza3e96f192014-03-03 10:16:19 -080073 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080074 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080075 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080076 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070077 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080078 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070079 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080080 return BAD_VALUE;
81 }
82
83 mSlots[slot].mRequestBufferCalled = true;
84 *buf = mSlots[slot].mGraphicBuffer;
85 return NO_ERROR;
86}
87
Pablo Ceballosfa455352015-08-12 17:47:47 -070088status_t BufferQueueProducer::setMaxDequeuedBufferCount(
89 int maxDequeuedBuffers) {
90 ATRACE_CALL();
91 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
92 maxDequeuedBuffers);
93
Pablo Ceballos981066c2016-02-18 12:54:37 -080094 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070095 { // Autolock scope
96 Mutex::Autolock lock(mCore->mMutex);
97 mCore->waitWhileAllocatingLocked();
98
99 if (mCore->mIsAbandoned) {
100 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
101 "abandoned");
102 return NO_INIT;
103 }
104
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700105 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
106 return NO_ERROR;
107 }
108
Pablo Ceballos72daab62015-12-07 16:38:43 -0800109 // The new maxDequeuedBuffer count should not be violated by the number
110 // of currently dequeued buffers
111 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800112 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700113 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800114 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700115 }
116 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800117 if (dequeuedCount > maxDequeuedBuffers) {
118 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
119 "count (%d) exceeds the current dequeued buffer count (%d)",
120 maxDequeuedBuffers, dequeuedCount);
121 return BAD_VALUE;
122 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700123
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700124 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700125 bufferCount += maxDequeuedBuffers;
126
127 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
128 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
129 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
130 return BAD_VALUE;
131 }
132
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700133 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700134 if (bufferCount < minBufferSlots) {
135 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
136 "less than minimum %d", bufferCount, minBufferSlots);
137 return BAD_VALUE;
138 }
139
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700140 if (bufferCount > mCore->mMaxBufferCount) {
141 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700142 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
143 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
144 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
145 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700146 return BAD_VALUE;
147 }
148
Pablo Ceballos72daab62015-12-07 16:38:43 -0800149 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800150 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800151 return BAD_VALUE;
152 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700153 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800154 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800156 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800157 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700158 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700159 } // Autolock scope
160
161 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800162 if (listener != NULL) {
163 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700164 }
165
166 return NO_ERROR;
167}
168
169status_t BufferQueueProducer::setAsyncMode(bool async) {
170 ATRACE_CALL();
171 BQ_LOGV("setAsyncMode: async = %d", async);
172
Pablo Ceballos981066c2016-02-18 12:54:37 -0800173 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700174 { // Autolock scope
175 Mutex::Autolock lock(mCore->mMutex);
176 mCore->waitWhileAllocatingLocked();
177
178 if (mCore->mIsAbandoned) {
179 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
180 return NO_INIT;
181 }
182
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700183 if (async == mCore->mAsyncMode) {
184 return NO_ERROR;
185 }
186
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700188 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
189 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700190 BQ_LOGE("setAsyncMode(%d): this call would cause the "
191 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700192 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
193 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
194 mCore->mMaxDequeuedBufferCount,
195 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700196 return BAD_VALUE;
197 }
198
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800199 int delta = mCore->getMaxBufferCountLocked(async,
200 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
201 - mCore->getMaxBufferCountLocked();
202
Pablo Ceballos981066c2016-02-18 12:54:37 -0800203 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800204 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
205 "available slots. Delta = %d", delta);
206 return BAD_VALUE;
207 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700208 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800209 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700210 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700211 if (delta < 0) {
212 listener = mCore->mConsumerListener;
213 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700214 } // Autolock scope
215
216 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800217 if (listener != NULL) {
218 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700219 }
220 return NO_ERROR;
221}
222
Dan Stoza5ecfb682016-01-04 17:01:02 -0800223int BufferQueueProducer::getFreeBufferLocked() const {
224 if (mCore->mFreeBuffers.empty()) {
225 return BufferQueueCore::INVALID_BUFFER_SLOT;
226 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800227 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800228 mCore->mFreeBuffers.pop_front();
229 return slot;
230}
231
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800232int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800233 if (mCore->mFreeSlots.empty()) {
234 return BufferQueueCore::INVALID_BUFFER_SLOT;
235 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800236 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800237 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800238 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800239}
240
241status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800242 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800243 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
244 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800245 bool tryAgain = true;
246 while (tryAgain) {
247 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800248 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800249 return NO_INIT;
250 }
251
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 int dequeuedCount = 0;
253 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800254 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700255 if (mSlots[s].mBufferState.isDequeued()) {
256 ++dequeuedCount;
257 }
258 if (mSlots[s].mBufferState.isAcquired()) {
259 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800260 }
261 }
262
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700263 // Producers are not allowed to dequeue more than
264 // mMaxDequeuedBufferCount buffers.
265 // This check is only done if a buffer has already been queued
266 if (mCore->mBufferHasBeenQueued &&
267 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
268 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800269 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800270 return INVALID_OPERATION;
271 }
272
Dan Stoza0de7ea72015-04-23 13:20:51 -0700273 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
274
Dan Stozaae3c3682014-04-18 15:43:35 -0700275 // If we disconnect and reconnect quickly, we can be in a state where
276 // our slots are empty but we have many buffers in the queue. This can
277 // cause us to run out of memory if we outrun the consumer. Wait here if
278 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700280 bool tooManyBuffers = mCore->mQueue.size()
281 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700282 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800283 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700284 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700285 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700286 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700287 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700288 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700289 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700290 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800291 } else {
292 if (caller == FreeSlotCaller::Dequeue) {
293 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800294 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800295 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
296 *found = slot;
297 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800298 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800299 }
300 } else {
301 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800302 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800303 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
304 *found = slot;
305 } else {
306 *found = getFreeBufferLocked();
307 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700308 }
309 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700310 }
311
312 // If no buffer is found, or if the queue has too many buffers
313 // outstanding, wait for a buffer to be acquired or released, or for the
314 // max buffer count to change.
315 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
316 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800317 if (tryAgain) {
318 // Return an error if we're in non-blocking mode (producer and
319 // consumer are controlled by the application).
320 // However, the consumer is allowed to briefly acquire an extra
321 // buffer (which could cause us to have to wait here), which is
322 // okay, since it is only used to implement an atomic acquire +
323 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700324 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
326 return WOULD_BLOCK;
327 }
Dan Stoza127fc632015-06-30 13:43:32 -0700328 if (mDequeueTimeout >= 0) {
329 status_t result = mCore->mDequeueCondition.waitRelative(
330 mCore->mMutex, mDequeueTimeout);
331 if (result == TIMED_OUT) {
332 return result;
333 }
334 } else {
335 mCore->mDequeueCondition.wait(mCore->mMutex);
336 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800337 }
338 } // while (tryAgain)
339
340 return NO_ERROR;
341}
342
Dan Stoza289ade12014-02-28 11:17:17 -0800343status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700344 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
345 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800346 ATRACE_CALL();
347 { // Autolock scope
348 Mutex::Autolock lock(mCore->mMutex);
349 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700350
351 if (mCore->mIsAbandoned) {
352 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
353 return NO_INIT;
354 }
355
356 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
357 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
358 return NO_INIT;
359 }
Dan Stoza289ade12014-02-28 11:17:17 -0800360 } // Autolock scope
361
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700362 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
363 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800364
365 if ((width && !height) || (!width && height)) {
366 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
367 return BAD_VALUE;
368 }
369
370 status_t returnFlags = NO_ERROR;
371 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
372 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800374
375 { // Autolock scope
376 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700377 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800378
379 if (format == 0) {
380 format = mCore->mDefaultBufferFormat;
381 }
382
383 // Enable the usage bits the consumer requested
384 usage |= mCore->mConsumerUsageBits;
385
Dan Stoza9de72932015-04-16 17:28:43 -0700386 const bool useDefaultSize = !width && !height;
387 if (useDefaultSize) {
388 width = mCore->mDefaultWidth;
389 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800390 }
Dan Stoza289ade12014-02-28 11:17:17 -0800391
Pablo Ceballos981066c2016-02-18 12:54:37 -0800392 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700393 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800394 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800395 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700396 if (status != NO_ERROR) {
397 return status;
398 }
399
400 // This should not happen
401 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
402 BQ_LOGE("dequeueBuffer: no available buffer slots");
403 return -EBUSY;
404 }
405
406 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
407
408 // If we are not allowed to allocate new buffers,
409 // waitForFreeSlotThenRelock must have returned a slot containing a
410 // buffer. If this buffer would require reallocation to meet the
411 // requested attributes, we free it and attempt to get another one.
412 if (!mCore->mAllowAllocation) {
413 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700414 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700415 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
416 "buffer");
417 return BAD_VALUE;
418 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800419 mCore->mFreeSlots.insert(found);
420 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700421 found = BufferItem::INVALID_BUFFER_SLOT;
422 continue;
423 }
424 }
Dan Stoza289ade12014-02-28 11:17:17 -0800425 }
426
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800427 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700428 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800429 buffer->needsReallocation(width, height, format, usage)) {
430 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
431 "buffer");
432
433 return BAD_VALUE;
434 }
435
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700436 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800437 mCore->mActiveBuffers.insert(found);
438 }
Dan Stoza289ade12014-02-28 11:17:17 -0800439 *outSlot = found;
440 ATRACE_BUFFER_INDEX(found);
441
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 attachedByConsumer = mSlots[found].mNeedsReallocation;
443 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800444
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700445 mSlots[found].mBufferState.dequeue();
446
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700447 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700448 // first buffer that is dequeued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700449 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700450 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700451 mCore->mSharedBufferSlot = found;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700452 mSlots[found].mBufferState.mShared = true;
453 }
Dan Stoza289ade12014-02-28 11:17:17 -0800454
Dan Stoza289ade12014-02-28 11:17:17 -0800455 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700456 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800457 {
458 mSlots[found].mAcquireCalled = false;
459 mSlots[found].mGraphicBuffer = NULL;
460 mSlots[found].mRequestBufferCalled = false;
461 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
462 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
463 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800464 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700465 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800466
467 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800468 } else {
469 // We add 1 because that will be the frame number when this buffer
470 // is queued
471 mCore->mBufferAge =
472 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800473 }
474
Dan Stoza800b41a2015-04-28 14:20:04 -0700475 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
476 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800477
Dan Stoza289ade12014-02-28 11:17:17 -0800478 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
479 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
480 "slot=%d w=%d h=%d format=%u",
481 found, buffer->width, buffer->height, buffer->format);
482 }
483
484 eglDisplay = mSlots[found].mEglDisplay;
485 eglFence = mSlots[found].mEglFence;
486 *outFence = mSlots[found].mFence;
487 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
488 mSlots[found].mFence = Fence::NO_FENCE;
489 } // Autolock scope
490
491 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
492 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700493 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800494 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800495 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800496 { // Autolock scope
497 Mutex::Autolock lock(mCore->mMutex);
498
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700499 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
500 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
501 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
502 }
503
504 mCore->mIsAllocating = false;
505 mCore->mIsAllocatingCondition.broadcast();
506
507 if (graphicBuffer == NULL) {
508 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
509 return error;
510 }
511
Dan Stoza289ade12014-02-28 11:17:17 -0800512 if (mCore->mIsAbandoned) {
513 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
514 return NO_INIT;
515 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800516
Pablo Ceballos9e314332016-01-12 13:49:19 -0800517 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800518 } // Autolock scope
519 }
520
Dan Stoza9f3053d2014-03-06 15:14:33 -0800521 if (attachedByConsumer) {
522 returnFlags |= BUFFER_NEEDS_REALLOCATION;
523 }
524
Dan Stoza289ade12014-02-28 11:17:17 -0800525 if (eglFence != EGL_NO_SYNC_KHR) {
526 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
527 1000000000);
528 // If something goes wrong, log the error, but return the buffer without
529 // synchronizing access to it. It's too late at this point to abort the
530 // dequeue operation.
531 if (result == EGL_FALSE) {
532 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
533 eglGetError());
534 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
535 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
536 }
537 eglDestroySyncKHR(eglDisplay, eglFence);
538 }
539
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700540 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
541 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800542 mSlots[*outSlot].mFrameNumber,
543 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
544
545 return returnFlags;
546}
547
Dan Stoza9f3053d2014-03-06 15:14:33 -0800548status_t BufferQueueProducer::detachBuffer(int slot) {
549 ATRACE_CALL();
550 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700551 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos981066c2016-02-18 12:54:37 -0800552 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800553
Pablo Ceballos981066c2016-02-18 12:54:37 -0800554 if (mCore->mIsAbandoned) {
555 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
556 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800557 }
558
Pablo Ceballos981066c2016-02-18 12:54:37 -0800559 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
560 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
561 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700562 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800563
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700564 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
565 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800566 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700567 }
568
Pablo Ceballos981066c2016-02-18 12:54:37 -0800569 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
570 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
571 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
572 return BAD_VALUE;
573 } else if (!mSlots[slot].mBufferState.isDequeued()) {
574 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
575 "(state = %s)", slot, mSlots[slot].mBufferState.string());
576 return BAD_VALUE;
577 } else if (!mSlots[slot].mRequestBufferCalled) {
578 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
579 slot);
580 return BAD_VALUE;
581 }
582
583 mSlots[slot].mBufferState.detachProducer();
584 mCore->mActiveBuffers.erase(slot);
585 mCore->mFreeSlots.insert(slot);
586 mCore->clearBufferSlotLocked(slot);
587 mCore->mDequeueCondition.broadcast();
588 VALIDATE_CONSISTENCY();
589
Dan Stoza9f3053d2014-03-06 15:14:33 -0800590 return NO_ERROR;
591}
592
Dan Stozad9822a32014-03-28 15:25:31 -0700593status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
594 sp<Fence>* outFence) {
595 ATRACE_CALL();
596
597 if (outBuffer == NULL) {
598 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
599 return BAD_VALUE;
600 } else if (outFence == NULL) {
601 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
602 return BAD_VALUE;
603 }
604
Pablo Ceballos981066c2016-02-18 12:54:37 -0800605 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700606
Pablo Ceballos981066c2016-02-18 12:54:37 -0800607 if (mCore->mIsAbandoned) {
608 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
609 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700610 }
611
Pablo Ceballos981066c2016-02-18 12:54:37 -0800612 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
613 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
614 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700615 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800616
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700617 if (mCore->mSharedBufferMode) {
618 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
619 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800620 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700621 }
622
Pablo Ceballos981066c2016-02-18 12:54:37 -0800623 mCore->waitWhileAllocatingLocked();
624
625 if (mCore->mFreeBuffers.empty()) {
626 return NO_MEMORY;
627 }
628
629 int found = mCore->mFreeBuffers.front();
630 mCore->mFreeBuffers.remove(found);
631 mCore->mFreeSlots.insert(found);
632
633 BQ_LOGV("detachNextBuffer detached slot %d", found);
634
635 *outBuffer = mSlots[found].mGraphicBuffer;
636 *outFence = mSlots[found].mFence;
637 mCore->clearBufferSlotLocked(found);
638 VALIDATE_CONSISTENCY();
639
Dan Stozad9822a32014-03-28 15:25:31 -0700640 return NO_ERROR;
641}
642
Dan Stoza9f3053d2014-03-06 15:14:33 -0800643status_t BufferQueueProducer::attachBuffer(int* outSlot,
644 const sp<android::GraphicBuffer>& buffer) {
645 ATRACE_CALL();
646
647 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700648 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800649 return BAD_VALUE;
650 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700651 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800652 return BAD_VALUE;
653 }
654
655 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700656
657 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700658 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700659 return NO_INIT;
660 }
661
662 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700663 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700664 return NO_INIT;
665 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800666
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700667 if (mCore->mSharedBufferMode) {
668 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700669 return BAD_VALUE;
670 }
671
Dan Stoza812ed062015-06-02 15:45:22 -0700672 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
673 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
674 "[queue %u]", buffer->getGenerationNumber(),
675 mCore->mGenerationNumber);
676 return BAD_VALUE;
677 }
678
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700679 mCore->waitWhileAllocatingLocked();
680
Dan Stoza9f3053d2014-03-06 15:14:33 -0800681 status_t returnFlags = NO_ERROR;
682 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800683 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800684 if (status != NO_ERROR) {
685 return status;
686 }
687
688 // This should not happen
689 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700690 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800691 return -EBUSY;
692 }
693
694 *outSlot = found;
695 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700696 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697 *outSlot, returnFlags);
698
699 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700700 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800701 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
702 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700703 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800704 mSlots[*outSlot].mAcquireCalled = false;
705 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800706 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700707
Dan Stoza9f3053d2014-03-06 15:14:33 -0800708 return returnFlags;
709}
710
Dan Stoza289ade12014-02-28 11:17:17 -0800711status_t BufferQueueProducer::queueBuffer(int slot,
712 const QueueBufferInput &input, QueueBufferOutput *output) {
713 ATRACE_CALL();
714 ATRACE_BUFFER_INDEX(slot);
715
716 int64_t timestamp;
717 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800718 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700719 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800720 int scalingMode;
721 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700722 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800723 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800724 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700725 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700726 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800727
728 if (fence == NULL) {
729 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800730 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800731 }
732
733 switch (scalingMode) {
734 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
735 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
736 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
737 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
738 break;
739 default:
740 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800741 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800742 }
743
Dan Stoza8dc55392014-11-04 11:37:46 -0800744 sp<IConsumerListener> frameAvailableListener;
745 sp<IConsumerListener> frameReplacedListener;
746 int callbackTicket = 0;
747 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800748 { // Autolock scope
749 Mutex::Autolock lock(mCore->mMutex);
750
751 if (mCore->mIsAbandoned) {
752 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
753 return NO_INIT;
754 }
755
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700756 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
757 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
758 return NO_INIT;
759 }
760
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800761 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800762 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800763 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800764 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700765 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800766 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700767 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800768 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800769 } else if (!mSlots[slot].mRequestBufferCalled) {
770 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
771 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800772 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800773 }
774
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700775 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700776 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700777 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700778 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700779 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700780 mSlots[slot].mBufferState.mShared = true;
781 }
782
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800783 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700784 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800785 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800786 crop.left, crop.top, crop.right, crop.bottom, transform,
787 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800788
789 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
790 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700791 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800792 crop.intersect(bufferRect, &croppedRect);
793 if (croppedRect != crop) {
794 BQ_LOGE("queueBuffer: crop rect is not contained within the "
795 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800796 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800797 }
798
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800799 // Override UNKNOWN dataspace with consumer default
800 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
801 dataSpace = mCore->mDefaultBufferDataSpace;
802 }
803
Dan Stoza289ade12014-02-28 11:17:17 -0800804 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700805 mSlots[slot].mBufferState.queue();
806
Dan Stoza289ade12014-02-28 11:17:17 -0800807 ++mCore->mFrameCounter;
808 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
809
Dan Stoza289ade12014-02-28 11:17:17 -0800810 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
811 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
812 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800813 item.mTransform = transform &
814 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800815 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800816 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
817 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800818 item.mTimestamp = timestamp;
819 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800820 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800821 item.mFrameNumber = mCore->mFrameCounter;
822 item.mSlot = slot;
823 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700824 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700825 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700826 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700827 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700828 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700829 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800830
Ruben Brunk1681d952014-06-27 15:51:55 -0700831 mStickyTransform = stickyTransform;
832
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700833 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700834 if (mCore->mSharedBufferMode) {
835 mCore->mSharedBufferCache.crop = crop;
836 mCore->mSharedBufferCache.transform = transform;
837 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700838 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700839 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700840 }
841
Dan Stoza289ade12014-02-28 11:17:17 -0800842 if (mCore->mQueue.empty()) {
843 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
844 // and simply queue this buffer
845 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800846 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800847 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700848 // When the queue is not empty, we need to look at the last buffer
849 // in the queue to see if we need to replace it
850 const BufferItem& last = mCore->mQueue.itemAt(
851 mCore->mQueue.size() - 1);
852 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800853
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700854 if (!last.mIsStale) {
855 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700856
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700857 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700858 // still be around. Mark it as no longer shared if this
859 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700860 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700861 mSlots[last.mSlot].mBufferState.isFree()) {
862 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700863 }
864 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700865 if (!mSlots[last.mSlot].mBufferState.isShared()) {
866 mCore->mActiveBuffers.erase(last.mSlot);
867 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700868 }
Dan Stoza289ade12014-02-28 11:17:17 -0800869 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800870
Dan Stoza289ade12014-02-28 11:17:17 -0800871 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700872 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800873 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800874 } else {
875 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800876 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800877 }
878 }
879
880 mCore->mBufferHasBeenQueued = true;
881 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700882 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800883
884 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800885 mCore->mTransformHint,
886 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800887
888 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800889
890 // Take a ticket for the callback functions
891 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700892
Pablo Ceballos9e314332016-01-12 13:49:19 -0800893 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800894 } // Autolock scope
895
Dan Stoza8dc55392014-11-04 11:37:46 -0800896 // Don't send the GraphicBuffer through the callback, and don't send
897 // the slot number, since the consumer shouldn't need it
898 item.mGraphicBuffer.clear();
899 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
900
901 // Call back without the main BufferQueue lock held, but with the callback
902 // lock held so we can ensure that callbacks occur in order
903 {
904 Mutex::Autolock lock(mCallbackMutex);
905 while (callbackTicket != mCurrentCallbackTicket) {
906 mCallbackCondition.wait(mCallbackMutex);
907 }
908
909 if (frameAvailableListener != NULL) {
910 frameAvailableListener->onFrameAvailable(item);
911 } else if (frameReplacedListener != NULL) {
912 frameReplacedListener->onFrameReplaced(item);
913 }
914
915 ++mCurrentCallbackTicket;
916 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800917 }
918
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000919 // Wait without lock held
920 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
921 // Waiting here allows for two full buffers to be queued but not a
922 // third. In the event that frames take varying time, this makes a
923 // small trade-off in favor of latency rather than throughput.
924 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000925 }
Dan Stoza50101d02016-04-07 16:53:23 -0700926 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700927 mLastQueuedCrop = item.mCrop;
928 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000929
Dan Stoza289ade12014-02-28 11:17:17 -0800930 return NO_ERROR;
931}
932
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700933status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800934 ATRACE_CALL();
935 BQ_LOGV("cancelBuffer: slot %d", slot);
936 Mutex::Autolock lock(mCore->mMutex);
937
938 if (mCore->mIsAbandoned) {
939 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700940 return NO_INIT;
941 }
942
943 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
944 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
945 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800946 }
947
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700948 if (mCore->mSharedBufferMode) {
949 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700950 return BAD_VALUE;
951 }
952
Dan Stoza3e96f192014-03-03 10:16:19 -0800953 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800954 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800955 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700956 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700957 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800958 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700959 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700960 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800961 } else if (fence == NULL) {
962 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700963 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800964 }
965
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700966 mSlots[slot].mBufferState.cancel();
967
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700968 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700969 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700970 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700971 mSlots[slot].mBufferState.mShared = false;
972 }
973
974 // Don't put the shared buffer on the free list.
975 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800976 mCore->mActiveBuffers.erase(slot);
977 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700978 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800979
Dan Stoza289ade12014-02-28 11:17:17 -0800980 mSlots[slot].mFence = fence;
981 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800982 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700983
984 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800985}
986
987int BufferQueueProducer::query(int what, int *outValue) {
988 ATRACE_CALL();
989 Mutex::Autolock lock(mCore->mMutex);
990
991 if (outValue == NULL) {
992 BQ_LOGE("query: outValue was NULL");
993 return BAD_VALUE;
994 }
995
996 if (mCore->mIsAbandoned) {
997 BQ_LOGE("query: BufferQueue has been abandoned");
998 return NO_INIT;
999 }
1000
1001 int value;
1002 switch (what) {
1003 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001004 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001005 break;
1006 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001007 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001008 break;
1009 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001010 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001011 break;
1012 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001013 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001014 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001015 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001016 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001017 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001018 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1019 value = (mCore->mQueue.size() > 1);
1020 break;
1021 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001022 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001023 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001024 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1025 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1026 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001027 case NATIVE_WINDOW_BUFFER_AGE:
1028 if (mCore->mBufferAge > INT32_MAX) {
1029 value = 0;
1030 } else {
1031 value = static_cast<int32_t>(mCore->mBufferAge);
1032 }
1033 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001034 default:
1035 return BAD_VALUE;
1036 }
1037
1038 BQ_LOGV("query: %d? %d", what, value);
1039 *outValue = value;
1040 return NO_ERROR;
1041}
1042
Dan Stozaf0eaf252014-03-21 13:05:51 -07001043status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001044 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1045 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001046 Mutex::Autolock lock(mCore->mMutex);
1047 mConsumerName = mCore->mConsumerName;
1048 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1049 producerControlledByApp ? "true" : "false");
1050
1051 if (mCore->mIsAbandoned) {
1052 BQ_LOGE("connect: BufferQueue has been abandoned");
1053 return NO_INIT;
1054 }
1055
1056 if (mCore->mConsumerListener == NULL) {
1057 BQ_LOGE("connect: BufferQueue has no consumer");
1058 return NO_INIT;
1059 }
1060
1061 if (output == NULL) {
1062 BQ_LOGE("connect: output was NULL");
1063 return BAD_VALUE;
1064 }
1065
1066 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1067 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1068 mCore->mConnectedApi, api);
1069 return BAD_VALUE;
1070 }
1071
1072 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1073 mDequeueTimeout < 0 ?
1074 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1075 mCore->mMaxBufferCount) -
1076 mCore->getMaxBufferCountLocked();
1077 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1078 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1079 "slots. Delta = %d", delta);
1080 return BAD_VALUE;
1081 }
1082
Dan Stoza289ade12014-02-28 11:17:17 -08001083 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001084 switch (api) {
1085 case NATIVE_WINDOW_API_EGL:
1086 case NATIVE_WINDOW_API_CPU:
1087 case NATIVE_WINDOW_API_MEDIA:
1088 case NATIVE_WINDOW_API_CAMERA:
1089 mCore->mConnectedApi = api;
1090 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1091 mCore->mTransformHint,
1092 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001093
Pablo Ceballos981066c2016-02-18 12:54:37 -08001094 // Set up a death notification so that we can disconnect
1095 // automatically if the remote producer dies
1096 if (listener != NULL &&
1097 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1098 status = IInterface::asBinder(listener)->linkToDeath(
1099 static_cast<IBinder::DeathRecipient*>(this));
1100 if (status != NO_ERROR) {
1101 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1102 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001103 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001104 }
1105 mCore->mConnectedProducerListener = listener;
1106 break;
1107 default:
1108 BQ_LOGE("connect: unknown API %d", api);
1109 status = BAD_VALUE;
1110 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001111 }
1112
Pablo Ceballos981066c2016-02-18 12:54:37 -08001113 mCore->mBufferHasBeenQueued = false;
1114 mCore->mDequeueBufferCannotBlock = false;
1115 if (mDequeueTimeout < 0) {
1116 mCore->mDequeueBufferCannotBlock =
1117 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001118 }
Dan Stoza289ade12014-02-28 11:17:17 -08001119
Pablo Ceballos981066c2016-02-18 12:54:37 -08001120 mCore->mAllowAllocation = true;
1121 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001122 return status;
1123}
1124
1125status_t BufferQueueProducer::disconnect(int api) {
1126 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001127 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001128
1129 int status = NO_ERROR;
1130 sp<IConsumerListener> listener;
1131 { // Autolock scope
1132 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001133 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001134
1135 if (mCore->mIsAbandoned) {
1136 // It's not really an error to disconnect after the surface has
1137 // been abandoned; it should just be a no-op.
1138 return NO_ERROR;
1139 }
1140
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001141 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1142 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001143 // If we're asked to disconnect the currently connected api but
1144 // nobody is connected, it's not really an error.
1145 if (api == BufferQueueCore::NO_CONNECTED_API) {
1146 return NO_ERROR;
1147 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001148 }
1149
Dan Stoza289ade12014-02-28 11:17:17 -08001150 switch (api) {
1151 case NATIVE_WINDOW_API_EGL:
1152 case NATIVE_WINDOW_API_CPU:
1153 case NATIVE_WINDOW_API_MEDIA:
1154 case NATIVE_WINDOW_API_CAMERA:
1155 if (mCore->mConnectedApi == api) {
1156 mCore->freeAllBuffersLocked();
1157
1158 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001159 if (mCore->mConnectedProducerListener != NULL) {
1160 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001161 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001162 // This can fail if we're here because of the death
1163 // notification, but we just ignore it
1164 token->unlinkToDeath(
1165 static_cast<IBinder::DeathRecipient*>(this));
1166 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001167 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001168 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001169 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001170 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001171 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001172 mCore->mDequeueCondition.broadcast();
1173 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001174 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001175 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001176 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001177 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001178 }
1179 break;
1180 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001181 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001182 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001183 break;
1184 }
1185 } // Autolock scope
1186
1187 // Call back without lock held
1188 if (listener != NULL) {
1189 listener->onBuffersReleased();
1190 }
1191
1192 return status;
1193}
1194
Jesse Hall399184a2014-03-03 15:42:54 -08001195status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001196 sp<IConsumerListener> listener;
1197 { // Autolock scope
1198 Mutex::Autolock _l(mCore->mMutex);
1199 mCore->mSidebandStream = stream;
1200 listener = mCore->mConsumerListener;
1201 } // Autolock scope
1202
1203 if (listener != NULL) {
1204 listener->onSidebandStreamChanged();
1205 }
Jesse Hall399184a2014-03-03 15:42:54 -08001206 return NO_ERROR;
1207}
1208
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001209void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1210 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001211 ATRACE_CALL();
1212 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001213 size_t newBufferCount = 0;
1214 uint32_t allocWidth = 0;
1215 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001216 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001217 uint32_t allocUsage = 0;
1218 { // Autolock scope
1219 Mutex::Autolock lock(mCore->mMutex);
1220 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001221
Dan Stoza9de72932015-04-16 17:28:43 -07001222 if (!mCore->mAllowAllocation) {
1223 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1224 "BufferQueue");
1225 return;
1226 }
1227
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001228 newBufferCount = mCore->mFreeSlots.size();
1229 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001230 return;
1231 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001232
Antoine Labour78014f32014-07-15 21:17:03 -07001233 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1234 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1235 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1236 allocUsage = usage | mCore->mConsumerUsageBits;
1237
1238 mCore->mIsAllocating = true;
1239 } // Autolock scope
1240
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001241 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001242 for (size_t i = 0; i < newBufferCount; ++i) {
1243 status_t result = NO_ERROR;
1244 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1245 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1246 if (result != NO_ERROR) {
1247 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1248 " %u, usage %u)", width, height, format, usage);
1249 Mutex::Autolock lock(mCore->mMutex);
1250 mCore->mIsAllocating = false;
1251 mCore->mIsAllocatingCondition.broadcast();
1252 return;
1253 }
1254 buffers.push_back(graphicBuffer);
1255 }
1256
1257 { // Autolock scope
1258 Mutex::Autolock lock(mCore->mMutex);
1259 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1260 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001261 PixelFormat checkFormat = format != 0 ?
1262 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001263 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1264 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1265 checkFormat != allocFormat || checkUsage != allocUsage) {
1266 // Something changed while we released the lock. Retry.
1267 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1268 mCore->mIsAllocating = false;
1269 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001270 continue;
1271 }
1272
Antoine Labour78014f32014-07-15 21:17:03 -07001273 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001274 if (mCore->mFreeSlots.empty()) {
1275 BQ_LOGV("allocateBuffers: a slot was occupied while "
1276 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001277 continue;
1278 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001279 auto slot = mCore->mFreeSlots.begin();
1280 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1281 mSlots[*slot].mGraphicBuffer = buffers[i];
1282 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001283
1284 // freeBufferLocked puts this slot on the free slots list. Since
1285 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001286 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001287
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001288 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1289 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001290
1291 // Make sure the erase is done after all uses of the slot
1292 // iterator since it will be invalid after this point.
1293 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001294 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001295
Antoine Labour78014f32014-07-15 21:17:03 -07001296 mCore->mIsAllocating = false;
1297 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001298 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001299 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001300 }
1301}
1302
Dan Stoza9de72932015-04-16 17:28:43 -07001303status_t BufferQueueProducer::allowAllocation(bool allow) {
1304 ATRACE_CALL();
1305 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1306
1307 Mutex::Autolock lock(mCore->mMutex);
1308 mCore->mAllowAllocation = allow;
1309 return NO_ERROR;
1310}
1311
Dan Stoza812ed062015-06-02 15:45:22 -07001312status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1313 ATRACE_CALL();
1314 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1315
1316 Mutex::Autolock lock(mCore->mMutex);
1317 mCore->mGenerationNumber = generationNumber;
1318 return NO_ERROR;
1319}
1320
Dan Stozac6f30bd2015-06-08 09:32:50 -07001321String8 BufferQueueProducer::getConsumerName() const {
1322 ATRACE_CALL();
1323 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1324 return mConsumerName;
1325}
1326
Dan Stoza7dde5992015-05-22 09:51:44 -07001327uint64_t BufferQueueProducer::getNextFrameNumber() const {
1328 ATRACE_CALL();
1329
1330 Mutex::Autolock lock(mCore->mMutex);
1331 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1332 return nextFrameNumber;
1333}
1334
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001335status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001336 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001337 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001338
1339 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001340 if (!sharedBufferMode) {
1341 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001342 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001343 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001344 return NO_ERROR;
1345}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001346
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001347status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1348 ATRACE_CALL();
1349 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1350
1351 Mutex::Autolock lock(mCore->mMutex);
1352
1353 mCore->mAutoRefresh = autoRefresh;
1354 return NO_ERROR;
1355}
1356
Dan Stoza127fc632015-06-30 13:43:32 -07001357status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1358 ATRACE_CALL();
1359 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1360
Pablo Ceballos981066c2016-02-18 12:54:37 -08001361 Mutex::Autolock lock(mCore->mMutex);
1362 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1363 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1364 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1365 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1366 "available slots. Delta = %d", delta);
1367 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001368 }
1369
Pablo Ceballos981066c2016-02-18 12:54:37 -08001370 mDequeueTimeout = timeout;
1371 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001372
Pablo Ceballos981066c2016-02-18 12:54:37 -08001373 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001374 return NO_ERROR;
1375}
1376
Dan Stoza50101d02016-04-07 16:53:23 -07001377status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001378 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001379 ATRACE_CALL();
1380 BQ_LOGV("getLastQueuedBuffer");
1381
1382 Mutex::Autolock lock(mCore->mMutex);
1383 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1384 *outBuffer = nullptr;
1385 *outFence = Fence::NO_FENCE;
1386 return NO_ERROR;
1387 }
1388
1389 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1390 *outFence = mLastQueueBufferFence;
1391
John Reck1a61da52016-04-28 13:18:15 -07001392 // Currently only SurfaceFlinger internally ever changes
1393 // GLConsumer's filtering mode, so we just use 'true' here as
1394 // this is slightly specialized for the current client of this API,
1395 // which does want filtering.
1396 GLConsumer::computeTransformMatrix(outTransformMatrix,
1397 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1398 mLastQueuedTransform, true /* filter */);
1399
Dan Stoza50101d02016-04-07 16:53:23 -07001400 return NO_ERROR;
1401}
1402
Dan Stoza289ade12014-02-28 11:17:17 -08001403void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1404 // If we're here, it means that a producer we were connected to died.
1405 // We're guaranteed that we are still connected to it because we remove
1406 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1407 // without synchronization here.
1408 int api = mCore->mConnectedApi;
1409 disconnect(api);
1410}
1411
1412} // namespace android