默认的Gralloc模块实现的代码路径如下:
hardware/libhardware/modules/gralloc/*
- $ ls hardware/libhardware/modules/gralloc/
- Android.mk framebuffer.cpp gralloc.cpp gralloc_priv.h gr.h mapper.cpp
int gralloc_device_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
int status = -EINVAL;
if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { //打开alloc_device_t设备
gralloc_context_t *dev;
dev = (gralloc_context_t*)malloc(sizeof(*dev));
/* initialize our state here */
memset(dev, 0, sizeof(*dev));
/* initialize the procs */
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = 0;
dev->device.common.module = const_cast<hw_module_t*>(module);
dev->device.common.close = gralloc_close;
dev->device.alloc = gralloc_alloc; //alloc_device_t的alloc接口
dev->device.free = gralloc_free; //alloc_device_t的free接口
*device = &dev->device.common;
status = 0;
} else {
status = fb_device_open(module, name, device); //打开framebuffer_device_t设备
}
return status;
}
private_module_t 扩展了 gralloc_module_t 结构:
struct private_module_t HAL_MODULE_INFO_SYM= {
base:{ //base 就是gralloc_module_t
common:{
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: GRALLOC_HARDWARE_MODULE_ID,
name:"Graphics Memory Allocator Module",
author:"The Android Open Source Project",
methods:&gralloc_module_methods
},
registerBuffer: gralloc_register_buffer, //都是gralloc_module_t的方法,这些方法都在mapper.cpp中实现。
unregisterBuffer: gralloc_unregister_buffer,
lock: gralloc_lock,
unlock: gralloc_unlock,
},
framebuffer: 0,
flags: 0,
numBuffers: 0,
bufferMask: 0,
lock: PTHREAD_MUTEX_INITIALIZER,
currentBuffer: 0,
};
private_module_t 原型定义在gralloc_priv.h中 :
struct private_module_t{
gralloc_module_t base;
private_handle_t* framebuffer;
uint32_t flags;
uint32_t numBuffers;
uint32_t bufferMask;
pthread_mutex_t lock;
buffer_handle_t currentBuffer;
int pmem_master;
void* pmem_master_