blob: 995ed5e817a0e0c7a3ad3bc163674a7b9a3dedbd [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);
163 // TODO: Should this call be after we free a slot while dropping buffers?
164 // Simply acquiring the next buffer doesn't enable a producer to dequeue.
165 mCore->mDequeueCondition.broadcast();
166
167 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
168
169 return NO_ERROR;
170}
171
Dan Stoza9f3053d2014-03-06 15:14:33 -0800172status_t BufferQueueConsumer::detachBuffer(int slot) {
173 ATRACE_CALL();
174 ATRACE_BUFFER_INDEX(slot);
175 BQ_LOGV("detachBuffer(C): slot %d", slot);
176 Mutex::Autolock lock(mCore->mMutex);
177
178 if (mCore->mIsAbandoned) {
179 BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned");
180 return NO_INIT;
181 }
182
183 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
184 BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)",
185 slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
186 return BAD_VALUE;
187 } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) {
188 BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer "
189 "(state = %d)", slot, mSlots[slot].mBufferState);
190 return BAD_VALUE;
191 }
192
193 mCore->freeBufferLocked(slot);
194 mCore->mDequeueCondition.broadcast();
195
196 return NO_ERROR;
197}
198
199status_t BufferQueueConsumer::attachBuffer(int* outSlot,
200 const sp<android::GraphicBuffer>& buffer) {
201 ATRACE_CALL();
202
203 if (outSlot == NULL) {
204 BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
205 return BAD_VALUE;
206 } else if (buffer == NULL) {
207 BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
208 return BAD_VALUE;
209 }
210
211 Mutex::Autolock lock(mCore->mMutex);
212
213 // Make sure we don't have too many acquired buffers and find a free slot
214 // to put the buffer into (the oldest if there are multiple).
215 int numAcquiredBuffers = 0;
216 int found = BufferQueueCore::INVALID_BUFFER_SLOT;
217 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
218 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
219 ++numAcquiredBuffers;
220 } else if (mSlots[s].mBufferState == BufferSlot::FREE) {
221 if (found == BufferQueueCore::INVALID_BUFFER_SLOT ||
222 mSlots[s].mFrameNumber < mSlots[found].mFrameNumber) {
223 found = s;
224 }
225 }
226 }
227
228 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
229 BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d "
230 "(max %d)", numAcquiredBuffers,
231 mCore->mMaxAcquiredBufferCount);
232 return INVALID_OPERATION;
233 }
234 if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
235 BQ_LOGE("attachBuffer(P): could not find free buffer slot");
236 return NO_MEMORY;
237 }
238
239 *outSlot = found;
240 ATRACE_BUFFER_INDEX(*outSlot);
241 BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot);
242
243 mSlots[*outSlot].mGraphicBuffer = buffer;
244 mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
245 mSlots[*outSlot].mAttachedByConsumer = true;
246 mSlots[*outSlot].mAcquireCalled = true;
247 mSlots[*outSlot].mNeedsCleanupOnRelease = false;
248 mSlots[*outSlot].mFence = Fence::NO_FENCE;
249 mSlots[*outSlot].mFrameNumber = 0;
250
251 return NO_ERROR;
252}
253
Dan Stoza289ade12014-02-28 11:17:17 -0800254status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
255 const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
256 EGLSyncKHR eglFence) {
257 ATRACE_CALL();
258 ATRACE_BUFFER_INDEX(slot);
259
Dan Stoza9f3053d2014-03-06 15:14:33 -0800260 if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
261 releaseFence == NULL) {
Dan Stoza289ade12014-02-28 11:17:17 -0800262 return BAD_VALUE;
263 }
264
Dan Stozad1c10362014-03-28 15:19:08 -0700265 sp<IProducerListener> listener;
266 { // Autolock scope
267 Mutex::Autolock lock(mCore->mMutex);
Dan Stoza289ade12014-02-28 11:17:17 -0800268
Dan Stozad1c10362014-03-28 15:19:08 -0700269 // If the frame number has changed because the buffer has been reallocated,
270 // we can ignore this releaseBuffer for the old buffer
271 if (frameNumber != mSlots[slot].mFrameNumber) {
272 return STALE_BUFFER_SLOT;
273 }
Dan Stoza289ade12014-02-28 11:17:17 -0800274
Dan Stozad1c10362014-03-28 15:19:08 -0700275 // Make sure this buffer hasn't been queued while acquired by the consumer
276 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
277 while (current != mCore->mQueue.end()) {
278 if (current->mSlot == slot) {
279 BQ_LOGE("releaseBuffer: buffer slot %d pending release is "
280 "currently queued", slot);
281 return BAD_VALUE;
282 }
283 ++current;
284 }
285
286 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
287 mSlots[slot].mEglDisplay = eglDisplay;
288 mSlots[slot].mEglFence = eglFence;
289 mSlots[slot].mFence = releaseFence;
290 mSlots[slot].mBufferState = BufferSlot::FREE;
291 listener = mCore->mConnectedProducerListener;
292 BQ_LOGV("releaseBuffer: releasing slot %d", slot);
293 } else if (mSlots[slot].mNeedsCleanupOnRelease) {
294 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
295 "(state = %d)", slot, mSlots[slot].mBufferState);
296 mSlots[slot].mNeedsCleanupOnRelease = false;
297 return STALE_BUFFER_SLOT;
298 } else {
299 BQ_LOGV("releaseBuffer: attempted to release buffer slot %d "
300 "but its state was %d", slot, mSlots[slot].mBufferState);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800301 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800302 }
Dan Stoza289ade12014-02-28 11:17:17 -0800303
Dan Stozad1c10362014-03-28 15:19:08 -0700304 mCore->mDequeueCondition.broadcast();
305 } // Autolock scope
Dan Stoza289ade12014-02-28 11:17:17 -0800306
Dan Stozad1c10362014-03-28 15:19:08 -0700307 // Call back without lock held
308 if (listener != NULL) {
309 listener->onBufferReleased();
310 }
Dan Stoza289ade12014-02-28 11:17:17 -0800311
312 return NO_ERROR;
313}
314
315status_t BufferQueueConsumer::connect(
316 const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
317 ATRACE_CALL();
318
319 if (consumerListener == NULL) {
320 BQ_LOGE("connect(C): consumerListener may not be NULL");
321 return BAD_VALUE;
322 }
323
324 BQ_LOGV("connect(C): controlledByApp=%s",
325 controlledByApp ? "true" : "false");
326
327 Mutex::Autolock lock(mCore->mMutex);
328
329 if (mCore->mIsAbandoned) {
330 BQ_LOGE("connect(C): BufferQueue has been abandoned");
331 return NO_INIT;
332 }
333
334 mCore->mConsumerListener = consumerListener;
335 mCore->mConsumerControlledByApp = controlledByApp;
336
337 return NO_ERROR;
338}
339
340status_t BufferQueueConsumer::disconnect() {
341 ATRACE_CALL();
342
343 BQ_LOGV("disconnect(C)");
344
345 Mutex::Autolock lock(mCore->mMutex);
346
347 if (mCore->mConsumerListener == NULL) {
348 BQ_LOGE("disconnect(C): no consumer is connected");
Dan Stoza9f3053d2014-03-06 15:14:33 -0800349 return BAD_VALUE;
Dan Stoza289ade12014-02-28 11:17:17 -0800350 }
351
352 mCore->mIsAbandoned = true;
353 mCore->mConsumerListener = NULL;
354 mCore->mQueue.clear();
355 mCore->freeAllBuffersLocked();
356 mCore->mDequeueCondition.broadcast();
357 return NO_ERROR;
358}
359
360status_t BufferQueueConsumer::getReleasedBuffers(uint32_t *outSlotMask) {
361 ATRACE_CALL();
362
363 if (outSlotMask == NULL) {
364 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
365 return BAD_VALUE;
366 }
367
368 Mutex::Autolock lock(mCore->mMutex);
369
370 if (mCore->mIsAbandoned) {
371 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
372 return NO_INIT;
373 }
374
375 uint32_t mask = 0;
Dan Stoza3e96f192014-03-03 10:16:19 -0800376 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
Dan Stoza289ade12014-02-28 11:17:17 -0800377 if (!mSlots[s].mAcquireCalled) {
378 mask |= (1u << s);
379 }
380 }
381
382 // Remove from the mask queued buffers for which acquire has been called,
383 // since the consumer will not receive their buffer addresses and so must
384 // retain their cached information
385 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
386 while (current != mCore->mQueue.end()) {
387 if (current->mAcquireCalled) {
388 mask &= ~(1u << current->mSlot);
389 }
390 ++current;
391 }
392
393 BQ_LOGV("getReleasedBuffers: returning mask %#x", mask);
394 *outSlotMask = mask;
395 return NO_ERROR;
396}
397
398status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
399 uint32_t height) {
400 ATRACE_CALL();
401
402 if (width == 0 || height == 0) {
403 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
404 "height=%u)", width, height);
405 return BAD_VALUE;
406 }
407
408 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
409
410 Mutex::Autolock lock(mCore->mMutex);
411 mCore->mDefaultWidth = width;
412 mCore->mDefaultHeight = height;
413 return NO_ERROR;
414}
415
416status_t BufferQueueConsumer::setDefaultMaxBufferCount(int bufferCount) {
417 ATRACE_CALL();
418 Mutex::Autolock lock(mCore->mMutex);
419 return mCore->setDefaultMaxBufferCountLocked(bufferCount);
420}
421
422status_t BufferQueueConsumer::disableAsyncBuffer() {
423 ATRACE_CALL();
424
425 Mutex::Autolock lock(mCore->mMutex);
426
427 if (mCore->mConsumerListener != NULL) {
428 BQ_LOGE("disableAsyncBuffer: consumer already connected");
429 return INVALID_OPERATION;
430 }
431
432 BQ_LOGV("disableAsyncBuffer");
433 mCore->mUseAsyncBuffer = false;
434 return NO_ERROR;
435}
436
437status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
438 int maxAcquiredBuffers) {
439 ATRACE_CALL();
440
441 if (maxAcquiredBuffers < 1 ||
442 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
443 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
444 maxAcquiredBuffers);
445 return BAD_VALUE;
446 }
447
448 Mutex::Autolock lock(mCore->mMutex);
449
450 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
451 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
452 return INVALID_OPERATION;
453 }
454
455 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
456 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
457 return NO_ERROR;
458}
459
460void BufferQueueConsumer::setConsumerName(const String8& name) {
461 ATRACE_CALL();
462 BQ_LOGV("setConsumerName: '%s'", name.string());
463 Mutex::Autolock lock(mCore->mMutex);
464 mCore->mConsumerName = name;
465 mConsumerName = name;
466}
467
468status_t BufferQueueConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
469 ATRACE_CALL();
470 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
471 Mutex::Autolock lock(mCore->mMutex);
472 mCore->mDefaultBufferFormat = defaultFormat;
473 return NO_ERROR;
474}
475
476status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
477 ATRACE_CALL();
478 BQ_LOGV("setConsumerUsageBits: %#x", usage);
479 Mutex::Autolock lock(mCore->mMutex);
480 mCore->mConsumerUsageBits = usage;
481 return NO_ERROR;
482}
483
484status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
485 ATRACE_CALL();
486 BQ_LOGV("setTransformHint: %#x", hint);
487 Mutex::Autolock lock(mCore->mMutex);
488 mCore->mTransformHint = hint;
489 return NO_ERROR;
490}
491
Jesse Hall399184a2014-03-03 15:42:54 -0800492sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
493 return mCore->mSidebandStream;
494}
495
Dan Stoza289ade12014-02-28 11:17:17 -0800496void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
497 mCore->dump(result, prefix);
498}
499
500} // namespace android