blob: b8df436bd9093bd6cf78523eee4df3012a0d8cbd [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
Dan Stoza3e96f192014-03-03 10:16:19 -080060 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080061 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080062 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080063 return BAD_VALUE;
64 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
65 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
66 "(state = %d)", slot, mSlots[slot].mBufferState);
67 return BAD_VALUE;
68 }
69
70 mSlots[slot].mRequestBufferCalled = true;
71 *buf = mSlots[slot].mGraphicBuffer;
72 return NO_ERROR;
73}
74
75status_t BufferQueueProducer::setBufferCount(int bufferCount) {
76 ATRACE_CALL();
77 BQ_LOGV("setBufferCount: count = %d", bufferCount);
78
79 sp<IConsumerListener> listener;
80 { // Autolock scope
81 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -070082 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -080083
84 if (mCore->mIsAbandoned) {
85 BQ_LOGE("setBufferCount: BufferQueue has been abandoned");
86 return NO_INIT;
87 }
88
Dan Stoza3e96f192014-03-03 10:16:19 -080089 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080090 BQ_LOGE("setBufferCount: bufferCount %d too large (max %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080091 bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080092 return BAD_VALUE;
93 }
94
95 // There must be no dequeued buffers when changing the buffer count.
Dan Stoza3e96f192014-03-03 10:16:19 -080096 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080097 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
98 BQ_LOGE("setBufferCount: buffer owned by producer");
Dan Stoza9f3053d2014-03-06 15:14:33 -080099 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800100 }
101 }
102
103 if (bufferCount == 0) {
104 mCore->mOverrideMaxBufferCount = 0;
105 mCore->mDequeueCondition.broadcast();
106 return NO_ERROR;
107 }
108
109 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(false);
110 if (bufferCount < minBufferSlots) {
111 BQ_LOGE("setBufferCount: requested buffer count %d is less than "
112 "minimum %d", bufferCount, minBufferSlots);
113 return BAD_VALUE;
114 }
115
116 // Here we are guaranteed that the producer doesn't have any dequeued
117 // buffers and will release all of its buffer references. We don't
118 // clear the queue, however, so that currently queued buffers still
119 // get displayed.
120 mCore->freeAllBuffersLocked();
121 mCore->mOverrideMaxBufferCount = bufferCount;
122 mCore->mDequeueCondition.broadcast();
123 listener = mCore->mConsumerListener;
124 } // Autolock scope
125
126 // Call back without lock held
127 if (listener != NULL) {
128 listener->onBuffersReleased();
129 }
130
131 return NO_ERROR;
132}
133
Dan Stoza9f3053d2014-03-06 15:14:33 -0800134status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
135 bool async, int* found, status_t* returnFlags) const {
136 bool tryAgain = true;
137 while (tryAgain) {
138 if (mCore->mIsAbandoned) {
139 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
140 return NO_INIT;
141 }
142
143 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
144 if (async && mCore->mOverrideMaxBufferCount) {
145 // FIXME: Some drivers are manually setting the buffer count
146 // (which they shouldn't), so we do this extra test here to
147 // handle that case. This is TEMPORARY until we get this fixed.
148 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
149 BQ_LOGE("%s: async mode is invalid with buffer count override",
150 caller);
151 return BAD_VALUE;
152 }
153 }
154
155 // Free up any buffers that are in slots beyond the max buffer count
156 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
157 assert(mSlots[s].mBufferState == BufferSlot::FREE);
158 if (mSlots[s].mGraphicBuffer != NULL) {
159 mCore->freeBufferLocked(s);
160 *returnFlags |= RELEASE_ALL_BUFFERS;
161 }
162 }
163
Dan Stoza9f3053d2014-03-06 15:14:33 -0800164 int dequeuedCount = 0;
165 int acquiredCount = 0;
166 for (int s = 0; s < maxBufferCount; ++s) {
167 switch (mSlots[s].mBufferState) {
168 case BufferSlot::DEQUEUED:
169 ++dequeuedCount;
170 break;
171 case BufferSlot::ACQUIRED:
172 ++acquiredCount;
173 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800174 default:
175 break;
176 }
177 }
178
179 // Producers are not allowed to dequeue more than one buffer if they
180 // did not set a buffer count
181 if (!mCore->mOverrideMaxBufferCount && dequeuedCount) {
182 BQ_LOGE("%s: can't dequeue multiple buffers without setting the "
183 "buffer count", caller);
184 return INVALID_OPERATION;
185 }
186
187 // See whether a buffer has been queued since the last
188 // setBufferCount so we know whether to perform the min undequeued
189 // buffers check below
190 if (mCore->mBufferHasBeenQueued) {
191 // Make sure the producer is not trying to dequeue more buffers
192 // than allowed
193 const int newUndequeuedCount =
194 maxBufferCount - (dequeuedCount + 1);
195 const int minUndequeuedCount =
196 mCore->getMinUndequeuedBufferCountLocked(async);
197 if (newUndequeuedCount < minUndequeuedCount) {
198 BQ_LOGE("%s: min undequeued buffer count (%d) exceeded "
199 "(dequeued=%d undequeued=%d)",
200 caller, minUndequeuedCount,
201 dequeuedCount, newUndequeuedCount);
202 return INVALID_OPERATION;
203 }
204 }
205
Dan Stoza0de7ea72015-04-23 13:20:51 -0700206 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
207
Dan Stozaae3c3682014-04-18 15:43:35 -0700208 // If we disconnect and reconnect quickly, we can be in a state where
209 // our slots are empty but we have many buffers in the queue. This can
210 // cause us to run out of memory if we outrun the consumer. Wait here if
211 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700212 bool tooManyBuffers = mCore->mQueue.size()
213 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700214 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700215 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700216 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700217 } else {
218 if (!mCore->mFreeBuffers.empty()) {
219 auto slot = mCore->mFreeBuffers.begin();
220 *found = *slot;
221 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700222 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700223 auto slot = mCore->mFreeSlots.begin();
224 // Only return free slots up to the max buffer count
225 if (*slot < maxBufferCount) {
226 *found = *slot;
227 mCore->mFreeSlots.erase(slot);
228 }
229 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700230 }
231
232 // If no buffer is found, or if the queue has too many buffers
233 // outstanding, wait for a buffer to be acquired or released, or for the
234 // max buffer count to change.
235 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
236 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800237 if (tryAgain) {
238 // Return an error if we're in non-blocking mode (producer and
239 // consumer are controlled by the application).
240 // However, the consumer is allowed to briefly acquire an extra
241 // buffer (which could cause us to have to wait here), which is
242 // okay, since it is only used to implement an atomic acquire +
243 // release (e.g., in GLConsumer::updateTexImage())
244 if (mCore->mDequeueBufferCannotBlock &&
245 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
246 return WOULD_BLOCK;
247 }
248 mCore->mDequeueCondition.wait(mCore->mMutex);
249 }
250 } // while (tryAgain)
251
252 return NO_ERROR;
253}
254
Dan Stoza289ade12014-02-28 11:17:17 -0800255status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
256 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800257 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800258 ATRACE_CALL();
259 { // Autolock scope
260 Mutex::Autolock lock(mCore->mMutex);
261 mConsumerName = mCore->mConsumerName;
262 } // Autolock scope
263
264 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
265 async ? "true" : "false", width, height, format, usage);
266
267 if ((width && !height) || (!width && height)) {
268 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
269 return BAD_VALUE;
270 }
271
272 status_t returnFlags = NO_ERROR;
273 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
274 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800275 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800276
277 { // Autolock scope
278 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700279 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800280
281 if (format == 0) {
282 format = mCore->mDefaultBufferFormat;
283 }
284
285 // Enable the usage bits the consumer requested
286 usage |= mCore->mConsumerUsageBits;
287
Dan Stoza9de72932015-04-16 17:28:43 -0700288 const bool useDefaultSize = !width && !height;
289 if (useDefaultSize) {
290 width = mCore->mDefaultWidth;
291 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800292 }
Dan Stoza289ade12014-02-28 11:17:17 -0800293
Dan Stoza9de72932015-04-16 17:28:43 -0700294 int found = BufferItem::INVALID_BUFFER_SLOT;
295 while (found == BufferItem::INVALID_BUFFER_SLOT) {
296 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
297 &found, &returnFlags);
298 if (status != NO_ERROR) {
299 return status;
300 }
301
302 // This should not happen
303 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
304 BQ_LOGE("dequeueBuffer: no available buffer slots");
305 return -EBUSY;
306 }
307
308 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
309
310 // If we are not allowed to allocate new buffers,
311 // waitForFreeSlotThenRelock must have returned a slot containing a
312 // buffer. If this buffer would require reallocation to meet the
313 // requested attributes, we free it and attempt to get another one.
314 if (!mCore->mAllowAllocation) {
315 if (buffer->needsReallocation(width, height, format, usage)) {
316 mCore->freeBufferLocked(found);
317 found = BufferItem::INVALID_BUFFER_SLOT;
318 continue;
319 }
320 }
Dan Stoza289ade12014-02-28 11:17:17 -0800321 }
322
323 *outSlot = found;
324 ATRACE_BUFFER_INDEX(found);
325
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 attachedByConsumer = mSlots[found].mAttachedByConsumer;
327
Dan Stoza289ade12014-02-28 11:17:17 -0800328 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
329
330 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
331 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700332 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800333 {
334 mSlots[found].mAcquireCalled = false;
335 mSlots[found].mGraphicBuffer = NULL;
336 mSlots[found].mRequestBufferCalled = false;
337 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
338 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
339 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800340 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800341
342 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800343 } else {
344 // We add 1 because that will be the frame number when this buffer
345 // is queued
346 mCore->mBufferAge =
347 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800348 }
349
Dan Stoza4afd8b62015-02-25 16:49:08 -0800350 BQ_LOGV("dequeueBuffer: setting buffer age to %llu", mCore->mBufferAge);
351
Dan Stoza289ade12014-02-28 11:17:17 -0800352 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
353 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
354 "slot=%d w=%d h=%d format=%u",
355 found, buffer->width, buffer->height, buffer->format);
356 }
357
358 eglDisplay = mSlots[found].mEglDisplay;
359 eglFence = mSlots[found].mEglFence;
360 *outFence = mSlots[found].mFence;
361 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
362 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700363
364 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800365 } // Autolock scope
366
367 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
368 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700369 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800370 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800371 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800372 if (graphicBuffer == NULL) {
373 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
374 return error;
375 }
376
377 { // Autolock scope
378 Mutex::Autolock lock(mCore->mMutex);
379
380 if (mCore->mIsAbandoned) {
381 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
382 return NO_INIT;
383 }
384
Dan Stoza289ade12014-02-28 11:17:17 -0800385 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
386 } // Autolock scope
387 }
388
Dan Stoza9f3053d2014-03-06 15:14:33 -0800389 if (attachedByConsumer) {
390 returnFlags |= BUFFER_NEEDS_REALLOCATION;
391 }
392
Dan Stoza289ade12014-02-28 11:17:17 -0800393 if (eglFence != EGL_NO_SYNC_KHR) {
394 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
395 1000000000);
396 // If something goes wrong, log the error, but return the buffer without
397 // synchronizing access to it. It's too late at this point to abort the
398 // dequeue operation.
399 if (result == EGL_FALSE) {
400 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
401 eglGetError());
402 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
403 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
404 }
405 eglDestroySyncKHR(eglDisplay, eglFence);
406 }
407
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700408 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
409 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800410 mSlots[*outSlot].mFrameNumber,
411 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
412
413 return returnFlags;
414}
415
Dan Stoza9f3053d2014-03-06 15:14:33 -0800416status_t BufferQueueProducer::detachBuffer(int slot) {
417 ATRACE_CALL();
418 ATRACE_BUFFER_INDEX(slot);
419 BQ_LOGV("detachBuffer(P): slot %d", slot);
420 Mutex::Autolock lock(mCore->mMutex);
421
422 if (mCore->mIsAbandoned) {
423 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
424 return NO_INIT;
425 }
426
427 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
428 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
429 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
430 return BAD_VALUE;
431 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
432 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
433 "(state = %d)", slot, mSlots[slot].mBufferState);
434 return BAD_VALUE;
435 } else if (!mSlots[slot].mRequestBufferCalled) {
436 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
437 slot);
438 return BAD_VALUE;
439 }
440
441 mCore->freeBufferLocked(slot);
442 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700443 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800444
445 return NO_ERROR;
446}
447
Dan Stozad9822a32014-03-28 15:25:31 -0700448status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
449 sp<Fence>* outFence) {
450 ATRACE_CALL();
451
452 if (outBuffer == NULL) {
453 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
454 return BAD_VALUE;
455 } else if (outFence == NULL) {
456 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
457 return BAD_VALUE;
458 }
459
460 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700461 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700462
463 if (mCore->mIsAbandoned) {
464 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
465 return NO_INIT;
466 }
467
Dan Stoza0de7ea72015-04-23 13:20:51 -0700468 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000469 return NO_MEMORY;
470 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700471
Dan Stoza0de7ea72015-04-23 13:20:51 -0700472 int found = mCore->mFreeBuffers.front();
473 mCore->mFreeBuffers.remove(found);
474
Dan Stozad9822a32014-03-28 15:25:31 -0700475 BQ_LOGV("detachNextBuffer detached slot %d", found);
476
477 *outBuffer = mSlots[found].mGraphicBuffer;
478 *outFence = mSlots[found].mFence;
479 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700480 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700481
482 return NO_ERROR;
483}
484
Dan Stoza9f3053d2014-03-06 15:14:33 -0800485status_t BufferQueueProducer::attachBuffer(int* outSlot,
486 const sp<android::GraphicBuffer>& buffer) {
487 ATRACE_CALL();
488
489 if (outSlot == NULL) {
490 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
491 return BAD_VALUE;
492 } else if (buffer == NULL) {
493 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
494 return BAD_VALUE;
495 }
496
497 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700498 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800499
500 status_t returnFlags = NO_ERROR;
501 int found;
502 // TODO: Should we provide an async flag to attachBuffer? It seems
503 // unlikely that buffers which we are attaching to a BufferQueue will
504 // be asynchronous (droppable), but it may not be impossible.
505 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
506 &found, &returnFlags);
507 if (status != NO_ERROR) {
508 return status;
509 }
510
511 // This should not happen
512 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
513 BQ_LOGE("attachBuffer(P): no available buffer slots");
514 return -EBUSY;
515 }
516
517 *outSlot = found;
518 ATRACE_BUFFER_INDEX(*outSlot);
519 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
520 *outSlot, returnFlags);
521
522 mSlots[*outSlot].mGraphicBuffer = buffer;
523 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
524 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
525 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700526 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800527
Dan Stoza0de7ea72015-04-23 13:20:51 -0700528 mCore->validateConsistencyLocked();
529
Dan Stoza9f3053d2014-03-06 15:14:33 -0800530 return returnFlags;
531}
532
Dan Stoza289ade12014-02-28 11:17:17 -0800533status_t BufferQueueProducer::queueBuffer(int slot,
534 const QueueBufferInput &input, QueueBufferOutput *output) {
535 ATRACE_CALL();
536 ATRACE_BUFFER_INDEX(slot);
537
538 int64_t timestamp;
539 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800540 android_dataspace dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800541 Rect crop;
542 int scalingMode;
543 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700544 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800545 bool async;
546 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800547 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
548 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700549 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800550
551 if (fence == NULL) {
552 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800553 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800554 }
555
556 switch (scalingMode) {
557 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
558 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
559 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
560 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
561 break;
562 default:
563 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800564 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800565 }
566
Dan Stoza8dc55392014-11-04 11:37:46 -0800567 sp<IConsumerListener> frameAvailableListener;
568 sp<IConsumerListener> frameReplacedListener;
569 int callbackTicket = 0;
570 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800571 { // Autolock scope
572 Mutex::Autolock lock(mCore->mMutex);
573
574 if (mCore->mIsAbandoned) {
575 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
576 return NO_INIT;
577 }
578
579 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
580 if (async && mCore->mOverrideMaxBufferCount) {
581 // FIXME: Some drivers are manually setting the buffer count
582 // (which they shouldn't), so we do this extra test here to
583 // handle that case. This is TEMPORARY until we get this fixed.
584 if (mCore->mOverrideMaxBufferCount < maxBufferCount) {
585 BQ_LOGE("queueBuffer: async mode is invalid with "
586 "buffer count override");
587 return BAD_VALUE;
588 }
589 }
590
591 if (slot < 0 || slot >= maxBufferCount) {
592 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
593 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800594 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800595 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
596 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
597 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800598 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800599 } else if (!mSlots[slot].mRequestBufferCalled) {
600 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
601 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800602 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800603 }
604
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800605 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700606 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800607 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800608 crop.left, crop.top, crop.right, crop.bottom, transform,
609 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800610
611 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
612 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
613 Rect croppedRect;
614 crop.intersect(bufferRect, &croppedRect);
615 if (croppedRect != crop) {
616 BQ_LOGE("queueBuffer: crop rect is not contained within the "
617 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800618 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800619 }
620
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800621 // Override UNKNOWN dataspace with consumer default
622 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
623 dataSpace = mCore->mDefaultBufferDataSpace;
624 }
625
Dan Stoza289ade12014-02-28 11:17:17 -0800626 mSlots[slot].mFence = fence;
627 mSlots[slot].mBufferState = BufferSlot::QUEUED;
628 ++mCore->mFrameCounter;
629 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
630
Dan Stoza289ade12014-02-28 11:17:17 -0800631 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
632 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
633 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800634 item.mTransform = transform &
635 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800636 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800637 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
638 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800639 item.mTimestamp = timestamp;
640 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800641 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800642 item.mFrameNumber = mCore->mFrameCounter;
643 item.mSlot = slot;
644 item.mFence = fence;
645 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700646 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800647
Ruben Brunk1681d952014-06-27 15:51:55 -0700648 mStickyTransform = stickyTransform;
649
Dan Stoza289ade12014-02-28 11:17:17 -0800650 if (mCore->mQueue.empty()) {
651 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
652 // and simply queue this buffer
653 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800654 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800655 } else {
656 // When the queue is not empty, we need to look at the front buffer
657 // state to see if we need to replace it
658 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
659 if (front->mIsDroppable) {
660 // If the front queued buffer is still being tracked, we first
661 // mark it as freed
662 if (mCore->stillTracking(front)) {
663 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700664 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800665 }
666 // Overwrite the droppable buffer with the incoming one
667 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800668 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800669 } else {
670 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800671 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800672 }
673 }
674
675 mCore->mBufferHasBeenQueued = true;
676 mCore->mDequeueCondition.broadcast();
677
678 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800679 mCore->mTransformHint,
680 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800681
682 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800683
684 // Take a ticket for the callback functions
685 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700686
687 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800688 } // Autolock scope
689
Eric Penner99a0afb2014-09-30 11:28:30 -0700690 // Wait without lock held
691 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
692 // Waiting here allows for two full buffers to be queued but not a
693 // third. In the event that frames take varying time, this makes a
694 // small trade-off in favor of latency rather than throughput.
695 mLastQueueBufferFence->waitForever("Throttling EGL Production");
696 mLastQueueBufferFence = fence;
697 }
698
Dan Stoza8dc55392014-11-04 11:37:46 -0800699 // Don't send the GraphicBuffer through the callback, and don't send
700 // the slot number, since the consumer shouldn't need it
701 item.mGraphicBuffer.clear();
702 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
703
704 // Call back without the main BufferQueue lock held, but with the callback
705 // lock held so we can ensure that callbacks occur in order
706 {
707 Mutex::Autolock lock(mCallbackMutex);
708 while (callbackTicket != mCurrentCallbackTicket) {
709 mCallbackCondition.wait(mCallbackMutex);
710 }
711
712 if (frameAvailableListener != NULL) {
713 frameAvailableListener->onFrameAvailable(item);
714 } else if (frameReplacedListener != NULL) {
715 frameReplacedListener->onFrameReplaced(item);
716 }
717
718 ++mCurrentCallbackTicket;
719 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800720 }
721
722 return NO_ERROR;
723}
724
725void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
726 ATRACE_CALL();
727 BQ_LOGV("cancelBuffer: slot %d", slot);
728 Mutex::Autolock lock(mCore->mMutex);
729
730 if (mCore->mIsAbandoned) {
731 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
732 return;
733 }
734
Dan Stoza3e96f192014-03-03 10:16:19 -0800735 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800736 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800737 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800738 return;
739 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
740 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
741 "(state = %d)", slot, mSlots[slot].mBufferState);
742 return;
743 } else if (fence == NULL) {
744 BQ_LOGE("cancelBuffer: fence is NULL");
745 return;
746 }
747
Dan Stoza0de7ea72015-04-23 13:20:51 -0700748 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800749 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800750 mSlots[slot].mFence = fence;
751 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700752 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800753}
754
755int BufferQueueProducer::query(int what, int *outValue) {
756 ATRACE_CALL();
757 Mutex::Autolock lock(mCore->mMutex);
758
759 if (outValue == NULL) {
760 BQ_LOGE("query: outValue was NULL");
761 return BAD_VALUE;
762 }
763
764 if (mCore->mIsAbandoned) {
765 BQ_LOGE("query: BufferQueue has been abandoned");
766 return NO_INIT;
767 }
768
769 int value;
770 switch (what) {
771 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800772 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800773 break;
774 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800775 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800776 break;
777 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800778 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800779 break;
780 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
781 value = mCore->getMinUndequeuedBufferCountLocked(false);
782 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700783 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800784 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700785 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800786 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
787 value = (mCore->mQueue.size() > 1);
788 break;
789 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800790 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800791 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800792 case NATIVE_WINDOW_DEFAULT_DATASPACE:
793 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
794 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800795 case NATIVE_WINDOW_BUFFER_AGE:
796 if (mCore->mBufferAge > INT32_MAX) {
797 value = 0;
798 } else {
799 value = static_cast<int32_t>(mCore->mBufferAge);
800 }
801 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 default:
803 return BAD_VALUE;
804 }
805
806 BQ_LOGV("query: %d? %d", what, value);
807 *outValue = value;
808 return NO_ERROR;
809}
810
Dan Stozaf0eaf252014-03-21 13:05:51 -0700811status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800812 int api, bool producerControlledByApp, QueueBufferOutput *output) {
813 ATRACE_CALL();
814 Mutex::Autolock lock(mCore->mMutex);
815 mConsumerName = mCore->mConsumerName;
816 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
817 producerControlledByApp ? "true" : "false");
818
Dan Stozaae3c3682014-04-18 15:43:35 -0700819 if (mCore->mIsAbandoned) {
820 BQ_LOGE("connect(P): BufferQueue has been abandoned");
821 return NO_INIT;
822 }
Dan Stoza289ade12014-02-28 11:17:17 -0800823
Dan Stozaae3c3682014-04-18 15:43:35 -0700824 if (mCore->mConsumerListener == NULL) {
825 BQ_LOGE("connect(P): BufferQueue has no consumer");
826 return NO_INIT;
827 }
Dan Stoza289ade12014-02-28 11:17:17 -0800828
Dan Stozaae3c3682014-04-18 15:43:35 -0700829 if (output == NULL) {
830 BQ_LOGE("connect(P): output was NULL");
831 return BAD_VALUE;
832 }
Dan Stoza289ade12014-02-28 11:17:17 -0800833
Dan Stozaae3c3682014-04-18 15:43:35 -0700834 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
835 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
836 mCore->mConnectedApi, api);
837 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800838 }
839
840 int status = NO_ERROR;
841 switch (api) {
842 case NATIVE_WINDOW_API_EGL:
843 case NATIVE_WINDOW_API_CPU:
844 case NATIVE_WINDOW_API_MEDIA:
845 case NATIVE_WINDOW_API_CAMERA:
846 mCore->mConnectedApi = api;
847 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800848 mCore->mTransformHint,
849 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800850
851 // Set up a death notification so that we can disconnect
852 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700853 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800854 IInterface::asBinder(listener)->remoteBinder() != NULL) {
855 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800856 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700857 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800858 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
859 strerror(-status), status);
860 }
861 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700862 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800863 break;
864 default:
865 BQ_LOGE("connect(P): unknown API %d", api);
866 status = BAD_VALUE;
867 break;
868 }
869
870 mCore->mBufferHasBeenQueued = false;
871 mCore->mDequeueBufferCannotBlock =
872 mCore->mConsumerControlledByApp && producerControlledByApp;
873
874 return status;
875}
876
877status_t BufferQueueProducer::disconnect(int api) {
878 ATRACE_CALL();
879 BQ_LOGV("disconnect(P): api %d", api);
880
881 int status = NO_ERROR;
882 sp<IConsumerListener> listener;
883 { // Autolock scope
884 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700885 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800886
887 if (mCore->mIsAbandoned) {
888 // It's not really an error to disconnect after the surface has
889 // been abandoned; it should just be a no-op.
890 return NO_ERROR;
891 }
892
893 switch (api) {
894 case NATIVE_WINDOW_API_EGL:
895 case NATIVE_WINDOW_API_CPU:
896 case NATIVE_WINDOW_API_MEDIA:
897 case NATIVE_WINDOW_API_CAMERA:
898 if (mCore->mConnectedApi == api) {
899 mCore->freeAllBuffersLocked();
900
901 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700902 if (mCore->mConnectedProducerListener != NULL) {
903 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800904 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800905 // This can fail if we're here because of the death
906 // notification, but we just ignore it
907 token->unlinkToDeath(
908 static_cast<IBinder::DeathRecipient*>(this));
909 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700910 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800911 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800912 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800913 mCore->mDequeueCondition.broadcast();
914 listener = mCore->mConsumerListener;
Michael Lentine45e2fc22014-08-08 10:30:44 -0700915 } else {
916 BQ_LOGE("disconnect(P): connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800917 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800918 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800919 }
920 break;
921 default:
922 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800923 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800924 break;
925 }
926 } // Autolock scope
927
928 // Call back without lock held
929 if (listener != NULL) {
930 listener->onBuffersReleased();
931 }
932
933 return status;
934}
935
Jesse Hall399184a2014-03-03 15:42:54 -0800936status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900937 sp<IConsumerListener> listener;
938 { // Autolock scope
939 Mutex::Autolock _l(mCore->mMutex);
940 mCore->mSidebandStream = stream;
941 listener = mCore->mConsumerListener;
942 } // Autolock scope
943
944 if (listener != NULL) {
945 listener->onSidebandStreamChanged();
946 }
Jesse Hall399184a2014-03-03 15:42:54 -0800947 return NO_ERROR;
948}
949
Dan Stoza29a3e902014-06-20 13:13:57 -0700950void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800951 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700952 ATRACE_CALL();
953 while (true) {
954 Vector<int> freeSlots;
955 size_t newBufferCount = 0;
956 uint32_t allocWidth = 0;
957 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800958 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700959 uint32_t allocUsage = 0;
960 { // Autolock scope
961 Mutex::Autolock lock(mCore->mMutex);
962 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700963
Dan Stoza9de72932015-04-16 17:28:43 -0700964 if (!mCore->mAllowAllocation) {
965 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
966 "BufferQueue");
967 return;
968 }
969
Antoine Labour78014f32014-07-15 21:17:03 -0700970 int currentBufferCount = 0;
971 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
972 if (mSlots[slot].mGraphicBuffer != NULL) {
973 ++currentBufferCount;
974 } else {
975 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
976 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
977 slot);
978 continue;
979 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700980
Antoine Labour11f14872014-07-25 18:14:42 -0700981 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -0700982 }
983 }
984
985 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
986 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
987 currentBufferCount, maxBufferCount);
988 if (maxBufferCount <= currentBufferCount)
989 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800990 newBufferCount =
991 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -0700992 if (freeSlots.size() < newBufferCount) {
993 BQ_LOGE("allocateBuffers: ran out of free slots");
994 return;
995 }
996 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
997 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
998 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
999 allocUsage = usage | mCore->mConsumerUsageBits;
1000
1001 mCore->mIsAllocating = true;
1002 } // Autolock scope
1003
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001004 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001005 for (size_t i = 0; i < newBufferCount; ++i) {
1006 status_t result = NO_ERROR;
1007 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1008 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1009 if (result != NO_ERROR) {
1010 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1011 " %u, usage %u)", width, height, format, usage);
1012 Mutex::Autolock lock(mCore->mMutex);
1013 mCore->mIsAllocating = false;
1014 mCore->mIsAllocatingCondition.broadcast();
1015 return;
1016 }
1017 buffers.push_back(graphicBuffer);
1018 }
1019
1020 { // Autolock scope
1021 Mutex::Autolock lock(mCore->mMutex);
1022 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1023 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001024 PixelFormat checkFormat = format != 0 ?
1025 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001026 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1027 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1028 checkFormat != allocFormat || checkUsage != allocUsage) {
1029 // Something changed while we released the lock. Retry.
1030 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1031 mCore->mIsAllocating = false;
1032 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001033 continue;
1034 }
1035
Antoine Labour78014f32014-07-15 21:17:03 -07001036 for (size_t i = 0; i < newBufferCount; ++i) {
1037 int slot = freeSlots[i];
1038 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1039 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1040 // allocated.
1041 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1042 "Dropping allocated buffer.", slot);
1043 continue;
1044 }
1045 mCore->freeBufferLocked(slot); // Clean up the slot first
1046 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001047 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001048
1049 // freeBufferLocked puts this slot on the free slots list. Since
1050 // we then attached a buffer, move the slot to free buffer list.
1051 mCore->mFreeSlots.erase(slot);
1052 mCore->mFreeBuffers.push_front(slot);
1053
Antoine Labour78014f32014-07-15 21:17:03 -07001054 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1055 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001056
Antoine Labour78014f32014-07-15 21:17:03 -07001057 mCore->mIsAllocating = false;
1058 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001059 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001060 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001061 }
1062}
1063
Dan Stoza9de72932015-04-16 17:28:43 -07001064status_t BufferQueueProducer::allowAllocation(bool allow) {
1065 ATRACE_CALL();
1066 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1067
1068 Mutex::Autolock lock(mCore->mMutex);
1069 mCore->mAllowAllocation = allow;
1070 return NO_ERROR;
1071}
1072
Dan Stoza289ade12014-02-28 11:17:17 -08001073void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1074 // If we're here, it means that a producer we were connected to died.
1075 // We're guaranteed that we are still connected to it because we remove
1076 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1077 // without synchronization here.
1078 int api = mCore->mConnectedApi;
1079 disconnect(api);
1080}
1081
1082} // namespace android