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