listener;
{ // Autolock scope
std::unique_lockstatus_t BufferQueueConsumer::setMaxAcquiredBufferCount(
int maxAcquiredBuffers) {
if (maxAcquiredBuffers < 1 ||
maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
maxAcquiredBuffers);
return BAD_VALUE;
}
sp<IConsumerListener> listener;
{ // Autolock scope
std::unique_lock<std::mutex> lock(mCore->mMutex);
mCore->waitWhileAllocatingLocked(lock);
if (mCore->mIsAbandoned) {
BQ_LOGE("setMaxAcquiredBufferCount: consumer is abandoned");
return NO_INIT;
}
if (maxAcquiredBuffers == mCore->mMaxAcquiredBufferCount) {
return NO_ERROR;
}
maxAcquiredBuffers
,它不能小于 1 且不能大于 MAX_MAX_ACQUIRED_BUFFERS
。mIsAbandoned
是 true
,说明消费者调用了 BufferQueueConsumer::disconnect(),无法再使用。 // The new maxAcquiredBuffers count should not be violated by the number
// of currently acquired buffers
int acquiredCount = 0;
for (int slot : mCore->mActiveBuffers) {
if (mSlots[slot].mBufferState.isAcquired()) {
acquiredCount++;
}
}
if (acquiredCount > maxAcquiredBuffers) {
BQ_LOGE("setMaxAcquiredBufferCount: the requested maxAcquiredBuffer"
"count (%d) exceeds the current acquired buffer count (%d)",
maxAcquiredBuffers, acquiredCount);
return BAD_VALUE;
}
if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount +
(mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0))
> mCore->mMaxBufferCount) {
BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would "
"exceed the maxBufferCount (%d) (maxDequeued %d async %d)",
maxAcquiredBuffers, mCore->mMaxBufferCount,
mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode ||
mCore->mDequeueBufferCannotBlock);
return BAD_VALUE;
}
ACQUIRED
的 slot 个数 acquiredCount
,它不能大于 maxAcquiredBuffers
。ACQUIRED
个数和最大 DEQUEUED
个数之和不能大于 mMaxBufferCount
,当 mAsyncMode
或 mDequeueBufferCannotBlock
是 true
时可超 1 个。mMaxBufferCount
的默认值是 NUM_BUFFER_SLOTS
,即 64。 int delta = maxAcquiredBuffers - mCore->mMaxAcquiredBufferCount;
if (!mCore->adjustAvailableSlotsLocked(delta)) {
return BAD_VALUE;
}
mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
if (delta < 0 && mCore->mBufferReleasedCbEnabled) {
listener = mCore->mConsumerListener;
}
} // Autolock scope
// Call back without lock held
if (listener != nullptr) {
listener->onBuffersReleased();
}
return NO_ERROR;
}
FREE
slot 数。 如果调整失败,则返回 BAD_VALUE
。mMaxAcquiredBufferCount
。如果 delta
小于零时,可能释放了 mFreeBuffers
中的 buffer,所以回调 onBuffersReleased()
。