2009-04-10 23:24:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2009-06-12 01:32:05 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2009-07-07 18:29:00 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2009-06-12 01:32:05 +02:00
|
|
|
#include <sys/ioctl.h>
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
#include <cutils/ashmem.h>
|
|
|
|
#include <cutils/log.h>
|
2009-06-12 01:32:05 +02:00
|
|
|
#include <cutils/atomic.h>
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <hardware/gralloc.h>
|
|
|
|
|
|
|
|
#include "gralloc_priv.h"
|
2009-12-15 03:27:09 +01:00
|
|
|
#include "gr.h"
|
2009-07-17 03:04:54 +02:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
struct gralloc_context_t {
|
|
|
|
alloc_device_t device;
|
|
|
|
/* our private data here */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gralloc_alloc_buffer(alloc_device_t* dev,
|
|
|
|
size_t size, int usage, buffer_handle_t* pHandle);
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int fb_device_open(const hw_module_t* module, const char* name,
|
|
|
|
hw_device_t** device);
|
|
|
|
|
|
|
|
static int gralloc_device_open(const hw_module_t* module, const char* name,
|
|
|
|
hw_device_t** device);
|
|
|
|
|
|
|
|
extern int gralloc_lock(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle, int usage,
|
2009-05-04 23:26:56 +02:00
|
|
|
int l, int t, int w, int h,
|
|
|
|
void** vaddr);
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
extern int gralloc_unlock(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle);
|
|
|
|
|
2009-05-04 23:26:56 +02:00
|
|
|
extern int gralloc_register_buffer(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle);
|
|
|
|
|
|
|
|
extern int gralloc_unregister_buffer(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle);
|
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static struct hw_module_methods_t gralloc_module_methods = {
|
2013-11-07 02:27:44 +01:00
|
|
|
.open = gralloc_device_open
|
2009-04-10 23:24:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct private_module_t HAL_MODULE_INFO_SYM = {
|
2013-11-07 02:27:44 +01:00
|
|
|
.base = {
|
|
|
|
.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
|
2009-04-10 23:24:31 +02:00
|
|
|
},
|
2013-11-07 02:27:44 +01:00
|
|
|
.registerBuffer = gralloc_register_buffer,
|
|
|
|
.unregisterBuffer = gralloc_unregister_buffer,
|
|
|
|
.lock = gralloc_lock,
|
|
|
|
.unlock = gralloc_unlock,
|
2009-04-10 23:24:31 +02:00
|
|
|
},
|
2013-11-07 02:27:44 +01:00
|
|
|
.framebuffer = 0,
|
|
|
|
.flags = 0,
|
|
|
|
.numBuffers = 0,
|
|
|
|
.bufferMask = 0,
|
|
|
|
.lock = PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
.currentBuffer = 0,
|
2009-04-10 23:24:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
|
|
|
|
size_t size, int usage, buffer_handle_t* pHandle)
|
|
|
|
{
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
|
|
|
|
// allocate the framebuffer
|
|
|
|
if (m->framebuffer == NULL) {
|
2009-05-04 23:26:56 +02:00
|
|
|
// initialize the framebuffer, the framebuffer is mapped once
|
|
|
|
// and forever.
|
2009-04-10 23:24:31 +02:00
|
|
|
int err = mapFrameBufferLocked(m);
|
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t bufferMask = m->bufferMask;
|
|
|
|
const uint32_t numBuffers = m->numBuffers;
|
|
|
|
const size_t bufferSize = m->finfo.line_length * m->info.yres;
|
|
|
|
if (numBuffers == 1) {
|
|
|
|
// If we have only one buffer, we never use page-flipping. Instead,
|
|
|
|
// we return a regular buffer which will be memcpy'ed to the main
|
|
|
|
// screen when post is called.
|
|
|
|
int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
|
|
|
|
return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferMask >= ((1LU<<numBuffers)-1)) {
|
|
|
|
// We ran out of buffers.
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a "fake" handles for it
|
|
|
|
intptr_t vaddr = intptr_t(m->framebuffer->base);
|
|
|
|
private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
|
|
|
|
private_handle_t::PRIV_FLAGS_FRAMEBUFFER);
|
|
|
|
|
|
|
|
// find a free slot
|
|
|
|
for (uint32_t i=0 ; i<numBuffers ; i++) {
|
|
|
|
if ((bufferMask & (1LU<<i)) == 0) {
|
|
|
|
m->bufferMask |= (1LU<<i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vaddr += bufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
hnd->base = vaddr;
|
2009-06-11 01:06:28 +02:00
|
|
|
hnd->offset = vaddr - intptr_t(m->framebuffer->base);
|
2009-04-10 23:24:31 +02:00
|
|
|
*pHandle = hnd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gralloc_alloc_framebuffer(alloc_device_t* dev,
|
|
|
|
size_t size, int usage, buffer_handle_t* pHandle)
|
|
|
|
{
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
pthread_mutex_lock(&m->lock);
|
|
|
|
int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle);
|
|
|
|
pthread_mutex_unlock(&m->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gralloc_alloc_buffer(alloc_device_t* dev,
|
2014-02-06 03:21:09 +01:00
|
|
|
size_t size, int /*usage*/, buffer_handle_t* pHandle)
|
2009-04-10 23:24:31 +02:00
|
|
|
{
|
2009-06-12 01:32:05 +02:00
|
|
|
int err = 0;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
size = roundUpToPageSize(size);
|
|
|
|
|
2009-12-15 03:27:09 +01:00
|
|
|
fd = ashmem_create_region("gralloc-buffer", size);
|
2009-07-07 21:43:35 +02:00
|
|
|
if (fd < 0) {
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("couldn't create ashmem (%s)", strerror(-errno));
|
2009-07-07 21:43:35 +02:00
|
|
|
err = -errno;
|
|
|
|
}
|
|
|
|
|
2009-06-12 01:32:05 +02:00
|
|
|
if (err == 0) {
|
2009-12-15 03:27:09 +01:00
|
|
|
private_handle_t* hnd = new private_handle_t(fd, size, 0);
|
|
|
|
gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
err = mapBuffer(module, hnd);
|
|
|
|
if (err == 0) {
|
|
|
|
*pHandle = hnd;
|
|
|
|
}
|
2009-04-10 23:24:31 +02:00
|
|
|
}
|
2009-06-12 01:32:05 +02:00
|
|
|
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
|
2009-06-12 01:32:05 +02:00
|
|
|
|
|
|
|
return err;
|
2009-04-10 23:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2015-10-01 21:51:34 +02:00
|
|
|
inline size_t align(size_t value, size_t alignment)
|
|
|
|
{
|
|
|
|
return ((value + alignment - 1) / alignment) * alignment;
|
|
|
|
}
|
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
static int gralloc_alloc(alloc_device_t* dev,
|
2015-10-01 21:51:34 +02:00
|
|
|
int width, int height, int format, int usage,
|
2009-04-10 23:24:31 +02:00
|
|
|
buffer_handle_t* pHandle, int* pStride)
|
|
|
|
{
|
|
|
|
if (!pHandle || !pStride)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-10-01 21:51:34 +02:00
|
|
|
int bytesPerPixel = 0;
|
2010-02-16 23:04:00 +01:00
|
|
|
switch (format) {
|
|
|
|
case HAL_PIXEL_FORMAT_RGBA_8888:
|
|
|
|
case HAL_PIXEL_FORMAT_RGBX_8888:
|
|
|
|
case HAL_PIXEL_FORMAT_BGRA_8888:
|
2015-10-01 21:51:34 +02:00
|
|
|
bytesPerPixel = 4;
|
2010-02-16 23:04:00 +01:00
|
|
|
break;
|
|
|
|
case HAL_PIXEL_FORMAT_RGB_888:
|
2015-10-01 21:51:34 +02:00
|
|
|
bytesPerPixel = 3;
|
2010-02-16 23:04:00 +01:00
|
|
|
break;
|
|
|
|
case HAL_PIXEL_FORMAT_RGB_565:
|
2015-03-06 22:19:36 +01:00
|
|
|
case HAL_PIXEL_FORMAT_RAW16:
|
2015-10-01 21:51:34 +02:00
|
|
|
bytesPerPixel = 2;
|
2010-02-16 23:04:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2009-04-10 23:24:31 +02:00
|
|
|
}
|
2015-10-01 21:51:34 +02:00
|
|
|
|
|
|
|
const size_t tileWidth = 2;
|
|
|
|
const size_t tileHeight = 2;
|
|
|
|
|
|
|
|
size_t stride = align(width, tileWidth);
|
|
|
|
size_t size = align(height, tileHeight) * stride * bytesPerPixel + 4;
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
int err;
|
|
|
|
if (usage & GRALLOC_USAGE_HW_FB) {
|
|
|
|
err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
|
|
|
|
} else {
|
|
|
|
err = gralloc_alloc_buffer(dev, size, usage, pHandle);
|
|
|
|
}
|
2009-06-26 02:38:50 +02:00
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pStride = stride;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2009-07-08 02:53:43 +02:00
|
|
|
if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
|
2009-04-10 23:24:31 +02:00
|
|
|
// 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);
|
2009-07-08 02:53:43 +02:00
|
|
|
} else {
|
|
|
|
gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
terminateBuffer(module, const_cast<private_handle_t*>(hnd));
|
|
|
|
}
|
2009-05-04 23:26:56 +02:00
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
close(hnd->fd);
|
|
|
|
delete hnd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static int gralloc_close(struct hw_device_t *dev)
|
|
|
|
{
|
|
|
|
gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
|
|
|
|
if (ctx) {
|
|
|
|
/* TODO: keep a list of all buffer_handle_t created, and free them
|
2009-05-04 23:26:56 +02:00
|
|
|
* all here.
|
2009-04-10 23:24:31 +02:00
|
|
|
*/
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
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;
|
|
|
|
dev->device.free = gralloc_free;
|
|
|
|
|
|
|
|
*device = &dev->device.common;
|
|
|
|
status = 0;
|
|
|
|
} else {
|
|
|
|
status = fb_device_open(module, name, device);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|