-
static int gralloc_free(alloc_device_t* dev,
-
buffer_handle_t handle)
-
{
-
if (private_handle_t::validate(handle) < 0)
-
return -EINVAL;
-
-
private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
-
if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
-
// free this buffer
-
private_module_t* m = reinterpret_cast<private_module_t*>(
-
dev->common.module);
-
const size_t bufferSize = m->finfo.line_length * m->info.yres;
-
int index = (hnd->base - m->framebuffer->base) / bufferSize;
-
m->bufferMask &= ~(1<<index);
-
} else {
-
gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
-
dev->common.module);
-
terminateBuffer(module, const_cast<private_handle_t*>(hnd));
-
}
-
-
close(hnd->fd);
-
delete hnd;
-
return 0;
-
}
要釋放的圖形緩衝區使用參數handle來描述。前面提到,從Gralloc模塊中分配的圖形緩衝區是使用private_handle_t結構體來描述的,因此,這裏的參數handle應該指向一個private_handle_t結構體,這是通過調用private_handle_t類的靜態成員函數validate來驗證的。private_handle_t類的靜態成員函數validate的實現可以參考前面第1部分的內容。
-
int terminateBuffer(gralloc_module_t const* module,
-
private_handle_t* hnd)
-
{
-
if (hnd->base) {
-
// this buffer was mapped, unmap it now
-
gralloc_unmap(module, hnd);
-
}
-
-
return 0;
-
}
-
int gralloc_register_buffer(gralloc_module_t const* module,
-
buffer_handle_t handle)
-
{
-
if (private_handle_t::validate(handle) < 0)
-
return -EINVAL;
-
-
// if this handle was created in this process, then we keep it as is.
-
int err = 0;
-
private_handle_t* hnd = (private_handle_t*)handle;
-
if (hnd->pid != getpid()) {
-
void *vaddr;
-
err = gralloc_map(module, handle, &vaddr);
-
}
-
return err;
-
}
-
static int gralloc_map(gralloc_module_t const* module,
-
buffer_handle_t handle,
-
void** vaddr)
-
{
-
private_handle_t* hnd = (private_handle_t*)handle;
-
if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
-
size_t size = hnd->size;
-
void* mappedAddress = mmap(0, size,
-
PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
-
if (mappedAddress == MAP_FAILED) {
-
LOGE("Could not mmap %s", strerror(errno));
-
return -errno;
-
}
-
hnd->base = intptr_t(mappedAddress) + hnd->offset;
-
//LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
-
// hnd->fd, hnd->offset, hnd->size, mappedAddress);
-
}
-
*vaddr = (void*)hnd->base;
-
return 0;
-
}