1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
41 #include "video_output.h"
46 /*****************************************************************************
47 * vout_sys_t: video output SDL method descriptor
48 *****************************************************************************
49 * This structure is part of the video output thread descriptor.
50 * It describes the SDL specific properties of an output thread.
51 *****************************************************************************/
52 /* FIXME: SOME CLUELESS MORON DEFINED THIS STRUCTURE IN INTF_SDL.C AS WELL */
53 typedef struct vout_sys_s
57 SDL_Surface * p_display; /* display device */
58 SDL_Overlay * p_overlay; /* overlay device */
59 boolean_t b_fullscreen;
61 boolean_t b_reopen_display;
62 boolean_t b_toggle_fullscreen;
64 /* Buffers informations */
67 /*****************************************************************************
69 *****************************************************************************/
70 static int SDLOpenDisplay ( vout_thread_t *p_vout );
71 static void SDLCloseDisplay ( vout_thread_t *p_vout );
72 static void SDLToggleFullScreen ( vout_thread_t *p_vout );
74 /*****************************************************************************
75 * vout_SDLCreate: allocate SDL video thread output method
76 *****************************************************************************
77 * This function allocate and initialize a SDL vout method. It uses some of the
78 * vout properties to choose the correct mode, and change them according to the
80 *****************************************************************************/
81 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
82 int i_root_window, void *p_data )
84 /* Allocate structure */
85 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
86 if( p_vout->p_sys == NULL )
88 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
92 p_vout->p_sys->p_display = NULL;
93 p_vout->p_sys->p_overlay = NULL;
95 /* Initialize library */
96 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0 )
98 intf_ErrMsg( "error: can't initialize SDL library: %s",
100 free( p_vout->p_sys );
104 /* Force the software yuv even if it is not used */
105 /* If we don't do this, p_vout is not correctly initialized
106 and it's impossible to switch between soft/hard yuv */
107 p_vout->b_need_render = 1;
109 p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
110 VOUT_FULLSCREEN_DEFAULT );
111 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
112 VOUT_OVERLAY_DEFAULT );
113 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
114 VOUT_WIDTH_DEFAULT );
115 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
116 VOUT_HEIGHT_DEFAULT );
118 if( SDLOpenDisplay(p_vout) )
120 intf_ErrMsg( "error: can't initialize SDL library: %s",
125 p_vout->p_sys->b_toggle_fullscreen = 0;
130 /*****************************************************************************
131 * vout_SDLInit: initialize SDL video thread output method
132 *****************************************************************************
133 * This function initialize the SDL display device.
134 *****************************************************************************/
135 int vout_SDLInit( vout_thread_t *p_vout )
140 /*****************************************************************************
141 * vout_SDLEnd: terminate Sys video thread output method
142 *****************************************************************************
143 * Terminate an output method created by vout_SDLCreate
144 *****************************************************************************/
145 void vout_SDLEnd( vout_thread_t *p_vout )
147 SDLCloseDisplay( p_vout );
151 /*****************************************************************************
152 * vout_SDLDestroy: destroy Sys video thread output method
153 *****************************************************************************
154 * Terminate an output method created by vout_SDLCreate
155 *****************************************************************************/
156 void vout_SDLDestroy( vout_thread_t *p_vout )
158 free( p_vout->p_sys );
161 /*****************************************************************************
162 * vout_SDLManage: handle Sys events
163 *****************************************************************************
164 * This function should be called regularly by video output thread. It returns
165 * a non null value if an error occured.
166 *****************************************************************************/
167 int vout_SDLManage( vout_thread_t *p_vout )
169 /* If the display has to be reopened we do so */
170 if( p_vout->p_sys->b_reopen_display )
172 SDLCloseDisplay(p_vout);
174 if( SDLOpenDisplay(p_vout) )
176 intf_ErrMsg( "error: can't open DISPLAY default display" );
181 /* if fullscreen has to be toggled we do so */
182 if( p_vout->p_sys->b_toggle_fullscreen )
184 SDLToggleFullScreen(p_vout);
190 /*****************************************************************************
191 * vout_SDLSetPalette: sets an 8 bpp palette
192 *****************************************************************************
193 * This function sets the palette given as an argument. It does not return
194 * anything, but could later send information on which colors it was unable
196 *****************************************************************************/
197 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green, u16 *blue, u16 *transp)
199 /* Create a display surface with a grayscale palette */
200 SDL_Color colors[256];
203 /* Fill colors with color information */
204 for( i = 0; i < 256; i++ )
206 colors[ i ].r = red[ i ] >> 8;
207 colors[ i ].g = green[ i ] >> 8;
208 colors[ i ].b = blue[ i ] >> 8;
212 if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
214 intf_ErrMsg( "vout error: failed setting palette\n" );
219 /*****************************************************************************
220 * vout_SDLDisplay: displays previously rendered output
221 *****************************************************************************
222 * This function send the currently rendered image to the display, wait until
223 * it is displayed and switch the two rendering buffer, preparing next frame.
224 *****************************************************************************/
225 void vout_SDLDisplay( vout_thread_t *p_vout )
228 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
230 if(p_vout->b_need_render)
232 /* Change display frame */
233 SDL_Flip( p_vout->p_sys->p_display );
239 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
242 /* TODO: support for streams other than 4:2:0 */
243 /* create the overlay if necessary */
244 if( p_vout->p_sys->p_overlay == NULL )
246 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
247 p_vout->p_rendered_pic->i_width,
248 p_vout->p_rendered_pic->i_height,
250 p_vout->p_sys->p_display
252 intf_Msg("vout: YUV acceleration %s",
253 p_vout->p_sys->p_overlay->hw_overlay
254 ? "activated" : "unavailable !" );
257 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
258 /* copy the data into video buffers */
260 memcpy(p_vout->p_sys->p_overlay->pixels[0],
261 p_vout->p_rendered_pic->p_y,
262 p_vout->p_sys->p_overlay->h *
263 p_vout->p_sys->p_overlay->pitches[0]);
265 memcpy(p_vout->p_sys->p_overlay->pixels[1],
266 p_vout->p_rendered_pic->p_v,
267 p_vout->p_sys->p_overlay->h *
268 p_vout->p_sys->p_overlay->pitches[1] / 2);
270 memcpy(p_vout->p_sys->p_overlay->pixels[2],
271 p_vout->p_rendered_pic->p_u,
272 p_vout->p_sys->p_overlay->h *
273 p_vout->p_sys->p_overlay->pitches[2] / 2);
275 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
276 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
277 disp.x = (p_vout->i_width - disp.w)/2;
278 disp.y = (p_vout->i_height - disp.h)/2;
280 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
281 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
286 /* following functions are local */
288 /*****************************************************************************
289 * SDLOpenDisplay: open and initialize SDL device
290 *****************************************************************************
291 * Open and initialize display according to preferences specified in the vout
293 *****************************************************************************/
294 static int SDLOpenDisplay( vout_thread_t *p_vout )
296 SDL_Rect clipping_rect;
300 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
303 /* init flags and cursor */
304 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
306 if( p_vout->p_sys->b_fullscreen )
307 flags |= SDL_FULLSCREEN;
309 flags |= SDL_RESIZABLE;
311 if( p_vout->b_need_render )
312 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
314 flags |= SDL_SWSURFACE; /* save video memory */
316 bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
317 p_vout->p_sys->i_height,
318 p_vout->i_screen_depth, flags);
322 intf_ErrMsg( "error: can't open DISPLAY default display" );
326 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
327 p_vout->p_sys->i_height,
330 if( p_vout->p_sys->p_display == NULL )
332 intf_ErrMsg( "error: can't open DISPLAY default display" );
336 SDL_LockSurface(p_vout->p_sys->p_display);
338 if( p_vout->p_sys->b_fullscreen )
343 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
344 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
346 if( p_vout->b_need_render )
348 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
349 SDL_Flip(p_vout->p_sys->p_display);
350 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
351 SDL_Flip(p_vout->p_sys->p_display);
353 /* Set clipping for text */
356 clipping_rect.w = p_vout->p_sys->p_display->w;
357 clipping_rect.h = p_vout->p_sys->p_display->h;
358 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
360 /* Set thread information */
361 p_vout->i_width = p_vout->p_sys->p_display->w;
362 p_vout->i_height = p_vout->p_sys->p_display->h;
363 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
365 p_vout->i_screen_depth =
366 p_vout->p_sys->p_display->format->BitsPerPixel;
367 p_vout->i_bytes_per_pixel =
368 p_vout->p_sys->p_display->format->BytesPerPixel;
370 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
371 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
372 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
374 /* FIXME: palette in 8bpp ?? */
375 /* Set and initialize buffers */
376 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
377 p_vout->p_sys->p_buffer[ 1 ] );
381 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
382 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
384 /* Set thread information */
385 p_vout->i_width = p_vout->p_sys->p_display->w;
386 p_vout->i_height = p_vout->p_sys->p_display->h;
387 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
389 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
390 p_vout->p_sys->p_buffer[ 1 ] );
393 p_vout->i_changes |= VOUT_YUV_CHANGE;
395 p_vout->p_sys->b_reopen_display = 0;
400 /*****************************************************************************
401 * SDLCloseDisplay: close and reset SDL device
402 *****************************************************************************
403 * This function returns all resources allocated by SDLOpenDisplay and restore
404 * the original state of the device.
405 *****************************************************************************/
406 static void SDLCloseDisplay( vout_thread_t *p_vout )
408 if( p_vout->p_sys->p_display != NULL )
410 if( p_vout->p_sys->p_overlay != NULL )
412 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
413 p_vout->p_sys->p_overlay = NULL;
415 SDL_UnlockSurface ( p_vout->p_sys->p_display );
416 SDL_FreeSurface( p_vout->p_sys->p_display );
417 p_vout->p_sys->p_display = NULL;
421 /*****************************************************************************
422 * SDLToggleFullScreen: toggle fullscreen
423 *****************************************************************************
424 * This function toggles the fullscreen state of the surface.
425 *****************************************************************************/
426 static void SDLToggleFullScreen( vout_thread_t *p_vout )
428 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
430 if( p_vout->p_sys->b_fullscreen )
435 p_vout->p_sys->b_toggle_fullscreen = 0;