【Android系列1.2 HAL---硬件抽象層- 驅動規範 】

HAL

hardware module(硬件模塊、驅動)的規範

HAL 對比 應用開發(驅動開發)

  • 就是FrameWork層(內核)
  • Activity(hw_module_t)
  • Activity.onCreate (*open)

規範

  • 必須”繼承“自hw_module_t
    • 作者、版本等描述
    • 實現 hw_module_methods_t 結構體(內 有個函數指針 open)

/hardware/libhardware/include/hardware/Hardware.h

typedef struct hw_module_t {
	struct hw_module_methods_t* methods;
	...
} hw_module_t

typedef struct hw_module_methods_t {
	int (*open) (const struct hw_module_t* module, const char( id, struct hw_device_t** device);
} hw_module_methods_t

typedef struct hw_device_t {
    /** tag must be initialized to HARDWARE_DEVICE_TAG */
    uint32_t tag;
    /** version number for hw_device_t */
    uint32_t version;
    /** reference to the module this device belongs to */
    struct hw_module_t* module;
    /** padding reserved for future use */
    uint32_t reserved[12];
    /** Close this device */
    int (*close)(struct hw_device_t* device);
} hw_device_t;

gralloc_module_t是 hw_module_t的”子類“

  • 所謂"繼承"(c語言沒有繼承),”子類“第一個成員變量 是”父類“
typedef struct gralloc_module_t {
struct hw_module_t common;
int (*registerBuffer)(***)
int (unregisterBuffer)(***)
//還有 lock、unlock、perform、reserved_proc
  1. HAL通過hw_get_module函數獲取hw_module_t
  2. HAL通過hw_module_t->methods->open獲取hw_device_t指針,並在此open函數中初始化hw_device_t的包裝結構中的函數及hw_device_t中的close函數,如gralloc_device_open。
  3. 三個重要的數據結構:
    • struct hw_device_t: 表示硬件設備,存儲了各種硬件設備的公共屬性和方法
    • struct hw_module_t: 可用hw_get_module進行加載的module
    • struct hw_module_methods_t: 用於定義操作設備的方法,其中只定義了一個

舉例

針對圖像相關的硬件模塊 Malloc

聲明Gralloc硬件模塊

typedef struct gralloc_module_t {
    struct hw_module_t common;
    ....

調用Gralloc 硬件模塊

hw_module_t const* module;
if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章