blob: 9d4246482c6266495aa5ded1074ab8e62a44a5ef [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
93 sp<IConsumerListener> listener;
94 { // 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;
145 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) {
151 listener = mCore->mConsumerListener;
152 }
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
157 if (listener != NULL) {
158 listener->onBuffersReleased();
159 }
160
161 return NO_ERROR;
162}
163
164status_t BufferQueueProducer::setAsyncMode(bool async) {
165 ATRACE_CALL();
166 BQ_LOGV("setAsyncMode: async = %d", async);
167
168 sp<IConsumerListener> listener;
169 { // 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
194 if (!mCore->adjustAvailableSlotsLocked(delta)) {
195 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();
202 listener = mCore->mConsumerListener;
203 } // Autolock scope
204
205 // Call back without lock held
206 if (listener != NULL) {
207 listener->onBuffersReleased();
208 }
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 Ceballos23b4abe2016-01-08 12:15:22 -0800225 auto slot = mCore->mFreeSlots.begin();
226 mCore->mFreeSlots.erase(slot);
227 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 Ceballosccdfd602015-10-07 15:05:45 -0700275 // If in single buffer mode and a shared buffer exists, always
276 // return it.
277 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot !=
278 BufferQueueCore::INVALID_BUFFER_SLOT) {
279 *found = mCore->mSingleBufferSlot;
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
Dan Stoza9de72932015-04-16 17:28:43 -0700381 int found = BufferItem::INVALID_BUFFER_SLOT;
382 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 Ceballos23b4abe2016-01-08 12:15:22 -0800403 if (mCore->mSingleBufferSlot == 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);
417 if (mCore->mSingleBufferSlot == found &&
418 buffer->needsReallocation(width, height, format, usage)) {
419 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
420 "buffer");
421
422 return BAD_VALUE;
423 }
424
425 if (mCore->mSingleBufferSlot != found) {
426 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
436 // If single buffer mode has just been enabled, cache the slot of the
437 // first buffer that is dequeued and mark it as the shared buffer.
438 if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot ==
439 BufferQueueCore::INVALID_BUFFER_SLOT) {
440 mCore->mSingleBufferSlot = found;
441 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);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800541 Mutex::Autolock lock(mCore->mMutex);
542
543 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700544 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800545 return NO_INIT;
546 }
547
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700548 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700549 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700550 return NO_INIT;
551 }
552
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800553 if (mCore->mSingleBufferMode || mCore->mSingleBufferSlot == slot) {
554 BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700555 return BAD_VALUE;
556 }
557
Dan Stoza9f3053d2014-03-06 15:14:33 -0800558 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700559 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800560 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
561 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700562 } 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());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800565 return BAD_VALUE;
566 } else if (!mSlots[slot].mRequestBufferCalled) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700567 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800568 slot);
569 return BAD_VALUE;
570 }
571
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700572 mSlots[slot].mBufferState.detachProducer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800573 mCore->mActiveBuffers.erase(slot);
574 mCore->mFreeSlots.insert(slot);
575 mCore->clearBufferSlotLocked(slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800576 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800577 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800578
579 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
594 Mutex::Autolock lock(mCore->mMutex);
595
596 if (mCore->mIsAbandoned) {
597 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
598 return NO_INIT;
599 }
600
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700601 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
602 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
603 return NO_INIT;
604 }
605
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700606 if (mCore->mSingleBufferMode) {
607 BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer"
608 "mode");
609 return BAD_VALUE;
610 }
611
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700612 mCore->waitWhileAllocatingLocked();
613
Dan Stoza0de7ea72015-04-23 13:20:51 -0700614 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000615 return NO_MEMORY;
616 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700617
Dan Stoza0de7ea72015-04-23 13:20:51 -0700618 int found = mCore->mFreeBuffers.front();
619 mCore->mFreeBuffers.remove(found);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800620 mCore->mFreeSlots.insert(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700621
Dan Stozad9822a32014-03-28 15:25:31 -0700622 BQ_LOGV("detachNextBuffer detached slot %d", found);
623
624 *outBuffer = mSlots[found].mGraphicBuffer;
625 *outFence = mSlots[found].mFence;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800626 mCore->clearBufferSlotLocked(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800627 VALIDATE_CONSISTENCY();
Dan Stozad9822a32014-03-28 15:25:31 -0700628
629 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 Ceballosccdfd602015-10-07 15:05:45 -0700656 if (mCore->mSingleBufferMode) {
Craig Donner05249fc2016-01-15 19:33:55 -0800657 BQ_LOGE("attachBuffer: cannot attach a buffer in single 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
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800764 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700765 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800766 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800767 crop.left, crop.top, crop.right, crop.bottom, transform,
768 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800769
770 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
771 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700772 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800773 crop.intersect(bufferRect, &croppedRect);
774 if (croppedRect != crop) {
775 BQ_LOGE("queueBuffer: crop rect is not contained within the "
776 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800777 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800778 }
779
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800780 // Override UNKNOWN dataspace with consumer default
781 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
782 dataSpace = mCore->mDefaultBufferDataSpace;
783 }
784
Dan Stoza289ade12014-02-28 11:17:17 -0800785 mSlots[slot].mFence = fence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700786 mSlots[slot].mBufferState.queue();
787
Dan Stoza289ade12014-02-28 11:17:17 -0800788 ++mCore->mFrameCounter;
789 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
790
Dan Stoza289ade12014-02-28 11:17:17 -0800791 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
792 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
793 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800794 item.mTransform = transform &
795 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800796 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800797 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
798 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800799 item.mTimestamp = timestamp;
800 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800801 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 item.mFrameNumber = mCore->mFrameCounter;
803 item.mSlot = slot;
804 item.mFence = fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700805 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700806 mCore->mDequeueBufferCannotBlock ||
807 (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700808 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700809 item.mSingleBufferMode = mCore->mSingleBufferMode;
810 item.mQueuedBuffer = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800811
Ruben Brunk1681d952014-06-27 15:51:55 -0700812 mStickyTransform = stickyTransform;
813
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700814 // Cache the shared buffer data so that the BufferItem can be recreated.
815 if (mCore->mSingleBufferMode) {
816 mCore->mSingleBufferCache.crop = crop;
817 mCore->mSingleBufferCache.transform = transform;
818 mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>(
819 scalingMode);
820 mCore->mSingleBufferCache.dataspace = dataSpace;
821 }
822
Dan Stoza289ade12014-02-28 11:17:17 -0800823 if (mCore->mQueue.empty()) {
824 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
825 // and simply queue this buffer
826 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800827 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800828 } else {
829 // When the queue is not empty, we need to look at the front buffer
830 // state to see if we need to replace it
831 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
832 if (front->mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800833
834 if (!front->mIsStale) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700835 mSlots[front->mSlot].mBufferState.freeQueued();
836
837 // After leaving single buffer mode, the shared buffer will
838 // still be around. Mark it as no longer shared if this
839 // operation causes it to be free.
840 if (!mCore->mSingleBufferMode &&
841 mSlots[front->mSlot].mBufferState.isFree()) {
842 mSlots[front->mSlot].mBufferState.mShared = false;
843 }
844 // Don't put the shared buffer on the free list.
845 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800846 mCore->mActiveBuffers.erase(front->mSlot);
847 mCore->mFreeBuffers.push_back(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700848 }
Dan Stoza289ade12014-02-28 11:17:17 -0800849 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800850
Dan Stoza289ade12014-02-28 11:17:17 -0800851 // Overwrite the droppable buffer with the incoming one
852 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800853 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800854 } else {
855 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800856 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800857 }
858 }
859
860 mCore->mBufferHasBeenQueued = true;
861 mCore->mDequeueCondition.broadcast();
862
863 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 mCore->mTransformHint,
865 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800866
867 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800868
869 // Take a ticket for the callback functions
870 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700871
Pablo Ceballos9e314332016-01-12 13:49:19 -0800872 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800873 } // Autolock scope
874
Dan Stoza8dc55392014-11-04 11:37:46 -0800875 // Don't send the GraphicBuffer through the callback, and don't send
876 // the slot number, since the consumer shouldn't need it
877 item.mGraphicBuffer.clear();
878 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
879
880 // Call back without the main BufferQueue lock held, but with the callback
881 // lock held so we can ensure that callbacks occur in order
882 {
883 Mutex::Autolock lock(mCallbackMutex);
884 while (callbackTicket != mCurrentCallbackTicket) {
885 mCallbackCondition.wait(mCallbackMutex);
886 }
887
888 if (frameAvailableListener != NULL) {
889 frameAvailableListener->onFrameAvailable(item);
890 } else if (frameReplacedListener != NULL) {
891 frameReplacedListener->onFrameReplaced(item);
892 }
893
894 ++mCurrentCallbackTicket;
895 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800896 }
897
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000898 // Wait without lock held
899 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
900 // Waiting here allows for two full buffers to be queued but not a
901 // third. In the event that frames take varying time, this makes a
902 // small trade-off in favor of latency rather than throughput.
903 mLastQueueBufferFence->waitForever("Throttling EGL Production");
904 mLastQueueBufferFence = fence;
905 }
906
Dan Stoza289ade12014-02-28 11:17:17 -0800907 return NO_ERROR;
908}
909
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700910status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800911 ATRACE_CALL();
912 BQ_LOGV("cancelBuffer: slot %d", slot);
913 Mutex::Autolock lock(mCore->mMutex);
914
915 if (mCore->mIsAbandoned) {
916 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700917 return NO_INIT;
918 }
919
920 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
921 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
922 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -0800923 }
924
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700925 if (mCore->mSingleBufferMode) {
926 BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode");
927 return BAD_VALUE;
928 }
929
Dan Stoza3e96f192014-03-03 10:16:19 -0800930 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800931 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800932 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700933 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700934 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800935 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700936 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700937 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800938 } else if (fence == NULL) {
939 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700940 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800941 }
942
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700943 mSlots[slot].mBufferState.cancel();
944
945 // After leaving single buffer mode, the shared buffer will still be around.
946 // Mark it as no longer shared if this operation causes it to be free.
947 if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) {
948 mSlots[slot].mBufferState.mShared = false;
949 }
950
951 // Don't put the shared buffer on the free list.
952 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800953 mCore->mActiveBuffers.erase(slot);
954 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700955 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800956
Dan Stoza289ade12014-02-28 11:17:17 -0800957 mSlots[slot].mFence = fence;
958 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800959 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700960
961 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800962}
963
964int BufferQueueProducer::query(int what, int *outValue) {
965 ATRACE_CALL();
966 Mutex::Autolock lock(mCore->mMutex);
967
968 if (outValue == NULL) {
969 BQ_LOGE("query: outValue was NULL");
970 return BAD_VALUE;
971 }
972
973 if (mCore->mIsAbandoned) {
974 BQ_LOGE("query: BufferQueue has been abandoned");
975 return NO_INIT;
976 }
977
978 int value;
979 switch (what) {
980 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800981 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800982 break;
983 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800984 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800985 break;
986 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800987 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800988 break;
989 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700990 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800991 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700992 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800993 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700994 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800995 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
996 value = (mCore->mQueue.size() > 1);
997 break;
998 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800999 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001000 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001001 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1002 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1003 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001004 case NATIVE_WINDOW_BUFFER_AGE:
1005 if (mCore->mBufferAge > INT32_MAX) {
1006 value = 0;
1007 } else {
1008 value = static_cast<int32_t>(mCore->mBufferAge);
1009 }
1010 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001011 default:
1012 return BAD_VALUE;
1013 }
1014
1015 BQ_LOGV("query: %d? %d", what, value);
1016 *outValue = value;
1017 return NO_ERROR;
1018}
1019
Dan Stozaf0eaf252014-03-21 13:05:51 -07001020status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001021 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1022 ATRACE_CALL();
1023 Mutex::Autolock lock(mCore->mMutex);
1024 mConsumerName = mCore->mConsumerName;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001025 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
Dan Stoza289ade12014-02-28 11:17:17 -08001026 producerControlledByApp ? "true" : "false");
1027
Dan Stozaae3c3682014-04-18 15:43:35 -07001028 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001029 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stozaae3c3682014-04-18 15:43:35 -07001030 return NO_INIT;
1031 }
Dan Stoza289ade12014-02-28 11:17:17 -08001032
Dan Stozaae3c3682014-04-18 15:43:35 -07001033 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001034 BQ_LOGE("connect: BufferQueue has no consumer");
Dan Stozaae3c3682014-04-18 15:43:35 -07001035 return NO_INIT;
1036 }
Dan Stoza289ade12014-02-28 11:17:17 -08001037
Dan Stozaae3c3682014-04-18 15:43:35 -07001038 if (output == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001039 BQ_LOGE("connect: output was NULL");
Dan Stozaae3c3682014-04-18 15:43:35 -07001040 return BAD_VALUE;
1041 }
Dan Stoza289ade12014-02-28 11:17:17 -08001042
Dan Stozaae3c3682014-04-18 15:43:35 -07001043 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001044 BQ_LOGE("connect: already connected (cur=%d req=%d)",
Dan Stozaae3c3682014-04-18 15:43:35 -07001045 mCore->mConnectedApi, api);
1046 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001047 }
1048
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001049 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1050 mDequeueTimeout < 0 ?
1051 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1052 mCore->mMaxBufferCount) -
1053 mCore->getMaxBufferCountLocked();
1054 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1055 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1056 "slots. Delta = %d", delta);
1057 return BAD_VALUE;
1058 }
1059
Dan Stoza289ade12014-02-28 11:17:17 -08001060 int status = NO_ERROR;
1061 switch (api) {
1062 case NATIVE_WINDOW_API_EGL:
1063 case NATIVE_WINDOW_API_CPU:
1064 case NATIVE_WINDOW_API_MEDIA:
1065 case NATIVE_WINDOW_API_CAMERA:
1066 mCore->mConnectedApi = api;
1067 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001068 mCore->mTransformHint,
1069 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -08001070
1071 // Set up a death notification so that we can disconnect
1072 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -07001073 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -08001074 IInterface::asBinder(listener)->remoteBinder() != NULL) {
1075 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -08001076 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -07001077 if (status != NO_ERROR) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001078 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
Dan Stoza289ade12014-02-28 11:17:17 -08001079 strerror(-status), status);
1080 }
1081 }
Dan Stozaf0eaf252014-03-21 13:05:51 -07001082 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001083 break;
1084 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001085 BQ_LOGE("connect: unknown API %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001086 status = BAD_VALUE;
1087 break;
1088 }
1089
1090 mCore->mBufferHasBeenQueued = false;
Dan Stoza127fc632015-06-30 13:43:32 -07001091 mCore->mDequeueBufferCannotBlock = false;
1092 if (mDequeueTimeout < 0) {
1093 mCore->mDequeueBufferCannotBlock =
1094 mCore->mConsumerControlledByApp && producerControlledByApp;
1095 }
Dan Stoza289ade12014-02-28 11:17:17 -08001096
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001097 mCore->mAllowAllocation = true;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001098 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001099 return status;
1100}
1101
1102status_t BufferQueueProducer::disconnect(int api) {
1103 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001104 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001105
1106 int status = NO_ERROR;
1107 sp<IConsumerListener> listener;
1108 { // Autolock scope
1109 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001110 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001111
1112 if (mCore->mIsAbandoned) {
1113 // It's not really an error to disconnect after the surface has
1114 // been abandoned; it should just be a no-op.
1115 return NO_ERROR;
1116 }
1117
1118 switch (api) {
1119 case NATIVE_WINDOW_API_EGL:
1120 case NATIVE_WINDOW_API_CPU:
1121 case NATIVE_WINDOW_API_MEDIA:
1122 case NATIVE_WINDOW_API_CAMERA:
1123 if (mCore->mConnectedApi == api) {
1124 mCore->freeAllBuffersLocked();
1125
1126 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -07001127 if (mCore->mConnectedProducerListener != NULL) {
1128 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -08001129 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -08001130 // This can fail if we're here because of the death
1131 // notification, but we just ignore it
1132 token->unlinkToDeath(
1133 static_cast<IBinder::DeathRecipient*>(this));
1134 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001135 mCore->mSingleBufferSlot =
1136 BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001137 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001138 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -08001139 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001140 mCore->mDequeueCondition.broadcast();
1141 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001142 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001143 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001144 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001145 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001146 }
1147 break;
1148 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001149 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001150 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001151 break;
1152 }
1153 } // Autolock scope
1154
1155 // Call back without lock held
1156 if (listener != NULL) {
1157 listener->onBuffersReleased();
1158 }
1159
1160 return status;
1161}
1162
Jesse Hall399184a2014-03-03 15:42:54 -08001163status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001164 sp<IConsumerListener> listener;
1165 { // Autolock scope
1166 Mutex::Autolock _l(mCore->mMutex);
1167 mCore->mSidebandStream = stream;
1168 listener = mCore->mConsumerListener;
1169 } // Autolock scope
1170
1171 if (listener != NULL) {
1172 listener->onSidebandStreamChanged();
1173 }
Jesse Hall399184a2014-03-03 15:42:54 -08001174 return NO_ERROR;
1175}
1176
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001177void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1178 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001179 ATRACE_CALL();
1180 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001181 size_t newBufferCount = 0;
1182 uint32_t allocWidth = 0;
1183 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001184 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001185 uint32_t allocUsage = 0;
1186 { // Autolock scope
1187 Mutex::Autolock lock(mCore->mMutex);
1188 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001189
Dan Stoza9de72932015-04-16 17:28:43 -07001190 if (!mCore->mAllowAllocation) {
1191 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1192 "BufferQueue");
1193 return;
1194 }
1195
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001196 newBufferCount = mCore->mFreeSlots.size();
1197 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001198 return;
1199 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001200
Antoine Labour78014f32014-07-15 21:17:03 -07001201 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1202 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1203 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1204 allocUsage = usage | mCore->mConsumerUsageBits;
1205
1206 mCore->mIsAllocating = true;
1207 } // Autolock scope
1208
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001209 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001210 for (size_t i = 0; i < newBufferCount; ++i) {
1211 status_t result = NO_ERROR;
1212 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1213 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1214 if (result != NO_ERROR) {
1215 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1216 " %u, usage %u)", width, height, format, usage);
1217 Mutex::Autolock lock(mCore->mMutex);
1218 mCore->mIsAllocating = false;
1219 mCore->mIsAllocatingCondition.broadcast();
1220 return;
1221 }
1222 buffers.push_back(graphicBuffer);
1223 }
1224
1225 { // Autolock scope
1226 Mutex::Autolock lock(mCore->mMutex);
1227 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1228 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001229 PixelFormat checkFormat = format != 0 ?
1230 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001231 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1232 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1233 checkFormat != allocFormat || checkUsage != allocUsage) {
1234 // Something changed while we released the lock. Retry.
1235 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1236 mCore->mIsAllocating = false;
1237 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001238 continue;
1239 }
1240
Antoine Labour78014f32014-07-15 21:17:03 -07001241 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001242 if (mCore->mFreeSlots.empty()) {
1243 BQ_LOGV("allocateBuffers: a slot was occupied while "
1244 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001245 continue;
1246 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001247 auto slot = mCore->mFreeSlots.begin();
1248 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1249 mSlots[*slot].mGraphicBuffer = buffers[i];
1250 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001251
1252 // freeBufferLocked puts this slot on the free slots list. Since
1253 // we then attached a buffer, move the slot to free buffer list.
1254 mCore->mFreeSlots.erase(slot);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001255 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001256
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001257 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1258 *slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001259 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001260
Antoine Labour78014f32014-07-15 21:17:03 -07001261 mCore->mIsAllocating = false;
1262 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001263 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001264 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001265 }
1266}
1267
Dan Stoza9de72932015-04-16 17:28:43 -07001268status_t BufferQueueProducer::allowAllocation(bool allow) {
1269 ATRACE_CALL();
1270 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1271
1272 Mutex::Autolock lock(mCore->mMutex);
1273 mCore->mAllowAllocation = allow;
1274 return NO_ERROR;
1275}
1276
Dan Stoza812ed062015-06-02 15:45:22 -07001277status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1278 ATRACE_CALL();
1279 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1280
1281 Mutex::Autolock lock(mCore->mMutex);
1282 mCore->mGenerationNumber = generationNumber;
1283 return NO_ERROR;
1284}
1285
Dan Stozac6f30bd2015-06-08 09:32:50 -07001286String8 BufferQueueProducer::getConsumerName() const {
1287 ATRACE_CALL();
1288 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1289 return mConsumerName;
1290}
1291
Dan Stoza7dde5992015-05-22 09:51:44 -07001292uint64_t BufferQueueProducer::getNextFrameNumber() const {
1293 ATRACE_CALL();
1294
1295 Mutex::Autolock lock(mCore->mMutex);
1296 uint64_t nextFrameNumber = mCore->mFrameCounter + 1;
1297 return nextFrameNumber;
1298}
1299
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001300status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) {
1301 ATRACE_CALL();
1302 BQ_LOGV("setSingleBufferMode: %d", singleBufferMode);
1303
1304 Mutex::Autolock lock(mCore->mMutex);
1305 if (!singleBufferMode) {
1306 mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1307 }
1308 mCore->mSingleBufferMode = singleBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001309 return NO_ERROR;
1310}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001311
Dan Stoza127fc632015-06-30 13:43:32 -07001312status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1313 ATRACE_CALL();
1314 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1315
1316 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001317 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1318 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1319 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1320 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1321 "available slots. Delta = %d", delta);
1322 return BAD_VALUE;
1323 }
1324
Dan Stoza127fc632015-06-30 13:43:32 -07001325 mDequeueTimeout = timeout;
1326 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001327
1328 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001329 return NO_ERROR;
1330}
1331
Dan Stoza289ade12014-02-28 11:17:17 -08001332void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1333 // If we're here, it means that a producer we were connected to died.
1334 // We're guaranteed that we are still connected to it because we remove
1335 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1336 // without synchronization here.
1337 int api = mCore->mConnectedApi;
1338 disconnect(api);
1339}
1340
1341} // namespace android