- one typo.
[vlc.git] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  * 
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <SDL/SDL.h>
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 #include "intf_msg.h"
44
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
52 {
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;
57     Uint8   *   p_buffer[2];
58                                                      /* Buffers informations */
59     boolean_t   b_must_acquire;           /* must be acquired before writing */
60 }   vout_sys_t;
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int     SDLOpenDisplay   ( vout_thread_t *p_vout );
66 static void    SDLCloseDisplay  ( vout_thread_t *p_vout );
67
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
73  * mode actually used.
74  *****************************************************************************/
75 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
76                     int i_root_window, void *p_data )
77 {
78     /* Allocate structure */
79     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
80     if( p_vout->p_sys == NULL )
81     {
82         intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
83         return( 1 );
84     }
85
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;
89
90     /* Initialize library */
91     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
92     {
93         intf_ErrMsg( "error: can't initialize SDL library: %s\n",
94                      SDL_GetError() );
95         free( p_vout->p_sys );
96         return( 1 );
97     }
98
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;
103
104     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
105     {
106         p_vout->p_sys->b_fullscreen = 1;
107     }
108     else
109     {
110         p_vout->p_sys->b_fullscreen = 0;
111     }
112
113     p_vout->p_sys->b_reopen_display = 1;
114
115     if( SDLOpenDisplay(p_vout) )
116     {
117         intf_ErrMsg( "error: can't initialize SDL library: %s\n",
118                      SDL_GetError() );
119         free( p_vout->p_sys );
120         return( 1 );
121     }
122
123     return( 0 );
124 }
125
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 )
132 {
133     /* Acquire first buffer */
134     if( p_vout->p_sys->b_must_acquire )
135     {
136         SDL_LockSurface(p_vout->p_sys->p_display);
137     }
138
139     return( 0 );
140 }
141
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 )
148 {
149     /* Release buffer */
150     if( p_vout->p_sys->b_must_acquire )
151     {
152         SDL_UnlockSurface ( p_vout->p_sys->p_display );
153     }
154     free( p_vout->p_sys );
155 }
156
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 )
163 {
164     SDLCloseDisplay( p_vout );
165     free( p_vout->p_sys );
166 }
167
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 )
175 {
176     /* If the display has to be reopened we do so */
177     if( p_vout->p_sys->b_reopen_display )
178     {
179         p_vout->p_sys->b_must_acquire = 0;
180         if( p_vout->p_sys->p_display )
181         {
182             if( p_vout->p_sys->p_overlay )
183             {
184                 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
185                 p_vout->p_sys->p_overlay = NULL;
186             }
187             SDL_FreeSurface( p_vout->p_sys->p_display );
188             p_vout->p_sys->p_display = NULL;
189         }
190
191         if( SDLOpenDisplay(p_vout) )
192         {
193             intf_ErrMsg( "error: can't open DISPLAY default display\n" );
194             return( 1 );
195         }
196         p_vout->p_sys->b_reopen_display = 0;
197     }
198     return( 0 );
199 }
200
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 )
208 {
209     SDL_Rect    disp;
210     if(p_vout->b_need_render)
211     {  
212         /* Change display frame */
213         if( p_vout->p_sys->b_must_acquire )
214         {
215             
216             SDL_Flip( p_vout->p_sys->p_display );
217             
218             /* Swap buffers and change write frame */
219             SDL_LockSurface ( p_vout->p_sys->p_display );
220         }
221      }
222      else
223      {
224         
225         /*
226          * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to render 
227          */
228         if( p_vout->p_sys->b_must_acquire )
229         {
230             /* TODO: support for streams other than 4:2:0 */
231             /* create the overlay if necessary */
232             if( !p_vout->p_sys->p_overlay )
233             {
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, 
237                                              SDL_YV12_OVERLAY, 
238                                              p_vout->p_sys->p_display
239                                            );
240                 intf_Msg("[YUV acceleration] : %d,\n",
241                             p_vout->p_sys->p_overlay->hw_overlay); 
242             }
243
244             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
245             /* copy the data into video buffers */
246             /* Y first */
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]);
251             /* then V */
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);
256             /* and U */
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);
261        
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;
266  
267             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
268             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
269         }
270     }
271 }
272
273 /* following functions are local */
274
275 /*****************************************************************************
276  * SDLOpenDisplay: open and initialize SDL device
277  *****************************************************************************
278  * Open and initialize display according to preferences specified in the vout
279  * thread fields.
280  *****************************************************************************/
281 static int SDLOpenDisplay( vout_thread_t *p_vout )
282 {
283     SDL_Rect    clipping_rect;
284     
285     /* Open display 
286      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
287      */
288     if( p_vout->p_sys->b_fullscreen )
289     {
290         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
291             p_vout->i_height, 
292             0, 
293             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN );
294         SDL_ShowCursor( 0 );
295     } else {
296         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
297             p_vout->i_height, 
298             0, 
299             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF );
300         SDL_ShowCursor( 1 );
301     }
302         
303     if( p_vout->p_sys->p_display == NULL )
304     {
305         intf_ErrMsg( "error: can't open DISPLAY default display\n" );
306         return( 1 );
307     }
308     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
309     SDL_EventState(SDL_KEYUP , SDL_IGNORE);     /* ignore keys up */
310
311     /* Check buffers properties */      
312     p_vout->p_sys->b_must_acquire = 1;          /* always acquire */
313     p_vout->p_sys->p_buffer[ 0 ] =
314          p_vout->p_sys->p_display->pixels;
315
316     SDL_Flip(p_vout->p_sys->p_display);
317     p_vout->p_sys->p_buffer[ 1 ] =
318          p_vout->p_sys->p_display->pixels;
319     SDL_Flip(p_vout->p_sys->p_display);
320
321     /* Set graphic context colors */
322
323 /*
324     col_fg.r = col_fg.g = col_fg.b = -1;
325     col_bg.r = col_bg.g = col_bg.b = 0;
326     if( ggiSetGCForeground(p_vout->p_sys->p_display,
327                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
328         ggiSetGCBackground(p_vout->p_sys->p_display,
329                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
330     {
331         intf_ErrMsg("error: can't set colors\n");
332         ggiClose( p_vout->p_sys->p_display );
333         ggiExit();
334         return( 1 );
335     }
336 */
337                 
338     /* Set clipping for text */
339     clipping_rect.x = 0;
340     clipping_rect.y = 0;
341     clipping_rect.w = p_vout->p_sys->p_display->w;
342     clipping_rect.h = p_vout->p_sys->p_display->h;
343     SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
344
345
346         
347     /* Set thread information */
348     p_vout->i_width =           p_vout->p_sys->p_display->w;
349     p_vout->i_height =          p_vout->p_sys->p_display->h;
350
351     p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
352                                p_vout->p_sys->p_display->w ;
353                 
354     p_vout->i_screen_depth =    p_vout->p_sys->p_display->format->BitsPerPixel;
355     p_vout->i_bytes_per_pixel = p_vout->p_sys->p_display->format->BytesPerPixel;
356     p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
357     p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
358     p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
359
360     /* FIXME: palette in 8bpp ?? */
361     /* Set and initialize buffers */
362     vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
363                              p_vout->p_sys->p_buffer[ 1 ] );
364
365     return( 0 );
366 }
367
368 /*****************************************************************************
369  * SDLCloseDisplay: close and reset SDL device
370  *****************************************************************************
371  * This function returns all resources allocated by SDLOpenDisplay and restore
372  * the original state of the device.
373  *****************************************************************************/
374 static void SDLCloseDisplay( vout_thread_t *p_vout )
375 {
376     if( p_vout->p_sys->p_display )
377     {
378         p_vout->p_sys->b_must_acquire = 0;
379         if( p_vout->p_sys->p_overlay )
380         {
381             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
382             p_vout->p_sys->p_overlay = NULL;
383         }
384         SDL_FreeSurface( p_vout->p_sys->p_display );
385         p_vout->p_sys->p_display = NULL;
386     }
387
388     SDL_Quit();
389 }
390