blob: 06cdeab4c9fdc71b01aac5051aa8d7f81d7369a9 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Dan Stoza289ade12014-02-28 11:17:17 -080023#define EGL_EGLEXT_PROTOTYPES
24
25#include <gui/BufferItem.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
28#include <gui/IConsumerListener.h>
29#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080031
32#include <utils/Log.h>
33#include <utils/Trace.h>
34
35namespace android {
36
37BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
38 mCore(core),
39 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070040 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070041 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080042 mLastQueueBufferFence(Fence::NO_FENCE),
43 mCallbackMutex(),
44 mNextCallbackTicket(0),
45 mCurrentCallbackTicket(0),
46 mCallbackCondition() {}
Dan Stoza289ade12014-02-28 11:17:17 -080047
48BufferQueueProducer::~BufferQueueProducer() {}
49
50status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
51 ATRACE_CALL();
52 BQ_LOGV("requestBuffer: slot %d", slot);
53 Mutex::Autolock lock(mCore->mMutex);
54
55 if (mCore->mIsAbandoned) {
56 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
57 return NO_INIT;
58 }
59
Pablo Ceballos583b1b32015-09-03 18:23:52 -070060 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
61 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
62 return NO_INIT;
63 }
64
Dan Stoza3e96f192014-03-03 10:16:19 -080065 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080066 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080067 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080068 return BAD_VALUE;
69 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
70 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
71 "(state = %d)", slot, mSlots[slot].mBufferState);
72 return BAD_VALUE;
73 }
74
75 mSlots[slot].mRequestBufferCalled = true;
76 *buf = mSlots[slot].mGraphicBuffer;
77 return NO_ERROR;
78}
79
Pablo Ceballosfa455352015-08-12 17:47:47 -070080status_t BufferQueueProducer::setMaxDequeuedBufferCount(
81 int maxDequeuedBuffers) {
82 ATRACE_CALL();
83 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
84 maxDequeuedBuffers);
85
86 sp<IConsumerListener> listener;
87 { // Autolock scope
88 Mutex::Autolock lock(mCore->mMutex);
89 mCore->waitWhileAllocatingLocked();
90
91 if (mCore->mIsAbandoned) {
92 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
93 "abandoned");
94 return NO_INIT;
95 }
96
97 // There must be no dequeued buffers when changing the buffer count.
98 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
99 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
100 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
101 return BAD_VALUE;
102 }
103 }
104
105 int bufferCount = mCore->getMinUndequeuedBufferCountLocked(
106 mCore->mAsyncMode);
107 bufferCount += maxDequeuedBuffers;
108
109 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
110 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
111 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
112 return BAD_VALUE;
113 }
114
115 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(
116 mCore->mAsyncMode);
117 if (bufferCount < minBufferSlots) {
118 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
119 "less than minimum %d", bufferCount, minBufferSlots);
120 return BAD_VALUE;
121 }
122
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700123 if (bufferCount > mCore->mMaxBufferCount) {
124 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
125 "exceed the maxBufferCount (%d) (maxAcquired %d async %d)",
126 maxDequeuedBuffers, mCore->mMaxBufferCount,
127 mCore->mMaxAcquiredBufferCount, mCore->mAsyncMode);
128 return BAD_VALUE;
129 }
130
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131 // Here we are guaranteed that the producer doesn't have any dequeued
132 // buffers and will release all of its buffer references. We don't
133 // clear the queue, however, so that currently queued buffers still
134 // get displayed.
135 mCore->freeAllBuffersLocked();
136 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137 mCore->mDequeueCondition.broadcast();
138 listener = mCore->mConsumerListener;
139 } // Autolock scope
140
141 // Call back without lock held
142 if (listener != NULL) {
143 listener->onBuffersReleased();
144 }
145
146 return NO_ERROR;
147}
148
149status_t BufferQueueProducer::setAsyncMode(bool async) {
150 ATRACE_CALL();
151 BQ_LOGV("setAsyncMode: async = %d", async);
152
153 sp<IConsumerListener> listener;
154 { // Autolock scope
155 Mutex::Autolock lock(mCore->mMutex);
156 mCore->waitWhileAllocatingLocked();
157
158 if (mCore->mIsAbandoned) {
159 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
160 return NO_INIT;
161 }
162
163 // There must be no dequeued buffers when changing the async mode.
164 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
165 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
166 BQ_LOGE("setAsyncMode: buffer owned by producer");
167 return BAD_VALUE;
168 }
169 }
170
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700171 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
172 (async ? 1 : 0)) > mCore->mMaxBufferCount) {
173 BQ_LOGE("setAsyncMode(%d): this call would cause the "
174 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
175 "maxDequeued %d)", async,mCore->mMaxBufferCount,
176 mCore->mMaxAcquiredBufferCount,
177 mCore->mMaxDequeuedBufferCount);
178 return BAD_VALUE;
179 }
180
Pablo Ceballosfa455352015-08-12 17:47:47 -0700181 mCore->mAsyncMode = async;
182 mCore->mDequeueCondition.broadcast();
183 listener = mCore->mConsumerListener;
184 } // Autolock scope
185
186 // Call back without lock held
187 if (listener != NULL) {
188 listener->onBuffersReleased();
189 }
190 return NO_ERROR;
191}
192
Dan Stoza9f3053d2014-03-06 15:14:33 -0800193status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
194 bool async, int* found, status_t* returnFlags) const {
195 bool tryAgain = true;
196 while (tryAgain) {
197 if (mCore->mIsAbandoned) {
198 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
199 return NO_INIT;
200 }
201
202 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800203
204 // Free up any buffers that are in slots beyond the max buffer count
205 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
206 assert(mSlots[s].mBufferState == BufferSlot::FREE);
207 if (mSlots[s].mGraphicBuffer != NULL) {
208 mCore->freeBufferLocked(s);
209 *returnFlags |= RELEASE_ALL_BUFFERS;
210 }
211 }
212
Dan Stoza9f3053d2014-03-06 15:14:33 -0800213 int dequeuedCount = 0;
214 int acquiredCount = 0;
215 for (int s = 0; s < maxBufferCount; ++s) {
216 switch (mSlots[s].mBufferState) {
217 case BufferSlot::DEQUEUED:
218 ++dequeuedCount;
219 break;
220 case BufferSlot::ACQUIRED:
221 ++acquiredCount;
222 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800223 default:
224 break;
225 }
226 }
227
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700228 // Producers are not allowed to dequeue more than
229 // mMaxDequeuedBufferCount buffers.
230 // This check is only done if a buffer has already been queued
231 if (mCore->mBufferHasBeenQueued &&
232 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
233 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
234 "(%d)", caller, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800235 return INVALID_OPERATION;
236 }
237
Dan Stoza0de7ea72015-04-23 13:20:51 -0700238 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
239
Dan Stozaae3c3682014-04-18 15:43:35 -0700240 // If we disconnect and reconnect quickly, we can be in a state where
241 // our slots are empty but we have many buffers in the queue. This can
242 // cause us to run out of memory if we outrun the consumer. Wait here if
243 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700244 bool tooManyBuffers = mCore->mQueue.size()
245 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700246 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700247 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700248 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700249 } else {
250 if (!mCore->mFreeBuffers.empty()) {
251 auto slot = mCore->mFreeBuffers.begin();
252 *found = *slot;
253 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700254 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700255 auto slot = mCore->mFreeSlots.begin();
256 // Only return free slots up to the max buffer count
257 if (*slot < maxBufferCount) {
258 *found = *slot;
259 mCore->mFreeSlots.erase(slot);
260 }
261 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700262 }
263
264 // If no buffer is found, or if the queue has too many buffers
265 // outstanding, wait for a buffer to be acquired or released, or for the
266 // max buffer count to change.
267 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
268 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800269 if (tryAgain) {
270 // Return an error if we're in non-blocking mode (producer and
271 // consumer are controlled by the application).
272 // However, the consumer is allowed to briefly acquire an extra
273 // buffer (which could cause us to have to wait here), which is
274 // okay, since it is only used to implement an atomic acquire +
275 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700276 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
278 return WOULD_BLOCK;
279 }
280 mCore->mDequeueCondition.wait(mCore->mMutex);
281 }
282 } // while (tryAgain)
283
284 return NO_ERROR;
285}
286
Dan Stoza289ade12014-02-28 11:17:17 -0800287status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
288 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800289 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800290 ATRACE_CALL();
291 { // Autolock scope
292 Mutex::Autolock lock(mCore->mMutex);
293 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700294
295 if (mCore->mIsAbandoned) {
296 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
297 return NO_INIT;
298 }
299
300 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
301 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
302 return NO_INIT;
303 }
Dan Stoza289ade12014-02-28 11:17:17 -0800304 } // Autolock scope
305
306 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
307 async ? "true" : "false", width, height, format, usage);
308
309 if ((width && !height) || (!width && height)) {
310 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
311 return BAD_VALUE;
312 }
313
314 status_t returnFlags = NO_ERROR;
315 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
316 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800317 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800318
319 { // Autolock scope
320 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700321 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800322
323 if (format == 0) {
324 format = mCore->mDefaultBufferFormat;
325 }
326
327 // Enable the usage bits the consumer requested
328 usage |= mCore->mConsumerUsageBits;
329
Dan Stoza9de72932015-04-16 17:28:43 -0700330 const bool useDefaultSize = !width && !height;
331 if (useDefaultSize) {
332 width = mCore->mDefaultWidth;
333 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800334 }
Dan Stoza289ade12014-02-28 11:17:17 -0800335
Dan Stoza9de72932015-04-16 17:28:43 -0700336 int found = BufferItem::INVALID_BUFFER_SLOT;
337 while (found == BufferItem::INVALID_BUFFER_SLOT) {
338 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
339 &found, &returnFlags);
340 if (status != NO_ERROR) {
341 return status;
342 }
343
344 // This should not happen
345 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
346 BQ_LOGE("dequeueBuffer: no available buffer slots");
347 return -EBUSY;
348 }
349
350 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
351
352 // If we are not allowed to allocate new buffers,
353 // waitForFreeSlotThenRelock must have returned a slot containing a
354 // buffer. If this buffer would require reallocation to meet the
355 // requested attributes, we free it and attempt to get another one.
356 if (!mCore->mAllowAllocation) {
357 if (buffer->needsReallocation(width, height, format, usage)) {
358 mCore->freeBufferLocked(found);
359 found = BufferItem::INVALID_BUFFER_SLOT;
360 continue;
361 }
362 }
Dan Stoza289ade12014-02-28 11:17:17 -0800363 }
364
365 *outSlot = found;
366 ATRACE_BUFFER_INDEX(found);
367
Dan Stoza9f3053d2014-03-06 15:14:33 -0800368 attachedByConsumer = mSlots[found].mAttachedByConsumer;
369
Dan Stoza289ade12014-02-28 11:17:17 -0800370 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
371
372 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
373 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700374 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800375 {
376 mSlots[found].mAcquireCalled = false;
377 mSlots[found].mGraphicBuffer = NULL;
378 mSlots[found].mRequestBufferCalled = false;
379 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
380 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
381 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800382 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800383
384 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800385 } else {
386 // We add 1 because that will be the frame number when this buffer
387 // is queued
388 mCore->mBufferAge =
389 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800390 }
391
Dan Stoza800b41a2015-04-28 14:20:04 -0700392 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
393 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800394
Dan Stoza289ade12014-02-28 11:17:17 -0800395 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
396 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
397 "slot=%d w=%d h=%d format=%u",
398 found, buffer->width, buffer->height, buffer->format);
399 }
400
401 eglDisplay = mSlots[found].mEglDisplay;
402 eglFence = mSlots[found].mEglFence;
403 *outFence = mSlots[found].mFence;
404 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
405 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700406
407 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800408 } // Autolock scope
409
410 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
411 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700412 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800413 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800414 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800415 if (graphicBuffer == NULL) {
416 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
417 return error;
418 }
419
420 { // Autolock scope
421 Mutex::Autolock lock(mCore->mMutex);
422
423 if (mCore->mIsAbandoned) {
424 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
425 return NO_INIT;
426 }
427
Dan Stoza812ed062015-06-02 15:45:22 -0700428 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800429 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
430 } // Autolock scope
431 }
432
Dan Stoza9f3053d2014-03-06 15:14:33 -0800433 if (attachedByConsumer) {
434 returnFlags |= BUFFER_NEEDS_REALLOCATION;
435 }
436
Dan Stoza289ade12014-02-28 11:17:17 -0800437 if (eglFence != EGL_NO_SYNC_KHR) {
438 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
439 1000000000);
440 // If something goes wrong, log the error, but return the buffer without
441 // synchronizing access to it. It's too late at this point to abort the
442 // dequeue operation.
443 if (result == EGL_FALSE) {
444 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
445 eglGetError());
446 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
447 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
448 }
449 eglDestroySyncKHR(eglDisplay, eglFence);
450 }
451
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700452 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
453 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800454 mSlots[*outSlot].mFrameNumber,
455 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
456
457 return returnFlags;
458}
459
Dan Stoza9f3053d2014-03-06 15:14:33 -0800460status_t BufferQueueProducer::detachBuffer(int slot) {
461 ATRACE_CALL();
462 ATRACE_BUFFER_INDEX(slot);
463 BQ_LOGV("detachBuffer(P): slot %d", slot);
464 Mutex::Autolock lock(mCore->mMutex);
465
466 if (mCore->mIsAbandoned) {
467 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
468 return NO_INIT;
469 }
470
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700471 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
472 BQ_LOGE("detachBuffer(P): BufferQueue has no connected producer");
473 return NO_INIT;
474 }
475
Dan Stoza9f3053d2014-03-06 15:14:33 -0800476 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
477 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
478 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
479 return BAD_VALUE;
480 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
481 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
482 "(state = %d)", slot, mSlots[slot].mBufferState);
483 return BAD_VALUE;
484 } else if (!mSlots[slot].mRequestBufferCalled) {
485 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
486 slot);
487 return BAD_VALUE;
488 }
489
490 mCore->freeBufferLocked(slot);
491 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700492 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800493
494 return NO_ERROR;
495}
496
Dan Stozad9822a32014-03-28 15:25:31 -0700497status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
498 sp<Fence>* outFence) {
499 ATRACE_CALL();
500
501 if (outBuffer == NULL) {
502 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
503 return BAD_VALUE;
504 } else if (outFence == NULL) {
505 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
506 return BAD_VALUE;
507 }
508
509 Mutex::Autolock lock(mCore->mMutex);
510
511 if (mCore->mIsAbandoned) {
512 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
513 return NO_INIT;
514 }
515
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700516 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
517 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
518 return NO_INIT;
519 }
520
521 mCore->waitWhileAllocatingLocked();
522
Dan Stoza0de7ea72015-04-23 13:20:51 -0700523 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000524 return NO_MEMORY;
525 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700526
Dan Stoza0de7ea72015-04-23 13:20:51 -0700527 int found = mCore->mFreeBuffers.front();
528 mCore->mFreeBuffers.remove(found);
529
Dan Stozad9822a32014-03-28 15:25:31 -0700530 BQ_LOGV("detachNextBuffer detached slot %d", found);
531
532 *outBuffer = mSlots[found].mGraphicBuffer;
533 *outFence = mSlots[found].mFence;
534 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700535 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700536
537 return NO_ERROR;
538}
539
Dan Stoza9f3053d2014-03-06 15:14:33 -0800540status_t BufferQueueProducer::attachBuffer(int* outSlot,
541 const sp<android::GraphicBuffer>& buffer) {
542 ATRACE_CALL();
543
544 if (outSlot == NULL) {
545 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
546 return BAD_VALUE;
547 } else if (buffer == NULL) {
548 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
549 return BAD_VALUE;
550 }
551
552 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700553
554 if (mCore->mIsAbandoned) {
555 BQ_LOGE("attachBuffer(P): BufferQueue has been abandoned");
556 return NO_INIT;
557 }
558
559 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
560 BQ_LOGE("attachBuffer(P): BufferQueue has no connected producer");
561 return NO_INIT;
562 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800563
Dan Stoza812ed062015-06-02 15:45:22 -0700564 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
565 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
566 "[queue %u]", buffer->getGenerationNumber(),
567 mCore->mGenerationNumber);
568 return BAD_VALUE;
569 }
570
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700571 mCore->waitWhileAllocatingLocked();
572
Dan Stoza9f3053d2014-03-06 15:14:33 -0800573 status_t returnFlags = NO_ERROR;
574 int found;
575 // TODO: Should we provide an async flag to attachBuffer? It seems
576 // unlikely that buffers which we are attaching to a BufferQueue will
577 // be asynchronous (droppable), but it may not be impossible.
578 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
579 &found, &returnFlags);
580 if (status != NO_ERROR) {
581 return status;
582 }
583
584 // This should not happen
585 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
586 BQ_LOGE("attachBuffer(P): no available buffer slots");
587 return -EBUSY;
588 }
589
590 *outSlot = found;
591 ATRACE_BUFFER_INDEX(*outSlot);
592 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
593 *outSlot, returnFlags);
594
595 mSlots[*outSlot].mGraphicBuffer = buffer;
596 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
597 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
598 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700599 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800600
Dan Stoza0de7ea72015-04-23 13:20:51 -0700601 mCore->validateConsistencyLocked();
602
Dan Stoza9f3053d2014-03-06 15:14:33 -0800603 return returnFlags;
604}
605
Dan Stoza289ade12014-02-28 11:17:17 -0800606status_t BufferQueueProducer::queueBuffer(int slot,
607 const QueueBufferInput &input, QueueBufferOutput *output) {
608 ATRACE_CALL();
609 ATRACE_BUFFER_INDEX(slot);
610
611 int64_t timestamp;
612 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800613 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700614 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800615 int scalingMode;
616 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700617 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800618 bool async;
619 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800620 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
621 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700622 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800623
624 if (fence == NULL) {
625 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800626 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800627 }
628
629 switch (scalingMode) {
630 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
631 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
632 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
633 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
634 break;
635 default:
636 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800637 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800638 }
639
Dan Stoza8dc55392014-11-04 11:37:46 -0800640 sp<IConsumerListener> frameAvailableListener;
641 sp<IConsumerListener> frameReplacedListener;
642 int callbackTicket = 0;
643 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800644 { // Autolock scope
645 Mutex::Autolock lock(mCore->mMutex);
646
647 if (mCore->mIsAbandoned) {
648 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
649 return NO_INIT;
650 }
651
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700652 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
653 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
654 return NO_INIT;
655 }
656
Dan Stoza289ade12014-02-28 11:17:17 -0800657 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza289ade12014-02-28 11:17:17 -0800658
659 if (slot < 0 || slot >= maxBufferCount) {
660 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
661 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800662 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800663 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
664 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
665 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800666 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800667 } else if (!mSlots[slot].mRequestBufferCalled) {
668 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
669 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800670 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800671 }
672
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800673 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700674 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800675 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800676 crop.left, crop.top, crop.right, crop.bottom, transform,
677 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800678
679 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
680 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700681 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800682 crop.intersect(bufferRect, &croppedRect);
683 if (croppedRect != crop) {
684 BQ_LOGE("queueBuffer: crop rect is not contained within the "
685 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800687 }
688
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800689 // Override UNKNOWN dataspace with consumer default
690 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
691 dataSpace = mCore->mDefaultBufferDataSpace;
692 }
693
Dan Stoza289ade12014-02-28 11:17:17 -0800694 mSlots[slot].mFence = fence;
695 mSlots[slot].mBufferState = BufferSlot::QUEUED;
696 ++mCore->mFrameCounter;
697 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
698
Dan Stoza289ade12014-02-28 11:17:17 -0800699 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
700 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
701 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800702 item.mTransform = transform &
703 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800704 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800705 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
706 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800707 item.mTimestamp = timestamp;
708 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800709 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800710 item.mFrameNumber = mCore->mFrameCounter;
711 item.mSlot = slot;
712 item.mFence = fence;
713 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700714 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800715
Ruben Brunk1681d952014-06-27 15:51:55 -0700716 mStickyTransform = stickyTransform;
717
Dan Stoza289ade12014-02-28 11:17:17 -0800718 if (mCore->mQueue.empty()) {
719 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
720 // and simply queue this buffer
721 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800722 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800723 } else {
724 // When the queue is not empty, we need to look at the front buffer
725 // state to see if we need to replace it
726 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
727 if (front->mIsDroppable) {
728 // If the front queued buffer is still being tracked, we first
729 // mark it as freed
730 if (mCore->stillTracking(front)) {
731 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700732 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800733 }
734 // Overwrite the droppable buffer with the incoming one
735 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800736 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800737 } else {
738 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800739 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800740 }
741 }
742
743 mCore->mBufferHasBeenQueued = true;
744 mCore->mDequeueCondition.broadcast();
745
746 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800747 mCore->mTransformHint,
748 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800749
750 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800751
752 // Take a ticket for the callback functions
753 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700754
755 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800756 } // Autolock scope
757
Eric Penner99a0afb2014-09-30 11:28:30 -0700758 // Wait without lock held
759 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
760 // Waiting here allows for two full buffers to be queued but not a
761 // third. In the event that frames take varying time, this makes a
762 // small trade-off in favor of latency rather than throughput.
763 mLastQueueBufferFence->waitForever("Throttling EGL Production");
764 mLastQueueBufferFence = fence;
765 }
766
Dan Stoza8dc55392014-11-04 11:37:46 -0800767 // Don't send the GraphicBuffer through the callback, and don't send
768 // the slot number, since the consumer shouldn't need it
769 item.mGraphicBuffer.clear();
770 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
771
772 // Call back without the main BufferQueue lock held, but with the callback
773 // lock held so we can ensure that callbacks occur in order
774 {
775 Mutex::Autolock lock(mCallbackMutex);
776 while (callbackTicket != mCurrentCallbackTicket) {
777 mCallbackCondition.wait(mCallbackMutex);
778 }
779
780 if (frameAvailableListener != NULL) {
781 frameAvailableListener->onFrameAvailable(item);
782 } else if (frameReplacedListener != NULL) {
783 frameReplacedListener->onFrameReplaced(item);
784 }
785
786 ++mCurrentCallbackTicket;
787 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800788 }
789
790 return NO_ERROR;
791}
792
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700793status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800794 ATRACE_CALL();
795 BQ_LOGV("cancelBuffer: slot %d", slot);
796 Mutex::Autolock lock(mCore->mMutex);
797
798 if (mCore->mIsAbandoned) {
799 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700800 return NO_INIT;
801 }
802
803 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
804 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
805 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800806 }
807
Dan Stoza3e96f192014-03-03 10:16:19 -0800808 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800809 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800810 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700811 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800812 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
813 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
814 "(state = %d)", slot, mSlots[slot].mBufferState);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700815 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800816 } else if (fence == NULL) {
817 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700818 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800819 }
820
Dan Stoza0de7ea72015-04-23 13:20:51 -0700821 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800822 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800823 mSlots[slot].mFence = fence;
824 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700825 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700826
827 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800828}
829
830int BufferQueueProducer::query(int what, int *outValue) {
831 ATRACE_CALL();
832 Mutex::Autolock lock(mCore->mMutex);
833
834 if (outValue == NULL) {
835 BQ_LOGE("query: outValue was NULL");
836 return BAD_VALUE;
837 }
838
839 if (mCore->mIsAbandoned) {
840 BQ_LOGE("query: BufferQueue has been abandoned");
841 return NO_INIT;
842 }
843
844 int value;
845 switch (what) {
846 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800848 break;
849 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800850 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800851 break;
852 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800853 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800854 break;
855 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
856 value = mCore->getMinUndequeuedBufferCountLocked(false);
857 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700858 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800859 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700860 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800861 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
862 value = (mCore->mQueue.size() > 1);
863 break;
864 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800865 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800866 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800867 case NATIVE_WINDOW_DEFAULT_DATASPACE:
868 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
869 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800870 case NATIVE_WINDOW_BUFFER_AGE:
871 if (mCore->mBufferAge > INT32_MAX) {
872 value = 0;
873 } else {
874 value = static_cast<int32_t>(mCore->mBufferAge);
875 }
876 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800877 default:
878 return BAD_VALUE;
879 }
880
881 BQ_LOGV("query: %d? %d", what, value);
882 *outValue = value;
883 return NO_ERROR;
884}
885
Dan Stozaf0eaf252014-03-21 13:05:51 -0700886status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800887 int api, bool producerControlledByApp, QueueBufferOutput *output) {
888 ATRACE_CALL();
889 Mutex::Autolock lock(mCore->mMutex);
890 mConsumerName = mCore->mConsumerName;
891 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
892 producerControlledByApp ? "true" : "false");
893
Dan Stozaae3c3682014-04-18 15:43:35 -0700894 if (mCore->mIsAbandoned) {
895 BQ_LOGE("connect(P): BufferQueue has been abandoned");
896 return NO_INIT;
897 }
Dan Stoza289ade12014-02-28 11:17:17 -0800898
Dan Stozaae3c3682014-04-18 15:43:35 -0700899 if (mCore->mConsumerListener == NULL) {
900 BQ_LOGE("connect(P): BufferQueue has no consumer");
901 return NO_INIT;
902 }
Dan Stoza289ade12014-02-28 11:17:17 -0800903
Dan Stozaae3c3682014-04-18 15:43:35 -0700904 if (output == NULL) {
905 BQ_LOGE("connect(P): output was NULL");
906 return BAD_VALUE;
907 }
Dan Stoza289ade12014-02-28 11:17:17 -0800908
Dan Stozaae3c3682014-04-18 15:43:35 -0700909 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
910 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
911 mCore->mConnectedApi, api);
912 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800913 }
914
915 int status = NO_ERROR;
916 switch (api) {
917 case NATIVE_WINDOW_API_EGL:
918 case NATIVE_WINDOW_API_CPU:
919 case NATIVE_WINDOW_API_MEDIA:
920 case NATIVE_WINDOW_API_CAMERA:
921 mCore->mConnectedApi = api;
922 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800923 mCore->mTransformHint,
924 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800925
926 // Set up a death notification so that we can disconnect
927 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700928 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800929 IInterface::asBinder(listener)->remoteBinder() != NULL) {
930 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800931 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700932 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800933 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
934 strerror(-status), status);
935 }
936 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700937 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 break;
939 default:
940 BQ_LOGE("connect(P): unknown API %d", api);
941 status = BAD_VALUE;
942 break;
943 }
944
945 mCore->mBufferHasBeenQueued = false;
946 mCore->mDequeueBufferCannotBlock =
947 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -0700948 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800949
950 return status;
951}
952
953status_t BufferQueueProducer::disconnect(int api) {
954 ATRACE_CALL();
955 BQ_LOGV("disconnect(P): api %d", api);
956
957 int status = NO_ERROR;
958 sp<IConsumerListener> listener;
959 { // Autolock scope
960 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700961 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800962
963 if (mCore->mIsAbandoned) {
964 // It's not really an error to disconnect after the surface has
965 // been abandoned; it should just be a no-op.
966 return NO_ERROR;
967 }
968
969 switch (api) {
970 case NATIVE_WINDOW_API_EGL:
971 case NATIVE_WINDOW_API_CPU:
972 case NATIVE_WINDOW_API_MEDIA:
973 case NATIVE_WINDOW_API_CAMERA:
974 if (mCore->mConnectedApi == api) {
975 mCore->freeAllBuffersLocked();
976
977 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700978 if (mCore->mConnectedProducerListener != NULL) {
979 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800980 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800981 // This can fail if we're here because of the death
982 // notification, but we just ignore it
983 token->unlinkToDeath(
984 static_cast<IBinder::DeathRecipient*>(this));
985 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700986 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800987 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800988 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800989 mCore->mDequeueCondition.broadcast();
990 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700991 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
992 BQ_LOGE("disconnect(P): still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800993 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800994 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800995 }
996 break;
997 default:
998 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800999 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001000 break;
1001 }
1002 } // Autolock scope
1003
1004 // Call back without lock held
1005 if (listener != NULL) {
1006 listener->onBuffersReleased();
1007 }
1008
1009 return status;
1010}
1011
Jesse Hall399184a2014-03-03 15:42:54 -08001012status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001013 sp<IConsumerListener> listener;
1014 { // Autolock scope
1015 Mutex::Autolock _l(mCore->mMutex);
1016 mCore->mSidebandStream = stream;
1017 listener = mCore->mConsumerListener;
1018 } // Autolock scope
1019
1020 if (listener != NULL) {
1021 listener->onSidebandStreamChanged();
1022 }
Jesse Hall399184a2014-03-03 15:42:54 -08001023 return NO_ERROR;
1024}
1025
Dan Stoza29a3e902014-06-20 13:13:57 -07001026void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001027 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001028 ATRACE_CALL();
1029 while (true) {
1030 Vector<int> freeSlots;
1031 size_t newBufferCount = 0;
1032 uint32_t allocWidth = 0;
1033 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001034 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001035 uint32_t allocUsage = 0;
1036 { // Autolock scope
1037 Mutex::Autolock lock(mCore->mMutex);
1038 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001039
Dan Stoza9de72932015-04-16 17:28:43 -07001040 if (!mCore->mAllowAllocation) {
1041 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1042 "BufferQueue");
1043 return;
1044 }
1045
Antoine Labour78014f32014-07-15 21:17:03 -07001046 int currentBufferCount = 0;
1047 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1048 if (mSlots[slot].mGraphicBuffer != NULL) {
1049 ++currentBufferCount;
1050 } else {
1051 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1052 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1053 slot);
1054 continue;
1055 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001056
Antoine Labour11f14872014-07-25 18:14:42 -07001057 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001058 }
1059 }
1060
1061 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
1062 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1063 currentBufferCount, maxBufferCount);
1064 if (maxBufferCount <= currentBufferCount)
1065 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001066 newBufferCount =
1067 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001068 if (freeSlots.size() < newBufferCount) {
1069 BQ_LOGE("allocateBuffers: ran out of free slots");
1070 return;
1071 }
1072 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1073 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1074 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1075 allocUsage = usage | mCore->mConsumerUsageBits;
1076
1077 mCore->mIsAllocating = true;
1078 } // Autolock scope
1079
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001080 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001081 for (size_t i = 0; i < newBufferCount; ++i) {
1082 status_t result = NO_ERROR;
1083 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1084 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1085 if (result != NO_ERROR) {
1086 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1087 " %u, usage %u)", width, height, format, usage);
1088 Mutex::Autolock lock(mCore->mMutex);
1089 mCore->mIsAllocating = false;
1090 mCore->mIsAllocatingCondition.broadcast();
1091 return;
1092 }
1093 buffers.push_back(graphicBuffer);
1094 }
1095
1096 { // Autolock scope
1097 Mutex::Autolock lock(mCore->mMutex);
1098 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1099 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001100 PixelFormat checkFormat = format != 0 ?
1101 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001102 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1103 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1104 checkFormat != allocFormat || checkUsage != allocUsage) {
1105 // Something changed while we released the lock. Retry.
1106 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1107 mCore->mIsAllocating = false;
1108 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001109 continue;
1110 }
1111
Antoine Labour78014f32014-07-15 21:17:03 -07001112 for (size_t i = 0; i < newBufferCount; ++i) {
1113 int slot = freeSlots[i];
1114 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1115 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1116 // allocated.
1117 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1118 "Dropping allocated buffer.", slot);
1119 continue;
1120 }
1121 mCore->freeBufferLocked(slot); // Clean up the slot first
1122 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001123 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001124
1125 // freeBufferLocked puts this slot on the free slots list. Since
1126 // we then attached a buffer, move the slot to free buffer list.
1127 mCore->mFreeSlots.erase(slot);
1128 mCore->mFreeBuffers.push_front(slot);
1129
Antoine Labour78014f32014-07-15 21:17:03 -07001130 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1131 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001132
Antoine Labour78014f32014-07-15 21:17:03 -07001133 mCore->mIsAllocating = false;
1134 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001135 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001136 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001137 }
1138}
1139
Dan Stoza9de72932015-04-16 17:28:43 -07001140status_t BufferQueueProducer::allowAllocation(bool allow) {
1141 ATRACE_CALL();
1142 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1143
1144 Mutex::Autolock lock(mCore->mMutex);
1145 mCore->mAllowAllocation = allow;
1146 return NO_ERROR;
1147}
1148
Dan Stoza812ed062015-06-02 15:45:22 -07001149status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1150 ATRACE_CALL();
1151 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1152
1153 Mutex::Autolock lock(mCore->mMutex);
1154 mCore->mGenerationNumber = generationNumber;
1155 return NO_ERROR;
1156}
1157
Dan Stozac6f30bd2015-06-08 09:32:50 -07001158String8 BufferQueueProducer::getConsumerName() const {
1159 ATRACE_CALL();
1160 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1161 return mConsumerName;
1162}
1163
Dan Stoza289ade12014-02-28 11:17:17 -08001164void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1165 // If we're here, it means that a producer we were connected to died.
1166 // We're guaranteed that we are still connected to it because we remove
1167 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1168 // without synchronization here.
1169 int api = mCore->mConnectedApi;
1170 disconnect(api);
1171}
1172
1173} // namespace android