]> git.sesse.net Git - vlc/blob - modules/video_output/directfb.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / video_output / directfb.c
1 /*****************************************************************************
2  * directfb.c: DirectFB video output display method
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Iuri Diniz <iuri@digizap.com.br>
7  *
8  * This code is based in sdl.c and fb.c, thanks for VideoLAN team.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32 #include <directfb.h>
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create    ( vlc_object_t * );
38 static void Destroy   ( vlc_object_t * );
39
40 static int  Init      ( vout_thread_t * );
41 static void End       ( vout_thread_t * );
42 static int  Manage    ( vout_thread_t * );
43 static void Display   ( vout_thread_t *, picture_t * );
44
45 static int  OpenDisplay    ( vout_thread_t * );
46 static void CloseDisplay   ( vout_thread_t * );
47
48 struct vout_sys_t
49 {
50     IDirectFB *p_directfb;
51     IDirectFBSurface *p_primary;
52     DFBSurfacePixelFormat p_pixel_format;
53
54     int i_width;
55     int i_height;
56
57     byte_t* p_pixels;
58 };
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin();
64     set_shortname( "DirectFB" );
65     set_category( CAT_VIDEO );
66     set_subcategory( SUBCAT_VIDEO_VOUT );
67     set_description( _("DirectFB video output http://www.directfb.org/") );
68     set_capability( "video output", 60 );
69     add_shortcut( "directfb" );
70     set_callbacks( Create, Destroy );
71 vlc_module_end();
72
73
74 static int Create( vlc_object_t *p_this )
75 {
76     vout_thread_t *p_vout = (vout_thread_t *)p_this;
77     vout_sys_t *p_sys = NULL;
78     p_vout->pf_init = Init;
79     p_vout->pf_end = End;
80     p_vout->pf_manage = Manage;
81     p_vout->pf_render = NULL;
82     p_vout->pf_display = Display;
83
84     /* Allocate structure */
85     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
86     if( !p_sys )
87     {
88         msg_Err( p_vout, "out of memory" );
89         return VLC_ENOMEM;
90     }
91
92     p_sys->p_directfb = NULL;
93     p_sys->p_primary = NULL;
94     p_sys->p_pixels = NULL;
95     p_sys->i_width = 0;
96     p_sys->i_height = 0;
97
98     /* Init DirectFB */
99     if( DirectFBInit(NULL,NULL) != DFB_OK )
100     {
101         msg_Err(p_vout, "Cannot init DirectFB");
102         return VLC_EGENERIC;
103     }
104
105     if( OpenDisplay( p_vout ) )
106     {
107         msg_Err(p_vout, "Cannot create primary surface");
108         free( p_sys );
109         return VLC_EGENERIC;
110     }
111     return VLC_SUCCESS;
112 }
113
114
115 static int Init( vout_thread_t *p_vout )
116 {
117     vout_sys_t *p_sys = p_vout->p_sys;
118     IDirectFBSurface *p_primary = (IDirectFBSurface *) p_vout->p_sys->p_primary;
119     byte_t* p_pixels = NULL;
120     picture_t *p_pic = NULL;
121     int i_rlength, i_glength, i_blength;
122     int i_roffset, i_goffset, i_boffset;
123     int i_line_pitch;
124     int i_size;
125     int i_index;
126
127     I_OUTPUTPICTURES = 0;
128
129     switch( p_sys->p_pixel_format )
130     {
131         case DSPF_RGB332:
132             /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
133             /* i_pixel_pitch = 1; */
134             i_rlength = 3;
135             i_roffset = 5;
136             i_glength = 3;
137             i_goffset = 2;
138             i_blength = 2;
139             i_boffset = 0;
140             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
141             break;
142
143         case DSPF_RGB16:
144             /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
145             /* i_pixel_pitch = 2; */
146             i_rlength = 5;
147             i_roffset = 11;
148             i_glength = 6;
149             i_goffset = 5;
150             i_blength = 5;
151             i_boffset = 0;
152             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
153             break;
154
155         case DSPF_RGB24:
156             /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
157             /* i_pixel_pitch = 3; */
158             i_rlength = 8;
159             i_roffset = 16;
160             i_glength = 8;
161             i_goffset = 8;
162             i_blength = 8;
163             i_boffset = 0;
164             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
165             break;
166
167         case DSPF_RGB32:
168             /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
169             /* i_pixel_pitch = 4; */
170             i_rlength = 8;
171             i_roffset = 16;
172             i_glength = 8;
173             i_goffset = 8;
174             i_blength = 8;
175             i_boffset = 0;
176             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
177             break;
178
179         default:
180             msg_Err( p_vout, "unknown screen depth %i",
181                      p_sys->p_pixel_format );
182             return VLC_EGENERIC;
183     }
184     /* Set the RGB masks */
185     p_vout->output.i_rmask = ( (1 << i_rlength) - 1 ) << i_roffset;
186     p_vout->output.i_gmask = ( (1 << i_glength) - 1 ) << i_goffset;
187     p_vout->output.i_bmask = ( (1 << i_blength) - 1 ) << i_boffset;
188
189     /* Width and height */
190     p_vout->output.i_width  = p_sys->i_width;
191     p_vout->output.i_height = p_sys->i_height;
192
193     /* The aspect */
194     p_vout->output.i_aspect = (p_sys->i_width * VOUT_ASPECT_FACTOR) /
195                                p_sys->i_height;
196
197     /* Try to initialize 1 buffer */
198     /* Find an empty picture slot */
199     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
200     {
201         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
202         {
203             p_pic = p_vout->p_picture + i_index;
204             break;
205         }
206     }
207
208     /* Allocate the picture */
209     if( !p_pic )
210         return VLC_EGENERIC;
211
212     /* get the pixels */
213     if( p_primary->Lock( p_primary, DSLF_READ, (void **) &p_pixels,
214                          &i_line_pitch) != DFB_OK )
215         return VLC_EGENERIC;
216
217     /* allocate p_pixels */
218     i_size = i_line_pitch * p_sys->i_height;
219     p_sys->p_pixels = malloc( sizeof(byte_t) * i_size );
220     if( p_sys->p_pixels == NULL )
221     {
222         p_primary->Unlock(p_primary);
223         return VLC_ENOMEM;
224     }
225
226     /* copy pixels */
227     memcpy( p_sys->p_pixels,  p_pixels, i_size );
228     if( p_primary->Unlock(p_primary) != DFB_OK )
229     {
230         return VLC_EGENERIC;
231     }
232
233     p_pic->p->p_pixels = p_sys->p_pixels;
234     p_pic->p->i_pixel_pitch = i_line_pitch / p_sys->i_width;
235     p_pic->p->i_lines = p_sys->i_height;
236     p_pic->p->i_visible_lines = p_sys->i_height;
237     p_pic->p->i_pitch = i_line_pitch;
238     p_pic->p->i_visible_pitch = i_line_pitch;
239     p_pic->i_planes = 1;
240     p_pic->i_status = DESTROYED_PICTURE;
241     p_pic->i_type   = DIRECT_PICTURE;
242
243     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
244
245     I_OUTPUTPICTURES++;
246
247     return VLC_SUCCESS;
248 }
249
250 static void End( vout_thread_t *p_vout )
251 {
252     vout_sys_t *p_sys = p_vout->p_sys;
253
254     if( p_sys->p_pixels )
255         free( p_sys->p_pixels );
256 }
257
258 static void Destroy( vlc_object_t *p_this )
259 {
260     vout_thread_t *p_vout = (vout_thread_t *)p_this;
261     vout_sys_t *p_sys = p_vout->p_sys;
262
263     CloseDisplay( p_vout );
264     if( p_sys ) free( p_sys );
265     p_sys = NULL;
266 }
267
268 static int Manage( vout_thread_t *p_vout )
269 {
270     return VLC_SUCCESS;
271 }
272
273 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
274 {
275     vout_sys_t *p_sys = p_vout->p_sys;
276     IDirectFBSurface *p_primary = (IDirectFBSurface *) p_sys->p_primary;
277     byte_t* p_pixels = NULL;
278     int i_size;
279     int i_line_pitch;
280
281     /* get the pixels */
282     if( p_primary->Lock( p_primary, DSLF_WRITE,
283                          (void **) &p_pixels,
284                          &i_line_pitch) == DFB_OK )
285     {
286         i_size = i_line_pitch * p_vout->p_sys->i_height;
287
288         /* copy pixels */
289         memcpy( p_pixels, p_pic->p->p_pixels, i_size);
290         if( p_primary->Unlock(p_primary) == DFB_OK )
291         {
292             p_primary->Flip(p_primary, NULL, 0);
293         }
294     }
295 }
296
297 static int OpenDisplay( vout_thread_t *p_vout )
298 {
299     vout_sys_t *p_sys = p_vout->p_sys;
300     IDirectFB *p_directfb = NULL;
301     IDirectFBSurface *p_primary = NULL;
302     DFBSurfaceDescription dsc;
303
304     /*dsc.flags = DSDESC_CAPS | DSDESC_HEIGHT | DSDESC_WIDTH;*/
305     dsc.flags = DSDESC_CAPS;
306     dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
307
308     /*dsc.width = 352;*/
309     /*dsc.height = 240;*/
310     if( DirectFBCreate( &p_directfb ) != DFB_OK )
311         return VLC_EGENERIC;
312
313     p_sys->p_directfb = p_directfb;
314     if( !p_directfb )
315         return VLC_EGENERIC;
316
317     if( p_directfb->CreateSurface( p_directfb, &dsc, &p_primary ) )
318         return VLC_EGENERIC;
319
320     p_sys->p_primary = p_primary;
321     if( !p_primary )
322         return VLC_EGENERIC;
323
324     p_primary->GetSize( p_primary, &p_sys->i_width,
325                         &p_sys->i_height );
326     p_primary->GetPixelFormat( p_primary, &p_sys->p_pixel_format );
327     p_primary->FillRectangle( p_primary, 0, 0, p_sys->i_width,
328                               p_sys->i_height );
329     p_primary->Flip( p_primary, NULL, 0 );
330
331     return VLC_SUCCESS;
332 }
333
334 static void CloseDisplay( vout_thread_t *p_vout )
335 {
336     vout_sys_t *p_sys = p_vout->p_sys;
337     IDirectFB *p_directfb = p_sys->p_directfb;
338     IDirectFBSurface *p_primary = p_sys->p_primary;
339
340     if( p_primary )
341         p_primary->Release( p_primary );
342
343     if( p_directfb )
344         p_directfb->Release( p_directfb );
345 }