Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android Camera 從 AP 到 HAL 的呼叫流程

本文梳理 Android Camera 從應用層一路到 HAL 的呼叫路徑。

1、首先呼叫 framework 層的 Camera.java(frameworks\base\core\java\Android\hardware)的 takePicture 方法。

2、接著進入 JNI 層的 android_hardware_Camera.cpp(frameworks\base\core\jni)的 android_hardware_Camera_takePicture 方法,其中會透過 get_native_camera 取得一個 Camera 物件。

3、上述 Camera 物件定義在 Camera.h(frameworks\av\include\camera),該檔同時定義了 CameraInfo。Camera 繼承自 public BnCameraClient 與 DeathRecipient,真正的實作在 Camera.cpp(frameworks\av\camera)。BnCameraClient 作為 CameraService 的回呼而存在,Camera 物件內含一個 mCameraService 變數用於保存 CameraService 引用。

class Camera : public BnCameraClient, public IBinder::DeathRecipient

const sp<ICameraService>& Camera::getCameraService()
{
    Mutex::Autolock _l(mLock);
    if (mCameraService.get() == 0) {
        sp<IServiceManager> sm = defaultServiceManager();
        sp<IBinder> binder;
        do {
            binder = sm->getService(String16("media.camera"));
            if (binder != 0)
                break;
            ALOGW("CameraService not published, waiting...");
            usleep(500000); // 0.5 s
        } while(true);
        if (mDeathNotifier == NULL) {
            mDeathNotifier = new DeathNotifier();
        }
        binder->linkToDeath(mDeathNotifier);
        mCameraService = interface_cast<ICameraService>(binder);
    }

4、在 ICameraClient.h 與 ICameraService.h 中,ICameraClient、ICameraService 與 ICamera 都繼承自 IInterface,也就是 Binder 通信的基礎:

class ICameraClient: public IInterface
class ICameraService : public IInterface
class ICamera: public IInterface

接著可以發現,BnCameraClient 繼承自 BnInterface<ICameraClient>,作為 ICameraClient 的服務器端;BnCameraService 繼承自 BnInterface<ICameraService>,作為 ICameraService 的服務器端;BnCamera 繼承自 BnInterface<ICamera>,作為 ICamera 的服務器端:

class BnCameraClient: public BnInterface<ICameraClient>
class BnCameraService: public BnInterface<ICameraService>
class BnCamera: public BnInterface<ICamera>

對應上述三者,各自還有一個作為 Binder 客戶端的類 Bp***:

class BpCameraClient: public BpInterface<ICameraClient>
class BpCameraService: public BpInterface<ICameraService>
class BpCamera: public BpInterface<ICamera>

這三組的實作分別在 ICameraClient.cpp、ICameraService.cpp、ICamera.cpp 中,各自實現了 Binder 的 Server 端與 Client 端,而 Camera 的常用方法都在 ICamera 中實作。

Copyright © Linux教程網 All Rights Reserved