blob: 9ef8ff715f2d6b7d11d85c58b18f2ab742525f94 [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
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700105 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700106 bufferCount += maxDequeuedBuffers;
107
108 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
109 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
110 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
111 return BAD_VALUE;
112 }
113
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700114 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700115 if (bufferCount < minBufferSlots) {
116 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
117 "less than minimum %d", bufferCount, minBufferSlots);
118 return BAD_VALUE;
119 }
120
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700121 if (bufferCount > mCore->mMaxBufferCount) {
122 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700123 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
124 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
125 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
126 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700127 return BAD_VALUE;
128 }
129
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130 // Here we are guaranteed that the producer doesn't have any dequeued
131 // buffers and will release all of its buffer references. We don't
132 // clear the queue, however, so that currently queued buffers still
133 // get displayed.
134 mCore->freeAllBuffersLocked();
135 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700136 mCore->mDequeueCondition.broadcast();
137 listener = mCore->mConsumerListener;
138 } // Autolock scope
139
140 // Call back without lock held
141 if (listener != NULL) {
142 listener->onBuffersReleased();
143 }
144
145 return NO_ERROR;
146}
147
148status_t BufferQueueProducer::setAsyncMode(bool async) {
149 ATRACE_CALL();
150 BQ_LOGV("setAsyncMode: async = %d", async);
151
152 sp<IConsumerListener> listener;
153 { // Autolock scope
154 Mutex::Autolock lock(mCore->mMutex);
155 mCore->waitWhileAllocatingLocked();
156
157 if (mCore->mIsAbandoned) {
158 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
159 return NO_INIT;
160 }
161
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700162 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700163 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
164 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700165 BQ_LOGE("setAsyncMode(%d): this call would cause the "
166 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700167 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
168 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
169 mCore->mMaxDequeuedBufferCount,
170 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700171 return BAD_VALUE;
172 }
173
Pablo Ceballosfa455352015-08-12 17:47:47 -0700174 mCore->mAsyncMode = async;
175 mCore->mDequeueCondition.broadcast();
176 listener = mCore->mConsumerListener;
177 } // Autolock scope
178
179 // Call back without lock held
180 if (listener != NULL) {
181 listener->onBuffersReleased();
182 }
183 return NO_ERROR;
184}
185
Dan Stoza9f3053d2014-03-06 15:14:33 -0800186status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700187 int* found, status_t* returnFlags) const {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800188 bool tryAgain = true;
189 while (tryAgain) {
190 if (mCore->mIsAbandoned) {
191 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
192 return NO_INIT;
193 }
194
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700195 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800196
197 // Free up any buffers that are in slots beyond the max buffer count
198 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
199 assert(mSlots[s].mBufferState == BufferSlot::FREE);
200 if (mSlots[s].mGraphicBuffer != NULL) {
201 mCore->freeBufferLocked(s);
202 *returnFlags |= RELEASE_ALL_BUFFERS;
203 }
204 }
205
Dan Stoza9f3053d2014-03-06 15:14:33 -0800206 int dequeuedCount = 0;
207 int acquiredCount = 0;
208 for (int s = 0; s < maxBufferCount; ++s) {
209 switch (mSlots[s].mBufferState) {
210 case BufferSlot::DEQUEUED:
211 ++dequeuedCount;
212 break;
213 case BufferSlot::ACQUIRED:
214 ++acquiredCount;
215 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800216 default:
217 break;
218 }
219 }
220
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700221 // Producers are not allowed to dequeue more than
222 // mMaxDequeuedBufferCount buffers.
223 // This check is only done if a buffer has already been queued
224 if (mCore->mBufferHasBeenQueued &&
225 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
226 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
227 "(%d)", caller, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800228 return INVALID_OPERATION;
229 }
230
Dan Stoza0de7ea72015-04-23 13:20:51 -0700231 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
232
Dan Stozaae3c3682014-04-18 15:43:35 -0700233 // If we disconnect and reconnect quickly, we can be in a state where
234 // our slots are empty but we have many buffers in the queue. This can
235 // cause us to run out of memory if we outrun the consumer. Wait here if
236 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700237 bool tooManyBuffers = mCore->mQueue.size()
238 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700239 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700240 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700241 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700242 } else {
243 if (!mCore->mFreeBuffers.empty()) {
244 auto slot = mCore->mFreeBuffers.begin();
245 *found = *slot;
246 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700247 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700248 auto slot = mCore->mFreeSlots.begin();
249 // Only return free slots up to the max buffer count
250 if (*slot < maxBufferCount) {
251 *found = *slot;
252 mCore->mFreeSlots.erase(slot);
253 }
254 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700255 }
256
257 // If no buffer is found, or if the queue has too many buffers
258 // outstanding, wait for a buffer to be acquired or released, or for the
259 // max buffer count to change.
260 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
261 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800262 if (tryAgain) {
263 // Return an error if we're in non-blocking mode (producer and
264 // consumer are controlled by the application).
265 // However, the consumer is allowed to briefly acquire an extra
266 // buffer (which could cause us to have to wait here), which is
267 // okay, since it is only used to implement an atomic acquire +
268 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700269 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800270 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
271 return WOULD_BLOCK;
272 }
273 mCore->mDequeueCondition.wait(mCore->mMutex);
274 }
275 } // while (tryAgain)
276
277 return NO_ERROR;
278}
279
Dan Stoza289ade12014-02-28 11:17:17 -0800280status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700281 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
282 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800283 ATRACE_CALL();
284 { // Autolock scope
285 Mutex::Autolock lock(mCore->mMutex);
286 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700287
288 if (mCore->mIsAbandoned) {
289 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
290 return NO_INIT;
291 }
292
293 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
294 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
295 return NO_INIT;
296 }
Dan Stoza289ade12014-02-28 11:17:17 -0800297 } // Autolock scope
298
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700299 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
300 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800301
302 if ((width && !height) || (!width && height)) {
303 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
304 return BAD_VALUE;
305 }
306
307 status_t returnFlags = NO_ERROR;
308 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
309 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800310 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800311
312 { // Autolock scope
313 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700314 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800315
316 if (format == 0) {
317 format = mCore->mDefaultBufferFormat;
318 }
319
320 // Enable the usage bits the consumer requested
321 usage |= mCore->mConsumerUsageBits;
322
Dan Stoza9de72932015-04-16 17:28:43 -0700323 const bool useDefaultSize = !width && !height;
324 if (useDefaultSize) {
325 width = mCore->mDefaultWidth;
326 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800327 }
Dan Stoza289ade12014-02-28 11:17:17 -0800328
Dan Stoza9de72932015-04-16 17:28:43 -0700329 int found = BufferItem::INVALID_BUFFER_SLOT;
330 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700331 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", &found,
332 &returnFlags);
Dan Stoza9de72932015-04-16 17:28:43 -0700333 if (status != NO_ERROR) {
334 return status;
335 }
336
337 // This should not happen
338 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
339 BQ_LOGE("dequeueBuffer: no available buffer slots");
340 return -EBUSY;
341 }
342
343 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
344
345 // If we are not allowed to allocate new buffers,
346 // waitForFreeSlotThenRelock must have returned a slot containing a
347 // buffer. If this buffer would require reallocation to meet the
348 // requested attributes, we free it and attempt to get another one.
349 if (!mCore->mAllowAllocation) {
350 if (buffer->needsReallocation(width, height, format, usage)) {
351 mCore->freeBufferLocked(found);
352 found = BufferItem::INVALID_BUFFER_SLOT;
353 continue;
354 }
355 }
Dan Stoza289ade12014-02-28 11:17:17 -0800356 }
357
358 *outSlot = found;
359 ATRACE_BUFFER_INDEX(found);
360
Dan Stoza9f3053d2014-03-06 15:14:33 -0800361 attachedByConsumer = mSlots[found].mAttachedByConsumer;
362
Dan Stoza289ade12014-02-28 11:17:17 -0800363 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
364
365 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
366 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700367 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800368 {
369 mSlots[found].mAcquireCalled = false;
370 mSlots[found].mGraphicBuffer = NULL;
371 mSlots[found].mRequestBufferCalled = false;
372 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
373 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
374 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800375 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800376
377 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800378 } else {
379 // We add 1 because that will be the frame number when this buffer
380 // is queued
381 mCore->mBufferAge =
382 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800383 }
384
Dan Stoza800b41a2015-04-28 14:20:04 -0700385 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
386 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800387
Dan Stoza289ade12014-02-28 11:17:17 -0800388 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
389 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
390 "slot=%d w=%d h=%d format=%u",
391 found, buffer->width, buffer->height, buffer->format);
392 }
393
394 eglDisplay = mSlots[found].mEglDisplay;
395 eglFence = mSlots[found].mEglFence;
396 *outFence = mSlots[found].mFence;
397 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
398 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700399
400 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800401 } // Autolock scope
402
403 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
404 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700405 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800406 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800407 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800408 if (graphicBuffer == NULL) {
409 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
410 return error;
411 }
412
413 { // Autolock scope
414 Mutex::Autolock lock(mCore->mMutex);
415
416 if (mCore->mIsAbandoned) {
417 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
418 return NO_INIT;
419 }
420
Dan Stoza812ed062015-06-02 15:45:22 -0700421 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800422 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
423 } // Autolock scope
424 }
425
Dan Stoza9f3053d2014-03-06 15:14:33 -0800426 if (attachedByConsumer) {
427 returnFlags |= BUFFER_NEEDS_REALLOCATION;
428 }
429
Dan Stoza289ade12014-02-28 11:17:17 -0800430 if (eglFence != EGL_NO_SYNC_KHR) {
431 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
432 1000000000);
433 // If something goes wrong, log the error, but return the buffer without
434 // synchronizing access to it. It's too late at this point to abort the
435 // dequeue operation.
436 if (result == EGL_FALSE) {
437 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
438 eglGetError());
439 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
440 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
441 }
442 eglDestroySyncKHR(eglDisplay, eglFence);
443 }
444
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700445 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
446 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800447 mSlots[*outSlot].mFrameNumber,
448 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
449
450 return returnFlags;
451}
452
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453status_t BufferQueueProducer::detachBuffer(int slot) {
454 ATRACE_CALL();
455 ATRACE_BUFFER_INDEX(slot);
456 BQ_LOGV("detachBuffer(P): slot %d", slot);
457 Mutex::Autolock lock(mCore->mMutex);
458
459 if (mCore->mIsAbandoned) {
460 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
461 return NO_INIT;
462 }
463
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700464 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
465 BQ_LOGE("detachBuffer(P): BufferQueue has no connected producer");
466 return NO_INIT;
467 }
468
Dan Stoza9f3053d2014-03-06 15:14:33 -0800469 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
470 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
471 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
472 return BAD_VALUE;
473 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
474 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
475 "(state = %d)", slot, mSlots[slot].mBufferState);
476 return BAD_VALUE;
477 } else if (!mSlots[slot].mRequestBufferCalled) {
478 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
479 slot);
480 return BAD_VALUE;
481 }
482
483 mCore->freeBufferLocked(slot);
484 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700485 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800486
487 return NO_ERROR;
488}
489
Dan Stozad9822a32014-03-28 15:25:31 -0700490status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
491 sp<Fence>* outFence) {
492 ATRACE_CALL();
493
494 if (outBuffer == NULL) {
495 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
496 return BAD_VALUE;
497 } else if (outFence == NULL) {
498 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
499 return BAD_VALUE;
500 }
501
502 Mutex::Autolock lock(mCore->mMutex);
503
504 if (mCore->mIsAbandoned) {
505 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
506 return NO_INIT;
507 }
508
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700509 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
510 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
511 return NO_INIT;
512 }
513
514 mCore->waitWhileAllocatingLocked();
515
Dan Stoza0de7ea72015-04-23 13:20:51 -0700516 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000517 return NO_MEMORY;
518 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700519
Dan Stoza0de7ea72015-04-23 13:20:51 -0700520 int found = mCore->mFreeBuffers.front();
521 mCore->mFreeBuffers.remove(found);
522
Dan Stozad9822a32014-03-28 15:25:31 -0700523 BQ_LOGV("detachNextBuffer detached slot %d", found);
524
525 *outBuffer = mSlots[found].mGraphicBuffer;
526 *outFence = mSlots[found].mFence;
527 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700528 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700529
530 return NO_ERROR;
531}
532
Dan Stoza9f3053d2014-03-06 15:14:33 -0800533status_t BufferQueueProducer::attachBuffer(int* outSlot,
534 const sp<android::GraphicBuffer>& buffer) {
535 ATRACE_CALL();
536
537 if (outSlot == NULL) {
538 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
539 return BAD_VALUE;
540 } else if (buffer == NULL) {
541 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
542 return BAD_VALUE;
543 }
544
545 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700546
547 if (mCore->mIsAbandoned) {
548 BQ_LOGE("attachBuffer(P): BufferQueue has been abandoned");
549 return NO_INIT;
550 }
551
552 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
553 BQ_LOGE("attachBuffer(P): BufferQueue has no connected producer");
554 return NO_INIT;
555 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800556
Dan Stoza812ed062015-06-02 15:45:22 -0700557 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
558 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
559 "[queue %u]", buffer->getGenerationNumber(),
560 mCore->mGenerationNumber);
561 return BAD_VALUE;
562 }
563
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700564 mCore->waitWhileAllocatingLocked();
565
Dan Stoza9f3053d2014-03-06 15:14:33 -0800566 status_t returnFlags = NO_ERROR;
567 int found;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700568 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", &found,
569 &returnFlags);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800570 if (status != NO_ERROR) {
571 return status;
572 }
573
574 // This should not happen
575 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
576 BQ_LOGE("attachBuffer(P): no available buffer slots");
577 return -EBUSY;
578 }
579
580 *outSlot = found;
581 ATRACE_BUFFER_INDEX(*outSlot);
582 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
583 *outSlot, returnFlags);
584
585 mSlots[*outSlot].mGraphicBuffer = buffer;
586 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
587 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
588 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700589 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800590
Dan Stoza0de7ea72015-04-23 13:20:51 -0700591 mCore->validateConsistencyLocked();
592
Dan Stoza9f3053d2014-03-06 15:14:33 -0800593 return returnFlags;
594}
595
Dan Stoza289ade12014-02-28 11:17:17 -0800596status_t BufferQueueProducer::queueBuffer(int slot,
597 const QueueBufferInput &input, QueueBufferOutput *output) {
598 ATRACE_CALL();
599 ATRACE_BUFFER_INDEX(slot);
600
601 int64_t timestamp;
602 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800603 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700604 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800605 int scalingMode;
606 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700607 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800608 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800609 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700610 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700611 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800612
613 if (fence == NULL) {
614 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800615 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800616 }
617
618 switch (scalingMode) {
619 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
620 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
621 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
622 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
623 break;
624 default:
625 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800626 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800627 }
628
Dan Stoza8dc55392014-11-04 11:37:46 -0800629 sp<IConsumerListener> frameAvailableListener;
630 sp<IConsumerListener> frameReplacedListener;
631 int callbackTicket = 0;
632 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800633 { // Autolock scope
634 Mutex::Autolock lock(mCore->mMutex);
635
636 if (mCore->mIsAbandoned) {
637 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
638 return NO_INIT;
639 }
640
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700641 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
642 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
643 return NO_INIT;
644 }
645
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700646 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800647
648 if (slot < 0 || slot >= maxBufferCount) {
649 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
650 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800651 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800652 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
653 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
654 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800655 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800656 } else if (!mSlots[slot].mRequestBufferCalled) {
657 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
658 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800659 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800660 }
661
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800662 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700663 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800664 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800665 crop.left, crop.top, crop.right, crop.bottom, transform,
666 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800667
668 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
669 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700670 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800671 crop.intersect(bufferRect, &croppedRect);
672 if (croppedRect != crop) {
673 BQ_LOGE("queueBuffer: crop rect is not contained within the "
674 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800675 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800676 }
677
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800678 // Override UNKNOWN dataspace with consumer default
679 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
680 dataSpace = mCore->mDefaultBufferDataSpace;
681 }
682
Dan Stoza289ade12014-02-28 11:17:17 -0800683 mSlots[slot].mFence = fence;
684 mSlots[slot].mBufferState = BufferSlot::QUEUED;
685 ++mCore->mFrameCounter;
686 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
687
Dan Stoza289ade12014-02-28 11:17:17 -0800688 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
689 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
690 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800691 item.mTransform = transform &
692 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800693 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800694 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
695 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800696 item.mTimestamp = timestamp;
697 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800698 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800699 item.mFrameNumber = mCore->mFrameCounter;
700 item.mSlot = slot;
701 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700702 item.mIsDroppable = mCore->mAsyncMode ||
703 mCore->mDequeueBufferCannotBlock;
Dan Stoza5065a552015-03-17 16:23:42 -0700704 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800705
Ruben Brunk1681d952014-06-27 15:51:55 -0700706 mStickyTransform = stickyTransform;
707
Dan Stoza289ade12014-02-28 11:17:17 -0800708 if (mCore->mQueue.empty()) {
709 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
710 // and simply queue this buffer
711 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800712 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800713 } else {
714 // When the queue is not empty, we need to look at the front buffer
715 // state to see if we need to replace it
716 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
717 if (front->mIsDroppable) {
718 // If the front queued buffer is still being tracked, we first
719 // mark it as freed
720 if (mCore->stillTracking(front)) {
721 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700722 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800723 }
724 // Overwrite the droppable buffer with the incoming one
725 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800726 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800727 } else {
728 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800729 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800730 }
731 }
732
733 mCore->mBufferHasBeenQueued = true;
734 mCore->mDequeueCondition.broadcast();
735
736 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800737 mCore->mTransformHint,
738 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800739
740 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800741
742 // Take a ticket for the callback functions
743 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700744
745 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800746 } // Autolock scope
747
Eric Penner99a0afb2014-09-30 11:28:30 -0700748 // Wait without lock held
749 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
750 // Waiting here allows for two full buffers to be queued but not a
751 // third. In the event that frames take varying time, this makes a
752 // small trade-off in favor of latency rather than throughput.
753 mLastQueueBufferFence->waitForever("Throttling EGL Production");
754 mLastQueueBufferFence = fence;
755 }
756
Dan Stoza8dc55392014-11-04 11:37:46 -0800757 // Don't send the GraphicBuffer through the callback, and don't send
758 // the slot number, since the consumer shouldn't need it
759 item.mGraphicBuffer.clear();
760 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
761
762 // Call back without the main BufferQueue lock held, but with the callback
763 // lock held so we can ensure that callbacks occur in order
764 {
765 Mutex::Autolock lock(mCallbackMutex);
766 while (callbackTicket != mCurrentCallbackTicket) {
767 mCallbackCondition.wait(mCallbackMutex);
768 }
769
770 if (frameAvailableListener != NULL) {
771 frameAvailableListener->onFrameAvailable(item);
772 } else if (frameReplacedListener != NULL) {
773 frameReplacedListener->onFrameReplaced(item);
774 }
775
776 ++mCurrentCallbackTicket;
777 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800778 }
779
780 return NO_ERROR;
781}
782
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700783status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800784 ATRACE_CALL();
785 BQ_LOGV("cancelBuffer: slot %d", slot);
786 Mutex::Autolock lock(mCore->mMutex);
787
788 if (mCore->mIsAbandoned) {
789 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700790 return NO_INIT;
791 }
792
793 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
794 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
795 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800796 }
797
Dan Stoza3e96f192014-03-03 10:16:19 -0800798 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800799 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800800 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700801 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
803 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
804 "(state = %d)", slot, mSlots[slot].mBufferState);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700805 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800806 } else if (fence == NULL) {
807 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700808 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800809 }
810
Dan Stoza0de7ea72015-04-23 13:20:51 -0700811 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800812 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800813 mSlots[slot].mFence = fence;
814 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700815 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700816
817 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800818}
819
820int BufferQueueProducer::query(int what, int *outValue) {
821 ATRACE_CALL();
822 Mutex::Autolock lock(mCore->mMutex);
823
824 if (outValue == NULL) {
825 BQ_LOGE("query: outValue was NULL");
826 return BAD_VALUE;
827 }
828
829 if (mCore->mIsAbandoned) {
830 BQ_LOGE("query: BufferQueue has been abandoned");
831 return NO_INIT;
832 }
833
834 int value;
835 switch (what) {
836 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800837 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800838 break;
839 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800840 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800841 break;
842 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800844 break;
845 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700846 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800847 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700848 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800849 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700850 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800851 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
852 value = (mCore->mQueue.size() > 1);
853 break;
854 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800855 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800856 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800857 case NATIVE_WINDOW_DEFAULT_DATASPACE:
858 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
859 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800860 case NATIVE_WINDOW_BUFFER_AGE:
861 if (mCore->mBufferAge > INT32_MAX) {
862 value = 0;
863 } else {
864 value = static_cast<int32_t>(mCore->mBufferAge);
865 }
866 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 default:
868 return BAD_VALUE;
869 }
870
871 BQ_LOGV("query: %d? %d", what, value);
872 *outValue = value;
873 return NO_ERROR;
874}
875
Dan Stozaf0eaf252014-03-21 13:05:51 -0700876status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800877 int api, bool producerControlledByApp, QueueBufferOutput *output) {
878 ATRACE_CALL();
879 Mutex::Autolock lock(mCore->mMutex);
880 mConsumerName = mCore->mConsumerName;
881 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
882 producerControlledByApp ? "true" : "false");
883
Dan Stozaae3c3682014-04-18 15:43:35 -0700884 if (mCore->mIsAbandoned) {
885 BQ_LOGE("connect(P): BufferQueue has been abandoned");
886 return NO_INIT;
887 }
Dan Stoza289ade12014-02-28 11:17:17 -0800888
Dan Stozaae3c3682014-04-18 15:43:35 -0700889 if (mCore->mConsumerListener == NULL) {
890 BQ_LOGE("connect(P): BufferQueue has no consumer");
891 return NO_INIT;
892 }
Dan Stoza289ade12014-02-28 11:17:17 -0800893
Dan Stozaae3c3682014-04-18 15:43:35 -0700894 if (output == NULL) {
895 BQ_LOGE("connect(P): output was NULL");
896 return BAD_VALUE;
897 }
Dan Stoza289ade12014-02-28 11:17:17 -0800898
Dan Stozaae3c3682014-04-18 15:43:35 -0700899 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
900 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
901 mCore->mConnectedApi, api);
902 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800903 }
904
905 int status = NO_ERROR;
906 switch (api) {
907 case NATIVE_WINDOW_API_EGL:
908 case NATIVE_WINDOW_API_CPU:
909 case NATIVE_WINDOW_API_MEDIA:
910 case NATIVE_WINDOW_API_CAMERA:
911 mCore->mConnectedApi = api;
912 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800913 mCore->mTransformHint,
914 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800915
916 // Set up a death notification so that we can disconnect
917 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700918 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800919 IInterface::asBinder(listener)->remoteBinder() != NULL) {
920 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800921 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700922 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800923 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
924 strerror(-status), status);
925 }
926 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700927 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800928 break;
929 default:
930 BQ_LOGE("connect(P): unknown API %d", api);
931 status = BAD_VALUE;
932 break;
933 }
934
935 mCore->mBufferHasBeenQueued = false;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700936 mCore->mDequeueBufferCannotBlock = mCore->mConsumerControlledByApp &&
937 producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -0700938 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800939
940 return status;
941}
942
943status_t BufferQueueProducer::disconnect(int api) {
944 ATRACE_CALL();
945 BQ_LOGV("disconnect(P): api %d", api);
946
947 int status = NO_ERROR;
948 sp<IConsumerListener> listener;
949 { // Autolock scope
950 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700951 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800952
953 if (mCore->mIsAbandoned) {
954 // It's not really an error to disconnect after the surface has
955 // been abandoned; it should just be a no-op.
956 return NO_ERROR;
957 }
958
959 switch (api) {
960 case NATIVE_WINDOW_API_EGL:
961 case NATIVE_WINDOW_API_CPU:
962 case NATIVE_WINDOW_API_MEDIA:
963 case NATIVE_WINDOW_API_CAMERA:
964 if (mCore->mConnectedApi == api) {
965 mCore->freeAllBuffersLocked();
966
967 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700968 if (mCore->mConnectedProducerListener != NULL) {
969 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800970 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800971 // This can fail if we're here because of the death
972 // notification, but we just ignore it
973 token->unlinkToDeath(
974 static_cast<IBinder::DeathRecipient*>(this));
975 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700976 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800977 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800978 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800979 mCore->mDequeueCondition.broadcast();
980 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700981 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
982 BQ_LOGE("disconnect(P): still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800983 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800984 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800985 }
986 break;
987 default:
988 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800989 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800990 break;
991 }
992 } // Autolock scope
993
994 // Call back without lock held
995 if (listener != NULL) {
996 listener->onBuffersReleased();
997 }
998
999 return status;
1000}
1001
Jesse Hall399184a2014-03-03 15:42:54 -08001002status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001003 sp<IConsumerListener> listener;
1004 { // Autolock scope
1005 Mutex::Autolock _l(mCore->mMutex);
1006 mCore->mSidebandStream = stream;
1007 listener = mCore->mConsumerListener;
1008 } // Autolock scope
1009
1010 if (listener != NULL) {
1011 listener->onSidebandStreamChanged();
1012 }
Jesse Hall399184a2014-03-03 15:42:54 -08001013 return NO_ERROR;
1014}
1015
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001016void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1017 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001018 ATRACE_CALL();
1019 while (true) {
1020 Vector<int> freeSlots;
1021 size_t newBufferCount = 0;
1022 uint32_t allocWidth = 0;
1023 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001024 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001025 uint32_t allocUsage = 0;
1026 { // Autolock scope
1027 Mutex::Autolock lock(mCore->mMutex);
1028 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001029
Dan Stoza9de72932015-04-16 17:28:43 -07001030 if (!mCore->mAllowAllocation) {
1031 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1032 "BufferQueue");
1033 return;
1034 }
1035
Antoine Labour78014f32014-07-15 21:17:03 -07001036 int currentBufferCount = 0;
1037 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
1038 if (mSlots[slot].mGraphicBuffer != NULL) {
1039 ++currentBufferCount;
1040 } else {
1041 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1042 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
1043 slot);
1044 continue;
1045 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001046
Antoine Labour11f14872014-07-25 18:14:42 -07001047 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001048 }
1049 }
1050
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001051 int maxBufferCount = mCore->getMaxBufferCountLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001052 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
1053 currentBufferCount, maxBufferCount);
1054 if (maxBufferCount <= currentBufferCount)
1055 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001056 newBufferCount =
1057 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001058 if (freeSlots.size() < newBufferCount) {
1059 BQ_LOGE("allocateBuffers: ran out of free slots");
1060 return;
1061 }
1062 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1063 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1064 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1065 allocUsage = usage | mCore->mConsumerUsageBits;
1066
1067 mCore->mIsAllocating = true;
1068 } // Autolock scope
1069
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001070 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001071 for (size_t i = 0; i < newBufferCount; ++i) {
1072 status_t result = NO_ERROR;
1073 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1074 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1075 if (result != NO_ERROR) {
1076 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1077 " %u, usage %u)", width, height, format, usage);
1078 Mutex::Autolock lock(mCore->mMutex);
1079 mCore->mIsAllocating = false;
1080 mCore->mIsAllocatingCondition.broadcast();
1081 return;
1082 }
1083 buffers.push_back(graphicBuffer);
1084 }
1085
1086 { // Autolock scope
1087 Mutex::Autolock lock(mCore->mMutex);
1088 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1089 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001090 PixelFormat checkFormat = format != 0 ?
1091 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001092 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1093 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1094 checkFormat != allocFormat || checkUsage != allocUsage) {
1095 // Something changed while we released the lock. Retry.
1096 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1097 mCore->mIsAllocating = false;
1098 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001099 continue;
1100 }
1101
Antoine Labour78014f32014-07-15 21:17:03 -07001102 for (size_t i = 0; i < newBufferCount; ++i) {
1103 int slot = freeSlots[i];
1104 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1105 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1106 // allocated.
1107 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1108 "Dropping allocated buffer.", slot);
1109 continue;
1110 }
1111 mCore->freeBufferLocked(slot); // Clean up the slot first
1112 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001113 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001114
1115 // freeBufferLocked puts this slot on the free slots list. Since
1116 // we then attached a buffer, move the slot to free buffer list.
1117 mCore->mFreeSlots.erase(slot);
1118 mCore->mFreeBuffers.push_front(slot);
1119
Antoine Labour78014f32014-07-15 21:17:03 -07001120 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1121 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001122
Antoine Labour78014f32014-07-15 21:17:03 -07001123 mCore->mIsAllocating = false;
1124 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001125 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001126 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001127 }
1128}
1129
Dan Stoza9de72932015-04-16 17:28:43 -07001130status_t BufferQueueProducer::allowAllocation(bool allow) {
1131 ATRACE_CALL();
1132 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1133
1134 Mutex::Autolock lock(mCore->mMutex);
1135 mCore->mAllowAllocation = allow;
1136 return NO_ERROR;
1137}
1138
Dan Stoza812ed062015-06-02 15:45:22 -07001139status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1140 ATRACE_CALL();
1141 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1142
1143 Mutex::Autolock lock(mCore->mMutex);
1144 mCore->mGenerationNumber = generationNumber;
1145 return NO_ERROR;
1146}
1147
Dan Stozac6f30bd2015-06-08 09:32:50 -07001148String8 BufferQueueProducer::getConsumerName() const {
1149 ATRACE_CALL();
1150 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1151 return mConsumerName;
1152}
1153
Dan Stoza7dde5992015-05-22 09:51:44 -07001154uint64_t BufferQueueProducer::getNextFrameNumber() const {
1155 ATRACE_CALL();
1156
1157 Mutex::Autolock lock(mCore->mMutex);
1158 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1159 return nextFrameNumber;
1160}
1161
Dan Stoza289ade12014-02-28 11:17:17 -08001162void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1163 // If we're here, it means that a producer we were connected to died.
1164 // We're guaranteed that we are still connected to it because we remove
1165 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1166 // without synchronization here.
1167 int api = mCore->mConnectedApi;
1168 disconnect(api);
1169}
1170
1171} // namespace android