blob: 8cc0cfab4a0139f108353e56c750742323903ab6 [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
Pablo Ceballosfa455352015-08-12 17:47:47 -070075status_t BufferQueueProducer::setMaxDequeuedBufferCount(
76 int maxDequeuedBuffers) {
77 ATRACE_CALL();
78 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
79 maxDequeuedBuffers);
80
81 sp<IConsumerListener> listener;
82 { // Autolock scope
83 Mutex::Autolock lock(mCore->mMutex);
84 mCore->waitWhileAllocatingLocked();
85
86 if (mCore->mIsAbandoned) {
87 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
88 "abandoned");
89 return NO_INIT;
90 }
91
92 // There must be no dequeued buffers when changing the buffer count.
93 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
94 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
95 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
96 return BAD_VALUE;
97 }
98 }
99
100 int bufferCount = mCore->getMinUndequeuedBufferCountLocked(
101 mCore->mAsyncMode);
102 bufferCount += maxDequeuedBuffers;
103
104 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
105 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
106 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
107 return BAD_VALUE;
108 }
109
110 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(
111 mCore->mAsyncMode);
112 if (bufferCount < minBufferSlots) {
113 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
114 "less than minimum %d", bufferCount, minBufferSlots);
115 return BAD_VALUE;
116 }
117
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700118 if (bufferCount > mCore->mMaxBufferCount) {
119 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
120 "exceed the maxBufferCount (%d) (maxAcquired %d async %d)",
121 maxDequeuedBuffers, mCore->mMaxBufferCount,
122 mCore->mMaxAcquiredBufferCount, mCore->mAsyncMode);
123 return BAD_VALUE;
124 }
125
Pablo Ceballosfa455352015-08-12 17:47:47 -0700126 // Here we are guaranteed that the producer doesn't have any dequeued
127 // buffers and will release all of its buffer references. We don't
128 // clear the queue, however, so that currently queued buffers still
129 // get displayed.
130 mCore->freeAllBuffersLocked();
131 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132 mCore->mDequeueCondition.broadcast();
133 listener = mCore->mConsumerListener;
134 } // Autolock scope
135
136 // Call back without lock held
137 if (listener != NULL) {
138 listener->onBuffersReleased();
139 }
140
141 return NO_ERROR;
142}
143
144status_t BufferQueueProducer::setAsyncMode(bool async) {
145 ATRACE_CALL();
146 BQ_LOGV("setAsyncMode: async = %d", async);
147
148 sp<IConsumerListener> listener;
149 { // Autolock scope
150 Mutex::Autolock lock(mCore->mMutex);
151 mCore->waitWhileAllocatingLocked();
152
153 if (mCore->mIsAbandoned) {
154 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
155 return NO_INIT;
156 }
157
158 // There must be no dequeued buffers when changing the async mode.
159 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
160 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
161 BQ_LOGE("setAsyncMode: buffer owned by producer");
162 return BAD_VALUE;
163 }
164 }
165
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700166 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
167 (async ? 1 : 0)) > mCore->mMaxBufferCount) {
168 BQ_LOGE("setAsyncMode(%d): this call would cause the "
169 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
170 "maxDequeued %d)", async,mCore->mMaxBufferCount,
171 mCore->mMaxAcquiredBufferCount,
172 mCore->mMaxDequeuedBufferCount);
173 return BAD_VALUE;
174 }
175
Pablo Ceballosfa455352015-08-12 17:47:47 -0700176 mCore->mAsyncMode = async;
177 mCore->mDequeueCondition.broadcast();
178 listener = mCore->mConsumerListener;
179 } // Autolock scope
180
181 // Call back without lock held
182 if (listener != NULL) {
183 listener->onBuffersReleased();
184 }
185 return NO_ERROR;
186}
187
Dan Stoza9f3053d2014-03-06 15:14:33 -0800188status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
189 bool async, int* found, status_t* returnFlags) const {
190 bool tryAgain = true;
191 while (tryAgain) {
192 if (mCore->mIsAbandoned) {
193 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
194 return NO_INIT;
195 }
196
197 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800198
199 // Free up any buffers that are in slots beyond the max buffer count
200 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
201 assert(mSlots[s].mBufferState == BufferSlot::FREE);
202 if (mSlots[s].mGraphicBuffer != NULL) {
203 mCore->freeBufferLocked(s);
204 *returnFlags |= RELEASE_ALL_BUFFERS;
205 }
206 }
207
Dan Stoza9f3053d2014-03-06 15:14:33 -0800208 int dequeuedCount = 0;
209 int acquiredCount = 0;
210 for (int s = 0; s < maxBufferCount; ++s) {
211 switch (mSlots[s].mBufferState) {
212 case BufferSlot::DEQUEUED:
213 ++dequeuedCount;
214 break;
215 case BufferSlot::ACQUIRED:
216 ++acquiredCount;
217 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800218 default:
219 break;
220 }
221 }
222
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700223 // Producers are not allowed to dequeue more than
224 // mMaxDequeuedBufferCount buffers.
225 // This check is only done if a buffer has already been queued
226 if (mCore->mBufferHasBeenQueued &&
227 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
228 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
229 "(%d)", caller, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800230 return INVALID_OPERATION;
231 }
232
Dan Stoza0de7ea72015-04-23 13:20:51 -0700233 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
234
Dan Stozaae3c3682014-04-18 15:43:35 -0700235 // If we disconnect and reconnect quickly, we can be in a state where
236 // our slots are empty but we have many buffers in the queue. This can
237 // cause us to run out of memory if we outrun the consumer. Wait here if
238 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700239 bool tooManyBuffers = mCore->mQueue.size()
240 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700241 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700242 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700243 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700244 } else {
245 if (!mCore->mFreeBuffers.empty()) {
246 auto slot = mCore->mFreeBuffers.begin();
247 *found = *slot;
248 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700249 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700250 auto slot = mCore->mFreeSlots.begin();
251 // Only return free slots up to the max buffer count
252 if (*slot < maxBufferCount) {
253 *found = *slot;
254 mCore->mFreeSlots.erase(slot);
255 }
256 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700257 }
258
259 // If no buffer is found, or if the queue has too many buffers
260 // outstanding, wait for a buffer to be acquired or released, or for the
261 // max buffer count to change.
262 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
263 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800264 if (tryAgain) {
265 // Return an error if we're in non-blocking mode (producer and
266 // consumer are controlled by the application).
267 // However, the consumer is allowed to briefly acquire an extra
268 // buffer (which could cause us to have to wait here), which is
269 // okay, since it is only used to implement an atomic acquire +
270 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700271 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800272 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
273 return WOULD_BLOCK;
274 }
275 mCore->mDequeueCondition.wait(mCore->mMutex);
276 }
277 } // while (tryAgain)
278
279 return NO_ERROR;
280}
281
Dan Stoza289ade12014-02-28 11:17:17 -0800282status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
283 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800284 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800285 ATRACE_CALL();
286 { // Autolock scope
287 Mutex::Autolock lock(mCore->mMutex);
288 mConsumerName = mCore->mConsumerName;
289 } // Autolock scope
290
291 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
292 async ? "true" : "false", width, height, format, usage);
293
294 if ((width && !height) || (!width && height)) {
295 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
296 return BAD_VALUE;
297 }
298
299 status_t returnFlags = NO_ERROR;
300 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
301 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800302 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800303
304 { // Autolock scope
305 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700306 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800307
308 if (format == 0) {
309 format = mCore->mDefaultBufferFormat;
310 }
311
312 // Enable the usage bits the consumer requested
313 usage |= mCore->mConsumerUsageBits;
314
Dan Stoza9de72932015-04-16 17:28:43 -0700315 const bool useDefaultSize = !width && !height;
316 if (useDefaultSize) {
317 width = mCore->mDefaultWidth;
318 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800319 }
Dan Stoza289ade12014-02-28 11:17:17 -0800320
Dan Stoza9de72932015-04-16 17:28:43 -0700321 int found = BufferItem::INVALID_BUFFER_SLOT;
322 while (found == BufferItem::INVALID_BUFFER_SLOT) {
323 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
324 &found, &returnFlags);
325 if (status != NO_ERROR) {
326 return status;
327 }
328
329 // This should not happen
330 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
331 BQ_LOGE("dequeueBuffer: no available buffer slots");
332 return -EBUSY;
333 }
334
335 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
336
337 // If we are not allowed to allocate new buffers,
338 // waitForFreeSlotThenRelock must have returned a slot containing a
339 // buffer. If this buffer would require reallocation to meet the
340 // requested attributes, we free it and attempt to get another one.
341 if (!mCore->mAllowAllocation) {
342 if (buffer->needsReallocation(width, height, format, usage)) {
343 mCore->freeBufferLocked(found);
344 found = BufferItem::INVALID_BUFFER_SLOT;
345 continue;
346 }
347 }
Dan Stoza289ade12014-02-28 11:17:17 -0800348 }
349
350 *outSlot = found;
351 ATRACE_BUFFER_INDEX(found);
352
Dan Stoza9f3053d2014-03-06 15:14:33 -0800353 attachedByConsumer = mSlots[found].mAttachedByConsumer;
354
Dan Stoza289ade12014-02-28 11:17:17 -0800355 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
356
357 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
358 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700359 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800360 {
361 mSlots[found].mAcquireCalled = false;
362 mSlots[found].mGraphicBuffer = NULL;
363 mSlots[found].mRequestBufferCalled = false;
364 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
365 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
366 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800367 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800368
369 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800370 } else {
371 // We add 1 because that will be the frame number when this buffer
372 // is queued
373 mCore->mBufferAge =
374 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800375 }
376
Dan Stoza800b41a2015-04-28 14:20:04 -0700377 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
378 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800379
Dan Stoza289ade12014-02-28 11:17:17 -0800380 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
381 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
382 "slot=%d w=%d h=%d format=%u",
383 found, buffer->width, buffer->height, buffer->format);
384 }
385
386 eglDisplay = mSlots[found].mEglDisplay;
387 eglFence = mSlots[found].mEglFence;
388 *outFence = mSlots[found].mFence;
389 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
390 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700391
392 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800393 } // Autolock scope
394
395 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
396 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700397 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800398 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800399 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800400 if (graphicBuffer == NULL) {
401 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
402 return error;
403 }
404
405 { // Autolock scope
406 Mutex::Autolock lock(mCore->mMutex);
407
408 if (mCore->mIsAbandoned) {
409 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
410 return NO_INIT;
411 }
412
Dan Stoza812ed062015-06-02 15:45:22 -0700413 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800414 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
415 } // Autolock scope
416 }
417
Dan Stoza9f3053d2014-03-06 15:14:33 -0800418 if (attachedByConsumer) {
419 returnFlags |= BUFFER_NEEDS_REALLOCATION;
420 }
421
Dan Stoza289ade12014-02-28 11:17:17 -0800422 if (eglFence != EGL_NO_SYNC_KHR) {
423 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
424 1000000000);
425 // If something goes wrong, log the error, but return the buffer without
426 // synchronizing access to it. It's too late at this point to abort the
427 // dequeue operation.
428 if (result == EGL_FALSE) {
429 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
430 eglGetError());
431 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
432 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
433 }
434 eglDestroySyncKHR(eglDisplay, eglFence);
435 }
436
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700437 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
438 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800439 mSlots[*outSlot].mFrameNumber,
440 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
441
442 return returnFlags;
443}
444
Dan Stoza9f3053d2014-03-06 15:14:33 -0800445status_t BufferQueueProducer::detachBuffer(int slot) {
446 ATRACE_CALL();
447 ATRACE_BUFFER_INDEX(slot);
448 BQ_LOGV("detachBuffer(P): slot %d", slot);
449 Mutex::Autolock lock(mCore->mMutex);
450
451 if (mCore->mIsAbandoned) {
452 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
453 return NO_INIT;
454 }
455
456 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
457 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
458 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
459 return BAD_VALUE;
460 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
461 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
462 "(state = %d)", slot, mSlots[slot].mBufferState);
463 return BAD_VALUE;
464 } else if (!mSlots[slot].mRequestBufferCalled) {
465 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
466 slot);
467 return BAD_VALUE;
468 }
469
470 mCore->freeBufferLocked(slot);
471 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700472 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800473
474 return NO_ERROR;
475}
476
Dan Stozad9822a32014-03-28 15:25:31 -0700477status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
478 sp<Fence>* outFence) {
479 ATRACE_CALL();
480
481 if (outBuffer == NULL) {
482 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
483 return BAD_VALUE;
484 } else if (outFence == NULL) {
485 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
486 return BAD_VALUE;
487 }
488
489 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700490 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700491
492 if (mCore->mIsAbandoned) {
493 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
494 return NO_INIT;
495 }
496
Dan Stoza0de7ea72015-04-23 13:20:51 -0700497 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000498 return NO_MEMORY;
499 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700500
Dan Stoza0de7ea72015-04-23 13:20:51 -0700501 int found = mCore->mFreeBuffers.front();
502 mCore->mFreeBuffers.remove(found);
503
Dan Stozad9822a32014-03-28 15:25:31 -0700504 BQ_LOGV("detachNextBuffer detached slot %d", found);
505
506 *outBuffer = mSlots[found].mGraphicBuffer;
507 *outFence = mSlots[found].mFence;
508 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700509 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700510
511 return NO_ERROR;
512}
513
Dan Stoza9f3053d2014-03-06 15:14:33 -0800514status_t BufferQueueProducer::attachBuffer(int* outSlot,
515 const sp<android::GraphicBuffer>& buffer) {
516 ATRACE_CALL();
517
518 if (outSlot == NULL) {
519 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
520 return BAD_VALUE;
521 } else if (buffer == NULL) {
522 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
523 return BAD_VALUE;
524 }
525
526 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700527 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800528
Dan Stoza812ed062015-06-02 15:45:22 -0700529 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
530 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
531 "[queue %u]", buffer->getGenerationNumber(),
532 mCore->mGenerationNumber);
533 return BAD_VALUE;
534 }
535
Dan Stoza9f3053d2014-03-06 15:14:33 -0800536 status_t returnFlags = NO_ERROR;
537 int found;
538 // TODO: Should we provide an async flag to attachBuffer? It seems
539 // unlikely that buffers which we are attaching to a BufferQueue will
540 // be asynchronous (droppable), but it may not be impossible.
541 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
542 &found, &returnFlags);
543 if (status != NO_ERROR) {
544 return status;
545 }
546
547 // This should not happen
548 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
549 BQ_LOGE("attachBuffer(P): no available buffer slots");
550 return -EBUSY;
551 }
552
553 *outSlot = found;
554 ATRACE_BUFFER_INDEX(*outSlot);
555 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
556 *outSlot, returnFlags);
557
558 mSlots[*outSlot].mGraphicBuffer = buffer;
559 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
560 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
561 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700562 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800563
Dan Stoza0de7ea72015-04-23 13:20:51 -0700564 mCore->validateConsistencyLocked();
565
Dan Stoza9f3053d2014-03-06 15:14:33 -0800566 return returnFlags;
567}
568
Dan Stoza289ade12014-02-28 11:17:17 -0800569status_t BufferQueueProducer::queueBuffer(int slot,
570 const QueueBufferInput &input, QueueBufferOutput *output) {
571 ATRACE_CALL();
572 ATRACE_BUFFER_INDEX(slot);
573
574 int64_t timestamp;
575 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800576 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700577 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800578 int scalingMode;
579 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700580 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800581 bool async;
582 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800583 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
584 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700585 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800586
587 if (fence == NULL) {
588 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800589 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800590 }
591
592 switch (scalingMode) {
593 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
594 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
595 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
596 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
597 break;
598 default:
599 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800600 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800601 }
602
Dan Stoza8dc55392014-11-04 11:37:46 -0800603 sp<IConsumerListener> frameAvailableListener;
604 sp<IConsumerListener> frameReplacedListener;
605 int callbackTicket = 0;
606 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800607 { // Autolock scope
608 Mutex::Autolock lock(mCore->mMutex);
609
610 if (mCore->mIsAbandoned) {
611 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
612 return NO_INIT;
613 }
614
615 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza289ade12014-02-28 11:17:17 -0800616
617 if (slot < 0 || slot >= maxBufferCount) {
618 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
619 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800620 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800621 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
622 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
623 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800624 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800625 } else if (!mSlots[slot].mRequestBufferCalled) {
626 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
627 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800628 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800629 }
630
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800631 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700632 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800633 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800634 crop.left, crop.top, crop.right, crop.bottom, transform,
635 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800636
637 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
638 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700639 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800640 crop.intersect(bufferRect, &croppedRect);
641 if (croppedRect != crop) {
642 BQ_LOGE("queueBuffer: crop rect is not contained within the "
643 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800644 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800645 }
646
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800647 // Override UNKNOWN dataspace with consumer default
648 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
649 dataSpace = mCore->mDefaultBufferDataSpace;
650 }
651
Dan Stoza289ade12014-02-28 11:17:17 -0800652 mSlots[slot].mFence = fence;
653 mSlots[slot].mBufferState = BufferSlot::QUEUED;
654 ++mCore->mFrameCounter;
655 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
656
Dan Stoza289ade12014-02-28 11:17:17 -0800657 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
658 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
659 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800660 item.mTransform = transform &
661 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800662 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800663 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
664 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800665 item.mTimestamp = timestamp;
666 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800667 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800668 item.mFrameNumber = mCore->mFrameCounter;
669 item.mSlot = slot;
670 item.mFence = fence;
671 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700672 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800673
Ruben Brunk1681d952014-06-27 15:51:55 -0700674 mStickyTransform = stickyTransform;
675
Dan Stoza289ade12014-02-28 11:17:17 -0800676 if (mCore->mQueue.empty()) {
677 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
678 // and simply queue this buffer
679 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800680 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800681 } else {
682 // When the queue is not empty, we need to look at the front buffer
683 // state to see if we need to replace it
684 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
685 if (front->mIsDroppable) {
686 // If the front queued buffer is still being tracked, we first
687 // mark it as freed
688 if (mCore->stillTracking(front)) {
689 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700690 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800691 }
692 // Overwrite the droppable buffer with the incoming one
693 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800694 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800695 } else {
696 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800697 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800698 }
699 }
700
701 mCore->mBufferHasBeenQueued = true;
702 mCore->mDequeueCondition.broadcast();
703
704 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800705 mCore->mTransformHint,
706 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800707
708 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800709
710 // Take a ticket for the callback functions
711 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700712
713 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800714 } // Autolock scope
715
Eric Penner99a0afb2014-09-30 11:28:30 -0700716 // Wait without lock held
717 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
718 // Waiting here allows for two full buffers to be queued but not a
719 // third. In the event that frames take varying time, this makes a
720 // small trade-off in favor of latency rather than throughput.
721 mLastQueueBufferFence->waitForever("Throttling EGL Production");
722 mLastQueueBufferFence = fence;
723 }
724
Dan Stoza8dc55392014-11-04 11:37:46 -0800725 // Don't send the GraphicBuffer through the callback, and don't send
726 // the slot number, since the consumer shouldn't need it
727 item.mGraphicBuffer.clear();
728 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
729
730 // Call back without the main BufferQueue lock held, but with the callback
731 // lock held so we can ensure that callbacks occur in order
732 {
733 Mutex::Autolock lock(mCallbackMutex);
734 while (callbackTicket != mCurrentCallbackTicket) {
735 mCallbackCondition.wait(mCallbackMutex);
736 }
737
738 if (frameAvailableListener != NULL) {
739 frameAvailableListener->onFrameAvailable(item);
740 } else if (frameReplacedListener != NULL) {
741 frameReplacedListener->onFrameReplaced(item);
742 }
743
744 ++mCurrentCallbackTicket;
745 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800746 }
747
748 return NO_ERROR;
749}
750
751void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
752 ATRACE_CALL();
753 BQ_LOGV("cancelBuffer: slot %d", slot);
754 Mutex::Autolock lock(mCore->mMutex);
755
756 if (mCore->mIsAbandoned) {
757 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
758 return;
759 }
760
Dan Stoza3e96f192014-03-03 10:16:19 -0800761 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800762 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800763 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800764 return;
765 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
766 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
767 "(state = %d)", slot, mSlots[slot].mBufferState);
768 return;
769 } else if (fence == NULL) {
770 BQ_LOGE("cancelBuffer: fence is NULL");
771 return;
772 }
773
Dan Stoza0de7ea72015-04-23 13:20:51 -0700774 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800775 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800776 mSlots[slot].mFence = fence;
777 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700778 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800779}
780
781int BufferQueueProducer::query(int what, int *outValue) {
782 ATRACE_CALL();
783 Mutex::Autolock lock(mCore->mMutex);
784
785 if (outValue == NULL) {
786 BQ_LOGE("query: outValue was NULL");
787 return BAD_VALUE;
788 }
789
790 if (mCore->mIsAbandoned) {
791 BQ_LOGE("query: BufferQueue has been abandoned");
792 return NO_INIT;
793 }
794
795 int value;
796 switch (what) {
797 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800798 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800799 break;
800 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800801 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800802 break;
803 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800804 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800805 break;
806 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
807 value = mCore->getMinUndequeuedBufferCountLocked(false);
808 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700809 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800810 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700811 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800812 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
813 value = (mCore->mQueue.size() > 1);
814 break;
815 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800816 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800817 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800818 case NATIVE_WINDOW_DEFAULT_DATASPACE:
819 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
820 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800821 case NATIVE_WINDOW_BUFFER_AGE:
822 if (mCore->mBufferAge > INT32_MAX) {
823 value = 0;
824 } else {
825 value = static_cast<int32_t>(mCore->mBufferAge);
826 }
827 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800828 default:
829 return BAD_VALUE;
830 }
831
832 BQ_LOGV("query: %d? %d", what, value);
833 *outValue = value;
834 return NO_ERROR;
835}
836
Dan Stozaf0eaf252014-03-21 13:05:51 -0700837status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800838 int api, bool producerControlledByApp, QueueBufferOutput *output) {
839 ATRACE_CALL();
840 Mutex::Autolock lock(mCore->mMutex);
841 mConsumerName = mCore->mConsumerName;
842 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
843 producerControlledByApp ? "true" : "false");
844
Dan Stozaae3c3682014-04-18 15:43:35 -0700845 if (mCore->mIsAbandoned) {
846 BQ_LOGE("connect(P): BufferQueue has been abandoned");
847 return NO_INIT;
848 }
Dan Stoza289ade12014-02-28 11:17:17 -0800849
Dan Stozaae3c3682014-04-18 15:43:35 -0700850 if (mCore->mConsumerListener == NULL) {
851 BQ_LOGE("connect(P): BufferQueue has no consumer");
852 return NO_INIT;
853 }
Dan Stoza289ade12014-02-28 11:17:17 -0800854
Dan Stozaae3c3682014-04-18 15:43:35 -0700855 if (output == NULL) {
856 BQ_LOGE("connect(P): output was NULL");
857 return BAD_VALUE;
858 }
Dan Stoza289ade12014-02-28 11:17:17 -0800859
Dan Stozaae3c3682014-04-18 15:43:35 -0700860 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
861 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
862 mCore->mConnectedApi, api);
863 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800864 }
865
866 int status = NO_ERROR;
867 switch (api) {
868 case NATIVE_WINDOW_API_EGL:
869 case NATIVE_WINDOW_API_CPU:
870 case NATIVE_WINDOW_API_MEDIA:
871 case NATIVE_WINDOW_API_CAMERA:
872 mCore->mConnectedApi = api;
873 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800874 mCore->mTransformHint,
875 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800876
877 // Set up a death notification so that we can disconnect
878 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700879 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800880 IInterface::asBinder(listener)->remoteBinder() != NULL) {
881 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800882 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700883 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800884 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
885 strerror(-status), status);
886 }
887 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700888 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800889 break;
890 default:
891 BQ_LOGE("connect(P): unknown API %d", api);
892 status = BAD_VALUE;
893 break;
894 }
895
896 mCore->mBufferHasBeenQueued = false;
897 mCore->mDequeueBufferCannotBlock =
898 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -0700899 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800900
901 return status;
902}
903
904status_t BufferQueueProducer::disconnect(int api) {
905 ATRACE_CALL();
906 BQ_LOGV("disconnect(P): api %d", api);
907
908 int status = NO_ERROR;
909 sp<IConsumerListener> listener;
910 { // Autolock scope
911 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700912 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800913
914 if (mCore->mIsAbandoned) {
915 // It's not really an error to disconnect after the surface has
916 // been abandoned; it should just be a no-op.
917 return NO_ERROR;
918 }
919
920 switch (api) {
921 case NATIVE_WINDOW_API_EGL:
922 case NATIVE_WINDOW_API_CPU:
923 case NATIVE_WINDOW_API_MEDIA:
924 case NATIVE_WINDOW_API_CAMERA:
925 if (mCore->mConnectedApi == api) {
926 mCore->freeAllBuffersLocked();
927
928 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700929 if (mCore->mConnectedProducerListener != NULL) {
930 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800931 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800932 // This can fail if we're here because of the death
933 // notification, but we just ignore it
934 token->unlinkToDeath(
935 static_cast<IBinder::DeathRecipient*>(this));
936 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700937 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800939 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800940 mCore->mDequeueCondition.broadcast();
941 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700942 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
943 BQ_LOGE("disconnect(P): still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800944 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800945 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800946 }
947 break;
948 default:
949 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800950 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800951 break;
952 }
953 } // Autolock scope
954
955 // Call back without lock held
956 if (listener != NULL) {
957 listener->onBuffersReleased();
958 }
959
960 return status;
961}
962
Jesse Hall399184a2014-03-03 15:42:54 -0800963status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900964 sp<IConsumerListener> listener;
965 { // Autolock scope
966 Mutex::Autolock _l(mCore->mMutex);
967 mCore->mSidebandStream = stream;
968 listener = mCore->mConsumerListener;
969 } // Autolock scope
970
971 if (listener != NULL) {
972 listener->onSidebandStreamChanged();
973 }
Jesse Hall399184a2014-03-03 15:42:54 -0800974 return NO_ERROR;
975}
976
Dan Stoza29a3e902014-06-20 13:13:57 -0700977void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800978 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700979 ATRACE_CALL();
980 while (true) {
981 Vector<int> freeSlots;
982 size_t newBufferCount = 0;
983 uint32_t allocWidth = 0;
984 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800985 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700986 uint32_t allocUsage = 0;
987 { // Autolock scope
988 Mutex::Autolock lock(mCore->mMutex);
989 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700990
Dan Stoza9de72932015-04-16 17:28:43 -0700991 if (!mCore->mAllowAllocation) {
992 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
993 "BufferQueue");
994 return;
995 }
996
Antoine Labour78014f32014-07-15 21:17:03 -0700997 int currentBufferCount = 0;
998 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
999 if (mSlots[slot].mGraphicBuffer != NULL) {
1000 ++currentBufferCount;
1001 } else {
1002 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1003 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1004 slot);
1005 continue;
1006 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001007
Antoine Labour11f14872014-07-25 18:14:42 -07001008 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001009 }
1010 }
1011
1012 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
1013 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1014 currentBufferCount, maxBufferCount);
1015 if (maxBufferCount <= currentBufferCount)
1016 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001017 newBufferCount =
1018 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001019 if (freeSlots.size() < newBufferCount) {
1020 BQ_LOGE("allocateBuffers: ran out of free slots");
1021 return;
1022 }
1023 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1024 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1025 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1026 allocUsage = usage | mCore->mConsumerUsageBits;
1027
1028 mCore->mIsAllocating = true;
1029 } // Autolock scope
1030
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001031 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001032 for (size_t i = 0; i < newBufferCount; ++i) {
1033 status_t result = NO_ERROR;
1034 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1035 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1036 if (result != NO_ERROR) {
1037 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1038 " %u, usage %u)", width, height, format, usage);
1039 Mutex::Autolock lock(mCore->mMutex);
1040 mCore->mIsAllocating = false;
1041 mCore->mIsAllocatingCondition.broadcast();
1042 return;
1043 }
1044 buffers.push_back(graphicBuffer);
1045 }
1046
1047 { // Autolock scope
1048 Mutex::Autolock lock(mCore->mMutex);
1049 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1050 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001051 PixelFormat checkFormat = format != 0 ?
1052 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001053 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1054 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1055 checkFormat != allocFormat || checkUsage != allocUsage) {
1056 // Something changed while we released the lock. Retry.
1057 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1058 mCore->mIsAllocating = false;
1059 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001060 continue;
1061 }
1062
Antoine Labour78014f32014-07-15 21:17:03 -07001063 for (size_t i = 0; i < newBufferCount; ++i) {
1064 int slot = freeSlots[i];
1065 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1066 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1067 // allocated.
1068 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1069 "Dropping allocated buffer.", slot);
1070 continue;
1071 }
1072 mCore->freeBufferLocked(slot); // Clean up the slot first
1073 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001074 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001075
1076 // freeBufferLocked puts this slot on the free slots list. Since
1077 // we then attached a buffer, move the slot to free buffer list.
1078 mCore->mFreeSlots.erase(slot);
1079 mCore->mFreeBuffers.push_front(slot);
1080
Antoine Labour78014f32014-07-15 21:17:03 -07001081 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1082 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001083
Antoine Labour78014f32014-07-15 21:17:03 -07001084 mCore->mIsAllocating = false;
1085 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001086 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001087 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001088 }
1089}
1090
Dan Stoza9de72932015-04-16 17:28:43 -07001091status_t BufferQueueProducer::allowAllocation(bool allow) {
1092 ATRACE_CALL();
1093 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1094
1095 Mutex::Autolock lock(mCore->mMutex);
1096 mCore->mAllowAllocation = allow;
1097 return NO_ERROR;
1098}
1099
Dan Stoza812ed062015-06-02 15:45:22 -07001100status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1101 ATRACE_CALL();
1102 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1103
1104 Mutex::Autolock lock(mCore->mMutex);
1105 mCore->mGenerationNumber = generationNumber;
1106 return NO_ERROR;
1107}
1108
Dan Stozac6f30bd2015-06-08 09:32:50 -07001109String8 BufferQueueProducer::getConsumerName() const {
1110 ATRACE_CALL();
1111 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1112 return mConsumerName;
1113}
1114
Dan Stoza289ade12014-02-28 11:17:17 -08001115void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1116 // If we're here, it means that a producer we were connected to died.
1117 // We're guaranteed that we are still connected to it because we remove
1118 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1119 // without synchronization here.
1120 int api = mCore->mConnectedApi;
1121 disconnect(api);
1122}
1123
1124} // namespace android