Android P之Camera HAL3流程分析(1)

时间:2024-03-23 10:01:35

Camera2中打开相机也需要通过CameraManager类
    private void openCamera() {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        //打开相机设备
        manager.openCamera(mCameraId, mStateCallback, mCameraHandler);
    }
实现StateCallback 接口,当相机打开后会回调onOpened方法,在这个方法里面开启预览
    private CameraDevice mCameraDevice;
    private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            //保存相机设备
            mCameraDevice = camera;
            startPreview();
        }

        @Override
        public void onDisconnected(CameraDevice camera) {
            camera.close();
            mCameraDevice = null;
        }

        @Override
        public void onError(CameraDevice camera, int error) {
            camera.close();
            mCameraDevice = null;
        }
    };

Open阶段

frameworks/base/core/java/android/hardware/camera2/CameraManager.java
    public void openCamera(@NonNull String cameraId,
            @NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler) {
        openCameraForUid(cameraId, callback, CameraDeviceImpl.checkAndWrapHandler(handler),
                USE_CALLING_UID);
    }
    public void openCameraForUid(@NonNull String cameraId,
            @NonNull final CameraDevice.StateCallback callback, @NonNull Executor executor,
            int clientUid) {
        openCameraDeviceUserAsync(cameraId, callback, executor, clientUid);
    }
frameworks/base/core/java/android/hardware/camera2/CameraManager.java
    private CameraDevice openCameraDeviceUserAsync(String cameraId,
            CameraDevice.StateCallback callback, Executor executor, final int uid) {
           ICameraDeviceUser cameraUser = null;
            //返回给应用端的相机对象
            android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =
                    new android.hardware.camera2.impl.CameraDeviceImpl(
                        cameraId,
                        callback,
                        executor,
                        characteristics,
                        mContext.getApplicationInfo().targetSdkVersion);

            //应用端的回调函数
            ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();
            try {
                if (supportsCamera2ApiLocked(cameraId)) {
                    // Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
                    ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
                    //通过cameraService跨进程获得BpCameraDeviceUser对象
                    cameraUser = cameraService.connectDevice(callbacks, cameraId,
                            mContext.getOpPackageName(), uid);
                }
            }
            //保存CameraDeviceClient对象到CameraDeviceImpl,触发应用的回调函数
            deviceImpl.setRemoteDevice(cameraUser);
     }
-----------------------------------------------------------------------------------------------------------------------------------
CameraDeviceImpl继承CameraDevice
public class CameraDeviceImpl extends CameraDevice
    public CameraDeviceImpl(String cameraId, StateCallback callback, Executor executor,
                        CameraCharacteristics characteristics, int appTargetSdkVersion) {
        mCameraId = cameraId;
        mDeviceCallback = callback;
        mDeviceExecutor = executor;
    }
}

-----------------------------------------------------------------------------------------------------------------------------------
setRemoteDevice的实现
frameworks/base/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
private ICameraDeviceUserWrapper mRemoteDevice;
    public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException {
        synchronized(mInterfaceLock) {
            mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice);
            mDeviceExecutor.execute(mCallOnOpened);
            mDeviceExecutor.execute(mCallOnUnconfigured);
        }
    }
    private final Runnable mCallOnOpened = new Runnable() {
        public void run() {
            sessionCallback = mSessionStateCallback;
            sessionCallback.onOpened(CameraDeviceImpl.this);
            //通知相机应用设备已打开,并且返回CameraDeviceImpl对象,相机应用已实现该函数
            mDeviceCallback.onOpened(CameraDeviceImpl.this);
        }
    };
frameworks/base/core/java/android/hardware/camera2/impl/ICameraDeviceUserWrapper.java
    public ICameraDeviceUserWrapper(ICameraDeviceUser remoteDevice) {
        mRemoteDevice = remoteDevice;
    }
通过以上流程,相机应用获得了CameraDevice对象,而CameraDevice保存ICameraDeviceUserWrapper对象,ICameraDeviceUserWrapper保存BpCameraDeviceUser对象,BpCameraDeviceUser具有跨进程的能力,这样应用就可以和CameraService端的CameraDeviceClient进行通信了。
-----------------------------------------------------------------------------------------------------------------------------------
CameraManagerGlobal.get().getCameraService()的实现
通过ServiceManager获的cameraService,CameraManager通过CameraManagerGlobal访问CameraService服务,并注册监听,CamreaService持有CameraServiceListener列表,并回调结果给CameraManager。
frameworks/base/core/java/android/hardware/camera2/CameraManager.java
private static final class CameraManagerGlobal extends ICameraServiceListener.Stub {
         public ICameraService getCameraService() {
           //连接服务
            connectCameraServiceLocked();
            return mCameraService;
        }
}
private void connectCameraServiceLocked() {
        //查询服务引用,CAMERA_SERVICE_BINDER_NAME="media.camera"
        IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
        //转换服务接口
        ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
        //注册回调监听,camerService可以通知CameraManagerGlobal状态变化
        CameraStatus[] cameraStatuses = cameraService.addListener(this);
        //存副本
        mCameraService = cameraService;
}
frameworks/av/services/camera/libcameraservice/CameraService.h
static char const* getServiceName() { return "media.camera"; }

-----------------------------------------------------------------------------------------------------------------------------------
cameraService.connectDevice的实现
跨进程调用CameraManager->CameraService,返回CameraDeviceClient,将应用端的回调函数设置给cameraService。
frameworks/av/services/camera/libcameraservice/CameraService.cpp
Status CameraService::connectDevice(
        const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
        const String16& cameraId,
        const String16& clientPackageName,
        int clientUid,
        /*out*/
        sp<hardware::camera2::ICameraDeviceUser>* device) {
    sp<CameraDeviceClient> client = nullptr;
    ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
            /*api1CameraId*/-1,
            CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
            clientUid, USE_CALLING_PID, API_2,
            /*legacyMode*/ false, /*shimUpdateOnly*/ false,
            /*out*/client);
    *device = client; // client = CameraDeviceClient
    return ret;
}
template<class CALLBACK, class CLIENT>
Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
        int api1CameraId, int halVersion, const String16& clientPackageName, int clientUid,
        int clientPid, apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
        /*out*/sp<CLIENT>& device) {
        if(!(ret = makeClient(this, cameraCb, clientPackageName,
                cameraId, api1CameraId, facing,
                clientPid, clientUid, getpid(), legacyMode,
                halVersion, deviceVersion, effectiveApiLevel,
                /*out*/&tmp)).isOk()) {
            return ret;
        }
        //转换BasicClient类型为CLIENT=CameraDeviceClient
        client = static_cast<CLIENT*>(tmp.get());
        //初始化CameraDeviceClient对象
        err = client->initialize(mCameraProviderManager, mMonitorTags);
        device = client;
}
Android P之Camera HAL3流程分析(1)

-----------------------------------------------------------------------------------------------------------------------------------
makeClient的实现
Status CameraService::makeClient(const sp<CameraService>& cameraService,
        const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
        int api1CameraId, int facing, int clientPid, uid_t clientUid, int servicePid,
        bool legacyMode, int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
        /*out*/sp<BasicClient>* client) {

    if (halVersion < 0 || halVersion == deviceVersion) {
        switch(deviceVersion) {
          case CAMERA_DEVICE_API_VERSION_3_0:
          case CAMERA_DEVICE_API_VERSION_3_1:
          case CAMERA_DEVICE_API_VERSION_3_2:
          case CAMERA_DEVICE_API_VERSION_3_3:
          case CAMERA_DEVICE_API_VERSION_3_4:
            if (effectiveApiLevel == API_1) { // Camera1 API route
                sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
                *client = new Camera2Client(cameraService, tmp, packageName,
                        cameraId, api1CameraId,
                        facing, clientPid, clientUid,
                        servicePid, legacyMode);
            } else { // Camera2 API route
                sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
                        static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
                *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
                        facing, clientPid, clientUid, servicePid);
            }
            break;
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------

获得CameraDeviceClient之后进行initialize
frameworks/av/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
        const String8& monitorTags) {
    return initializeImpl(manager, monitorTags);
}
//调用父类函数,CameraDeviceClient继承Camera2ClientBase,初始化时会创建Camera3Device对象
Camera2ClientBase<TClientBase>::Camera2ClientBase(){
    mInitialClientPid = clientPid;
    mDevice = new Camera3Device(cameraId);
}
status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
    res = Camera2ClientBase::initialize(providerPtr, monitorTags);
    mFrameProcessor = new FrameProcessorBase(mDevice);
    mFrameProcessor->run(threadName.string());
    mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
                                      FRAME_PROCESSOR_LISTENER_MAX_ID,
                                      /*listener*/this,
                                      /*sendPartials*/true);
}
frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.cpp
status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
        const String8& monitorTags) {
    return initializeImpl(manager, monitorTags);
}
status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
        const String8& monitorTags) {
    status_t res;
    res = TClientBase::startCameraOps();
    //初始化Camera3Device
    res = mDevice->initialize(providerPtr, monitorTags);
    wp<CameraDeviceBase::NotificationListener> weakThis(this);
    res = mDevice->setNotifyCallback(weakThis);
    return OK;
}

接下来分析Camera3Device的初始化,Camera3Device通过CameraService保存的CameraProviderManager对象获得mProviders,调用流程mProviders->DeviceInfo->CameraDevice3Impl->CameraDevice3SessionImpl,最终获得CameraDevice3SessionImpl对象,其中CameraDevice3Impl和CameraDevice3SessionImpl由厂商来实现。

alps/frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp
status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
    sp<ICameraDeviceSession> session;
    //通过CameraProviderManager打开对应的会话,this为ICameraDeviceCallback类型
    status_t res = manager->openSession(mId.string(), this,
            /*out*/ &session);
    //获得请求队列
   
std::shared_ptr<RequestMetadataQueue> queue;
    auto requestQueueRet = session->getCaptureRequestMetadataQueue(
        [&queue](const auto& descriptor) {
            queue = std::make_shared<RequestMetadataQueue>(descriptor);
            if (!queue->isValid() || queue->availableToWrite() <= 0) {
                queue = nullptr;
            }
        });
   //获得结果队列,最终保存在mResultMetadataQueue对象中。
   
std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
    auto resultQueueRet = session->getCaptureResultMetadataQueue(
        [&resQueue](const auto& descriptor) {
            resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
        });
    //封装CameraDevice3SessionImpl对象,包括请求队列
    mInterface = new HalInterface(session, queue);
}
-----------------------------------------------------------------------------------------------------------------------------------
Metadata的处理
openSession的时候创建了请求和结果队列,之后会对这两个队列进行处理
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/device/CameraDevice3SessionImpl.cpp
ThisNamespace::
getCaptureRequestMetadataQueue(getCaptureRequestMetadataQueue_cb _hidl_cb){
    auto pAppStreamManager = getSafeAppStreamManager();
    _hidl_cb(pAppStreamManager->getCaptureRequestMetadataQueue());
}
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.cpp
AppStreamMgr::
getCaptureRequestMetadataQueue() -> const ::android::hardware::MQDescriptorSync<uint8_t>&{
    return *mRequestMetadataQueue->getDesc();
}

alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/device/CameraDevice3SessionImpl.cpp
ThisNamespace::
getCaptureResultMetadataQueue(getCaptureResultMetadataQueue_cb _hidl_cb){
    auto pAppStreamManager = getSafeAppStreamManager();
    _hidl_cb(pAppStreamManager->getCaptureResultMetadataQueue());
    return Void();
}
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.cpp
AppStreamMgr::
getCaptureResultMetadataQueue() -> const ::android::hardware::MQDescriptorSync<uint8_t>&{
    return *mResultMetadataQueue->getDesc();
}

AppStreamMgr初始化时会构造请求和结果的metadata队列
-----------------------------------------------------------------------------------------------------------------------------------
openSession的实现,根据id打开CameraDevice3SessionImpl
alps/frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp
status_t CameraProviderManager::openSession(const std::string &id,
        const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
        /*out*/ sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session) {
    //根据id和version找到对应的CameraDevice3Impl
    auto deviceInfo = findDeviceInfoLocked(id, /*minVersion*/ {3,0}, /*maxVersion*/ {4,0});

    auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo);
    //其中mInterface等于CameraDevice3Impl, cameraSession等于CameraDevice3SessionImpl
    ret = deviceInfo3->mInterface->open(callback, [&status, &session]
            (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) {
                status = s;
                *session = cameraSession;
            });
    return mapToStatusT(status);
}
根据id和version找到对应的CameraDevice3Impl
CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked(
        const std::string& id,
        hardware::hidl_version minVersion, hardware::hidl_version maxVersion) const {
    //provider = CameraProvider,包含多个CameraDevice3Impl
    for (auto& provider : mProviders) {
        for (auto& deviceInfo : provider->mDevices) { //deviceInfo = CameraDevice3Impl
            if (deviceInfo->mId == id &&
                    minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) {
                return deviceInfo.get();
            }
        }
    }
}
Android P之Camera HAL3流程分析(1)

跨进程调用CameraProviderManager->CameraDevice3Impl
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/device/CameraDevice3Impl.cpp
CameraDevice3Impl::
open(const ::android::sp<V3_2::ICameraDeviceCallback>& callback, open_cb _hidl_cb){
    //打开mSession,CameraDevice3Impl初始化时会创建mSession
    ::android::status_t status = mSession->open(V3_4::ICameraDeviceCallback::castFrom(callback));
    //返回CameraDevice3SessionImpl对象
    _hidl_cb(mapToHidlCameraStatus(status), mSession);
    return Void();
}

alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/device/CameraDevice3SessionImpl.cpp
open( const ::android::sp<V3_4::ICameraDeviceCallback>& callback ){
    onOpenLocked(callback);
}
onOpenLocked( const ::android::sp<V3_4::ICameraDeviceCallback>& callback ){
        mCameraDeviceCallback = callback;
        //构造AppStreamManager对象,相机应用通过AppStreamManager来控制Pipline,同时处理Pipline的回调
        mAppStreamManager = IAppStreamManager::create(
            IAppStreamManager::CreationInfo{
                .mInstanceId            = getInstanceId(),
                .mCameraDeviceCallback  = callback,
                .mMetadataProvider      = mStaticInfo.mMetadataProvider,//读取底层相机信息,初始化时会赋值
                .mMetadataConverter     = mStaticInfo.mMetadataConverter,//读取底层相机信息,初始化时会赋值
                .mErrorPrinter          = std::static_pointer_cast<android::Printer>(mAppStreamManagerErrorState),
                .mWarningPrinter        = std::static_pointer_cast<android::Printer>(mAppStreamManagerWarningState),
                .mDebugPrinter          = std::static_pointer_cast<android::Printer>(mAppStreamManagerDebugState),
            }
        );
    //获得PipelineModelManager
    auto pPipelineModelMgr = IPipelineModelManager::get();
    //获得PipelineModel
    auto pPipelineModel = pPipelineModelMgr->getPipelineModel( getInstanceId() );
    //打开PipelineModel
    pPipelineModel->open(getInstanceName().c_str(), this);
    //保存PipelineModel
    mPipelineModel = pPipelineModel;
    return OK;
}
-----------------------------------------------------------------------------------------------------------------------------------
创建AppStreamMgr,之后进行初始化,
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.cpp
IAppStreamManager::
create(const CreationInfo& creationInfo){
    auto pInstance = new AppStreamMgr(creationInfo);
    pInstance->initialize();
    return pInstance;
}

AppStreamMgr的构造函数,继承IAppStreamManager
AppStreamMgr::
AppStreamMgr(const CreationInfo& creationInfo)
    : mCommonInfo(std::make_shared<CommonInfo>())
    , mInstanceName{ std::to_string(creationInfo.mInstanceId) + ":AppStreamMgr" }
{
    IGrallocHelper* pGrallocHelper = IGrallocHelper::singleton();
    if ( mCommonInfo != nullptr ) {
        mCommonInfo->mInstanceId = creationInfo.mInstanceId;
        mCommonInfo->mErrorPrinter = creationInfo.mErrorPrinter;
        mCommonInfo->mWarningPrinter = creationInfo.mWarningPrinter;
        mCommonInfo->mDeviceCallback = creationInfo.mCameraDeviceCallback;
        mCommonInfo->mMetadataProvider = creationInfo.mMetadataProvider;
        mCommonInfo->mMetadataConverter = creationInfo.mMetadataConverter;

        mCommonInfo->mGrallocHelper = pGrallocHelper;
        mCommonInfo->mAtMostMetaStreamCount = aAtMostMetaStreamCount;
    }
}

初始化的时候创建CallbackHandler、CallbackHandler、FrameHandler、ResultHandler、RequestHandler、ConfigHandler对象,不同对象处理不同类型的事件,这样可以最大程度减小阻塞。
AppStreamMgr::
initialize(){
    //构造请求metadata
    mRequestMetadataQueue = std::make_shared<RequestMetadataQueue>(
        CAMERA_REQUEST_METADATA_QUEUE_SIZE, false /* non blocking */);
    //构造结果metadata
    mResultMetadataQueue = std::make_shared<RequestMetadataQueue>(
        CAMERA_RESULT_METADATA_QUEUE_SIZE, false /* non blocking */);
    {
        mCallbackHandler = new CallbackHandler(mCommonInfo, mResultMetadataQueue);
        const std::string threadName{std::to_string(mCommonInfo->mInstanceId)+":AppMgr-CbHdl"};
        status = mCallbackHandler->run(threadName.c_str());

        mBatchHandler = new CallbackHandler(mCommonInfo, mCallbackHandler);
        mFrameHandler = new FrameHandler(mCommonInfo, mBatchHandler);
        mResultHandler = new ResultHandler(mCommonInfo, mFrameHandler);
 
        const std::string threadName{std::to_string(mCommonInfo->mInstanceId)+":AppMgr-RstHdl"};
        status = mResultHandler->run(threadName.c_str());

        //处理请求的handler
        mRequestHandler = new RequestHandler(mCommonInfo, mRequestMetadataQueue, mFrameHandler, mBatchHandler);
        //
        mConfigHandler = new ConfigHandler(mCommonInfo, mFrameHandler, mBatchHandler);
    }
}

负责AppStreamMgr的回调
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.CallbackHandler.cpp

负责AppStreamMgr的Frame处理
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.FrameHandler.cpp

负责AppStreamMgr的Batch处理
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.BatchHandler.cpp

负责AppStreamMgr的处理请求
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.RequestHandler.cpp

负责AppStreamMgr的结果返回
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.ResultHandler.cpp

负责AppStreamMgr的Config处理
alps/vendor/mediatek/proprietary/hardware/mtkcam3/main/hal/device/3.x/app/AppStreamMgr.ConfigHandler.cpp

AppStreamMgr结构

Android P之Camera HAL3流程分析(1)

-----------------------------------------------------------------------------------------------------------------------------------

getPipelineModel的实现
alps/vendor/mediatek/proprietary/hardware/mtkcam3/pipeline/model/PipelineModelManagerImpl.cpp
PipelineModelManagerImpl::
getPipelineModel( int32_t const openId __unused ){
    auto pPipelineModel = pPipelineModelInfo->mPipelineModel =
                            PipelineModelImpl::createInstance(
                                PipelineModelImpl::CreationParams{
                                    .openId         = openId
                            });
    return pPipelineModelInfo->mPipelineModel;
}
alps/vendor/mediatek/proprietary/hardware/mtkcam3/pipeline/model/PipelineModelImpl.cpp
PipelineModelImpl::
createInstance(CreationParams const& creationParams){
    //使用libmtkcam_modulefactory_utils.so
    sp<PipelineModelImpl> pPipeline = new PipelineModelImpl(creationParams);
    //初始化PipelineModelImpl
    pPipeline->init();
    return pPipeline;
}
PipelineModelImpl::
open( std::string const& userName, android::wp<IPipelineModelCallback> const& callback ){
    //创建线程初始化驱动
    mvOpenFutures.push_back(
        std::async(std::launch::async,
            [this]() {
                return CC_LIKELY( mHalDeviceAdapter!=nullptr )
                    && CC_LIKELY( mHalDeviceAdapter->open() ) //使用libmtkcam_modulefactory_drv.so
                    && CC_LIKELY( mHalDeviceAdapter->powerOn() );
            }
        )
    );
}
PipelineModelImpl::
init(){
    //从camera驱动中读取信息
    initPipelineStaticInfo();
}
HalInterface的实现
Camera3Device::HalInterface::HalInterface(
            sp<ICameraDeviceSession> &session,
            std::shared_ptr<RequestMetadataQueue> queue) :
        mHidlSession(session),
        mRequestMetadataQueue(queue) {
    auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
    if (castResult_3_4.isOk()) {
        mHidlSession_3_4 = castResult_3_4;
    }
    auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
    if (castResult_3_3.isOk()) {
        mHidlSession_3_3 = castResult_3_3;
    }
}

经过以上步骤,打开设备CameraDevice3Impl之后,返回CameraDevice3SessionImpl,控制流程CameraDeviceImpl->CameraDeviceClient ->Camera3Device->HalInterface->CameraDevice3SessionImpl->(PipelineModel AppStreamManager)

Android P之Camera HAL3流程分析(1)

AppStreamMgr和PipelineModel在HAL3架构中的位置

其中Device HALv3等于CameraDevice3SessionImpl

Android P之Camera HAL3流程分析(1)

 

负责和驱动交互
alps/vendor/mediatek/proprietary/hardware/mtkcam3/pipeline/model/adapter/HalDeviceAdapter.cpp
IHalDeviceAdapter::
create(int32_t id) -> android::sp<IHalDeviceAdapter>
{
    android::sp<HalDeviceAdapter> p = new HalDeviceAdapter(id);
    p->init();
    return p;
}
init() {
    auto pHalDeviceList = MAKE_HalLogicalDeviceList();
    mvPhySensorId = pHalDeviceList->getSensorId(mId);
}

alps/vendor/mediatek/proprietary/hardware/mtkcam/include/mtkcam/utils/LogicalCam/IHalLogicalDeviceList.h
typedef NSCam::IHalLogicalDeviceList* (*HalLogicalDeviceList_FACTORY_T)();
 
#define MAKE_HalLogicalDeviceList(...) \
    MAKE_MTKCAM_MODULE(MTKCAM_MODULE_ID_UTILS_LOGICALDEV, HalLogicalDeviceList_FACTORY_T, __VA_ARGS__)


公共接口
alps/vendor/mediatek/proprietary/hardware/mtkcam/include/mtkcam/utils/module/module.h
#define MAKE_MTKCAM_MODULE(_module_id_, _module_factory_type_, ...) \
    ({void* factory = getMtkcamModuleFactory(_module_id_); (factory ? (((_module_factory_type_)factory)(__VA_ARGS__)) : NULL);})

alps/vendor/mediatek/proprietary/hardware/mtkcam/utils/module/helper.cpp
getMtkcamModuleFactory(uint32_t module_id){
    switch  (MTKCAM_GET_MODULE_GROUP_ID(module_id))
    {
    //case的具体实现,加载动态库,返回
    #define CASE(_group_id_, _group_shared_lib_, _group_factory_) \
        case _group_id_:{ \
            static MyHolder singleton(_group_shared_lib_, _group_factory_); \
            if  ( auto factory = singleton.get() ) { \
                return factory(module_id); \
            } \
        }break

    CASE(MTKCAM_MODULE_GROUP_ID_DRV,
        "libmtkcam_modulefactory_drv.so",
        "MtkCam_getModuleFactory_drv");

    CASE(MTKCAM_MODULE_GROUP_ID_AAA,
        "libmtkcam_modulefactory_aaa.so",
        "MtkCam_getModuleFactory_aaa");

    CASE(MTKCAM_MODULE_GROUP_ID_FEATURE,
        "libmtkcam_modulefactory_feature.so",
        "MtkCam_getModuleFactory_feature");

    CASE(MTKCAM_MODULE_GROUP_ID_CUSTOM,
        "libmtkcam_modulefactory_custom.so",
        "MtkCam_getModuleFactory_custom");

    CASE(MTKCAM_MODULE_GROUP_ID_UTILS,
        "libmtkcam_modulefactory_utils.so",
        "MtkCam_getModuleFactory_utils");

    return NULL;
}
MTKCAM_GET_MODULE_GROUP_ID的实现,多个module组合成一个group,对应一个动态库。
#define MTKCAM_GET_MODULE_GROUP_ID(module_id)   (0xFFFF & (module_id >> 16))
enum
{
    MTKCAM_MODULE_GROUP_ID_DRV,//驱动
    MTKCAM_MODULE_GROUP_ID_AAA,//3A
    MTKCAM_MODULE_GROUP_ID_FEATURE,//美颜/HDR等特效
    MTKCAM_MODULE_GROUP_ID_CUSTOM,//第三方特效
    MTKCAM_MODULE_GROUP_ID_UTILS,//工具类
};
/**
 * mtkcam module id
 *
 * |     32     |      32      |
 * |  group id  | module index |
 */
enum
{
    MTKCAM_MODULE_ID_UTILS_START        = MTKCAM_MODULE_GROUP_ID_UTILS << 16,
    MTKCAM_MODULE_ID_UTILS_LOGICALDEV,
    //--------------------------------------------------------------------------
    MTKCAM_MODULE_ID_UTILS_END,
}
alps/vendor/mediatek/proprietary/hardware/mtkcam/utils/module/helper.cpp
//szTargetLibPath等于libmtkcam_modulefactory_utils.so,szTargetSymbolName等于 MtkCam_getModuleFactory_utils
MY_T mTarget_ctor;   //from constructor
MyHolder(char const* szTargetLibPath, char const* szTargetSymbolName)
    : mTarget_ctor(nullptr)
    , mTarget_atomic()
    , mLibrary(nullptr)
    , mTargetLibPath(szTargetLibPath)
    , mTargetSymbolName(szTargetSymbolName){
    load(mLibrary, mTarget_ctor);
}
void load(void*& rpLib, MY_T& rTarget){
    void* pfnEntry = nullptr;
    void* lib = ::dlopen(mTargetLibPath, RTLD_NOW);
    pfnEntry = ::dlsym(lib, mTargetSymbolName);//mTargetSymbolName等于 MtkCam_getModuleFactory_utils
    rpLib = lib;
    rTarget = reinterpret_cast<MY_T>(pfnEntry);//类型转换MY_T
    return;
}
//最终返回MtkCam_getModuleFactory_utils函数
MY_T get(){
    if  ( mTarget_ctor ) {
        return mTarget_ctor;
    }
}


MtkCam_getModuleFactory_utils函数的实现如下:

其中MTKCAM_GET_MODULE_FACTORY在编译时会替换成实际值
alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/utils/Android.mk
MTKCAM_GET_MODULE_FACTORY="MtkCam_getModuleFactory_utils"

根据moduleId加载动态库中的model,因为一个动态库中包含多个module
alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/store.cpp
void* MTKCAM_GET_MODULE_FACTORY(unsigned int moduleId)
{
    return Store::get_module_factory(moduleId);
}
get_module_factory(unsigned int module_id){
    mtkcam_module_info const* info = get_module_info(module_id);
    return info->module_factory;
}
get_module_info(unsigned int module_id){
    struct ModuleStore{
        mtkcam_module_info  table[MODULE_GROUP_COUNT];
        ModuleStore(){
            for (auto i = 0; i < MODULE_GROUP_COUNT; i++) {
                table[i].module_id = 0;
                table[i].module_factory = nullptr;
                table[i].register_name = nullptr;
            }
         }
    };
    static ModuleStore store;
    return &store.table[MTKCAM_GET_MODULE_INDEX(module_id)];
}

注册module
动态库中会包含多个model,使用moduleId进行区分
alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/drv/register_HalSensor.cpp
REGISTER_MTKCAM_MODULE(MTKCAM_MODULE_ID_DRV_HAL_SENSORLIST, NSCam::IHalSensorList::get);

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/store.cpp
注册动态库,执行auto_register_mtkcam_module_##_module_id_的构造函数
#define REGISTER_MTKCAM_MODULE(_module_id_, _factory_) \
    namespace \
    { \
        struct auto_register_mtkcam_module_##_module_id_ \
        { \
            auto_register_mtkcam_module_##_module_id_() \
            { \
                mtkcam_module_info const info = { \
                    .module_id      = _module_id_, \
                    .module_factory = reinterpret_cast<void*>(_factory_), \
                    .register_name  = __BASE_FILE__, \
                }; \
                register_mtkcam_module(&info, NSCam::Int2Type<MTKCAM_GET_MODULE_GROUP_ID(_module_id_)>()); \
            } \
        }; \
        static const auto_register_mtkcam_module_##_module_id_ singleton; \
    }

void register_mtkcam_module(mtkcam_module_info const* info, NSCam::Int2Type<MTKCAM_MODULE_GROUP_ID>){
    Store::register_module(info);
}
register_module(mtkcam_module_info const* info){
    *get_module_info(info->module_id) = *info;
}
get_module_info(unsigned int module_id){
    struct ModuleStore{
        mtkcam_module_info  table[MODULE_GROUP_COUNT];
        ModuleStore(){
            for (auto i = 0; i < MODULE_GROUP_COUNT; i++) {
                table[i].module_id = 0;
                table[i].module_factory = nullptr;
                table[i].register_name = nullptr;
            }
        }
    };
    static ModuleStore store;
    return &store.table[MTKCAM_GET_MODULE_INDEX(module_id)];
}

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/aaa/
libmtkcam_modulefactory_aaa.so

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/custom/
libmtkcam_modulefactory_custom.so

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/drv/
libmtkcam_modulefactory_drv.so

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/feature/
libmtkcam_modulefactory_feature.so

alps/vendor/mediatek/proprietary/hardware/mtkcam/main/core/module/utils/
libmtkcam_modulefactory_utils.so

 

 

为什么要用std::async代替线程的创建

std::async是为了让用户的少费点脑子的,它让这三个对象默契的工作。大概的工作过程是这样的:std::async先将异步操作用std::packaged_task包装起来,然后将异步操作的结果放到std::promise中,这个过程就是创造未来的过程。外面再通过future.get/wait来获取这个未来的结果,怎么样,std::async真的是来帮忙的吧,你不用再想到底该怎么用std::future、std::promise和std::packaged_task了,std::async已经帮你搞定一切了!

现在来看看std::async的原型async(std::launch::async | std::launch::deferred, f, args...),第一个参数是线程的创建策略,有两种策略,默认的策略是立即创建线程:

    std::launch::async:在调用async就开始创建线程。
    std::launch::deferred:延迟加载方式创建线程。调用async时不创建线程,直到调用了future的get或者wait时才创建线程。

第二个参数是线程函数,第三个参数是线程函数的参数。
https://www.cnblogs.com/qicosmos/p/3534211.html
https://www.cnblogs.com/diegodu/p/6737973.html