Android Camera系统架构

随着Android系统的不断升级,Camera子系统框架也在不断进化
由最初的API1和HAL1到现在的API2和HAL3,由最初简单的拍照,录制到现在的连拍,AI人像;可以说是架构上变动最大最频繁的子系统。

很多设备仍然依赖相机 HAL1,因此 Android 7.0 继续支持该模块。此外,Android 相机服务还支持同时实现两种 HAL(1 和 3),
如果您希望通过相机 HAL1 支持性能略低的前置摄像头,并通过相机 HAL3 支持更为高级的后置摄像头。

Android 的相机硬件抽象层 (HAL) 可将 Camera 2 中较高级别的相机框架 API 连接到底层的相机驱动程序和硬件。
相机子系统包括相机管道组件的实现,而相机 HAL 则可提供用于实现您的这些组件版本的接口。

从 Android 8.0 开始,相机 HAL 接口是 Project Treble 的一部分,相应的 HIDL 接口在硬件/接口/相机中定义。该实现会封装仍在使用旧版 API 的旧 HAL。从 Android 8.0 开始,相机 HAL 实现必须使用 HIDL API;不支持使用旧版接口。

Android8.0下最新的相机架构具有更高的灵活性。架构如下:
Android Camera系统架构_第1张图片
重新设计 Android Camera API 的目的在于大幅提高应用对于 Android 设备上的相机子系统的控制能力,同时重新组织 API,提高其效率和可维护性。
助额外的控制能力,您可以更轻松地在 Android 设备上构建高品质的相机应用,这些应用可在多种产品上稳定运行,同时仍会尽可能使用设备专用算法来最大限度地提升质量和性能。

版本 3 相机子系统将多个运行模式整合为一个统一的视图,您可以使用这种视图实现之前的任何模式以及一些其他模式,例如连拍模式。这样一来,便可以提高用户对聚焦、曝光以及更多后期处理(例如降噪、对比度和锐化)效果的控制能力。

此外,这种简化的视图还能够使应用开发者更轻松地使用相机的各种功能。架构图已经很清晰的描述了各层架构之间的关系,我们按图索骥从最新的架构开始,再看完整的架构,最后我们回到应用层来看Camera子系统的设计。

CameraManager 与 CameraService 的调用关系

CameraManager与CameraService通过Binder机制 进行沟通的
整个实现过程如下:

  1. CameraManager 通过 CameraManagerGlobal 访问 CameraService 服务

  2. 连接 CameraService 服务

  3. 注册 cameraService 回调监听

  4. CameraService onFirstRef 初始化

  5. 调用 CameraProviderManager 枚举设备
    5.1 CameraProviderManager 初始化
    5.2 获得 Camera 相机数目
    5.3 初始化第三方厂商 Tag
    5.4 Camera Flashlight 闪光灯初始化

  6. CameraProviderManager 初始化
    6.1 查找初始化并保存Provider
    6.2 调用getService 从hal 层获取CameraProviderManager 服务
    6.3 调用CameraProvider hal 层的初始化函数 执行初始化
        6.3.1 获取Camera device list
        6.3.2 添加加camera device
            6.3.2.1 解析Camera设备名
            6.3.2.3 初始化设备信息
                6.3.2.3.1 获取HAL设备远程接口
                6.3.2.3.2 通过ICameraDevice关联硬件抽象层
               
        6.3.3 检查Camera 是否有需要兼容 API1 接口
       

    ------- 硬件抽象层 -----
    6.4 CameraProvider hardware 层 initialize 执行初始化
    6.5 通过hw_get_module 获得CameraModule
    6.6 通过rawModule 获得 CameraModule
    6.7 CameraModule 执行init 初始化
        6.71 从hal 层调用驱动层 init 函数初始化
    6.8 获得Camera 个数
    6.9 查相机信息

1. CameraManagerGlobal 调用流程

代码如下:

frameworks\base\core\java\android\hardware\camera2\CameraManager.java
frameworks\av\camera\aidl\android\hardware\ICameraService.aidl
frameworks\av\camera\aidl\android\hardware\ICameraServiceListener.aidl
/// 1. CameraManager通过CameraManagerGlobal访问CameraService服务,
private static final class CameraManagerGlobal extends ICameraServiceListener.Stub {
         ......
		 public ICameraService getCameraService() {
            synchronized(mLock) {
                connectCameraServiceLocked(); /// 2.连接CameraService服务
                	=======>
	                |	private void connectCameraServiceLocked() {
					|	        //查询服务引用
					|	        IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
					|	        //转换服务接口
					|	        ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
					|	        try {
					|	           CameraStatus[] cameraStatuses = cameraService.addListener(this); //3. 注册cameraService回调监听
					|			  //3.注册cameraService回调监听,CamreaService持有CameraServiceListener列表,并回调结果给CameraManager
					|						=======>
					|						|Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
					|						|        std::vector<hardware::CameraStatus> *cameraStatuses)
					|						|    {
					|						|        Mutex::Autolock lock(mStatusListenerLock);
					|						|        for (auto& it : mListenerList)
					|						|            if (IInterface::asBinder(it) == IInterface::asBinder(listener))
					|						|                return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
					|						|        mListenerList.push_back(listener); //注册
					|						|    }
					|						|    return Status::ok();
					|						|}
					|						<=======-=
					|	           //存副本
					|	           mCameraService = cameraService;
					|	       }
					|	}
					<=========
                if (mCameraService == null) {
                    Log.e(TAG, "Camera service is unavailable");
                }
                return mCameraService;
            }
        }
        ......
}


frameworks\av\services\camera\libcameraservice\CameraService.h
class CameraService :
    public BinderService<CameraService>,
    public virtual ::android::hardware::BnCameraService, //Bn端(服务端)
    public virtual IBinder::DeathRecipient {.....}

//###########################################################################################################

// 4. CameraService onFirstRef 初始化
void CameraService::onFirstRef()
{
    BnCameraService::onFirstRef();
    res = enumerateProviders(); //5. 调用CameraProviderManager枚举设备
		======>
		|	status_t CameraService::enumerateProviders() {
		|	    if (nullptr == mCameraProviderManager.get()) {
		|	        mCameraProviderManager = new CameraProviderManager();
		|	        res = mCameraProviderManager->initialize(this); // 5.1 CameraProviderManager 初始化
		|	    }
		|	    mNumberOfCameras = mCameraProviderManager->getCameraCount(); // 5.2 获得Camera 相机数目
		|	    mNumberOfNormalCameras = mCameraProviderManager->getAPI1CompatibleCameraCount(); //可用API
		|	            
		|	    mCameraProviderManager->setUpVendorTags(); //5.3 初始化第三方厂商Tag
		|	
		|	    if (nullptr == mFlashlight.get()) {
		|	        mFlashlight = new CameraFlashlight(mCameraProviderManager, this); //5.4 Camera Flashlight 闪光灯初始化
		|	    }
		|	    res = mFlashlight->findFlashUnits();
		|	    return OK;
		|	}
		|<=========
    CameraService::pingCameraServiceProxy();
}

//###########################################################################################################

// 6. CameraProviderManager 初始化
frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.cpp

status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
        ServiceInteractionProxy* proxy) {
    std::lock_guard<std::mutex> lock(mInterfaceMutex);
    mListener = listener;
    mServiceProxy = proxy;

    // Registering will trigger notifications for all already-known providers
    bool success = mServiceProxy->registerForNotifications( //注册代理通知, mServiceProxy是ServiceInteractionProxy*;
   		 /* instance name, empty means no filter */ "",
        this);
    	========>  //调用hal层接口 // frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.h
    	|	return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(serviceName, notification);
   		<=======
        
        return INVALID_OPERATION;
    }

    //6.1 查找初始化并保存Provider 
    addProviderLocked(kLegacyProviderName, /*expected*/ false);
		======>
		|	status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
		|	    sp<provider::V2_4::ICameraProvider> interface;
		|	    interface = mServiceProxy->getService(newProvider); //6.2 调用getService 从hal 层获取CameraProviderManager 服务
		|			========>  // frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.h
		|			|	virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
		|		    |            const std::string &serviceName) override { //调用HAL
		|		    |        return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
		|		    |    }
		|		    <=======    
		|	    sp<ProviderInfo> providerInfo = new ProviderInfo(newProvider, interface, this);
		|	    status_t res = providerInfo->initialize(); //6.3 调用CameraProvider hardware 层的初始化函数 执行初始化
		|	    mProviders.push_back(providerInfo); //备份
		|	    return OK;
		|	}
		<=======
    return OK;
}

2. CameraProvider 硬件抽象层 初始化

hardware\interfaces\camera\provider\2.4\default\CameraProvider.h

struct CameraProvider : public ICameraProvider, public camera_module_callbacks_t {......}

// 6.4 CameraProvider hardware 层 initialize 执行初始化
bool CameraProvider::initialize() {
    camera_module_t *rawModule;
    int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, 			// 6.5 通过hw_get_module 获得rawModule
            (const hw_module_t **)&rawModule);
    mModule = new CameraModule(rawModule); 		// 6.6 通过rawModule 获得 CameraModule
    	======> mModule = module; //save this ref
    	
    err = mModule->init();  					// 6.7 CameraModule 执行init 初始化
    	======>
		|	int CameraModule::init() {
		|	    if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&  mModule->init != NULL) {
		|				// hardware\libhardware\include\hardware\camera_common.h
		|	        res = mModule->init(); 		// 6.71 从hal 层调用驱动层 init 函数初始化  
		|	    }
		|	    mCameraInfoMap.setCapacity(getNumberOfCameras());
		|	}
		<=======
    // Setup callback now because we are going to try openLegacy next
    err = mModule->setCallbacks(this);
    mNumberOfLegacyCameras = mModule->getNumberOfCameras();  	// 6.8 获得Camera 个数
    for (int i = 0; i < mNumberOfLegacyCameras; i++) {
        struct camera_info info;
        auto rc = mModule->getCameraInfo(i, &info); 			// 6.9 查相机信息
        char cameraId[kMaxCameraIdLen];
        snprintf(cameraId, sizeof(cameraId), "%d", i);
        std::string cameraIdStr(cameraId);
        mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT;
        mCameraIds.add(cameraIdStr);
        ......
    }

    return false; // mInitFailed
}



3. CameraProviderManager -> ProviderInfo -> initialize() 初始化

status_t CameraProviderManager::ProviderInfo::initialize() {
    status_t res = parseProviderName(mProviderName, &mType, &mId);
    hardware::Return<Status> status = mInterface->setCallback(this);
    hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId);
    //初始化相机设备
    // Get initial list of camera devices, if any
    std::vector<std::string> devices;
    hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices]( // 6.3.1 获取Camera device list
            Status idStatus,
            const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
        status = idStatus;

    sp<StatusListener> listener = mManager->getStatusListener();
    for (auto& device : devices) {
        std::string id;
        // // 6.3.2 添加加camera device
        status_t res = addDevice(device, hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);  
        ==========================>
        |	status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
		|	        CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
		|	        
		|	    uint16_t major, minor;
		|	    status_t res = parseDeviceName(name, &major, &minor, &type, &id); // 6.3.2.1 解析Camera设备名
		|	    if (mManager->isValidDeviceLocked(id, major)) // 6.3.2.2 验证Camera 是否正常
		|	        return BAD_VALUE;
		|	
		|	    std::unique_ptr<DeviceInfo> deviceInfo; 
		|	    switch (major) {    // 6.3.2.3 初始化设备信息
		|	        case 1:
		|	            deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid, id, minor);//Device1
		|	            break;
		|	        case 3:
		|	            deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid, id, minor);//Device3    
		|					==========================>
		|					|	auto cameraInterface = getDeviceInterface<typename DeviceInfoT::InterfaceT>(name); // 6.3.2.3.1 获取HAL设备远程接口	
		|					|		==========================>  // 6.3.2.3.2 通过ICameraDevice关联硬件抽象层
		|					|		|	sp<device::V1_0::ICameraDevice> cameraInterface;
		|					|		|    ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface](
		|					|		|        Status s, sp<device::V1_0::ICameraDevice> interface) 
							|		|			{status = s; cameraInterface = interface;});
		|					|		|    return cameraInterface;
		|					|		|<==========================	
		|				   	| 	if (cameraInterface == nullptr) return nullptr;
		|				    |	//返回设备信息
		|				    |	return std::unique_ptr<DeviceInfo>( 
		|				    |   	new DeviceInfoT(name, tagId, id, minorVersion, resourceCost, cameraInterface));
		|				    <==========================
		|	            break;
		|	        default:
		|	            return BAD_VALUE;
		|	    }
		|	    if (deviceInfo == nullptr) return BAD_VALUE;
		|	    deviceInfo->mStatus = initialStatus; //回调设置
		|	    mDevices.push_back(std::move(deviceInfo)); //存储引用
		|	    if (parsedId != nullptr) {
		|	        *parsedId = id;
		|	    }
		|	    return OK;
		|	}
        <====================== 
    }	

    for (auto& device : mDevices) {
        mUniqueCameraIds.insert(device->mId);
        if (device->isAPI1Compatible()) {   // 6.3.3 检查Camera 是否有需要兼容 API1 接口
            mUniqueAPI1CompatibleCameraIds.insert(device->mId);
        }
    }
    mUniqueDeviceCount = mUniqueCameraIds.size();
    return OK;
}

4. CameraDevice 和 CameraDeviceClient 的关系

新的架构图中右边的分支已经跟踪完毕,我们回过头来看左边的分支
frameworks/base/core/java/android/hardware/camera2/CameraManager.java

private CameraDevice openCameraDeviceUserAsync(String cameraId,
            CameraDevice.StateCallback callback, Handler handler, final int uid)
            throws CameraAccessException {
        CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
        CameraDevice device = null;

        synchronized (mLock) {

            ICameraDeviceUser cameraUser = null;
            //CameraDeviceImpl 为 CameraDevice抽象类的自类
            android.hardware.camera2.impl.CameraDeviceImpl deviceImpl =
                    new android.hardware.camera2.impl.CameraDeviceImpl(
                        cameraId,
                        callback,
                        handler,
                        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();
                    //连接相机Device
                    cameraUser = cameraService.connectDevice(callbacks, cameraId,mContext.getOpPackageName(), uid);
					|==========================> 
					| //frameworks/av/services/camera/libcameraservice/CameraService.cpp
					|		Status CameraService::connectDevice(
					|		        const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
					|		        const String16& cameraId,const String16& clientPackageName,int clientUid,
					|		        sp<hardware::camera2::ICameraDeviceUser>* device) 
					|		{
					|		    String8 id = String8(cameraId);
					|		    sp<CameraDeviceClient> client = nullptr;
					|		    //调用模板函数
					|		    ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
					|		            CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
					|		            clientUid, USE_CALLING_PID, API_2,
					|		            /* legacyMode */ false, /* shimUpdateOnly */ false,
					|		            /* out */client);
					|		    *device = client; //返回设备
									|==========================> 
										template<class CALLBACK, class CLIENT>
										Status CameraService::connectHelper() {
										    String8 clientName8(clientPackageName);
										    {
										        ......
										        // Enforce client permissions and do basic sanity checks
										        //连接验证
										        if(!(ret = validateConnectLocked(cameraId, clientName8, /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) 
										            return ret;
										            
										        // Check the shim parameters after acquiring lock, if they have already been updated and
										        // we were doing a shim update, return immediately
										        if (shimUpdateOnly) {
										            auto cameraState = getCameraState(cameraId);
										            if (cameraState != nullptr) {
										                if (!cameraState->getShimParams().isEmpty()) return ret;
										            }
										        }
										        ......
										        // 创建不同版本对应的client
										        if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid, clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,/*out*/&tmp)).isOk()) 
										        {
										            return ret;
										        }
												|==========================> 
												Status CameraService::makeClient() {
											    if (halVersion < 0 || halVersion == deviceVersion) {
											        // Default path: HAL version is unspecified by caller, create CameraClient
											        // based on device version reported by the HAL.
											        switch(deviceVersion) {
											          //HAL1 的架构
											          case CAMERA_DEVICE_API_VERSION_1_0:
											            if (effectiveApiLevel == API_1) {  // Camera1 API route
											                sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
											                *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),facing, clientPid, clientUid, getpid(), legacyMode);
											            } else { // Camera2 API route
											                ALOGW("Camera using old HAL version: %d", deviceVersion);
											                return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
											                        "Camera device \"%s\" HAL version %d does not support camera2 API",
											                        cameraId.string(), deviceVersion);
											            }
											            break;
											          //HAL3的架构
											          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, cameraIdToInt(cameraId),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;
											          default:
											            // Should not be reachable
											            ALOGE("Unknown camera device HAL version: %d", deviceVersion);
											            return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,"Camera device \"%s\" has unknown HAL version %d",cameraId.string(), deviceVersion);
											        }
											    } else {
											        // A particular HAL version is requested by caller. Create CameraClient
											        // based on the requested HAL version.
											        if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
											            // Only support higher HAL version device opened as HAL1.0 device.
											            sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
											            *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),facing, clientPid, clientUid, servicePid, legacyMode);
											            
											        } else {
											            // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
											            ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
											                    " opened as HAL %x device", halVersion, deviceVersion,
											                    CAMERA_DEVICE_API_VERSION_1_0);
											            return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
											                    "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
											                    cameraId.string(), deviceVersion, halVersion);
											        }
											    }
											    return Status::ok();
												}

												|<==========================
										        client = static_cast<CLIENT*>(tmp.get());
										        //初始化    此处见 5. CameraDeviceClient与Camera3Device
										        err = client->initialize(mCameraProviderManager);
										
										        // Update shim paremeters for legacy clients
										        if (effectiveApiLevel == API_1) { //系统旧版本API1
										            // Assume we have always received a Client subclass for API1
										            sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
										            String8 rawParams = shimClient->getParameters();
										            CameraParameters params(rawParams);
										
										            auto cameraState = getCameraState(cameraId);
										            if (cameraState != nullptr) {
										                cameraState->setShimParams(params);
										            }
										        }
										    } // lock is destroyed, allow further connect calls
										    device = client;
										    return ret;
										}

									|<==========================
					|		}
					|<==========================
                } else {
                    // Use legacy camera implementation for HAL1 devices
                    //使用旧版HAL1
                    cameraUser = CameraDeviceUserShim.connectBinderShim(callbacks, id);
                }
            } catch (ServiceSpecificException e) {
               ......
            }

            // TODO: factor out callback to be non-nested, then move setter to constructor
            // For now, calling setRemoteDevice will fire initial
            // onOpened/onUnconfigured callbacks.
            // This function call may post onDisconnected and throw CAMERA_DISCONNECTED if
            // cameraUser dies during setup.
            deviceImpl.setRemoteDevice(cameraUser); //设置
            device = deviceImpl;
        }

        return device;
    }


// frameworks/av/services/camera/libcameraservice/api2/CameraDeviceClient.h

struct CameraDeviceClientBase :
         public CameraService::BasicClient,
         public hardware::camera2::BnCameraDeviceUser //CameraDeviceUser的服务端
{
    typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks; //该回调在CameraDeviceImpl

    const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
        return mRemoteCallback;
    }
    ......
};

// HAL3对应的CameraDeviceClient

class CameraDeviceClient :
        public Camera2ClientBase<CameraDeviceClientBase>,
        public camera2::FrameProcessorBase::FilteredListener
{......}

// 如此以来CameraDeviceClient就继承了CameraDeviceClientBase,间接继承了BnCameraDeviceUser

template <typename TClientBase>
class Camera2ClientBase :
        public TClientBase,
        public CameraDeviceBase::NotificationListener
{

// 持有远程mRemoteDevice的Binder对象

    public void setRemoteDevice(ICameraDeviceUser remoteDevice) throws CameraAccessException {
        synchronized(mInterfaceLock) {
            mRemoteDevice = new ICameraDeviceUserWrapper(remoteDevice); //包装

            IBinder remoteDeviceBinder = remoteDevice.asBinder();//远程Binder服务,HAL3架构下是CameraDeviceClient
            ......

            mDeviceHandler.post(mCallOnOpened);
            mDeviceHandler.post(mCallOnUnconfigured);
        }
    }
    
// 通过此远程回调将CamreaDevice与CameraDeviceClient联系起来,处理来自CameraDeviceClient的消息

public class CameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {......}

5. CameraDeviceClient与Camera3Device

回到刚才第四章的err = client->initialize(mCameraProviderManager);

status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
    return initializeImpl(manager);
}

template<typename TProviderPtr>
status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {

    res = Camera2ClientBase::initialize(providerPtr); //调用初始化
		|==========================>  
		| 	frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.cpp
		|	template <typename TClientBase>
		|	status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager) {
		|	    return initializeImpl(manager);
		|	}
		|	
		|	template <typename TClientBase>
		|	template <typename TProviderPtr>
		|	status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr) {
		|	    ALOGV("%s: Initializing client for camera %s", __FUNCTION__, TClientBase::mCameraIdStr.string());
		|	
		|	    // Verify ops permissions
		|	    res = TClientBase::startCameraOps();
		|	
		|	    if (mDevice == NULL)
		|	        ALOGE("%s: Camera %s: No device connected",__FUNCTION__, TClientBase::mCameraIdStr.string());
		|	
		|	    res = mDevice->initialize(providerPtr); //调用具体的设备初始化,   实现函数见后
		|	    if (res != OK)
		|	        ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",__FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
		|	
		|	    wp<CameraDeviceBase::NotificationListener> weakThis(this);
		|	    res = mDevice->setNotifyCallback(weakThis);
		|	    return OK;
		|	}
		|<==========================
    ......
    return OK;
}




// frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.h
sp<CameraDeviceBase>  mDevice;

// rameworks/av/services/camera/libcameraservice/device3/Camera3Device.h
class Camera3Device :
            public CameraDeviceBase, //继承了CameraDeviceBase
            virtual public hardware::camera::device::V3_2::ICameraDeviceCallback,
            private camera3_callback_ops {





// frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp
status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
    ......
    sp<ICameraDeviceSession> session;
    ATRACE_BEGIN("CameraHal::openSession");
    status_t res = manager->openSession(mId.string(), this, //通过session获取Device
            /*out*/ &session);
    res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo); //通过相机参数获取Device
    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) {
                ALOGE("HAL returns empty request metadata fmq, not use it");
                queue = nullptr;
                // don't use the queue onwards.
            }
        });

    std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
    auto resultQueueRet = session->getCaptureResultMetadataQueue(//元数据捕获结果队列
        [&resQueue](const auto& descriptor) {
            resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
            if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
                ALOGE("HAL returns empty result metadata fmq, not use it");
                resQueue = nullptr;
                // Don't use the resQueue onwards.
            }
        });
    IF_ALOGV() {
        session->interfaceChain([](
            ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
                ALOGV("Session interface chain:");
                for (auto iface : interfaceChain) {
                    ALOGV("  %s", iface.c_str());
                }
            });
    }

    mInterface = new HalInterface(session, queue); //新建硬件抽象层接口实例
    std::string providerType;
    mVendorTagId = manager->getProviderTagIdLocked(mId.string());

    return initializeCommonLocked();
}


至此新Camera架构整个蓝图构建完成,我们只是按图索骥从Java层一直到HAL层,事实上Camera大部分重要的东西都在其驱动算法层,我们暂且不去深究。

本文学习自: https://blog.csdn.net/u013928208/article/details/82110174

你可能感兴趣的:(Android驱动)