]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/xcb.c
XCB: support for MIT-SHM
[vlc] / modules / video_output / xcb / xcb.c
1 /**
2  * @file xcb.c
3  * @brief X C Bindings video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2.0
11  * of the License, or (at your option) any later version.
12  *
13  * This library 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 Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <sys/types.h>
31 #include <sys/shm.h>
32
33 #include <xcb/xcb.h>
34 #include <xcb/shm.h>
35
36 #include <xcb/xcb_aux.h>
37 #include <xcb/xcb_image.h>
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_vout.h>
42 #include <vlc_window.h>
43
44 #define DISPLAY_TEXT N_("X11 display")
45 #define DISPLAY_LONGTEXT N_( \
46     "X11 hardware display to use. By default VLC will " \
47     "use the value of the DISPLAY environment variable.")
48
49 #define SHM_TEXT N_("Use shared memory")
50 #define SHM_LONGTEXT N_( \
51     "Use shared memory to communicate between VLC and the X server.")
52
53 static int  Open (vlc_object_t *);
54 static void Close (vlc_object_t *);
55
56 /*
57  * Module descriptor
58  */
59 vlc_module_begin ()
60     set_shortname (_("XCB"))
61     set_description (_("(Experimental) XCB video output"))
62     set_category (CAT_VIDEO)
63     set_subcategory (SUBCAT_VIDEO_VOUT)
64     set_capability ("video output", 0)
65     set_callbacks (Open, Close)
66
67     add_string ("x11-display", NULL, NULL,
68                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
69     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
70 vlc_module_end ()
71
72 struct vout_sys_t
73 {
74     xcb_connection_t *conn;
75     xcb_screen_t *screen;
76     vout_window_t *embed; /* VLC window (when windowed) */
77
78     xcb_visualid_t vid;
79     xcb_window_t parent; /* parent X window */
80     xcb_window_t window; /* drawable X window */
81     xcb_gcontext_t gc; /* context to put images */
82     bool shm; /* whether to use MIT-SHM */
83     uint8_t bpp; /* bits per pixel */
84 };
85
86 static int Init (vout_thread_t *);
87 static void Deinit (vout_thread_t *);
88 static void Display (vout_thread_t *, picture_t *);
89 static int Manage (vout_thread_t *);
90
91 static int CheckError (vout_thread_t *vout, const char *str,
92                        xcb_void_cookie_t ck)
93 {
94     xcb_generic_error_t *err;
95
96     err = xcb_request_check (vout->p_sys->conn, ck);
97     if (err)
98     {
99         msg_Err (vout, "%s: X11 error %d", str, err->error_code);
100         return VLC_EGENERIC;
101     }
102     return VLC_SUCCESS;
103 }
104
105 #define p_vout vout
106
107 /**
108  * Probe the X server.
109  */
110 static int Open (vlc_object_t *obj)
111 {
112     vout_thread_t *vout = (vout_thread_t *)obj;
113     vout_sys_t *p_sys = malloc (sizeof (*p_sys));
114     if (p_sys == NULL)
115         return VLC_ENOMEM;
116
117     vout->p_sys = p_sys;
118     p_sys->conn = NULL;
119 #ifndef NDEBUG
120     p_sys->embed = NULL;
121 #endif
122
123     /* Connect to X */
124     char *display = var_CreateGetNonEmptyString (vout, "x11-display");
125     int snum;
126     p_sys->conn = xcb_connect (display, &snum);
127     if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
128     {
129         msg_Err (vout, "cannot connect to X server %s",
130                  display ? display : "");
131         goto error;
132     }
133
134     /* Get the preferred screen */
135     xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
136     p_sys->screen = scr;
137     assert (p_sys->screen);
138     msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
139
140     if (strchr ("\x08\x0f\x10\x18\x20", scr->root_depth) == NULL)
141     {
142         msg_Err (vout, "unsupported %"PRIu8"-bits color depth",
143                  scr->root_depth);
144         goto error;
145     }
146
147     /* Determine the visual (color depth and palette) */
148     xcb_visualtype_t *vt = NULL;
149     if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
150                                             scr->root_depth)) != NULL)
151         msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
152     else
153     if ((vt = xcb_aux_find_visual_by_attrs (scr,XCB_VISUAL_CLASS_STATIC_COLOR,
154                                             scr->root_depth)) != NULL)
155         msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
156     else
157     {
158         vt = xcb_aux_get_visualtype (p_sys->conn, snum, scr->root_visual);
159         assert (vt);
160         msg_Err (vout, "unsupported visual class %"PRIu8, vt->_class);
161         goto error;
162     }
163     p_sys->vid = vt->visual_id;
164
165     p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
166     if (p_sys->shm)
167     {
168         xcb_shm_query_version_cookie_t ck;
169         xcb_shm_query_version_reply_t *r;
170         xcb_generic_error_t *err;
171
172         ck = xcb_shm_query_version (p_sys->conn);
173         r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
174         if (!r)
175         {
176             msg_Err (vout, "shared memory (MIT-SHM) not available");
177             msg_Warn (vout, "display will be slow");
178             p_sys->shm = false;
179         }
180     }
181
182     vout->pf_init = Init;
183     vout->pf_end = Deinit;
184     vout->pf_display = Display;
185     vout->pf_manage = Manage;
186     return VLC_SUCCESS;
187
188 error:
189     Close (obj);
190     return VLC_EGENERIC;
191 }
192
193
194 /**
195  * Disconnect from the X server.
196  */
197 static void Close (vlc_object_t *obj)
198 {
199     vout_thread_t *vout = (vout_thread_t *)obj;
200     vout_sys_t *p_sys = vout->p_sys;
201
202     assert (p_sys->embed == NULL);
203     if (p_sys->conn)
204         xcb_disconnect (p_sys->conn);
205     free (p_sys);
206 }
207
208 struct picture_sys_t
209 {
210     xcb_connection_t *conn;
211     xcb_image_t *image;
212     xcb_shm_seg_t segment;
213 };
214
215 static void PictureRelease (picture_t *pic);
216 static void PictureShmRelease (picture_t *pic);
217 #define SHM_ERR ((void *)(intptr_t)(-1))
218
219 static int PictureInit (vout_thread_t *vout, picture_t *pic)
220 {
221     vout_sys_t *p_sys = vout->p_sys;
222     picture_sys_t *priv = malloc (sizeof (*p_sys));
223
224     if (priv == NULL)
225         return VLC_ENOMEM;
226
227     assert (pic->i_status == FREE_PICTURE);
228     vout_InitPicture (vout, pic, vout->output.i_chroma,
229                       vout->output.i_width, vout->output.i_height,
230                       vout->output.i_aspect);
231
232     void *shm = SHM_ERR;
233     const size_t size = pic->p->i_pitch * pic->p->i_lines;
234
235     if (p_sys->shm)
236     {   /* Allocate shared memory segment */
237         int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
238
239         if (id == -1)
240         {
241             msg_Err (vout, "shared memory allocation error: %m");
242             goto error;
243         }
244
245         /* Attach the segment to VLC */
246         shm = shmat (id, NULL, 0 /* read/write */);
247         if (shm == SHM_ERR)
248         {
249             msg_Err (vout, "shared memory attachment error: %m");
250             shmctl (id, IPC_RMID, 0);
251             goto error;
252         }
253
254         /* Attach the segment to X */
255         xcb_void_cookie_t ck;
256
257         priv->segment = xcb_generate_id (p_sys->conn);
258         ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
259         shmctl (id, IPC_RMID, 0);
260
261         if (CheckError (vout, "shared memory server-side error", ck))
262         {
263             msg_Info (vout, "using buggy X (remote) server? SSH?");
264             shmdt (shm);
265             shm = SHM_ERR;
266         }
267     }
268
269     const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
270     /* FIXME: anyway to getthing more intuitive than that?? */
271
272     /* NOTE: 32-bits scanline_pad assumed, FIXME? (see xdpyinfo) */
273     xcb_image_t *img;
274     img = xcb_image_create (real_width, pic->p->i_lines,
275                             XCB_IMAGE_FORMAT_Z_PIXMAP, 32,
276                             p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
277 #ifdef WORDS_BIGENDIAN
278                             XCB_IMAGE_ORDER_MSB_FIRST,
279 #else
280                             XCB_IMAGE_ORDER_LSB_FIRST,
281 #endif
282                             XCB_IMAGE_ORDER_MSB_FIRST,
283                             NULL,
284                             (shm != SHM_ERR) ? size : 0,
285                             (shm != SHM_ERR) ? shm : NULL);
286
287     if (img == NULL)
288     {
289         if (shm != SHM_ERR)
290             xcb_shm_detach (p_sys->conn, priv->segment);
291         goto error;
292     }
293     if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
294     {
295         msg_Err (vout, "incompatible X server image format");
296         xcb_image_destroy (img);
297         goto error;
298     }
299
300     priv->conn = p_sys->conn;
301     priv->image = img;
302     pic->p_sys = priv;
303     pic->p->p_pixels = img->data;
304     pic->pf_release = (shm != SHM_ERR) ? PictureShmRelease
305                                        : PictureRelease;
306     pic->i_status = DESTROYED_PICTURE;
307     pic->i_type = DIRECT_PICTURE;
308     return VLC_SUCCESS;
309
310 error:
311     if (shm != SHM_ERR)
312         shmdt (shm);
313     free (p_sys);
314     return VLC_EGENERIC;
315 }
316
317
318 /**
319  * Release picture private data
320  */
321 static void PictureRelease (picture_t *pic)
322 {
323     struct picture_sys_t *p_sys = pic->p_sys;
324
325     xcb_image_destroy (p_sys->image);
326     free (p_sys);
327 }
328
329 /**
330  * Release shared memory picture private data
331  */
332 static void PictureShmRelease (picture_t *pic)
333 {
334     struct picture_sys_t *p_sys = pic->p_sys;
335
336     xcb_shm_detach (p_sys->conn, p_sys->segment);
337     shmdt (p_sys->image->data);
338     PictureRelease (pic);
339 }
340
341 /**
342  * Allocate drawable window and picture buffers.
343  */
344 static int Init (vout_thread_t *vout)
345 {
346     vout_sys_t *p_sys = vout->p_sys;
347     const xcb_screen_t *screen = p_sys->screen;
348     unsigned x, y, width, height;
349
350     /* Determine parent window */
351     if (vout->b_fullscreen)
352     {
353         p_sys->embed = NULL;
354         p_sys->parent = screen->root;
355         width = screen->width_in_pixels;
356         height = screen->height_in_pixels;
357     }
358     else
359     {
360         p_sys->embed = vout_RequestWindow (vout, &(int){ 0 }, &(int){ 0 },
361                                             &width, &height);
362         if (p_sys->embed == NULL)
363         {
364             msg_Err (vout, "cannot get window");
365             return VLC_EGENERIC;
366         }
367         p_sys->parent = (intptr_t)p_sys->embed->handle;
368     }
369
370     /* Determine our input format */
371     p_sys->bpp = screen->root_depth;
372     switch (screen->root_depth)
373     {
374         case 24:
375             p_sys->bpp = 32;
376         case 32: /* FIXME: untested */
377             vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
378             break;
379
380         case 16:
381             vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
382             break;
383
384         case 15:
385             p_sys->bpp = 16;
386             vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
387             break;
388
389         case 8: /* FIXME: VLC cannot convert */
390             vout->output.i_chroma = VLC_FOURCC ('R', 'G', 'B', '2');
391             break;
392
393         default:
394             assert (0);
395     }
396
397     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
398     vout->output.i_width = width;
399     vout->output.i_height = height;
400     assert (height > 0);
401     vout->output.i_aspect = width * VOUT_ASPECT_FACTOR / height;
402     /* FIXME: I don't get the subtlety between output and fmt_out here */
403
404     /* Create window */
405     xcb_void_cookie_t c;
406     xcb_window_t window = xcb_generate_id (p_sys->conn);
407
408     p_sys->window = window;
409     c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
410                                    p_sys->parent, x, y, width, height, 0,
411                                    XCB_WINDOW_CLASS_INPUT_OUTPUT,
412                                    screen->root_visual, 0, NULL);
413     if (CheckError (vout, "cannot create X11 window", c))
414         goto error;
415     msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
416     xcb_map_window (p_sys->conn, window);
417
418     /* Create graphic context (I wonder why the heck do we need this) */
419     p_sys->gc = xcb_generate_id (p_sys->conn);
420     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
421     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
422     xcb_flush (p_sys->conn);
423
424     /* Allocate picture buffers */
425     do
426     {
427         picture_t *pic = vout->p_picture + I_OUTPUTPICTURES;
428
429         if (PictureInit (vout, pic))
430             break;
431         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
432     }
433     while (I_OUTPUTPICTURES < 2);
434
435     return VLC_SUCCESS;
436
437 error:
438     Deinit (vout);
439     return VLC_EGENERIC;
440 }
441
442 /**
443  * Free picture buffers.
444  */
445 static void Deinit (vout_thread_t *vout)
446 {
447     vout_sys_t *p_sys = vout->p_sys;
448
449     while (I_OUTPUTPICTURES > 0)
450         picture_Release (PP_OUTPUTPICTURE[--I_OUTPUTPICTURES]);
451
452     xcb_unmap_window (p_sys->conn, p_sys->window);
453     xcb_destroy_window (p_sys->conn, p_sys->window);
454     vout_ReleaseWindow (p_sys->embed);
455     p_sys->embed = NULL;
456 }
457
458 /**
459  * Sends an image to the X server.
460  */
461 static void Display (vout_thread_t *vout, picture_t *pic)
462 {
463     vout_sys_t *p_sys = vout->p_sys;
464     picture_sys_t *priv = pic->p_sys;
465     xcb_image_t *img = priv->image;
466
467     if (img->base == NULL)
468     {
469         xcb_shm_segment_info_t info = {
470             .shmseg = priv->segment,
471             .shmid = -1, /* lost track of it, unimportant */
472             .shmaddr = img->data,
473         };
474
475         xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
476                         0, 0, 0, 0, img->width, img->height, 0);
477     }
478     else
479     {
480         xcb_image_t *native = xcb_image_native (p_sys->conn, img, 1);
481
482         if (native == NULL)
483             return;
484
485         xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
486         if (native != img)
487             xcb_image_destroy (native);
488     }
489     xcb_flush (p_sys->conn);
490 }
491
492 /**
493  * Process incoming X events.
494  */
495 static int Manage (vout_thread_t *vout)
496 {
497     vout_sys_t *p_sys = vout->p_sys;
498     xcb_generic_event_t *ev;
499
500     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
501     {
502         switch (ev->response_type & ~0x80)
503         {
504         default:
505             msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
506         }
507         free (ev);
508     }
509
510     if (xcb_connection_has_error (p_sys->conn))
511     {
512         msg_Err (vout, "X server failure");
513         return VLC_EGENERIC;
514     }
515     return VLC_SUCCESS;
516 }