blob: 4c3bcd96fc42fc5ad1f6bfb0f3ba1074f18f1b21 [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();
Dan Stoza50101d02016-04-07 16:53:23 -0700870 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800871
872 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800873 mCore->mTransformHint,
874 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800875
876 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800877
878 // Take a ticket for the callback functions
879 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700880
Pablo Ceballos9e314332016-01-12 13:49:19 -0800881 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800882 } // Autolock scope
883
Dan Stoza8dc55392014-11-04 11:37:46 -0800884 // Don't send the GraphicBuffer through the callback, and don't send
885 // the slot number, since the consumer shouldn't need it
886 item.mGraphicBuffer.clear();
887 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
888
889 // Call back without the main BufferQueue lock held, but with the callback
890 // lock held so we can ensure that callbacks occur in order
891 {
892 Mutex::Autolock lock(mCallbackMutex);
893 while (callbackTicket != mCurrentCallbackTicket) {
894 mCallbackCondition.wait(mCallbackMutex);
895 }
896
897 if (frameAvailableListener != NULL) {
898 frameAvailableListener->onFrameAvailable(item);
899 } else if (frameReplacedListener != NULL) {
900 frameReplacedListener->onFrameReplaced(item);
901 }
902
903 ++mCurrentCallbackTicket;
904 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800905 }
906
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000907 // Wait without lock held
908 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
909 // Waiting here allows for two full buffers to be queued but not a
910 // third. In the event that frames take varying time, this makes a
911 // small trade-off in favor of latency rather than throughput.
912 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000913 }
Dan Stoza50101d02016-04-07 16:53:23 -0700914 mLastQueueBufferFence = fence;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000915
Dan Stoza289ade12014-02-28 11:17:17 -0800916 return NO_ERROR;
917}
918
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700919status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800920 ATRACE_CALL();
921 BQ_LOGV("cancelBuffer: slot %d", slot);
922 Mutex::Autolock lock(mCore->mMutex);
923
924 if (mCore->mIsAbandoned) {
925 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700926 return NO_INIT;
927 }
928
929 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
930 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
931 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800932 }
933
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700934 if (mCore->mSharedBufferMode) {
935 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700936 return BAD_VALUE;
937 }
938
Dan Stoza3e96f192014-03-03 10:16:19 -0800939 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800940 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800941 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700942 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700943 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800944 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700945 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700946 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800947 } else if (fence == NULL) {
948 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700949 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800950 }
951
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700952 mSlots[slot].mBufferState.cancel();
953
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700954 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700955 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700956 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700957 mSlots[slot].mBufferState.mShared = false;
958 }
959
960 // Don't put the shared buffer on the free list.
961 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800962 mCore->mActiveBuffers.erase(slot);
963 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700964 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800965
Dan Stoza289ade12014-02-28 11:17:17 -0800966 mSlots[slot].mFence = fence;
967 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800968 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700969
970 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800971}
972
973int BufferQueueProducer::query(int what, int *outValue) {
974 ATRACE_CALL();
975 Mutex::Autolock lock(mCore->mMutex);
976
977 if (outValue == NULL) {
978 BQ_LOGE("query: outValue was NULL");
979 return BAD_VALUE;
980 }
981
982 if (mCore->mIsAbandoned) {
983 BQ_LOGE("query: BufferQueue has been abandoned");
984 return NO_INIT;
985 }
986
987 int value;
988 switch (what) {
989 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800990 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800991 break;
992 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800993 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800994 break;
995 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800996 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800997 break;
998 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700999 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001000 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001001 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001002 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001003 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001004 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1005 value = (mCore->mQueue.size() > 1);
1006 break;
1007 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001008 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001009 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001010 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1011 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1012 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001013 case NATIVE_WINDOW_BUFFER_AGE:
1014 if (mCore->mBufferAge > INT32_MAX) {
1015 value = 0;
1016 } else {
1017 value = static_cast<int32_t>(mCore->mBufferAge);
1018 }
1019 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001020 default:
1021 return BAD_VALUE;
1022 }
1023
1024 BQ_LOGV("query: %d? %d", what, value);
1025 *outValue = value;
1026 return NO_ERROR;
1027}
1028
Dan Stozaf0eaf252014-03-21 13:05:51 -07001029status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001030 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1031 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001032 Mutex::Autolock lock(mCore->mMutex);
1033 mConsumerName = mCore->mConsumerName;
1034 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1035 producerControlledByApp ? "true" : "false");
1036
1037 if (mCore->mIsAbandoned) {
1038 BQ_LOGE("connect: BufferQueue has been abandoned");
1039 return NO_INIT;
1040 }
1041
1042 if (mCore->mConsumerListener == NULL) {
1043 BQ_LOGE("connect: BufferQueue has no consumer");
1044 return NO_INIT;
1045 }
1046
1047 if (output == NULL) {
1048 BQ_LOGE("connect: output was NULL");
1049 return BAD_VALUE;
1050 }
1051
1052 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1053 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1054 mCore->mConnectedApi, api);
1055 return BAD_VALUE;
1056 }
1057
1058 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1059 mDequeueTimeout < 0 ?
1060 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1061 mCore->mMaxBufferCount) -
1062 mCore->getMaxBufferCountLocked();
1063 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1064 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1065 "slots. Delta = %d", delta);
1066 return BAD_VALUE;
1067 }
1068
Dan Stoza289ade12014-02-28 11:17:17 -08001069 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001070 switch (api) {
1071 case NATIVE_WINDOW_API_EGL:
1072 case NATIVE_WINDOW_API_CPU:
1073 case NATIVE_WINDOW_API_MEDIA:
1074 case NATIVE_WINDOW_API_CAMERA:
1075 mCore->mConnectedApi = api;
1076 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
1077 mCore->mTransformHint,
1078 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001079
Pablo Ceballos981066c2016-02-18 12:54:37 -08001080 // Set up a death notification so that we can disconnect
1081 // automatically if the remote producer dies
1082 if (listener != NULL &&
1083 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1084 status = IInterface::asBinder(listener)->linkToDeath(
1085 static_cast<IBinder::DeathRecipient*>(this));
1086 if (status != NO_ERROR) {
1087 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1088 strerror(-status), status);
Dan Stoza289ade12014-02-28 11:17:17 -08001089 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001090 }
1091 mCore->mConnectedProducerListener = listener;
1092 break;
1093 default:
1094 BQ_LOGE("connect: unknown API %d", api);
1095 status = BAD_VALUE;
1096 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001097 }
1098
Pablo Ceballos981066c2016-02-18 12:54:37 -08001099 mCore->mBufferHasBeenQueued = false;
1100 mCore->mDequeueBufferCannotBlock = false;
1101 if (mDequeueTimeout < 0) {
1102 mCore->mDequeueBufferCannotBlock =
1103 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001104 }
Dan Stoza289ade12014-02-28 11:17:17 -08001105
Pablo Ceballos981066c2016-02-18 12:54:37 -08001106 mCore->mAllowAllocation = true;
1107 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001108 return status;
1109}
1110
1111status_t BufferQueueProducer::disconnect(int api) {
1112 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001113 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001114
1115 int status = NO_ERROR;
1116 sp<IConsumerListener> listener;
1117 { // Autolock scope
1118 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001119 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001120
1121 if (mCore->mIsAbandoned) {
1122 // It's not really an error to disconnect after the surface has
1123 // been abandoned; it should just be a no-op.
1124 return NO_ERROR;
1125 }
1126
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001127 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1128 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001129 // If we're asked to disconnect the currently connected api but
1130 // nobody is connected, it's not really an error.
1131 if (api == BufferQueueCore::NO_CONNECTED_API) {
1132 return NO_ERROR;
1133 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001134 }
1135
Dan Stoza289ade12014-02-28 11:17:17 -08001136 switch (api) {
1137 case NATIVE_WINDOW_API_EGL:
1138 case NATIVE_WINDOW_API_CPU:
1139 case NATIVE_WINDOW_API_MEDIA:
1140 case NATIVE_WINDOW_API_CAMERA:
1141 if (mCore->mConnectedApi == api) {
1142 mCore->freeAllBuffersLocked();
1143
1144 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001145 if (mCore->mConnectedProducerListener != NULL) {
1146 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001147 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001148 // This can fail if we're here because of the death
1149 // notification, but we just ignore it
1150 token->unlinkToDeath(
1151 static_cast<IBinder::DeathRecipient*>(this));
1152 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001153 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001154 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001155 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001156 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001157 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001158 mCore->mDequeueCondition.broadcast();
1159 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001160 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001161 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001162 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001163 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001164 }
1165 break;
1166 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001167 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001168 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001169 break;
1170 }
1171 } // Autolock scope
1172
1173 // Call back without lock held
1174 if (listener != NULL) {
1175 listener->onBuffersReleased();
1176 }
1177
1178 return status;
1179}
1180
Jesse Hall399184a2014-03-03 15:42:54 -08001181status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001182 sp<IConsumerListener> listener;
1183 { // Autolock scope
1184 Mutex::Autolock _l(mCore->mMutex);
1185 mCore->mSidebandStream = stream;
1186 listener = mCore->mConsumerListener;
1187 } // Autolock scope
1188
1189 if (listener != NULL) {
1190 listener->onSidebandStreamChanged();
1191 }
Jesse Hall399184a2014-03-03 15:42:54 -08001192 return NO_ERROR;
1193}
1194
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001195void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1196 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001197 ATRACE_CALL();
1198 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001199 size_t newBufferCount = 0;
1200 uint32_t allocWidth = 0;
1201 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001202 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001203 uint32_t allocUsage = 0;
1204 { // Autolock scope
1205 Mutex::Autolock lock(mCore->mMutex);
1206 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001207
Dan Stoza9de72932015-04-16 17:28:43 -07001208 if (!mCore->mAllowAllocation) {
1209 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1210 "BufferQueue");
1211 return;
1212 }
1213
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001214 newBufferCount = mCore->mFreeSlots.size();
1215 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001216 return;
1217 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001218
Antoine Labour78014f32014-07-15 21:17:03 -07001219 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1220 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1221 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1222 allocUsage = usage | mCore->mConsumerUsageBits;
1223
1224 mCore->mIsAllocating = true;
1225 } // Autolock scope
1226
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001227 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001228 for (size_t i = 0; i < newBufferCount; ++i) {
1229 status_t result = NO_ERROR;
1230 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1231 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1232 if (result != NO_ERROR) {
1233 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1234 " %u, usage %u)", width, height, format, usage);
1235 Mutex::Autolock lock(mCore->mMutex);
1236 mCore->mIsAllocating = false;
1237 mCore->mIsAllocatingCondition.broadcast();
1238 return;
1239 }
1240 buffers.push_back(graphicBuffer);
1241 }
1242
1243 { // Autolock scope
1244 Mutex::Autolock lock(mCore->mMutex);
1245 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1246 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001247 PixelFormat checkFormat = format != 0 ?
1248 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001249 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1250 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1251 checkFormat != allocFormat || checkUsage != allocUsage) {
1252 // Something changed while we released the lock. Retry.
1253 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1254 mCore->mIsAllocating = false;
1255 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001256 continue;
1257 }
1258
Antoine Labour78014f32014-07-15 21:17:03 -07001259 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001260 if (mCore->mFreeSlots.empty()) {
1261 BQ_LOGV("allocateBuffers: a slot was occupied while "
1262 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001263 continue;
1264 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001265 auto slot = mCore->mFreeSlots.begin();
1266 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1267 mSlots[*slot].mGraphicBuffer = buffers[i];
1268 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001269
1270 // freeBufferLocked puts this slot on the free slots list. Since
1271 // we then attached a buffer, move the slot to free buffer list.
1272 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001273 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001274
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001275 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1276 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001277 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001278
Antoine Labour78014f32014-07-15 21:17:03 -07001279 mCore->mIsAllocating = false;
1280 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001281 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001282 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001283 }
1284}
1285
Dan Stoza9de72932015-04-16 17:28:43 -07001286status_t BufferQueueProducer::allowAllocation(bool allow) {
1287 ATRACE_CALL();
1288 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1289
1290 Mutex::Autolock lock(mCore->mMutex);
1291 mCore->mAllowAllocation = allow;
1292 return NO_ERROR;
1293}
1294
Dan Stoza812ed062015-06-02 15:45:22 -07001295status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1296 ATRACE_CALL();
1297 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1298
1299 Mutex::Autolock lock(mCore->mMutex);
1300 mCore->mGenerationNumber = generationNumber;
1301 return NO_ERROR;
1302}
1303
Dan Stozac6f30bd2015-06-08 09:32:50 -07001304String8 BufferQueueProducer::getConsumerName() const {
1305 ATRACE_CALL();
1306 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1307 return mConsumerName;
1308}
1309
Dan Stoza7dde5992015-05-22 09:51:44 -07001310uint64_t BufferQueueProducer::getNextFrameNumber() const {
1311 ATRACE_CALL();
1312
1313 Mutex::Autolock lock(mCore->mMutex);
1314 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1315 return nextFrameNumber;
1316}
1317
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001318status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001319 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001320 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001321
1322 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001323 if (!sharedBufferMode) {
1324 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001325 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001326 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001327 return NO_ERROR;
1328}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001329
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001330status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1331 ATRACE_CALL();
1332 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1333
1334 Mutex::Autolock lock(mCore->mMutex);
1335
1336 mCore->mAutoRefresh = autoRefresh;
1337 return NO_ERROR;
1338}
1339
Dan Stoza127fc632015-06-30 13:43:32 -07001340status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1341 ATRACE_CALL();
1342 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1343
Pablo Ceballos981066c2016-02-18 12:54:37 -08001344 Mutex::Autolock lock(mCore->mMutex);
1345 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1346 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1347 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1348 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1349 "available slots. Delta = %d", delta);
1350 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001351 }
1352
Pablo Ceballos981066c2016-02-18 12:54:37 -08001353 mDequeueTimeout = timeout;
1354 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001355
Pablo Ceballos981066c2016-02-18 12:54:37 -08001356 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001357 return NO_ERROR;
1358}
1359
Dan Stoza50101d02016-04-07 16:53:23 -07001360status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
1361 sp<Fence>* outFence) {
1362 ATRACE_CALL();
1363 BQ_LOGV("getLastQueuedBuffer");
1364
1365 Mutex::Autolock lock(mCore->mMutex);
1366 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1367 *outBuffer = nullptr;
1368 *outFence = Fence::NO_FENCE;
1369 return NO_ERROR;
1370 }
1371
1372 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1373 *outFence = mLastQueueBufferFence;
1374
1375 return NO_ERROR;
1376}
1377
Dan Stoza289ade12014-02-28 11:17:17 -08001378void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1379 // If we're here, it means that a producer we were connected to died.
1380 // We're guaranteed that we are still connected to it because we remove
1381 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1382 // without synchronization here.
1383 int api = mCore->mConnectedApi;
1384 disconnect(api);
1385}
1386
1387} // namespace android