blob: 3411dcaafe757483e05c7aca8f337fb4e62b5447 [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>
John Reck1a61da52016-04-28 13:18:15 -070034#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080035#include <gui/IConsumerListener.h>
36#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038
39#include <utils/Log.h>
40#include <utils/Trace.h>
41
42namespace android {
43
Irvel468051e2016-06-13 16:44:44 -070044BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
45 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080046 mCore(core),
47 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070048 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070049 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070050 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080051 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070052 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080053 mCallbackMutex(),
54 mNextCallbackTicket(0),
55 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070056 mCallbackCondition(),
57 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080058
59BufferQueueProducer::~BufferQueueProducer() {}
60
61status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
62 ATRACE_CALL();
63 BQ_LOGV("requestBuffer: slot %d", slot);
64 Mutex::Autolock lock(mCore->mMutex);
65
66 if (mCore->mIsAbandoned) {
67 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
68 return NO_INIT;
69 }
70
Pablo Ceballos583b1b32015-09-03 18:23:52 -070071 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
72 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
73 return NO_INIT;
74 }
75
Dan Stoza3e96f192014-03-03 10:16:19 -080076 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080077 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080078 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080079 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070080 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080081 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070082 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080083 return BAD_VALUE;
84 }
85
86 mSlots[slot].mRequestBufferCalled = true;
87 *buf = mSlots[slot].mGraphicBuffer;
88 return NO_ERROR;
89}
90
Pablo Ceballosfa455352015-08-12 17:47:47 -070091status_t BufferQueueProducer::setMaxDequeuedBufferCount(
92 int maxDequeuedBuffers) {
93 ATRACE_CALL();
94 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
95 maxDequeuedBuffers);
96
Pablo Ceballos981066c2016-02-18 12:54:37 -080097 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070098 { // Autolock scope
99 Mutex::Autolock lock(mCore->mMutex);
100 mCore->waitWhileAllocatingLocked();
101
102 if (mCore->mIsAbandoned) {
103 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
104 "abandoned");
105 return NO_INIT;
106 }
107
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700108 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
109 return NO_ERROR;
110 }
111
Pablo Ceballos72daab62015-12-07 16:38:43 -0800112 // The new maxDequeuedBuffer count should not be violated by the number
113 // of currently dequeued buffers
114 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800115 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700116 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800117 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700118 }
119 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800120 if (dequeuedCount > maxDequeuedBuffers) {
121 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
122 "count (%d) exceeds the current dequeued buffer count (%d)",
123 maxDequeuedBuffers, dequeuedCount);
124 return BAD_VALUE;
125 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700126
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700127 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700128 bufferCount += maxDequeuedBuffers;
129
130 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
131 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
132 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
133 return BAD_VALUE;
134 }
135
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700136 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137 if (bufferCount < minBufferSlots) {
138 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
139 "less than minimum %d", bufferCount, minBufferSlots);
140 return BAD_VALUE;
141 }
142
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700143 if (bufferCount > mCore->mMaxBufferCount) {
144 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700145 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
146 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
147 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
148 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700149 return BAD_VALUE;
150 }
151
Pablo Ceballos72daab62015-12-07 16:38:43 -0800152 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800153 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800154 return BAD_VALUE;
155 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700156 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800157 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800159 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800160 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700161 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700162 } // Autolock scope
163
164 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800165 if (listener != NULL) {
166 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700167 }
168
169 return NO_ERROR;
170}
171
172status_t BufferQueueProducer::setAsyncMode(bool async) {
173 ATRACE_CALL();
174 BQ_LOGV("setAsyncMode: async = %d", async);
175
Pablo Ceballos981066c2016-02-18 12:54:37 -0800176 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700177 { // Autolock scope
178 Mutex::Autolock lock(mCore->mMutex);
179 mCore->waitWhileAllocatingLocked();
180
181 if (mCore->mIsAbandoned) {
182 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
183 return NO_INIT;
184 }
185
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700186 if (async == mCore->mAsyncMode) {
187 return NO_ERROR;
188 }
189
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700190 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700191 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
192 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700193 BQ_LOGE("setAsyncMode(%d): this call would cause the "
194 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700195 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
196 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
197 mCore->mMaxDequeuedBufferCount,
198 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700199 return BAD_VALUE;
200 }
201
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800202 int delta = mCore->getMaxBufferCountLocked(async,
203 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
204 - mCore->getMaxBufferCountLocked();
205
Pablo Ceballos981066c2016-02-18 12:54:37 -0800206 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800207 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
208 "available slots. Delta = %d", delta);
209 return BAD_VALUE;
210 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700211 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800212 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700213 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700214 if (delta < 0) {
215 listener = mCore->mConsumerListener;
216 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700217 } // Autolock scope
218
219 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800220 if (listener != NULL) {
221 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700222 }
223 return NO_ERROR;
224}
225
Dan Stoza5ecfb682016-01-04 17:01:02 -0800226int BufferQueueProducer::getFreeBufferLocked() const {
227 if (mCore->mFreeBuffers.empty()) {
228 return BufferQueueCore::INVALID_BUFFER_SLOT;
229 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800230 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800231 mCore->mFreeBuffers.pop_front();
232 return slot;
233}
234
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800236 if (mCore->mFreeSlots.empty()) {
237 return BufferQueueCore::INVALID_BUFFER_SLOT;
238 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800239 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800240 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800241 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800242}
243
244status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800245 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800246 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
247 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800248 bool tryAgain = true;
249 while (tryAgain) {
250 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800251 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 return NO_INIT;
253 }
254
Dan Stoza9f3053d2014-03-06 15:14:33 -0800255 int dequeuedCount = 0;
256 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800257 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700258 if (mSlots[s].mBufferState.isDequeued()) {
259 ++dequeuedCount;
260 }
261 if (mSlots[s].mBufferState.isAcquired()) {
262 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800263 }
264 }
265
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700266 // Producers are not allowed to dequeue more than
267 // mMaxDequeuedBufferCount buffers.
268 // This check is only done if a buffer has already been queued
269 if (mCore->mBufferHasBeenQueued &&
270 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
271 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800272 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800273 return INVALID_OPERATION;
274 }
275
Dan Stoza0de7ea72015-04-23 13:20:51 -0700276 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
277
Dan Stozaae3c3682014-04-18 15:43:35 -0700278 // If we disconnect and reconnect quickly, we can be in a state where
279 // our slots are empty but we have many buffers in the queue. This can
280 // cause us to run out of memory if we outrun the consumer. Wait here if
281 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800282 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700283 bool tooManyBuffers = mCore->mQueue.size()
284 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700285 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800286 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700287 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700288 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700289 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700290 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700292 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700293 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800294 } else {
295 if (caller == FreeSlotCaller::Dequeue) {
296 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800297 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800298 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
299 *found = slot;
300 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800302 }
303 } else {
304 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800305 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800306 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
307 *found = slot;
308 } else {
309 *found = getFreeBufferLocked();
310 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700311 }
312 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700313 }
314
315 // If no buffer is found, or if the queue has too many buffers
316 // outstanding, wait for a buffer to be acquired or released, or for the
317 // max buffer count to change.
318 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
319 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800320 if (tryAgain) {
321 // Return an error if we're in non-blocking mode (producer and
322 // consumer are controlled by the application).
323 // However, the consumer is allowed to briefly acquire an extra
324 // buffer (which could cause us to have to wait here), which is
325 // okay, since it is only used to implement an atomic acquire +
326 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700327 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800328 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
329 return WOULD_BLOCK;
330 }
Dan Stoza127fc632015-06-30 13:43:32 -0700331 if (mDequeueTimeout >= 0) {
332 status_t result = mCore->mDequeueCondition.waitRelative(
333 mCore->mMutex, mDequeueTimeout);
334 if (result == TIMED_OUT) {
335 return result;
336 }
337 } else {
338 mCore->mDequeueCondition.wait(mCore->mMutex);
339 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800340 }
341 } // while (tryAgain)
342
343 return NO_ERROR;
344}
345
Dan Stoza289ade12014-02-28 11:17:17 -0800346status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700347 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
348 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800349 ATRACE_CALL();
350 { // Autolock scope
351 Mutex::Autolock lock(mCore->mMutex);
352 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700353
354 if (mCore->mIsAbandoned) {
355 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
356 return NO_INIT;
357 }
358
359 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
360 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
361 return NO_INIT;
362 }
Dan Stoza289ade12014-02-28 11:17:17 -0800363 } // Autolock scope
364
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700365 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
366 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800367
368 if ((width && !height) || (!width && height)) {
369 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
370 return BAD_VALUE;
371 }
372
373 status_t returnFlags = NO_ERROR;
374 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
375 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800376 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800377
378 { // Autolock scope
379 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700380 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800381
382 if (format == 0) {
383 format = mCore->mDefaultBufferFormat;
384 }
385
386 // Enable the usage bits the consumer requested
387 usage |= mCore->mConsumerUsageBits;
388
Dan Stoza9de72932015-04-16 17:28:43 -0700389 const bool useDefaultSize = !width && !height;
390 if (useDefaultSize) {
391 width = mCore->mDefaultWidth;
392 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800393 }
Dan Stoza289ade12014-02-28 11:17:17 -0800394
Pablo Ceballos981066c2016-02-18 12:54:37 -0800395 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700396 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800397 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800398 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700399 if (status != NO_ERROR) {
400 return status;
401 }
402
403 // This should not happen
404 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
405 BQ_LOGE("dequeueBuffer: no available buffer slots");
406 return -EBUSY;
407 }
408
409 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
410
411 // If we are not allowed to allocate new buffers,
412 // waitForFreeSlotThenRelock must have returned a slot containing a
413 // buffer. If this buffer would require reallocation to meet the
414 // requested attributes, we free it and attempt to get another one.
415 if (!mCore->mAllowAllocation) {
416 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700417 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700418 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
419 "buffer");
420 return BAD_VALUE;
421 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800422 mCore->mFreeSlots.insert(found);
423 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700424 found = BufferItem::INVALID_BUFFER_SLOT;
425 continue;
426 }
427 }
Dan Stoza289ade12014-02-28 11:17:17 -0800428 }
429
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800430 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700431 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800432 buffer->needsReallocation(width, height, format, usage)) {
433 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
434 "buffer");
435
436 return BAD_VALUE;
437 }
438
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700439 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800440 mCore->mActiveBuffers.insert(found);
441 }
Dan Stoza289ade12014-02-28 11:17:17 -0800442 *outSlot = found;
443 ATRACE_BUFFER_INDEX(found);
444
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 attachedByConsumer = mSlots[found].mNeedsReallocation;
446 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800447
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700448 mSlots[found].mBufferState.dequeue();
449
Dan Stoza289ade12014-02-28 11:17:17 -0800450 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700451 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800452 {
453 mSlots[found].mAcquireCalled = false;
454 mSlots[found].mGraphicBuffer = NULL;
455 mSlots[found].mRequestBufferCalled = false;
456 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
457 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
458 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800459 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700460 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800461
462 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800463 } else {
464 // We add 1 because that will be the frame number when this buffer
465 // is queued
466 mCore->mBufferAge =
467 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800468 }
469
Dan Stoza800b41a2015-04-28 14:20:04 -0700470 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
471 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800472
Dan Stoza289ade12014-02-28 11:17:17 -0800473 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
474 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
475 "slot=%d w=%d h=%d format=%u",
476 found, buffer->width, buffer->height, buffer->format);
477 }
478
479 eglDisplay = mSlots[found].mEglDisplay;
480 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700481 // Don't return a fence in shared buffer mode, except for the first
482 // frame.
483 *outFence = (mCore->mSharedBufferMode &&
484 mCore->mSharedBufferSlot == found) ?
485 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800486 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
487 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700488
489 // If shared buffer mode has just been enabled, cache the slot of the
490 // first buffer that is dequeued and mark it as the shared buffer.
491 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
492 BufferQueueCore::INVALID_BUFFER_SLOT) {
493 mCore->mSharedBufferSlot = found;
494 mSlots[found].mBufferState.mShared = true;
495 }
Dan Stoza289ade12014-02-28 11:17:17 -0800496 } // Autolock scope
497
498 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
499 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700500 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800501 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800502 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800503 { // Autolock scope
504 Mutex::Autolock lock(mCore->mMutex);
505
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700506 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
507 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
508 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
509 }
510
511 mCore->mIsAllocating = false;
512 mCore->mIsAllocatingCondition.broadcast();
513
514 if (graphicBuffer == NULL) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700515 mCore->mFreeSlots.insert(*outSlot);
516 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700517 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
518 return error;
519 }
520
Dan Stoza289ade12014-02-28 11:17:17 -0800521 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700522 mCore->mFreeSlots.insert(*outSlot);
523 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800524 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
525 return NO_INIT;
526 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800527
Pablo Ceballos9e314332016-01-12 13:49:19 -0800528 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800529 } // Autolock scope
530 }
531
Dan Stoza9f3053d2014-03-06 15:14:33 -0800532 if (attachedByConsumer) {
533 returnFlags |= BUFFER_NEEDS_REALLOCATION;
534 }
535
Dan Stoza289ade12014-02-28 11:17:17 -0800536 if (eglFence != EGL_NO_SYNC_KHR) {
537 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
538 1000000000);
539 // If something goes wrong, log the error, but return the buffer without
540 // synchronizing access to it. It's too late at this point to abort the
541 // dequeue operation.
542 if (result == EGL_FALSE) {
543 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
544 eglGetError());
545 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
546 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
547 }
548 eglDestroySyncKHR(eglDisplay, eglFence);
549 }
550
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700551 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
552 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800553 mSlots[*outSlot].mFrameNumber,
554 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
555
556 return returnFlags;
557}
558
Dan Stoza9f3053d2014-03-06 15:14:33 -0800559status_t BufferQueueProducer::detachBuffer(int slot) {
560 ATRACE_CALL();
561 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700562 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800563
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700564 sp<IConsumerListener> listener;
565 {
566 Mutex::Autolock lock(mCore->mMutex);
567
568 if (mCore->mIsAbandoned) {
569 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
570 return NO_INIT;
571 }
572
573 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
574 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
575 return NO_INIT;
576 }
577
578 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
579 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
580 return BAD_VALUE;
581 }
582
583 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
584 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
585 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
586 return BAD_VALUE;
587 } else if (!mSlots[slot].mBufferState.isDequeued()) {
588 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
589 "(state = %s)", slot, mSlots[slot].mBufferState.string());
590 return BAD_VALUE;
591 } else if (!mSlots[slot].mRequestBufferCalled) {
592 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
593 slot);
594 return BAD_VALUE;
595 }
596
597 mSlots[slot].mBufferState.detachProducer();
598 mCore->mActiveBuffers.erase(slot);
599 mCore->mFreeSlots.insert(slot);
600 mCore->clearBufferSlotLocked(slot);
601 mCore->mDequeueCondition.broadcast();
602 VALIDATE_CONSISTENCY();
603 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800604 }
605
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700606 if (listener != NULL) {
607 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700608 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800609
Dan Stoza9f3053d2014-03-06 15:14:33 -0800610 return NO_ERROR;
611}
612
Dan Stozad9822a32014-03-28 15:25:31 -0700613status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
614 sp<Fence>* outFence) {
615 ATRACE_CALL();
616
617 if (outBuffer == NULL) {
618 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
619 return BAD_VALUE;
620 } else if (outFence == NULL) {
621 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
622 return BAD_VALUE;
623 }
624
Pablo Ceballos981066c2016-02-18 12:54:37 -0800625 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700626
Pablo Ceballos981066c2016-02-18 12:54:37 -0800627 if (mCore->mIsAbandoned) {
628 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
629 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700630 }
631
Pablo Ceballos981066c2016-02-18 12:54:37 -0800632 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
633 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
634 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700635 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800636
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700637 if (mCore->mSharedBufferMode) {
638 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
639 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800640 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700641 }
642
Pablo Ceballos981066c2016-02-18 12:54:37 -0800643 mCore->waitWhileAllocatingLocked();
644
645 if (mCore->mFreeBuffers.empty()) {
646 return NO_MEMORY;
647 }
648
649 int found = mCore->mFreeBuffers.front();
650 mCore->mFreeBuffers.remove(found);
651 mCore->mFreeSlots.insert(found);
652
653 BQ_LOGV("detachNextBuffer detached slot %d", found);
654
655 *outBuffer = mSlots[found].mGraphicBuffer;
656 *outFence = mSlots[found].mFence;
657 mCore->clearBufferSlotLocked(found);
658 VALIDATE_CONSISTENCY();
659
Dan Stozad9822a32014-03-28 15:25:31 -0700660 return NO_ERROR;
661}
662
Dan Stoza9f3053d2014-03-06 15:14:33 -0800663status_t BufferQueueProducer::attachBuffer(int* outSlot,
664 const sp<android::GraphicBuffer>& buffer) {
665 ATRACE_CALL();
666
667 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700668 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800669 return BAD_VALUE;
670 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700671 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800672 return BAD_VALUE;
673 }
674
675 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700676
677 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700678 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700679 return NO_INIT;
680 }
681
682 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700683 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700684 return NO_INIT;
685 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700687 if (mCore->mSharedBufferMode) {
688 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700689 return BAD_VALUE;
690 }
691
Dan Stoza812ed062015-06-02 15:45:22 -0700692 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
693 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
694 "[queue %u]", buffer->getGenerationNumber(),
695 mCore->mGenerationNumber);
696 return BAD_VALUE;
697 }
698
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700699 mCore->waitWhileAllocatingLocked();
700
Dan Stoza9f3053d2014-03-06 15:14:33 -0800701 status_t returnFlags = NO_ERROR;
702 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800703 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800704 if (status != NO_ERROR) {
705 return status;
706 }
707
708 // This should not happen
709 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700710 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800711 return -EBUSY;
712 }
713
714 *outSlot = found;
715 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700716 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800717 *outSlot, returnFlags);
718
719 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700720 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800721 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
722 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700723 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800724 mSlots[*outSlot].mAcquireCalled = false;
725 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800726 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700727
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 return returnFlags;
729}
730
Dan Stoza289ade12014-02-28 11:17:17 -0800731status_t BufferQueueProducer::queueBuffer(int slot,
732 const QueueBufferInput &input, QueueBufferOutput *output) {
733 ATRACE_CALL();
734 ATRACE_BUFFER_INDEX(slot);
735
736 int64_t timestamp;
737 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800738 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700739 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800740 int scalingMode;
741 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700742 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800743 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800744 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700745 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700746 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800747
748 if (fence == NULL) {
749 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800750 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800751 }
752
753 switch (scalingMode) {
754 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
755 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
756 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
757 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
758 break;
759 default:
760 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800761 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800762 }
763
Dan Stoza8dc55392014-11-04 11:37:46 -0800764 sp<IConsumerListener> frameAvailableListener;
765 sp<IConsumerListener> frameReplacedListener;
766 int callbackTicket = 0;
767 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800768 { // Autolock scope
769 Mutex::Autolock lock(mCore->mMutex);
770
771 if (mCore->mIsAbandoned) {
772 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
773 return NO_INIT;
774 }
775
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700776 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
777 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
778 return NO_INIT;
779 }
780
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800781 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800782 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800783 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700785 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800786 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700787 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800788 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800789 } else if (!mSlots[slot].mRequestBufferCalled) {
790 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
791 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800792 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800793 }
794
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700795 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700796 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700797 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700798 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700799 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700800 mSlots[slot].mBufferState.mShared = true;
801 }
802
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800803 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700804 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800805 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800806 crop.left, crop.top, crop.right, crop.bottom, transform,
807 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800808
809 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
810 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700811 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800812 crop.intersect(bufferRect, &croppedRect);
813 if (croppedRect != crop) {
814 BQ_LOGE("queueBuffer: crop rect is not contained within the "
815 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800817 }
818
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800819 // Override UNKNOWN dataspace with consumer default
820 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
821 dataSpace = mCore->mDefaultBufferDataSpace;
822 }
823
Dan Stoza289ade12014-02-28 11:17:17 -0800824 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700825 mSlots[slot].mBufferState.queue();
826
Dan Stoza289ade12014-02-28 11:17:17 -0800827 ++mCore->mFrameCounter;
828 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
829
Dan Stoza289ade12014-02-28 11:17:17 -0800830 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
831 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
832 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 item.mTransform = transform &
834 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800835 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800836 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
837 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800838 item.mTimestamp = timestamp;
839 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800840 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800841 item.mFrameNumber = mCore->mFrameCounter;
842 item.mSlot = slot;
843 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700844 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700845 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700846 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700847 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700848 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700849 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800850
Ruben Brunk1681d952014-06-27 15:51:55 -0700851 mStickyTransform = stickyTransform;
852
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700853 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700854 if (mCore->mSharedBufferMode) {
855 mCore->mSharedBufferCache.crop = crop;
856 mCore->mSharedBufferCache.transform = transform;
857 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700858 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700859 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700860 }
861
Dan Stoza289ade12014-02-28 11:17:17 -0800862 if (mCore->mQueue.empty()) {
863 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
864 // and simply queue this buffer
865 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800866 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700868 // When the queue is not empty, we need to look at the last buffer
869 // in the queue to see if we need to replace it
870 const BufferItem& last = mCore->mQueue.itemAt(
871 mCore->mQueue.size() - 1);
872 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800873
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700874 if (!last.mIsStale) {
875 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700877 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 // still be around. Mark it as no longer shared if this
879 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700880 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700881 mSlots[last.mSlot].mBufferState.isFree()) {
882 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700883 }
884 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700885 if (!mSlots[last.mSlot].mBufferState.isShared()) {
886 mCore->mActiveBuffers.erase(last.mSlot);
887 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700888 }
Dan Stoza289ade12014-02-28 11:17:17 -0800889 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800890
Dan Stoza289ade12014-02-28 11:17:17 -0800891 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700892 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800893 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800894 } else {
895 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800896 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800897 }
898 }
899
900 mCore->mBufferHasBeenQueued = true;
901 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700902 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800903
904 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800905 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700906 static_cast<uint32_t>(mCore->mQueue.size()),
907 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -0800908
909 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stozae77c7662016-05-13 11:37:28 -0700910 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800911
912 // Take a ticket for the callback functions
913 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700914
Pablo Ceballos9e314332016-01-12 13:49:19 -0800915 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800916 } // Autolock scope
917
Irvel468051e2016-06-13 16:44:44 -0700918 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
919 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
920 // there will be no Binder call
921 if (!mConsumerIsSurfaceFlinger) {
922 item.mGraphicBuffer.clear();
923 }
924
925 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800926 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
927
928 // Call back without the main BufferQueue lock held, but with the callback
929 // lock held so we can ensure that callbacks occur in order
930 {
931 Mutex::Autolock lock(mCallbackMutex);
932 while (callbackTicket != mCurrentCallbackTicket) {
933 mCallbackCondition.wait(mCallbackMutex);
934 }
935
936 if (frameAvailableListener != NULL) {
937 frameAvailableListener->onFrameAvailable(item);
938 } else if (frameReplacedListener != NULL) {
939 frameReplacedListener->onFrameReplaced(item);
940 }
941
942 ++mCurrentCallbackTicket;
943 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800944 }
945
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000946 // Wait without lock held
947 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
948 // Waiting here allows for two full buffers to be queued but not a
949 // third. In the event that frames take varying time, this makes a
950 // small trade-off in favor of latency rather than throughput.
951 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000952 }
Dan Stoza50101d02016-04-07 16:53:23 -0700953 mLastQueueBufferFence = fence;
John Reck1a61da52016-04-28 13:18:15 -0700954 mLastQueuedCrop = item.mCrop;
955 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000956
Dan Stoza289ade12014-02-28 11:17:17 -0800957 return NO_ERROR;
958}
959
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700960status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800961 ATRACE_CALL();
962 BQ_LOGV("cancelBuffer: slot %d", slot);
963 Mutex::Autolock lock(mCore->mMutex);
964
965 if (mCore->mIsAbandoned) {
966 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700967 return NO_INIT;
968 }
969
970 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
971 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
972 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800973 }
974
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700975 if (mCore->mSharedBufferMode) {
976 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700977 return BAD_VALUE;
978 }
979
Dan Stoza3e96f192014-03-03 10:16:19 -0800980 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800981 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800982 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700983 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700984 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800985 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700986 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700987 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800988 } else if (fence == NULL) {
989 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700990 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800991 }
992
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700993 mSlots[slot].mBufferState.cancel();
994
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700995 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700996 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700997 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700998 mSlots[slot].mBufferState.mShared = false;
999 }
1000
1001 // Don't put the shared buffer on the free list.
1002 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001003 mCore->mActiveBuffers.erase(slot);
1004 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001005 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001006
Dan Stoza289ade12014-02-28 11:17:17 -08001007 mSlots[slot].mFence = fence;
1008 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001009 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001010
1011 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001012}
1013
1014int BufferQueueProducer::query(int what, int *outValue) {
1015 ATRACE_CALL();
1016 Mutex::Autolock lock(mCore->mMutex);
1017
1018 if (outValue == NULL) {
1019 BQ_LOGE("query: outValue was NULL");
1020 return BAD_VALUE;
1021 }
1022
1023 if (mCore->mIsAbandoned) {
1024 BQ_LOGE("query: BufferQueue has been abandoned");
1025 return NO_INIT;
1026 }
1027
1028 int value;
1029 switch (what) {
1030 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001031 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001032 break;
1033 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001034 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001035 break;
1036 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001037 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001038 break;
1039 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001040 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001041 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001042 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001044 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001045 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1046 value = (mCore->mQueue.size() > 1);
1047 break;
1048 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001049 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001050 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001051 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1052 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1053 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001054 case NATIVE_WINDOW_BUFFER_AGE:
1055 if (mCore->mBufferAge > INT32_MAX) {
1056 value = 0;
1057 } else {
1058 value = static_cast<int32_t>(mCore->mBufferAge);
1059 }
1060 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001061 default:
1062 return BAD_VALUE;
1063 }
1064
1065 BQ_LOGV("query: %d? %d", what, value);
1066 *outValue = value;
1067 return NO_ERROR;
1068}
1069
Dan Stozaf0eaf252014-03-21 13:05:51 -07001070status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001071 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1072 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001073 Mutex::Autolock lock(mCore->mMutex);
1074 mConsumerName = mCore->mConsumerName;
1075 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1076 producerControlledByApp ? "true" : "false");
1077
1078 if (mCore->mIsAbandoned) {
1079 BQ_LOGE("connect: BufferQueue has been abandoned");
1080 return NO_INIT;
1081 }
1082
1083 if (mCore->mConsumerListener == NULL) {
1084 BQ_LOGE("connect: BufferQueue has no consumer");
1085 return NO_INIT;
1086 }
1087
1088 if (output == NULL) {
1089 BQ_LOGE("connect: output was NULL");
1090 return BAD_VALUE;
1091 }
1092
1093 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1094 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1095 mCore->mConnectedApi, api);
1096 return BAD_VALUE;
1097 }
1098
1099 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1100 mDequeueTimeout < 0 ?
1101 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1102 mCore->mMaxBufferCount) -
1103 mCore->getMaxBufferCountLocked();
1104 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1105 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1106 "slots. Delta = %d", delta);
1107 return BAD_VALUE;
1108 }
1109
Dan Stoza289ade12014-02-28 11:17:17 -08001110 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001111 switch (api) {
1112 case NATIVE_WINDOW_API_EGL:
1113 case NATIVE_WINDOW_API_CPU:
1114 case NATIVE_WINDOW_API_MEDIA:
1115 case NATIVE_WINDOW_API_CAMERA:
1116 mCore->mConnectedApi = api;
1117 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1118 mCore->mTransformHint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001119 static_cast<uint32_t>(mCore->mQueue.size()),
1120 mCore->mFrameCounter + 1);
Dan Stoza289ade12014-02-28 11:17:17 -08001121
Pablo Ceballos981066c2016-02-18 12:54:37 -08001122 // Set up a death notification so that we can disconnect
1123 // automatically if the remote producer dies
1124 if (listener != NULL &&
1125 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1126 status = IInterface::asBinder(listener)->linkToDeath(
1127 static_cast<IBinder::DeathRecipient*>(this));
1128 if (status != NO_ERROR) {
1129 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1130 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001131 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001132 }
1133 mCore->mConnectedProducerListener = listener;
1134 break;
1135 default:
1136 BQ_LOGE("connect: unknown API %d", api);
1137 status = BAD_VALUE;
1138 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001139 }
1140
Pablo Ceballos981066c2016-02-18 12:54:37 -08001141 mCore->mBufferHasBeenQueued = false;
1142 mCore->mDequeueBufferCannotBlock = false;
1143 if (mDequeueTimeout < 0) {
1144 mCore->mDequeueBufferCannotBlock =
1145 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001146 }
Dan Stoza289ade12014-02-28 11:17:17 -08001147
Pablo Ceballos981066c2016-02-18 12:54:37 -08001148 mCore->mAllowAllocation = true;
1149 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001150 return status;
1151}
1152
1153status_t BufferQueueProducer::disconnect(int api) {
1154 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001155 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001156
1157 int status = NO_ERROR;
1158 sp<IConsumerListener> listener;
1159 { // Autolock scope
1160 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001161 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001162
1163 if (mCore->mIsAbandoned) {
1164 // It's not really an error to disconnect after the surface has
1165 // been abandoned; it should just be a no-op.
1166 return NO_ERROR;
1167 }
1168
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001169 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1170 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001171 // If we're asked to disconnect the currently connected api but
1172 // nobody is connected, it's not really an error.
1173 if (api == BufferQueueCore::NO_CONNECTED_API) {
1174 return NO_ERROR;
1175 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001176 }
1177
Dan Stoza289ade12014-02-28 11:17:17 -08001178 switch (api) {
1179 case NATIVE_WINDOW_API_EGL:
1180 case NATIVE_WINDOW_API_CPU:
1181 case NATIVE_WINDOW_API_MEDIA:
1182 case NATIVE_WINDOW_API_CAMERA:
1183 if (mCore->mConnectedApi == api) {
1184 mCore->freeAllBuffersLocked();
1185
1186 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001187 if (mCore->mConnectedProducerListener != NULL) {
1188 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001189 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001190 // This can fail if we're here because of the death
1191 // notification, but we just ignore it
1192 token->unlinkToDeath(
1193 static_cast<IBinder::DeathRecipient*>(this));
1194 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001195 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001196 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001197 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001198 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001199 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001200 mCore->mDequeueCondition.broadcast();
1201 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001202 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001203 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001204 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001205 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001206 }
1207 break;
1208 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001209 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001210 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001211 break;
1212 }
1213 } // Autolock scope
1214
1215 // Call back without lock held
1216 if (listener != NULL) {
1217 listener->onBuffersReleased();
1218 }
1219
1220 return status;
1221}
1222
Jesse Hall399184a2014-03-03 15:42:54 -08001223status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001224 sp<IConsumerListener> listener;
1225 { // Autolock scope
1226 Mutex::Autolock _l(mCore->mMutex);
1227 mCore->mSidebandStream = stream;
1228 listener = mCore->mConsumerListener;
1229 } // Autolock scope
1230
1231 if (listener != NULL) {
1232 listener->onSidebandStreamChanged();
1233 }
Jesse Hall399184a2014-03-03 15:42:54 -08001234 return NO_ERROR;
1235}
1236
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001237void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1238 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001239 ATRACE_CALL();
1240 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001241 size_t newBufferCount = 0;
1242 uint32_t allocWidth = 0;
1243 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001244 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001245 uint32_t allocUsage = 0;
1246 { // Autolock scope
1247 Mutex::Autolock lock(mCore->mMutex);
1248 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001249
Dan Stoza9de72932015-04-16 17:28:43 -07001250 if (!mCore->mAllowAllocation) {
1251 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1252 "BufferQueue");
1253 return;
1254 }
1255
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001256 newBufferCount = mCore->mFreeSlots.size();
1257 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001258 return;
1259 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001260
Antoine Labour78014f32014-07-15 21:17:03 -07001261 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1262 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1263 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1264 allocUsage = usage | mCore->mConsumerUsageBits;
1265
1266 mCore->mIsAllocating = true;
1267 } // Autolock scope
1268
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001269 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001270 for (size_t i = 0; i < newBufferCount; ++i) {
1271 status_t result = NO_ERROR;
1272 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1273 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1274 if (result != NO_ERROR) {
1275 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1276 " %u, usage %u)", width, height, format, usage);
1277 Mutex::Autolock lock(mCore->mMutex);
1278 mCore->mIsAllocating = false;
1279 mCore->mIsAllocatingCondition.broadcast();
1280 return;
1281 }
1282 buffers.push_back(graphicBuffer);
1283 }
1284
1285 { // Autolock scope
1286 Mutex::Autolock lock(mCore->mMutex);
1287 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1288 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001289 PixelFormat checkFormat = format != 0 ?
1290 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001291 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1292 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1293 checkFormat != allocFormat || checkUsage != allocUsage) {
1294 // Something changed while we released the lock. Retry.
1295 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1296 mCore->mIsAllocating = false;
1297 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001298 continue;
1299 }
1300
Antoine Labour78014f32014-07-15 21:17:03 -07001301 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001302 if (mCore->mFreeSlots.empty()) {
1303 BQ_LOGV("allocateBuffers: a slot was occupied while "
1304 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001305 continue;
1306 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001307 auto slot = mCore->mFreeSlots.begin();
1308 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1309 mSlots[*slot].mGraphicBuffer = buffers[i];
1310 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001311
1312 // freeBufferLocked puts this slot on the free slots list. Since
1313 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001314 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001315
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001316 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1317 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001318
1319 // Make sure the erase is done after all uses of the slot
1320 // iterator since it will be invalid after this point.
1321 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001322 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001323
Antoine Labour78014f32014-07-15 21:17:03 -07001324 mCore->mIsAllocating = false;
1325 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001326 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001327 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001328 }
1329}
1330
Dan Stoza9de72932015-04-16 17:28:43 -07001331status_t BufferQueueProducer::allowAllocation(bool allow) {
1332 ATRACE_CALL();
1333 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1334
1335 Mutex::Autolock lock(mCore->mMutex);
1336 mCore->mAllowAllocation = allow;
1337 return NO_ERROR;
1338}
1339
Dan Stoza812ed062015-06-02 15:45:22 -07001340status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1341 ATRACE_CALL();
1342 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1343
1344 Mutex::Autolock lock(mCore->mMutex);
1345 mCore->mGenerationNumber = generationNumber;
1346 return NO_ERROR;
1347}
1348
Dan Stozac6f30bd2015-06-08 09:32:50 -07001349String8 BufferQueueProducer::getConsumerName() const {
1350 ATRACE_CALL();
1351 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1352 return mConsumerName;
1353}
1354
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001355status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001356 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001357 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001358
1359 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001360 if (!sharedBufferMode) {
1361 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001362 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001363 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001364 return NO_ERROR;
1365}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001366
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001367status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1368 ATRACE_CALL();
1369 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1370
1371 Mutex::Autolock lock(mCore->mMutex);
1372
1373 mCore->mAutoRefresh = autoRefresh;
1374 return NO_ERROR;
1375}
1376
Dan Stoza127fc632015-06-30 13:43:32 -07001377status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1378 ATRACE_CALL();
1379 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1380
Pablo Ceballos981066c2016-02-18 12:54:37 -08001381 Mutex::Autolock lock(mCore->mMutex);
1382 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1383 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1384 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1385 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1386 "available slots. Delta = %d", delta);
1387 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001388 }
1389
Pablo Ceballos981066c2016-02-18 12:54:37 -08001390 mDequeueTimeout = timeout;
1391 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001392
Pablo Ceballos981066c2016-02-18 12:54:37 -08001393 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001394 return NO_ERROR;
1395}
1396
Dan Stoza50101d02016-04-07 16:53:23 -07001397status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001398 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001399 ATRACE_CALL();
1400 BQ_LOGV("getLastQueuedBuffer");
1401
1402 Mutex::Autolock lock(mCore->mMutex);
1403 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1404 *outBuffer = nullptr;
1405 *outFence = Fence::NO_FENCE;
1406 return NO_ERROR;
1407 }
1408
1409 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1410 *outFence = mLastQueueBufferFence;
1411
John Reck1a61da52016-04-28 13:18:15 -07001412 // Currently only SurfaceFlinger internally ever changes
1413 // GLConsumer's filtering mode, so we just use 'true' here as
1414 // this is slightly specialized for the current client of this API,
1415 // which does want filtering.
1416 GLConsumer::computeTransformMatrix(outTransformMatrix,
1417 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1418 mLastQueuedTransform, true /* filter */);
1419
Dan Stoza50101d02016-04-07 16:53:23 -07001420 return NO_ERROR;
1421}
1422
Pablo Ceballosce796e72016-02-04 19:10:51 -08001423bool BufferQueueProducer::getFrameTimestamps(uint64_t frameNumber,
1424 FrameTimestamps* outTimestamps) const {
1425 ATRACE_CALL();
1426 BQ_LOGV("getFrameTimestamps, %" PRIu64, frameNumber);
1427 sp<IConsumerListener> listener;
1428
1429 {
1430 Mutex::Autolock lock(mCore->mMutex);
1431 listener = mCore->mConsumerListener;
1432 }
1433 if (listener != NULL) {
1434 return listener->getFrameTimestamps(frameNumber, outTimestamps);
1435 }
1436 return false;
1437}
1438
Dan Stoza289ade12014-02-28 11:17:17 -08001439void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1440 // If we're here, it means that a producer we were connected to died.
1441 // We're guaranteed that we are still connected to it because we remove
1442 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1443 // without synchronization here.
1444 int api = mCore->mConnectedApi;
1445 disconnect(api);
1446}
1447
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001448status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1449 BQ_LOGV("getUniqueId");
1450
1451 *outId = mCore->mUniqueId;
1452 return NO_ERROR;
1453}
1454
Dan Stoza289ade12014-02-28 11:17:17 -08001455} // namespace android