blob: 872eae6d77a5434d7e1c96930f018f83dbf718e2 [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
Dan Stoza3e96f192014-03-03 10:16:19 -080017#define LOG_TAG "BufferQueueConsumer"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Dan Stoza289ade12014-02-28 11:17:17 -080021#include <gui/BufferItem.h>
22#include <gui/BufferQueueConsumer.h>
23#include <gui/BufferQueueCore.h>
24#include <gui/IConsumerListener.h>
Dan Stozad1c10362014-03-28 15:19:08 -070025#include <gui/IProducerListener.h>
Dan Stoza289ade12014-02-28 11:17:17 -080026
27namespace android {
28
29BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
30 mCore(core),
31 mSlots(core->mSlots),
32 mConsumerName() {}
33
34BufferQueueConsumer::~BufferQueueConsumer() {}
35
36status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
37 nsecs_t expectedPresent) {
38 ATRACE_CALL();
39 Mutex::Autolock lock(mCore->mMutex);
40
41 // Check that the consumer doesn't currently have the maximum number of
42 // buffers acquired. We allow the max buffer count to be exceeded by one
43 // buffer so that the consumer can successfully set up the newly acquired
44 // buffer before releasing the old one.
45 int numAcquiredBuffers = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -080046 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -080047 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
48 ++numAcquiredBuffers;
49 }
50 }
51 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
52 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
53 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
54 return INVALID_OPERATION;
55 }
56
57 // Check if the queue is empty.
58 // In asynchronous mode the list is guaranteed to be one buffer deep,
59 // while in synchronous mode we use the oldest buffer.
60 if (mCore->mQueue.empty()) {
61 return NO_BUFFER_AVAILABLE;
62 }
63
64 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
65
66 // If expectedPresent is specified, we may not want to return a buffer yet.
67 // If it's specified and there's more than one buffer queued, we may want
68 // to drop a buffer.
69 if (expectedPresent != 0) {
70 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
71
72 // The 'expectedPresent' argument indicates when the buffer is expected
73 // to be presented on-screen. If the buffer's desired present time is
74 // earlier (less) than expectedPresent -- meaning it will be displayed
75 // on time or possibly late if we show it as soon as possible -- we
76 // acquire and return it. If we don't want to display it until after the
77 // expectedPresent time, we return PRESENT_LATER without acquiring it.
78 //
79 // To be safe, we don't defer acquisition if expectedPresent is more
80 // than one second in the future beyond the desired present time
81 // (i.e., we'd be holding the buffer for a long time).
82 //
83 // NOTE: Code assumes monotonic time values from the system clock
84 // are positive.
85
86 // Start by checking to see if we can drop frames. We skip this check if
87 // the timestamps are being auto-generated by Surface. If the app isn't
88 // generating timestamps explicitly, it probably doesn't want frames to
89 // be discarded based on them.
90 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
91 // If entry[1] is timely, drop entry[0] (and repeat). We apply an
92 // additional criterion here: we only drop the earlier buffer if our
93 // desiredPresent falls within +/- 1 second of the expected present.
94 // Otherwise, bogus desiredPresent times (e.g., 0 or a small
95 // relative timestamp), which normally mean "ignore the timestamp
96 // and acquire immediately", would cause us to drop frames.
97 //
98 // We may want to add an additional criterion: don't drop the
99 // earlier buffer if entry[1]'s fence hasn't signaled yet.
100 const BufferItem& bufferItem(mCore->mQueue[1]);
101 nsecs_t desiredPresent = bufferItem.mTimestamp;
102 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
103 desiredPresent > expectedPresent) {
104 // This buffer is set to display in the near future, or
105 // desiredPresent is garbage. Either way we don't want to drop
106 // the previous buffer just to get this on the screen sooner.
107 BQ_LOGV("acquireBuffer: nodrop desire=%lld expect=%lld "
108 "(%lld) now=%lld", desiredPresent, expectedPresent,
109 desiredPresent - expectedPresent,
110 systemTime(CLOCK_MONOTONIC));
111 break;
112 }
113
114 BQ_LOGV("acquireBuffer: drop desire=%lld expect=%lld size=%d",
115 desiredPresent, expectedPresent, mCore->mQueue.size());
116 if (mCore->stillTracking(front)) {
117 // Front buffer is still in mSlots, so mark the slot as free
118 mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
119 }
120 mCore->mQueue.erase(front);
121 front = mCore->mQueue.begin();
122 }
123
124 // See if the front buffer is due
125 nsecs_t desiredPresent = front->mTimestamp;
126 if (desiredPresent > expectedPresent &&
127 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
128 BQ_LOGV("acquireBuffer: defer desire=%lld expect=%lld "
129 "(%lld) now=%lld", desiredPresent, expectedPresent,
130 desiredPresent - expectedPresent,
131 systemTime(CLOCK_MONOTONIC));
132 return PRESENT_LATER;
133 }
134
135 BQ_LOGV("acquireBuffer: accept desire=%lld expect=%lld "
136 "(%lld) now=%lld", desiredPresent, expectedPresent,
137 desiredPresent - expectedPresent,
138 systemTime(CLOCK_MONOTONIC));
139 }
140
141 int slot = front->mSlot;
142 *outBuffer = *front;
143 ATRACE_BUFFER_INDEX(slot);
144
145 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%llu buffer=%p }",
146 slot, front->mFrameNumber, front->mGraphicBuffer->handle);
147 // If the front buffer is still being tracked, update its slot state
148 if (mCore->stillTracking(front)) {
149 mSlots[slot].mAcquireCalled = true;
150 mSlots[slot].mNeedsCleanupOnRelease = false;
151 mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
152 mSlots[slot].mFence = Fence::NO_FENCE;
153 }
154
155 // If the buffer has previously been acquired by the consumer, set
156 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
157 // on the consumer side
158 if (outBuffer->mAcquireCalled) {
159 outBuffer->mGraphicBuffer = NULL;
160 }
161
162 mCore->mQueue.erase(front);
Dan Stozaae3c3682014-04-18 15:43:35 -0700163
164 // We might have freed a slot while dropping old buffers, or the producer
165 // may be blocked waiting for the number of buffers in the queue to
166 // decrease.
Dan Stoza289ade12014-02-28 11:17:17 -0800167 mCore->mDequeueCondition.broadcast();
168
169 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
170
171 return NO_ERROR;
172}
173
Dan Stoza9f3053d2014-03-06 15:14:33 -0800174status_t BufferQueueConsumer::detachBuffer(int slot) {
175 ATRACE_CALL();
176 ATRACE_BUFFER_INDEX(slot);
177 BQ_LOGV("detachBuffer(C): slot %d", slot);
178 Mutex::Autolock lock(mCore->mMutex);
179
180 if (mCore->mIsAbandoned) {
181 BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned");
182 return NO_INIT;
183 }
184
185 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
186 BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)",
187 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
188 return BAD_VALUE;
189 } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) {
190 BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer "
191 "(state = %d)", slot, mSlots[slot].mBufferState);
192 return BAD_VALUE;
193 }
194
195 mCore->freeBufferLocked(slot);
196 mCore->mDequeueCondition.broadcast();
197
198 return NO_ERROR;
199}
200
201status_t BufferQueueConsumer::attachBuffer(int* outSlot,
202 const sp<android::GraphicBuffer>& buffer) {
203 ATRACE_CALL();
204
205 if (outSlot == NULL) {
206 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
207 return BAD_VALUE;
208 } else if (buffer == NULL) {
209 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
210 return BAD_VALUE;
211 }
212
213 Mutex::Autolock lock(mCore->mMutex);
214
215 // Make sure we don't have too many acquired buffers and find a free slot
216 // to put the buffer into (the oldest if there are multiple).
217 int numAcquiredBuffers = 0;
218 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
219 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
220 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
221 ++numAcquiredBuffers;
222 } else if (mSlots[s].mBufferState == BufferSlot::FREE) {
223 if (found == BufferQueueCore::INVALID_BUFFER_SLOT ||
224 mSlots[s].mFrameNumber < mSlots[found].mFrameNumber) {
225 found = s;
226 }
227 }
228 }
229
230 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
231 BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d "
232 "(max %d)", numAcquiredBuffers,
233 mCore->mMaxAcquiredBufferCount);
234 return INVALID_OPERATION;
235 }
236 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
237 BQ_LOGE("attachBuffer(P): could not find free buffer slot");
238 return NO_MEMORY;
239 }
240
241 *outSlot = found;
242 ATRACE_BUFFER_INDEX(*outSlot);
243 BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot);
244
245 mSlots[*outSlot].mGraphicBuffer = buffer;
246 mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
247 mSlots[*outSlot].mAttachedByConsumer = true;
248 mSlots[*outSlot].mAcquireCalled = true;
249 mSlots[*outSlot].mNeedsCleanupOnRelease = false;
250 mSlots[*outSlot].mFence = Fence::NO_FENCE;
251 mSlots[*outSlot].mFrameNumber = 0;
252
253 return NO_ERROR;
254}
255
Dan Stoza289ade12014-02-28 11:17:17 -0800256status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
257 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
258 EGLSyncKHR eglFence) {
259 ATRACE_CALL();
260 ATRACE_BUFFER_INDEX(slot);
261
Dan Stoza9f3053d2014-03-06 15:14:33 -0800262 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
263 releaseFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800264 return BAD_VALUE;
265 }
266
Dan Stozad1c10362014-03-28 15:19:08 -0700267 sp<IProducerListener> listener;
268 { // Autolock scope
269 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800270
Dan Stozad1c10362014-03-28 15:19:08 -0700271 // If the frame number has changed because the buffer has been reallocated,
272 // we can ignore this releaseBuffer for the old buffer
273 if (frameNumber != mSlots[slot].mFrameNumber) {
274 return STALE_BUFFER_SLOT;
275 }
Dan Stoza289ade12014-02-28 11:17:17 -0800276
Dan Stozad1c10362014-03-28 15:19:08 -0700277 // Make sure this buffer hasn't been queued while acquired by the consumer
278 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
279 while (current != mCore->mQueue.end()) {
280 if (current->mSlot == slot) {
281 BQ_LOGE("releaseBuffer: buffer slot %d pending release is "
282 "currently queued", slot);
283 return BAD_VALUE;
284 }
285 ++current;
286 }
287
288 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
289 mSlots[slot].mEglDisplay = eglDisplay;
290 mSlots[slot].mEglFence = eglFence;
291 mSlots[slot].mFence = releaseFence;
292 mSlots[slot].mBufferState = BufferSlot::FREE;
293 listener = mCore->mConnectedProducerListener;
294 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
295 } else if (mSlots[slot].mNeedsCleanupOnRelease) {
296 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
297 "(state = %d)", slot, mSlots[slot].mBufferState);
298 mSlots[slot].mNeedsCleanupOnRelease = false;
299 return STALE_BUFFER_SLOT;
300 } else {
301 BQ_LOGV("releaseBuffer: attempted to release buffer slot %d "
302 "but its state was %d", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800303 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800304 }
Dan Stoza289ade12014-02-28 11:17:17 -0800305
Dan Stozad1c10362014-03-28 15:19:08 -0700306 mCore->mDequeueCondition.broadcast();
307 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800308
Dan Stozad1c10362014-03-28 15:19:08 -0700309 // Call back without lock held
310 if (listener != NULL) {
311 listener->onBufferReleased();
312 }
Dan Stoza289ade12014-02-28 11:17:17 -0800313
314 return NO_ERROR;
315}
316
317status_t BufferQueueConsumer::connect(
318 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
319 ATRACE_CALL();
320
321 if (consumerListener == NULL) {
322 BQ_LOGE("connect(C): consumerListener may not be NULL");
323 return BAD_VALUE;
324 }
325
326 BQ_LOGV("connect(C): controlledByApp=%s",
327 controlledByApp ? "true" : "false");
328
329 Mutex::Autolock lock(mCore->mMutex);
330
331 if (mCore->mIsAbandoned) {
332 BQ_LOGE("connect(C): BufferQueue has been abandoned");
333 return NO_INIT;
334 }
335
336 mCore->mConsumerListener = consumerListener;
337 mCore->mConsumerControlledByApp = controlledByApp;
338
339 return NO_ERROR;
340}
341
342status_t BufferQueueConsumer::disconnect() {
343 ATRACE_CALL();
344
345 BQ_LOGV("disconnect(C)");
346
347 Mutex::Autolock lock(mCore->mMutex);
348
349 if (mCore->mConsumerListener == NULL) {
350 BQ_LOGE("disconnect(C): no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800351 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800352 }
353
354 mCore->mIsAbandoned = true;
355 mCore->mConsumerListener = NULL;
356 mCore->mQueue.clear();
357 mCore->freeAllBuffersLocked();
358 mCore->mDequeueCondition.broadcast();
359 return NO_ERROR;
360}
361
362status_t BufferQueueConsumer::getReleasedBuffers(uint32_t *outSlotMask) {
363 ATRACE_CALL();
364
365 if (outSlotMask == NULL) {
366 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
367 return BAD_VALUE;
368 }
369
370 Mutex::Autolock lock(mCore->mMutex);
371
372 if (mCore->mIsAbandoned) {
373 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
374 return NO_INIT;
375 }
376
377 uint32_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800378 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800379 if (!mSlots[s].mAcquireCalled) {
380 mask |= (1u << s);
381 }
382 }
383
384 // Remove from the mask queued buffers for which acquire has been called,
385 // since the consumer will not receive their buffer addresses and so must
386 // retain their cached information
387 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
388 while (current != mCore->mQueue.end()) {
389 if (current->mAcquireCalled) {
390 mask &= ~(1u << current->mSlot);
391 }
392 ++current;
393 }
394
395 BQ_LOGV("getReleasedBuffers: returning mask %#x", mask);
396 *outSlotMask = mask;
397 return NO_ERROR;
398}
399
400status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
401 uint32_t height) {
402 ATRACE_CALL();
403
404 if (width == 0 || height == 0) {
405 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
406 "height=%u)", width, height);
407 return BAD_VALUE;
408 }
409
410 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
411
412 Mutex::Autolock lock(mCore->mMutex);
413 mCore->mDefaultWidth = width;
414 mCore->mDefaultHeight = height;
415 return NO_ERROR;
416}
417
418status_t BufferQueueConsumer::setDefaultMaxBufferCount(int bufferCount) {
419 ATRACE_CALL();
420 Mutex::Autolock lock(mCore->mMutex);
421 return mCore->setDefaultMaxBufferCountLocked(bufferCount);
422}
423
424status_t BufferQueueConsumer::disableAsyncBuffer() {
425 ATRACE_CALL();
426
427 Mutex::Autolock lock(mCore->mMutex);
428
429 if (mCore->mConsumerListener != NULL) {
430 BQ_LOGE("disableAsyncBuffer: consumer already connected");
431 return INVALID_OPERATION;
432 }
433
434 BQ_LOGV("disableAsyncBuffer");
435 mCore->mUseAsyncBuffer = false;
436 return NO_ERROR;
437}
438
439status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
440 int maxAcquiredBuffers) {
441 ATRACE_CALL();
442
443 if (maxAcquiredBuffers < 1 ||
444 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
445 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
446 maxAcquiredBuffers);
447 return BAD_VALUE;
448 }
449
450 Mutex::Autolock lock(mCore->mMutex);
451
452 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
453 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
454 return INVALID_OPERATION;
455 }
456
457 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
458 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
459 return NO_ERROR;
460}
461
462void BufferQueueConsumer::setConsumerName(const String8& name) {
463 ATRACE_CALL();
464 BQ_LOGV("setConsumerName: '%s'", name.string());
465 Mutex::Autolock lock(mCore->mMutex);
466 mCore->mConsumerName = name;
467 mConsumerName = name;
468}
469
470status_t BufferQueueConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
471 ATRACE_CALL();
472 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
473 Mutex::Autolock lock(mCore->mMutex);
474 mCore->mDefaultBufferFormat = defaultFormat;
475 return NO_ERROR;
476}
477
478status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
479 ATRACE_CALL();
480 BQ_LOGV("setConsumerUsageBits: %#x", usage);
481 Mutex::Autolock lock(mCore->mMutex);
482 mCore->mConsumerUsageBits = usage;
483 return NO_ERROR;
484}
485
486status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
487 ATRACE_CALL();
488 BQ_LOGV("setTransformHint: %#x", hint);
489 Mutex::Autolock lock(mCore->mMutex);
490 mCore->mTransformHint = hint;
491 return NO_ERROR;
492}
493
Jesse Hall399184a2014-03-03 15:42:54 -0800494sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
495 return mCore->mSidebandStream;
496}
497
Dan Stoza289ade12014-02-28 11:17:17 -0800498void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
499 mCore->dump(result, prefix);
500}
501
502} // namespace android