blob: 6a5593cc1c92ad26fa92b67b17c0fe685a1ddc48 [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
42namespace android {
43
Craig Donner6ebc46a2016-10-21 15:23:44 -070044static constexpr uint32_t BQ_LAYER_COUNT = 1;
45
Irvel468051e2016-06-13 16:44:44 -070046BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
47 bool consumerIsSurfaceFlinger) :
Dan Stoza289ade12014-02-28 11:17:17 -080048 mCore(core),
49 mSlots(core->mSlots),
Ruben Brunk1681d952014-06-27 15:51:55 -070050 mConsumerName(),
Eric Penner99a0afb2014-09-30 11:28:30 -070051 mStickyTransform(0),
Irvel468051e2016-06-13 16:44:44 -070052 mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
Dan Stoza8dc55392014-11-04 11:37:46 -080053 mLastQueueBufferFence(Fence::NO_FENCE),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070054 mLastQueuedTransform(0),
Dan Stoza8dc55392014-11-04 11:37:46 -080055 mCallbackMutex(),
56 mNextCallbackTicket(0),
57 mCurrentCallbackTicket(0),
Dan Stoza127fc632015-06-30 13:43:32 -070058 mCallbackCondition(),
59 mDequeueTimeout(-1) {}
Dan Stoza289ade12014-02-28 11:17:17 -080060
61BufferQueueProducer::~BufferQueueProducer() {}
62
63status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
64 ATRACE_CALL();
65 BQ_LOGV("requestBuffer: slot %d", slot);
66 Mutex::Autolock lock(mCore->mMutex);
67
68 if (mCore->mIsAbandoned) {
69 BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
70 return NO_INIT;
71 }
72
Pablo Ceballos583b1b32015-09-03 18:23:52 -070073 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
74 BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
75 return NO_INIT;
76 }
77
Dan Stoza3e96f192014-03-03 10:16:19 -080078 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -080079 BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -080080 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza289ade12014-02-28 11:17:17 -080081 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -070082 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -080083 BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -070084 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza289ade12014-02-28 11:17:17 -080085 return BAD_VALUE;
86 }
87
88 mSlots[slot].mRequestBufferCalled = true;
89 *buf = mSlots[slot].mGraphicBuffer;
90 return NO_ERROR;
91}
92
Pablo Ceballosfa455352015-08-12 17:47:47 -070093status_t BufferQueueProducer::setMaxDequeuedBufferCount(
94 int maxDequeuedBuffers) {
95 ATRACE_CALL();
96 BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
97 maxDequeuedBuffers);
98
Pablo Ceballos981066c2016-02-18 12:54:37 -080099 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700100 { // Autolock scope
101 Mutex::Autolock lock(mCore->mMutex);
102 mCore->waitWhileAllocatingLocked();
103
104 if (mCore->mIsAbandoned) {
105 BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
106 "abandoned");
107 return NO_INIT;
108 }
109
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700110 if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
111 return NO_ERROR;
112 }
113
Pablo Ceballos72daab62015-12-07 16:38:43 -0800114 // The new maxDequeuedBuffer count should not be violated by the number
115 // of currently dequeued buffers
116 int dequeuedCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800117 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700118 if (mSlots[s].mBufferState.isDequeued()) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800119 dequeuedCount++;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700120 }
121 }
Pablo Ceballos72daab62015-12-07 16:38:43 -0800122 if (dequeuedCount > maxDequeuedBuffers) {
123 BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
124 "count (%d) exceeds the current dequeued buffer count (%d)",
125 maxDequeuedBuffers, dequeuedCount);
126 return BAD_VALUE;
127 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700128
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700129 int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700130 bufferCount += maxDequeuedBuffers;
131
132 if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
133 BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
134 "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
135 return BAD_VALUE;
136 }
137
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700138 const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700139 if (bufferCount < minBufferSlots) {
140 BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
141 "less than minimum %d", bufferCount, minBufferSlots);
142 return BAD_VALUE;
143 }
144
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700145 if (bufferCount > mCore->mMaxBufferCount) {
146 BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700147 "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
148 "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
149 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
150 mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700151 return BAD_VALUE;
152 }
153
Pablo Ceballos72daab62015-12-07 16:38:43 -0800154 int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
Pablo Ceballos981066c2016-02-18 12:54:37 -0800155 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800156 return BAD_VALUE;
157 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700158 mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800159 VALIDATE_CONSISTENCY();
Pablo Ceballos72daab62015-12-07 16:38:43 -0800160 if (delta < 0) {
Pablo Ceballos981066c2016-02-18 12:54:37 -0800161 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800162 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700163 mCore->mDequeueCondition.broadcast();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700164 } // Autolock scope
165
166 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800167 if (listener != NULL) {
168 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700169 }
170
171 return NO_ERROR;
172}
173
174status_t BufferQueueProducer::setAsyncMode(bool async) {
175 ATRACE_CALL();
176 BQ_LOGV("setAsyncMode: async = %d", async);
177
Pablo Ceballos981066c2016-02-18 12:54:37 -0800178 sp<IConsumerListener> listener;
Pablo Ceballosfa455352015-08-12 17:47:47 -0700179 { // Autolock scope
180 Mutex::Autolock lock(mCore->mMutex);
181 mCore->waitWhileAllocatingLocked();
182
183 if (mCore->mIsAbandoned) {
184 BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
185 return NO_INIT;
186 }
187
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700188 if (async == mCore->mAsyncMode) {
189 return NO_ERROR;
190 }
191
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700192 if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700193 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
194 mCore->mMaxBufferCount) {
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700195 BQ_LOGE("setAsyncMode(%d): this call would cause the "
196 "maxBufferCount (%d) to be exceeded (maxAcquired %d "
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700197 "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
198 mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
199 mCore->mMaxDequeuedBufferCount,
200 mCore->mDequeueBufferCannotBlock);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700201 return BAD_VALUE;
202 }
203
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800204 int delta = mCore->getMaxBufferCountLocked(async,
205 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
206 - mCore->getMaxBufferCountLocked();
207
Pablo Ceballos981066c2016-02-18 12:54:37 -0800208 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800209 BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
210 "available slots. Delta = %d", delta);
211 return BAD_VALUE;
212 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700213 mCore->mAsyncMode = async;
Pablo Ceballos9e314332016-01-12 13:49:19 -0800214 VALIDATE_CONSISTENCY();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700215 mCore->mDequeueCondition.broadcast();
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700216 if (delta < 0) {
217 listener = mCore->mConsumerListener;
218 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700219 } // Autolock scope
220
221 // Call back without lock held
Pablo Ceballos981066c2016-02-18 12:54:37 -0800222 if (listener != NULL) {
223 listener->onBuffersReleased();
Pablo Ceballosfa455352015-08-12 17:47:47 -0700224 }
225 return NO_ERROR;
226}
227
Dan Stoza5ecfb682016-01-04 17:01:02 -0800228int BufferQueueProducer::getFreeBufferLocked() const {
229 if (mCore->mFreeBuffers.empty()) {
230 return BufferQueueCore::INVALID_BUFFER_SLOT;
231 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800232 int slot = mCore->mFreeBuffers.front();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800233 mCore->mFreeBuffers.pop_front();
234 return slot;
235}
236
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800237int BufferQueueProducer::getFreeSlotLocked() const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800238 if (mCore->mFreeSlots.empty()) {
239 return BufferQueueCore::INVALID_BUFFER_SLOT;
240 }
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800241 int slot = *(mCore->mFreeSlots.begin());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800242 mCore->mFreeSlots.erase(slot);
Pablo Ceballosdce5c552016-02-10 15:43:22 -0800243 return slot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800244}
245
246status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800247 int* found) const {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800248 auto callerString = (caller == FreeSlotCaller::Dequeue) ?
249 "dequeueBuffer" : "attachBuffer";
Dan Stoza9f3053d2014-03-06 15:14:33 -0800250 bool tryAgain = true;
251 while (tryAgain) {
252 if (mCore->mIsAbandoned) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800253 BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800254 return NO_INIT;
255 }
256
Dan Stoza9f3053d2014-03-06 15:14:33 -0800257 int dequeuedCount = 0;
258 int acquiredCount = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800259 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700260 if (mSlots[s].mBufferState.isDequeued()) {
261 ++dequeuedCount;
262 }
263 if (mSlots[s].mBufferState.isAcquired()) {
264 ++acquiredCount;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800265 }
266 }
267
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700268 // Producers are not allowed to dequeue more than
269 // mMaxDequeuedBufferCount buffers.
270 // This check is only done if a buffer has already been queued
271 if (mCore->mBufferHasBeenQueued &&
272 dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
273 BQ_LOGE("%s: attempting to exceed the max dequeued buffer count "
Dan Stoza5ecfb682016-01-04 17:01:02 -0800274 "(%d)", callerString, mCore->mMaxDequeuedBufferCount);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800275 return INVALID_OPERATION;
276 }
277
Dan Stoza0de7ea72015-04-23 13:20:51 -0700278 *found = BufferQueueCore::INVALID_BUFFER_SLOT;
279
Dan Stozaae3c3682014-04-18 15:43:35 -0700280 // If we disconnect and reconnect quickly, we can be in a state where
281 // our slots are empty but we have many buffers in the queue. This can
282 // cause us to run out of memory if we outrun the consumer. Wait here if
283 // it looks like we have too many buffers queued up.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800284 const int maxBufferCount = mCore->getMaxBufferCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700285 bool tooManyBuffers = mCore->mQueue.size()
286 > static_cast<size_t>(maxBufferCount);
Dan Stozaae3c3682014-04-18 15:43:35 -0700287 if (tooManyBuffers) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800288 BQ_LOGV("%s: queue size is %zu, waiting", callerString,
Dan Stozaae3c3682014-04-18 15:43:35 -0700289 mCore->mQueue.size());
Dan Stoza0de7ea72015-04-23 13:20:51 -0700290 } else {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 // If in shared buffer mode and a shared buffer exists, always
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700292 // return it.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700293 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700294 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700295 *found = mCore->mSharedBufferSlot;
Dan Stoza5ecfb682016-01-04 17:01:02 -0800296 } else {
297 if (caller == FreeSlotCaller::Dequeue) {
298 // If we're calling this from dequeue, prefer free buffers
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800299 int slot = getFreeBufferLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800300 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
301 *found = slot;
302 } else if (mCore->mAllowAllocation) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800303 *found = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800304 }
305 } else {
306 // If we're calling this from attach, prefer free slots
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800307 int slot = getFreeSlotLocked();
Dan Stoza5ecfb682016-01-04 17:01:02 -0800308 if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
309 *found = slot;
310 } else {
311 *found = getFreeBufferLocked();
312 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700313 }
314 }
Dan Stozaae3c3682014-04-18 15:43:35 -0700315 }
316
317 // If no buffer is found, or if the queue has too many buffers
318 // outstanding, wait for a buffer to be acquired or released, or for the
319 // max buffer count to change.
320 tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
321 tooManyBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322 if (tryAgain) {
323 // Return an error if we're in non-blocking mode (producer and
324 // consumer are controlled by the application).
325 // However, the consumer is allowed to briefly acquire an extra
326 // buffer (which could cause us to have to wait here), which is
327 // okay, since it is only used to implement an atomic acquire +
328 // release (e.g., in GLConsumer::updateTexImage())
Pablo Ceballosfa455352015-08-12 17:47:47 -0700329 if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
Dan Stoza9f3053d2014-03-06 15:14:33 -0800330 (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
331 return WOULD_BLOCK;
332 }
Dan Stoza127fc632015-06-30 13:43:32 -0700333 if (mDequeueTimeout >= 0) {
334 status_t result = mCore->mDequeueCondition.waitRelative(
335 mCore->mMutex, mDequeueTimeout);
336 if (result == TIMED_OUT) {
337 return result;
338 }
339 } else {
340 mCore->mDequeueCondition.wait(mCore->mMutex);
341 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800342 }
343 } // while (tryAgain)
344
345 return NO_ERROR;
346}
347
Dan Stoza289ade12014-02-28 11:17:17 -0800348status_t BufferQueueProducer::dequeueBuffer(int *outSlot,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700349 sp<android::Fence> *outFence, uint32_t width, uint32_t height,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700350 PixelFormat format, uint32_t usage,
351 FrameEventHistoryDelta* outTimestamps) {
Dan Stoza289ade12014-02-28 11:17:17 -0800352 ATRACE_CALL();
353 { // Autolock scope
354 Mutex::Autolock lock(mCore->mMutex);
355 mConsumerName = mCore->mConsumerName;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700356
357 if (mCore->mIsAbandoned) {
358 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
359 return NO_INIT;
360 }
361
362 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
363 BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
364 return NO_INIT;
365 }
Dan Stoza289ade12014-02-28 11:17:17 -0800366 } // Autolock scope
367
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700368 BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height,
369 format, usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800370
371 if ((width && !height) || (!width && height)) {
372 BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
373 return BAD_VALUE;
374 }
375
376 status_t returnFlags = NO_ERROR;
377 EGLDisplay eglDisplay = EGL_NO_DISPLAY;
378 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800379 bool attachedByConsumer = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800380
381 { // Autolock scope
382 Mutex::Autolock lock(mCore->mMutex);
Antoine Labour78014f32014-07-15 21:17:03 -0700383 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800384
385 if (format == 0) {
386 format = mCore->mDefaultBufferFormat;
387 }
388
389 // Enable the usage bits the consumer requested
390 usage |= mCore->mConsumerUsageBits;
391
Dan Stoza9de72932015-04-16 17:28:43 -0700392 const bool useDefaultSize = !width && !height;
393 if (useDefaultSize) {
394 width = mCore->mDefaultWidth;
395 height = mCore->mDefaultHeight;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800396 }
Dan Stoza289ade12014-02-28 11:17:17 -0800397
Pablo Ceballos981066c2016-02-18 12:54:37 -0800398 int found = BufferItem::INVALID_BUFFER_SLOT;
Dan Stoza9de72932015-04-16 17:28:43 -0700399 while (found == BufferItem::INVALID_BUFFER_SLOT) {
Dan Stoza5ecfb682016-01-04 17:01:02 -0800400 status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue,
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800401 &found);
Dan Stoza9de72932015-04-16 17:28:43 -0700402 if (status != NO_ERROR) {
403 return status;
404 }
405
406 // This should not happen
407 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
408 BQ_LOGE("dequeueBuffer: no available buffer slots");
409 return -EBUSY;
410 }
411
412 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
413
414 // If we are not allowed to allocate new buffers,
415 // waitForFreeSlotThenRelock must have returned a slot containing a
416 // buffer. If this buffer would require reallocation to meet the
417 // requested attributes, we free it and attempt to get another one.
418 if (!mCore->mAllowAllocation) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700419 if (buffer->needsReallocation(width, height, format,
420 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700421 if (mCore->mSharedBufferSlot == found) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700422 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
423 "buffer");
424 return BAD_VALUE;
425 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800426 mCore->mFreeSlots.insert(found);
427 mCore->clearBufferSlotLocked(found);
Dan Stoza9de72932015-04-16 17:28:43 -0700428 found = BufferItem::INVALID_BUFFER_SLOT;
429 continue;
430 }
431 }
Dan Stoza289ade12014-02-28 11:17:17 -0800432 }
433
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800434 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700435 if (mCore->mSharedBufferSlot == found &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700436 buffer->needsReallocation(width, height, format,
437 BQ_LAYER_COUNT, usage)) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800438 BQ_LOGE("dequeueBuffer: cannot re-allocate a shared"
439 "buffer");
440
441 return BAD_VALUE;
442 }
443
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700444 if (mCore->mSharedBufferSlot != found) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 mCore->mActiveBuffers.insert(found);
446 }
Dan Stoza289ade12014-02-28 11:17:17 -0800447 *outSlot = found;
448 ATRACE_BUFFER_INDEX(found);
449
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800450 attachedByConsumer = mSlots[found].mNeedsReallocation;
451 mSlots[found].mNeedsReallocation = false;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800452
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700453 mSlots[found].mBufferState.dequeue();
454
Dan Stoza289ade12014-02-28 11:17:17 -0800455 if ((buffer == NULL) ||
Mathias Agopian0556d792017-03-22 15:49:32 -0700456 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage))
Dan Stoza289ade12014-02-28 11:17:17 -0800457 {
458 mSlots[found].mAcquireCalled = false;
459 mSlots[found].mGraphicBuffer = NULL;
460 mSlots[found].mRequestBufferCalled = false;
461 mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
462 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
463 mSlots[found].mFence = Fence::NO_FENCE;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800464 mCore->mBufferAge = 0;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700465 mCore->mIsAllocating = true;
Dan Stoza289ade12014-02-28 11:17:17 -0800466
467 returnFlags |= BUFFER_NEEDS_REALLOCATION;
Dan Stoza4afd8b62015-02-25 16:49:08 -0800468 } else {
469 // We add 1 because that will be the frame number when this buffer
470 // is queued
471 mCore->mBufferAge =
472 mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800473 }
474
Dan Stoza800b41a2015-04-28 14:20:04 -0700475 BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
476 mCore->mBufferAge);
Dan Stoza4afd8b62015-02-25 16:49:08 -0800477
Dan Stoza289ade12014-02-28 11:17:17 -0800478 if (CC_UNLIKELY(mSlots[found].mFence == NULL)) {
479 BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
480 "slot=%d w=%d h=%d format=%u",
481 found, buffer->width, buffer->height, buffer->format);
482 }
483
484 eglDisplay = mSlots[found].mEglDisplay;
485 eglFence = mSlots[found].mEglFence;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700486 // Don't return a fence in shared buffer mode, except for the first
487 // frame.
488 *outFence = (mCore->mSharedBufferMode &&
489 mCore->mSharedBufferSlot == found) ?
490 Fence::NO_FENCE : mSlots[found].mFence;
Dan Stoza289ade12014-02-28 11:17:17 -0800491 mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
492 mSlots[found].mFence = Fence::NO_FENCE;
Pablo Ceballos28c65ad2016-06-01 15:03:21 -0700493
494 // If shared buffer mode has just been enabled, cache the slot of the
495 // first buffer that is dequeued and mark it as the shared buffer.
496 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
497 BufferQueueCore::INVALID_BUFFER_SLOT) {
498 mCore->mSharedBufferSlot = found;
499 mSlots[found].mBufferState.mShared = true;
500 }
Dan Stoza289ade12014-02-28 11:17:17 -0800501 } // Autolock scope
502
503 if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700504 BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
Mathias Agopian0556d792017-03-22 15:49:32 -0700505 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Chris Forbesf3ef3ea2017-04-20 12:43:28 -0700506 width, height, format, BQ_LAYER_COUNT, usage,
Mathias Agopian0556d792017-03-22 15:49:32 -0700507 {mConsumerName.string(), mConsumerName.size()});
508
509 status_t error = graphicBuffer->initCheck();
510
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;
Jammy Yuc5cd2072017-02-22 16:41:38 -0800735 mSlots[*outSlot].mNeedsReallocation = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800736 mCore->mActiveBuffers.insert(found);
Pablo Ceballos9e314332016-01-12 13:49:19 -0800737 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700738
Dan Stoza9f3053d2014-03-06 15:14:33 -0800739 return returnFlags;
740}
741
Dan Stoza289ade12014-02-28 11:17:17 -0800742status_t BufferQueueProducer::queueBuffer(int slot,
743 const QueueBufferInput &input, QueueBufferOutput *output) {
744 ATRACE_CALL();
745 ATRACE_BUFFER_INDEX(slot);
746
Brian Andersond6927fb2016-07-23 23:37:30 -0700747 int64_t requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800748 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800749 android_dataspace dataSpace;
Pablo Ceballos60d69222015-08-07 14:47:20 -0700750 Rect crop(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800751 int scalingMode;
752 uint32_t transform;
Ruben Brunk1681d952014-06-27 15:51:55 -0700753 uint32_t stickyTransform;
Brian Andersond6927fb2016-07-23 23:37:30 -0700754 sp<Fence> acquireFence;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700755 bool getFrameTimestamps = false;
Brian Andersond6927fb2016-07-23 23:37:30 -0700756 input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700757 &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
758 &getFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700759 Region surfaceDamage = input.getSurfaceDamage();
Dan Stoza289ade12014-02-28 11:17:17 -0800760
Brian Andersond6927fb2016-07-23 23:37:30 -0700761 if (acquireFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800762 BQ_LOGE("queueBuffer: fence is NULL");
Jesse Hallde288fe2014-11-04 08:30:48 -0800763 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800764 }
765
Brian Anderson3d4039d2016-09-23 16:31:30 -0700766 auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
767
Dan Stoza289ade12014-02-28 11:17:17 -0800768 switch (scalingMode) {
769 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
770 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
771 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
772 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
773 break;
774 default:
775 BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800776 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800777 }
778
Dan Stoza8dc55392014-11-04 11:37:46 -0800779 sp<IConsumerListener> frameAvailableListener;
780 sp<IConsumerListener> frameReplacedListener;
781 int callbackTicket = 0;
Brian Andersond6927fb2016-07-23 23:37:30 -0700782 uint64_t currentFrameNumber = 0;
Dan Stoza8dc55392014-11-04 11:37:46 -0800783 BufferItem item;
Dan Stoza289ade12014-02-28 11:17:17 -0800784 { // Autolock scope
785 Mutex::Autolock lock(mCore->mMutex);
786
787 if (mCore->mIsAbandoned) {
788 BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
789 return NO_INIT;
790 }
791
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700792 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
793 BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
794 return NO_INIT;
795 }
796
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800797 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -0800798 BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)",
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800799 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800800 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700801 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800802 BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700803 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800804 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800805 } else if (!mSlots[slot].mRequestBufferCalled) {
806 BQ_LOGE("queueBuffer: slot %d was queued without requesting "
807 "a buffer", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800808 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800809 }
810
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700811 // If shared buffer mode has just been enabled, cache the slot of the
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700812 // first buffer that is queued and mark it as the shared buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700813 if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700814 BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700815 mCore->mSharedBufferSlot = slot;
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700816 mSlots[slot].mBufferState.mShared = true;
817 }
818
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800819 BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700820 " crop=[%d,%d,%d,%d] transform=%#x scale=%s",
Brian Andersond6927fb2016-07-23 23:37:30 -0700821 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp,
822 dataSpace, crop.left, crop.top, crop.right, crop.bottom,
823 transform,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800824 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
Dan Stoza289ade12014-02-28 11:17:17 -0800825
826 const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
827 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
Pablo Ceballos60d69222015-08-07 14:47:20 -0700828 Rect croppedRect(Rect::EMPTY_RECT);
Dan Stoza289ade12014-02-28 11:17:17 -0800829 crop.intersect(bufferRect, &croppedRect);
830 if (croppedRect != crop) {
831 BQ_LOGE("queueBuffer: crop rect is not contained within the "
832 "buffer in slot %d", slot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800833 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800834 }
835
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800836 // Override UNKNOWN dataspace with consumer default
837 if (dataSpace == HAL_DATASPACE_UNKNOWN) {
838 dataSpace = mCore->mDefaultBufferDataSpace;
839 }
840
Brian Andersond6927fb2016-07-23 23:37:30 -0700841 mSlots[slot].mFence = acquireFence;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700842 mSlots[slot].mBufferState.queue();
843
Brian Andersond6927fb2016-07-23 23:37:30 -0700844 // Increment the frame counter and store a local version of it
845 // for use outside the lock on mCore->mMutex.
Dan Stoza289ade12014-02-28 11:17:17 -0800846 ++mCore->mFrameCounter;
Brian Andersond6927fb2016-07-23 23:37:30 -0700847 currentFrameNumber = mCore->mFrameCounter;
848 mSlots[slot].mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800849
Dan Stoza289ade12014-02-28 11:17:17 -0800850 item.mAcquireCalled = mSlots[slot].mAcquireCalled;
851 item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
852 item.mCrop = crop;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800853 item.mTransform = transform &
854 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Dan Stoza289ade12014-02-28 11:17:17 -0800855 item.mTransformToDisplayInverse =
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800856 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
857 item.mScalingMode = static_cast<uint32_t>(scalingMode);
Brian Andersond6927fb2016-07-23 23:37:30 -0700858 item.mTimestamp = requestedPresentTimestamp;
Dan Stoza289ade12014-02-28 11:17:17 -0800859 item.mIsAutoTimestamp = isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800860 item.mDataSpace = dataSpace;
Brian Andersond6927fb2016-07-23 23:37:30 -0700861 item.mFrameNumber = currentFrameNumber;
Dan Stoza289ade12014-02-28 11:17:17 -0800862 item.mSlot = slot;
Brian Andersond6927fb2016-07-23 23:37:30 -0700863 item.mFence = acquireFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700864 item.mFenceTime = acquireFenceTime;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700865 item.mIsDroppable = mCore->mAsyncMode ||
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700866 mCore->mDequeueBufferCannotBlock ||
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700867 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
Dan Stoza5065a552015-03-17 16:23:42 -0700868 item.mSurfaceDamage = surfaceDamage;
Pablo Ceballos06312182015-10-07 16:32:12 -0700869 item.mQueuedBuffer = true;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700870 item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
Dan Stoza289ade12014-02-28 11:17:17 -0800871
Ruben Brunk1681d952014-06-27 15:51:55 -0700872 mStickyTransform = stickyTransform;
873
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700874 // Cache the shared buffer data so that the BufferItem can be recreated.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700875 if (mCore->mSharedBufferMode) {
876 mCore->mSharedBufferCache.crop = crop;
877 mCore->mSharedBufferCache.transform = transform;
878 mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700879 scalingMode);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700880 mCore->mSharedBufferCache.dataspace = dataSpace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700881 }
882
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800883 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -0800884 if (mCore->mQueue.empty()) {
885 // When the queue is empty, we can ignore mDequeueBufferCannotBlock
886 // and simply queue this buffer
887 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800888 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800889 } else {
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700890 // When the queue is not empty, we need to look at the last buffer
891 // in the queue to see if we need to replace it
892 const BufferItem& last = mCore->mQueue.itemAt(
893 mCore->mQueue.size() - 1);
894 if (last.mIsDroppable) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800895
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700896 if (!last.mIsStale) {
897 mSlots[last.mSlot].mBufferState.freeQueued();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700898
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700899 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700900 // still be around. Mark it as no longer shared if this
901 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700902 if (!mCore->mSharedBufferMode &&
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700903 mSlots[last.mSlot].mBufferState.isFree()) {
904 mSlots[last.mSlot].mBufferState.mShared = false;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700905 }
906 // Don't put the shared buffer on the free list.
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700907 if (!mSlots[last.mSlot].mBufferState.isShared()) {
908 mCore->mActiveBuffers.erase(last.mSlot);
909 mCore->mFreeBuffers.push_back(last.mSlot);
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800910 output->bufferReplaced = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700911 }
Dan Stoza289ade12014-02-28 11:17:17 -0800912 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800913
Dan Stoza289ade12014-02-28 11:17:17 -0800914 // Overwrite the droppable buffer with the incoming one
Pablo Ceballos4d85da42016-04-19 20:11:56 -0700915 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
Dan Stoza8dc55392014-11-04 11:37:46 -0800916 frameReplacedListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800917 } else {
918 mCore->mQueue.push_back(item);
Dan Stoza8dc55392014-11-04 11:37:46 -0800919 frameAvailableListener = mCore->mConsumerListener;
Dan Stoza289ade12014-02-28 11:17:17 -0800920 }
921 }
922
923 mCore->mBufferHasBeenQueued = true;
924 mCore->mDequeueCondition.broadcast();
Dan Stoza50101d02016-04-07 16:53:23 -0700925 mCore->mLastQueuedSlot = slot;
Dan Stoza289ade12014-02-28 11:17:17 -0800926
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700927 output->width = mCore->mDefaultWidth;
928 output->height = mCore->mDefaultHeight;
929 output->transformHint = mCore->mTransformHint;
930 output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
931 output->nextFrameNumber = mCore->mFrameCounter + 1;
Dan Stoza289ade12014-02-28 11:17:17 -0800932
Colin Cross152c3b72016-09-27 14:08:19 -0700933 ATRACE_INT(mCore->mConsumerName.string(),
934 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700935 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Dan Stoza8dc55392014-11-04 11:37:46 -0800936
937 // Take a ticket for the callback functions
938 callbackTicket = mNextCallbackTicket++;
Dan Stoza0de7ea72015-04-23 13:20:51 -0700939
Pablo Ceballos9e314332016-01-12 13:49:19 -0800940 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800941 } // Autolock scope
942
Irvel468051e2016-06-13 16:44:44 -0700943 // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
944 // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
945 // there will be no Binder call
946 if (!mConsumerIsSurfaceFlinger) {
947 item.mGraphicBuffer.clear();
948 }
949
950 // Don't send the slot number through the callback since the consumer shouldn't need it
Dan Stoza8dc55392014-11-04 11:37:46 -0800951 item.mSlot = BufferItem::INVALID_BUFFER_SLOT;
952
953 // Call back without the main BufferQueue lock held, but with the callback
954 // lock held so we can ensure that callbacks occur in order
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700955
956 int connectedApi;
957 sp<Fence> lastQueuedFence;
958
959 { // scope for the lock
Dan Stoza8dc55392014-11-04 11:37:46 -0800960 Mutex::Autolock lock(mCallbackMutex);
961 while (callbackTicket != mCurrentCallbackTicket) {
962 mCallbackCondition.wait(mCallbackMutex);
963 }
964
965 if (frameAvailableListener != NULL) {
966 frameAvailableListener->onFrameAvailable(item);
967 } else if (frameReplacedListener != NULL) {
968 frameReplacedListener->onFrameReplaced(item);
969 }
970
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700971 connectedApi = mCore->mConnectedApi;
972 lastQueuedFence = std::move(mLastQueueBufferFence);
973
974 mLastQueueBufferFence = std::move(acquireFence);
975 mLastQueuedCrop = item.mCrop;
976 mLastQueuedTransform = item.mTransform;
977
Dan Stoza8dc55392014-11-04 11:37:46 -0800978 ++mCurrentCallbackTicket;
979 mCallbackCondition.broadcast();
Dan Stoza289ade12014-02-28 11:17:17 -0800980 }
981
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000982 // Wait without lock held
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700983 if (connectedApi == NATIVE_WINDOW_API_EGL) {
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000984 // Waiting here allows for two full buffers to be queued but not a
985 // third. In the event that frames take varying time, this makes a
986 // small trade-off in favor of latency rather than throughput.
Mathias Agopian4f6ce7c2017-04-03 17:14:31 -0700987 lastQueuedFence->waitForever("Throttling EGL Production");
Christian Poetzsch82fbb122015-12-07 13:36:22 +0000988 }
989
Brian Andersond6927fb2016-07-23 23:37:30 -0700990 // Update and get FrameEventHistory.
991 nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
992 NewFrameEventsEntry newFrameEventsEntry = {
993 currentFrameNumber,
994 postedTime,
995 requestedPresentTimestamp,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700996 std::move(acquireFenceTime)
Brian Andersond6927fb2016-07-23 23:37:30 -0700997 };
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700998 addAndGetFrameTimestamps(&newFrameEventsEntry,
999 getFrameTimestamps ? &output->frameTimestamps : nullptr);
Brian Andersond6927fb2016-07-23 23:37:30 -07001000
Dan Stoza289ade12014-02-28 11:17:17 -08001001 return NO_ERROR;
1002}
1003
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001004status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
Dan Stoza289ade12014-02-28 11:17:17 -08001005 ATRACE_CALL();
1006 BQ_LOGV("cancelBuffer: slot %d", slot);
1007 Mutex::Autolock lock(mCore->mMutex);
1008
1009 if (mCore->mIsAbandoned) {
1010 BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001011 return NO_INIT;
1012 }
1013
1014 if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1015 BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1016 return NO_INIT;
Dan Stoza289ade12014-02-28 11:17:17 -08001017 }
1018
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001019 if (mCore->mSharedBufferMode) {
1020 BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001021 return BAD_VALUE;
1022 }
1023
Dan Stoza3e96f192014-03-03 10:16:19 -08001024 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
Dan Stoza289ade12014-02-28 11:17:17 -08001025 BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)",
Dan Stoza3e96f192014-03-03 10:16:19 -08001026 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001027 return BAD_VALUE;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001028 } else if (!mSlots[slot].mBufferState.isDequeued()) {
Dan Stoza289ade12014-02-28 11:17:17 -08001029 BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001030 "(state = %s)", slot, mSlots[slot].mBufferState.string());
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001031 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001032 } else if (fence == NULL) {
1033 BQ_LOGE("cancelBuffer: fence is NULL");
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001034 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001035 }
1036
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001037 mSlots[slot].mBufferState.cancel();
1038
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001039 // After leaving shared buffer mode, the shared buffer will still be around.
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001040 // Mark it as no longer shared if this operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001041 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001042 mSlots[slot].mBufferState.mShared = false;
1043 }
1044
1045 // Don't put the shared buffer on the free list.
1046 if (!mSlots[slot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001047 mCore->mActiveBuffers.erase(slot);
1048 mCore->mFreeBuffers.push_back(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001049 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001050
Dan Stoza289ade12014-02-28 11:17:17 -08001051 mSlots[slot].mFence = fence;
1052 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001053 VALIDATE_CONSISTENCY();
Pablo Ceballos583b1b32015-09-03 18:23:52 -07001054
1055 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -08001056}
1057
1058int BufferQueueProducer::query(int what, int *outValue) {
1059 ATRACE_CALL();
1060 Mutex::Autolock lock(mCore->mMutex);
1061
1062 if (outValue == NULL) {
1063 BQ_LOGE("query: outValue was NULL");
1064 return BAD_VALUE;
1065 }
1066
1067 if (mCore->mIsAbandoned) {
1068 BQ_LOGE("query: BufferQueue has been abandoned");
1069 return NO_INIT;
1070 }
1071
1072 int value;
1073 switch (what) {
1074 case NATIVE_WINDOW_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001075 value = static_cast<int32_t>(mCore->mDefaultWidth);
Dan Stoza289ade12014-02-28 11:17:17 -08001076 break;
1077 case NATIVE_WINDOW_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001078 value = static_cast<int32_t>(mCore->mDefaultHeight);
Dan Stoza289ade12014-02-28 11:17:17 -08001079 break;
1080 case NATIVE_WINDOW_FORMAT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001081 value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
Dan Stoza289ade12014-02-28 11:17:17 -08001082 break;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001083 case NATIVE_WINDOW_LAYER_COUNT:
1084 // All BufferQueue buffers have a single layer.
1085 value = BQ_LAYER_COUNT;
1086 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001087 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001088 value = mCore->getMinUndequeuedBufferCountLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001089 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001090 case NATIVE_WINDOW_STICKY_TRANSFORM:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001091 value = static_cast<int32_t>(mStickyTransform);
Ruben Brunk1681d952014-06-27 15:51:55 -07001092 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001093 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1094 value = (mCore->mQueue.size() > 1);
1095 break;
1096 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001097 value = static_cast<int32_t>(mCore->mConsumerUsageBits);
Dan Stoza289ade12014-02-28 11:17:17 -08001098 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001099 case NATIVE_WINDOW_DEFAULT_DATASPACE:
1100 value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1101 break;
Dan Stoza4afd8b62015-02-25 16:49:08 -08001102 case NATIVE_WINDOW_BUFFER_AGE:
1103 if (mCore->mBufferAge > INT32_MAX) {
1104 value = 0;
1105 } else {
1106 value = static_cast<int32_t>(mCore->mBufferAge);
1107 }
1108 break;
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -07001109 case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1110 value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1111 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001112 default:
1113 return BAD_VALUE;
1114 }
1115
1116 BQ_LOGV("query: %d? %d", what, value);
1117 *outValue = value;
1118 return NO_ERROR;
1119}
1120
Dan Stozaf0eaf252014-03-21 13:05:51 -07001121status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -08001122 int api, bool producerControlledByApp, QueueBufferOutput *output) {
1123 ATRACE_CALL();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001124 Mutex::Autolock lock(mCore->mMutex);
1125 mConsumerName = mCore->mConsumerName;
1126 BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1127 producerControlledByApp ? "true" : "false");
1128
1129 if (mCore->mIsAbandoned) {
1130 BQ_LOGE("connect: BufferQueue has been abandoned");
1131 return NO_INIT;
1132 }
1133
1134 if (mCore->mConsumerListener == NULL) {
1135 BQ_LOGE("connect: BufferQueue has no consumer");
1136 return NO_INIT;
1137 }
1138
1139 if (output == NULL) {
1140 BQ_LOGE("connect: output was NULL");
1141 return BAD_VALUE;
1142 }
1143
1144 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1145 BQ_LOGE("connect: already connected (cur=%d req=%d)",
1146 mCore->mConnectedApi, api);
1147 return BAD_VALUE;
1148 }
1149
1150 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1151 mDequeueTimeout < 0 ?
1152 mCore->mConsumerControlledByApp && producerControlledByApp : false,
1153 mCore->mMaxBufferCount) -
1154 mCore->getMaxBufferCountLocked();
1155 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1156 BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1157 "slots. Delta = %d", delta);
1158 return BAD_VALUE;
1159 }
1160
Dan Stoza289ade12014-02-28 11:17:17 -08001161 int status = NO_ERROR;
Pablo Ceballos981066c2016-02-18 12:54:37 -08001162 switch (api) {
1163 case NATIVE_WINDOW_API_EGL:
1164 case NATIVE_WINDOW_API_CPU:
1165 case NATIVE_WINDOW_API_MEDIA:
1166 case NATIVE_WINDOW_API_CAMERA:
1167 mCore->mConnectedApi = api;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001168
1169 output->width = mCore->mDefaultWidth;
1170 output->height = mCore->mDefaultHeight;
1171 output->transformHint = mCore->mTransformHint;
1172 output->numPendingBuffers =
1173 static_cast<uint32_t>(mCore->mQueue.size());
1174 output->nextFrameNumber = mCore->mFrameCounter + 1;
Shuzhen Wang22f842b2017-01-18 23:02:36 -08001175 output->bufferReplaced = false;
Dan Stoza289ade12014-02-28 11:17:17 -08001176
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001177 if (listener != NULL) {
1178 // Set up a death notification so that we can disconnect
1179 // automatically if the remote producer dies
1180 if (IInterface::asBinder(listener)->remoteBinder() != NULL) {
1181 status = IInterface::asBinder(listener)->linkToDeath(
1182 static_cast<IBinder::DeathRecipient*>(this));
1183 if (status != NO_ERROR) {
1184 BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1185 strerror(-status), status);
1186 }
1187 mCore->mLinkedToDeath = listener;
1188 }
1189 if (listener->needsReleaseNotify()) {
1190 mCore->mConnectedProducerListener = listener;
Dan Stoza289ade12014-02-28 11:17:17 -08001191 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001192 }
Pablo Ceballos981066c2016-02-18 12:54:37 -08001193 break;
1194 default:
1195 BQ_LOGE("connect: unknown API %d", api);
1196 status = BAD_VALUE;
1197 break;
Dan Stoza289ade12014-02-28 11:17:17 -08001198 }
Robert Carr97b9c862016-09-08 13:54:35 -07001199 mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
Pablo Ceballos981066c2016-02-18 12:54:37 -08001200 mCore->mBufferHasBeenQueued = false;
1201 mCore->mDequeueBufferCannotBlock = false;
1202 if (mDequeueTimeout < 0) {
1203 mCore->mDequeueBufferCannotBlock =
1204 mCore->mConsumerControlledByApp && producerControlledByApp;
Dan Stoza127fc632015-06-30 13:43:32 -07001205 }
Dan Stoza289ade12014-02-28 11:17:17 -08001206
Pablo Ceballos981066c2016-02-18 12:54:37 -08001207 mCore->mAllowAllocation = true;
1208 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -08001209 return status;
1210}
1211
Robert Carr97b9c862016-09-08 13:54:35 -07001212status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
Dan Stoza289ade12014-02-28 11:17:17 -08001213 ATRACE_CALL();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001214 BQ_LOGV("disconnect: api %d", api);
Dan Stoza289ade12014-02-28 11:17:17 -08001215
1216 int status = NO_ERROR;
1217 sp<IConsumerListener> listener;
1218 { // Autolock scope
1219 Mutex::Autolock lock(mCore->mMutex);
Robert Carr97b9c862016-09-08 13:54:35 -07001220
1221 if (mode == DisconnectMode::AllLocal) {
1222 if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
1223 return NO_ERROR;
1224 }
1225 api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1226 }
1227
Antoine Labour78014f32014-07-15 21:17:03 -07001228 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -08001229
1230 if (mCore->mIsAbandoned) {
1231 // It's not really an error to disconnect after the surface has
1232 // been abandoned; it should just be a no-op.
1233 return NO_ERROR;
1234 }
1235
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001236 if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
Chong Zhang5ac51482017-02-16 15:52:33 -08001237 if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1238 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1239 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001240 api = mCore->mConnectedApi;
Chong Zhang26bb2b12016-03-08 12:08:33 -08001241 // If we're asked to disconnect the currently connected api but
1242 // nobody is connected, it's not really an error.
1243 if (api == BufferQueueCore::NO_CONNECTED_API) {
1244 return NO_ERROR;
1245 }
Chong Zhang1b3a9ac2016-02-29 16:47:47 -08001246 }
1247
Dan Stoza289ade12014-02-28 11:17:17 -08001248 switch (api) {
1249 case NATIVE_WINDOW_API_EGL:
1250 case NATIVE_WINDOW_API_CPU:
1251 case NATIVE_WINDOW_API_MEDIA:
1252 case NATIVE_WINDOW_API_CAMERA:
1253 if (mCore->mConnectedApi == api) {
1254 mCore->freeAllBuffersLocked();
1255
1256 // Remove our death notification callback if we have one
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001257 if (mCore->mLinkedToDeath != NULL) {
Dan Stozaf0eaf252014-03-21 13:05:51 -07001258 sp<IBinder> token =
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001259 IInterface::asBinder(mCore->mLinkedToDeath);
Dan Stoza289ade12014-02-28 11:17:17 -08001260 // This can fail if we're here because of the death
1261 // notification, but we just ignore it
1262 token->unlinkToDeath(
1263 static_cast<IBinder::DeathRecipient*>(this));
1264 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001265 mCore->mSharedBufferSlot =
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001266 BufferQueueCore::INVALID_BUFFER_SLOT;
Matthew Bouyack3b8e6b22016-10-03 16:24:26 -07001267 mCore->mLinkedToDeath = NULL;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001268 mCore->mConnectedProducerListener = NULL;
Dan Stoza289ade12014-02-28 11:17:17 -08001269 mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
Robert Carr97b9c862016-09-08 13:54:35 -07001270 mCore->mConnectedPid = -1;
Jesse Hall399184a2014-03-03 15:42:54 -08001271 mCore->mSidebandStream.clear();
Dan Stoza289ade12014-02-28 11:17:17 -08001272 mCore->mDequeueCondition.broadcast();
1273 listener = mCore->mConsumerListener;
Wonsik Kim3e198b22017-04-07 15:43:16 -07001274 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1275 BQ_LOGE("disconnect: not connected (req=%d)", api);
1276 status = NO_INIT;
1277 } else {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001278 BQ_LOGE("disconnect: still connected to another API "
Dan Stoza289ade12014-02-28 11:17:17 -08001279 "(cur=%d req=%d)", mCore->mConnectedApi, api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001280 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001281 }
1282 break;
1283 default:
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001284 BQ_LOGE("disconnect: unknown API %d", api);
Dan Stoza9f3053d2014-03-06 15:14:33 -08001285 status = BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -08001286 break;
1287 }
1288 } // Autolock scope
1289
1290 // Call back without lock held
1291 if (listener != NULL) {
1292 listener->onBuffersReleased();
Brian Anderson5ea5e592016-12-01 16:54:33 -08001293 listener->onDisconnect();
Dan Stoza289ade12014-02-28 11:17:17 -08001294 }
1295
1296 return status;
1297}
1298
Jesse Hall399184a2014-03-03 15:42:54 -08001299status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
Wonsik Kimafe30812014-03-31 23:16:08 +09001300 sp<IConsumerListener> listener;
1301 { // Autolock scope
1302 Mutex::Autolock _l(mCore->mMutex);
1303 mCore->mSidebandStream = stream;
1304 listener = mCore->mConsumerListener;
1305 } // Autolock scope
1306
1307 if (listener != NULL) {
1308 listener->onSidebandStreamChanged();
1309 }
Jesse Hall399184a2014-03-03 15:42:54 -08001310 return NO_ERROR;
1311}
1312
Pablo Ceballos567dbbb2015-08-26 18:59:08 -07001313void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1314 PixelFormat format, uint32_t usage) {
Antoine Labour78014f32014-07-15 21:17:03 -07001315 ATRACE_CALL();
1316 while (true) {
Antoine Labour78014f32014-07-15 21:17:03 -07001317 size_t newBufferCount = 0;
1318 uint32_t allocWidth = 0;
1319 uint32_t allocHeight = 0;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001320 PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
Antoine Labour78014f32014-07-15 21:17:03 -07001321 uint32_t allocUsage = 0;
1322 { // Autolock scope
1323 Mutex::Autolock lock(mCore->mMutex);
1324 mCore->waitWhileAllocatingLocked();
Dan Stoza29a3e902014-06-20 13:13:57 -07001325
Dan Stoza9de72932015-04-16 17:28:43 -07001326 if (!mCore->mAllowAllocation) {
1327 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1328 "BufferQueue");
1329 return;
1330 }
1331
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001332 newBufferCount = mCore->mFreeSlots.size();
1333 if (newBufferCount == 0) {
Antoine Labour78014f32014-07-15 21:17:03 -07001334 return;
1335 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001336
Antoine Labour78014f32014-07-15 21:17:03 -07001337 allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1338 allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1339 allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1340 allocUsage = usage | mCore->mConsumerUsageBits;
1341
1342 mCore->mIsAllocating = true;
1343 } // Autolock scope
1344
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001345 Vector<sp<GraphicBuffer>> buffers;
Antoine Labour78014f32014-07-15 21:17:03 -07001346 for (size_t i = 0; i < newBufferCount; ++i) {
Mathias Agopian0556d792017-03-22 15:49:32 -07001347 sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(
Craig Donner6ebc46a2016-10-21 15:23:44 -07001348 allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
Chris Forbesf3ef3ea2017-04-20 12:43:28 -07001349 allocUsage, {mConsumerName.string(), mConsumerName.size()});
Mathias Agopian0556d792017-03-22 15:49:32 -07001350
1351 status_t result = graphicBuffer->initCheck();
1352
Antoine Labour78014f32014-07-15 21:17:03 -07001353 if (result != NO_ERROR) {
1354 BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1355 " %u, usage %u)", width, height, format, usage);
1356 Mutex::Autolock lock(mCore->mMutex);
1357 mCore->mIsAllocating = false;
1358 mCore->mIsAllocatingCondition.broadcast();
1359 return;
1360 }
1361 buffers.push_back(graphicBuffer);
1362 }
1363
1364 { // Autolock scope
1365 Mutex::Autolock lock(mCore->mMutex);
1366 uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1367 uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001368 PixelFormat checkFormat = format != 0 ?
1369 format : mCore->mDefaultBufferFormat;
Antoine Labour78014f32014-07-15 21:17:03 -07001370 uint32_t checkUsage = usage | mCore->mConsumerUsageBits;
1371 if (checkWidth != allocWidth || checkHeight != allocHeight ||
1372 checkFormat != allocFormat || checkUsage != allocUsage) {
1373 // Something changed while we released the lock. Retry.
1374 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1375 mCore->mIsAllocating = false;
1376 mCore->mIsAllocatingCondition.broadcast();
Dan Stoza29a3e902014-06-20 13:13:57 -07001377 continue;
1378 }
1379
Antoine Labour78014f32014-07-15 21:17:03 -07001380 for (size_t i = 0; i < newBufferCount; ++i) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001381 if (mCore->mFreeSlots.empty()) {
1382 BQ_LOGV("allocateBuffers: a slot was occupied while "
1383 "allocating. Dropping allocated buffer.");
Antoine Labour78014f32014-07-15 21:17:03 -07001384 continue;
1385 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001386 auto slot = mCore->mFreeSlots.begin();
1387 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1388 mSlots[*slot].mGraphicBuffer = buffers[i];
1389 mSlots[*slot].mFence = Fence::NO_FENCE;
Dan Stoza0de7ea72015-04-23 13:20:51 -07001390
1391 // freeBufferLocked puts this slot on the free slots list. Since
1392 // we then attached a buffer, move the slot to free buffer list.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001393 mCore->mFreeBuffers.push_front(*slot);
Dan Stoza0de7ea72015-04-23 13:20:51 -07001394
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001395 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1396 *slot);
Christopher Ferris87e94cd2016-04-26 11:29:08 -07001397
1398 // Make sure the erase is done after all uses of the slot
1399 // iterator since it will be invalid after this point.
1400 mCore->mFreeSlots.erase(slot);
Antoine Labour78014f32014-07-15 21:17:03 -07001401 }
Dan Stoza29a3e902014-06-20 13:13:57 -07001402
Antoine Labour78014f32014-07-15 21:17:03 -07001403 mCore->mIsAllocating = false;
1404 mCore->mIsAllocatingCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -08001405 VALIDATE_CONSISTENCY();
Antoine Labour78014f32014-07-15 21:17:03 -07001406 } // Autolock scope
Dan Stoza29a3e902014-06-20 13:13:57 -07001407 }
1408}
1409
Dan Stoza9de72932015-04-16 17:28:43 -07001410status_t BufferQueueProducer::allowAllocation(bool allow) {
1411 ATRACE_CALL();
1412 BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1413
1414 Mutex::Autolock lock(mCore->mMutex);
1415 mCore->mAllowAllocation = allow;
1416 return NO_ERROR;
1417}
1418
Dan Stoza812ed062015-06-02 15:45:22 -07001419status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1420 ATRACE_CALL();
1421 BQ_LOGV("setGenerationNumber: %u", generationNumber);
1422
1423 Mutex::Autolock lock(mCore->mMutex);
1424 mCore->mGenerationNumber = generationNumber;
1425 return NO_ERROR;
1426}
1427
Dan Stozac6f30bd2015-06-08 09:32:50 -07001428String8 BufferQueueProducer::getConsumerName() const {
1429 ATRACE_CALL();
Fabien Sanglardd073eb72016-11-08 15:35:02 -08001430 Mutex::Autolock lock(mCore->mMutex);
Dan Stozac6f30bd2015-06-08 09:32:50 -07001431 BQ_LOGV("getConsumerName: %s", mConsumerName.string());
1432 return mConsumerName;
1433}
1434
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001435status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001436 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001437 BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001438
1439 Mutex::Autolock lock(mCore->mMutex);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001440 if (!sharedBufferMode) {
1441 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001442 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001443 mCore->mSharedBufferMode = sharedBufferMode;
Dan Stoza127fc632015-06-30 13:43:32 -07001444 return NO_ERROR;
1445}
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001446
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001447status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1448 ATRACE_CALL();
1449 BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1450
1451 Mutex::Autolock lock(mCore->mMutex);
1452
1453 mCore->mAutoRefresh = autoRefresh;
1454 return NO_ERROR;
1455}
1456
Dan Stoza127fc632015-06-30 13:43:32 -07001457status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1458 ATRACE_CALL();
1459 BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1460
Pablo Ceballos981066c2016-02-18 12:54:37 -08001461 Mutex::Autolock lock(mCore->mMutex);
1462 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false,
1463 mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1464 if (!mCore->adjustAvailableSlotsLocked(delta)) {
1465 BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1466 "available slots. Delta = %d", delta);
1467 return BAD_VALUE;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001468 }
1469
Pablo Ceballos981066c2016-02-18 12:54:37 -08001470 mDequeueTimeout = timeout;
1471 mCore->mDequeueBufferCannotBlock = false;
Pablo Ceballos9e314332016-01-12 13:49:19 -08001472
Pablo Ceballos981066c2016-02-18 12:54:37 -08001473 VALIDATE_CONSISTENCY();
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001474 return NO_ERROR;
1475}
1476
Dan Stoza50101d02016-04-07 16:53:23 -07001477status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -07001478 sp<Fence>* outFence, float outTransformMatrix[16]) {
Dan Stoza50101d02016-04-07 16:53:23 -07001479 ATRACE_CALL();
1480 BQ_LOGV("getLastQueuedBuffer");
1481
1482 Mutex::Autolock lock(mCore->mMutex);
1483 if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1484 *outBuffer = nullptr;
1485 *outFence = Fence::NO_FENCE;
1486 return NO_ERROR;
1487 }
1488
1489 *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1490 *outFence = mLastQueueBufferFence;
1491
John Reck1a61da52016-04-28 13:18:15 -07001492 // Currently only SurfaceFlinger internally ever changes
1493 // GLConsumer's filtering mode, so we just use 'true' here as
1494 // this is slightly specialized for the current client of this API,
1495 // which does want filtering.
1496 GLConsumer::computeTransformMatrix(outTransformMatrix,
1497 mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1498 mLastQueuedTransform, true /* filter */);
1499
Dan Stoza50101d02016-04-07 16:53:23 -07001500 return NO_ERROR;
1501}
1502
Brian Anderson3890c392016-07-25 12:48:08 -07001503void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1504 addAndGetFrameTimestamps(nullptr, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001505}
Pablo Ceballosce796e72016-02-04 19:10:51 -08001506
Brian Anderson3890c392016-07-25 12:48:08 -07001507void BufferQueueProducer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -07001508 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -07001509 FrameEventHistoryDelta* outDelta) {
1510 if (newTimestamps == nullptr && outDelta == nullptr) {
1511 return;
Brian Andersond6927fb2016-07-23 23:37:30 -07001512 }
1513
1514 ATRACE_CALL();
1515 BQ_LOGV("addAndGetFrameTimestamps");
1516 sp<IConsumerListener> listener;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001517 {
1518 Mutex::Autolock lock(mCore->mMutex);
1519 listener = mCore->mConsumerListener;
1520 }
1521 if (listener != NULL) {
Brian Anderson3890c392016-07-25 12:48:08 -07001522 listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001523 }
Pablo Ceballosce796e72016-02-04 19:10:51 -08001524}
1525
Dan Stoza289ade12014-02-28 11:17:17 -08001526void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1527 // If we're here, it means that a producer we were connected to died.
1528 // We're guaranteed that we are still connected to it because we remove
1529 // this callback upon disconnect. It's therefore safe to read mConnectedApi
1530 // without synchronization here.
1531 int api = mCore->mConnectedApi;
1532 disconnect(api);
1533}
1534
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001535status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1536 BQ_LOGV("getUniqueId");
1537
1538 *outId = mCore->mUniqueId;
1539 return NO_ERROR;
1540}
1541
Dan Stoza289ade12014-02-28 11:17:17 -08001542} // namespace android