本轮文章均使用sprd 12 设备进行分析
CameraService初始化于 cameraserver.rc文件
service cameraserver /system/bin/cameraserver
class main
user cameraserver
group audio camera input drmrpc
ioprio rt 4
task_profiles CameraServiceCapacity MaxPerformance
rlimit rtprio 10 10
在main_cameraservice创建时会实例一个CameraService,并会调用其中的publish方法,将该服务加入ServiceManager管理。
static status_t publish(bool allowIsolated = false,
int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
sp<IServiceManager> sm(defaultServiceManager());
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,dumpFlags);
}
在获取CameraService引用后会触发onFirstRef方法
BnCameraService::onFirstRef();
// Update battery life tracking if service is restarting
BatteryNotifier& notifier(BatteryNotifier::getInstance());
notifier.noteResetCamera();
notifier.noteResetFlashlight();
status_t res = INVALID_OPERATION;
res = enumerateProviders();
if (res == OK) {
mInitialized = true;
}
mUidPolicy = new UidPolicy(this);
mUidPolicy->registerSelf();
mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
mSensorPrivacyPolicy->registerSelf();
mInjectionStatusListener = new InjectionStatusListener(this);
mAppOps.setCameraAudioRestriction(mAudioRestriction);
sp<HidlCameraService> hcs = HidlCameraService::getInstance(this);
继续跟踪enumerateProviders()方法
if (nullptr == mCameraProviderManager.get()) {
mCameraProviderManager = new CameraProviderManager();
//CameraProviderManager初始化
res = mCameraProviderManager->initialize(this);
if (res != OK) {
ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
__FUNCTION__, strerror(-res), res);
logServiceError(String8::format("Unable to initialize camera provider manager"),
ERROR_DISCONNECTED);
return res;
}
}
mCameraProviderManager->setUpVendorTags();
if (nullptr == mFlashlight.get()) {
mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
}
//初始化闪光灯
res = mFlashlight->findFlashUnits();
if (res != OK) {
ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
}
//获取相机列表id
deviceIds = mCameraProviderManager->getCameraDeviceIds();
...
for (auto& cameraId : deviceIds) {
String8 id8 = String8(cameraId.c_str());
if (getCameraState(id8) == nullptr) {
//初始化设备状态
onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
}
}
比较关键的地方是
mCameraProviderManager->initialize(this);
CameraProviderManager初始化时会实例一个CameraProvider,并通过
addProviderLocked(instance)和Service绑定
//在此注册CameraProvider
bool success = mServiceProxy->registerForNotifications(
/* instance name, empty means no filter */ "",
this);
if (!success) {
ALOGE("%s: Unable to register with hardware service manager for notifications "
"about camera providers", __FUNCTION__);
return INVALID_OPERATION;
}
for (const auto& instance : mServiceProxy->listServices()) {
将CameraProvider添加进来
this->addProviderLocked(instance);
}
执行mServiceProxy->registerForNotifications时会去实例化一个CameraProvider(具体怎么转变过去的还不知道怎么分析)
LegacyCameraProviderImpl_2_4::LegacyCameraProviderImpl_2_4() :
camera_module_callbacks_t({sCameraDeviceStatusChange,
sTorchModeStatusChange}) {
mInitFailed = initialize();
}
camera_module_t *rawModule;
int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t **)&rawModule);
if (err < 0) {
ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
return true;
}
mModule = new CameraModule(rawModule);
err = mModule->init();
if (err != OK) {
ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err));
mModule.clear();
return true;
}
initialize中需要关注camera_module_t 这个结构体的定义位置,以及这个CameraModule
camera_module_t赋值的地方
camera_module_t HAL_MODULE_INFO_SYM = {
.common =
{
.tag = HARDWARE_MODULE_TAG,
.module_api_version = CAMERA_MODULE_API_VERSION_2_5,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "Sprd Camera HAL3",
.author = "Spreadtrum Corporation",
.methods = &methods,
.dso = NULL,
},
.get_number_of_cameras =
sprdcamera::SprdCamera3Factory::get_number_of_cameras,
.get_camera_info = sprdcamera::SprdCamera3Factory::get_camera_info,
.set_callbacks = sprdcamera::SprdCamera3Factory::set_callbacks,
.get_vendor_tag_ops = sprdcamera::SprdCamera3Factory::get_vendor_tag_ops,
.open_legacy = sprdcamera::SprdCamera3Factory::open_legacy,
.set_torch_mode = sprdcamera::SprdCamera3Factory::set_torch_mode,
.init = sprdcamera::SprdCamera3Factory::init,
.get_physical_camera_info =
sprdcamera::SprdCamera3Factory::get_physical_camera_info,
.is_stream_combination_supported =
sprdcamera::SprdCamera3Factory::is_stream_combination_supported,
.notify_device_state_change =
sprdcamera::SprdCamera3Factory::notify_device_state_change,
};
可以看到
CameraModule实际上就是对SprdCamera3Factory SprdCameraImpl的一层封装。