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
53 SDL_Surface * p_display; /* display device */
54 SDL_Overlay * p_overlay; /* overlay device */
55 boolean_t b_fullscreen;
56 boolean_t b_reopen_display;
58 /* Buffers informations */
59 boolean_t b_must_acquire; /* must be acquired before writing */
62 /*****************************************************************************
64 *****************************************************************************/
65 static int SDLOpenDisplay ( vout_thread_t *p_vout );
66 static void SDLCloseDisplay ( vout_thread_t *p_vout );
68 /*****************************************************************************
69 * vout_SDLCreate: allocate SDL video thread output method
70 *****************************************************************************
71 * This function allocate and initialize a SDL vout method. It uses some of the
72 * vout properties to choose the correct mode, and change them according to the
74 *****************************************************************************/
75 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
76 int i_root_window, void *p_data )
78 /* Allocate structure */
79 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
80 if( p_vout->p_sys == NULL )
82 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
86 p_vout->p_sys->p_display = NULL;
87 p_vout->p_sys->p_overlay = NULL;
89 /* Initialize library */
90 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
92 intf_ErrMsg( "error: can't initialize SDL library: %s\n",
94 free( p_vout->p_sys );
98 if( SDLOpenDisplay(p_vout) )
100 intf_ErrMsg( "error: can't initialize SDL library: %s\n",
102 free( p_vout->p_sys );
106 /* Force the software yuv even if it is not used */
107 /* If we don't do this, p_vout is not correctly initialized
108 and it's impossible to switch between soft/hard yuv */
109 p_vout->b_need_render = 1;
111 if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
113 p_vout->p_sys->b_fullscreen = 1;
115 p_vout->p_sys->b_fullscreen = 0;
117 p_vout->p_sys->b_reopen_display = 1;
122 /*****************************************************************************
123 * vout_SDLInit: initialize SDL video thread output method
124 *****************************************************************************
125 * This function initialize the SDL display device.
126 *****************************************************************************/
127 int vout_SDLInit( vout_thread_t *p_vout )
129 /* Acquire first buffer */
130 if( p_vout->p_sys->b_must_acquire )
132 SDL_LockSurface(p_vout->p_sys->p_display);
138 /*****************************************************************************
139 * vout_SDLEnd: terminate Sys video thread output method
140 *****************************************************************************
141 * Terminate an output method created by vout_SDLCreate
142 *****************************************************************************/
143 void vout_SDLEnd( vout_thread_t *p_vout )
146 if( p_vout->p_sys->b_must_acquire )
148 SDL_UnlockSurface ( p_vout->p_sys->p_display );
150 free( p_vout->p_sys );
153 /*****************************************************************************
154 * vout_SDLDestroy: destroy Sys video thread output method
155 *****************************************************************************
156 * Terminate an output method created by vout_SDLCreate
157 *****************************************************************************/
158 void vout_SDLDestroy( vout_thread_t *p_vout )
160 SDLCloseDisplay( p_vout );
161 free( p_vout->p_sys );
164 /*****************************************************************************
165 * vout_SDLManage: handle Sys events
166 *****************************************************************************
167 * This function should be called regularly by video output thread. It returns
168 * a non null value if an error occured.
169 *****************************************************************************/
170 int vout_SDLManage( vout_thread_t *p_vout )
172 /* If the display has to be reopened we do so */
173 if( p_vout->p_sys->b_reopen_display )
175 p_vout->p_sys->b_must_acquire = 0;
176 if( p_vout->p_sys->p_display )
178 if( p_vout->p_sys->p_overlay )
180 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
181 p_vout->p_sys->p_overlay = NULL;
183 SDL_FreeSurface( p_vout->p_sys->p_display );
184 p_vout->p_sys->p_display = NULL;
187 if( SDLOpenDisplay(p_vout) )
189 intf_ErrMsg( "error: can't open DISPLAY default display\n" );
192 p_vout->p_sys->b_reopen_display = 0;
197 /*****************************************************************************
198 * vout_SDLDisplay: displays previously rendered output
199 *****************************************************************************
200 * This function send the currently rendered image to the display, wait until
201 * it is displayed and switch the two rendering buffer, preparing next frame.
202 *****************************************************************************/
203 void vout_SDLDisplay( vout_thread_t *p_vout )
206 if(p_vout->b_need_render)
208 /* Change display frame */
209 if( p_vout->p_sys->b_must_acquire )
212 SDL_Flip( p_vout->p_sys->p_display );
214 /* Swap buffers and change write frame */
215 SDL_LockSurface ( p_vout->p_sys->p_display );
222 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to render
224 if( p_vout->p_sys->b_must_acquire )
226 /* TODO: support for streams other than 4:2:0 */
227 /* create the overlay if necessary */
228 if( !p_vout->p_sys->p_overlay )
230 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
231 p_vout->p_rendered_pic->i_width,
232 p_vout->p_rendered_pic->i_height,
234 p_vout->p_sys->p_display
236 intf_Msg("[YUV acceleration] : %d\n",
237 p_vout->p_sys->p_overlay->hw_overlay);
240 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
241 /* copy the data into video buffers */
243 memcpy(p_vout->p_sys->p_overlay->pixels[0],
244 p_vout->p_rendered_pic->p_y,
245 p_vout->p_sys->p_overlay->h *
246 p_vout->p_sys->p_overlay->pitches[0]);
248 memcpy(p_vout->p_sys->p_overlay->pixels[1],
249 p_vout->p_rendered_pic->p_v,
250 p_vout->p_sys->p_overlay->h *
251 p_vout->p_sys->p_overlay->pitches[1] / 2);
253 memcpy(p_vout->p_sys->p_overlay->pixels[2],
254 p_vout->p_rendered_pic->p_u,
255 p_vout->p_sys->p_overlay->h *
256 p_vout->p_sys->p_overlay->pitches[2] / 2);
260 disp.w = p_vout->i_width;
261 disp.h = p_vout->i_height;
263 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
264 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
269 /* following functions are local */
271 /*****************************************************************************
272 * SDLOpenDisplay: open and initialize SDL device
273 *****************************************************************************
274 * Open and initialize display according to preferences specified in the vout
276 *****************************************************************************/
277 static int SDLOpenDisplay( vout_thread_t *p_vout )
279 SDL_Rect clipping_rect;
282 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
284 if( p_vout->p_sys->b_fullscreen )
286 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width,
289 SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN );
292 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width,
295 SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF );
299 if( p_vout->p_sys->p_display == NULL )
301 intf_ErrMsg( "error: can't open DISPLAY default display\n" );
304 p_vout->p_sys->p_overlay = NULL;
305 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
306 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
308 /* Check buffers properties */
309 p_vout->p_sys->b_must_acquire = 1; /* always acquire */
310 p_vout->p_sys->p_buffer[ 0 ] =
311 p_vout->p_sys->p_display->pixels;
313 SDL_Flip(p_vout->p_sys->p_display);
314 p_vout->p_sys->p_buffer[ 1 ] =
315 p_vout->p_sys->p_display->pixels;
316 SDL_Flip(p_vout->p_sys->p_display);
318 /* Set graphic context colors */
321 col_fg.r = col_fg.g = col_fg.b = -1;
322 col_bg.r = col_bg.g = col_bg.b = 0;
323 if( ggiSetGCForeground(p_vout->p_sys->p_display,
324 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
325 ggiSetGCBackground(p_vout->p_sys->p_display,
326 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
328 intf_ErrMsg("error: can't set colors\n");
329 ggiClose( p_vout->p_sys->p_display );
335 /* Set clipping for text */
338 clipping_rect.w = p_vout->p_sys->p_display->w;
339 clipping_rect.h = p_vout->p_sys->p_display->h;
340 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
344 /* Set thread information */
345 p_vout->i_width = p_vout->p_sys->p_display->w;
346 p_vout->i_height = p_vout->p_sys->p_display->h;
348 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
349 p_vout->p_sys->p_display->w ;
351 p_vout->i_screen_depth = p_vout->p_sys->p_display->format->BitsPerPixel;
352 p_vout->i_bytes_per_pixel = p_vout->p_sys->p_display->format->BytesPerPixel;
353 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
354 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
355 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
357 /* FIXME: palette in 8bpp ?? */
358 /* Set and initialize buffers */
359 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
360 p_vout->p_sys->p_buffer[ 1 ] );
365 /*****************************************************************************
366 * SDLCloseDisplay: close and reset SDL device
367 *****************************************************************************
368 * This function returns all resources allocated by SDLOpenDisplay and restore
369 * the original state of the device.
370 *****************************************************************************/
371 static void SDLCloseDisplay( vout_thread_t *p_vout )
373 if( p_vout->p_sys->p_display )
375 p_vout->p_sys->b_must_acquire = 0;
376 if( p_vout->p_sys->p_overlay )
378 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
379 p_vout->p_sys->p_overlay = NULL;
381 SDL_FreeSurface( p_vout->p_sys->p_display );
382 p_vout->p_sys->p_display = NULL;