下面分析下CAMERA的开机过程:
开机过程重要做了下面几件事情:
1, camera service的注册。
2, 统计有效CAMERA的信息
STEP1:
\frameworks\av\services\camera\libcameraservice\CameraService.cpp
void CameraService::onFirstRef()
{
ALOGI("CameraService process starting");
BnCameraService::onFirstRef();
// Update battery life tracking if service is restarting
BatteryNotifier& notifier(BatteryNotifier::getInstance());
notifier.noteResetCamera();
notifier.noteResetFlashlight();
camera_module_t *rawModule;
int err =hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t**)&rawModule);
/*
主要的事情都是在hw_get_module里面做的。会像HAL去搜索有效的SENSOR,并且获取SENSOR的INFO。
*/
if (err < 0) {
ALOGE("Could not load camera HAL module: %d (%s)", err,strerror(-err));
logServiceError("Could not load camera HAL module", err);
mNumberOfCameras = 0;
return;
}
mModule = new CameraModule(rawModule);
ALOGI("Loaded \"%s\" camera module",mModule->getModuleName());
err = mModule->init();
if (err != OK) {
ALOGE("Could not initialize camera HAL module: %d (%s)", err,
strerror(-err));
logServiceError("Could not initialize camera HAL module",err);
mNumberOfCameras = 0;
delete mModule;
mModule = nullptr;
return;
}
mNumberOfCameras = mModule->getNumberOfCameras();
mNumberOfNormalCameras = mNumberOfCameras;
/*
前面的参数已经获取到了,这里只是返回前面获取的参数而已。
*/
mFlashlight = new CameraFlashlight(*mModule, *this);
status_t res = mFlashlight->findFlashUnits();
if (res) {
// impossible because we haven't open any camera devices.
ALOGE("Failed to find flash units.");
}
int latestStrangeCameraId = INT_MAX;
for (int i = 0; i < mNumberOfCameras; i++) {
String8 cameraId = String8::format("%d", i);
// Get camera info
struct camera_info info;
bool haveInfo = true;
status_t rc = mModule->getCameraInfo(i, &info);
if (rc != NO_ERROR) {
ALOGE("%s: Received error loading camera info for device %d, costand"
" conflicting devicesfields set to defaults for this device.",
__FUNCTION__, i);
haveInfo = false;
}
// Check for backwards-compatibility support
if (haveInfo) {
if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK){
delete mModule;
mModule = nullptr;
return;
}
}
// Defaults to use for cost and conflicting devices
int cost = 100;
char** conflicting_devices = nullptr;
size_t conflicting_devices_length = 0;
// If using post-2.4 module version, query the cost + conflictingdevices from the HAL
if (mModule->getModuleApiVersion() >=CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
cost = info.resource_cost;
conflicting_devices = info.conflicting_devices;
conflicting_devices_length = info.conflicting_devices_length;
}
std::set<String8> conflicting;
for (size_t i = 0; i < conflicting_devices_length; i++) {
conflicting.emplace(String8(conflicting_devices[i]));
}
// Initialize state for each camera device
{
Mutex::Autolock lock(mCameraStatesLock);
mCameraStates.emplace(cameraId,std::make_shared<CameraState>(cameraId, cost,
conflicting));
}
if (mFlashlight->hasFlashUnit(cameraId)) {
mTorchStatusMap.add(cameraId,
ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
}
}
if (mModule->getModuleApiVersion() >=CAMERA_MODULE_API_VERSION_2_1) {
mModule->setCallbacks(this);
}
VendorTagDescriptor::clearGlobalVendorTagDescriptor();
if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2){
setUpVendorTags();
}
CameraDeviceFactory::registerService(this);
CameraService::pingCameraServiceProxy();
}
/*hw_get_module是根据CAMERA_HARDWARE_MODULE_ID加载相应的MODELE。具体的加载过程有一篇文章有专门的描述,这里就不深入了。
*/
STEP 2:
\vendor\mediatek\proprietary\hardware\mtkcam\legacy\module_hal\module\module.h
static
camera_module
get_camera_module()
{
camera_module module = {
common:{
tag : HARDWARE_MODULE_TAG,
#if (PLATFORM_SDK_VERSION >= 21)
#if NEED_MODULE_API_VERSION_2_3
#warning"NEED_MODULE_API_VERSION_2_3 is: 1, useCAMERA_MODULE_API_VERSION_2_3"
module_api_version : CAMERA_MODULE_API_VERSION_2_3,
#else
#warning"NEED_MODULE_API_VERSION_2_3 is: 0, use defaultCAMERA_MODULE_API_VERSION_2_4"
module_api_version : CAMERA_MODULE_API_VERSION_2_4,
#endif
#else
module_api_version : CAMERA_DEVICE_API_VERSION_1_0,
#endif
hal_api_version :HARDWARE_HAL_API_VERSION,
id : CAMERA_HARDWARE_MODULE_ID,
name :"MediaTek Camera Module",
author :"MediaTek",
methods :get_module_methods(),
dso : NULL,
reserved : {0},
},
get_number_of_cameras :get_number_of_cameras,
get_camera_info :get_camera_info,
set_callbacks :set_callbacks,
get_vendor_tag_ops :get_vendor_tag_ops,
#if (PLATFORM_SDK_VERSION >= 21)
open_legacy :open_legacy,
#endif
set_torch_mode :set_torch_mode,
init :NULL,
reserved : {0},
};
return module;
};
Step 3:
\frameworks\av\services\camera\libcameraservice\common\CameraModule.cpp
//进入CameraModule的构造函数。
CameraModule::CameraModule(camera_module_t*module) {
if (module == NULL) {
ALOGE("%s: camera hardware module must not be null",__FUNCTION__);
assert(0);
}
mModule = module;
mCameraInfoMap.setCapacity(getNumberOfCameras());
}
Step 4:
int CameraModule::getNumberOfCameras() {
return mModule->get_number_of_cameras();
}
//返回到module.h中对于的get_number_of_cameras函数;
Step 5:
\vendor\mediatek\proprietary\hardware\mtkcam\legacy\module_hal\module\module.h
static
int
get_number_of_cameras(void)
{
return NSCam::getCamDeviceManager()->getNumberOfDevices();
}
/*
getCamDeviceManager类中没有实现getNumberOfDevices,具体是实现是在其父类CamDeviceManagerBase中完成的。
*/
STEP 6:
\vendor\mediatek\proprietary\hardware\mtkcam\legacy\module_hal\devicemgr\CamDeviceManagerBase.cpp
int32_t
CamDeviceManagerBase::
getNumberOfDevices()
{
RWLock::AutoWLock _l(mRWLock);
MY_LOGD("larrytest--->getNumberOfDevices#devices:%d",mi4DeviceNum);
if ( 0 != mi4DeviceNum )
{
MY_LOGD("#devices:%d", mi4DeviceNum);
}
else
{
Utils::CamProfile _profile(__FUNCTION__,"CamDeviceManagerBase");
mi4DeviceNum = enumDeviceLocked();
/*
第一次进来mi4DeviceNum为0,进入enumDeviceLocked。这个是很关键的函数,干了很多事实。
*/
_profile.print("");
}
//
return mi4DeviceNum;
}
Step 7:
\vendor\mediatek\proprietary\hardware\mtkcam\legacy\platform\mt8127\devicemgr\CamDeviceManagerImp.cpp
int32_t
CamDeviceManagerImp::
enumDeviceLocked()
{
Utils::CamProfile _profile(__FUNCTION__,"CamDeviceManagerImp");
//
status_t status = OK;
int32_t i4DeviceNum = 0;
//
#if '1'==MTKCAM_HAVE_METADATA
NSMetadataProviderManager::clear();
#endif
mEnumMap.clear();
//------------------------------------------------------------------------------
#if '1'==MTKCAM_HAVE_SENSOR_HAL
mEnumMap.clear();
DevMetaInfo::clear();
//
int32_t isFakeOrientation = 0;
int32_t i4DevSetupOrientation = 0;
camera_info camInfo;
camInfo.device_version = CAMERA_DEVICE_API_VERSION_1_0;
camInfo.static_camera_characteristics = NULL;
//
SensorHal* pSensorHal = SensorHal::createInstance();
/*
创建是的是其之类:SensorHalImp
SensorHal* SensorHal::createInstance()
{
returnSensorHalImp::getInstance();
}
*/
if ( ! pSensorHal )
{
MY_LOGE("pSensorHal == NULL");
return 0;
}
//
int32_t const iSensorsList = pSensorHal->searchSensor();
//所以也就是SensorHalImp的searchSensor
//
if ( (iSensorsList &SENSOR_DEV_MAIN_3D) == SENSOR_DEV_MAIN_3D )
{
MY_LOGI("Stereo 3D Camera found");
}
//
if ( (iSensorsList &SENSOR_DEV_MAIN) == SENSOR_DEV_MAIN )
{
int32_t const deviceId = i4DeviceNum;
#if '1'==MTKCAM_HAVE_METADATA
sp<IMetadataProvider> pMetadataProvider =IMetadataProvider::create(deviceId);
NSMetadataProviderManager::add(deviceId, pMetadataProvider.get());
#endif
//
halSensorDev_e const eHalSensorDev = SENSOR_DEV_MAIN;
pSensorHal->sendCommand(eHalSensorDev,SENSOR_CMD_GET_FAKE_ORIENTATION, (int)&isFakeOrientation);
pSensorHal->sendCommand(eHalSensorDev,SENSOR_CMD_GET_SENSOR_ORIENTATION_ANGLE, (int)&i4DevSetupOrientation);
pSensorHal->sendCommand(eHalSensorDev,SENSOR_CMD_GET_SENSOR_FACING_DIRECTION, (int)&camInfo.facing);
camInfo.orientation = i4DevSetupOrientation;
if ( isFakeOrientation )
{
camInfo.orientation = (0==camInfo.facing) ? 90 : 270;
MY_LOGW("Fake orientation:%d instead of %d, facing=%dHalSensorDev=%#x", camInfo.orientation, i4DevSetupOrientation,camInfo.facing, eHalSensorDev);
}
DevMetaInfo::add(deviceId, camInfo, i4DevSetupOrientation,eDevId_ImgSensor, eHalSensorDev);
//
sp<EnumInfo> pInfo = new EnumInfo;
pInfo->uDeviceVersion =CAMERA_DEVICE_API_VERSION_1_0;
pInfo->pMetadata =NULL;
pInfo->iFacing =camInfo.facing;