Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

從應用到驅動(二):Camera HAL 的基本實現框架

編輯:關於 Android 程式設計

原本打算在一篇裡把 Camera HAL 的實現與流程寫完,結果東西實在太多,只好拆成兩篇。本篇先建立框架:HAL 到底是什麼、系統怎麼找到它、一個 HAL 模組最少要實作哪些東西。下一篇再談具體流程。

初學者最大的疑問:系統如何呼叫到硬體?

答案藏在 CameraService 裡。既然呼叫硬體的是它,那就直接去看它怎麼找硬體。

在 CameraService.cpp 的 onFirstRef() 中,會找到 hw_get_module() 這個呼叫——看名字就知道是「取得硬體模組」,關鍵就在這裡。

onFirstRef() 是什麼時候被呼叫的?

onFirstRef() 定義在父類 RefBase 中,當有 sp(強參照)包裝的物件首次增加引用計數時觸發。也就是說,當 CameraService 這個 sp 包裝的類別第一次被初始化時,它就會被呼叫。這機制保證了「只初始化一次」。

宣告在 frameworks/base/services/camera/libcameraservice/CameraService.h 中:

class CameraService : public BinderService<CameraService>, ...

透過 BinderService 的機制,service 在 main() 中 publishAndJoinThreadPool() 或首次被 addService 時完成初始化。這部分細節先不展開,否則寫不完。

hw_get_module() 的實作

位置:hardware/libhardware/hardware.c

它的流程可以濃縮成一句話:用 ID 字串去比對所有已註冊的硬體模組,找到符合的那一個。核心判斷是:

if (strcmp(id, hmi->id) != 0)

這裡的 id 是 framework 層傳進來的,在 CameraService.cpp 中直接賦值(如 CAMERA_HARDWARE_MODULE_ID);而 hmi->id 則是硬體層必須自己定義的。

hmi 從哪來?從 dlsym() 取得。也就是說,HAL 必須導出一個特定名稱的結構體符號,dynamic linker 才找得到它。這也是 hardware.h 中特別說明的:實作一個 HAL 的第一步,就是提供名為 HAL_MODULE_INFO_SYM 的結構體。

Step 1:實作 HAL_MODULE_INFO_SYM 結構體

該結構體必須以 hw_module_t 開頭,後面接這個模組特有的欄位。Camera 的定義在 hardware/libhardware/include/hardware/camera.h:

typedef struct camera_module {
    hw_module_t common;
    int (*get_number_of_cameras)(void);
    int (*get_camera_info)(int camera_id, struct camera_info *info);
    int (*set_callbacks)(const camera_module_callbacks_t *callbacks);
    void (*get_vendor_tag_ops)(vendor_tag_ops_t* ops);
    int (*open_legacy)(const struct hw_module_t* module,
                       const char* id, uint32_t halVersion);
    int (*set_torch_mode)(const char* camera_id, bool enabled);
    int (*init)(void);
    int (*connect)(int camera_id, int mode);
    int (*disconnect)(int camera_id);
    int (*add_device)(int camera_id);
    int (*remove_device)(int camera_id);
    // ...
} camera_module_t;

// 導出示別名必須是 HAL_MODULE_INFO_SYM
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"
#define CAMERA_HARDWARE_MODULE_ID "camera"

結構體有了,接著要給它初始值並實作其中的函式指標。

Step 2:open 函式的實作與作用

open() 是 hw_module_methods_t 中的成員,職責是「依 ID 開啟指定裝置並填好 device 結構體」。Camera HAL 的實作經過一層包裝,最終呼叫 HAL_camera_device_open()。

hardware.h 對此有明確規範:每個裝置結構體都必須以 hw_device_t 開頭,後面接 methods 與 attributes。因此做法是:定義一個 static 的結構體,把欄位填好,回傳它的指標。

static int HAL_camera_device_open(const struct hw_module_t* module,
                                  const char* id,
                                  struct hw_device_t** device)
{
    int cameraId = atoi(id);
    if (cameraId >= MAX_NUM_CAMERAS) {
        ALOGE("%s: invalid camera ID %s", __FUNCTION__, id);
        return -EINVAL;
    }

    if (gCameraDeviceOpen(cameraId) != NO_ERROR) {
        return -EUSERS;
    }

    camera_device_t *camera_device = ...;
    camera_device->common.tag = HARDWARE_DEVICE_TAG;
    camera_device->common.version = CAMERA_DEVICE_API_VERSION_1_0;
    camera_device->common.module = (struct hw_module_t *) module;
    camera_device->common.close = HAL_close_camera_device;
    camera_device->ops = &camera_device_ops;   // 關鍵:掛上操作函式表

    *device = &camera_device->common;
    return 0;
}

這裡的 camera_device->ops 就是整個 HAL 的樞紐:它把「設定預覽、拍照、停止」等抽象操作對應到具體實作,上層才能直接呼叫。

Step 3:具體裝置函式的實作

camera_device_ops 是一張函式指標表,裡面每個成員都要指向真實的實作:

static camera_device_ops_t camera_device_ops = {
    set_preview_window:         HAL_set_preview_window,
    set_callbacks:              HAL_set_callbacks,
    enable_msg_type:            HAL_enable_msg_type,
    disable_msg_type:           HAL_disable_msg_type,
    msg_type_enabled:           HAL_msg_type_enabled,
    start_preview:              HAL_start_preview,
    stop_preview:               HAL_stop_preview,
    preview_enabled:            HAL_preview_enabled,
    store_meta_data_in_buffers: HAL_store_meta_data_in_buffers,
    start_recording:            HAL_start_recording,
    stop_recording:             HAL_stop_recording,
    recording_enabled:          HAL_recording_enabled,
    release_recording_frame:    HAL_release_recording_frame,
    auto_focus:                 HAL_auto_focus,
    cancel_auto_focus:          HAL_cancel_auto_focus,
    take_picture:               HAL_take_picture,
    cancel_picture:             HAL_cancel_picture,
    set_parameters:             HAL_set_parameters,
    get_parameters:             HAL_get_parameters,
    put_parameters:             HAL_put_parameters,
    send_command:               HAL_send_command,
    release:                    HAL_release,
    dump:                       HAL_dump_state,
};

上層如何呼叫到這張表

以 start_preview 為例,CameraService 拿到 camera_device_t 之後,呼叫 device->ops->start_preview(device),就落到 HAL 的 HAL_start_preview()。整個路徑是:

App → Camera (Java) → Camera Native (libcamera_client) → CameraService → camera_device_ops → HAL 實作 → driver (V4L2 / 平台 ISP)

一路下來,framework 與 HAL 的交界就在那張 camera_device_ops 表上。

小結

寫一個 HAL 其實就是完成三件事:

Step 1:導出名為 HAL_MODULE_INFO_SYM 的 hw_module_t 結構體(含 id 與 methods)。
Step 2:實作 open(),填入 hw_device_t 並回傳。
Step 3:實作裝置操作函式表,讓上層能透過它下達指令。

三者彼此協作,就構成一個可用的 HAL。下一篇再來看 camera HAL 的資料流與流程細節。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved