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