MTK Camera Open流程

首先看下MTKcam的整体框架图如下包含了很多的内容,其中camera的打开流程也贯穿在其中,从Camera APK 一层层的系统调用到driver再到Hardware层,这篇文章主要从Pipeline开始介绍打开camera的流程。

MiddleWare(MW)层介绍

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

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 yuvs,Support scale/crop;
  • P2StreamingNode:和P2CaptureNode功能类似;
  • JPEGNode:Convert YUV to Jpeg;
  • FDNode:Generate the FD information;

Pipeline介绍

Camera Open流程

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

Camera 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;
}

这里主要看PipelineModel的Open函数如下:

//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交互的部分:

(1)初始化SeninfDrv;

(2)初始化SensorDrv;

(3)setSensorMclk和setSensorMclkDrivingCurrent;

(4)最后通过mpSensorDrv->open;

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);
            }
        }
    }
}

mpSensorDrv->open会调用如下,到此featureControl调用到驱动的SENSOR_FEATURE_OPEN:

//vendor/mediatek/proprietary/hardware/mtkcam/drv/src/sensor/mt6765/../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);
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程