blob: 3c06899ed0c622cea525db51e3f2eb3f0acb6b02 [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
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
31#include <gui/BufferItem.h>
32#include <gui/BufferQueueCore.h>
33#include <gui/BufferQueueProducer.h>
34#include <gui/IConsumerListener.h>
35#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070036#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
38#include <utils/Log.h>
39#include <utils/Trace.h>
40
41namespace android {
42
43BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
44 mCore(core),
45 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070046 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070047 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080048 mLastQueueBufferFence(Fence::NO_FENCE),
49 mCallbackMutex(),
50 mNextCallbackTicket(0),
51 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070052 mCallbackCondition(),
53 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080054
55BufferQueueProducer::~BufferQueueProducer() {}
56
57status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
58 ATRACE_CALL();
59 BQ_LOGV("requestBuffer: slot %d", slot);
60 Mutex::Autolock lock(mCore->mMutex);
61
62 if (mCore->mIsAbandoned) {
63 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
64 return NO_INIT;
65 }
66
Pablo Ceballos583b1b32015-09-03 18:23:52 -070067 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
68 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
69 return NO_INIT;
70 }
71
Dan Stoza3e96f192014-03-03 10:16:19 -080072 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080073 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080074 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080075 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070076 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080077 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070078 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080079 return BAD_VALUE;
80 }
81
82 mSlots[slot].mRequestBufferCalled = true;
83 *buf = mSlots[slot].mGraphicBuffer;
84 return NO_ERROR;
85}
86
Pablo Ceballosfa455352015-08-12 17:47:47 -070087status_t BufferQueueProducer::setMaxDequeuedBufferCount(
88 int maxDequeuedBuffers) {
89 ATRACE_CALL();
90 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
91 maxDequeuedBuffers);
92
93 sp<IConsumerListener> listener;
94 { // Autolock scope
95 Mutex::Autolock lock(mCore->mMutex);
96 mCore->waitWhileAllocatingLocked();
97
98 if (mCore->mIsAbandoned) {
99 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
100 "abandoned");
101 return NO_INIT;
102 }
103
104 // There must be no dequeued buffers when changing the buffer count.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800105 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700106 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700107 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
108 return BAD_VALUE;
109 }
110 }
111
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700112 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700113 bufferCount += maxDequeuedBuffers;
114
115 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
116 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
117 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
118 return BAD_VALUE;
119 }
120
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700121 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700122 if (bufferCount < minBufferSlots) {
123 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
124 "less than minimum %d", bufferCount, minBufferSlots);
125 return BAD_VALUE;
126 }
127
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700128 if (bufferCount > mCore->mMaxBufferCount) {
129 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700130 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
131 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
132 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
133 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700134 return BAD_VALUE;
135 }
136
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137 // Here we are guaranteed that the producer doesn't have any dequeued
138 // buffers and will release all of its buffer references. We don't
139 // clear the queue, however, so that currently queued buffers still
140 // get displayed.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800141 if (!mCore->adjustAvailableSlotsLocked(
142 maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount)) {
143 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue failed to adjust "
144 "the number of available slots. Delta = %d",
145 maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount);
146 return BAD_VALUE;
147 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700148 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800149 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700150 mCore->mDequeueCondition.broadcast();
151 listener = mCore->mConsumerListener;
152 } // Autolock scope
153
154 // Call back without lock held
155 if (listener != NULL) {
156 listener->onBuffersReleased();
157 }
158
159 return NO_ERROR;
160}
161
162status_t BufferQueueProducer::setAsyncMode(bool async) {
163 ATRACE_CALL();
164 BQ_LOGV("setAsyncMode: async = %d", async);
165
166 sp<IConsumerListener> listener;
167 { // Autolock scope
168 Mutex::Autolock lock(mCore->mMutex);
169 mCore->waitWhileAllocatingLocked();
170
171 if (mCore->mIsAbandoned) {
172 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
173 return NO_INIT;
174 }
175
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700176 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700177 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
178 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700179 BQ_LOGE("setAsyncMode(%d): this call would cause the "
180 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700181 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
182 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
183 mCore->mMaxDequeuedBufferCount,
184 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700185 return BAD_VALUE;
186 }
187
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800188 int delta = mCore->getMaxBufferCountLocked(async,
189 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
190 - mCore->getMaxBufferCountLocked();
191
192 if (!mCore->adjustAvailableSlotsLocked(delta)) {
193 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
194 "available slots. Delta = %d", delta);
195 return BAD_VALUE;
196 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700197 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800198 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700199 mCore->mDequeueCondition.broadcast();
200 listener = mCore->mConsumerListener;
201 } // Autolock scope
202
203 // Call back without lock held
204 if (listener != NULL) {
205 listener->onBuffersReleased();
206 }
207 return NO_ERROR;
208}
209
Dan Stoza5ecfb682016-01-04 17:01:02 -0800210int BufferQueueProducer::getFreeBufferLocked() const {
211 if (mCore->mFreeBuffers.empty()) {
212 return BufferQueueCore::INVALID_BUFFER_SLOT;
213 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800214 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800215 mCore->mFreeBuffers.pop_front();
216 return slot;
217}
218
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800219int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800220 if (mCore->mFreeSlots.empty()) {
221 return BufferQueueCore::INVALID_BUFFER_SLOT;
222 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800223 auto slot = mCore->mFreeSlots.begin();
224 mCore->mFreeSlots.erase(slot);
225 return *slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800226}
227
228status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800229 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800230 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
231 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800232 bool tryAgain = true;
233 while (tryAgain) {
234 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800235 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800236 return NO_INIT;
237 }
238
Dan Stoza9f3053d2014-03-06 15:14:33 -0800239 int dequeuedCount = 0;
240 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700242 if (mSlots[s].mBufferState.isDequeued()) {
243 ++dequeuedCount;
244 }
245 if (mSlots[s].mBufferState.isAcquired()) {
246 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800247 }
248 }
249
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700250 // Producers are not allowed to dequeue more than
251 // mMaxDequeuedBufferCount buffers.
252 // This check is only done if a buffer has already been queued
253 if (mCore->mBufferHasBeenQueued &&
254 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
255 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800256 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800257 return INVALID_OPERATION;
258 }
259
Dan Stoza0de7ea72015-04-23 13:20:51 -0700260 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
261
Dan Stozaae3c3682014-04-18 15:43:35 -0700262 // If we disconnect and reconnect quickly, we can be in a state where
263 // our slots are empty but we have many buffers in the queue. This can
264 // cause us to run out of memory if we outrun the consumer. Wait here if
265 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800266 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700267 bool tooManyBuffers = mCore->mQueue.size()
268 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700269 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700271 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700272 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700273 // If in single buffer mode and a shared buffer exists, always
274 // return it.
275 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
276 BufferQueueCore::INVALID_BUFFER_SLOT) {
277 *found = mCore->mSingleBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800278 } else {
279 if (caller == FreeSlotCaller::Dequeue) {
280 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800281 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800282 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
283 *found = slot;
284 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800285 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800286 }
287 } else {
288 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800289 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
291 *found = slot;
292 } else {
293 *found = getFreeBufferLocked();
294 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700295 }
296 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700297 }
298
299 // If no buffer is found, or if the queue has too many buffers
300 // outstanding, wait for a buffer to be acquired or released, or for the
301 // max buffer count to change.
302 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
303 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800304 if (tryAgain) {
305 // Return an error if we're in non-blocking mode (producer and
306 // consumer are controlled by the application).
307 // However, the consumer is allowed to briefly acquire an extra
308 // buffer (which could cause us to have to wait here), which is
309 // okay, since it is only used to implement an atomic acquire +
310 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700311 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800312 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
313 return WOULD_BLOCK;
314 }
Dan Stoza127fc632015-06-30 13:43:32 -0700315 if (mDequeueTimeout >= 0) {
316 status_t result = mCore->mDequeueCondition.waitRelative(
317 mCore->mMutex, mDequeueTimeout);
318 if (result == TIMED_OUT) {
319 return result;
320 }
321 } else {
322 mCore->mDequeueCondition.wait(mCore->mMutex);
323 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 }
325 } // while (tryAgain)
326
327 return NO_ERROR;
328}
329
Dan Stoza289ade12014-02-28 11:17:17 -0800330status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700331 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
332 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800333 ATRACE_CALL();
334 { // Autolock scope
335 Mutex::Autolock lock(mCore->mMutex);
336 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700337
338 if (mCore->mIsAbandoned) {
339 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
340 return NO_INIT;
341 }
342
343 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
344 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
345 return NO_INIT;
346 }
Dan Stoza289ade12014-02-28 11:17:17 -0800347 } // Autolock scope
348
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700349 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
350 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800351
352 if ((width && !height) || (!width && height)) {
353 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
354 return BAD_VALUE;
355 }
356
357 status_t returnFlags = NO_ERROR;
358 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
359 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800360 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800361
362 { // Autolock scope
363 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700364 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800365
366 if (format == 0) {
367 format = mCore->mDefaultBufferFormat;
368 }
369
370 // Enable the usage bits the consumer requested
371 usage |= mCore->mConsumerUsageBits;
372
Dan Stoza9de72932015-04-16 17:28:43 -0700373 const bool useDefaultSize = !width && !height;
374 if (useDefaultSize) {
375 width = mCore->mDefaultWidth;
376 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800377 }
Dan Stoza289ade12014-02-28 11:17:17 -0800378
Dan Stoza9de72932015-04-16 17:28:43 -0700379 int found = BufferItem::INVALID_BUFFER_SLOT;
380 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800381 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800382 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700383 if (status != NO_ERROR) {
384 return status;
385 }
386
387 // This should not happen
388 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
389 BQ_LOGE("dequeueBuffer: no available buffer slots");
390 return -EBUSY;
391 }
392
393 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
394
395 // If we are not allowed to allocate new buffers,
396 // waitForFreeSlotThenRelock must have returned a slot containing a
397 // buffer. If this buffer would require reallocation to meet the
398 // requested attributes, we free it and attempt to get another one.
399 if (!mCore->mAllowAllocation) {
400 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800401 if (mCore->mSingleBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700402 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
403 "buffer");
404 return BAD_VALUE;
405 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800406 mCore->mFreeSlots.insert(found);
407 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700408 found = BufferItem::INVALID_BUFFER_SLOT;
409 continue;
410 }
411 }
Dan Stoza289ade12014-02-28 11:17:17 -0800412 }
413
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800414 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
415 if (mCore->mSingleBufferSlot == found &&
416 buffer->needsReallocation(width, height, format, usage)) {
417 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
418 "buffer");
419
420 return BAD_VALUE;
421 }
422
423 if (mCore->mSingleBufferSlot != found) {
424 mCore->mActiveBuffers.insert(found);
425 }
Dan Stoza289ade12014-02-28 11:17:17 -0800426 *outSlot = found;
427 ATRACE_BUFFER_INDEX(found);
428
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800429 attachedByConsumer = mSlots[found].mNeedsReallocation;
430 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800431
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700432 mSlots[found].mBufferState.dequeue();
433
434 // If single buffer mode has just been enabled, cache the slot of the
435 // first buffer that is dequeued and mark it as the shared buffer.
436 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
437 BufferQueueCore::INVALID_BUFFER_SLOT) {
438 mCore->mSingleBufferSlot = found;
439 mSlots[found].mBufferState.mShared = true;
440 }
Dan Stoza289ade12014-02-28 11:17:17 -0800441
Dan Stoza289ade12014-02-28 11:17:17 -0800442 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700443 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800444 {
445 mSlots[found].mAcquireCalled = false;
446 mSlots[found].mGraphicBuffer = NULL;
447 mSlots[found].mRequestBufferCalled = false;
448 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
449 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
450 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800451 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700452 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800453
454 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800455 } else {
456 // We add 1 because that will be the frame number when this buffer
457 // is queued
458 mCore->mBufferAge =
459 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800460 }
461
Dan Stoza800b41a2015-04-28 14:20:04 -0700462 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
463 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800464
Dan Stoza289ade12014-02-28 11:17:17 -0800465 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
466 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
467 "slot=%d w=%d h=%d format=%u",
468 found, buffer->width, buffer->height, buffer->format);
469 }
470
471 eglDisplay = mSlots[found].mEglDisplay;
472 eglFence = mSlots[found].mEglFence;
473 *outFence = mSlots[found].mFence;
474 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
475 mSlots[found].mFence = Fence::NO_FENCE;
476 } // Autolock scope
477
478 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
479 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700480 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800481 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800482 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800483 { // Autolock scope
484 Mutex::Autolock lock(mCore->mMutex);
485
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700486 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
487 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
488 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
489 }
490
491 mCore->mIsAllocating = false;
492 mCore->mIsAllocatingCondition.broadcast();
493
494 if (graphicBuffer == NULL) {
495 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
496 return error;
497 }
498
Dan Stoza289ade12014-02-28 11:17:17 -0800499 if (mCore->mIsAbandoned) {
500 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
501 return NO_INIT;
502 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800503
Pablo Ceballos9e314332016-01-12 13:49:19 -0800504 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800505 } // Autolock scope
506 }
507
Dan Stoza9f3053d2014-03-06 15:14:33 -0800508 if (attachedByConsumer) {
509 returnFlags |= BUFFER_NEEDS_REALLOCATION;
510 }
511
Dan Stoza289ade12014-02-28 11:17:17 -0800512 if (eglFence != EGL_NO_SYNC_KHR) {
513 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
514 1000000000);
515 // If something goes wrong, log the error, but return the buffer without
516 // synchronizing access to it. It's too late at this point to abort the
517 // dequeue operation.
518 if (result == EGL_FALSE) {
519 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
520 eglGetError());
521 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
522 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
523 }
524 eglDestroySyncKHR(eglDisplay, eglFence);
525 }
526
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700527 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
528 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800529 mSlots[*outSlot].mFrameNumber,
530 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
531
532 return returnFlags;
533}
534
Dan Stoza9f3053d2014-03-06 15:14:33 -0800535status_t BufferQueueProducer::detachBuffer(int slot) {
536 ATRACE_CALL();
537 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700538 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800539 Mutex::Autolock lock(mCore->mMutex);
540
541 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700542 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800543 return NO_INIT;
544 }
545
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700546 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700547 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700548 return NO_INIT;
549 }
550
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800551 if (mCore->mSingleBufferMode || mCore->mSingleBufferSlot == slot) {
552 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700553 return BAD_VALUE;
554 }
555
Dan Stoza9f3053d2014-03-06 15:14:33 -0800556 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700557 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800558 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
559 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700560 } else if (!mSlots[slot].mBufferState.isDequeued()) {
561 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
562 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800563 return BAD_VALUE;
564 } else if (!mSlots[slot].mRequestBufferCalled) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700565 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800566 slot);
567 return BAD_VALUE;
568 }
569
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700570 mSlots[slot].mBufferState.detachProducer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800571 mCore->mActiveBuffers.erase(slot);
572 mCore->mFreeSlots.insert(slot);
573 mCore->clearBufferSlotLocked(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800574 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800575 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800576
577 return NO_ERROR;
578}
579
Dan Stozad9822a32014-03-28 15:25:31 -0700580status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
581 sp<Fence>* outFence) {
582 ATRACE_CALL();
583
584 if (outBuffer == NULL) {
585 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
586 return BAD_VALUE;
587 } else if (outFence == NULL) {
588 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
589 return BAD_VALUE;
590 }
591
592 Mutex::Autolock lock(mCore->mMutex);
593
594 if (mCore->mIsAbandoned) {
595 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
596 return NO_INIT;
597 }
598
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700599 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
600 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
601 return NO_INIT;
602 }
603
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700604 if (mCore->mSingleBufferMode) {
605 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
606 "mode");
607 return BAD_VALUE;
608 }
609
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700610 mCore->waitWhileAllocatingLocked();
611
Dan Stoza0de7ea72015-04-23 13:20:51 -0700612 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000613 return NO_MEMORY;
614 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700615
Dan Stoza0de7ea72015-04-23 13:20:51 -0700616 int found = mCore->mFreeBuffers.front();
617 mCore->mFreeBuffers.remove(found);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800618 mCore->mFreeSlots.insert(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700619
Dan Stozad9822a32014-03-28 15:25:31 -0700620 BQ_LOGV("detachNextBuffer detached slot %d", found);
621
622 *outBuffer = mSlots[found].mGraphicBuffer;
623 *outFence = mSlots[found].mFence;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800624 mCore->clearBufferSlotLocked(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800625 VALIDATE_CONSISTENCY();
Dan Stozad9822a32014-03-28 15:25:31 -0700626
627 return NO_ERROR;
628}
629
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630status_t BufferQueueProducer::attachBuffer(int* outSlot,
631 const sp<android::GraphicBuffer>& buffer) {
632 ATRACE_CALL();
633
634 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700635 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800636 return BAD_VALUE;
637 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700638 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800639 return BAD_VALUE;
640 }
641
642 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700643
644 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700645 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700646 return NO_INIT;
647 }
648
649 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700650 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700651 return NO_INIT;
652 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800653
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700654 if (mCore->mSingleBufferMode) {
Craig Donner05249fc2016-01-15 19:33:55 -0800655 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700656 return BAD_VALUE;
657 }
658
Dan Stoza812ed062015-06-02 15:45:22 -0700659 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
660 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
661 "[queue %u]", buffer->getGenerationNumber(),
662 mCore->mGenerationNumber);
663 return BAD_VALUE;
664 }
665
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700666 mCore->waitWhileAllocatingLocked();
667
Dan Stoza9f3053d2014-03-06 15:14:33 -0800668 status_t returnFlags = NO_ERROR;
669 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800670 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800671 if (status != NO_ERROR) {
672 return status;
673 }
674
675 // This should not happen
676 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700677 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800678 return -EBUSY;
679 }
680
681 *outSlot = found;
682 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700683 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800684 *outSlot, returnFlags);
685
686 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700687 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800688 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
689 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700690 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800691 mSlots[*outSlot].mAcquireCalled = false;
692 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800693 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700694
Dan Stoza9f3053d2014-03-06 15:14:33 -0800695 return returnFlags;
696}
697
Dan Stoza289ade12014-02-28 11:17:17 -0800698status_t BufferQueueProducer::queueBuffer(int slot,
699 const QueueBufferInput &input, QueueBufferOutput *output) {
700 ATRACE_CALL();
701 ATRACE_BUFFER_INDEX(slot);
702
703 int64_t timestamp;
704 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800705 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700706 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800707 int scalingMode;
708 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700709 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800710 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800711 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700712 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700713 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800714
715 if (fence == NULL) {
716 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800717 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800718 }
719
720 switch (scalingMode) {
721 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
722 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
723 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
724 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
725 break;
726 default:
727 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800729 }
730
Dan Stoza8dc55392014-11-04 11:37:46 -0800731 sp<IConsumerListener> frameAvailableListener;
732 sp<IConsumerListener> frameReplacedListener;
733 int callbackTicket = 0;
734 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800735 { // Autolock scope
736 Mutex::Autolock lock(mCore->mMutex);
737
738 if (mCore->mIsAbandoned) {
739 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
740 return NO_INIT;
741 }
742
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700743 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
744 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
745 return NO_INIT;
746 }
747
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800748 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800749 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800750 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800751 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700752 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800753 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700754 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800755 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800756 } else if (!mSlots[slot].mRequestBufferCalled) {
757 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
758 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800759 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800760 }
761
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800762 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700763 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800764 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800765 crop.left, crop.top, crop.right, crop.bottom, transform,
766 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800767
768 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
769 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700770 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800771 crop.intersect(bufferRect, &croppedRect);
772 if (croppedRect != crop) {
773 BQ_LOGE("queueBuffer: crop rect is not contained within the "
774 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800775 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800776 }
777
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800778 // Override UNKNOWN dataspace with consumer default
779 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
780 dataSpace = mCore->mDefaultBufferDataSpace;
781 }
782
Dan Stoza289ade12014-02-28 11:17:17 -0800783 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700784 mSlots[slot].mBufferState.queue();
785
Dan Stoza289ade12014-02-28 11:17:17 -0800786 ++mCore->mFrameCounter;
787 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
788
Dan Stoza289ade12014-02-28 11:17:17 -0800789 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
790 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
791 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800792 item.mTransform = transform &
793 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800794 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800795 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
796 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800797 item.mTimestamp = timestamp;
798 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800799 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800800 item.mFrameNumber = mCore->mFrameCounter;
801 item.mSlot = slot;
802 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700803 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700804 mCore->mDequeueBufferCannotBlock ||
805 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700806 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700807 item.mSingleBufferMode = mCore->mSingleBufferMode;
808 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800809
Ruben Brunk1681d952014-06-27 15:51:55 -0700810 mStickyTransform = stickyTransform;
811
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700812 // Cache the shared buffer data so that the BufferItem can be recreated.
813 if (mCore->mSingleBufferMode) {
814 mCore->mSingleBufferCache.crop = crop;
815 mCore->mSingleBufferCache.transform = transform;
816 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
817 scalingMode);
818 mCore->mSingleBufferCache.dataspace = dataSpace;
819 }
820
Dan Stoza289ade12014-02-28 11:17:17 -0800821 if (mCore->mQueue.empty()) {
822 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
823 // and simply queue this buffer
824 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800825 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800826 } else {
827 // When the queue is not empty, we need to look at the front buffer
828 // state to see if we need to replace it
829 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
830 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800831
832 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700833 mSlots[front->mSlot].mBufferState.freeQueued();
834
835 // After leaving single buffer mode, the shared buffer will
836 // still be around. Mark it as no longer shared if this
837 // operation causes it to be free.
838 if (!mCore->mSingleBufferMode &&
839 mSlots[front->mSlot].mBufferState.isFree()) {
840 mSlots[front->mSlot].mBufferState.mShared = false;
841 }
842 // Don't put the shared buffer on the free list.
843 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800844 mCore->mActiveBuffers.erase(front->mSlot);
845 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700846 }
Dan Stoza289ade12014-02-28 11:17:17 -0800847 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800848
Dan Stoza289ade12014-02-28 11:17:17 -0800849 // Overwrite the droppable buffer with the incoming one
850 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800851 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800852 } else {
853 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800854 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800855 }
856 }
857
858 mCore->mBufferHasBeenQueued = true;
859 mCore->mDequeueCondition.broadcast();
860
861 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800862 mCore->mTransformHint,
863 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800864
865 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800866
867 // Take a ticket for the callback functions
868 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700869
Pablo Ceballos9e314332016-01-12 13:49:19 -0800870 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800871 } // Autolock scope
872
Dan Stoza8dc55392014-11-04 11:37:46 -0800873 // Don't send the GraphicBuffer through the callback, and don't send
874 // the slot number, since the consumer shouldn't need it
875 item.mGraphicBuffer.clear();
876 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
877
878 // Call back without the main BufferQueue lock held, but with the callback
879 // lock held so we can ensure that callbacks occur in order
880 {
881 Mutex::Autolock lock(mCallbackMutex);
882 while (callbackTicket != mCurrentCallbackTicket) {
883 mCallbackCondition.wait(mCallbackMutex);
884 }
885
886 if (frameAvailableListener != NULL) {
887 frameAvailableListener->onFrameAvailable(item);
888 } else if (frameReplacedListener != NULL) {
889 frameReplacedListener->onFrameReplaced(item);
890 }
891
892 ++mCurrentCallbackTicket;
893 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800894 }
895
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000896 // Wait without lock held
897 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
898 // Waiting here allows for two full buffers to be queued but not a
899 // third. In the event that frames take varying time, this makes a
900 // small trade-off in favor of latency rather than throughput.
901 mLastQueueBufferFence->waitForever("Throttling EGL Production");
902 mLastQueueBufferFence = fence;
903 }
904
Dan Stoza289ade12014-02-28 11:17:17 -0800905 return NO_ERROR;
906}
907
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700908status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800909 ATRACE_CALL();
910 BQ_LOGV("cancelBuffer: slot %d", slot);
911 Mutex::Autolock lock(mCore->mMutex);
912
913 if (mCore->mIsAbandoned) {
914 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700915 return NO_INIT;
916 }
917
918 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
919 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
920 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800921 }
922
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700923 if (mCore->mSingleBufferMode) {
924 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
925 return BAD_VALUE;
926 }
927
Dan Stoza3e96f192014-03-03 10:16:19 -0800928 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800929 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800930 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700931 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700932 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800933 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700934 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700935 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800936 } else if (fence == NULL) {
937 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700938 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800939 }
940
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700941 mSlots[slot].mBufferState.cancel();
942
943 // After leaving single buffer mode, the shared buffer will still be around.
944 // Mark it as no longer shared if this operation causes it to be free.
945 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
946 mSlots[slot].mBufferState.mShared = false;
947 }
948
949 // Don't put the shared buffer on the free list.
950 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800951 mCore->mActiveBuffers.erase(slot);
952 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700953 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800954
Dan Stoza289ade12014-02-28 11:17:17 -0800955 mSlots[slot].mFence = fence;
956 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800957 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700958
959 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800960}
961
962int BufferQueueProducer::query(int what, int *outValue) {
963 ATRACE_CALL();
964 Mutex::Autolock lock(mCore->mMutex);
965
966 if (outValue == NULL) {
967 BQ_LOGE("query: outValue was NULL");
968 return BAD_VALUE;
969 }
970
971 if (mCore->mIsAbandoned) {
972 BQ_LOGE("query: BufferQueue has been abandoned");
973 return NO_INIT;
974 }
975
976 int value;
977 switch (what) {
978 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800979 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800980 break;
981 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800982 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800983 break;
984 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800985 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800986 break;
987 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700988 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800989 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700990 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800991 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700992 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800993 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
994 value = (mCore->mQueue.size() > 1);
995 break;
996 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800997 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800998 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800999 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1000 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1001 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001002 case NATIVE_WINDOW_BUFFER_AGE:
1003 if (mCore->mBufferAge > INT32_MAX) {
1004 value = 0;
1005 } else {
1006 value = static_cast<int32_t>(mCore->mBufferAge);
1007 }
1008 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001009 default:
1010 return BAD_VALUE;
1011 }
1012
1013 BQ_LOGV("query: %d? %d", what, value);
1014 *outValue = value;
1015 return NO_ERROR;
1016}
1017
Dan Stozaf0eaf252014-03-21 13:05:51 -07001018status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001019 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1020 ATRACE_CALL();
1021 Mutex::Autolock lock(mCore->mMutex);
1022 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001023 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -08001024 producerControlledByApp ? "true" : "false");
1025
Dan Stozaae3c3682014-04-18 15:43:35 -07001026 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001027 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -07001028 return NO_INIT;
1029 }
Dan Stoza289ade12014-02-28 11:17:17 -08001030
Dan Stozaae3c3682014-04-18 15:43:35 -07001031 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001032 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -07001033 return NO_INIT;
1034 }
Dan Stoza289ade12014-02-28 11:17:17 -08001035
Dan Stozaae3c3682014-04-18 15:43:35 -07001036 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001037 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -07001038 return BAD_VALUE;
1039 }
Dan Stoza289ade12014-02-28 11:17:17 -08001040
Dan Stozaae3c3682014-04-18 15:43:35 -07001041 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001042 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -07001043 mCore->mConnectedApi, api);
1044 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001045 }
1046
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001047 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1048 mDequeueTimeout < 0 ?
1049 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1050 mCore->mMaxBufferCount) -
1051 mCore->getMaxBufferCountLocked();
1052 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1053 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1054 "slots. Delta = %d", delta);
1055 return BAD_VALUE;
1056 }
1057
Dan Stoza289ade12014-02-28 11:17:17 -08001058 int status = NO_ERROR;
1059 switch (api) {
1060 case NATIVE_WINDOW_API_EGL:
1061 case NATIVE_WINDOW_API_CPU:
1062 case NATIVE_WINDOW_API_MEDIA:
1063 case NATIVE_WINDOW_API_CAMERA:
1064 mCore->mConnectedApi = api;
1065 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001066 mCore->mTransformHint,
1067 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001068
1069 // Set up a death notification so that we can disconnect
1070 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -07001071 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -08001072 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1073 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -08001074 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -07001075 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001076 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001077 strerror(-status), status);
1078 }
1079 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001080 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001081 break;
1082 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001083 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001084 status = BAD_VALUE;
1085 break;
1086 }
1087
1088 mCore->mBufferHasBeenQueued = false;
Dan Stoza127fc632015-06-30 13:43:32 -07001089 mCore->mDequeueBufferCannotBlock = false;
1090 if (mDequeueTimeout < 0) {
1091 mCore->mDequeueBufferCannotBlock =
1092 mCore->mConsumerControlledByApp && producerControlledByApp;
1093 }
Dan Stoza289ade12014-02-28 11:17:17 -08001094
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001095 mCore->mAllowAllocation = true;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001096 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001097 return status;
1098}
1099
1100status_t BufferQueueProducer::disconnect(int api) {
1101 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001102 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001103
1104 int status = NO_ERROR;
1105 sp<IConsumerListener> listener;
1106 { // Autolock scope
1107 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001108 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001109
1110 if (mCore->mIsAbandoned) {
1111 // It's not really an error to disconnect after the surface has
1112 // been abandoned; it should just be a no-op.
1113 return NO_ERROR;
1114 }
1115
1116 switch (api) {
1117 case NATIVE_WINDOW_API_EGL:
1118 case NATIVE_WINDOW_API_CPU:
1119 case NATIVE_WINDOW_API_MEDIA:
1120 case NATIVE_WINDOW_API_CAMERA:
1121 if (mCore->mConnectedApi == api) {
1122 mCore->freeAllBuffersLocked();
1123
1124 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001125 if (mCore->mConnectedProducerListener != NULL) {
1126 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001127 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001128 // This can fail if we're here because of the death
1129 // notification, but we just ignore it
1130 token->unlinkToDeath(
1131 static_cast<IBinder::DeathRecipient*>(this));
1132 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001133 mCore->mSingleBufferSlot =
1134 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001135 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001136 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001137 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001138 mCore->mDequeueCondition.broadcast();
1139 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001140 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001141 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001142 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001143 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001144 }
1145 break;
1146 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001147 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001148 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001149 break;
1150 }
1151 } // Autolock scope
1152
1153 // Call back without lock held
1154 if (listener != NULL) {
1155 listener->onBuffersReleased();
1156 }
1157
1158 return status;
1159}
1160
Jesse Hall399184a2014-03-03 15:42:54 -08001161status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001162 sp<IConsumerListener> listener;
1163 { // Autolock scope
1164 Mutex::Autolock _l(mCore->mMutex);
1165 mCore->mSidebandStream = stream;
1166 listener = mCore->mConsumerListener;
1167 } // Autolock scope
1168
1169 if (listener != NULL) {
1170 listener->onSidebandStreamChanged();
1171 }
Jesse Hall399184a2014-03-03 15:42:54 -08001172 return NO_ERROR;
1173}
1174
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001175void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1176 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001177 ATRACE_CALL();
1178 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001179 size_t newBufferCount = 0;
1180 uint32_t allocWidth = 0;
1181 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001182 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001183 uint32_t allocUsage = 0;
1184 { // Autolock scope
1185 Mutex::Autolock lock(mCore->mMutex);
1186 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001187
Dan Stoza9de72932015-04-16 17:28:43 -07001188 if (!mCore->mAllowAllocation) {
1189 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1190 "BufferQueue");
1191 return;
1192 }
1193
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001194 newBufferCount = mCore->mFreeSlots.size();
1195 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001196 return;
1197 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001198
Antoine Labour78014f32014-07-15 21:17:03 -07001199 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1200 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1201 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1202 allocUsage = usage | mCore->mConsumerUsageBits;
1203
1204 mCore->mIsAllocating = true;
1205 } // Autolock scope
1206
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001207 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001208 for (size_t i = 0; i < newBufferCount; ++i) {
1209 status_t result = NO_ERROR;
1210 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1211 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1212 if (result != NO_ERROR) {
1213 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1214 " %u, usage %u)", width, height, format, usage);
1215 Mutex::Autolock lock(mCore->mMutex);
1216 mCore->mIsAllocating = false;
1217 mCore->mIsAllocatingCondition.broadcast();
1218 return;
1219 }
1220 buffers.push_back(graphicBuffer);
1221 }
1222
1223 { // Autolock scope
1224 Mutex::Autolock lock(mCore->mMutex);
1225 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1226 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001227 PixelFormat checkFormat = format != 0 ?
1228 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001229 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1230 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1231 checkFormat != allocFormat || checkUsage != allocUsage) {
1232 // Something changed while we released the lock. Retry.
1233 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1234 mCore->mIsAllocating = false;
1235 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001236 continue;
1237 }
1238
Antoine Labour78014f32014-07-15 21:17:03 -07001239 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001240 if (mCore->mFreeSlots.empty()) {
1241 BQ_LOGV("allocateBuffers: a slot was occupied while "
1242 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001243 continue;
1244 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001245 auto slot = mCore->mFreeSlots.begin();
1246 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1247 mSlots[*slot].mGraphicBuffer = buffers[i];
1248 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001249
1250 // freeBufferLocked puts this slot on the free slots list. Since
1251 // we then attached a buffer, move the slot to free buffer list.
1252 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001253 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001254
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001255 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1256 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001257 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001258
Antoine Labour78014f32014-07-15 21:17:03 -07001259 mCore->mIsAllocating = false;
1260 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001261 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001262 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001263 }
1264}
1265
Dan Stoza9de72932015-04-16 17:28:43 -07001266status_t BufferQueueProducer::allowAllocation(bool allow) {
1267 ATRACE_CALL();
1268 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1269
1270 Mutex::Autolock lock(mCore->mMutex);
1271 mCore->mAllowAllocation = allow;
1272 return NO_ERROR;
1273}
1274
Dan Stoza812ed062015-06-02 15:45:22 -07001275status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1276 ATRACE_CALL();
1277 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1278
1279 Mutex::Autolock lock(mCore->mMutex);
1280 mCore->mGenerationNumber = generationNumber;
1281 return NO_ERROR;
1282}
1283
Dan Stozac6f30bd2015-06-08 09:32:50 -07001284String8 BufferQueueProducer::getConsumerName() const {
1285 ATRACE_CALL();
1286 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1287 return mConsumerName;
1288}
1289
Dan Stoza7dde5992015-05-22 09:51:44 -07001290uint64_t BufferQueueProducer::getNextFrameNumber() const {
1291 ATRACE_CALL();
1292
1293 Mutex::Autolock lock(mCore->mMutex);
1294 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1295 return nextFrameNumber;
1296}
1297
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001298status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1299 ATRACE_CALL();
1300 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1301
1302 Mutex::Autolock lock(mCore->mMutex);
1303 if (!singleBufferMode) {
1304 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1305 }
1306 mCore->mSingleBufferMode = singleBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001307 return NO_ERROR;
1308}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001309
Dan Stoza127fc632015-06-30 13:43:32 -07001310status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1311 ATRACE_CALL();
1312 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1313
1314 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001315 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1316 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1317 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1318 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1319 "available slots. Delta = %d", delta);
1320 return BAD_VALUE;
1321 }
1322
Dan Stoza127fc632015-06-30 13:43:32 -07001323 mDequeueTimeout = timeout;
1324 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001325
1326 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001327 return NO_ERROR;
1328}
1329
Dan Stoza289ade12014-02-28 11:17:17 -08001330void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1331 // If we're here, it means that a producer we were connected to died.
1332 // We're guaranteed that we are still connected to it because we remove
1333 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1334 // without synchronization here.
1335 int api = mCore->mConnectedApi;
1336 disconnect(api);
1337}
1338
1339} // namespace android