2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2012
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
13 #include <sys/types.h>
16 #include <sys/ioctl.h>
22 #include "linux/fbdev.h"
23 #include "linux/xenv.h"
25 #define MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
28 struct omapfb_plane_info pi;
29 struct omapfb_mem_info mi;
30 struct omapfb_plane_info pi_old;
31 struct omapfb_mem_info mi_old;
34 static const char *get_fb_device(void)
36 const char *fbname = getenv("SDL_FBDEV");
43 static int osdl_setup_omapfb(struct omapfb_state *ostate, int fd, int enabled,
44 int x, int y, int w, int h, int mem, int *buffer_count)
46 struct omapfb_plane_info pi;
47 struct omapfb_mem_info mi;
48 int mem_blocks = *buffer_count;
49 unsigned int size_cur;
52 memset(&pi, 0, sizeof(pi));
53 memset(&mi, 0, sizeof(mi));
55 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
57 err_perror("QUERY_PLANE");
61 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
63 err_perror("QUERY_MEM");
68 /* must disable when changing stuff */
71 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
73 err_perror("SETUP_PLANE");
76 /* allocate more mem, if needed */
77 for (; size_cur < mem * mem_blocks && mem_blocks > 0; mem_blocks--) {
78 mi.size = mem * mem_blocks;
79 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
84 if (mem_blocks <= 0) {
85 err("failed to allocate at least %d bytes of vram:\n", mem);
86 err_perror("SETUP_MEM");
90 *buffer_count = mem_blocks;
98 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
100 err_perror("SETUP_PLANE");
101 err("(%d %d %d %d)\n", x, y, w, h);
110 static int osdl_setup_omapfb_enable(struct omapfb_state *ostate,
115 ostate->pi.enabled = enabled;
116 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &ostate->pi);
118 err_perror("SETUP_PLANE");
123 static int read_sysfs(const char *fname, char *buff, size_t size)
128 f = fopen(fname, "r");
130 err_perror("open %s: ", fname);
134 ret = fread(buff, 1, size - 1, f);
137 err_perror("read %s: ", fname);
142 for (ret--; ret >= 0 && isspace(buff[ret]); ret--)
148 int read_vscreeninfo(const char *fbname, int *w, int *h)
150 struct fb_var_screeninfo fbvar;
153 fd = open(fbname, O_RDWR);
155 err_perror("open %s", fbname);
159 ret = ioctl(fd, FBIOGET_VSCREENINFO, &fbvar);
163 err_perror("ioctl %s", fbname);
167 if (fbvar.xres == 0 || fbvar.yres == 0)
175 int osdl_video_detect_screen(struct SDL_PrivateVideoData *pdata)
177 int fb_id, overlay_id = -1, screen_id = -1;
178 char buff[64], screen_name[64];
185 pdata->phys_w = pdata->phys_h = 0;
187 fbname = get_fb_device();
189 /* Figure out screen resolution, we need to know default resolution
190 * to report to SDL and for centering stuff.
191 * The only way to achieve this seems to be walking some sysfs files.. */
192 ret = stat(fbname, &status);
194 err_perror("can't stat %s", fbname);
197 fb_id = minor(status.st_rdev);
199 snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id);
200 f = fopen(buff, "r");
202 err("can't open %s, skip screen detection", buff);
206 ret = fscanf(f, "%d", &overlay_id);
209 err("can't parse %s, skip screen detection", buff);
213 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id);
214 ret = read_sysfs(buff, screen_name, sizeof(screen_name));
216 err("skip screen detection");
221 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i);
222 ret = read_sysfs(buff, buff, sizeof(buff));
226 if (strcmp(screen_name, buff) == 0) {
233 err("could not find screen");
237 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", screen_id);
238 f = fopen(buff, "r");
240 err("can't open %s, skip screen detection", buff);
244 ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h);
247 err("can't parse %s (%d), skip screen detection", buff, ret);
251 log("detected %dx%d '%s' (%d) screen attached to fb %d and overlay %d",
252 w, h, screen_name, screen_id, fb_id, overlay_id);
259 /* attempt to extract this from FB then */
260 ret = read_vscreeninfo(fbname, &pdata->phys_w, &pdata->phys_h);
261 if (ret != 0 && strcmp(fbname, "/dev/fb0") != 0) {
263 ret = read_vscreeninfo("/dev/fb0", &pdata->phys_w, &pdata->phys_h);
267 err("VSCREENINFO has nothing meaningful");
274 static int osdl_setup_omap_layer(struct SDL_PrivateVideoData *pdata,
275 const char *fbname, int width, int height, int bpp, int *buffer_count)
277 int x = 0, y = 0, w = width, h = height; /* layer size and pos */
278 int screen_w = w, screen_h = h;
284 pdata->layer_x = pdata->layer_y = pdata->layer_w = pdata->layer_h = 0;
286 if (pdata->phys_w != 0)
287 screen_w = pdata->phys_w;
288 if (pdata->phys_h != 0)
289 screen_h = pdata->phys_h;
291 fd = open(fbname, O_RDWR);
293 err_perror("open %s", fbname);
297 /* FIXME: assuming layer doesn't change here */
298 if (pdata->saved_layer == NULL) {
299 struct omapfb_state *slayer;
300 slayer = calloc(1, sizeof(*slayer));
304 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &slayer->pi_old);
306 err_perror("QUERY_PLANE");
310 ret = ioctl(fd, OMAPFB_QUERY_MEM, &slayer->mi_old);
312 err_perror("QUERY_MEM");
316 pdata->saved_layer = slayer;
319 tmp = getenv("SDL_OMAP_LAYER_SIZE");
321 if (strcasecmp(tmp, "fullscreen") == 0) {
322 w = screen_w, h = screen_h;
324 else if (strcasecmp(tmp, "scaled") == 0) {
325 float factor = MIN(((float)screen_w) / width, ((float)screen_h) / height);
326 w = (int)(factor*width), h = (int)(factor*height);
328 else if (strcasecmp(tmp, "pixelperfect") == 0) {
329 float factor = MIN(((float)screen_w) / width, ((float)screen_h) / height);
330 w = ((int)factor) * width, h = ((int)factor) * height;
331 /* factor < 1.f => 0x0 layer, so fall back to 'scaled' */
333 w = (int)(factor * width), h = (int)(factor * height);
336 else if (sscanf(tmp, "%dx%d", &tmp_w, &tmp_h) == 2) {
337 w = tmp_w, h = tmp_h;
340 err("layer size specified incorrectly, "
341 "should be like 800x480");
345 /* the layer can't be set larger than screen */
346 tmp_w = w, tmp_h = h;
351 if (w != tmp_w || h != tmp_h)
352 log("layer resized %dx%d -> %dx%d to fit screen", tmp_w, tmp_h, w, h);
354 x = screen_w / 2 - w / 2;
355 y = screen_h / 2 - h / 2;
356 ret = osdl_setup_omapfb(pdata->saved_layer, fd, 0, x, y, w, h,
357 width * height * ((bpp + 7) / 8), buffer_count);
371 void *osdl_video_set_mode(struct SDL_PrivateVideoData *pdata,
372 int border_l, int border_r, int border_t, int border_b,
373 int width, int height, int bpp, int *doublebuf,
374 const char *wm_title)
376 int buffers_try, buffers_set;
381 fbname = get_fb_device();
383 if (pdata->fbdev != NULL) {
384 vout_fbdev_finish(pdata->fbdev);
388 buffers_try = buffers_set = 1;
390 /* actually try tripple buffering for reduced chance of tearing */
391 buffers_try = buffers_set = 3;
393 ret = osdl_setup_omap_layer(pdata, fbname, width, height, bpp, &buffers_set);
397 pdata->fbdev = vout_fbdev_init(fbname, &width, &height, bpp, buffers_set);
398 if (pdata->fbdev == NULL)
401 if (border_l | border_r | border_t | border_b) {
402 width -= border_l + border_r;
403 height -= border_t + border_b;
404 result = vout_fbdev_resize(pdata->fbdev, width, height, bpp,
405 border_l, border_r, border_t, border_b, buffers_set);
408 result = osdl_video_flip(pdata);
413 if (!pdata->xenv_up) {
414 int xenv_flags = XENV_CAP_KEYS | XENV_CAP_MOUSE;
415 ret = xenv_init(&xenv_flags, wm_title);
418 pdata->xenv_mouse = (xenv_flags & XENV_CAP_MOUSE) ? 1 : 0;
422 ret = osdl_setup_omapfb_enable(pdata->saved_layer,
423 vout_fbdev_get_fd(pdata->fbdev), 1);
425 err("layer enable failed");
429 if (buffers_try != buffers_set) {
430 log("only %d/%d buffers available, expect tearing\n",
431 buffers_set, buffers_try);
432 *doublebuf = buffers_set > 1;
438 osdl_video_finish(pdata);
442 void *osdl_video_flip(struct SDL_PrivateVideoData *pdata)
446 if (pdata->fbdev == NULL)
449 ret = vout_fbdev_flip(pdata->fbdev);
451 if (pdata->cfg_force_vsync)
452 vout_fbdev_wait_vsync(pdata->fbdev);
457 void *osdl_video_get_active_buffer(struct SDL_PrivateVideoData *pdata)
459 if (pdata->fbdev == NULL)
462 return vout_fbdev_get_active_mem(pdata->fbdev);
465 int osdl_video_pause(struct SDL_PrivateVideoData *pdata, int is_pause)
467 struct omapfb_state *state = pdata->saved_layer;
468 struct omapfb_plane_info pi;
469 struct omapfb_mem_info mi;
474 if (pdata->fbdev != NULL)
475 fd = vout_fbdev_get_fd(pdata->fbdev);
477 err("bad fd %d", fd);
481 err("missing layer state\n");
486 ret = vout_fbdev_save(pdata->fbdev);
491 enabled = pi.enabled;
498 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
500 err_perror("SETUP_PLANE");
504 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
506 err_perror("SETUP_MEM");
509 ret = vout_fbdev_restore(pdata->fbdev);
511 err("fbdev_restore failed\n");
518 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
520 err_perror("SETUP_PLANE");
528 void osdl_video_finish(struct SDL_PrivateVideoData *pdata)
530 static const char *fbname;
532 fbname = get_fb_device();
533 if (pdata->fbdev != NULL) {
534 vout_fbdev_finish(pdata->fbdev);
538 /* restore the OMAP layer */
539 if (pdata->saved_layer != NULL) {
540 struct omapfb_state *slayer = pdata->saved_layer;
543 fd = open(fbname, O_RDWR);
545 int enabled = slayer->pi_old.enabled;
547 /* be sure to disable while setting up */
548 slayer->pi_old.enabled = 0;
549 ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old);
550 ioctl(fd, OMAPFB_SETUP_MEM, &slayer->mi_old);
552 slayer->pi_old.enabled = enabled;
553 ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old);
558 pdata->saved_layer = NULL;
561 if (pdata->xenv_up) {