blob: b39ecc5856c85ecf443fcb22f096cba8efabeac0 [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
Pablo Ceballos981066c2016-02-18 12:54:37 -0800632 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700633
Pablo Ceballos981066c2016-02-18 12:54:37 -0800634 if (mCore->mIsAbandoned) {
635 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
636 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700637 }
638
Pablo Ceballos981066c2016-02-18 12:54:37 -0800639 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
640 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
641 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700642 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800643
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700644 if (mCore->mSharedBufferMode) {
645 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
646 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800647 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700648 }
649
Pablo Ceballos981066c2016-02-18 12:54:37 -0800650 mCore->waitWhileAllocatingLocked();
651
652 if (mCore->mFreeBuffers.empty()) {
653 return NO_MEMORY;
654 }
655
656 int found = mCore->mFreeBuffers.front();
657 mCore->mFreeBuffers.remove(found);
658 mCore->mFreeSlots.insert(found);
659
660 BQ_LOGV("detachNextBuffer detached slot %d", found);
661
662 *outBuffer = mSlots[found].mGraphicBuffer;
663 *outFence = mSlots[found].mFence;
664 mCore->clearBufferSlotLocked(found);
665 VALIDATE_CONSISTENCY();
666
Dan Stozad9822a32014-03-28 15:25:31 -0700667 return NO_ERROR;
668}
669
Dan Stoza9f3053d2014-03-06 15:14:33 -0800670status_t BufferQueueProducer::attachBuffer(int* outSlot,
671 const sp<android::GraphicBuffer>& buffer) {
672 ATRACE_CALL();
673
674 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700675 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800676 return BAD_VALUE;
677 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700678 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800679 return BAD_VALUE;
680 }
681
682 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700683
684 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700685 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700686 return NO_INIT;
687 }
688
689 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700690 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700691 return NO_INIT;
692 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800693
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700694 if (mCore->mSharedBufferMode) {
695 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700696 return BAD_VALUE;
697 }
698
Dan Stoza812ed062015-06-02 15:45:22 -0700699 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
700 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
701 "[queue %u]", buffer->getGenerationNumber(),
702 mCore->mGenerationNumber);
703 return BAD_VALUE;
704 }
705
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700706 mCore->waitWhileAllocatingLocked();
707
Dan Stoza9f3053d2014-03-06 15:14:33 -0800708 status_t returnFlags = NO_ERROR;
709 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800710 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800711 if (status != NO_ERROR) {
712 return status;
713 }
714
715 // This should not happen
716 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700717 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800718 return -EBUSY;
719 }
720
721 *outSlot = found;
722 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700723 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800724 *outSlot, returnFlags);
725
726 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700727 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800728 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
729 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700730 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800731 mSlots[*outSlot].mAcquireCalled = false;
Jammy Yu69958b82017-02-22 16:41:38 -0800732 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800733 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800734 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700735
Dan Stoza9f3053d2014-03-06 15:14:33 -0800736 return returnFlags;
737}
738
Dan Stoza289ade12014-02-28 11:17:17 -0800739status_t BufferQueueProducer::queueBuffer(int slot,
740 const QueueBufferInput &input, QueueBufferOutput *output) {
741 ATRACE_CALL();
742 ATRACE_BUFFER_INDEX(slot);
743
Brian Andersond6927fb2016-07-23 23:37:30 -0700744 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800745 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800746 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700747 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800748 int scalingMode;
749 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700750 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700751 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700752 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700753 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700754 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
755 &getFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700756 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800757
Brian Andersond6927fb2016-07-23 23:37:30 -0700758 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800759 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800760 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800761 }
762
Brian Anderson3d4039d2016-09-23 16:31:30 -0700763 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
764
Dan Stoza289ade12014-02-28 11:17:17 -0800765 switch (scalingMode) {
766 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
767 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
768 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
769 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
770 break;
771 default:
772 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800773 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800774 }
775
Dan Stoza8dc55392014-11-04 11:37:46 -0800776 sp<IConsumerListener> frameAvailableListener;
777 sp<IConsumerListener> frameReplacedListener;
778 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700779 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800780 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800781 { // Autolock scope
782 Mutex::Autolock lock(mCore->mMutex);
783
784 if (mCore->mIsAbandoned) {
785 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
786 return NO_INIT;
787 }
788
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700789 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
790 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
791 return NO_INIT;
792 }
793
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800794 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800795 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800796 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800797 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700798 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800799 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700800 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800801 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800802 } else if (!mSlots[slot].mRequestBufferCalled) {
803 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
804 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800805 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800806 }
807
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700808 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700809 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700810 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700811 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700812 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700813 mSlots[slot].mBufferState.mShared = true;
814 }
815
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800816 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700817 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Brian Andersond6927fb2016-07-23 23:37:30 -0700818 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp,
819 dataSpace, crop.left, crop.top, crop.right, crop.bottom,
820 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800821 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800822
823 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
824 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700825 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800826 crop.intersect(bufferRect, &croppedRect);
827 if (croppedRect != crop) {
828 BQ_LOGE("queueBuffer: crop rect is not contained within the "
829 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800830 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800831 }
832
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800833 // Override UNKNOWN dataspace with consumer default
834 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
835 dataSpace = mCore->mDefaultBufferDataSpace;
836 }
837
Brian Andersond6927fb2016-07-23 23:37:30 -0700838 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700839 mSlots[slot].mBufferState.queue();
840
Brian Andersond6927fb2016-07-23 23:37:30 -0700841 // Increment the frame counter and store a local version of it
842 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800843 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700844 currentFrameNumber = mCore->mFrameCounter;
845 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800846
Dan Stoza289ade12014-02-28 11:17:17 -0800847 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
848 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
849 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800850 item.mTransform = transform &
851 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800852 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800853 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
854 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700855 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800856 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800857 item.mDataSpace = dataSpace;
Brian Andersond6927fb2016-07-23 23:37:30 -0700858 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800859 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700860 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700861 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700862 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700863 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700864 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700865 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700866 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700867 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800868
Ruben Brunk1681d952014-06-27 15:51:55 -0700869 mStickyTransform = stickyTransform;
870
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700871 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700872 if (mCore->mSharedBufferMode) {
873 mCore->mSharedBufferCache.crop = crop;
874 mCore->mSharedBufferCache.transform = transform;
875 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700876 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700877 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 }
879
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800880 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800881 if (mCore->mQueue.empty()) {
882 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
883 // and simply queue this buffer
884 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800885 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800886 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700887 // When the queue is not empty, we need to look at the last buffer
888 // in the queue to see if we need to replace it
889 const BufferItem& last = mCore->mQueue.itemAt(
890 mCore->mQueue.size() - 1);
891 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800892
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700893 if (!last.mIsStale) {
894 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700895
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700896 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700897 // still be around. Mark it as no longer shared if this
898 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700899 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700900 mSlots[last.mSlot].mBufferState.isFree()) {
901 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700902 }
903 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700904 if (!mSlots[last.mSlot].mBufferState.isShared()) {
905 mCore->mActiveBuffers.erase(last.mSlot);
906 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800907 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700908 }
Dan Stoza289ade12014-02-28 11:17:17 -0800909 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800910
Dan Stoza289ade12014-02-28 11:17:17 -0800911 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700912 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800913 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800914 } else {
915 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800916 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800917 }
918 }
919
920 mCore->mBufferHasBeenQueued = true;
921 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700922 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800923
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700924 output->width = mCore->mDefaultWidth;
925 output->height = mCore->mDefaultHeight;
926 output->transformHint = mCore->mTransformHint;
927 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
928 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800929
Colin Cross152c3b72016-09-27 14:08:19 -0700930 ATRACE_INT(mCore->mConsumerName.string(),
931 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700932 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800933
934 // Take a ticket for the callback functions
935 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700936
Pablo Ceballos9e314332016-01-12 13:49:19 -0800937 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800938 } // Autolock scope
939
Irvel468051e2016-06-13 16:44:44 -0700940 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
941 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
942 // there will be no Binder call
943 if (!mConsumerIsSurfaceFlinger) {
944 item.mGraphicBuffer.clear();
945 }
946
947 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800948 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
949
950 // Call back without the main BufferQueue lock held, but with the callback
951 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700952
953 int connectedApi;
954 sp<Fence> lastQueuedFence;
955
956 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800957 Mutex::Autolock lock(mCallbackMutex);
958 while (callbackTicket != mCurrentCallbackTicket) {
959 mCallbackCondition.wait(mCallbackMutex);
960 }
961
962 if (frameAvailableListener != NULL) {
963 frameAvailableListener->onFrameAvailable(item);
964 } else if (frameReplacedListener != NULL) {
965 frameReplacedListener->onFrameReplaced(item);
966 }
967
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700968 connectedApi = mCore->mConnectedApi;
969 lastQueuedFence = std::move(mLastQueueBufferFence);
970
971 mLastQueueBufferFence = std::move(acquireFence);
972 mLastQueuedCrop = item.mCrop;
973 mLastQueuedTransform = item.mTransform;
974
Dan Stoza8dc55392014-11-04 11:37:46 -0800975 ++mCurrentCallbackTicket;
976 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800977 }
978
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000979 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700980 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000981 // Waiting here allows for two full buffers to be queued but not a
982 // third. In the event that frames take varying time, this makes a
983 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700984 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000985 }
986
Brian Andersond6927fb2016-07-23 23:37:30 -0700987 // Update and get FrameEventHistory.
988 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
989 NewFrameEventsEntry newFrameEventsEntry = {
990 currentFrameNumber,
991 postedTime,
992 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700993 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -0700994 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700995 addAndGetFrameTimestamps(&newFrameEventsEntry,
996 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -0700997
Dan Stoza289ade12014-02-28 11:17:17 -0800998 return NO_ERROR;
999}
1000
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001001status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001002 ATRACE_CALL();
1003 BQ_LOGV("cancelBuffer: slot %d", slot);
1004 Mutex::Autolock lock(mCore->mMutex);
1005
1006 if (mCore->mIsAbandoned) {
1007 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001008 return NO_INIT;
1009 }
1010
1011 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1012 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1013 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001014 }
1015
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001016 if (mCore->mSharedBufferMode) {
1017 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001018 return BAD_VALUE;
1019 }
1020
Dan Stoza3e96f192014-03-03 10:16:19 -08001021 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001022 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001023 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001024 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001025 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001026 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001027 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001028 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001029 } else if (fence == NULL) {
1030 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001031 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001032 }
1033
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001034 mSlots[slot].mBufferState.cancel();
1035
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001036 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001037 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001038 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001039 mSlots[slot].mBufferState.mShared = false;
1040 }
1041
1042 // Don't put the shared buffer on the free list.
1043 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001044 mCore->mActiveBuffers.erase(slot);
1045 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001046 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001047
Dan Stoza289ade12014-02-28 11:17:17 -08001048 mSlots[slot].mFence = fence;
1049 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001050 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001051
1052 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001053}
1054
1055int BufferQueueProducer::query(int what, int *outValue) {
1056 ATRACE_CALL();
1057 Mutex::Autolock lock(mCore->mMutex);
1058
1059 if (outValue == NULL) {
1060 BQ_LOGE("query: outValue was NULL");
1061 return BAD_VALUE;
1062 }
1063
1064 if (mCore->mIsAbandoned) {
1065 BQ_LOGE("query: BufferQueue has been abandoned");
1066 return NO_INIT;
1067 }
1068
1069 int value;
1070 switch (what) {
1071 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001072 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001073 break;
1074 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001075 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001076 break;
1077 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001078 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001079 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001080 case NATIVE_WINDOW_LAYER_COUNT:
1081 // All BufferQueue buffers have a single layer.
1082 value = BQ_LAYER_COUNT;
1083 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001084 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001085 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001086 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001087 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001088 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001089 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001090 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1091 value = (mCore->mQueue.size() > 1);
1092 break;
1093 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001094 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001095 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001096 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1097 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1098 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001099 case NATIVE_WINDOW_BUFFER_AGE:
1100 if (mCore->mBufferAge > INT32_MAX) {
1101 value = 0;
1102 } else {
1103 value = static_cast<int32_t>(mCore->mBufferAge);
1104 }
1105 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001106 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1107 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1108 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001109 default:
1110 return BAD_VALUE;
1111 }
1112
1113 BQ_LOGV("query: %d? %d", what, value);
1114 *outValue = value;
1115 return NO_ERROR;
1116}
1117
Dan Stozaf0eaf252014-03-21 13:05:51 -07001118status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001119 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1120 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001121 Mutex::Autolock lock(mCore->mMutex);
1122 mConsumerName = mCore->mConsumerName;
1123 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1124 producerControlledByApp ? "true" : "false");
1125
1126 if (mCore->mIsAbandoned) {
1127 BQ_LOGE("connect: BufferQueue has been abandoned");
1128 return NO_INIT;
1129 }
1130
1131 if (mCore->mConsumerListener == NULL) {
1132 BQ_LOGE("connect: BufferQueue has no consumer");
1133 return NO_INIT;
1134 }
1135
1136 if (output == NULL) {
1137 BQ_LOGE("connect: output was NULL");
1138 return BAD_VALUE;
1139 }
1140
1141 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1142 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1143 mCore->mConnectedApi, api);
1144 return BAD_VALUE;
1145 }
1146
1147 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1148 mDequeueTimeout < 0 ?
1149 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1150 mCore->mMaxBufferCount) -
1151 mCore->getMaxBufferCountLocked();
1152 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1153 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1154 "slots. Delta = %d", delta);
1155 return BAD_VALUE;
1156 }
1157
Dan Stoza289ade12014-02-28 11:17:17 -08001158 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001159 switch (api) {
1160 case NATIVE_WINDOW_API_EGL:
1161 case NATIVE_WINDOW_API_CPU:
1162 case NATIVE_WINDOW_API_MEDIA:
1163 case NATIVE_WINDOW_API_CAMERA:
1164 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001165
1166 output->width = mCore->mDefaultWidth;
1167 output->height = mCore->mDefaultHeight;
1168 output->transformHint = mCore->mTransformHint;
1169 output->numPendingBuffers =
1170 static_cast<uint32_t>(mCore->mQueue.size());
1171 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001172 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001173
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001174 if (listener != NULL) {
1175 // Set up a death notification so that we can disconnect
1176 // automatically if the remote producer dies
1177 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1178 status = IInterface::asBinder(listener)->linkToDeath(
1179 static_cast<IBinder::DeathRecipient*>(this));
1180 if (status != NO_ERROR) {
1181 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1182 strerror(-status), status);
1183 }
1184 mCore->mLinkedToDeath = listener;
1185 }
1186 if (listener->needsReleaseNotify()) {
1187 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001188 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001189 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001190 break;
1191 default:
1192 BQ_LOGE("connect: unknown API %d", api);
1193 status = BAD_VALUE;
1194 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001195 }
Robert Carr97b9c862016-09-08 13:54:35 -07001196 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001197 mCore->mBufferHasBeenQueued = false;
1198 mCore->mDequeueBufferCannotBlock = false;
1199 if (mDequeueTimeout < 0) {
1200 mCore->mDequeueBufferCannotBlock =
1201 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001202 }
Dan Stoza289ade12014-02-28 11:17:17 -08001203
Pablo Ceballos981066c2016-02-18 12:54:37 -08001204 mCore->mAllowAllocation = true;
1205 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001206 return status;
1207}
1208
Robert Carr97b9c862016-09-08 13:54:35 -07001209status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001210 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001211 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001212
1213 int status = NO_ERROR;
1214 sp<IConsumerListener> listener;
1215 { // Autolock scope
1216 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001217
1218 if (mode == DisconnectMode::AllLocal) {
1219 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1220 return NO_ERROR;
1221 }
1222 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1223 }
1224
Antoine Labour78014f32014-07-15 21:17:03 -07001225 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001226
1227 if (mCore->mIsAbandoned) {
1228 // It's not really an error to disconnect after the surface has
1229 // been abandoned; it should just be a no-op.
1230 return NO_ERROR;
1231 }
1232
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001233 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001234 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1235 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1236 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001237 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001238 // If we're asked to disconnect the currently connected api but
1239 // nobody is connected, it's not really an error.
1240 if (api == BufferQueueCore::NO_CONNECTED_API) {
1241 return NO_ERROR;
1242 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001243 }
1244
Dan Stoza289ade12014-02-28 11:17:17 -08001245 switch (api) {
1246 case NATIVE_WINDOW_API_EGL:
1247 case NATIVE_WINDOW_API_CPU:
1248 case NATIVE_WINDOW_API_MEDIA:
1249 case NATIVE_WINDOW_API_CAMERA:
1250 if (mCore->mConnectedApi == api) {
1251 mCore->freeAllBuffersLocked();
1252
1253 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001254 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001255 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001256 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001257 // This can fail if we're here because of the death
1258 // notification, but we just ignore it
1259 token->unlinkToDeath(
1260 static_cast<IBinder::DeathRecipient*>(this));
1261 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001262 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001263 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001264 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001265 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001266 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001267 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001268 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001269 mCore->mDequeueCondition.broadcast();
1270 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001271 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1272 BQ_LOGE("disconnect: not connected (req=%d)", api);
1273 status = NO_INIT;
1274 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001275 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001276 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001277 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001278 }
1279 break;
1280 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001281 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001282 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001283 break;
1284 }
1285 } // Autolock scope
1286
1287 // Call back without lock held
1288 if (listener != NULL) {
1289 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001290 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001291 }
1292
1293 return status;
1294}
1295
Jesse Hall399184a2014-03-03 15:42:54 -08001296status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001297 sp<IConsumerListener> listener;
1298 { // Autolock scope
1299 Mutex::Autolock _l(mCore->mMutex);
1300 mCore->mSidebandStream = stream;
1301 listener = mCore->mConsumerListener;
1302 } // Autolock scope
1303
1304 if (listener != NULL) {
1305 listener->onSidebandStreamChanged();
1306 }
Jesse Hall399184a2014-03-03 15:42:54 -08001307 return NO_ERROR;
1308}
1309
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001310void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001311 PixelFormat format, uint64_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001312 ATRACE_CALL();
1313 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001314 size_t newBufferCount = 0;
1315 uint32_t allocWidth = 0;
1316 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001317 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001318 uint64_t allocUsage = 0;
Antoine Labour78014f32014-07-15 21:17:03 -07001319 { // Autolock scope
1320 Mutex::Autolock lock(mCore->mMutex);
1321 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001322
Dan Stoza9de72932015-04-16 17:28:43 -07001323 if (!mCore->mAllowAllocation) {
1324 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1325 "BufferQueue");
1326 return;
1327 }
1328
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001329 newBufferCount = mCore->mFreeSlots.size();
1330 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001331 return;
1332 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001333
Antoine Labour78014f32014-07-15 21:17:03 -07001334 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1335 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1336 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1337 allocUsage = usage | mCore->mConsumerUsageBits;
1338
1339 mCore->mIsAllocating = true;
1340 } // Autolock scope
1341
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001342 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001343 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001344 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001345 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chris Forbesf3ef3ea2017-04-20 12:43:28 -07001346 allocUsage, {mConsumerName.string(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -07001347
1348 status_t result = graphicBuffer->initCheck();
1349
Antoine Labour78014f32014-07-15 21:17:03 -07001350 if (result != NO_ERROR) {
1351 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001352 " %u, usage %#" PRIx64 ")", width, height, format, usage);
Antoine Labour78014f32014-07-15 21:17:03 -07001353 Mutex::Autolock lock(mCore->mMutex);
1354 mCore->mIsAllocating = false;
1355 mCore->mIsAllocatingCondition.broadcast();
1356 return;
1357 }
1358 buffers.push_back(graphicBuffer);
1359 }
1360
1361 { // Autolock scope
1362 Mutex::Autolock lock(mCore->mMutex);
1363 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1364 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001365 PixelFormat checkFormat = format != 0 ?
1366 format : mCore->mDefaultBufferFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001367 uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
Antoine Labour78014f32014-07-15 21:17:03 -07001368 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1369 checkFormat != allocFormat || checkUsage != allocUsage) {
1370 // Something changed while we released the lock. Retry.
1371 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1372 mCore->mIsAllocating = false;
1373 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001374 continue;
1375 }
1376
Antoine Labour78014f32014-07-15 21:17:03 -07001377 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001378 if (mCore->mFreeSlots.empty()) {
1379 BQ_LOGV("allocateBuffers: a slot was occupied while "
1380 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001381 continue;
1382 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001383 auto slot = mCore->mFreeSlots.begin();
1384 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1385 mSlots[*slot].mGraphicBuffer = buffers[i];
1386 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001387
1388 // freeBufferLocked puts this slot on the free slots list. Since
1389 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001390 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001391
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001392 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1393 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001394
1395 // Make sure the erase is done after all uses of the slot
1396 // iterator since it will be invalid after this point.
1397 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001398 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001399
Antoine Labour78014f32014-07-15 21:17:03 -07001400 mCore->mIsAllocating = false;
1401 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001402 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001403 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001404 }
1405}
1406
Dan Stoza9de72932015-04-16 17:28:43 -07001407status_t BufferQueueProducer::allowAllocation(bool allow) {
1408 ATRACE_CALL();
1409 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1410
1411 Mutex::Autolock lock(mCore->mMutex);
1412 mCore->mAllowAllocation = allow;
1413 return NO_ERROR;
1414}
1415
Dan Stoza812ed062015-06-02 15:45:22 -07001416status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1417 ATRACE_CALL();
1418 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1419
1420 Mutex::Autolock lock(mCore->mMutex);
1421 mCore->mGenerationNumber = generationNumber;
1422 return NO_ERROR;
1423}
1424
Dan Stozac6f30bd2015-06-08 09:32:50 -07001425String8 BufferQueueProducer::getConsumerName() const {
1426 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001427 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001428 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1429 return mConsumerName;
1430}
1431
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001432status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001433 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001434 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001435
1436 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001437 if (!sharedBufferMode) {
1438 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001439 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001440 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001441 return NO_ERROR;
1442}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001443
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001444status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1445 ATRACE_CALL();
1446 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1447
1448 Mutex::Autolock lock(mCore->mMutex);
1449
1450 mCore->mAutoRefresh = autoRefresh;
1451 return NO_ERROR;
1452}
1453
Dan Stoza127fc632015-06-30 13:43:32 -07001454status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1455 ATRACE_CALL();
1456 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1457
Pablo Ceballos981066c2016-02-18 12:54:37 -08001458 Mutex::Autolock lock(mCore->mMutex);
1459 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1460 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1461 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1462 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1463 "available slots. Delta = %d", delta);
1464 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001465 }
1466
Pablo Ceballos981066c2016-02-18 12:54:37 -08001467 mDequeueTimeout = timeout;
1468 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001469
Pablo Ceballos981066c2016-02-18 12:54:37 -08001470 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001471 return NO_ERROR;
1472}
1473
Dan Stoza50101d02016-04-07 16:53:23 -07001474status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001475 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001476 ATRACE_CALL();
1477 BQ_LOGV("getLastQueuedBuffer");
1478
1479 Mutex::Autolock lock(mCore->mMutex);
1480 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1481 *outBuffer = nullptr;
1482 *outFence = Fence::NO_FENCE;
1483 return NO_ERROR;
1484 }
1485
1486 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1487 *outFence = mLastQueueBufferFence;
1488
John Reck1a61da52016-04-28 13:18:15 -07001489 // Currently only SurfaceFlinger internally ever changes
1490 // GLConsumer's filtering mode, so we just use 'true' here as
1491 // this is slightly specialized for the current client of this API,
1492 // which does want filtering.
1493 GLConsumer::computeTransformMatrix(outTransformMatrix,
1494 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1495 mLastQueuedTransform, true /* filter */);
1496
Dan Stoza50101d02016-04-07 16:53:23 -07001497 return NO_ERROR;
1498}
1499
Brian Anderson3890c392016-07-25 12:48:08 -07001500void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1501 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001502}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001503
Brian Anderson3890c392016-07-25 12:48:08 -07001504void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001505 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001506 FrameEventHistoryDelta* outDelta) {
1507 if (newTimestamps == nullptr && outDelta == nullptr) {
1508 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001509 }
1510
1511 ATRACE_CALL();
1512 BQ_LOGV("addAndGetFrameTimestamps");
1513 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001514 {
1515 Mutex::Autolock lock(mCore->mMutex);
1516 listener = mCore->mConsumerListener;
1517 }
1518 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001519 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001520 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001521}
1522
Dan Stoza289ade12014-02-28 11:17:17 -08001523void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1524 // If we're here, it means that a producer we were connected to died.
1525 // We're guaranteed that we are still connected to it because we remove
1526 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1527 // without synchronization here.
1528 int api = mCore->mConnectedApi;
1529 disconnect(api);
1530}
1531
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001532status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1533 BQ_LOGV("getUniqueId");
1534
1535 *outId = mCore->mUniqueId;
1536 return NO_ERROR;
1537}
1538
Dan Stoza289ade12014-02-28 11:17:17 -08001539} // namespace android