blob: 48b1db8f592b73f3b478ebd4420c086528d9c5e8 [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
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
37#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070038#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
43namespace android {
44
45BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
46 mCore(core),
47 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070048 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070049 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080050 mLastQueueBufferFence(Fence::NO_FENCE),
51 mCallbackMutex(),
52 mNextCallbackTicket(0),
53 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070054 mCallbackCondition(),
55 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080056
57BufferQueueProducer::~BufferQueueProducer() {}
58
59status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
60 ATRACE_CALL();
61 BQ_LOGV("requestBuffer: slot %d", slot);
62 Mutex::Autolock lock(mCore->mMutex);
63
64 if (mCore->mIsAbandoned) {
65 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
66 return NO_INIT;
67 }
68
Pablo Ceballos583b1b32015-09-03 18:23:52 -070069 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
70 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
71 return NO_INIT;
72 }
73
Dan Stoza3e96f192014-03-03 10:16:19 -080074 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080075 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080076 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080077 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070078 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080079 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070080 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080081 return BAD_VALUE;
82 }
83
84 mSlots[slot].mRequestBufferCalled = true;
85 *buf = mSlots[slot].mGraphicBuffer;
86 return NO_ERROR;
87}
88
Pablo Ceballosfa455352015-08-12 17:47:47 -070089status_t BufferQueueProducer::setMaxDequeuedBufferCount(
90 int maxDequeuedBuffers) {
91 ATRACE_CALL();
92 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
93 maxDequeuedBuffers);
94
Pablo Ceballos981066c2016-02-18 12:54:37 -080095 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070096 { // Autolock scope
97 Mutex::Autolock lock(mCore->mMutex);
98 mCore->waitWhileAllocatingLocked();
99
100 if (mCore->mIsAbandoned) {
101 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
102 "abandoned");
103 return NO_INIT;
104 }
105
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700106 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
107 return NO_ERROR;
108 }
109
Pablo Ceballos72daab62015-12-07 16:38:43 -0800110 // The new maxDequeuedBuffer count should not be violated by the number
111 // of currently dequeued buffers
112 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800113 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700114 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800115 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700116 }
117 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800118 if (dequeuedCount > maxDequeuedBuffers) {
119 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
120 "count (%d) exceeds the current dequeued buffer count (%d)",
121 maxDequeuedBuffers, dequeuedCount);
122 return BAD_VALUE;
123 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700124
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700125 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700126 bufferCount += maxDequeuedBuffers;
127
128 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
129 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
130 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
131 return BAD_VALUE;
132 }
133
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700134 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700135 if (bufferCount < minBufferSlots) {
136 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
137 "less than minimum %d", bufferCount, minBufferSlots);
138 return BAD_VALUE;
139 }
140
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700141 if (bufferCount > mCore->mMaxBufferCount) {
142 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700143 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
144 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
145 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
146 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700147 return BAD_VALUE;
148 }
149
Pablo Ceballos72daab62015-12-07 16:38:43 -0800150 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800151 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800152 return BAD_VALUE;
153 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700154 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800155 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800156 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800157 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700159 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700160 } // Autolock scope
161
162 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800163 if (listener != NULL) {
164 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 }
166
167 return NO_ERROR;
168}
169
170status_t BufferQueueProducer::setAsyncMode(bool async) {
171 ATRACE_CALL();
172 BQ_LOGV("setAsyncMode: async = %d", async);
173
Pablo Ceballos981066c2016-02-18 12:54:37 -0800174 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700175 { // Autolock scope
176 Mutex::Autolock lock(mCore->mMutex);
177 mCore->waitWhileAllocatingLocked();
178
179 if (mCore->mIsAbandoned) {
180 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
181 return NO_INIT;
182 }
183
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700184 if (async == mCore->mAsyncMode) {
185 return NO_ERROR;
186 }
187
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700188 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700189 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
190 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700191 BQ_LOGE("setAsyncMode(%d): this call would cause the "
192 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700193 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
194 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
195 mCore->mMaxDequeuedBufferCount,
196 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700197 return BAD_VALUE;
198 }
199
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800200 int delta = mCore->getMaxBufferCountLocked(async,
201 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
202 - mCore->getMaxBufferCountLocked();
203
Pablo Ceballos981066c2016-02-18 12:54:37 -0800204 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800205 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
206 "available slots. Delta = %d", delta);
207 return BAD_VALUE;
208 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700209 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800210 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700211 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700212 if (delta < 0) {
213 listener = mCore->mConsumerListener;
214 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 } // Autolock scope
216
217 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800218 if (listener != NULL) {
219 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700220 }
221 return NO_ERROR;
222}
223
Dan Stoza5ecfb682016-01-04 17:01:02 -0800224int BufferQueueProducer::getFreeBufferLocked() const {
225 if (mCore->mFreeBuffers.empty()) {
226 return BufferQueueCore::INVALID_BUFFER_SLOT;
227 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800228 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800229 mCore->mFreeBuffers.pop_front();
230 return slot;
231}
232
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800233int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800234 if (mCore->mFreeSlots.empty()) {
235 return BufferQueueCore::INVALID_BUFFER_SLOT;
236 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800237 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800238 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800239 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800240}
241
242status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800243 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800244 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
245 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800246 bool tryAgain = true;
247 while (tryAgain) {
248 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800249 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800250 return NO_INIT;
251 }
252
Dan Stoza9f3053d2014-03-06 15:14:33 -0800253 int dequeuedCount = 0;
254 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800255 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700256 if (mSlots[s].mBufferState.isDequeued()) {
257 ++dequeuedCount;
258 }
259 if (mSlots[s].mBufferState.isAcquired()) {
260 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800261 }
262 }
263
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700264 // Producers are not allowed to dequeue more than
265 // mMaxDequeuedBufferCount buffers.
266 // This check is only done if a buffer has already been queued
267 if (mCore->mBufferHasBeenQueued &&
268 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
269 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800271 return INVALID_OPERATION;
272 }
273
Dan Stoza0de7ea72015-04-23 13:20:51 -0700274 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
275
Dan Stozaae3c3682014-04-18 15:43:35 -0700276 // If we disconnect and reconnect quickly, we can be in a state where
277 // our slots are empty but we have many buffers in the queue. This can
278 // cause us to run out of memory if we outrun the consumer. Wait here if
279 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800280 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700281 bool tooManyBuffers = mCore->mQueue.size()
282 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700283 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700285 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700286 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700287 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700288 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700289 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700290 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 } else {
293 if (caller == FreeSlotCaller::Dequeue) {
294 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800295 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800296 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
297 *found = slot;
298 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800299 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800300 }
301 } else {
302 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800303 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
305 *found = slot;
306 } else {
307 *found = getFreeBufferLocked();
308 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700309 }
310 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700311 }
312
313 // If no buffer is found, or if the queue has too many buffers
314 // outstanding, wait for a buffer to be acquired or released, or for the
315 // max buffer count to change.
316 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
317 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318 if (tryAgain) {
319 // Return an error if we're in non-blocking mode (producer and
320 // consumer are controlled by the application).
321 // However, the consumer is allowed to briefly acquire an extra
322 // buffer (which could cause us to have to wait here), which is
323 // okay, since it is only used to implement an atomic acquire +
324 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700325 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
327 return WOULD_BLOCK;
328 }
Dan Stoza127fc632015-06-30 13:43:32 -0700329 if (mDequeueTimeout >= 0) {
330 status_t result = mCore->mDequeueCondition.waitRelative(
331 mCore->mMutex, mDequeueTimeout);
332 if (result == TIMED_OUT) {
333 return result;
334 }
335 } else {
336 mCore->mDequeueCondition.wait(mCore->mMutex);
337 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800338 }
339 } // while (tryAgain)
340
341 return NO_ERROR;
342}
343
Dan Stoza289ade12014-02-28 11:17:17 -0800344status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700345 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
346 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800347 ATRACE_CALL();
348 { // Autolock scope
349 Mutex::Autolock lock(mCore->mMutex);
350 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700351
352 if (mCore->mIsAbandoned) {
353 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
354 return NO_INIT;
355 }
356
357 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
358 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
359 return NO_INIT;
360 }
Dan Stoza289ade12014-02-28 11:17:17 -0800361 } // Autolock scope
362
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700363 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
364 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800365
366 if ((width && !height) || (!width && height)) {
367 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
368 return BAD_VALUE;
369 }
370
371 status_t returnFlags = NO_ERROR;
372 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
373 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800374 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800375
376 { // Autolock scope
377 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700378 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800379
380 if (format == 0) {
381 format = mCore->mDefaultBufferFormat;
382 }
383
384 // Enable the usage bits the consumer requested
385 usage |= mCore->mConsumerUsageBits;
386
Dan Stoza9de72932015-04-16 17:28:43 -0700387 const bool useDefaultSize = !width && !height;
388 if (useDefaultSize) {
389 width = mCore->mDefaultWidth;
390 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800391 }
Dan Stoza289ade12014-02-28 11:17:17 -0800392
Pablo Ceballos981066c2016-02-18 12:54:37 -0800393 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700394 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800395 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800396 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700397 if (status != NO_ERROR) {
398 return status;
399 }
400
401 // This should not happen
402 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
403 BQ_LOGE("dequeueBuffer: no available buffer slots");
404 return -EBUSY;
405 }
406
407 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
408
409 // If we are not allowed to allocate new buffers,
410 // waitForFreeSlotThenRelock must have returned a slot containing a
411 // buffer. If this buffer would require reallocation to meet the
412 // requested attributes, we free it and attempt to get another one.
413 if (!mCore->mAllowAllocation) {
414 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700415 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700416 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
417 "buffer");
418 return BAD_VALUE;
419 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800420 mCore->mFreeSlots.insert(found);
421 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700422 found = BufferItem::INVALID_BUFFER_SLOT;
423 continue;
424 }
425 }
Dan Stoza289ade12014-02-28 11:17:17 -0800426 }
427
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800428 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700429 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800430 buffer->needsReallocation(width, height, format, usage)) {
431 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
432 "buffer");
433
434 return BAD_VALUE;
435 }
436
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700437 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800438 mCore->mActiveBuffers.insert(found);
439 }
Dan Stoza289ade12014-02-28 11:17:17 -0800440 *outSlot = found;
441 ATRACE_BUFFER_INDEX(found);
442
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800443 attachedByConsumer = mSlots[found].mNeedsReallocation;
444 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800445
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700446 mSlots[found].mBufferState.dequeue();
447
Dan Stoza289ade12014-02-28 11:17:17 -0800448 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700449 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800450 {
451 mSlots[found].mAcquireCalled = false;
452 mSlots[found].mGraphicBuffer = NULL;
453 mSlots[found].mRequestBufferCalled = false;
454 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
455 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
456 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800457 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700458 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800459
460 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800461 } else {
462 // We add 1 because that will be the frame number when this buffer
463 // is queued
464 mCore->mBufferAge =
465 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800466 }
467
Dan Stoza800b41a2015-04-28 14:20:04 -0700468 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
469 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800470
Dan Stoza289ade12014-02-28 11:17:17 -0800471 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
472 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
473 "slot=%d w=%d h=%d format=%u",
474 found, buffer->width, buffer->height, buffer->format);
475 }
476
477 eglDisplay = mSlots[found].mEglDisplay;
478 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700479 // Don't return a fence in shared buffer mode, except for the first
480 // frame.
481 *outFence = (mCore->mSharedBufferMode &&
482 mCore->mSharedBufferSlot == found) ?
483 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800484 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
485 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700486
487 // If shared buffer mode has just been enabled, cache the slot of the
488 // first buffer that is dequeued and mark it as the shared buffer.
489 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
490 BufferQueueCore::INVALID_BUFFER_SLOT) {
491 mCore->mSharedBufferSlot = found;
492 mSlots[found].mBufferState.mShared = true;
493 }
Dan Stoza289ade12014-02-28 11:17:17 -0800494 } // Autolock scope
495
496 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
497 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700498 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800499 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stozad4079af2016-08-22 17:26:41 -0700500 width, height, format, usage,
501 {mConsumerName.string(), mConsumerName.size()}, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800502 { // Autolock scope
503 Mutex::Autolock lock(mCore->mMutex);
504
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700505 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
506 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
507 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
508 }
509
510 mCore->mIsAllocating = false;
511 mCore->mIsAllocatingCondition.broadcast();
512
513 if (graphicBuffer == NULL) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700514 mCore->mFreeSlots.insert(*outSlot);
515 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700516 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
517 return error;
518 }
519
Dan Stoza289ade12014-02-28 11:17:17 -0800520 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700521 mCore->mFreeSlots.insert(*outSlot);
522 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800523 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
524 return NO_INIT;
525 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800526
Pablo Ceballos9e314332016-01-12 13:49:19 -0800527 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800528 } // Autolock scope
529 }
530
Dan Stoza9f3053d2014-03-06 15:14:33 -0800531 if (attachedByConsumer) {
532 returnFlags |= BUFFER_NEEDS_REALLOCATION;
533 }
534
Dan Stoza289ade12014-02-28 11:17:17 -0800535 if (eglFence != EGL_NO_SYNC_KHR) {
536 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
537 1000000000);
538 // If something goes wrong, log the error, but return the buffer without
539 // synchronizing access to it. It's too late at this point to abort the
540 // dequeue operation.
541 if (result == EGL_FALSE) {
542 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
543 eglGetError());
544 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
545 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
546 }
547 eglDestroySyncKHR(eglDisplay, eglFence);
548 }
549
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700550 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
551 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800552 mSlots[*outSlot].mFrameNumber,
553 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
554
555 return returnFlags;
556}
557
Dan Stoza9f3053d2014-03-06 15:14:33 -0800558status_t BufferQueueProducer::detachBuffer(int slot) {
559 ATRACE_CALL();
560 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700561 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800562
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700563 sp<IConsumerListener> listener;
564 {
565 Mutex::Autolock lock(mCore->mMutex);
566
567 if (mCore->mIsAbandoned) {
568 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
569 return NO_INIT;
570 }
571
572 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
573 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
574 return NO_INIT;
575 }
576
577 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
578 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
579 return BAD_VALUE;
580 }
581
582 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
583 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
584 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
585 return BAD_VALUE;
586 } else if (!mSlots[slot].mBufferState.isDequeued()) {
587 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
588 "(state = %s)", slot, mSlots[slot].mBufferState.string());
589 return BAD_VALUE;
590 } else if (!mSlots[slot].mRequestBufferCalled) {
591 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
592 slot);
593 return BAD_VALUE;
594 }
595
596 mSlots[slot].mBufferState.detachProducer();
597 mCore->mActiveBuffers.erase(slot);
598 mCore->mFreeSlots.insert(slot);
599 mCore->clearBufferSlotLocked(slot);
600 mCore->mDequeueCondition.broadcast();
601 VALIDATE_CONSISTENCY();
602 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800603 }
604
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700605 if (listener != NULL) {
606 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700607 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800608
Dan Stoza9f3053d2014-03-06 15:14:33 -0800609 return NO_ERROR;
610}
611
Dan Stozad9822a32014-03-28 15:25:31 -0700612status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
613 sp<Fence>* outFence) {
614 ATRACE_CALL();
615
616 if (outBuffer == NULL) {
617 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
618 return BAD_VALUE;
619 } else if (outFence == NULL) {
620 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
621 return BAD_VALUE;
622 }
623
Pablo Ceballos981066c2016-02-18 12:54:37 -0800624 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700625
Pablo Ceballos981066c2016-02-18 12:54:37 -0800626 if (mCore->mIsAbandoned) {
627 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
628 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700629 }
630
Pablo Ceballos981066c2016-02-18 12:54:37 -0800631 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
632 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
633 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700634 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800635
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700636 if (mCore->mSharedBufferMode) {
637 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
638 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800639 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700640 }
641
Pablo Ceballos981066c2016-02-18 12:54:37 -0800642 mCore->waitWhileAllocatingLocked();
643
644 if (mCore->mFreeBuffers.empty()) {
645 return NO_MEMORY;
646 }
647
648 int found = mCore->mFreeBuffers.front();
649 mCore->mFreeBuffers.remove(found);
650 mCore->mFreeSlots.insert(found);
651
652 BQ_LOGV("detachNextBuffer detached slot %d", found);
653
654 *outBuffer = mSlots[found].mGraphicBuffer;
655 *outFence = mSlots[found].mFence;
656 mCore->clearBufferSlotLocked(found);
657 VALIDATE_CONSISTENCY();
658
Dan Stozad9822a32014-03-28 15:25:31 -0700659 return NO_ERROR;
660}
661
Dan Stoza9f3053d2014-03-06 15:14:33 -0800662status_t BufferQueueProducer::attachBuffer(int* outSlot,
663 const sp<android::GraphicBuffer>& buffer) {
664 ATRACE_CALL();
665
666 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700667 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800668 return BAD_VALUE;
669 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700670 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800671 return BAD_VALUE;
672 }
673
674 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700675
676 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700677 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700678 return NO_INIT;
679 }
680
681 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700682 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700683 return NO_INIT;
684 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800685
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700686 if (mCore->mSharedBufferMode) {
687 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700688 return BAD_VALUE;
689 }
690
Dan Stoza812ed062015-06-02 15:45:22 -0700691 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
692 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
693 "[queue %u]", buffer->getGenerationNumber(),
694 mCore->mGenerationNumber);
695 return BAD_VALUE;
696 }
697
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700698 mCore->waitWhileAllocatingLocked();
699
Dan Stoza9f3053d2014-03-06 15:14:33 -0800700 status_t returnFlags = NO_ERROR;
701 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800702 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703 if (status != NO_ERROR) {
704 return status;
705 }
706
707 // This should not happen
708 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700709 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800710 return -EBUSY;
711 }
712
713 *outSlot = found;
714 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700715 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800716 *outSlot, returnFlags);
717
718 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700719 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800720 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
721 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700722 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800723 mSlots[*outSlot].mAcquireCalled = false;
724 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800725 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700726
Dan Stoza9f3053d2014-03-06 15:14:33 -0800727 return returnFlags;
728}
729
Dan Stoza289ade12014-02-28 11:17:17 -0800730status_t BufferQueueProducer::queueBuffer(int slot,
731 const QueueBufferInput &input, QueueBufferOutput *output) {
732 ATRACE_CALL();
733 ATRACE_BUFFER_INDEX(slot);
734
735 int64_t timestamp;
736 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800737 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700738 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800739 int scalingMode;
740 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700741 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800742 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800743 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700744 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700745 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800746
747 if (fence == NULL) {
748 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800749 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800750 }
751
752 switch (scalingMode) {
753 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
754 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
755 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
756 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
757 break;
758 default:
759 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800760 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800761 }
762
Dan Stoza8dc55392014-11-04 11:37:46 -0800763 sp<IConsumerListener> frameAvailableListener;
764 sp<IConsumerListener> frameReplacedListener;
765 int callbackTicket = 0;
766 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800767 { // Autolock scope
768 Mutex::Autolock lock(mCore->mMutex);
769
770 if (mCore->mIsAbandoned) {
771 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
772 return NO_INIT;
773 }
774
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700775 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
776 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
777 return NO_INIT;
778 }
779
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800780 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800781 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800782 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800783 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700784 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800785 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700786 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800787 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800788 } else if (!mSlots[slot].mRequestBufferCalled) {
789 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
790 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800791 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800792 }
793
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700794 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700795 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700796 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700797 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700798 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700799 mSlots[slot].mBufferState.mShared = true;
800 }
801
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800802 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700803 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800804 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800805 crop.left, crop.top, crop.right, crop.bottom, transform,
806 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800807
808 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
809 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700810 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800811 crop.intersect(bufferRect, &croppedRect);
812 if (croppedRect != crop) {
813 BQ_LOGE("queueBuffer: crop rect is not contained within the "
814 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800815 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800816 }
817
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800818 // Override UNKNOWN dataspace with consumer default
819 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
820 dataSpace = mCore->mDefaultBufferDataSpace;
821 }
822
Dan Stoza289ade12014-02-28 11:17:17 -0800823 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700824 mSlots[slot].mBufferState.queue();
825
Dan Stoza289ade12014-02-28 11:17:17 -0800826 ++mCore->mFrameCounter;
827 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
828
Dan Stoza289ade12014-02-28 11:17:17 -0800829 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
830 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
831 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800832 item.mTransform = transform &
833 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800834 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800835 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
836 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800837 item.mTimestamp = timestamp;
838 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800839 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800840 item.mFrameNumber = mCore->mFrameCounter;
841 item.mSlot = slot;
842 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700843 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700844 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700845 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700846 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700847 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700848 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800849
Ruben Brunk1681d952014-06-27 15:51:55 -0700850 mStickyTransform = stickyTransform;
851
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700852 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700853 if (mCore->mSharedBufferMode) {
854 mCore->mSharedBufferCache.crop = crop;
855 mCore->mSharedBufferCache.transform = transform;
856 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700857 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700858 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700859 }
860
Dan Stoza289ade12014-02-28 11:17:17 -0800861 if (mCore->mQueue.empty()) {
862 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
863 // and simply queue this buffer
864 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800865 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800866 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700867 // When the queue is not empty, we need to look at the last buffer
868 // in the queue to see if we need to replace it
869 const BufferItem& last = mCore->mQueue.itemAt(
870 mCore->mQueue.size() - 1);
871 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800872
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700873 if (!last.mIsStale) {
874 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700875
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700877 // still be around. Mark it as no longer shared if this
878 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700879 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700880 mSlots[last.mSlot].mBufferState.isFree()) {
881 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700882 }
883 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700884 if (!mSlots[last.mSlot].mBufferState.isShared()) {
885 mCore->mActiveBuffers.erase(last.mSlot);
886 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700887 }
Dan Stoza289ade12014-02-28 11:17:17 -0800888 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800889
Dan Stoza289ade12014-02-28 11:17:17 -0800890 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700891 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800892 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800893 } else {
894 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800895 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800896 }
897 }
898
899 mCore->mBufferHasBeenQueued = true;
900 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700901 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800902
903 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800904 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700905 static_cast<uint32_t>(mCore->mQueue.size()),
906 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -0800907
908 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stozae77c7662016-05-13 11:37:28 -0700909 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800910
911 // Take a ticket for the callback functions
912 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700913
Pablo Ceballos9e314332016-01-12 13:49:19 -0800914 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800915 } // Autolock scope
916
Dan Stoza8dc55392014-11-04 11:37:46 -0800917 // Don't send the GraphicBuffer through the callback, and don't send
918 // the slot number, since the consumer shouldn't need it
919 item.mGraphicBuffer.clear();
920 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
921
922 // Call back without the main BufferQueue lock held, but with the callback
923 // lock held so we can ensure that callbacks occur in order
924 {
925 Mutex::Autolock lock(mCallbackMutex);
926 while (callbackTicket != mCurrentCallbackTicket) {
927 mCallbackCondition.wait(mCallbackMutex);
928 }
929
930 if (frameAvailableListener != NULL) {
931 frameAvailableListener->onFrameAvailable(item);
932 } else if (frameReplacedListener != NULL) {
933 frameReplacedListener->onFrameReplaced(item);
934 }
935
936 ++mCurrentCallbackTicket;
937 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800938 }
939
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000940 // Wait without lock held
941 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
942 // Waiting here allows for two full buffers to be queued but not a
943 // third. In the event that frames take varying time, this makes a
944 // small trade-off in favor of latency rather than throughput.
945 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000946 }
Dan Stoza50101d02016-04-07 16:53:23 -0700947 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700948 mLastQueuedCrop = item.mCrop;
949 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000950
Dan Stoza289ade12014-02-28 11:17:17 -0800951 return NO_ERROR;
952}
953
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700954status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800955 ATRACE_CALL();
956 BQ_LOGV("cancelBuffer: slot %d", slot);
957 Mutex::Autolock lock(mCore->mMutex);
958
959 if (mCore->mIsAbandoned) {
960 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700961 return NO_INIT;
962 }
963
964 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
965 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
966 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800967 }
968
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700969 if (mCore->mSharedBufferMode) {
970 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700971 return BAD_VALUE;
972 }
973
Dan Stoza3e96f192014-03-03 10:16:19 -0800974 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800975 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800976 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700977 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700978 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800979 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700980 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700981 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800982 } else if (fence == NULL) {
983 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700984 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800985 }
986
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700987 mSlots[slot].mBufferState.cancel();
988
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700989 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700990 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700991 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700992 mSlots[slot].mBufferState.mShared = false;
993 }
994
995 // Don't put the shared buffer on the free list.
996 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800997 mCore->mActiveBuffers.erase(slot);
998 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700999 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001000
Dan Stoza289ade12014-02-28 11:17:17 -08001001 mSlots[slot].mFence = fence;
1002 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001003 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001004
1005 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001006}
1007
1008int BufferQueueProducer::query(int what, int *outValue) {
1009 ATRACE_CALL();
1010 Mutex::Autolock lock(mCore->mMutex);
1011
1012 if (outValue == NULL) {
1013 BQ_LOGE("query: outValue was NULL");
1014 return BAD_VALUE;
1015 }
1016
1017 if (mCore->mIsAbandoned) {
1018 BQ_LOGE("query: BufferQueue has been abandoned");
1019 return NO_INIT;
1020 }
1021
1022 int value;
1023 switch (what) {
1024 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001025 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001026 break;
1027 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001028 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001029 break;
1030 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001031 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001032 break;
1033 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001034 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001035 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001036 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001037 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001038 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001039 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1040 value = (mCore->mQueue.size() > 1);
1041 break;
1042 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001044 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001045 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1046 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1047 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001048 case NATIVE_WINDOW_BUFFER_AGE:
1049 if (mCore->mBufferAge > INT32_MAX) {
1050 value = 0;
1051 } else {
1052 value = static_cast<int32_t>(mCore->mBufferAge);
1053 }
1054 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001055 default:
1056 return BAD_VALUE;
1057 }
1058
1059 BQ_LOGV("query: %d? %d", what, value);
1060 *outValue = value;
1061 return NO_ERROR;
1062}
1063
Dan Stozaf0eaf252014-03-21 13:05:51 -07001064status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001065 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1066 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001067 Mutex::Autolock lock(mCore->mMutex);
1068 mConsumerName = mCore->mConsumerName;
1069 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1070 producerControlledByApp ? "true" : "false");
1071
1072 if (mCore->mIsAbandoned) {
1073 BQ_LOGE("connect: BufferQueue has been abandoned");
1074 return NO_INIT;
1075 }
1076
1077 if (mCore->mConsumerListener == NULL) {
1078 BQ_LOGE("connect: BufferQueue has no consumer");
1079 return NO_INIT;
1080 }
1081
1082 if (output == NULL) {
1083 BQ_LOGE("connect: output was NULL");
1084 return BAD_VALUE;
1085 }
1086
1087 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1088 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1089 mCore->mConnectedApi, api);
1090 return BAD_VALUE;
1091 }
1092
1093 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1094 mDequeueTimeout < 0 ?
1095 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1096 mCore->mMaxBufferCount) -
1097 mCore->getMaxBufferCountLocked();
1098 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1099 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1100 "slots. Delta = %d", delta);
1101 return BAD_VALUE;
1102 }
1103
Dan Stoza289ade12014-02-28 11:17:17 -08001104 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001105 switch (api) {
1106 case NATIVE_WINDOW_API_EGL:
1107 case NATIVE_WINDOW_API_CPU:
1108 case NATIVE_WINDOW_API_MEDIA:
1109 case NATIVE_WINDOW_API_CAMERA:
1110 mCore->mConnectedApi = api;
1111 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1112 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001113 static_cast<uint32_t>(mCore->mQueue.size()),
1114 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -08001115
Pablo Ceballos981066c2016-02-18 12:54:37 -08001116 // Set up a death notification so that we can disconnect
1117 // automatically if the remote producer dies
1118 if (listener != NULL &&
1119 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1120 status = IInterface::asBinder(listener)->linkToDeath(
1121 static_cast<IBinder::DeathRecipient*>(this));
1122 if (status != NO_ERROR) {
1123 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1124 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001125 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001126 }
1127 mCore->mConnectedProducerListener = listener;
1128 break;
1129 default:
1130 BQ_LOGE("connect: unknown API %d", api);
1131 status = BAD_VALUE;
1132 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001133 }
Robert Carr97b9c862016-09-08 13:54:35 -07001134 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001135 mCore->mBufferHasBeenQueued = false;
1136 mCore->mDequeueBufferCannotBlock = false;
1137 if (mDequeueTimeout < 0) {
1138 mCore->mDequeueBufferCannotBlock =
1139 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001140 }
Dan Stoza289ade12014-02-28 11:17:17 -08001141
Pablo Ceballos981066c2016-02-18 12:54:37 -08001142 mCore->mAllowAllocation = true;
1143 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001144 return status;
1145}
1146
Robert Carr97b9c862016-09-08 13:54:35 -07001147status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001148 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001149 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001150
1151 int status = NO_ERROR;
1152 sp<IConsumerListener> listener;
1153 { // Autolock scope
1154 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001155
1156 if (mode == DisconnectMode::AllLocal) {
1157 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1158 return NO_ERROR;
1159 }
1160 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1161 }
1162
Antoine Labour78014f32014-07-15 21:17:03 -07001163 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001164
1165 if (mCore->mIsAbandoned) {
1166 // It's not really an error to disconnect after the surface has
1167 // been abandoned; it should just be a no-op.
1168 return NO_ERROR;
1169 }
1170
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001171 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1172 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001173 // If we're asked to disconnect the currently connected api but
1174 // nobody is connected, it's not really an error.
1175 if (api == BufferQueueCore::NO_CONNECTED_API) {
1176 return NO_ERROR;
1177 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001178 }
1179
Dan Stoza289ade12014-02-28 11:17:17 -08001180 switch (api) {
1181 case NATIVE_WINDOW_API_EGL:
1182 case NATIVE_WINDOW_API_CPU:
1183 case NATIVE_WINDOW_API_MEDIA:
1184 case NATIVE_WINDOW_API_CAMERA:
1185 if (mCore->mConnectedApi == api) {
1186 mCore->freeAllBuffersLocked();
1187
1188 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001189 if (mCore->mConnectedProducerListener != NULL) {
1190 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001191 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001192 // This can fail if we're here because of the death
1193 // notification, but we just ignore it
1194 token->unlinkToDeath(
1195 static_cast<IBinder::DeathRecipient*>(this));
1196 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001197 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001198 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001199 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001200 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001201 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001202 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001203 mCore->mDequeueCondition.broadcast();
1204 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001205 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001206 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001207 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001208 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001209 }
1210 break;
1211 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001212 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001213 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001214 break;
1215 }
1216 } // Autolock scope
1217
1218 // Call back without lock held
1219 if (listener != NULL) {
1220 listener->onBuffersReleased();
1221 }
1222
1223 return status;
1224}
1225
Jesse Hall399184a2014-03-03 15:42:54 -08001226status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001227 sp<IConsumerListener> listener;
1228 { // Autolock scope
1229 Mutex::Autolock _l(mCore->mMutex);
1230 mCore->mSidebandStream = stream;
1231 listener = mCore->mConsumerListener;
1232 } // Autolock scope
1233
1234 if (listener != NULL) {
1235 listener->onSidebandStreamChanged();
1236 }
Jesse Hall399184a2014-03-03 15:42:54 -08001237 return NO_ERROR;
1238}
1239
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001240void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1241 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001242 ATRACE_CALL();
1243 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001244 size_t newBufferCount = 0;
1245 uint32_t allocWidth = 0;
1246 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001247 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001248 uint32_t allocUsage = 0;
1249 { // Autolock scope
1250 Mutex::Autolock lock(mCore->mMutex);
1251 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001252
Dan Stoza9de72932015-04-16 17:28:43 -07001253 if (!mCore->mAllowAllocation) {
1254 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1255 "BufferQueue");
1256 return;
1257 }
1258
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001259 newBufferCount = mCore->mFreeSlots.size();
1260 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001261 return;
1262 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001263
Antoine Labour78014f32014-07-15 21:17:03 -07001264 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1265 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1266 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1267 allocUsage = usage | mCore->mConsumerUsageBits;
1268
1269 mCore->mIsAllocating = true;
1270 } // Autolock scope
1271
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001272 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001273 for (size_t i = 0; i < newBufferCount; ++i) {
1274 status_t result = NO_ERROR;
1275 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stozad4079af2016-08-22 17:26:41 -07001276 allocWidth, allocHeight, allocFormat, allocUsage,
1277 {mConsumerName.string(), mConsumerName.size()}, &result));
Antoine Labour78014f32014-07-15 21:17:03 -07001278 if (result != NO_ERROR) {
1279 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1280 " %u, usage %u)", width, height, format, usage);
1281 Mutex::Autolock lock(mCore->mMutex);
1282 mCore->mIsAllocating = false;
1283 mCore->mIsAllocatingCondition.broadcast();
1284 return;
1285 }
1286 buffers.push_back(graphicBuffer);
1287 }
1288
1289 { // Autolock scope
1290 Mutex::Autolock lock(mCore->mMutex);
1291 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1292 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001293 PixelFormat checkFormat = format != 0 ?
1294 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001295 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1296 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1297 checkFormat != allocFormat || checkUsage != allocUsage) {
1298 // Something changed while we released the lock. Retry.
1299 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1300 mCore->mIsAllocating = false;
1301 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001302 continue;
1303 }
1304
Antoine Labour78014f32014-07-15 21:17:03 -07001305 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001306 if (mCore->mFreeSlots.empty()) {
1307 BQ_LOGV("allocateBuffers: a slot was occupied while "
1308 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001309 continue;
1310 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001311 auto slot = mCore->mFreeSlots.begin();
1312 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1313 mSlots[*slot].mGraphicBuffer = buffers[i];
1314 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001315
1316 // freeBufferLocked puts this slot on the free slots list. Since
1317 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001318 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001319
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001320 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1321 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001322
1323 // Make sure the erase is done after all uses of the slot
1324 // iterator since it will be invalid after this point.
1325 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001326 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001327
Antoine Labour78014f32014-07-15 21:17:03 -07001328 mCore->mIsAllocating = false;
1329 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001330 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001331 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001332 }
1333}
1334
Dan Stoza9de72932015-04-16 17:28:43 -07001335status_t BufferQueueProducer::allowAllocation(bool allow) {
1336 ATRACE_CALL();
1337 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1338
1339 Mutex::Autolock lock(mCore->mMutex);
1340 mCore->mAllowAllocation = allow;
1341 return NO_ERROR;
1342}
1343
Dan Stoza812ed062015-06-02 15:45:22 -07001344status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1345 ATRACE_CALL();
1346 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1347
1348 Mutex::Autolock lock(mCore->mMutex);
1349 mCore->mGenerationNumber = generationNumber;
1350 return NO_ERROR;
1351}
1352
Dan Stozac6f30bd2015-06-08 09:32:50 -07001353String8 BufferQueueProducer::getConsumerName() const {
1354 ATRACE_CALL();
1355 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1356 return mConsumerName;
1357}
1358
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001359status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001360 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001361 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001362
1363 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001364 if (!sharedBufferMode) {
1365 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001366 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001367 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001368 return NO_ERROR;
1369}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001370
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001371status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1372 ATRACE_CALL();
1373 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1374
1375 Mutex::Autolock lock(mCore->mMutex);
1376
1377 mCore->mAutoRefresh = autoRefresh;
1378 return NO_ERROR;
1379}
1380
Dan Stoza127fc632015-06-30 13:43:32 -07001381status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1382 ATRACE_CALL();
1383 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1384
Pablo Ceballos981066c2016-02-18 12:54:37 -08001385 Mutex::Autolock lock(mCore->mMutex);
1386 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1387 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1388 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1389 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1390 "available slots. Delta = %d", delta);
1391 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001392 }
1393
Pablo Ceballos981066c2016-02-18 12:54:37 -08001394 mDequeueTimeout = timeout;
1395 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001396
Pablo Ceballos981066c2016-02-18 12:54:37 -08001397 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001398 return NO_ERROR;
1399}
1400
Dan Stoza50101d02016-04-07 16:53:23 -07001401status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001402 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001403 ATRACE_CALL();
1404 BQ_LOGV("getLastQueuedBuffer");
1405
1406 Mutex::Autolock lock(mCore->mMutex);
1407 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1408 *outBuffer = nullptr;
1409 *outFence = Fence::NO_FENCE;
1410 return NO_ERROR;
1411 }
1412
1413 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1414 *outFence = mLastQueueBufferFence;
1415
John Reck1a61da52016-04-28 13:18:15 -07001416 // Currently only SurfaceFlinger internally ever changes
1417 // GLConsumer's filtering mode, so we just use 'true' here as
1418 // this is slightly specialized for the current client of this API,
1419 // which does want filtering.
1420 GLConsumer::computeTransformMatrix(outTransformMatrix,
1421 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1422 mLastQueuedTransform, true /* filter */);
1423
Dan Stoza50101d02016-04-07 16:53:23 -07001424 return NO_ERROR;
1425}
1426
Pablo Ceballosce796e72016-02-04 19:10:51 -08001427bool BufferQueueProducer::getFrameTimestamps(uint64_t frameNumber,
1428 FrameTimestamps* outTimestamps) const {
1429 ATRACE_CALL();
1430 BQ_LOGV("getFrameTimestamps, %" PRIu64, frameNumber);
1431 sp<IConsumerListener> listener;
1432
1433 {
1434 Mutex::Autolock lock(mCore->mMutex);
1435 listener = mCore->mConsumerListener;
1436 }
1437 if (listener != NULL) {
1438 return listener->getFrameTimestamps(frameNumber, outTimestamps);
1439 }
1440 return false;
1441}
1442
Dan Stoza289ade12014-02-28 11:17:17 -08001443void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1444 // If we're here, it means that a producer we were connected to died.
1445 // We're guaranteed that we are still connected to it because we remove
1446 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1447 // without synchronization here.
1448 int api = mCore->mConnectedApi;
1449 disconnect(api);
1450}
1451
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001452status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1453 BQ_LOGV("getUniqueId");
1454
1455 *outId = mCore->mUniqueId;
1456 return NO_ERROR;
1457}
1458
Dan Stoza289ade12014-02-28 11:17:17 -08001459} // namespace android