031e1621eaed96b5e447eb6cabc279d701a36949
[libpicofe.git] / plat_sdl.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2012
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <SDL.h>
15 #include <SDL_syswm.h>
16
17 #include "menu.h"
18 #include "plat.h"
19 #include "gl.h"
20 #include "plat_sdl.h"
21
22 // XXX: maybe determine this instead..
23 #define WM_DECORATION_H 32
24
25 SDL_Surface *plat_sdl_screen;
26 SDL_Overlay *plat_sdl_overlay;
27 int plat_sdl_gl_active;
28 void (*plat_sdl_resize_cb)(int w, int h);
29 void (*plat_sdl_quit_cb)(void);
30 int plat_sdl_no_overlay2x;
31
32 static char vid_drv_name[32];
33 static int window_w, window_h, window_b;
34 static int fs_w, fs_h;
35 static int old_fullscreen;
36 static int vout_mode_overlay = -1, vout_mode_overlay2x = -1, vout_mode_gl = -1;
37 static void *display, *window;
38 static int gl_quirks;
39 static Uint32 screen_flags =
40 #if defined(SDL_SURFACE_SW)
41   SDL_SWSURFACE;
42 #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
43   SDL_HWSURFACE | SDL_TRIPLEBUF;
44 #else
45   SDL_HWSURFACE | SDL_DOUBLEBUF;
46 #endif
47
48 static Uint32 get_screen_flags(void)
49 {
50   Uint32 flags = screen_flags;
51   flags &= ~(SDL_FULLSCREEN | SDL_RESIZABLE);
52   if (plat_target.vout_fullscreen && fs_w && fs_h)
53     flags |= SDL_FULLSCREEN;
54   else if (window_b)
55     flags |= SDL_RESIZABLE;
56   return flags;
57 }
58
59 static void update_wm_display_window(void)
60 {
61   // get x11 display/window for GL
62 #ifdef SDL_VIDEO_DRIVER_X11
63   SDL_SysWMinfo wminfo;
64   int ret;
65   if (strcmp(vid_drv_name, "x11") == 0) {
66     SDL_VERSION(&wminfo.version);
67     ret = SDL_GetWMInfo(&wminfo);
68     if (ret > 0) {
69       display = wminfo.info.x11.display;
70       window = (void *)wminfo.info.x11.window;
71     }
72   }
73 #endif
74 }
75
76 /* w, h is layer resolution */
77 int plat_sdl_change_video_mode(int w, int h, int force)
78 {
79   static int prev_w, prev_h;
80
81   // skip GL recreation if window doesn't change - avoids flicker
82   if (plat_target.vout_method == vout_mode_gl && plat_sdl_gl_active
83       && plat_target.vout_fullscreen == old_fullscreen
84       && w == prev_w && h == prev_h && !force)
85   {
86     return 0;
87   }
88
89   if (w == 0)
90     w = prev_w;
91   else
92     prev_w = w;
93   if (h == 0)
94     h = prev_h;
95   else
96     prev_h = h;
97
98   // invalid method might come from config..
99   if (plat_target.vout_method != 0
100       && plat_target.vout_method != vout_mode_overlay
101       && plat_target.vout_method != vout_mode_overlay2x
102       && plat_target.vout_method != vout_mode_gl)
103   {
104     fprintf(stderr, "invalid vout_method: %d\n", plat_target.vout_method);
105     plat_target.vout_method = 0;
106   }
107
108   if (plat_sdl_overlay != NULL) {
109     SDL_FreeYUVOverlay(plat_sdl_overlay);
110     plat_sdl_overlay = NULL;
111   }
112   if (plat_sdl_gl_active) {
113     gl_destroy();
114     plat_sdl_gl_active = 0;
115   }
116
117   if (plat_target.vout_method == 0 || (force && (window_w != w || window_h != h
118       || plat_target.vout_fullscreen != old_fullscreen
119       || !plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))) {
120     Uint32 flags = get_screen_flags();
121     int win_w = w;
122     int win_h = h;
123
124     if (plat_target.vout_fullscreen && fs_w && fs_h) {
125       win_w = fs_w;
126       win_h = fs_h;
127     }
128
129     // XXX: workaround some occasional mysterious deadlock in SDL_SetVideoMode
130     SDL_PumpEvents();
131
132     if (!plat_sdl_screen || screen_flags != flags ||
133         plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h ||
134         (!plat_sdl_screen->format || plat_sdl_screen->format->BitsPerPixel != 16))
135       plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags);
136     if (plat_sdl_screen == NULL) {
137       fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
138       return -1;
139     }
140     if (vout_mode_gl != -1)
141       update_wm_display_window();
142     screen_flags = flags;
143     window_w = win_w;
144     window_h = win_h;
145   }
146
147   if (plat_target.vout_method == vout_mode_overlay
148       || plat_target.vout_method == vout_mode_overlay2x) {
149     int W = plat_target.vout_method == vout_mode_overlay2x && !plat_sdl_no_overlay2x ? 2*w : w;
150     plat_sdl_overlay = SDL_CreateYUVOverlay(W, h, SDL_UYVY_OVERLAY, plat_sdl_screen);
151     if (plat_sdl_overlay != NULL && SDL_LockYUVOverlay(plat_sdl_overlay) == 0) {
152       if ((uintptr_t)plat_sdl_overlay->pixels[0] & 3)
153         fprintf(stderr, "warning: overlay pointer is unaligned\n");
154
155       plat_sdl_overlay_clear();
156
157       SDL_UnlockYUVOverlay(plat_sdl_overlay);
158     }
159     else {
160       fprintf(stderr, "warning: could not create overlay.\n");
161       plat_target.vout_method = 0;
162     }
163   }
164   else if (plat_target.vout_method == vout_mode_gl) {
165     int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
166     plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
167     if (!plat_sdl_gl_active) {
168       fprintf(stderr, "warning: could not init GL.\n");
169       plat_target.vout_method = 0;
170     }
171   }
172
173   old_fullscreen = plat_target.vout_fullscreen;
174   if (plat_sdl_resize_cb != NULL)
175     plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
176
177   return 0;
178 }
179
180 void plat_sdl_event_handler(void *event_)
181 {
182   static int was_active;
183   SDL_Event *event = event_;
184
185   switch (event->type) {
186   case SDL_VIDEORESIZE:
187     //printf("resize %dx%d\n", event->resize.w, event->resize.h);
188     if ((plat_target.vout_method != 0 || window_b) && !plat_target.vout_fullscreen)
189     {
190       int win_w = event->resize.w & ~3;
191       int win_h = event->resize.h & ~3;
192       plat_sdl_change_video_mode(win_w, win_h, 1);
193     }
194     break;
195   case SDL_ACTIVEEVENT:
196     if (event->active.gain && !was_active) {
197       if (plat_sdl_overlay != NULL) {
198         SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
199         SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
200       }
201       else if (plat_sdl_gl_active) {
202         if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
203           int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
204           gl_destroy();
205           plat_sdl_gl_active = (gl_create(window, &gl_quirks, sw, sh) == 0);
206         }
207         gl_flip(NULL, 0, 0);
208       }
209       // else SDL takes care of it
210     }
211     was_active = event->active.gain;
212     break;
213   case SDL_QUIT:
214     if (plat_sdl_quit_cb != NULL)
215       plat_sdl_quit_cb();
216     break;
217   }
218 }
219
220 int plat_sdl_init(void)
221 {
222   static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
223   const SDL_VideoInfo *info;
224   const char *env;
225   int overlay_works = 0;
226   int gl_works = 0;
227   int i, ret, h;
228   Uint32 flags;
229   int try_gl;
230
231   ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
232   if (ret != 0) {
233     fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
234     return -1;
235   }
236
237   info = SDL_GetVideoInfo();
238   if (info != NULL) {
239     fs_w = info->current_w;
240     fs_h = info->current_h;
241     if (info->wm_available)
242       window_b = WM_DECORATION_H;
243     printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
244   }
245
246   g_menuscreen_w = 640;
247   if (fs_w != 0 && g_menuscreen_w > fs_w)
248     g_menuscreen_w = fs_w;
249   g_menuscreen_h = 480;
250   if (fs_h != 0) {
251     h = fs_h;
252     if (window_b && h > window_b)
253       h -= window_b;
254     if (g_menuscreen_h > h)
255       g_menuscreen_h = h;
256   }
257
258   plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 0,
259       (flags = get_screen_flags()));
260   if (plat_sdl_screen == NULL) {
261     fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
262     plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
263     if (plat_sdl_screen == NULL) {
264       fprintf(stderr, "attempting SDL_SWSURFACE fallback\n");
265       screen_flags = SDL_SWSURFACE;
266       plat_sdl_screen = SDL_SetVideoMode(0, 0, 0, (flags = get_screen_flags()));
267       if (plat_sdl_screen == NULL) {
268         fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
269         goto fail;
270       }
271     }
272
273     if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
274       fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
275               plat_sdl_screen->w, plat_sdl_screen->h);
276       goto fail;
277     }
278   }
279   g_menuscreen_w = plat_sdl_screen->w;
280   g_menuscreen_h = plat_sdl_screen->h;
281   g_menuscreen_pp = g_menuscreen_w;
282   screen_flags = flags;
283   window_w = plat_sdl_screen->w;
284   window_h = plat_sdl_screen->h;
285
286   plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
287     SDL_UYVY_OVERLAY, plat_sdl_screen);
288   if (plat_sdl_overlay != NULL) {
289     printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
290       plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
291       plat_sdl_overlay->hw_overlay);
292
293     // some platforms require "native" bpp for this to work
294     if (plat_sdl_overlay->hw_overlay)
295       overlay_works = 1;
296     else
297       fprintf(stderr, "warning: video overlay is not hardware accelerated, "
298                       "not going to use it.\n");
299     SDL_FreeYUVOverlay(plat_sdl_overlay);
300     plat_sdl_overlay = NULL;
301   }
302   else
303     fprintf(stderr, "overlay is not available.\n");
304
305   SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
306   ret = -1;
307   try_gl = 1;
308   env = getenv("DISPLAY");
309   if (env && env[0] != ':') {
310     fprintf(stderr, "looks like a remote DISPLAY, "
311       "not trying GL (use PICOFE_GL=1 to override)\n");
312     // because some drivers just kill the program with no way to recover
313     try_gl = 0;
314   }
315   env = getenv("PICOFE_GL");
316   if (env)
317     try_gl = atoi(env);
318   if (try_gl) {
319     ret = gl_init(display, &gl_quirks);
320     if (ret == 0) {
321       update_wm_display_window();
322       ret = gl_create(window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
323     }
324   }
325   if (ret == 0) {
326     gl_announce();
327     gl_works = 1;
328     gl_destroy();
329   }
330
331   i = 0;
332   vout_list[i++] = "SDL Window";
333   if (overlay_works) {
334     plat_target.vout_method = vout_mode_overlay = i;
335     vout_list[i++] = "Video Overlay";
336 #ifdef SDL_OVERLAY_2X
337     plat_target.vout_method = vout_mode_overlay2x = i;
338     vout_list[i++] = "Video Overlay 2x";
339 #endif
340   }
341   if (gl_works) {
342     plat_target.vout_method = vout_mode_gl = i;
343     vout_list[i++] = "OpenGL";
344   }
345   plat_target.vout_methods = vout_list;
346
347   plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
348   return 0;
349
350 fail:
351   SDL_Quit();
352   return -1;
353 }
354
355 void plat_sdl_finish(void)
356 {
357   if (plat_sdl_overlay != NULL) {
358     SDL_FreeYUVOverlay(plat_sdl_overlay);
359     plat_sdl_overlay = NULL;
360   }
361   if (plat_sdl_gl_active) {
362     gl_destroy();
363     plat_sdl_gl_active = 0;
364   }
365   gl_shutdown();
366
367   // restore back to initial resolution
368   // resolves black screen issue on R-Pi
369   if (strcmp(vid_drv_name, "x11") != 0)
370     SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
371   SDL_Quit();
372 }
373
374 void plat_sdl_overlay_clear(void)
375 {
376   int pixels = plat_sdl_overlay->pitches[0]/2 * plat_sdl_overlay->h;
377   int *dst = (int *)plat_sdl_overlay->pixels[0];
378   int v = 0x10801080;
379
380   for (; pixels > 7; dst += 4, pixels -= 2 * 4)
381     dst[0] = dst[1] = dst[2] = dst[3] = v;
382
383   for (; pixels > 1; dst++, pixels -= 2)
384     *dst = v;
385 }
386
387 int plat_sdl_is_windowed(void)
388 {
389   return window_b != 0;
390 }
391
392 int plat_sdl_is_fullscreen(void)
393 {
394   // consider window title bar and screen menu here
395   return window_w >= fs_w && window_h >= fs_h - 2*window_b;
396 }
397
398 void plat_sdl_gl_scaling(int type)
399 {
400   gl_quirks &= ~GL_QUIRK_SCALING_NEAREST;
401   if (type == 0)
402     gl_quirks |= GL_QUIRK_SCALING_NEAREST;
403 }
404
405 void plat_sdl_gl_vsync(int on)
406 {
407   gl_quirks &= ~GL_QUIRK_VSYNC_ON;
408   if (on)
409     gl_quirks |= GL_QUIRK_VSYNC_ON;
410 }
411
412 // vim:shiftwidth=2:expandtab