blob: 56f1a0905c7738e871226a346dcc0dba6f54ccbb [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
Dan Stoza289ade12014-02-28 11:17:17 -080023#define EGL_EGLEXT_PROTOTYPES
24
25#include <gui/BufferItem.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
28#include <gui/IConsumerListener.h>
29#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080031
32#include <utils/Log.h>
33#include <utils/Trace.h>
34
35namespace android {
36
37BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
38 mCore(core),
39 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070040 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070041 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080042 mLastQueueBufferFence(Fence::NO_FENCE),
43 mCallbackMutex(),
44 mNextCallbackTicket(0),
45 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070046 mCallbackCondition(),
47 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080048
49BufferQueueProducer::~BufferQueueProducer() {}
50
51status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
52 ATRACE_CALL();
53 BQ_LOGV("requestBuffer: slot %d", slot);
54 Mutex::Autolock lock(mCore->mMutex);
55
56 if (mCore->mIsAbandoned) {
57 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
58 return NO_INIT;
59 }
60
Pablo Ceballos583b1b32015-09-03 18:23:52 -070061 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
62 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
63 return NO_INIT;
64 }
65
Dan Stoza3e96f192014-03-03 10:16:19 -080066 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080067 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080068 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080069 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070070 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080071 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070072 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080073 return BAD_VALUE;
74 }
75
76 mSlots[slot].mRequestBufferCalled = true;
77 *buf = mSlots[slot].mGraphicBuffer;
78 return NO_ERROR;
79}
80
Pablo Ceballosfa455352015-08-12 17:47:47 -070081status_t BufferQueueProducer::setMaxDequeuedBufferCount(
82 int maxDequeuedBuffers) {
83 ATRACE_CALL();
84 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
85 maxDequeuedBuffers);
86
87 sp<IConsumerListener> listener;
88 { // Autolock scope
89 Mutex::Autolock lock(mCore->mMutex);
90 mCore->waitWhileAllocatingLocked();
91
92 if (mCore->mIsAbandoned) {
93 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
94 "abandoned");
95 return NO_INIT;
96 }
97
98 // There must be no dequeued buffers when changing the buffer count.
99 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700100 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700101 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
102 return BAD_VALUE;
103 }
104 }
105
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700106 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700107 bufferCount += maxDequeuedBuffers;
108
109 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
110 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
111 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
112 return BAD_VALUE;
113 }
114
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700115 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700116 if (bufferCount < minBufferSlots) {
117 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
118 "less than minimum %d", bufferCount, minBufferSlots);
119 return BAD_VALUE;
120 }
121
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700122 if (bufferCount > mCore->mMaxBufferCount) {
123 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700124 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
125 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
126 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
127 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700128 return BAD_VALUE;
129 }
130
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131 // Here we are guaranteed that the producer doesn't have any dequeued
132 // buffers and will release all of its buffer references. We don't
133 // clear the queue, however, so that currently queued buffers still
134 // get displayed.
135 mCore->freeAllBuffersLocked();
136 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137 mCore->mDequeueCondition.broadcast();
138 listener = mCore->mConsumerListener;
139 } // Autolock scope
140
141 // Call back without lock held
142 if (listener != NULL) {
143 listener->onBuffersReleased();
144 }
145
146 return NO_ERROR;
147}
148
149status_t BufferQueueProducer::setAsyncMode(bool async) {
150 ATRACE_CALL();
151 BQ_LOGV("setAsyncMode: async = %d", async);
152
153 sp<IConsumerListener> listener;
154 { // Autolock scope
155 Mutex::Autolock lock(mCore->mMutex);
156 mCore->waitWhileAllocatingLocked();
157
158 if (mCore->mIsAbandoned) {
159 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
160 return NO_INIT;
161 }
162
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700163 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700164 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
165 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700166 BQ_LOGE("setAsyncMode(%d): this call would cause the "
167 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700168 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
169 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
170 mCore->mMaxDequeuedBufferCount,
171 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700172 return BAD_VALUE;
173 }
174
Pablo Ceballosfa455352015-08-12 17:47:47 -0700175 mCore->mAsyncMode = async;
176 mCore->mDequeueCondition.broadcast();
177 listener = mCore->mConsumerListener;
178 } // Autolock scope
179
180 // Call back without lock held
181 if (listener != NULL) {
182 listener->onBuffersReleased();
183 }
184 return NO_ERROR;
185}
186
Dan Stoza5ecfb682016-01-04 17:01:02 -0800187int BufferQueueProducer::getFreeBufferLocked() const {
188 if (mCore->mFreeBuffers.empty()) {
189 return BufferQueueCore::INVALID_BUFFER_SLOT;
190 }
191 auto slot = mCore->mFreeBuffers.front();
192 mCore->mFreeBuffers.pop_front();
193 return slot;
194}
195
196int BufferQueueProducer::getFreeSlotLocked(int maxBufferCount) const {
197 if (mCore->mFreeSlots.empty()) {
198 return BufferQueueCore::INVALID_BUFFER_SLOT;
199 }
200 auto slot = *(mCore->mFreeSlots.begin());
201 if (slot < maxBufferCount) {
202 mCore->mFreeSlots.erase(slot);
203 return slot;
204 }
205 return BufferQueueCore::INVALID_BUFFER_SLOT;
206}
207
208status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700209 int* found, status_t* returnFlags) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800210 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
211 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800212 bool tryAgain = true;
213 while (tryAgain) {
214 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800215 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800216 return NO_INIT;
217 }
218
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700219 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800220
221 // Free up any buffers that are in slots beyond the max buffer count
222 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700223 assert(mSlots[s].mBufferState.isFree());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800224 if (mSlots[s].mGraphicBuffer != NULL) {
225 mCore->freeBufferLocked(s);
226 *returnFlags |= RELEASE_ALL_BUFFERS;
227 }
228 }
229
Dan Stoza9f3053d2014-03-06 15:14:33 -0800230 int dequeuedCount = 0;
231 int acquiredCount = 0;
232 for (int s = 0; s < maxBufferCount; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700233 if (mSlots[s].mBufferState.isDequeued()) {
234 ++dequeuedCount;
235 }
236 if (mSlots[s].mBufferState.isAcquired()) {
237 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800238 }
239 }
240
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700241 // Producers are not allowed to dequeue more than
242 // mMaxDequeuedBufferCount buffers.
243 // This check is only done if a buffer has already been queued
244 if (mCore->mBufferHasBeenQueued &&
245 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
246 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800247 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800248 return INVALID_OPERATION;
249 }
250
Dan Stoza0de7ea72015-04-23 13:20:51 -0700251 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
252
Dan Stozaae3c3682014-04-18 15:43:35 -0700253 // If we disconnect and reconnect quickly, we can be in a state where
254 // our slots are empty but we have many buffers in the queue. This can
255 // cause us to run out of memory if we outrun the consumer. Wait here if
256 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700257 bool tooManyBuffers = mCore->mQueue.size()
258 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700259 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800260 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700261 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700262 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700263 // If in single buffer mode and a shared buffer exists, always
264 // return it.
265 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
266 BufferQueueCore::INVALID_BUFFER_SLOT) {
267 *found = mCore->mSingleBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800268 } else {
269 if (caller == FreeSlotCaller::Dequeue) {
270 // If we're calling this from dequeue, prefer free buffers
271 auto slot = getFreeBufferLocked();
272 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
273 *found = slot;
274 } else if (mCore->mAllowAllocation) {
275 *found = getFreeSlotLocked(maxBufferCount);
276 }
277 } else {
278 // If we're calling this from attach, prefer free slots
279 auto slot = getFreeSlotLocked(maxBufferCount);
280 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
281 *found = slot;
282 } else {
283 *found = getFreeBufferLocked();
284 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700285 }
286 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700287 }
288
289 // If no buffer is found, or if the queue has too many buffers
290 // outstanding, wait for a buffer to be acquired or released, or for the
291 // max buffer count to change.
292 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
293 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800294 if (tryAgain) {
295 // Return an error if we're in non-blocking mode (producer and
296 // consumer are controlled by the application).
297 // However, the consumer is allowed to briefly acquire an extra
298 // buffer (which could cause us to have to wait here), which is
299 // okay, since it is only used to implement an atomic acquire +
300 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700301 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800302 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
303 return WOULD_BLOCK;
304 }
Dan Stoza127fc632015-06-30 13:43:32 -0700305 if (mDequeueTimeout >= 0) {
306 status_t result = mCore->mDequeueCondition.waitRelative(
307 mCore->mMutex, mDequeueTimeout);
308 if (result == TIMED_OUT) {
309 return result;
310 }
311 } else {
312 mCore->mDequeueCondition.wait(mCore->mMutex);
313 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800314 }
315 } // while (tryAgain)
316
317 return NO_ERROR;
318}
319
Dan Stoza289ade12014-02-28 11:17:17 -0800320status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700321 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
322 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800323 ATRACE_CALL();
324 { // Autolock scope
325 Mutex::Autolock lock(mCore->mMutex);
326 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700327
328 if (mCore->mIsAbandoned) {
329 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
330 return NO_INIT;
331 }
332
333 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
334 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
335 return NO_INIT;
336 }
Dan Stoza289ade12014-02-28 11:17:17 -0800337 } // Autolock scope
338
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700339 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
340 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800341
342 if ((width && !height) || (!width && height)) {
343 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
344 return BAD_VALUE;
345 }
346
347 status_t returnFlags = NO_ERROR;
348 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
349 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800350 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800351
352 { // Autolock scope
353 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700354 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800355
356 if (format == 0) {
357 format = mCore->mDefaultBufferFormat;
358 }
359
360 // Enable the usage bits the consumer requested
361 usage |= mCore->mConsumerUsageBits;
362
Dan Stoza9de72932015-04-16 17:28:43 -0700363 const bool useDefaultSize = !width && !height;
364 if (useDefaultSize) {
365 width = mCore->mDefaultWidth;
366 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800367 }
Dan Stoza289ade12014-02-28 11:17:17 -0800368
Dan Stoza9de72932015-04-16 17:28:43 -0700369 int found = BufferItem::INVALID_BUFFER_SLOT;
370 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800371 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
372 &found, &returnFlags);
Dan Stoza9de72932015-04-16 17:28:43 -0700373 if (status != NO_ERROR) {
374 return status;
375 }
376
377 // This should not happen
378 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
379 BQ_LOGE("dequeueBuffer: no available buffer slots");
380 return -EBUSY;
381 }
382
383 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
384
385 // If we are not allowed to allocate new buffers,
386 // waitForFreeSlotThenRelock must have returned a slot containing a
387 // buffer. If this buffer would require reallocation to meet the
388 // requested attributes, we free it and attempt to get another one.
389 if (!mCore->mAllowAllocation) {
390 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700391 if (mCore->mSingleBufferMode &&
392 mCore->mSingleBufferSlot == found) {
393 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
394 "buffer");
395 return BAD_VALUE;
396 }
397
Dan Stoza9de72932015-04-16 17:28:43 -0700398 mCore->freeBufferLocked(found);
399 found = BufferItem::INVALID_BUFFER_SLOT;
400 continue;
401 }
402 }
Dan Stoza289ade12014-02-28 11:17:17 -0800403 }
404
405 *outSlot = found;
406 ATRACE_BUFFER_INDEX(found);
407
Dan Stoza9f3053d2014-03-06 15:14:33 -0800408 attachedByConsumer = mSlots[found].mAttachedByConsumer;
409
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700410 mSlots[found].mBufferState.dequeue();
411
412 // If single buffer mode has just been enabled, cache the slot of the
413 // first buffer that is dequeued and mark it as the shared buffer.
414 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
415 BufferQueueCore::INVALID_BUFFER_SLOT) {
416 mCore->mSingleBufferSlot = found;
417 mSlots[found].mBufferState.mShared = true;
418 }
Dan Stoza289ade12014-02-28 11:17:17 -0800419
420 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
421 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700422 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800423 {
424 mSlots[found].mAcquireCalled = false;
425 mSlots[found].mGraphicBuffer = NULL;
426 mSlots[found].mRequestBufferCalled = false;
427 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
428 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
429 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800430 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700431 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800432
433 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800434 } else {
435 // We add 1 because that will be the frame number when this buffer
436 // is queued
437 mCore->mBufferAge =
438 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800439 }
440
Dan Stoza800b41a2015-04-28 14:20:04 -0700441 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
442 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800443
Dan Stoza289ade12014-02-28 11:17:17 -0800444 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
445 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
446 "slot=%d w=%d h=%d format=%u",
447 found, buffer->width, buffer->height, buffer->format);
448 }
449
450 eglDisplay = mSlots[found].mEglDisplay;
451 eglFence = mSlots[found].mEglFence;
452 *outFence = mSlots[found].mFence;
453 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
454 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700455
456 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800457 } // Autolock scope
458
459 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
460 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700461 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800462 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800463 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800464 { // Autolock scope
465 Mutex::Autolock lock(mCore->mMutex);
466
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700467 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
468 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
469 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
470 }
471
472 mCore->mIsAllocating = false;
473 mCore->mIsAllocatingCondition.broadcast();
474
475 if (graphicBuffer == NULL) {
476 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
477 return error;
478 }
479
Dan Stoza289ade12014-02-28 11:17:17 -0800480 if (mCore->mIsAbandoned) {
481 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
482 return NO_INIT;
483 }
Dan Stoza289ade12014-02-28 11:17:17 -0800484 } // Autolock scope
485 }
486
Dan Stoza9f3053d2014-03-06 15:14:33 -0800487 if (attachedByConsumer) {
488 returnFlags |= BUFFER_NEEDS_REALLOCATION;
489 }
490
Dan Stoza289ade12014-02-28 11:17:17 -0800491 if (eglFence != EGL_NO_SYNC_KHR) {
492 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
493 1000000000);
494 // If something goes wrong, log the error, but return the buffer without
495 // synchronizing access to it. It's too late at this point to abort the
496 // dequeue operation.
497 if (result == EGL_FALSE) {
498 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
499 eglGetError());
500 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
501 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
502 }
503 eglDestroySyncKHR(eglDisplay, eglFence);
504 }
505
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700506 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
507 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800508 mSlots[*outSlot].mFrameNumber,
509 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
510
511 return returnFlags;
512}
513
Dan Stoza9f3053d2014-03-06 15:14:33 -0800514status_t BufferQueueProducer::detachBuffer(int slot) {
515 ATRACE_CALL();
516 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700517 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800518 Mutex::Autolock lock(mCore->mMutex);
519
520 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800522 return NO_INIT;
523 }
524
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700525 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700526 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700527 return NO_INIT;
528 }
529
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700530 if (mCore->mSingleBufferMode) {
531 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer"
532 "mode");
533 return BAD_VALUE;
534 }
535
Dan Stoza9f3053d2014-03-06 15:14:33 -0800536 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700537 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800538 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
539 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700540 } else if (!mSlots[slot].mBufferState.isDequeued()) {
541 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
542 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800543 return BAD_VALUE;
544 } else if (!mSlots[slot].mRequestBufferCalled) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700545 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800546 slot);
547 return BAD_VALUE;
548 }
549
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700550 mSlots[slot].mBufferState.detachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800551 mCore->freeBufferLocked(slot);
552 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700553 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800554
555 return NO_ERROR;
556}
557
Dan Stozad9822a32014-03-28 15:25:31 -0700558status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
559 sp<Fence>* outFence) {
560 ATRACE_CALL();
561
562 if (outBuffer == NULL) {
563 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
564 return BAD_VALUE;
565 } else if (outFence == NULL) {
566 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
567 return BAD_VALUE;
568 }
569
570 Mutex::Autolock lock(mCore->mMutex);
571
572 if (mCore->mIsAbandoned) {
573 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
574 return NO_INIT;
575 }
576
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700577 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
578 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
579 return NO_INIT;
580 }
581
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700582 if (mCore->mSingleBufferMode) {
583 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
584 "mode");
585 return BAD_VALUE;
586 }
587
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700588 mCore->waitWhileAllocatingLocked();
589
Dan Stoza0de7ea72015-04-23 13:20:51 -0700590 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000591 return NO_MEMORY;
592 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700593
Dan Stoza0de7ea72015-04-23 13:20:51 -0700594 int found = mCore->mFreeBuffers.front();
595 mCore->mFreeBuffers.remove(found);
596
Dan Stozad9822a32014-03-28 15:25:31 -0700597 BQ_LOGV("detachNextBuffer detached slot %d", found);
598
599 *outBuffer = mSlots[found].mGraphicBuffer;
600 *outFence = mSlots[found].mFence;
601 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700602 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700603
604 return NO_ERROR;
605}
606
Dan Stoza9f3053d2014-03-06 15:14:33 -0800607status_t BufferQueueProducer::attachBuffer(int* outSlot,
608 const sp<android::GraphicBuffer>& buffer) {
609 ATRACE_CALL();
610
611 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700612 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800613 return BAD_VALUE;
614 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700615 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800616 return BAD_VALUE;
617 }
618
619 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700620
621 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700622 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700623 return NO_INIT;
624 }
625
626 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700627 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700628 return NO_INIT;
629 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700631 if (mCore->mSingleBufferMode) {
632 BQ_LOGE("attachBuffer: cannot atach a buffer in single buffer mode");
633 return BAD_VALUE;
634 }
635
Dan Stoza812ed062015-06-02 15:45:22 -0700636 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
637 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
638 "[queue %u]", buffer->getGenerationNumber(),
639 mCore->mGenerationNumber);
640 return BAD_VALUE;
641 }
642
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700643 mCore->waitWhileAllocatingLocked();
644
Dan Stoza9f3053d2014-03-06 15:14:33 -0800645 status_t returnFlags = NO_ERROR;
646 int found;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800647 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700648 &returnFlags);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800649 if (status != NO_ERROR) {
650 return status;
651 }
652
653 // This should not happen
654 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700655 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800656 return -EBUSY;
657 }
658
659 *outSlot = found;
660 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700661 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800662 *outSlot, returnFlags);
663
664 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700665 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800666 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
667 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700668 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800669
Dan Stoza0de7ea72015-04-23 13:20:51 -0700670 mCore->validateConsistencyLocked();
671
Dan Stoza9f3053d2014-03-06 15:14:33 -0800672 return returnFlags;
673}
674
Dan Stoza289ade12014-02-28 11:17:17 -0800675status_t BufferQueueProducer::queueBuffer(int slot,
676 const QueueBufferInput &input, QueueBufferOutput *output) {
677 ATRACE_CALL();
678 ATRACE_BUFFER_INDEX(slot);
679
680 int64_t timestamp;
681 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800682 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700683 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800684 int scalingMode;
685 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700686 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800687 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800688 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700689 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700690 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800691
692 if (fence == NULL) {
693 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800694 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800695 }
696
697 switch (scalingMode) {
698 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
699 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
700 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
701 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
702 break;
703 default:
704 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800705 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800706 }
707
Dan Stoza8dc55392014-11-04 11:37:46 -0800708 sp<IConsumerListener> frameAvailableListener;
709 sp<IConsumerListener> frameReplacedListener;
710 int callbackTicket = 0;
711 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800712 { // Autolock scope
713 Mutex::Autolock lock(mCore->mMutex);
714
715 if (mCore->mIsAbandoned) {
716 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
717 return NO_INIT;
718 }
719
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700720 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
721 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
722 return NO_INIT;
723 }
724
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700725 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800726
727 if (slot < 0 || slot >= maxBufferCount) {
728 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
729 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800730 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700731 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800732 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700733 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800734 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800735 } else if (!mSlots[slot].mRequestBufferCalled) {
736 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
737 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800739 }
740
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800741 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700742 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800743 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800744 crop.left, crop.top, crop.right, crop.bottom, transform,
745 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800746
747 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
748 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700749 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800750 crop.intersect(bufferRect, &croppedRect);
751 if (croppedRect != crop) {
752 BQ_LOGE("queueBuffer: crop rect is not contained within the "
753 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800755 }
756
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800757 // Override UNKNOWN dataspace with consumer default
758 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
759 dataSpace = mCore->mDefaultBufferDataSpace;
760 }
761
Dan Stoza289ade12014-02-28 11:17:17 -0800762 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700763 mSlots[slot].mBufferState.queue();
764
Dan Stoza289ade12014-02-28 11:17:17 -0800765 ++mCore->mFrameCounter;
766 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
767
Dan Stoza289ade12014-02-28 11:17:17 -0800768 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
769 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
770 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800771 item.mTransform = transform &
772 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800773 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800774 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
775 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800776 item.mTimestamp = timestamp;
777 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800778 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800779 item.mFrameNumber = mCore->mFrameCounter;
780 item.mSlot = slot;
781 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700782 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700783 mCore->mDequeueBufferCannotBlock ||
784 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700785 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700786 item.mSingleBufferMode = mCore->mSingleBufferMode;
787 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800788
Ruben Brunk1681d952014-06-27 15:51:55 -0700789 mStickyTransform = stickyTransform;
790
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700791 // Cache the shared buffer data so that the BufferItem can be recreated.
792 if (mCore->mSingleBufferMode) {
793 mCore->mSingleBufferCache.crop = crop;
794 mCore->mSingleBufferCache.transform = transform;
795 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
796 scalingMode);
797 mCore->mSingleBufferCache.dataspace = dataSpace;
798 }
799
Dan Stoza289ade12014-02-28 11:17:17 -0800800 if (mCore->mQueue.empty()) {
801 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
802 // and simply queue this buffer
803 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800804 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800805 } else {
806 // When the queue is not empty, we need to look at the front buffer
807 // state to see if we need to replace it
808 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
809 if (front->mIsDroppable) {
810 // If the front queued buffer is still being tracked, we first
811 // mark it as freed
812 if (mCore->stillTracking(front)) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700813 mSlots[front->mSlot].mBufferState.freeQueued();
814
815 // After leaving single buffer mode, the shared buffer will
816 // still be around. Mark it as no longer shared if this
817 // operation causes it to be free.
818 if (!mCore->mSingleBufferMode &&
819 mSlots[front->mSlot].mBufferState.isFree()) {
820 mSlots[front->mSlot].mBufferState.mShared = false;
821 }
822 // Don't put the shared buffer on the free list.
823 if (!mSlots[front->mSlot].mBufferState.isShared()) {
824 mCore->mFreeBuffers.push_front(front->mSlot);
825 }
Dan Stoza289ade12014-02-28 11:17:17 -0800826 }
827 // Overwrite the droppable buffer with the incoming one
828 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800829 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800830 } else {
831 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800832 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800833 }
834 }
835
836 mCore->mBufferHasBeenQueued = true;
837 mCore->mDequeueCondition.broadcast();
838
839 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800840 mCore->mTransformHint,
841 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800842
843 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800844
845 // Take a ticket for the callback functions
846 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700847
848 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800849 } // Autolock scope
850
Dan Stoza8dc55392014-11-04 11:37:46 -0800851 // Don't send the GraphicBuffer through the callback, and don't send
852 // the slot number, since the consumer shouldn't need it
853 item.mGraphicBuffer.clear();
854 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
855
856 // Call back without the main BufferQueue lock held, but with the callback
857 // lock held so we can ensure that callbacks occur in order
858 {
859 Mutex::Autolock lock(mCallbackMutex);
860 while (callbackTicket != mCurrentCallbackTicket) {
861 mCallbackCondition.wait(mCallbackMutex);
862 }
863
864 if (frameAvailableListener != NULL) {
865 frameAvailableListener->onFrameAvailable(item);
866 } else if (frameReplacedListener != NULL) {
867 frameReplacedListener->onFrameReplaced(item);
868 }
869
870 ++mCurrentCallbackTicket;
871 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800872 }
873
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000874 // Wait without lock held
875 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
876 // Waiting here allows for two full buffers to be queued but not a
877 // third. In the event that frames take varying time, this makes a
878 // small trade-off in favor of latency rather than throughput.
879 mLastQueueBufferFence->waitForever("Throttling EGL Production");
880 mLastQueueBufferFence = fence;
881 }
882
Dan Stoza289ade12014-02-28 11:17:17 -0800883 return NO_ERROR;
884}
885
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700886status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800887 ATRACE_CALL();
888 BQ_LOGV("cancelBuffer: slot %d", slot);
889 Mutex::Autolock lock(mCore->mMutex);
890
891 if (mCore->mIsAbandoned) {
892 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700893 return NO_INIT;
894 }
895
896 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
897 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
898 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800899 }
900
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700901 if (mCore->mSingleBufferMode) {
902 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
903 return BAD_VALUE;
904 }
905
Dan Stoza3e96f192014-03-03 10:16:19 -0800906 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800907 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800908 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700909 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800911 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700912 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700913 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800914 } else if (fence == NULL) {
915 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700916 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800917 }
918
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700919 mSlots[slot].mBufferState.cancel();
920
921 // After leaving single buffer mode, the shared buffer will still be around.
922 // Mark it as no longer shared if this operation causes it to be free.
923 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
924 mSlots[slot].mBufferState.mShared = false;
925 }
926
927 // Don't put the shared buffer on the free list.
928 if (!mSlots[slot].mBufferState.isShared()) {
929 mCore->mFreeBuffers.push_front(slot);
930 }
Dan Stoza289ade12014-02-28 11:17:17 -0800931 mSlots[slot].mFence = fence;
932 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700933 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700934
935 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800936}
937
938int BufferQueueProducer::query(int what, int *outValue) {
939 ATRACE_CALL();
940 Mutex::Autolock lock(mCore->mMutex);
941
942 if (outValue == NULL) {
943 BQ_LOGE("query: outValue was NULL");
944 return BAD_VALUE;
945 }
946
947 if (mCore->mIsAbandoned) {
948 BQ_LOGE("query: BufferQueue has been abandoned");
949 return NO_INIT;
950 }
951
952 int value;
953 switch (what) {
954 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800955 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800956 break;
957 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800958 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800959 break;
960 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800961 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800962 break;
963 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700964 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800965 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700966 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800967 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700968 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800969 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
970 value = (mCore->mQueue.size() > 1);
971 break;
972 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800973 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800974 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800975 case NATIVE_WINDOW_DEFAULT_DATASPACE:
976 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
977 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800978 case NATIVE_WINDOW_BUFFER_AGE:
979 if (mCore->mBufferAge > INT32_MAX) {
980 value = 0;
981 } else {
982 value = static_cast<int32_t>(mCore->mBufferAge);
983 }
984 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800985 default:
986 return BAD_VALUE;
987 }
988
989 BQ_LOGV("query: %d? %d", what, value);
990 *outValue = value;
991 return NO_ERROR;
992}
993
Dan Stozaf0eaf252014-03-21 13:05:51 -0700994status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800995 int api, bool producerControlledByApp, QueueBufferOutput *output) {
996 ATRACE_CALL();
997 Mutex::Autolock lock(mCore->mMutex);
998 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700999 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -08001000 producerControlledByApp ? "true" : "false");
1001
Dan Stozaae3c3682014-04-18 15:43:35 -07001002 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001003 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -07001004 return NO_INIT;
1005 }
Dan Stoza289ade12014-02-28 11:17:17 -08001006
Dan Stozaae3c3682014-04-18 15:43:35 -07001007 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001008 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -07001009 return NO_INIT;
1010 }
Dan Stoza289ade12014-02-28 11:17:17 -08001011
Dan Stozaae3c3682014-04-18 15:43:35 -07001012 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001013 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -07001014 return BAD_VALUE;
1015 }
Dan Stoza289ade12014-02-28 11:17:17 -08001016
Dan Stozaae3c3682014-04-18 15:43:35 -07001017 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001018 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -07001019 mCore->mConnectedApi, api);
1020 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001021 }
1022
1023 int status = NO_ERROR;
1024 switch (api) {
1025 case NATIVE_WINDOW_API_EGL:
1026 case NATIVE_WINDOW_API_CPU:
1027 case NATIVE_WINDOW_API_MEDIA:
1028 case NATIVE_WINDOW_API_CAMERA:
1029 mCore->mConnectedApi = api;
1030 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001031 mCore->mTransformHint,
1032 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001033
1034 // Set up a death notification so that we can disconnect
1035 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -07001036 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -08001037 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1038 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -08001039 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -07001040 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001041 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001042 strerror(-status), status);
1043 }
1044 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001045 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001046 break;
1047 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001048 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001049 status = BAD_VALUE;
1050 break;
1051 }
1052
1053 mCore->mBufferHasBeenQueued = false;
Dan Stoza127fc632015-06-30 13:43:32 -07001054 mCore->mDequeueBufferCannotBlock = false;
1055 if (mDequeueTimeout < 0) {
1056 mCore->mDequeueBufferCannotBlock =
1057 mCore->mConsumerControlledByApp && producerControlledByApp;
1058 }
Dan Stoza2b83cc92015-05-12 14:55:15 -07001059 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -08001060
1061 return status;
1062}
1063
1064status_t BufferQueueProducer::disconnect(int api) {
1065 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001066 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001067
1068 int status = NO_ERROR;
1069 sp<IConsumerListener> listener;
1070 { // Autolock scope
1071 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001072 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001073
1074 if (mCore->mIsAbandoned) {
1075 // It's not really an error to disconnect after the surface has
1076 // been abandoned; it should just be a no-op.
1077 return NO_ERROR;
1078 }
1079
1080 switch (api) {
1081 case NATIVE_WINDOW_API_EGL:
1082 case NATIVE_WINDOW_API_CPU:
1083 case NATIVE_WINDOW_API_MEDIA:
1084 case NATIVE_WINDOW_API_CAMERA:
1085 if (mCore->mConnectedApi == api) {
1086 mCore->freeAllBuffersLocked();
1087
1088 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001089 if (mCore->mConnectedProducerListener != NULL) {
1090 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001091 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001092 // This can fail if we're here because of the death
1093 // notification, but we just ignore it
1094 token->unlinkToDeath(
1095 static_cast<IBinder::DeathRecipient*>(this));
1096 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001097 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001098 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001099 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001100 mCore->mDequeueCondition.broadcast();
1101 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001102 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001103 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001104 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001105 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001106 }
1107 break;
1108 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001109 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001110 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001111 break;
1112 }
1113 } // Autolock scope
1114
1115 // Call back without lock held
1116 if (listener != NULL) {
1117 listener->onBuffersReleased();
1118 }
1119
1120 return status;
1121}
1122
Jesse Hall399184a2014-03-03 15:42:54 -08001123status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001124 sp<IConsumerListener> listener;
1125 { // Autolock scope
1126 Mutex::Autolock _l(mCore->mMutex);
1127 mCore->mSidebandStream = stream;
1128 listener = mCore->mConsumerListener;
1129 } // Autolock scope
1130
1131 if (listener != NULL) {
1132 listener->onSidebandStreamChanged();
1133 }
Jesse Hall399184a2014-03-03 15:42:54 -08001134 return NO_ERROR;
1135}
1136
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001137void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1138 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001139 ATRACE_CALL();
1140 while (true) {
1141 Vector<int> freeSlots;
1142 size_t newBufferCount = 0;
1143 uint32_t allocWidth = 0;
1144 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001145 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001146 uint32_t allocUsage = 0;
1147 { // Autolock scope
1148 Mutex::Autolock lock(mCore->mMutex);
1149 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001150
Dan Stoza9de72932015-04-16 17:28:43 -07001151 if (!mCore->mAllowAllocation) {
1152 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1153 "BufferQueue");
1154 return;
1155 }
1156
Antoine Labour78014f32014-07-15 21:17:03 -07001157 int currentBufferCount = 0;
1158 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1159 if (mSlots[slot].mGraphicBuffer != NULL) {
1160 ++currentBufferCount;
1161 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001162 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001163 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1164 slot);
1165 continue;
1166 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001167
Antoine Labour11f14872014-07-25 18:14:42 -07001168 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001169 }
1170 }
1171
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001172 int maxBufferCount = mCore->getMaxBufferCountLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001173 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1174 currentBufferCount, maxBufferCount);
1175 if (maxBufferCount <= currentBufferCount)
1176 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001177 newBufferCount =
1178 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001179 if (freeSlots.size() < newBufferCount) {
1180 BQ_LOGE("allocateBuffers: ran out of free slots");
1181 return;
1182 }
1183 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1184 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1185 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1186 allocUsage = usage | mCore->mConsumerUsageBits;
1187
1188 mCore->mIsAllocating = true;
1189 } // Autolock scope
1190
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001191 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001192 for (size_t i = 0; i < newBufferCount; ++i) {
1193 status_t result = NO_ERROR;
1194 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1195 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1196 if (result != NO_ERROR) {
1197 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1198 " %u, usage %u)", width, height, format, usage);
1199 Mutex::Autolock lock(mCore->mMutex);
1200 mCore->mIsAllocating = false;
1201 mCore->mIsAllocatingCondition.broadcast();
1202 return;
1203 }
1204 buffers.push_back(graphicBuffer);
1205 }
1206
1207 { // Autolock scope
1208 Mutex::Autolock lock(mCore->mMutex);
1209 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1210 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001211 PixelFormat checkFormat = format != 0 ?
1212 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001213 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1214 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1215 checkFormat != allocFormat || checkUsage != allocUsage) {
1216 // Something changed while we released the lock. Retry.
1217 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1218 mCore->mIsAllocating = false;
1219 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001220 continue;
1221 }
1222
Antoine Labour78014f32014-07-15 21:17:03 -07001223 for (size_t i = 0; i < newBufferCount; ++i) {
1224 int slot = freeSlots[i];
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001225 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001226 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1227 // allocated.
1228 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1229 "Dropping allocated buffer.", slot);
1230 continue;
1231 }
1232 mCore->freeBufferLocked(slot); // Clean up the slot first
1233 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001234 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001235
1236 // freeBufferLocked puts this slot on the free slots list. Since
1237 // we then attached a buffer, move the slot to free buffer list.
1238 mCore->mFreeSlots.erase(slot);
1239 mCore->mFreeBuffers.push_front(slot);
1240
Antoine Labour78014f32014-07-15 21:17:03 -07001241 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1242 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001243
Antoine Labour78014f32014-07-15 21:17:03 -07001244 mCore->mIsAllocating = false;
1245 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001246 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001247 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001248 }
1249}
1250
Dan Stoza9de72932015-04-16 17:28:43 -07001251status_t BufferQueueProducer::allowAllocation(bool allow) {
1252 ATRACE_CALL();
1253 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1254
1255 Mutex::Autolock lock(mCore->mMutex);
1256 mCore->mAllowAllocation = allow;
1257 return NO_ERROR;
1258}
1259
Dan Stoza812ed062015-06-02 15:45:22 -07001260status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1261 ATRACE_CALL();
1262 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1263
1264 Mutex::Autolock lock(mCore->mMutex);
1265 mCore->mGenerationNumber = generationNumber;
1266 return NO_ERROR;
1267}
1268
Dan Stozac6f30bd2015-06-08 09:32:50 -07001269String8 BufferQueueProducer::getConsumerName() const {
1270 ATRACE_CALL();
1271 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1272 return mConsumerName;
1273}
1274
Dan Stoza7dde5992015-05-22 09:51:44 -07001275uint64_t BufferQueueProducer::getNextFrameNumber() const {
1276 ATRACE_CALL();
1277
1278 Mutex::Autolock lock(mCore->mMutex);
1279 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1280 return nextFrameNumber;
1281}
1282
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001283status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1284 ATRACE_CALL();
1285 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1286
1287 Mutex::Autolock lock(mCore->mMutex);
1288 if (!singleBufferMode) {
1289 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1290 }
1291 mCore->mSingleBufferMode = singleBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001292 return NO_ERROR;
1293}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001294
Dan Stoza127fc632015-06-30 13:43:32 -07001295status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1296 ATRACE_CALL();
1297 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1298
1299 Mutex::Autolock lock(mCore->mMutex);
1300 mDequeueTimeout = timeout;
1301 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001302 return NO_ERROR;
1303}
1304
Dan Stoza289ade12014-02-28 11:17:17 -08001305void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1306 // If we're here, it means that a producer we were connected to died.
1307 // We're guaranteed that we are still connected to it because we remove
1308 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1309 // without synchronization here.
1310 int api = mCore->mConnectedApi;
1311 disconnect(api);
1312}
1313
1314} // namespace android