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