aa9c0ad54b30dafaf7159c55ee797234b21e78f5
[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     int i_width;
54     int i_height;
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     Uint8   *   p_buffer[2];
60                                                      /* Buffers informations */
61 }   vout_sys_t;
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int     SDLOpenDisplay   ( vout_thread_t *p_vout );
67 static void    SDLCloseDisplay  ( vout_thread_t *p_vout );
68
69 /*****************************************************************************
70  * vout_SDLCreate: allocate SDL video thread output method
71  *****************************************************************************
72  * This function allocate and initialize a SDL vout method. It uses some of the
73  * vout properties to choose the correct mode, and change them according to the
74  * mode actually used.
75  *****************************************************************************/
76 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
77                     int i_root_window, void *p_data )
78 {
79     /* Allocate structure */
80     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
81     if( p_vout->p_sys == NULL )
82     {
83         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
84         return( 1 );
85     }
86
87     p_vout->p_sys->p_display = NULL;
88     p_vout->p_sys->p_overlay = NULL;
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->i_width = VOUT_WIDTH_DEFAULT;
114     p_vout->p_sys->i_height = VOUT_HEIGHT_DEFAULT;
115
116     p_vout->p_sys->b_reopen_display = 1;
117
118     return( 0 );
119 }
120
121 /*****************************************************************************
122  * vout_SDLInit: initialize SDL video thread output method
123  *****************************************************************************
124  * This function initialize the SDL display device.
125  *****************************************************************************/
126 int vout_SDLInit( vout_thread_t *p_vout )
127 {
128     if( SDLOpenDisplay(p_vout) )
129     {
130       intf_ErrMsg( "error: can't initialize SDL library: %s",
131                    SDL_GetError() );
132       return( 1 );
133     }
134
135     return( 0 );
136 }
137
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 )
144 {
145     SDLCloseDisplay( p_vout );
146     SDL_Quit();
147 }
148
149 /*****************************************************************************
150  * vout_SDLDestroy: destroy Sys video thread output method
151  *****************************************************************************
152  * Terminate an output method created by vout_SDLCreate
153  *****************************************************************************/
154 void vout_SDLDestroy( vout_thread_t *p_vout )
155 {
156     free( p_vout->p_sys );
157 }
158
159 /*****************************************************************************
160  * vout_SDLManage: handle Sys events
161  *****************************************************************************
162  * This function should be called regularly by video output thread. It returns
163  * a non null value if an error occured.
164  *****************************************************************************/
165 int vout_SDLManage( vout_thread_t *p_vout )
166 {
167     /* If the display has to be reopened we do so */
168     if( p_vout->p_sys->b_reopen_display )
169     {
170         SDLCloseDisplay(p_vout);
171
172         if( SDLOpenDisplay(p_vout) )
173         {
174             intf_ErrMsg( "error: can't open DISPLAY default display" );
175             return( 1 );
176         }
177     }
178     return( 0 );
179 }
180
181 /*****************************************************************************
182  * vout_SDLDisplay: displays previously rendered output
183  *****************************************************************************
184  * This function send the currently rendered image to the display, wait until
185  * it is displayed and switch the two rendering buffer, preparing next frame.
186  *****************************************************************************/
187 void vout_SDLDisplay( vout_thread_t *p_vout )
188 {
189     SDL_Rect    disp;
190     if(p_vout->p_sys->p_display && !p_vout->p_sys->b_reopen_display)
191     {
192         if(p_vout->b_need_render)
193         {  
194             /* Change display frame */
195             SDL_Flip( p_vout->p_sys->p_display );
196         }
197         else
198         {
199         
200             /*
201              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
202              * render 
203              */
204             /* TODO: support for streams other than 4:2:0 */
205             /* create the overlay if necessary */
206             if( p_vout->p_sys->p_overlay == NULL )
207             {
208                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
209                                              p_vout->p_rendered_pic->i_width, 
210                                              p_vout->p_rendered_pic->i_height,
211                                              SDL_YV12_OVERLAY, 
212                                              p_vout->p_sys->p_display
213                                            );
214                 intf_Msg("[YUV acceleration] : %d,",
215                             p_vout->p_sys->p_overlay->hw_overlay); 
216             }
217
218             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
219             /* copy the data into video buffers */
220             /* Y first */
221             memcpy(p_vout->p_sys->p_overlay->pixels[0],
222                    p_vout->p_rendered_pic->p_y,
223                    p_vout->p_sys->p_overlay->h *
224                    p_vout->p_sys->p_overlay->pitches[0]);
225             /* then V */
226             memcpy(p_vout->p_sys->p_overlay->pixels[1],
227                    p_vout->p_rendered_pic->p_v,
228                    p_vout->p_sys->p_overlay->h *
229                    p_vout->p_sys->p_overlay->pitches[1] / 2);
230             /* and U */
231             memcpy(p_vout->p_sys->p_overlay->pixels[2],
232                    p_vout->p_rendered_pic->p_u,
233                    p_vout->p_sys->p_overlay->h *
234                    p_vout->p_sys->p_overlay->pitches[2] / 2);
235
236             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
237             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
238             disp.x = (p_vout->i_width - disp.w)/2;
239             disp.y = (p_vout->i_height - disp.h)/2;
240
241             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
242             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
243         }
244     }
245 }
246
247 /* following functions are local */
248
249 /*****************************************************************************
250  * SDLOpenDisplay: open and initialize SDL device
251  *****************************************************************************
252  * Open and initialize display according to preferences specified in the vout
253  * thread fields.
254  *****************************************************************************/
255 static int SDLOpenDisplay( vout_thread_t *p_vout )
256 {
257     SDL_Rect    clipping_rect;
258     Uint32      flags;
259     int bpp;
260     /* Open display 
261      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
262      */
263
264     /* init flags and cursor */
265     flags = SDL_ANYFORMAT;
266
267     if( p_vout->p_sys->b_fullscreen )
268         flags |= SDL_FULLSCREEN;
269     else
270         flags |= SDL_RESIZABLE;
271
272     if( p_vout->b_need_render )
273         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
274     else
275         flags |= SDL_SWSURFACE; /* save video memory */
276
277     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
278                           p_vout->p_sys->i_height,
279                           p_vout->i_screen_depth, flags);
280
281     if(bpp == 0)
282     {
283         intf_ErrMsg( "error: can't open DISPLAY default display" );
284         return( 1 );
285     }
286
287     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
288                                                 p_vout->p_sys->i_height,
289                                                 bpp, flags);
290
291     if( p_vout->p_sys->p_display == NULL )
292     {
293         intf_ErrMsg( "error: can't open DISPLAY default display" );
294         return( 1 );
295     }
296
297     SDL_LockSurface(p_vout->p_sys->p_display);
298
299     if( p_vout->p_sys->b_fullscreen )
300         SDL_ShowCursor( 0 );
301     else
302         SDL_ShowCursor( 1 );
303
304     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
305     SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
306
307     if( p_vout->b_need_render )
308     {
309         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
310         SDL_Flip(p_vout->p_sys->p_display);
311         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
312         SDL_Flip(p_vout->p_sys->p_display);
313
314         /* Set clipping for text */
315         clipping_rect.x = 0;
316         clipping_rect.y = 0;
317         clipping_rect.w = p_vout->p_sys->p_display->w;
318         clipping_rect.h = p_vout->p_sys->p_display->h;
319         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
320
321         /* Set thread information */
322         p_vout->i_width =           p_vout->p_sys->p_display->w;
323         p_vout->i_height =          p_vout->p_sys->p_display->h;
324         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
325
326         p_vout->i_screen_depth =
327             p_vout->p_sys->p_display->format->BitsPerPixel;
328         p_vout->i_bytes_per_pixel =
329             p_vout->p_sys->p_display->format->BytesPerPixel;
330
331         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
332         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
333         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
334
335         /* FIXME: palette in 8bpp ?? */
336         /* Set and initialize buffers */
337         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
338                                  p_vout->p_sys->p_buffer[ 1 ] );
339     }
340     else
341     {
342         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
343         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
344
345         /* Set thread information */
346         p_vout->i_width =           p_vout->p_sys->p_display->w;
347         p_vout->i_height =          p_vout->p_sys->p_display->h;
348         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
349
350         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
351                                  p_vout->p_sys->p_buffer[ 1 ] );
352     }
353
354     p_vout->i_changes |= VOUT_YUV_CHANGE;
355
356     p_vout->p_sys->b_reopen_display = 0;
357
358     return( 0 );
359 }
360
361 /*****************************************************************************
362  * SDLCloseDisplay: close and reset SDL device
363  *****************************************************************************
364  * This function returns all resources allocated by SDLOpenDisplay and restore
365  * the original state of the device.
366  *****************************************************************************/
367 static void SDLCloseDisplay( vout_thread_t *p_vout )
368 {
369     if( p_vout->p_sys->p_display != NULL )
370     {
371         SDL_UnlockSurface ( p_vout->p_sys->p_display );
372         if( p_vout->p_sys->p_overlay != NULL )
373         {            
374             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
375             p_vout->p_sys->p_overlay = NULL;
376         }
377         SDL_FreeSurface( p_vout->p_sys->p_display );
378         p_vout->p_sys->p_display = NULL;
379     }
380 }
381