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 <sys/mman.h>
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include <cutils/ashmem.h>
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <hardware/gralloc.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2009-07-07 18:29:00 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <cutils/atomic.h>
|
|
|
|
|
2015-08-13 01:42:13 +02:00
|
|
|
#ifdef __ANDROID__
|
2009-04-10 23:24:31 +02:00
|
|
|
#include <linux/fb.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gralloc_priv.h"
|
2009-08-19 02:35:44 +02:00
|
|
|
#include "gr.h"
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2014-02-25 09:20:34 +01:00
|
|
|
// Set TARGET_USE_PAN_DISPLAY to true at compile time if the
|
|
|
|
// board uses FBIOPAN_DISPLAY to setup page flipping, otherwise
|
|
|
|
// default ioctl to do page-flipping is FBIOPUT_VSCREENINFO.
|
|
|
|
#ifndef USE_PAN_DISPLAY
|
|
|
|
#define USE_PAN_DISPLAY 0
|
|
|
|
#endif
|
|
|
|
|
2009-07-07 05:49:05 +02:00
|
|
|
// numbers of buffers for page flipping
|
2009-04-10 23:24:31 +02:00
|
|
|
#define NUM_BUFFERS 2
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PAGE_FLIP = 0x00000001,
|
|
|
|
LOCKED = 0x00000002
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fb_context_t {
|
|
|
|
framebuffer_device_t device;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static int fb_setSwapInterval(struct framebuffer_device_t* dev,
|
|
|
|
int interval)
|
|
|
|
{
|
|
|
|
fb_context_t* ctx = (fb_context_t*)dev;
|
|
|
|
if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
|
|
|
|
return -EINVAL;
|
|
|
|
// FIXME: implement fb_setSwapInterval
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fb_setUpdateRect(struct framebuffer_device_t* dev,
|
|
|
|
int l, int t, int w, int h)
|
|
|
|
{
|
|
|
|
if (((w|h) <= 0) || ((l|t)<0))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
fb_context_t* ctx = (fb_context_t*)dev;
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
m->info.reserved[0] = 0x54445055; // "UPDT";
|
|
|
|
m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
|
|
|
|
m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
|
|
|
|
{
|
|
|
|
if (private_handle_t::validate(buffer) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
fb_context_t* ctx = (fb_context_t*)dev;
|
|
|
|
|
|
|
|
private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(
|
|
|
|
dev->common.module);
|
|
|
|
|
|
|
|
if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
|
|
|
|
const size_t offset = hnd->base - m->framebuffer->base;
|
|
|
|
m->info.activate = FB_ACTIVATE_VBL;
|
|
|
|
m->info.yoffset = offset / m->finfo.line_length;
|
|
|
|
if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("FBIOPUT_VSCREENINFO failed");
|
2009-04-10 23:24:31 +02:00
|
|
|
m->base.unlock(&m->base, buffer);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
m->currentBuffer = buffer;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If we can't do the page_flip, just copy the buffer to the front
|
|
|
|
// FIXME: use copybit HAL instead of memcpy
|
|
|
|
|
2009-05-04 23:26:56 +02:00
|
|
|
void* fb_vaddr;
|
|
|
|
void* buffer_vaddr;
|
2009-04-10 23:24:31 +02:00
|
|
|
|
2009-05-04 23:26:56 +02:00
|
|
|
m->base.lock(&m->base, m->framebuffer,
|
|
|
|
GRALLOC_USAGE_SW_WRITE_RARELY,
|
|
|
|
0, 0, m->info.xres, m->info.yres,
|
|
|
|
&fb_vaddr);
|
|
|
|
|
|
|
|
m->base.lock(&m->base, buffer,
|
|
|
|
GRALLOC_USAGE_SW_READ_RARELY,
|
|
|
|
0, 0, m->info.xres, m->info.yres,
|
|
|
|
&buffer_vaddr);
|
|
|
|
|
|
|
|
memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
m->base.unlock(&m->base, buffer);
|
2009-05-04 23:26:56 +02:00
|
|
|
m->base.unlock(&m->base, m->framebuffer);
|
2009-04-10 23:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int mapFrameBufferLocked(struct private_module_t* module)
|
|
|
|
{
|
|
|
|
// already initialized...
|
|
|
|
if (module->framebuffer) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * const device_template[] = {
|
|
|
|
"/dev/graphics/fb%u",
|
|
|
|
"/dev/fb%u",
|
|
|
|
0 };
|
|
|
|
|
|
|
|
int fd = -1;
|
|
|
|
int i=0;
|
|
|
|
char name[64];
|
|
|
|
|
|
|
|
while ((fd==-1) && device_template[i]) {
|
|
|
|
snprintf(name, 64, device_template[i], 0);
|
|
|
|
fd = open(name, O_RDWR, 0);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
struct fb_fix_screeninfo finfo;
|
|
|
|
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
struct fb_var_screeninfo info;
|
|
|
|
if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
info.reserved[0] = 0;
|
|
|
|
info.reserved[1] = 0;
|
|
|
|
info.reserved[2] = 0;
|
|
|
|
info.xoffset = 0;
|
|
|
|
info.yoffset = 0;
|
|
|
|
info.activate = FB_ACTIVATE_NOW;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request NUM_BUFFERS screens (at lest 2 for page flipping)
|
|
|
|
*/
|
|
|
|
info.yres_virtual = info.yres * NUM_BUFFERS;
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t flags = PAGE_FLIP;
|
2014-02-25 09:20:34 +01:00
|
|
|
#if USE_PAN_DISPLAY
|
|
|
|
if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) {
|
|
|
|
ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported");
|
|
|
|
#else
|
2009-04-10 23:24:31 +02:00
|
|
|
if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
|
2014-02-25 09:20:34 +01:00
|
|
|
ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
|
|
|
|
#endif
|
2009-04-10 23:24:31 +02:00
|
|
|
info.yres_virtual = info.yres;
|
|
|
|
flags &= ~PAGE_FLIP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.yres_virtual < info.yres * 2) {
|
|
|
|
// we need at least 2 for page-flipping
|
|
|
|
info.yres_virtual = info.yres;
|
|
|
|
flags &= ~PAGE_FLIP;
|
2012-01-06 00:27:52 +01:00
|
|
|
ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
|
2009-04-10 23:24:31 +02:00
|
|
|
info.yres_virtual, info.yres*2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
|
|
|
|
return -errno;
|
|
|
|
|
2011-01-15 21:06:12 +01:00
|
|
|
uint64_t refreshQuotient =
|
2009-04-10 23:24:31 +02:00
|
|
|
(
|
|
|
|
uint64_t( info.upper_margin + info.lower_margin + info.yres )
|
|
|
|
* ( info.left_margin + info.right_margin + info.xres )
|
|
|
|
* info.pixclock
|
|
|
|
);
|
|
|
|
|
2011-01-15 21:06:12 +01:00
|
|
|
/* Beware, info.pixclock might be 0 under emulation, so avoid a
|
|
|
|
* division-by-0 here (SIGFPE on ARM) */
|
|
|
|
int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
|
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
if (refreshRate == 0) {
|
|
|
|
// bleagh, bad info from the driver
|
|
|
|
refreshRate = 60*1000; // 60 Hz
|
|
|
|
}
|
|
|
|
|
|
|
|
if (int(info.width) <= 0 || int(info.height) <= 0) {
|
|
|
|
// the driver doesn't return that information
|
|
|
|
// default to 160 dpi
|
|
|
|
info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
|
|
|
|
info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float xdpi = (info.xres * 25.4f) / info.width;
|
|
|
|
float ydpi = (info.yres * 25.4f) / info.height;
|
|
|
|
float fps = refreshRate / 1000.0f;
|
|
|
|
|
2012-01-04 21:07:12 +01:00
|
|
|
ALOGI( "using (fd=%d)\n"
|
2009-04-10 23:24:31 +02:00
|
|
|
"id = %s\n"
|
|
|
|
"xres = %d px\n"
|
|
|
|
"yres = %d px\n"
|
|
|
|
"xres_virtual = %d px\n"
|
|
|
|
"yres_virtual = %d px\n"
|
|
|
|
"bpp = %d\n"
|
|
|
|
"r = %2u:%u\n"
|
|
|
|
"g = %2u:%u\n"
|
|
|
|
"b = %2u:%u\n",
|
|
|
|
fd,
|
|
|
|
finfo.id,
|
|
|
|
info.xres,
|
|
|
|
info.yres,
|
|
|
|
info.xres_virtual,
|
|
|
|
info.yres_virtual,
|
|
|
|
info.bits_per_pixel,
|
|
|
|
info.red.offset, info.red.length,
|
|
|
|
info.green.offset, info.green.length,
|
|
|
|
info.blue.offset, info.blue.length
|
|
|
|
);
|
|
|
|
|
2012-01-04 21:07:12 +01:00
|
|
|
ALOGI( "width = %d mm (%f dpi)\n"
|
2009-04-10 23:24:31 +02:00
|
|
|
"height = %d mm (%f dpi)\n"
|
|
|
|
"refresh rate = %.2f Hz\n",
|
|
|
|
info.width, xdpi,
|
|
|
|
info.height, ydpi,
|
|
|
|
fps
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (finfo.smem_len <= 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
|
|
module->flags = flags;
|
|
|
|
module->info = info;
|
|
|
|
module->finfo = finfo;
|
|
|
|
module->xdpi = xdpi;
|
|
|
|
module->ydpi = ydpi;
|
|
|
|
module->fps = fps;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map the framebuffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
int err;
|
|
|
|
size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
|
2009-12-15 03:27:09 +01:00
|
|
|
module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
module->numBuffers = info.yres_virtual / info.yres;
|
|
|
|
module->bufferMask = 0;
|
2009-05-04 23:26:56 +02:00
|
|
|
|
2009-06-10 03:55:49 +02:00
|
|
|
void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (vaddr == MAP_FAILED) {
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
|
2009-06-10 03:55:49 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
module->framebuffer->base = intptr_t(vaddr);
|
2009-04-10 23:24:31 +02:00
|
|
|
memset(vaddr, 0, fbSize);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mapFrameBuffer(struct private_module_t* module)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&module->lock);
|
|
|
|
int err = mapFrameBufferLocked(module);
|
|
|
|
pthread_mutex_unlock(&module->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static int fb_close(struct hw_device_t *dev)
|
|
|
|
{
|
|
|
|
fb_context_t* ctx = (fb_context_t*)dev;
|
|
|
|
if (ctx) {
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fb_device_open(hw_module_t const* module, const char* name,
|
|
|
|
hw_device_t** device)
|
|
|
|
{
|
|
|
|
int status = -EINVAL;
|
|
|
|
if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
|
|
|
|
/* initialize our state here */
|
|
|
|
fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
|
|
|
|
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 = fb_close;
|
|
|
|
dev->device.setSwapInterval = fb_setSwapInterval;
|
|
|
|
dev->device.post = fb_post;
|
2009-07-07 21:20:31 +02:00
|
|
|
dev->device.setUpdateRect = 0;
|
2009-04-10 23:24:31 +02:00
|
|
|
|
|
|
|
private_module_t* m = (private_module_t*)module;
|
|
|
|
status = mapFrameBuffer(m);
|
|
|
|
if (status >= 0) {
|
|
|
|
int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
|
2011-01-16 19:52:22 +01:00
|
|
|
int format = (m->info.bits_per_pixel == 32)
|
2014-02-20 21:08:44 +01:00
|
|
|
? (m->info.red.offset ? HAL_PIXEL_FORMAT_BGRA_8888 : HAL_PIXEL_FORMAT_RGBX_8888)
|
2011-01-16 19:52:22 +01:00
|
|
|
: HAL_PIXEL_FORMAT_RGB_565;
|
2009-05-06 03:30:52 +02:00
|
|
|
const_cast<uint32_t&>(dev->device.flags) = 0;
|
2009-04-10 23:24:31 +02:00
|
|
|
const_cast<uint32_t&>(dev->device.width) = m->info.xres;
|
|
|
|
const_cast<uint32_t&>(dev->device.height) = m->info.yres;
|
|
|
|
const_cast<int&>(dev->device.stride) = stride;
|
2011-01-16 19:52:22 +01:00
|
|
|
const_cast<int&>(dev->device.format) = format;
|
2009-04-10 23:24:31 +02:00
|
|
|
const_cast<float&>(dev->device.xdpi) = m->xdpi;
|
|
|
|
const_cast<float&>(dev->device.ydpi) = m->ydpi;
|
|
|
|
const_cast<float&>(dev->device.fps) = m->fps;
|
|
|
|
const_cast<int&>(dev->device.minSwapInterval) = 1;
|
|
|
|
const_cast<int&>(dev->device.maxSwapInterval) = 1;
|
|
|
|
*device = &dev->device.common;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|