make gl layer reinitializable
[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     int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
121     plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0);
122     if (!plat_sdl_gl_active) {
123       fprintf(stderr, "warning: could not init GL.\n");
124       plat_target.vout_method = 0;
125     }
126   }
127
128   if (plat_target.vout_method == 0) {
129     Uint32 flags;
130     int win_w = w;
131     int win_h = h;
132
133 #if defined SDL_SURFACE_SW
134     flags = SDL_SWSURFACE;
135 #elif defined(SDL_TRIPLEBUF) && defined(SDL_BUFFER_3X)
136     flags = SDL_HWSURFACE | SDL_TRIPLEBUF;
137 #else
138     flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
139 #endif
140     if (plat_target.vout_fullscreen && fs_w && fs_h) {
141       flags |= SDL_FULLSCREEN;
142       win_w = fs_w;
143       win_h = fs_h;
144     }
145
146     SDL_PumpEvents();
147
148     if (!plat_sdl_screen || screen_flags != flags ||
149         plat_sdl_screen->w != win_w || plat_sdl_screen->h != win_h)
150       plat_sdl_screen = SDL_SetVideoMode(win_w, win_h, 16, flags);
151     screen_flags = flags;
152     if (plat_sdl_screen == NULL) {
153       fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
154       return -1;
155     }
156   }
157
158   old_fullscreen = plat_target.vout_fullscreen;
159   if (plat_sdl_resize_cb != NULL)
160     plat_sdl_resize_cb(plat_sdl_screen->w, plat_sdl_screen->h);
161
162   return 0;
163 }
164
165 void plat_sdl_event_handler(void *event_)
166 {
167   static int was_active;
168   SDL_Event *event = event_;
169
170   switch (event->type) {
171   case SDL_VIDEORESIZE:
172     //printf("resize %dx%d\n", event->resize.w, event->resize.h);
173     if (plat_target.vout_method != 0
174         && !plat_target.vout_fullscreen && !old_fullscreen)
175     {
176       window_w = event->resize.w & ~3;
177       window_h = event->resize.h & ~3;
178       plat_sdl_change_video_mode(0, 0, 1);
179     }
180     break;
181   case SDL_ACTIVEEVENT:
182     if (event->active.gain && !was_active) {
183       if (plat_sdl_overlay != NULL) {
184         SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
185         SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
186       }
187       else if (plat_sdl_gl_active) {
188         if (gl_quirks & GL_QUIRK_ACTIVATE_RECREATE) {
189           int sw = plat_sdl_screen->w, sh = plat_sdl_screen->h;
190           gl_finish();
191           plat_sdl_gl_active = (gl_init(display, window, &gl_quirks, sw, sh) == 0);
192         }
193         gl_flip(NULL, 0, 0);
194       }
195       // else SDL takes care of it
196     }
197     was_active = event->active.gain;
198     break;
199   case SDL_QUIT:
200     if (plat_sdl_quit_cb != NULL)
201       plat_sdl_quit_cb();
202     break;
203   }
204 }
205
206 int plat_sdl_init(void)
207 {
208   static const char *vout_list[] = { NULL, NULL, NULL, NULL, NULL };
209   const SDL_VideoInfo *info;
210   SDL_SysWMinfo wminfo;
211   const char *env;
212   int overlay_works = 0;
213   int gl_works = 0;
214   int i, ret, h;
215   int try_gl;
216
217   ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
218   if (ret != 0) {
219     fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
220     return -1;
221   }
222
223   info = SDL_GetVideoInfo();
224   if (info != NULL) {
225     fs_w = info->current_w;
226     fs_h = info->current_h;
227     if (info->wm_available)
228       window_b = WM_DECORATION_H;
229     printf("plat_sdl: using %dx%d as fullscreen resolution\n", fs_w, fs_h);
230   }
231
232   g_menuscreen_w = 640;
233   if (fs_w != 0 && g_menuscreen_w > fs_w)
234     g_menuscreen_w = fs_w;
235   g_menuscreen_h = 480;
236   if (fs_h != 0) {
237     h = fs_h;
238     if (window_b && h > window_b)
239       h -= window_b;
240     if (g_menuscreen_h > h)
241       g_menuscreen_h = h;
242   }
243
244   plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h, 16, SDL_HWSURFACE);
245   if (plat_sdl_screen == NULL) {
246     plat_sdl_screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
247     if (plat_sdl_screen == NULL) {
248       fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
249       goto fail;
250     }
251
252     if (plat_sdl_screen->w < 320 || plat_sdl_screen->h < 240) {
253       fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
254               plat_sdl_screen->w, plat_sdl_screen->h);
255       goto fail;
256     }
257   }
258   g_menuscreen_w = window_w = plat_sdl_screen->w;
259   g_menuscreen_h = window_h = plat_sdl_screen->h;
260   g_menuscreen_pp = g_menuscreen_w;
261
262   // overlay/gl require native bpp in some cases..
263   plat_sdl_screen = SDL_SetVideoMode(g_menuscreen_w, g_menuscreen_h,
264     0, plat_sdl_screen->flags);
265   if (plat_sdl_screen == NULL) {
266     fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
267     goto fail;
268   }
269
270   plat_sdl_overlay = SDL_CreateYUVOverlay(plat_sdl_screen->w, plat_sdl_screen->h,
271     SDL_UYVY_OVERLAY, plat_sdl_screen);
272   if (plat_sdl_overlay != NULL) {
273     printf("plat_sdl: overlay: fmt %x, planes: %d, pitch: %d, hw: %d\n",
274       plat_sdl_overlay->format, plat_sdl_overlay->planes, *plat_sdl_overlay->pitches,
275       plat_sdl_overlay->hw_overlay);
276
277     if (plat_sdl_overlay->hw_overlay)
278       overlay_works = 1;
279     else
280       fprintf(stderr, "warning: video overlay is not hardware accelerated, "
281                       "not going to use it.\n");
282     SDL_FreeYUVOverlay(plat_sdl_overlay);
283     plat_sdl_overlay = NULL;
284   }
285   else
286     fprintf(stderr, "overlay is not available.\n");
287
288   // get x11 display/window for GL
289   SDL_VideoDriverName(vid_drv_name, sizeof(vid_drv_name));
290 #ifdef SDL_VIDEO_DRIVER_X11
291   if (strcmp(vid_drv_name, "x11") == 0) {
292     SDL_VERSION(&wminfo.version);
293     ret = SDL_GetWMInfo(&wminfo);
294     if (ret > 0) {
295       display = wminfo.info.x11.display;
296       window = (void *)wminfo.info.x11.window;
297     }
298   }
299 #else
300   (void)wminfo;
301 #endif
302
303   ret = -1;
304   try_gl = 1;
305   env = getenv("DISPLAY");
306   if (env && env[0] != ':') {
307     fprintf(stderr, "looks like a remote DISPLAY, "
308       "not trying GL (use PICOFE_GL=1 to override)\n");
309     // because some drivers just kill the program with no way to recover
310     try_gl = 0;
311   }
312   env = getenv("PICOFE_GL");
313   if (env)
314     try_gl = atoi(env);
315   if (try_gl)
316     ret = gl_init(display, window, &gl_quirks, g_menuscreen_w, g_menuscreen_h);
317   if (ret == 0) {
318     gl_announce();
319     gl_works = 1;
320     gl_finish();
321   }
322
323   i = 0;
324   vout_list[i++] = "SDL Window";
325   if (overlay_works) {
326     plat_target.vout_method = vout_mode_overlay = i;
327     vout_list[i++] = "Video Overlay";
328 #ifdef SDL_OVERLAY_2X
329     vout_mode_overlay2x = i;
330     vout_list[i++] = "Video Overlay 2x";
331 #endif
332   }
333   if (gl_works) {
334     plat_target.vout_method = vout_mode_gl = i;
335     vout_list[i++] = "OpenGL";
336   }
337   plat_target.vout_methods = vout_list;
338
339   plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
340   return 0;
341
342 fail:
343   SDL_Quit();
344   return -1;
345 }
346
347 void plat_sdl_finish(void)
348 {
349   if (plat_sdl_overlay != NULL) {
350     SDL_FreeYUVOverlay(plat_sdl_overlay);
351     plat_sdl_overlay = NULL;
352   }
353   if (plat_sdl_gl_active) {
354     gl_finish();
355     plat_sdl_gl_active = 0;
356   }
357   // restore back to initial resolution
358   // resolves black screen issue on R-Pi
359   if (strcmp(vid_drv_name, "x11") != 0)
360     SDL_SetVideoMode(fs_w, fs_h, 16, SDL_SWSURFACE);
361   SDL_Quit();
362 }
363
364 void plat_sdl_overlay_clear(void)
365 {
366   int pixels = plat_sdl_overlay->w * plat_sdl_overlay->h;
367   int *dst = (int *)plat_sdl_overlay->pixels[0];
368   int v = 0x10801080;
369
370   for (; pixels > 7; dst += 4, pixels -= 2 * 4)
371     dst[0] = dst[1] = dst[2] = dst[3] = v;
372
373   for (; pixels > 1; dst++, pixels -= 2)
374     *dst = v;
375 }
376
377 // vim:shiftwidth=2:expandtab