blob: 61878f6ce3b017641c5c6efc6212952f142b45c5 [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
Dan Stoza289ade12014-02-28 11:17:17 -080023#define EGL_EGLEXT_PROTOTYPES
24
25#include <gui/BufferItem.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
28#include <gui/IConsumerListener.h>
29#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080031
32#include <utils/Log.h>
33#include <utils/Trace.h>
34
35namespace android {
36
37BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) :
38 mCore(core),
39 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070040 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070041 mStickyTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080042 mLastQueueBufferFence(Fence::NO_FENCE),
43 mCallbackMutex(),
44 mNextCallbackTicket(0),
45 mCurrentCallbackTicket(0),
46 mCallbackCondition() {}
Dan Stoza289ade12014-02-28 11:17:17 -080047
48BufferQueueProducer::~BufferQueueProducer() {}
49
50status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
51 ATRACE_CALL();
52 BQ_LOGV("requestBuffer: slot %d", slot);
53 Mutex::Autolock lock(mCore->mMutex);
54
55 if (mCore->mIsAbandoned) {
56 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
57 return NO_INIT;
58 }
59
Dan Stoza3e96f192014-03-03 10:16:19 -080060 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080061 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080062 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080063 return BAD_VALUE;
64 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
65 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
66 "(state = %d)", slot, mSlots[slot].mBufferState);
67 return BAD_VALUE;
68 }
69
70 mSlots[slot].mRequestBufferCalled = true;
71 *buf = mSlots[slot].mGraphicBuffer;
72 return NO_ERROR;
73}
74
Pablo Ceballosfa455352015-08-12 17:47:47 -070075status_t BufferQueueProducer::setMaxDequeuedBufferCount(
76 int maxDequeuedBuffers) {
77 ATRACE_CALL();
78 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
79 maxDequeuedBuffers);
80
81 sp<IConsumerListener> listener;
82 { // Autolock scope
83 Mutex::Autolock lock(mCore->mMutex);
84 mCore->waitWhileAllocatingLocked();
85
86 if (mCore->mIsAbandoned) {
87 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
88 "abandoned");
89 return NO_INIT;
90 }
91
92 // There must be no dequeued buffers when changing the buffer count.
93 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
94 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
95 BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer");
96 return BAD_VALUE;
97 }
98 }
99
100 int bufferCount = mCore->getMinUndequeuedBufferCountLocked(
101 mCore->mAsyncMode);
102 bufferCount += maxDequeuedBuffers;
103
104 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
105 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
106 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
107 return BAD_VALUE;
108 }
109
110 const int minBufferSlots = mCore->getMinMaxBufferCountLocked(
111 mCore->mAsyncMode);
112 if (bufferCount < minBufferSlots) {
113 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
114 "less than minimum %d", bufferCount, minBufferSlots);
115 return BAD_VALUE;
116 }
117
118 // Here we are guaranteed that the producer doesn't have any dequeued
119 // buffers and will release all of its buffer references. We don't
120 // clear the queue, however, so that currently queued buffers still
121 // get displayed.
122 mCore->freeAllBuffersLocked();
123 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700124 mCore->mOverrideMaxBufferCount = true;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700125 mCore->mDequeueCondition.broadcast();
126 listener = mCore->mConsumerListener;
127 } // Autolock scope
128
129 // Call back without lock held
130 if (listener != NULL) {
131 listener->onBuffersReleased();
132 }
133
134 return NO_ERROR;
135}
136
137status_t BufferQueueProducer::setAsyncMode(bool async) {
138 ATRACE_CALL();
139 BQ_LOGV("setAsyncMode: async = %d", async);
140
141 sp<IConsumerListener> listener;
142 { // Autolock scope
143 Mutex::Autolock lock(mCore->mMutex);
144 mCore->waitWhileAllocatingLocked();
145
146 if (mCore->mIsAbandoned) {
147 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
148 return NO_INIT;
149 }
150
151 // There must be no dequeued buffers when changing the async mode.
152 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
153 if (mSlots[s].mBufferState == BufferSlot::DEQUEUED) {
154 BQ_LOGE("setAsyncMode: buffer owned by producer");
155 return BAD_VALUE;
156 }
157 }
158
159 mCore->mAsyncMode = async;
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700160 mCore->mOverrideMaxBufferCount = true;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700161 mCore->mDequeueCondition.broadcast();
162 listener = mCore->mConsumerListener;
163 } // Autolock scope
164
165 // Call back without lock held
166 if (listener != NULL) {
167 listener->onBuffersReleased();
168 }
169 return NO_ERROR;
170}
171
Dan Stoza9f3053d2014-03-06 15:14:33 -0800172status_t BufferQueueProducer::waitForFreeSlotThenRelock(const char* caller,
173 bool async, int* found, status_t* returnFlags) const {
174 bool tryAgain = true;
175 while (tryAgain) {
176 if (mCore->mIsAbandoned) {
177 BQ_LOGE("%s: BufferQueue has been abandoned", caller);
178 return NO_INIT;
179 }
180
181 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800182
183 // Free up any buffers that are in slots beyond the max buffer count
184 for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
185 assert(mSlots[s].mBufferState == BufferSlot::FREE);
186 if (mSlots[s].mGraphicBuffer != NULL) {
187 mCore->freeBufferLocked(s);
188 *returnFlags |= RELEASE_ALL_BUFFERS;
189 }
190 }
191
Dan Stoza9f3053d2014-03-06 15:14:33 -0800192 int dequeuedCount = 0;
193 int acquiredCount = 0;
194 for (int s = 0; s < maxBufferCount; ++s) {
195 switch (mSlots[s].mBufferState) {
196 case BufferSlot::DEQUEUED:
197 ++dequeuedCount;
198 break;
199 case BufferSlot::ACQUIRED:
200 ++acquiredCount;
201 break;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800202 default:
203 break;
204 }
205 }
206
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700207 // Producers are not allowed to dequeue more than
208 // mMaxDequeuedBufferCount buffers.
209 // This check is only done if a buffer has already been queued
210 if (mCore->mBufferHasBeenQueued &&
211 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
212 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
213 "(%d)", caller, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800214 return INVALID_OPERATION;
215 }
216
Dan Stoza0de7ea72015-04-23 13:20:51 -0700217 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
218
Dan Stozaae3c3682014-04-18 15:43:35 -0700219 // If we disconnect and reconnect quickly, we can be in a state where
220 // our slots are empty but we have many buffers in the queue. This can
221 // cause us to run out of memory if we outrun the consumer. Wait here if
222 // it looks like we have too many buffers queued up.
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700223 bool tooManyBuffers = mCore->mQueue.size()
224 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700225 if (tooManyBuffers) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700226 BQ_LOGV("%s: queue size is %zu, waiting", caller,
Dan Stozaae3c3682014-04-18 15:43:35 -0700227 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700228 } else {
229 if (!mCore->mFreeBuffers.empty()) {
230 auto slot = mCore->mFreeBuffers.begin();
231 *found = *slot;
232 mCore->mFreeBuffers.erase(slot);
Dan Stoza9de72932015-04-16 17:28:43 -0700233 } else if (mCore->mAllowAllocation && !mCore->mFreeSlots.empty()) {
Dan Stoza0de7ea72015-04-23 13:20:51 -0700234 auto slot = mCore->mFreeSlots.begin();
235 // Only return free slots up to the max buffer count
236 if (*slot < maxBufferCount) {
237 *found = *slot;
238 mCore->mFreeSlots.erase(slot);
239 }
240 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700241 }
242
243 // If no buffer is found, or if the queue has too many buffers
244 // outstanding, wait for a buffer to be acquired or released, or for the
245 // max buffer count to change.
246 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
247 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800248 if (tryAgain) {
249 // Return an error if we're in non-blocking mode (producer and
250 // consumer are controlled by the application).
251 // However, the consumer is allowed to briefly acquire an extra
252 // buffer (which could cause us to have to wait here), which is
253 // okay, since it is only used to implement an atomic acquire +
254 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700255 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800256 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
257 return WOULD_BLOCK;
258 }
259 mCore->mDequeueCondition.wait(mCore->mMutex);
260 }
261 } // while (tryAgain)
262
263 return NO_ERROR;
264}
265
Dan Stoza289ade12014-02-28 11:17:17 -0800266status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
267 sp<android::Fence> *outFence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800268 uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) {
Dan Stoza289ade12014-02-28 11:17:17 -0800269 ATRACE_CALL();
270 { // Autolock scope
271 Mutex::Autolock lock(mCore->mMutex);
272 mConsumerName = mCore->mConsumerName;
273 } // Autolock scope
274
275 BQ_LOGV("dequeueBuffer: async=%s w=%u h=%u format=%#x, usage=%#x",
276 async ? "true" : "false", width, height, format, usage);
277
278 if ((width && !height) || (!width && height)) {
279 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
280 return BAD_VALUE;
281 }
282
283 status_t returnFlags = NO_ERROR;
284 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
285 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800286 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800287
288 { // Autolock scope
289 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700290 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800291
292 if (format == 0) {
293 format = mCore->mDefaultBufferFormat;
294 }
295
296 // Enable the usage bits the consumer requested
297 usage |= mCore->mConsumerUsageBits;
298
Dan Stoza9de72932015-04-16 17:28:43 -0700299 const bool useDefaultSize = !width && !height;
300 if (useDefaultSize) {
301 width = mCore->mDefaultWidth;
302 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800303 }
Dan Stoza289ade12014-02-28 11:17:17 -0800304
Dan Stoza9de72932015-04-16 17:28:43 -0700305 int found = BufferItem::INVALID_BUFFER_SLOT;
306 while (found == BufferItem::INVALID_BUFFER_SLOT) {
307 status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
308 &found, &returnFlags);
309 if (status != NO_ERROR) {
310 return status;
311 }
312
313 // This should not happen
314 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
315 BQ_LOGE("dequeueBuffer: no available buffer slots");
316 return -EBUSY;
317 }
318
319 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
320
321 // If we are not allowed to allocate new buffers,
322 // waitForFreeSlotThenRelock must have returned a slot containing a
323 // buffer. If this buffer would require reallocation to meet the
324 // requested attributes, we free it and attempt to get another one.
325 if (!mCore->mAllowAllocation) {
326 if (buffer->needsReallocation(width, height, format, usage)) {
327 mCore->freeBufferLocked(found);
328 found = BufferItem::INVALID_BUFFER_SLOT;
329 continue;
330 }
331 }
Dan Stoza289ade12014-02-28 11:17:17 -0800332 }
333
334 *outSlot = found;
335 ATRACE_BUFFER_INDEX(found);
336
Dan Stoza9f3053d2014-03-06 15:14:33 -0800337 attachedByConsumer = mSlots[found].mAttachedByConsumer;
338
Dan Stoza289ade12014-02-28 11:17:17 -0800339 mSlots[found].mBufferState = BufferSlot::DEQUEUED;
340
341 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
342 if ((buffer == NULL) ||
Dan Stoza9de72932015-04-16 17:28:43 -0700343 buffer->needsReallocation(width, height, format, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800344 {
345 mSlots[found].mAcquireCalled = false;
346 mSlots[found].mGraphicBuffer = NULL;
347 mSlots[found].mRequestBufferCalled = false;
348 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
349 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
350 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800351 mCore->mBufferAge = 0;
Dan Stoza289ade12014-02-28 11:17:17 -0800352
353 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800354 } else {
355 // We add 1 because that will be the frame number when this buffer
356 // is queued
357 mCore->mBufferAge =
358 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800359 }
360
Dan Stoza800b41a2015-04-28 14:20:04 -0700361 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
362 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800363
Dan Stoza289ade12014-02-28 11:17:17 -0800364 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
365 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
366 "slot=%d w=%d h=%d format=%u",
367 found, buffer->width, buffer->height, buffer->format);
368 }
369
370 eglDisplay = mSlots[found].mEglDisplay;
371 eglFence = mSlots[found].mEglFence;
372 *outFence = mSlots[found].mFence;
373 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
374 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700375
376 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800377 } // Autolock scope
378
379 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
380 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700381 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800382 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800383 width, height, format, usage, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800384 if (graphicBuffer == NULL) {
385 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
386 return error;
387 }
388
389 { // Autolock scope
390 Mutex::Autolock lock(mCore->mMutex);
391
392 if (mCore->mIsAbandoned) {
393 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
394 return NO_INIT;
395 }
396
Dan Stoza812ed062015-06-02 15:45:22 -0700397 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
Dan Stoza289ade12014-02-28 11:17:17 -0800398 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
399 } // Autolock scope
400 }
401
Dan Stoza9f3053d2014-03-06 15:14:33 -0800402 if (attachedByConsumer) {
403 returnFlags |= BUFFER_NEEDS_REALLOCATION;
404 }
405
Dan Stoza289ade12014-02-28 11:17:17 -0800406 if (eglFence != EGL_NO_SYNC_KHR) {
407 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
408 1000000000);
409 // If something goes wrong, log the error, but return the buffer without
410 // synchronizing access to it. It's too late at this point to abort the
411 // dequeue operation.
412 if (result == EGL_FALSE) {
413 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
414 eglGetError());
415 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
416 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
417 }
418 eglDestroySyncKHR(eglDisplay, eglFence);
419 }
420
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700421 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
422 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800423 mSlots[*outSlot].mFrameNumber,
424 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
425
426 return returnFlags;
427}
428
Dan Stoza9f3053d2014-03-06 15:14:33 -0800429status_t BufferQueueProducer::detachBuffer(int slot) {
430 ATRACE_CALL();
431 ATRACE_BUFFER_INDEX(slot);
432 BQ_LOGV("detachBuffer(P): slot %d", slot);
433 Mutex::Autolock lock(mCore->mMutex);
434
435 if (mCore->mIsAbandoned) {
436 BQ_LOGE("detachBuffer(P): BufferQueue has been abandoned");
437 return NO_INIT;
438 }
439
440 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
441 BQ_LOGE("detachBuffer(P): slot index %d out of range [0, %d)",
442 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
443 return BAD_VALUE;
444 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
445 BQ_LOGE("detachBuffer(P): slot %d is not owned by the producer "
446 "(state = %d)", slot, mSlots[slot].mBufferState);
447 return BAD_VALUE;
448 } else if (!mSlots[slot].mRequestBufferCalled) {
449 BQ_LOGE("detachBuffer(P): buffer in slot %d has not been requested",
450 slot);
451 return BAD_VALUE;
452 }
453
454 mCore->freeBufferLocked(slot);
455 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700456 mCore->validateConsistencyLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800457
458 return NO_ERROR;
459}
460
Dan Stozad9822a32014-03-28 15:25:31 -0700461status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
462 sp<Fence>* outFence) {
463 ATRACE_CALL();
464
465 if (outBuffer == NULL) {
466 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
467 return BAD_VALUE;
468 } else if (outFence == NULL) {
469 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
470 return BAD_VALUE;
471 }
472
473 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700474 mCore->waitWhileAllocatingLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700475
476 if (mCore->mIsAbandoned) {
477 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
478 return NO_INIT;
479 }
480
Dan Stoza0de7ea72015-04-23 13:20:51 -0700481 if (mCore->mFreeBuffers.empty()) {
Dan Stoza1fc9cc22015-04-22 18:57:39 +0000482 return NO_MEMORY;
483 }
Dan Stoza8dddc992015-04-16 15:39:18 -0700484
Dan Stoza0de7ea72015-04-23 13:20:51 -0700485 int found = mCore->mFreeBuffers.front();
486 mCore->mFreeBuffers.remove(found);
487
Dan Stozad9822a32014-03-28 15:25:31 -0700488 BQ_LOGV("detachNextBuffer detached slot %d", found);
489
490 *outBuffer = mSlots[found].mGraphicBuffer;
491 *outFence = mSlots[found].mFence;
492 mCore->freeBufferLocked(found);
Dan Stoza0de7ea72015-04-23 13:20:51 -0700493 mCore->validateConsistencyLocked();
Dan Stozad9822a32014-03-28 15:25:31 -0700494
495 return NO_ERROR;
496}
497
Dan Stoza9f3053d2014-03-06 15:14:33 -0800498status_t BufferQueueProducer::attachBuffer(int* outSlot,
499 const sp<android::GraphicBuffer>& buffer) {
500 ATRACE_CALL();
501
502 if (outSlot == NULL) {
503 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
504 return BAD_VALUE;
505 } else if (buffer == NULL) {
506 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
507 return BAD_VALUE;
508 }
509
510 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700511 mCore->waitWhileAllocatingLocked();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800512
Dan Stoza812ed062015-06-02 15:45:22 -0700513 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
514 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
515 "[queue %u]", buffer->getGenerationNumber(),
516 mCore->mGenerationNumber);
517 return BAD_VALUE;
518 }
519
Dan Stoza9f3053d2014-03-06 15:14:33 -0800520 status_t returnFlags = NO_ERROR;
521 int found;
522 // TODO: Should we provide an async flag to attachBuffer? It seems
523 // unlikely that buffers which we are attaching to a BufferQueue will
524 // be asynchronous (droppable), but it may not be impossible.
525 status_t status = waitForFreeSlotThenRelock("attachBuffer(P)", false,
526 &found, &returnFlags);
527 if (status != NO_ERROR) {
528 return status;
529 }
530
531 // This should not happen
532 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
533 BQ_LOGE("attachBuffer(P): no available buffer slots");
534 return -EBUSY;
535 }
536
537 *outSlot = found;
538 ATRACE_BUFFER_INDEX(*outSlot);
539 BQ_LOGV("attachBuffer(P): returning slot %d flags=%#x",
540 *outSlot, returnFlags);
541
542 mSlots[*outSlot].mGraphicBuffer = buffer;
543 mSlots[*outSlot].mBufferState = BufferSlot::DEQUEUED;
544 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
545 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700546 mSlots[*outSlot].mRequestBufferCalled = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800547
Dan Stoza0de7ea72015-04-23 13:20:51 -0700548 mCore->validateConsistencyLocked();
549
Dan Stoza9f3053d2014-03-06 15:14:33 -0800550 return returnFlags;
551}
552
Dan Stoza289ade12014-02-28 11:17:17 -0800553status_t BufferQueueProducer::queueBuffer(int slot,
554 const QueueBufferInput &input, QueueBufferOutput *output) {
555 ATRACE_CALL();
556 ATRACE_BUFFER_INDEX(slot);
557
558 int64_t timestamp;
559 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800560 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700561 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800562 int scalingMode;
563 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700564 uint32_t stickyTransform;
Dan Stoza289ade12014-02-28 11:17:17 -0800565 bool async;
566 sp<Fence> fence;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800567 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
568 &transform, &async, &fence, &stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700569 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800570
571 if (fence == NULL) {
572 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800573 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800574 }
575
576 switch (scalingMode) {
577 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
578 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
579 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
580 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
581 break;
582 default:
583 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800584 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800585 }
586
Dan Stoza8dc55392014-11-04 11:37:46 -0800587 sp<IConsumerListener> frameAvailableListener;
588 sp<IConsumerListener> frameReplacedListener;
589 int callbackTicket = 0;
590 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800591 { // Autolock scope
592 Mutex::Autolock lock(mCore->mMutex);
593
594 if (mCore->mIsAbandoned) {
595 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
596 return NO_INIT;
597 }
598
599 const int maxBufferCount = mCore->getMaxBufferCountLocked(async);
Dan Stoza289ade12014-02-28 11:17:17 -0800600
601 if (slot < 0 || slot >= maxBufferCount) {
602 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
603 slot, maxBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800604 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800605 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
606 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
607 "(state = %d)", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800608 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800609 } else if (!mSlots[slot].mRequestBufferCalled) {
610 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
611 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800612 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800613 }
614
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800615 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700616 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800617 slot, mCore->mFrameCounter + 1, timestamp, dataSpace,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800618 crop.left, crop.top, crop.right, crop.bottom, transform,
619 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800620
621 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
622 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700623 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800624 crop.intersect(bufferRect, &croppedRect);
625 if (croppedRect != crop) {
626 BQ_LOGE("queueBuffer: crop rect is not contained within the "
627 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800628 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800629 }
630
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800631 // Override UNKNOWN dataspace with consumer default
632 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
633 dataSpace = mCore->mDefaultBufferDataSpace;
634 }
635
Dan Stoza289ade12014-02-28 11:17:17 -0800636 mSlots[slot].mFence = fence;
637 mSlots[slot].mBufferState = BufferSlot::QUEUED;
638 ++mCore->mFrameCounter;
639 mSlots[slot].mFrameNumber = mCore->mFrameCounter;
640
Dan Stoza289ade12014-02-28 11:17:17 -0800641 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
642 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
643 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800644 item.mTransform = transform &
645 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800646 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800647 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
648 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Dan Stoza289ade12014-02-28 11:17:17 -0800649 item.mTimestamp = timestamp;
650 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800651 item.mDataSpace = dataSpace;
Dan Stoza289ade12014-02-28 11:17:17 -0800652 item.mFrameNumber = mCore->mFrameCounter;
653 item.mSlot = slot;
654 item.mFence = fence;
655 item.mIsDroppable = mCore->mDequeueBufferCannotBlock || async;
Dan Stoza5065a552015-03-17 16:23:42 -0700656 item.mSurfaceDamage = surfaceDamage;
Dan Stoza289ade12014-02-28 11:17:17 -0800657
Ruben Brunk1681d952014-06-27 15:51:55 -0700658 mStickyTransform = stickyTransform;
659
Dan Stoza289ade12014-02-28 11:17:17 -0800660 if (mCore->mQueue.empty()) {
661 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
662 // and simply queue this buffer
663 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800664 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800665 } else {
666 // When the queue is not empty, we need to look at the front buffer
667 // state to see if we need to replace it
668 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
669 if (front->mIsDroppable) {
670 // If the front queued buffer is still being tracked, we first
671 // mark it as freed
672 if (mCore->stillTracking(front)) {
673 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700674 mCore->mFreeBuffers.push_front(front->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800675 }
676 // Overwrite the droppable buffer with the incoming one
677 *front = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800678 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800679 } else {
680 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800681 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800682 }
683 }
684
685 mCore->mBufferHasBeenQueued = true;
686 mCore->mDequeueCondition.broadcast();
687
688 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800689 mCore->mTransformHint,
690 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800691
692 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800693
694 // Take a ticket for the callback functions
695 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700696
697 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800698 } // Autolock scope
699
Eric Penner99a0afb2014-09-30 11:28:30 -0700700 // Wait without lock held
701 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
702 // Waiting here allows for two full buffers to be queued but not a
703 // third. In the event that frames take varying time, this makes a
704 // small trade-off in favor of latency rather than throughput.
705 mLastQueueBufferFence->waitForever("Throttling EGL Production");
706 mLastQueueBufferFence = fence;
707 }
708
Dan Stoza8dc55392014-11-04 11:37:46 -0800709 // Don't send the GraphicBuffer through the callback, and don't send
710 // the slot number, since the consumer shouldn't need it
711 item.mGraphicBuffer.clear();
712 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
713
714 // Call back without the main BufferQueue lock held, but with the callback
715 // lock held so we can ensure that callbacks occur in order
716 {
717 Mutex::Autolock lock(mCallbackMutex);
718 while (callbackTicket != mCurrentCallbackTicket) {
719 mCallbackCondition.wait(mCallbackMutex);
720 }
721
722 if (frameAvailableListener != NULL) {
723 frameAvailableListener->onFrameAvailable(item);
724 } else if (frameReplacedListener != NULL) {
725 frameReplacedListener->onFrameReplaced(item);
726 }
727
728 ++mCurrentCallbackTicket;
729 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800730 }
731
732 return NO_ERROR;
733}
734
735void BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
736 ATRACE_CALL();
737 BQ_LOGV("cancelBuffer: slot %d", slot);
738 Mutex::Autolock lock(mCore->mMutex);
739
740 if (mCore->mIsAbandoned) {
741 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
742 return;
743 }
744
Dan Stoza3e96f192014-03-03 10:16:19 -0800745 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800746 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800747 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800748 return;
749 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
750 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
751 "(state = %d)", slot, mSlots[slot].mBufferState);
752 return;
753 } else if (fence == NULL) {
754 BQ_LOGE("cancelBuffer: fence is NULL");
755 return;
756 }
757
Dan Stoza0de7ea72015-04-23 13:20:51 -0700758 mCore->mFreeBuffers.push_front(slot);
Dan Stoza289ade12014-02-28 11:17:17 -0800759 mSlots[slot].mBufferState = BufferSlot::FREE;
Dan Stoza289ade12014-02-28 11:17:17 -0800760 mSlots[slot].mFence = fence;
761 mCore->mDequeueCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700762 mCore->validateConsistencyLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800763}
764
765int BufferQueueProducer::query(int what, int *outValue) {
766 ATRACE_CALL();
767 Mutex::Autolock lock(mCore->mMutex);
768
769 if (outValue == NULL) {
770 BQ_LOGE("query: outValue was NULL");
771 return BAD_VALUE;
772 }
773
774 if (mCore->mIsAbandoned) {
775 BQ_LOGE("query: BufferQueue has been abandoned");
776 return NO_INIT;
777 }
778
779 int value;
780 switch (what) {
781 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800782 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -0800783 break;
784 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800785 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -0800786 break;
787 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800788 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -0800789 break;
790 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
791 value = mCore->getMinUndequeuedBufferCountLocked(false);
792 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700793 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800794 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700795 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800796 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
797 value = (mCore->mQueue.size() > 1);
798 break;
799 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800800 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -0800801 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800802 case NATIVE_WINDOW_DEFAULT_DATASPACE:
803 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
804 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800805 case NATIVE_WINDOW_BUFFER_AGE:
806 if (mCore->mBufferAge > INT32_MAX) {
807 value = 0;
808 } else {
809 value = static_cast<int32_t>(mCore->mBufferAge);
810 }
811 break;
Dan Stoza289ade12014-02-28 11:17:17 -0800812 default:
813 return BAD_VALUE;
814 }
815
816 BQ_LOGV("query: %d? %d", what, value);
817 *outValue = value;
818 return NO_ERROR;
819}
820
Dan Stozaf0eaf252014-03-21 13:05:51 -0700821status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800822 int api, bool producerControlledByApp, QueueBufferOutput *output) {
823 ATRACE_CALL();
824 Mutex::Autolock lock(mCore->mMutex);
825 mConsumerName = mCore->mConsumerName;
826 BQ_LOGV("connect(P): api=%d producerControlledByApp=%s", api,
827 producerControlledByApp ? "true" : "false");
828
Dan Stozaae3c3682014-04-18 15:43:35 -0700829 if (mCore->mIsAbandoned) {
830 BQ_LOGE("connect(P): BufferQueue has been abandoned");
831 return NO_INIT;
832 }
Dan Stoza289ade12014-02-28 11:17:17 -0800833
Dan Stozaae3c3682014-04-18 15:43:35 -0700834 if (mCore->mConsumerListener == NULL) {
835 BQ_LOGE("connect(P): BufferQueue has no consumer");
836 return NO_INIT;
837 }
Dan Stoza289ade12014-02-28 11:17:17 -0800838
Dan Stozaae3c3682014-04-18 15:43:35 -0700839 if (output == NULL) {
840 BQ_LOGE("connect(P): output was NULL");
841 return BAD_VALUE;
842 }
Dan Stoza289ade12014-02-28 11:17:17 -0800843
Dan Stozaae3c3682014-04-18 15:43:35 -0700844 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
845 BQ_LOGE("connect(P): already connected (cur=%d req=%d)",
846 mCore->mConnectedApi, api);
847 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800848 }
849
850 int status = NO_ERROR;
851 switch (api) {
852 case NATIVE_WINDOW_API_EGL:
853 case NATIVE_WINDOW_API_CPU:
854 case NATIVE_WINDOW_API_MEDIA:
855 case NATIVE_WINDOW_API_CAMERA:
856 mCore->mConnectedApi = api;
857 output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 mCore->mTransformHint,
859 static_cast<uint32_t>(mCore->mQueue.size()));
Dan Stoza289ade12014-02-28 11:17:17 -0800860
861 // Set up a death notification so that we can disconnect
862 // automatically if the remote producer dies
Dan Stozaf0eaf252014-03-21 13:05:51 -0700863 if (listener != NULL &&
Marco Nelissen097ca272014-11-14 08:01:01 -0800864 IInterface::asBinder(listener)->remoteBinder() != NULL) {
865 status = IInterface::asBinder(listener)->linkToDeath(
Dan Stoza289ade12014-02-28 11:17:17 -0800866 static_cast<IBinder::DeathRecipient*>(this));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700867 if (status != NO_ERROR) {
Dan Stoza289ade12014-02-28 11:17:17 -0800868 BQ_LOGE("connect(P): linkToDeath failed: %s (%d)",
869 strerror(-status), status);
870 }
871 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700872 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -0800873 break;
874 default:
875 BQ_LOGE("connect(P): unknown API %d", api);
876 status = BAD_VALUE;
877 break;
878 }
879
880 mCore->mBufferHasBeenQueued = false;
881 mCore->mDequeueBufferCannotBlock =
882 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza2b83cc92015-05-12 14:55:15 -0700883 mCore->mAllowAllocation = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800884
885 return status;
886}
887
888status_t BufferQueueProducer::disconnect(int api) {
889 ATRACE_CALL();
890 BQ_LOGV("disconnect(P): api %d", api);
891
892 int status = NO_ERROR;
893 sp<IConsumerListener> listener;
894 { // Autolock scope
895 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700896 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800897
898 if (mCore->mIsAbandoned) {
899 // It's not really an error to disconnect after the surface has
900 // been abandoned; it should just be a no-op.
901 return NO_ERROR;
902 }
903
904 switch (api) {
905 case NATIVE_WINDOW_API_EGL:
906 case NATIVE_WINDOW_API_CPU:
907 case NATIVE_WINDOW_API_MEDIA:
908 case NATIVE_WINDOW_API_CAMERA:
909 if (mCore->mConnectedApi == api) {
910 mCore->freeAllBuffersLocked();
911
912 // Remove our death notification callback if we have one
Dan Stozaf0eaf252014-03-21 13:05:51 -0700913 if (mCore->mConnectedProducerListener != NULL) {
914 sp<IBinder> token =
Marco Nelissen097ca272014-11-14 08:01:01 -0800915 IInterface::asBinder(mCore->mConnectedProducerListener);
Dan Stoza289ade12014-02-28 11:17:17 -0800916 // This can fail if we're here because of the death
917 // notification, but we just ignore it
918 token->unlinkToDeath(
919 static_cast<IBinder::DeathRecipient*>(this));
920 }
Dan Stozaf0eaf252014-03-21 13:05:51 -0700921 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -0800922 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Jesse Hall399184a2014-03-03 15:42:54 -0800923 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -0800924 mCore->mDequeueCondition.broadcast();
925 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700926 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
927 BQ_LOGE("disconnect(P): still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -0800928 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800929 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 }
931 break;
932 default:
933 BQ_LOGE("disconnect(P): unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800934 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800935 break;
936 }
937 } // Autolock scope
938
939 // Call back without lock held
940 if (listener != NULL) {
941 listener->onBuffersReleased();
942 }
943
944 return status;
945}
946
Jesse Hall399184a2014-03-03 15:42:54 -0800947status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +0900948 sp<IConsumerListener> listener;
949 { // Autolock scope
950 Mutex::Autolock _l(mCore->mMutex);
951 mCore->mSidebandStream = stream;
952 listener = mCore->mConsumerListener;
953 } // Autolock scope
954
955 if (listener != NULL) {
956 listener->onSidebandStreamChanged();
957 }
Jesse Hall399184a2014-03-03 15:42:54 -0800958 return NO_ERROR;
959}
960
Dan Stoza29a3e902014-06-20 13:13:57 -0700961void BufferQueueProducer::allocateBuffers(bool async, uint32_t width,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800962 uint32_t height, PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -0700963 ATRACE_CALL();
964 while (true) {
965 Vector<int> freeSlots;
966 size_t newBufferCount = 0;
967 uint32_t allocWidth = 0;
968 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800969 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -0700970 uint32_t allocUsage = 0;
971 { // Autolock scope
972 Mutex::Autolock lock(mCore->mMutex);
973 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -0700974
Dan Stoza9de72932015-04-16 17:28:43 -0700975 if (!mCore->mAllowAllocation) {
976 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
977 "BufferQueue");
978 return;
979 }
980
Antoine Labour78014f32014-07-15 21:17:03 -0700981 int currentBufferCount = 0;
982 for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
983 if (mSlots[slot].mGraphicBuffer != NULL) {
984 ++currentBufferCount;
985 } else {
986 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
987 BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE",
988 slot);
989 continue;
990 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700991
Antoine Labour11f14872014-07-25 18:14:42 -0700992 freeSlots.push_back(slot);
Antoine Labour78014f32014-07-15 21:17:03 -0700993 }
994 }
995
996 int maxBufferCount = mCore->getMaxBufferCountLocked(async);
997 BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers",
998 currentBufferCount, maxBufferCount);
999 if (maxBufferCount <= currentBufferCount)
1000 return;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 newBufferCount =
1002 static_cast<size_t>(maxBufferCount - currentBufferCount);
Antoine Labour78014f32014-07-15 21:17:03 -07001003 if (freeSlots.size() < newBufferCount) {
1004 BQ_LOGE("allocateBuffers: ran out of free slots");
1005 return;
1006 }
1007 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1008 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1009 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1010 allocUsage = usage | mCore->mConsumerUsageBits;
1011
1012 mCore->mIsAllocating = true;
1013 } // Autolock scope
1014
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001015 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001016 for (size_t i = 0; i < newBufferCount; ++i) {
1017 status_t result = NO_ERROR;
1018 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
1019 allocWidth, allocHeight, allocFormat, allocUsage, &result));
1020 if (result != NO_ERROR) {
1021 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1022 " %u, usage %u)", width, height, format, usage);
1023 Mutex::Autolock lock(mCore->mMutex);
1024 mCore->mIsAllocating = false;
1025 mCore->mIsAllocatingCondition.broadcast();
1026 return;
1027 }
1028 buffers.push_back(graphicBuffer);
1029 }
1030
1031 { // Autolock scope
1032 Mutex::Autolock lock(mCore->mMutex);
1033 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1034 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001035 PixelFormat checkFormat = format != 0 ?
1036 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001037 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1038 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1039 checkFormat != allocFormat || checkUsage != allocUsage) {
1040 // Something changed while we released the lock. Retry.
1041 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1042 mCore->mIsAllocating = false;
1043 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001044 continue;
1045 }
1046
Antoine Labour78014f32014-07-15 21:17:03 -07001047 for (size_t i = 0; i < newBufferCount; ++i) {
1048 int slot = freeSlots[i];
1049 if (mSlots[slot].mBufferState != BufferSlot::FREE) {
1050 // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we
1051 // allocated.
1052 BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. "
1053 "Dropping allocated buffer.", slot);
1054 continue;
1055 }
1056 mCore->freeBufferLocked(slot); // Clean up the slot first
1057 mSlots[slot].mGraphicBuffer = buffers[i];
Antoine Labour78014f32014-07-15 21:17:03 -07001058 mSlots[slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001059
1060 // freeBufferLocked puts this slot on the free slots list. Since
1061 // we then attached a buffer, move the slot to free buffer list.
1062 mCore->mFreeSlots.erase(slot);
1063 mCore->mFreeBuffers.push_front(slot);
1064
Antoine Labour78014f32014-07-15 21:17:03 -07001065 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot);
1066 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001067
Antoine Labour78014f32014-07-15 21:17:03 -07001068 mCore->mIsAllocating = false;
1069 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza0de7ea72015-04-23 13:20:51 -07001070 mCore->validateConsistencyLocked();
Antoine Labour78014f32014-07-15 21:17:03 -07001071 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001072 }
1073}
1074
Dan Stoza9de72932015-04-16 17:28:43 -07001075status_t BufferQueueProducer::allowAllocation(bool allow) {
1076 ATRACE_CALL();
1077 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1078
1079 Mutex::Autolock lock(mCore->mMutex);
1080 mCore->mAllowAllocation = allow;
1081 return NO_ERROR;
1082}
1083
Dan Stoza812ed062015-06-02 15:45:22 -07001084status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1085 ATRACE_CALL();
1086 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1087
1088 Mutex::Autolock lock(mCore->mMutex);
1089 mCore->mGenerationNumber = generationNumber;
1090 return NO_ERROR;
1091}
1092
Dan Stozac6f30bd2015-06-08 09:32:50 -07001093String8 BufferQueueProducer::getConsumerName() const {
1094 ATRACE_CALL();
1095 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1096 return mConsumerName;
1097}
1098
Dan Stoza289ade12014-02-28 11:17:17 -08001099void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1100 // If we're here, it means that a producer we were connected to died.
1101 // We're guaranteed that we are still connected to it because we remove
1102 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1103 // without synchronization here.
1104 int api = mCore->mConnectedApi;
1105 disconnect(api);
1106}
1107
1108} // namespace android