blob: 873814913f773beea9d0c82991d882bb8ad2aec2 [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>
Yifan Hongfc8d8852017-07-26 10:47:14 -070018#include <pwd.h>
19#include <sys/types.h>
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070020
Dan Stoza3e96f192014-03-03 10:16:19 -080021#define LOG_TAG "BufferQueueConsumer"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23//#define LOG_NDEBUG 0
24
Pablo Ceballos9e314332016-01-12 13:49:19 -080025#if DEBUG_ONLY_CODE
26#define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
27#else
28#define VALIDATE_CONSISTENCY()
29#endif
30
Dan Stoza289ade12014-02-28 11:17:17 -080031#include <gui/BufferItem.h>
32#include <gui/BufferQueueConsumer.h>
33#include <gui/BufferQueueCore.h>
34#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070035#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080036
Pablo Ceballos88f69282016-02-11 18:01:49 -080037#include <binder/IPCThreadState.h>
38#include <binder/PermissionCache.h>
Pablo Ceballos88f69282016-02-11 18:01:49 -080039
Dan Stoza289ade12014-02-28 11:17:17 -080040namespace android {
41
42BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
43 mCore(core),
44 mSlots(core->mSlots),
45 mConsumerName() {}
46
47BufferQueueConsumer::~BufferQueueConsumer() {}
48
49status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
Dan Stozaa4650a52015-05-12 12:56:16 -070050 nsecs_t expectedPresent, uint64_t maxFrameNumber) {
Dan Stoza289ade12014-02-28 11:17:17 -080051 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -080052
Lajos Molnar5f920c12015-07-13 16:04:24 -070053 int numDroppedBuffers = 0;
54 sp<IProducerListener> listener;
55 {
56 Mutex::Autolock lock(mCore->mMutex);
57
58 // Check that the consumer doesn't currently have the maximum number of
59 // buffers acquired. We allow the max buffer count to be exceeded by one
60 // buffer so that the consumer can successfully set up the newly acquired
61 // buffer before releasing the old one.
62 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080063 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -070064 if (mSlots[s].mBufferState.isAcquired()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070065 ++numAcquiredBuffers;
66 }
Dan Stoza289ade12014-02-28 11:17:17 -080067 }
Lajos Molnar5f920c12015-07-13 16:04:24 -070068 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
69 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
70 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
71 return INVALID_OPERATION;
72 }
Dan Stoza289ade12014-02-28 11:17:17 -080073
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070074 bool sharedBufferAvailable = mCore->mSharedBufferMode &&
75 mCore->mAutoRefresh && mCore->mSharedBufferSlot !=
Pablo Ceballosccdfd602015-10-07 15:05:45 -070076 BufferQueueCore::INVALID_BUFFER_SLOT;
77
Lajos Molnar5f920c12015-07-13 16:04:24 -070078 // In asynchronous mode the list is guaranteed to be one buffer deep,
79 // while in synchronous mode we use the oldest buffer.
Pablo Ceballosccdfd602015-10-07 15:05:45 -070080 if (mCore->mQueue.empty() && !sharedBufferAvailable) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070081 return NO_BUFFER_AVAILABLE;
82 }
Dan Stoza289ade12014-02-28 11:17:17 -080083
Lajos Molnar5f920c12015-07-13 16:04:24 -070084 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
Dan Stoza289ade12014-02-28 11:17:17 -080085
Lajos Molnar5f920c12015-07-13 16:04:24 -070086 // If expectedPresent is specified, we may not want to return a buffer yet.
87 // If it's specified and there's more than one buffer queued, we may want
88 // to drop a buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070089 // Skip this if we're in shared buffer mode and the queue is empty,
Pablo Ceballosccdfd602015-10-07 15:05:45 -070090 // since in that case we'll just return the shared buffer.
91 if (expectedPresent != 0 && !mCore->mQueue.empty()) {
Lajos Molnar5f920c12015-07-13 16:04:24 -070092 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
Dan Stoza289ade12014-02-28 11:17:17 -080093
Lajos Molnar5f920c12015-07-13 16:04:24 -070094 // The 'expectedPresent' argument indicates when the buffer is expected
95 // to be presented on-screen. If the buffer's desired present time is
96 // earlier (less) than expectedPresent -- meaning it will be displayed
97 // on time or possibly late if we show it as soon as possible -- we
98 // acquire and return it. If we don't want to display it until after the
99 // expectedPresent time, we return PRESENT_LATER without acquiring it.
100 //
101 // To be safe, we don't defer acquisition if expectedPresent is more
102 // than one second in the future beyond the desired present time
103 // (i.e., we'd be holding the buffer for a long time).
104 //
105 // NOTE: Code assumes monotonic time values from the system clock
106 // are positive.
Dan Stoza289ade12014-02-28 11:17:17 -0800107
Lajos Molnar5f920c12015-07-13 16:04:24 -0700108 // Start by checking to see if we can drop frames. We skip this check if
109 // the timestamps are being auto-generated by Surface. If the app isn't
110 // generating timestamps explicitly, it probably doesn't want frames to
111 // be discarded based on them.
112 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
113 const BufferItem& bufferItem(mCore->mQueue[1]);
Dan Stozaa4650a52015-05-12 12:56:16 -0700114
Lajos Molnar5f920c12015-07-13 16:04:24 -0700115 // If dropping entry[0] would leave us with a buffer that the
116 // consumer is not yet ready for, don't drop it.
117 if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
118 break;
119 }
120
121 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
122 // additional criterion here: we only drop the earlier buffer if our
123 // desiredPresent falls within +/- 1 second of the expected present.
124 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
125 // relative timestamp), which normally mean "ignore the timestamp
126 // and acquire immediately", would cause us to drop frames.
127 //
128 // We may want to add an additional criterion: don't drop the
129 // earlier buffer if entry[1]'s fence hasn't signaled yet.
130 nsecs_t desiredPresent = bufferItem.mTimestamp;
131 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
132 desiredPresent > expectedPresent) {
133 // This buffer is set to display in the near future, or
134 // desiredPresent is garbage. Either way we don't want to drop
135 // the previous buffer just to get this on the screen sooner.
136 BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
137 PRId64 " (%" PRId64 ") now=%" PRId64,
138 desiredPresent, expectedPresent,
139 desiredPresent - expectedPresent,
140 systemTime(CLOCK_MONOTONIC));
141 break;
142 }
143
144 BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
145 " size=%zu",
146 desiredPresent, expectedPresent, mCore->mQueue.size());
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800147
148 if (!front->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700149 // Front buffer is still in mSlots, so mark the slot as free
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700150 mSlots[front->mSlot].mBufferState.freeQueued();
151
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700152 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700153 // still be around. Mark it as no longer shared if this
154 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700155 if (!mCore->mSharedBufferMode &&
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700156 mSlots[front->mSlot].mBufferState.isFree()) {
157 mSlots[front->mSlot].mBufferState.mShared = false;
158 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800159
160 // Don't put the shared buffer on the free list
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700161 if (!mSlots[front->mSlot].mBufferState.isShared()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800162 mCore->mActiveBuffers.erase(front->mSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700163 mCore->mFreeBuffers.push_back(front->mSlot);
164 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800165
Lajos Molnar5f920c12015-07-13 16:04:24 -0700166 listener = mCore->mConnectedProducerListener;
167 ++numDroppedBuffers;
168 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800169
Lajos Molnar5f920c12015-07-13 16:04:24 -0700170 mCore->mQueue.erase(front);
171 front = mCore->mQueue.begin();
Dan Stozaecc50402015-04-28 14:42:06 -0700172 }
173
Lajos Molnar5f920c12015-07-13 16:04:24 -0700174 // See if the front buffer is ready to be acquired
175 nsecs_t desiredPresent = front->mTimestamp;
176 bool bufferIsDue = desiredPresent <= expectedPresent ||
177 desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
178 bool consumerIsReady = maxFrameNumber > 0 ?
179 front->mFrameNumber <= maxFrameNumber : true;
180 if (!bufferIsDue || !consumerIsReady) {
181 BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
182 " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
183 " consumer=%" PRIu64,
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700184 desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800185 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700186 systemTime(CLOCK_MONOTONIC),
187 front->mFrameNumber, maxFrameNumber);
188 return PRESENT_LATER;
Dan Stoza289ade12014-02-28 11:17:17 -0800189 }
190
Lajos Molnar5f920c12015-07-13 16:04:24 -0700191 BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
192 "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
Dan Stoza289ade12014-02-28 11:17:17 -0800193 desiredPresent - expectedPresent,
Lajos Molnar5f920c12015-07-13 16:04:24 -0700194 systemTime(CLOCK_MONOTONIC));
Dan Stoza289ade12014-02-28 11:17:17 -0800195 }
196
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700197 int slot = BufferQueueCore::INVALID_BUFFER_SLOT;
198
199 if (sharedBufferAvailable && mCore->mQueue.empty()) {
200 // make sure the buffer has finished allocating before acquiring it
201 mCore->waitWhileAllocatingLocked();
202
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700203 slot = mCore->mSharedBufferSlot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700204
205 // Recreate the BufferItem for the shared buffer from the data that
206 // was cached when it was last queued.
207 outBuffer->mGraphicBuffer = mSlots[slot].mGraphicBuffer;
208 outBuffer->mFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700209 outBuffer->mFenceTime = FenceTime::NO_FENCE;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700210 outBuffer->mCrop = mCore->mSharedBufferCache.crop;
211 outBuffer->mTransform = mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700212 ~static_cast<uint32_t>(
213 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700214 outBuffer->mScalingMode = mCore->mSharedBufferCache.scalingMode;
215 outBuffer->mDataSpace = mCore->mSharedBufferCache.dataspace;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700216 outBuffer->mFrameNumber = mCore->mFrameCounter;
217 outBuffer->mSlot = slot;
218 outBuffer->mAcquireCalled = mSlots[slot].mAcquireCalled;
219 outBuffer->mTransformToDisplayInverse =
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700220 (mCore->mSharedBufferCache.transform &
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700221 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
222 outBuffer->mSurfaceDamage = Region::INVALID_REGION;
Pablo Ceballos06312182015-10-07 16:32:12 -0700223 outBuffer->mQueuedBuffer = false;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800224 outBuffer->mIsStale = false;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700225 outBuffer->mAutoRefresh = mCore->mSharedBufferMode &&
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800226 mCore->mAutoRefresh;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700227 } else {
228 slot = front->mSlot;
229 *outBuffer = *front;
230 }
231
Lajos Molnar5f920c12015-07-13 16:04:24 -0700232 ATRACE_BUFFER_INDEX(slot);
233
234 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700235 slot, outBuffer->mFrameNumber, outBuffer->mGraphicBuffer->handle);
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800236
237 if (!outBuffer->mIsStale) {
Lajos Molnar5f920c12015-07-13 16:04:24 -0700238 mSlots[slot].mAcquireCalled = true;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700239 // Don't decrease the queue count if the BufferItem wasn't
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700240 // previously in the queue. This happens in shared buffer mode when
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700241 // the queue is empty and the BufferItem is created above.
242 if (mCore->mQueue.empty()) {
243 mSlots[slot].mBufferState.acquireNotInQueue();
244 } else {
245 mSlots[slot].mBufferState.acquire();
246 }
Lajos Molnar5f920c12015-07-13 16:04:24 -0700247 mSlots[slot].mFence = Fence::NO_FENCE;
248 }
249
250 // If the buffer has previously been acquired by the consumer, set
251 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
252 // on the consumer side
253 if (outBuffer->mAcquireCalled) {
254 outBuffer->mGraphicBuffer = NULL;
255 }
256
257 mCore->mQueue.erase(front);
258
259 // We might have freed a slot while dropping old buffers, or the producer
260 // may be blocked waiting for the number of buffers in the queue to
261 // decrease.
262 mCore->mDequeueCondition.broadcast();
263
Colin Cross6e7e2b42016-09-27 14:08:19 -0700264 ATRACE_INT(mCore->mConsumerName.string(),
265 static_cast<int32_t>(mCore->mQueue.size()));
Dan Stozae77c7662016-05-13 11:37:28 -0700266 mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
Lajos Molnar5f920c12015-07-13 16:04:24 -0700267
Pablo Ceballos9e314332016-01-12 13:49:19 -0800268 VALIDATE_CONSISTENCY();
Dan Stoza289ade12014-02-28 11:17:17 -0800269 }
270
Lajos Molnar5f920c12015-07-13 16:04:24 -0700271 if (listener != NULL) {
272 for (int i = 0; i < numDroppedBuffers; ++i) {
273 listener->onBufferReleased();
274 }
Dan Stoza289ade12014-02-28 11:17:17 -0800275 }
276
Dan Stoza289ade12014-02-28 11:17:17 -0800277 return NO_ERROR;
278}
279
Dan Stoza9f3053d2014-03-06 15:14:33 -0800280status_t BufferQueueConsumer::detachBuffer(int slot) {
281 ATRACE_CALL();
282 ATRACE_BUFFER_INDEX(slot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700283 BQ_LOGV("detachBuffer: slot %d", slot);
Pablo Ceballos38273792016-03-02 01:38:10 +0000284 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800285
Pablo Ceballos38273792016-03-02 01:38:10 +0000286 if (mCore->mIsAbandoned) {
287 BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
288 return NO_INIT;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800289 }
290
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 if (mCore->mSharedBufferMode || slot == mCore->mSharedBufferSlot) {
292 BQ_LOGE("detachBuffer: detachBuffer not allowed in shared buffer mode");
Pablo Ceballos38273792016-03-02 01:38:10 +0000293 return BAD_VALUE;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800294 }
295
Pablo Ceballos38273792016-03-02 01:38:10 +0000296 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
297 BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)",
298 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
299 return BAD_VALUE;
300 } else if (!mSlots[slot].mBufferState.isAcquired()) {
301 BQ_LOGE("detachBuffer: slot %d is not owned by the consumer "
302 "(state = %s)", slot, mSlots[slot].mBufferState.string());
303 return BAD_VALUE;
304 }
305
306 mSlots[slot].mBufferState.detachConsumer();
307 mCore->mActiveBuffers.erase(slot);
308 mCore->mFreeSlots.insert(slot);
309 mCore->clearBufferSlotLocked(slot);
310 mCore->mDequeueCondition.broadcast();
311 VALIDATE_CONSISTENCY();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800312
313 return NO_ERROR;
314}
315
316status_t BufferQueueConsumer::attachBuffer(int* outSlot,
317 const sp<android::GraphicBuffer>& buffer) {
318 ATRACE_CALL();
319
320 if (outSlot == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700321 BQ_LOGE("attachBuffer: outSlot must not be NULL");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800322 return BAD_VALUE;
323 } else if (buffer == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700324 BQ_LOGE("attachBuffer: cannot attach NULL buffer");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800325 return BAD_VALUE;
326 }
327
328 Mutex::Autolock lock(mCore->mMutex);
329
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700330 if (mCore->mSharedBufferMode) {
331 BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700332 return BAD_VALUE;
333 }
334
Dan Stoza0de7ea72015-04-23 13:20:51 -0700335 // Make sure we don't have too many acquired buffers
Dan Stoza9f3053d2014-03-06 15:14:33 -0800336 int numAcquiredBuffers = 0;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800337 for (int s : mCore->mActiveBuffers) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700338 if (mSlots[s].mBufferState.isAcquired()) {
Dan Stoza9f3053d2014-03-06 15:14:33 -0800339 ++numAcquiredBuffers;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800340 }
341 }
342
343 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700344 BQ_LOGE("attachBuffer: max acquired buffer count reached: %d "
Dan Stoza9f3053d2014-03-06 15:14:33 -0800345 "(max %d)", numAcquiredBuffers,
346 mCore->mMaxAcquiredBufferCount);
347 return INVALID_OPERATION;
348 }
Dan Stoza0de7ea72015-04-23 13:20:51 -0700349
Dan Stoza812ed062015-06-02 15:45:22 -0700350 if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
351 BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
352 "[queue %u]", buffer->getGenerationNumber(),
353 mCore->mGenerationNumber);
354 return BAD_VALUE;
355 }
356
Dan Stoza0de7ea72015-04-23 13:20:51 -0700357 // Find a free slot to put the buffer into
358 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
359 if (!mCore->mFreeSlots.empty()) {
360 auto slot = mCore->mFreeSlots.begin();
361 found = *slot;
362 mCore->mFreeSlots.erase(slot);
363 } else if (!mCore->mFreeBuffers.empty()) {
364 found = mCore->mFreeBuffers.front();
365 mCore->mFreeBuffers.remove(found);
366 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800367 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700368 BQ_LOGE("attachBuffer: could not find free buffer slot");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800369 return NO_MEMORY;
370 }
371
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800372 mCore->mActiveBuffers.insert(found);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800373 *outSlot = found;
374 ATRACE_BUFFER_INDEX(*outSlot);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700375 BQ_LOGV("attachBuffer: returning slot %d", *outSlot);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800376
377 mSlots[*outSlot].mGraphicBuffer = buffer;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700378 mSlots[*outSlot].mBufferState.attachConsumer();
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800379 mSlots[*outSlot].mNeedsReallocation = true;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380 mSlots[*outSlot].mFence = Fence::NO_FENCE;
381 mSlots[*outSlot].mFrameNumber = 0;
382
Dan Stoza99b18b42014-03-28 15:34:33 -0700383 // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
384 // GraphicBuffer pointer on the next acquireBuffer call, which decreases
385 // Binder traffic by not un/flattening the GraphicBuffer. However, it
386 // requires that the consumer maintain a cached copy of the slot <--> buffer
387 // mappings, which is why the consumer doesn't need the valid pointer on
388 // acquire.
389 //
390 // The StreamSplitter is one of the primary users of the attach/detach
391 // logic, and while it is running, all buffers it acquires are immediately
392 // detached, and all buffers it eventually releases are ones that were
393 // attached (as opposed to having been obtained from acquireBuffer), so it
394 // doesn't make sense to maintain the slot/buffer mappings, which would
395 // become invalid for every buffer during detach/attach. By setting this to
396 // false, the valid GraphicBuffer pointer will always be sent with acquire
397 // for attached buffers.
398 mSlots[*outSlot].mAcquireCalled = false;
399
Pablo Ceballos9e314332016-01-12 13:49:19 -0800400 VALIDATE_CONSISTENCY();
Dan Stoza0de7ea72015-04-23 13:20:51 -0700401
Dan Stoza9f3053d2014-03-06 15:14:33 -0800402 return NO_ERROR;
403}
404
Dan Stoza289ade12014-02-28 11:17:17 -0800405status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
406 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
407 EGLSyncKHR eglFence) {
408 ATRACE_CALL();
409 ATRACE_BUFFER_INDEX(slot);
410
Dan Stoza9f3053d2014-03-06 15:14:33 -0800411 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
412 releaseFence == NULL) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700413 BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot,
414 releaseFence.get());
Dan Stoza289ade12014-02-28 11:17:17 -0800415 return BAD_VALUE;
416 }
417
Dan Stozad1c10362014-03-28 15:19:08 -0700418 sp<IProducerListener> listener;
419 { // Autolock scope
420 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800421
Dan Stozad1c10362014-03-28 15:19:08 -0700422 // If the frame number has changed because the buffer has been reallocated,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700423 // we can ignore this releaseBuffer for the old buffer.
424 // Ignore this for the shared buffer where the frame number can easily
425 // get out of sync due to the buffer being queued and acquired at the
426 // same time.
427 if (frameNumber != mSlots[slot].mFrameNumber &&
428 !mSlots[slot].mBufferState.isShared()) {
Dan Stozad1c10362014-03-28 15:19:08 -0700429 return STALE_BUFFER_SLOT;
430 }
Dan Stoza289ade12014-02-28 11:17:17 -0800431
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800432 if (!mSlots[slot].mBufferState.isAcquired()) {
Dan Stoza52937cd2015-05-01 16:42:55 -0700433 BQ_LOGE("releaseBuffer: attempted to release buffer slot %d "
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700434 "but its state was %s", slot,
435 mSlots[slot].mBufferState.string());
Dan Stoza9f3053d2014-03-06 15:14:33 -0800436 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800437 }
Dan Stoza289ade12014-02-28 11:17:17 -0800438
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800439 mSlots[slot].mEglDisplay = eglDisplay;
440 mSlots[slot].mEglFence = eglFence;
441 mSlots[slot].mFence = releaseFence;
442 mSlots[slot].mBufferState.release();
443
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700444 // After leaving shared buffer mode, the shared buffer will
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800445 // still be around. Mark it as no longer shared if this
446 // operation causes it to be free.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700447 if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800448 mSlots[slot].mBufferState.mShared = false;
449 }
450 // Don't put the shared buffer on the free list.
451 if (!mSlots[slot].mBufferState.isShared()) {
452 mCore->mActiveBuffers.erase(slot);
453 mCore->mFreeBuffers.push_back(slot);
454 }
455
456 listener = mCore->mConnectedProducerListener;
457 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
458
Dan Stozad1c10362014-03-28 15:19:08 -0700459 mCore->mDequeueCondition.broadcast();
Pablo Ceballos9e314332016-01-12 13:49:19 -0800460 VALIDATE_CONSISTENCY();
Dan Stozad1c10362014-03-28 15:19:08 -0700461 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800462
Dan Stozad1c10362014-03-28 15:19:08 -0700463 // Call back without lock held
464 if (listener != NULL) {
465 listener->onBufferReleased();
466 }
Dan Stoza289ade12014-02-28 11:17:17 -0800467
468 return NO_ERROR;
469}
470
471status_t BufferQueueConsumer::connect(
472 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
473 ATRACE_CALL();
474
475 if (consumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700476 BQ_LOGE("connect: consumerListener may not be NULL");
Dan Stoza289ade12014-02-28 11:17:17 -0800477 return BAD_VALUE;
478 }
479
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700480 BQ_LOGV("connect: controlledByApp=%s",
Dan Stoza289ade12014-02-28 11:17:17 -0800481 controlledByApp ? "true" : "false");
482
483 Mutex::Autolock lock(mCore->mMutex);
484
485 if (mCore->mIsAbandoned) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700486 BQ_LOGE("connect: BufferQueue has been abandoned");
Dan Stoza289ade12014-02-28 11:17:17 -0800487 return NO_INIT;
488 }
489
490 mCore->mConsumerListener = consumerListener;
491 mCore->mConsumerControlledByApp = controlledByApp;
492
493 return NO_ERROR;
494}
495
496status_t BufferQueueConsumer::disconnect() {
497 ATRACE_CALL();
498
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700499 BQ_LOGV("disconnect");
Dan Stoza289ade12014-02-28 11:17:17 -0800500
501 Mutex::Autolock lock(mCore->mMutex);
502
503 if (mCore->mConsumerListener == NULL) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700504 BQ_LOGE("disconnect: no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800505 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800506 }
507
508 mCore->mIsAbandoned = true;
509 mCore->mConsumerListener = NULL;
510 mCore->mQueue.clear();
511 mCore->freeAllBuffersLocked();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700512 mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
Dan Stoza289ade12014-02-28 11:17:17 -0800513 mCore->mDequeueCondition.broadcast();
514 return NO_ERROR;
515}
516
Dan Stozafebd4f42014-04-09 16:14:51 -0700517status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
Dan Stoza289ade12014-02-28 11:17:17 -0800518 ATRACE_CALL();
519
520 if (outSlotMask == NULL) {
521 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
522 return BAD_VALUE;
523 }
524
525 Mutex::Autolock lock(mCore->mMutex);
526
527 if (mCore->mIsAbandoned) {
528 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
529 return NO_INIT;
530 }
531
Dan Stozafebd4f42014-04-09 16:14:51 -0700532 uint64_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800533 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800534 if (!mSlots[s].mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700535 mask |= (1ULL << s);
Dan Stoza289ade12014-02-28 11:17:17 -0800536 }
537 }
538
539 // Remove from the mask queued buffers for which acquire has been called,
540 // since the consumer will not receive their buffer addresses and so must
541 // retain their cached information
542 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
543 while (current != mCore->mQueue.end()) {
544 if (current->mAcquireCalled) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700545 mask &= ~(1ULL << current->mSlot);
Dan Stoza289ade12014-02-28 11:17:17 -0800546 }
547 ++current;
548 }
549
Dan Stozafebd4f42014-04-09 16:14:51 -0700550 BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
Dan Stoza289ade12014-02-28 11:17:17 -0800551 *outSlotMask = mask;
552 return NO_ERROR;
553}
554
555status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
556 uint32_t height) {
557 ATRACE_CALL();
558
559 if (width == 0 || height == 0) {
560 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
561 "height=%u)", width, height);
562 return BAD_VALUE;
563 }
564
565 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
566
567 Mutex::Autolock lock(mCore->mMutex);
568 mCore->mDefaultWidth = width;
569 mCore->mDefaultHeight = height;
570 return NO_ERROR;
571}
572
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700573status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) {
Dan Stoza289ade12014-02-28 11:17:17 -0800574 ATRACE_CALL();
Dan Stoza289ade12014-02-28 11:17:17 -0800575
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700576 if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) {
577 BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount);
578 return BAD_VALUE;
579 }
Dan Stoza289ade12014-02-28 11:17:17 -0800580
Pablo Ceballos38273792016-03-02 01:38:10 +0000581 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800582
Pablo Ceballos38273792016-03-02 01:38:10 +0000583 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
584 BQ_LOGE("setMaxBufferCount: producer is already connected");
585 return INVALID_OPERATION;
Dan Stoza289ade12014-02-28 11:17:17 -0800586 }
587
Pablo Ceballos38273792016-03-02 01:38:10 +0000588 if (bufferCount < mCore->mMaxAcquiredBufferCount) {
589 BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than"
590 "mMaxAcquiredBufferCount (%d)", bufferCount,
591 mCore->mMaxAcquiredBufferCount);
592 return BAD_VALUE;
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700593 }
Pablo Ceballos38273792016-03-02 01:38:10 +0000594
595 int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
596 mCore->mDequeueBufferCannotBlock, bufferCount) -
597 mCore->getMaxBufferCountLocked();
598 if (!mCore->adjustAvailableSlotsLocked(delta)) {
599 BQ_LOGE("setMaxBufferCount: BufferQueue failed to adjust the number of "
600 "available slots. Delta = %d", delta);
601 return BAD_VALUE;
602 }
603
604 mCore->mMaxBufferCount = bufferCount;
Dan Stoza289ade12014-02-28 11:17:17 -0800605 return NO_ERROR;
606}
607
608status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
609 int maxAcquiredBuffers) {
610 ATRACE_CALL();
611
612 if (maxAcquiredBuffers < 1 ||
613 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
614 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
615 maxAcquiredBuffers);
616 return BAD_VALUE;
617 }
618
Pablo Ceballos38273792016-03-02 01:38:10 +0000619 sp<IConsumerListener> listener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800620 { // Autolock scope
621 Mutex::Autolock lock(mCore->mMutex);
622 mCore->waitWhileAllocatingLocked();
Dan Stoza289ade12014-02-28 11:17:17 -0800623
Pablo Ceballos72daab62015-12-07 16:38:43 -0800624 if (mCore->mIsAbandoned) {
625 BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
626 return NO_INIT;
627 }
628
Pablo Ceballos245cc5b2016-04-19 11:33:00 -0700629 if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
630 return NO_ERROR;
631 }
632
Pablo Ceballos72daab62015-12-07 16:38:43 -0800633 // The new maxAcquiredBuffers count should not be violated by the number
634 // of currently acquired buffers
635 int acquiredCount = 0;
636 for (int slot : mCore->mActiveBuffers) {
637 if (mSlots[slot].mBufferState.isAcquired()) {
638 acquiredCount++;
639 }
640 }
641 if (acquiredCount > maxAcquiredBuffers) {
642 BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
643 "count (%d) exceeds the current acquired buffer count (%d)",
644 maxAcquiredBuffers, acquiredCount);
645 return BAD_VALUE;
646 }
647
648 if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
649 (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
650 > mCore->mMaxBufferCount) {
651 BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
652 "exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
653 maxAcquiredBuffers, mCore->mMaxBufferCount,
654 mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
655 mCore->mDequeueBufferCannotBlock);
656 return BAD_VALUE;
657 }
658
659 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
Pablo Ceballos38273792016-03-02 01:38:10 +0000660 if (!mCore->adjustAvailableSlotsLocked(delta)) {
Pablo Ceballos72daab62015-12-07 16:38:43 -0800661 return BAD_VALUE;
662 }
663
664 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
665 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
666 VALIDATE_CONSISTENCY();
667 if (delta < 0) {
Pablo Ceballos38273792016-03-02 01:38:10 +0000668 listener = mCore->mConsumerListener;
Pablo Ceballos72daab62015-12-07 16:38:43 -0800669 }
670 }
671 // Call back without lock held
Pablo Ceballos38273792016-03-02 01:38:10 +0000672 if (listener != NULL) {
673 listener->onBuffersReleased();
Dan Stoza289ade12014-02-28 11:17:17 -0800674 }
675
Dan Stoza289ade12014-02-28 11:17:17 -0800676 return NO_ERROR;
677}
678
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700679status_t BufferQueueConsumer::setConsumerName(const String8& name) {
Dan Stoza289ade12014-02-28 11:17:17 -0800680 ATRACE_CALL();
681 BQ_LOGV("setConsumerName: '%s'", name.string());
682 Mutex::Autolock lock(mCore->mMutex);
683 mCore->mConsumerName = name;
684 mConsumerName = name;
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700685 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800686}
687
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800688status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Dan Stoza289ade12014-02-28 11:17:17 -0800689 ATRACE_CALL();
690 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
691 Mutex::Autolock lock(mCore->mMutex);
692 mCore->mDefaultBufferFormat = defaultFormat;
693 return NO_ERROR;
694}
695
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800696status_t BufferQueueConsumer::setDefaultBufferDataSpace(
697 android_dataspace defaultDataSpace) {
698 ATRACE_CALL();
699 BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
700 Mutex::Autolock lock(mCore->mMutex);
701 mCore->mDefaultBufferDataSpace = defaultDataSpace;
702 return NO_ERROR;
703}
704
Dan Stoza289ade12014-02-28 11:17:17 -0800705status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
706 ATRACE_CALL();
707 BQ_LOGV("setConsumerUsageBits: %#x", usage);
708 Mutex::Autolock lock(mCore->mMutex);
709 mCore->mConsumerUsageBits = usage;
710 return NO_ERROR;
711}
712
Jiwen 'Steve' Cai20419132017-04-21 18:49:53 -0700713status_t BufferQueueConsumer::setConsumerIsProtected(bool isProtected) {
714 ATRACE_CALL();
715 BQ_LOGV("setConsumerIsProtected: %s", isProtected ? "true" : "false");
716 Mutex::Autolock lock(mCore->mMutex);
717 mCore->mConsumerIsProtected = isProtected;
718 return NO_ERROR;
719}
720
Dan Stoza289ade12014-02-28 11:17:17 -0800721status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
722 ATRACE_CALL();
723 BQ_LOGV("setTransformHint: %#x", hint);
724 Mutex::Autolock lock(mCore->mMutex);
725 mCore->mTransformHint = hint;
726 return NO_ERROR;
727}
728
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700729status_t BufferQueueConsumer::getSidebandStream(sp<NativeHandle>* outStream) const {
Fabien Sanglard2d8a2432016-11-08 15:31:32 -0800730 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700731 *outStream = mCore->mSidebandStream;
732 return NO_ERROR;
Jesse Hall399184a2014-03-03 15:42:54 -0800733}
734
Dan Stozae77c7662016-05-13 11:37:28 -0700735status_t BufferQueueConsumer::getOccupancyHistory(bool forceFlush,
736 std::vector<OccupancyTracker::Segment>* outHistory) {
737 Mutex::Autolock lock(mCore->mMutex);
738 *outHistory = mCore->mOccupancyTracker.getSegmentHistory(forceFlush);
739 return NO_ERROR;
740}
741
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700742status_t BufferQueueConsumer::discardFreeBuffers() {
743 Mutex::Autolock lock(mCore->mMutex);
744 mCore->discardFreeBuffersLocked();
745 return NO_ERROR;
746}
747
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700748status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResult) const {
Yifan Hongfc8d8852017-07-26 10:47:14 -0700749 struct passwd* pwd = getpwnam("shell");
750 uid_t shellUid = pwd ? pwd->pw_uid : 0;
751 if (!shellUid) {
Yifan Hong84548cf2017-08-03 12:35:02 -0700752 int savedErrno = errno;
Yifan Hongfc8d8852017-07-26 10:47:14 -0700753 BQ_LOGE("Cannot get AID_SHELL");
Yifan Hong84548cf2017-08-03 12:35:02 -0700754 return savedErrno ? -savedErrno : UNKNOWN_ERROR;
Yifan Hongfc8d8852017-07-26 10:47:14 -0700755 }
756
Pablo Ceballos88f69282016-02-11 18:01:49 -0800757 const IPCThreadState* ipc = IPCThreadState::self();
758 const pid_t pid = ipc->getCallingPid();
759 const uid_t uid = ipc->getCallingUid();
Yifan Hongfc8d8852017-07-26 10:47:14 -0700760 if ((uid != shellUid) &&
761 !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700762 outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
Pablo Ceballos88f69282016-02-11 18:01:49 -0800763 "from pid=%d, uid=%d\n", pid, uid);
Colin Cross6e7e2b42016-09-27 14:08:19 -0700764 android_errorWriteWithInfoLog(0x534e4554, "27046057",
765 static_cast<int32_t>(uid), NULL, 0);
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700766 return PERMISSION_DENIED;
Pablo Ceballos88f69282016-02-11 18:01:49 -0800767 }
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700768
769 mCore->dumpState(prefix, outResult);
770 return NO_ERROR;
Dan Stoza289ade12014-02-28 11:17:17 -0800771}
772
773} // namespace android