美文网首页Camera
MTK Camera HAL到驱动的流程总结一

MTK Camera HAL到驱动的流程总结一

作者: 程序员Android1 | 来源:发表于2022-09-03 17:38 被阅读0次

    和你一起终身学习,这里是程序员 Android

    经典好文推荐,通过阅读本文,您将收获以下知识点:

    一、架构介绍
    二、Camera Open流程(Hal-Sensor)
    三、Sensor Search流程

    一、架构介绍

    1.1 Camera的框架分为Kernel部分和Hal部分

    Kernel部分:

    image sensor driver——负责具体型号的sensor的id检测,上电,以及在preview、capture、初始化、3A等等功能设定时的寄存器配置;
    ISP driver——通过DMA将sensor数据流上传;
    Hal部分:

    imageio——主要负责数据buffer上传的pipe;
    drv——包含imgsensor和isp的hal层控制;
    feature io——包含各种3A等性能配置;
    来看一张图,大致来了解一下Camera的整体架构。


    1.2 MiddleWare(MW)层

    ICameraProvider——向上暴露的接口调用,实现是在CameraProvider中;
    Device@3.2ICameraDevice——用于Camera Service去操作各个Camera device的操作,实现在CameraDevice3中;
    Device@3.2ICameraDeviceSession——Camera会话的接口;
    ICameraDeviceCallBack——底层对上层的CallBack接口;
    CameraDeviceManager——用于管理CameraDevice,包括查找,打开,关闭等。

    1.3 Pipeline介绍

    PipelineModel是HAL3核心架构,对上需要开放对Pipeline创建 / 操作的API,对下需要建立Pipeline / 管理Pipeline的生命周期。
    PipelineModel会针对不同的场景创建不同的Pipeline和HWNode,HWNode向下传输APP层的命令,向上传递图形数据。

    P1Node——pipeline的root node,input app命令,output raw data to P2CaptureNode and P2StreamNode;
    P2CaptureNode——转换raw data to yuv,Support scale/crop;
    P2StreamingNode——和P2CaptureNode功能类似;
    JPEGNode——Convert YUV to Jpeg;
    FDNode——Generate the FD information;

    二、Camera Open流程(Hal-Sensor)

    在APP层调用openCamera后会调用的CameraDevice层,最后调用到driver中,整体的调用流程如下:


    先从CameraDevice3SessionImpl的open函数分析。

    //vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/device/CameraDevice3SessionImpl.cpp
    
    auto ThisNamespace::open(
        const ::android::sp<V3_4::ICameraDeviceCallback>& callback) -> ::android::status_t
    {
        auto pDeviceManager = mStaticInfo.mDeviceManager;
        auto const& instanceName = mStaticInfo.mStaticDeviceInfo->mInstanceName;
         
        status = pDeviceManager->startOpenDevice(instanceName);
         
        err = onOpenLocked(callback);
        pDeviceManager->updatePowerOnDone();
        status = pDeviceManager->finishOpenDevice(instanceName, false/*cancel*/);
    }
         
    auto ThisNamespace::onOpenLocked(
        const ::android::sp<V3_4::ICameraDeviceCallback>& callback
    ) -> ::android::status_t
    {
        //--------------------------------------------------------------------------
        {
            Mutex::Autolock _l(mPipelineModelLock);
            auto pPipelineModelMgr = IPipelineModelManager::get();
            auto pPipelineModel = pPipelineModelMgr->getPipelineModel( getInstanceId() );
            ::android::status_t err = OK;
            err = pPipelineModel->open(getInstanceName().c_str(), this);
            mPipelineModel = pPipelineModel;
        }
        //--------------------------------------------------------------------------
        return OK;
    }
    

    //vendor/mediatek/proprietary/hardware/mtkcam3/pipeline/model/PipelineModelImpl.cpp

    auto PipelineModelImpl::open(
        std::string const& userName,
        android::wp<IPipelineModelCallback> const& callback) -> int
    {
        {
            std::lock_guard<std::timed_mutex> _l(mLock);
            mUserName = userName;
            mCallback = callback;
            mvOpenFutures.push_back(
                std::async(std::launch::async,
                    [this]() {
                        return CC_LIKELY( mHalDeviceAdapter!=nullptr )
                            && CC_LIKELY( mHalDeviceAdapter->open() ) 
                            //android::sp<IHalDeviceAdapter> const    mHalDeviceAdapter;
                            && CC_LIKELY( mHalDeviceAdapter->powerOn() );
                    }
                )
            ); 
        }
        return OK;
    }
    

    调用mHalDeviceAdapter的open用于初始化DeviceAdapter,这里重点看powerOn函数,这里的powerOn有另起一个线程去操作sensor,等待sensor上电完成后对3A进行powerOn操作。

    //vendor/mediatek/proprietary/hardware/mtkcam3/pipeline/model/adapter/HalDeviceAdapter.cpp

    virtual auto powerOn() -> bool override
    {
        //1.调用HalSensor的powerOn
        std::future<bool> future_initSensor =
            std::async(std::launch::async,
                [ this ]() {
                        if (CC_UNLIKELY( !mvHalSensor[i]->powerOn(mName.c_str(), 1, &sensorIndex) ))
            }
         
        //2.init 3A and poweron 3A
        bool success_sensorPowerOn = false;
        bool success_init3A = true;
        for (size_t i = 0; i < mvPhySensorId.size(); i++)
        {
            mvHal3A.push_back(IHal3AAdapter::create(mvPhySensorId[i], mName.c_str()));
            mvHalIsp.push_back(MAKE_HalISP(mvPhySensorId[i], mName.c_str()));
        }
         
        //3.Wait for Sensor PowerOn
        {
            success_sensorPowerOn = future_initSensor.get();
            if  ( ! success_sensorPowerOn ) {
                return false;
            }
        }
            
        //4.Notify 3A of Power On
        for (size_t i = 0; i < mvHal3A.size(); i++){
           if (mvHal3A[i] != nullptr){
               mvHal3A[i]->notifyPowerOn();
            }
        }
    }
    

    这里继续跟踪mvHalSensor[i]->powerOn,会调用到HalSensor.cpp中,这里到了和Driver交互的部分:

    初始化SeninfDrv和SensorDrv;
    setSensorMclk和setSensorMclkDrivingCurrent;
    最后通过mpSensorDrv->open;
    //vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/HalSensor.cpp

    MBOOL HalSensor:: powerOn(){
         
        mpSeninfDrv->init();
        mpSensorDrv->init();
        for (MUINT i = 0; i < uCountOfIndex; i++)
        {
            setSensorMclk(sensorIdx, 1)
            setSensorMclkDrivingCurrent(sensorIdx)
         
            // Open sensor, try to open 3 time
            for (int i =0; i < 3; i++) {
                if ((ret = mpSensorDrv->open(sensorIdx)) != SENSOR_NO_ERROR) {
                    MY_LOGE("pSensorDrv->open fail, retry = %d ", i);
                }
            }
        }
    }
    

    接下来会调用到imgsensor_drv的open函数,到此featureControl调用到驱动的SENSOR_FEATURE_OPEN。

    //vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/imgsensor_drv.cpp

    MINT32
    ImgSensorDrv::open(IMGSENSOR_SENSOR_IDX sensorIdx)
    {
        MUINT32                           featureParaLen = sizeof(MUINT32);
        MUINT32                           featurePara;
    
        return featureControl(sensorIdx, SENSOR_FEATURE_OPEN, (MUINT8 *)&featurePara, &featureParaLen);
    }
    
    MINT32  ImgSensorDrv::featureControl(
        IMGSENSOR_SENSOR_IDX sensorIdx,
        ACDK_SENSOR_FEATURE_ENUM FeatureId,
        MUINT8 *pFeaturePara,
        MUINT32 *pFeatureParaLen
    )
    {
    
        ACDK_SENSOR_FEATURECONTROL_STRUCT featureCtrl;
        
        //结构ACDK_SENSOR_FEATURECONTROL_STRUCT和kernel中一致
        featureCtrl.InvokeCamera = sensorIdx;
        featureCtrl.FeatureId = FeatureId;//SENSOR_FEATURE_SET_DRIVER
        featureCtrl.pFeaturePara = pFeaturePara;
        featureCtrl.pFeatureParaLen = pFeatureParaLen;
            
        if (ioctl(m_fdSensor, KDIMGSENSORIOC_X_FEATURECONCTROL , &featureCtrl) < 0) {
            LOG_ERR("[featureControl] Err-ctrlCode (%s)", strerror(errno));
            return -errno;
        }
        
        return SENSOR_NO_ERROR;
    }
    

    三、Sensor Search流程

    CameraService是在开机时启动的,启动后进行searchSensor的操作,会search系统有多少camera,开机时的search操作,只进行camera支持数量的遍历,以及sensor ID的读取操作。

    HalSensorList:
    vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/HalSensorList.enumList.cpp
    vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/HalSensorList.cpp

    SeninfDrv:
    vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/mt6765/seninf_drv.cpp

    SensorDrv:
    vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/imgsensor_drv.cpp

    //vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/HalSensorList.cpp

    MUINT
    HalSensorList::
    searchSensors()
    {
        Mutex::Autolock _l(mEnumSensorMutex);
    
        MY_LOGD("searchSensors");
        return  enumerateSensor_Locked();
    }
    
    MUINT
    HalSensorList::
    queryNumberOfSensors() const
    {
        Mutex::Autolock _l(mEnumSensorMutex);
    
        return  mEnumSensorList.size();
    }
    
    IMetadata const&
    HalSensorList::
    queryStaticInfo(MUINT const index) const
    {
        EnumInfo const* pInfo = queryEnumInfoByIndex(index);
        MY_LOGF_IF(pInfo==NULL, "NULL EnumInfo for sensor %d", index);
    
        return  pInfo->mMetadata;
    }
    

    searchSensors()会调用enumerateSensor_Locked()。

    //vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/HalSensorList.enumList.cpp

    MUINT HalSensorList::enumerateSensor_Locked()
    {
        SensorDrv *const pSensorDrv = SensorDrv::get();
        SeninfDrv *const pSeninfDrv = SeninfDrv::createInstance();
        //初始化seninf,配置ISP相关内容
        pSeninfDrv->init();
         
        //将所有的clk全部打开
        pSeninfDrv->setAllMclkOnOff(ISP_DRIVING_8MA, TRUE);
            
        pSensorDrv->init();
        for (MUINT i = IMGSENSOR_SENSOR_IDX_MIN_NUM; i <= max_index_of_camera; i++) {
            if((ret = pSensorDrv->searchSensor((IMGSENSOR_SENSOR_IDX)i)) == SENSOR_NO_ERROR){
                //query sensorinfo
               querySensorDrvInfo((IMGSENSOR_SENSOR_IDX)i);
               //fill in metadata
               buildSensorMetadata((IMGSENSOR_SENSOR_IDX)i);
               pSensorInfo = pSensorDrv->getSensorInfo((IMGSENSOR_SENSOR_IDX)i);
               addAndInitSensorEnumInfo_Locked(
                    (IMGSENSOR_SENSOR_IDX)i,
                    mapToSensorType(pSensorInfo->GetType()),
                    pSensorInfo->getDrvMacroName());
            }
        }     
    }
    

    下面看下pSensorDrv->getSensorInfo的流程,这里有去获取sensorList的内容。

    //vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/common/v1/imgsensor_drv.cpp

    MINT32 ImgSensorDrv::searchSensor(IMGSENSOR_SENSOR_IDX sensorIdx)
    {
        GetSensorInitFuncList(&pSensorInitFunc);
         
        featureControl(sensorIdx, SENSOR_FEATURE_SET_DRIVER, (MUINT8 *)&idx, &featureParaLen);
         
        NSFeature::SensorInfoBase* pSensorInfo = pSensorInitFunc[idx].pSensorInfo;
    }
    

    (A)GetSensorInitFuncList是获取到配置的sensorList的内容,此sensorList需要与kernel层配置的一致,不一致的话在打开camera时会出现异常。

    //vendor/mediatek/proprietary/custom/mtxxxx/hal/imgsensor_src/sensorlist.cpp

    MSDK_SENSOR_INIT_FUNCTION_STRUCT SensorList[] =
    {
    /*IMX*/
    #if defined(IMX586_MIPI_RAW)
        RAW_INFO_M(IMX586_SENSOR_ID, DEFAULT_MODULE_INDEX, DEFAULT_MODULE_ID, SENSOR_DRVNAME_IMX586_MIPI_RAW, CAM_CALGetCalData),
    #endif
    #if defined(IMX519_MIPI_RAW)
        RAW_INFO_M(IMX519_SENSOR_ID, DEFAULT_MODULE_INDEX, DEFAULT_MODULE_ID, SENSOR_DRVNAME_IMX519_MIPI_RAW, CAM_CALGetCalData),
    #endif
    //...
    };
    
    UINT32 GetSensorInitFuncList(MSDK_SENSOR_INIT_FUNCTION_STRUCT **ppSensorList)
    {
        if (NULL == ppSensorList) {
            ALOGE("ERROR: NULL pSensorList\n");
            return MHAL_UNKNOWN_ERROR;
        }
        *ppSensorList = &SensorList[0];
        return MHAL_NO_ERROR;
    }
    

    (B)featureControl的setDriver流程同上面的一致。

    原文链接:https://blog.csdn.net/dongxianfei/article/details/121382327

    至此,本篇已结束。转载网络的文章,小编觉得很优秀,欢迎点击阅读原文,支持原创作者,如有侵权,恳请联系小编删除,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

    相关文章

      网友评论

        本文标题:MTK Camera HAL到驱动的流程总结一

        本文链接:https://www.haomeiwen.com/subject/ijnunrtx.html