f641fccb |
1 | /* |
0c7caf2b |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2010-2012 |
f641fccb |
3 | * |
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. |
6 | */ |
7 | |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include <strings.h> |
12 | #include <ctype.h> |
13 | #include <sys/types.h> |
14 | #include <sys/stat.h> |
15 | #include <fcntl.h> |
16 | #include <sys/ioctl.h> |
17 | #include <unistd.h> |
18 | #include <linux/fb.h> |
19 | |
9ab462cd |
20 | #include "osdl.h" |
f641fccb |
21 | #include "omapfb.h" |
22 | #include "linux/fbdev.h" |
f9b3f440 |
23 | #include "linux/xenv.h" |
f641fccb |
24 | |
090bcb85 |
25 | struct omapfb_state { |
f641fccb |
26 | struct omapfb_plane_info pi; |
27 | struct omapfb_mem_info mi; |
090bcb85 |
28 | struct omapfb_plane_info pi_old; |
29 | struct omapfb_mem_info mi_old; |
f641fccb |
30 | }; |
31 | |
b9a19b44 |
32 | static const char *get_fb_device(void) |
33 | { |
34 | const char *fbname = getenv("SDL_FBDEV"); |
35 | if (fbname == NULL) |
36 | fbname = "/dev/fb1"; |
37 | |
38 | return fbname; |
39 | } |
40 | |
090bcb85 |
41 | static int osdl_setup_omapfb(struct omapfb_state *ostate, int fd, int enabled, |
42 | int x, int y, int w, int h, int mem, int *buffer_count) |
f641fccb |
43 | { |
44 | struct omapfb_plane_info pi; |
45 | struct omapfb_mem_info mi; |
0c7caf2b |
46 | int mem_blocks = *buffer_count; |
f641fccb |
47 | int ret; |
48 | |
5f4b1fd3 |
49 | memset(&pi, 0, sizeof(pi)); |
50 | memset(&mi, 0, sizeof(mi)); |
51 | |
f641fccb |
52 | ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi); |
53 | if (ret != 0) { |
7b66578c |
54 | err_perror("QUERY_PLANE"); |
f641fccb |
55 | return -1; |
56 | } |
57 | |
58 | ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi); |
59 | if (ret != 0) { |
7b66578c |
60 | err_perror("QUERY_MEM"); |
f641fccb |
61 | return -1; |
62 | } |
63 | |
64 | /* must disable when changing stuff */ |
65 | if (pi.enabled) { |
66 | pi.enabled = 0; |
67 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
68 | if (ret != 0) |
7b66578c |
69 | err_perror("SETUP_PLANE"); |
f641fccb |
70 | } |
71 | |
0c7caf2b |
72 | if (mi.size < mem * mem_blocks) { |
73 | for (; mem_blocks > 0; mem_blocks--) { |
74 | mi.size = mem * mem_blocks; |
75 | ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi); |
76 | if (ret == 0) |
77 | break; |
78 | } |
79 | if (ret != 0 || mem_blocks <= 0) { |
80 | err("failed to allocate at least %d bytes of vram:\n", mem); |
13f953aa |
81 | err_perror("SETUP_MEM"); |
82 | return -1; |
83 | } |
f641fccb |
84 | } |
0c7caf2b |
85 | *buffer_count = mem_blocks; |
f641fccb |
86 | |
87 | pi.pos_x = x; |
88 | pi.pos_y = y; |
89 | pi.out_width = w; |
90 | pi.out_height = h; |
91 | pi.enabled = enabled; |
92 | |
93 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
94 | if (ret != 0) { |
7b66578c |
95 | err_perror("SETUP_PLANE"); |
6f13ad97 |
96 | err("(%d %d %d %d)\n", x, y, w, h); |
f641fccb |
97 | return -1; |
98 | } |
99 | |
090bcb85 |
100 | ostate->pi = pi; |
101 | ostate->mi = mi; |
f641fccb |
102 | return 0; |
103 | } |
104 | |
105 | static int read_sysfs(const char *fname, char *buff, size_t size) |
106 | { |
107 | FILE *f; |
108 | int ret; |
109 | |
110 | f = fopen(fname, "r"); |
111 | if (f == NULL) { |
7b66578c |
112 | err_perror("open %s: ", fname); |
f641fccb |
113 | return -1; |
114 | } |
115 | |
116 | ret = fread(buff, 1, size - 1, f); |
117 | fclose(f); |
118 | if (ret <= 0) { |
7b66578c |
119 | err_perror("read %s: ", fname); |
f641fccb |
120 | return -1; |
121 | } |
122 | |
123 | buff[ret] = 0; |
124 | for (ret--; ret >= 0 && isspace(buff[ret]); ret--) |
125 | buff[ret] = 0; |
126 | |
127 | return 0; |
128 | } |
129 | |
34edc246 |
130 | int read_vscreeninfo(const char *fbname, int *w, int *h) |
131 | { |
132 | struct fb_var_screeninfo fbvar; |
133 | int ret, fd; |
134 | |
135 | fd = open(fbname, O_RDWR); |
136 | if (fd == -1) { |
137 | err_perror("open %s", fbname); |
138 | return -1; |
139 | } |
140 | |
141 | ret = ioctl(fd, FBIOGET_VSCREENINFO, &fbvar); |
142 | close(fd); |
143 | |
144 | if (ret == -1) { |
145 | err_perror("ioctl %s", fbname); |
146 | return -1; |
147 | } |
148 | |
149 | if (fbvar.xres == 0 || fbvar.yres == 0) |
150 | return -1; |
151 | |
152 | *w = fbvar.xres; |
153 | *h = fbvar.yres; |
154 | return 0; |
155 | } |
156 | |
b9a19b44 |
157 | int osdl_video_detect_screen(struct SDL_PrivateVideoData *pdata) |
f641fccb |
158 | { |
f641fccb |
159 | int fb_id, overlay_id = -1, screen_id = -1; |
160 | char buff[64], screen_name[64]; |
b9a19b44 |
161 | const char *fbname; |
f641fccb |
162 | struct stat status; |
b9a19b44 |
163 | int fd, i, ret; |
164 | int w, h; |
f641fccb |
165 | FILE *f; |
166 | |
5f4b1fd3 |
167 | pdata->phys_w = pdata->phys_h = 0; |
168 | |
b9a19b44 |
169 | fbname = get_fb_device(); |
f641fccb |
170 | |
b9a19b44 |
171 | /* Figure out screen resolution, we need to know default resolution |
172 | * to report to SDL and for centering stuff. |
f641fccb |
173 | * The only way to achieve this seems to be walking some sysfs files.. */ |
174 | ret = stat(fbname, &status); |
175 | if (ret != 0) { |
7b66578c |
176 | err_perror("can't stat %s", fbname); |
b9a19b44 |
177 | goto skip_screen; |
f641fccb |
178 | } |
179 | fb_id = minor(status.st_rdev); |
180 | |
181 | snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id); |
182 | f = fopen(buff, "r"); |
183 | if (f == NULL) { |
7b66578c |
184 | err("can't open %s, skip screen detection", buff); |
f641fccb |
185 | goto skip_screen; |
186 | } |
187 | |
188 | ret = fscanf(f, "%d", &overlay_id); |
189 | fclose(f); |
190 | if (ret != 1) { |
7b66578c |
191 | err("can't parse %s, skip screen detection", buff); |
f641fccb |
192 | goto skip_screen; |
193 | } |
194 | |
195 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id); |
196 | ret = read_sysfs(buff, screen_name, sizeof(screen_name)); |
197 | if (ret < 0) { |
7b66578c |
198 | err("skip screen detection"); |
f641fccb |
199 | goto skip_screen; |
200 | } |
201 | |
202 | for (i = 0; ; i++) { |
203 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i); |
204 | ret = read_sysfs(buff, buff, sizeof(buff)); |
205 | if (ret < 0) |
206 | break; |
207 | |
208 | if (strcmp(screen_name, buff) == 0) { |
209 | screen_id = i; |
210 | break; |
211 | } |
212 | } |
213 | |
214 | if (screen_id < 0) { |
7b66578c |
215 | err("could not find screen"); |
f641fccb |
216 | goto skip_screen; |
217 | } |
218 | |
219 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", screen_id); |
220 | f = fopen(buff, "r"); |
221 | if (f == NULL) { |
7b66578c |
222 | err("can't open %s, skip screen detection", buff); |
f641fccb |
223 | goto skip_screen; |
224 | } |
225 | |
b9a19b44 |
226 | ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h); |
f641fccb |
227 | fclose(f); |
228 | if (ret != 2) { |
7b66578c |
229 | err("can't parse %s (%d), skip screen detection", buff, ret); |
f641fccb |
230 | goto skip_screen; |
231 | } |
232 | |
7b66578c |
233 | log("detected %dx%d '%s' (%d) screen attached to fb %d and overlay %d", |
b9a19b44 |
234 | w, h, screen_name, screen_id, fb_id, overlay_id); |
235 | |
5f4b1fd3 |
236 | pdata->phys_w = w; |
237 | pdata->phys_h = h; |
b9a19b44 |
238 | return 0; |
f641fccb |
239 | |
240 | skip_screen: |
b9a19b44 |
241 | /* attempt to extract this from FB then */ |
34edc246 |
242 | ret = read_vscreeninfo(fbname, &pdata->phys_w, &pdata->phys_h); |
243 | if (ret != 0 && strcmp(fbname, "/dev/fb0") != 0) { |
244 | /* last resort */ |
245 | ret = read_vscreeninfo("/dev/fb0", &pdata->phys_w, &pdata->phys_h); |
b9a19b44 |
246 | } |
247 | |
34edc246 |
248 | if (ret != 0) { |
b9a19b44 |
249 | err("VSCREENINFO has nothing meaningful"); |
250 | return -1; |
251 | } |
252 | |
b9a19b44 |
253 | return 0; |
254 | } |
255 | |
256 | static int osdl_setup_omap_layer(struct SDL_PrivateVideoData *pdata, |
0c7caf2b |
257 | const char *fbname, int width, int height, int bpp, int *buffer_count) |
b9a19b44 |
258 | { |
259 | int x = 0, y = 0, w = width, h = height; /* layer size and pos */ |
260 | int screen_w = w, screen_h = h; |
f151701d |
261 | int tmp_w, tmp_h; |
b9a19b44 |
262 | const char *tmp; |
f11c56a6 |
263 | int retval = -1; |
b9a19b44 |
264 | int ret, fd; |
265 | |
5f4b1fd3 |
266 | pdata->layer_x = pdata->layer_y = pdata->layer_w = pdata->layer_h = 0; |
267 | |
268 | if (pdata->phys_w != 0) |
269 | screen_w = pdata->phys_w; |
270 | if (pdata->phys_h != 0) |
271 | screen_h = pdata->phys_h; |
b9a19b44 |
272 | |
273 | fd = open(fbname, O_RDWR); |
274 | if (fd == -1) { |
275 | err_perror("open %s", fbname); |
276 | return -1; |
277 | } |
278 | |
279 | /* FIXME: assuming layer doesn't change here */ |
280 | if (pdata->saved_layer == NULL) { |
090bcb85 |
281 | struct omapfb_state *slayer; |
b9a19b44 |
282 | slayer = calloc(1, sizeof(*slayer)); |
283 | if (slayer == NULL) |
f11c56a6 |
284 | goto out; |
b9a19b44 |
285 | |
090bcb85 |
286 | ret = ioctl(fd, OMAPFB_QUERY_PLANE, &slayer->pi_old); |
b9a19b44 |
287 | if (ret != 0) { |
288 | err_perror("QUERY_PLANE"); |
f11c56a6 |
289 | goto out; |
b9a19b44 |
290 | } |
291 | |
090bcb85 |
292 | ret = ioctl(fd, OMAPFB_QUERY_MEM, &slayer->mi_old); |
b9a19b44 |
293 | if (ret != 0) { |
294 | err_perror("QUERY_MEM"); |
f11c56a6 |
295 | goto out; |
b9a19b44 |
296 | } |
297 | |
298 | pdata->saved_layer = slayer; |
299 | } |
300 | |
f641fccb |
301 | tmp = getenv("SDL_OMAP_LAYER_SIZE"); |
302 | if (tmp != NULL) { |
f641fccb |
303 | if (strcasecmp(tmp, "fullscreen") == 0) |
304 | w = screen_w, h = screen_h; |
f151701d |
305 | else if (sscanf(tmp, "%dx%d", &tmp_w, &tmp_h) == 2) |
306 | w = tmp_w, h = tmp_h; |
f641fccb |
307 | else |
7b66578c |
308 | err("layer size specified incorrectly, " |
309 | "should be like 800x480"); |
f641fccb |
310 | } |
f11c56a6 |
311 | |
312 | /* the layer can't be set larger than screen */ |
313 | tmp_w = w, tmp_h = h; |
314 | if (w > screen_w) |
315 | w = screen_w; |
316 | if (h > screen_h) |
317 | h = screen_h; |
318 | if (w != tmp_w || h != tmp_h) |
319 | log("layer resized %dx%d -> %dx%d to fit screen", tmp_w, tmp_h, w, h); |
f641fccb |
320 | |
321 | x = screen_w / 2 - w / 2; |
322 | y = screen_h / 2 - h / 2; |
090bcb85 |
323 | ret = osdl_setup_omapfb(pdata->saved_layer, fd, 1, x, y, w, h, |
0c7caf2b |
324 | width * height * ((bpp + 7) / 8), buffer_count); |
5f4b1fd3 |
325 | if (ret == 0) { |
326 | pdata->layer_x = x; |
327 | pdata->layer_y = y; |
328 | pdata->layer_w = w; |
329 | pdata->layer_h = h; |
330 | } |
f641fccb |
331 | |
f11c56a6 |
332 | retval = ret; |
333 | out: |
334 | close(fd); |
335 | return retval; |
f641fccb |
336 | } |
337 | |
455c8c43 |
338 | void *osdl_video_set_mode(struct SDL_PrivateVideoData *pdata, |
339 | int border_l, int border_r, int border_t, int border_b, |
090bcb85 |
340 | int width, int height, int bpp, int *doublebuf, |
341 | const char *wm_title) |
f641fccb |
342 | { |
0c7caf2b |
343 | int buffers_try, buffers_set; |
f641fccb |
344 | const char *fbname; |
455c8c43 |
345 | void *result; |
f641fccb |
346 | int ret; |
347 | |
b9a19b44 |
348 | fbname = get_fb_device(); |
f641fccb |
349 | |
350 | if (pdata->fbdev != NULL) { |
351 | vout_fbdev_finish(pdata->fbdev); |
352 | pdata->fbdev = NULL; |
353 | } |
354 | |
0c7caf2b |
355 | buffers_try = buffers_set = 1; |
356 | if (*doublebuf) |
357 | /* actually try tripple buffering for reduced chance of tearing */ |
358 | buffers_try = buffers_set = 3; |
359 | |
360 | ret = osdl_setup_omap_layer(pdata, fbname, width, height, bpp, &buffers_set); |
f641fccb |
361 | if (ret < 0) |
0c7caf2b |
362 | goto fail; |
f641fccb |
363 | |
0c7caf2b |
364 | pdata->fbdev = vout_fbdev_init(fbname, &width, &height, bpp, buffers_set); |
f641fccb |
365 | if (pdata->fbdev == NULL) |
0c7caf2b |
366 | goto fail; |
455c8c43 |
367 | |
368 | if (border_l | border_r | border_t | border_b) { |
369 | width -= border_l + border_r; |
370 | height -= border_t + border_b; |
371 | result = vout_fbdev_resize(pdata->fbdev, width, height, bpp, |
0c7caf2b |
372 | border_l, border_r, border_t, border_b, buffers_set); |
455c8c43 |
373 | } |
374 | else { |
375 | result = osdl_video_flip(pdata); |
376 | } |
0c7caf2b |
377 | if (result == NULL) |
378 | goto fail; |
f641fccb |
379 | |
f9b3f440 |
380 | if (!pdata->xenv_up) { |
090bcb85 |
381 | int xenv_flags = XENV_CAP_KEYS | XENV_CAP_MOUSE; |
382 | ret = xenv_init(&xenv_flags, wm_title); |
e1837b2c |
383 | if (ret == 0) { |
f9b3f440 |
384 | pdata->xenv_up = 1; |
090bcb85 |
385 | pdata->xenv_mouse = (xenv_flags & XENV_CAP_MOUSE) ? 1 : 0; |
e1837b2c |
386 | } |
f641fccb |
387 | } |
388 | |
0c7caf2b |
389 | if (buffers_try != buffers_set) { |
390 | log("only %d/%d buffers available, expect tearing\n", |
391 | buffers_set, buffers_try); |
392 | *doublebuf = buffers_set > 1; |
393 | } |
394 | |
455c8c43 |
395 | return result; |
0c7caf2b |
396 | |
397 | fail: |
398 | osdl_video_finish(pdata); |
399 | return NULL; |
f641fccb |
400 | } |
401 | |
402 | void *osdl_video_flip(struct SDL_PrivateVideoData *pdata) |
403 | { |
52a1749b |
404 | void *ret; |
405 | |
f641fccb |
406 | if (pdata->fbdev == NULL) |
407 | return NULL; |
408 | |
52a1749b |
409 | ret = vout_fbdev_flip(pdata->fbdev); |
410 | |
5f4b1fd3 |
411 | if (pdata->cfg_force_vsync) |
f641fccb |
412 | vout_fbdev_wait_vsync(pdata->fbdev); |
413 | |
52a1749b |
414 | return ret; |
f641fccb |
415 | } |
416 | |
090bcb85 |
417 | int osdl_video_pause(struct SDL_PrivateVideoData *pdata, int is_pause) |
418 | { |
419 | struct omapfb_state *state = pdata->saved_layer; |
420 | struct omapfb_plane_info pi; |
421 | struct omapfb_mem_info mi; |
422 | int enabled; |
423 | int fd = -1; |
424 | int ret; |
425 | |
426 | if (pdata->fbdev != NULL) |
427 | fd = vout_fbdev_get_fd(pdata->fbdev); |
428 | if (fd == -1) { |
429 | err("bad fd %d", fd); |
430 | return -1; |
431 | } |
432 | if (state == NULL) { |
433 | err("missing layer state\n"); |
434 | return -1; |
435 | } |
436 | |
437 | if (is_pause) { |
438 | ret = vout_fbdev_save(pdata->fbdev); |
439 | if (ret != 0) |
440 | return ret; |
441 | pi = state->pi_old; |
442 | mi = state->mi_old; |
443 | enabled = pi.enabled; |
444 | } else { |
445 | pi = state->pi; |
446 | mi = state->mi; |
447 | enabled = 1; |
448 | } |
449 | pi.enabled = 0; |
450 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
451 | if (ret != 0) { |
452 | err_perror("SETUP_PLANE"); |
453 | return -1; |
454 | } |
455 | |
456 | ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi); |
457 | if (ret != 0) |
458 | err_perror("SETUP_MEM"); |
459 | |
460 | if (!is_pause) { |
461 | ret = vout_fbdev_restore(pdata->fbdev); |
462 | if (ret != 0) { |
463 | err("fbdev_restore failed\n"); |
464 | return ret; |
465 | } |
466 | } |
467 | |
468 | if (enabled) { |
469 | pi.enabled = 1; |
470 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
471 | if (ret != 0) { |
472 | err_perror("SETUP_PLANE"); |
473 | return -1; |
474 | } |
475 | } |
476 | |
477 | return 0; |
478 | } |
479 | |
f641fccb |
480 | void osdl_video_finish(struct SDL_PrivateVideoData *pdata) |
481 | { |
482 | static const char *fbname; |
483 | |
b9a19b44 |
484 | fbname = get_fb_device(); |
f641fccb |
485 | if (pdata->fbdev != NULL) { |
486 | vout_fbdev_finish(pdata->fbdev); |
487 | pdata->fbdev = NULL; |
488 | } |
489 | |
490 | /* restore the OMAP layer */ |
491 | if (pdata->saved_layer != NULL) { |
090bcb85 |
492 | struct omapfb_state *slayer = pdata->saved_layer; |
f641fccb |
493 | int fd; |
494 | |
495 | fd = open(fbname, O_RDWR); |
496 | if (fd != -1) { |
090bcb85 |
497 | int enabled = slayer->pi_old.enabled; |
f641fccb |
498 | |
499 | /* be sure to disable while setting up */ |
090bcb85 |
500 | slayer->pi_old.enabled = 0; |
501 | ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old); |
502 | ioctl(fd, OMAPFB_SETUP_MEM, &slayer->mi_old); |
f641fccb |
503 | if (enabled) { |
090bcb85 |
504 | slayer->pi_old.enabled = enabled; |
505 | ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old); |
f641fccb |
506 | } |
507 | close(fd); |
508 | } |
509 | free(slayer); |
510 | pdata->saved_layer = NULL; |
511 | } |
512 | |
f9b3f440 |
513 | if (pdata->xenv_up) { |
514 | xenv_finish(); |
515 | pdata->xenv_up = 0; |
f641fccb |
516 | } |
517 | } |
518 | |