BufferQueueConsumer: signal onFrameReleased on dropped frames
Bug: 22552826 Change-Id: I9bdfeb8c68f403301af90d4b494f0ae7166a767c
This commit is contained in:
parent
c968c0175e
commit
5f920c1a2c
@ -38,156 +38,169 @@ BufferQueueConsumer::~BufferQueueConsumer() {}
|
|||||||
status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
|
status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
|
||||||
nsecs_t expectedPresent, uint64_t maxFrameNumber) {
|
nsecs_t expectedPresent, uint64_t maxFrameNumber) {
|
||||||
ATRACE_CALL();
|
ATRACE_CALL();
|
||||||
Mutex::Autolock lock(mCore->mMutex);
|
|
||||||
|
|
||||||
// Check that the consumer doesn't currently have the maximum number of
|
int numDroppedBuffers = 0;
|
||||||
// buffers acquired. We allow the max buffer count to be exceeded by one
|
sp<IProducerListener> listener;
|
||||||
// buffer so that the consumer can successfully set up the newly acquired
|
{
|
||||||
// buffer before releasing the old one.
|
Mutex::Autolock lock(mCore->mMutex);
|
||||||
int numAcquiredBuffers = 0;
|
|
||||||
for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
|
// Check that the consumer doesn't currently have the maximum number of
|
||||||
if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
|
// buffers acquired. We allow the max buffer count to be exceeded by one
|
||||||
++numAcquiredBuffers;
|
// buffer so that the consumer can successfully set up the newly acquired
|
||||||
|
// buffer before releasing the old one.
|
||||||
|
int numAcquiredBuffers = 0;
|
||||||
|
for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
|
||||||
|
if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
|
||||||
|
++numAcquiredBuffers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
|
||||||
|
BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
|
||||||
|
numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
|
||||||
|
return INVALID_OPERATION;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
|
|
||||||
BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
|
|
||||||
numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
|
|
||||||
return INVALID_OPERATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the queue is empty.
|
// Check if the queue is empty.
|
||||||
// In asynchronous mode the list is guaranteed to be one buffer deep,
|
// In asynchronous mode the list is guaranteed to be one buffer deep,
|
||||||
// while in synchronous mode we use the oldest buffer.
|
// while in synchronous mode we use the oldest buffer.
|
||||||
if (mCore->mQueue.empty()) {
|
if (mCore->mQueue.empty()) {
|
||||||
return NO_BUFFER_AVAILABLE;
|
return NO_BUFFER_AVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
|
BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
|
||||||
|
|
||||||
// If expectedPresent is specified, we may not want to return a buffer yet.
|
// If expectedPresent is specified, we may not want to return a buffer yet.
|
||||||
// If it's specified and there's more than one buffer queued, we may want
|
// If it's specified and there's more than one buffer queued, we may want
|
||||||
// to drop a buffer.
|
// to drop a buffer.
|
||||||
if (expectedPresent != 0) {
|
if (expectedPresent != 0) {
|
||||||
const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
|
const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
|
||||||
|
|
||||||
// The 'expectedPresent' argument indicates when the buffer is expected
|
// The 'expectedPresent' argument indicates when the buffer is expected
|
||||||
// to be presented on-screen. If the buffer's desired present time is
|
// to be presented on-screen. If the buffer's desired present time is
|
||||||
// earlier (less) than expectedPresent -- meaning it will be displayed
|
// earlier (less) than expectedPresent -- meaning it will be displayed
|
||||||
// on time or possibly late if we show it as soon as possible -- we
|
// on time or possibly late if we show it as soon as possible -- we
|
||||||
// acquire and return it. If we don't want to display it until after the
|
// acquire and return it. If we don't want to display it until after the
|
||||||
// expectedPresent time, we return PRESENT_LATER without acquiring it.
|
// expectedPresent time, we return PRESENT_LATER without acquiring it.
|
||||||
//
|
//
|
||||||
// To be safe, we don't defer acquisition if expectedPresent is more
|
// To be safe, we don't defer acquisition if expectedPresent is more
|
||||||
// than one second in the future beyond the desired present time
|
// than one second in the future beyond the desired present time
|
||||||
// (i.e., we'd be holding the buffer for a long time).
|
// (i.e., we'd be holding the buffer for a long time).
|
||||||
//
|
//
|
||||||
// NOTE: Code assumes monotonic time values from the system clock
|
// NOTE: Code assumes monotonic time values from the system clock
|
||||||
// are positive.
|
// are positive.
|
||||||
|
|
||||||
// Start by checking to see if we can drop frames. We skip this check if
|
// Start by checking to see if we can drop frames. We skip this check if
|
||||||
// the timestamps are being auto-generated by Surface. If the app isn't
|
// the timestamps are being auto-generated by Surface. If the app isn't
|
||||||
// generating timestamps explicitly, it probably doesn't want frames to
|
// generating timestamps explicitly, it probably doesn't want frames to
|
||||||
// be discarded based on them.
|
// be discarded based on them.
|
||||||
while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
|
while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
|
||||||
const BufferItem& bufferItem(mCore->mQueue[1]);
|
const BufferItem& bufferItem(mCore->mQueue[1]);
|
||||||
|
|
||||||
// If dropping entry[0] would leave us with a buffer that the
|
// If dropping entry[0] would leave us with a buffer that the
|
||||||
// consumer is not yet ready for, don't drop it.
|
// consumer is not yet ready for, don't drop it.
|
||||||
if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
|
if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If entry[1] is timely, drop entry[0] (and repeat). We apply an
|
||||||
|
// additional criterion here: we only drop the earlier buffer if our
|
||||||
|
// desiredPresent falls within +/- 1 second of the expected present.
|
||||||
|
// Otherwise, bogus desiredPresent times (e.g., 0 or a small
|
||||||
|
// relative timestamp), which normally mean "ignore the timestamp
|
||||||
|
// and acquire immediately", would cause us to drop frames.
|
||||||
|
//
|
||||||
|
// We may want to add an additional criterion: don't drop the
|
||||||
|
// earlier buffer if entry[1]'s fence hasn't signaled yet.
|
||||||
|
nsecs_t desiredPresent = bufferItem.mTimestamp;
|
||||||
|
if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
|
||||||
|
desiredPresent > expectedPresent) {
|
||||||
|
// This buffer is set to display in the near future, or
|
||||||
|
// desiredPresent is garbage. Either way we don't want to drop
|
||||||
|
// the previous buffer just to get this on the screen sooner.
|
||||||
|
BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
|
||||||
|
PRId64 " (%" PRId64 ") now=%" PRId64,
|
||||||
|
desiredPresent, expectedPresent,
|
||||||
|
desiredPresent - expectedPresent,
|
||||||
|
systemTime(CLOCK_MONOTONIC));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
|
||||||
|
" size=%zu",
|
||||||
|
desiredPresent, expectedPresent, mCore->mQueue.size());
|
||||||
|
if (mCore->stillTracking(front)) {
|
||||||
|
// Front buffer is still in mSlots, so mark the slot as free
|
||||||
|
mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
|
||||||
|
mCore->mFreeBuffers.push_back(front->mSlot);
|
||||||
|
listener = mCore->mConnectedProducerListener;
|
||||||
|
++numDroppedBuffers;
|
||||||
|
}
|
||||||
|
mCore->mQueue.erase(front);
|
||||||
|
front = mCore->mQueue.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If entry[1] is timely, drop entry[0] (and repeat). We apply an
|
// See if the front buffer is ready to be acquired
|
||||||
// additional criterion here: we only drop the earlier buffer if our
|
nsecs_t desiredPresent = front->mTimestamp;
|
||||||
// desiredPresent falls within +/- 1 second of the expected present.
|
bool bufferIsDue = desiredPresent <= expectedPresent ||
|
||||||
// Otherwise, bogus desiredPresent times (e.g., 0 or a small
|
desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
|
||||||
// relative timestamp), which normally mean "ignore the timestamp
|
bool consumerIsReady = maxFrameNumber > 0 ?
|
||||||
// and acquire immediately", would cause us to drop frames.
|
front->mFrameNumber <= maxFrameNumber : true;
|
||||||
//
|
if (!bufferIsDue || !consumerIsReady) {
|
||||||
// We may want to add an additional criterion: don't drop the
|
BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
|
||||||
// earlier buffer if entry[1]'s fence hasn't signaled yet.
|
" (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
|
||||||
nsecs_t desiredPresent = bufferItem.mTimestamp;
|
" consumer=%" PRIu64,
|
||||||
if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
|
|
||||||
desiredPresent > expectedPresent) {
|
|
||||||
// This buffer is set to display in the near future, or
|
|
||||||
// desiredPresent is garbage. Either way we don't want to drop
|
|
||||||
// the previous buffer just to get this on the screen sooner.
|
|
||||||
BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
|
|
||||||
PRId64 " (%" PRId64 ") now=%" PRId64,
|
|
||||||
desiredPresent, expectedPresent,
|
desiredPresent, expectedPresent,
|
||||||
desiredPresent - expectedPresent,
|
desiredPresent - expectedPresent,
|
||||||
systemTime(CLOCK_MONOTONIC));
|
systemTime(CLOCK_MONOTONIC),
|
||||||
break;
|
front->mFrameNumber, maxFrameNumber);
|
||||||
|
return PRESENT_LATER;
|
||||||
}
|
}
|
||||||
|
|
||||||
BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
|
BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
|
||||||
" size=%zu",
|
"(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
|
||||||
desiredPresent, expectedPresent, mCore->mQueue.size());
|
|
||||||
if (mCore->stillTracking(front)) {
|
|
||||||
// Front buffer is still in mSlots, so mark the slot as free
|
|
||||||
mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
|
|
||||||
mCore->mFreeBuffers.push_back(front->mSlot);
|
|
||||||
}
|
|
||||||
mCore->mQueue.erase(front);
|
|
||||||
front = mCore->mQueue.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
// See if the front buffer is ready to be acquired
|
|
||||||
nsecs_t desiredPresent = front->mTimestamp;
|
|
||||||
bool bufferIsDue = desiredPresent <= expectedPresent ||
|
|
||||||
desiredPresent > expectedPresent + MAX_REASONABLE_NSEC;
|
|
||||||
bool consumerIsReady = maxFrameNumber > 0 ?
|
|
||||||
front->mFrameNumber <= maxFrameNumber : true;
|
|
||||||
if (!bufferIsDue || !consumerIsReady) {
|
|
||||||
BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
|
|
||||||
" (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64
|
|
||||||
" consumer=%" PRIu64,
|
|
||||||
desiredPresent, expectedPresent,
|
|
||||||
desiredPresent - expectedPresent,
|
desiredPresent - expectedPresent,
|
||||||
systemTime(CLOCK_MONOTONIC),
|
systemTime(CLOCK_MONOTONIC));
|
||||||
front->mFrameNumber, maxFrameNumber);
|
|
||||||
return PRESENT_LATER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
|
int slot = front->mSlot;
|
||||||
"(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
|
*outBuffer = *front;
|
||||||
desiredPresent - expectedPresent,
|
ATRACE_BUFFER_INDEX(slot);
|
||||||
systemTime(CLOCK_MONOTONIC));
|
|
||||||
|
BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
|
||||||
|
slot, front->mFrameNumber, front->mGraphicBuffer->handle);
|
||||||
|
// If the front buffer is still being tracked, update its slot state
|
||||||
|
if (mCore->stillTracking(front)) {
|
||||||
|
mSlots[slot].mAcquireCalled = true;
|
||||||
|
mSlots[slot].mNeedsCleanupOnRelease = false;
|
||||||
|
mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
|
||||||
|
mSlots[slot].mFence = Fence::NO_FENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the buffer has previously been acquired by the consumer, set
|
||||||
|
// mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
|
||||||
|
// on the consumer side
|
||||||
|
if (outBuffer->mAcquireCalled) {
|
||||||
|
outBuffer->mGraphicBuffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mCore->mQueue.erase(front);
|
||||||
|
|
||||||
|
// We might have freed a slot while dropping old buffers, or the producer
|
||||||
|
// may be blocked waiting for the number of buffers in the queue to
|
||||||
|
// decrease.
|
||||||
|
mCore->mDequeueCondition.broadcast();
|
||||||
|
|
||||||
|
ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
|
||||||
|
|
||||||
|
mCore->validateConsistencyLocked();
|
||||||
}
|
}
|
||||||
|
|
||||||
int slot = front->mSlot;
|
if (listener != NULL) {
|
||||||
*outBuffer = *front;
|
for (int i = 0; i < numDroppedBuffers; ++i) {
|
||||||
ATRACE_BUFFER_INDEX(slot);
|
listener->onBufferReleased();
|
||||||
|
}
|
||||||
BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
|
|
||||||
slot, front->mFrameNumber, front->mGraphicBuffer->handle);
|
|
||||||
// If the front buffer is still being tracked, update its slot state
|
|
||||||
if (mCore->stillTracking(front)) {
|
|
||||||
mSlots[slot].mAcquireCalled = true;
|
|
||||||
mSlots[slot].mNeedsCleanupOnRelease = false;
|
|
||||||
mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
|
|
||||||
mSlots[slot].mFence = Fence::NO_FENCE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the buffer has previously been acquired by the consumer, set
|
|
||||||
// mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
|
|
||||||
// on the consumer side
|
|
||||||
if (outBuffer->mAcquireCalled) {
|
|
||||||
outBuffer->mGraphicBuffer = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
mCore->mQueue.erase(front);
|
|
||||||
|
|
||||||
// We might have freed a slot while dropping old buffers, or the producer
|
|
||||||
// may be blocked waiting for the number of buffers in the queue to
|
|
||||||
// decrease.
|
|
||||||
mCore->mDequeueCondition.broadcast();
|
|
||||||
|
|
||||||
ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
|
|
||||||
|
|
||||||
mCore->validateConsistencyLocked();
|
|
||||||
|
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user