// dequeueBuffer gets the next buffer slot index for the producer to use.
// If a buffer slot is available then that slot index is written to the
// location pointed to by the buf argument and a status of OK is returned.
// If no slot is available then a status of -EBUSY is returned and buf is
// unmodified.
//
// The outFence parameter will be updated to hold the fence associated with
// the buffer. The contents of the buffer must not be overwritten until the
// fence signals. If the fence is Fence::NO_FENCE, the buffer may be
// written immediately.
//
// The width and height parameters must be no greater than the minimum of
// GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
// An error due to invalid dimensions might not be reported until
// updateTexImage() is called.  If width and height are both zero, the
// default values specified by setDefaultBufferSize() are used instead.
//
// If the format is 0, the default format will be used.
//
// The usage argument specifies gralloc buffer usage flags.  The values
// are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER.  These
// will be merged with the usage flags specified by setConsumerUsageBits.
//
// The return value may be a negative error value or a non-negative
// collection of flags.  If the flags are set, the return values are
// valid, but additional actions must be performed.
//
// If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
// producer must discard cached GraphicBuffer references for the slot
// returned in buf.
// If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
// must discard cached GraphicBuffer references for all slots.
//
// In both cases, the producer will need to call requestBuffer to get a
// GraphicBuffer handle for the returned slot.
status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
                                            uint32_t width, uint32_t height, PixelFormat format,
                                            uint64_t usage, uint64_t* outBufferAge,
                                            FrameEventHistoryDelta* outTimestamps) {

上面是注释说明。下面看实现。

status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
                                            uint32_t width, uint32_t height, PixelFormat format,
                                            uint64_t usage, uint64_t* outBufferAge,
                                            FrameEventHistoryDelta* outTimestamps) {
    ATRACE_CALL();
    { // Autolock scope
        std::lock_guard<std::mutex> lock(mCore->mMutex);
        mConsumerName = mCore->mConsumerName;

        if (mCore->mIsAbandoned) {
            BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
            return NO_INIT;
        }

        if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
            BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
            return NO_INIT;
        }
    } // Autolock scope
if ((width && !height) || (!width && height)) {
    BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
    return BAD_VALUE;
}
{ // Autolock scope
    std::unique_lock<std::mutex> lock(mCore->mMutex);

    // If we don't have a free buffer, but we are currently allocating, we wait until allocation
    // is finished such that we don't allocate in parallel.
    if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
        mDequeueWaitingForAllocation = true;
        mCore->waitWhileAllocatingLocked(lock);
        mDequeueWaitingForAllocation = false;
        mDequeueWaitingForAllocationCondition.notify_all();
    }
    if (format == 0) {
        format = mCore->mDefaultBufferFormat;
    }

    // Enable the usage bits the consumer requested
    usage |= mCore->mConsumerUsageBits;

    const bool useDefaultSize = !width && !height;
    if (useDefaultSize) {
        width = mCore->mDefaultWidth;
        height = mCore->mDefaultHeight;
        if (mCore->mAutoPrerotation &&
            (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
            std::swap(width, height);
        }
    }