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