]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
- Hardware YUV overlay (SDL) with real colors ;p It may suck for non
[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\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
89     /* Initialize library */
90     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
91     {
92         intf_ErrMsg( "error: can't initialize SDL library: %s\n",
93                      SDL_GetError() );
94         free( p_vout->p_sys );
95         return( 1 );
96     }
97
98     if( SDLOpenDisplay(p_vout) )
99     {
100         intf_ErrMsg( "error: can't initialize SDL library: %s\n",
101                      SDL_GetError() );
102         free( p_vout->p_sys );
103         return( 1 );
104     }
105
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;
110
111     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
112     {
113         p_vout->p_sys->b_fullscreen = 1;
114     } else {
115         p_vout->p_sys->b_fullscreen = 0;
116     }
117     p_vout->p_sys->b_reopen_display = 1;
118
119     return( 0 );
120 }
121
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 )
128 {
129     /* Acquire first buffer */
130     if( p_vout->p_sys->b_must_acquire )
131     {
132         SDL_LockSurface(p_vout->p_sys->p_display);
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     /* Release buffer */
146     if( p_vout->p_sys->b_must_acquire )
147     {
148         SDL_UnlockSurface ( p_vout->p_sys->p_display );
149     }
150     free( p_vout->p_sys );
151 }
152
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 )
159 {
160     SDLCloseDisplay( p_vout );
161     free( p_vout->p_sys );
162 }
163
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 )
171 {
172     /* If the display has to be reopened we do so */
173     if( p_vout->p_sys->b_reopen_display )
174     {
175         p_vout->p_sys->b_must_acquire = 0;
176         if( p_vout->p_sys->p_display )
177         {
178             if( p_vout->p_sys->p_overlay )
179             {
180                 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
181                 p_vout->p_sys->p_overlay = NULL;
182             }
183             SDL_FreeSurface( p_vout->p_sys->p_display );
184             p_vout->p_sys->p_display = NULL;
185         }
186
187         if( SDLOpenDisplay(p_vout) )
188         {
189           intf_ErrMsg( "error: can't open DISPLAY default display\n" );
190           return( 1 );
191         }
192         p_vout->p_sys->b_reopen_display = 0;
193     }
194     return( 0 );
195 }
196
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 )
204 {
205     SDL_Rect    disp;
206     if(p_vout->b_need_render)
207     {  
208         /* Change display frame */
209         if( p_vout->p_sys->b_must_acquire )
210         {
211             
212             SDL_Flip( p_vout->p_sys->p_display );
213             
214             /* Swap buffers and change write frame */
215             SDL_LockSurface ( p_vout->p_sys->p_display );
216         }
217      }
218      else
219      {
220         
221         /*
222          * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to render 
223          */
224         if( p_vout->p_sys->b_must_acquire )
225         {
226             /* TODO: support for streams other than 4:2:0 */
227             /* create the overlay if necessary */
228             if( !p_vout->p_sys->p_overlay )
229             {
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, 
233                                              SDL_YV12_OVERLAY, 
234                                              p_vout->p_sys->p_display
235                                            );
236                 intf_Msg("[YUV acceleration] : %d\n",
237                             p_vout->p_sys->p_overlay->hw_overlay);
238             }
239
240             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
241             /* copy the data into video buffers */
242             /* Y first */
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]);
247             /* then V */
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);
252             /* and U */
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);
257         
258             disp.x = 0;
259             disp.y = 0;
260             disp.w = p_vout->i_width;
261             disp.h = p_vout->i_height;
262         
263             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
264             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
265         }
266     }
267 }
268
269 /* following functions are local */
270
271 /*****************************************************************************
272  * SDLOpenDisplay: open and initialize SDL device
273  *****************************************************************************
274  * Open and initialize display according to preferences specified in the vout
275  * thread fields.
276  *****************************************************************************/
277 static int SDLOpenDisplay( vout_thread_t *p_vout )
278 {
279     SDL_Rect    clipping_rect;
280     
281     /* Open display 
282      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
283      */
284     if( p_vout->p_sys->b_fullscreen )
285     {
286         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
287             p_vout->i_height, 
288             0, 
289             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN );
290         SDL_ShowCursor( 0 );
291     } else {
292         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
293             p_vout->i_height, 
294             0, 
295             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF  );
296         SDL_ShowCursor( 1 );
297     }
298         
299     if( p_vout->p_sys->p_display == NULL )
300     {
301         intf_ErrMsg( "error: can't open DISPLAY default display\n" );
302         return( 1 );
303     }
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 */
307
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;
312         
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);
317
318     /* Set graphic context colors */
319
320 /*
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)) )
327     {
328         intf_ErrMsg("error: can't set colors\n");
329         ggiClose( p_vout->p_sys->p_display );
330         ggiExit();
331         return( 1 );
332     }
333 */
334                 
335     /* Set clipping for text */
336     clipping_rect.x = 0;
337     clipping_rect.y = 0;
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);
341
342
343         
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;
347
348     p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
349                                 p_vout->p_sys->p_display->w ;
350                 
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;
356
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 ] );
361
362     return( 0 );
363 }
364
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 )
372 {
373     if( p_vout->p_sys->p_display )
374     {
375         p_vout->p_sys->b_must_acquire = 0;
376         if( p_vout->p_sys->p_overlay )
377         {
378             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
379             p_vout->p_sys->p_overlay = NULL;
380         }
381         SDL_FreeSurface( p_vout->p_sys->p_display );
382         p_vout->p_sys->p_display = NULL;
383     }
384
385     SDL_Quit();
386 }
387