blob: 3d94a029e57a7440f1782685e3eb019289e41bee [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,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700352 PixelFormat format, uint64_t usage,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700353 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800354 ATRACE_CALL();
355 { // Autolock scope
356 Mutex::Autolock lock(mCore->mMutex);
357 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700358
359 if (mCore->mIsAbandoned) {
360 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
361 return NO_INIT;
362 }
363
364 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
365 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
366 return NO_INIT;
367 }
Dan Stoza289ade12014-02-28 11:17:17 -0800368 } // Autolock scope
369
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700370 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800371
372 if ((width && !height) || (!width && height)) {
373 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
374 return BAD_VALUE;
375 }
376
377 status_t returnFlags = NO_ERROR;
378 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
379 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800381
382 { // Autolock scope
383 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700384 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800385
386 if (format == 0) {
387 format = mCore->mDefaultBufferFormat;
388 }
389
390 // Enable the usage bits the consumer requested
391 usage |= mCore->mConsumerUsageBits;
392
Dan Stoza9de72932015-04-16 17:28:43 -0700393 const bool useDefaultSize = !width && !height;
394 if (useDefaultSize) {
395 width = mCore->mDefaultWidth;
396 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800397 }
Dan Stoza289ade12014-02-28 11:17:17 -0800398
Pablo Ceballos981066c2016-02-18 12:54:37 -0800399 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700400 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800401 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800402 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700403 if (status != NO_ERROR) {
404 return status;
405 }
406
407 // This should not happen
408 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
409 BQ_LOGE("dequeueBuffer: no available buffer slots");
410 return -EBUSY;
411 }
412
413 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
414
415 // If we are not allowed to allocate new buffers,
416 // waitForFreeSlotThenRelock must have returned a slot containing a
417 // buffer. If this buffer would require reallocation to meet the
418 // requested attributes, we free it and attempt to get another one.
419 if (!mCore->mAllowAllocation) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700420 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700421 if (mCore->mSharedBufferSlot == found) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700422 BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 return BAD_VALUE;
424 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800425 mCore->mFreeSlots.insert(found);
426 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700427 found = BufferItem::INVALID_BUFFER_SLOT;
428 continue;
429 }
430 }
Dan Stoza289ade12014-02-28 11:17:17 -0800431 }
432
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800433 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700434 if (mCore->mSharedBufferSlot == found &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700435 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800436 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
437 "buffer");
438
439 return BAD_VALUE;
440 }
441
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700442 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800443 mCore->mActiveBuffers.insert(found);
444 }
Dan Stoza289ade12014-02-28 11:17:17 -0800445 *outSlot = found;
446 ATRACE_BUFFER_INDEX(found);
447
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800448 attachedByConsumer = mSlots[found].mNeedsReallocation;
449 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800450
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700451 mSlots[found].mBufferState.dequeue();
452
Dan Stoza289ade12014-02-28 11:17:17 -0800453 if ((buffer == NULL) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700454 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800455 {
456 mSlots[found].mAcquireCalled = false;
457 mSlots[found].mGraphicBuffer = NULL;
458 mSlots[found].mRequestBufferCalled = false;
459 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
460 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
461 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800462 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700463 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800464
465 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800466 } else {
467 // We add 1 because that will be the frame number when this buffer
468 // is queued
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700469 mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800470 }
471
Dan Stoza800b41a2015-04-28 14:20:04 -0700472 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
473 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800474
Dan Stoza289ade12014-02-28 11:17:17 -0800475 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
476 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
477 "slot=%d w=%d h=%d format=%u",
478 found, buffer->width, buffer->height, buffer->format);
479 }
480
481 eglDisplay = mSlots[found].mEglDisplay;
482 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700483 // Don't return a fence in shared buffer mode, except for the first
484 // frame.
485 *outFence = (mCore->mSharedBufferMode &&
486 mCore->mSharedBufferSlot == found) ?
487 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800488 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
489 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700490
491 // If shared buffer mode has just been enabled, cache the slot of the
492 // first buffer that is dequeued and mark it as the shared buffer.
493 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
494 BufferQueueCore::INVALID_BUFFER_SLOT) {
495 mCore->mSharedBufferSlot = found;
496 mSlots[found].mBufferState.mShared = true;
497 }
Dan Stoza289ade12014-02-28 11:17:17 -0800498 } // Autolock scope
499
500 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700501 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700502 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700503 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700504 {mConsumerName.string(), mConsumerName.size()});
505
506 status_t error = graphicBuffer->initCheck();
507
Dan Stoza289ade12014-02-28 11:17:17 -0800508 { // Autolock scope
509 Mutex::Autolock lock(mCore->mMutex);
510
Chia-I Wufeec3b12017-05-25 09:34:56 -0700511 if (error == NO_ERROR && !mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700512 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
513 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
514 }
515
516 mCore->mIsAllocating = false;
517 mCore->mIsAllocatingCondition.broadcast();
518
Chia-I Wufeec3b12017-05-25 09:34:56 -0700519 if (error != NO_ERROR) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700520 mCore->mFreeSlots.insert(*outSlot);
521 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700522 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
523 return error;
524 }
525
Dan Stoza289ade12014-02-28 11:17:17 -0800526 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700527 mCore->mFreeSlots.insert(*outSlot);
528 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800529 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
530 return NO_INIT;
531 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800532
Pablo Ceballos9e314332016-01-12 13:49:19 -0800533 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800534 } // Autolock scope
535 }
536
Dan Stoza9f3053d2014-03-06 15:14:33 -0800537 if (attachedByConsumer) {
538 returnFlags |= BUFFER_NEEDS_REALLOCATION;
539 }
540
Dan Stoza289ade12014-02-28 11:17:17 -0800541 if (eglFence != EGL_NO_SYNC_KHR) {
542 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
543 1000000000);
544 // If something goes wrong, log the error, but return the buffer without
545 // synchronizing access to it. It's too late at this point to abort the
546 // dequeue operation.
547 if (result == EGL_FALSE) {
548 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
549 eglGetError());
550 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
551 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
552 }
553 eglDestroySyncKHR(eglDisplay, eglFence);
554 }
555
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700556 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
557 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800558 mSlots[*outSlot].mFrameNumber,
559 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
560
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700561 addAndGetFrameTimestamps(nullptr, outTimestamps);
562
Dan Stoza289ade12014-02-28 11:17:17 -0800563 return returnFlags;
564}
565
Dan Stoza9f3053d2014-03-06 15:14:33 -0800566status_t BufferQueueProducer::detachBuffer(int slot) {
567 ATRACE_CALL();
568 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700569 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800570
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700571 sp<IConsumerListener> listener;
572 {
573 Mutex::Autolock lock(mCore->mMutex);
574
575 if (mCore->mIsAbandoned) {
576 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
577 return NO_INIT;
578 }
579
580 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
581 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
582 return NO_INIT;
583 }
584
585 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
586 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
587 return BAD_VALUE;
588 }
589
590 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
591 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
592 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
593 return BAD_VALUE;
594 } else if (!mSlots[slot].mBufferState.isDequeued()) {
595 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
596 "(state = %s)", slot, mSlots[slot].mBufferState.string());
597 return BAD_VALUE;
598 } else if (!mSlots[slot].mRequestBufferCalled) {
599 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
600 slot);
601 return BAD_VALUE;
602 }
603
604 mSlots[slot].mBufferState.detachProducer();
605 mCore->mActiveBuffers.erase(slot);
606 mCore->mFreeSlots.insert(slot);
607 mCore->clearBufferSlotLocked(slot);
608 mCore->mDequeueCondition.broadcast();
609 VALIDATE_CONSISTENCY();
610 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800611 }
612
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700613 if (listener != NULL) {
614 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700615 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800616
Dan Stoza9f3053d2014-03-06 15:14:33 -0800617 return NO_ERROR;
618}
619
Dan Stozad9822a32014-03-28 15:25:31 -0700620status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
621 sp<Fence>* outFence) {
622 ATRACE_CALL();
623
624 if (outBuffer == NULL) {
625 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
626 return BAD_VALUE;
627 } else if (outFence == NULL) {
628 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
629 return BAD_VALUE;
630 }
631
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700632 sp<IConsumerListener> listener;
633 {
634 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700635
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700636 if (mCore->mIsAbandoned) {
637 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
638 return NO_INIT;
639 }
640
641 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
642 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
643 return NO_INIT;
644 }
645
646 if (mCore->mSharedBufferMode) {
647 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
648 "mode");
649 return BAD_VALUE;
650 }
651
652 mCore->waitWhileAllocatingLocked();
653
654 if (mCore->mFreeBuffers.empty()) {
655 return NO_MEMORY;
656 }
657
658 int found = mCore->mFreeBuffers.front();
659 mCore->mFreeBuffers.remove(found);
660 mCore->mFreeSlots.insert(found);
661
662 BQ_LOGV("detachNextBuffer detached slot %d", found);
663
664 *outBuffer = mSlots[found].mGraphicBuffer;
665 *outFence = mSlots[found].mFence;
666 mCore->clearBufferSlotLocked(found);
667 VALIDATE_CONSISTENCY();
668 listener = mCore->mConsumerListener;
Dan Stozad9822a32014-03-28 15:25:31 -0700669 }
670
Eino-Ville Talvala2672dec2017-06-13 16:39:11 -0700671 if (listener != NULL) {
672 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700673 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800674
Dan Stozad9822a32014-03-28 15:25:31 -0700675 return NO_ERROR;
676}
677
Dan Stoza9f3053d2014-03-06 15:14:33 -0800678status_t BufferQueueProducer::attachBuffer(int* outSlot,
679 const sp<android::GraphicBuffer>& buffer) {
680 ATRACE_CALL();
681
682 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700683 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800684 return BAD_VALUE;
685 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700686 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800687 return BAD_VALUE;
688 }
689
690 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700691
692 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700693 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700694 return NO_INIT;
695 }
696
697 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700698 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700699 return NO_INIT;
700 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800701
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700702 if (mCore->mSharedBufferMode) {
703 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700704 return BAD_VALUE;
705 }
706
Dan Stoza812ed062015-06-02 15:45:22 -0700707 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
708 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
709 "[queue %u]", buffer->getGenerationNumber(),
710 mCore->mGenerationNumber);
711 return BAD_VALUE;
712 }
713
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700714 mCore->waitWhileAllocatingLocked();
715
Dan Stoza9f3053d2014-03-06 15:14:33 -0800716 status_t returnFlags = NO_ERROR;
717 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800718 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800719 if (status != NO_ERROR) {
720 return status;
721 }
722
723 // This should not happen
724 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700725 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800726 return -EBUSY;
727 }
728
729 *outSlot = found;
730 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700731 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800732 *outSlot, returnFlags);
733
734 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700735 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800736 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
737 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700738 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800739 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800740 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800741 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800742 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700743
Dan Stoza9f3053d2014-03-06 15:14:33 -0800744 return returnFlags;
745}
746
Dan Stoza289ade12014-02-28 11:17:17 -0800747status_t BufferQueueProducer::queueBuffer(int slot,
748 const QueueBufferInput &input, QueueBufferOutput *output) {
749 ATRACE_CALL();
750 ATRACE_BUFFER_INDEX(slot);
751
Brian Andersond6927fb2016-07-23 23:37:30 -0700752 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800753 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800754 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700755 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800756 int scalingMode;
757 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700758 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700759 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700760 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700761 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
763 &getFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700764 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800765
Brian Andersond6927fb2016-07-23 23:37:30 -0700766 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800767 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800768 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800769 }
770
Brian Anderson3d4039d2016-09-23 16:31:30 -0700771 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
772
Dan Stoza289ade12014-02-28 11:17:17 -0800773 switch (scalingMode) {
774 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
775 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
776 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
777 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
778 break;
779 default:
780 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800781 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800782 }
783
Dan Stoza8dc55392014-11-04 11:37:46 -0800784 sp<IConsumerListener> frameAvailableListener;
785 sp<IConsumerListener> frameReplacedListener;
786 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700787 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800788 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800789 { // Autolock scope
790 Mutex::Autolock lock(mCore->mMutex);
791
792 if (mCore->mIsAbandoned) {
793 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
794 return NO_INIT;
795 }
796
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700797 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
798 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
799 return NO_INIT;
800 }
801
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800802 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800803 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800804 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800805 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700806 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800807 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700808 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800809 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800810 } else if (!mSlots[slot].mRequestBufferCalled) {
811 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
812 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800813 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800814 }
815
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700816 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700817 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700818 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700819 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700820 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700821 mSlots[slot].mBufferState.mShared = true;
822 }
823
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800824 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700825 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Brian Andersond6927fb2016-07-23 23:37:30 -0700826 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp,
827 dataSpace, crop.left, crop.top, crop.right, crop.bottom,
828 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800829 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800830
831 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
832 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700833 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800834 crop.intersect(bufferRect, &croppedRect);
835 if (croppedRect != crop) {
836 BQ_LOGE("queueBuffer: crop rect is not contained within the "
837 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800838 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800839 }
840
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800841 // Override UNKNOWN dataspace with consumer default
842 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
843 dataSpace = mCore->mDefaultBufferDataSpace;
844 }
845
Brian Andersond6927fb2016-07-23 23:37:30 -0700846 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700847 mSlots[slot].mBufferState.queue();
848
Brian Andersond6927fb2016-07-23 23:37:30 -0700849 // Increment the frame counter and store a local version of it
850 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800851 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700852 currentFrameNumber = mCore->mFrameCounter;
853 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800854
Dan Stoza289ade12014-02-28 11:17:17 -0800855 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
856 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
857 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 item.mTransform = transform &
859 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800860 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800861 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
862 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700863 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800864 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800865 item.mDataSpace = dataSpace;
Brian Andersond6927fb2016-07-23 23:37:30 -0700866 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800867 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700868 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700869 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700870 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700872 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700873 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700874 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700875 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800876
Ruben Brunk1681d952014-06-27 15:51:55 -0700877 mStickyTransform = stickyTransform;
878
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700879 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700880 if (mCore->mSharedBufferMode) {
881 mCore->mSharedBufferCache.crop = crop;
882 mCore->mSharedBufferCache.transform = transform;
883 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700884 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700885 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700886 }
887
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800888 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800889 if (mCore->mQueue.empty()) {
890 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
891 // and simply queue this buffer
892 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800893 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800894 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700895 // When the queue is not empty, we need to look at the last buffer
896 // in the queue to see if we need to replace it
897 const BufferItem& last = mCore->mQueue.itemAt(
898 mCore->mQueue.size() - 1);
899 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800900
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700901 if (!last.mIsStale) {
902 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700903
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700904 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700905 // still be around. Mark it as no longer shared if this
906 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700907 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700908 mSlots[last.mSlot].mBufferState.isFree()) {
909 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 }
911 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700912 if (!mSlots[last.mSlot].mBufferState.isShared()) {
913 mCore->mActiveBuffers.erase(last.mSlot);
914 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800915 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700916 }
Dan Stoza289ade12014-02-28 11:17:17 -0800917 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800918
Dan Stoza289ade12014-02-28 11:17:17 -0800919 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700920 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800921 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800922 } else {
923 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800924 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800925 }
926 }
927
928 mCore->mBufferHasBeenQueued = true;
929 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700930 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800931
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700932 output->width = mCore->mDefaultWidth;
933 output->height = mCore->mDefaultHeight;
934 output->transformHint = mCore->mTransformHint;
935 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
936 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800937
Colin Cross152c3b72016-09-27 14:08:19 -0700938 ATRACE_INT(mCore->mConsumerName.string(),
939 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700940 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800941
942 // Take a ticket for the callback functions
943 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700944
Pablo Ceballos9e314332016-01-12 13:49:19 -0800945 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800946 } // Autolock scope
947
Irvel468051e2016-06-13 16:44:44 -0700948 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
949 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
950 // there will be no Binder call
951 if (!mConsumerIsSurfaceFlinger) {
952 item.mGraphicBuffer.clear();
953 }
954
955 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800956 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
957
958 // Call back without the main BufferQueue lock held, but with the callback
959 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700960
961 int connectedApi;
962 sp<Fence> lastQueuedFence;
963
964 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800965 Mutex::Autolock lock(mCallbackMutex);
966 while (callbackTicket != mCurrentCallbackTicket) {
967 mCallbackCondition.wait(mCallbackMutex);
968 }
969
970 if (frameAvailableListener != NULL) {
971 frameAvailableListener->onFrameAvailable(item);
972 } else if (frameReplacedListener != NULL) {
973 frameReplacedListener->onFrameReplaced(item);
974 }
975
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700976 connectedApi = mCore->mConnectedApi;
977 lastQueuedFence = std::move(mLastQueueBufferFence);
978
979 mLastQueueBufferFence = std::move(acquireFence);
980 mLastQueuedCrop = item.mCrop;
981 mLastQueuedTransform = item.mTransform;
982
Dan Stoza8dc55392014-11-04 11:37:46 -0800983 ++mCurrentCallbackTicket;
984 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800985 }
986
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000987 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700988 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000989 // Waiting here allows for two full buffers to be queued but not a
990 // third. In the event that frames take varying time, this makes a
991 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700992 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000993 }
994
Brian Andersond6927fb2016-07-23 23:37:30 -0700995 // Update and get FrameEventHistory.
996 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
997 NewFrameEventsEntry newFrameEventsEntry = {
998 currentFrameNumber,
999 postedTime,
1000 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -07001001 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -07001002 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001003 addAndGetFrameTimestamps(&newFrameEventsEntry,
1004 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001005
Dan Stoza289ade12014-02-28 11:17:17 -08001006 return NO_ERROR;
1007}
1008
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001009status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001010 ATRACE_CALL();
1011 BQ_LOGV("cancelBuffer: slot %d", slot);
1012 Mutex::Autolock lock(mCore->mMutex);
1013
1014 if (mCore->mIsAbandoned) {
1015 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001016 return NO_INIT;
1017 }
1018
1019 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1020 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1021 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001022 }
1023
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001024 if (mCore->mSharedBufferMode) {
1025 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001026 return BAD_VALUE;
1027 }
1028
Dan Stoza3e96f192014-03-03 10:16:19 -08001029 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001030 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001031 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001032 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001033 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001034 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001035 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001036 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001037 } else if (fence == NULL) {
1038 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001039 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001040 }
1041
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001042 mSlots[slot].mBufferState.cancel();
1043
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001044 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001045 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001046 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001047 mSlots[slot].mBufferState.mShared = false;
1048 }
1049
1050 // Don't put the shared buffer on the free list.
1051 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001052 mCore->mActiveBuffers.erase(slot);
1053 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001054 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001055
Dan Stoza289ade12014-02-28 11:17:17 -08001056 mSlots[slot].mFence = fence;
1057 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001058 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001059
1060 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001061}
1062
1063int BufferQueueProducer::query(int what, int *outValue) {
1064 ATRACE_CALL();
1065 Mutex::Autolock lock(mCore->mMutex);
1066
1067 if (outValue == NULL) {
1068 BQ_LOGE("query: outValue was NULL");
1069 return BAD_VALUE;
1070 }
1071
1072 if (mCore->mIsAbandoned) {
1073 BQ_LOGE("query: BufferQueue has been abandoned");
1074 return NO_INIT;
1075 }
1076
1077 int value;
1078 switch (what) {
1079 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001080 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001081 break;
1082 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001083 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001084 break;
1085 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001086 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001087 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001088 case NATIVE_WINDOW_LAYER_COUNT:
1089 // All BufferQueue buffers have a single layer.
1090 value = BQ_LAYER_COUNT;
1091 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001092 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001093 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001094 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001095 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001096 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001097 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001098 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1099 value = (mCore->mQueue.size() > 1);
1100 break;
1101 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001102 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001103 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001104 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1105 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1106 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001107 case NATIVE_WINDOW_BUFFER_AGE:
1108 if (mCore->mBufferAge > INT32_MAX) {
1109 value = 0;
1110 } else {
1111 value = static_cast<int32_t>(mCore->mBufferAge);
1112 }
1113 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001114 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1115 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1116 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001117 default:
1118 return BAD_VALUE;
1119 }
1120
1121 BQ_LOGV("query: %d? %d", what, value);
1122 *outValue = value;
1123 return NO_ERROR;
1124}
1125
Dan Stozaf0eaf252014-03-21 13:05:51 -07001126status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001127 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1128 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001129 Mutex::Autolock lock(mCore->mMutex);
1130 mConsumerName = mCore->mConsumerName;
1131 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1132 producerControlledByApp ? "true" : "false");
1133
1134 if (mCore->mIsAbandoned) {
1135 BQ_LOGE("connect: BufferQueue has been abandoned");
1136 return NO_INIT;
1137 }
1138
1139 if (mCore->mConsumerListener == NULL) {
1140 BQ_LOGE("connect: BufferQueue has no consumer");
1141 return NO_INIT;
1142 }
1143
1144 if (output == NULL) {
1145 BQ_LOGE("connect: output was NULL");
1146 return BAD_VALUE;
1147 }
1148
1149 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1150 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1151 mCore->mConnectedApi, api);
1152 return BAD_VALUE;
1153 }
1154
1155 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1156 mDequeueTimeout < 0 ?
1157 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1158 mCore->mMaxBufferCount) -
1159 mCore->getMaxBufferCountLocked();
1160 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1161 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1162 "slots. Delta = %d", delta);
1163 return BAD_VALUE;
1164 }
1165
Dan Stoza289ade12014-02-28 11:17:17 -08001166 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001167 switch (api) {
1168 case NATIVE_WINDOW_API_EGL:
1169 case NATIVE_WINDOW_API_CPU:
1170 case NATIVE_WINDOW_API_MEDIA:
1171 case NATIVE_WINDOW_API_CAMERA:
1172 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001173
1174 output->width = mCore->mDefaultWidth;
1175 output->height = mCore->mDefaultHeight;
1176 output->transformHint = mCore->mTransformHint;
1177 output->numPendingBuffers =
1178 static_cast<uint32_t>(mCore->mQueue.size());
1179 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001180 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001181
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001182 if (listener != NULL) {
1183 // Set up a death notification so that we can disconnect
1184 // automatically if the remote producer dies
1185 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1186 status = IInterface::asBinder(listener)->linkToDeath(
1187 static_cast<IBinder::DeathRecipient*>(this));
1188 if (status != NO_ERROR) {
1189 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1190 strerror(-status), status);
1191 }
1192 mCore->mLinkedToDeath = listener;
1193 }
1194 if (listener->needsReleaseNotify()) {
1195 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001196 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001197 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001198 break;
1199 default:
1200 BQ_LOGE("connect: unknown API %d", api);
1201 status = BAD_VALUE;
1202 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001203 }
Robert Carr97b9c862016-09-08 13:54:35 -07001204 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001205 mCore->mBufferHasBeenQueued = false;
1206 mCore->mDequeueBufferCannotBlock = false;
1207 if (mDequeueTimeout < 0) {
1208 mCore->mDequeueBufferCannotBlock =
1209 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001210 }
Dan Stoza289ade12014-02-28 11:17:17 -08001211
Pablo Ceballos981066c2016-02-18 12:54:37 -08001212 mCore->mAllowAllocation = true;
1213 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001214 return status;
1215}
1216
Robert Carr97b9c862016-09-08 13:54:35 -07001217status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001218 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001219 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001220
1221 int status = NO_ERROR;
1222 sp<IConsumerListener> listener;
1223 { // Autolock scope
1224 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001225
1226 if (mode == DisconnectMode::AllLocal) {
1227 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1228 return NO_ERROR;
1229 }
1230 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1231 }
1232
Antoine Labour78014f32014-07-15 21:17:03 -07001233 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001234
1235 if (mCore->mIsAbandoned) {
1236 // It's not really an error to disconnect after the surface has
1237 // been abandoned; it should just be a no-op.
1238 return NO_ERROR;
1239 }
1240
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001241 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001242 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1243 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1244 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001245 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001246 // If we're asked to disconnect the currently connected api but
1247 // nobody is connected, it's not really an error.
1248 if (api == BufferQueueCore::NO_CONNECTED_API) {
1249 return NO_ERROR;
1250 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001251 }
1252
Dan Stoza289ade12014-02-28 11:17:17 -08001253 switch (api) {
1254 case NATIVE_WINDOW_API_EGL:
1255 case NATIVE_WINDOW_API_CPU:
1256 case NATIVE_WINDOW_API_MEDIA:
1257 case NATIVE_WINDOW_API_CAMERA:
1258 if (mCore->mConnectedApi == api) {
1259 mCore->freeAllBuffersLocked();
1260
1261 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001262 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001263 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001264 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001265 // This can fail if we're here because of the death
1266 // notification, but we just ignore it
1267 token->unlinkToDeath(
1268 static_cast<IBinder::DeathRecipient*>(this));
1269 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001270 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001271 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001272 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001273 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001274 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001275 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001276 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001277 mCore->mDequeueCondition.broadcast();
1278 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001279 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1280 BQ_LOGE("disconnect: not connected (req=%d)", api);
1281 status = NO_INIT;
1282 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001283 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001284 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001285 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001286 }
1287 break;
1288 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001289 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001290 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001291 break;
1292 }
1293 } // Autolock scope
1294
1295 // Call back without lock held
1296 if (listener != NULL) {
1297 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001298 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001299 }
1300
1301 return status;
1302}
1303
Jesse Hall399184a2014-03-03 15:42:54 -08001304status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001305 sp<IConsumerListener> listener;
1306 { // Autolock scope
1307 Mutex::Autolock _l(mCore->mMutex);
1308 mCore->mSidebandStream = stream;
1309 listener = mCore->mConsumerListener;
1310 } // Autolock scope
1311
1312 if (listener != NULL) {
1313 listener->onSidebandStreamChanged();
1314 }
Jesse Hall399184a2014-03-03 15:42:54 -08001315 return NO_ERROR;
1316}
1317
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001318void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001319 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001320 ATRACE_CALL();
1321 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001322 size_t newBufferCount = 0;
1323 uint32_t allocWidth = 0;
1324 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001325 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001326 uint64_t allocUsage = 0;
Antoine Labour78014f32014-07-15 21:17:03 -07001327 { // Autolock scope
1328 Mutex::Autolock lock(mCore->mMutex);
1329 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001330
Dan Stoza9de72932015-04-16 17:28:43 -07001331 if (!mCore->mAllowAllocation) {
1332 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1333 "BufferQueue");
1334 return;
1335 }
1336
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001337 newBufferCount = mCore->mFreeSlots.size();
1338 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001339 return;
1340 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001341
Antoine Labour78014f32014-07-15 21:17:03 -07001342 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1343 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1344 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1345 allocUsage = usage | mCore->mConsumerUsageBits;
1346
1347 mCore->mIsAllocating = true;
1348 } // Autolock scope
1349
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001350 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001351 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001352 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001353 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chris Forbesf3ef3ea2017-04-20 12:43:28 -07001354 allocUsage, {mConsumerName.string(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -07001355
1356 status_t result = graphicBuffer->initCheck();
1357
Antoine Labour78014f32014-07-15 21:17:03 -07001358 if (result != NO_ERROR) {
1359 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001360 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Antoine Labour78014f32014-07-15 21:17:03 -07001361 Mutex::Autolock lock(mCore->mMutex);
1362 mCore->mIsAllocating = false;
1363 mCore->mIsAllocatingCondition.broadcast();
1364 return;
1365 }
1366 buffers.push_back(graphicBuffer);
1367 }
1368
1369 { // Autolock scope
1370 Mutex::Autolock lock(mCore->mMutex);
1371 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1372 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001373 PixelFormat checkFormat = format != 0 ?
1374 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001375 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001376 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1377 checkFormat != allocFormat || checkUsage != allocUsage) {
1378 // Something changed while we released the lock. Retry.
1379 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1380 mCore->mIsAllocating = false;
1381 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001382 continue;
1383 }
1384
Antoine Labour78014f32014-07-15 21:17:03 -07001385 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001386 if (mCore->mFreeSlots.empty()) {
1387 BQ_LOGV("allocateBuffers: a slot was occupied while "
1388 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001389 continue;
1390 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001391 auto slot = mCore->mFreeSlots.begin();
1392 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1393 mSlots[*slot].mGraphicBuffer = buffers[i];
1394 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001395
1396 // freeBufferLocked puts this slot on the free slots list. Since
1397 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001398 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001399
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001400 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1401 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001402
1403 // Make sure the erase is done after all uses of the slot
1404 // iterator since it will be invalid after this point.
1405 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001406 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001407
Antoine Labour78014f32014-07-15 21:17:03 -07001408 mCore->mIsAllocating = false;
1409 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001410 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001411 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001412 }
1413}
1414
Dan Stoza9de72932015-04-16 17:28:43 -07001415status_t BufferQueueProducer::allowAllocation(bool allow) {
1416 ATRACE_CALL();
1417 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1418
1419 Mutex::Autolock lock(mCore->mMutex);
1420 mCore->mAllowAllocation = allow;
1421 return NO_ERROR;
1422}
1423
Dan Stoza812ed062015-06-02 15:45:22 -07001424status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1425 ATRACE_CALL();
1426 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1427
1428 Mutex::Autolock lock(mCore->mMutex);
1429 mCore->mGenerationNumber = generationNumber;
1430 return NO_ERROR;
1431}
1432
Dan Stozac6f30bd2015-06-08 09:32:50 -07001433String8 BufferQueueProducer::getConsumerName() const {
1434 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001435 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001436 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1437 return mConsumerName;
1438}
1439
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001440status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001441 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001442 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001443
1444 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001445 if (!sharedBufferMode) {
1446 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001447 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001448 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001449 return NO_ERROR;
1450}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001451
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001452status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1453 ATRACE_CALL();
1454 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1455
1456 Mutex::Autolock lock(mCore->mMutex);
1457
1458 mCore->mAutoRefresh = autoRefresh;
1459 return NO_ERROR;
1460}
1461
Dan Stoza127fc632015-06-30 13:43:32 -07001462status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1463 ATRACE_CALL();
1464 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1465
Pablo Ceballos981066c2016-02-18 12:54:37 -08001466 Mutex::Autolock lock(mCore->mMutex);
1467 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1468 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1469 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1470 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1471 "available slots. Delta = %d", delta);
1472 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001473 }
1474
Pablo Ceballos981066c2016-02-18 12:54:37 -08001475 mDequeueTimeout = timeout;
1476 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001477
Pablo Ceballos981066c2016-02-18 12:54:37 -08001478 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001479 return NO_ERROR;
1480}
1481
Dan Stoza50101d02016-04-07 16:53:23 -07001482status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001483 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001484 ATRACE_CALL();
1485 BQ_LOGV("getLastQueuedBuffer");
1486
1487 Mutex::Autolock lock(mCore->mMutex);
1488 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1489 *outBuffer = nullptr;
1490 *outFence = Fence::NO_FENCE;
1491 return NO_ERROR;
1492 }
1493
1494 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1495 *outFence = mLastQueueBufferFence;
1496
John Reck1a61da52016-04-28 13:18:15 -07001497 // Currently only SurfaceFlinger internally ever changes
1498 // GLConsumer's filtering mode, so we just use 'true' here as
1499 // this is slightly specialized for the current client of this API,
1500 // which does want filtering.
1501 GLConsumer::computeTransformMatrix(outTransformMatrix,
1502 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1503 mLastQueuedTransform, true /* filter */);
1504
Dan Stoza50101d02016-04-07 16:53:23 -07001505 return NO_ERROR;
1506}
1507
Brian Anderson3890c392016-07-25 12:48:08 -07001508void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1509 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001510}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001511
Brian Anderson3890c392016-07-25 12:48:08 -07001512void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001513 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001514 FrameEventHistoryDelta* outDelta) {
1515 if (newTimestamps == nullptr && outDelta == nullptr) {
1516 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001517 }
1518
1519 ATRACE_CALL();
1520 BQ_LOGV("addAndGetFrameTimestamps");
1521 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001522 {
1523 Mutex::Autolock lock(mCore->mMutex);
1524 listener = mCore->mConsumerListener;
1525 }
1526 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001527 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001528 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001529}
1530
Dan Stoza289ade12014-02-28 11:17:17 -08001531void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1532 // If we're here, it means that a producer we were connected to died.
1533 // We're guaranteed that we are still connected to it because we remove
1534 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1535 // without synchronization here.
1536 int api = mCore->mConnectedApi;
1537 disconnect(api);
1538}
1539
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001540status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1541 BQ_LOGV("getUniqueId");
1542
1543 *outId = mCore->mUniqueId;
1544 return NO_ERROR;
1545}
1546
Dan Stoza289ade12014-02-28 11:17:17 -08001547} // namespace android