-、Android app
private Camera mCamera; <<==========>> frameworks/base/core/java/android/hardware/Camera.java
Camera.getNumberOfCameras();<<==========>> frameworks/base/core/jni/android_hardware_Camera.cpp
二、android_hardware_Camera.cpp 调用的是 Camera.cpp 里面的方法 <<==========>>frameworks/av/camera/Camera.cpp
我们看看Camera 这个类的定义
class Camera :public CameraBase
public ::android::hardware::BnCameraClient
{
.....
}
这个类继承与CameraBase 和 BnCameraClient
三、我们看 CameraBase 这个类的定义
template
class CameraBase : public IBinder::DeathRecipient
{
.......
}
这是一个模板类该类继承于 IBinder::DeathRecipient 而 IBinder::DeathRecipient 继承于 public virtual RefBase hal层的智能指针
static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
{
return Camera::getNumberOfCameras();
}
这个方法的最终实现是在CameraBase里面
我们再看看CameraBase这个类的方法实现 <<==========>> frameworks/av/camera/CameraBase.cpp
template
int CameraBase
const sp<::android::hardware::ICameraService> cs = getCameraService();
if (!cs.get()) {
// as required by the public Java APIs
return 0;
}
int32_t count;
binder::Status res = cs->getNumberOfCameras(
::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
&count);
if (!res.isOk()) {
ALOGE("Error reading number of cameras: %s",
res.toString8().string());
count = 0;
}
return count;
}
我们发现该方法最终调用了getCameraService 方法来获取了CameraService 服务。再继续跟踪代码,
template
const sp<::android::hardware::ICameraService> CameraBase
{
Mutex::Autolock _l(gLock);
if (gCameraService.get() == 0) {
char value[PROPERTY_VALUE_MAX];
property_get("config.disable_cameraservice", value, "0");
if (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0) {
return gCameraService;
}
sp
sp
do {
binder = sm->getService(String16(kCameraServiceName));
if (binder != 0) {
break;
}
ALOGW("CameraService not published, waiting...");
usleep(kCameraServicePollDelay);
} while(true);
if (gDeathNotifier == NULL) {
gDeathNotifier = new DeathNotifier();
}
binder->linkToDeath(gDeathNotifier);
gCameraService = interface_cast<::android::hardware::ICameraService>(binder);
}
ALOGE_IF(gCameraService == 0, "no CameraService!?");
return gCameraService;
}
我们发现 getCameraService 方法返回了一个静态全局指针 gCameraService 继续查找 gCameraService 的声明与初始化,
发现在 CameraService.cpp 里面初始化 <<==========>> frameworks/av/services/camera/libcameraservice/CameraService.cpp
static CameraService *gCameraService;
CameraService::CameraService() :
mEventLog(DEFAULT_EVENT_LOG_LENGTH),
mNumberOfCameras(0), mNumberOfNormalCameras(0),
mSoundRef(0), mModule(nullptr) {
ALOGI("CameraService started (pid=%d)", getpid());
gCameraService = this;
this->camera_device_status_change = android::camera_device_status_change;
this->torch_mode_status_change = android::torch_mode_status_change;
mServiceLockWrapper = std::make_shared
}
而 CameraServic的初始化是在android开机的时候就启动了的这个服务,这个我们后续再详细讲解。
我们继续跟踪代码 cs->getNumberOfCameras(::android::hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,&count);
此方法的实现为
Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
ATRACE_CALL();
switch (type) {
case CAMERA_TYPE_BACKWARD_COMPATIBLE:
if(0 == mNumberOfNormalCameras) {
ALOGE("No camera be found ! check again...");
onFirstRef();
}
*numCameras = mNumberOfNormalCameras;
break;
case CAMERA_TYPE_ALL:
*numCameras = mNumberOfCameras;
break;
default:
ALOGW("%s: Unknown camera type %d",
__FUNCTION__, type);
return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
"Unknown camera type %d", type);
}
return Status::ok();
}
如果开机阶段没有找到Camera 这里会从新再找一次。 我们假设这里没有找到camera 继续跟踪代码。 跟踪onFirstRef();
进入 onFirstRef();我们跟踪 hw_get_module()方法。 <<==========>> hardware/libhardware$
int hw_get_module(const char *id, const struct hw_module_t **module)
{
return hw_get_module_by_class(id, NULL, module);
}
hw_get_module()方法只是对 hw_get_module_by_class(id, NULL, module)方法的一种封装,
hw_get_module_by_class(id, NULL, module) 方法能够根据模块ID寻找硬件模块动态链接库的地址,然后调用load打开动态链接库从中获取硬件模块地址