1、Camera启动速度(Hal层的耗时)
Camera的启动速度,是我们经常做优化的地方,除了通过仪器去测量,还可以通过Log来查看!
搜索关键字:[KPI Perf]
01-24 17:55:57.657 371 4074 D QCamera2HWI: [KPI Perf] int qcamera::QCamera2HardwareInterface::openCamera(hw_device_t**):
E PROFILE_OPEN_CAMERA camera id 0 //这里打开摄像头
01-24 17:55:58.139 371 2380 D QCamera2HWI: [KPI Perf]
static int qcamera::QCamera2HardwareInterface::start_preview(camera_device*):
E PROFILE_START_PREVIEW
01-24 17:55:58.528 371 16975 D QCamera2HWI: int qcamera::QCamera2HardwareInterface::startPreview(): X
01-24 17:55:58.528 371 2380 D QCamera2HWI: [KPI Perf] static int qcamera::QCamera2HardwareInterface::
start_preview(camera_device*): X
01-24 17:55:58.530 2971 2971 D CameraManager: {CameraManager.initVideoCamera-199}
摄像头打开预览.
01-24 17:55:58.654 371 17015 D QCamera2HWI: [KPI Perf] static void qcamera::QCamera2HardwareInterface::
preview_stream_cb_routine(mm_camera_super_buf_t*, qcamera::QCameraStream*, void*) :
PROFILE_FIRST_PREVIEW_FRAME //第一帧预览画面出现
打开Camera的时间点:01-24 17:55:57.657
第一帧预览画面出现的时间点:01-24 17:55:58.654
耗时:0.997s(将近1s左右)
注意这是从进入开始Hal层的计算的耗时
我们来看看源码,
int QCamera2HardwareInterface::openCamera(struct hw_device_t **hw_device)
{
···
CDBG_HIGH("[KPI Perf] %s: E PROFILE_OPEN_CAMERA camera id %d", __func__,mCameraId);
rc = openCamera();
···
}
void QCamera2HardwareInterface::preview_stream_cb_routine(mm_camera_super_buf_t *super_frame,
QCameraStream * stream,
void *userdata)
{
ALOGD("[KPI Perf] %s : BEGIN", __func__);
···
if(pme->m_bPreviewStarted) {
CDBG_HIGH("[KPI Perf] %s : PROFILE_FIRST_PREVIEW_FRAME", __func__);
pme->m_bPreviewStarted = false ;
}
···
}
事实上,高通也只是在调用地方打印了一下当前的时间点,因此如果你想分析某个功能的耗时,
就按照高通的思路,调用前后打印一下当前函数就可以了,最后再通过时间戳计算耗时!
当然了,在哪里加log就要自己对流程熟悉了!!!
1 拍照帧率
源码:
hardware/qcom/camera/QCamera2/HAL/QCamera2HWICallbacks.cp
void QCamera2HardwareInterface::debugShowPreviewFPS()
{
static int n_pFrameCount = 0;
static int n_pLastFrameCount = 0;
static nsecs_t n_pLastFpsTime = 0;
static double n_pFps = 0;
n_pFrameCount++;
nsecs_t now = systemTime();
nsecs_t diff = now - n_pLastFpsTime;
if (diff > ms2ns(250)) {
n_pFps = (((double)(n_pFrameCount - n_pLastFrameCount)) *
(double)(s2ns(1))) / (double)diff;
CDBG_HIGH("[KPI Perf] %s: PROFILE_PREVIEW_FRAMES_PER_SECOND : %.4f", __func__, n_pFps);
n_pLastFpsTime = now;
n_pLastFrameCount = n_pFrameCount;
}
}
hal层是如何计算实际输出的帧率:
n_pFps = (((double)(n_pFrameCount - n_pLastFrameCount)) *(double)(s2ns(1))) / (double)diff;
搜索关键字:PROFILE_PREVIEW_FRAMES_PER_SECOND
默认fps是不输出的,需要用到adb命令打开
adb root
adb shell setprop persist.debug.sf.showfps 1
adb logcat | findstr "PROFILE_PREVIEW_FRAMES_PER_SECOND"
void QCamera2HardwareInterface::debugShowVideoFPS()
{
static int n_vFrameCount = 0;
static int n_vLastFrameCount = 0;
static nsecs_t n_vLastFpsTime = 0;
static double n_vFps = 0;
n_vFrameCount++;
nsecs_t now = systemTime();
nsecs_t diff = now - n_vLastFpsTime;
if (diff > ms2ns(250)) {
n_vFps = (((double)(n_vFrameCount - n_vLastFrameCount)) *
(double)(s2ns(1))) / (double)diff;
ALOGE("Video Frames Per Second: %.4f", n_vFps);
n_vLastFpsTime = now;
n_vLastFrameCount = n_vFrameCount;
}
}
搜索关键字:Video Frames Per Second
执行命令
adb root
adb shell setprop persist.debug.sf.showfps 1
adb shell
logcat | grep "Video Frames Per Second"
源码:
hardware/qcom/camera/QCamera2/HAL/QCamera2HWICallbacks.cpp
void QCamera2HardwareInterface::dumpFrameToFile(QCameraStream *stream,
mm_camera_buf_def_t *frame, uint32_t dump_type)
{
char value[PROPERTY_VALUE_MAX];
property_get("persist.camera.dumpimg", value, "0");
uint32_t enabled = (uint32_t) atoi(value);
uint32_t frm_num = 0;
uint32_t skip_mode = 0;
if (stream)
mDumpFrmCnt = stream->mDumpFrame;
if(enabled & QCAMERA_DUMP_FRM_MASK_ALL) {
if((enabled & dump_type) && stream && frame) {
frm_num = ((enabled & 0xffff0000) >> 16);
if(frm_num == 0) {
frm_num = 10; //default 10 frames
}
if(frm_num > 256) {
frm_num = 256; //256 buffers cycle around
}
skip_mode = ((enabled & 0x0000ff00) >> 8);
if(skip_mode == 0) {
skip_mode = 1; //no-skip
}
if(stream->mDumpSkipCnt == 0)
stream->mDumpSkipCnt = 1;
if( stream->mDumpSkipCnt % skip_mode == 0) {
if((frm_num == 256) && (mDumpFrmCnt >= frm_num)) {
// reset frame count if cycling
mDumpFrmCnt = 0;
}
if (timeinfo != NULL)
strftime (timeBuf, sizeof(timeBuf), "/data/misc/camera/%Y%m%d%H%M%S", timeinfo);
···
}
从源码中我们可以得出很多信息:
如果你想查看预览、视频、快照、缩略图、Raw 或 JPEG 帧,那么:
以下命令可在启动摄像头应用之前启用帧转储设置。
adb root
adb shell chmod 777 /data/misc/camera
//存10帧 655615 =A00FF
adb shell setprop persist.camera.dumpimg 655615
//存256帧 100 00 FF
adb shell setprop persist.camera.dumpimg 16777471
//最大dump 268帧图像1 000 00 FF
adb shell setprop persist.camera.dumpimg 33554687
在本例中,655615(十六进制表示为 0xA00FF)是指根据摄像头应用的工作模式,
连续十 个帧的所用预览、视频、快照、缩略图、Raw 或 JPEG 帧
都会被转储到 /data/misc/camera 文件夹。 /vendor/qcom/hardware/qcom/camera/QCamera2/HAL/QCamera2HWICallbacks.cpp 文件的 QCamera2HardwareInterface::dumpFrameToFile() 函数用于转储 /data 文件夹下的文件。
有关 为每种帧类型使用的文件命名约定,可参见源代码中的 dumpFrameToFile() 函数.
我们来看实际效果:
以下是预览的时候的yuv图,使用YUVViewer工具查看
YUVView工具下载链接 提取码: h6j6
为啥是从第三张开始:
因为默认跳过了前2帧图像
vendor/qcom/proprietary/mm-camera/mm-camera2/media-controller/
modules/sensors/sensor_libs/ov5675_d5v15b/ov5675_d5v15b_lib.c
static sensor_lib_t sensor_lib_ptr = {
···
/* number of frames to skip after start stream */
.sensor_num_frame_skip = 2,
···
}
开发记录:
增加性能日志:Tag:[KPI_CAMERA]
日志打印:
//zcf
void dumpFile(void* frame,int w,int h,int size,int base_index)
{
char buf[128];
int res=0;
snprintf(buf, sizeof(buf), "%s%d_%dx%d.yuv","/data/misc/camera/",base_index, w, h);
int file_fd = open(buf,O_RDWR | O_CREAT,0777);
if(file_fd == -1){
LOGI("zcf open file failed file_fd=%d",file_fd);
}
res = write(file_fd,frame,size);
if(res !=size)
LOGI("zcf dumpFile failed!res=%d",res);
close(file_fd);
}
//
kernel/arch/arm/boot/dts/qcom/msm8909.dtsi
i2c_3: i2c@78b7000 { /* BLSP1 QUP3 */
···
- qcom,clk-freq-out = <100000>;
+ qcom,clk-freq-out = <400000>;//修改成400k
qcom,clk-freq-in = <19200000>;
pinctrl-names = "i2c_active", "i2c_sleep";
···
}
I2C资料:80-NU767-1,参考68-69页
举个例子:
i2c速率(I2C_FS_CLK ):400k
I2C_CLK:24M
占空比改成:50%
I2C_FS_CLK = I2C_CLK/(fs_div+hs_div+6)
fs_div + hs_div = 24M/400k - 6 = 60-6=54
如果占空比为50%
则
fs_div=hs_div=54/2=27
kernel/arch/arm/boot/dts/qcom/msm8909.dtsi
i2c_3: i2c@78b7000 { /* BLSP1 QUP3 */
···
qcom,clk-freq-out = <400000>;//修改成400k
//snip
qcom,fs-clk-div = <27>;
qcom,high-time-clk-div = <27>;
···
}
I2C节点解释:
kernel/Documentation/devicetree/bindings/i2c/i2c-msm-v2.txt
qcom,high-time-clk-div :
high time divider value to configure clk-ctl register.
When missing, default to the value given in driver.
qcom,fs-clk-div:
fs divider value to configure clk-ctl register.
When missing, default to the value given in driver.
git diff > a.patch
patch -p1 < a.patch
80 static module_debug_t cam_loginfo[(int)CAM_LAST_MODULE] = {
81 {CAM_GLBL_DBG_ERR, 1,
82 "", "persist.vendor.camera.global.debug" }, /* CAM_NO_MODULE */
83 {CAM_GLBL_DBG_ERR, 1,
84 "", "persist.vendor.camera.mct.debug" }, /* CAM_MCT_MODULE */
85 {CAM_GLBL_DBG_ERR, 1,
86 "", "persist.vendor.camera.sensor.debug" }, /* CAM_SENSOR_MODULE */
87 {CAM_GLBL_DBG_WARN, 1,
88 "", "persist.vendor.camera.iface.logs" }, /* CAM_IFACE_MODULE */
89 {CAM_GLBL_DBG_ERR, 1,
90 "", "persist.vendor.camera.isp.debug" }, /* CAM_ISP_MODULE */
91 {CAM_GLBL_DBG_ERR, 1,
92 "", "persist.vendor.camera.pproc.debug.mask" }, /* CAM_PPROC_MODULE */
93 {CAM_GLBL_DBG_WARN, 1,
94 "", "persist.vendor.camera.imglib.logs" }, /* CAM_IMGLIB_MODULE */
95 {CAM_GLBL_DBG_WARN, 1,
96 "", "persist.vendor.camera.cpp.debug.mask" }, /* CAM_CPP_MODULE */
97 {CAM_GLBL_DBG_ERR, 1,
98 "", "persist.vendor.camera.hal.debug" }, /* CAM_HAL_MODULE */
99 {CAM_GLBL_DBG_ERR, 1,
100 "", "persist.vendor.camera.mmstill.logs" }, /* CAM_JPEG_MODULE */
101 {CAM_GLBL_DBG_WARN, 1,
102 "", "persist.vendor.camera.c2d.debug.mask" }, /* CAM_C2D_MODULE */
103 {CAM_GLBL_DBG_ERR, 1,
104 "", "persist.vendor.camera.stats.debug" }, /* CAM_STATS_MODULE */
105 {CAM_GLBL_DBG_ERR, 1,
106 "", "persist.vendor.camera.stats.af.debug" }, /* CAM_STATS_AF_MODULE */
107 {CAM_GLBL_DBG_ERR, 1,
108 "", "persist.vendor.camera.stats.aec.debug" }, /* CAM_STATS_AEC_MODULE */
109 {CAM_GLBL_DBG_ERR, 1,
110 "", "persist.vendor.camera.stats.awb.debug" }, /* CAM_STATS_AWB_MODULE */
111 {CAM_GLBL_DBG_ERR, 1,
112 "", "persist.vendor.camera.stats.asd.debug" }, /* CAM_STATS_ASD_MODULE */
113 {CAM_GLBL_DBG_ERR, 1,
114 "", "persist.vendor.camera.stats.afd.debug" }, /* CAM_STATS_AFD_MODULE */
115 {CAM_GLBL_DBG_ERR, 1,
116 "", "persist.vendor.camera.stats.q3a.debug" }, /* CAM_STATS_Q3A_MODULE */
117 {CAM_GLBL_DBG_ERR, 1,
118 "", "persist.vendor.camera.stats.is.debug" }, /* CAM_STATS_IS_MODULE */
119 {CAM_GLBL_DBG_ERR, 1,
120 "", "persist.vendor.camera.stats.haf.debug" }, /* CAM_STATS_HAF_MODULE */
121 {CAM_GLBL_DBG_ERR, 1,
122 "", "persist.vendor.camera.stats.cafscan" }, /* CAM_STATS_CAFSCAN_MODULE */
123 {CAM_GLBL_DBG_ERR, 1,
124 "", "persist.vendor.camera.shim.debug" }, /* CAM_SHIM_LAYER */
125
126 };
13966void QCamera3HardwareInterface::getLogLevel()
13967 {
13968 char prop[PROPERTY_VALUE_MAX];
13969 uint32_t globalLogLevel = 0;
13970
13971 property_get("persist.vendor.camera.hal.debug", prop, "0");
13972 int val = atoi(prop);
13973 if (0 <= val) {
13974 gCamHal3LogLevel = (uint32_t)val;
13975 }
13976
13977 property_get("persist.vendor.camera.kpi.debug", prop, "0");
13978 gKpiDebugLevel = atoi(prop);
13979
13980 property_get("persist.vendor.camera.global.debug", prop, "0");
13981 val = atoi(prop);
13982 if (0 <= val) {
13983 globalLogLevel = (uint32_t)val;
13984 }
13985
13986 /* Highest log level among hal.logs and global.logs is selected */
13987 if (gCamHal3LogLevel < globalLogLevel)
13988 gCamHal3LogLevel = globalLogLevel;
13989
13990 return;
13991 }