]> git.sesse.net Git - vlc/blob - plugins/fb/vout_fb.c
. nouveaux plugins - ne fonctionnent pas encore tous
[vlc] / plugins / fb / vout_fb.c
1 /*****************************************************************************
2  * vout_fb.c: Linux framebuffer video output display method
3  *****************************************************************************
4  * Copyright (C) 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 <fcntl.h>                                                 /* open() */
33 #include <unistd.h>                                               /* close() */
34 #include <linux/fb.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>                                              /* mmap() */
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42 #include "plugins.h"
43
44 #include "video.h"
45 #include "video_output.h"
46
47 #include "intf_msg.h"
48 #include "main.h"
49
50 /*****************************************************************************
51  * vout_sys_t: video output framebuffer method descriptor
52  *****************************************************************************
53  * This structure is part of the video output thread descriptor.
54  * It describes the FB specific properties of an output thread.
55  *****************************************************************************/
56 typedef struct vout_sys_s
57 {
58     /* System informations */
59     int                         i_fb_dev;       /* framebuffer device handle */
60     struct fb_var_screeninfo    var_info;   /* framebuffer mode informations */
61
62     /* Video memory */
63     byte_t *                    p_video;                      /* base adress */
64     size_t                      i_page_size;                    /* page size */
65
66     struct fb_cmap              fb_cmap;                /* original colormap */
67     unsigned short              *fb_palette;             /* original palette */
68
69 } vout_sys_t;
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 static int     FBOpenDisplay   ( vout_thread_t *p_vout );
75 static void    FBCloseDisplay  ( vout_thread_t *p_vout );
76 static void    FBSetPalette    ( p_vout_thread_t p_vout,
77                                  u16 *red, u16 *green, u16 *blue, u16 *transp );
78
79 /*****************************************************************************
80  * vout_SysCreate: allocates FB video thread output method
81  *****************************************************************************
82  * This function allocates and initializes a FB vout method.
83  *****************************************************************************/
84 int vout_SysCreate( vout_thread_t *p_vout, char *psz_display,
85                     int i_root_window, void *p_data )
86 {
87     /* Allocate structure */
88     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
89     if( p_vout->p_sys == NULL )
90     {
91         intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
92         return( 1 );
93     }
94
95     /* Open and initialize device */
96     if( FBOpenDisplay( p_vout ) )
97     {
98         intf_ErrMsg("vout error: can't open display\n");
99         free( p_vout->p_sys );
100         return( 1 );
101     }
102
103     return( 0 );
104 }
105
106 /*****************************************************************************
107  * vout_SysInit: initialize framebuffer video thread output method
108  *****************************************************************************/
109 int vout_SysInit( vout_thread_t *p_vout )
110 {
111     p_vout->p_set_palette       = FBSetPalette;
112     return( 0 );
113 }
114
115 /*****************************************************************************
116  * vout_SysEnd: terminate FB video thread output method
117  *****************************************************************************/
118 void vout_SysEnd( vout_thread_t *p_vout )
119 {
120     ;
121 }
122
123 /*****************************************************************************
124  * vout_SysDestroy: destroy FB video thread output method
125  *****************************************************************************
126  * Terminate an output method created by vout_CreateOutputMethod
127  *****************************************************************************/
128 void vout_SysDestroy( vout_thread_t *p_vout )
129 {
130     FBCloseDisplay( p_vout );
131     free( p_vout->p_sys );
132 }
133
134 /*****************************************************************************
135  * vout_SysManage: handle FB events
136  *****************************************************************************
137  * This function should be called regularly by video output thread. It manages
138  * console events. It returns a non null value on error.
139  *****************************************************************************/
140 int vout_SysManage( vout_thread_t *p_vout )
141 {
142     /*
143      * Size change
144      */
145     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
146     {
147         intf_DbgMsg("resizing window\n");
148         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
149
150         /* Destroy XImages to change their size */
151         vout_SysEnd( p_vout );
152
153         /* Recreate XImages. If SysInit failed, the thread can't go on. */
154         if( vout_SysInit( p_vout ) )
155         {
156             intf_ErrMsg("error: can't resize display\n");
157             return( 1 );
158         }
159
160 #if 1
161         /* Tell the video output thread that it will need to rebuild YUV
162          * tables. This is needed since conversion buffer size may have changed */
163         p_vout->i_changes |= VOUT_YUV_CHANGE;
164         intf_Msg("Video display resized (%dx%d)\n", p_vout->i_width, p_vout->i_height);
165 #endif
166     }
167
168     return 0;
169 }
170
171 /*****************************************************************************
172  * vout_SysDisplay: displays previously rendered output
173  *****************************************************************************
174  * This function send the currently rendered image to FB image, waits until
175  * it is displayed and switch the two rendering buffers, preparing next frame.
176  *****************************************************************************/
177 void vout_SysDisplay( vout_thread_t *p_vout )
178 {
179     /* swap the two Y offsets */
180     p_vout->p_sys->var_info.yoffset = p_vout->i_buffer_index ? p_vout->p_sys->var_info.yres : 0;
181     /* the X offset should be 0, but who knows ...
182      * some other app might have played with the framebuffer */
183     p_vout->p_sys->var_info.xoffset = 0;
184
185     //ioctl( p_vout->p_sys->i_fb_dev, FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
186     ioctl( p_vout->p_sys->i_fb_dev, FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
187 }
188
189 /* following functions are local */
190
191 /*****************************************************************************
192  * FBOpenDisplay: open and initialize framebuffer device
193  *****************************************************************************
194  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
195  * display video, providing the card is configured and the mode ok. It is
196  * not portable, and is not supposed to work with many cards. Use at your
197  * own risk !
198  *****************************************************************************/
199
200 static int FBOpenDisplay( vout_thread_t *p_vout )
201 {
202     char *psz_device;                             /* framebuffer device path */
203     struct fb_fix_screeninfo    fix_info;     /* framebuffer fix information */
204
205     /* Open framebuffer device */
206     psz_device = main_GetPszVariable( VOUT_FB_DEV_VAR, VOUT_FB_DEV_DEFAULT );
207     p_vout->p_sys->i_fb_dev = open( psz_device, O_RDWR);
208     if( p_vout->p_sys->i_fb_dev == -1 )
209     {
210         intf_ErrMsg("vout error: can't open %s (%s)\n", psz_device, strerror(errno) );
211         return( 1 );
212     }
213
214     /* Get framebuffer device informations */
215     if( ioctl( p_vout->p_sys->i_fb_dev, FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
216     {
217         intf_ErrMsg( "vout error: can't get framebuffer informations (%s)\n", strerror(errno) );
218         close( p_vout->p_sys->i_fb_dev );
219         return( 1 );
220     }
221
222     /* Framebuffer must have some basic properties to be usable */
223     /* XXX?? */
224
225     /* Set some attributes */
226     p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
227     p_vout->p_sys->var_info.xoffset =  0;
228     p_vout->p_sys->var_info.yoffset =  0;
229     intf_ErrMsg( "vout: ypanstep is %i\n", fix_info.ypanstep );
230     /* XXX?? ask sam p_vout->p_sys->mode_info.sync = FB_SYNC_VERT_HIGH_ACT; */
231
232     if( ioctl( p_vout->p_sys->i_fb_dev, FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
233     {
234         intf_ErrMsg("vout error: can't set framebuffer informations (%s)\n", strerror(errno) );
235         close( p_vout->p_sys->i_fb_dev );
236         return( 1 );
237     }
238
239     /* Get some informations again, in the definitive configuration */
240     if( ioctl( p_vout->p_sys->i_fb_dev, FBIOGET_FSCREENINFO, &fix_info ) ||
241         ioctl( p_vout->p_sys->i_fb_dev, FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
242     {
243         intf_ErrMsg("vout error: can't get framebuffer informations (%s)\n", strerror(errno) );
244         /* FIXME: restore fb config ?? */
245         close( p_vout->p_sys->i_fb_dev );
246         return( 1 );
247     }
248
249     /* FIXME: if the image is full-size, it gets cropped on the left
250      * because of the xres / xres_virtual slight difference */
251     intf_Msg( "%ix%i (virtual %ix%i)\n", p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres, p_vout->p_sys->var_info.xres_virtual, p_vout->p_sys->var_info.yres_virtual );
252     p_vout->i_width =                   p_vout->p_sys->var_info.xres_virtual ? p_vout->p_sys->var_info.xres_virtual : p_vout->p_sys->var_info.xres;
253     p_vout->i_height =                  p_vout->p_sys->var_info.yres;
254     p_vout->i_screen_depth =            p_vout->p_sys->var_info.bits_per_pixel;
255     switch( p_vout->i_screen_depth )
256     {
257     case 8:                                                         /* 8 bpp */
258         p_vout->p_sys->fb_palette = malloc( 8 * 256 * sizeof(unsigned short) );
259         p_vout->p_sys->fb_cmap.start = 0;
260         p_vout->p_sys->fb_cmap.len = 256;
261         p_vout->p_sys->fb_cmap.red = p_vout->p_sys->fb_palette;
262         p_vout->p_sys->fb_cmap.green = p_vout->p_sys->fb_palette + 256 * sizeof(unsigned short);
263         p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->fb_palette + 2 * 256 * sizeof(unsigned short);
264         p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->fb_palette + 3 * 256 * sizeof(unsigned short);
265
266         /* saves the colormap */
267         ioctl( p_vout->p_sys->i_fb_dev, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
268
269         p_vout->i_bytes_per_pixel = 1;
270         p_vout->i_bytes_per_line = p_vout->i_width;
271         break;
272
273     case 15:                      /* 15 bpp (16bpp with a missing green bit) */
274     case 16:                                        /* 16 bpp (65536 colors) */
275         p_vout->i_bytes_per_pixel = 2;
276         p_vout->i_bytes_per_line = p_vout->i_width * 2;
277         break;
278
279     case 24:                                  /* 24 bpp (millions of colors) */
280         p_vout->i_bytes_per_pixel = 3;
281         p_vout->i_bytes_per_line = p_vout->i_width * 3;
282         break;
283
284     case 32:                                  /* 32 bpp (millions of colors) */
285         p_vout->i_bytes_per_pixel = 4;
286         p_vout->i_bytes_per_line = p_vout->i_width * 4;
287         break;
288
289     default:                                     /* unsupported screen depth */
290         intf_ErrMsg( "vout error: screen depth %d is not supported\n",
291                      p_vout->i_screen_depth);
292         return( 1 );
293         break;
294     }
295
296     switch( p_vout->i_screen_depth )
297     {
298     case 15:
299     case 16:
300     case 24:
301     case 32:
302         p_vout->i_red_mask =    ( (1 << p_vout->p_sys->var_info.red.length) - 1 )
303                                     << p_vout->p_sys->var_info.red.offset;
304         p_vout->i_green_mask =    ( (1 << p_vout->p_sys->var_info.green.length) - 1 )
305                                     << p_vout->p_sys->var_info.green.offset;
306         p_vout->i_blue_mask =    ( (1 << p_vout->p_sys->var_info.blue.length) - 1 )
307                                     << p_vout->p_sys->var_info.blue.offset;
308     }
309
310     p_vout->p_sys->i_page_size = p_vout->i_width *
311                 p_vout->i_height * p_vout->i_bytes_per_pixel;
312
313     /* Map two framebuffers a the very beginning of the fb */
314     p_vout->p_sys->p_video = mmap(0, p_vout->p_sys->i_page_size * 2,
315                           PROT_READ | PROT_WRITE, MAP_SHARED,
316                           p_vout->p_sys->i_fb_dev, 0 );
317     if( (int)p_vout->p_sys->p_video == -1 ) /* XXX?? according to man, it is -1. What about NULL ? */
318     {
319         intf_ErrMsg("vout error: can't map video memory (%s)\n", strerror(errno) );
320         /* FIXME: restore fb config ?? */
321         close( p_vout->p_sys->i_fb_dev );
322         return( 1 );
323     }
324
325     /* Set and initialize buffers */
326     vout_SetBuffers( p_vout, p_vout->p_sys->p_video,
327                      p_vout->p_sys->p_video + p_vout->p_sys->i_page_size );
328     intf_DbgMsg("framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d\n",
329                 fix_info.type, fix_info.visual, fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
330     return( 0 );
331 }
332
333 /*****************************************************************************
334  * FBCloseDisplay: close and reset framebuffer device
335  *****************************************************************************
336  * Returns all resources allocated by FBOpenDisplay and restore the original
337  * state of the device.
338  *****************************************************************************/
339 static void FBCloseDisplay( vout_thread_t *p_vout )
340 {
341     /* Restore palette */
342     if( p_vout->i_screen_depth == 8 );
343     {
344         ioctl( p_vout->p_sys->i_fb_dev, FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
345         free( p_vout->p_sys->fb_palette );
346     }
347
348     /* Destroy window and close display */
349     close( p_vout->p_sys->i_fb_dev );
350 }
351
352 /*****************************************************************************
353  * FBSetPalette: sets an 8 bpp palette
354  *****************************************************************************
355  * This function sets the palette given as an argument. It does not return
356  * anything, but could later send information on which colors it was unable
357  * to set.
358  *****************************************************************************/
359 static void    FBSetPalette   ( p_vout_thread_t p_vout,
360                                 u16 *red, u16 *green, u16 *blue, u16 *transp )
361 {
362     struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
363     ioctl( p_vout->p_sys->i_fb_dev, FBIOPUTCMAP, &cmap );
364 }
365