【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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章