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"
45 /*****************************************************************************
46 * vout_sys_t: video output SDL method descriptor
47 *****************************************************************************
48 * This structure is part of the video output thread descriptor.
49 * It describes the SDL specific properties of an output thread.
50 *****************************************************************************/
51 typedef struct vout_sys_s
55 SDL_Surface * p_display; /* display device */
56 SDL_Overlay * p_overlay; /* overlay device */
57 boolean_t b_fullscreen;
58 boolean_t b_reopen_display;
59 boolean_t b_toggle_fullscreen;
61 /* Buffers informations */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int SDLOpenDisplay ( vout_thread_t *p_vout );
68 static void SDLCloseDisplay ( vout_thread_t *p_vout );
69 static void SDLToggleFullScreen ( vout_thread_t *p_vout );
71 /*****************************************************************************
72 * vout_SDLCreate: allocate SDL video thread output method
73 *****************************************************************************
74 * This function allocate and initialize a SDL vout method. It uses some of the
75 * vout properties to choose the correct mode, and change them according to the
77 *****************************************************************************/
78 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
79 int i_root_window, void *p_data )
81 /* Allocate structure */
82 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
83 if( p_vout->p_sys == NULL )
85 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
89 p_vout->p_sys->p_display = NULL;
90 p_vout->p_sys->p_overlay = NULL;
92 /* Initialize library */
93 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0 )
95 intf_ErrMsg( "error: can't initialize SDL library: %s",
97 free( p_vout->p_sys );
101 /* Force the software yuv even if it is not used */
102 /* If we don't do this, p_vout is not correctly initialized
103 and it's impossible to switch between soft/hard yuv */
104 p_vout->b_need_render = 1;
106 if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
108 p_vout->p_sys->b_fullscreen = 1;
112 p_vout->p_sys->b_fullscreen = 0;
115 p_vout->p_sys->i_width = VOUT_WIDTH_DEFAULT;
116 p_vout->p_sys->i_height = 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_SDLDisplay: displays previously rendered output
192 *****************************************************************************
193 * This function send the currently rendered image to the display, wait until
194 * it is displayed and switch the two rendering buffer, preparing next frame.
195 *****************************************************************************/
196 void vout_SDLDisplay( vout_thread_t *p_vout )
199 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
201 if(p_vout->b_need_render)
203 /* Change display frame */
204 SDL_Flip( p_vout->p_sys->p_display );
210 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
213 /* TODO: support for streams other than 4:2:0 */
214 /* create the overlay if necessary */
215 if( p_vout->p_sys->p_overlay == NULL )
217 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
218 p_vout->p_rendered_pic->i_width,
219 p_vout->p_rendered_pic->i_height,
221 p_vout->p_sys->p_display
223 intf_Msg("[YUV acceleration] : %d,",
224 p_vout->p_sys->p_overlay->hw_overlay);
227 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
228 /* copy the data into video buffers */
230 memcpy(p_vout->p_sys->p_overlay->pixels[0],
231 p_vout->p_rendered_pic->p_y,
232 p_vout->p_sys->p_overlay->h *
233 p_vout->p_sys->p_overlay->pitches[0]);
235 memcpy(p_vout->p_sys->p_overlay->pixels[1],
236 p_vout->p_rendered_pic->p_v,
237 p_vout->p_sys->p_overlay->h *
238 p_vout->p_sys->p_overlay->pitches[1] / 2);
240 memcpy(p_vout->p_sys->p_overlay->pixels[2],
241 p_vout->p_rendered_pic->p_u,
242 p_vout->p_sys->p_overlay->h *
243 p_vout->p_sys->p_overlay->pitches[2] / 2);
245 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
246 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
247 disp.x = (p_vout->i_width - disp.w)/2;
248 disp.y = (p_vout->i_height - disp.h)/2;
250 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
251 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
256 /* following functions are local */
258 /*****************************************************************************
259 * SDLOpenDisplay: open and initialize SDL device
260 *****************************************************************************
261 * Open and initialize display according to preferences specified in the vout
263 *****************************************************************************/
264 static int SDLOpenDisplay( vout_thread_t *p_vout )
266 SDL_Rect clipping_rect;
270 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
273 /* init flags and cursor */
274 flags = SDL_ANYFORMAT;
276 if( p_vout->p_sys->b_fullscreen )
277 flags |= SDL_FULLSCREEN;
279 flags |= SDL_RESIZABLE;
281 if( p_vout->b_need_render )
282 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
284 flags |= SDL_SWSURFACE; /* save video memory */
286 bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
287 p_vout->p_sys->i_height,
288 p_vout->i_screen_depth, flags);
292 intf_ErrMsg( "error: can't open DISPLAY default display" );
296 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
297 p_vout->p_sys->i_height,
300 if( p_vout->p_sys->p_display == NULL )
302 intf_ErrMsg( "error: can't open DISPLAY default display" );
306 SDL_LockSurface(p_vout->p_sys->p_display);
308 if( p_vout->p_sys->b_fullscreen )
313 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
314 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
316 if( p_vout->b_need_render )
318 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
319 SDL_Flip(p_vout->p_sys->p_display);
320 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
321 SDL_Flip(p_vout->p_sys->p_display);
323 /* Set clipping for text */
326 clipping_rect.w = p_vout->p_sys->p_display->w;
327 clipping_rect.h = p_vout->p_sys->p_display->h;
328 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
330 /* Set thread information */
331 p_vout->i_width = p_vout->p_sys->p_display->w;
332 p_vout->i_height = p_vout->p_sys->p_display->h;
333 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
335 p_vout->i_screen_depth =
336 p_vout->p_sys->p_display->format->BitsPerPixel;
337 p_vout->i_bytes_per_pixel =
338 p_vout->p_sys->p_display->format->BytesPerPixel;
340 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
341 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
342 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
344 /* FIXME: palette in 8bpp ?? */
345 /* Set and initialize buffers */
346 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
347 p_vout->p_sys->p_buffer[ 1 ] );
351 p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
352 p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
354 /* Set thread information */
355 p_vout->i_width = p_vout->p_sys->p_display->w;
356 p_vout->i_height = p_vout->p_sys->p_display->h;
357 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
359 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
360 p_vout->p_sys->p_buffer[ 1 ] );
363 p_vout->i_changes |= VOUT_YUV_CHANGE;
365 p_vout->p_sys->b_reopen_display = 0;
370 /*****************************************************************************
371 * SDLCloseDisplay: close and reset SDL device
372 *****************************************************************************
373 * This function returns all resources allocated by SDLOpenDisplay and restore
374 * the original state of the device.
375 *****************************************************************************/
376 static void SDLCloseDisplay( vout_thread_t *p_vout )
378 if( p_vout->p_sys->p_display != NULL )
380 if( p_vout->p_sys->p_overlay != NULL )
382 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
383 p_vout->p_sys->p_overlay = NULL;
385 SDL_UnlockSurface ( p_vout->p_sys->p_display );
386 SDL_FreeSurface( p_vout->p_sys->p_display );
387 p_vout->p_sys->p_display = NULL;
391 /*****************************************************************************
392 * SDLToggleFullScreen: toggle fullscreen
393 *****************************************************************************
394 * This function toggles the fullscreen state of the surface.
395 *****************************************************************************/
396 static void SDLToggleFullScreen( vout_thread_t *p_vout )
398 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
400 if( p_vout->p_sys->b_fullscreen )
405 p_vout->p_sys->b_toggle_fullscreen = 0;