blob: 268c9da0de0130d7477902e356faaa2d8ef47444 [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),
46 mCallbackCondition() {}
Dan Stoza289ade12014-02-28 11:17:17 -080047
48BufferQueueProducer::~BufferQueueProducer() {}
49
50status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
51 ATRACE_CALL();
52 BQ_LOGV("requestBuffer: slot %d", slot);
53 Mutex::Autolock lock(mCore->mMutex);
54
55 if (mCore->mIsAbandoned) {
56 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
57 return NO_INIT;
58 }
59
Pablo Ceballos583b1b32015-09-03 18:23:52 -070060 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
61 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
62 return NO_INIT;
63 }
64
Dan Stoza3e96f192014-03-03 10:16:19 -080065 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080066 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080067 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080068 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070069 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080070 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070071 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080072 return BAD_VALUE;
73 }
74
75 mSlots[slot].mRequestBufferCalled = true;
76 *buf = mSlots[slot].mGraphicBuffer;
77 return NO_ERROR;
78}
79
Pablo Ceballosfa455352015-08-12 17:47:47 -070080status_t BufferQueueProducer::setMaxDequeuedBufferCount(
81 int maxDequeuedBuffers) {
82 ATRACE_CALL();
83 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
84 maxDequeuedBuffers);
85
86 sp<IConsumerListener> listener;
87 { // Autolock scope
88 Mutex::Autolock lock(mCore->mMutex);
89 mCore->waitWhileAllocatingLocked();
90
91 if (mCore->mIsAbandoned) {
92 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
93 "abandoned");
94 return NO_INIT;
95 }
96
97 // There must be no dequeued buffers when changing the buffer count.
98 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070099 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700100 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
101 return BAD_VALUE;
102 }
103 }
104
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700105 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700106 bufferCount += maxDequeuedBuffers;
107
108 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
109 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
110 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
111 return BAD_VALUE;
112 }
113
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700114 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700115 if (bufferCount < minBufferSlots) {
116 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
117 "less than minimum %d", bufferCount, minBufferSlots);
118 return BAD_VALUE;
119 }
120
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700121 if (bufferCount > mCore->mMaxBufferCount) {
122 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700123 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
124 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
125 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
126 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700127 return BAD_VALUE;
128 }
129
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130 // Here we are guaranteed that the producer doesn't have any dequeued
131 // buffers and will release all of its buffer references. We don't
132 // clear the queue, however, so that currently queued buffers still
133 // get displayed.
134 mCore->freeAllBuffersLocked();
135 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700136 mCore->mDequeueCondition.broadcast();
137 listener = mCore->mConsumerListener;
138 } // Autolock scope
139
140 // Call back without lock held
141 if (listener != NULL) {
142 listener->onBuffersReleased();
143 }
144
145 return NO_ERROR;
146}
147
148status_t BufferQueueProducer::setAsyncMode(bool async) {
149 ATRACE_CALL();
150 BQ_LOGV("setAsyncMode: async = %d", async);
151
152 sp<IConsumerListener> listener;
153 { // Autolock scope
154 Mutex::Autolock lock(mCore->mMutex);
155 mCore->waitWhileAllocatingLocked();
156
157 if (mCore->mIsAbandoned) {
158 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
159 return NO_INIT;
160 }
161
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700162 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700163 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
164 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700165 BQ_LOGE("setAsyncMode(%d): this call would cause the "
166 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700167 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
168 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
169 mCore->mMaxDequeuedBufferCount,
170 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700171 return BAD_VALUE;
172 }
173
Pablo Ceballosfa455352015-08-12 17:47:47 -0700174 mCore->mAsyncMode = async;
175 mCore->mDequeueCondition.broadcast();
176 listener = mCore->mConsumerListener;
177 } // Autolock scope
178
179 // Call back without lock held
180 if (listener != NULL) {
181 listener->onBuffersReleased();
182 }
183 return NO_ERROR;
184}
185
Dan Stoza9f3053d2014-03-06 15:14:33 -0800186status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700187 int* found, status_t* returnFlags) const {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800188 bool tryAgain = true;
189 while (tryAgain) {
190 if (mCore->mIsAbandoned) {
191 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
192 return NO_INIT;
193 }
194
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700195 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800196
197 // Free up any buffers that are in slots beyond the max buffer count
198 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700199 assert(mSlots[s].mBufferState.isFree());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800200 if (mSlots[s].mGraphicBuffer != NULL) {
201 mCore->freeBufferLocked(s);
202 *returnFlags |= RELEASE_ALL_BUFFERS;
203 }
204 }
205
Dan Stoza9f3053d2014-03-06 15:14:33 -0800206 int dequeuedCount = 0;
207 int acquiredCount = 0;
208 for (int s = 0; s < maxBufferCount; ++s) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700209 if (mSlots[s].mBufferState.isDequeued()) {
210 ++dequeuedCount;
211 }
212 if (mSlots[s].mBufferState.isAcquired()) {
213 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800214 }
215 }
216
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700217 // Producers are not allowed to dequeue more than
218 // mMaxDequeuedBufferCount buffers.
219 // This check is only done if a buffer has already been queued
220 if (mCore->mBufferHasBeenQueued &&
221 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
222 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
223 "(%d)", caller, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800224 return INVALID_OPERATION;
225 }
226
Dan Stoza0de7ea72015-04-23 13:20:51 -0700227 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
228
Dan Stozaae3c3682014-04-18 15:43:35 -0700229 // If we disconnect and reconnect quickly, we can be in a state where
230 // our slots are empty but we have many buffers in the queue. This can
231 // cause us to run out of memory if we outrun the consumer. Wait here if
232 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700233 bool tooManyBuffers = mCore->mQueue.size()
234 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700235 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700236 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700237 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700238 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700239 // If in single buffer mode and a shared buffer exists, always
240 // return it.
241 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
242 BufferQueueCore::INVALID_BUFFER_SLOT) {
243 *found = mCore->mSingleBufferSlot;
244 } else if (!mCore->mFreeBuffers.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700245 auto slot = mCore->mFreeBuffers.begin();
246 *found = *slot;
247 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700248 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700249 auto slot = mCore->mFreeSlots.begin();
250 // Only return free slots up to the max buffer count
251 if (*slot < maxBufferCount) {
252 *found = *slot;
253 mCore->mFreeSlots.erase(slot);
254 }
255 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700256 }
257
258 // If no buffer is found, or if the queue has too many buffers
259 // outstanding, wait for a buffer to be acquired or released, or for the
260 // max buffer count to change.
261 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
262 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800263 if (tryAgain) {
264 // Return an error if we're in non-blocking mode (producer and
265 // consumer are controlled by the application).
266 // However, the consumer is allowed to briefly acquire an extra
267 // buffer (which could cause us to have to wait here), which is
268 // okay, since it is only used to implement an atomic acquire +
269 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700270 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800271 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
272 return WOULD_BLOCK;
273 }
274 mCore->mDequeueCondition.wait(mCore->mMutex);
275 }
276 } // while (tryAgain)
277
278 return NO_ERROR;
279}
280
Dan Stoza289ade12014-02-28 11:17:17 -0800281status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700282 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
283 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800284 ATRACE_CALL();
285 { // Autolock scope
286 Mutex::Autolock lock(mCore->mMutex);
287 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700288
289 if (mCore->mIsAbandoned) {
290 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
291 return NO_INIT;
292 }
293
294 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
295 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
296 return NO_INIT;
297 }
Dan Stoza289ade12014-02-28 11:17:17 -0800298 } // Autolock scope
299
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700300 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
301 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800302
303 if ((width && !height) || (!width && height)) {
304 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
305 return BAD_VALUE;
306 }
307
308 status_t returnFlags = NO_ERROR;
309 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
310 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800311 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800312
313 { // Autolock scope
314 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700315 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800316
317 if (format == 0) {
318 format = mCore->mDefaultBufferFormat;
319 }
320
321 // Enable the usage bits the consumer requested
322 usage |= mCore->mConsumerUsageBits;
323
Dan Stoza9de72932015-04-16 17:28:43 -0700324 const bool useDefaultSize = !width && !height;
325 if (useDefaultSize) {
326 width = mCore->mDefaultWidth;
327 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800328 }
Dan Stoza289ade12014-02-28 11:17:17 -0800329
Dan Stoza9de72932015-04-16 17:28:43 -0700330 int found = BufferItem::INVALID_BUFFER_SLOT;
331 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700332 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", &found,
333 &returnFlags);
Dan Stoza9de72932015-04-16 17:28:43 -0700334 if (status != NO_ERROR) {
335 return status;
336 }
337
338 // This should not happen
339 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
340 BQ_LOGE("dequeueBuffer: no available buffer slots");
341 return -EBUSY;
342 }
343
344 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
345
346 // If we are not allowed to allocate new buffers,
347 // waitForFreeSlotThenRelock must have returned a slot containing a
348 // buffer. If this buffer would require reallocation to meet the
349 // requested attributes, we free it and attempt to get another one.
350 if (!mCore->mAllowAllocation) {
351 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700352 if (mCore->mSingleBufferMode &&
353 mCore->mSingleBufferSlot == found) {
354 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
355 "buffer");
356 return BAD_VALUE;
357 }
358
Dan Stoza9de72932015-04-16 17:28:43 -0700359 mCore->freeBufferLocked(found);
360 found = BufferItem::INVALID_BUFFER_SLOT;
361 continue;
362 }
363 }
Dan Stoza289ade12014-02-28 11:17:17 -0800364 }
365
366 *outSlot = found;
367 ATRACE_BUFFER_INDEX(found);
368
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 attachedByConsumer = mSlots[found].mAttachedByConsumer;
370
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700371 mSlots[found].mBufferState.dequeue();
372
373 // If single buffer mode has just been enabled, cache the slot of the
374 // first buffer that is dequeued and mark it as the shared buffer.
375 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
376 BufferQueueCore::INVALID_BUFFER_SLOT) {
377 mCore->mSingleBufferSlot = found;
378 mSlots[found].mBufferState.mShared = true;
379 }
Dan Stoza289ade12014-02-28 11:17:17 -0800380
381 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
382 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700383 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800384 {
385 mSlots[found].mAcquireCalled = false;
386 mSlots[found].mGraphicBuffer = NULL;
387 mSlots[found].mRequestBufferCalled = false;
388 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
389 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
390 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800391 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700392 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800393
394 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800395 } else {
396 // We add 1 because that will be the frame number when this buffer
397 // is queued
398 mCore->mBufferAge =
399 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800400 }
401
Dan Stoza800b41a2015-04-28 14:20:04 -0700402 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
403 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800404
Dan Stoza289ade12014-02-28 11:17:17 -0800405 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
406 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
407 "slot=%d w=%d h=%d format=%u",
408 found, buffer->width, buffer->height, buffer->format);
409 }
410
411 eglDisplay = mSlots[found].mEglDisplay;
412 eglFence = mSlots[found].mEglFence;
413 *outFence = mSlots[found].mFence;
414 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
415 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700416
417 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800418 } // Autolock scope
419
420 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
421 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700422 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800423 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800424 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800425 { // Autolock scope
426 Mutex::Autolock lock(mCore->mMutex);
427
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700428 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
429 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
430 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
431 }
432
433 mCore->mIsAllocating = false;
434 mCore->mIsAllocatingCondition.broadcast();
435
436 if (graphicBuffer == NULL) {
437 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
438 return error;
439 }
440
Dan Stoza289ade12014-02-28 11:17:17 -0800441 if (mCore->mIsAbandoned) {
442 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
443 return NO_INIT;
444 }
Dan Stoza289ade12014-02-28 11:17:17 -0800445 } // Autolock scope
446 }
447
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448 if (attachedByConsumer) {
449 returnFlags |= BUFFER_NEEDS_REALLOCATION;
450 }
451
Dan Stoza289ade12014-02-28 11:17:17 -0800452 if (eglFence != EGL_NO_SYNC_KHR) {
453 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
454 1000000000);
455 // If something goes wrong, log the error, but return the buffer without
456 // synchronizing access to it. It's too late at this point to abort the
457 // dequeue operation.
458 if (result == EGL_FALSE) {
459 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
460 eglGetError());
461 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
462 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
463 }
464 eglDestroySyncKHR(eglDisplay, eglFence);
465 }
466
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700467 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
468 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800469 mSlots[*outSlot].mFrameNumber,
470 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
471
472 return returnFlags;
473}
474
Dan Stoza9f3053d2014-03-06 15:14:33 -0800475status_t BufferQueueProducer::detachBuffer(int slot) {
476 ATRACE_CALL();
477 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700478 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800479 Mutex::Autolock lock(mCore->mMutex);
480
481 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700482 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800483 return NO_INIT;
484 }
485
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700486 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700487 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700488 return NO_INIT;
489 }
490
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700491 if (mCore->mSingleBufferMode) {
492 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer"
493 "mode");
494 return BAD_VALUE;
495 }
496
Dan Stoza9f3053d2014-03-06 15:14:33 -0800497 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700498 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800499 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
500 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700501 } else if (!mSlots[slot].mBufferState.isDequeued()) {
502 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
503 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800504 return BAD_VALUE;
505 } else if (!mSlots[slot].mRequestBufferCalled) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700506 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800507 slot);
508 return BAD_VALUE;
509 }
510
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700511 mSlots[slot].mBufferState.detachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800512 mCore->freeBufferLocked(slot);
513 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700514 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800515
516 return NO_ERROR;
517}
518
Dan Stozad9822a32014-03-28 15:25:31 -0700519status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
520 sp<Fence>* outFence) {
521 ATRACE_CALL();
522
523 if (outBuffer == NULL) {
524 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
525 return BAD_VALUE;
526 } else if (outFence == NULL) {
527 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
528 return BAD_VALUE;
529 }
530
531 Mutex::Autolock lock(mCore->mMutex);
532
533 if (mCore->mIsAbandoned) {
534 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
535 return NO_INIT;
536 }
537
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700538 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
539 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
540 return NO_INIT;
541 }
542
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700543 if (mCore->mSingleBufferMode) {
544 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
545 "mode");
546 return BAD_VALUE;
547 }
548
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700549 mCore->waitWhileAllocatingLocked();
550
Dan Stoza0de7ea72015-04-23 13:20:51 -0700551 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000552 return NO_MEMORY;
553 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700554
Dan Stoza0de7ea72015-04-23 13:20:51 -0700555 int found = mCore->mFreeBuffers.front();
556 mCore->mFreeBuffers.remove(found);
557
Dan Stozad9822a32014-03-28 15:25:31 -0700558 BQ_LOGV("detachNextBuffer detached slot %d", found);
559
560 *outBuffer = mSlots[found].mGraphicBuffer;
561 *outFence = mSlots[found].mFence;
562 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700563 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700564
565 return NO_ERROR;
566}
567
Dan Stoza9f3053d2014-03-06 15:14:33 -0800568status_t BufferQueueProducer::attachBuffer(int* outSlot,
569 const sp<android::GraphicBuffer>& buffer) {
570 ATRACE_CALL();
571
572 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700573 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800574 return BAD_VALUE;
575 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700576 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800577 return BAD_VALUE;
578 }
579
580 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700581
582 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700583 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700584 return NO_INIT;
585 }
586
587 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700589 return NO_INIT;
590 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800591
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700592 if (mCore->mSingleBufferMode) {
593 BQ_LOGE("attachBuffer: cannot atach a buffer in single buffer mode");
594 return BAD_VALUE;
595 }
596
Dan Stoza812ed062015-06-02 15:45:22 -0700597 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
598 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
599 "[queue %u]", buffer->getGenerationNumber(),
600 mCore->mGenerationNumber);
601 return BAD_VALUE;
602 }
603
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700604 mCore->waitWhileAllocatingLocked();
605
Dan Stoza9f3053d2014-03-06 15:14:33 -0800606 status_t returnFlags = NO_ERROR;
607 int found;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700608 status_t status = waitForFreeSlotThenRelock("attachBuffer", &found,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700609 &returnFlags);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800610 if (status != NO_ERROR) {
611 return status;
612 }
613
614 // This should not happen
615 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700616 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800617 return -EBUSY;
618 }
619
620 *outSlot = found;
621 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700622 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800623 *outSlot, returnFlags);
624
625 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700626 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800627 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
628 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700629 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630
Dan Stoza0de7ea72015-04-23 13:20:51 -0700631 mCore->validateConsistencyLocked();
632
Dan Stoza9f3053d2014-03-06 15:14:33 -0800633 return returnFlags;
634}
635
Dan Stoza289ade12014-02-28 11:17:17 -0800636status_t BufferQueueProducer::queueBuffer(int slot,
637 const QueueBufferInput &input, QueueBufferOutput *output) {
638 ATRACE_CALL();
639 ATRACE_BUFFER_INDEX(slot);
640
641 int64_t timestamp;
642 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800643 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700644 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800645 int scalingMode;
646 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700647 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800648 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800649 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700650 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700651 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800652
653 if (fence == NULL) {
654 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800655 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800656 }
657
658 switch (scalingMode) {
659 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
660 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
661 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
662 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
663 break;
664 default:
665 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800666 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800667 }
668
Dan Stoza8dc55392014-11-04 11:37:46 -0800669 sp<IConsumerListener> frameAvailableListener;
670 sp<IConsumerListener> frameReplacedListener;
671 int callbackTicket = 0;
672 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800673 { // Autolock scope
674 Mutex::Autolock lock(mCore->mMutex);
675
676 if (mCore->mIsAbandoned) {
677 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
678 return NO_INIT;
679 }
680
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700681 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
682 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
683 return NO_INIT;
684 }
685
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700686 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800687
688 if (slot < 0 || slot >= maxBufferCount) {
689 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
690 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800691 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700692 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800693 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700694 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800695 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800696 } else if (!mSlots[slot].mRequestBufferCalled) {
697 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
698 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800699 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800700 }
701
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800702 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700703 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800704 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800705 crop.left, crop.top, crop.right, crop.bottom, transform,
706 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800707
708 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
709 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700710 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800711 crop.intersect(bufferRect, &croppedRect);
712 if (croppedRect != crop) {
713 BQ_LOGE("queueBuffer: crop rect is not contained within the "
714 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800715 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800716 }
717
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800718 // Override UNKNOWN dataspace with consumer default
719 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
720 dataSpace = mCore->mDefaultBufferDataSpace;
721 }
722
Dan Stoza289ade12014-02-28 11:17:17 -0800723 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700724 mSlots[slot].mBufferState.queue();
725
Dan Stoza289ade12014-02-28 11:17:17 -0800726 ++mCore->mFrameCounter;
727 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
728
Dan Stoza289ade12014-02-28 11:17:17 -0800729 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
730 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
731 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800732 item.mTransform = transform &
733 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800734 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800735 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
736 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800737 item.mTimestamp = timestamp;
738 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800739 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800740 item.mFrameNumber = mCore->mFrameCounter;
741 item.mSlot = slot;
742 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700743 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700744 mCore->mDequeueBufferCannotBlock ||
745 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700746 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800747
Ruben Brunk1681d952014-06-27 15:51:55 -0700748 mStickyTransform = stickyTransform;
749
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700750 // Cache the shared buffer data so that the BufferItem can be recreated.
751 if (mCore->mSingleBufferMode) {
752 mCore->mSingleBufferCache.crop = crop;
753 mCore->mSingleBufferCache.transform = transform;
754 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
755 scalingMode);
756 mCore->mSingleBufferCache.dataspace = dataSpace;
757 }
758
Dan Stoza289ade12014-02-28 11:17:17 -0800759 if (mCore->mQueue.empty()) {
760 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
761 // and simply queue this buffer
762 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800763 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800764 } else {
765 // When the queue is not empty, we need to look at the front buffer
766 // state to see if we need to replace it
767 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
768 if (front->mIsDroppable) {
769 // If the front queued buffer is still being tracked, we first
770 // mark it as freed
771 if (mCore->stillTracking(front)) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700772 mSlots[front->mSlot].mBufferState.freeQueued();
773
774 // After leaving single buffer mode, the shared buffer will
775 // still be around. Mark it as no longer shared if this
776 // operation causes it to be free.
777 if (!mCore->mSingleBufferMode &&
778 mSlots[front->mSlot].mBufferState.isFree()) {
779 mSlots[front->mSlot].mBufferState.mShared = false;
780 }
781 // Don't put the shared buffer on the free list.
782 if (!mSlots[front->mSlot].mBufferState.isShared()) {
783 mCore->mFreeBuffers.push_front(front->mSlot);
784 }
Dan Stoza289ade12014-02-28 11:17:17 -0800785 }
786 // Overwrite the droppable buffer with the incoming one
787 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800788 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800789 } else {
790 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800791 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800792 }
793 }
794
795 mCore->mBufferHasBeenQueued = true;
796 mCore->mDequeueCondition.broadcast();
797
798 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800799 mCore->mTransformHint,
800 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800801
802 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800803
804 // Take a ticket for the callback functions
805 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700806
807 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800808 } // Autolock scope
809
Eric Penner99a0afb2014-09-30 11:28:30 -0700810 // Wait without lock held
811 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
812 // Waiting here allows for two full buffers to be queued but not a
813 // third. In the event that frames take varying time, this makes a
814 // small trade-off in favor of latency rather than throughput.
815 mLastQueueBufferFence->waitForever("Throttling EGL Production");
816 mLastQueueBufferFence = fence;
817 }
818
Dan Stoza8dc55392014-11-04 11:37:46 -0800819 // Don't send the GraphicBuffer through the callback, and don't send
820 // the slot number, since the consumer shouldn't need it
821 item.mGraphicBuffer.clear();
822 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
823
824 // Call back without the main BufferQueue lock held, but with the callback
825 // lock held so we can ensure that callbacks occur in order
826 {
827 Mutex::Autolock lock(mCallbackMutex);
828 while (callbackTicket != mCurrentCallbackTicket) {
829 mCallbackCondition.wait(mCallbackMutex);
830 }
831
832 if (frameAvailableListener != NULL) {
833 frameAvailableListener->onFrameAvailable(item);
834 } else if (frameReplacedListener != NULL) {
835 frameReplacedListener->onFrameReplaced(item);
836 }
837
838 ++mCurrentCallbackTicket;
839 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800840 }
841
842 return NO_ERROR;
843}
844
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700845status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800846 ATRACE_CALL();
847 BQ_LOGV("cancelBuffer: slot %d", slot);
848 Mutex::Autolock lock(mCore->mMutex);
849
850 if (mCore->mIsAbandoned) {
851 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700852 return NO_INIT;
853 }
854
855 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
856 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
857 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800858 }
859
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700860 if (mCore->mSingleBufferMode) {
861 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
862 return BAD_VALUE;
863 }
864
Dan Stoza3e96f192014-03-03 10:16:19 -0800865 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800866 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800867 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700868 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700869 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800870 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700872 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800873 } else if (fence == NULL) {
874 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700875 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800876 }
877
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 mSlots[slot].mBufferState.cancel();
879
880 // After leaving single buffer mode, the shared buffer will still be around.
881 // Mark it as no longer shared if this operation causes it to be free.
882 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
883 mSlots[slot].mBufferState.mShared = false;
884 }
885
886 // Don't put the shared buffer on the free list.
887 if (!mSlots[slot].mBufferState.isShared()) {
888 mCore->mFreeBuffers.push_front(slot);
889 }
Dan Stoza289ade12014-02-28 11:17:17 -0800890 mSlots[slot].mFence = fence;
891 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700892 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700893
894 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800895}
896
897int BufferQueueProducer::query(int what, int *outValue) {
898 ATRACE_CALL();
899 Mutex::Autolock lock(mCore->mMutex);
900
901 if (outValue == NULL) {
902 BQ_LOGE("query: outValue was NULL");
903 return BAD_VALUE;
904 }
905
906 if (mCore->mIsAbandoned) {
907 BQ_LOGE("query: BufferQueue has been abandoned");
908 return NO_INIT;
909 }
910
911 int value;
912 switch (what) {
913 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800914 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800915 break;
916 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800917 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800918 break;
919 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800920 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800921 break;
922 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700923 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800924 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700925 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800926 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700927 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800928 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
929 value = (mCore->mQueue.size() > 1);
930 break;
931 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800932 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800933 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800934 case NATIVE_WINDOW_DEFAULT_DATASPACE:
935 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
936 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800937 case NATIVE_WINDOW_BUFFER_AGE:
938 if (mCore->mBufferAge > INT32_MAX) {
939 value = 0;
940 } else {
941 value = static_cast<int32_t>(mCore->mBufferAge);
942 }
943 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800944 default:
945 return BAD_VALUE;
946 }
947
948 BQ_LOGV("query: %d? %d", what, value);
949 *outValue = value;
950 return NO_ERROR;
951}
952
Dan Stozaf0eaf252014-03-21 13:05:51 -0700953status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800954 int api, bool producerControlledByApp, QueueBufferOutput *output) {
955 ATRACE_CALL();
956 Mutex::Autolock lock(mCore->mMutex);
957 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700958 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -0800959 producerControlledByApp ? "true" : "false");
960
Dan Stozaae3c3682014-04-18 15:43:35 -0700961 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700962 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -0700963 return NO_INIT;
964 }
Dan Stoza289ade12014-02-28 11:17:17 -0800965
Dan Stozaae3c3682014-04-18 15:43:35 -0700966 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700967 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -0700968 return NO_INIT;
969 }
Dan Stoza289ade12014-02-28 11:17:17 -0800970
Dan Stozaae3c3682014-04-18 15:43:35 -0700971 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700972 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -0700973 return BAD_VALUE;
974 }
Dan Stoza289ade12014-02-28 11:17:17 -0800975
Dan Stozaae3c3682014-04-18 15:43:35 -0700976 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700977 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -0700978 mCore->mConnectedApi, api);
979 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800980 }
981
982 int status = NO_ERROR;
983 switch (api) {
984 case NATIVE_WINDOW_API_EGL:
985 case NATIVE_WINDOW_API_CPU:
986 case NATIVE_WINDOW_API_MEDIA:
987 case NATIVE_WINDOW_API_CAMERA:
988 mCore->mConnectedApi = api;
989 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800990 mCore->mTransformHint,
991 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800992
993 // Set up a death notification so that we can disconnect
994 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700995 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800996 IInterface::asBinder(listener)->remoteBinder() != NULL) {
997 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800998 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700999 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001000 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001001 strerror(-status), status);
1002 }
1003 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001004 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001005 break;
1006 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001008 status = BAD_VALUE;
1009 break;
1010 }
1011
1012 mCore->mBufferHasBeenQueued = false;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001013 mCore->mDequeueBufferCannotBlock = mCore->mConsumerControlledByApp &&
1014 producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -07001015 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -08001016
1017 return status;
1018}
1019
1020status_t BufferQueueProducer::disconnect(int api) {
1021 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001022 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001023
1024 int status = NO_ERROR;
1025 sp<IConsumerListener> listener;
1026 { // Autolock scope
1027 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001028 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001029
1030 if (mCore->mIsAbandoned) {
1031 // It's not really an error to disconnect after the surface has
1032 // been abandoned; it should just be a no-op.
1033 return NO_ERROR;
1034 }
1035
1036 switch (api) {
1037 case NATIVE_WINDOW_API_EGL:
1038 case NATIVE_WINDOW_API_CPU:
1039 case NATIVE_WINDOW_API_MEDIA:
1040 case NATIVE_WINDOW_API_CAMERA:
1041 if (mCore->mConnectedApi == api) {
1042 mCore->freeAllBuffersLocked();
1043
1044 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001045 if (mCore->mConnectedProducerListener != NULL) {
1046 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001047 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001048 // This can fail if we're here because of the death
1049 // notification, but we just ignore it
1050 token->unlinkToDeath(
1051 static_cast<IBinder::DeathRecipient*>(this));
1052 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001053 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001054 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001055 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001056 mCore->mDequeueCondition.broadcast();
1057 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001058 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001059 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001060 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001061 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001062 }
1063 break;
1064 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001065 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001066 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001067 break;
1068 }
1069 } // Autolock scope
1070
1071 // Call back without lock held
1072 if (listener != NULL) {
1073 listener->onBuffersReleased();
1074 }
1075
1076 return status;
1077}
1078
Jesse Hall399184a2014-03-03 15:42:54 -08001079status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001080 sp<IConsumerListener> listener;
1081 { // Autolock scope
1082 Mutex::Autolock _l(mCore->mMutex);
1083 mCore->mSidebandStream = stream;
1084 listener = mCore->mConsumerListener;
1085 } // Autolock scope
1086
1087 if (listener != NULL) {
1088 listener->onSidebandStreamChanged();
1089 }
Jesse Hall399184a2014-03-03 15:42:54 -08001090 return NO_ERROR;
1091}
1092
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001093void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1094 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001095 ATRACE_CALL();
1096 while (true) {
1097 Vector<int> freeSlots;
1098 size_t newBufferCount = 0;
1099 uint32_t allocWidth = 0;
1100 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001101 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001102 uint32_t allocUsage = 0;
1103 { // Autolock scope
1104 Mutex::Autolock lock(mCore->mMutex);
1105 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001106
Dan Stoza9de72932015-04-16 17:28:43 -07001107 if (!mCore->mAllowAllocation) {
1108 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1109 "BufferQueue");
1110 return;
1111 }
1112
Antoine Labour78014f32014-07-15 21:17:03 -07001113 int currentBufferCount = 0;
1114 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1115 if (mSlots[slot].mGraphicBuffer != NULL) {
1116 ++currentBufferCount;
1117 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001118 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001119 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1120 slot);
1121 continue;
1122 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001123
Antoine Labour11f14872014-07-25 18:14:42 -07001124 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001125 }
1126 }
1127
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001128 int maxBufferCount = mCore->getMaxBufferCountLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001129 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1130 currentBufferCount, maxBufferCount);
1131 if (maxBufferCount <= currentBufferCount)
1132 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001133 newBufferCount =
1134 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001135 if (freeSlots.size() < newBufferCount) {
1136 BQ_LOGE("allocateBuffers: ran out of free slots");
1137 return;
1138 }
1139 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1140 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1141 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1142 allocUsage = usage | mCore->mConsumerUsageBits;
1143
1144 mCore->mIsAllocating = true;
1145 } // Autolock scope
1146
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001147 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001148 for (size_t i = 0; i < newBufferCount; ++i) {
1149 status_t result = NO_ERROR;
1150 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1151 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1152 if (result != NO_ERROR) {
1153 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1154 " %u, usage %u)", width, height, format, usage);
1155 Mutex::Autolock lock(mCore->mMutex);
1156 mCore->mIsAllocating = false;
1157 mCore->mIsAllocatingCondition.broadcast();
1158 return;
1159 }
1160 buffers.push_back(graphicBuffer);
1161 }
1162
1163 { // Autolock scope
1164 Mutex::Autolock lock(mCore->mMutex);
1165 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1166 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001167 PixelFormat checkFormat = format != 0 ?
1168 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001169 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1170 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1171 checkFormat != allocFormat || checkUsage != allocUsage) {
1172 // Something changed while we released the lock. Retry.
1173 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1174 mCore->mIsAllocating = false;
1175 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001176 continue;
1177 }
1178
Antoine Labour78014f32014-07-15 21:17:03 -07001179 for (size_t i = 0; i < newBufferCount; ++i) {
1180 int slot = freeSlots[i];
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001181 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001182 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1183 // allocated.
1184 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1185 "Dropping allocated buffer.", slot);
1186 continue;
1187 }
1188 mCore->freeBufferLocked(slot); // Clean up the slot first
1189 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001190 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001191
1192 // freeBufferLocked puts this slot on the free slots list. Since
1193 // we then attached a buffer, move the slot to free buffer list.
1194 mCore->mFreeSlots.erase(slot);
1195 mCore->mFreeBuffers.push_front(slot);
1196
Antoine Labour78014f32014-07-15 21:17:03 -07001197 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1198 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001199
Antoine Labour78014f32014-07-15 21:17:03 -07001200 mCore->mIsAllocating = false;
1201 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001202 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001203 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001204 }
1205}
1206
Dan Stoza9de72932015-04-16 17:28:43 -07001207status_t BufferQueueProducer::allowAllocation(bool allow) {
1208 ATRACE_CALL();
1209 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1210
1211 Mutex::Autolock lock(mCore->mMutex);
1212 mCore->mAllowAllocation = allow;
1213 return NO_ERROR;
1214}
1215
Dan Stoza812ed062015-06-02 15:45:22 -07001216status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1217 ATRACE_CALL();
1218 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1219
1220 Mutex::Autolock lock(mCore->mMutex);
1221 mCore->mGenerationNumber = generationNumber;
1222 return NO_ERROR;
1223}
1224
Dan Stozac6f30bd2015-06-08 09:32:50 -07001225String8 BufferQueueProducer::getConsumerName() const {
1226 ATRACE_CALL();
1227 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1228 return mConsumerName;
1229}
1230
Dan Stoza7dde5992015-05-22 09:51:44 -07001231uint64_t BufferQueueProducer::getNextFrameNumber() const {
1232 ATRACE_CALL();
1233
1234 Mutex::Autolock lock(mCore->mMutex);
1235 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1236 return nextFrameNumber;
1237}
1238
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001239status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1240 ATRACE_CALL();
1241 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1242
1243 Mutex::Autolock lock(mCore->mMutex);
1244 if (!singleBufferMode) {
1245 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1246 }
1247 mCore->mSingleBufferMode = singleBufferMode;
1248
1249 return NO_ERROR;
1250}
1251
Dan Stoza289ade12014-02-28 11:17:17 -08001252void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1253 // If we're here, it means that a producer we were connected to died.
1254 // We're guaranteed that we are still connected to it because we remove
1255 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1256 // without synchronization here.
1257 int api = mCore->mConnectedApi;
1258 disconnect(api);
1259}
1260
1261} // namespace android