blob: 9271ed8bcd5c2fb7589fda53df0b27f44ec1dccd [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;
Pablo Ceballos06312182015-10-07 16:32:12 -0700747 item.mSingleBufferMode = mCore->mSingleBufferMode;
748 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800749
Ruben Brunk1681d952014-06-27 15:51:55 -0700750 mStickyTransform = stickyTransform;
751
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700752 // Cache the shared buffer data so that the BufferItem can be recreated.
753 if (mCore->mSingleBufferMode) {
754 mCore->mSingleBufferCache.crop = crop;
755 mCore->mSingleBufferCache.transform = transform;
756 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
757 scalingMode);
758 mCore->mSingleBufferCache.dataspace = dataSpace;
759 }
760
Dan Stoza289ade12014-02-28 11:17:17 -0800761 if (mCore->mQueue.empty()) {
762 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
763 // and simply queue this buffer
764 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800765 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800766 } else {
767 // When the queue is not empty, we need to look at the front buffer
768 // state to see if we need to replace it
769 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
770 if (front->mIsDroppable) {
771 // If the front queued buffer is still being tracked, we first
772 // mark it as freed
773 if (mCore->stillTracking(front)) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700774 mSlots[front->mSlot].mBufferState.freeQueued();
775
776 // After leaving single buffer mode, the shared buffer will
777 // still be around. Mark it as no longer shared if this
778 // operation causes it to be free.
779 if (!mCore->mSingleBufferMode &&
780 mSlots[front->mSlot].mBufferState.isFree()) {
781 mSlots[front->mSlot].mBufferState.mShared = false;
782 }
783 // Don't put the shared buffer on the free list.
784 if (!mSlots[front->mSlot].mBufferState.isShared()) {
785 mCore->mFreeBuffers.push_front(front->mSlot);
786 }
Dan Stoza289ade12014-02-28 11:17:17 -0800787 }
788 // Overwrite the droppable buffer with the incoming one
789 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800790 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800791 } else {
792 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800793 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800794 }
795 }
796
797 mCore->mBufferHasBeenQueued = true;
798 mCore->mDequeueCondition.broadcast();
799
800 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800801 mCore->mTransformHint,
802 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800803
804 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800805
806 // Take a ticket for the callback functions
807 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700808
809 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800810 } // Autolock scope
811
Dan Stoza8dc55392014-11-04 11:37:46 -0800812 // Don't send the GraphicBuffer through the callback, and don't send
813 // the slot number, since the consumer shouldn't need it
814 item.mGraphicBuffer.clear();
815 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
816
817 // Call back without the main BufferQueue lock held, but with the callback
818 // lock held so we can ensure that callbacks occur in order
819 {
820 Mutex::Autolock lock(mCallbackMutex);
821 while (callbackTicket != mCurrentCallbackTicket) {
822 mCallbackCondition.wait(mCallbackMutex);
823 }
824
825 if (frameAvailableListener != NULL) {
826 frameAvailableListener->onFrameAvailable(item);
827 } else if (frameReplacedListener != NULL) {
828 frameReplacedListener->onFrameReplaced(item);
829 }
830
831 ++mCurrentCallbackTicket;
832 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800833 }
834
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000835 // Wait without lock held
836 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
837 // Waiting here allows for two full buffers to be queued but not a
838 // third. In the event that frames take varying time, this makes a
839 // small trade-off in favor of latency rather than throughput.
840 mLastQueueBufferFence->waitForever("Throttling EGL Production");
841 mLastQueueBufferFence = fence;
842 }
843
Dan Stoza289ade12014-02-28 11:17:17 -0800844 return NO_ERROR;
845}
846
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700847status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800848 ATRACE_CALL();
849 BQ_LOGV("cancelBuffer: slot %d", slot);
850 Mutex::Autolock lock(mCore->mMutex);
851
852 if (mCore->mIsAbandoned) {
853 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700854 return NO_INIT;
855 }
856
857 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
858 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
859 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800860 }
861
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 if (mCore->mSingleBufferMode) {
863 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
864 return BAD_VALUE;
865 }
866
Dan Stoza3e96f192014-03-03 10:16:19 -0800867 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800868 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800869 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700870 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800872 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700873 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700874 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800875 } else if (fence == NULL) {
876 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700877 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800878 }
879
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700880 mSlots[slot].mBufferState.cancel();
881
882 // After leaving single buffer mode, the shared buffer will still be around.
883 // Mark it as no longer shared if this operation causes it to be free.
884 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
885 mSlots[slot].mBufferState.mShared = false;
886 }
887
888 // Don't put the shared buffer on the free list.
889 if (!mSlots[slot].mBufferState.isShared()) {
890 mCore->mFreeBuffers.push_front(slot);
891 }
Dan Stoza289ade12014-02-28 11:17:17 -0800892 mSlots[slot].mFence = fence;
893 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700894 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700895
896 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800897}
898
899int BufferQueueProducer::query(int what, int *outValue) {
900 ATRACE_CALL();
901 Mutex::Autolock lock(mCore->mMutex);
902
903 if (outValue == NULL) {
904 BQ_LOGE("query: outValue was NULL");
905 return BAD_VALUE;
906 }
907
908 if (mCore->mIsAbandoned) {
909 BQ_LOGE("query: BufferQueue has been abandoned");
910 return NO_INIT;
911 }
912
913 int value;
914 switch (what) {
915 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800916 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800917 break;
918 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800919 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800920 break;
921 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800922 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800923 break;
924 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700925 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800926 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700927 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800928 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700929 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
931 value = (mCore->mQueue.size() > 1);
932 break;
933 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800934 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800935 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800936 case NATIVE_WINDOW_DEFAULT_DATASPACE:
937 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
938 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800939 case NATIVE_WINDOW_BUFFER_AGE:
940 if (mCore->mBufferAge > INT32_MAX) {
941 value = 0;
942 } else {
943 value = static_cast<int32_t>(mCore->mBufferAge);
944 }
945 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800946 default:
947 return BAD_VALUE;
948 }
949
950 BQ_LOGV("query: %d? %d", what, value);
951 *outValue = value;
952 return NO_ERROR;
953}
954
Dan Stozaf0eaf252014-03-21 13:05:51 -0700955status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800956 int api, bool producerControlledByApp, QueueBufferOutput *output) {
957 ATRACE_CALL();
958 Mutex::Autolock lock(mCore->mMutex);
959 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700960 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -0800961 producerControlledByApp ? "true" : "false");
962
Dan Stozaae3c3682014-04-18 15:43:35 -0700963 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700964 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -0700965 return NO_INIT;
966 }
Dan Stoza289ade12014-02-28 11:17:17 -0800967
Dan Stozaae3c3682014-04-18 15:43:35 -0700968 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700969 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -0700970 return NO_INIT;
971 }
Dan Stoza289ade12014-02-28 11:17:17 -0800972
Dan Stozaae3c3682014-04-18 15:43:35 -0700973 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700974 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -0700975 return BAD_VALUE;
976 }
Dan Stoza289ade12014-02-28 11:17:17 -0800977
Dan Stozaae3c3682014-04-18 15:43:35 -0700978 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700979 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -0700980 mCore->mConnectedApi, api);
981 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800982 }
983
984 int status = NO_ERROR;
985 switch (api) {
986 case NATIVE_WINDOW_API_EGL:
987 case NATIVE_WINDOW_API_CPU:
988 case NATIVE_WINDOW_API_MEDIA:
989 case NATIVE_WINDOW_API_CAMERA:
990 mCore->mConnectedApi = api;
991 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800992 mCore->mTransformHint,
993 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800994
995 // Set up a death notification so that we can disconnect
996 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700997 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800998 IInterface::asBinder(listener)->remoteBinder() != NULL) {
999 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -08001000 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -07001001 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001002 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001003 strerror(-status), status);
1004 }
1005 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001006 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001007 break;
1008 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001009 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001010 status = BAD_VALUE;
1011 break;
1012 }
1013
1014 mCore->mBufferHasBeenQueued = false;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001015 mCore->mDequeueBufferCannotBlock = mCore->mConsumerControlledByApp &&
1016 producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -07001017 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -08001018
1019 return status;
1020}
1021
1022status_t BufferQueueProducer::disconnect(int api) {
1023 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001024 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001025
1026 int status = NO_ERROR;
1027 sp<IConsumerListener> listener;
1028 { // Autolock scope
1029 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001030 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001031
1032 if (mCore->mIsAbandoned) {
1033 // It's not really an error to disconnect after the surface has
1034 // been abandoned; it should just be a no-op.
1035 return NO_ERROR;
1036 }
1037
1038 switch (api) {
1039 case NATIVE_WINDOW_API_EGL:
1040 case NATIVE_WINDOW_API_CPU:
1041 case NATIVE_WINDOW_API_MEDIA:
1042 case NATIVE_WINDOW_API_CAMERA:
1043 if (mCore->mConnectedApi == api) {
1044 mCore->freeAllBuffersLocked();
1045
1046 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001047 if (mCore->mConnectedProducerListener != NULL) {
1048 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001049 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001050 // This can fail if we're here because of the death
1051 // notification, but we just ignore it
1052 token->unlinkToDeath(
1053 static_cast<IBinder::DeathRecipient*>(this));
1054 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001055 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001056 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001057 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001058 mCore->mDequeueCondition.broadcast();
1059 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001060 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001061 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001062 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001063 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001064 }
1065 break;
1066 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001067 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001068 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001069 break;
1070 }
1071 } // Autolock scope
1072
1073 // Call back without lock held
1074 if (listener != NULL) {
1075 listener->onBuffersReleased();
1076 }
1077
1078 return status;
1079}
1080
Jesse Hall399184a2014-03-03 15:42:54 -08001081status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001082 sp<IConsumerListener> listener;
1083 { // Autolock scope
1084 Mutex::Autolock _l(mCore->mMutex);
1085 mCore->mSidebandStream = stream;
1086 listener = mCore->mConsumerListener;
1087 } // Autolock scope
1088
1089 if (listener != NULL) {
1090 listener->onSidebandStreamChanged();
1091 }
Jesse Hall399184a2014-03-03 15:42:54 -08001092 return NO_ERROR;
1093}
1094
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001095void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1096 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001097 ATRACE_CALL();
1098 while (true) {
1099 Vector<int> freeSlots;
1100 size_t newBufferCount = 0;
1101 uint32_t allocWidth = 0;
1102 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001103 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001104 uint32_t allocUsage = 0;
1105 { // Autolock scope
1106 Mutex::Autolock lock(mCore->mMutex);
1107 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001108
Dan Stoza9de72932015-04-16 17:28:43 -07001109 if (!mCore->mAllowAllocation) {
1110 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1111 "BufferQueue");
1112 return;
1113 }
1114
Antoine Labour78014f32014-07-15 21:17:03 -07001115 int currentBufferCount = 0;
1116 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1117 if (mSlots[slot].mGraphicBuffer != NULL) {
1118 ++currentBufferCount;
1119 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001120 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001121 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1122 slot);
1123 continue;
1124 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001125
Antoine Labour11f14872014-07-25 18:14:42 -07001126 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001127 }
1128 }
1129
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001130 int maxBufferCount = mCore->getMaxBufferCountLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001131 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1132 currentBufferCount, maxBufferCount);
1133 if (maxBufferCount <= currentBufferCount)
1134 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001135 newBufferCount =
1136 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001137 if (freeSlots.size() < newBufferCount) {
1138 BQ_LOGE("allocateBuffers: ran out of free slots");
1139 return;
1140 }
1141 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1142 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1143 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1144 allocUsage = usage | mCore->mConsumerUsageBits;
1145
1146 mCore->mIsAllocating = true;
1147 } // Autolock scope
1148
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001149 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001150 for (size_t i = 0; i < newBufferCount; ++i) {
1151 status_t result = NO_ERROR;
1152 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1153 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1154 if (result != NO_ERROR) {
1155 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1156 " %u, usage %u)", width, height, format, usage);
1157 Mutex::Autolock lock(mCore->mMutex);
1158 mCore->mIsAllocating = false;
1159 mCore->mIsAllocatingCondition.broadcast();
1160 return;
1161 }
1162 buffers.push_back(graphicBuffer);
1163 }
1164
1165 { // Autolock scope
1166 Mutex::Autolock lock(mCore->mMutex);
1167 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1168 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001169 PixelFormat checkFormat = format != 0 ?
1170 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001171 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1172 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1173 checkFormat != allocFormat || checkUsage != allocUsage) {
1174 // Something changed while we released the lock. Retry.
1175 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1176 mCore->mIsAllocating = false;
1177 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001178 continue;
1179 }
1180
Antoine Labour78014f32014-07-15 21:17:03 -07001181 for (size_t i = 0; i < newBufferCount; ++i) {
1182 int slot = freeSlots[i];
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001183 if (!mSlots[slot].mBufferState.isFree()) {
Antoine Labour78014f32014-07-15 21:17:03 -07001184 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1185 // allocated.
1186 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1187 "Dropping allocated buffer.", slot);
1188 continue;
1189 }
1190 mCore->freeBufferLocked(slot); // Clean up the slot first
1191 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001192 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001193
1194 // freeBufferLocked puts this slot on the free slots list. Since
1195 // we then attached a buffer, move the slot to free buffer list.
1196 mCore->mFreeSlots.erase(slot);
1197 mCore->mFreeBuffers.push_front(slot);
1198
Antoine Labour78014f32014-07-15 21:17:03 -07001199 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1200 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001201
Antoine Labour78014f32014-07-15 21:17:03 -07001202 mCore->mIsAllocating = false;
1203 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001204 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001205 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001206 }
1207}
1208
Dan Stoza9de72932015-04-16 17:28:43 -07001209status_t BufferQueueProducer::allowAllocation(bool allow) {
1210 ATRACE_CALL();
1211 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1212
1213 Mutex::Autolock lock(mCore->mMutex);
1214 mCore->mAllowAllocation = allow;
1215 return NO_ERROR;
1216}
1217
Dan Stoza812ed062015-06-02 15:45:22 -07001218status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1219 ATRACE_CALL();
1220 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1221
1222 Mutex::Autolock lock(mCore->mMutex);
1223 mCore->mGenerationNumber = generationNumber;
1224 return NO_ERROR;
1225}
1226
Dan Stozac6f30bd2015-06-08 09:32:50 -07001227String8 BufferQueueProducer::getConsumerName() const {
1228 ATRACE_CALL();
1229 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1230 return mConsumerName;
1231}
1232
Dan Stoza7dde5992015-05-22 09:51:44 -07001233uint64_t BufferQueueProducer::getNextFrameNumber() const {
1234 ATRACE_CALL();
1235
1236 Mutex::Autolock lock(mCore->mMutex);
1237 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1238 return nextFrameNumber;
1239}
1240
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001241status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1242 ATRACE_CALL();
1243 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1244
1245 Mutex::Autolock lock(mCore->mMutex);
1246 if (!singleBufferMode) {
1247 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1248 }
1249 mCore->mSingleBufferMode = singleBufferMode;
1250
1251 return NO_ERROR;
1252}
1253
Dan Stoza289ade12014-02-28 11:17:17 -08001254void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1255 // If we're here, it means that a producer we were connected to died.
1256 // We're guaranteed that we are still connected to it because we remove
1257 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1258 // without synchronization here.
1259 int api = mCore->mConnectedApi;
1260 disconnect(api);
1261}
1262
1263} // namespace android