]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
- sdl resising rulze, still buggy
[vlc] / 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", 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",
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",
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" );
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 == NULL )
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,",
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 | SDL_RESIZABLE );
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" );
306         return( 1 );
307     }
308     intf_DbgMsg( "sdl display size : %dx%d - pitch : %d",
309                  p_vout->p_sys->p_display->w,
310                  p_vout->p_sys->p_display->h,
311                  p_vout->p_sys->p_display->pitch);
312
313     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
314     SDL_EventState(SDL_KEYUP , SDL_IGNORE);     /* ignore keys up */
315
316     /* Check buffers properties */      
317     p_vout->p_sys->b_must_acquire = 1;          /* always acquire */
318     p_vout->p_sys->p_buffer[ 0 ] =
319          p_vout->p_sys->p_display->pixels;
320
321     SDL_Flip(p_vout->p_sys->p_display);
322     p_vout->p_sys->p_buffer[ 1 ] =
323          p_vout->p_sys->p_display->pixels;
324     SDL_Flip(p_vout->p_sys->p_display);
325
326     /* Set graphic context colors */
327
328 /*
329     col_fg.r = col_fg.g = col_fg.b = -1;
330     col_bg.r = col_bg.g = col_bg.b = 0;
331     if( ggiSetGCForeground(p_vout->p_sys->p_display,
332                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
333         ggiSetGCBackground(p_vout->p_sys->p_display,
334                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
335     {
336         intf_ErrMsg("error: can't set colors");
337         ggiClose( p_vout->p_sys->p_display );
338         ggiExit();
339         return( 1 );
340     }
341 */
342                 
343     /* Set clipping for text */
344     clipping_rect.x = 0;
345     clipping_rect.y = 0;
346     clipping_rect.w = p_vout->p_sys->p_display->w;
347     clipping_rect.h = p_vout->p_sys->p_display->h;
348     SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
349
350
351         
352     /* Set thread information */
353     p_vout->i_width =           p_vout->p_sys->p_display->w;
354     p_vout->i_height =          p_vout->p_sys->p_display->h;
355
356     p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
357                                p_vout->p_sys->p_display->w ;
358                 
359     p_vout->i_screen_depth =    p_vout->p_sys->p_display->format->BitsPerPixel;
360     p_vout->i_bytes_per_pixel = p_vout->p_sys->p_display->format->BytesPerPixel;
361     p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
362     p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
363     p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
364
365     /* FIXME: palette in 8bpp ?? */
366     /* Set and initialize buffers */
367     vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
368                              p_vout->p_sys->p_buffer[ 1 ] );
369
370     return( 0 );
371 }
372
373 /*****************************************************************************
374  * SDLCloseDisplay: close and reset SDL device
375  *****************************************************************************
376  * This function returns all resources allocated by SDLOpenDisplay and restore
377  * the original state of the device.
378  *****************************************************************************/
379 static void SDLCloseDisplay( vout_thread_t *p_vout )
380 {
381     if( p_vout->p_sys->p_display )
382     {
383         p_vout->p_sys->b_must_acquire = 0;
384         if( p_vout->p_sys->p_overlay )
385         {
386             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
387             p_vout->p_sys->p_overlay = NULL;
388         }
389         SDL_FreeSurface( p_vout->p_sys->p_display );
390         p_vout->p_sys->p_display = NULL;
391     }
392
393     SDL_Quit();
394 }
395