本文分析的是Android Hal层的源码,硬件平台基于mt6735。之前几篇讲的预览流程中3A相关的环节都忽略了,现在重新整理下。
3A指的是Auto Exposure,Auto Focus,Auto White Balance。这三个一起放上来代码实在太多了,这里将重点记录AF的代码。AF的部分工作是由ISP完成的,而ISP的大部分代码mtk都没有开放给我们,比如ISP是如何计算得到对焦位置信息的,但得到对焦位置之后怎么操作对焦马达的代码我们是看得到的,所以涉及到ISP的一些代码将被略过
2. 初始化3A
3A的初始化在DefaultCam1Device的onInit函数里面开始,之前在camera打开流程里面已经提到过
bool
DefaultCam1Device::
onInit()
{
......
mpHal3a = NS3A::IHal3A::createInstance(
NS3A::IHal3A::E_Camera_1,
getOpenId(),
LOG_TAG);
......
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
构造一个Hal3A对象,看下Hal3A::createInstance的实现
Hal3A*
Hal3A::
createInstance(MINT32 i4SensorDevId, MINT32 i4SensorOpenIndex)
{
switch (i4SensorDevId)
{
case SENSOR_DEV_MAIN:
Hal3ADev<SENSOR_DEV_MAIN>::getInstance()->init(i4SensorDevId, i4SensorOpenIndex);
return Hal3ADev<SENSOR_DEV_MAIN>::getInstance();
break;
case SENSOR_DEV_SUB:
Hal3ADev<SENSOR_DEV_SUB>::getInstance()->init(i4SensorDevId, i4SensorOpenIndex);
return Hal3ADev<SENSOR_DEV_SUB>::getInstance();
break;
......
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
其实这里的Hal3A并没有直接继承IHal3A,也就是说从IHal3A::createInstance到Hal3A::createInstance的调用过程经历了一番波折,但暂时不用关心它。从Hal3A::createInstance可以看到除了实例化以外还会调用init函数。构造函数没什么好看的-略过,直接看init函数
MRESULT
Hal3A::
init(MINT32 i4SensorDevId, MINT32 i4SensorOpenIndex)
{
......
mpStateMgr = new StateMgr(i4SensorDevId);
bRet = postCommand(ECmd_Init);
createThread();
bRet = IspTuningMgr::getInstance().init(m_i4SensorDev, m_i4SensorOpenIdx);
ret = EnableAFThread(1);
......
return S_3A_OK;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
步骤(1) new StateMgr,构造函数如下
StateMgr::StateMgr(MINT32 sensorDevId)
: ......
{
#define STATE_INITIALIZE(_state_)\
mpIState[eState_##_state_] = new State##_state_(sensorDevId, this);
STATE_INITIALIZE(Init);
STATE_INITIALIZE(Uninit);
STATE_INITIALIZE(CameraPreview);
STATE_INITIALIZE(CamcorderPreview);
STATE_INITIALIZE(Recording);
STATE_INITIALIZE(Precapture);
STATE_INITIALIZE(Capture);
STATE_INITIALIZE(AF);
mpCurrentState = mpIState[eState_Uninit];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
初始化3A的状态管理,将各个子状态都保存在mpIState数组里面,并将当前状态设置为Uninit状态
步骤(2) postCommand
MBOOL Hal3A::postCommand(ECmd_T const eCmd, MINTPTR const i4Arg)
{
......
ERROR_CHECK(mpStateMgr->sendCmd(eCmd))
......
}
MRESULT StateMgr::sendCmd(ECmd_T eCmd)
{
Mutex::Autolock lock(m_Lock);
EIntent_T eNewIntent = static_cast<EIntent_T>(eCmd);
#define SEND_INTENT(_intent_)\
case _intent_: return mpCurrentState->sendIntent(intent2type<_intent_>());\
switch (eNewIntent)
{
SEND_INTENT(eIntent_CameraPreviewStart)
SEND_INTENT(eIntent_CameraPreviewEnd)
SEND_INTENT(eIntent_CaptureStart)
SEND_INTENT(eIntent_CaptureEnd)
SEND_INTENT(eIntent_RecordingStart)
SEND_INTENT(eIntent_RecordingEnd)
SEND_INTENT(eIntent_AFUpdate)
SEND_INTENT(eIntent_AFStart)
SEND_INTENT(eIntent_AFEnd)
SEND_INTENT(eIntent_Init)
SEND_INTENT(eIntent_Uninit)
}
return -1;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
从步骤(1)可以看出这里的mpCurrentState指向的是StateUninit对象,所以接着看StateUninit的sendIntent函数
MRESULT
StateUninit::
sendIntent(intent2type<eIntent_Init>)
{
MY_LOG("[StateUninit::sendIntent]<eIntent_Init>");
MINT32 i4SensorIdx = m_pHal3A->getSensorOpenIdx();
if (ENABLE_3A_GENERAL & m_pHal3A->m_3ACtrlEnable) {
if (ENABLE_AAOBUF & m_pHal3A->m_3ACtrlEnable) {
if (!IAAOBufMgr::getInstance().init(m_SensorDevId, i4SensorIdx)) {
MY_ERR("IAAOBufMgr::getInstance().init() fail");
return E_3A_ERR;
}
if (!IAEBufMgr::getInstance().init(m_SensorDevId, i4SensorIdx)) {
MY_ERR("IAEBufMgr::getInstance().init() fail");
return E_3A_ERR;
}
}
if (ENABLE_AFOBUF & m_pHal3A->m_3ACtrlEnable) {
if (!IAFOBufMgr::getInstance().init(m_SensorDevId, i4SensorIdx)) {
MY_ERR("IAFOBufMgr::getInstance().init() fail");
return E_3A_ERR;
}
}
}
m_pStateMgr->transitState(eState_Uninit, eState_Init);
return S_3A_OK;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
做了一堆乱七八糟的初始化之后将3A状态从Uninit状态切换到Init状态
步骤(3) createThread和步骤(5) EnableAFThread
MVOID
Hal3A::createThread()
{
......
pthread_create(&mThread, NULL, onThreadLoop, this);
pthread_create(&mPDThread, NULL, PDThreadLoop, this);
pthread_create(&mPDVCThread, NULL, PDVCThreadLoop, this);
......
}
MRESULT Hal3A::EnableAFThread(MINT32 a_bEnable)
{
if (a_bEnable) {
if (mbAFThreadLoop== 0)
{
......
pthread_create(&mAFThread, &attr, AFThreadFunc, this);
}
} else {
......
}
return ret;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
一共创建了4个线程,暂时只关心onThreadLoop 和AFThreadFunc。onThreadLoop是3A主线程,负责接收处理命令;AFThreadFunc负责实时更新AF参数
3. 处理PASS1_START_ISP事件
前面的3A初始化做的事情并不多,更多的准备工作是在接收到PASS1_START_ISP事件之后做的,PASS1_START_ISP事件是在之前的Camera预览流程控制流中提到的Pass1Node的startHw函数里面发送
MBOOL
Pass1NodeImpl::
startHw(list<HwPortConfig_t> & plPortCfg)
{
......
handleNotify(PASS1_START_ISP, newMagicNum, 0);
......
}
3.1 DefaultCtrlNode接收处理PASS1_START_ISP事件
Pass1Node发出的event将在DefaultCtrlNode的onNotify函数中接收处理
MBOOL
DefaultCtrlNodeImpl::
onNotify(MUINT32 const msg, MUINT32 const ext1, MUINT32 const ext2)
{
switch(msg)
{
case PASS1_START_ISP:
{
if(mpHal3a)
{
cmd = ECmd_CameraPreviewStart;
......
mpHal3a->sendCommand(cmd);
}
case PASS1_STOP_ISP:
{
......
}
case PASS1_EOF:
{
......
}
default:
{
ret = MTRUE;
}
}
return ret;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
Hal3a的sendCommand函数会把命令加入到命令队列,然后由主线程onThreadLoop获取
MVOID*
Hal3A::onThreadLoop(MVOID *arg)
{
while (_this->getCommand(rCmd, bGetCmd, MFALSE))
{
switch (rCmd.eCmd)
{
case ECmd_PrecaptureStart:
{
......
}
case ECmd_Update:
{
......
}
default:
if ( ! _this->postCommand(rCmd.eCmd, reinterpret_cast<MINTPTR>(&rCmd.rParamIspProfile)))
{
MY_ERR("Cmd(%d) failed(0x%x)", rCmd.eCmd, _this->getErrorCode());
AEE_ASSERT_3A_HAL("onThreadLoop postCommand fail(2).");
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24