Android 5.0 Camera系统源码分析(2):Camera打开流程
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
jobject weak_this, jint cameraId, jint halVersion, jstring
clientPackageName)
template <typename TCam, typename TCamTraits = CameraTraits<TCam>> class CameraBase {
...... typedef CameraBase<TCam> CameraBaseT; } 这里使用了C++模版,其实就是调用CameraBase::connect函数
return mNumberOfCameras;
}
代码很简单,返回上一篇博文讲到的,千辛万苦从hal层拿到的数据
3.2 Camera构造函数分析
回到最开始的Camera.open函数,在第10行,将会构造一个Camera对象
[cpp] view plain copy
private int cameraInitVersion(int cameraId, int halVersion) {
Android 5.0 Camera系统源码分析
(2):Camera打开流程
1. 前言 本文将分析Android系统源码,从frameworks层到hal层,暂不涉及app层 和kernel层。由于某些函数比较复杂,在贴出代码时会适当对其进行简 化。本文属于自己对源码的总结,仅仅是贯穿代码流程,不会深入分析 各个细节。欢迎联系讨论,QQ:1026656828 2. app层 从apk开始,简单列出各个入口函数 [cpp] view plain copy private void initCamera() {
mEventHandler = new EventHandler(this, looper); } else {
mEventHandler = null; }
String packageName = ActivityThread.currentPackageName();
return native_setup(new WeakReference<Camera>(this), halVersion, packageName); }
const String16& clientPackageName, int clientUid) { sp<TCam> c = new TCam(cameraId); sp<TCamCallbacks> cl = c;
后,就可以开始和Bn端通讯了。在第4行,当调用cs-
>getNumberOfCameras函数时,将会进入CameraService.cpp的
getNumberOfCameras函数
[cpp] view plain copy
int32_t CameraService::getNumberOfCameras() {
int err = cameraInitNormal(cameraId);
......
}
第14行,
native_setup同样是个JNI接口,对应
android_hardware_Camera.cpp里的android_hardware_Camera_native_setup
函数
[cpp] view plain copy
[cpp] view plain copy CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService = &ICameraService::connect;
template <typename TCam, typename TCamTraits> sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
const sp<ICameraService> cs = getCameraService(); return cs->getNumberOfCameras(); } 第3行, getCameraService函数用来获取ICameraService的Bp端,代码实现
如下 [cpp] view plain copy const char* kCameraServiceName = "media.camera";
return new Camera(i);
} } return null; } 第5行, 通过getNumberOfCameras函数来获取Camera的个数。从上一篇博 文CameraService的启动流程可以看出,这个信息保存在CameraService 中。 第10行,需重点关注,构造一个Camera对象,并将它返回给app层。 3.1 getNumberOfCameras函数分析 getNumberOfCameras函数进入到CameraService获取Camera个数的流程 如下:
weak_this, clazz, camera); #else
sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
#endif } 第4行, 调用了Camera.cpp的connect函数,同时返回一个Camera对象,保 存在JNICameraContext当中 [cpp] view plain copy sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
...... static TCamConnectService fnConnectService; };
class Camera : public CameraBase<Camera> {
...... } /* -------- CameraBase.h -------- */ <pre name="code" class="cpp">template <typename TCam> struct CameraTraits { };
return Camera::getNumberOfCameras(); } 这里只是简单调用了Camera.cpp的getNumberOfCameras函数,Camera继 承了CameraBase,该函数由它实现 [cpp] view plain copy template <typename TCam, typename TCamTraits> int CameraBase<TCam, TCamTraits>::getNumberOfCameras() {
{
camera = Camera::connect(cameraId,
clientName,
Camera::USE_CALLING_UID);
#if 1 // defined(MTK_CAMERA_BSP_SUPPORT) sp<JNICameraContext> context = new MtkJNICameraContext(env,
}
Android的Binder通讯机制
第1行,
获取的ServiceName为"media.camera",结合上一篇博文
CameraService的启动流程可以看出Bn端的实现在CameraService.cpp
回到之前的getNumberOfCameras函数,在获取到ICameraService的Bp端
ቤተ መጻሕፍቲ ባይዱ
......
Looper looper;
if ((looper = Looper.myLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else if ((looper = Looper.getMainLooper()) != null) {
template <typename TCam, typename TCamTraits>
const
sp<ICameraService>&
CameraBase<TCam,
TCamTraits>::getCameraService()
{
if (gCameraService.get() == 0) {
sp<IServiceManager> sm = defaultServiceManager();
cameraId,
private int cameraInitNormal(int cameraId) { return cameraInitVersion(cameraId,
CAMERA_HAL_API_VERSION_NORMAL_CONNECT); }
Camera(int cameraId) {
Camera.Java调用的getNumberOfCameras函数是一个JNI接口,对应的函 数是android_hardware_Camera.cpp里的 android_hardware_Camera_getNumberOfCameras函数 [cpp] view plain copy static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz) {
sp<IBinder> binder;
binder = sm->getService(String16(kCameraServiceName));
gCameraService = interface_cast<ICameraService>(binder);
}
return gCameraService;
return null; } int numberOfCameras = getNumberOfCameras(); CameraInfo cameraInfo = new CameraInfo(); for (int i = 0; i < numberOfCameras; i++) {
getCameraInfo(i, cameraInfo); if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
int clientUid) {
return CameraBaseT::connect(cameraId, clientPackageName, clientUid); } 先来看看Camera和CameraBase的类定义 [cpp] view plain copy /* ---------- Camera.h ---------- */ template <> struct CameraTraits<Camera> {
Camera mCamera = Camera.open(); Camera.Parameters mParameters = mCamera.getParameters(); mParameters.setPictureFormat(PixelFormat.JPEG); mCamera.setParameters(mParameters); mCamera.setPreviewDisplay(mSurfaceHolder); mCamera.startPreview(); mCamera.takePicture(null, null , mJpegCallback); } 3. frameworks层 这里将重点介绍Camera.open函数,其余函数将在后续博文分析。先来 看看Camera.open函数在frameworks层的实现,代码路径为: frameworks/base/core/java/android/hardware/Camera.java [cpp] view plain copy public static Camera open() { if (!isPermissionGranted()) {