blob: c96a2dd6a3631dffebdea527f1e650591d2e4026 [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
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038
39#include <utils/Log.h>
40#include <utils/Trace.h>
41
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070042#include <system/window.h>
43
Dan Stoza289ade12014-02-28 11:17:17 -080044namespace android {
45
Craig Donner6ebc46a2016-10-21 15:23:44 -070046static constexpr uint32_t BQ_LAYER_COUNT = 1;
47
Irvel468051e2016-06-13 16:44:44 -070048BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
49 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080050 mCore(core),
51 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070052 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070053 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070054 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080055 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070056 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080057 mCallbackMutex(),
58 mNextCallbackTicket(0),
59 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070060 mCallbackCondition(),
61 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080062
63BufferQueueProducer::~BufferQueueProducer() {}
64
65status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
66 ATRACE_CALL();
67 BQ_LOGV("requestBuffer: slot %d", slot);
68 Mutex::Autolock lock(mCore->mMutex);
69
70 if (mCore->mIsAbandoned) {
71 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
72 return NO_INIT;
73 }
74
Pablo Ceballos583b1b32015-09-03 18:23:52 -070075 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
76 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
77 return NO_INIT;
78 }
79
Dan Stoza3e96f192014-03-03 10:16:19 -080080 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080081 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080082 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080083 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070084 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080085 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070086 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080087 return BAD_VALUE;
88 }
89
90 mSlots[slot].mRequestBufferCalled = true;
91 *buf = mSlots[slot].mGraphicBuffer;
92 return NO_ERROR;
93}
94
Pablo Ceballosfa455352015-08-12 17:47:47 -070095status_t BufferQueueProducer::setMaxDequeuedBufferCount(
96 int maxDequeuedBuffers) {
97 ATRACE_CALL();
98 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
99 maxDequeuedBuffers);
100
Pablo Ceballos981066c2016-02-18 12:54:37 -0800101 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700102 { // Autolock scope
103 Mutex::Autolock lock(mCore->mMutex);
104 mCore->waitWhileAllocatingLocked();
105
106 if (mCore->mIsAbandoned) {
107 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
108 "abandoned");
109 return NO_INIT;
110 }
111
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700112 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
113 return NO_ERROR;
114 }
115
Pablo Ceballos72daab62015-12-07 16:38:43 -0800116 // The new maxDequeuedBuffer count should not be violated by the number
117 // of currently dequeued buffers
118 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800119 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700120 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800121 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700122 }
123 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800124 if (dequeuedCount > maxDequeuedBuffers) {
125 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
126 "count (%d) exceeds the current dequeued buffer count (%d)",
127 maxDequeuedBuffers, dequeuedCount);
128 return BAD_VALUE;
129 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700131 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132 bufferCount += maxDequeuedBuffers;
133
134 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
135 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
136 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
137 return BAD_VALUE;
138 }
139
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700140 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700141 if (bufferCount < minBufferSlots) {
142 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
143 "less than minimum %d", bufferCount, minBufferSlots);
144 return BAD_VALUE;
145 }
146
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700147 if (bufferCount > mCore->mMaxBufferCount) {
148 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700149 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
150 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
151 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
152 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700153 return BAD_VALUE;
154 }
155
Pablo Ceballos72daab62015-12-07 16:38:43 -0800156 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800157 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800158 return BAD_VALUE;
159 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700160 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800161 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800162 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800163 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800164 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 } // Autolock scope
167
168 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800169 if (listener != NULL) {
170 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700171 }
172
173 return NO_ERROR;
174}
175
176status_t BufferQueueProducer::setAsyncMode(bool async) {
177 ATRACE_CALL();
178 BQ_LOGV("setAsyncMode: async = %d", async);
179
Pablo Ceballos981066c2016-02-18 12:54:37 -0800180 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700181 { // Autolock scope
182 Mutex::Autolock lock(mCore->mMutex);
183 mCore->waitWhileAllocatingLocked();
184
185 if (mCore->mIsAbandoned) {
186 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
187 return NO_INIT;
188 }
189
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700190 if (async == mCore->mAsyncMode) {
191 return NO_ERROR;
192 }
193
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700194 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700195 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
196 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700197 BQ_LOGE("setAsyncMode(%d): this call would cause the "
198 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700199 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
200 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
201 mCore->mMaxDequeuedBufferCount,
202 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700203 return BAD_VALUE;
204 }
205
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800206 int delta = mCore->getMaxBufferCountLocked(async,
207 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
208 - mCore->getMaxBufferCountLocked();
209
Pablo Ceballos981066c2016-02-18 12:54:37 -0800210 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800211 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
212 "available slots. Delta = %d", delta);
213 return BAD_VALUE;
214 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800216 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700217 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700218 if (delta < 0) {
219 listener = mCore->mConsumerListener;
220 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700221 } // Autolock scope
222
223 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800224 if (listener != NULL) {
225 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700226 }
227 return NO_ERROR;
228}
229
Dan Stoza5ecfb682016-01-04 17:01:02 -0800230int BufferQueueProducer::getFreeBufferLocked() const {
231 if (mCore->mFreeBuffers.empty()) {
232 return BufferQueueCore::INVALID_BUFFER_SLOT;
233 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800234 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800235 mCore->mFreeBuffers.pop_front();
236 return slot;
237}
238
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800239int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800240 if (mCore->mFreeSlots.empty()) {
241 return BufferQueueCore::INVALID_BUFFER_SLOT;
242 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800243 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800244 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800245 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800246}
247
248status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800249 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800250 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
251 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 bool tryAgain = true;
253 while (tryAgain) {
254 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800255 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800256 return NO_INIT;
257 }
258
Dan Stoza9f3053d2014-03-06 15:14:33 -0800259 int dequeuedCount = 0;
260 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800261 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700262 if (mSlots[s].mBufferState.isDequeued()) {
263 ++dequeuedCount;
264 }
265 if (mSlots[s].mBufferState.isAcquired()) {
266 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800267 }
268 }
269
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700270 // Producers are not allowed to dequeue more than
271 // mMaxDequeuedBufferCount buffers.
272 // This check is only done if a buffer has already been queued
273 if (mCore->mBufferHasBeenQueued &&
274 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
275 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800276 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 return INVALID_OPERATION;
278 }
279
Dan Stoza0de7ea72015-04-23 13:20:51 -0700280 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
281
Dan Stozaae3c3682014-04-18 15:43:35 -0700282 // If we disconnect and reconnect quickly, we can be in a state where
283 // our slots are empty but we have many buffers in the queue. This can
284 // cause us to run out of memory if we outrun the consumer. Wait here if
285 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800286 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700287 bool tooManyBuffers = mCore->mQueue.size()
288 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700289 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700292 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700293 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700294 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800298 } else {
299 if (caller == FreeSlotCaller::Dequeue) {
300 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800302 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
303 *found = slot;
304 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800305 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800306 }
307 } else {
308 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800309 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800310 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
311 *found = slot;
312 } else {
313 *found = getFreeBufferLocked();
314 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700315 }
316 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700317 }
318
319 // If no buffer is found, or if the queue has too many buffers
320 // outstanding, wait for a buffer to be acquired or released, or for the
321 // max buffer count to change.
322 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
323 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 if (tryAgain) {
325 // Return an error if we're in non-blocking mode (producer and
326 // consumer are controlled by the application).
327 // However, the consumer is allowed to briefly acquire an extra
328 // buffer (which could cause us to have to wait here), which is
329 // okay, since it is only used to implement an atomic acquire +
330 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700331 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800332 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
333 return WOULD_BLOCK;
334 }
Dan Stoza127fc632015-06-30 13:43:32 -0700335 if (mDequeueTimeout >= 0) {
336 status_t result = mCore->mDequeueCondition.waitRelative(
337 mCore->mMutex, mDequeueTimeout);
338 if (result == TIMED_OUT) {
339 return result;
340 }
341 } else {
342 mCore->mDequeueCondition.wait(mCore->mMutex);
343 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800344 }
345 } // while (tryAgain)
346
347 return NO_ERROR;
348}
349
Ian Elliottd11b0442017-07-18 11:05:49 -0600350status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
351 uint32_t width, uint32_t height, PixelFormat format,
352 uint64_t usage, uint64_t* outBufferAge,
353 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800354 ATRACE_CALL();
355 { // Autolock scope
356 Mutex::Autolock lock(mCore->mMutex);
357 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700358
359 if (mCore->mIsAbandoned) {
360 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
361 return NO_INIT;
362 }
363
364 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
365 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
366 return NO_INIT;
367 }
Dan Stoza289ade12014-02-28 11:17:17 -0800368 } // Autolock scope
369
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700370 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800371
372 if ((width && !height) || (!width && height)) {
373 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
374 return BAD_VALUE;
375 }
376
377 status_t returnFlags = NO_ERROR;
378 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
379 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800381
382 { // Autolock scope
383 Mutex::Autolock lock(mCore->mMutex);
384
385 if (format == 0) {
386 format = mCore->mDefaultBufferFormat;
387 }
388
389 // Enable the usage bits the consumer requested
390 usage |= mCore->mConsumerUsageBits;
391
Dan Stoza9de72932015-04-16 17:28:43 -0700392 const bool useDefaultSize = !width && !height;
393 if (useDefaultSize) {
394 width = mCore->mDefaultWidth;
395 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800396 }
Dan Stoza289ade12014-02-28 11:17:17 -0800397
Pablo Ceballos981066c2016-02-18 12:54:37 -0800398 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700399 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800400 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800401 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700402 if (status != NO_ERROR) {
403 return status;
404 }
405
406 // This should not happen
407 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
408 BQ_LOGE("dequeueBuffer: no available buffer slots");
409 return -EBUSY;
410 }
411
412 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
413
414 // If we are not allowed to allocate new buffers,
415 // waitForFreeSlotThenRelock must have returned a slot containing a
416 // buffer. If this buffer would require reallocation to meet the
417 // requested attributes, we free it and attempt to get another one.
418 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700419 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700420 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700421 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700422 return BAD_VALUE;
423 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800424 mCore->mFreeSlots.insert(found);
425 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700426 found = BufferItem::INVALID_BUFFER_SLOT;
427 continue;
428 }
429 }
Dan Stoza289ade12014-02-28 11:17:17 -0800430 }
431
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800432 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700433 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700434 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800435 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
436 "buffer");
437
438 return BAD_VALUE;
439 }
440
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700441 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800442 mCore->mActiveBuffers.insert(found);
443 }
Dan Stoza289ade12014-02-28 11:17:17 -0800444 *outSlot = found;
445 ATRACE_BUFFER_INDEX(found);
446
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800447 attachedByConsumer = mSlots[found].mNeedsReallocation;
448 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800449
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700450 mSlots[found].mBufferState.dequeue();
451
Dan Stoza289ade12014-02-28 11:17:17 -0800452 if ((buffer == NULL) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700453 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800454 {
455 mSlots[found].mAcquireCalled = false;
456 mSlots[found].mGraphicBuffer = NULL;
457 mSlots[found].mRequestBufferCalled = false;
458 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
459 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
460 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800461 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700462 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800463
464 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800465 } else {
466 // We add 1 because that will be the frame number when this buffer
467 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700468 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800469 }
470
Dan Stoza800b41a2015-04-28 14:20:04 -0700471 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
472 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800473
Dan Stoza289ade12014-02-28 11:17:17 -0800474 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
475 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
476 "slot=%d w=%d h=%d format=%u",
477 found, buffer->width, buffer->height, buffer->format);
478 }
479
480 eglDisplay = mSlots[found].mEglDisplay;
481 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700482 // Don't return a fence in shared buffer mode, except for the first
483 // frame.
484 *outFence = (mCore->mSharedBufferMode &&
485 mCore->mSharedBufferSlot == found) ?
486 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800487 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
488 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700489
490 // If shared buffer mode has just been enabled, cache the slot of the
491 // first buffer that is dequeued and mark it as the shared buffer.
492 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
493 BufferQueueCore::INVALID_BUFFER_SLOT) {
494 mCore->mSharedBufferSlot = found;
495 mSlots[found].mBufferState.mShared = true;
496 }
Dan Stoza289ade12014-02-28 11:17:17 -0800497 } // Autolock scope
498
499 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700500 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700501 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700502 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700503 {mConsumerName.string(), mConsumerName.size()});
504
505 status_t error = graphicBuffer->initCheck();
506
Dan Stoza289ade12014-02-28 11:17:17 -0800507 { // Autolock scope
508 Mutex::Autolock lock(mCore->mMutex);
509
Chia-I Wufeec3b12017-05-25 09:34:56 -0700510 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700511 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
512 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
513 }
514
515 mCore->mIsAllocating = false;
516 mCore->mIsAllocatingCondition.broadcast();
517
Chia-I Wufeec3b12017-05-25 09:34:56 -0700518 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700519 mCore->mFreeSlots.insert(*outSlot);
520 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700521 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
522 return error;
523 }
524
Dan Stoza289ade12014-02-28 11:17:17 -0800525 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700526 mCore->mFreeSlots.insert(*outSlot);
527 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800528 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
529 return NO_INIT;
530 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800531
Pablo Ceballos9e314332016-01-12 13:49:19 -0800532 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800533 } // Autolock scope
534 }
535
Dan Stoza9f3053d2014-03-06 15:14:33 -0800536 if (attachedByConsumer) {
537 returnFlags |= BUFFER_NEEDS_REALLOCATION;
538 }
539
Dan Stoza289ade12014-02-28 11:17:17 -0800540 if (eglFence != EGL_NO_SYNC_KHR) {
541 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
542 1000000000);
543 // If something goes wrong, log the error, but return the buffer without
544 // synchronizing access to it. It's too late at this point to abort the
545 // dequeue operation.
546 if (result == EGL_FALSE) {
547 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
548 eglGetError());
549 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
550 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
551 }
552 eglDestroySyncKHR(eglDisplay, eglFence);
553 }
554
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700555 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
556 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800557 mSlots[*outSlot].mFrameNumber,
558 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
559
Ian Elliottd11b0442017-07-18 11:05:49 -0600560 if (outBufferAge) {
561 *outBufferAge = mCore->mBufferAge;
562 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700563 addAndGetFrameTimestamps(nullptr, outTimestamps);
564
Dan Stoza289ade12014-02-28 11:17:17 -0800565 return returnFlags;
566}
567
Dan Stoza9f3053d2014-03-06 15:14:33 -0800568status_t BufferQueueProducer::detachBuffer(int slot) {
569 ATRACE_CALL();
570 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700571 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800572
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700573 sp<IConsumerListener> listener;
574 {
575 Mutex::Autolock lock(mCore->mMutex);
576
577 if (mCore->mIsAbandoned) {
578 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
579 return NO_INIT;
580 }
581
582 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
583 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
584 return NO_INIT;
585 }
586
587 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
588 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
589 return BAD_VALUE;
590 }
591
592 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
593 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
594 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
595 return BAD_VALUE;
596 } else if (!mSlots[slot].mBufferState.isDequeued()) {
597 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
598 "(state = %s)", slot, mSlots[slot].mBufferState.string());
599 return BAD_VALUE;
600 } else if (!mSlots[slot].mRequestBufferCalled) {
601 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
602 slot);
603 return BAD_VALUE;
604 }
605
606 mSlots[slot].mBufferState.detachProducer();
607 mCore->mActiveBuffers.erase(slot);
608 mCore->mFreeSlots.insert(slot);
609 mCore->clearBufferSlotLocked(slot);
610 mCore->mDequeueCondition.broadcast();
611 VALIDATE_CONSISTENCY();
612 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800613 }
614
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700615 if (listener != NULL) {
616 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700617 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800618
Dan Stoza9f3053d2014-03-06 15:14:33 -0800619 return NO_ERROR;
620}
621
Dan Stozad9822a32014-03-28 15:25:31 -0700622status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
623 sp<Fence>* outFence) {
624 ATRACE_CALL();
625
626 if (outBuffer == NULL) {
627 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
628 return BAD_VALUE;
629 } else if (outFence == NULL) {
630 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
631 return BAD_VALUE;
632 }
633
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700634 sp<IConsumerListener> listener;
635 {
636 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700637
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700638 if (mCore->mIsAbandoned) {
639 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
640 return NO_INIT;
641 }
642
643 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
644 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
645 return NO_INIT;
646 }
647
648 if (mCore->mSharedBufferMode) {
649 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
650 "mode");
651 return BAD_VALUE;
652 }
653
654 mCore->waitWhileAllocatingLocked();
655
656 if (mCore->mFreeBuffers.empty()) {
657 return NO_MEMORY;
658 }
659
660 int found = mCore->mFreeBuffers.front();
661 mCore->mFreeBuffers.remove(found);
662 mCore->mFreeSlots.insert(found);
663
664 BQ_LOGV("detachNextBuffer detached slot %d", found);
665
666 *outBuffer = mSlots[found].mGraphicBuffer;
667 *outFence = mSlots[found].mFence;
668 mCore->clearBufferSlotLocked(found);
669 VALIDATE_CONSISTENCY();
670 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700671 }
672
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700673 if (listener != NULL) {
674 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700675 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800676
Dan Stozad9822a32014-03-28 15:25:31 -0700677 return NO_ERROR;
678}
679
Dan Stoza9f3053d2014-03-06 15:14:33 -0800680status_t BufferQueueProducer::attachBuffer(int* outSlot,
681 const sp<android::GraphicBuffer>& buffer) {
682 ATRACE_CALL();
683
684 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700685 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800686 return BAD_VALUE;
687 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700688 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800689 return BAD_VALUE;
690 }
691
692 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700693
694 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700696 return NO_INIT;
697 }
698
699 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700700 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700701 return NO_INIT;
702 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800703
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700704 if (mCore->mSharedBufferMode) {
705 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700706 return BAD_VALUE;
707 }
708
Dan Stoza812ed062015-06-02 15:45:22 -0700709 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
710 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
711 "[queue %u]", buffer->getGenerationNumber(),
712 mCore->mGenerationNumber);
713 return BAD_VALUE;
714 }
715
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700716 mCore->waitWhileAllocatingLocked();
717
Dan Stoza9f3053d2014-03-06 15:14:33 -0800718 status_t returnFlags = NO_ERROR;
719 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800720 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800721 if (status != NO_ERROR) {
722 return status;
723 }
724
725 // This should not happen
726 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700727 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 return -EBUSY;
729 }
730
731 *outSlot = found;
732 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700733 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800734 *outSlot, returnFlags);
735
736 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700737 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
739 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700740 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800741 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800742 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800743 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800744 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700745
Dan Stoza9f3053d2014-03-06 15:14:33 -0800746 return returnFlags;
747}
748
Dan Stoza289ade12014-02-28 11:17:17 -0800749status_t BufferQueueProducer::queueBuffer(int slot,
750 const QueueBufferInput &input, QueueBufferOutput *output) {
751 ATRACE_CALL();
752 ATRACE_BUFFER_INDEX(slot);
753
Brian Andersond6927fb2016-07-23 23:37:30 -0700754 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800755 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800756 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700757 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800758 int scalingMode;
759 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700760 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700761 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700763 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700764 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
765 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700766 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700767 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800768
Brian Andersond6927fb2016-07-23 23:37:30 -0700769 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800770 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800771 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800772 }
773
Brian Anderson3d4039d2016-09-23 16:31:30 -0700774 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
775
Dan Stoza289ade12014-02-28 11:17:17 -0800776 switch (scalingMode) {
777 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
778 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
779 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
780 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
781 break;
782 default:
783 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800784 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800785 }
786
Dan Stoza8dc55392014-11-04 11:37:46 -0800787 sp<IConsumerListener> frameAvailableListener;
788 sp<IConsumerListener> frameReplacedListener;
789 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700790 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800791 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800792 { // Autolock scope
793 Mutex::Autolock lock(mCore->mMutex);
794
795 if (mCore->mIsAbandoned) {
796 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
797 return NO_INIT;
798 }
799
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700800 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
801 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
802 return NO_INIT;
803 }
804
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800805 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800806 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800807 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800808 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700809 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800810 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700811 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800812 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800813 } else if (!mSlots[slot].mRequestBufferCalled) {
814 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
815 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800816 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800817 }
818
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700819 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700820 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700821 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700822 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700823 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700824 mSlots[slot].mBufferState.mShared = true;
825 }
826
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800827 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700828 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
829 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
830 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700831 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800832 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800833
834 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
835 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700836 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800837 crop.intersect(bufferRect, &croppedRect);
838 if (croppedRect != crop) {
839 BQ_LOGE("queueBuffer: crop rect is not contained within the "
840 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800841 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800842 }
843
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800844 // Override UNKNOWN dataspace with consumer default
845 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
846 dataSpace = mCore->mDefaultBufferDataSpace;
847 }
848
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700850 mSlots[slot].mBufferState.queue();
851
Brian Andersond6927fb2016-07-23 23:37:30 -0700852 // Increment the frame counter and store a local version of it
853 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800854 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700855 currentFrameNumber = mCore->mFrameCounter;
856 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800857
Dan Stoza289ade12014-02-28 11:17:17 -0800858 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
859 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
860 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800861 item.mTransform = transform &
862 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800863 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
865 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700866 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800868 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700869 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700870 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800871 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700872 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700873 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700874 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700875 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700877 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700878 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700879 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800880 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800881
Ruben Brunk1681d952014-06-27 15:51:55 -0700882 mStickyTransform = stickyTransform;
883
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700884 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700885 if (mCore->mSharedBufferMode) {
886 mCore->mSharedBufferCache.crop = crop;
887 mCore->mSharedBufferCache.transform = transform;
888 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700889 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700890 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700891 }
892
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800893 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800894 if (mCore->mQueue.empty()) {
895 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
896 // and simply queue this buffer
897 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800898 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800899 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700900 // When the queue is not empty, we need to look at the last buffer
901 // in the queue to see if we need to replace it
902 const BufferItem& last = mCore->mQueue.itemAt(
903 mCore->mQueue.size() - 1);
904 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800905
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700906 if (!last.mIsStale) {
907 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700908
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700909 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 // still be around. Mark it as no longer shared if this
911 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700912 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700913 mSlots[last.mSlot].mBufferState.isFree()) {
914 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700915 }
916 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700917 if (!mSlots[last.mSlot].mBufferState.isShared()) {
918 mCore->mActiveBuffers.erase(last.mSlot);
919 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800920 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700921 }
Dan Stoza289ade12014-02-28 11:17:17 -0800922 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800923
Dan Stoza289ade12014-02-28 11:17:17 -0800924 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700925 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800926 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800927 } else {
928 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800929 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800930 }
931 }
932
933 mCore->mBufferHasBeenQueued = true;
934 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700935 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800936
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700937 output->width = mCore->mDefaultWidth;
938 output->height = mCore->mDefaultHeight;
939 output->transformHint = mCore->mTransformHint;
940 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
941 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800942
Colin Cross152c3b72016-09-27 14:08:19 -0700943 ATRACE_INT(mCore->mConsumerName.string(),
944 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700945 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800946
947 // Take a ticket for the callback functions
948 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700949
Pablo Ceballos9e314332016-01-12 13:49:19 -0800950 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800951 } // Autolock scope
952
Irvel468051e2016-06-13 16:44:44 -0700953 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
954 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
955 // there will be no Binder call
956 if (!mConsumerIsSurfaceFlinger) {
957 item.mGraphicBuffer.clear();
958 }
959
960 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800961 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
962
963 // Call back without the main BufferQueue lock held, but with the callback
964 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700965
966 int connectedApi;
967 sp<Fence> lastQueuedFence;
968
969 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800970 Mutex::Autolock lock(mCallbackMutex);
971 while (callbackTicket != mCurrentCallbackTicket) {
972 mCallbackCondition.wait(mCallbackMutex);
973 }
974
975 if (frameAvailableListener != NULL) {
976 frameAvailableListener->onFrameAvailable(item);
977 } else if (frameReplacedListener != NULL) {
978 frameReplacedListener->onFrameReplaced(item);
979 }
980
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700981 connectedApi = mCore->mConnectedApi;
982 lastQueuedFence = std::move(mLastQueueBufferFence);
983
984 mLastQueueBufferFence = std::move(acquireFence);
985 mLastQueuedCrop = item.mCrop;
986 mLastQueuedTransform = item.mTransform;
987
Dan Stoza8dc55392014-11-04 11:37:46 -0800988 ++mCurrentCallbackTicket;
989 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800990 }
991
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000992 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700993 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000994 // Waiting here allows for two full buffers to be queued but not a
995 // third. In the event that frames take varying time, this makes a
996 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700997 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000998 }
999
Brian Andersond6927fb2016-07-23 23:37:30 -07001000 // Update and get FrameEventHistory.
1001 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1002 NewFrameEventsEntry newFrameEventsEntry = {
1003 currentFrameNumber,
1004 postedTime,
1005 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001006 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001007 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001008 addAndGetFrameTimestamps(&newFrameEventsEntry,
1009 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001010
Dan Stoza289ade12014-02-28 11:17:17 -08001011 return NO_ERROR;
1012}
1013
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001014status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001015 ATRACE_CALL();
1016 BQ_LOGV("cancelBuffer: slot %d", slot);
1017 Mutex::Autolock lock(mCore->mMutex);
1018
1019 if (mCore->mIsAbandoned) {
1020 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001021 return NO_INIT;
1022 }
1023
1024 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1025 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1026 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001027 }
1028
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001029 if (mCore->mSharedBufferMode) {
1030 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001031 return BAD_VALUE;
1032 }
1033
Dan Stoza3e96f192014-03-03 10:16:19 -08001034 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001035 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001036 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001037 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001038 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001039 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001040 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001041 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001042 } else if (fence == NULL) {
1043 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001044 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001045 }
1046
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001047 mSlots[slot].mBufferState.cancel();
1048
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001049 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001050 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001051 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001052 mSlots[slot].mBufferState.mShared = false;
1053 }
1054
1055 // Don't put the shared buffer on the free list.
1056 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001057 mCore->mActiveBuffers.erase(slot);
1058 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001059 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001060
Dan Stoza289ade12014-02-28 11:17:17 -08001061 mSlots[slot].mFence = fence;
1062 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001063 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001064
1065 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001066}
1067
1068int BufferQueueProducer::query(int what, int *outValue) {
1069 ATRACE_CALL();
1070 Mutex::Autolock lock(mCore->mMutex);
1071
1072 if (outValue == NULL) {
1073 BQ_LOGE("query: outValue was NULL");
1074 return BAD_VALUE;
1075 }
1076
1077 if (mCore->mIsAbandoned) {
1078 BQ_LOGE("query: BufferQueue has been abandoned");
1079 return NO_INIT;
1080 }
1081
1082 int value;
1083 switch (what) {
1084 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001085 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001086 break;
1087 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001088 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001089 break;
1090 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001091 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001092 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001093 case NATIVE_WINDOW_LAYER_COUNT:
1094 // All BufferQueue buffers have a single layer.
1095 value = BQ_LAYER_COUNT;
1096 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001097 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001098 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001099 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001100 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001101 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001102 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001103 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1104 value = (mCore->mQueue.size() > 1);
1105 break;
1106 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001107 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001108 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001109 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001110 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1111 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1112 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001113 case NATIVE_WINDOW_BUFFER_AGE:
1114 if (mCore->mBufferAge > INT32_MAX) {
1115 value = 0;
1116 } else {
1117 value = static_cast<int32_t>(mCore->mBufferAge);
1118 }
1119 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001120 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1121 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1122 break;
Yiwei Zhangdbd96152018-02-08 14:22:53 -08001123 case NATIVE_WINDOW_MAX_BUFFER_COUNT:
1124 value = static_cast<int32_t>(mCore->mMaxBufferCount);
1125 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001126 default:
1127 return BAD_VALUE;
1128 }
1129
1130 BQ_LOGV("query: %d? %d", what, value);
1131 *outValue = value;
1132 return NO_ERROR;
1133}
1134
Dan Stozaf0eaf252014-03-21 13:05:51 -07001135status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001136 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1137 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001138 Mutex::Autolock lock(mCore->mMutex);
1139 mConsumerName = mCore->mConsumerName;
1140 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1141 producerControlledByApp ? "true" : "false");
1142
1143 if (mCore->mIsAbandoned) {
1144 BQ_LOGE("connect: BufferQueue has been abandoned");
1145 return NO_INIT;
1146 }
1147
1148 if (mCore->mConsumerListener == NULL) {
1149 BQ_LOGE("connect: BufferQueue has no consumer");
1150 return NO_INIT;
1151 }
1152
1153 if (output == NULL) {
1154 BQ_LOGE("connect: output was NULL");
1155 return BAD_VALUE;
1156 }
1157
1158 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1159 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1160 mCore->mConnectedApi, api);
1161 return BAD_VALUE;
1162 }
1163
1164 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1165 mDequeueTimeout < 0 ?
1166 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1167 mCore->mMaxBufferCount) -
1168 mCore->getMaxBufferCountLocked();
1169 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1170 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1171 "slots. Delta = %d", delta);
1172 return BAD_VALUE;
1173 }
1174
Dan Stoza289ade12014-02-28 11:17:17 -08001175 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001176 switch (api) {
1177 case NATIVE_WINDOW_API_EGL:
1178 case NATIVE_WINDOW_API_CPU:
1179 case NATIVE_WINDOW_API_MEDIA:
1180 case NATIVE_WINDOW_API_CAMERA:
1181 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001182
1183 output->width = mCore->mDefaultWidth;
1184 output->height = mCore->mDefaultHeight;
1185 output->transformHint = mCore->mTransformHint;
1186 output->numPendingBuffers =
1187 static_cast<uint32_t>(mCore->mQueue.size());
1188 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001189 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001190
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001191 if (listener != NULL) {
1192 // Set up a death notification so that we can disconnect
1193 // automatically if the remote producer dies
1194 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1195 status = IInterface::asBinder(listener)->linkToDeath(
1196 static_cast<IBinder::DeathRecipient*>(this));
1197 if (status != NO_ERROR) {
1198 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1199 strerror(-status), status);
1200 }
1201 mCore->mLinkedToDeath = listener;
1202 }
1203 if (listener->needsReleaseNotify()) {
1204 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001205 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001206 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001207 break;
1208 default:
1209 BQ_LOGE("connect: unknown API %d", api);
1210 status = BAD_VALUE;
1211 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001212 }
Robert Carr97b9c862016-09-08 13:54:35 -07001213 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001214 mCore->mBufferHasBeenQueued = false;
1215 mCore->mDequeueBufferCannotBlock = false;
1216 if (mDequeueTimeout < 0) {
1217 mCore->mDequeueBufferCannotBlock =
1218 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001219 }
Dan Stoza289ade12014-02-28 11:17:17 -08001220
Pablo Ceballos981066c2016-02-18 12:54:37 -08001221 mCore->mAllowAllocation = true;
1222 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001223 return status;
1224}
1225
Robert Carr97b9c862016-09-08 13:54:35 -07001226status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001227 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001228 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001229
1230 int status = NO_ERROR;
1231 sp<IConsumerListener> listener;
1232 { // Autolock scope
1233 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001234
1235 if (mode == DisconnectMode::AllLocal) {
1236 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1237 return NO_ERROR;
1238 }
1239 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1240 }
1241
Antoine Labour78014f32014-07-15 21:17:03 -07001242 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001243
1244 if (mCore->mIsAbandoned) {
1245 // It's not really an error to disconnect after the surface has
1246 // been abandoned; it should just be a no-op.
1247 return NO_ERROR;
1248 }
1249
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001250 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001251 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1252 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1253 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001254 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001255 // If we're asked to disconnect the currently connected api but
1256 // nobody is connected, it's not really an error.
1257 if (api == BufferQueueCore::NO_CONNECTED_API) {
1258 return NO_ERROR;
1259 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001260 }
1261
Dan Stoza289ade12014-02-28 11:17:17 -08001262 switch (api) {
1263 case NATIVE_WINDOW_API_EGL:
1264 case NATIVE_WINDOW_API_CPU:
1265 case NATIVE_WINDOW_API_MEDIA:
1266 case NATIVE_WINDOW_API_CAMERA:
1267 if (mCore->mConnectedApi == api) {
1268 mCore->freeAllBuffersLocked();
1269
1270 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001271 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001272 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001273 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001274 // This can fail if we're here because of the death
1275 // notification, but we just ignore it
1276 token->unlinkToDeath(
1277 static_cast<IBinder::DeathRecipient*>(this));
1278 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001279 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001280 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001281 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001282 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001283 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001284 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001285 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001286 mCore->mDequeueCondition.broadcast();
1287 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001288 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1289 BQ_LOGE("disconnect: not connected (req=%d)", api);
1290 status = NO_INIT;
1291 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001292 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001293 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001294 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001295 }
1296 break;
1297 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001298 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001299 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001300 break;
1301 }
1302 } // Autolock scope
1303
1304 // Call back without lock held
1305 if (listener != NULL) {
1306 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001307 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001308 }
1309
1310 return status;
1311}
1312
Jesse Hall399184a2014-03-03 15:42:54 -08001313status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001314 sp<IConsumerListener> listener;
1315 { // Autolock scope
1316 Mutex::Autolock _l(mCore->mMutex);
1317 mCore->mSidebandStream = stream;
1318 listener = mCore->mConsumerListener;
1319 } // Autolock scope
1320
1321 if (listener != NULL) {
1322 listener->onSidebandStreamChanged();
1323 }
Jesse Hall399184a2014-03-03 15:42:54 -08001324 return NO_ERROR;
1325}
1326
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001327void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001328 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001329 ATRACE_CALL();
1330 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001331 size_t newBufferCount = 0;
1332 uint32_t allocWidth = 0;
1333 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001334 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001335 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001336 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001337 { // Autolock scope
1338 Mutex::Autolock lock(mCore->mMutex);
1339 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001340
Dan Stoza9de72932015-04-16 17:28:43 -07001341 if (!mCore->mAllowAllocation) {
1342 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1343 "BufferQueue");
1344 return;
1345 }
1346
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001347 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1348 // both allocateBuffers and dequeueBuffer.
1349 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001350 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001351 return;
1352 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001353
Antoine Labour78014f32014-07-15 21:17:03 -07001354 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1355 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1356 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1357 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001358 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001359
1360 mCore->mIsAllocating = true;
1361 } // Autolock scope
1362
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001363 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001364 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001365 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001366 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001367 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001368
1369 status_t result = graphicBuffer->initCheck();
1370
Antoine Labour78014f32014-07-15 21:17:03 -07001371 if (result != NO_ERROR) {
1372 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001373 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Antoine Labour78014f32014-07-15 21:17:03 -07001374 Mutex::Autolock lock(mCore->mMutex);
1375 mCore->mIsAllocating = false;
1376 mCore->mIsAllocatingCondition.broadcast();
1377 return;
1378 }
1379 buffers.push_back(graphicBuffer);
1380 }
1381
1382 { // Autolock scope
1383 Mutex::Autolock lock(mCore->mMutex);
1384 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1385 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001386 PixelFormat checkFormat = format != 0 ?
1387 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001388 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001389 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1390 checkFormat != allocFormat || checkUsage != allocUsage) {
1391 // Something changed while we released the lock. Retry.
1392 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1393 mCore->mIsAllocating = false;
1394 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001395 continue;
1396 }
1397
Antoine Labour78014f32014-07-15 21:17:03 -07001398 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001399 if (mCore->mFreeSlots.empty()) {
1400 BQ_LOGV("allocateBuffers: a slot was occupied while "
1401 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001402 continue;
1403 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001404 auto slot = mCore->mFreeSlots.begin();
1405 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1406 mSlots[*slot].mGraphicBuffer = buffers[i];
1407 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001408
1409 // freeBufferLocked puts this slot on the free slots list. Since
1410 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001411 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001412
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001413 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1414 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001415
1416 // Make sure the erase is done after all uses of the slot
1417 // iterator since it will be invalid after this point.
1418 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001419 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001420
Antoine Labour78014f32014-07-15 21:17:03 -07001421 mCore->mIsAllocating = false;
1422 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001423 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001424 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001425 }
1426}
1427
Dan Stoza9de72932015-04-16 17:28:43 -07001428status_t BufferQueueProducer::allowAllocation(bool allow) {
1429 ATRACE_CALL();
1430 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1431
1432 Mutex::Autolock lock(mCore->mMutex);
1433 mCore->mAllowAllocation = allow;
1434 return NO_ERROR;
1435}
1436
Dan Stoza812ed062015-06-02 15:45:22 -07001437status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1438 ATRACE_CALL();
1439 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1440
1441 Mutex::Autolock lock(mCore->mMutex);
1442 mCore->mGenerationNumber = generationNumber;
1443 return NO_ERROR;
1444}
1445
Dan Stozac6f30bd2015-06-08 09:32:50 -07001446String8 BufferQueueProducer::getConsumerName() const {
1447 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001448 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001449 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1450 return mConsumerName;
1451}
1452
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001453status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001454 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001455 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001456
1457 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001458 if (!sharedBufferMode) {
1459 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001460 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001461 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001462 return NO_ERROR;
1463}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001464
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001465status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1466 ATRACE_CALL();
1467 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1468
1469 Mutex::Autolock lock(mCore->mMutex);
1470
1471 mCore->mAutoRefresh = autoRefresh;
1472 return NO_ERROR;
1473}
1474
Dan Stoza127fc632015-06-30 13:43:32 -07001475status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1476 ATRACE_CALL();
1477 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1478
Pablo Ceballos981066c2016-02-18 12:54:37 -08001479 Mutex::Autolock lock(mCore->mMutex);
1480 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1481 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1482 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1483 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1484 "available slots. Delta = %d", delta);
1485 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001486 }
1487
Pablo Ceballos981066c2016-02-18 12:54:37 -08001488 mDequeueTimeout = timeout;
1489 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001490
Pablo Ceballos981066c2016-02-18 12:54:37 -08001491 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001492 return NO_ERROR;
1493}
1494
Dan Stoza50101d02016-04-07 16:53:23 -07001495status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001496 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001497 ATRACE_CALL();
1498 BQ_LOGV("getLastQueuedBuffer");
1499
1500 Mutex::Autolock lock(mCore->mMutex);
1501 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1502 *outBuffer = nullptr;
1503 *outFence = Fence::NO_FENCE;
1504 return NO_ERROR;
1505 }
1506
1507 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1508 *outFence = mLastQueueBufferFence;
1509
John Reck1a61da52016-04-28 13:18:15 -07001510 // Currently only SurfaceFlinger internally ever changes
1511 // GLConsumer's filtering mode, so we just use 'true' here as
1512 // this is slightly specialized for the current client of this API,
1513 // which does want filtering.
1514 GLConsumer::computeTransformMatrix(outTransformMatrix,
1515 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1516 mLastQueuedTransform, true /* filter */);
1517
Dan Stoza50101d02016-04-07 16:53:23 -07001518 return NO_ERROR;
1519}
1520
Brian Anderson3890c392016-07-25 12:48:08 -07001521void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1522 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001523}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001524
Brian Anderson3890c392016-07-25 12:48:08 -07001525void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001527 FrameEventHistoryDelta* outDelta) {
1528 if (newTimestamps == nullptr && outDelta == nullptr) {
1529 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001530 }
1531
1532 ATRACE_CALL();
1533 BQ_LOGV("addAndGetFrameTimestamps");
1534 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001535 {
1536 Mutex::Autolock lock(mCore->mMutex);
1537 listener = mCore->mConsumerListener;
1538 }
1539 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001540 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001541 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001542}
1543
Dan Stoza289ade12014-02-28 11:17:17 -08001544void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1545 // If we're here, it means that a producer we were connected to died.
1546 // We're guaranteed that we are still connected to it because we remove
1547 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1548 // without synchronization here.
1549 int api = mCore->mConnectedApi;
1550 disconnect(api);
1551}
1552
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001553status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1554 BQ_LOGV("getUniqueId");
1555
1556 *outId = mCore->mUniqueId;
1557 return NO_ERROR;
1558}
1559
Chia-I Wue2786ea2017-08-07 10:36:08 -07001560status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1561 BQ_LOGV("getConsumerUsage");
1562
1563 Mutex::Autolock lock(mCore->mMutex);
1564 *outUsage = mCore->mConsumerUsageBits;
1565 return NO_ERROR;
1566}
1567
Dan Stoza289ade12014-02-28 11:17:17 -08001568} // namespace android