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