_buffer_set {
/**
* The stream handle for the stream these buffers belong to
*/
camera3_stream_t *stream;
/**
* The number of buffers in this stream. It is guaranteed to be at least
* stream->max_buffers.
*/
uint32_t num_buffers;
/**
* The array of gralloc buffer handles for this stream. If the stream format
* is set to HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, the camera HAL device
* should inspect the passed-in buffers to determine any platform-private
* pixel format information.
*/
buffer_handle_t **buffers;
} camera3_stream_buffer_set_t;
三个变量分别保存stream的buffer个数,当前这个set集合所属的stream,以及他所包含的所有buffer的handle信息列表。
d getBufferLocked获取当前buffer
status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
ATRACE_CALL();
status_t res;
if ((res = getBufferPreconditionCheckLocked()) != OK) {
return res;
}
ANativeWindowBuffer* anb;
int fenceFd;
/**
* Release the lock briefly to avoid deadlock for below scenario:
* Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
* This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
* Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
* This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
* StreamingProcessor lock.
* Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
* and try to lock bufferQueue lock.
* Then there is circular locking dependency.
*/
sp
currentConsumer = mConsumer;
mLock.unlock();
res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
mLock.lock();
if (res != OK) {
ALOGE(%s: Stream %d: Can't dequeue next output buffer: %s (%d),
__FUNCTION__, mId, strerror(-res), res);
return res;
}
/**
* FenceFD now owned by HAL except in case of error,
* in which case we reassign it to acquire_fence
*/
handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
/*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
return OK;
}
该函数主要是从由ANativeWindow从Consumer端dequeue获取一个buffer,本质上这个过程中首次执行是会有Consumer端去分配一个由实际物理空间的给当前的一个buffer的。
接着执行handoutBufferLocked,填充camera3_stream_buffer这个结构体,其中设置的acquireFence为-1值表明hal3的这个buffer可被Framewrok直接使用,而acquireFence表示HAL3如何想使用这个buffer时需要等待其变为1,因为buffer分配和handler返回不一定是一致同步的。还会切换当前buffer的状态CAMERA3_BUFFER_STATUS_OK。
void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
buffer_handle_t *handle,
int acquireFence,
int releaseFence,
camera3_buffer_status_t status,
bool output) {
/**
* Note that all fences are now owned by HAL.
*/
// Handing out a raw pointer to this object. Increment internal refcount.
incStrong(this);
buffer.stream = this;
buffer.buffer = handle;
buffer.acquire_fence = acquireFence;
buffer.release_fence = releaseFence;
buffer.status = status;
// Inform tracker about becoming busy
if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
mState != STATE_IN_RECONFIG) {
/**
* Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
* before/after register_stream_buffers during initial configuration
* or re-configuration.
*
* TODO: IN_CONFIG and IN_RECONFIG checks only make sense for
statusTracker = mStatusTr