Camera2 Preview流程
CameraDevice createCaptureSession
Surface Consumer/Hal Producter
注意下面方法的注释,processCaptureResult这个方法就是hal到时候回调图像数据的地方
- framworks
/**
*
* Camera HAL device callback methods
*/
void Camera3Device::processCaptureResult(const camera3_capture_result *result)
- hal 返回数据
void ResultProcessor::returnPendingBuffers(RequestState_t * reqState)
{
unsigned int i;
camera3_capture_result_t result;
camera3_stream_buffer_t buf;
std::shared_ptr<CameraBuffer> pendingBuf;
Camera3Request *request = reqState->request;
/**
* protection against duplicated calls when all buffers have been returned
*/
if (reqState->buffersReturned == reqState->buffersToReturn)
{
LOGW("trying to return buffers twice. Check PSL implementation");
return;
}
for (i = 0; i < reqState->pendingBuffers.size(); i++)
{
CLEAR(buf);
CLEAR(result);
pendingBuf = reqState->pendingBuffers[i];
if (!request->isInputBuffer(pendingBuf))
{
result.num_output_buffers = 1;
}
result.frame_number = reqState->reqId;
// Force drop buffers when request error
buf.status = request->getError() ? CAMERA3_BUFFER_STATUS_ERROR : pendingBuf->status();
buf.stream = pendingBuf->getOwner()->getStream();
/* framework check the handle point other than handle */
/* buf.buffer = pendingBuf->getBufferHandle(); */
buf.buffer = pendingBuf->getBufferHandlePtr();
pendingBuf->getFence(&buf);
result.result = nullptr;
if (request->isInputBuffer(pendingBuf))
{
result.input_buffer = &buf;
LOGD(" <Request %d> return an input buffer", reqState->reqId);
}
else
{
result.output_buffers = &buf;
}
processCaptureResult(reqState, &result);//回调数据
pendingBuf->getOwner()->decOutBuffersInHal();
reqState->buffersReturned += 1;
LOGD(" <Request %d> camera id %d buffer done %d/%d ", reqState->reqId,
reqState->request->getCameraId(), reqState->buffersReturned, reqState->buffersToReturn);
}
reqState->pendingBuffers.clear();
}
void ResultProcessor::processCaptureResult(RequestState_t * reqState, camera3_capture_result * result)
{
int numMetaLeft = mPartialResultCount - reqState->partialResultReturned;
int numBufLeft = reqState->buffersToReturn - reqState->buffersReturned;
// Report request error when it's the last result
if (numMetaLeft + numBufLeft == 1)
{
Camera3Request *request = reqState->request;
if (request->getError())
returnRequestError(request->getId());
}
mCallbackOps->process_capture_result(mCallbackOps, result); // callback to framworks
}
Camera3Stream类图
Surface和SurfaceFlinger
Surface创建
注意:Native层的Surface和Java层的Surface有所区别:
1. Native Surface和ANativeWindow强绑定
2. Java Surface 是对包含handle和gbp引用的SurfaceControl的引用的封装
Surface和ANativeWindow
追踪Surface创建过程主要是为了搞清Surface和ANativeWindow的关系
- Surface.cpp
Surface::Surface(const sp<IGraphicBufferProducer> &bufferProducer, bool controlledByApp)
: mGraphicBufferProducer(bufferProducer),
mCrop(Rect::EMPTY_RECT),
mBufferAge(0),
mGenerationNumber(0),
mSharedBufferMode(false),
mAutoRefresh(false),
mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
mSharedBufferHasBeenQueued(false),
mQueriedSupportedTimestamps(false),
mFrameTimestampsSupportsPresent(false),
mEnableFrameTimestamps(false),
mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
{
// Initialize the ANativeWindow function pointers.
ANativeWindow::setSwapInterval = hook_setSwapInterval;
ANativeWindow::dequeueBuffer = hook_dequeueBuffer; // gmq 这
ANativeWindow::cancelBuffer = hook_cancelBuffer;
ANativeWindow::queueBuffer = hook_queueBuffer;
ANativeWindow::query = hook_query;
ANativeWindow::perform = hook_perform;
ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
....
}