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