Android8.0 Camera系统架构(一)

随着Android系统的不断升级,相机子系统框架也在不断进化,由最初的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下最新的相机架构具有更高的灵活性。架构如下:

Android8.0 Camera系统架构(一)_第1张图片

重新设计 Android Camera API 的目的在于大幅提高应用对于 Android 设备上的相机子系统的控制能力,同时重新组织 API,提高其效率和可维护性。借助额外的控制能力,您可以更轻松地在 Android 设备上构建高品质的相机应用,这些应用可在多种产品上稳定运行,同时仍会尽可能使用设备专用算法来最大限度地提升质量和性能。版本 3 相机子系统将多个运行模式整合为一个统一的视图,您可以使用这种视图实现之前的任何模式以及一些其他模式,例如连拍模式。这样一来,便可以提高用户对聚焦、曝光以及更多后期处理(例如降噪、对比度和锐化)效果的控制能力。此外,这种简化的视图还能够使应用开发者更轻松地使用相机的各种功能。架构图已经很清晰的描述了各层架构之间的关系,我们按图索骥从最新的架构开始,再看完整的架构,最后我们回到应用层来看Camera子系统的设计。

1. CameraService

CameraManager与CameraService通过Binder机制,形成推拉回调

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

private static final class CameraManagerGlobal extends ICameraServiceListener.Stub {
         ......
		 public ICameraService getCameraService() {
            synchronized(mLock) {
                connectCameraServiceLocked(); //连接服务
                if (mCameraService == null) {
                    Log.e(TAG, "Camera service is unavailable");
                }
                return mCameraService;
            }
        }
        ......
}

CameraManager通过CameraManagerGlobal访问CameraService服务,并注册监听,CamreaService持有CameraServiceListener列表,并回调结果给CameraManager

private void connectCameraServiceLocked() {
        //查询服务引用
        IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
        //转换服务接口
        ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
        try {
           CameraStatus[] cameraStatuses = cameraService.addListener(this); //注册回调监听
           //存副本
           mCameraService = cameraService;
       }
}

frameworks\av\services\camera\libcameraservice\CameraService.h

class CameraService :
    public BinderService<CameraService>,
    public virtual ::android::hardware::BnCameraService, //Bn端(服务端)
    public virtual IBinder::DeathRecipient {.....}

注册CameraService回调监听

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

CameraService初始化

void CameraService::onFirstRef()
{
    BnCameraService::onFirstRef();
    res = enumerateProviders(); //枚举Provider
    CameraService::pingCameraServiceProxy();
}

调用CameraProviderManager枚举设备

status_t CameraService::enumerateProviders() {
   
    if (nullptr == mCameraProviderManager.get()) {
        mCameraProviderManager = new CameraProviderManager();
        res = mCameraProviderManager->initialize(this); //初始化
    }

    mNumberOfCameras = mCameraProviderManager->getCameraCount(); //相机数目
    mNumberOfNormalCameras =
            mCameraProviderManager->getAPI1CompatibleCameraCount(); //可用API
            
    mCameraProviderManager->setUpVendorTags(); //第三方厂商Tag

    if (nullptr == mFlashlight.get()) {
        mFlashlight = new CameraFlashlight(mCameraProviderManager, this); //闪光灯
    }
    res = mFlashlight->findFlashUnits();
    return OK;
}

2. 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( //注册代理通知
        /* instance name, empty means no filter */ "",
        this);
        return INVALID_OPERATION;
    }

    //添加提供者
    addProviderLocked(kLegacyProviderName, /*expected*/ false);
    return OK;
}

查找初始化并保存Provider; mServiceProxy是ServiceInteractionProxy*;

status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
   
    sp<provider::V2_4::ICameraProvider> interface;
    interface = mServiceProxy->getService(newProvider); //获取服务

    sp<ProviderInfo> providerInfo =
            new ProviderInfo(newProvider, interface, this);
    status_t res = providerInfo->initialize(); //执行初始化

    mProviders.push_back(providerInfo); //备份

    return OK;
}

frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.h

struct ServiceInteractionProxy {
        virtual bool registerForNotifications(
                const std::string &serviceName,
                const sp<hidl::manager::V1_0::IServiceNotification>
                &notification) = 0;
        virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
                const std::string &serviceName) = 0;
        virtual ~ServiceInteractionProxy() {}
    };

    // Standard use case - call into the normal generated static methods which invoke
    // the real hardware service manager
    struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
        virtual bool registerForNotifications(
                const std::string &serviceName,
                const sp<hidl::manager::V1_0::IServiceNotification>
                &notification) override {
            return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
                    serviceName, notification);
        }
        virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
                const std::string &serviceName) override { //调用HAL
            return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
        }
    };

3. Camera硬件抽象层

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

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

执行初始化

bool CameraProvider::initialize() {
    camera_module_t *rawModule;
    int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, //熟悉的配方,熟悉的操作
            (const hw_module_t **)&rawModule);
    mModule = new CameraModule(rawModule); //封装了一层
    err = mModule->init();
    // Setup callback now because we are going to try openLegacy next
    err = mModule->setCallbacks(this);
    mNumberOfLegacyCameras = mModule->getNumberOfCameras();
    for (int i = 0; i < mNumberOfLegacyCameras; i++) {
        struct camera_info info;
        auto rc = mModule->getCameraInfo(i, &info); //查相机信息
        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
}

hardware\interfaces\camera\common\1.0\default\CameraModule.cpp

CameraModule::CameraModule(camera_module_t *module) {
    mModule = module; //save this ref
}

做了一些版本相关处理

int CameraModule::init() {
    ATRACE_CALL();
    int res = OK;
    if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
            mModule->init != NULL) {
        ATRACE_BEGIN("camera_module->init");
        res = mModule->init(); //初始化
        ATRACE_END();
    }
    mCameraInfoMap.setCapacity(getNumberOfCameras());
    return res;
}

hardware\libhardware\include\hardware\camera_common.h
最终通过HAL与相机设备驱动交互

{
    ......
     */
    int (*init)();

    /* reserved for future use */
    void* reserved[5];
} camera_module_t;

让我们往回倒一下车,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]( //获取device
            Status idStatus,
            const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
        status = idStatus;

    sp<StatusListener> listener = mManager->getStatusListener();
    for (auto& device : devices) {
        std::string id;
        status_t res = addDevice(device, //天加device
                hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);
    }

    for (auto& device : mDevices) {
        mUniqueCameraIds.insert(device->mId);
        if (device->isAPI1Compatible()) {
            mUniqueAPI1CompatibleCameraIds.insert(device->mId);
        }
    }
    mUniqueDeviceCount = mUniqueCameraIds.size();
    return OK;
}
status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
        CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
        
    uint16_t major, minor;
    std::string type, id;

    status_t res = parseDeviceName(name, &major, &minor, &type, &id); //解析设备名
    
    if (mManager->isValidDeviceLocked(id, major)) { //验证
        return BAD_VALUE;
    }

    std::unique_ptr<DeviceInfo> deviceInfo;
    switch (major) {
        case 1:
            deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid, //Device1
                    id, minor);
            break;
        case 3:
            deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid, //Device3
                    id, minor);
            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;
}

初始化设备信息

template<class DeviceInfoT>
std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
    CameraProviderManager::ProviderInfo::initializeDeviceInfo(
        const std::string &name, const metadata_vendor_id_t tagId,
        const std::string &id, uint16_t minorVersion) const {
    Status status;

    auto cameraInterface =
            getDeviceInterface<typename DeviceInfoT::InterfaceT>(name); //获取HAL设备远程接口
    if (cameraInterface == nullptr) return nullptr;
    return std::unique_ptr<DeviceInfo>( //返回设备信息
        new DeviceInfoT(name, tagId, id, minorVersion, resourceCost,
                cameraInterface));
}

通过ICameraDevice关联硬件抽象层

template<>
sp<device::V1_0::ICameraDevice>
CameraProviderManager::ProviderInfo::getDeviceInterface
        <device::V1_0::ICameraDevice>(const std::string &name) const {
    Status status;
    sp<device::V1_0::ICameraDevice> cameraInterface;
    hardware::Return<void> ret;
    ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface](
        Status s, sp<device::V1_0::ICameraDevice> interface) {
                status = s;
                cameraInterface = interface;
            });
    return cameraInterface;
}

硬件抽象层调用接口(Treble架构)

struct CameraDevice : public ICameraDevice {......}

4. CameraDeviceClient与CameraDevice

新的架构图中右边的分支已经跟踪完毕,我们回过头来看左边的分支
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);
                } 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/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) {

    ATRACE_CALL();
    Status ret = Status::ok();
    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);

    if(!ret.isOk()) {
        logRejected(id, getCallingPid(), String8(clientPackageName),
                ret.toString8());
        return ret;
    }

    *device = client; //返回设备
    return ret;
}

模板函数

template<class CALLBACK, class CLIENT>
Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
        int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
        apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
        /*out*/sp<CLIENT>& device) {
    binder::Status ret = binder::Status::ok();

    String8 clientName8(clientPackageName);

    int originalClientPid = 0;
    sp<CLIENT> client = nullptr;
    {
        ......
        // 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;
            }
        }
        
        ......

        sp<BasicClient> tmp = nullptr;
        if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
                clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
                /*out*/&tmp)).isOk()) {
            return ret;
        }
        client = static_cast<CLIENT*>(tmp.get());
        //初始化
        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;
}

创建不同HAL版本对应的相机Client

Status CameraService::makeClient(const sp<CameraService>& cameraService,
        const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
        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) {
        // 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();
}

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) {
    ATRACE_CALL();
    status_t res;

    res = Camera2ClientBase::initialize(providerPtr); //调用初始化
    if (res != OK) {
        return res;
    }
    ......
    return OK;
}

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) {
    ATRACE_CALL();
    ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
          TClientBase::mCameraIdStr.string());
    status_t res;

    // Verify ops permissions
    res = TClientBase::startCameraOps();
    if (res != OK) {
        return res;
    }

    if (mDevice == NULL) {
        ALOGE("%s: Camera %s: No device connected",
                __FUNCTION__, TClientBase::mCameraIdStr.string());
        return NO_INIT;
    }

    res = mDevice->initialize(providerPtr); //调用具体的设备初始化
    if (res != OK) {
        ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
                __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
        return res;
    }

    wp<CameraDeviceBase::NotificationListener> weakThis(this);
    res = mDevice->setNotifyCallback(weakThis);

    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大部分重要的东西都在其驱动算法层,我们暂且不去深究。下一篇我们将从整个新旧架构的角度去分析Camera系统。

你可能感兴趣的:(Android)