blob: 7ca92b77bffef34e774a0bd34b7e1b41c55c318b [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
Dan Stoza289ade12014-02-28 11:17:17 -0800447 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700448 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800449 {
450 mSlots[found].mAcquireCalled = false;
451 mSlots[found].mGraphicBuffer = NULL;
452 mSlots[found].mRequestBufferCalled = false;
453 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
454 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
455 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800456 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700457 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800458
459 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800460 } else {
461 // We add 1 because that will be the frame number when this buffer
462 // is queued
463 mCore->mBufferAge =
464 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800465 }
466
Dan Stoza800b41a2015-04-28 14:20:04 -0700467 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
468 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800469
Dan Stoza289ade12014-02-28 11:17:17 -0800470 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
471 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
472 "slot=%d w=%d h=%d format=%u",
473 found, buffer->width, buffer->height, buffer->format);
474 }
475
476 eglDisplay = mSlots[found].mEglDisplay;
477 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700478 // Don't return a fence in shared buffer mode, except for the first
479 // frame.
480 *outFence = (mCore->mSharedBufferMode &&
481 mCore->mSharedBufferSlot == found) ?
482 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800483 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
484 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700485
486 // If shared buffer mode has just been enabled, cache the slot of the
487 // first buffer that is dequeued and mark it as the shared buffer.
488 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
489 BufferQueueCore::INVALID_BUFFER_SLOT) {
490 mCore->mSharedBufferSlot = found;
491 mSlots[found].mBufferState.mShared = true;
492 }
Dan Stoza289ade12014-02-28 11:17:17 -0800493 } // Autolock scope
494
495 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
496 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700497 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800498 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800499 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800500 { // Autolock scope
501 Mutex::Autolock lock(mCore->mMutex);
502
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700503 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
504 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
505 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
506 }
507
508 mCore->mIsAllocating = false;
509 mCore->mIsAllocatingCondition.broadcast();
510
511 if (graphicBuffer == NULL) {
512 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
513 return error;
514 }
515
Dan Stoza289ade12014-02-28 11:17:17 -0800516 if (mCore->mIsAbandoned) {
517 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
518 return NO_INIT;
519 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800520
Pablo Ceballos9e314332016-01-12 13:49:19 -0800521 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800522 } // Autolock scope
523 }
524
Dan Stoza9f3053d2014-03-06 15:14:33 -0800525 if (attachedByConsumer) {
526 returnFlags |= BUFFER_NEEDS_REALLOCATION;
527 }
528
Dan Stoza289ade12014-02-28 11:17:17 -0800529 if (eglFence != EGL_NO_SYNC_KHR) {
530 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
531 1000000000);
532 // If something goes wrong, log the error, but return the buffer without
533 // synchronizing access to it. It's too late at this point to abort the
534 // dequeue operation.
535 if (result == EGL_FALSE) {
536 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
537 eglGetError());
538 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
539 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
540 }
541 eglDestroySyncKHR(eglDisplay, eglFence);
542 }
543
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700544 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
545 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800546 mSlots[*outSlot].mFrameNumber,
547 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
548
549 return returnFlags;
550}
551
Dan Stoza9f3053d2014-03-06 15:14:33 -0800552status_t BufferQueueProducer::detachBuffer(int slot) {
553 ATRACE_CALL();
554 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700555 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800556
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700557 sp<IConsumerListener> listener;
558 {
559 Mutex::Autolock lock(mCore->mMutex);
560
561 if (mCore->mIsAbandoned) {
562 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
563 return NO_INIT;
564 }
565
566 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
567 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
568 return NO_INIT;
569 }
570
571 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
572 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
573 return BAD_VALUE;
574 }
575
576 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
577 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
578 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
579 return BAD_VALUE;
580 } else if (!mSlots[slot].mBufferState.isDequeued()) {
581 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
582 "(state = %s)", slot, mSlots[slot].mBufferState.string());
583 return BAD_VALUE;
584 } else if (!mSlots[slot].mRequestBufferCalled) {
585 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
586 slot);
587 return BAD_VALUE;
588 }
589
590 mSlots[slot].mBufferState.detachProducer();
591 mCore->mActiveBuffers.erase(slot);
592 mCore->mFreeSlots.insert(slot);
593 mCore->clearBufferSlotLocked(slot);
594 mCore->mDequeueCondition.broadcast();
595 VALIDATE_CONSISTENCY();
596 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800597 }
598
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700599 if (listener != NULL) {
600 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700601 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800602
Dan Stoza9f3053d2014-03-06 15:14:33 -0800603 return NO_ERROR;
604}
605
Dan Stozad9822a32014-03-28 15:25:31 -0700606status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
607 sp<Fence>* outFence) {
608 ATRACE_CALL();
609
610 if (outBuffer == NULL) {
611 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
612 return BAD_VALUE;
613 } else if (outFence == NULL) {
614 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
615 return BAD_VALUE;
616 }
617
Pablo Ceballos981066c2016-02-18 12:54:37 -0800618 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700619
Pablo Ceballos981066c2016-02-18 12:54:37 -0800620 if (mCore->mIsAbandoned) {
621 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
622 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700623 }
624
Pablo Ceballos981066c2016-02-18 12:54:37 -0800625 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
626 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
627 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700628 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800629
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700630 if (mCore->mSharedBufferMode) {
631 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
632 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800633 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700634 }
635
Pablo Ceballos981066c2016-02-18 12:54:37 -0800636 mCore->waitWhileAllocatingLocked();
637
638 if (mCore->mFreeBuffers.empty()) {
639 return NO_MEMORY;
640 }
641
642 int found = mCore->mFreeBuffers.front();
643 mCore->mFreeBuffers.remove(found);
644 mCore->mFreeSlots.insert(found);
645
646 BQ_LOGV("detachNextBuffer detached slot %d", found);
647
648 *outBuffer = mSlots[found].mGraphicBuffer;
649 *outFence = mSlots[found].mFence;
650 mCore->clearBufferSlotLocked(found);
651 VALIDATE_CONSISTENCY();
652
Dan Stozad9822a32014-03-28 15:25:31 -0700653 return NO_ERROR;
654}
655
Dan Stoza9f3053d2014-03-06 15:14:33 -0800656status_t BufferQueueProducer::attachBuffer(int* outSlot,
657 const sp<android::GraphicBuffer>& buffer) {
658 ATRACE_CALL();
659
660 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700661 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800662 return BAD_VALUE;
663 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700664 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800665 return BAD_VALUE;
666 }
667
668 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700669
670 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700671 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700672 return NO_INIT;
673 }
674
675 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700676 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700677 return NO_INIT;
678 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800679
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700680 if (mCore->mSharedBufferMode) {
681 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700682 return BAD_VALUE;
683 }
684
Dan Stoza812ed062015-06-02 15:45:22 -0700685 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
686 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
687 "[queue %u]", buffer->getGenerationNumber(),
688 mCore->mGenerationNumber);
689 return BAD_VALUE;
690 }
691
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700692 mCore->waitWhileAllocatingLocked();
693
Dan Stoza9f3053d2014-03-06 15:14:33 -0800694 status_t returnFlags = NO_ERROR;
695 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800696 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697 if (status != NO_ERROR) {
698 return status;
699 }
700
701 // This should not happen
702 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700703 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800704 return -EBUSY;
705 }
706
707 *outSlot = found;
708 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700709 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800710 *outSlot, returnFlags);
711
712 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700713 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800714 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
715 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700716 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800717 mSlots[*outSlot].mAcquireCalled = false;
718 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800719 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700720
Dan Stoza9f3053d2014-03-06 15:14:33 -0800721 return returnFlags;
722}
723
Dan Stoza289ade12014-02-28 11:17:17 -0800724status_t BufferQueueProducer::queueBuffer(int slot,
725 const QueueBufferInput &input, QueueBufferOutput *output) {
726 ATRACE_CALL();
727 ATRACE_BUFFER_INDEX(slot);
728
729 int64_t timestamp;
730 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800731 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700732 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800733 int scalingMode;
734 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700735 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800736 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800737 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700738 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700739 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800740
741 if (fence == NULL) {
742 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800743 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800744 }
745
746 switch (scalingMode) {
747 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
748 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
749 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
750 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
751 break;
752 default:
753 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800755 }
756
Dan Stoza8dc55392014-11-04 11:37:46 -0800757 sp<IConsumerListener> frameAvailableListener;
758 sp<IConsumerListener> frameReplacedListener;
759 int callbackTicket = 0;
760 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800761 { // Autolock scope
762 Mutex::Autolock lock(mCore->mMutex);
763
764 if (mCore->mIsAbandoned) {
765 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
766 return NO_INIT;
767 }
768
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700769 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
770 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
771 return NO_INIT;
772 }
773
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800774 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800775 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800776 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800777 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700778 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800779 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700780 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800781 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800782 } else if (!mSlots[slot].mRequestBufferCalled) {
783 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
784 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800785 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800786 }
787
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700788 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700789 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700790 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700791 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700792 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700793 mSlots[slot].mBufferState.mShared = true;
794 }
795
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800796 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700797 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800798 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800799 crop.left, crop.top, crop.right, crop.bottom, transform,
800 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800801
802 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
803 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700804 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800805 crop.intersect(bufferRect, &croppedRect);
806 if (croppedRect != crop) {
807 BQ_LOGE("queueBuffer: crop rect is not contained within the "
808 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800809 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800810 }
811
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800812 // Override UNKNOWN dataspace with consumer default
813 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
814 dataSpace = mCore->mDefaultBufferDataSpace;
815 }
816
Dan Stoza289ade12014-02-28 11:17:17 -0800817 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700818 mSlots[slot].mBufferState.queue();
819
Dan Stoza289ade12014-02-28 11:17:17 -0800820 ++mCore->mFrameCounter;
821 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
822
Dan Stoza289ade12014-02-28 11:17:17 -0800823 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
824 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
825 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800826 item.mTransform = transform &
827 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800828 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800829 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
830 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800831 item.mTimestamp = timestamp;
832 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800833 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800834 item.mFrameNumber = mCore->mFrameCounter;
835 item.mSlot = slot;
836 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700837 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700838 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700839 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700840 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700841 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700842 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800843
Ruben Brunk1681d952014-06-27 15:51:55 -0700844 mStickyTransform = stickyTransform;
845
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700846 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700847 if (mCore->mSharedBufferMode) {
848 mCore->mSharedBufferCache.crop = crop;
849 mCore->mSharedBufferCache.transform = transform;
850 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700851 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700852 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700853 }
854
Dan Stoza289ade12014-02-28 11:17:17 -0800855 if (mCore->mQueue.empty()) {
856 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
857 // and simply queue this buffer
858 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800859 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800860 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700861 // When the queue is not empty, we need to look at the last buffer
862 // in the queue to see if we need to replace it
863 const BufferItem& last = mCore->mQueue.itemAt(
864 mCore->mQueue.size() - 1);
865 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800866
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700867 if (!last.mIsStale) {
868 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700869
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700870 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 // still be around. Mark it as no longer shared if this
872 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700873 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700874 mSlots[last.mSlot].mBufferState.isFree()) {
875 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876 }
877 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700878 if (!mSlots[last.mSlot].mBufferState.isShared()) {
879 mCore->mActiveBuffers.erase(last.mSlot);
880 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700881 }
Dan Stoza289ade12014-02-28 11:17:17 -0800882 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800883
Dan Stoza289ade12014-02-28 11:17:17 -0800884 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700885 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800886 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800887 } else {
888 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800889 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800890 }
891 }
892
893 mCore->mBufferHasBeenQueued = true;
894 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700895 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800896
897 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800898 mCore->mTransformHint,
899 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800900
901 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stozae77c7662016-05-13 11:37:28 -0700902 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800903
904 // Take a ticket for the callback functions
905 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700906
Pablo Ceballos9e314332016-01-12 13:49:19 -0800907 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800908 } // Autolock scope
909
Dan Stoza8dc55392014-11-04 11:37:46 -0800910 // Don't send the GraphicBuffer through the callback, and don't send
911 // the slot number, since the consumer shouldn't need it
912 item.mGraphicBuffer.clear();
913 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
914
915 // Call back without the main BufferQueue lock held, but with the callback
916 // lock held so we can ensure that callbacks occur in order
917 {
918 Mutex::Autolock lock(mCallbackMutex);
919 while (callbackTicket != mCurrentCallbackTicket) {
920 mCallbackCondition.wait(mCallbackMutex);
921 }
922
923 if (frameAvailableListener != NULL) {
924 frameAvailableListener->onFrameAvailable(item);
925 } else if (frameReplacedListener != NULL) {
926 frameReplacedListener->onFrameReplaced(item);
927 }
928
929 ++mCurrentCallbackTicket;
930 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800931 }
932
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000933 // Wait without lock held
934 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
935 // Waiting here allows for two full buffers to be queued but not a
936 // third. In the event that frames take varying time, this makes a
937 // small trade-off in favor of latency rather than throughput.
938 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000939 }
Dan Stoza50101d02016-04-07 16:53:23 -0700940 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700941 mLastQueuedCrop = item.mCrop;
942 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000943
Dan Stoza289ade12014-02-28 11:17:17 -0800944 return NO_ERROR;
945}
946
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700947status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800948 ATRACE_CALL();
949 BQ_LOGV("cancelBuffer: slot %d", slot);
950 Mutex::Autolock lock(mCore->mMutex);
951
952 if (mCore->mIsAbandoned) {
953 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700954 return NO_INIT;
955 }
956
957 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
958 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
959 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800960 }
961
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700962 if (mCore->mSharedBufferMode) {
963 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700964 return BAD_VALUE;
965 }
966
Dan Stoza3e96f192014-03-03 10:16:19 -0800967 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800968 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800969 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700970 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700971 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800972 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700973 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700974 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800975 } else if (fence == NULL) {
976 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700977 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800978 }
979
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700980 mSlots[slot].mBufferState.cancel();
981
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700982 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700983 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700984 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700985 mSlots[slot].mBufferState.mShared = false;
986 }
987
988 // Don't put the shared buffer on the free list.
989 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800990 mCore->mActiveBuffers.erase(slot);
991 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700992 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800993
Dan Stoza289ade12014-02-28 11:17:17 -0800994 mSlots[slot].mFence = fence;
995 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800996 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700997
998 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800999}
1000
1001int BufferQueueProducer::query(int what, int *outValue) {
1002 ATRACE_CALL();
1003 Mutex::Autolock lock(mCore->mMutex);
1004
1005 if (outValue == NULL) {
1006 BQ_LOGE("query: outValue was NULL");
1007 return BAD_VALUE;
1008 }
1009
1010 if (mCore->mIsAbandoned) {
1011 BQ_LOGE("query: BufferQueue has been abandoned");
1012 return NO_INIT;
1013 }
1014
1015 int value;
1016 switch (what) {
1017 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001018 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001019 break;
1020 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001021 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001022 break;
1023 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001024 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001025 break;
1026 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001027 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001028 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001029 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001030 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001031 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001032 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1033 value = (mCore->mQueue.size() > 1);
1034 break;
1035 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001036 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001037 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001038 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1039 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1040 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001041 case NATIVE_WINDOW_BUFFER_AGE:
1042 if (mCore->mBufferAge > INT32_MAX) {
1043 value = 0;
1044 } else {
1045 value = static_cast<int32_t>(mCore->mBufferAge);
1046 }
1047 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001048 default:
1049 return BAD_VALUE;
1050 }
1051
1052 BQ_LOGV("query: %d? %d", what, value);
1053 *outValue = value;
1054 return NO_ERROR;
1055}
1056
Dan Stozaf0eaf252014-03-21 13:05:51 -07001057status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001058 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1059 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001060 Mutex::Autolock lock(mCore->mMutex);
1061 mConsumerName = mCore->mConsumerName;
1062 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1063 producerControlledByApp ? "true" : "false");
1064
1065 if (mCore->mIsAbandoned) {
1066 BQ_LOGE("connect: BufferQueue has been abandoned");
1067 return NO_INIT;
1068 }
1069
1070 if (mCore->mConsumerListener == NULL) {
1071 BQ_LOGE("connect: BufferQueue has no consumer");
1072 return NO_INIT;
1073 }
1074
1075 if (output == NULL) {
1076 BQ_LOGE("connect: output was NULL");
1077 return BAD_VALUE;
1078 }
1079
1080 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1081 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1082 mCore->mConnectedApi, api);
1083 return BAD_VALUE;
1084 }
1085
1086 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1087 mDequeueTimeout < 0 ?
1088 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1089 mCore->mMaxBufferCount) -
1090 mCore->getMaxBufferCountLocked();
1091 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1092 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1093 "slots. Delta = %d", delta);
1094 return BAD_VALUE;
1095 }
1096
Dan Stoza289ade12014-02-28 11:17:17 -08001097 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001098 switch (api) {
1099 case NATIVE_WINDOW_API_EGL:
1100 case NATIVE_WINDOW_API_CPU:
1101 case NATIVE_WINDOW_API_MEDIA:
1102 case NATIVE_WINDOW_API_CAMERA:
1103 mCore->mConnectedApi = api;
1104 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1105 mCore->mTransformHint,
1106 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001107
Pablo Ceballos981066c2016-02-18 12:54:37 -08001108 // Set up a death notification so that we can disconnect
1109 // automatically if the remote producer dies
1110 if (listener != NULL &&
1111 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1112 status = IInterface::asBinder(listener)->linkToDeath(
1113 static_cast<IBinder::DeathRecipient*>(this));
1114 if (status != NO_ERROR) {
1115 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1116 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001117 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001118 }
1119 mCore->mConnectedProducerListener = listener;
1120 break;
1121 default:
1122 BQ_LOGE("connect: unknown API %d", api);
1123 status = BAD_VALUE;
1124 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001125 }
1126
Pablo Ceballos981066c2016-02-18 12:54:37 -08001127 mCore->mBufferHasBeenQueued = false;
1128 mCore->mDequeueBufferCannotBlock = false;
1129 if (mDequeueTimeout < 0) {
1130 mCore->mDequeueBufferCannotBlock =
1131 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001132 }
Dan Stoza289ade12014-02-28 11:17:17 -08001133
Pablo Ceballos981066c2016-02-18 12:54:37 -08001134 mCore->mAllowAllocation = true;
1135 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001136 return status;
1137}
1138
1139status_t BufferQueueProducer::disconnect(int api) {
1140 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001141 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001142
1143 int status = NO_ERROR;
1144 sp<IConsumerListener> listener;
1145 { // Autolock scope
1146 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001147 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001148
1149 if (mCore->mIsAbandoned) {
1150 // It's not really an error to disconnect after the surface has
1151 // been abandoned; it should just be a no-op.
1152 return NO_ERROR;
1153 }
1154
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001155 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1156 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001157 // If we're asked to disconnect the currently connected api but
1158 // nobody is connected, it's not really an error.
1159 if (api == BufferQueueCore::NO_CONNECTED_API) {
1160 return NO_ERROR;
1161 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001162 }
1163
Dan Stoza289ade12014-02-28 11:17:17 -08001164 switch (api) {
1165 case NATIVE_WINDOW_API_EGL:
1166 case NATIVE_WINDOW_API_CPU:
1167 case NATIVE_WINDOW_API_MEDIA:
1168 case NATIVE_WINDOW_API_CAMERA:
1169 if (mCore->mConnectedApi == api) {
1170 mCore->freeAllBuffersLocked();
1171
1172 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001173 if (mCore->mConnectedProducerListener != NULL) {
1174 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001175 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001176 // This can fail if we're here because of the death
1177 // notification, but we just ignore it
1178 token->unlinkToDeath(
1179 static_cast<IBinder::DeathRecipient*>(this));
1180 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001181 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001182 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001183 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001184 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001185 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001186 mCore->mDequeueCondition.broadcast();
1187 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001188 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001189 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001190 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001191 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001192 }
1193 break;
1194 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001195 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001196 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001197 break;
1198 }
1199 } // Autolock scope
1200
1201 // Call back without lock held
1202 if (listener != NULL) {
1203 listener->onBuffersReleased();
1204 }
1205
1206 return status;
1207}
1208
Jesse Hall399184a2014-03-03 15:42:54 -08001209status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001210 sp<IConsumerListener> listener;
1211 { // Autolock scope
1212 Mutex::Autolock _l(mCore->mMutex);
1213 mCore->mSidebandStream = stream;
1214 listener = mCore->mConsumerListener;
1215 } // Autolock scope
1216
1217 if (listener != NULL) {
1218 listener->onSidebandStreamChanged();
1219 }
Jesse Hall399184a2014-03-03 15:42:54 -08001220 return NO_ERROR;
1221}
1222
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001223void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1224 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001225 ATRACE_CALL();
1226 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001227 size_t newBufferCount = 0;
1228 uint32_t allocWidth = 0;
1229 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001230 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001231 uint32_t allocUsage = 0;
1232 { // Autolock scope
1233 Mutex::Autolock lock(mCore->mMutex);
1234 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001235
Dan Stoza9de72932015-04-16 17:28:43 -07001236 if (!mCore->mAllowAllocation) {
1237 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1238 "BufferQueue");
1239 return;
1240 }
1241
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001242 newBufferCount = mCore->mFreeSlots.size();
1243 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001244 return;
1245 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001246
Antoine Labour78014f32014-07-15 21:17:03 -07001247 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1248 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1249 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1250 allocUsage = usage | mCore->mConsumerUsageBits;
1251
1252 mCore->mIsAllocating = true;
1253 } // Autolock scope
1254
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001255 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001256 for (size_t i = 0; i < newBufferCount; ++i) {
1257 status_t result = NO_ERROR;
1258 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1259 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1260 if (result != NO_ERROR) {
1261 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1262 " %u, usage %u)", width, height, format, usage);
1263 Mutex::Autolock lock(mCore->mMutex);
1264 mCore->mIsAllocating = false;
1265 mCore->mIsAllocatingCondition.broadcast();
1266 return;
1267 }
1268 buffers.push_back(graphicBuffer);
1269 }
1270
1271 { // Autolock scope
1272 Mutex::Autolock lock(mCore->mMutex);
1273 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1274 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001275 PixelFormat checkFormat = format != 0 ?
1276 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001277 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1278 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1279 checkFormat != allocFormat || checkUsage != allocUsage) {
1280 // Something changed while we released the lock. Retry.
1281 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1282 mCore->mIsAllocating = false;
1283 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001284 continue;
1285 }
1286
Antoine Labour78014f32014-07-15 21:17:03 -07001287 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001288 if (mCore->mFreeSlots.empty()) {
1289 BQ_LOGV("allocateBuffers: a slot was occupied while "
1290 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001291 continue;
1292 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001293 auto slot = mCore->mFreeSlots.begin();
1294 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1295 mSlots[*slot].mGraphicBuffer = buffers[i];
1296 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001297
1298 // freeBufferLocked puts this slot on the free slots list. Since
1299 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001300 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001301
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001302 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1303 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001304
1305 // Make sure the erase is done after all uses of the slot
1306 // iterator since it will be invalid after this point.
1307 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001308 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001309
Antoine Labour78014f32014-07-15 21:17:03 -07001310 mCore->mIsAllocating = false;
1311 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001312 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001313 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001314 }
1315}
1316
Dan Stoza9de72932015-04-16 17:28:43 -07001317status_t BufferQueueProducer::allowAllocation(bool allow) {
1318 ATRACE_CALL();
1319 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1320
1321 Mutex::Autolock lock(mCore->mMutex);
1322 mCore->mAllowAllocation = allow;
1323 return NO_ERROR;
1324}
1325
Dan Stoza812ed062015-06-02 15:45:22 -07001326status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1327 ATRACE_CALL();
1328 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1329
1330 Mutex::Autolock lock(mCore->mMutex);
1331 mCore->mGenerationNumber = generationNumber;
1332 return NO_ERROR;
1333}
1334
Dan Stozac6f30bd2015-06-08 09:32:50 -07001335String8 BufferQueueProducer::getConsumerName() const {
1336 ATRACE_CALL();
1337 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1338 return mConsumerName;
1339}
1340
Dan Stoza7dde5992015-05-22 09:51:44 -07001341uint64_t BufferQueueProducer::getNextFrameNumber() const {
1342 ATRACE_CALL();
1343
1344 Mutex::Autolock lock(mCore->mMutex);
1345 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1346 return nextFrameNumber;
1347}
1348
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001349status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001350 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001351 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001352
1353 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001354 if (!sharedBufferMode) {
1355 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001356 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001357 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001358 return NO_ERROR;
1359}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001360
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001361status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1362 ATRACE_CALL();
1363 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1364
1365 Mutex::Autolock lock(mCore->mMutex);
1366
1367 mCore->mAutoRefresh = autoRefresh;
1368 return NO_ERROR;
1369}
1370
Dan Stoza127fc632015-06-30 13:43:32 -07001371status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1372 ATRACE_CALL();
1373 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1374
Pablo Ceballos981066c2016-02-18 12:54:37 -08001375 Mutex::Autolock lock(mCore->mMutex);
1376 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1377 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1378 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1379 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1380 "available slots. Delta = %d", delta);
1381 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001382 }
1383
Pablo Ceballos981066c2016-02-18 12:54:37 -08001384 mDequeueTimeout = timeout;
1385 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001386
Pablo Ceballos981066c2016-02-18 12:54:37 -08001387 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001388 return NO_ERROR;
1389}
1390
Dan Stoza50101d02016-04-07 16:53:23 -07001391status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001392 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001393 ATRACE_CALL();
1394 BQ_LOGV("getLastQueuedBuffer");
1395
1396 Mutex::Autolock lock(mCore->mMutex);
1397 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1398 *outBuffer = nullptr;
1399 *outFence = Fence::NO_FENCE;
1400 return NO_ERROR;
1401 }
1402
1403 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1404 *outFence = mLastQueueBufferFence;
1405
John Reck1a61da52016-04-28 13:18:15 -07001406 // Currently only SurfaceFlinger internally ever changes
1407 // GLConsumer's filtering mode, so we just use 'true' here as
1408 // this is slightly specialized for the current client of this API,
1409 // which does want filtering.
1410 GLConsumer::computeTransformMatrix(outTransformMatrix,
1411 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1412 mLastQueuedTransform, true /* filter */);
1413
Dan Stoza50101d02016-04-07 16:53:23 -07001414 return NO_ERROR;
1415}
1416
Pablo Ceballosce796e72016-02-04 19:10:51 -08001417bool BufferQueueProducer::getFrameTimestamps(uint64_t frameNumber,
1418 FrameTimestamps* outTimestamps) const {
1419 ATRACE_CALL();
1420 BQ_LOGV("getFrameTimestamps, %" PRIu64, frameNumber);
1421 sp<IConsumerListener> listener;
1422
1423 {
1424 Mutex::Autolock lock(mCore->mMutex);
1425 listener = mCore->mConsumerListener;
1426 }
1427 if (listener != NULL) {
1428 return listener->getFrameTimestamps(frameNumber, outTimestamps);
1429 }
1430 return false;
1431}
1432
Dan Stoza289ade12014-02-28 11:17:17 -08001433void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1434 // If we're here, it means that a producer we were connected to died.
1435 // We're guaranteed that we are still connected to it because we remove
1436 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1437 // without synchronization here.
1438 int api = mCore->mConnectedApi;
1439 disconnect(api);
1440}
1441
1442} // namespace android