androidcamerahal
Ⅰ Android Camera 驱动开发入门必备知识有哪些
1:硬件相关:简单的数字电路要懂,看的懂电路图(这个很简单),能根据电路找到相应Camera硬件连接的GPIO,并根据芯片手册配置GPIO为相应的模式;比如GPIO模式,输入IO或输出IO;或配置成PWM模式;不同的模式这个GPIO的工作方式是不同的;
2:Camera驱动:比如ov5640;要找本linux驱动相关的书看看,比如《linux设备驱动程序》知道简单的linux设备驱动写法;当然工作中Camera驱动不需要你写的,有参考的可以使用,但是参考驱动一般支持实现录像、拍照功能,别的白平衡、对焦等都需要你去自己调试;
3:要学习V4L2协议,因为驱动导出/dev/video0接口是通过V4L2协议的;上层C的应用也是通过V4L2协议访问/dev/video0的;实际V4L2协议就是通过一些标志linux ioctl()函数和底层驱动通信的;V4L2协议是为了屏蔽各种Camera驱动的不同(比如usb摄像头,mipi接口摄像头)而提供的统一和摄像头驱动通信的规范;
4:要知道C++的class的相关知识,最好能看到C++的代码,既然有C的知识,哪把C++的面相对象看懂,知道C++的class就可以了;为什么要懂C++呢?因为Android Camera HAL访问/dev/video0都是用C++写的,二十几个个xxxx.cpp不同的类相互继承实现的;而且要懂Android HAL层的写法,至少查查资料知道用哪里看;一般都是CameraMole.cpp或CameraHal.cpp;
5:JNI的写法最好以知道一些,至少可以抄就可以,因为这些写法都是固定的,因为调试的过程中有时候你一个人要从java层跟踪代码到HAL层再到驱动;
6:要会用Linux,因为所有的Android源码都是在Linux下编译的;要知道ARM架构,交叉编译器;比如arm-linux-gcc;知道怎么编译Android和kernel源码;怎么刷机;怎么调试;最好买块带Camera的Android开发板;
Ⅱ android中camera的hal模块怎么被调用
CameraService.cpp (frameworks\base\services\camera\libcameraservice)
中调用hw_get_mole
[cpp] view plain print?
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();
<span style="color: rgb(255, 0, 0);">if (hw_get_mole(CAMERA_HARDWARE_MODULE_ID,
(const hw_mole_t **)&mMole) < 0)</span> {
LOGE("Could not load camera HAL mole");
mNumberOfCameras = 0;
}
else {
mNumberOfCameras = mMole->get_number_of_cameras();
if (mNumberOfCameras > MAX_CAMERAS) {
LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
mNumberOfCameras, MAX_CAMERAS);
mNumberOfCameras = MAX_CAMERAS;
}
for (int i = 0; i < mNumberOfCameras; i++) {
setCameraFree(i);
}
}
}
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();
if (hw_get_mole(CAMERA_HARDWARE_MODULE_ID,
(const hw_mole_t **)&mMole) < 0) {
LOGE("Could not load camera HAL mole");
mNumberOfCameras = 0;
}
else {
mNumberOfCameras = mMole->get_number_of_cameras();
if (mNumberOfCameras > MAX_CAMERAS) {
LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
mNumberOfCameras, MAX_CAMERAS);
mNumberOfCameras = MAX_CAMERAS;
}
for (int i = 0; i < mNumberOfCameras; i++) {
setCameraFree(i);
}
}
}
看一下hw_get_mole是怎么回事
[cpp] view plain print?
int hw_get_mole(const char *id, const struct hw_mole_t **mole)
{
return <span style="color: rgb(255, 0, 0);">hw_get_mole_by_class(id, NULL, mole);
</span>}
int hw_get_mole(const char *id, const struct hw_mole_t **mole)
{
return hw_get_mole_by_class(id, NULL, mole);
}
他只是一个封装实际调用了[email protected] (hardware\libhardware)
好在不长,看看吧
[cpp] view plain print?
int hw_get_mole_by_class(const char *class_id, const char *inst,
const struct hw_mole_t **mole)
{
int status;
int i;
const struct hw_mole_t *hmi = NULL;
<span style="color: rgb(255, 0, 0);"> char prop[PATH_MAX];//几个关键的数组
char path[PATH_MAX];//在下面起了重要
char name[PATH_MAX];//作用
</span>
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);//走这里
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new of the library).
* We also assume that dlopen() is thread-safe.
*/
/* Loop through the configuration variants looking for a mole */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
if (i < HAL_VARIANT_KEYS_COUNT) {
if (property_get(variant_keys[i], prop, NULL) == 0)
<span style="color: rgb(255, 0, 0);">//在这里将prop的路径得到,分别从
"ro.hardware[qcom]"
"ro.proct.board"[7x27],
"ro.board.platform"[msm7627a],
"ro.arch",
"ro.hw_platform"[QRD_SKU3-1100]
这几个属性文件中获得硬件的信息
有些硬件信息的字符串会出现在编译后生成的.so名字中</span>
{
continue;
}
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, prop);
if (access(path, R_OK) == 0) break;
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, prop);<span style="color: rgb(255, 0, 0);">//走这里,在这里得到/system/lib/hw/camera.msm7627a.so
这样一个路径,这个库里有QualcommCamera.cpp,这是
camera模块HAL代码开始的地方</span>
if (access(path, R_OK) == 0) break;
} else {
snprintf(path, sizeof(path), "%s/%s.default.so",
HAL_LIBRARY_PATH1, name);
if (access(path, R_OK) == 0) break;
}
}
status = -ENOENT;
if (i < HAL_VARIANT_KEYS_COUNT+1) {
/* load the mole, if this fails, we're doomed, and we should not try
* to load a different variant. */
status = load(class_id, path, mole);<span style="color: rgb(255, 0, 0);">//这里关键,函数的三个参数可以串联成一句话:
到path(/system/lib/hw/camera.msm7627a.so)这个路径下找到一个id(camera)匹配的mole</span>
}
return status;
}
int hw_get_mole_by_class(const char *class_id, const char *inst,
const struct hw_mole_t **mole)
{
int status;
int i;
const struct hw_mole_t *hmi = NULL;
char prop[PATH_MAX];//几个关键的数组
char path[PATH_MAX];//在下面起了重要
char name[PATH_MAX];//作用
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);//走这里
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new of the library).
* We also assume that dlopen() is thread-safe.
*/
/* Loop through the configuration variants looking for a mole */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
if (i < HAL_VARIANT_KEYS_COUNT) {
if (property_get(variant_keys[i], prop, NULL) == 0)
//在这里将prop的路径得到,分别从
"ro.hardware[qcom]"
"ro.proct.board"[7x27],
"ro.board.platform"[msm7627a],
"ro.arch",
"ro.hw_platform"[QRD_SKU3-1100]
这几个属性文件中获得硬件的信息
有些硬件信息的字符串会出现在编译后生成的.so名字中
{
continue;
}
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, prop);
if (access(path, R_OK) == 0) break;
snprintf(path, sizeof(path), "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, prop);//走这里,在这里得到/system/lib/hw/camera.msm7627a.so
这样一个路径,这个库里有QualcommCamera.cpp,这是
camera模块HAL代码开始的地方
if (access(path, R_OK) == 0) break;
} else {
snprintf(path, sizeof(path), "%s/%s.default.so",
HAL_LIBRARY_PATH1, name);
if (access(path, R_OK) == 0) break;
}
}
status = -ENOENT;
if (i < HAL_VARIANT_KEYS_COUNT+1) {
/* load the mole, if this fails, we're doomed, and we should not try
* to load a different variant. */
status = load(class_id, path, mole);//这里关键,函数的三个参数可以串联成一句话:
到path(/system/lib/hw/camera.msm7627a.so)这个路径下找到一个id(camera)匹配的mole
}
return status;
}
再来看看load这个函数@hardware.c (hardware\libhardware)
[cpp] view plain print?
static int load(const char *id,
const char *path,
const struct hw_mole_t **pHmi)
{
int status;
void *handle;
struct hw_mole_t *hmi;
/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
char const *err_str = dlerror();
LOGE("load: mole=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}
<span style="color: rgb(255, 0, 0);"> /* Get the address of the struct hal_mole_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_mole_t *)dlsym(handle, sym);
</span> if (hmi == NULL) {
LOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}
/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) {
LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}
hmi->dso = handle;
/* success */
status = 0;
done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}
*pHmi = hmi;
return status;
}
static int load(const char *id,
const char *path,
const struct hw_mole_t **pHmi)
{
int status;
void *handle;
struct hw_mole_t *hmi;
/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
char const *err_str = dlerror();
LOGE("load: mole=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}
/* Get the address of the struct hal_mole_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_mole_t *)dlsym(handle, sym);
if (hmi == NULL) {
LOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}
/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) {
LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}
hmi->dso = handle;
/* success */
status = 0;
done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}
*pHmi = hmi;
return status;
}
在打开的.so(camera.msm7627a.so)中查找HMI符号的地址,并保存在hmi中。至此,.so中的hw_mole_t已经被成功获取,从而可以根
据它获取别的相关接口。
1)HAL通过hw_get_mole函数获取hw_mole_t
2)HAL通过hw_mole_t->methods->open获取hw_device_t指针,并在此open函数中初始化hw_device_t的包装结构中的
函数及hw_device_t中的close函数,如gralloc_device_open。
3)三个重要的数据结构:
a) struct hw_device_t: 表示硬件设备,存储了各种硬件设备的公共属性和方法
b)struct hw_mole_t: 可用hw_get_mole进行加载的mole
c)struct hw_mole_methods_t: 用于定义操作设备的方法,其中只定义了一个打开设备的方法open.
Ⅲ Android Camera API 1.0,2.0,3.0 知多少
看到源码里面有诸如Camera.h , Camera2.h, Camera3.h的文件,觉得奇怪,后面在Camera3.h
中找到了对前后3个版本的概要性描述。
BTW, 我们看代码的过程中,如何避免不需要的文件被添加到工程?
我是指: 我只想看Camera3.0的代码,怎么去除掉其他版本的呢?
不知道各位是否知道这些牵涉到哪些文件? 谢谢!!
* S1. Version history:
* 1.0: Initial Android camera HAL (Android 4.0) [camera.h]:
*
* - Converted from C3;3; CameraHardwareInterface abstraction layer.
*
* - Supports android.hardware.Camera API.
*
* 2.0: Initial release of expanded-capability HAL (Android 4.2) [camera2.h]:
*
* - Sufficient for implementing existing android.hardware.Camera API.
*
* - Allows for ZSL queue in camera service layer
*
* - Not tested for any new features such manual capture control, Bayer RAW
* capture, reprocessing of RAW data.
* 3.0: First revision of expanded-capability HAL:
*
* - Major version change since the ABI is completely different. No change to
* the required hardware capabilities or operational model from 2.0.
*
* - Reworked input request and stream queue interfaces: Framework calls into
* HAL with next request and stream buffers already dequeued. Sync framework
* support is included, necessary for efficient implementations.
*
* - Moved triggers into requests, most notifications into results.
*
* - Consolidated all callbacks into framework into one structure, and all
* setup methods into a single initialize() call.
* - Made stream configuration into a single call to simplify stream
* management. Bidirectional streams replace STREAM_FROM_STREAM construct.