camerahal之---hal3主要接口实现(1)

Hal需要实现的接口就是libhardware\include\hardware\camera3.h

typedef struct camera3_device {
    /**
     * common.version must equal CAMERA_DEVICE_API_VERSION_3_0 to identify this
     * device as implementing version 3.0 of the camera device HAL.
     *
     * Performance requirements:
     *
     * Camera open (common.module->common.methods->open) should return in 200ms, and must return
     * in 500ms.
     * Camera close (common.close) should return in 200ms, and must return in 500ms.
     *
     */
    hw_device_t common;//这个是hal层的公用结构体,必须包含才能加载
    camera3_device_ops_t *ops;//这个是camerahal和上层的交互接口
    void *priv;
} camera3_device_t;

在rk目录中hal\rockchip\camera\AAL是实现上层定义的接口,
hal\rockchip\camera\AAL\Camera3HAL.cpp

static camera3_device_ops hal_dev_ops = {
    NAMED_FIELD_INITIALIZER(initialize)                         hal_dev_initialize,
    NAMED_FIELD_INITIALIZER(configure_streams)                  hal_dev_configure_streams,
    NAMED_FIELD_INITIALIZER(register_stream_buffers)            nullptr,
    NAMED_FIELD_INITIALIZER(construct_default_request_settings) hal_dev_construct_default_request_settings,
    NAMED_FIELD_INITIALIZER(process_capture_request)            hal_dev_process_capture_request,
    NAMED_FIELD_INITIALIZER(get_metadata_vendor_tag_ops)        nullptr,
    NAMED_FIELD_INITIALIZER(dump)                               hal_dev_dump,
    NAMED_FIELD_INITIALIZER(flush)                              hal_dev_flush,
    NAMED_FIELD_INITIALIZER(reserved)                           {0},
};
定义了camera3_device_ops_t交互接口。

其实这些接口相较hal1有所减少,但是根据pipeline特性,hal代码实际变得更加复杂。
看一下赋值流程
hardware\rockchip\camera\Camera3HALModule.cpp
这个函数定义camerahal的接口

camera_module_t VISIBILITY_PUBLIC HAL_MODULE_INFO_SYM = {
NAMED_FIELD_INITIALIZER(common) {
……
NAMED_FIELD_INITIALIZER(methods) &hal_module_methods,
……
}
static struct hw_module_methods_t hal_module_methods = {
    NAMED_FIELD_INITIALIZER(open) hal_dev_open
};

也就是camerahal模块的open函数是hal_dev_open

static int hal_dev_open(const hw_module_t* module, const char* name,
                        hw_device_t** device)
{
……
return openCameraHardware(camera_id, module, device);
}
int openCameraHardware(int id, const hw_module_t* module, hw_device_t** device)
{
……
Camera3HAL* halDev = new Camera3HAL(id, module);
if (halDev->init() != NO_ERROR) {//init的时候会new mRequestThread
……
}
Camera3HAL::Camera3HAL(int cameraId, const hw_module_t* module) :
    mCameraId(cameraId),
    mCameraHw(nullptr),
    mRequestThread(nullptr)
{
……
    mDevice.common.tag = HARDWARE_DEVICE_TAG;
    mDevice.common.version = info.device_version;
    mDevice.common.module = (hw_module_t *)(module);
    // hal_dev_close is kept in the module for symmetry with dev_open
    // it will be set there
    mDevice.common.close = nullptr;
    mDevice.ops = &hal_dev_ops;//赋值接口函数定义
mDevice.priv = this;//记住这个私有指针就是Camera3HAL
}

这种是通用做法,将设备(camera3_device)包含在各家厂商实现的类(Camera3HAL)中,设备的私有指针又指向Camera3HAL,这样,设备camera3_device里的函数又可以通过priv调用Camera3HAL中的函数。
这样比较绕,比如

Camera3HAL* camera_priv = (Camera3HAL*)(dev->priv);
static int
hal_dev_initialize(const struct camera3_device * dev,
                   const camera3_callback_ops_t *callback_ops)
{
……
    return camera_priv->initialize(callback_ops);
}

正常来说接口函数就只到这里,继续要看这几个接口函数的实现。
先接着看这个initialize函数的实现把
callback_ops是在Camera3Device::Camera3Device上层就已经初始化好了。
hal_dev_initialize(camera3_device-> camera3_device_ops-> initialize)实际执行的是Camera3HAL中的initialize函数

int Camera3HAL::initialize(const camera3_callback_ops_t *callback_ops)
{
……
    status = mRequestThread->init(callback_ops);
……
}

通过发现几个接口函数都是执行mRequestThread下面的对应函数
函数定义在RequestThread.cpp 关注下对应几个函数做了什么

status_t
RequestThread::init(const camera3_callback_ops_t *callback_ops)
{
……// ItemPool<Camera3Request> mRequestsPool 定义
//看init函数是分配MAX_REQUEST_IN_PROCESS_NUM个Camera3Request
    status = mRequestsPool.init(MAX_REQUEST_IN_PROCESS_NUM);
……//new结果处理类,然后注册错误返回
    mResultProcessor = new ResultProcessor(this, callback_ops);
    mCameraHw->registerErrorCallback(mResultProcessor);
    mInitialized = true;
    return NO_ERROR;
}
RequestThread::configureStreams(camera3_stream_configuration_t *stream_list)
{
    Message msg;
    msg.id = MESSAGE_ID_CONFIGURE_STREAMS;
    msg.data.streams.list = stream_list;
//只是向mMessageQueue队列发送消息,之后看处理函数
    return mMessageQueue.send(&msg, MESSAGE_ID_CONFIGURE_STREAMS);
}

同理constructDefaultRequest,processCaptureRequest也是发送到消息队列
register_stream_buffers,get_metadata_vendor_tag_ops函数因为是3.2的版本,只需要为空即可

对应看看接受消息的处理
mRequestThread 里的mMessageQueue消息处理
mRequestThread 的构造函数,这个线程就跑起来了
RequestThread::messageThreadLoop是循环loop函数,里面接受消息。

switch (msg.id) {
        case MESSAGE_ID_CONFIGURE_STREAMS:
            status = handleConfigureStreams(msg);
            break;
        case MESSAGE_ID_CONSTRUCT_DEFAULT_REQUEST:
            status = handleConstructDefaultRequest(msg);
            break;
        case MESSAGE_ID_PROCESS_CAPTURE_REQUEST:
            status = handleProcessCaptureRequest(msg);
            replyImmediately = (mBlockAction == REQBLK_NONBLOCKING);
            break;
        case MESSAGE_ID_REQUEST_DONE:
            status = handleReturnRequest(msg);
            break;
        case MESSAGE_ID_FLUSH:
            break;
        default:
            LOGE("ERROR @%s: Unknow message %d", __FUNCTION__, msg.id);
            status = BAD_VALUE;
            break;
        }

这就是上层的接口都是发消息到mRequestThread,然后mRequestThread接收消息处理上层事件,所有调用统一到这里。然后处理这些消息。
具体函数大致实现原理,后续再讲把

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页