blob: b76e2c68b53f35c74a53db804f7637c62fb2f0b9 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza3e96f192014-03-03 10:16:19 -080019#define LOG_TAG "BufferQueueProducer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Pablo Ceballos9e314332016-01-12 13:49:19 -080023#if DEBUG_ONLY_CODE
24#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25#else
26#define VALIDATE_CONSISTENCY()
27#endif
28
Dan Stoza289ade12014-02-28 11:17:17 -080029#define EGL_EGLEXT_PROTOTYPES
30
Robert Carr97b9c862016-09-08 13:54:35 -070031#include <binder/IPCThreadState.h>
Dan Stoza289ade12014-02-28 11:17:17 -080032#include <gui/BufferItem.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/BufferQueueProducer.h>
John Reck1a61da52016-04-28 13:18:15 -070035#include <gui/GLConsumer.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036#include <gui/IConsumerListener.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080038
39#include <utils/Log.h>
40#include <utils/Trace.h>
41
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070042#include <system/window.h>
43
Dan Stoza289ade12014-02-28 11:17:17 -080044namespace android {
45
Craig Donner6ebc46a2016-10-21 15:23:44 -070046static constexpr uint32_t BQ_LAYER_COUNT = 1;
47
Irvel468051e2016-06-13 16:44:44 -070048BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
49 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080050 mCore(core),
51 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070052 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070053 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070054 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080055 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070056 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080057 mCallbackMutex(),
58 mNextCallbackTicket(0),
59 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070060 mCallbackCondition(),
61 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080062
63BufferQueueProducer::~BufferQueueProducer() {}
64
65status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
66 ATRACE_CALL();
67 BQ_LOGV("requestBuffer: slot %d", slot);
68 Mutex::Autolock lock(mCore->mMutex);
69
70 if (mCore->mIsAbandoned) {
71 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
72 return NO_INIT;
73 }
74
Pablo Ceballos583b1b32015-09-03 18:23:52 -070075 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
76 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
77 return NO_INIT;
78 }
79
Dan Stoza3e96f192014-03-03 10:16:19 -080080 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080081 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080082 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080083 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070084 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080085 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070086 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080087 return BAD_VALUE;
88 }
89
90 mSlots[slot].mRequestBufferCalled = true;
91 *buf = mSlots[slot].mGraphicBuffer;
92 return NO_ERROR;
93}
94
Pablo Ceballosfa455352015-08-12 17:47:47 -070095status_t BufferQueueProducer::setMaxDequeuedBufferCount(
96 int maxDequeuedBuffers) {
97 ATRACE_CALL();
98 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
99 maxDequeuedBuffers);
100
Pablo Ceballos981066c2016-02-18 12:54:37 -0800101 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700102 { // Autolock scope
103 Mutex::Autolock lock(mCore->mMutex);
104 mCore->waitWhileAllocatingLocked();
105
106 if (mCore->mIsAbandoned) {
107 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
108 "abandoned");
109 return NO_INIT;
110 }
111
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700112 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
113 return NO_ERROR;
114 }
115
Pablo Ceballos72daab62015-12-07 16:38:43 -0800116 // The new maxDequeuedBuffer count should not be violated by the number
117 // of currently dequeued buffers
118 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800119 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700120 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800121 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700122 }
123 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800124 if (dequeuedCount > maxDequeuedBuffers) {
125 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
126 "count (%d) exceeds the current dequeued buffer count (%d)",
127 maxDequeuedBuffers, dequeuedCount);
128 return BAD_VALUE;
129 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700131 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700132 bufferCount += maxDequeuedBuffers;
133
134 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
135 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
136 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
137 return BAD_VALUE;
138 }
139
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700140 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700141 if (bufferCount < minBufferSlots) {
142 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
143 "less than minimum %d", bufferCount, minBufferSlots);
144 return BAD_VALUE;
145 }
146
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700147 if (bufferCount > mCore->mMaxBufferCount) {
148 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700149 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
150 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
151 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
152 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700153 return BAD_VALUE;
154 }
155
Pablo Ceballos72daab62015-12-07 16:38:43 -0800156 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800157 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800158 return BAD_VALUE;
159 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700160 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800161 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800162 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800163 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800164 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700166 } // Autolock scope
167
168 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800169 if (listener != NULL) {
170 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700171 }
172
173 return NO_ERROR;
174}
175
176status_t BufferQueueProducer::setAsyncMode(bool async) {
177 ATRACE_CALL();
178 BQ_LOGV("setAsyncMode: async = %d", async);
179
Pablo Ceballos981066c2016-02-18 12:54:37 -0800180 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700181 { // Autolock scope
182 Mutex::Autolock lock(mCore->mMutex);
183 mCore->waitWhileAllocatingLocked();
184
185 if (mCore->mIsAbandoned) {
186 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
187 return NO_INIT;
188 }
189
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700190 if (async == mCore->mAsyncMode) {
191 return NO_ERROR;
192 }
193
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700194 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700195 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
196 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700197 BQ_LOGE("setAsyncMode(%d): this call would cause the "
198 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700199 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
200 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
201 mCore->mMaxDequeuedBufferCount,
202 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700203 return BAD_VALUE;
204 }
205
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800206 int delta = mCore->getMaxBufferCountLocked(async,
207 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
208 - mCore->getMaxBufferCountLocked();
209
Pablo Ceballos981066c2016-02-18 12:54:37 -0800210 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800211 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
212 "available slots. Delta = %d", delta);
213 return BAD_VALUE;
214 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800216 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700217 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700218 if (delta < 0) {
219 listener = mCore->mConsumerListener;
220 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700221 } // Autolock scope
222
223 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800224 if (listener != NULL) {
225 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700226 }
227 return NO_ERROR;
228}
229
Dan Stoza5ecfb682016-01-04 17:01:02 -0800230int BufferQueueProducer::getFreeBufferLocked() const {
231 if (mCore->mFreeBuffers.empty()) {
232 return BufferQueueCore::INVALID_BUFFER_SLOT;
233 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800234 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800235 mCore->mFreeBuffers.pop_front();
236 return slot;
237}
238
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800239int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800240 if (mCore->mFreeSlots.empty()) {
241 return BufferQueueCore::INVALID_BUFFER_SLOT;
242 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800243 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800244 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800245 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800246}
247
248status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800249 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800250 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
251 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 bool tryAgain = true;
253 while (tryAgain) {
254 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800255 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800256 return NO_INIT;
257 }
258
Dan Stoza9f3053d2014-03-06 15:14:33 -0800259 int dequeuedCount = 0;
260 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800261 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700262 if (mSlots[s].mBufferState.isDequeued()) {
263 ++dequeuedCount;
264 }
265 if (mSlots[s].mBufferState.isAcquired()) {
266 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800267 }
268 }
269
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700270 // Producers are not allowed to dequeue more than
271 // mMaxDequeuedBufferCount buffers.
272 // This check is only done if a buffer has already been queued
273 if (mCore->mBufferHasBeenQueued &&
274 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
275 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800276 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800277 return INVALID_OPERATION;
278 }
279
Dan Stoza0de7ea72015-04-23 13:20:51 -0700280 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
281
Dan Stozaae3c3682014-04-18 15:43:35 -0700282 // If we disconnect and reconnect quickly, we can be in a state where
283 // our slots are empty but we have many buffers in the queue. This can
284 // cause us to run out of memory if we outrun the consumer. Wait here if
285 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800286 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700287 bool tooManyBuffers = mCore->mQueue.size()
288 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700289 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800290 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700291 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700292 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700293 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700294 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700296 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700297 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800298 } else {
299 if (caller == FreeSlotCaller::Dequeue) {
300 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800301 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800302 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
303 *found = slot;
304 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800305 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800306 }
307 } else {
308 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800309 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800310 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
311 *found = slot;
312 } else {
313 *found = getFreeBufferLocked();
314 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700315 }
316 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700317 }
318
319 // If no buffer is found, or if the queue has too many buffers
320 // outstanding, wait for a buffer to be acquired or released, or for the
321 // max buffer count to change.
322 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
323 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800324 if (tryAgain) {
325 // Return an error if we're in non-blocking mode (producer and
326 // consumer are controlled by the application).
327 // However, the consumer is allowed to briefly acquire an extra
328 // buffer (which could cause us to have to wait here), which is
329 // okay, since it is only used to implement an atomic acquire +
330 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700331 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800332 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
333 return WOULD_BLOCK;
334 }
Dan Stoza127fc632015-06-30 13:43:32 -0700335 if (mDequeueTimeout >= 0) {
336 status_t result = mCore->mDequeueCondition.waitRelative(
337 mCore->mMutex, mDequeueTimeout);
338 if (result == TIMED_OUT) {
339 return result;
340 }
341 } else {
342 mCore->mDequeueCondition.wait(mCore->mMutex);
343 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800344 }
345 } // while (tryAgain)
346
347 return NO_ERROR;
348}
349
Dan Stoza289ade12014-02-28 11:17:17 -0800350status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700351 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700352 PixelFormat format, uint32_t usage,
353 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800354 ATRACE_CALL();
355 { // Autolock scope
356 Mutex::Autolock lock(mCore->mMutex);
357 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700358
359 if (mCore->mIsAbandoned) {
360 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
361 return NO_INIT;
362 }
363
364 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
365 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
366 return NO_INIT;
367 }
Dan Stoza289ade12014-02-28 11:17:17 -0800368 } // Autolock scope
369
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700370 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
371 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800372
373 if ((width && !height) || (!width && height)) {
374 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
375 return BAD_VALUE;
376 }
377
378 status_t returnFlags = NO_ERROR;
379 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
380 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800381 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800382
383 { // Autolock scope
384 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700385 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800386
387 if (format == 0) {
388 format = mCore->mDefaultBufferFormat;
389 }
390
391 // Enable the usage bits the consumer requested
392 usage |= mCore->mConsumerUsageBits;
393
Dan Stoza9de72932015-04-16 17:28:43 -0700394 const bool useDefaultSize = !width && !height;
395 if (useDefaultSize) {
396 width = mCore->mDefaultWidth;
397 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800398 }
Dan Stoza289ade12014-02-28 11:17:17 -0800399
Pablo Ceballos981066c2016-02-18 12:54:37 -0800400 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700401 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800402 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800403 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700404 if (status != NO_ERROR) {
405 return status;
406 }
407
408 // This should not happen
409 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
410 BQ_LOGE("dequeueBuffer: no available buffer slots");
411 return -EBUSY;
412 }
413
414 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
415
416 // If we are not allowed to allocate new buffers,
417 // waitForFreeSlotThenRelock must have returned a slot containing a
418 // buffer. If this buffer would require reallocation to meet the
419 // requested attributes, we free it and attempt to get another one.
420 if (!mCore->mAllowAllocation) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700421 if (buffer->needsReallocation(width, height, format,
422 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700423 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700424 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
425 "buffer");
426 return BAD_VALUE;
427 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800428 mCore->mFreeSlots.insert(found);
429 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700430 found = BufferItem::INVALID_BUFFER_SLOT;
431 continue;
432 }
433 }
Dan Stoza289ade12014-02-28 11:17:17 -0800434 }
435
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800436 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700437 if (mCore->mSharedBufferSlot == found &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700438 buffer->needsReallocation(width, height, format,
439 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800440 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
441 "buffer");
442
443 return BAD_VALUE;
444 }
445
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700446 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800447 mCore->mActiveBuffers.insert(found);
448 }
Dan Stoza289ade12014-02-28 11:17:17 -0800449 *outSlot = found;
450 ATRACE_BUFFER_INDEX(found);
451
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800452 attachedByConsumer = mSlots[found].mNeedsReallocation;
453 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800454
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700455 mSlots[found].mBufferState.dequeue();
456
Dan Stoza289ade12014-02-28 11:17:17 -0800457 if ((buffer == NULL) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700458 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800459 {
460 mSlots[found].mAcquireCalled = false;
461 mSlots[found].mGraphicBuffer = NULL;
462 mSlots[found].mRequestBufferCalled = false;
463 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
464 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
465 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800466 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700467 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800468
469 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800470 } else {
471 // We add 1 because that will be the frame number when this buffer
472 // is queued
473 mCore->mBufferAge =
474 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800475 }
476
Dan Stoza800b41a2015-04-28 14:20:04 -0700477 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
478 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800479
Dan Stoza289ade12014-02-28 11:17:17 -0800480 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
481 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
482 "slot=%d w=%d h=%d format=%u",
483 found, buffer->width, buffer->height, buffer->format);
484 }
485
486 eglDisplay = mSlots[found].mEglDisplay;
487 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700488 // Don't return a fence in shared buffer mode, except for the first
489 // frame.
490 *outFence = (mCore->mSharedBufferMode &&
491 mCore->mSharedBufferSlot == found) ?
492 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800493 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
494 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700495
496 // If shared buffer mode has just been enabled, cache the slot of the
497 // first buffer that is dequeued and mark it as the shared buffer.
498 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
499 BufferQueueCore::INVALID_BUFFER_SLOT) {
500 mCore->mSharedBufferSlot = found;
501 mSlots[found].mBufferState.mShared = true;
502 }
Dan Stoza289ade12014-02-28 11:17:17 -0800503 } // Autolock scope
504
505 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700506 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700507 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700508 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700509 {mConsumerName.string(), mConsumerName.size()});
510
511 status_t error = graphicBuffer->initCheck();
512
Dan Stoza289ade12014-02-28 11:17:17 -0800513 { // Autolock scope
514 Mutex::Autolock lock(mCore->mMutex);
515
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700516 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
517 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
518 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
519 }
520
521 mCore->mIsAllocating = false;
522 mCore->mIsAllocatingCondition.broadcast();
523
524 if (graphicBuffer == NULL) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700525 mCore->mFreeSlots.insert(*outSlot);
526 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700527 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
528 return error;
529 }
530
Dan Stoza289ade12014-02-28 11:17:17 -0800531 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700532 mCore->mFreeSlots.insert(*outSlot);
533 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800534 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
535 return NO_INIT;
536 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800537
Pablo Ceballos9e314332016-01-12 13:49:19 -0800538 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800539 } // Autolock scope
540 }
541
Dan Stoza9f3053d2014-03-06 15:14:33 -0800542 if (attachedByConsumer) {
543 returnFlags |= BUFFER_NEEDS_REALLOCATION;
544 }
545
Dan Stoza289ade12014-02-28 11:17:17 -0800546 if (eglFence != EGL_NO_SYNC_KHR) {
547 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
548 1000000000);
549 // If something goes wrong, log the error, but return the buffer without
550 // synchronizing access to it. It's too late at this point to abort the
551 // dequeue operation.
552 if (result == EGL_FALSE) {
553 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
554 eglGetError());
555 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
556 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
557 }
558 eglDestroySyncKHR(eglDisplay, eglFence);
559 }
560
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700561 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
562 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800563 mSlots[*outSlot].mFrameNumber,
564 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
565
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700566 addAndGetFrameTimestamps(nullptr, outTimestamps);
567
Dan Stoza289ade12014-02-28 11:17:17 -0800568 return returnFlags;
569}
570
Dan Stoza9f3053d2014-03-06 15:14:33 -0800571status_t BufferQueueProducer::detachBuffer(int slot) {
572 ATRACE_CALL();
573 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700574 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800575
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700576 sp<IConsumerListener> listener;
577 {
578 Mutex::Autolock lock(mCore->mMutex);
579
580 if (mCore->mIsAbandoned) {
581 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
582 return NO_INIT;
583 }
584
585 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
586 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
587 return NO_INIT;
588 }
589
590 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
591 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
592 return BAD_VALUE;
593 }
594
595 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
596 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
597 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
598 return BAD_VALUE;
599 } else if (!mSlots[slot].mBufferState.isDequeued()) {
600 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
601 "(state = %s)", slot, mSlots[slot].mBufferState.string());
602 return BAD_VALUE;
603 } else if (!mSlots[slot].mRequestBufferCalled) {
604 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
605 slot);
606 return BAD_VALUE;
607 }
608
609 mSlots[slot].mBufferState.detachProducer();
610 mCore->mActiveBuffers.erase(slot);
611 mCore->mFreeSlots.insert(slot);
612 mCore->clearBufferSlotLocked(slot);
613 mCore->mDequeueCondition.broadcast();
614 VALIDATE_CONSISTENCY();
615 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800616 }
617
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700618 if (listener != NULL) {
619 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700620 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800621
Dan Stoza9f3053d2014-03-06 15:14:33 -0800622 return NO_ERROR;
623}
624
Dan Stozad9822a32014-03-28 15:25:31 -0700625status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
626 sp<Fence>* outFence) {
627 ATRACE_CALL();
628
629 if (outBuffer == NULL) {
630 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
631 return BAD_VALUE;
632 } else if (outFence == NULL) {
633 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
634 return BAD_VALUE;
635 }
636
Pablo Ceballos981066c2016-02-18 12:54:37 -0800637 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700638
Pablo Ceballos981066c2016-02-18 12:54:37 -0800639 if (mCore->mIsAbandoned) {
640 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
641 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700642 }
643
Pablo Ceballos981066c2016-02-18 12:54:37 -0800644 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
645 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
646 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700647 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800648
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700649 if (mCore->mSharedBufferMode) {
650 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
651 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800652 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700653 }
654
Pablo Ceballos981066c2016-02-18 12:54:37 -0800655 mCore->waitWhileAllocatingLocked();
656
657 if (mCore->mFreeBuffers.empty()) {
658 return NO_MEMORY;
659 }
660
661 int found = mCore->mFreeBuffers.front();
662 mCore->mFreeBuffers.remove(found);
663 mCore->mFreeSlots.insert(found);
664
665 BQ_LOGV("detachNextBuffer detached slot %d", found);
666
667 *outBuffer = mSlots[found].mGraphicBuffer;
668 *outFence = mSlots[found].mFence;
669 mCore->clearBufferSlotLocked(found);
670 VALIDATE_CONSISTENCY();
671
Dan Stozad9822a32014-03-28 15:25:31 -0700672 return NO_ERROR;
673}
674
Dan Stoza9f3053d2014-03-06 15:14:33 -0800675status_t BufferQueueProducer::attachBuffer(int* outSlot,
676 const sp<android::GraphicBuffer>& buffer) {
677 ATRACE_CALL();
678
679 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700680 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800681 return BAD_VALUE;
682 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700683 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800684 return BAD_VALUE;
685 }
686
687 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700688
689 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700690 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700691 return NO_INIT;
692 }
693
694 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700695 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700696 return NO_INIT;
697 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800698
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700699 if (mCore->mSharedBufferMode) {
700 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700701 return BAD_VALUE;
702 }
703
Dan Stoza812ed062015-06-02 15:45:22 -0700704 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
705 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
706 "[queue %u]", buffer->getGenerationNumber(),
707 mCore->mGenerationNumber);
708 return BAD_VALUE;
709 }
710
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700711 mCore->waitWhileAllocatingLocked();
712
Dan Stoza9f3053d2014-03-06 15:14:33 -0800713 status_t returnFlags = NO_ERROR;
714 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800715 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800716 if (status != NO_ERROR) {
717 return status;
718 }
719
720 // This should not happen
721 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700722 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800723 return -EBUSY;
724 }
725
726 *outSlot = found;
727 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700728 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800729 *outSlot, returnFlags);
730
731 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700732 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800733 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
734 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700735 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800736 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800737 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800738 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800739 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700740
Dan Stoza9f3053d2014-03-06 15:14:33 -0800741 return returnFlags;
742}
743
Dan Stoza289ade12014-02-28 11:17:17 -0800744status_t BufferQueueProducer::queueBuffer(int slot,
745 const QueueBufferInput &input, QueueBufferOutput *output) {
746 ATRACE_CALL();
747 ATRACE_BUFFER_INDEX(slot);
748
Brian Andersond6927fb2016-07-23 23:37:30 -0700749 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800750 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800751 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700752 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800753 int scalingMode;
754 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700755 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700756 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700757 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700758 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700759 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
760 &getFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700761 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800762
Brian Andersond6927fb2016-07-23 23:37:30 -0700763 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800764 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800765 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800766 }
767
Brian Anderson3d4039d2016-09-23 16:31:30 -0700768 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
769
Dan Stoza289ade12014-02-28 11:17:17 -0800770 switch (scalingMode) {
771 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
772 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
773 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
774 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
775 break;
776 default:
777 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800778 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800779 }
780
Dan Stoza8dc55392014-11-04 11:37:46 -0800781 sp<IConsumerListener> frameAvailableListener;
782 sp<IConsumerListener> frameReplacedListener;
783 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700784 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800785 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800786 { // Autolock scope
787 Mutex::Autolock lock(mCore->mMutex);
788
789 if (mCore->mIsAbandoned) {
790 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
791 return NO_INIT;
792 }
793
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700794 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
795 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
796 return NO_INIT;
797 }
798
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800799 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800800 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800801 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800802 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700803 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800804 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700805 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800806 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800807 } else if (!mSlots[slot].mRequestBufferCalled) {
808 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
809 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800810 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800811 }
812
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700813 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700814 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700815 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700816 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700817 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700818 mSlots[slot].mBufferState.mShared = true;
819 }
820
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800821 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700822 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Brian Andersond6927fb2016-07-23 23:37:30 -0700823 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp,
824 dataSpace, crop.left, crop.top, crop.right, crop.bottom,
825 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800826 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800827
828 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
829 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700830 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800831 crop.intersect(bufferRect, &croppedRect);
832 if (croppedRect != crop) {
833 BQ_LOGE("queueBuffer: crop rect is not contained within the "
834 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800835 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800836 }
837
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800838 // Override UNKNOWN dataspace with consumer default
839 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
840 dataSpace = mCore->mDefaultBufferDataSpace;
841 }
842
Brian Andersond6927fb2016-07-23 23:37:30 -0700843 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700844 mSlots[slot].mBufferState.queue();
845
Brian Andersond6927fb2016-07-23 23:37:30 -0700846 // Increment the frame counter and store a local version of it
847 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800848 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 currentFrameNumber = mCore->mFrameCounter;
850 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800851
Dan Stoza289ade12014-02-28 11:17:17 -0800852 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
853 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
854 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800855 item.mTransform = transform &
856 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800857 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
859 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700860 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800861 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800862 item.mDataSpace = dataSpace;
Brian Andersond6927fb2016-07-23 23:37:30 -0700863 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800864 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700865 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700866 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700867 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700868 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700869 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700870 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700871 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700872 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800873
Ruben Brunk1681d952014-06-27 15:51:55 -0700874 mStickyTransform = stickyTransform;
875
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700877 if (mCore->mSharedBufferMode) {
878 mCore->mSharedBufferCache.crop = crop;
879 mCore->mSharedBufferCache.transform = transform;
880 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700881 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700882 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700883 }
884
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800885 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800886 if (mCore->mQueue.empty()) {
887 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
888 // and simply queue this buffer
889 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800890 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800891 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700892 // When the queue is not empty, we need to look at the last buffer
893 // in the queue to see if we need to replace it
894 const BufferItem& last = mCore->mQueue.itemAt(
895 mCore->mQueue.size() - 1);
896 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800897
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700898 if (!last.mIsStale) {
899 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700900
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700901 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700902 // still be around. Mark it as no longer shared if this
903 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700904 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700905 mSlots[last.mSlot].mBufferState.isFree()) {
906 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700907 }
908 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700909 if (!mSlots[last.mSlot].mBufferState.isShared()) {
910 mCore->mActiveBuffers.erase(last.mSlot);
911 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800912 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700913 }
Dan Stoza289ade12014-02-28 11:17:17 -0800914 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800915
Dan Stoza289ade12014-02-28 11:17:17 -0800916 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700917 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800918 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800919 } else {
920 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800921 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800922 }
923 }
924
925 mCore->mBufferHasBeenQueued = true;
926 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700927 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800928
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700929 output->width = mCore->mDefaultWidth;
930 output->height = mCore->mDefaultHeight;
931 output->transformHint = mCore->mTransformHint;
932 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
933 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800934
Colin Cross152c3b72016-09-27 14:08:19 -0700935 ATRACE_INT(mCore->mConsumerName.string(),
936 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700937 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800938
939 // Take a ticket for the callback functions
940 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700941
Pablo Ceballos9e314332016-01-12 13:49:19 -0800942 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800943 } // Autolock scope
944
Irvel468051e2016-06-13 16:44:44 -0700945 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
946 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
947 // there will be no Binder call
948 if (!mConsumerIsSurfaceFlinger) {
949 item.mGraphicBuffer.clear();
950 }
951
952 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800953 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
954
955 // Call back without the main BufferQueue lock held, but with the callback
956 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700957
958 int connectedApi;
959 sp<Fence> lastQueuedFence;
960
961 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800962 Mutex::Autolock lock(mCallbackMutex);
963 while (callbackTicket != mCurrentCallbackTicket) {
964 mCallbackCondition.wait(mCallbackMutex);
965 }
966
967 if (frameAvailableListener != NULL) {
968 frameAvailableListener->onFrameAvailable(item);
969 } else if (frameReplacedListener != NULL) {
970 frameReplacedListener->onFrameReplaced(item);
971 }
972
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700973 connectedApi = mCore->mConnectedApi;
974 lastQueuedFence = std::move(mLastQueueBufferFence);
975
976 mLastQueueBufferFence = std::move(acquireFence);
977 mLastQueuedCrop = item.mCrop;
978 mLastQueuedTransform = item.mTransform;
979
Dan Stoza8dc55392014-11-04 11:37:46 -0800980 ++mCurrentCallbackTicket;
981 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800982 }
983
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000984 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700985 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000986 // Waiting here allows for two full buffers to be queued but not a
987 // third. In the event that frames take varying time, this makes a
988 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700989 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000990 }
991
Brian Andersond6927fb2016-07-23 23:37:30 -0700992 // Update and get FrameEventHistory.
993 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
994 NewFrameEventsEntry newFrameEventsEntry = {
995 currentFrameNumber,
996 postedTime,
997 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700998 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -0700999 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001000 addAndGetFrameTimestamps(&newFrameEventsEntry,
1001 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001002
Dan Stoza289ade12014-02-28 11:17:17 -08001003 return NO_ERROR;
1004}
1005
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001006status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001007 ATRACE_CALL();
1008 BQ_LOGV("cancelBuffer: slot %d", slot);
1009 Mutex::Autolock lock(mCore->mMutex);
1010
1011 if (mCore->mIsAbandoned) {
1012 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001013 return NO_INIT;
1014 }
1015
1016 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1017 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1018 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001019 }
1020
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001021 if (mCore->mSharedBufferMode) {
1022 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001023 return BAD_VALUE;
1024 }
1025
Dan Stoza3e96f192014-03-03 10:16:19 -08001026 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001027 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001028 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001029 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001030 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001031 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001032 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001033 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001034 } else if (fence == NULL) {
1035 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001036 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001037 }
1038
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001039 mSlots[slot].mBufferState.cancel();
1040
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001041 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001042 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001043 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001044 mSlots[slot].mBufferState.mShared = false;
1045 }
1046
1047 // Don't put the shared buffer on the free list.
1048 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001049 mCore->mActiveBuffers.erase(slot);
1050 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001051 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001052
Dan Stoza289ade12014-02-28 11:17:17 -08001053 mSlots[slot].mFence = fence;
1054 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001055 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001056
1057 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001058}
1059
1060int BufferQueueProducer::query(int what, int *outValue) {
1061 ATRACE_CALL();
1062 Mutex::Autolock lock(mCore->mMutex);
1063
1064 if (outValue == NULL) {
1065 BQ_LOGE("query: outValue was NULL");
1066 return BAD_VALUE;
1067 }
1068
1069 if (mCore->mIsAbandoned) {
1070 BQ_LOGE("query: BufferQueue has been abandoned");
1071 return NO_INIT;
1072 }
1073
1074 int value;
1075 switch (what) {
1076 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001077 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001078 break;
1079 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001080 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001081 break;
1082 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001083 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001084 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001085 case NATIVE_WINDOW_LAYER_COUNT:
1086 // All BufferQueue buffers have a single layer.
1087 value = BQ_LAYER_COUNT;
1088 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001089 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001090 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001091 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001092 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001093 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001094 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001095 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1096 value = (mCore->mQueue.size() > 1);
1097 break;
1098 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001099 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001100 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001101 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1102 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1103 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001104 case NATIVE_WINDOW_BUFFER_AGE:
1105 if (mCore->mBufferAge > INT32_MAX) {
1106 value = 0;
1107 } else {
1108 value = static_cast<int32_t>(mCore->mBufferAge);
1109 }
1110 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001111 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1112 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1113 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001114 default:
1115 return BAD_VALUE;
1116 }
1117
1118 BQ_LOGV("query: %d? %d", what, value);
1119 *outValue = value;
1120 return NO_ERROR;
1121}
1122
Dan Stozaf0eaf252014-03-21 13:05:51 -07001123status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001124 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1125 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001126 Mutex::Autolock lock(mCore->mMutex);
1127 mConsumerName = mCore->mConsumerName;
1128 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1129 producerControlledByApp ? "true" : "false");
1130
1131 if (mCore->mIsAbandoned) {
1132 BQ_LOGE("connect: BufferQueue has been abandoned");
1133 return NO_INIT;
1134 }
1135
1136 if (mCore->mConsumerListener == NULL) {
1137 BQ_LOGE("connect: BufferQueue has no consumer");
1138 return NO_INIT;
1139 }
1140
1141 if (output == NULL) {
1142 BQ_LOGE("connect: output was NULL");
1143 return BAD_VALUE;
1144 }
1145
1146 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1147 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1148 mCore->mConnectedApi, api);
1149 return BAD_VALUE;
1150 }
1151
1152 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1153 mDequeueTimeout < 0 ?
1154 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1155 mCore->mMaxBufferCount) -
1156 mCore->getMaxBufferCountLocked();
1157 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1158 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1159 "slots. Delta = %d", delta);
1160 return BAD_VALUE;
1161 }
1162
Dan Stoza289ade12014-02-28 11:17:17 -08001163 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001164 switch (api) {
1165 case NATIVE_WINDOW_API_EGL:
1166 case NATIVE_WINDOW_API_CPU:
1167 case NATIVE_WINDOW_API_MEDIA:
1168 case NATIVE_WINDOW_API_CAMERA:
1169 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001170
1171 output->width = mCore->mDefaultWidth;
1172 output->height = mCore->mDefaultHeight;
1173 output->transformHint = mCore->mTransformHint;
1174 output->numPendingBuffers =
1175 static_cast<uint32_t>(mCore->mQueue.size());
1176 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001177 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001178
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001179 if (listener != NULL) {
1180 // Set up a death notification so that we can disconnect
1181 // automatically if the remote producer dies
1182 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1183 status = IInterface::asBinder(listener)->linkToDeath(
1184 static_cast<IBinder::DeathRecipient*>(this));
1185 if (status != NO_ERROR) {
1186 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1187 strerror(-status), status);
1188 }
1189 mCore->mLinkedToDeath = listener;
1190 }
1191 if (listener->needsReleaseNotify()) {
1192 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001193 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001194 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001195 break;
1196 default:
1197 BQ_LOGE("connect: unknown API %d", api);
1198 status = BAD_VALUE;
1199 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001200 }
Robert Carr97b9c862016-09-08 13:54:35 -07001201 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001202 mCore->mBufferHasBeenQueued = false;
1203 mCore->mDequeueBufferCannotBlock = false;
1204 if (mDequeueTimeout < 0) {
1205 mCore->mDequeueBufferCannotBlock =
1206 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001207 }
Dan Stoza289ade12014-02-28 11:17:17 -08001208
Pablo Ceballos981066c2016-02-18 12:54:37 -08001209 mCore->mAllowAllocation = true;
1210 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001211 return status;
1212}
1213
Robert Carr97b9c862016-09-08 13:54:35 -07001214status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001215 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001216 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001217
1218 int status = NO_ERROR;
1219 sp<IConsumerListener> listener;
1220 { // Autolock scope
1221 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001222
1223 if (mode == DisconnectMode::AllLocal) {
1224 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1225 return NO_ERROR;
1226 }
1227 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1228 }
1229
Antoine Labour78014f32014-07-15 21:17:03 -07001230 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001231
1232 if (mCore->mIsAbandoned) {
1233 // It's not really an error to disconnect after the surface has
1234 // been abandoned; it should just be a no-op.
1235 return NO_ERROR;
1236 }
1237
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001238 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001239 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1240 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1241 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001242 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001243 // If we're asked to disconnect the currently connected api but
1244 // nobody is connected, it's not really an error.
1245 if (api == BufferQueueCore::NO_CONNECTED_API) {
1246 return NO_ERROR;
1247 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001248 }
1249
Dan Stoza289ade12014-02-28 11:17:17 -08001250 switch (api) {
1251 case NATIVE_WINDOW_API_EGL:
1252 case NATIVE_WINDOW_API_CPU:
1253 case NATIVE_WINDOW_API_MEDIA:
1254 case NATIVE_WINDOW_API_CAMERA:
1255 if (mCore->mConnectedApi == api) {
1256 mCore->freeAllBuffersLocked();
1257
1258 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001259 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001260 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001261 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001262 // This can fail if we're here because of the death
1263 // notification, but we just ignore it
1264 token->unlinkToDeath(
1265 static_cast<IBinder::DeathRecipient*>(this));
1266 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001267 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001268 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001269 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001270 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001271 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001272 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001273 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001274 mCore->mDequeueCondition.broadcast();
1275 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001276 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1277 BQ_LOGE("disconnect: not connected (req=%d)", api);
1278 status = NO_INIT;
1279 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001280 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001281 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001282 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001283 }
1284 break;
1285 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001286 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001287 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001288 break;
1289 }
1290 } // Autolock scope
1291
1292 // Call back without lock held
1293 if (listener != NULL) {
1294 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001295 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001296 }
1297
1298 return status;
1299}
1300
Jesse Hall399184a2014-03-03 15:42:54 -08001301status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001302 sp<IConsumerListener> listener;
1303 { // Autolock scope
1304 Mutex::Autolock _l(mCore->mMutex);
1305 mCore->mSidebandStream = stream;
1306 listener = mCore->mConsumerListener;
1307 } // Autolock scope
1308
1309 if (listener != NULL) {
1310 listener->onSidebandStreamChanged();
1311 }
Jesse Hall399184a2014-03-03 15:42:54 -08001312 return NO_ERROR;
1313}
1314
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001315void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1316 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001317 ATRACE_CALL();
1318 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001319 size_t newBufferCount = 0;
1320 uint32_t allocWidth = 0;
1321 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001322 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001323 uint32_t allocUsage = 0;
1324 { // Autolock scope
1325 Mutex::Autolock lock(mCore->mMutex);
1326 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001327
Dan Stoza9de72932015-04-16 17:28:43 -07001328 if (!mCore->mAllowAllocation) {
1329 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1330 "BufferQueue");
1331 return;
1332 }
1333
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001334 newBufferCount = mCore->mFreeSlots.size();
1335 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001336 return;
1337 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001338
Antoine Labour78014f32014-07-15 21:17:03 -07001339 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1340 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1341 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1342 allocUsage = usage | mCore->mConsumerUsageBits;
1343
1344 mCore->mIsAllocating = true;
1345 } // Autolock scope
1346
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001347 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001348 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001349 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001350 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chris Forbesf3ef3ea2017-04-20 12:43:28 -07001351 allocUsage, {mConsumerName.string(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -07001352
1353 status_t result = graphicBuffer->initCheck();
1354
Antoine Labour78014f32014-07-15 21:17:03 -07001355 if (result != NO_ERROR) {
1356 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1357 " %u, usage %u)", width, height, format, usage);
1358 Mutex::Autolock lock(mCore->mMutex);
1359 mCore->mIsAllocating = false;
1360 mCore->mIsAllocatingCondition.broadcast();
1361 return;
1362 }
1363 buffers.push_back(graphicBuffer);
1364 }
1365
1366 { // Autolock scope
1367 Mutex::Autolock lock(mCore->mMutex);
1368 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1369 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001370 PixelFormat checkFormat = format != 0 ?
1371 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001372 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1373 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1374 checkFormat != allocFormat || checkUsage != allocUsage) {
1375 // Something changed while we released the lock. Retry.
1376 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1377 mCore->mIsAllocating = false;
1378 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001379 continue;
1380 }
1381
Antoine Labour78014f32014-07-15 21:17:03 -07001382 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001383 if (mCore->mFreeSlots.empty()) {
1384 BQ_LOGV("allocateBuffers: a slot was occupied while "
1385 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001386 continue;
1387 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001388 auto slot = mCore->mFreeSlots.begin();
1389 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1390 mSlots[*slot].mGraphicBuffer = buffers[i];
1391 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001392
1393 // freeBufferLocked puts this slot on the free slots list. Since
1394 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001395 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001396
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001397 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1398 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001399
1400 // Make sure the erase is done after all uses of the slot
1401 // iterator since it will be invalid after this point.
1402 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001403 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001404
Antoine Labour78014f32014-07-15 21:17:03 -07001405 mCore->mIsAllocating = false;
1406 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001407 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001408 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001409 }
1410}
1411
Dan Stoza9de72932015-04-16 17:28:43 -07001412status_t BufferQueueProducer::allowAllocation(bool allow) {
1413 ATRACE_CALL();
1414 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1415
1416 Mutex::Autolock lock(mCore->mMutex);
1417 mCore->mAllowAllocation = allow;
1418 return NO_ERROR;
1419}
1420
Dan Stoza812ed062015-06-02 15:45:22 -07001421status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1422 ATRACE_CALL();
1423 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1424
1425 Mutex::Autolock lock(mCore->mMutex);
1426 mCore->mGenerationNumber = generationNumber;
1427 return NO_ERROR;
1428}
1429
Dan Stozac6f30bd2015-06-08 09:32:50 -07001430String8 BufferQueueProducer::getConsumerName() const {
1431 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001432 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001433 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1434 return mConsumerName;
1435}
1436
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001437status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001438 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001439 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001440
1441 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001442 if (!sharedBufferMode) {
1443 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001444 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001445 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001446 return NO_ERROR;
1447}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001448
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001449status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1450 ATRACE_CALL();
1451 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1452
1453 Mutex::Autolock lock(mCore->mMutex);
1454
1455 mCore->mAutoRefresh = autoRefresh;
1456 return NO_ERROR;
1457}
1458
Dan Stoza127fc632015-06-30 13:43:32 -07001459status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1460 ATRACE_CALL();
1461 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1462
Pablo Ceballos981066c2016-02-18 12:54:37 -08001463 Mutex::Autolock lock(mCore->mMutex);
1464 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1465 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1466 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1467 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1468 "available slots. Delta = %d", delta);
1469 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001470 }
1471
Pablo Ceballos981066c2016-02-18 12:54:37 -08001472 mDequeueTimeout = timeout;
1473 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001474
Pablo Ceballos981066c2016-02-18 12:54:37 -08001475 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001476 return NO_ERROR;
1477}
1478
Dan Stoza50101d02016-04-07 16:53:23 -07001479status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001480 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001481 ATRACE_CALL();
1482 BQ_LOGV("getLastQueuedBuffer");
1483
1484 Mutex::Autolock lock(mCore->mMutex);
1485 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1486 *outBuffer = nullptr;
1487 *outFence = Fence::NO_FENCE;
1488 return NO_ERROR;
1489 }
1490
1491 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1492 *outFence = mLastQueueBufferFence;
1493
John Reck1a61da52016-04-28 13:18:15 -07001494 // Currently only SurfaceFlinger internally ever changes
1495 // GLConsumer's filtering mode, so we just use 'true' here as
1496 // this is slightly specialized for the current client of this API,
1497 // which does want filtering.
1498 GLConsumer::computeTransformMatrix(outTransformMatrix,
1499 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1500 mLastQueuedTransform, true /* filter */);
1501
Dan Stoza50101d02016-04-07 16:53:23 -07001502 return NO_ERROR;
1503}
1504
Brian Anderson3890c392016-07-25 12:48:08 -07001505void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1506 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001507}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001508
Brian Anderson3890c392016-07-25 12:48:08 -07001509void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001510 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001511 FrameEventHistoryDelta* outDelta) {
1512 if (newTimestamps == nullptr && outDelta == nullptr) {
1513 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001514 }
1515
1516 ATRACE_CALL();
1517 BQ_LOGV("addAndGetFrameTimestamps");
1518 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001519 {
1520 Mutex::Autolock lock(mCore->mMutex);
1521 listener = mCore->mConsumerListener;
1522 }
1523 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001524 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001525 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001526}
1527
Dan Stoza289ade12014-02-28 11:17:17 -08001528void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1529 // If we're here, it means that a producer we were connected to died.
1530 // We're guaranteed that we are still connected to it because we remove
1531 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1532 // without synchronization here.
1533 int api = mCore->mConnectedApi;
1534 disconnect(api);
1535}
1536
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001537status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1538 BQ_LOGV("getUniqueId");
1539
1540 *outId = mCore->mUniqueId;
1541 return NO_ERROR;
1542}
1543
Dan Stoza289ade12014-02-28 11:17:17 -08001544} // namespace android