blob: 22a2d79f51ec3e04e743c2ce0e8ac856422b3d7d [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),
Dan Stoza127fc632015-06-30 13:43:32 -070046 mCallbackCondition(),
47 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080048
49BufferQueueProducer::~BufferQueueProducer() {}
50
51status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
52 ATRACE_CALL();
53 BQ_LOGV("requestBuffer: slot %d", slot);
54 Mutex::Autolock lock(mCore->mMutex);
55
56 if (mCore->mIsAbandoned) {
57 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
58 return NO_INIT;
59 }
60
Pablo Ceballos583b1b32015-09-03 18:23:52 -070061 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
62 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
63 return NO_INIT;
64 }
65
Dan Stoza3e96f192014-03-03 10:16:19 -080066 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080067 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080068 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080069 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070070 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080071 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070072 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080073 return BAD_VALUE;
74 }
75
76 mSlots[slot].mRequestBufferCalled = true;
77 *buf = mSlots[slot].mGraphicBuffer;
78 return NO_ERROR;
79}
80
Pablo Ceballosfa455352015-08-12 17:47:47 -070081status_t BufferQueueProducer::setMaxDequeuedBufferCount(
82 int maxDequeuedBuffers) {
83 ATRACE_CALL();
84 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
85 maxDequeuedBuffers);
86
87 sp<IConsumerListener> listener;
88 { // Autolock scope
89 Mutex::Autolock lock(mCore->mMutex);
90 mCore->waitWhileAllocatingLocked();
91
92 if (mCore->mIsAbandoned) {
93 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
94 "abandoned");
95 return NO_INIT;
96 }
97
98 // There must be no dequeued buffers when changing the buffer count.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080099 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700100 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballosfa455352015-08-12 17:47:47 -0700101 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
102 return BAD_VALUE;
103 }
104 }
105
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700106 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700107 bufferCount += maxDequeuedBuffers;
108
109 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
110 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
111 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
112 return BAD_VALUE;
113 }
114
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700115 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700116 if (bufferCount < minBufferSlots) {
117 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
118 "less than minimum %d", bufferCount, minBufferSlots);
119 return BAD_VALUE;
120 }
121
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700122 if (bufferCount > mCore->mMaxBufferCount) {
123 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700124 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
125 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
126 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
127 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700128 return BAD_VALUE;
129 }
130
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131 // Here we are guaranteed that the producer doesn't have any dequeued
132 // buffers and will release all of its buffer references. We don't
133 // clear the queue, however, so that currently queued buffers still
134 // get displayed.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800135 if (!mCore->adjustAvailableSlotsLocked(
136 maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount)) {
137 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue failed to adjust "
138 "the number of available slots. Delta = %d",
139 maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount);
140 return BAD_VALUE;
141 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700142 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800143 mCore->validateConsistencyLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700144 mCore->mDequeueCondition.broadcast();
145 listener = mCore->mConsumerListener;
146 } // Autolock scope
147
148 // Call back without lock held
149 if (listener != NULL) {
150 listener->onBuffersReleased();
151 }
152
153 return NO_ERROR;
154}
155
156status_t BufferQueueProducer::setAsyncMode(bool async) {
157 ATRACE_CALL();
158 BQ_LOGV("setAsyncMode: async = %d", async);
159
160 sp<IConsumerListener> listener;
161 { // Autolock scope
162 Mutex::Autolock lock(mCore->mMutex);
163 mCore->waitWhileAllocatingLocked();
164
165 if (mCore->mIsAbandoned) {
166 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
167 return NO_INIT;
168 }
169
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700170 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700171 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
172 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700173 BQ_LOGE("setAsyncMode(%d): this call would cause the "
174 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700175 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
176 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
177 mCore->mMaxDequeuedBufferCount,
178 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700179 return BAD_VALUE;
180 }
181
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800182 int delta = mCore->getMaxBufferCountLocked(async,
183 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
184 - mCore->getMaxBufferCountLocked();
185
186 if (!mCore->adjustAvailableSlotsLocked(delta)) {
187 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
188 "available slots. Delta = %d", delta);
189 return BAD_VALUE;
190 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700191 mCore->mAsyncMode = async;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800192 mCore->validateConsistencyLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700193 mCore->mDequeueCondition.broadcast();
194 listener = mCore->mConsumerListener;
195 } // Autolock scope
196
197 // Call back without lock held
198 if (listener != NULL) {
199 listener->onBuffersReleased();
200 }
201 return NO_ERROR;
202}
203
Dan Stoza5ecfb682016-01-04 17:01:02 -0800204int BufferQueueProducer::getFreeBufferLocked() const {
205 if (mCore->mFreeBuffers.empty()) {
206 return BufferQueueCore::INVALID_BUFFER_SLOT;
207 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800208 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800209 mCore->mFreeBuffers.pop_front();
210 return slot;
211}
212
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800213int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800214 if (mCore->mFreeSlots.empty()) {
215 return BufferQueueCore::INVALID_BUFFER_SLOT;
216 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800217 auto slot = mCore->mFreeSlots.begin();
218 mCore->mFreeSlots.erase(slot);
219 return *slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800220}
221
222status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800223 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800224 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
225 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800226 bool tryAgain = true;
227 while (tryAgain) {
228 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800229 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800230 return NO_INIT;
231 }
232
Dan Stoza9f3053d2014-03-06 15:14:33 -0800233 int dequeuedCount = 0;
234 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700236 if (mSlots[s].mBufferState.isDequeued()) {
237 ++dequeuedCount;
238 }
239 if (mSlots[s].mBufferState.isAcquired()) {
240 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800241 }
242 }
243
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700244 // Producers are not allowed to dequeue more than
245 // mMaxDequeuedBufferCount buffers.
246 // This check is only done if a buffer has already been queued
247 if (mCore->mBufferHasBeenQueued &&
248 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
249 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800250 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800251 return INVALID_OPERATION;
252 }
253
Dan Stoza0de7ea72015-04-23 13:20:51 -0700254 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
255
Dan Stozaae3c3682014-04-18 15:43:35 -0700256 // If we disconnect and reconnect quickly, we can be in a state where
257 // our slots are empty but we have many buffers in the queue. This can
258 // cause us to run out of memory if we outrun the consumer. Wait here if
259 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800260 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700261 bool tooManyBuffers = mCore->mQueue.size()
262 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700263 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800264 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700265 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700266 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700267 // If in single buffer mode and a shared buffer exists, always
268 // return it.
269 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
270 BufferQueueCore::INVALID_BUFFER_SLOT) {
271 *found = mCore->mSingleBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800272 } else {
273 if (caller == FreeSlotCaller::Dequeue) {
274 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800275 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800276 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
277 *found = slot;
278 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800280 }
281 } else {
282 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800283 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
285 *found = slot;
286 } else {
287 *found = getFreeBufferLocked();
288 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700289 }
290 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 }
292
293 // If no buffer is found, or if the queue has too many buffers
294 // outstanding, wait for a buffer to be acquired or released, or for the
295 // max buffer count to change.
296 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
297 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800298 if (tryAgain) {
299 // Return an error if we're in non-blocking mode (producer and
300 // consumer are controlled by the application).
301 // However, the consumer is allowed to briefly acquire an extra
302 // buffer (which could cause us to have to wait here), which is
303 // okay, since it is only used to implement an atomic acquire +
304 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700305 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800306 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
307 return WOULD_BLOCK;
308 }
Dan Stoza127fc632015-06-30 13:43:32 -0700309 if (mDequeueTimeout >= 0) {
310 status_t result = mCore->mDequeueCondition.waitRelative(
311 mCore->mMutex, mDequeueTimeout);
312 if (result == TIMED_OUT) {
313 return result;
314 }
315 } else {
316 mCore->mDequeueCondition.wait(mCore->mMutex);
317 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800318 }
319 } // while (tryAgain)
320
321 return NO_ERROR;
322}
323
Dan Stoza289ade12014-02-28 11:17:17 -0800324status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700325 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
326 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800327 ATRACE_CALL();
328 { // Autolock scope
329 Mutex::Autolock lock(mCore->mMutex);
330 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700331
332 if (mCore->mIsAbandoned) {
333 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
334 return NO_INIT;
335 }
336
337 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
338 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
339 return NO_INIT;
340 }
Dan Stoza289ade12014-02-28 11:17:17 -0800341 } // Autolock scope
342
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700343 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
344 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800345
346 if ((width && !height) || (!width && height)) {
347 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
348 return BAD_VALUE;
349 }
350
351 status_t returnFlags = NO_ERROR;
352 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
353 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800354 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800355
356 { // Autolock scope
357 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700358 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800359
360 if (format == 0) {
361 format = mCore->mDefaultBufferFormat;
362 }
363
364 // Enable the usage bits the consumer requested
365 usage |= mCore->mConsumerUsageBits;
366
Dan Stoza9de72932015-04-16 17:28:43 -0700367 const bool useDefaultSize = !width && !height;
368 if (useDefaultSize) {
369 width = mCore->mDefaultWidth;
370 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371 }
Dan Stoza289ade12014-02-28 11:17:17 -0800372
Dan Stoza9de72932015-04-16 17:28:43 -0700373 int found = BufferItem::INVALID_BUFFER_SLOT;
374 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800375 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800376 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700377 if (status != NO_ERROR) {
378 return status;
379 }
380
381 // This should not happen
382 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
383 BQ_LOGE("dequeueBuffer: no available buffer slots");
384 return -EBUSY;
385 }
386
387 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
388
389 // If we are not allowed to allocate new buffers,
390 // waitForFreeSlotThenRelock must have returned a slot containing a
391 // buffer. If this buffer would require reallocation to meet the
392 // requested attributes, we free it and attempt to get another one.
393 if (!mCore->mAllowAllocation) {
394 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800395 if (mCore->mSingleBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700396 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
397 "buffer");
398 return BAD_VALUE;
399 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800400 mCore->mFreeSlots.insert(found);
401 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700402 found = BufferItem::INVALID_BUFFER_SLOT;
403 continue;
404 }
405 }
Dan Stoza289ade12014-02-28 11:17:17 -0800406 }
407
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800408 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
409 if (mCore->mSingleBufferSlot == found &&
410 buffer->needsReallocation(width, height, format, usage)) {
411 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
412 "buffer");
413
414 return BAD_VALUE;
415 }
416
417 if (mCore->mSingleBufferSlot != found) {
418 mCore->mActiveBuffers.insert(found);
419 }
Dan Stoza289ade12014-02-28 11:17:17 -0800420 *outSlot = found;
421 ATRACE_BUFFER_INDEX(found);
422
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800423 attachedByConsumer = mSlots[found].mNeedsReallocation;
424 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700426 mSlots[found].mBufferState.dequeue();
427
428 // If single buffer mode has just been enabled, cache the slot of the
429 // first buffer that is dequeued and mark it as the shared buffer.
430 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
431 BufferQueueCore::INVALID_BUFFER_SLOT) {
432 mCore->mSingleBufferSlot = found;
433 mSlots[found].mBufferState.mShared = true;
434 }
Dan Stoza289ade12014-02-28 11:17:17 -0800435
Dan Stoza289ade12014-02-28 11:17:17 -0800436 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700437 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800438 {
439 mSlots[found].mAcquireCalled = false;
440 mSlots[found].mGraphicBuffer = NULL;
441 mSlots[found].mRequestBufferCalled = false;
442 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
443 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
444 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800445 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700446 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800447
448 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800449 } else {
450 // We add 1 because that will be the frame number when this buffer
451 // is queued
452 mCore->mBufferAge =
453 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800454 }
455
Dan Stoza800b41a2015-04-28 14:20:04 -0700456 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
457 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800458
Dan Stoza289ade12014-02-28 11:17:17 -0800459 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
460 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
461 "slot=%d w=%d h=%d format=%u",
462 found, buffer->width, buffer->height, buffer->format);
463 }
464
465 eglDisplay = mSlots[found].mEglDisplay;
466 eglFence = mSlots[found].mEglFence;
467 *outFence = mSlots[found].mFence;
468 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
469 mSlots[found].mFence = Fence::NO_FENCE;
470 } // Autolock scope
471
472 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
473 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700474 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800475 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800476 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800477 { // Autolock scope
478 Mutex::Autolock lock(mCore->mMutex);
479
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
481 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
482 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
483 }
484
485 mCore->mIsAllocating = false;
486 mCore->mIsAllocatingCondition.broadcast();
487
488 if (graphicBuffer == NULL) {
489 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
490 return error;
491 }
492
Dan Stoza289ade12014-02-28 11:17:17 -0800493 if (mCore->mIsAbandoned) {
494 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
495 return NO_INIT;
496 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800497
498 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800499 } // Autolock scope
500 }
501
Dan Stoza9f3053d2014-03-06 15:14:33 -0800502 if (attachedByConsumer) {
503 returnFlags |= BUFFER_NEEDS_REALLOCATION;
504 }
505
Dan Stoza289ade12014-02-28 11:17:17 -0800506 if (eglFence != EGL_NO_SYNC_KHR) {
507 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
508 1000000000);
509 // If something goes wrong, log the error, but return the buffer without
510 // synchronizing access to it. It's too late at this point to abort the
511 // dequeue operation.
512 if (result == EGL_FALSE) {
513 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
514 eglGetError());
515 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
516 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
517 }
518 eglDestroySyncKHR(eglDisplay, eglFence);
519 }
520
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700521 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
522 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800523 mSlots[*outSlot].mFrameNumber,
524 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
525
526 return returnFlags;
527}
528
Dan Stoza9f3053d2014-03-06 15:14:33 -0800529status_t BufferQueueProducer::detachBuffer(int slot) {
530 ATRACE_CALL();
531 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700532 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800533 Mutex::Autolock lock(mCore->mMutex);
534
535 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700536 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800537 return NO_INIT;
538 }
539
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700540 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700541 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700542 return NO_INIT;
543 }
544
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800545 if (mCore->mSingleBufferMode || mCore->mSingleBufferSlot == slot) {
546 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700547 return BAD_VALUE;
548 }
549
Dan Stoza9f3053d2014-03-06 15:14:33 -0800550 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700551 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800552 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
553 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700554 } else if (!mSlots[slot].mBufferState.isDequeued()) {
555 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
556 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800557 return BAD_VALUE;
558 } else if (!mSlots[slot].mRequestBufferCalled) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700559 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800560 slot);
561 return BAD_VALUE;
562 }
563
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700564 mSlots[slot].mBufferState.detachProducer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800565 mCore->mActiveBuffers.erase(slot);
566 mCore->mFreeSlots.insert(slot);
567 mCore->clearBufferSlotLocked(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800568 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700569 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800570
571 return NO_ERROR;
572}
573
Dan Stozad9822a32014-03-28 15:25:31 -0700574status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
575 sp<Fence>* outFence) {
576 ATRACE_CALL();
577
578 if (outBuffer == NULL) {
579 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
580 return BAD_VALUE;
581 } else if (outFence == NULL) {
582 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
583 return BAD_VALUE;
584 }
585
586 Mutex::Autolock lock(mCore->mMutex);
587
588 if (mCore->mIsAbandoned) {
589 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
590 return NO_INIT;
591 }
592
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700593 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
594 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
595 return NO_INIT;
596 }
597
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700598 if (mCore->mSingleBufferMode) {
599 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
600 "mode");
601 return BAD_VALUE;
602 }
603
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700604 mCore->waitWhileAllocatingLocked();
605
Dan Stoza0de7ea72015-04-23 13:20:51 -0700606 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000607 return NO_MEMORY;
608 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700609
Dan Stoza0de7ea72015-04-23 13:20:51 -0700610 int found = mCore->mFreeBuffers.front();
611 mCore->mFreeBuffers.remove(found);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800612 mCore->mFreeSlots.insert(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700613
Dan Stozad9822a32014-03-28 15:25:31 -0700614 BQ_LOGV("detachNextBuffer detached slot %d", found);
615
616 *outBuffer = mSlots[found].mGraphicBuffer;
617 *outFence = mSlots[found].mFence;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800618 mCore->clearBufferSlotLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700619 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700620
621 return NO_ERROR;
622}
623
Dan Stoza9f3053d2014-03-06 15:14:33 -0800624status_t BufferQueueProducer::attachBuffer(int* outSlot,
625 const sp<android::GraphicBuffer>& buffer) {
626 ATRACE_CALL();
627
628 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700629 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800630 return BAD_VALUE;
631 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700632 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800633 return BAD_VALUE;
634 }
635
636 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700637
638 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700639 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700640 return NO_INIT;
641 }
642
643 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700644 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700645 return NO_INIT;
646 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800647
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700648 if (mCore->mSingleBufferMode) {
Craig Donner05249fc2016-01-15 19:33:55 -0800649 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700650 return BAD_VALUE;
651 }
652
Dan Stoza812ed062015-06-02 15:45:22 -0700653 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
654 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
655 "[queue %u]", buffer->getGenerationNumber(),
656 mCore->mGenerationNumber);
657 return BAD_VALUE;
658 }
659
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700660 mCore->waitWhileAllocatingLocked();
661
Dan Stoza9f3053d2014-03-06 15:14:33 -0800662 status_t returnFlags = NO_ERROR;
663 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800664 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800665 if (status != NO_ERROR) {
666 return status;
667 }
668
669 // This should not happen
670 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700671 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800672 return -EBUSY;
673 }
674
675 *outSlot = found;
676 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700677 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800678 *outSlot, returnFlags);
679
680 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700681 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800682 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
683 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700684 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800685 mSlots[*outSlot].mAcquireCalled = false;
686 mCore->mActiveBuffers.insert(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700687 mCore->validateConsistencyLocked();
688
Dan Stoza9f3053d2014-03-06 15:14:33 -0800689 return returnFlags;
690}
691
Dan Stoza289ade12014-02-28 11:17:17 -0800692status_t BufferQueueProducer::queueBuffer(int slot,
693 const QueueBufferInput &input, QueueBufferOutput *output) {
694 ATRACE_CALL();
695 ATRACE_BUFFER_INDEX(slot);
696
697 int64_t timestamp;
698 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800699 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700700 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800701 int scalingMode;
702 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700703 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800704 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800705 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700706 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700707 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800708
709 if (fence == NULL) {
710 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800711 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800712 }
713
714 switch (scalingMode) {
715 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
716 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
717 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
718 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
719 break;
720 default:
721 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800722 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800723 }
724
Dan Stoza8dc55392014-11-04 11:37:46 -0800725 sp<IConsumerListener> frameAvailableListener;
726 sp<IConsumerListener> frameReplacedListener;
727 int callbackTicket = 0;
728 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800729 { // Autolock scope
730 Mutex::Autolock lock(mCore->mMutex);
731
732 if (mCore->mIsAbandoned) {
733 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
734 return NO_INIT;
735 }
736
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700737 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
738 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
739 return NO_INIT;
740 }
741
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800742 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800743 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800744 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800745 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700746 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800747 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700748 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800749 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800750 } else if (!mSlots[slot].mRequestBufferCalled) {
751 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
752 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800753 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800754 }
755
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800756 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700757 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800758 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800759 crop.left, crop.top, crop.right, crop.bottom, transform,
760 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800761
762 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
763 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700764 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800765 crop.intersect(bufferRect, &croppedRect);
766 if (croppedRect != crop) {
767 BQ_LOGE("queueBuffer: crop rect is not contained within the "
768 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800769 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800770 }
771
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800772 // Override UNKNOWN dataspace with consumer default
773 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
774 dataSpace = mCore->mDefaultBufferDataSpace;
775 }
776
Dan Stoza289ade12014-02-28 11:17:17 -0800777 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700778 mSlots[slot].mBufferState.queue();
779
Dan Stoza289ade12014-02-28 11:17:17 -0800780 ++mCore->mFrameCounter;
781 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
782
Dan Stoza289ade12014-02-28 11:17:17 -0800783 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
784 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
785 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800786 item.mTransform = transform &
787 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800788 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800789 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
790 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800791 item.mTimestamp = timestamp;
792 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800793 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800794 item.mFrameNumber = mCore->mFrameCounter;
795 item.mSlot = slot;
796 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700797 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700798 mCore->mDequeueBufferCannotBlock ||
799 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700800 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700801 item.mSingleBufferMode = mCore->mSingleBufferMode;
802 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800803
Ruben Brunk1681d952014-06-27 15:51:55 -0700804 mStickyTransform = stickyTransform;
805
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700806 // Cache the shared buffer data so that the BufferItem can be recreated.
807 if (mCore->mSingleBufferMode) {
808 mCore->mSingleBufferCache.crop = crop;
809 mCore->mSingleBufferCache.transform = transform;
810 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
811 scalingMode);
812 mCore->mSingleBufferCache.dataspace = dataSpace;
813 }
814
Dan Stoza289ade12014-02-28 11:17:17 -0800815 if (mCore->mQueue.empty()) {
816 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
817 // and simply queue this buffer
818 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800819 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800820 } else {
821 // When the queue is not empty, we need to look at the front buffer
822 // state to see if we need to replace it
823 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
824 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800825
826 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700827 mSlots[front->mSlot].mBufferState.freeQueued();
828
829 // After leaving single buffer mode, the shared buffer will
830 // still be around. Mark it as no longer shared if this
831 // operation causes it to be free.
832 if (!mCore->mSingleBufferMode &&
833 mSlots[front->mSlot].mBufferState.isFree()) {
834 mSlots[front->mSlot].mBufferState.mShared = false;
835 }
836 // Don't put the shared buffer on the free list.
837 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800838 mCore->mActiveBuffers.erase(front->mSlot);
839 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700840 }
Dan Stoza289ade12014-02-28 11:17:17 -0800841 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800842
Dan Stoza289ade12014-02-28 11:17:17 -0800843 // Overwrite the droppable buffer with the incoming one
844 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800845 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800846 } else {
847 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800848 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800849 }
850 }
851
852 mCore->mBufferHasBeenQueued = true;
853 mCore->mDequeueCondition.broadcast();
854
855 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800856 mCore->mTransformHint,
857 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800858
859 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800860
861 // Take a ticket for the callback functions
862 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700863
864 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800865 } // Autolock scope
866
Dan Stoza8dc55392014-11-04 11:37:46 -0800867 // Don't send the GraphicBuffer through the callback, and don't send
868 // the slot number, since the consumer shouldn't need it
869 item.mGraphicBuffer.clear();
870 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
871
872 // Call back without the main BufferQueue lock held, but with the callback
873 // lock held so we can ensure that callbacks occur in order
874 {
875 Mutex::Autolock lock(mCallbackMutex);
876 while (callbackTicket != mCurrentCallbackTicket) {
877 mCallbackCondition.wait(mCallbackMutex);
878 }
879
880 if (frameAvailableListener != NULL) {
881 frameAvailableListener->onFrameAvailable(item);
882 } else if (frameReplacedListener != NULL) {
883 frameReplacedListener->onFrameReplaced(item);
884 }
885
886 ++mCurrentCallbackTicket;
887 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800888 }
889
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000890 // Wait without lock held
891 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
892 // Waiting here allows for two full buffers to be queued but not a
893 // third. In the event that frames take varying time, this makes a
894 // small trade-off in favor of latency rather than throughput.
895 mLastQueueBufferFence->waitForever("Throttling EGL Production");
896 mLastQueueBufferFence = fence;
897 }
898
Dan Stoza289ade12014-02-28 11:17:17 -0800899 return NO_ERROR;
900}
901
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700902status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800903 ATRACE_CALL();
904 BQ_LOGV("cancelBuffer: slot %d", slot);
905 Mutex::Autolock lock(mCore->mMutex);
906
907 if (mCore->mIsAbandoned) {
908 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700909 return NO_INIT;
910 }
911
912 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
913 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
914 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800915 }
916
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700917 if (mCore->mSingleBufferMode) {
918 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
919 return BAD_VALUE;
920 }
921
Dan Stoza3e96f192014-03-03 10:16:19 -0800922 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800923 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800924 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700925 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700926 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800927 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700928 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700929 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 } else if (fence == NULL) {
931 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700932 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800933 }
934
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700935 mSlots[slot].mBufferState.cancel();
936
937 // After leaving single buffer mode, the shared buffer will still be around.
938 // Mark it as no longer shared if this operation causes it to be free.
939 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
940 mSlots[slot].mBufferState.mShared = false;
941 }
942
943 // Don't put the shared buffer on the free list.
944 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800945 mCore->mActiveBuffers.erase(slot);
946 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700947 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800948
Dan Stoza289ade12014-02-28 11:17:17 -0800949 mSlots[slot].mFence = fence;
950 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700951 mCore->validateConsistencyLocked();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700952
953 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800954}
955
956int BufferQueueProducer::query(int what, int *outValue) {
957 ATRACE_CALL();
958 Mutex::Autolock lock(mCore->mMutex);
959
960 if (outValue == NULL) {
961 BQ_LOGE("query: outValue was NULL");
962 return BAD_VALUE;
963 }
964
965 if (mCore->mIsAbandoned) {
966 BQ_LOGE("query: BufferQueue has been abandoned");
967 return NO_INIT;
968 }
969
970 int value;
971 switch (what) {
972 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800973 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800974 break;
975 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800976 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800977 break;
978 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800979 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800980 break;
981 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700982 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800983 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700984 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800985 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700986 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800987 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
988 value = (mCore->mQueue.size() > 1);
989 break;
990 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800991 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800992 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800993 case NATIVE_WINDOW_DEFAULT_DATASPACE:
994 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
995 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800996 case NATIVE_WINDOW_BUFFER_AGE:
997 if (mCore->mBufferAge > INT32_MAX) {
998 value = 0;
999 } else {
1000 value = static_cast<int32_t>(mCore->mBufferAge);
1001 }
1002 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001003 default:
1004 return BAD_VALUE;
1005 }
1006
1007 BQ_LOGV("query: %d? %d", what, value);
1008 *outValue = value;
1009 return NO_ERROR;
1010}
1011
Dan Stozaf0eaf252014-03-21 13:05:51 -07001012status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001013 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1014 ATRACE_CALL();
1015 Mutex::Autolock lock(mCore->mMutex);
1016 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001017 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -08001018 producerControlledByApp ? "true" : "false");
1019
Dan Stozaae3c3682014-04-18 15:43:35 -07001020 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001021 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -07001022 return NO_INIT;
1023 }
Dan Stoza289ade12014-02-28 11:17:17 -08001024
Dan Stozaae3c3682014-04-18 15:43:35 -07001025 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001026 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -07001027 return NO_INIT;
1028 }
Dan Stoza289ade12014-02-28 11:17:17 -08001029
Dan Stozaae3c3682014-04-18 15:43:35 -07001030 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001031 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -07001032 return BAD_VALUE;
1033 }
Dan Stoza289ade12014-02-28 11:17:17 -08001034
Dan Stozaae3c3682014-04-18 15:43:35 -07001035 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001036 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -07001037 mCore->mConnectedApi, api);
1038 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001039 }
1040
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001041 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1042 mDequeueTimeout < 0 ?
1043 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1044 mCore->mMaxBufferCount) -
1045 mCore->getMaxBufferCountLocked();
1046 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1047 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1048 "slots. Delta = %d", delta);
1049 return BAD_VALUE;
1050 }
1051
Dan Stoza289ade12014-02-28 11:17:17 -08001052 int status = NO_ERROR;
1053 switch (api) {
1054 case NATIVE_WINDOW_API_EGL:
1055 case NATIVE_WINDOW_API_CPU:
1056 case NATIVE_WINDOW_API_MEDIA:
1057 case NATIVE_WINDOW_API_CAMERA:
1058 mCore->mConnectedApi = api;
1059 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001060 mCore->mTransformHint,
1061 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001062
1063 // Set up a death notification so that we can disconnect
1064 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -07001065 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -08001066 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1067 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -08001068 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -07001069 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001070 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001071 strerror(-status), status);
1072 }
1073 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001074 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001075 break;
1076 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001077 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001078 status = BAD_VALUE;
1079 break;
1080 }
1081
1082 mCore->mBufferHasBeenQueued = false;
Dan Stoza127fc632015-06-30 13:43:32 -07001083 mCore->mDequeueBufferCannotBlock = false;
1084 if (mDequeueTimeout < 0) {
1085 mCore->mDequeueBufferCannotBlock =
1086 mCore->mConsumerControlledByApp && producerControlledByApp;
1087 }
Dan Stoza289ade12014-02-28 11:17:17 -08001088
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001089 mCore->mAllowAllocation = true;
1090 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001091 return status;
1092}
1093
1094status_t BufferQueueProducer::disconnect(int api) {
1095 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001096 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001097
1098 int status = NO_ERROR;
1099 sp<IConsumerListener> listener;
1100 { // Autolock scope
1101 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001102 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001103
1104 if (mCore->mIsAbandoned) {
1105 // It's not really an error to disconnect after the surface has
1106 // been abandoned; it should just be a no-op.
1107 return NO_ERROR;
1108 }
1109
1110 switch (api) {
1111 case NATIVE_WINDOW_API_EGL:
1112 case NATIVE_WINDOW_API_CPU:
1113 case NATIVE_WINDOW_API_MEDIA:
1114 case NATIVE_WINDOW_API_CAMERA:
1115 if (mCore->mConnectedApi == api) {
1116 mCore->freeAllBuffersLocked();
1117
1118 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001119 if (mCore->mConnectedProducerListener != NULL) {
1120 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001121 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001122 // This can fail if we're here because of the death
1123 // notification, but we just ignore it
1124 token->unlinkToDeath(
1125 static_cast<IBinder::DeathRecipient*>(this));
1126 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001127 mCore->mSingleBufferSlot =
1128 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001129 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001130 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001131 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001132 mCore->mDequeueCondition.broadcast();
1133 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001134 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001135 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001136 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001137 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001138 }
1139 break;
1140 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001141 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001142 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001143 break;
1144 }
1145 } // Autolock scope
1146
1147 // Call back without lock held
1148 if (listener != NULL) {
1149 listener->onBuffersReleased();
1150 }
1151
1152 return status;
1153}
1154
Jesse Hall399184a2014-03-03 15:42:54 -08001155status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001156 sp<IConsumerListener> listener;
1157 { // Autolock scope
1158 Mutex::Autolock _l(mCore->mMutex);
1159 mCore->mSidebandStream = stream;
1160 listener = mCore->mConsumerListener;
1161 } // Autolock scope
1162
1163 if (listener != NULL) {
1164 listener->onSidebandStreamChanged();
1165 }
Jesse Hall399184a2014-03-03 15:42:54 -08001166 return NO_ERROR;
1167}
1168
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001169void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1170 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001171 ATRACE_CALL();
1172 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001173 size_t newBufferCount = 0;
1174 uint32_t allocWidth = 0;
1175 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001176 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001177 uint32_t allocUsage = 0;
1178 { // Autolock scope
1179 Mutex::Autolock lock(mCore->mMutex);
1180 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001181
Dan Stoza9de72932015-04-16 17:28:43 -07001182 if (!mCore->mAllowAllocation) {
1183 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1184 "BufferQueue");
1185 return;
1186 }
1187
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001188 newBufferCount = mCore->mFreeSlots.size();
1189 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001190 return;
1191 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001192
Antoine Labour78014f32014-07-15 21:17:03 -07001193 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1194 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1195 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1196 allocUsage = usage | mCore->mConsumerUsageBits;
1197
1198 mCore->mIsAllocating = true;
1199 } // Autolock scope
1200
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001201 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001202 for (size_t i = 0; i < newBufferCount; ++i) {
1203 status_t result = NO_ERROR;
1204 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1205 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1206 if (result != NO_ERROR) {
1207 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1208 " %u, usage %u)", width, height, format, usage);
1209 Mutex::Autolock lock(mCore->mMutex);
1210 mCore->mIsAllocating = false;
1211 mCore->mIsAllocatingCondition.broadcast();
1212 return;
1213 }
1214 buffers.push_back(graphicBuffer);
1215 }
1216
1217 { // Autolock scope
1218 Mutex::Autolock lock(mCore->mMutex);
1219 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1220 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001221 PixelFormat checkFormat = format != 0 ?
1222 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001223 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1224 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1225 checkFormat != allocFormat || checkUsage != allocUsage) {
1226 // Something changed while we released the lock. Retry.
1227 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1228 mCore->mIsAllocating = false;
1229 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001230 continue;
1231 }
1232
Antoine Labour78014f32014-07-15 21:17:03 -07001233 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001234 if (mCore->mFreeSlots.empty()) {
1235 BQ_LOGV("allocateBuffers: a slot was occupied while "
1236 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001237 continue;
1238 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001239 auto slot = mCore->mFreeSlots.begin();
1240 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1241 mSlots[*slot].mGraphicBuffer = buffers[i];
1242 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001243
1244 // freeBufferLocked puts this slot on the free slots list. Since
1245 // we then attached a buffer, move the slot to free buffer list.
1246 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001247 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001248
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001249 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1250 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001251 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001252
Antoine Labour78014f32014-07-15 21:17:03 -07001253 mCore->mIsAllocating = false;
1254 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001255 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001256 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001257 }
1258}
1259
Dan Stoza9de72932015-04-16 17:28:43 -07001260status_t BufferQueueProducer::allowAllocation(bool allow) {
1261 ATRACE_CALL();
1262 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1263
1264 Mutex::Autolock lock(mCore->mMutex);
1265 mCore->mAllowAllocation = allow;
1266 return NO_ERROR;
1267}
1268
Dan Stoza812ed062015-06-02 15:45:22 -07001269status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1270 ATRACE_CALL();
1271 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1272
1273 Mutex::Autolock lock(mCore->mMutex);
1274 mCore->mGenerationNumber = generationNumber;
1275 return NO_ERROR;
1276}
1277
Dan Stozac6f30bd2015-06-08 09:32:50 -07001278String8 BufferQueueProducer::getConsumerName() const {
1279 ATRACE_CALL();
1280 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1281 return mConsumerName;
1282}
1283
Dan Stoza7dde5992015-05-22 09:51:44 -07001284uint64_t BufferQueueProducer::getNextFrameNumber() const {
1285 ATRACE_CALL();
1286
1287 Mutex::Autolock lock(mCore->mMutex);
1288 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1289 return nextFrameNumber;
1290}
1291
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001292status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1293 ATRACE_CALL();
1294 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1295
1296 Mutex::Autolock lock(mCore->mMutex);
1297 if (!singleBufferMode) {
1298 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1299 }
1300 mCore->mSingleBufferMode = singleBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001301 return NO_ERROR;
1302}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001303
Dan Stoza127fc632015-06-30 13:43:32 -07001304status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1305 ATRACE_CALL();
1306 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1307
1308 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001309 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1310 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1311 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1312 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1313 "available slots. Delta = %d", delta);
1314 return BAD_VALUE;
1315 }
1316
Dan Stoza127fc632015-06-30 13:43:32 -07001317 mDequeueTimeout = timeout;
1318 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001319 mCore->validateConsistencyLocked();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001320 return NO_ERROR;
1321}
1322
Dan Stoza289ade12014-02-28 11:17:17 -08001323void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1324 // If we're here, it means that a producer we were connected to died.
1325 // We're guaranteed that we are still connected to it because we remove
1326 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1327 // without synchronization here.
1328 int api = mCore->mConnectedApi;
1329 disconnect(api);
1330}
1331
1332} // namespace android