]> git.sesse.net Git - vlc/blob - modules/video_output/directfb.c
Move x11-display config to core
[vlc] / modules / video_output / directfb.c
1 /*****************************************************************************
2  * directfb.c: DirectFB video output display method
3  *****************************************************************************
4  * Copyright (C) 2005-2009 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_picture_pool.h>
36
37 #include <directfb.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open (vlc_object_t *);
43 static void Close(vlc_object_t *);
44
45 vlc_module_begin()
46     set_shortname("DirectFB")
47     set_category(CAT_VIDEO)
48     set_subcategory(SUBCAT_VIDEO_VOUT)
49     set_description(N_("DirectFB video output http://www.directfb.org/"))
50     set_capability("vout display", 60)
51     add_shortcut("directfb")
52     set_callbacks(Open, Close)
53 vlc_module_end()
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static picture_t *Get    (vout_display_t *);
59 static void       Display(vout_display_t *, picture_t *);
60 static int        Control(vout_display_t *, int, va_list);
61 static void       Manage (vout_display_t *);
62
63 /* */
64 static int  OpenDisplay (vout_display_t *);
65 static void CloseDisplay(vout_display_t *);
66
67 /* */
68 struct vout_display_sys_t {
69     /* */
70     IDirectFB             *directfb;
71     IDirectFBSurface      *primary;
72     DFBSurfacePixelFormat pixel_format;
73
74     /* */
75     int width;
76     int height;
77
78     /* */
79     picture_pool_t *pool;
80 };
81
82 /* */
83 static int Open(vlc_object_t *object)
84 {
85     vout_display_t *vd = (vout_display_t *)object;
86     vout_display_sys_t *sys;
87
88     /* Allocate structure */
89     vd->sys = sys = malloc(sizeof(*sys));
90     if (!sys)
91         return VLC_ENOMEM;
92
93     sys->directfb = NULL;
94     sys->primary  = NULL;
95     sys->width    = 0;
96     sys->height   = 0;
97     sys->pool     = NULL;
98
99     /* Init DirectFB */
100     if (DirectFBInit(NULL,NULL) != DFB_OK) {
101         msg_Err(vd, "Cannot init DirectFB");
102         free(sys);
103         return VLC_EGENERIC;
104     }
105
106     if (OpenDisplay(vd)) {
107         msg_Err(vd, "Cannot create primary surface");
108         Close(VLC_OBJECT(vd));
109         return VLC_EGENERIC;
110     }
111
112     /* */
113     video_format_t fmt = vd->fmt;
114
115     switch (sys->pixel_format) {
116     case DSPF_RGB332:
117         /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
118         fmt.i_chroma = VLC_CODEC_RGB8;
119         fmt.i_rmask = 0x7 << 5;
120         fmt.i_gmask = 0x7 << 2;
121         fmt.i_bmask = 0x3 << 0;
122         break;
123     case DSPF_RGB16:
124         /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
125         fmt.i_chroma = VLC_CODEC_RGB16;
126         fmt.i_rmask = 0x1f << 11;
127         fmt.i_gmask = 0x3f <<  5;
128         fmt.i_bmask = 0x1f <<  0;
129         break;
130     case DSPF_RGB24:
131         /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
132         fmt.i_chroma = VLC_CODEC_RGB24;
133         fmt.i_rmask = 0xff << 16;
134         fmt.i_gmask = 0xff <<  8;
135         fmt.i_bmask = 0xff <<  0;
136         break;
137     case DSPF_RGB32:
138         /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
139         fmt.i_chroma = VLC_CODEC_RGB32;
140         fmt.i_rmask = 0xff << 16;
141         fmt.i_gmask = 0xff <<  8;
142         fmt.i_bmask = 0xff <<  0;
143         break;
144     default:
145         msg_Err(vd, "unknown screen depth %i", sys->pixel_format);
146         Close(VLC_OBJECT(vd));
147         return VLC_EGENERIC;
148     }
149
150     fmt.i_width  = sys->width;
151     fmt.i_height = sys->height;
152
153     /* */
154     vout_display_info_t info = vd->info;
155     info.has_hide_mouse = true;
156
157     /* */
158     vd->fmt     = fmt;
159     vd->info    = info;
160     vd->get     = Get;
161     vd->prepare = NULL;
162     vd->display = Display;
163     vd->control = Control;
164     vd->manage  = Manage;
165
166     /* */
167     vout_display_SendEventFullscreen(vd, true);
168     vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, true);
169     return VLC_SUCCESS;
170 }
171
172 static void Close(vlc_object_t *object)
173 {
174     vout_display_t *vd = (vout_display_t *)object;
175     vout_display_sys_t *sys = vd->sys;
176
177     if (sys->pool)
178         picture_pool_Delete(sys->pool);
179
180     CloseDisplay(vd);
181     free(sys);
182 }
183
184 /* */
185 static picture_t *Get(vout_display_t *vd)
186 {
187     vout_display_sys_t *sys = vd->sys;
188
189     if (!sys->pool) {
190         sys->pool = picture_pool_NewFromFormat(&vd->fmt, 1);
191         if (!sys->pool)
192             return NULL;
193     }
194     return picture_pool_Get(sys->pool);
195 }
196
197 static void Display(vout_display_t *vd, picture_t *picture)
198 {
199     vout_display_sys_t *sys = vd->sys;
200
201     IDirectFBSurface *primary = sys->primary;
202
203     void *pixels;
204     int  pitch;
205     if (primary->Lock(primary, DSLF_WRITE, &pixels, &pitch) == DFB_OK) {
206         picture_resource_t rsc;
207
208         memset(&rsc, 0, sizeof(rsc));
209         rsc.p[0].p_pixels = pixels;
210         rsc.p[0].i_lines  = sys->height;
211         rsc.p[0].i_pitch  = pitch;
212
213         picture_t *direct = picture_NewFromResource(&vd->fmt, &rsc);
214         if (direct) {
215             picture_Copy(direct, picture);
216             picture_Release(direct);
217         }
218
219         if (primary->Unlock(primary) == DFB_OK)
220             primary->Flip(primary, NULL, 0);
221     }
222     picture_Release(picture);
223 }
224
225 static int Control(vout_display_t *vd, int query, va_list args)
226 {
227     switch (query) {
228     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
229         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
230         if (cfg->display.width  != vd->fmt.i_width ||
231             cfg->display.height != vd->fmt.i_height)
232             return VLC_EGENERIC;
233         return VLC_SUCCESS;
234     }
235     default:
236         msg_Err(vd, "Unsupported query in vout display directfb");
237         return VLC_EGENERIC;
238     }
239 }
240
241 static void Manage (vout_display_t *vd)
242 {
243     VLC_UNUSED(vd);
244 }
245
246 static int OpenDisplay(vout_display_t *vd)
247 {
248     vout_display_sys_t *sys = vd->sys;
249
250     DFBSurfaceDescription dsc;
251     /*dsc.flags = DSDESC_CAPS | DSDESC_HEIGHT | DSDESC_WIDTH;*/
252     dsc.flags = DSDESC_CAPS;
253     dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
254     /*dsc.width = 352;*/
255     /*dsc.height = 240;*/
256
257     IDirectFB *directfb = NULL;
258     if (DirectFBCreate(&directfb) != DFB_OK || !directfb)
259         return VLC_EGENERIC;
260     sys->directfb = directfb;
261
262     IDirectFBSurface *primary = NULL;
263     if (directfb->CreateSurface(directfb, &dsc, &primary) || !primary)
264         return VLC_EGENERIC;
265     sys->primary = primary;
266
267     primary->GetSize(primary, &sys->width, &sys->height);
268     primary->GetPixelFormat(primary, &sys->pixel_format);
269     primary->FillRectangle(primary, 0, 0, sys->width, sys->height);
270     primary->Flip(primary, NULL, 0);
271
272     return VLC_SUCCESS;
273 }
274
275 static void CloseDisplay(vout_display_t *vd)
276 {
277     vout_display_sys_t *sys = vd->sys;
278
279     IDirectFBSurface *primary = sys->primary;
280     if (primary)
281         primary->Release(primary);
282
283     IDirectFB *directfb = sys->directfb;
284     if (directfb)
285         directfb->Release(directfb);
286 }
287