这篇文章我们主要分析下显示中gralloc模块分配内存以及一些数据结构的介绍。
在博客http://blog.csdn.net/kc58236582/article/details/52681363中,我们分析过了从BufferQueueProducer的dequeueBuffer函数开始分配内存的流程。我们直接从如下开始分析。
分配内存流程
我们是调用了GraphicBufferAllocator的alloc函数,并且最后内存地址是保存在buffer_handle_t类型的handle入参中。
status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
PixelFormat format, uint32_t usage, buffer_handle_t* handle,
uint32_t* stride)
{
if (!width || !height)
width = height = 1;
// we have a h/w allocator and h/w buffer is requested
status_t err;
// Filter out any usage bits that should not be passed to the gralloc module
usage &= GRALLOC_USAGE_ALLOC_MASK;
int outStride = 0;
err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
static_cast<int>(height), format, static_cast<int>(usage), handle,
&outStride);
......
}
我们是调用了mAllocDev的alloc函数,而mAllocDev的赋值如下
GraphicBufferAllocator::GraphicBufferAllocator()
: mAllocDev(0)
{
hw_module_t const* module;
int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
if (err == 0) {
gralloc_open(module, &mAllocDev);
}
}
这个函数的定义在hardware/libhardware/include/hardware/gralloc.h中
static inline int gralloc_open(const struct hw_module_t* module,
struct alloc_device_t** device) {
return module->methods->open(module,
GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);
}
再看如下定义,当然这块hal就是每个厂商自己的了(比如arm 高通),因此最后gralloc_open函数是调用了gralloc_device_open函数
static struct hw_module_methods_t gralloc_module_methods =
{
open: gralloc_device_open
};
private_module_t::private_module_t()
{
#define INIT_ZERO(obj) (memset(&(ob