blob: d3ce06f5fe0970b175384c6edc5824f41066df1e [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>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080038#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070043#include <system/window.h>
44
Dan Stoza289ade12014-02-28 11:17:17 -080045namespace android {
46
Iris Chang430193f2019-12-04 16:25:46 +080047// Macros for include BufferQueueCore information in log messages
48#define BQ_LOGV(x, ...) \
49 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
50 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
51 ##__VA_ARGS__)
52#define BQ_LOGD(x, ...) \
53 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
54 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
55 ##__VA_ARGS__)
56#define BQ_LOGI(x, ...) \
57 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
58 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
59 ##__VA_ARGS__)
60#define BQ_LOGW(x, ...) \
61 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
62 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
63 ##__VA_ARGS__)
64#define BQ_LOGE(x, ...) \
65 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.string(), \
66 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
67 ##__VA_ARGS__)
68
Craig Donner6ebc46a2016-10-21 15:23:44 -070069static constexpr uint32_t BQ_LAYER_COUNT = 1;
70
Irvel468051e2016-06-13 16:44:44 -070071BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
72 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080073 mCore(core),
74 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070075 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070076 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070077 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080078 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070079 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080080 mCallbackMutex(),
81 mNextCallbackTicket(0),
82 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070083 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010084 mDequeueTimeout(-1),
85 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080086
87BufferQueueProducer::~BufferQueueProducer() {}
88
89status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
90 ATRACE_CALL();
91 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020092 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080093
94 if (mCore->mIsAbandoned) {
95 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
96 return NO_INIT;
97 }
98
Pablo Ceballos583b1b32015-09-03 18:23:52 -070099 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
100 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
101 return NO_INIT;
102 }
103
Dan Stoza3e96f192014-03-03 10:16:19 -0800104 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800105 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800106 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800107 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700108 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800109 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700110 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800111 return BAD_VALUE;
112 }
113
114 mSlots[slot].mRequestBufferCalled = true;
115 *buf = mSlots[slot].mGraphicBuffer;
116 return NO_ERROR;
117}
118
Pablo Ceballosfa455352015-08-12 17:47:47 -0700119status_t BufferQueueProducer::setMaxDequeuedBufferCount(
120 int maxDequeuedBuffers) {
121 ATRACE_CALL();
122 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
123 maxDequeuedBuffers);
124
Pablo Ceballos981066c2016-02-18 12:54:37 -0800125 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700126 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200127 std::unique_lock<std::mutex> lock(mCore->mMutex);
128 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700129
130 if (mCore->mIsAbandoned) {
131 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
132 "abandoned");
133 return NO_INIT;
134 }
135
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700136 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
137 return NO_ERROR;
138 }
139
Pablo Ceballos72daab62015-12-07 16:38:43 -0800140 // The new maxDequeuedBuffer count should not be violated by the number
141 // of currently dequeued buffers
142 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800143 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700144 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800145 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700146 }
147 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800148 if (dequeuedCount > maxDequeuedBuffers) {
149 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
150 "count (%d) exceeds the current dequeued buffer count (%d)",
151 maxDequeuedBuffers, dequeuedCount);
152 return BAD_VALUE;
153 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700154
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700155 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700156 bufferCount += maxDequeuedBuffers;
157
158 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
159 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
160 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
161 return BAD_VALUE;
162 }
163
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700164 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 if (bufferCount < minBufferSlots) {
166 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
167 "less than minimum %d", bufferCount, minBufferSlots);
168 return BAD_VALUE;
169 }
170
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700171 if (bufferCount > mCore->mMaxBufferCount) {
172 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700173 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
174 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
175 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
176 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700177 return BAD_VALUE;
178 }
179
Pablo Ceballos72daab62015-12-07 16:38:43 -0800180 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800181 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800182 return BAD_VALUE;
183 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700184 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800185 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800186 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800187 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800188 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200189 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700190 } // Autolock scope
191
192 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700193 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800194 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700195 }
196
197 return NO_ERROR;
198}
199
200status_t BufferQueueProducer::setAsyncMode(bool async) {
201 ATRACE_CALL();
202 BQ_LOGV("setAsyncMode: async = %d", async);
203
Pablo Ceballos981066c2016-02-18 12:54:37 -0800204 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700205 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200206 std::unique_lock<std::mutex> lock(mCore->mMutex);
207 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700208
209 if (mCore->mIsAbandoned) {
210 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
211 return NO_INIT;
212 }
213
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700214 if (async == mCore->mAsyncMode) {
215 return NO_ERROR;
216 }
217
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700218 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700219 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
220 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700221 BQ_LOGE("setAsyncMode(%d): this call would cause the "
222 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700223 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
224 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
225 mCore->mMaxDequeuedBufferCount,
226 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700227 return BAD_VALUE;
228 }
229
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800230 int delta = mCore->getMaxBufferCountLocked(async,
231 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
232 - mCore->getMaxBufferCountLocked();
233
Pablo Ceballos981066c2016-02-18 12:54:37 -0800234 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800235 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
236 "available slots. Delta = %d", delta);
237 return BAD_VALUE;
238 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700239 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800240 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200241 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700242 if (delta < 0) {
243 listener = mCore->mConsumerListener;
244 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700245 } // Autolock scope
246
247 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700248 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800249 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700250 }
251 return NO_ERROR;
252}
253
Dan Stoza5ecfb682016-01-04 17:01:02 -0800254int BufferQueueProducer::getFreeBufferLocked() const {
255 if (mCore->mFreeBuffers.empty()) {
256 return BufferQueueCore::INVALID_BUFFER_SLOT;
257 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800258 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800259 mCore->mFreeBuffers.pop_front();
260 return slot;
261}
262
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800263int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800264 if (mCore->mFreeSlots.empty()) {
265 return BufferQueueCore::INVALID_BUFFER_SLOT;
266 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800267 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800268 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800269 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270}
271
272status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200273 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800274 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
275 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800276 bool tryAgain = true;
277 while (tryAgain) {
278 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800279 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800280 return NO_INIT;
281 }
282
Dan Stoza9f3053d2014-03-06 15:14:33 -0800283 int dequeuedCount = 0;
284 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800285 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700286 if (mSlots[s].mBufferState.isDequeued()) {
287 ++dequeuedCount;
288 }
289 if (mSlots[s].mBufferState.isAcquired()) {
290 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800291 }
292 }
293
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700294 // Producers are not allowed to dequeue more than
295 // mMaxDequeuedBufferCount buffers.
296 // This check is only done if a buffer has already been queued
297 if (mCore->mBufferHasBeenQueued &&
298 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700299 // Supress error logs when timeout is non-negative.
300 if (mDequeueTimeout < 0) {
301 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
302 "count (%d)", callerString,
303 mCore->mMaxDequeuedBufferCount);
304 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800305 return INVALID_OPERATION;
306 }
307
Dan Stoza0de7ea72015-04-23 13:20:51 -0700308 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
309
Dan Stozaae3c3682014-04-18 15:43:35 -0700310 // If we disconnect and reconnect quickly, we can be in a state where
311 // our slots are empty but we have many buffers in the queue. This can
312 // cause us to run out of memory if we outrun the consumer. Wait here if
313 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800314 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700315 bool tooManyBuffers = mCore->mQueue.size()
316 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700317 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800318 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700319 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700320 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700321 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700322 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700323 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700324 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700325 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800326 } else {
327 if (caller == FreeSlotCaller::Dequeue) {
328 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800329 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800330 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
331 *found = slot;
332 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800333 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800334 }
335 } else {
336 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800337 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800338 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
339 *found = slot;
340 } else {
341 *found = getFreeBufferLocked();
342 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700343 }
344 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700345 }
346
347 // If no buffer is found, or if the queue has too many buffers
348 // outstanding, wait for a buffer to be acquired or released, or for the
349 // max buffer count to change.
350 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
351 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800352 if (tryAgain) {
353 // Return an error if we're in non-blocking mode (producer and
354 // consumer are controlled by the application).
355 // However, the consumer is allowed to briefly acquire an extra
356 // buffer (which could cause us to have to wait here), which is
357 // okay, since it is only used to implement an atomic acquire +
358 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700359 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800360 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
361 return WOULD_BLOCK;
362 }
Dan Stoza127fc632015-06-30 13:43:32 -0700363 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200364 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
365 std::chrono::nanoseconds(mDequeueTimeout));
366 if (result == std::cv_status::timeout) {
367 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700368 }
369 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200370 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700371 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800372 }
373 } // while (tryAgain)
374
375 return NO_ERROR;
376}
377
Ian Elliottd11b0442017-07-18 11:05:49 -0600378status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
379 uint32_t width, uint32_t height, PixelFormat format,
380 uint64_t usage, uint64_t* outBufferAge,
381 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800382 ATRACE_CALL();
383 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200384 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800385 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700386
387 if (mCore->mIsAbandoned) {
388 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
389 return NO_INIT;
390 }
391
392 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
393 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
394 return NO_INIT;
395 }
Dan Stoza289ade12014-02-28 11:17:17 -0800396 } // Autolock scope
397
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700398 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800399
400 if ((width && !height) || (!width && height)) {
401 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
402 return BAD_VALUE;
403 }
404
405 status_t returnFlags = NO_ERROR;
406 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
407 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800408 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800409
410 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200411 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800412
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100413 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
414 // is finished such that we don't allocate in parallel.
415 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
416 mDequeueWaitingForAllocation = true;
417 mCore->waitWhileAllocatingLocked(lock);
418 mDequeueWaitingForAllocation = false;
419 mDequeueWaitingForAllocationCondition.notify_all();
420 }
Dan Stoza289ade12014-02-28 11:17:17 -0800421
422 if (format == 0) {
423 format = mCore->mDefaultBufferFormat;
424 }
425
426 // Enable the usage bits the consumer requested
427 usage |= mCore->mConsumerUsageBits;
428
Dan Stoza9de72932015-04-16 17:28:43 -0700429 const bool useDefaultSize = !width && !height;
430 if (useDefaultSize) {
431 width = mCore->mDefaultWidth;
432 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700433 if (mCore->mAutoPrerotation &&
434 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
435 std::swap(width, height);
436 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800437 }
Dan Stoza289ade12014-02-28 11:17:17 -0800438
Pablo Ceballos981066c2016-02-18 12:54:37 -0800439 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700440 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200441 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700442 if (status != NO_ERROR) {
443 return status;
444 }
445
446 // This should not happen
447 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
448 BQ_LOGE("dequeueBuffer: no available buffer slots");
449 return -EBUSY;
450 }
451
452 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
453
454 // If we are not allowed to allocate new buffers,
455 // waitForFreeSlotThenRelock must have returned a slot containing a
456 // buffer. If this buffer would require reallocation to meet the
457 // requested attributes, we free it and attempt to get another one.
458 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700459 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700460 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700461 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700462 return BAD_VALUE;
463 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800464 mCore->mFreeSlots.insert(found);
465 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700466 found = BufferItem::INVALID_BUFFER_SLOT;
467 continue;
468 }
469 }
Dan Stoza289ade12014-02-28 11:17:17 -0800470 }
471
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800472 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700473 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700474 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800475 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
476 "buffer");
477
478 return BAD_VALUE;
479 }
480
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700481 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800482 mCore->mActiveBuffers.insert(found);
483 }
Dan Stoza289ade12014-02-28 11:17:17 -0800484 *outSlot = found;
485 ATRACE_BUFFER_INDEX(found);
486
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800487 attachedByConsumer = mSlots[found].mNeedsReallocation;
488 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800489
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700490 mSlots[found].mBufferState.dequeue();
491
Yi Kong48a619f2018-06-05 16:34:59 -0700492 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700493 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800494 {
495 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700496 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800497 mSlots[found].mRequestBufferCalled = false;
498 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
499 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
500 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800501 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700502 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800503
504 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800505 } else {
506 // We add 1 because that will be the frame number when this buffer
507 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700508 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800509 }
510
Dan Stoza800b41a2015-04-28 14:20:04 -0700511 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
512 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800513
Yi Kong48a619f2018-06-05 16:34:59 -0700514 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800515 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
516 "slot=%d w=%d h=%d format=%u",
517 found, buffer->width, buffer->height, buffer->format);
518 }
519
520 eglDisplay = mSlots[found].mEglDisplay;
521 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700522 // Don't return a fence in shared buffer mode, except for the first
523 // frame.
524 *outFence = (mCore->mSharedBufferMode &&
525 mCore->mSharedBufferSlot == found) ?
526 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800527 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
528 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700529
530 // If shared buffer mode has just been enabled, cache the slot of the
531 // first buffer that is dequeued and mark it as the shared buffer.
532 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
533 BufferQueueCore::INVALID_BUFFER_SLOT) {
534 mCore->mSharedBufferSlot = found;
535 mSlots[found].mBufferState.mShared = true;
536 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700537
538 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
539 if (mCore->mConsumerListener != nullptr) {
540 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
541 }
542 }
Dan Stoza289ade12014-02-28 11:17:17 -0800543 } // Autolock scope
544
545 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700546 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700547 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700548 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700549 {mConsumerName.string(), mConsumerName.size()});
550
551 status_t error = graphicBuffer->initCheck();
552
Dan Stoza289ade12014-02-28 11:17:17 -0800553 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200554 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800555
Chia-I Wufeec3b12017-05-25 09:34:56 -0700556 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700557 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
558 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Adithya Srinivasana820af92019-11-01 13:55:17 -0700559 if (mCore->mConsumerListener != nullptr) {
560 mCore->mConsumerListener->onFrameDequeued(
561 mSlots[*outSlot].mGraphicBuffer->getId());
562 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700563 }
564
565 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200566 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700567
Chia-I Wufeec3b12017-05-25 09:34:56 -0700568 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700569 mCore->mFreeSlots.insert(*outSlot);
570 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700571 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
572 return error;
573 }
574
Dan Stoza289ade12014-02-28 11:17:17 -0800575 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700576 mCore->mFreeSlots.insert(*outSlot);
577 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800578 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
579 return NO_INIT;
580 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800581
Pablo Ceballos9e314332016-01-12 13:49:19 -0800582 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800583 } // Autolock scope
584 }
585
Dan Stoza9f3053d2014-03-06 15:14:33 -0800586 if (attachedByConsumer) {
587 returnFlags |= BUFFER_NEEDS_REALLOCATION;
588 }
589
Dan Stoza289ade12014-02-28 11:17:17 -0800590 if (eglFence != EGL_NO_SYNC_KHR) {
591 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
592 1000000000);
593 // If something goes wrong, log the error, but return the buffer without
594 // synchronizing access to it. It's too late at this point to abort the
595 // dequeue operation.
596 if (result == EGL_FALSE) {
597 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
598 eglGetError());
599 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
600 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
601 }
602 eglDestroySyncKHR(eglDisplay, eglFence);
603 }
604
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700605 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
606 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800607 mSlots[*outSlot].mFrameNumber,
608 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
609
Ian Elliottd11b0442017-07-18 11:05:49 -0600610 if (outBufferAge) {
611 *outBufferAge = mCore->mBufferAge;
612 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700613 addAndGetFrameTimestamps(nullptr, outTimestamps);
614
Dan Stoza289ade12014-02-28 11:17:17 -0800615 return returnFlags;
616}
617
Dan Stoza9f3053d2014-03-06 15:14:33 -0800618status_t BufferQueueProducer::detachBuffer(int slot) {
619 ATRACE_CALL();
620 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700621 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800622
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700623 sp<IConsumerListener> listener;
624 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200625 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700626
627 if (mCore->mIsAbandoned) {
628 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
629 return NO_INIT;
630 }
631
632 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
633 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
634 return NO_INIT;
635 }
636
637 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
638 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
639 return BAD_VALUE;
640 }
641
642 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
643 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
644 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
645 return BAD_VALUE;
646 } else if (!mSlots[slot].mBufferState.isDequeued()) {
647 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
648 "(state = %s)", slot, mSlots[slot].mBufferState.string());
649 return BAD_VALUE;
650 } else if (!mSlots[slot].mRequestBufferCalled) {
651 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
652 slot);
653 return BAD_VALUE;
654 }
655
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700656 listener = mCore->mConsumerListener;
657 if (listener != nullptr) {
658 listener->onFrameDetached(mSlots[slot].mGraphicBuffer->getId());
659 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700660 mSlots[slot].mBufferState.detachProducer();
661 mCore->mActiveBuffers.erase(slot);
662 mCore->mFreeSlots.insert(slot);
663 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200664 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700665 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800666 }
667
Yi Kong48a619f2018-06-05 16:34:59 -0700668 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700669 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700670 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800671
Dan Stoza9f3053d2014-03-06 15:14:33 -0800672 return NO_ERROR;
673}
674
Dan Stozad9822a32014-03-28 15:25:31 -0700675status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
676 sp<Fence>* outFence) {
677 ATRACE_CALL();
678
Yi Kong48a619f2018-06-05 16:34:59 -0700679 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700680 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
681 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700682 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700683 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
684 return BAD_VALUE;
685 }
686
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700687 sp<IConsumerListener> listener;
688 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200689 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700690
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700691 if (mCore->mIsAbandoned) {
692 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
693 return NO_INIT;
694 }
695
696 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
697 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
698 return NO_INIT;
699 }
700
701 if (mCore->mSharedBufferMode) {
702 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
703 "mode");
704 return BAD_VALUE;
705 }
706
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200707 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700708
709 if (mCore->mFreeBuffers.empty()) {
710 return NO_MEMORY;
711 }
712
713 int found = mCore->mFreeBuffers.front();
714 mCore->mFreeBuffers.remove(found);
715 mCore->mFreeSlots.insert(found);
716
717 BQ_LOGV("detachNextBuffer detached slot %d", found);
718
719 *outBuffer = mSlots[found].mGraphicBuffer;
720 *outFence = mSlots[found].mFence;
721 mCore->clearBufferSlotLocked(found);
722 VALIDATE_CONSISTENCY();
723 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700724 }
725
Yi Kong48a619f2018-06-05 16:34:59 -0700726 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700727 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700728 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800729
Dan Stozad9822a32014-03-28 15:25:31 -0700730 return NO_ERROR;
731}
732
Dan Stoza9f3053d2014-03-06 15:14:33 -0800733status_t BufferQueueProducer::attachBuffer(int* outSlot,
734 const sp<android::GraphicBuffer>& buffer) {
735 ATRACE_CALL();
736
Yi Kong48a619f2018-06-05 16:34:59 -0700737 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700738 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800739 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700740 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700741 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800742 return BAD_VALUE;
743 }
744
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200745 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700746
747 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700748 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700749 return NO_INIT;
750 }
751
752 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700753 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700754 return NO_INIT;
755 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800756
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700757 if (mCore->mSharedBufferMode) {
758 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700759 return BAD_VALUE;
760 }
761
Dan Stoza812ed062015-06-02 15:45:22 -0700762 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
763 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
764 "[queue %u]", buffer->getGenerationNumber(),
765 mCore->mGenerationNumber);
766 return BAD_VALUE;
767 }
768
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200769 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700770
Dan Stoza9f3053d2014-03-06 15:14:33 -0800771 status_t returnFlags = NO_ERROR;
772 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200773 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800774 if (status != NO_ERROR) {
775 return status;
776 }
777
778 // This should not happen
779 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700780 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800781 return -EBUSY;
782 }
783
784 *outSlot = found;
785 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700786 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800787 *outSlot, returnFlags);
788
789 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700790 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800791 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
792 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700793 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800794 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800795 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800796 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800797 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700798
Dan Stoza9f3053d2014-03-06 15:14:33 -0800799 return returnFlags;
800}
801
Dan Stoza289ade12014-02-28 11:17:17 -0800802status_t BufferQueueProducer::queueBuffer(int slot,
803 const QueueBufferInput &input, QueueBufferOutput *output) {
804 ATRACE_CALL();
805 ATRACE_BUFFER_INDEX(slot);
806
Brian Andersond6927fb2016-07-23 23:37:30 -0700807 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800808 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800809 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700810 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800811 int scalingMode;
812 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700813 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700814 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700815 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700816 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700817 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
818 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700819 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700820 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800821
Yi Kong48a619f2018-06-05 16:34:59 -0700822 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800823 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800824 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800825 }
826
Brian Anderson3d4039d2016-09-23 16:31:30 -0700827 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
828
Dan Stoza289ade12014-02-28 11:17:17 -0800829 switch (scalingMode) {
830 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
831 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
832 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
833 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
834 break;
835 default:
836 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800837 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800838 }
839
Dan Stoza8dc55392014-11-04 11:37:46 -0800840 sp<IConsumerListener> frameAvailableListener;
841 sp<IConsumerListener> frameReplacedListener;
842 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700843 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800844 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800845 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200846 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800847
848 if (mCore->mIsAbandoned) {
849 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
850 return NO_INIT;
851 }
852
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700853 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
854 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
855 return NO_INIT;
856 }
857
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800858 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800859 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800860 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800861 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800863 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700864 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800865 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800866 } else if (!mSlots[slot].mRequestBufferCalled) {
867 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
868 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800869 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800870 }
871
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700872 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700873 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700874 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700875 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700877 mSlots[slot].mBufferState.mShared = true;
878 }
879
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800880 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700881 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
882 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
883 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700884 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800885 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800886
887 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
888 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700889 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800890 crop.intersect(bufferRect, &croppedRect);
891 if (croppedRect != crop) {
892 BQ_LOGE("queueBuffer: crop rect is not contained within the "
893 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800894 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800895 }
896
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800897 // Override UNKNOWN dataspace with consumer default
898 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
899 dataSpace = mCore->mDefaultBufferDataSpace;
900 }
901
Brian Andersond6927fb2016-07-23 23:37:30 -0700902 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700903 mSlots[slot].mBufferState.queue();
904
Brian Andersond6927fb2016-07-23 23:37:30 -0700905 // Increment the frame counter and store a local version of it
906 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800907 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700908 currentFrameNumber = mCore->mFrameCounter;
909 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800910
Dan Stoza289ade12014-02-28 11:17:17 -0800911 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
912 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
913 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800914 item.mTransform = transform &
915 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800916 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800917 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
918 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700919 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800920 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800921 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700922 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700923 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800924 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700925 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700926 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700927 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700928 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800929 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700930 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700931 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700932 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700933 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800934 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800935
Ruben Brunk1681d952014-06-27 15:51:55 -0700936 mStickyTransform = stickyTransform;
937
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700938 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700939 if (mCore->mSharedBufferMode) {
940 mCore->mSharedBufferCache.crop = crop;
941 mCore->mSharedBufferCache.transform = transform;
942 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700943 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700944 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700945 }
946
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800947 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800948 if (mCore->mQueue.empty()) {
949 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
950 // and simply queue this buffer
951 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800952 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800953 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700954 // When the queue is not empty, we need to look at the last buffer
955 // in the queue to see if we need to replace it
956 const BufferItem& last = mCore->mQueue.itemAt(
957 mCore->mQueue.size() - 1);
958 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800959
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700960 if (!last.mIsStale) {
961 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700962
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700963 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700964 // still be around. Mark it as no longer shared if this
965 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700966 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700967 mSlots[last.mSlot].mBufferState.isFree()) {
968 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700969 }
970 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700971 if (!mSlots[last.mSlot].mBufferState.isShared()) {
972 mCore->mActiveBuffers.erase(last.mSlot);
973 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800974 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700975 }
Dan Stoza289ade12014-02-28 11:17:17 -0800976 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800977
Steven Thomas862e7532019-07-10 19:21:03 -0700978 // Make sure to merge the damage rect from the frame we're about
979 // to drop into the new frame's damage rect.
980 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
981 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
982 item.mSurfaceDamage = Region::INVALID_REGION;
983 } else {
984 item.mSurfaceDamage |= last.mSurfaceDamage;
985 }
986
Dan Stoza289ade12014-02-28 11:17:17 -0800987 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700988 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800989 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800990 } else {
991 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800992 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800993 }
994 }
995
996 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200997 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -0700998 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800999
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001000 output->width = mCore->mDefaultWidth;
1001 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001002 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001003 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1004 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001005
Colin Cross152c3b72016-09-27 14:08:19 -07001006 ATRACE_INT(mCore->mConsumerName.string(),
1007 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -07001008 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -08001009
1010 // Take a ticket for the callback functions
1011 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001012
Pablo Ceballos9e314332016-01-12 13:49:19 -08001013 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001014 } // Autolock scope
1015
Irvel468051e2016-06-13 16:44:44 -07001016 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1017 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1018 // there will be no Binder call
1019 if (!mConsumerIsSurfaceFlinger) {
1020 item.mGraphicBuffer.clear();
1021 }
1022
Dan Stoza8dc55392014-11-04 11:37:46 -08001023 // Call back without the main BufferQueue lock held, but with the callback
1024 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001025
1026 int connectedApi;
1027 sp<Fence> lastQueuedFence;
1028
1029 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001030 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001031 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001032 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001033 }
1034
Yi Kong48a619f2018-06-05 16:34:59 -07001035 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001036 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001037 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001038 frameReplacedListener->onFrameReplaced(item);
1039 }
1040
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001041 connectedApi = mCore->mConnectedApi;
1042 lastQueuedFence = std::move(mLastQueueBufferFence);
1043
1044 mLastQueueBufferFence = std::move(acquireFence);
1045 mLastQueuedCrop = item.mCrop;
1046 mLastQueuedTransform = item.mTransform;
1047
Dan Stoza8dc55392014-11-04 11:37:46 -08001048 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001049 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001050 }
1051
Brian Andersond6927fb2016-07-23 23:37:30 -07001052 // Update and get FrameEventHistory.
1053 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1054 NewFrameEventsEntry newFrameEventsEntry = {
1055 currentFrameNumber,
1056 postedTime,
1057 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001058 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001059 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001060 addAndGetFrameTimestamps(&newFrameEventsEntry,
1061 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001062
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001063 // Wait without lock held
1064 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1065 // Waiting here allows for two full buffers to be queued but not a
1066 // third. In the event that frames take varying time, this makes a
1067 // small trade-off in favor of latency rather than throughput.
1068 lastQueuedFence->waitForever("Throttling EGL Production");
1069 }
1070
Dan Stoza289ade12014-02-28 11:17:17 -08001071 return NO_ERROR;
1072}
1073
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001074status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001075 ATRACE_CALL();
1076 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001077 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001078
1079 if (mCore->mIsAbandoned) {
1080 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001081 return NO_INIT;
1082 }
1083
1084 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1085 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1086 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001087 }
1088
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001089 if (mCore->mSharedBufferMode) {
1090 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001091 return BAD_VALUE;
1092 }
1093
Dan Stoza3e96f192014-03-03 10:16:19 -08001094 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001095 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001096 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001097 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001098 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001099 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001100 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001101 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001102 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001103 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001104 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001105 }
1106
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001107 mSlots[slot].mBufferState.cancel();
1108
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001109 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001110 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001111 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001112 mSlots[slot].mBufferState.mShared = false;
1113 }
1114
1115 // Don't put the shared buffer on the free list.
1116 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001117 mCore->mActiveBuffers.erase(slot);
1118 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001119 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001120
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001121 if (mCore->mConsumerListener != nullptr) {
1122 mCore->mConsumerListener->onFrameCancelled(mSlots[slot].mGraphicBuffer->getId());
1123 }
Dan Stoza289ade12014-02-28 11:17:17 -08001124 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001125 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001126 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001127
1128 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001129}
1130
1131int BufferQueueProducer::query(int what, int *outValue) {
1132 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001133 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001134
Yi Kong48a619f2018-06-05 16:34:59 -07001135 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001136 BQ_LOGE("query: outValue was NULL");
1137 return BAD_VALUE;
1138 }
1139
1140 if (mCore->mIsAbandoned) {
1141 BQ_LOGE("query: BufferQueue has been abandoned");
1142 return NO_INIT;
1143 }
1144
1145 int value;
1146 switch (what) {
1147 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001148 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001149 break;
1150 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001151 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001152 break;
1153 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001154 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001155 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001156 case NATIVE_WINDOW_LAYER_COUNT:
1157 // All BufferQueue buffers have a single layer.
1158 value = BQ_LAYER_COUNT;
1159 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001160 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001161 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001162 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001163 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001164 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001165 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001166 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1167 value = (mCore->mQueue.size() > 1);
1168 break;
1169 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001170 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001171 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001172 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001173 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1174 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1175 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001176 case NATIVE_WINDOW_BUFFER_AGE:
1177 if (mCore->mBufferAge > INT32_MAX) {
1178 value = 0;
1179 } else {
1180 value = static_cast<int32_t>(mCore->mBufferAge);
1181 }
1182 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001183 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1184 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1185 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001186 default:
1187 return BAD_VALUE;
1188 }
1189
1190 BQ_LOGV("query: %d? %d", what, value);
1191 *outValue = value;
1192 return NO_ERROR;
1193}
1194
Dan Stozaf0eaf252014-03-21 13:05:51 -07001195status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001196 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1197 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001198 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001199 mConsumerName = mCore->mConsumerName;
1200 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1201 producerControlledByApp ? "true" : "false");
1202
1203 if (mCore->mIsAbandoned) {
1204 BQ_LOGE("connect: BufferQueue has been abandoned");
1205 return NO_INIT;
1206 }
1207
Yi Kong48a619f2018-06-05 16:34:59 -07001208 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001209 BQ_LOGE("connect: BufferQueue has no consumer");
1210 return NO_INIT;
1211 }
1212
Yi Kong48a619f2018-06-05 16:34:59 -07001213 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001214 BQ_LOGE("connect: output was NULL");
1215 return BAD_VALUE;
1216 }
1217
1218 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1219 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1220 mCore->mConnectedApi, api);
1221 return BAD_VALUE;
1222 }
1223
1224 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1225 mDequeueTimeout < 0 ?
1226 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1227 mCore->mMaxBufferCount) -
1228 mCore->getMaxBufferCountLocked();
1229 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1230 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1231 "slots. Delta = %d", delta);
1232 return BAD_VALUE;
1233 }
1234
Dan Stoza289ade12014-02-28 11:17:17 -08001235 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001236 switch (api) {
1237 case NATIVE_WINDOW_API_EGL:
1238 case NATIVE_WINDOW_API_CPU:
1239 case NATIVE_WINDOW_API_MEDIA:
1240 case NATIVE_WINDOW_API_CAMERA:
1241 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001242
1243 output->width = mCore->mDefaultWidth;
1244 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001245 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001246 output->numPendingBuffers =
1247 static_cast<uint32_t>(mCore->mQueue.size());
1248 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001249 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001250 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001251
Yi Kong48a619f2018-06-05 16:34:59 -07001252 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001253 // Set up a death notification so that we can disconnect
1254 // automatically if the remote producer dies
Yi Kong48a619f2018-06-05 16:34:59 -07001255 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001256 status = IInterface::asBinder(listener)->linkToDeath(
1257 static_cast<IBinder::DeathRecipient*>(this));
1258 if (status != NO_ERROR) {
1259 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1260 strerror(-status), status);
1261 }
1262 mCore->mLinkedToDeath = listener;
1263 }
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001264 mCore->mConnectedProducerListener = listener;
1265 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001266 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001267 break;
1268 default:
1269 BQ_LOGE("connect: unknown API %d", api);
1270 status = BAD_VALUE;
1271 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001272 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001273 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001274 mCore->mBufferHasBeenQueued = false;
1275 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001276 mCore->mQueueBufferCanDrop = false;
1277 mCore->mLegacyBufferDrop = true;
1278 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1279 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1280 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001281 }
Dan Stoza289ade12014-02-28 11:17:17 -08001282
Pablo Ceballos981066c2016-02-18 12:54:37 -08001283 mCore->mAllowAllocation = true;
1284 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001285 return status;
1286}
1287
Robert Carr97b9c862016-09-08 13:54:35 -07001288status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001289 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001290 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001291
1292 int status = NO_ERROR;
1293 sp<IConsumerListener> listener;
1294 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001295 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001296
1297 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001298 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001299 return NO_ERROR;
1300 }
1301 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1302 }
1303
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001304 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001305
1306 if (mCore->mIsAbandoned) {
1307 // It's not really an error to disconnect after the surface has
1308 // been abandoned; it should just be a no-op.
1309 return NO_ERROR;
1310 }
1311
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001312 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001313 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1314 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1315 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001316 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001317 // If we're asked to disconnect the currently connected api but
1318 // nobody is connected, it's not really an error.
1319 if (api == BufferQueueCore::NO_CONNECTED_API) {
1320 return NO_ERROR;
1321 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001322 }
1323
Dan Stoza289ade12014-02-28 11:17:17 -08001324 switch (api) {
1325 case NATIVE_WINDOW_API_EGL:
1326 case NATIVE_WINDOW_API_CPU:
1327 case NATIVE_WINDOW_API_MEDIA:
1328 case NATIVE_WINDOW_API_CAMERA:
1329 if (mCore->mConnectedApi == api) {
1330 mCore->freeAllBuffersLocked();
1331
1332 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001333 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001334 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001335 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001336 // This can fail if we're here because of the death
1337 // notification, but we just ignore it
1338 token->unlinkToDeath(
1339 static_cast<IBinder::DeathRecipient*>(this));
1340 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001341 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001342 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001343 mCore->mLinkedToDeath = nullptr;
1344 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001345 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001346 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001347 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001348 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001349 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001350 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001351 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1352 BQ_LOGE("disconnect: not connected (req=%d)", api);
1353 status = NO_INIT;
1354 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001355 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001356 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001357 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001358 }
1359 break;
1360 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001361 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001362 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001363 break;
1364 }
1365 } // Autolock scope
1366
1367 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001368 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001369 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001370 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001371 }
1372
1373 return status;
1374}
1375
Jesse Hall399184a2014-03-03 15:42:54 -08001376status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001377 sp<IConsumerListener> listener;
1378 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001379 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001380 mCore->mSidebandStream = stream;
1381 listener = mCore->mConsumerListener;
1382 } // Autolock scope
1383
Yi Kong48a619f2018-06-05 16:34:59 -07001384 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001385 listener->onSidebandStreamChanged();
1386 }
Jesse Hall399184a2014-03-03 15:42:54 -08001387 return NO_ERROR;
1388}
1389
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001390void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001391 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001392 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001393
1394 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001395 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001396 size_t newBufferCount = 0;
1397 uint32_t allocWidth = 0;
1398 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001399 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001400 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001401 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001402 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001403 std::unique_lock<std::mutex> lock(mCore->mMutex);
1404 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001405
Dan Stoza9de72932015-04-16 17:28:43 -07001406 if (!mCore->mAllowAllocation) {
1407 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1408 "BufferQueue");
1409 return;
1410 }
1411
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001412 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1413 // both allocateBuffers and dequeueBuffer.
1414 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001415 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001416 return;
1417 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001418
Antoine Labour78014f32014-07-15 21:17:03 -07001419 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1420 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001421 if (useDefaultSize && mCore->mAutoPrerotation &&
1422 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1423 std::swap(allocWidth, allocHeight);
1424 }
1425
Antoine Labour78014f32014-07-15 21:17:03 -07001426 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1427 allocUsage = usage | mCore->mConsumerUsageBits;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001428 allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001429
1430 mCore->mIsAllocating = true;
1431 } // Autolock scope
1432
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001433 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001434 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001435 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001436 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001437 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001438
1439 status_t result = graphicBuffer->initCheck();
1440
Antoine Labour78014f32014-07-15 21:17:03 -07001441 if (result != NO_ERROR) {
1442 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001443 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001444 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001445 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001446 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001447 return;
1448 }
1449 buffers.push_back(graphicBuffer);
1450 }
1451
1452 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001453 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001454 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1455 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001456 if (useDefaultSize && mCore->mAutoPrerotation &&
1457 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1458 std::swap(checkWidth, checkHeight);
1459 }
1460
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001461 PixelFormat checkFormat = format != 0 ?
1462 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001463 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001464 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1465 checkFormat != allocFormat || checkUsage != allocUsage) {
1466 // Something changed while we released the lock. Retry.
1467 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1468 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001469 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001470 continue;
1471 }
1472
Antoine Labour78014f32014-07-15 21:17:03 -07001473 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001474 if (mCore->mFreeSlots.empty()) {
1475 BQ_LOGV("allocateBuffers: a slot was occupied while "
1476 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001477 continue;
1478 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001479 auto slot = mCore->mFreeSlots.begin();
1480 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1481 mSlots[*slot].mGraphicBuffer = buffers[i];
1482 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001483
1484 // freeBufferLocked puts this slot on the free slots list. Since
1485 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001486 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001487
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001488 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1489 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001490
1491 // Make sure the erase is done after all uses of the slot
1492 // iterator since it will be invalid after this point.
1493 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001494 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001495
Antoine Labour78014f32014-07-15 21:17:03 -07001496 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001497 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001498 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001499
1500 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1501 // waiting anymore so it can use the buffer we just allocated.
1502 while (mDequeueWaitingForAllocation) {
1503 mDequeueWaitingForAllocationCondition.wait(lock);
1504 }
Antoine Labour78014f32014-07-15 21:17:03 -07001505 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001506 }
1507}
1508
Dan Stoza9de72932015-04-16 17:28:43 -07001509status_t BufferQueueProducer::allowAllocation(bool allow) {
1510 ATRACE_CALL();
1511 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1512
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001513 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001514 mCore->mAllowAllocation = allow;
1515 return NO_ERROR;
1516}
1517
Dan Stoza812ed062015-06-02 15:45:22 -07001518status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1519 ATRACE_CALL();
1520 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1521
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001522 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001523 mCore->mGenerationNumber = generationNumber;
1524 return NO_ERROR;
1525}
1526
Dan Stozac6f30bd2015-06-08 09:32:50 -07001527String8 BufferQueueProducer::getConsumerName() const {
1528 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001529 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001530 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1531 return mConsumerName;
1532}
1533
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001534status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001535 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001536 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001537
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001538 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001539 if (!sharedBufferMode) {
1540 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001541 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001542 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001543 return NO_ERROR;
1544}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001545
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001546status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1547 ATRACE_CALL();
1548 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1549
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001550 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001551
1552 mCore->mAutoRefresh = autoRefresh;
1553 return NO_ERROR;
1554}
1555
Dan Stoza127fc632015-06-30 13:43:32 -07001556status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1557 ATRACE_CALL();
1558 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1559
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001560 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001561 bool dequeueBufferCannotBlock =
1562 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1563 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001564 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1565 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1566 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1567 "available slots. Delta = %d", delta);
1568 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001569 }
1570
Pablo Ceballos981066c2016-02-18 12:54:37 -08001571 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001572 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1573 if (timeout > 0) {
1574 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001575 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001576
Pablo Ceballos981066c2016-02-18 12:54:37 -08001577 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001578 return NO_ERROR;
1579}
1580
Sungtak Lee3249fb62019-03-02 16:40:47 -08001581status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1582 ATRACE_CALL();
1583 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1584
1585 std::lock_guard<std::mutex> lock(mCore->mMutex);
1586 mCore->mLegacyBufferDrop = drop;
1587 return NO_ERROR;
1588}
1589
Dan Stoza50101d02016-04-07 16:53:23 -07001590status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001591 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001592 ATRACE_CALL();
1593 BQ_LOGV("getLastQueuedBuffer");
1594
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001595 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001596 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1597 *outBuffer = nullptr;
1598 *outFence = Fence::NO_FENCE;
1599 return NO_ERROR;
1600 }
1601
1602 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1603 *outFence = mLastQueueBufferFence;
1604
John Reck1a61da52016-04-28 13:18:15 -07001605 // Currently only SurfaceFlinger internally ever changes
1606 // GLConsumer's filtering mode, so we just use 'true' here as
1607 // this is slightly specialized for the current client of this API,
1608 // which does want filtering.
1609 GLConsumer::computeTransformMatrix(outTransformMatrix,
1610 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1611 mLastQueuedTransform, true /* filter */);
1612
Dan Stoza50101d02016-04-07 16:53:23 -07001613 return NO_ERROR;
1614}
1615
Brian Anderson3890c392016-07-25 12:48:08 -07001616void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1617 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001618}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001619
Brian Anderson3890c392016-07-25 12:48:08 -07001620void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001621 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001622 FrameEventHistoryDelta* outDelta) {
1623 if (newTimestamps == nullptr && outDelta == nullptr) {
1624 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001625 }
1626
1627 ATRACE_CALL();
1628 BQ_LOGV("addAndGetFrameTimestamps");
1629 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001630 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001631 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001632 listener = mCore->mConsumerListener;
1633 }
Yi Kong48a619f2018-06-05 16:34:59 -07001634 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001635 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001636 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001637}
1638
Dan Stoza289ade12014-02-28 11:17:17 -08001639void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1640 // If we're here, it means that a producer we were connected to died.
1641 // We're guaranteed that we are still connected to it because we remove
1642 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1643 // without synchronization here.
1644 int api = mCore->mConnectedApi;
1645 disconnect(api);
1646}
1647
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001648status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1649 BQ_LOGV("getUniqueId");
1650
1651 *outId = mCore->mUniqueId;
1652 return NO_ERROR;
1653}
1654
Chia-I Wue2786ea2017-08-07 10:36:08 -07001655status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1656 BQ_LOGV("getConsumerUsage");
1657
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001658 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001659 *outUsage = mCore->mConsumerUsageBits;
1660 return NO_ERROR;
1661}
1662
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001663status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1664 ATRACE_CALL();
1665 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1666
1667 std::lock_guard<std::mutex> lock(mCore->mMutex);
1668
1669 mCore->mAutoPrerotation = autoPrerotation;
1670 return NO_ERROR;
1671}
1672
Dan Stoza289ade12014-02-28 11:17:17 -08001673} // namespace android