CameraService初始化

CameraService运行在MediaServer进程,在MediaServer进程的入口函数main()中调用CameraService::instantiate()进行实例化,

相关代码在main_mediaserver.cpp中.CameraService是BinderService的派生类,而instantiate()函数在BinderService中定义,如下:

template
class BinderService
{
public:
    static status_t publish(bool allowIsolated = false) {
        sp sm(defaultServiceManager());
        return sm->addService(
                String16(SERVICE::getServiceName()),
                new SERVICE(), allowIsolated);
    }

    static void publishAndJoinThreadPool(bool allowIsolated = false) {
        publish(allowIsolated);
        joinThreadPool();
    }

    static void instantiate() { publish(); }

    static status_t shutdown() { return NO_ERROR; }

private:
    static void joinThreadPool() {
        sp ps(ProcessState::self());
        ps->startThreadPool();
        ps->giveThreadPoolName();
        IPCThreadState::self()->joinThreadPool();
    }
};

BinderService是一个模板类,它的参数是Service类即IBinder的派生类,由上述代码可以了解到,instantiate()函数有两个作用:(1) 实例化Service类;(2) 注册Service类到ServiceManager中.

CameraService的超级父类是RefBase,因此在实例化的时候会调用onFirstRef()函数:

void CameraService::onFirstRef()
{
    LOG1("CameraService::onFirstRef");

    BnCameraService::onFirstRef();

    if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
                (const hw_module_t **)&mModule) < 0) {
        ALOGE("Could not load camera HAL module");
        mNumberOfCameras = 0;
    }
    else {
        ALOGI("Loaded \"%s\" camera module", mModule->common.name);
        mNumberOfCameras = mModule->get_number_of_cameras();
        if (mNumberOfCameras > MAX_CAMERAS) {
            ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
                    mNumberOfCameras, MAX_CAMERAS);
            mNumberOfCameras = MAX_CAMERAS;
        }
        for (int i = 0; i < mNumberOfCameras; i++) {
            setCameraFree(i);
        }

        if (mModule->common.module_api_version >=
                CAMERA_MODULE_API_VERSION_2_1) {
            mModule->set_callbacks(this);
        }

        VendorTagDescriptor::clearGlobalVendorTagDescriptor();

        if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) {
            setUpVendorTags();
        }

        CameraDeviceFactory::registerService(this);
    }
}

onFirstRef()的作用有以下几条:

(1) hw_get_module()加载hardware库并获取hw_module_t模块mModule;

(2) 通过mModule的方法get_number_of_cameras()得到系统Camera设备的个数,并设置每个设备的空闲标志;

(3) 当camera module 版本大于等于2.1时,设置设备状态改变回调接口

(4) 当camera module 版本大于等于2.2时,设置vendor标志

(5) 注入CameraService对象到CameraServiceFactory中

你可能感兴趣的:(android之camera)