blob: 46e759111f8bdc945404d4738e91671d4f0adeca [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
31#include <gui/BufferItem.h>
32#include <gui/BufferQueueCore.h>
33#include <gui/BufferQueueProducer.h>
34#include <gui/IConsumerListener.h>
35#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070036#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080037
38#include <utils/Log.h>
39#include <utils/Trace.h>
40
41namespace android {
42
43BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
44 mCore(core),
45 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070046 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070047 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080048 mLastQueueBufferFence(Fence::NO_FENCE),
49 mCallbackMutex(),
50 mNextCallbackTicket(0),
51 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070052 mCallbackCondition(),
53 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080054
55BufferQueueProducer::~BufferQueueProducer() {}
56
57status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
58 ATRACE_CALL();
59 BQ_LOGV("requestBuffer: slot %d", slot);
60 Mutex::Autolock lock(mCore->mMutex);
61
62 if (mCore->mIsAbandoned) {
63 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
64 return NO_INIT;
65 }
66
Pablo Ceballos583b1b32015-09-03 18:23:52 -070067 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
68 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
69 return NO_INIT;
70 }
71
Dan Stoza3e96f192014-03-03 10:16:19 -080072 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080073 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080074 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080075 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070076 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080077 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070078 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080079 return BAD_VALUE;
80 }
81
82 mSlots[slot].mRequestBufferCalled = true;
83 *buf = mSlots[slot].mGraphicBuffer;
84 return NO_ERROR;
85}
86
Pablo Ceballosfa455352015-08-12 17:47:47 -070087status_t BufferQueueProducer::setMaxDequeuedBufferCount(
88 int maxDequeuedBuffers) {
89 ATRACE_CALL();
90 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
91 maxDequeuedBuffers);
92
Pablo Ceballos789a0c82016-02-05 13:39:27 -080093 sp<IConsumerListener> consumerListener;
94 sp<IProducerListener> producerListener;
95 std::vector<int> freedSlots;
Pablo Ceballosfa455352015-08-12 17:47:47 -070096 { // Autolock scope
97 Mutex::Autolock lock(mCore->mMutex);
98 mCore->waitWhileAllocatingLocked();
99
100 if (mCore->mIsAbandoned) {
101 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
102 "abandoned");
103 return NO_INIT;
104 }
105
Pablo Ceballos72daab62015-12-07 16:38:43 -0800106 // The new maxDequeuedBuffer count should not be violated by the number
107 // of currently dequeued buffers
108 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800109 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700110 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800111 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700112 }
113 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800114 if (dequeuedCount > maxDequeuedBuffers) {
115 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
116 "count (%d) exceeds the current dequeued buffer count (%d)",
117 maxDequeuedBuffers, dequeuedCount);
118 return BAD_VALUE;
119 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700120
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700121 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700122 bufferCount += maxDequeuedBuffers;
123
124 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
125 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
126 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
127 return BAD_VALUE;
128 }
129
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700130 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131 if (bufferCount < minBufferSlots) {
132 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
133 "less than minimum %d", bufferCount, minBufferSlots);
134 return BAD_VALUE;
135 }
136
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700137 if (bufferCount > mCore->mMaxBufferCount) {
138 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700139 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
140 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
141 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
142 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700143 return BAD_VALUE;
144 }
145
Pablo Ceballos72daab62015-12-07 16:38:43 -0800146 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800147 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800148 return BAD_VALUE;
149 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700150 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800151 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800152 if (delta < 0) {
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800153 consumerListener = mCore->mConsumerListener;
154 producerListener = mCore->mConnectedProducerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700156 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700157 } // Autolock scope
158
159 // Call back without lock held
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800160 if (consumerListener != NULL) {
161 consumerListener->onBuffersReleased();
162 }
163 if (producerListener != NULL) {
164 for (int i : freedSlots) {
165 producerListener->onSlotFreed(i);
166 }
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 Ceballos789a0c82016-02-05 13:39:27 -0800176 sp<IConsumerListener> consumerListener;
177 sp<IProducerListener> producerListener;
178 std::vector<int> freedSlots;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700179 { // Autolock scope
180 Mutex::Autolock lock(mCore->mMutex);
181 mCore->waitWhileAllocatingLocked();
182
183 if (mCore->mIsAbandoned) {
184 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
185 return NO_INIT;
186 }
187
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700188 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700189 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
190 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700191 BQ_LOGE("setAsyncMode(%d): this call would cause the "
192 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700193 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
194 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
195 mCore->mMaxDequeuedBufferCount,
196 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700197 return BAD_VALUE;
198 }
199
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800200 int delta = mCore->getMaxBufferCountLocked(async,
201 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
202 - mCore->getMaxBufferCountLocked();
203
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800204 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800205 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
206 "available slots. Delta = %d", delta);
207 return BAD_VALUE;
208 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700209 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800210 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700211 mCore->mDequeueCondition.broadcast();
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800212 if (delta < 0) {
213 consumerListener = mCore->mConsumerListener;
214 producerListener = mCore->mConnectedProducerListener;
215 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 } // Autolock scope
217
218 // Call back without lock held
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800219 if (consumerListener != NULL) {
220 consumerListener->onBuffersReleased();
221 }
222 if (producerListener != NULL) {
223 for (int i : freedSlots) {
224 producerListener->onSlotFreed(i);
225 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700226 }
227 return NO_ERROR;
228}
229
Dan Stoza5ecfb682016-01-04 17:01:02 -0800230int BufferQueueProducer::getFreeBufferLocked() const {
231 if (mCore->mFreeBuffers.empty()) {
232 return BufferQueueCore::INVALID_BUFFER_SLOT;
233 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800234 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800235 mCore->mFreeBuffers.pop_front();
236 return slot;
237}
238
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800239int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800240 if (mCore->mFreeSlots.empty()) {
241 return BufferQueueCore::INVALID_BUFFER_SLOT;
242 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800243 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800244 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800245 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800246}
247
248status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800249 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800250 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
251 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 bool tryAgain = true;
253 while (tryAgain) {
254 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800255 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800256 return NO_INIT;
257 }
258
Dan Stoza9f3053d2014-03-06 15:14:33 -0800259 int dequeuedCount = 0;
260 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800261 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700262 if (mSlots[s].mBufferState.isDequeued()) {
263 ++dequeuedCount;
264 }
265 if (mSlots[s].mBufferState.isAcquired()) {
266 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800267 }
268 }
269
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700270 // Producers are not allowed to dequeue more than
271 // mMaxDequeuedBufferCount buffers.
272 // This check is only done if a buffer has already been queued
273 if (mCore->mBufferHasBeenQueued &&
274 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
275 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800276 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 return INVALID_OPERATION;
278 }
279
Dan Stoza0de7ea72015-04-23 13:20:51 -0700280 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
281
Dan Stozaae3c3682014-04-18 15:43:35 -0700282 // If we disconnect and reconnect quickly, we can be in a state where
283 // our slots are empty but we have many buffers in the queue. This can
284 // cause us to run out of memory if we outrun the consumer. Wait here if
285 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800286 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700287 bool tooManyBuffers = mCore->mQueue.size()
288 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700289 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700292 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700293 // If in single buffer mode and a shared buffer exists, always
294 // return it.
295 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
296 BufferQueueCore::INVALID_BUFFER_SLOT) {
297 *found = mCore->mSingleBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800298 } else {
299 if (caller == FreeSlotCaller::Dequeue) {
300 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800302 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
303 *found = slot;
304 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800305 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800306 }
307 } else {
308 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800309 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800310 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
311 *found = slot;
312 } else {
313 *found = getFreeBufferLocked();
314 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700315 }
316 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700317 }
318
319 // If no buffer is found, or if the queue has too many buffers
320 // outstanding, wait for a buffer to be acquired or released, or for the
321 // max buffer count to change.
322 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
323 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 if (tryAgain) {
325 // Return an error if we're in non-blocking mode (producer and
326 // consumer are controlled by the application).
327 // However, the consumer is allowed to briefly acquire an extra
328 // buffer (which could cause us to have to wait here), which is
329 // okay, since it is only used to implement an atomic acquire +
330 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700331 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800332 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
333 return WOULD_BLOCK;
334 }
Dan Stoza127fc632015-06-30 13:43:32 -0700335 if (mDequeueTimeout >= 0) {
336 status_t result = mCore->mDequeueCondition.waitRelative(
337 mCore->mMutex, mDequeueTimeout);
338 if (result == TIMED_OUT) {
339 return result;
340 }
341 } else {
342 mCore->mDequeueCondition.wait(mCore->mMutex);
343 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800344 }
345 } // while (tryAgain)
346
347 return NO_ERROR;
348}
349
Dan Stoza289ade12014-02-28 11:17:17 -0800350status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700351 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
352 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800353 ATRACE_CALL();
354 { // Autolock scope
355 Mutex::Autolock lock(mCore->mMutex);
356 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700357
358 if (mCore->mIsAbandoned) {
359 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
360 return NO_INIT;
361 }
362
363 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
364 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
365 return NO_INIT;
366 }
Dan Stoza289ade12014-02-28 11:17:17 -0800367 } // Autolock scope
368
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700369 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
370 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800371
372 if ((width && !height) || (!width && height)) {
373 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
374 return BAD_VALUE;
375 }
376
377 status_t returnFlags = NO_ERROR;
378 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
379 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800381
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800382 sp<IConsumerListener> consumerListener;
383 sp<IProducerListener> producerListener;
384 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800385 { // Autolock scope
386 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700387 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800388
389 if (format == 0) {
390 format = mCore->mDefaultBufferFormat;
391 }
392
393 // Enable the usage bits the consumer requested
394 usage |= mCore->mConsumerUsageBits;
395
Dan Stoza9de72932015-04-16 17:28:43 -0700396 const bool useDefaultSize = !width && !height;
397 if (useDefaultSize) {
398 width = mCore->mDefaultWidth;
399 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800400 }
Dan Stoza289ade12014-02-28 11:17:17 -0800401
Dan Stoza9de72932015-04-16 17:28:43 -0700402 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800403 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800404 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700405 if (status != NO_ERROR) {
406 return status;
407 }
408
409 // This should not happen
410 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
411 BQ_LOGE("dequeueBuffer: no available buffer slots");
412 return -EBUSY;
413 }
414
415 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
416
417 // If we are not allowed to allocate new buffers,
418 // waitForFreeSlotThenRelock must have returned a slot containing a
419 // buffer. If this buffer would require reallocation to meet the
420 // requested attributes, we free it and attempt to get another one.
421 if (!mCore->mAllowAllocation) {
422 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800423 if (mCore->mSingleBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700424 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
425 "buffer");
426 return BAD_VALUE;
427 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800428 mCore->mFreeSlots.insert(found);
429 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700430 found = BufferItem::INVALID_BUFFER_SLOT;
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800431 consumerListener = mCore->mConsumerListener;
432 producerListener = mCore->mConnectedProducerListener;
Dan Stoza9de72932015-04-16 17:28:43 -0700433 continue;
434 }
435 }
Dan Stoza289ade12014-02-28 11:17:17 -0800436 }
437
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800438 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
439 if (mCore->mSingleBufferSlot == found &&
440 buffer->needsReallocation(width, height, format, usage)) {
441 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
442 "buffer");
443
444 return BAD_VALUE;
445 }
446
447 if (mCore->mSingleBufferSlot != found) {
448 mCore->mActiveBuffers.insert(found);
449 }
Dan Stoza289ade12014-02-28 11:17:17 -0800450 *outSlot = found;
451 ATRACE_BUFFER_INDEX(found);
452
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800453 attachedByConsumer = mSlots[found].mNeedsReallocation;
454 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800455
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700456 mSlots[found].mBufferState.dequeue();
457
458 // If single buffer mode has just been enabled, cache the slot of the
459 // first buffer that is dequeued and mark it as the shared buffer.
460 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
461 BufferQueueCore::INVALID_BUFFER_SLOT) {
462 mCore->mSingleBufferSlot = found;
463 mSlots[found].mBufferState.mShared = true;
464 }
Dan Stoza289ade12014-02-28 11:17:17 -0800465
Dan Stoza289ade12014-02-28 11:17:17 -0800466 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700467 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800468 {
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800469 if (buffer != NULL) {
470 consumerListener = mCore->mConsumerListener;
471 producerListener = mCore->mConnectedProducerListener;
472 }
Dan Stoza289ade12014-02-28 11:17:17 -0800473 mSlots[found].mAcquireCalled = false;
474 mSlots[found].mGraphicBuffer = NULL;
475 mSlots[found].mRequestBufferCalled = false;
476 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
477 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
478 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800479 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800481
482 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800483 } else {
484 // We add 1 because that will be the frame number when this buffer
485 // is queued
486 mCore->mBufferAge =
487 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800488 }
489
Dan Stoza800b41a2015-04-28 14:20:04 -0700490 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
491 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800492
Dan Stoza289ade12014-02-28 11:17:17 -0800493 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
494 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
495 "slot=%d w=%d h=%d format=%u",
496 found, buffer->width, buffer->height, buffer->format);
497 }
498
499 eglDisplay = mSlots[found].mEglDisplay;
500 eglFence = mSlots[found].mEglFence;
501 *outFence = mSlots[found].mFence;
502 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
503 mSlots[found].mFence = Fence::NO_FENCE;
504 } // Autolock scope
505
506 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
507 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700508 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800509 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800510 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800511 { // Autolock scope
512 Mutex::Autolock lock(mCore->mMutex);
513
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700514 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
515 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
516 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
517 }
518
519 mCore->mIsAllocating = false;
520 mCore->mIsAllocatingCondition.broadcast();
521
522 if (graphicBuffer == NULL) {
523 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
524 return error;
525 }
526
Dan Stoza289ade12014-02-28 11:17:17 -0800527 if (mCore->mIsAbandoned) {
528 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
529 return NO_INIT;
530 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800531
Pablo Ceballos9e314332016-01-12 13:49:19 -0800532 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800533 } // Autolock scope
534 }
535
Dan Stoza9f3053d2014-03-06 15:14:33 -0800536 if (attachedByConsumer) {
537 returnFlags |= BUFFER_NEEDS_REALLOCATION;
538 }
539
Dan Stoza289ade12014-02-28 11:17:17 -0800540 if (eglFence != EGL_NO_SYNC_KHR) {
541 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
542 1000000000);
543 // If something goes wrong, log the error, but return the buffer without
544 // synchronizing access to it. It's too late at this point to abort the
545 // dequeue operation.
546 if (result == EGL_FALSE) {
547 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
548 eglGetError());
549 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
550 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
551 }
552 eglDestroySyncKHR(eglDisplay, eglFence);
553 }
554
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700555 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
556 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800557 mSlots[*outSlot].mFrameNumber,
558 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
559
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800560 // Call back without lock held
561 if (consumerListener != NULL) {
562 consumerListener->onBuffersReleased();
563 }
564 if (producerListener != NULL) {
565 producerListener->onSlotFreed(found);
566 }
567
Dan Stoza289ade12014-02-28 11:17:17 -0800568 return returnFlags;
569}
570
Dan Stoza9f3053d2014-03-06 15:14:33 -0800571status_t BufferQueueProducer::detachBuffer(int slot) {
572 ATRACE_CALL();
573 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700574 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800575
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800576 sp<IConsumerListener> consumerListener;
577 sp<IProducerListener> producerListener;
578 {
579 Mutex::Autolock lock(mCore->mMutex);
580
581 if (mCore->mIsAbandoned) {
582 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
583 return NO_INIT;
584 }
585
586 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
587 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
588 return NO_INIT;
589 }
590
591 if (mCore->mSingleBufferMode || mCore->mSingleBufferSlot == slot) {
592 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer "
593 "mode");
594 return BAD_VALUE;
595 }
596
597 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
598 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
599 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
600 return BAD_VALUE;
601 } else if (!mSlots[slot].mBufferState.isDequeued()) {
602 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
603 "(state = %s)", slot, mSlots[slot].mBufferState.string());
604 return BAD_VALUE;
605 } else if (!mSlots[slot].mRequestBufferCalled) {
606 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
607 slot);
608 return BAD_VALUE;
609 }
610
611 mSlots[slot].mBufferState.detachProducer();
612 mCore->mActiveBuffers.erase(slot);
613 mCore->mFreeSlots.insert(slot);
614 mCore->clearBufferSlotLocked(slot);
615 mCore->mDequeueCondition.broadcast();
616 VALIDATE_CONSISTENCY();
617 producerListener = mCore->mConnectedProducerListener;
618 consumerListener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800619 }
620
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800621 // Call back without lock held
622 if (consumerListener != NULL) {
623 consumerListener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700624 }
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800625 if (producerListener != NULL) {
626 producerListener->onSlotFreed(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700627 }
628
Dan Stoza9f3053d2014-03-06 15:14:33 -0800629 return NO_ERROR;
630}
631
Dan Stozad9822a32014-03-28 15:25:31 -0700632status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
633 sp<Fence>* outFence) {
634 ATRACE_CALL();
635
636 if (outBuffer == NULL) {
637 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
638 return BAD_VALUE;
639 } else if (outFence == NULL) {
640 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
641 return BAD_VALUE;
642 }
643
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800644 sp<IConsumerListener> consumerListener;
645 sp<IProducerListener> producerListener;
646 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
647 {
648 Mutex::Autolock lock(mCore->mMutex);
649 if (mCore->mIsAbandoned) {
650 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
651 return NO_INIT;
652 }
Dan Stozad9822a32014-03-28 15:25:31 -0700653
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800654 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
655 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
656 return NO_INIT;
657 }
658
659 if (mCore->mSingleBufferMode) {
660 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
661 "mode");
662 return BAD_VALUE;
663 }
664
665 mCore->waitWhileAllocatingLocked();
666
667 if (mCore->mFreeBuffers.empty()) {
668 return NO_MEMORY;
669 }
670
671 found = mCore->mFreeBuffers.front();
672 mCore->mFreeBuffers.remove(found);
673 mCore->mFreeSlots.insert(found);
674
675 BQ_LOGV("detachNextBuffer detached slot %d", found);
676
677 *outBuffer = mSlots[found].mGraphicBuffer;
678 *outFence = mSlots[found].mFence;
679 mCore->clearBufferSlotLocked(found);
680 VALIDATE_CONSISTENCY();
681 consumerListener = mCore->mConsumerListener;
682 producerListener = mCore->mConnectedProducerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700683 }
684
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800685 // Call back without lock held
686 if (consumerListener != NULL) {
687 consumerListener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700688 }
Pablo Ceballos789a0c82016-02-05 13:39:27 -0800689 if (producerListener != NULL) {
690 producerListener->onSlotFreed(found);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700691 }
692
Dan Stozad9822a32014-03-28 15:25:31 -0700693 return NO_ERROR;
694}
695
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696status_t BufferQueueProducer::attachBuffer(int* outSlot,
697 const sp<android::GraphicBuffer>& buffer) {
698 ATRACE_CALL();
699
700 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700701 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800702 return BAD_VALUE;
703 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700704 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800705 return BAD_VALUE;
706 }
707
708 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700709
710 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700711 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700712 return NO_INIT;
713 }
714
715 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700716 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700717 return NO_INIT;
718 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800719
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700720 if (mCore->mSingleBufferMode) {
Craig Donner05249fc2016-01-15 19:33:55 -0800721 BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700722 return BAD_VALUE;
723 }
724
Dan Stoza812ed062015-06-02 15:45:22 -0700725 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
726 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
727 "[queue %u]", buffer->getGenerationNumber(),
728 mCore->mGenerationNumber);
729 return BAD_VALUE;
730 }
731
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700732 mCore->waitWhileAllocatingLocked();
733
Dan Stoza9f3053d2014-03-06 15:14:33 -0800734 status_t returnFlags = NO_ERROR;
735 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800736 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800737 if (status != NO_ERROR) {
738 return status;
739 }
740
741 // This should not happen
742 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700743 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800744 return -EBUSY;
745 }
746
747 *outSlot = found;
748 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700749 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800750 *outSlot, returnFlags);
751
752 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700753 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800754 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
755 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700756 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800757 mSlots[*outSlot].mAcquireCalled = false;
758 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800759 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700760
Dan Stoza9f3053d2014-03-06 15:14:33 -0800761 return returnFlags;
762}
763
Dan Stoza289ade12014-02-28 11:17:17 -0800764status_t BufferQueueProducer::queueBuffer(int slot,
765 const QueueBufferInput &input, QueueBufferOutput *output) {
766 ATRACE_CALL();
767 ATRACE_BUFFER_INDEX(slot);
768
769 int64_t timestamp;
770 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800771 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700772 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800773 int scalingMode;
774 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700775 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800776 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800777 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700778 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700779 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800780
781 if (fence == NULL) {
782 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800783 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800784 }
785
786 switch (scalingMode) {
787 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
788 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
789 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
790 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
791 break;
792 default:
793 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800794 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800795 }
796
Dan Stoza8dc55392014-11-04 11:37:46 -0800797 sp<IConsumerListener> frameAvailableListener;
798 sp<IConsumerListener> frameReplacedListener;
799 int callbackTicket = 0;
800 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800801 { // Autolock scope
802 Mutex::Autolock lock(mCore->mMutex);
803
804 if (mCore->mIsAbandoned) {
805 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
806 return NO_INIT;
807 }
808
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700809 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
810 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
811 return NO_INIT;
812 }
813
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800814 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800815 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800816 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800817 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700818 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800819 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700820 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800821 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800822 } else if (!mSlots[slot].mRequestBufferCalled) {
823 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
824 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800825 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800826 }
827
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800828 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700829 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800830 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800831 crop.left, crop.top, crop.right, crop.bottom, transform,
832 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800833
834 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
835 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700836 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800837 crop.intersect(bufferRect, &croppedRect);
838 if (croppedRect != crop) {
839 BQ_LOGE("queueBuffer: crop rect is not contained within the "
840 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800842 }
843
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800844 // Override UNKNOWN dataspace with consumer default
845 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
846 dataSpace = mCore->mDefaultBufferDataSpace;
847 }
848
Dan Stoza289ade12014-02-28 11:17:17 -0800849 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700850 mSlots[slot].mBufferState.queue();
851
Dan Stoza289ade12014-02-28 11:17:17 -0800852 ++mCore->mFrameCounter;
853 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
854
Dan Stoza289ade12014-02-28 11:17:17 -0800855 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
856 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
857 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 item.mTransform = transform &
859 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800860 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800861 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
862 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800863 item.mTimestamp = timestamp;
864 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800865 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800866 item.mFrameNumber = mCore->mFrameCounter;
867 item.mSlot = slot;
868 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700869 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700870 mCore->mDequeueBufferCannotBlock ||
871 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700872 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700873 item.mSingleBufferMode = mCore->mSingleBufferMode;
874 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800875
Ruben Brunk1681d952014-06-27 15:51:55 -0700876 mStickyTransform = stickyTransform;
877
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 // Cache the shared buffer data so that the BufferItem can be recreated.
879 if (mCore->mSingleBufferMode) {
880 mCore->mSingleBufferCache.crop = crop;
881 mCore->mSingleBufferCache.transform = transform;
882 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
883 scalingMode);
884 mCore->mSingleBufferCache.dataspace = dataSpace;
885 }
886
Dan Stoza289ade12014-02-28 11:17:17 -0800887 if (mCore->mQueue.empty()) {
888 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
889 // and simply queue this buffer
890 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800891 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800892 } else {
893 // When the queue is not empty, we need to look at the front buffer
894 // state to see if we need to replace it
895 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
896 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800897
898 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700899 mSlots[front->mSlot].mBufferState.freeQueued();
900
901 // After leaving single buffer mode, the shared buffer will
902 // still be around. Mark it as no longer shared if this
903 // operation causes it to be free.
904 if (!mCore->mSingleBufferMode &&
905 mSlots[front->mSlot].mBufferState.isFree()) {
906 mSlots[front->mSlot].mBufferState.mShared = false;
907 }
908 // Don't put the shared buffer on the free list.
909 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800910 mCore->mActiveBuffers.erase(front->mSlot);
911 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700912 }
Dan Stoza289ade12014-02-28 11:17:17 -0800913 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800914
Dan Stoza289ade12014-02-28 11:17:17 -0800915 // Overwrite the droppable buffer with the incoming one
916 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800917 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800918 } else {
919 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800920 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800921 }
922 }
923
924 mCore->mBufferHasBeenQueued = true;
925 mCore->mDequeueCondition.broadcast();
926
927 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800928 mCore->mTransformHint,
929 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800930
931 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800932
933 // Take a ticket for the callback functions
934 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700935
Pablo Ceballos9e314332016-01-12 13:49:19 -0800936 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800937 } // Autolock scope
938
Dan Stoza8dc55392014-11-04 11:37:46 -0800939 // Don't send the GraphicBuffer through the callback, and don't send
940 // the slot number, since the consumer shouldn't need it
941 item.mGraphicBuffer.clear();
942 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
943
944 // Call back without the main BufferQueue lock held, but with the callback
945 // lock held so we can ensure that callbacks occur in order
946 {
947 Mutex::Autolock lock(mCallbackMutex);
948 while (callbackTicket != mCurrentCallbackTicket) {
949 mCallbackCondition.wait(mCallbackMutex);
950 }
951
952 if (frameAvailableListener != NULL) {
953 frameAvailableListener->onFrameAvailable(item);
954 } else if (frameReplacedListener != NULL) {
955 frameReplacedListener->onFrameReplaced(item);
956 }
957
958 ++mCurrentCallbackTicket;
959 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800960 }
961
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000962 // Wait without lock held
963 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
964 // Waiting here allows for two full buffers to be queued but not a
965 // third. In the event that frames take varying time, this makes a
966 // small trade-off in favor of latency rather than throughput.
967 mLastQueueBufferFence->waitForever("Throttling EGL Production");
968 mLastQueueBufferFence = fence;
969 }
970
Dan Stoza289ade12014-02-28 11:17:17 -0800971 return NO_ERROR;
972}
973
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700974status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800975 ATRACE_CALL();
976 BQ_LOGV("cancelBuffer: slot %d", slot);
977 Mutex::Autolock lock(mCore->mMutex);
978
979 if (mCore->mIsAbandoned) {
980 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700981 return NO_INIT;
982 }
983
984 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
985 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
986 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800987 }
988
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700989 if (mCore->mSingleBufferMode) {
990 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
991 return BAD_VALUE;
992 }
993
Dan Stoza3e96f192014-03-03 10:16:19 -0800994 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800995 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800996 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700997 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700998 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800999 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001000 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001001 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001002 } else if (fence == NULL) {
1003 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001004 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001005 }
1006
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007 mSlots[slot].mBufferState.cancel();
1008
1009 // After leaving single buffer mode, the shared buffer will still be around.
1010 // Mark it as no longer shared if this operation causes it to be free.
1011 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
1012 mSlots[slot].mBufferState.mShared = false;
1013 }
1014
1015 // Don't put the shared buffer on the free list.
1016 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001017 mCore->mActiveBuffers.erase(slot);
1018 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001019 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001020
Dan Stoza289ade12014-02-28 11:17:17 -08001021 mSlots[slot].mFence = fence;
1022 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001023 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001024
1025 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001026}
1027
1028int BufferQueueProducer::query(int what, int *outValue) {
1029 ATRACE_CALL();
1030 Mutex::Autolock lock(mCore->mMutex);
1031
1032 if (outValue == NULL) {
1033 BQ_LOGE("query: outValue was NULL");
1034 return BAD_VALUE;
1035 }
1036
1037 if (mCore->mIsAbandoned) {
1038 BQ_LOGE("query: BufferQueue has been abandoned");
1039 return NO_INIT;
1040 }
1041
1042 int value;
1043 switch (what) {
1044 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001045 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001046 break;
1047 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001048 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001049 break;
1050 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001051 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001052 break;
1053 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001054 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001055 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001056 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001057 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001058 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001059 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1060 value = (mCore->mQueue.size() > 1);
1061 break;
1062 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001063 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001064 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001065 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1066 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1067 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001068 case NATIVE_WINDOW_BUFFER_AGE:
1069 if (mCore->mBufferAge > INT32_MAX) {
1070 value = 0;
1071 } else {
1072 value = static_cast<int32_t>(mCore->mBufferAge);
1073 }
1074 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001075 default:
1076 return BAD_VALUE;
1077 }
1078
1079 BQ_LOGV("query: %d? %d", what, value);
1080 *outValue = value;
1081 return NO_ERROR;
1082}
1083
Dan Stozaf0eaf252014-03-21 13:05:51 -07001084status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001085 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1086 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -08001087 int status = NO_ERROR;
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001088 sp<IConsumerListener> consumerListener;
1089 sp<IProducerListener> producerListener;
1090 std::vector<int> freedSlots;
1091 {
1092 Mutex::Autolock lock(mCore->mMutex);
1093 mConsumerName = mCore->mConsumerName;
1094 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1095 producerControlledByApp ? "true" : "false");
Dan Stoza289ade12014-02-28 11:17:17 -08001096
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001097 if (mCore->mIsAbandoned) {
1098 BQ_LOGE("connect: BufferQueue has been abandoned");
1099 return NO_INIT;
1100 }
1101
1102 if (mCore->mConsumerListener == NULL) {
1103 BQ_LOGE("connect: BufferQueue has no consumer");
1104 return NO_INIT;
1105 }
1106
1107 if (output == NULL) {
1108 BQ_LOGE("connect: output was NULL");
1109 return BAD_VALUE;
1110 }
1111
1112 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1113 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1114 mCore->mConnectedApi, api);
1115 return BAD_VALUE;
1116 }
1117
1118 bool dequeueBufferCannotBlock = mDequeueTimeout < 0 ?
1119 mCore->mConsumerControlledByApp && producerControlledByApp :
1120 false;
1121 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1122 dequeueBufferCannotBlock, mCore->mMaxBufferCount) -
1123 mCore->getMaxBufferCountLocked();
1124
1125 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
1126 BQ_LOGE("connect: BufferQueue failed to adjust the number of "
1127 "available slots. Delta = %d", delta);
1128 return BAD_VALUE;
1129 }
1130
1131 switch (api) {
1132 case NATIVE_WINDOW_API_EGL:
1133 case NATIVE_WINDOW_API_CPU:
1134 case NATIVE_WINDOW_API_MEDIA:
1135 case NATIVE_WINDOW_API_CAMERA:
1136 mCore->mConnectedApi = api;
1137 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1138 mCore->mTransformHint,
1139 static_cast<uint32_t>(mCore->mQueue.size()));
1140
1141 // Set up a death notification so that we can disconnect
1142 // automatically if the remote producer dies
1143 if (listener != NULL &&
1144 IInterface::asBinder(listener)->remoteBinder() !=
1145 NULL) {
1146 status = IInterface::asBinder(listener)->linkToDeath(
1147 static_cast<IBinder::DeathRecipient*>(this));
1148 if (status != NO_ERROR) {
1149 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1150 strerror(-status), status);
1151 }
Dan Stoza289ade12014-02-28 11:17:17 -08001152 }
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001153 mCore->mConnectedProducerListener = listener;
1154 break;
1155 default:
1156 BQ_LOGE("connect: unknown API %d", api);
1157 status = BAD_VALUE;
1158 break;
1159 }
1160
1161 mCore->mBufferHasBeenQueued = false;
1162 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1163
1164 mCore->mAllowAllocation = true;
1165 VALIDATE_CONSISTENCY();
1166
1167 if (delta < 0) {
1168 consumerListener = mCore->mConsumerListener;
1169 producerListener = listener;
1170 }
Dan Stoza289ade12014-02-28 11:17:17 -08001171 }
1172
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001173 // Call back without lock held
1174 if (consumerListener != NULL) {
1175 consumerListener->onBuffersReleased();
1176 }
1177 if (producerListener != NULL) {
1178 for (int i : freedSlots) {
1179 producerListener->onSlotFreed(i);
1180 }
Dan Stoza127fc632015-06-30 13:43:32 -07001181 }
Dan Stoza289ade12014-02-28 11:17:17 -08001182
1183 return status;
1184}
1185
1186status_t BufferQueueProducer::disconnect(int api) {
1187 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001188 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001189
1190 int status = NO_ERROR;
1191 sp<IConsumerListener> listener;
1192 { // Autolock scope
1193 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001194 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001195
1196 if (mCore->mIsAbandoned) {
1197 // It's not really an error to disconnect after the surface has
1198 // been abandoned; it should just be a no-op.
1199 return NO_ERROR;
1200 }
1201
1202 switch (api) {
1203 case NATIVE_WINDOW_API_EGL:
1204 case NATIVE_WINDOW_API_CPU:
1205 case NATIVE_WINDOW_API_MEDIA:
1206 case NATIVE_WINDOW_API_CAMERA:
1207 if (mCore->mConnectedApi == api) {
1208 mCore->freeAllBuffersLocked();
1209
1210 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001211 if (mCore->mConnectedProducerListener != NULL) {
1212 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001213 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001214 // This can fail if we're here because of the death
1215 // notification, but we just ignore it
1216 token->unlinkToDeath(
1217 static_cast<IBinder::DeathRecipient*>(this));
1218 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001219 mCore->mSingleBufferSlot =
1220 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001221 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001222 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001223 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001224 mCore->mDequeueCondition.broadcast();
1225 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001226 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001227 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001228 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001229 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001230 }
1231 break;
1232 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001233 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001234 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001235 break;
1236 }
1237 } // Autolock scope
1238
1239 // Call back without lock held
1240 if (listener != NULL) {
1241 listener->onBuffersReleased();
1242 }
1243
1244 return status;
1245}
1246
Jesse Hall399184a2014-03-03 15:42:54 -08001247status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001248 sp<IConsumerListener> listener;
1249 { // Autolock scope
1250 Mutex::Autolock _l(mCore->mMutex);
1251 mCore->mSidebandStream = stream;
1252 listener = mCore->mConsumerListener;
1253 } // Autolock scope
1254
1255 if (listener != NULL) {
1256 listener->onSidebandStreamChanged();
1257 }
Jesse Hall399184a2014-03-03 15:42:54 -08001258 return NO_ERROR;
1259}
1260
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001261void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1262 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001263 ATRACE_CALL();
1264 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001265 size_t newBufferCount = 0;
1266 uint32_t allocWidth = 0;
1267 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001268 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001269 uint32_t allocUsage = 0;
1270 { // Autolock scope
1271 Mutex::Autolock lock(mCore->mMutex);
1272 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001273
Dan Stoza9de72932015-04-16 17:28:43 -07001274 if (!mCore->mAllowAllocation) {
1275 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1276 "BufferQueue");
1277 return;
1278 }
1279
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001280 newBufferCount = mCore->mFreeSlots.size();
1281 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001282 return;
1283 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001284
Antoine Labour78014f32014-07-15 21:17:03 -07001285 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1286 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1287 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1288 allocUsage = usage | mCore->mConsumerUsageBits;
1289
1290 mCore->mIsAllocating = true;
1291 } // Autolock scope
1292
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001293 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001294 for (size_t i = 0; i < newBufferCount; ++i) {
1295 status_t result = NO_ERROR;
1296 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1297 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1298 if (result != NO_ERROR) {
1299 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1300 " %u, usage %u)", width, height, format, usage);
1301 Mutex::Autolock lock(mCore->mMutex);
1302 mCore->mIsAllocating = false;
1303 mCore->mIsAllocatingCondition.broadcast();
1304 return;
1305 }
1306 buffers.push_back(graphicBuffer);
1307 }
1308
1309 { // Autolock scope
1310 Mutex::Autolock lock(mCore->mMutex);
1311 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1312 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001313 PixelFormat checkFormat = format != 0 ?
1314 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001315 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1316 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1317 checkFormat != allocFormat || checkUsage != allocUsage) {
1318 // Something changed while we released the lock. Retry.
1319 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1320 mCore->mIsAllocating = false;
1321 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001322 continue;
1323 }
1324
Antoine Labour78014f32014-07-15 21:17:03 -07001325 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001326 if (mCore->mFreeSlots.empty()) {
1327 BQ_LOGV("allocateBuffers: a slot was occupied while "
1328 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001329 continue;
1330 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001331 auto slot = mCore->mFreeSlots.begin();
1332 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1333 mSlots[*slot].mGraphicBuffer = buffers[i];
1334 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001335
1336 // freeBufferLocked puts this slot on the free slots list. Since
1337 // we then attached a buffer, move the slot to free buffer list.
1338 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001339 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001340
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001341 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1342 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001343 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001344
Antoine Labour78014f32014-07-15 21:17:03 -07001345 mCore->mIsAllocating = false;
1346 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001347 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001348 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001349 }
1350}
1351
Dan Stoza9de72932015-04-16 17:28:43 -07001352status_t BufferQueueProducer::allowAllocation(bool allow) {
1353 ATRACE_CALL();
1354 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1355
1356 Mutex::Autolock lock(mCore->mMutex);
1357 mCore->mAllowAllocation = allow;
1358 return NO_ERROR;
1359}
1360
Dan Stoza812ed062015-06-02 15:45:22 -07001361status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1362 ATRACE_CALL();
1363 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1364
1365 Mutex::Autolock lock(mCore->mMutex);
1366 mCore->mGenerationNumber = generationNumber;
1367 return NO_ERROR;
1368}
1369
Dan Stozac6f30bd2015-06-08 09:32:50 -07001370String8 BufferQueueProducer::getConsumerName() const {
1371 ATRACE_CALL();
1372 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1373 return mConsumerName;
1374}
1375
Dan Stoza7dde5992015-05-22 09:51:44 -07001376uint64_t BufferQueueProducer::getNextFrameNumber() const {
1377 ATRACE_CALL();
1378
1379 Mutex::Autolock lock(mCore->mMutex);
1380 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1381 return nextFrameNumber;
1382}
1383
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001384status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1385 ATRACE_CALL();
1386 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1387
1388 Mutex::Autolock lock(mCore->mMutex);
1389 if (!singleBufferMode) {
1390 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1391 }
1392 mCore->mSingleBufferMode = singleBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001393 return NO_ERROR;
1394}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001395
Dan Stoza127fc632015-06-30 13:43:32 -07001396status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1397 ATRACE_CALL();
1398 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1399
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001400 sp<IConsumerListener> consumerListener;
1401 sp<IProducerListener> producerListener;
1402 std::vector<int> freedSlots;
1403 {
1404 Mutex::Autolock lock(mCore->mMutex);
1405 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1406 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1407 if (!mCore->adjustAvailableSlotsLocked(delta, &freedSlots)) {
1408 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number"
1409 " of available slots. Delta = %d", delta);
1410 return BAD_VALUE;
1411 }
1412
1413 mDequeueTimeout = timeout;
1414 mCore->mDequeueBufferCannotBlock = false;
1415
1416 VALIDATE_CONSISTENCY();
1417
1418 if (delta < 0) {
1419 consumerListener = mCore->mConsumerListener;
1420 producerListener = mCore->mConnectedProducerListener;
1421 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001422 }
1423
Pablo Ceballos789a0c82016-02-05 13:39:27 -08001424 // Call back without lock held
1425 if (consumerListener != NULL) {
1426 consumerListener->onBuffersReleased();
1427 }
1428 if (producerListener != NULL) {
1429 for (int i : freedSlots) {
1430 producerListener->onSlotFreed(i);
1431 }
1432 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001433
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001434 return NO_ERROR;
1435}
1436
Dan Stoza289ade12014-02-28 11:17:17 -08001437void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1438 // If we're here, it means that a producer we were connected to died.
1439 // We're guaranteed that we are still connected to it because we remove
1440 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1441 // without synchronization here.
1442 int api = mCore->mConnectedApi;
1443 disconnect(api);
1444}
1445
1446} // namespace android