blob: cf5ad7b54f5c7fb224b6e56fbe6b9d9f514232d5 [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>
Vishnu Nair14a3c112023-04-21 14:49:47 -070038#include <gui/TraceUtils.h>
Jayant Chowdharyad9fe272019-03-07 22:36:06 -080039#include <private/gui/BufferQueueThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080040
41#include <utils/Log.h>
42#include <utils/Trace.h>
43
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070044#include <system/window.h>
45
Dan Stoza289ade12014-02-28 11:17:17 -080046namespace android {
47
Iris Chang430193f2019-12-04 16:25:46 +080048// Macros for include BufferQueueCore information in log messages
49#define BQ_LOGV(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000050 ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080051 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
52 ##__VA_ARGS__)
53#define BQ_LOGD(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000054 ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080055 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
56 ##__VA_ARGS__)
57#define BQ_LOGI(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000058 ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080059 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
60 ##__VA_ARGS__)
61#define BQ_LOGW(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000062 ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080063 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
64 ##__VA_ARGS__)
65#define BQ_LOGE(x, ...) \
Tomasz Wasilczykf2add402023-08-11 00:06:51 +000066 ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \
Iris Chang430193f2019-12-04 16:25:46 +080067 mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
68 ##__VA_ARGS__)
69
Craig Donner6ebc46a2016-10-21 15:23:44 -070070static constexpr uint32_t BQ_LAYER_COUNT = 1;
Chong Zhang62493092020-01-15 16:04:47 -080071ProducerListener::~ProducerListener() = default;
Craig Donner6ebc46a2016-10-21 15:23:44 -070072
Irvel468051e2016-06-13 16:44:44 -070073BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
74 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080075 mCore(core),
76 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070077 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070078 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070079 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080080 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070081 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080082 mCallbackMutex(),
83 mNextCallbackTicket(0),
84 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070085 mCallbackCondition(),
Jorim Jaggi35b4e382019-03-28 00:44:03 +010086 mDequeueTimeout(-1),
87 mDequeueWaitingForAllocation(false) {}
Dan Stoza289ade12014-02-28 11:17:17 -080088
89BufferQueueProducer::~BufferQueueProducer() {}
90
91status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
92 ATRACE_CALL();
93 BQ_LOGV("requestBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +020094 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -080095
96 if (mCore->mIsAbandoned) {
97 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
98 return NO_INIT;
99 }
100
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700101 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
102 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
103 return NO_INIT;
104 }
105
Dan Stoza3e96f192014-03-03 10:16:19 -0800106 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800107 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -0800108 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -0800109 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700110 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800111 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700112 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -0800113 return BAD_VALUE;
114 }
115
116 mSlots[slot].mRequestBufferCalled = true;
117 *buf = mSlots[slot].mGraphicBuffer;
118 return NO_ERROR;
119}
120
Pablo Ceballosfa455352015-08-12 17:47:47 -0700121status_t BufferQueueProducer::setMaxDequeuedBufferCount(
122 int maxDequeuedBuffers) {
Brian Lindahlc794b692023-01-31 15:42:47 -0700123 int maxBufferCount;
124 return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
125}
126
127status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
128 int* maxBufferCount) {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700129 ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
131 maxDequeuedBuffers);
132
Pablo Ceballos981066c2016-02-18 12:54:37 -0800133 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700134 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200135 std::unique_lock<std::mutex> lock(mCore->mMutex);
136 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700137
138 if (mCore->mIsAbandoned) {
139 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
140 "abandoned");
141 return NO_INIT;
142 }
143
Brian Lindahlc794b692023-01-31 15:42:47 -0700144 *maxBufferCount = mCore->getMaxBufferCountLocked();
145
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700146 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
147 return NO_ERROR;
148 }
149
Pablo Ceballos72daab62015-12-07 16:38:43 -0800150 // The new maxDequeuedBuffer count should not be violated by the number
151 // of currently dequeued buffers
152 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800153 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700154 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700156 }
157 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800158 if (dequeuedCount > maxDequeuedBuffers) {
159 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
160 "count (%d) exceeds the current dequeued buffer count (%d)",
161 maxDequeuedBuffers, dequeuedCount);
162 return BAD_VALUE;
163 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700164
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700165 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 bufferCount += maxDequeuedBuffers;
167
168 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
169 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
170 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
171 return BAD_VALUE;
172 }
173
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700174 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700175 if (bufferCount < minBufferSlots) {
176 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
177 "less than minimum %d", bufferCount, minBufferSlots);
178 return BAD_VALUE;
179 }
180
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700181 if (bufferCount > mCore->mMaxBufferCount) {
182 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700183 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
184 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
185 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
186 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 return BAD_VALUE;
188 }
189
Pablo Ceballos72daab62015-12-07 16:38:43 -0800190 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800191 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800192 return BAD_VALUE;
193 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700194 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Brian Lindahlc794b692023-01-31 15:42:47 -0700195 *maxBufferCount = mCore->getMaxBufferCountLocked();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800196 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800197 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800198 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800199 }
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200200 mCore->mDequeueCondition.notify_all();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700201 } // Autolock scope
202
203 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700204 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800205 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700206 }
207
208 return NO_ERROR;
209}
210
211status_t BufferQueueProducer::setAsyncMode(bool async) {
212 ATRACE_CALL();
213 BQ_LOGV("setAsyncMode: async = %d", async);
214
Pablo Ceballos981066c2016-02-18 12:54:37 -0800215 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200217 std::unique_lock<std::mutex> lock(mCore->mMutex);
218 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700219
220 if (mCore->mIsAbandoned) {
221 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
222 return NO_INIT;
223 }
224
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700225 if (async == mCore->mAsyncMode) {
226 return NO_ERROR;
227 }
228
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700229 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700230 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
231 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700232 BQ_LOGE("setAsyncMode(%d): this call would cause the "
233 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700234 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
235 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
236 mCore->mMaxDequeuedBufferCount,
237 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700238 return BAD_VALUE;
239 }
240
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800241 int delta = mCore->getMaxBufferCountLocked(async,
242 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
243 - mCore->getMaxBufferCountLocked();
244
Pablo Ceballos981066c2016-02-18 12:54:37 -0800245 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800246 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
247 "available slots. Delta = %d", delta);
248 return BAD_VALUE;
249 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700250 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800251 VALIDATE_CONSISTENCY();
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200252 mCore->mDequeueCondition.notify_all();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700253 if (delta < 0) {
254 listener = mCore->mConsumerListener;
255 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700256 } // Autolock scope
257
258 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -0700259 if (listener != nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800260 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700261 }
262 return NO_ERROR;
263}
264
Dan Stoza5ecfb682016-01-04 17:01:02 -0800265int BufferQueueProducer::getFreeBufferLocked() const {
266 if (mCore->mFreeBuffers.empty()) {
267 return BufferQueueCore::INVALID_BUFFER_SLOT;
268 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800269 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800270 mCore->mFreeBuffers.pop_front();
271 return slot;
272}
273
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800274int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800275 if (mCore->mFreeSlots.empty()) {
276 return BufferQueueCore::INVALID_BUFFER_SLOT;
277 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800278 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800279 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800280 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800281}
282
283status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200284 std::unique_lock<std::mutex>& lock, int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800285 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
286 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800287 bool tryAgain = true;
288 while (tryAgain) {
289 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800291 return NO_INIT;
292 }
293
Dan Stoza9f3053d2014-03-06 15:14:33 -0800294 int dequeuedCount = 0;
295 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800296 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700297 if (mSlots[s].mBufferState.isDequeued()) {
298 ++dequeuedCount;
299 }
300 if (mSlots[s].mBufferState.isAcquired()) {
301 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800302 }
303 }
304
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700305 // Producers are not allowed to dequeue more than
306 // mMaxDequeuedBufferCount buffers.
307 // This check is only done if a buffer has already been queued
308 if (mCore->mBufferHasBeenQueued &&
309 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
Sungtak Leeaf141242019-04-24 16:36:44 -0700310 // Supress error logs when timeout is non-negative.
311 if (mDequeueTimeout < 0) {
312 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
313 "count (%d)", callerString,
314 mCore->mMaxDequeuedBufferCount);
315 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800316 return INVALID_OPERATION;
317 }
318
Dan Stoza0de7ea72015-04-23 13:20:51 -0700319 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
320
Dan Stozaae3c3682014-04-18 15:43:35 -0700321 // If we disconnect and reconnect quickly, we can be in a state where
322 // our slots are empty but we have many buffers in the queue. This can
323 // cause us to run out of memory if we outrun the consumer. Wait here if
324 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800325 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700326 bool tooManyBuffers = mCore->mQueue.size()
327 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700328 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800329 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700330 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700331 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700332 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700333 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700335 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700336 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800337 } else {
338 if (caller == FreeSlotCaller::Dequeue) {
339 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800340 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800341 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
342 *found = slot;
343 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800344 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800345 }
346 } else {
347 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800348 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800349 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
350 *found = slot;
351 } else {
352 *found = getFreeBufferLocked();
353 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700354 }
355 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700356 }
357
358 // If no buffer is found, or if the queue has too many buffers
359 // outstanding, wait for a buffer to be acquired or released, or for the
360 // max buffer count to change.
361 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
362 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800363 if (tryAgain) {
364 // Return an error if we're in non-blocking mode (producer and
365 // consumer are controlled by the application).
366 // However, the consumer is allowed to briefly acquire an extra
367 // buffer (which could cause us to have to wait here), which is
368 // okay, since it is only used to implement an atomic acquire +
369 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700370 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800371 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
372 return WOULD_BLOCK;
373 }
Dan Stoza127fc632015-06-30 13:43:32 -0700374 if (mDequeueTimeout >= 0) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200375 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
376 std::chrono::nanoseconds(mDequeueTimeout));
377 if (result == std::cv_status::timeout) {
378 return TIMED_OUT;
Dan Stoza127fc632015-06-30 13:43:32 -0700379 }
380 } else {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200381 mCore->mDequeueCondition.wait(lock);
Dan Stoza127fc632015-06-30 13:43:32 -0700382 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800383 }
384 } // while (tryAgain)
385
386 return NO_ERROR;
387}
388
Ian Elliottd11b0442017-07-18 11:05:49 -0600389status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
390 uint32_t width, uint32_t height, PixelFormat format,
391 uint64_t usage, uint64_t* outBufferAge,
392 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800393 ATRACE_CALL();
394 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200395 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800396 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700397
398 if (mCore->mIsAbandoned) {
399 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
400 return NO_INIT;
401 }
402
403 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
404 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
405 return NO_INIT;
406 }
Dan Stoza289ade12014-02-28 11:17:17 -0800407 } // Autolock scope
408
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700409 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800410
411 if ((width && !height) || (!width && height)) {
412 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
413 return BAD_VALUE;
414 }
415
416 status_t returnFlags = NO_ERROR;
417 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
418 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800419 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800420
421 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200422 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800423
Jorim Jaggi35b4e382019-03-28 00:44:03 +0100424 // If we don't have a free buffer, but we are currently allocating, we wait until allocation
425 // is finished such that we don't allocate in parallel.
426 if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
427 mDequeueWaitingForAllocation = true;
428 mCore->waitWhileAllocatingLocked(lock);
429 mDequeueWaitingForAllocation = false;
430 mDequeueWaitingForAllocationCondition.notify_all();
431 }
Dan Stoza289ade12014-02-28 11:17:17 -0800432
433 if (format == 0) {
434 format = mCore->mDefaultBufferFormat;
435 }
436
437 // Enable the usage bits the consumer requested
438 usage |= mCore->mConsumerUsageBits;
439
Dan Stoza9de72932015-04-16 17:28:43 -0700440 const bool useDefaultSize = !width && !height;
441 if (useDefaultSize) {
442 width = mCore->mDefaultWidth;
443 height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -0700444 if (mCore->mAutoPrerotation &&
445 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
446 std::swap(width, height);
447 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448 }
Dan Stoza289ade12014-02-28 11:17:17 -0800449
Pablo Ceballos981066c2016-02-18 12:54:37 -0800450 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700451 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200452 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700453 if (status != NO_ERROR) {
454 return status;
455 }
456
457 // This should not happen
458 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
459 BQ_LOGE("dequeueBuffer: no available buffer slots");
460 return -EBUSY;
461 }
462
463 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
464
465 // If we are not allowed to allocate new buffers,
466 // waitForFreeSlotThenRelock must have returned a slot containing a
467 // buffer. If this buffer would require reallocation to meet the
468 // requested attributes, we free it and attempt to get another one.
469 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700470 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700471 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700472 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700473 return BAD_VALUE;
474 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800475 mCore->mFreeSlots.insert(found);
476 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700477 found = BufferItem::INVALID_BUFFER_SLOT;
478 continue;
479 }
480 }
Dan Stoza289ade12014-02-28 11:17:17 -0800481 }
482
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800483 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700484 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700485 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800486 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
487 "buffer");
488
489 return BAD_VALUE;
490 }
491
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700492 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800493 mCore->mActiveBuffers.insert(found);
494 }
Dan Stoza289ade12014-02-28 11:17:17 -0800495 *outSlot = found;
496 ATRACE_BUFFER_INDEX(found);
497
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800498 attachedByConsumer = mSlots[found].mNeedsReallocation;
499 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800500
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700501 mSlots[found].mBufferState.dequeue();
502
Yi Kong48a619f2018-06-05 16:34:59 -0700503 if ((buffer == nullptr) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700504 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800505 {
Vishnu Nair14a3c112023-04-21 14:49:47 -0700506 if (CC_UNLIKELY(ATRACE_ENABLED())) {
507 if (buffer == nullptr) {
508 ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.string());
509 } else {
510 ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
511 "layerCount:%d "
512 "usage:%d requested: %dx%d format:%d layerCount:%d "
513 "usage:%d ",
514 mConsumerName.string(), width, height, format,
515 BQ_LAYER_COUNT, usage, buffer->getWidth(),
516 buffer->getHeight(), buffer->getPixelFormat(),
517 buffer->getLayerCount(), buffer->getUsage());
518 }
519 }
Dan Stoza289ade12014-02-28 11:17:17 -0800520 mSlots[found].mAcquireCalled = false;
Yi Kong48a619f2018-06-05 16:34:59 -0700521 mSlots[found].mGraphicBuffer = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -0800522 mSlots[found].mRequestBufferCalled = false;
523 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
524 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
525 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800526 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700527 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800528
529 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800530 } else {
531 // We add 1 because that will be the frame number when this buffer
532 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700533 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800534 }
535
Dan Stoza800b41a2015-04-28 14:20:04 -0700536 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
537 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800538
Yi Kong48a619f2018-06-05 16:34:59 -0700539 if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800540 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
541 "slot=%d w=%d h=%d format=%u",
542 found, buffer->width, buffer->height, buffer->format);
543 }
544
545 eglDisplay = mSlots[found].mEglDisplay;
546 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700547 // Don't return a fence in shared buffer mode, except for the first
548 // frame.
549 *outFence = (mCore->mSharedBufferMode &&
550 mCore->mSharedBufferSlot == found) ?
551 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800552 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
553 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700554
555 // If shared buffer mode has just been enabled, cache the slot of the
556 // first buffer that is dequeued and mark it as the shared buffer.
557 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
558 BufferQueueCore::INVALID_BUFFER_SLOT) {
559 mCore->mSharedBufferSlot = found;
560 mSlots[found].mBufferState.mShared = true;
561 }
Adithya Srinivasana820af92019-11-01 13:55:17 -0700562
563 if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
564 if (mCore->mConsumerListener != nullptr) {
565 mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId());
566 }
567 }
Dan Stoza289ade12014-02-28 11:17:17 -0800568 } // Autolock scope
569
570 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700571 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000572 sp<GraphicBuffer> graphicBuffer =
573 new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage,
574 {mConsumerName.c_str(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -0700575
576 status_t error = graphicBuffer->initCheck();
577
Dan Stoza289ade12014-02-28 11:17:17 -0800578 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200579 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800580
Chia-I Wufeec3b12017-05-25 09:34:56 -0700581 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700582 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
583 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
Adithya Srinivasana820af92019-11-01 13:55:17 -0700584 if (mCore->mConsumerListener != nullptr) {
585 mCore->mConsumerListener->onFrameDequeued(
586 mSlots[*outSlot].mGraphicBuffer->getId());
587 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588 }
589
590 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200591 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700592
Chia-I Wufeec3b12017-05-25 09:34:56 -0700593 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700594 mCore->mFreeSlots.insert(*outSlot);
595 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700596 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
597 return error;
598 }
599
Dan Stoza289ade12014-02-28 11:17:17 -0800600 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700601 mCore->mFreeSlots.insert(*outSlot);
602 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800603 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
604 return NO_INIT;
605 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800606
Pablo Ceballos9e314332016-01-12 13:49:19 -0800607 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800608 } // Autolock scope
609 }
610
Dan Stoza9f3053d2014-03-06 15:14:33 -0800611 if (attachedByConsumer) {
612 returnFlags |= BUFFER_NEEDS_REALLOCATION;
613 }
614
Dan Stoza289ade12014-02-28 11:17:17 -0800615 if (eglFence != EGL_NO_SYNC_KHR) {
616 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
617 1000000000);
618 // If something goes wrong, log the error, but return the buffer without
619 // synchronizing access to it. It's too late at this point to abort the
620 // dequeue operation.
621 if (result == EGL_FALSE) {
622 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
623 eglGetError());
624 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
625 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
626 }
627 eglDestroySyncKHR(eglDisplay, eglFence);
628 }
629
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700630 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
631 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800632 mSlots[*outSlot].mFrameNumber,
tangcheng5614ca02022-04-07 14:26:05 +0800633 mSlots[*outSlot].mGraphicBuffer != nullptr ?
634 mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
Dan Stoza289ade12014-02-28 11:17:17 -0800635
Ian Elliottd11b0442017-07-18 11:05:49 -0600636 if (outBufferAge) {
637 *outBufferAge = mCore->mBufferAge;
638 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700639 addAndGetFrameTimestamps(nullptr, outTimestamps);
640
Dan Stoza289ade12014-02-28 11:17:17 -0800641 return returnFlags;
642}
643
Dan Stoza9f3053d2014-03-06 15:14:33 -0800644status_t BufferQueueProducer::detachBuffer(int slot) {
645 ATRACE_CALL();
646 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700647 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800648
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700649 sp<IConsumerListener> listener;
650 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200651 std::lock_guard<std::mutex> lock(mCore->mMutex);
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700652
653 if (mCore->mIsAbandoned) {
654 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
655 return NO_INIT;
656 }
657
658 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
659 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
660 return NO_INIT;
661 }
662
663 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
664 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
665 return BAD_VALUE;
666 }
667
668 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
669 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
670 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
671 return BAD_VALUE;
672 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Darwin Huang29936fa2021-09-08 18:29:18 +0000673 // TODO(http://b/140581935): This message is BQ_LOGW because it
674 // often logs when no actionable errors are present. Return to
675 // using BQ_LOGE after ensuring this only logs during errors.
676 BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700677 "(state = %s)", slot, mSlots[slot].mBufferState.string());
678 return BAD_VALUE;
679 } else if (!mSlots[slot].mRequestBufferCalled) {
680 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
681 slot);
682 return BAD_VALUE;
683 }
684
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700685 listener = mCore->mConsumerListener;
Robert Carrfc069ba2020-04-20 13:26:31 -0700686 auto gb = mSlots[slot].mGraphicBuffer;
687 if (listener != nullptr && gb != nullptr) {
688 listener->onFrameDetached(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700689 }
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700690 mSlots[slot].mBufferState.detachProducer();
691 mCore->mActiveBuffers.erase(slot);
692 mCore->mFreeSlots.insert(slot);
693 mCore->clearBufferSlotLocked(slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200694 mCore->mDequeueCondition.notify_all();
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700695 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696 }
697
Yi Kong48a619f2018-06-05 16:34:59 -0700698 if (listener != nullptr) {
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700699 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700700 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800701
Dan Stoza9f3053d2014-03-06 15:14:33 -0800702 return NO_ERROR;
703}
704
Dan Stozad9822a32014-03-28 15:25:31 -0700705status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
706 sp<Fence>* outFence) {
707 ATRACE_CALL();
708
Yi Kong48a619f2018-06-05 16:34:59 -0700709 if (outBuffer == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700710 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
711 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700712 } else if (outFence == nullptr) {
Dan Stozad9822a32014-03-28 15:25:31 -0700713 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
714 return BAD_VALUE;
715 }
716
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700717 sp<IConsumerListener> listener;
718 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200719 std::unique_lock<std::mutex> lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700720
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700721 if (mCore->mIsAbandoned) {
722 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
723 return NO_INIT;
724 }
725
726 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
727 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
728 return NO_INIT;
729 }
730
731 if (mCore->mSharedBufferMode) {
732 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
733 "mode");
734 return BAD_VALUE;
735 }
736
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200737 mCore->waitWhileAllocatingLocked(lock);
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700738
739 if (mCore->mFreeBuffers.empty()) {
740 return NO_MEMORY;
741 }
742
743 int found = mCore->mFreeBuffers.front();
744 mCore->mFreeBuffers.remove(found);
745 mCore->mFreeSlots.insert(found);
746
747 BQ_LOGV("detachNextBuffer detached slot %d", found);
748
749 *outBuffer = mSlots[found].mGraphicBuffer;
750 *outFence = mSlots[found].mFence;
751 mCore->clearBufferSlotLocked(found);
752 VALIDATE_CONSISTENCY();
753 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700754 }
755
Yi Kong48a619f2018-06-05 16:34:59 -0700756 if (listener != nullptr) {
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700757 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700758 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800759
Dan Stozad9822a32014-03-28 15:25:31 -0700760 return NO_ERROR;
761}
762
Dan Stoza9f3053d2014-03-06 15:14:33 -0800763status_t BufferQueueProducer::attachBuffer(int* outSlot,
764 const sp<android::GraphicBuffer>& buffer) {
765 ATRACE_CALL();
766
Yi Kong48a619f2018-06-05 16:34:59 -0700767 if (outSlot == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700768 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800769 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -0700770 } else if (buffer == nullptr) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700771 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800772 return BAD_VALUE;
773 }
774
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200775 std::unique_lock<std::mutex> lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700776
777 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700778 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700779 return NO_INIT;
780 }
781
782 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700783 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700784 return NO_INIT;
785 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800786
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700787 if (mCore->mSharedBufferMode) {
788 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700789 return BAD_VALUE;
790 }
791
Dan Stoza812ed062015-06-02 15:45:22 -0700792 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
793 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
794 "[queue %u]", buffer->getGenerationNumber(),
795 mCore->mGenerationNumber);
796 return BAD_VALUE;
797 }
798
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200799 mCore->waitWhileAllocatingLocked(lock);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700800
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801 status_t returnFlags = NO_ERROR;
802 int found;
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200803 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800804 if (status != NO_ERROR) {
805 return status;
806 }
807
808 // This should not happen
809 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700810 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800811 return -EBUSY;
812 }
813
814 *outSlot = found;
815 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700816 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800817 *outSlot, returnFlags);
818
819 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700820 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800821 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
822 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700823 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800824 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800825 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800826 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800827 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700828
Dan Stoza9f3053d2014-03-06 15:14:33 -0800829 return returnFlags;
830}
831
Dan Stoza289ade12014-02-28 11:17:17 -0800832status_t BufferQueueProducer::queueBuffer(int slot,
833 const QueueBufferInput &input, QueueBufferOutput *output) {
834 ATRACE_CALL();
835 ATRACE_BUFFER_INDEX(slot);
836
Brian Andersond6927fb2016-07-23 23:37:30 -0700837 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800838 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800839 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700840 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800841 int scalingMode;
842 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700843 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700844 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700845 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700846 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700847 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
848 &getFrameTimestamps);
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700849 const Region& surfaceDamage = input.getSurfaceDamage();
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700850 const HdrMetadata& hdrMetadata = input.getHdrMetadata();
Dan Stoza289ade12014-02-28 11:17:17 -0800851
Yi Kong48a619f2018-06-05 16:34:59 -0700852 if (acquireFence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -0800853 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800854 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800855 }
856
Brian Anderson3d4039d2016-09-23 16:31:30 -0700857 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
858
Dan Stoza289ade12014-02-28 11:17:17 -0800859 switch (scalingMode) {
860 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
861 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
862 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
863 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
864 break;
865 default:
866 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800867 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800868 }
869
Dan Stoza8dc55392014-11-04 11:37:46 -0800870 sp<IConsumerListener> frameAvailableListener;
871 sp<IConsumerListener> frameReplacedListener;
872 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700873 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800874 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800875 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +0200876 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800877
878 if (mCore->mIsAbandoned) {
879 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
880 return NO_INIT;
881 }
882
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700883 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
884 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
885 return NO_INIT;
886 }
887
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800888 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800889 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800890 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800891 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700892 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800893 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700894 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800895 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800896 } else if (!mSlots[slot].mRequestBufferCalled) {
897 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
898 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800899 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800900 }
901
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700902 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700903 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700904 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700905 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700906 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700907 mSlots[slot].mBufferState.mShared = true;
908 }
909
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800910 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700911 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
912 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
913 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
Brian Andersond6927fb2016-07-23 23:37:30 -0700914 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800915 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800916
917 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
918 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700919 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800920 crop.intersect(bufferRect, &croppedRect);
921 if (croppedRect != crop) {
922 BQ_LOGE("queueBuffer: crop rect is not contained within the "
923 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800924 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800925 }
926
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800927 // Override UNKNOWN dataspace with consumer default
928 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
929 dataSpace = mCore->mDefaultBufferDataSpace;
930 }
931
Brian Andersond6927fb2016-07-23 23:37:30 -0700932 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700933 mSlots[slot].mBufferState.queue();
934
Brian Andersond6927fb2016-07-23 23:37:30 -0700935 // Increment the frame counter and store a local version of it
936 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800937 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700938 currentFrameNumber = mCore->mFrameCounter;
939 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800940
Dan Stoza289ade12014-02-28 11:17:17 -0800941 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
942 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
943 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800944 item.mTransform = transform &
945 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800946 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800947 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
948 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700949 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800950 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800951 item.mDataSpace = dataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700952 item.mHdrMetadata = hdrMetadata;
Brian Andersond6927fb2016-07-23 23:37:30 -0700953 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800954 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700955 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700956 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700957 item.mIsDroppable = mCore->mAsyncMode ||
Sungtak Lee45e9e0b2019-05-28 13:38:23 -0700958 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
Sungtak Lee3249fb62019-03-02 16:40:47 -0800959 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700960 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700961 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700962 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700963 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800964 item.mApi = mCore->mConnectedApi;
Dan Stoza289ade12014-02-28 11:17:17 -0800965
Ruben Brunk1681d952014-06-27 15:51:55 -0700966 mStickyTransform = stickyTransform;
967
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700968 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700969 if (mCore->mSharedBufferMode) {
970 mCore->mSharedBufferCache.crop = crop;
971 mCore->mSharedBufferCache.transform = transform;
972 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700973 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700974 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700975 }
976
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800977 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800978 if (mCore->mQueue.empty()) {
979 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
980 // and simply queue this buffer
981 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800982 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800983 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700984 // When the queue is not empty, we need to look at the last buffer
985 // in the queue to see if we need to replace it
986 const BufferItem& last = mCore->mQueue.itemAt(
987 mCore->mQueue.size() - 1);
988 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800989
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700990 if (!last.mIsStale) {
991 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700992
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700993 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700994 // still be around. Mark it as no longer shared if this
995 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700996 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700997 mSlots[last.mSlot].mBufferState.isFree()) {
998 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700999 }
1000 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001001 if (!mSlots[last.mSlot].mBufferState.isShared()) {
1002 mCore->mActiveBuffers.erase(last.mSlot);
1003 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001004 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001005 }
Dan Stoza289ade12014-02-28 11:17:17 -08001006 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001007
Steven Thomas862e7532019-07-10 19:21:03 -07001008 // Make sure to merge the damage rect from the frame we're about
1009 // to drop into the new frame's damage rect.
1010 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1011 item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1012 item.mSurfaceDamage = Region::INVALID_REGION;
1013 } else {
1014 item.mSurfaceDamage |= last.mSurfaceDamage;
1015 }
1016
Dan Stoza289ade12014-02-28 11:17:17 -08001017 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -07001018 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -08001019 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001020 } else {
1021 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -08001022 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -08001023 }
1024 }
1025
1026 mCore->mBufferHasBeenQueued = true;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001027 mCore->mDequeueCondition.notify_all();
Dan Stoza50101d02016-04-07 16:53:23 -07001028 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -08001029
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001030 output->width = mCore->mDefaultWidth;
1031 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001032 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001033 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1034 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001035
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001036 ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
Chong Zhang62493092020-01-15 16:04:47 -08001037#ifndef NO_BINDER
Dan Stozae77c7662016-05-13 11:37:28 -07001038 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Chong Zhang62493092020-01-15 16:04:47 -08001039#endif
Dan Stoza8dc55392014-11-04 11:37:46 -08001040 // Take a ticket for the callback functions
1041 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001042
Pablo Ceballos9e314332016-01-12 13:49:19 -08001043 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001044 } // Autolock scope
1045
Irvel468051e2016-06-13 16:44:44 -07001046 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1047 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1048 // there will be no Binder call
1049 if (!mConsumerIsSurfaceFlinger) {
1050 item.mGraphicBuffer.clear();
1051 }
1052
JihCheng Chiu544c5222019-04-02 20:50:58 +08001053 // Update and get FrameEventHistory.
1054 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1055 NewFrameEventsEntry newFrameEventsEntry = {
1056 currentFrameNumber,
1057 postedTime,
1058 requestedPresentTimestamp,
1059 std::move(acquireFenceTime)
1060 };
1061 addAndGetFrameTimestamps(&newFrameEventsEntry,
1062 getFrameTimestamps ? &output->frameTimestamps : nullptr);
1063
Dan Stoza8dc55392014-11-04 11:37:46 -08001064 // Call back without the main BufferQueue lock held, but with the callback
1065 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001066
1067 int connectedApi;
1068 sp<Fence> lastQueuedFence;
1069
1070 { // scope for the lock
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001071 std::unique_lock<std::mutex> lock(mCallbackMutex);
Dan Stoza8dc55392014-11-04 11:37:46 -08001072 while (callbackTicket != mCurrentCallbackTicket) {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001073 mCallbackCondition.wait(lock);
Dan Stoza8dc55392014-11-04 11:37:46 -08001074 }
1075
Yi Kong48a619f2018-06-05 16:34:59 -07001076 if (frameAvailableListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001077 frameAvailableListener->onFrameAvailable(item);
Yi Kong48a619f2018-06-05 16:34:59 -07001078 } else if (frameReplacedListener != nullptr) {
Dan Stoza8dc55392014-11-04 11:37:46 -08001079 frameReplacedListener->onFrameReplaced(item);
1080 }
1081
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -07001082 connectedApi = mCore->mConnectedApi;
1083 lastQueuedFence = std::move(mLastQueueBufferFence);
1084
1085 mLastQueueBufferFence = std::move(acquireFence);
1086 mLastQueuedCrop = item.mCrop;
1087 mLastQueuedTransform = item.mTransform;
1088
Dan Stoza8dc55392014-11-04 11:37:46 -08001089 ++mCurrentCallbackTicket;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001090 mCallbackCondition.notify_all();
Dan Stoza289ade12014-02-28 11:17:17 -08001091 }
1092
Yiwei Zhangcb7dd002019-04-16 11:03:01 -07001093 // Wait without lock held
1094 if (connectedApi == NATIVE_WINDOW_API_EGL) {
1095 // Waiting here allows for two full buffers to be queued but not a
1096 // third. In the event that frames take varying time, this makes a
1097 // small trade-off in favor of latency rather than throughput.
1098 lastQueuedFence->waitForever("Throttling EGL Production");
1099 }
1100
Dan Stoza289ade12014-02-28 11:17:17 -08001101 return NO_ERROR;
1102}
1103
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001104status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001105 ATRACE_CALL();
1106 BQ_LOGV("cancelBuffer: slot %d", slot);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001107 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001108
1109 if (mCore->mIsAbandoned) {
1110 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001111 return NO_INIT;
1112 }
1113
1114 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1115 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1116 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001117 }
1118
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001119 if (mCore->mSharedBufferMode) {
1120 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001121 return BAD_VALUE;
1122 }
1123
Dan Stoza3e96f192014-03-03 10:16:19 -08001124 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001125 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001126 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001127 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001128 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001129 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001130 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001131 return BAD_VALUE;
Yi Kong48a619f2018-06-05 16:34:59 -07001132 } else if (fence == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001133 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001134 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001135 }
1136
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001137 mSlots[slot].mBufferState.cancel();
1138
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001139 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001140 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001141 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001142 mSlots[slot].mBufferState.mShared = false;
1143 }
1144
1145 // Don't put the shared buffer on the free list.
1146 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001147 mCore->mActiveBuffers.erase(slot);
1148 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001149 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001150
Robert Carra0aeedc2020-04-20 15:09:35 -07001151 auto gb = mSlots[slot].mGraphicBuffer;
1152 if (mCore->mConsumerListener != nullptr && gb != nullptr) {
1153 mCore->mConsumerListener->onFrameCancelled(gb->getId());
Adithya Srinivasan2e434382019-10-09 11:43:01 -07001154 }
Dan Stoza289ade12014-02-28 11:17:17 -08001155 mSlots[slot].mFence = fence;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001156 mCore->mDequeueCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001157 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001158
1159 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001160}
1161
1162int BufferQueueProducer::query(int what, int *outValue) {
1163 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001164 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -08001165
Yi Kong48a619f2018-06-05 16:34:59 -07001166 if (outValue == nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001167 BQ_LOGE("query: outValue was NULL");
1168 return BAD_VALUE;
1169 }
1170
1171 if (mCore->mIsAbandoned) {
1172 BQ_LOGE("query: BufferQueue has been abandoned");
1173 return NO_INIT;
1174 }
1175
1176 int value;
1177 switch (what) {
1178 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001179 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001180 break;
1181 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001182 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001183 break;
1184 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001185 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001186 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001187 case NATIVE_WINDOW_LAYER_COUNT:
1188 // All BufferQueue buffers have a single layer.
1189 value = BQ_LAYER_COUNT;
1190 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001191 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001192 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001193 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001194 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001195 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001196 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001197 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1198 value = (mCore->mQueue.size() > 1);
1199 break;
1200 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Chia-I Wue2786ea2017-08-07 10:36:08 -07001201 // deprecated; higher 32 bits are truncated
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001202 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001203 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001204 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1205 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1206 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001207 case NATIVE_WINDOW_BUFFER_AGE:
1208 if (mCore->mBufferAge > INT32_MAX) {
1209 value = 0;
1210 } else {
1211 value = static_cast<int32_t>(mCore->mBufferAge);
1212 }
1213 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001214 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1215 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1216 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001217 default:
1218 return BAD_VALUE;
1219 }
1220
1221 BQ_LOGV("query: %d? %d", what, value);
1222 *outValue = value;
1223 return NO_ERROR;
1224}
1225
Dan Stozaf0eaf252014-03-21 13:05:51 -07001226status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001227 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1228 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001229 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos981066c2016-02-18 12:54:37 -08001230 mConsumerName = mCore->mConsumerName;
1231 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1232 producerControlledByApp ? "true" : "false");
1233
1234 if (mCore->mIsAbandoned) {
1235 BQ_LOGE("connect: BufferQueue has been abandoned");
1236 return NO_INIT;
1237 }
1238
Yi Kong48a619f2018-06-05 16:34:59 -07001239 if (mCore->mConsumerListener == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001240 BQ_LOGE("connect: BufferQueue has no consumer");
1241 return NO_INIT;
1242 }
1243
Yi Kong48a619f2018-06-05 16:34:59 -07001244 if (output == nullptr) {
Pablo Ceballos981066c2016-02-18 12:54:37 -08001245 BQ_LOGE("connect: output was NULL");
1246 return BAD_VALUE;
1247 }
1248
1249 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1250 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1251 mCore->mConnectedApi, api);
1252 return BAD_VALUE;
1253 }
1254
1255 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1256 mDequeueTimeout < 0 ?
1257 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1258 mCore->mMaxBufferCount) -
1259 mCore->getMaxBufferCountLocked();
1260 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1261 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1262 "slots. Delta = %d", delta);
1263 return BAD_VALUE;
1264 }
1265
Dan Stoza289ade12014-02-28 11:17:17 -08001266 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001267 switch (api) {
1268 case NATIVE_WINDOW_API_EGL:
1269 case NATIVE_WINDOW_API_CPU:
1270 case NATIVE_WINDOW_API_MEDIA:
1271 case NATIVE_WINDOW_API_CAMERA:
1272 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001273
1274 output->width = mCore->mDefaultWidth;
1275 output->height = mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001276 output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001277 output->numPendingBuffers =
1278 static_cast<uint32_t>(mCore->mQueue.size());
1279 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001280 output->bufferReplaced = false;
silence_dogoode9d092a2019-06-19 16:14:53 -07001281 output->maxBufferCount = mCore->mMaxBufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -08001282
Yi Kong48a619f2018-06-05 16:34:59 -07001283 if (listener != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001284 // Set up a death notification so that we can disconnect
1285 // automatically if the remote producer dies
Chong Zhang62493092020-01-15 16:04:47 -08001286#ifndef NO_BINDER
Yi Kong48a619f2018-06-05 16:34:59 -07001287 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001288 status = IInterface::asBinder(listener)->linkToDeath(
1289 static_cast<IBinder::DeathRecipient*>(this));
1290 if (status != NO_ERROR) {
1291 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1292 strerror(-status), status);
1293 }
1294 mCore->mLinkedToDeath = listener;
1295 }
Chong Zhang62493092020-01-15 16:04:47 -08001296#endif
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001297 mCore->mConnectedProducerListener = listener;
1298 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001299 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001300 break;
1301 default:
1302 BQ_LOGE("connect: unknown API %d", api);
1303 status = BAD_VALUE;
1304 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001305 }
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001306 mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001307 mCore->mBufferHasBeenQueued = false;
1308 mCore->mDequeueBufferCannotBlock = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001309 mCore->mQueueBufferCanDrop = false;
1310 mCore->mLegacyBufferDrop = true;
1311 if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1312 mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1313 mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
Dan Stoza127fc632015-06-30 13:43:32 -07001314 }
Dan Stoza289ade12014-02-28 11:17:17 -08001315
Pablo Ceballos981066c2016-02-18 12:54:37 -08001316 mCore->mAllowAllocation = true;
1317 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001318 return status;
1319}
1320
Robert Carr97b9c862016-09-08 13:54:35 -07001321status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001322 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001323 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001324
1325 int status = NO_ERROR;
1326 sp<IConsumerListener> listener;
1327 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001328 std::unique_lock<std::mutex> lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001329
1330 if (mode == DisconnectMode::AllLocal) {
Jayant Chowdharyad9fe272019-03-07 22:36:06 -08001331 if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
Robert Carr97b9c862016-09-08 13:54:35 -07001332 return NO_ERROR;
1333 }
1334 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1335 }
1336
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001337 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza289ade12014-02-28 11:17:17 -08001338
1339 if (mCore->mIsAbandoned) {
1340 // It's not really an error to disconnect after the surface has
1341 // been abandoned; it should just be a no-op.
1342 return NO_ERROR;
1343 }
1344
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001345 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001346 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1347 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1348 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001349 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001350 // If we're asked to disconnect the currently connected api but
1351 // nobody is connected, it's not really an error.
1352 if (api == BufferQueueCore::NO_CONNECTED_API) {
1353 return NO_ERROR;
1354 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001355 }
1356
Dan Stoza289ade12014-02-28 11:17:17 -08001357 switch (api) {
1358 case NATIVE_WINDOW_API_EGL:
1359 case NATIVE_WINDOW_API_CPU:
1360 case NATIVE_WINDOW_API_MEDIA:
1361 case NATIVE_WINDOW_API_CAMERA:
1362 if (mCore->mConnectedApi == api) {
1363 mCore->freeAllBuffersLocked();
1364
Chong Zhang62493092020-01-15 16:04:47 -08001365#ifndef NO_BINDER
Dan Stoza289ade12014-02-28 11:17:17 -08001366 // Remove our death notification callback if we have one
Yi Kong48a619f2018-06-05 16:34:59 -07001367 if (mCore->mLinkedToDeath != nullptr) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001368 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001369 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001370 // This can fail if we're here because of the death
1371 // notification, but we just ignore it
1372 token->unlinkToDeath(
1373 static_cast<IBinder::DeathRecipient*>(this));
1374 }
Chong Zhang62493092020-01-15 16:04:47 -08001375#endif
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001376 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001377 BufferQueueCore::INVALID_BUFFER_SLOT;
Yi Kong48a619f2018-06-05 16:34:59 -07001378 mCore->mLinkedToDeath = nullptr;
1379 mCore->mConnectedProducerListener = nullptr;
Dan Stoza289ade12014-02-28 11:17:17 -08001380 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001381 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001382 mCore->mSidebandStream.clear();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001383 mCore->mDequeueCondition.notify_all();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001384 mCore->mAutoPrerotation = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001385 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001386 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1387 BQ_LOGE("disconnect: not connected (req=%d)", api);
1388 status = NO_INIT;
1389 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001390 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001391 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001392 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001393 }
1394 break;
1395 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001396 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001397 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001398 break;
1399 }
1400 } // Autolock scope
1401
1402 // Call back without lock held
Yi Kong48a619f2018-06-05 16:34:59 -07001403 if (listener != nullptr) {
Dan Stoza289ade12014-02-28 11:17:17 -08001404 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001405 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001406 }
1407
1408 return status;
1409}
1410
Jesse Hall399184a2014-03-03 15:42:54 -08001411status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001412 sp<IConsumerListener> listener;
1413 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001414 std::lock_guard<std::mutex> _l(mCore->mMutex);
Wonsik Kimafe30812014-03-31 23:16:08 +09001415 mCore->mSidebandStream = stream;
1416 listener = mCore->mConsumerListener;
1417 } // Autolock scope
1418
Yi Kong48a619f2018-06-05 16:34:59 -07001419 if (listener != nullptr) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001420 listener->onSidebandStreamChanged();
1421 }
Jesse Hall399184a2014-03-03 15:42:54 -08001422 return NO_ERROR;
1423}
1424
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001425void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001426 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001427 ATRACE_CALL();
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001428
1429 const bool useDefaultSize = !width && !height;
Antoine Labour78014f32014-07-15 21:17:03 -07001430 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001431 size_t newBufferCount = 0;
1432 uint32_t allocWidth = 0;
1433 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001434 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001435 uint64_t allocUsage = 0;
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001436 std::string allocName;
Antoine Labour78014f32014-07-15 21:17:03 -07001437 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001438 std::unique_lock<std::mutex> lock(mCore->mMutex);
1439 mCore->waitWhileAllocatingLocked(lock);
Dan Stoza29a3e902014-06-20 13:13:57 -07001440
Dan Stoza9de72932015-04-16 17:28:43 -07001441 if (!mCore->mAllowAllocation) {
1442 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1443 "BufferQueue");
1444 return;
1445 }
1446
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001447 // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1448 // both allocateBuffers and dequeueBuffer.
1449 newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001450 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001451 return;
1452 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001453
Antoine Labour78014f32014-07-15 21:17:03 -07001454 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1455 allocHeight = 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(allocWidth, allocHeight);
1459 }
1460
Antoine Labour78014f32014-07-15 21:17:03 -07001461 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1462 allocUsage = usage | mCore->mConsumerUsageBits;
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001463 allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
Antoine Labour78014f32014-07-15 21:17:03 -07001464
1465 mCore->mIsAllocating = true;
1466 } // Autolock scope
1467
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001468 Vector<sp<GraphicBuffer>> buffers;
Jorim Jaggi0a3e7842018-07-17 13:48:33 +02001469 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001470 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001471 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chia-I Wu1dbf0e32018-02-07 14:40:08 -08001472 allocUsage, allocName);
Mathias Agopian0556d792017-03-22 15:49:32 -07001473
1474 status_t result = graphicBuffer->initCheck();
1475
Antoine Labour78014f32014-07-15 21:17:03 -07001476 if (result != NO_ERROR) {
1477 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001478 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001479 std::lock_guard<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001480 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001481 mCore->mIsAllocatingCondition.notify_all();
Antoine Labour78014f32014-07-15 21:17:03 -07001482 return;
1483 }
1484 buffers.push_back(graphicBuffer);
1485 }
1486
1487 { // Autolock scope
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001488 std::unique_lock<std::mutex> lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -07001489 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1490 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001491 if (useDefaultSize && mCore->mAutoPrerotation &&
1492 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1493 std::swap(checkWidth, checkHeight);
1494 }
1495
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001496 PixelFormat checkFormat = format != 0 ?
1497 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001498 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001499 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1500 checkFormat != allocFormat || checkUsage != allocUsage) {
1501 // Something changed while we released the lock. Retry.
1502 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1503 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001504 mCore->mIsAllocatingCondition.notify_all();
Dan Stoza29a3e902014-06-20 13:13:57 -07001505 continue;
1506 }
1507
Antoine Labour78014f32014-07-15 21:17:03 -07001508 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001509 if (mCore->mFreeSlots.empty()) {
1510 BQ_LOGV("allocateBuffers: a slot was occupied while "
1511 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001512 continue;
1513 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001514 auto slot = mCore->mFreeSlots.begin();
1515 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1516 mSlots[*slot].mGraphicBuffer = buffers[i];
1517 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001518
1519 // freeBufferLocked puts this slot on the free slots list. Since
1520 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001521 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001522
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001523 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1524 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001525
1526 // Make sure the erase is done after all uses of the slot
1527 // iterator since it will be invalid after this point.
1528 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001529 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001530
Antoine Labour78014f32014-07-15 21:17:03 -07001531 mCore->mIsAllocating = false;
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001532 mCore->mIsAllocatingCondition.notify_all();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001533 VALIDATE_CONSISTENCY();
Jorim Jaggi35b4e382019-03-28 00:44:03 +01001534
1535 // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1536 // waiting anymore so it can use the buffer we just allocated.
1537 while (mDequeueWaitingForAllocation) {
1538 mDequeueWaitingForAllocationCondition.wait(lock);
1539 }
Antoine Labour78014f32014-07-15 21:17:03 -07001540 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001541 }
1542}
1543
Dan Stoza9de72932015-04-16 17:28:43 -07001544status_t BufferQueueProducer::allowAllocation(bool allow) {
1545 ATRACE_CALL();
1546 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1547
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001548 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza9de72932015-04-16 17:28:43 -07001549 mCore->mAllowAllocation = allow;
1550 return NO_ERROR;
1551}
1552
Dan Stoza812ed062015-06-02 15:45:22 -07001553status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1554 ATRACE_CALL();
1555 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1556
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001557 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza812ed062015-06-02 15:45:22 -07001558 mCore->mGenerationNumber = generationNumber;
1559 return NO_ERROR;
1560}
1561
Dan Stozac6f30bd2015-06-08 09:32:50 -07001562String8 BufferQueueProducer::getConsumerName() const {
1563 ATRACE_CALL();
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001564 std::lock_guard<std::mutex> lock(mCore->mMutex);
Tomasz Wasilczykf2add402023-08-11 00:06:51 +00001565 BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
Dan Stozac6f30bd2015-06-08 09:32:50 -07001566 return mConsumerName;
1567}
1568
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001569status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001570 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001571 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001572
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001573 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001574 if (!sharedBufferMode) {
1575 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001576 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001577 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001578 return NO_ERROR;
1579}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001580
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001581status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1582 ATRACE_CALL();
1583 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1584
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001585 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001586
1587 mCore->mAutoRefresh = autoRefresh;
1588 return NO_ERROR;
1589}
1590
Dan Stoza127fc632015-06-30 13:43:32 -07001591status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1592 ATRACE_CALL();
1593 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1594
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001595 std::lock_guard<std::mutex> lock(mCore->mMutex);
Sungtak Lee42a94c62019-05-24 14:27:14 -07001596 bool dequeueBufferCannotBlock =
1597 timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1598 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
Pablo Ceballos981066c2016-02-18 12:54:37 -08001599 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1600 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1601 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1602 "available slots. Delta = %d", delta);
1603 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001604 }
1605
Pablo Ceballos981066c2016-02-18 12:54:37 -08001606 mDequeueTimeout = timeout;
Sungtak Lee42a94c62019-05-24 14:27:14 -07001607 mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1608 if (timeout > 0) {
1609 mCore->mQueueBufferCanDrop = false;
Sungtak Lee3249fb62019-03-02 16:40:47 -08001610 }
Pablo Ceballos9e314332016-01-12 13:49:19 -08001611
Pablo Ceballos981066c2016-02-18 12:54:37 -08001612 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001613 return NO_ERROR;
1614}
1615
Sungtak Lee3249fb62019-03-02 16:40:47 -08001616status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1617 ATRACE_CALL();
1618 BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1619
1620 std::lock_guard<std::mutex> lock(mCore->mMutex);
1621 mCore->mLegacyBufferDrop = drop;
1622 return NO_ERROR;
1623}
1624
Dan Stoza50101d02016-04-07 16:53:23 -07001625status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001626 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001627 ATRACE_CALL();
1628 BQ_LOGV("getLastQueuedBuffer");
1629
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001630 std::lock_guard<std::mutex> lock(mCore->mMutex);
Dan Stoza50101d02016-04-07 16:53:23 -07001631 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1632 *outBuffer = nullptr;
1633 *outFence = Fence::NO_FENCE;
1634 return NO_ERROR;
1635 }
1636
1637 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1638 *outFence = mLastQueueBufferFence;
1639
John Reck1a61da52016-04-28 13:18:15 -07001640 // Currently only SurfaceFlinger internally ever changes
1641 // GLConsumer's filtering mode, so we just use 'true' here as
1642 // this is slightly specialized for the current client of this API,
1643 // which does want filtering.
1644 GLConsumer::computeTransformMatrix(outTransformMatrix,
1645 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1646 mLastQueuedTransform, true /* filter */);
1647
Dan Stoza50101d02016-04-07 16:53:23 -07001648 return NO_ERROR;
1649}
1650
John Reckf0f13e82021-05-18 00:42:56 -04001651status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1652 Rect* outRect, uint32_t* outTransform) {
1653 ATRACE_CALL();
1654 BQ_LOGV("getLastQueuedBuffer");
1655
1656 std::lock_guard<std::mutex> lock(mCore->mMutex);
1657 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1658 *outBuffer = nullptr;
1659 *outFence = Fence::NO_FENCE;
1660 return NO_ERROR;
1661 }
1662
1663 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1664 *outFence = mLastQueueBufferFence;
1665 *outRect = mLastQueuedCrop;
1666 *outTransform = mLastQueuedTransform;
1667
1668 return NO_ERROR;
1669}
1670
Brian Anderson3890c392016-07-25 12:48:08 -07001671void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1672 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001673}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001674
Brian Anderson3890c392016-07-25 12:48:08 -07001675void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001676 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001677 FrameEventHistoryDelta* outDelta) {
1678 if (newTimestamps == nullptr && outDelta == nullptr) {
1679 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001680 }
1681
1682 ATRACE_CALL();
1683 BQ_LOGV("addAndGetFrameTimestamps");
1684 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001685 {
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001686 std::lock_guard<std::mutex> lock(mCore->mMutex);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001687 listener = mCore->mConsumerListener;
1688 }
Yi Kong48a619f2018-06-05 16:34:59 -07001689 if (listener != nullptr) {
Brian Anderson3890c392016-07-25 12:48:08 -07001690 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001691 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001692}
1693
Dan Stoza289ade12014-02-28 11:17:17 -08001694void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1695 // If we're here, it means that a producer we were connected to died.
1696 // We're guaranteed that we are still connected to it because we remove
1697 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1698 // without synchronization here.
1699 int api = mCore->mConnectedApi;
1700 disconnect(api);
1701}
1702
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001703status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1704 BQ_LOGV("getUniqueId");
1705
1706 *outId = mCore->mUniqueId;
1707 return NO_ERROR;
1708}
1709
Chia-I Wue2786ea2017-08-07 10:36:08 -07001710status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1711 BQ_LOGV("getConsumerUsage");
1712
Jorim Jaggi6ae55032019-04-02 02:27:44 +02001713 std::lock_guard<std::mutex> lock(mCore->mMutex);
Chia-I Wue2786ea2017-08-07 10:36:08 -07001714 *outUsage = mCore->mConsumerUsageBits;
1715 return NO_ERROR;
1716}
1717
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001718status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1719 ATRACE_CALL();
1720 BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1721
1722 std::lock_guard<std::mutex> lock(mCore->mMutex);
1723
1724 mCore->mAutoPrerotation = autoPrerotation;
1725 return NO_ERROR;
1726}
1727
Dan Stoza289ade12014-02-28 11:17:17 -08001728} // namespace android