Camera流程之OpenCamera

Camera流程之OpenCamera,第1张

Camera流程之OpenCamera

APP 到 framework 阶段

CameraManager.java

manager.openCamera(cameraId, callback, handler);

openCamera(@NonNull String cameraId,
            @NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)
openCameraForUid(cameraId, callback, CameraDeviceImpl.checkAndWrapHandler(handler),
                USE_CALLING_UID);
openCameraForUid(cameraId, callback, executor, clientUid, 0);

openCameraDeviceUserAsync(cameraId, callback, executor, clientUid, oomScoreOffset){
    
    ICameraDeviceUser cameraUser = null;
    
    android.hardware.camera2.implCameraDeviceImpl deviceImpl = new     android.hardware.camera2.impl.CameraDeviceImpl(
        cameraId, callback, executor, characteristics, physicalIdsToChars, mContext.getApplicationInfo().targetSdkVersion, mContext)
    
    ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
    

    //这一步开始调用到C层
    cameraUser = cameraService.connectDevice(callbacks, cameraId, mContext.getOpPackageName(), mContext.getAttributionTag(), uid, oomScoreOffset, mContext.getApplicationInfo().targetSdkVersion)
    
    deviceImpl.setRemoteDevice(cameraUser)
    return deviceImpl
};

CaneraService.cpp

Status CameraService::connectDevice(const sp& cameraCb, const String16& cameraId, const String16& clientPackageName, int clientUid){
    sp client = nullptr;
    ret = connectHelper(cameraCb, id, -1, CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,  false,  false,  client);
}

Status CameraService::connectHelper(const sp& cameraCb, const String8& cameraId, int api1CameraId, int halVersion, const String16& clientPackageName, int clientUid, int clientPid, apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,  sp& device){
        
    ALOGI("CameraService::connect call (PID %d "%s", camera ID %s) for HAL version %s and Camera API version %d", clientPid, clientName8.string(), cameraId.string(),(halVersion == -1) ? "default" : std::to_string(halVersion).c_str(), static_cast(effectiveApiLevel));
    
    sp tmp = nullptr;
    if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, api1CameraId, facing, clientPid, clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel, &tmp)).isOk()){
        return ret;
    }
}

Status CameraService::makeClient(const sp& cameraService, const sp& cameraCb, const String16& packageName, const String8& cameraId, bool legacyMode, int halVersion, int deviceVersion, apiLevel effectiveApiLevel, sp* 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) {
          case CAMERA_DEVICE_API_VERSION_1_0:
            if (effectiveApiLevel == API_1) {  // Camera1 API route
                sp tmp = static_cast(cameraCb.get());
                 
                *client = new CameraClient(cameraService, tmp, packageName,
                        api1CameraId, 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;
          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:
            //根据使用的CameraApi的不同调用不同的方法创建client对象
            if (effectiveApiLevel == API_1) { // Camera1 API route
                sp tmp = static_cast(cameraCb.get());
                *client = new Camera2Client(cameraService, tmp, packageName,
                        cameraId, api1CameraId,
                        facing, clientPid, clientUid,
                        servicePid, legacyMode);
            } else { // Camera2 API route
                sp tmp =
                        static_cast(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 tmp = static_cast(cameraCb.get());
            *client = new CameraClient(cameraService, tmp, packageName,
                    api1CameraId, 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();
}

到了这一块就需要看 Camera2Client() 和 CameraDeviceClient() 这两个方法了

  1. Camera2Client.cpp

    status_t Camera2Client::initialize(sp manager, const String& monitorTags){
        return initializeImpl(manager, monitorTags);
    }
    ​
    status_t Camera2Client::initialize(TProviderPtr providerPtr, const String& monitorTags){
        status_t res;
        res = Camera2Clientbase::initialize(providerPtr, monitorTags);
        
        //对创建一些会使用到的Thread
        mStreamingProcessor = new StreamingProcessor(this);
        mframeProcessor = new frameProcessor(mDevice, this);
        mCaptureSequencer = new CaptureSequencer(this);
        mJpegProcessor = new JpegProcessor(this, mCaptureSequencer);
        mZslProcessor = new ZslProcessor(this, mCaptureSequencer);
        mCallbackProcessor = new CallbackProcessor(this);
        
        xxxProcessor-> run(threadName.string)
    }

  2. Camera2Clientbase.cpp

    //构造函数
    template 
    Camera2Client::Camera2Clientbase(....){
        mDevice = new Camera3Device(cameraId);
    }
    ​
    status_t Camera2Clientbase::initialize(sp manager,
            const String8& monitorTags) {
        return initializeImpl(manager, monitorTags);
    }
    ​
    status_t Camera2Clientbase::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags){
        ...
        //调用 Camera3Device 的 initialize 方法
        res = mDevice -> initialize(providerPtr, monitorTags);
    }

  3. Camera3Device.cpp

    status_t Camera3Device::initialize(sp manager, const String8& monitorTags){
        ...
        sp session;
        //打开 session
        status_t res = manager -> openSession(mId.stringg(), this,  &session);
        res = manager -> getCameraCharacteristics(mId.string(), &mDeviceInfo);
        ...
        // 获取 requestQueue
        auto requestQueueRet = session->getCaptureRequestmetadataQueue(
            [&queue](const auto& descriptor) {
                queue = std::make_shared(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.
                }
            });
        ...
        // 获取 resultQueue
        auto resultQueueRet = session->getCaptureResultmetadataQueue(
            [&resQueue](const auto& descriptor) {
                resQueue = std::make_unique(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.
                }
            });
    }

到这一步就可以说进入到 HAL 处理的阶段了,这一部分还没有学习后面我们继续分析 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4668309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存