RetroArch/gfx/drivers/omap_gfx.c

1188 lines
28 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2013-2014 - Tobias Jakobi
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2015-11-22 12:32:46 +00:00
#include <sys/mman.h>
#include <linux/omapfb.h>
#include <retro_inline.h>
#include <gfx/scaler/scaler.h>
2016-01-20 05:16:55 +00:00
#include <string/stdstring.h>
2015-11-22 12:32:46 +00:00
#include "../../driver.h"
#include "../../general.h"
#include "../../retroarch.h"
#include "../video_context_driver.h"
#include "../font_driver.h"
2015-11-22 12:32:46 +00:00
2015-01-26 18:42:04 +00:00
typedef struct omapfb_page
{
unsigned yoffset;
void *buf;
bool used;
} omapfb_page_t;
2015-01-26 18:42:04 +00:00
typedef struct omapfb_state
{
struct omapfb_plane_info pi;
struct omapfb_mem_info mi;
struct fb_var_screeninfo si;
void* mem;
} omapfb_state_t;
2015-01-26 18:42:04 +00:00
typedef struct omapfb_data
{
const char* fbname;
int fd;
void *fb_mem;
unsigned fb_framesize;
omapfb_page_t *pages;
int num_pages;
omapfb_page_t *cur_page;
omapfb_page_t *old_page;
/* current and saved (for later restore) states */
omapfb_state_t* current_state;
omapfb_state_t* saved_state;
/* native screen size */
unsigned nat_w, nat_h;
/* bytes per pixel */
unsigned bpp;
bool sync;
} omapfb_data_t;
2015-04-03 02:07:04 +00:00
static const char *omapfb_get_fb_device(void)
2015-01-26 18:42:04 +00:00
{
2015-06-13 00:49:29 +00:00
static char fbname[12] = {0};
settings_t *settings = config_get_ptr();
const int fbidx = settings->video.monitor_index;
2015-04-11 06:39:37 +00:00
if (fbidx == 0)
return "/dev/fb0";
2015-04-11 06:39:37 +00:00
snprintf(fbname, sizeof(fbname), "/dev/fb%d", fbidx - 1);
RARCH_LOG("[video_omap]: Using %s as framebuffer device.\n", fbname);
return fbname;
}
2015-04-03 02:07:04 +00:00
static omapfb_page_t *omapfb_get_page(omapfb_data_t *pdata)
2015-01-26 18:42:04 +00:00
{
unsigned i;
omapfb_page_t *page = NULL;
for (i = 0; i < pdata->num_pages; ++i)
{
if (&pdata->pages[i] == pdata->cur_page)
continue;
if (&pdata->pages[i] == pdata->old_page)
continue;
if (!pdata->pages[i].used)
{
page = &pdata->pages[i];
break;
}
}
2015-01-26 18:42:04 +00:00
return page;
}
2015-04-03 02:07:04 +00:00
static void omapfb_page_flip(omapfb_data_t *pdata)
2015-01-26 18:42:04 +00:00
{
2015-04-11 06:39:37 +00:00
if (pdata->sync)
ioctl(pdata->fd, OMAPFB_WAITFORGO);
2015-04-11 06:39:37 +00:00
/* TODO: should we use the manual update feature of the OMAP here? */
2015-04-11 06:39:37 +00:00
pdata->current_state->si.yoffset = pdata->cur_page->yoffset;
ioctl(pdata->fd, FBIOPAN_DISPLAY, &pdata->current_state->si);
2015-04-11 06:39:37 +00:00
if (pdata->old_page)
pdata->old_page->used = false;
}
2015-04-03 02:07:04 +00:00
static int omapfb_read_sysfs(const char *fname, char *buff, size_t size)
2015-01-26 18:42:04 +00:00
{
2015-04-11 06:39:37 +00:00
int ret;
FILE *f = fopen(fname, "r");
2015-04-11 06:39:37 +00:00
if (!f)
return -1;
2015-04-11 06:39:37 +00:00
ret = fread(buff, 1, size - 1, f);
fclose(f);
2015-01-26 18:42:04 +00:00
2015-04-11 06:39:37 +00:00
if (ret <= 0)
return -1;
2015-04-11 06:39:37 +00:00
buff[ret] = 0;
for (ret--; ret >= 0 && isspace(buff[ret]); ret--)
buff[ret] = 0;
2015-04-11 06:39:37 +00:00
return 0;
}
2015-04-03 02:07:04 +00:00
static INLINE void omapfb_put_pixel_rgb565(
uint16_t *p, unsigned r, unsigned g, unsigned b)
2015-01-26 18:42:04 +00:00
{
*p = (((r >> 3) & 0x1f) << 11) | (((g >> 2) & 0x3f) << 5) | ((b >> 3) & 0x1f);
}
2015-04-03 02:07:04 +00:00
static INLINE void omapfb_put_pixel_argb8888(
uint32_t *p, unsigned r, unsigned g, unsigned b)
2015-01-26 18:42:04 +00:00
{
2015-04-11 06:39:37 +00:00
*p = ((r << 16) & 0xff0000) | ((g << 8) & 0x00ff00) | ((b << 0) & 0x0000ff);
}
2015-01-26 18:42:04 +00:00
static int omapfb_detect_screen(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
struct stat status;
int i, ret;
int w, h;
FILE *f;
int fb_id, overlay_id = -1, display_id = -1;
2015-06-13 00:49:29 +00:00
char buff[64] = {0};
char manager_name[64] = {0};
char display_name[64] = {0};
2015-04-03 02:07:04 +00:00
/* Find out the native screen resolution, which is needed to
* properly center the scaled image data. */
ret = stat(pdata->fbname, &status);
if (ret != 0)
{
RARCH_ERR("[video_omap]: can't stat %s.\n", pdata->fbname);
return -1;
}
fb_id = minor(status.st_rdev);
2015-04-03 02:07:04 +00:00
snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id);
f = fopen(buff, "r");
if (!f)
{
RARCH_ERR("[video_omap]: can't open %s.\n", buff);
return -1;
}
2015-04-03 02:07:04 +00:00
ret = fscanf(f, "%d", &overlay_id);
fclose(f);
if (ret != 1)
{
RARCH_ERR("[video_omap]: can't parse %s.\n", buff);
return -1;
}
2015-04-03 02:07:04 +00:00
snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id);
ret = omapfb_read_sysfs(buff, manager_name, sizeof(manager_name));
if (ret < 0)
{
RARCH_ERR("[video_omap]: can't read manager name.\n");
return -1;
}
2015-01-26 18:42:04 +00:00
2015-04-03 02:07:04 +00:00
for (i = 0; ; i++)
{
snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/manager%d/name", i);
ret = omapfb_read_sysfs(buff, buff, sizeof(buff));
2015-01-26 18:42:04 +00:00
if (ret < 0)
2015-04-03 02:07:04 +00:00
break;
2016-01-20 05:16:55 +00:00
if (string_is_equal(manager_name, buff))
2015-01-26 18:42:04 +00:00
{
2015-04-03 02:07:04 +00:00
snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/manager%d/display", i);
ret = omapfb_read_sysfs(buff, display_name, sizeof(display_name));
2015-04-03 02:07:04 +00:00
if (ret < 0)
{
RARCH_ERR("[video_omap]: can't read display name.\n");
return -1;
}
2015-04-03 02:07:04 +00:00
break;
}
}
2015-04-03 02:07:04 +00:00
if (ret < 0)
{
RARCH_ERR("[video_omap]: couldn't find manager.\n");
return -1;
}
2015-04-03 02:07:04 +00:00
for (i = 0; ; i++)
{
snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i);
ret = omapfb_read_sysfs(buff, buff, sizeof(buff));
2015-01-26 18:42:04 +00:00
2015-04-03 02:07:04 +00:00
if (ret < 0)
break;
2016-01-20 05:16:55 +00:00
if (string_is_equal(display_name, buff))
2015-04-03 02:07:04 +00:00
{
display_id = i;
break;
}
}
2015-04-03 02:07:04 +00:00
if (display_id < 0)
{
RARCH_ERR("[video_omap]: couldn't find display.\n");
return -1;
}
2015-04-03 02:07:04 +00:00
snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", display_id);
f = fopen(buff, "r");
if (!f)
{
RARCH_ERR("[video_omap]: can't open %s.\n", buff);
return -1;
}
2015-04-03 02:07:04 +00:00
ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h);
fclose(f);
if (ret != 2)
{
RARCH_ERR("[video_omap]: can't parse %s (%d).\n", buff, ret);
return -1;
}
2015-04-03 02:07:04 +00:00
if (w <= 0 || h <= 0)
{
RARCH_ERR("[video_omap]: unsane dimensions detected (%dx%d).\n", w, h);
return -1;
}
2015-04-03 02:07:04 +00:00
RARCH_LOG("[video_omap]: detected %dx%d '%s' (%d) display attached to fb %d and overlay %d.\n",
w, h, display_name, display_id, fb_id, overlay_id);
2015-04-03 02:07:04 +00:00
pdata->nat_w = w;
pdata->nat_h = h;
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_setup_pages(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
int i;
2015-04-03 02:07:04 +00:00
if (!pdata->pages)
{
pdata->pages = (omapfb_page_t*)calloc(pdata->num_pages, sizeof(omapfb_page_t));
2015-04-03 02:07:04 +00:00
if (!pdata->pages)
{
RARCH_ERR("[video_omap]: pages allocation failed.\n");
return -1;
}
}
2015-04-03 02:07:04 +00:00
for (i = 0; i < pdata->num_pages; ++i)
{
pdata->pages[i].yoffset = i * pdata->current_state->si.yres;
2015-09-29 16:08:33 +00:00
pdata->pages[i].buf = (void*)((uint8_t*)pdata->fb_mem + (i * pdata->fb_framesize));
pdata->pages[i].used = false;
2015-04-03 02:07:04 +00:00
}
2015-04-03 02:07:04 +00:00
pdata->old_page = NULL;
pdata->cur_page = &pdata->pages[0];
2015-04-03 02:07:04 +00:00
memset(pdata->cur_page->buf, 0, pdata->fb_framesize);
2015-04-03 02:07:04 +00:00
omapfb_page_flip(pdata);
pdata->cur_page->used = true;
2015-04-03 02:07:04 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_mmap(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
assert(pdata->fb_mem == NULL);
2015-04-03 02:07:04 +00:00
pdata->fb_mem = mmap(NULL, pdata->current_state->mi.size, PROT_WRITE,
MAP_SHARED, pdata->fd, 0);
2015-04-03 02:07:04 +00:00
if (pdata->fb_mem == MAP_FAILED)
{
pdata->fb_mem = NULL;
RARCH_ERR("[video_omap]: framebuffer mmap failed\n");
2015-04-03 02:07:04 +00:00
return -1;
}
2015-04-03 02:07:04 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_backup_state(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
void* mem;
2015-04-03 02:07:04 +00:00
assert(pdata->saved_state == NULL);
2015-04-03 02:07:04 +00:00
pdata->saved_state = calloc(1, sizeof(omapfb_state_t));
if (!pdata->saved_state) return -1;
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, OMAPFB_QUERY_PLANE, &pdata->saved_state->pi) != 0)
{
RARCH_ERR("[video_omap]: backup layer (plane) failed\n");
return -1;
}
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, OMAPFB_QUERY_MEM, &pdata->saved_state->mi) != 0)
{
RARCH_ERR("[video_omap]: backup layer (mem) failed\n");
return -1;
}
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, FBIOGET_VSCREENINFO, &pdata->saved_state->si) != 0)
{
RARCH_ERR("[video_omap]: backup layer (screeninfo) failed\n");
return -1;
}
2015-04-03 02:07:04 +00:00
pdata->saved_state->mem = malloc(pdata->saved_state->mi.size);
mem = mmap(NULL, pdata->saved_state->mi.size, PROT_WRITE|PROT_READ,
MAP_SHARED, pdata->fd, 0);
if (pdata->saved_state->mem == NULL || mem == MAP_FAILED)
{
RARCH_ERR("[video_omap]: backup layer (mem backup) failed\n");
munmap(mem, pdata->saved_state->mi.size);
return -1;
}
memcpy(pdata->saved_state->mem, mem, pdata->saved_state->mi.size);
munmap(mem, pdata->saved_state->mi.size);
2015-04-03 02:07:04 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_alloc_mem(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
struct omapfb_plane_info pi;
struct omapfb_mem_info mi;
unsigned mem_size;
void* mem = NULL;
const struct retro_game_geometry *geom = NULL;
struct retro_system_av_info *av_info = NULL;
2015-04-03 02:07:04 +00:00
assert(pdata->current_state == NULL);
2015-04-03 02:07:04 +00:00
pdata->current_state = (omapfb_state_t*)calloc(1, sizeof(omapfb_state_t));
2015-04-03 02:07:04 +00:00
if (!pdata->current_state)
goto error;
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, OMAPFB_QUERY_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: alloc mem (query plane) failed\n");
goto error;
}
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, OMAPFB_QUERY_MEM, &mi) != 0)
{
RARCH_ERR("[video_omap]: alloc mem (query mem) failed\n");
goto error;
}
2015-04-03 02:07:04 +00:00
/* disable plane when changing memory allocation */
if (pi.enabled)
{
pi.enabled = 0;
if (ioctl(pdata->fd, OMAPFB_SETUP_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: alloc mem (disable plane) failed\n");
goto error;
}
}
av_info = video_viewport_get_system_av_info();
if (av_info)
geom = &av_info->geometry;
2015-09-28 14:20:26 +00:00
if (!geom)
goto error;
2015-04-03 02:07:04 +00:00
mem_size = geom->max_width * geom->max_height *
pdata->bpp * pdata->num_pages;
2015-04-03 02:07:04 +00:00
if (mi.size < mem_size)
{
mi.size = mem_size;
2015-04-03 02:07:04 +00:00
if (ioctl(pdata->fd, OMAPFB_SETUP_MEM, &mi) != 0)
{
RARCH_ERR("[video_omap]: allocation of %u bytes of VRAM failed\n", mem_size);
goto error;
}
}
mem = mmap(NULL, mi.size, PROT_WRITE|PROT_READ, MAP_SHARED, pdata->fd, 0);
if (mem == MAP_FAILED)
{
RARCH_ERR("[video_omap]: zeroing framebuffer failed\n");
goto error;
}
memset(mem, 0, mi.size);
munmap(mem, mi.size);
2015-04-03 02:07:04 +00:00
pdata->current_state->mi = mi;
2015-04-03 02:07:04 +00:00
/* Don't re-enable the plane here (setup not yet complete) */
2015-04-03 02:07:04 +00:00
return 0;
error:
if (pdata->current_state)
free(pdata->current_state);
return -1;
}
2015-01-26 18:42:04 +00:00
static int omapfb_setup_screeninfo(omapfb_data_t *pdata, int width, int height)
{
2015-04-11 06:39:37 +00:00
omapfb_state_t* state = pdata->current_state;
2015-04-03 02:07:04 +00:00
2015-04-11 06:39:37 +00:00
state->si.xres = width;
state->si.yres = height;
state->si.xres_virtual = width;
state->si.yres_virtual = height * pdata->num_pages;
state->si.xoffset = 0;
state->si.yoffset = 0;
state->si.bits_per_pixel = pdata->bpp * 8;
2015-04-11 06:39:37 +00:00
/* OMAPFB_COLOR_ARGB32 for bpp=4, OMAPFB_COLOR_RGB565 for bpp=2 */
state->si.nonstd = 0;
2015-04-11 06:39:37 +00:00
if (ioctl(pdata->fd, FBIOPUT_VSCREENINFO, &state->si) != 0)
{
RARCH_ERR("[video_omap]: setup screeninfo failed\n");
return -1;
}
2015-04-11 06:39:37 +00:00
pdata->fb_framesize = width * height * pdata->bpp;
2015-04-11 06:39:37 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static float omapfb_scaling(omapfb_data_t *pdata, int width, int height)
{
2015-04-11 06:39:37 +00:00
const float w_factor = (float)pdata->nat_w / (float)width;
const float h_factor = (float)pdata->nat_h / (float)height;
2015-04-11 06:39:37 +00:00
return (w_factor < h_factor ? w_factor : h_factor);
}
2015-01-26 18:42:04 +00:00
static int omapfb_setup_plane(omapfb_data_t *pdata, int width, int height)
{
2015-09-29 16:08:33 +00:00
int x, y;
2015-04-11 06:39:37 +00:00
struct omapfb_plane_info pi = {0};
float scale = omapfb_scaling(pdata, width, height);
int w = (int)(scale * width);
int h = (int)(scale * height);
RARCH_LOG("omap_video: scaling %dx%d to %dx%d\n", width, height, w, h);
x = pdata->nat_w / 2 - w / 2;
y = pdata->nat_h / 2 - h / 2;
if (width * height * pdata->bpp * pdata->num_pages > pdata->current_state->mi.size)
{
RARCH_ERR("omap_video: fb dimensions too large for allocated buffer\n");
return -1;
}
if (ioctl(pdata->fd, OMAPFB_QUERY_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: setup plane (query) failed\n");
return -1;
}
/* Disable the plane during setup to avoid garbage on screen. */
pi.pos_x = x;
pi.pos_y = y;
pi.out_width = w;
pi.out_height = h;
pi.enabled = 0;
if (ioctl(pdata->fd, OMAPFB_SETUP_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: setup plane (param = %d %d %d %d) failed\n", x, y, w, h);
return -1;
}
pdata->current_state->pi = pi;
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_enable_plane(omapfb_data_t *pdata)
{
2015-04-11 06:39:37 +00:00
struct omapfb_plane_info pi = {0};
2015-04-11 06:39:37 +00:00
if (ioctl(pdata->fd, OMAPFB_QUERY_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: enable plane (query) failed\n");
return -1;
}
2015-04-11 06:39:37 +00:00
pi.enabled = 1;
2015-04-11 06:39:37 +00:00
if (ioctl(pdata->fd, OMAPFB_SETUP_PLANE, &pi) != 0)
{
RARCH_ERR("[video_omap]: enable plane failed\n");
return -1;
}
2015-04-11 06:39:37 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static int omapfb_init(omapfb_data_t *pdata, unsigned bpp)
{
2015-04-11 06:39:37 +00:00
const char *fbname = omapfb_get_fb_device();
int fd = open(fbname, O_RDWR);
settings_t *settings = config_get_ptr();
2015-04-11 06:39:37 +00:00
if (fd == -1)
{
RARCH_ERR("[video_omap]: can't open framebuffer device\n");
return -1;
}
2015-04-11 06:39:37 +00:00
pdata->fbname = fbname;
pdata->fd = fd;
2015-04-11 06:39:37 +00:00
if (omapfb_detect_screen(pdata))
{
close(fd);
2015-04-11 06:39:37 +00:00
pdata->fbname = NULL;
pdata->fd = -1;
2015-04-11 06:39:37 +00:00
return -1;
}
2015-04-11 06:39:37 +00:00
/* always use triple buffering to reduce chance of tearing */
pdata->bpp = bpp;
pdata->num_pages = 3;
pdata->sync = settings->video.vsync;
2015-04-11 06:39:37 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
void omapfb_free(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
if (pdata->sync)
ioctl(pdata->fd, OMAPFB_WAITFORGO);
2015-04-03 02:07:04 +00:00
/* unmap the framebuffer memory */
if (pdata->fb_mem)
{
munmap(pdata->fb_mem, pdata->current_state->mi.size);
pdata->fb_mem = NULL;
}
2015-04-03 02:07:04 +00:00
/* restore the framebuffer state (OMAP plane state, screen info) */
if (pdata->saved_state)
{
void *mem;
int enabled = pdata->saved_state->pi.enabled;
2015-01-26 18:42:04 +00:00
2015-04-03 02:07:04 +00:00
/* be sure to disable while setting up */
pdata->saved_state->pi.enabled = 0;
ioctl(pdata->fd, OMAPFB_SETUP_PLANE, &pdata->saved_state->pi);
2015-04-03 02:07:04 +00:00
ioctl(pdata->fd, OMAPFB_SETUP_MEM, &pdata->saved_state->mi);
2015-04-03 02:07:04 +00:00
if (enabled)
{
pdata->saved_state->pi.enabled = enabled;
ioctl(pdata->fd, OMAPFB_SETUP_PLANE, &pdata->saved_state->pi);
}
2015-01-26 18:42:04 +00:00
2015-04-03 02:07:04 +00:00
/* restore framebuffer content */
mem = mmap(0, pdata->saved_state->mi.size, PROT_WRITE|PROT_READ,
MAP_SHARED, pdata->fd, 0);
if (mem != MAP_FAILED)
{
memcpy(mem, pdata->saved_state->mem, pdata->saved_state->mi.size);
munmap(mem, pdata->saved_state->mi.size);
}
2015-04-03 02:07:04 +00:00
/* restore screen info */
ioctl(pdata->fd, FBIOPUT_VSCREENINFO, &pdata->saved_state->si);
2015-04-03 02:07:04 +00:00
free(pdata->saved_state->mem);
pdata->saved_state->mem = NULL;
2015-04-03 02:07:04 +00:00
free(pdata->saved_state);
pdata->saved_state = NULL;
}
2015-04-03 02:07:04 +00:00
free(pdata->current_state);
pdata->current_state = NULL;
2015-04-03 02:07:04 +00:00
close(pdata->fd);
pdata->fd = -1;
}
2015-01-26 18:42:04 +00:00
static int omapfb_set_mode(omapfb_data_t *pdata, int width, int height)
{
2015-04-03 02:07:04 +00:00
if (pdata->sync)
ioctl(pdata->fd, OMAPFB_WAITFORGO);
2015-04-03 02:07:04 +00:00
if (omapfb_setup_plane(pdata, width, height) != 0)
return -1;
2015-04-03 02:07:04 +00:00
if (omapfb_setup_screeninfo(pdata, width, height) != 0 ||
omapfb_setup_pages(pdata) != 0 ||
omapfb_enable_plane(pdata) != 0)
return -1;
2015-04-03 02:07:04 +00:00
return 0;
}
2015-01-26 18:42:04 +00:00
static void omapfb_prepare(omapfb_data_t *pdata)
{
2015-04-03 02:07:04 +00:00
omapfb_page_t *page;
/* issue flip before getting free page */
omapfb_page_flip(pdata);
page = omapfb_get_page(pdata);
2015-04-03 02:07:04 +00:00
assert(page != NULL);
2015-04-03 02:07:04 +00:00
pdata->old_page = pdata->cur_page;
pdata->cur_page = page;
2015-04-03 02:07:04 +00:00
pdata->cur_page->used = true;
}
2015-01-26 18:42:04 +00:00
static void omapfb_blend_glyph_rgb565(omapfb_data_t *pdata,
const uint8_t *src, uint8_t *f_rgb,
unsigned g_width, unsigned g_height, unsigned g_pitch,
unsigned dst_x, unsigned dst_y)
{
2015-04-11 06:39:37 +00:00
unsigned x, y;
unsigned r, g, b;
unsigned dst_pitch = (pdata->current_state->si.xres * pdata->bpp) >> 1;
uint16_t *dst = (uint16_t*)pdata->cur_page->buf + dst_y * dst_pitch + dst_x;
for (y = 0; y < g_height; ++y, src += g_pitch, dst += dst_pitch)
{
for (x = 0; x < g_width; ++x)
{
const uint8_t blend = src[x];
const uint16_t out = dst[x];
if (blend == 0)
continue;
if (blend == 255)
{
omapfb_put_pixel_rgb565(&dst[x], f_rgb[0], f_rgb[1], f_rgb[2]);
continue;
}
r = (out & 0xf800) >> 11;
g = (out & 0x07e0) >> 5;
b = (out & 0x001f) >> 0;
r = (r << 3) | (r >> 2);
g = (g << 2) | (g >> 4);
b = (b << 3) | (b >> 2);
omapfb_put_pixel_rgb565(&dst[x], (r * (256 - blend) + f_rgb[0] * blend) >> 8,
(g * (256 - blend) + f_rgb[1] * blend) >> 8,
(b * (256 - blend) + f_rgb[2] * blend) >> 8);
}
}
}
2015-01-26 18:42:04 +00:00
static void omapfb_blend_glyph_argb8888(omapfb_data_t *pdata,
const uint8_t *src, uint8_t *f_rgb,
unsigned g_width, unsigned g_height, unsigned g_pitch,
unsigned dst_x, unsigned dst_y)
{
2015-04-11 06:39:37 +00:00
unsigned x, y;
unsigned r, g, b;
unsigned dst_pitch = (pdata->current_state->si.xres * pdata->bpp) >> 2;
uint32_t *dst = (uint32_t*)pdata->cur_page->buf + dst_y * dst_pitch + dst_x;
for (y = 0; y < g_height; ++y, src += g_pitch, dst += dst_pitch)
{
for (x = 0; x < g_width; ++x)
{
const uint8_t blend = src[x];
const uint32_t out = dst[x];
if (blend == 0)
continue;
if (blend == 255)
{
omapfb_put_pixel_argb8888(&dst[x], f_rgb[0], f_rgb[1], f_rgb[2]);
continue;
}
r = (out & 0xff0000) >> 16;
g = (out & 0x00ff00) >> 8;
b = (out & 0x0000ff) >> 0;
omapfb_put_pixel_argb8888(&dst[x], (r * (256 - blend) + f_rgb[0] * blend) >> 8,
(g * (256 - blend) + f_rgb[1] * blend) >> 8,
(b * (256 - blend) + f_rgb[2] * blend) >> 8);
}
}
}
static void omapfb_blit_frame(omapfb_data_t *pdata, const void *src,
2015-04-11 06:39:37 +00:00
unsigned height, unsigned src_pitch)
2015-01-26 18:42:04 +00:00
{
2015-04-03 02:07:04 +00:00
unsigned i;
void *dst = pdata->cur_page->buf;
unsigned dst_pitch = pdata->current_state->si.xres * pdata->bpp;
2015-01-26 18:42:04 +00:00
for (i = 0; i < height; i++)
memcpy(dst + dst_pitch * i, src + src_pitch * i, dst_pitch);
}
2015-01-26 18:42:04 +00:00
typedef struct omap_video
{
2015-04-11 06:39:37 +00:00
omapfb_data_t *omap;
2015-04-11 06:39:37 +00:00
void *font;
const font_renderer_driver_t *font_driver;
uint8_t font_rgb[4];
2015-04-11 06:39:37 +00:00
unsigned bytes_per_pixel;
2015-04-11 06:39:37 +00:00
/* current dimensions */
unsigned width;
unsigned height;
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
struct
{
bool active;
void *frame;
struct scaler_ctx scaler;
} menu;
} omap_video_t;
2015-01-26 18:42:04 +00:00
static void omap_gfx_free(void *data)
{
2015-04-11 06:39:37 +00:00
omap_video_t *vid = data;
if (!vid)
return;
2015-04-11 06:39:37 +00:00
omapfb_free(vid->omap);
free(vid->omap);
2015-04-11 06:39:37 +00:00
if (vid->font)
vid->font_driver->free(vid->font);
2015-04-11 06:39:37 +00:00
scaler_ctx_gen_reset(&vid->menu.scaler);
free(vid->menu.frame);
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
free(vid);
}
2015-01-26 18:42:04 +00:00
static void omap_init_font(omap_video_t *vid, const char *font_path, unsigned font_size)
{
int r, g, b;
2015-04-11 06:39:37 +00:00
settings_t *settings = config_get_ptr();
2015-03-20 21:08:36 +00:00
if (!settings->video.font_enable)
2015-01-26 18:42:04 +00:00
return;
2015-01-26 18:42:04 +00:00
if (!(font_renderer_create_default(&vid->font_driver, &vid->font,
2015-03-20 21:08:36 +00:00
*settings->video.font_path ? settings->video.font_path : NULL, settings->video.font_size)))
2015-01-26 18:42:04 +00:00
{
2015-04-03 02:07:04 +00:00
RARCH_LOG("[video_omap]: font init failed\n");
2015-01-26 18:42:04 +00:00
return;
}
2015-09-29 16:08:33 +00:00
r = settings->video.msg_color_r * 255;
g = settings->video.msg_color_g * 255;
b = settings->video.msg_color_b * 255;
2015-01-26 18:42:04 +00:00
2015-09-29 16:08:33 +00:00
r = (r < 0) ? 0 : (r > 255 ? 255 : r);
g = (g < 0) ? 0 : (g > 255 ? 255 : g);
b = (b < 0) ? 0 : (b > 255 ? 255 : b);
2015-01-26 18:42:04 +00:00
2015-09-29 16:08:33 +00:00
vid->font_rgb[0] = r;
vid->font_rgb[1] = g;
vid->font_rgb[2] = b;
}
2015-01-26 18:42:04 +00:00
static void omap_render_msg(omap_video_t *vid, const char *msg)
{
const struct font_atlas *atlas = NULL;
2015-03-20 21:08:36 +00:00
settings_t *settings = config_get_ptr();
int msg_base_x = settings->video.msg_pos_x * vid->width;
int msg_base_y = (1.0 - settings->video.msg_pos_y) * vid->height;
2015-01-26 18:42:04 +00:00
if (!vid->font)
return;
2015-01-26 18:42:04 +00:00
atlas = vid->font_driver->get_atlas(vid->font);
2015-01-26 18:42:04 +00:00
for (; *msg; msg++)
{
int base_x, base_y;
int glyph_width, glyph_height;
const uint8_t *src = NULL;
const struct font_glyph *glyph =
vid->font_driver->get_glyph(vid->font, (uint8_t)*msg);
2015-01-26 18:42:04 +00:00
if (!glyph)
continue;
2015-01-26 18:42:04 +00:00
base_x = msg_base_x + glyph->draw_offset_x;
base_y = msg_base_y + glyph->draw_offset_y;
2015-01-26 18:42:04 +00:00
const int max_width = vid->width - base_x;
const int max_height = vid->height - base_y;
2015-03-05 22:46:09 +00:00
glyph_width = glyph->width;
glyph_height = glyph->height;
2015-01-26 18:42:04 +00:00
src = atlas->buffer + glyph->atlas_offset_x +
glyph->atlas_offset_y * atlas->width;
2015-01-26 18:42:04 +00:00
if (base_x < 0)
{
2015-04-03 02:07:04 +00:00
src -= base_x;
2015-01-26 18:42:04 +00:00
glyph_width += base_x;
2015-04-03 02:07:04 +00:00
base_x = 0;
2015-01-26 18:42:04 +00:00
}
2015-01-26 18:42:04 +00:00
if (base_y < 0)
{
2015-04-03 02:07:04 +00:00
src -= base_y * (int)atlas->width;
2015-01-26 18:42:04 +00:00
glyph_height += base_y;
2015-04-03 02:07:04 +00:00
base_y = 0;
2015-01-26 18:42:04 +00:00
}
2015-01-26 18:42:04 +00:00
if (max_width <= 0 || max_height <= 0)
continue;
2015-01-26 18:42:04 +00:00
if (glyph_width > max_width)
glyph_width = max_width;
if (glyph_height > max_height)
glyph_height = max_height;
2015-01-26 18:42:04 +00:00
if (vid->bytes_per_pixel == 2)
{
omapfb_blend_glyph_rgb565(vid->omap, src, vid->font_rgb,
glyph_width, glyph_height,
atlas->width, base_x, base_y);
}
else
{
omapfb_blend_glyph_argb8888(vid->omap, src, vid->font_rgb,
glyph_width, glyph_height,
atlas->width, base_x, base_y);
}
msg_base_x += glyph->advance_x;
msg_base_y += glyph->advance_y;
}
}
/* FIXME/TODO: Filters not supported. */
2015-01-26 18:42:04 +00:00
static void *omap_gfx_init(const video_info_t *video,
const input_driver_t **input, void **input_data)
{
2015-04-11 06:39:37 +00:00
settings_t *settings = config_get_ptr();
omap_video_t *vid = (omap_video_t*)calloc(1, sizeof(omap_video_t));
2015-04-11 06:39:37 +00:00
if (!vid)
return NULL;
2015-04-11 06:39:37 +00:00
vid->omap = (omapfb_data_t*)calloc(1, sizeof(omapfb_data_t));
if (!vid->omap)
goto fail;
2015-04-11 06:39:37 +00:00
vid->bytes_per_pixel = video->rgb32 ? 4 : 2;
2015-04-11 06:39:37 +00:00
if (omapfb_init(vid->omap, vid->bytes_per_pixel) != 0)
goto fail_omapfb;
2015-04-11 06:39:37 +00:00
if (omapfb_backup_state(vid->omap) != 0 ||
omapfb_alloc_mem(vid->omap) != 0 ||
omapfb_mmap(vid->omap) != 0)
goto fail_omapfb;
2015-04-11 06:39:37 +00:00
/* set some initial mode for the menu */
vid->width = 320;
vid->height = 240;
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
if (omapfb_set_mode(vid->omap, vid->width, vid->height) != 0)
goto fail_omapfb;
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
if (input && input_data)
*input = NULL;
2015-04-11 06:39:37 +00:00
omap_init_font(vid, settings->video.font_path, settings->video.font_size);
2015-04-11 06:39:37 +00:00
vid->menu.frame = calloc(vid->width * vid->height, vid->bytes_per_pixel);
if (!vid->menu.frame)
goto fail_omapfb;
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
vid->menu.scaler.scaler_type = SCALER_TYPE_BILINEAR;
vid->menu.scaler.out_fmt = (vid->bytes_per_pixel == 4)
? SCALER_FMT_ARGB8888 : SCALER_FMT_RGB565;
2015-03-07 00:39:39 +00:00
2015-04-11 06:39:37 +00:00
return vid;
2014-02-24 20:12:12 +00:00
fail_omapfb:
2015-04-11 06:39:37 +00:00
omapfb_free(vid->omap);
free(vid->omap);
fail:
2015-04-11 06:39:37 +00:00
free(vid);
RARCH_ERR("[video_omap]: initialization failed\n");
return NULL;
}
static bool omap_gfx_frame(void *data, const void *frame, unsigned width,
2015-08-03 21:01:07 +00:00
unsigned height, uint64_t frame_count, unsigned pitch, const char *msg)
2015-01-26 18:42:04 +00:00
{
omap_video_t *vid = (omap_video_t*)data;
2015-04-11 06:39:37 +00:00
if (!frame)
return true;
if (width > 4 && height > 4 && (width != vid->width || height != vid->height))
{
RARCH_LOG("[video_omap]: mode set (resolution changed by core)\n");
if (omapfb_set_mode(vid->omap, width, height) != 0)
{
RARCH_ERR("[video_omap]: mode set failed\n");
return false;
}
vid->width = width;
vid->height = height;
}
omapfb_prepare(vid->omap);
omapfb_blit_frame(vid->omap, frame, vid->height, pitch);
if (vid->menu.active)
omapfb_blit_frame(vid->omap, vid->menu.frame,
vid->menu.scaler.out_height,
vid->menu.scaler.out_stride);
if (msg)
omap_render_msg(vid, msg);
return true;
}
2015-01-26 18:42:04 +00:00
static void omap_gfx_set_nonblock_state(void *data, bool state)
{
2015-04-11 06:39:37 +00:00
omap_video_t *vid;
2015-04-11 06:39:37 +00:00
if (!data)
return;
2015-04-11 06:39:37 +00:00
vid = data;
vid->omap->sync = !state;
}
2015-01-26 18:42:04 +00:00
static bool omap_gfx_alive(void *data)
{
2015-04-11 06:39:37 +00:00
(void)data;
return true; /* always alive */
}
2015-01-26 18:42:04 +00:00
static bool omap_gfx_focus(void *data)
{
2015-04-11 06:39:37 +00:00
(void)data;
return true; /* fb device always has focus */
}
2015-02-14 04:52:05 +00:00
static void omap_gfx_viewport_info(void *data, struct video_viewport *vp)
2015-01-26 18:42:04 +00:00
{
2015-04-11 06:39:37 +00:00
omap_video_t *vid = (omap_video_t*)data;
if (!vid)
return;
2015-01-26 18:42:04 +00:00
2015-04-11 06:39:37 +00:00
vp->x = vp->y = 0;
2015-04-11 06:39:37 +00:00
vp->width = vp->full_width = vid->width;
vp->height = vp->full_height = vid->height;
}
2015-01-18 21:32:14 +00:00
static bool omap_gfx_suppress_screensaver(void *data, bool enable)
{
(void)data;
(void)enable;
return false;
}
static bool omap_gfx_has_windowed(void *data)
{
(void)data;
/* TODO - implement. */
return true;
}
static bool omap_gfx_set_shader(void *data,
enum rarch_shader_type type, const char *path)
{
(void)data;
(void)type;
(void)path;
return false;
}
static void omap_gfx_set_rotation(void *data, unsigned rotation)
{
(void)data;
(void)rotation;
}
static bool omap_gfx_read_viewport(void *data, uint8_t *buffer)
{
(void)data;
(void)buffer;
return true;
}
2015-03-07 00:39:39 +00:00
static void update_scaler(omap_video_t *vid, struct scaler_ctx *scaler,
2015-04-11 06:39:37 +00:00
enum scaler_pix_fmt format, unsigned width,
unsigned height, unsigned pitch)
2015-03-07 00:39:39 +00:00
{
if (
2015-04-11 06:39:37 +00:00
width != scaler->in_width
|| height != scaler->in_height
|| format != scaler->in_fmt
|| pitch != scaler->in_stride
2015-03-07 00:39:39 +00:00
)
{
scaler->in_fmt = format;
scaler->in_width = width;
scaler->in_height = height;
scaler->in_stride = pitch;
scaler->out_width = vid->width;
scaler->out_height = vid->height;
scaler->out_stride = vid->width * vid->bytes_per_pixel;
if (!scaler_ctx_gen_filter(scaler))
2015-04-03 02:07:04 +00:00
RARCH_ERR("[video_omap]: scaler_ctx_gen_filter failed\n");
2015-03-07 00:39:39 +00:00
}
}
static void omap_gfx_set_texture_frame(void *data, const void *frame, bool rgb32,
unsigned width, unsigned height, float alpha)
{
2015-04-11 06:39:37 +00:00
omap_video_t *vid = (omap_video_t*)data;
2015-03-07 00:39:39 +00:00
enum scaler_pix_fmt format = rgb32 ? SCALER_FMT_ARGB8888 : SCALER_FMT_RGBA4444;
2015-04-11 06:39:37 +00:00
(void) alpha;
2015-03-07 00:39:39 +00:00
update_scaler(vid, &vid->menu.scaler, format, width, height,
2015-04-11 06:39:37 +00:00
width * (rgb32 ? sizeof(uint32_t) : sizeof(uint16_t)));
2015-03-07 00:39:39 +00:00
scaler_ctx_scale(&vid->menu.scaler, vid->menu.frame, frame);
}
static void omap_gfx_set_texture_enable(void *data, bool state, bool full_screen)
{
omap_video_t *vid = (omap_video_t*)data;
vid->menu.active = state;
2015-04-11 06:39:37 +00:00
(void) full_screen;
2015-03-07 00:39:39 +00:00
}
static const video_poke_interface_t omap_gfx_poke_interface = {
NULL,
NULL,
2015-03-07 00:39:39 +00:00
NULL,
NULL, /* set_filtering */
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
2015-04-25 00:35:06 +00:00
NULL, /* get_current_framebuffer */
NULL, /* get_proc_address */
2015-03-07 00:39:39 +00:00
NULL, /* set_aspect_ratio */
NULL, /* apply_state_changes */
#ifdef HAVE_MENU
omap_gfx_set_texture_frame,
omap_gfx_set_texture_enable,
#endif
NULL,
NULL, /* show_mouse */
NULL, /* grab_mouse_toggle */
NULL
};
static void omap_gfx_get_poke_interface(void *data,
const video_poke_interface_t **iface)
{
(void)data;
2015-03-07 00:39:39 +00:00
*iface = &omap_gfx_poke_interface;
}
2014-09-11 05:06:20 +00:00
video_driver_t video_omap = {
2015-04-11 06:39:37 +00:00
omap_gfx_init,
omap_gfx_frame,
omap_gfx_set_nonblock_state,
omap_gfx_alive,
omap_gfx_focus,
omap_gfx_suppress_screensaver,
omap_gfx_has_windowed,
omap_gfx_set_shader,
omap_gfx_free,
"omap",
NULL, /* set_viewport */
2015-04-11 06:39:37 +00:00
omap_gfx_set_rotation,
omap_gfx_viewport_info,
omap_gfx_read_viewport,
NULL, /* read_frame_raw */
#ifdef HAVE_OVERLAY
2015-04-11 06:39:37 +00:00
NULL, /* overlay_interface */
#endif
2015-04-11 06:39:37 +00:00
omap_gfx_get_poke_interface
};