blob: bb8d39b76005ccdc18cbd077e33f18039a2cc4be [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 Ceballos981066c2016-02-18 12:54:37 -080093 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -070094 { // Autolock scope
95 Mutex::Autolock lock(mCore->mMutex);
96 mCore->waitWhileAllocatingLocked();
97
98 if (mCore->mIsAbandoned) {
99 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
100 "abandoned");
101 return NO_INIT;
102 }
103
Pablo Ceballos72daab62015-12-07 16:38:43 -0800104 // The new maxDequeuedBuffer count should not be violated by the number
105 // of currently dequeued buffers
106 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800107 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700108 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800109 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700110 }
111 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800112 if (dequeuedCount > maxDequeuedBuffers) {
113 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
114 "count (%d) exceeds the current dequeued buffer count (%d)",
115 maxDequeuedBuffers, dequeuedCount);
116 return BAD_VALUE;
117 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700118
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700119 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700120 bufferCount += maxDequeuedBuffers;
121
122 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
123 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
124 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
125 return BAD_VALUE;
126 }
127
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700128 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700129 if (bufferCount < minBufferSlots) {
130 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
131 "less than minimum %d", bufferCount, minBufferSlots);
132 return BAD_VALUE;
133 }
134
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700135 if (bufferCount > mCore->mMaxBufferCount) {
136 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700137 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
138 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
139 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
140 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700141 return BAD_VALUE;
142 }
143
Pablo Ceballos72daab62015-12-07 16:38:43 -0800144 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800145 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800146 return BAD_VALUE;
147 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700148 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800149 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800150 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800151 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800152 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700153 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700154 } // Autolock scope
155
156 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800157 if (listener != NULL) {
158 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700159 }
160
161 return NO_ERROR;
162}
163
164status_t BufferQueueProducer::setAsyncMode(bool async) {
165 ATRACE_CALL();
166 BQ_LOGV("setAsyncMode: async = %d", async);
167
Pablo Ceballos981066c2016-02-18 12:54:37 -0800168 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700169 { // Autolock scope
170 Mutex::Autolock lock(mCore->mMutex);
171 mCore->waitWhileAllocatingLocked();
172
173 if (mCore->mIsAbandoned) {
174 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
175 return NO_INIT;
176 }
177
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700178 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700179 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
180 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700181 BQ_LOGE("setAsyncMode(%d): this call would cause the "
182 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700183 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
184 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
185 mCore->mMaxDequeuedBufferCount,
186 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 return BAD_VALUE;
188 }
189
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800190 int delta = mCore->getMaxBufferCountLocked(async,
191 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
192 - mCore->getMaxBufferCountLocked();
193
Pablo Ceballos981066c2016-02-18 12:54:37 -0800194 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800195 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
196 "available slots. Delta = %d", delta);
197 return BAD_VALUE;
198 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700199 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800200 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700201 mCore->mDequeueCondition.broadcast();
Pablo Ceballos981066c2016-02-18 12:54:37 -0800202 listener = mCore->mConsumerListener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700203 } // Autolock scope
204
205 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800206 if (listener != NULL) {
207 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700208 }
209 return NO_ERROR;
210}
211
Dan Stoza5ecfb682016-01-04 17:01:02 -0800212int BufferQueueProducer::getFreeBufferLocked() const {
213 if (mCore->mFreeBuffers.empty()) {
214 return BufferQueueCore::INVALID_BUFFER_SLOT;
215 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800216 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800217 mCore->mFreeBuffers.pop_front();
218 return slot;
219}
220
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800221int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800222 if (mCore->mFreeSlots.empty()) {
223 return BufferQueueCore::INVALID_BUFFER_SLOT;
224 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800225 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800226 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800227 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800228}
229
230status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800231 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800232 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
233 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800234 bool tryAgain = true;
235 while (tryAgain) {
236 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800237 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800238 return NO_INIT;
239 }
240
Dan Stoza9f3053d2014-03-06 15:14:33 -0800241 int dequeuedCount = 0;
242 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800243 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700244 if (mSlots[s].mBufferState.isDequeued()) {
245 ++dequeuedCount;
246 }
247 if (mSlots[s].mBufferState.isAcquired()) {
248 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800249 }
250 }
251
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700252 // Producers are not allowed to dequeue more than
253 // mMaxDequeuedBufferCount buffers.
254 // This check is only done if a buffer has already been queued
255 if (mCore->mBufferHasBeenQueued &&
256 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
257 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800258 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800259 return INVALID_OPERATION;
260 }
261
Dan Stoza0de7ea72015-04-23 13:20:51 -0700262 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
263
Dan Stozaae3c3682014-04-18 15:43:35 -0700264 // If we disconnect and reconnect quickly, we can be in a state where
265 // our slots are empty but we have many buffers in the queue. This can
266 // cause us to run out of memory if we outrun the consumer. Wait here if
267 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800268 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700269 bool tooManyBuffers = mCore->mQueue.size()
270 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700271 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800272 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700273 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700274 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700275 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700276 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700277 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700278 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700279 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800280 } else {
281 if (caller == FreeSlotCaller::Dequeue) {
282 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800283 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800284 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
285 *found = slot;
286 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800287 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800288 }
289 } else {
290 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800291 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800292 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
293 *found = slot;
294 } else {
295 *found = getFreeBufferLocked();
296 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700297 }
298 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700299 }
300
301 // If no buffer is found, or if the queue has too many buffers
302 // outstanding, wait for a buffer to be acquired or released, or for the
303 // max buffer count to change.
304 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
305 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800306 if (tryAgain) {
307 // Return an error if we're in non-blocking mode (producer and
308 // consumer are controlled by the application).
309 // However, the consumer is allowed to briefly acquire an extra
310 // buffer (which could cause us to have to wait here), which is
311 // okay, since it is only used to implement an atomic acquire +
312 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700313 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800314 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
315 return WOULD_BLOCK;
316 }
Dan Stoza127fc632015-06-30 13:43:32 -0700317 if (mDequeueTimeout >= 0) {
318 status_t result = mCore->mDequeueCondition.waitRelative(
319 mCore->mMutex, mDequeueTimeout);
320 if (result == TIMED_OUT) {
321 return result;
322 }
323 } else {
324 mCore->mDequeueCondition.wait(mCore->mMutex);
325 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800326 }
327 } // while (tryAgain)
328
329 return NO_ERROR;
330}
331
Dan Stoza289ade12014-02-28 11:17:17 -0800332status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700333 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
334 PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800335 ATRACE_CALL();
336 { // Autolock scope
337 Mutex::Autolock lock(mCore->mMutex);
338 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700339
340 if (mCore->mIsAbandoned) {
341 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
342 return NO_INIT;
343 }
344
345 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
346 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
347 return NO_INIT;
348 }
Dan Stoza289ade12014-02-28 11:17:17 -0800349 } // Autolock scope
350
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700351 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
352 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800353
354 if ((width && !height) || (!width && height)) {
355 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
356 return BAD_VALUE;
357 }
358
359 status_t returnFlags = NO_ERROR;
360 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
361 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800362 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800363
364 { // Autolock scope
365 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700366 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800367
368 if (format == 0) {
369 format = mCore->mDefaultBufferFormat;
370 }
371
372 // Enable the usage bits the consumer requested
373 usage |= mCore->mConsumerUsageBits;
374
Dan Stoza9de72932015-04-16 17:28:43 -0700375 const bool useDefaultSize = !width && !height;
376 if (useDefaultSize) {
377 width = mCore->mDefaultWidth;
378 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800379 }
Dan Stoza289ade12014-02-28 11:17:17 -0800380
Pablo Ceballos981066c2016-02-18 12:54:37 -0800381 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700382 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800383 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800384 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700385 if (status != NO_ERROR) {
386 return status;
387 }
388
389 // This should not happen
390 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
391 BQ_LOGE("dequeueBuffer: no available buffer slots");
392 return -EBUSY;
393 }
394
395 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
396
397 // If we are not allowed to allocate new buffers,
398 // waitForFreeSlotThenRelock must have returned a slot containing a
399 // buffer. If this buffer would require reallocation to meet the
400 // requested attributes, we free it and attempt to get another one.
401 if (!mCore->mAllowAllocation) {
402 if (buffer->needsReallocation(width, height, format, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700403 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700404 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
405 "buffer");
406 return BAD_VALUE;
407 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800408 mCore->mFreeSlots.insert(found);
409 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700410 found = BufferItem::INVALID_BUFFER_SLOT;
411 continue;
412 }
413 }
Dan Stoza289ade12014-02-28 11:17:17 -0800414 }
415
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800416 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700417 if (mCore->mSharedBufferSlot == found &&
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800418 buffer->needsReallocation(width, height, format, usage)) {
419 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
420 "buffer");
421
422 return BAD_VALUE;
423 }
424
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700425 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800426 mCore->mActiveBuffers.insert(found);
427 }
Dan Stoza289ade12014-02-28 11:17:17 -0800428 *outSlot = found;
429 ATRACE_BUFFER_INDEX(found);
430
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800431 attachedByConsumer = mSlots[found].mNeedsReallocation;
432 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800433
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700434 mSlots[found].mBufferState.dequeue();
435
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700436 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700437 // first buffer that is dequeued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700438 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700439 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700440 mCore->mSharedBufferSlot = found;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700441 mSlots[found].mBufferState.mShared = true;
442 }
Dan Stoza289ade12014-02-28 11:17:17 -0800443
Dan Stoza289ade12014-02-28 11:17:17 -0800444 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700445 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800446 {
447 mSlots[found].mAcquireCalled = false;
448 mSlots[found].mGraphicBuffer = NULL;
449 mSlots[found].mRequestBufferCalled = false;
450 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
451 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
452 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800453 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700454 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800455
456 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800457 } else {
458 // We add 1 because that will be the frame number when this buffer
459 // is queued
460 mCore->mBufferAge =
461 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800462 }
463
Dan Stoza800b41a2015-04-28 14:20:04 -0700464 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
465 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800466
Dan Stoza289ade12014-02-28 11:17:17 -0800467 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
468 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
469 "slot=%d w=%d h=%d format=%u",
470 found, buffer->width, buffer->height, buffer->format);
471 }
472
473 eglDisplay = mSlots[found].mEglDisplay;
474 eglFence = mSlots[found].mEglFence;
475 *outFence = mSlots[found].mFence;
476 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
477 mSlots[found].mFence = Fence::NO_FENCE;
478 } // Autolock scope
479
480 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
481 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700482 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800483 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800484 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800485 { // Autolock scope
486 Mutex::Autolock lock(mCore->mMutex);
487
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700488 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
489 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
490 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
491 }
492
493 mCore->mIsAllocating = false;
494 mCore->mIsAllocatingCondition.broadcast();
495
496 if (graphicBuffer == NULL) {
497 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
498 return error;
499 }
500
Dan Stoza289ade12014-02-28 11:17:17 -0800501 if (mCore->mIsAbandoned) {
502 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
503 return NO_INIT;
504 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800505
Pablo Ceballos9e314332016-01-12 13:49:19 -0800506 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800507 } // Autolock scope
508 }
509
Dan Stoza9f3053d2014-03-06 15:14:33 -0800510 if (attachedByConsumer) {
511 returnFlags |= BUFFER_NEEDS_REALLOCATION;
512 }
513
Dan Stoza289ade12014-02-28 11:17:17 -0800514 if (eglFence != EGL_NO_SYNC_KHR) {
515 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
516 1000000000);
517 // If something goes wrong, log the error, but return the buffer without
518 // synchronizing access to it. It's too late at this point to abort the
519 // dequeue operation.
520 if (result == EGL_FALSE) {
521 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
522 eglGetError());
523 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
524 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
525 }
526 eglDestroySyncKHR(eglDisplay, eglFence);
527 }
528
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700529 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
530 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800531 mSlots[*outSlot].mFrameNumber,
532 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
533
534 return returnFlags;
535}
536
Dan Stoza9f3053d2014-03-06 15:14:33 -0800537status_t BufferQueueProducer::detachBuffer(int slot) {
538 ATRACE_CALL();
539 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700540 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos981066c2016-02-18 12:54:37 -0800541 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800542
Pablo Ceballos981066c2016-02-18 12:54:37 -0800543 if (mCore->mIsAbandoned) {
544 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
545 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800546 }
547
Pablo Ceballos981066c2016-02-18 12:54:37 -0800548 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
549 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
550 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700551 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800552
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700553 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
554 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800555 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700556 }
557
Pablo Ceballos981066c2016-02-18 12:54:37 -0800558 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
559 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
560 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
561 return BAD_VALUE;
562 } else if (!mSlots[slot].mBufferState.isDequeued()) {
563 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
564 "(state = %s)", slot, mSlots[slot].mBufferState.string());
565 return BAD_VALUE;
566 } else if (!mSlots[slot].mRequestBufferCalled) {
567 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
568 slot);
569 return BAD_VALUE;
570 }
571
572 mSlots[slot].mBufferState.detachProducer();
573 mCore->mActiveBuffers.erase(slot);
574 mCore->mFreeSlots.insert(slot);
575 mCore->clearBufferSlotLocked(slot);
576 mCore->mDequeueCondition.broadcast();
577 VALIDATE_CONSISTENCY();
578
Dan Stoza9f3053d2014-03-06 15:14:33 -0800579 return NO_ERROR;
580}
581
Dan Stozad9822a32014-03-28 15:25:31 -0700582status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
583 sp<Fence>* outFence) {
584 ATRACE_CALL();
585
586 if (outBuffer == NULL) {
587 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
588 return BAD_VALUE;
589 } else if (outFence == NULL) {
590 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
591 return BAD_VALUE;
592 }
593
Pablo Ceballos981066c2016-02-18 12:54:37 -0800594 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700595
Pablo Ceballos981066c2016-02-18 12:54:37 -0800596 if (mCore->mIsAbandoned) {
597 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
598 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700599 }
600
Pablo Ceballos981066c2016-02-18 12:54:37 -0800601 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
602 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
603 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700604 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800605
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700606 if (mCore->mSharedBufferMode) {
607 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
608 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800609 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700610 }
611
Pablo Ceballos981066c2016-02-18 12:54:37 -0800612 mCore->waitWhileAllocatingLocked();
613
614 if (mCore->mFreeBuffers.empty()) {
615 return NO_MEMORY;
616 }
617
618 int found = mCore->mFreeBuffers.front();
619 mCore->mFreeBuffers.remove(found);
620 mCore->mFreeSlots.insert(found);
621
622 BQ_LOGV("detachNextBuffer detached slot %d", found);
623
624 *outBuffer = mSlots[found].mGraphicBuffer;
625 *outFence = mSlots[found].mFence;
626 mCore->clearBufferSlotLocked(found);
627 VALIDATE_CONSISTENCY();
628
Dan Stozad9822a32014-03-28 15:25:31 -0700629 return NO_ERROR;
630}
631
Dan Stoza9f3053d2014-03-06 15:14:33 -0800632status_t BufferQueueProducer::attachBuffer(int* outSlot,
633 const sp<android::GraphicBuffer>& buffer) {
634 ATRACE_CALL();
635
636 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700637 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800638 return BAD_VALUE;
639 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700640 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800641 return BAD_VALUE;
642 }
643
644 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700645
646 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700647 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700648 return NO_INIT;
649 }
650
651 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700652 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700653 return NO_INIT;
654 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800655
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700656 if (mCore->mSharedBufferMode) {
657 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700658 return BAD_VALUE;
659 }
660
Dan Stoza812ed062015-06-02 15:45:22 -0700661 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
662 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
663 "[queue %u]", buffer->getGenerationNumber(),
664 mCore->mGenerationNumber);
665 return BAD_VALUE;
666 }
667
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700668 mCore->waitWhileAllocatingLocked();
669
Dan Stoza9f3053d2014-03-06 15:14:33 -0800670 status_t returnFlags = NO_ERROR;
671 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800672 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800673 if (status != NO_ERROR) {
674 return status;
675 }
676
677 // This should not happen
678 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700679 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800680 return -EBUSY;
681 }
682
683 *outSlot = found;
684 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700685 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686 *outSlot, returnFlags);
687
688 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700689 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800690 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
691 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700692 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800693 mSlots[*outSlot].mAcquireCalled = false;
694 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800695 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700696
Dan Stoza9f3053d2014-03-06 15:14:33 -0800697 return returnFlags;
698}
699
Dan Stoza289ade12014-02-28 11:17:17 -0800700status_t BufferQueueProducer::queueBuffer(int slot,
701 const QueueBufferInput &input, QueueBufferOutput *output) {
702 ATRACE_CALL();
703 ATRACE_BUFFER_INDEX(slot);
704
705 int64_t timestamp;
706 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800707 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700708 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800709 int scalingMode;
710 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700711 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800712 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800713 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700714 &transform, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700715 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800716
717 if (fence == NULL) {
718 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800719 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800720 }
721
722 switch (scalingMode) {
723 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
724 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
725 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
726 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
727 break;
728 default:
729 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800730 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800731 }
732
Dan Stoza8dc55392014-11-04 11:37:46 -0800733 sp<IConsumerListener> frameAvailableListener;
734 sp<IConsumerListener> frameReplacedListener;
735 int callbackTicket = 0;
736 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800737 { // Autolock scope
738 Mutex::Autolock lock(mCore->mMutex);
739
740 if (mCore->mIsAbandoned) {
741 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
742 return NO_INIT;
743 }
744
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700745 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
746 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
747 return NO_INIT;
748 }
749
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800750 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800751 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800752 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800753 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700754 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800755 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700756 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800757 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800758 } else if (!mSlots[slot].mRequestBufferCalled) {
759 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
760 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800761 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800762 }
763
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700764 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700765 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700766 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700767 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700768 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700769 mSlots[slot].mBufferState.mShared = true;
770 }
771
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800772 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700773 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800774 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800775 crop.left, crop.top, crop.right, crop.bottom, transform,
776 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800777
778 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
779 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700780 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800781 crop.intersect(bufferRect, &croppedRect);
782 if (croppedRect != crop) {
783 BQ_LOGE("queueBuffer: crop rect is not contained within the "
784 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800785 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800786 }
787
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800788 // Override UNKNOWN dataspace with consumer default
789 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
790 dataSpace = mCore->mDefaultBufferDataSpace;
791 }
792
Dan Stoza289ade12014-02-28 11:17:17 -0800793 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700794 mSlots[slot].mBufferState.queue();
795
Dan Stoza289ade12014-02-28 11:17:17 -0800796 ++mCore->mFrameCounter;
797 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
798
Dan Stoza289ade12014-02-28 11:17:17 -0800799 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
800 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
801 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800802 item.mTransform = transform &
803 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800804 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800805 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
806 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800807 item.mTimestamp = timestamp;
808 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800809 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800810 item.mFrameNumber = mCore->mFrameCounter;
811 item.mSlot = slot;
812 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700813 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700814 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700815 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700816 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700817 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700818 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800819
Ruben Brunk1681d952014-06-27 15:51:55 -0700820 mStickyTransform = stickyTransform;
821
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700822 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700823 if (mCore->mSharedBufferMode) {
824 mCore->mSharedBufferCache.crop = crop;
825 mCore->mSharedBufferCache.transform = transform;
826 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700827 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700828 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700829 }
830
Dan Stoza289ade12014-02-28 11:17:17 -0800831 if (mCore->mQueue.empty()) {
832 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
833 // and simply queue this buffer
834 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800835 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800836 } else {
837 // When the queue is not empty, we need to look at the front buffer
838 // state to see if we need to replace it
839 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
840 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800841
842 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700843 mSlots[front->mSlot].mBufferState.freeQueued();
844
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700845 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700846 // still be around. Mark it as no longer shared if this
847 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700848 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700849 mSlots[front->mSlot].mBufferState.isFree()) {
850 mSlots[front->mSlot].mBufferState.mShared = false;
851 }
852 // Don't put the shared buffer on the free list.
853 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800854 mCore->mActiveBuffers.erase(front->mSlot);
855 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700856 }
Dan Stoza289ade12014-02-28 11:17:17 -0800857 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800858
Dan Stoza289ade12014-02-28 11:17:17 -0800859 // Overwrite the droppable buffer with the incoming one
860 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800861 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800862 } else {
863 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800864 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800865 }
866 }
867
868 mCore->mBufferHasBeenQueued = true;
869 mCore->mDequeueCondition.broadcast();
870
871 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800872 mCore->mTransformHint,
873 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800874
875 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800876
877 // Take a ticket for the callback functions
878 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700879
Pablo Ceballos9e314332016-01-12 13:49:19 -0800880 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800881 } // Autolock scope
882
Dan Stoza8dc55392014-11-04 11:37:46 -0800883 // Don't send the GraphicBuffer through the callback, and don't send
884 // the slot number, since the consumer shouldn't need it
885 item.mGraphicBuffer.clear();
886 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
887
888 // Call back without the main BufferQueue lock held, but with the callback
889 // lock held so we can ensure that callbacks occur in order
890 {
891 Mutex::Autolock lock(mCallbackMutex);
892 while (callbackTicket != mCurrentCallbackTicket) {
893 mCallbackCondition.wait(mCallbackMutex);
894 }
895
896 if (frameAvailableListener != NULL) {
897 frameAvailableListener->onFrameAvailable(item);
898 } else if (frameReplacedListener != NULL) {
899 frameReplacedListener->onFrameReplaced(item);
900 }
901
902 ++mCurrentCallbackTicket;
903 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800904 }
905
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000906 // Wait without lock held
907 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
908 // Waiting here allows for two full buffers to be queued but not a
909 // third. In the event that frames take varying time, this makes a
910 // small trade-off in favor of latency rather than throughput.
911 mLastQueueBufferFence->waitForever("Throttling EGL Production");
912 mLastQueueBufferFence = fence;
913 }
914
Dan Stoza289ade12014-02-28 11:17:17 -0800915 return NO_ERROR;
916}
917
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700918status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800919 ATRACE_CALL();
920 BQ_LOGV("cancelBuffer: slot %d", slot);
921 Mutex::Autolock lock(mCore->mMutex);
922
923 if (mCore->mIsAbandoned) {
924 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700925 return NO_INIT;
926 }
927
928 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
929 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
930 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800931 }
932
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700933 if (mCore->mSharedBufferMode) {
934 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700935 return BAD_VALUE;
936 }
937
Dan Stoza3e96f192014-03-03 10:16:19 -0800938 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800939 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800940 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700941 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700942 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800943 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700944 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700945 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800946 } else if (fence == NULL) {
947 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700948 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800949 }
950
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700951 mSlots[slot].mBufferState.cancel();
952
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700953 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700954 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700955 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700956 mSlots[slot].mBufferState.mShared = false;
957 }
958
959 // Don't put the shared buffer on the free list.
960 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800961 mCore->mActiveBuffers.erase(slot);
962 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700963 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800964
Dan Stoza289ade12014-02-28 11:17:17 -0800965 mSlots[slot].mFence = fence;
966 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800967 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700968
969 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800970}
971
972int BufferQueueProducer::query(int what, int *outValue) {
973 ATRACE_CALL();
974 Mutex::Autolock lock(mCore->mMutex);
975
976 if (outValue == NULL) {
977 BQ_LOGE("query: outValue was NULL");
978 return BAD_VALUE;
979 }
980
981 if (mCore->mIsAbandoned) {
982 BQ_LOGE("query: BufferQueue has been abandoned");
983 return NO_INIT;
984 }
985
986 int value;
987 switch (what) {
988 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800989 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800990 break;
991 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800992 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800993 break;
994 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800995 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800996 break;
997 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700998 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800999 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001000 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001002 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001003 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1004 value = (mCore->mQueue.size() > 1);
1005 break;
1006 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001007 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001008 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001009 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1010 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1011 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001012 case NATIVE_WINDOW_BUFFER_AGE:
1013 if (mCore->mBufferAge > INT32_MAX) {
1014 value = 0;
1015 } else {
1016 value = static_cast<int32_t>(mCore->mBufferAge);
1017 }
1018 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001019 default:
1020 return BAD_VALUE;
1021 }
1022
1023 BQ_LOGV("query: %d? %d", what, value);
1024 *outValue = value;
1025 return NO_ERROR;
1026}
1027
Dan Stozaf0eaf252014-03-21 13:05:51 -07001028status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001029 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1030 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001031 Mutex::Autolock lock(mCore->mMutex);
1032 mConsumerName = mCore->mConsumerName;
1033 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1034 producerControlledByApp ? "true" : "false");
1035
1036 if (mCore->mIsAbandoned) {
1037 BQ_LOGE("connect: BufferQueue has been abandoned");
1038 return NO_INIT;
1039 }
1040
1041 if (mCore->mConsumerListener == NULL) {
1042 BQ_LOGE("connect: BufferQueue has no consumer");
1043 return NO_INIT;
1044 }
1045
1046 if (output == NULL) {
1047 BQ_LOGE("connect: output was NULL");
1048 return BAD_VALUE;
1049 }
1050
1051 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1052 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1053 mCore->mConnectedApi, api);
1054 return BAD_VALUE;
1055 }
1056
1057 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1058 mDequeueTimeout < 0 ?
1059 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1060 mCore->mMaxBufferCount) -
1061 mCore->getMaxBufferCountLocked();
1062 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1063 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1064 "slots. Delta = %d", delta);
1065 return BAD_VALUE;
1066 }
1067
Dan Stoza289ade12014-02-28 11:17:17 -08001068 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001069 switch (api) {
1070 case NATIVE_WINDOW_API_EGL:
1071 case NATIVE_WINDOW_API_CPU:
1072 case NATIVE_WINDOW_API_MEDIA:
1073 case NATIVE_WINDOW_API_CAMERA:
1074 mCore->mConnectedApi = api;
1075 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1076 mCore->mTransformHint,
1077 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001078
Pablo Ceballos981066c2016-02-18 12:54:37 -08001079 // Set up a death notification so that we can disconnect
1080 // automatically if the remote producer dies
1081 if (listener != NULL &&
1082 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1083 status = IInterface::asBinder(listener)->linkToDeath(
1084 static_cast<IBinder::DeathRecipient*>(this));
1085 if (status != NO_ERROR) {
1086 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1087 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001088 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001089 }
1090 mCore->mConnectedProducerListener = listener;
1091 break;
1092 default:
1093 BQ_LOGE("connect: unknown API %d", api);
1094 status = BAD_VALUE;
1095 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001096 }
1097
Pablo Ceballos981066c2016-02-18 12:54:37 -08001098 mCore->mBufferHasBeenQueued = false;
1099 mCore->mDequeueBufferCannotBlock = false;
1100 if (mDequeueTimeout < 0) {
1101 mCore->mDequeueBufferCannotBlock =
1102 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001103 }
Dan Stoza289ade12014-02-28 11:17:17 -08001104
Pablo Ceballos981066c2016-02-18 12:54:37 -08001105 mCore->mAllowAllocation = true;
1106 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001107 return status;
1108}
1109
1110status_t BufferQueueProducer::disconnect(int api) {
1111 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001112 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001113
1114 int status = NO_ERROR;
1115 sp<IConsumerListener> listener;
1116 { // Autolock scope
1117 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001118 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001119
1120 if (mCore->mIsAbandoned) {
1121 // It's not really an error to disconnect after the surface has
1122 // been abandoned; it should just be a no-op.
1123 return NO_ERROR;
1124 }
1125
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001126 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1127 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001128 // If we're asked to disconnect the currently connected api but
1129 // nobody is connected, it's not really an error.
1130 if (api == BufferQueueCore::NO_CONNECTED_API) {
1131 return NO_ERROR;
1132 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001133 }
1134
Dan Stoza289ade12014-02-28 11:17:17 -08001135 switch (api) {
1136 case NATIVE_WINDOW_API_EGL:
1137 case NATIVE_WINDOW_API_CPU:
1138 case NATIVE_WINDOW_API_MEDIA:
1139 case NATIVE_WINDOW_API_CAMERA:
1140 if (mCore->mConnectedApi == api) {
1141 mCore->freeAllBuffersLocked();
1142
1143 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001144 if (mCore->mConnectedProducerListener != NULL) {
1145 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001146 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001147 // This can fail if we're here because of the death
1148 // notification, but we just ignore it
1149 token->unlinkToDeath(
1150 static_cast<IBinder::DeathRecipient*>(this));
1151 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001152 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001153 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001154 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001155 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001156 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001157 mCore->mDequeueCondition.broadcast();
1158 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001159 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001160 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001161 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001162 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001163 }
1164 break;
1165 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001166 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001167 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001168 break;
1169 }
1170 } // Autolock scope
1171
1172 // Call back without lock held
1173 if (listener != NULL) {
1174 listener->onBuffersReleased();
1175 }
1176
1177 return status;
1178}
1179
Jesse Hall399184a2014-03-03 15:42:54 -08001180status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001181 sp<IConsumerListener> listener;
1182 { // Autolock scope
1183 Mutex::Autolock _l(mCore->mMutex);
1184 mCore->mSidebandStream = stream;
1185 listener = mCore->mConsumerListener;
1186 } // Autolock scope
1187
1188 if (listener != NULL) {
1189 listener->onSidebandStreamChanged();
1190 }
Jesse Hall399184a2014-03-03 15:42:54 -08001191 return NO_ERROR;
1192}
1193
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001194void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1195 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001196 ATRACE_CALL();
1197 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001198 size_t newBufferCount = 0;
1199 uint32_t allocWidth = 0;
1200 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001201 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001202 uint32_t allocUsage = 0;
1203 { // Autolock scope
1204 Mutex::Autolock lock(mCore->mMutex);
1205 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001206
Dan Stoza9de72932015-04-16 17:28:43 -07001207 if (!mCore->mAllowAllocation) {
1208 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1209 "BufferQueue");
1210 return;
1211 }
1212
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001213 newBufferCount = mCore->mFreeSlots.size();
1214 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001215 return;
1216 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001217
Antoine Labour78014f32014-07-15 21:17:03 -07001218 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1219 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1220 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1221 allocUsage = usage | mCore->mConsumerUsageBits;
1222
1223 mCore->mIsAllocating = true;
1224 } // Autolock scope
1225
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001226 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001227 for (size_t i = 0; i < newBufferCount; ++i) {
1228 status_t result = NO_ERROR;
1229 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1230 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1231 if (result != NO_ERROR) {
1232 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1233 " %u, usage %u)", width, height, format, usage);
1234 Mutex::Autolock lock(mCore->mMutex);
1235 mCore->mIsAllocating = false;
1236 mCore->mIsAllocatingCondition.broadcast();
1237 return;
1238 }
1239 buffers.push_back(graphicBuffer);
1240 }
1241
1242 { // Autolock scope
1243 Mutex::Autolock lock(mCore->mMutex);
1244 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1245 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001246 PixelFormat checkFormat = format != 0 ?
1247 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001248 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1249 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1250 checkFormat != allocFormat || checkUsage != allocUsage) {
1251 // Something changed while we released the lock. Retry.
1252 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1253 mCore->mIsAllocating = false;
1254 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001255 continue;
1256 }
1257
Antoine Labour78014f32014-07-15 21:17:03 -07001258 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001259 if (mCore->mFreeSlots.empty()) {
1260 BQ_LOGV("allocateBuffers: a slot was occupied while "
1261 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001262 continue;
1263 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001264 auto slot = mCore->mFreeSlots.begin();
1265 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1266 mSlots[*slot].mGraphicBuffer = buffers[i];
1267 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001268
1269 // freeBufferLocked puts this slot on the free slots list. Since
1270 // we then attached a buffer, move the slot to free buffer list.
1271 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001272 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001273
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001274 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1275 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001276 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001277
Antoine Labour78014f32014-07-15 21:17:03 -07001278 mCore->mIsAllocating = false;
1279 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001280 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001281 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001282 }
1283}
1284
Dan Stoza9de72932015-04-16 17:28:43 -07001285status_t BufferQueueProducer::allowAllocation(bool allow) {
1286 ATRACE_CALL();
1287 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1288
1289 Mutex::Autolock lock(mCore->mMutex);
1290 mCore->mAllowAllocation = allow;
1291 return NO_ERROR;
1292}
1293
Dan Stoza812ed062015-06-02 15:45:22 -07001294status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1295 ATRACE_CALL();
1296 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1297
1298 Mutex::Autolock lock(mCore->mMutex);
1299 mCore->mGenerationNumber = generationNumber;
1300 return NO_ERROR;
1301}
1302
Dan Stozac6f30bd2015-06-08 09:32:50 -07001303String8 BufferQueueProducer::getConsumerName() const {
1304 ATRACE_CALL();
1305 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1306 return mConsumerName;
1307}
1308
Dan Stoza7dde5992015-05-22 09:51:44 -07001309uint64_t BufferQueueProducer::getNextFrameNumber() const {
1310 ATRACE_CALL();
1311
1312 Mutex::Autolock lock(mCore->mMutex);
1313 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1314 return nextFrameNumber;
1315}
1316
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001317status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001318 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001319 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001320
1321 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001322 if (!sharedBufferMode) {
1323 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001324 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001325 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001326 return NO_ERROR;
1327}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001328
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001329status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1330 ATRACE_CALL();
1331 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1332
1333 Mutex::Autolock lock(mCore->mMutex);
1334
1335 mCore->mAutoRefresh = autoRefresh;
1336 return NO_ERROR;
1337}
1338
Dan Stoza127fc632015-06-30 13:43:32 -07001339status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1340 ATRACE_CALL();
1341 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1342
Pablo Ceballos981066c2016-02-18 12:54:37 -08001343 Mutex::Autolock lock(mCore->mMutex);
1344 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1345 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1346 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1347 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1348 "available slots. Delta = %d", delta);
1349 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001350 }
1351
Pablo Ceballos981066c2016-02-18 12:54:37 -08001352 mDequeueTimeout = timeout;
1353 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001354
Pablo Ceballos981066c2016-02-18 12:54:37 -08001355 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001356 return NO_ERROR;
1357}
1358
Dan Stoza289ade12014-02-28 11:17:17 -08001359void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1360 // If we're here, it means that a producer we were connected to died.
1361 // We're guaranteed that we are still connected to it because we remove
1362 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1363 // without synchronization here.
1364 int api = mCore->mConnectedApi;
1365 disconnect(api);
1366}
1367
1368} // namespace android