blob: 75198d7c8bf0bc58d956b7156b43bcd718b71d00 [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>
37#include <gui/IGraphicBufferAlloc.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070038#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080039
40#include <utils/Log.h>
41#include <utils/Trace.h>
42
43namespace android {
44
Craig Donner6ebc46a2016-10-21 15:23:44 -070045static constexpr uint32_t BQ_LAYER_COUNT = 1;
46
Irvel468051e2016-06-13 16:44:44 -070047BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
48 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080049 mCore(core),
50 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070051 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070052 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070053 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080054 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070055 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080056 mCallbackMutex(),
57 mNextCallbackTicket(0),
58 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070059 mCallbackCondition(),
60 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080061
62BufferQueueProducer::~BufferQueueProducer() {}
63
64status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
65 ATRACE_CALL();
66 BQ_LOGV("requestBuffer: slot %d", slot);
67 Mutex::Autolock lock(mCore->mMutex);
68
69 if (mCore->mIsAbandoned) {
70 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
71 return NO_INIT;
72 }
73
Pablo Ceballos583b1b32015-09-03 18:23:52 -070074 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
75 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
76 return NO_INIT;
77 }
78
Dan Stoza3e96f192014-03-03 10:16:19 -080079 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080080 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080081 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080082 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070083 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080084 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070085 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080086 return BAD_VALUE;
87 }
88
89 mSlots[slot].mRequestBufferCalled = true;
90 *buf = mSlots[slot].mGraphicBuffer;
91 return NO_ERROR;
92}
93
Pablo Ceballosfa455352015-08-12 17:47:47 -070094status_t BufferQueueProducer::setMaxDequeuedBufferCount(
95 int maxDequeuedBuffers) {
96 ATRACE_CALL();
97 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
98 maxDequeuedBuffers);
99
Pablo Ceballos981066c2016-02-18 12:54:37 -0800100 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700101 { // Autolock scope
102 Mutex::Autolock lock(mCore->mMutex);
103 mCore->waitWhileAllocatingLocked();
104
105 if (mCore->mIsAbandoned) {
106 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
107 "abandoned");
108 return NO_INIT;
109 }
110
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700111 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
112 return NO_ERROR;
113 }
114
Pablo Ceballos72daab62015-12-07 16:38:43 -0800115 // The new maxDequeuedBuffer count should not be violated by the number
116 // of currently dequeued buffers
117 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800118 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700119 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800120 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700121 }
122 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800123 if (dequeuedCount > maxDequeuedBuffers) {
124 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
125 "count (%d) exceeds the current dequeued buffer count (%d)",
126 maxDequeuedBuffers, dequeuedCount);
127 return BAD_VALUE;
128 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700129
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700130 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700131 bufferCount += maxDequeuedBuffers;
132
133 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
134 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
135 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
136 return BAD_VALUE;
137 }
138
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700139 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700140 if (bufferCount < minBufferSlots) {
141 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
142 "less than minimum %d", bufferCount, minBufferSlots);
143 return BAD_VALUE;
144 }
145
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700146 if (bufferCount > mCore->mMaxBufferCount) {
147 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700148 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
149 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
150 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
151 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700152 return BAD_VALUE;
153 }
154
Pablo Ceballos72daab62015-12-07 16:38:43 -0800155 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800156 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800157 return BAD_VALUE;
158 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700159 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800160 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800161 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800162 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800163 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700164 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700165 } // Autolock scope
166
167 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800168 if (listener != NULL) {
169 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700170 }
171
172 return NO_ERROR;
173}
174
175status_t BufferQueueProducer::setAsyncMode(bool async) {
176 ATRACE_CALL();
177 BQ_LOGV("setAsyncMode: async = %d", async);
178
Pablo Ceballos981066c2016-02-18 12:54:37 -0800179 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700180 { // Autolock scope
181 Mutex::Autolock lock(mCore->mMutex);
182 mCore->waitWhileAllocatingLocked();
183
184 if (mCore->mIsAbandoned) {
185 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
186 return NO_INIT;
187 }
188
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700189 if (async == mCore->mAsyncMode) {
190 return NO_ERROR;
191 }
192
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700193 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700194 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
195 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700196 BQ_LOGE("setAsyncMode(%d): this call would cause the "
197 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700198 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
199 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
200 mCore->mMaxDequeuedBufferCount,
201 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700202 return BAD_VALUE;
203 }
204
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800205 int delta = mCore->getMaxBufferCountLocked(async,
206 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
207 - mCore->getMaxBufferCountLocked();
208
Pablo Ceballos981066c2016-02-18 12:54:37 -0800209 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800210 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
211 "available slots. Delta = %d", delta);
212 return BAD_VALUE;
213 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700214 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800215 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700216 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700217 if (delta < 0) {
218 listener = mCore->mConsumerListener;
219 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700220 } // Autolock scope
221
222 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800223 if (listener != NULL) {
224 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700225 }
226 return NO_ERROR;
227}
228
Dan Stoza5ecfb682016-01-04 17:01:02 -0800229int BufferQueueProducer::getFreeBufferLocked() const {
230 if (mCore->mFreeBuffers.empty()) {
231 return BufferQueueCore::INVALID_BUFFER_SLOT;
232 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800233 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800234 mCore->mFreeBuffers.pop_front();
235 return slot;
236}
237
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800238int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800239 if (mCore->mFreeSlots.empty()) {
240 return BufferQueueCore::INVALID_BUFFER_SLOT;
241 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800242 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800243 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800244 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800245}
246
247status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800248 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800249 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
250 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800251 bool tryAgain = true;
252 while (tryAgain) {
253 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800254 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800255 return NO_INIT;
256 }
257
Dan Stoza9f3053d2014-03-06 15:14:33 -0800258 int dequeuedCount = 0;
259 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800260 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700261 if (mSlots[s].mBufferState.isDequeued()) {
262 ++dequeuedCount;
263 }
264 if (mSlots[s].mBufferState.isAcquired()) {
265 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800266 }
267 }
268
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700269 // Producers are not allowed to dequeue more than
270 // mMaxDequeuedBufferCount buffers.
271 // This check is only done if a buffer has already been queued
272 if (mCore->mBufferHasBeenQueued &&
273 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
274 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800275 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800276 return INVALID_OPERATION;
277 }
278
Dan Stoza0de7ea72015-04-23 13:20:51 -0700279 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
280
Dan Stozaae3c3682014-04-18 15:43:35 -0700281 // If we disconnect and reconnect quickly, we can be in a state where
282 // our slots are empty but we have many buffers in the queue. This can
283 // cause us to run out of memory if we outrun the consumer. Wait here if
284 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800285 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700286 bool tooManyBuffers = mCore->mQueue.size()
287 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700288 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800289 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700290 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700291 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700292 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700293 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700294 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700295 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700296 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800297 } else {
298 if (caller == FreeSlotCaller::Dequeue) {
299 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800300 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800301 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
302 *found = slot;
303 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800304 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800305 }
306 } else {
307 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800308 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800309 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
310 *found = slot;
311 } else {
312 *found = getFreeBufferLocked();
313 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700314 }
315 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700316 }
317
318 // If no buffer is found, or if the queue has too many buffers
319 // outstanding, wait for a buffer to be acquired or released, or for the
320 // max buffer count to change.
321 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
322 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800323 if (tryAgain) {
324 // Return an error if we're in non-blocking mode (producer and
325 // consumer are controlled by the application).
326 // However, the consumer is allowed to briefly acquire an extra
327 // buffer (which could cause us to have to wait here), which is
328 // okay, since it is only used to implement an atomic acquire +
329 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700330 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800331 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
332 return WOULD_BLOCK;
333 }
Dan Stoza127fc632015-06-30 13:43:32 -0700334 if (mDequeueTimeout >= 0) {
335 status_t result = mCore->mDequeueCondition.waitRelative(
336 mCore->mMutex, mDequeueTimeout);
337 if (result == TIMED_OUT) {
338 return result;
339 }
340 } else {
341 mCore->mDequeueCondition.wait(mCore->mMutex);
342 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800343 }
344 } // while (tryAgain)
345
346 return NO_ERROR;
347}
348
Dan Stoza289ade12014-02-28 11:17:17 -0800349status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700350 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700351 PixelFormat format, uint32_t usage,
352 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800353 ATRACE_CALL();
354 { // Autolock scope
355 Mutex::Autolock lock(mCore->mMutex);
356 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700357
358 if (mCore->mIsAbandoned) {
359 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
360 return NO_INIT;
361 }
362
363 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
364 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
365 return NO_INIT;
366 }
Dan Stoza289ade12014-02-28 11:17:17 -0800367 } // Autolock scope
368
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700369 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
370 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) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700420 if (buffer->needsReallocation(width, height, format,
421 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700422 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
424 "buffer");
425 return BAD_VALUE;
426 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800427 mCore->mFreeSlots.insert(found);
428 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700429 found = BufferItem::INVALID_BUFFER_SLOT;
430 continue;
431 }
432 }
Dan Stoza289ade12014-02-28 11:17:17 -0800433 }
434
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800435 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700436 if (mCore->mSharedBufferSlot == found &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700437 buffer->needsReallocation(width, height, format,
438 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800439 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
440 "buffer");
441
442 return BAD_VALUE;
443 }
444
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700445 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800446 mCore->mActiveBuffers.insert(found);
447 }
Dan Stoza289ade12014-02-28 11:17:17 -0800448 *outSlot = found;
449 ATRACE_BUFFER_INDEX(found);
450
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800451 attachedByConsumer = mSlots[found].mNeedsReallocation;
452 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700454 mSlots[found].mBufferState.dequeue();
455
Dan Stoza289ade12014-02-28 11:17:17 -0800456 if ((buffer == NULL) ||
Craig Donner6ebc46a2016-10-21 15:23:44 -0700457 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT,
458 usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800459 {
460 mSlots[found].mAcquireCalled = false;
461 mSlots[found].mGraphicBuffer = NULL;
462 mSlots[found].mRequestBufferCalled = false;
463 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
464 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
465 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800466 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700467 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800468
469 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800470 } else {
471 // We add 1 because that will be the frame number when this buffer
472 // is queued
473 mCore->mBufferAge =
474 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800475 }
476
Dan Stoza800b41a2015-04-28 14:20:04 -0700477 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
478 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800479
Dan Stoza289ade12014-02-28 11:17:17 -0800480 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
481 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
482 "slot=%d w=%d h=%d format=%u",
483 found, buffer->width, buffer->height, buffer->format);
484 }
485
486 eglDisplay = mSlots[found].mEglDisplay;
487 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700488 // Don't return a fence in shared buffer mode, except for the first
489 // frame.
490 *outFence = (mCore->mSharedBufferMode &&
491 mCore->mSharedBufferSlot == found) ?
492 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800493 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
494 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700495
496 // If shared buffer mode has just been enabled, cache the slot of the
497 // first buffer that is dequeued and mark it as the shared buffer.
498 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
499 BufferQueueCore::INVALID_BUFFER_SLOT) {
500 mCore->mSharedBufferSlot = found;
501 mSlots[found].mBufferState.mShared = true;
502 }
Dan Stoza289ade12014-02-28 11:17:17 -0800503 } // Autolock scope
504
505 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
506 status_t error;
Dan Stoza29a3e902014-06-20 13:13:57 -0700507 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800508 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -0700509 width, height, format, BQ_LAYER_COUNT, usage,
Dan Stoza024e9312016-08-24 12:17:29 -0700510 {mConsumerName.string(), mConsumerName.size()}, &error));
Dan Stoza289ade12014-02-28 11:17:17 -0800511 { // Autolock scope
512 Mutex::Autolock lock(mCore->mMutex);
513
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700514 if (graphicBuffer != NULL && !mCore->mIsAbandoned) {
515 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
516 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
517 }
518
519 mCore->mIsAllocating = false;
520 mCore->mIsAllocatingCondition.broadcast();
521
522 if (graphicBuffer == NULL) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700523 mCore->mFreeSlots.insert(*outSlot);
524 mCore->clearBufferSlotLocked(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700525 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
526 return error;
527 }
528
Dan Stoza289ade12014-02-28 11:17:17 -0800529 if (mCore->mIsAbandoned) {
Pablo Ceballos0a068092016-06-29 15:08:33 -0700530 mCore->mFreeSlots.insert(*outSlot);
531 mCore->clearBufferSlotLocked(*outSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800532 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
533 return NO_INIT;
534 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800535
Pablo Ceballos9e314332016-01-12 13:49:19 -0800536 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800537 } // Autolock scope
538 }
539
Dan Stoza9f3053d2014-03-06 15:14:33 -0800540 if (attachedByConsumer) {
541 returnFlags |= BUFFER_NEEDS_REALLOCATION;
542 }
543
Dan Stoza289ade12014-02-28 11:17:17 -0800544 if (eglFence != EGL_NO_SYNC_KHR) {
545 EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
546 1000000000);
547 // If something goes wrong, log the error, but return the buffer without
548 // synchronizing access to it. It's too late at this point to abort the
549 // dequeue operation.
550 if (result == EGL_FALSE) {
551 BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
552 eglGetError());
553 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
554 BQ_LOGE("dequeueBuffer: timeout waiting for fence");
555 }
556 eglDestroySyncKHR(eglDisplay, eglFence);
557 }
558
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700559 BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
560 *outSlot,
Dan Stoza289ade12014-02-28 11:17:17 -0800561 mSlots[*outSlot].mFrameNumber,
562 mSlots[*outSlot].mGraphicBuffer->handle, returnFlags);
563
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700564 addAndGetFrameTimestamps(nullptr, outTimestamps);
565
Dan Stoza289ade12014-02-28 11:17:17 -0800566 return returnFlags;
567}
568
Dan Stoza9f3053d2014-03-06 15:14:33 -0800569status_t BufferQueueProducer::detachBuffer(int slot) {
570 ATRACE_CALL();
571 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700572 BQ_LOGV("detachBuffer: slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800573
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700574 sp<IConsumerListener> listener;
575 {
576 Mutex::Autolock lock(mCore->mMutex);
577
578 if (mCore->mIsAbandoned) {
579 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
580 return NO_INIT;
581 }
582
583 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
584 BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
585 return NO_INIT;
586 }
587
588 if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
589 BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
590 return BAD_VALUE;
591 }
592
593 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
594 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
595 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
596 return BAD_VALUE;
597 } else if (!mSlots[slot].mBufferState.isDequeued()) {
598 BQ_LOGE("detachBuffer: slot %d is not owned by the producer "
599 "(state = %s)", slot, mSlots[slot].mBufferState.string());
600 return BAD_VALUE;
601 } else if (!mSlots[slot].mRequestBufferCalled) {
602 BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
603 slot);
604 return BAD_VALUE;
605 }
606
607 mSlots[slot].mBufferState.detachProducer();
608 mCore->mActiveBuffers.erase(slot);
609 mCore->mFreeSlots.insert(slot);
610 mCore->clearBufferSlotLocked(slot);
611 mCore->mDequeueCondition.broadcast();
612 VALIDATE_CONSISTENCY();
613 listener = mCore->mConsumerListener;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800614 }
615
Eino-Ville Talvala93dd0512016-06-10 14:21:02 -0700616 if (listener != NULL) {
617 listener->onBuffersReleased();
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700618 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800619
Dan Stoza9f3053d2014-03-06 15:14:33 -0800620 return NO_ERROR;
621}
622
Dan Stozad9822a32014-03-28 15:25:31 -0700623status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
624 sp<Fence>* outFence) {
625 ATRACE_CALL();
626
627 if (outBuffer == NULL) {
628 BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
629 return BAD_VALUE;
630 } else if (outFence == NULL) {
631 BQ_LOGE("detachNextBuffer: outFence must not be NULL");
632 return BAD_VALUE;
633 }
634
Pablo Ceballos981066c2016-02-18 12:54:37 -0800635 Mutex::Autolock lock(mCore->mMutex);
Dan Stozad9822a32014-03-28 15:25:31 -0700636
Pablo Ceballos981066c2016-02-18 12:54:37 -0800637 if (mCore->mIsAbandoned) {
638 BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
639 return NO_INIT;
Dan Stozad9822a32014-03-28 15:25:31 -0700640 }
641
Pablo Ceballos981066c2016-02-18 12:54:37 -0800642 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
643 BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
644 return NO_INIT;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700645 }
Pablo Ceballos981066c2016-02-18 12:54:37 -0800646
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700647 if (mCore->mSharedBufferMode) {
648 BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
649 "mode");
Pablo Ceballos981066c2016-02-18 12:54:37 -0800650 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700651 }
652
Pablo Ceballos981066c2016-02-18 12:54:37 -0800653 mCore->waitWhileAllocatingLocked();
654
655 if (mCore->mFreeBuffers.empty()) {
656 return NO_MEMORY;
657 }
658
659 int found = mCore->mFreeBuffers.front();
660 mCore->mFreeBuffers.remove(found);
661 mCore->mFreeSlots.insert(found);
662
663 BQ_LOGV("detachNextBuffer detached slot %d", found);
664
665 *outBuffer = mSlots[found].mGraphicBuffer;
666 *outFence = mSlots[found].mFence;
667 mCore->clearBufferSlotLocked(found);
668 VALIDATE_CONSISTENCY();
669
Dan Stozad9822a32014-03-28 15:25:31 -0700670 return NO_ERROR;
671}
672
Dan Stoza9f3053d2014-03-06 15:14:33 -0800673status_t BufferQueueProducer::attachBuffer(int* outSlot,
674 const sp<android::GraphicBuffer>& buffer) {
675 ATRACE_CALL();
676
677 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700678 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800679 return BAD_VALUE;
680 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700681 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800682 return BAD_VALUE;
683 }
684
685 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700686
687 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700688 BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700689 return NO_INIT;
690 }
691
692 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700693 BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700694 return NO_INIT;
695 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800696
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700697 if (mCore->mSharedBufferMode) {
698 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700699 return BAD_VALUE;
700 }
701
Dan Stoza812ed062015-06-02 15:45:22 -0700702 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
703 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
704 "[queue %u]", buffer->getGenerationNumber(),
705 mCore->mGenerationNumber);
706 return BAD_VALUE;
707 }
708
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700709 mCore->waitWhileAllocatingLocked();
710
Dan Stoza9f3053d2014-03-06 15:14:33 -0800711 status_t returnFlags = NO_ERROR;
712 int found;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800713 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800714 if (status != NO_ERROR) {
715 return status;
716 }
717
718 // This should not happen
719 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700720 BQ_LOGE("attachBuffer: no available buffer slots");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800721 return -EBUSY;
722 }
723
724 *outSlot = found;
725 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700726 BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
Dan Stoza9f3053d2014-03-06 15:14:33 -0800727 *outSlot, returnFlags);
728
729 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700730 mSlots[*outSlot].mBufferState.attachProducer();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800731 mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
732 mSlots[*outSlot].mFence = Fence::NO_FENCE;
Dan Stoza2443c792014-03-24 15:03:46 -0700733 mSlots[*outSlot].mRequestBufferCalled = true;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800734 mSlots[*outSlot].mAcquireCalled = false;
735 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800736 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700737
Dan Stoza9f3053d2014-03-06 15:14:33 -0800738 return returnFlags;
739}
740
Dan Stoza289ade12014-02-28 11:17:17 -0800741status_t BufferQueueProducer::queueBuffer(int slot,
742 const QueueBufferInput &input, QueueBufferOutput *output) {
743 ATRACE_CALL();
744 ATRACE_BUFFER_INDEX(slot);
745
Brian Andersond6927fb2016-07-23 23:37:30 -0700746 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800747 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800748 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700749 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800750 int scalingMode;
751 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700752 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700753 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700754 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700755 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700756 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
757 &getFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700758 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800759
Brian Andersond6927fb2016-07-23 23:37:30 -0700760 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800761 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800762 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800763 }
764
765 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;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700861 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700862 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700863 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700864 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700865 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700866 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800867
Ruben Brunk1681d952014-06-27 15:51:55 -0700868 mStickyTransform = stickyTransform;
869
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700870 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700871 if (mCore->mSharedBufferMode) {
872 mCore->mSharedBufferCache.crop = crop;
873 mCore->mSharedBufferCache.transform = transform;
874 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700875 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700877 }
878
Dan Stoza289ade12014-02-28 11:17:17 -0800879 if (mCore->mQueue.empty()) {
880 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
881 // and simply queue this buffer
882 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800883 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800884 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700885 // When the queue is not empty, we need to look at the last buffer
886 // in the queue to see if we need to replace it
887 const BufferItem& last = mCore->mQueue.itemAt(
888 mCore->mQueue.size() - 1);
889 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800890
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700891 if (!last.mIsStale) {
892 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700893
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700894 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700895 // still be around. Mark it as no longer shared if this
896 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700897 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700898 mSlots[last.mSlot].mBufferState.isFree()) {
899 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700900 }
901 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700902 if (!mSlots[last.mSlot].mBufferState.isShared()) {
903 mCore->mActiveBuffers.erase(last.mSlot);
904 mCore->mFreeBuffers.push_back(last.mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700905 }
Dan Stoza289ade12014-02-28 11:17:17 -0800906 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800907
Dan Stoza289ade12014-02-28 11:17:17 -0800908 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700909 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800910 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800911 } else {
912 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800913 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800914 }
915 }
916
917 mCore->mBufferHasBeenQueued = true;
918 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700919 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800920
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700921 output->width = mCore->mDefaultWidth;
922 output->height = mCore->mDefaultHeight;
923 output->transformHint = mCore->mTransformHint;
924 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
925 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800926
Colin Cross152c3b72016-09-27 14:08:19 -0700927 ATRACE_INT(mCore->mConsumerName.string(),
928 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700929 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800930
931 // Take a ticket for the callback functions
932 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700933
Pablo Ceballos9e314332016-01-12 13:49:19 -0800934 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800935 } // Autolock scope
936
Irvel468051e2016-06-13 16:44:44 -0700937 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
938 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
939 // there will be no Binder call
940 if (!mConsumerIsSurfaceFlinger) {
941 item.mGraphicBuffer.clear();
942 }
943
944 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800945 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
946
947 // Call back without the main BufferQueue lock held, but with the callback
948 // lock held so we can ensure that callbacks occur in order
949 {
950 Mutex::Autolock lock(mCallbackMutex);
951 while (callbackTicket != mCurrentCallbackTicket) {
952 mCallbackCondition.wait(mCallbackMutex);
953 }
954
955 if (frameAvailableListener != NULL) {
956 frameAvailableListener->onFrameAvailable(item);
957 } else if (frameReplacedListener != NULL) {
958 frameReplacedListener->onFrameReplaced(item);
959 }
960
961 ++mCurrentCallbackTicket;
962 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800963 }
964
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000965 // Wait without lock held
966 if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) {
967 // Waiting here allows for two full buffers to be queued but not a
968 // third. In the event that frames take varying time, this makes a
969 // small trade-off in favor of latency rather than throughput.
970 mLastQueueBufferFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000971 }
Brian Andersond6927fb2016-07-23 23:37:30 -0700972 mLastQueueBufferFence = acquireFence;
John Reck1a61da52016-04-28 13:18:15 -0700973 mLastQueuedCrop = item.mCrop;
974 mLastQueuedTransform = item.mTransform;
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000975
Brian Andersond6927fb2016-07-23 23:37:30 -0700976 // Update and get FrameEventHistory.
977 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
978 NewFrameEventsEntry newFrameEventsEntry = {
979 currentFrameNumber,
980 postedTime,
981 requestedPresentTimestamp,
982 acquireFence
983 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700984 addAndGetFrameTimestamps(&newFrameEventsEntry,
985 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -0700986
Dan Stoza289ade12014-02-28 11:17:17 -0800987 return NO_ERROR;
988}
989
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700990status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -0800991 ATRACE_CALL();
992 BQ_LOGV("cancelBuffer: slot %d", slot);
993 Mutex::Autolock lock(mCore->mMutex);
994
995 if (mCore->mIsAbandoned) {
996 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700997 return NO_INIT;
998 }
999
1000 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1001 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1002 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001003 }
1004
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001005 if (mCore->mSharedBufferMode) {
1006 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001007 return BAD_VALUE;
1008 }
1009
Dan Stoza3e96f192014-03-03 10:16:19 -08001010 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001011 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001012 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001013 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001014 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001015 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001016 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001017 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001018 } else if (fence == NULL) {
1019 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001020 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001021 }
1022
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001023 mSlots[slot].mBufferState.cancel();
1024
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001025 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001026 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001027 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001028 mSlots[slot].mBufferState.mShared = false;
1029 }
1030
1031 // Don't put the shared buffer on the free list.
1032 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001033 mCore->mActiveBuffers.erase(slot);
1034 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001035 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001036
Dan Stoza289ade12014-02-28 11:17:17 -08001037 mSlots[slot].mFence = fence;
1038 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001039 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001040
1041 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001042}
1043
1044int BufferQueueProducer::query(int what, int *outValue) {
1045 ATRACE_CALL();
1046 Mutex::Autolock lock(mCore->mMutex);
1047
1048 if (outValue == NULL) {
1049 BQ_LOGE("query: outValue was NULL");
1050 return BAD_VALUE;
1051 }
1052
1053 if (mCore->mIsAbandoned) {
1054 BQ_LOGE("query: BufferQueue has been abandoned");
1055 return NO_INIT;
1056 }
1057
1058 int value;
1059 switch (what) {
1060 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001061 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001062 break;
1063 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001064 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001065 break;
1066 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001067 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001068 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001069 case NATIVE_WINDOW_LAYER_COUNT:
1070 // All BufferQueue buffers have a single layer.
1071 value = BQ_LAYER_COUNT;
1072 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001073 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001074 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001075 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001076 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001077 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001078 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001079 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1080 value = (mCore->mQueue.size() > 1);
1081 break;
1082 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001083 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001084 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001085 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1086 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1087 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001088 case NATIVE_WINDOW_BUFFER_AGE:
1089 if (mCore->mBufferAge > INT32_MAX) {
1090 value = 0;
1091 } else {
1092 value = static_cast<int32_t>(mCore->mBufferAge);
1093 }
1094 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001095 default:
1096 return BAD_VALUE;
1097 }
1098
1099 BQ_LOGV("query: %d? %d", what, value);
1100 *outValue = value;
1101 return NO_ERROR;
1102}
1103
Dan Stozaf0eaf252014-03-21 13:05:51 -07001104status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001105 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1106 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001107 Mutex::Autolock lock(mCore->mMutex);
1108 mConsumerName = mCore->mConsumerName;
1109 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1110 producerControlledByApp ? "true" : "false");
1111
1112 if (mCore->mIsAbandoned) {
1113 BQ_LOGE("connect: BufferQueue has been abandoned");
1114 return NO_INIT;
1115 }
1116
1117 if (mCore->mConsumerListener == NULL) {
1118 BQ_LOGE("connect: BufferQueue has no consumer");
1119 return NO_INIT;
1120 }
1121
1122 if (output == NULL) {
1123 BQ_LOGE("connect: output was NULL");
1124 return BAD_VALUE;
1125 }
1126
1127 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1128 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1129 mCore->mConnectedApi, api);
1130 return BAD_VALUE;
1131 }
1132
1133 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1134 mDequeueTimeout < 0 ?
1135 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1136 mCore->mMaxBufferCount) -
1137 mCore->getMaxBufferCountLocked();
1138 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1139 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1140 "slots. Delta = %d", delta);
1141 return BAD_VALUE;
1142 }
1143
Dan Stoza289ade12014-02-28 11:17:17 -08001144 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001145 switch (api) {
1146 case NATIVE_WINDOW_API_EGL:
1147 case NATIVE_WINDOW_API_CPU:
1148 case NATIVE_WINDOW_API_MEDIA:
1149 case NATIVE_WINDOW_API_CAMERA:
1150 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001151
1152 output->width = mCore->mDefaultWidth;
1153 output->height = mCore->mDefaultHeight;
1154 output->transformHint = mCore->mTransformHint;
1155 output->numPendingBuffers =
1156 static_cast<uint32_t>(mCore->mQueue.size());
1157 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -08001158
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001159 if (listener != NULL) {
1160 // Set up a death notification so that we can disconnect
1161 // automatically if the remote producer dies
1162 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1163 status = IInterface::asBinder(listener)->linkToDeath(
1164 static_cast<IBinder::DeathRecipient*>(this));
1165 if (status != NO_ERROR) {
1166 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1167 strerror(-status), status);
1168 }
1169 mCore->mLinkedToDeath = listener;
1170 }
1171 if (listener->needsReleaseNotify()) {
1172 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001173 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001174 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001175 break;
1176 default:
1177 BQ_LOGE("connect: unknown API %d", api);
1178 status = BAD_VALUE;
1179 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001180 }
Robert Carr97b9c862016-09-08 13:54:35 -07001181 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001182 mCore->mBufferHasBeenQueued = false;
1183 mCore->mDequeueBufferCannotBlock = false;
1184 if (mDequeueTimeout < 0) {
1185 mCore->mDequeueBufferCannotBlock =
1186 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001187 }
Dan Stoza289ade12014-02-28 11:17:17 -08001188
Pablo Ceballos981066c2016-02-18 12:54:37 -08001189 mCore->mAllowAllocation = true;
1190 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001191 return status;
1192}
1193
Robert Carr97b9c862016-09-08 13:54:35 -07001194status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001195 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001196 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001197
1198 int status = NO_ERROR;
1199 sp<IConsumerListener> listener;
1200 { // Autolock scope
1201 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001202
1203 if (mode == DisconnectMode::AllLocal) {
1204 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1205 return NO_ERROR;
1206 }
1207 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1208 }
1209
Antoine Labour78014f32014-07-15 21:17:03 -07001210 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001211
1212 if (mCore->mIsAbandoned) {
1213 // It's not really an error to disconnect after the surface has
1214 // been abandoned; it should just be a no-op.
1215 return NO_ERROR;
1216 }
1217
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001218 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1219 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001220 // If we're asked to disconnect the currently connected api but
1221 // nobody is connected, it's not really an error.
1222 if (api == BufferQueueCore::NO_CONNECTED_API) {
1223 return NO_ERROR;
1224 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001225 }
1226
Dan Stoza289ade12014-02-28 11:17:17 -08001227 switch (api) {
1228 case NATIVE_WINDOW_API_EGL:
1229 case NATIVE_WINDOW_API_CPU:
1230 case NATIVE_WINDOW_API_MEDIA:
1231 case NATIVE_WINDOW_API_CAMERA:
1232 if (mCore->mConnectedApi == api) {
1233 mCore->freeAllBuffersLocked();
1234
1235 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001236 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001237 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001238 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001239 // This can fail if we're here because of the death
1240 // notification, but we just ignore it
1241 token->unlinkToDeath(
1242 static_cast<IBinder::DeathRecipient*>(this));
1243 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001244 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001245 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001246 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001247 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001248 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001249 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001250 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001251 mCore->mDequeueCondition.broadcast();
1252 listener = mCore->mConsumerListener;
Amith Dsouza4f21a4c2015-06-30 22:54:16 -07001253 } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001254 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001255 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001256 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001257 }
1258 break;
1259 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001260 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001261 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001262 break;
1263 }
1264 } // Autolock scope
1265
1266 // Call back without lock held
1267 if (listener != NULL) {
1268 listener->onBuffersReleased();
1269 }
1270
1271 return status;
1272}
1273
Jesse Hall399184a2014-03-03 15:42:54 -08001274status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001275 sp<IConsumerListener> listener;
1276 { // Autolock scope
1277 Mutex::Autolock _l(mCore->mMutex);
1278 mCore->mSidebandStream = stream;
1279 listener = mCore->mConsumerListener;
1280 } // Autolock scope
1281
1282 if (listener != NULL) {
1283 listener->onSidebandStreamChanged();
1284 }
Jesse Hall399184a2014-03-03 15:42:54 -08001285 return NO_ERROR;
1286}
1287
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001288void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1289 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001290 ATRACE_CALL();
1291 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001292 size_t newBufferCount = 0;
1293 uint32_t allocWidth = 0;
1294 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001295 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001296 uint32_t allocUsage = 0;
1297 { // Autolock scope
1298 Mutex::Autolock lock(mCore->mMutex);
1299 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001300
Dan Stoza9de72932015-04-16 17:28:43 -07001301 if (!mCore->mAllowAllocation) {
1302 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1303 "BufferQueue");
1304 return;
1305 }
1306
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001307 newBufferCount = mCore->mFreeSlots.size();
1308 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001309 return;
1310 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001311
Antoine Labour78014f32014-07-15 21:17:03 -07001312 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1313 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1314 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1315 allocUsage = usage | mCore->mConsumerUsageBits;
1316
1317 mCore->mIsAllocating = true;
1318 } // Autolock scope
1319
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001320 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001321 for (size_t i = 0; i < newBufferCount; ++i) {
1322 status_t result = NO_ERROR;
1323 sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001324 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
1325 allocUsage, {mConsumerName.string(), mConsumerName.size()},
1326 &result));
Antoine Labour78014f32014-07-15 21:17:03 -07001327 if (result != NO_ERROR) {
1328 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1329 " %u, usage %u)", width, height, format, usage);
1330 Mutex::Autolock lock(mCore->mMutex);
1331 mCore->mIsAllocating = false;
1332 mCore->mIsAllocatingCondition.broadcast();
1333 return;
1334 }
1335 buffers.push_back(graphicBuffer);
1336 }
1337
1338 { // Autolock scope
1339 Mutex::Autolock lock(mCore->mMutex);
1340 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1341 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001342 PixelFormat checkFormat = format != 0 ?
1343 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001344 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1345 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1346 checkFormat != allocFormat || checkUsage != allocUsage) {
1347 // Something changed while we released the lock. Retry.
1348 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1349 mCore->mIsAllocating = false;
1350 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001351 continue;
1352 }
1353
Antoine Labour78014f32014-07-15 21:17:03 -07001354 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001355 if (mCore->mFreeSlots.empty()) {
1356 BQ_LOGV("allocateBuffers: a slot was occupied while "
1357 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001358 continue;
1359 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001360 auto slot = mCore->mFreeSlots.begin();
1361 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1362 mSlots[*slot].mGraphicBuffer = buffers[i];
1363 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001364
1365 // freeBufferLocked puts this slot on the free slots list. Since
1366 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001367 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001368
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001369 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1370 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001371
1372 // Make sure the erase is done after all uses of the slot
1373 // iterator since it will be invalid after this point.
1374 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001375 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001376
Antoine Labour78014f32014-07-15 21:17:03 -07001377 mCore->mIsAllocating = false;
1378 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001379 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001380 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001381 }
1382}
1383
Dan Stoza9de72932015-04-16 17:28:43 -07001384status_t BufferQueueProducer::allowAllocation(bool allow) {
1385 ATRACE_CALL();
1386 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1387
1388 Mutex::Autolock lock(mCore->mMutex);
1389 mCore->mAllowAllocation = allow;
1390 return NO_ERROR;
1391}
1392
Dan Stoza812ed062015-06-02 15:45:22 -07001393status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1394 ATRACE_CALL();
1395 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1396
1397 Mutex::Autolock lock(mCore->mMutex);
1398 mCore->mGenerationNumber = generationNumber;
1399 return NO_ERROR;
1400}
1401
Dan Stozac6f30bd2015-06-08 09:32:50 -07001402String8 BufferQueueProducer::getConsumerName() const {
1403 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001404 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001405 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1406 return mConsumerName;
1407}
1408
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001409status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001410 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001411 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001412
1413 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001414 if (!sharedBufferMode) {
1415 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001416 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001417 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001418 return NO_ERROR;
1419}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001420
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001421status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1422 ATRACE_CALL();
1423 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1424
1425 Mutex::Autolock lock(mCore->mMutex);
1426
1427 mCore->mAutoRefresh = autoRefresh;
1428 return NO_ERROR;
1429}
1430
Dan Stoza127fc632015-06-30 13:43:32 -07001431status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1432 ATRACE_CALL();
1433 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1434
Pablo Ceballos981066c2016-02-18 12:54:37 -08001435 Mutex::Autolock lock(mCore->mMutex);
1436 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1437 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1438 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1439 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1440 "available slots. Delta = %d", delta);
1441 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001442 }
1443
Pablo Ceballos981066c2016-02-18 12:54:37 -08001444 mDequeueTimeout = timeout;
1445 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001446
Pablo Ceballos981066c2016-02-18 12:54:37 -08001447 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001448 return NO_ERROR;
1449}
1450
Dan Stoza50101d02016-04-07 16:53:23 -07001451status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001452 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001453 ATRACE_CALL();
1454 BQ_LOGV("getLastQueuedBuffer");
1455
1456 Mutex::Autolock lock(mCore->mMutex);
1457 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1458 *outBuffer = nullptr;
1459 *outFence = Fence::NO_FENCE;
1460 return NO_ERROR;
1461 }
1462
1463 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1464 *outFence = mLastQueueBufferFence;
1465
John Reck1a61da52016-04-28 13:18:15 -07001466 // Currently only SurfaceFlinger internally ever changes
1467 // GLConsumer's filtering mode, so we just use 'true' here as
1468 // this is slightly specialized for the current client of this API,
1469 // which does want filtering.
1470 GLConsumer::computeTransformMatrix(outTransformMatrix,
1471 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1472 mLastQueuedTransform, true /* filter */);
1473
Dan Stoza50101d02016-04-07 16:53:23 -07001474 return NO_ERROR;
1475}
1476
Brian Anderson3890c392016-07-25 12:48:08 -07001477void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1478 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001479}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001480
Brian Anderson3890c392016-07-25 12:48:08 -07001481void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001482 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001483 FrameEventHistoryDelta* outDelta) {
1484 if (newTimestamps == nullptr && outDelta == nullptr) {
1485 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001486 }
1487
1488 ATRACE_CALL();
1489 BQ_LOGV("addAndGetFrameTimestamps");
1490 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001491 {
1492 Mutex::Autolock lock(mCore->mMutex);
1493 listener = mCore->mConsumerListener;
1494 }
1495 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001496 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001497 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001498}
1499
Dan Stoza289ade12014-02-28 11:17:17 -08001500void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1501 // If we're here, it means that a producer we were connected to died.
1502 // We're guaranteed that we are still connected to it because we remove
1503 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1504 // without synchronization here.
1505 int api = mCore->mConnectedApi;
1506 disconnect(api);
1507}
1508
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001509status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1510 BQ_LOGV("getUniqueId");
1511
1512 *outId = mCore->mUniqueId;
1513 return NO_ERROR;
1514}
1515
Dan Stoza289ade12014-02-28 11:17:17 -08001516} // namespace android