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;
88 p_vout->p_sys->b_must_acquire = 0;
90 /* Initialize library */
91 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
93 intf_ErrMsg( "error: can't initialize SDL library: %s\n",
95 free( p_vout->p_sys );
99 /* Force the software yuv even if it is not used */
100 /* If we don't do this, p_vout is not correctly initialized
101 and it's impossible to switch between soft/hard yuv */
102 p_vout->b_need_render = 1;
104 if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
106 p_vout->p_sys->b_fullscreen = 1;
110 p_vout->p_sys->b_fullscreen = 0;
113 p_vout->p_sys->b_reopen_display = 1;
115 if( SDLOpenDisplay(p_vout) )
117 intf_ErrMsg( "error: can't initialize SDL library: %s\n",
119 free( p_vout->p_sys );
126 /*****************************************************************************
127 * vout_SDLInit: initialize SDL video thread output method
128 *****************************************************************************
129 * This function initialize the SDL display device.
130 *****************************************************************************/
131 int vout_SDLInit( vout_thread_t *p_vout )
133 /* Acquire first buffer */
134 if( p_vout->p_sys->b_must_acquire )
136 SDL_LockSurface(p_vout->p_sys->p_display);
142 /*****************************************************************************
143 * vout_SDLEnd: terminate Sys video thread output method
144 *****************************************************************************
145 * Terminate an output method created by vout_SDLCreate
146 *****************************************************************************/
147 void vout_SDLEnd( vout_thread_t *p_vout )
150 if( p_vout->p_sys->b_must_acquire )
152 SDL_UnlockSurface ( p_vout->p_sys->p_display );
154 free( p_vout->p_sys );
157 /*****************************************************************************
158 * vout_SDLDestroy: destroy Sys video thread output method
159 *****************************************************************************
160 * Terminate an output method created by vout_SDLCreate
161 *****************************************************************************/
162 void vout_SDLDestroy( vout_thread_t *p_vout )
164 SDLCloseDisplay( p_vout );
165 free( p_vout->p_sys );
168 /*****************************************************************************
169 * vout_SDLManage: handle Sys events
170 *****************************************************************************
171 * This function should be called regularly by video output thread. It returns
172 * a non null value if an error occured.
173 *****************************************************************************/
174 int vout_SDLManage( vout_thread_t *p_vout )
176 /* If the display has to be reopened we do so */
177 if( p_vout->p_sys->b_reopen_display )
179 p_vout->p_sys->b_must_acquire = 0;
180 if( p_vout->p_sys->p_display )
182 if( p_vout->p_sys->p_overlay )
184 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
185 p_vout->p_sys->p_overlay = NULL;
187 SDL_FreeSurface( p_vout->p_sys->p_display );
188 p_vout->p_sys->p_display = NULL;
191 if( SDLOpenDisplay(p_vout) )
193 intf_ErrMsg( "error: can't open DISPLAY default display\n" );
196 p_vout->p_sys->b_reopen_display = 0;
201 /*****************************************************************************
202 * vout_SDLDisplay: displays previously rendered output
203 *****************************************************************************
204 * This function send the currently rendered image to the display, wait until
205 * it is displayed and switch the two rendering buffer, preparing next frame.
206 *****************************************************************************/
207 void vout_SDLDisplay( vout_thread_t *p_vout )
210 if(p_vout->b_need_render)
212 /* Change display frame */
213 if( p_vout->p_sys->b_must_acquire )
216 SDL_Flip( p_vout->p_sys->p_display );
218 /* Swap buffers and change write frame */
219 SDL_LockSurface ( p_vout->p_sys->p_display );
226 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to render
228 if( p_vout->p_sys->b_must_acquire )
230 /* TODO: support for streams other than 4:2:0 */
231 /* create the overlay if necessary */
232 if( !p_vout->p_sys->p_overlay )
234 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
235 p_vout->p_rendered_pic->i_width,
236 p_vout->p_rendered_pic->i_height,
238 p_vout->p_sys->p_display
240 intf_Msg("[YUV acceleration] : %d,\n",
241 p_vout->p_sys->p_overlay->hw_overlay);
244 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
245 /* copy the data into video buffers */
247 memcpy(p_vout->p_sys->p_overlay->pixels[0],
248 p_vout->p_rendered_pic->p_y,
249 p_vout->p_sys->p_overlay->h *
250 p_vout->p_sys->p_overlay->pitches[0]);
252 memcpy(p_vout->p_sys->p_overlay->pixels[1],
253 p_vout->p_rendered_pic->p_v,
254 p_vout->p_sys->p_overlay->h *
255 p_vout->p_sys->p_overlay->pitches[1] / 2);
257 memcpy(p_vout->p_sys->p_overlay->pixels[2],
258 p_vout->p_rendered_pic->p_u,
259 p_vout->p_sys->p_overlay->h *
260 p_vout->p_sys->p_overlay->pitches[2] / 2);
262 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
263 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
264 disp.x = (p_vout->i_width - disp.w)/2;
265 disp.y = (p_vout->i_height - disp.h)/2;
267 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
268 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
273 /* following functions are local */
275 /*****************************************************************************
276 * SDLOpenDisplay: open and initialize SDL device
277 *****************************************************************************
278 * Open and initialize display according to preferences specified in the vout
280 *****************************************************************************/
281 static int SDLOpenDisplay( vout_thread_t *p_vout )
283 SDL_Rect clipping_rect;
286 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
288 if( p_vout->p_sys->b_fullscreen )
290 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width,
293 SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN );
296 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width,
299 SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF );
303 if( p_vout->p_sys->p_display == NULL )
305 intf_ErrMsg( "error: can't open DISPLAY default display\n" );
308 SDL_LockSurface(p_vout->p_sys->p_display);
309 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
310 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
312 /* Check buffers properties */
313 p_vout->p_sys->b_must_acquire = 1; /* always acquire */
314 p_vout->p_sys->p_buffer[ 0 ] =
315 p_vout->p_sys->p_display->pixels;
317 SDL_Flip(p_vout->p_sys->p_display);
318 p_vout->p_sys->p_buffer[ 1 ] =
319 p_vout->p_sys->p_display->pixels;
320 SDL_Flip(p_vout->p_sys->p_display);
322 /* Set graphic context colors */
325 col_fg.r = col_fg.g = col_fg.b = -1;
326 col_bg.r = col_bg.g = col_bg.b = 0;
327 if( ggiSetGCForeground(p_vout->p_sys->p_display,
328 ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
329 ggiSetGCBackground(p_vout->p_sys->p_display,
330 ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
332 intf_ErrMsg("error: can't set colors\n");
333 ggiClose( p_vout->p_sys->p_display );
339 /* Set clipping for text */
342 clipping_rect.w = p_vout->p_sys->p_display->w;
343 clipping_rect.h = p_vout->p_sys->p_display->h;
344 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
348 /* Set thread information */
349 p_vout->i_width = p_vout->p_sys->p_display->w;
350 p_vout->i_height = p_vout->p_sys->p_display->h;
352 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
353 p_vout->p_sys->p_display->w ;
355 p_vout->i_screen_depth = p_vout->p_sys->p_display->format->BitsPerPixel;
356 p_vout->i_bytes_per_pixel = p_vout->p_sys->p_display->format->BytesPerPixel;
357 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
358 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
359 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
361 /* FIXME: palette in 8bpp ?? */
362 /* Set and initialize buffers */
363 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
364 p_vout->p_sys->p_buffer[ 1 ] );
369 /*****************************************************************************
370 * SDLCloseDisplay: close and reset SDL device
371 *****************************************************************************
372 * This function returns all resources allocated by SDLOpenDisplay and restore
373 * the original state of the device.
374 *****************************************************************************/
375 static void SDLCloseDisplay( vout_thread_t *p_vout )
377 if( p_vout->p_sys->p_display )
379 p_vout->p_sys->b_must_acquire = 0;
380 if( p_vout->p_sys->p_overlay )
382 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
383 p_vout->p_sys->p_overlay = NULL;
385 SDL_FreeSurface( p_vout->p_sys->p_display );
386 p_vout->p_sys->p_display = NULL;