private Camera mCamera;
mCamera = Camera.open(cameraID);
mCamera.startPreview();
native_setup(new WeakReference<Camera>(this), cameraId, packageName);
// connect to camera service
static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
jobject weak_this, jint cameraId, jstring clientPackageName)
{
//打开Camera,把回一个Camera设备
sp<Camera> camera = Camera::connect(cameraId, clientName,
Camera::USE_CALLING_UID);
// We use a weak reference so the Camera object can be garbage collected.
// The reference is only used as a proxy for callbacks.
sp<JNICameraContext> context = new MtkJNICameraContext(env, weak_this, clazz, camera);
}
sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName, int clientUid){
return CameraBaseT::connect(cameraId, clientPackageName, clientUid);
}
template <typename TCam>
struct CameraTraits {
};
template <typename TCam, typename TCamTraits = CameraTraits<TCam> >
class CameraBase : public IBinder::DeathRecipient
{
public:
typedef typename TCamTraits::TCamListener TCamListener;
typedef typename TCamTraits::TCamUser TCamUser;
typedef typename TCamTraits::TCamCallbacks TCamCallbacks;
typedef typename TCamTraits::TCamConnectService TCamConnectService;
static sp<TCam> connect(int cameraId, const String16& clientPackageName, int clientUid);
//...
protected:
CameraBase(int cameraId);
//...
typedef CameraBase<TCam> CameraBaseT;
};
}; // namespace android
class Camera : public CameraBase<Camera>, public BnCameraClient
template <>
struct CameraTraits<Camera>
{
typedef CameraListener TCamListener;
typedef ICamera TCamUser;
typedef ICameraClient TCamCallbacks;
typedef status_t (ICameraService::*TCamConnectService)(const sp<ICameraClient>&, int, const String16&, int,
/*out*/
sp<ICamera>&);
static TCamConnectService fnConnectService;
};
sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId, const String16& clientPackageName, int clientUid)
{
ALOGV('%s: connect', __FUNCTION__);
//把所有的TCam替换成Camera,构造了一个Camera对象。这个构造函数只有初始化mCameraId的值
sp<TCam> c = new TCam(cameraId);
sp<TCamCallbacks> cl = c;
//获取ICameraService
const sp<ICameraService>& cs = getCameraService();
//fnConnectService在Camera.cpp里被定义为&ICameraService::connect
TCamConnectService fnConnectService = TCamTraits::fnConnectService;
//调用CameraService里的connect函数
status = (cs.get()->*fnConnectService)(cl, cameraId, clientPackageName, clientUid, /*out*/ c->mCamera);
return c;
}
status_t CameraService::connect(const sp<ICameraClient>& cameraClient, int cameraId,
const String16& clientPackageName, int clientUid, /*out*/ sp<ICamera>& device) {
int facing = -1;
//获得hardware层的版本是CAMERA_DEVICE_API_VERSION_1_0
int deviceVersion = getDeviceVersion(cameraId, &facing);
switch(deviceVersion) {
case CAMERA_DEVICE_API_VERSION_1_0:
//CameraClient也在hardware层里定义了
client = new CameraClient(this, cameraClient,
clientPackageName, cameraId,
facing, callingPid, clientUid, getpid());
break;
case CAMERA_DEVICE_API_VERSION_2_0:
//...
}
status_t status = connectFinishUnsafe(client, client->getRemote());
mClient[cameraId] = client;
// important: release the mutex here so the client can call back
// into the service from its destructor (can be at the end of the call)
device = client;
return OK;
}
//getDeviceVersion
int CameraService::getDeviceVersion(int cameraId, int* facing) {
struct camera_info info;
mModule->get_camera_info(cameraId, &info);
int deviceVersion;
if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
deviceVersion = info.device_version;
} else {
//从hardware层的hw_module_t mModule里获得版本号
deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
}
return deviceVersion;
}
//mModule的初始化在onFirstRef函数里
void CameraService::onFirstRef()
{
hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t **)&mModule);
//......
}
static int get_camera_info(int cameraId, camera_info* info){
//CamDeviceManager继承于CamDeviceManagerBase
//这里相当于直接调用CamDeviceManagerBase的getDeviceInfo函数
return NSCam::getCamDeviceManager()->getDeviceInfo(cameraId, *info);
}
//CamDeviceManagerBase.cpp
CamDeviceManagerBase::
getDeviceInfo(int const deviceId, camera_info& rInfo){
//CameraService的getDeviceVersion()所需要的版本号
//mEumMap 在CamDeviceManagerImp.cpp 的 enumDeviceLocked() 函数里初始化
rInfo.device_version= mEnumMap.valueFor(deviceId)->uDeviceVersion;
rInfo.facing = mEnumMap.valueFor(deviceId)->iFacing;
rInfo.orientation = mEnumMap.valueFor(deviceId)->iWantedOrientation;
rInfo.static_camera_characteristics = mEnumMap.valueFor(deviceId)->pMetadata;
}
//CamDeviceManagerImp.cpp enumDeviceLocked()
enumDeviceLocked()
{
//增加ImageSensor的Sensor信息,后面会被get
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;
pInfo->iWantedOrientation = camInfo.orientation;
pInfo->iSetupOrientation = i4DevSetupOrientation;
mEnumMap.add(deviceId, pInfo);
i4DeviceNum++;
//...
}
CameraClient::CameraClient(const sp<CameraService>& cameraService,
Client(cameraService, cameraClient, clientPackageName,
cameraId, cameraFacing, clientPid, clientUid, servicePid)
{
// Callback is disabled by default
mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
//摄像头方向
mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
//拍照声音
mPlayShutterSound = true;
LOG1('CameraClient::CameraClient X (pid %d, id %d)', callingPid, cameraId);
}
status_t CameraClient::initialize(camera_module_t *module) {
// Verify ops permissions
res = startCameraOps();
char camera_device_name[10];
snprintf(camera_device_name, sizeof(camera_device_name), '%d', mCameraId);
//在CameraHardwareInterface.cpp
mHardware = new CameraHardwareInterface(camera_device_name);
//打开hardware mModule
res = mHardware->initialize(&module->common);
mHardware->setCallbacks(notifyCallback, dataCallback, dataCallbackTimestamp, (void *)mCameraId);
// Enable zoom, error, focus, and metadata messages by default
enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
// Enable MTK-extended messages by default
enableMsgType(MTK_CAMERA_MSG_EXT_NOTIFY | MTK_CAMERA_MSG_EXT_DATA);
}
status_t initialize(hw_module_t *module){
//调用了module.h open_device()
module->methods->open(module, mName.string(), (hw_device_t **)&mDevice);
//初始化预览窗口
initHalPreviewWindow();
}
open_device(hw_module_t const* module, const char* name, hw_device_t** device)
{
return NSCam::getCamDeviceManager()->open(module, name, device);
}
CamDeviceManagerBase::
openDeviceLocked( hw_module_t const* module, char const* name, hw_device_t** device)
{
//获得AppMode,这个属性在哪里被初始化的呢?我们这里的s8ClientAppMode = APP_MODE_NAME_DEFAULT
String8 const s8ClientAppMode = queryClientAppMode();
//这个我们在之前已经看到了在哪里初始化,结果为CAMERA_DEVICE_API_VERSION_1_0
uint32_t const version = determineOpenDeviceVersionLocked(s8ClientAppMode, i4OpenId);
if ( version == CAMERA_DEVICE_API_VERSION_1_0 )
{ //获得Camera设备,并打开硬件和初始化。这里的设备并不是指硬件设备
//而是包含硬件设备,设备参数,设备操作(预览等)。的一个操作类
pDevice = pPlatform->createCam1Device(s8ClientAppMode.string(), i4OpenId);
}
}
NSCam::Cam1Device*
createCam1Device(String8 const s8ClientAppMode, int32_t const i4OpenId)
{
// if ( s8ClientAppMode == MtkCameraParameters::APP_MODE_NAME_DEFAULT )
{
String8 const s8CamDeviceInstFactory = String8::format('createCam1Device_Default');
void* pCreateInstance = ::dlsym(RTLD_DEFAULT, s8CamDeviceInstFactory.string());
MY_LOGF_IF(0==pCreateInstance, 'Not exist: %s for %s', s8CamDeviceInstFactory.string(), s8ClientAppMode.string());
//这里相当于调用 createCam1Device_Default()函数
pdev = reinterpret_cast<NSCam::Cam1Device* (*)(String8 const&, int32_t const)>
(pCreateInstance)(s8ClientAppMode, i4OpenId);
}
if ( pdev )
{
//初始化硬件设备等
if ( OK != pdev->initialize() )
{
MY_LOGE('Cam1Device::initialize() device:%p', pdev);
// use ref. count to delete raw pointer.
pdev->incStrong(pdev);
pdev->decStrong(pdev);
pdev = NULL;
}
}
}
NSCam::Cam1Device*
createCam1Device_Default( String8 const& rDevName, int32_t const i4OpenId)
{
return new DefaultCam1Device(rDevName, i4OpenId);
}
//DefaultCam1Device继承于Cam1DeviceBase
DefaultCam1Device::
DefaultCam1Device(String8 const& rDevName, int32_t const i4OpenId)
: Cam1DeviceBase(rDevName, i4OpenId)
{
}
//Cam1DeviceBase继承于Cam1Device
Cam1DeviceBase::
Cam1DeviceBase( String8 const& rDevName, int32_t const i4OpenId)
: Cam1Device()
{
}
Cam1DeviceBase::
initialize()
{
if ( ! onInit() )
{
return -ENODEV;
}
}
DefaultCam1Device::onInit()
{
int const iHalSensorDevId = DevMetaInfo::queryHalSensorDev(getOpenId());
//Android的资源管理器
IResManager* pResManager = IResManager::getInstance();
if(!(pResManager->open('DefaultCam1Device'))){}
// (1) Open Sensor 下面的SensorHal包含了imageSensor和ISP
mpSensorHal = SensorHal::createInstance();
err = mpSensorHal->sendCommand((halSensorDev_e)iHalSensorDevId, SENSOR_CMD_SET_SENSOR_DEV);
err = mpSensorHal->init();
// (2) Open 3A
mp3AHal = NS3A::Hal3ABase::createInstance(iHalSensorDevId);
// (3) Init Base.
Cam1DeviceBase::onInit();
}
SensorHal* SensorHal::createInstance()
{
return SensorHalImp::getInstance();
}
SensorHal* SensorHalImp::getInstance()
{
static SensorHalImp singleton;
ret = singleton.createImp();
return &singleton;
}
MINT32 SensorHalImp::createImp()
{
mSensorDev = SENSOR_DEV_MAIN;
mIspSensorType[0] = SENSOR_TYPE_UNKNOWN;
mImageSensorType[0] = IMAGE_SENSOR_TYPE_UNKNOWN;
mIspSensorType[1] = SENSOR_TYPE_UNKNOWN;
mImageSensorType[1] = IMAGE_SENSOR_TYPE_UNKNOWN;
//创建ISP管理
pSeninfDrv = SeninfDrv::createInstance();
}
MINT32 SensorHalImp::init()
{
//这里直接调用的是ImgSensorDrv::getInstance()
//返回的是ImgSensorDrv(imgsensor_drv.cpp),也就是我们找的摄像头驱动管理
//构造函数只在做了简单的数据处理
pSensorDrv = SensorDrv::createInstance(mSensorDev);
//初始化ISP
ret = pSeninfDrv->init();
// Before searching sensor, need to turn on TG
ret = initSensor();
// Get sensor info before setting TG phase counter
ret = getSensorInfo(scenarioId);
ret = setTgPhase();
ret = setSensorIODrivingCurrent();
ret = initCSI2Peripheral(1); // if the interface is mipi, enable the csi2
ret = setCSI2Config(1); // enable and config CSI2.
#ifdef ATV_SUPPORT
#ifdef MTK_MATV_SERIAL_IF_SUPPORT
pSeninfDrv->initTg1Serial(MFALSE);
#endif
#endif
// Open sensor, try to open 3 time
for (int i =0; i < 3; i++)
ret = pSensorDrv->open();
}
MINT32 SensorHalImp::initSensor()
{
switch (mSensorDev)
{
case SENSOR_DEV_MAIN:
eSensorDev = SENSOR_MAIN;
if(!(mSearchSensorDev & SENSOR_DEV_MAIN)) {
LOG_ERR('initSensor fail,mSensorDev = 0x%x, mSearchSensorDev = 0x%x\n',mSensorDev,mSearchSensorDev);
return -1;
}
break;
case SENSOR_DEV_SUB:
//....
}
//imgsensor_drv.cpp ImgSensorDrv::init()函数
//在此打开底层的驱动文件,并获取一些信息
ret = pSensorDrv->init(mSensorDev);
// Get Sensor Resolution
pSensorResInfo[0] = &sensorResolution[0];
pSensorResInfo[1] = &sensorResolution[1];
ret = pSensorDrv->getResolution(pSensorResInfo);
if(eSensorDev == SENSOR_DEV_MAIN) {
if ( IMAGE_SENSOR_TYPE_RAW == pSensorDrv->getCurrentSensorType(eSensorDev) )
{
// Full Resolution
u4PaddedWidth = sensorResolution[0].SensorFullWidth + ISP_RAW_WIDTH_PADD;
u4PaddedHeight= sensorResolution[0].SensorFullHeight + ISP_RAW_HEIGHT_PADD;
if ( 0 != ((u4PaddedWidth * u4PaddedHeight) % 6) )
{
sensorResolution[0].SensorFullHeight -= (u4PaddedHeight % 6);
LOG_MSG(' Sensor resolution after fixing: Full: %d/%d\n', sensorResolution[0].SensorFullWidth, sensorResolution[0].SensorFullHeight);
}
}
}
if(eSensorDev == SENSOR_DEV_SUB) {
//...
}
}
MINT32 ImgSensorDrv::init(MINT32 sensorIdx)
{
//打开底层设备文件,至于是打开sensorlist哪个摄像头
//摄像头怎么匹配的就是靠CAMERA_HW_DEVNAME这个驱动了。
sprintf(cBuf,'/dev/%s',CAMERA_HW_DEVNAME);
m_fdSensor = ::open(cBuf, O_RDWR);
//set sensor driver
ret = ioctl(m_fdSensor, KDIMGSENSORIOC_X_SET_DRIVER,sensorDrvInfo);
android_atomic_inc(&mUsers);
//init. resolution
pSensorResInfo[0] = &m_SenosrResInfo[0];
pSensorResInfo[1] = &m_SenosrResInfo[1];
ret = getResolution(pSensorResInfo);
}
联系客服