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);
}
}
status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
PixelFormat format, uint32_t usage, buffer_handle_t* handle,
uint32_t* stride){
err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
static_cast<int>(height), format, static_cast<int>(usage), handle,
&outStride);
}
typedef struct native_handle
{
int version; /* sizeof(native_handle_t) /
int numFds; / number of file-descriptors at &data[0] /
int numInts; / number of ints at &data[numFds] /
int data[0]; / numFds + numInts ints /
} native_handle_t;
native_handle_t native_handle_create(int numFds, int numInts){
size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
native_handle_t* h = malloc(mallocSize);
if (h) {
h->version = sizeof(native_handle_t);
h->numFds = numFds;
h->numInts = numInts;
}
return h;
}
GraphicBuffer 通过initSize()调用allocator.alloc(inWidth, inHeight, inFormat, inUsage,&handle, &outStride); 去分配内存 , allocator对应硬件设备去执行内存分配
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);
}
包含alloc 和free 两个函数指针
typedef struct alloc_device_t {
struct hw_device_t common;
/
* (alloc)() Allocates a buffer in graphic memory with the requested
* parameters and returns a buffer_handle_t and the stride in pixels to
* allow the implementation to satisfy hardware constraints on the width
* of a pixmap (eg: it may have to be multiple of 8 pixels).
* The CALLER TAKES OWNERSHIP of the buffer_handle_t.
*
* If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be
* 0, since the actual strides are available from the android_ycbcr
* structure.
int (alloc)(struct alloc_device_t dev,int w, int h, int format, int usage,
buffer_handle_t* handle, int* stride);
int (free)(struct alloc_device_t dev,
buffer_handle_t handle);
hardware/libhardware/modules/gralloc/gralloc.cpp
static int gralloc_alloc(alloc_device_t* dev,
int w, int h, int format, int usage,
buffer_handle_t* pHandle, int* pStride){
int align = 4; //对齐和format影响存储大小
switch (format) {
case HAL_PIXEL_FORMAT_RGBA_8888:
case HAL_PIXEL_FORMAT_RGBX_8888:
case HAL_PIXEL_FORMAT_BGRA_8888:
bpp = 4;
break;
case HAL_PIXEL_FORMAT_RGB_888:
bpp = 3;
break;
case HAL_PIXEL_FORMAT_RGB_565:
case HAL_PIXEL_FORMAT_RAW16:
bpp = 2;
break;
default:
return -EINVAL;
}
size_t bpr = (w*bpp + (align-1)) & ~(align-1);
size = bpr * h;
stride = bpr / bpp; //stride是什么作用?
if (usage & GRALLOC_USAGE_HW_FB) { //用fb中映射的显存
err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
} else { //使用共享内存
err = gralloc_alloc_buffer(dev, size, usage, pHandle);
}
}
static int gralloc_alloc_framebuffer(alloc_device_t* dev,
size_t size, int usage, buffer_handle_t* pHandle){
gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle); }
static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
size_t size, int usage, buffer_handle_t* pHandle){
if (m->framebuffer == NULL)
mapFrameBufferLocked(m);// 打开 "/dev/graphics/fb%u",
"/dev/fb%u", fd = open(name, O_RDWR, 0); 设备, mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 到文件句柄
hnd->base = vaddr;
hnd->offset = vaddr - intptr_t(m->framebuffer->base);
*pHandle = hnd; //返回buffer_handle_t 地址
}
static int gralloc_alloc_buffer(alloc_device_t* dev,
size_t size, int /usage/, buffer_handle_t* pHandle)
{
fd = ashmem_create_region("gralloc-buffer", size);
mapBuffer(module, hnd);// mmap(0, size, //map到匿名共享内存
PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
}