]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
Heisenbug fix
[vlc] / modules / video_output / xcb / x11.c
1 /**
2  * @file x11.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 #include "xcb_vlc.h"
45
46 #define DISPLAY_TEXT N_("X11 display")
47 #define DISPLAY_LONGTEXT N_( \
48     "X11 hardware display to use. By default VLC will " \
49     "use the value of the DISPLAY environment variable.")
50
51 #define SHM_TEXT N_("Use shared memory")
52 #define SHM_LONGTEXT N_( \
53     "Use shared memory to communicate between VLC and the X server.")
54
55 static int  Open (vlc_object_t *);
56 static void Close (vlc_object_t *);
57
58 /*
59  * Module descriptor
60  */
61 vlc_module_begin ()
62     set_shortname (N_("XCB"))
63     set_description (N_("(Experimental) XCB video output"))
64     set_category (CAT_VIDEO)
65     set_subcategory (SUBCAT_VIDEO_VOUT)
66     set_capability ("video output", 0)
67     set_callbacks (Open, Close)
68
69     add_string ("x11-display", NULL, NULL,
70                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
71     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
72 vlc_module_end ()
73
74 struct vout_sys_t
75 {
76     xcb_connection_t *conn;
77     xcb_screen_t *screen;
78     vout_window_t *embed; /* VLC window (when windowed) */
79
80     xcb_visualid_t vid; /* selected visual */
81     xcb_window_t parent; /* parent X window */
82     xcb_colormap_t cmap; /* colormap for selected visual */
83     xcb_window_t window; /* drawable X window */
84     xcb_gcontext_t gc; /* context to put images */
85     bool shm; /* whether to use MIT-SHM */
86     uint8_t bpp; /* bits per pixel */
87     uint8_t pad; /* scanline pad */
88 };
89
90 static int Init (vout_thread_t *);
91 static void Deinit (vout_thread_t *);
92 static void Render (vout_thread_t *, picture_t *);
93 static void Display (vout_thread_t *, picture_t *);
94 static int Manage (vout_thread_t *);
95
96 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
97 {
98     xcb_generic_error_t *err;
99
100     err = xcb_request_check (vout->p_sys->conn, ck);
101     if (err)
102     {
103         msg_Err (vout, "%s: X11 error %d", str, err->error_code);
104         return VLC_EGENERIC;
105     }
106     return VLC_SUCCESS;
107 }
108
109 #define p_vout vout
110
111 /**
112  * Probe the X server.
113  */
114 static int Open (vlc_object_t *obj)
115 {
116     vout_thread_t *vout = (vout_thread_t *)obj;
117     vout_sys_t *p_sys = malloc (sizeof (*p_sys));
118     if (p_sys == NULL)
119         return VLC_ENOMEM;
120
121     vout->p_sys = p_sys;
122     p_sys->conn = NULL;
123     p_sys->embed = NULL;
124
125     /* Connect to X */
126     char *display = var_CreateGetNonEmptyString (vout, "x11-display");
127     int snum;
128     p_sys->conn = xcb_connect (display, &snum);
129     if (xcb_connection_has_error (p_sys->conn) /*== NULL*/)
130     {
131         msg_Err (vout, "cannot connect to X server %s",
132                  display ? display : "");
133         free (display);
134         goto error;
135     }
136     free (display);
137
138     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
139
140     /* Get the preferred screen */
141     xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
142     p_sys->screen = scr;
143     assert (p_sys->screen);
144     msg_Dbg (vout, "using screen %d (depth %"PRIu8")", snum, scr->root_depth);
145
146     /* Determine the visual (color depth and palette) */
147     xcb_visualtype_t *vt = NULL;
148     bool gray = false;
149
150     if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_TRUE_COLOR,
151                                             scr->root_depth)) != NULL)
152         msg_Dbg (vout, "using TrueColor visual ID %d", (int)vt->visual_id);
153     else
154 #if 0
155     if ((vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_COLOR,
156                                             scr->root_depth)) != NULL)
157         msg_Dbg (vout, "using static color visual ID %d", (int)vt->visual_id);
158     else
159 #endif
160     if ((scr->root_depth == 8)
161      && (vt = xcb_aux_find_visual_by_attrs (scr, XCB_VISUAL_CLASS_STATIC_GRAY,
162                                             scr->root_depth)) != NULL)
163     {
164         msg_Dbg (vout, "using static gray visual ID %d", (int)vt->visual_id);
165         gray = true;
166     }
167     else
168     {
169         msg_Err (vout, "no supported visual class");
170         goto error;
171     }
172     p_sys->vid = vt->visual_id;
173
174     /* Determine our input format (normally, done in Init() but X11
175      * never changes its format) */
176     vout->output.i_chroma = 0;
177     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
178              *end = fmt + xcb_setup_pixmap_formats_length (setup);
179          fmt < end; fmt++)
180     {
181         if (fmt->depth != scr->root_depth)
182             continue;
183
184         switch (fmt->depth)
185         {
186           case 24:
187             if (fmt->bits_per_pixel == 32)
188                 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '3', '2');
189             else if (fmt->bits_per_pixel == 24)
190                 vout->output.i_chroma = VLC_FOURCC ('R', 'V', '2', '4');
191             else
192                 continue;
193             break;
194           case 16:
195             if (fmt->bits_per_pixel != 16)
196                 continue;
197             vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '6');
198             break;
199           case 15:
200             if (fmt->bits_per_pixel != 16)
201                 continue;
202             vout->output.i_chroma = VLC_FOURCC ('R', 'V', '1', '5');
203             break;
204           case 8:
205             if (fmt->bits_per_pixel != 8)
206                 continue;
207             vout->output.i_chroma = gray ? VLC_FOURCC ('G', 'R', 'E', 'Y')
208                                          : VLC_FOURCC ('R', 'G', 'B', '2');
209             break;
210         }
211         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
212             continue; /* VLC pads lines to 16 pixels internally */
213         p_sys->bpp = fmt->bits_per_pixel;
214         p_sys->pad = fmt->scanline_pad;
215         msg_Dbg (vout, "using %"PRIu8" bits per pixels (line pad: %"PRIu8")",
216                  p_sys->bpp, p_sys->pad);
217         break;
218     }
219
220     if (!vout->output.i_chroma)
221     {
222         msg_Err (vout, "no supported pixmap formats");
223         goto error;
224     }
225
226     vout->fmt_out.i_chroma = vout->output.i_chroma;
227     if (!gray)
228     {
229         vout->output.i_rmask = vt->red_mask;
230         vout->output.i_gmask = vt->green_mask;
231         vout->output.i_bmask = vt->blue_mask;
232     }
233
234     /* Create colormap (needed to select non-default visual) */
235     p_sys->cmap = xcb_generate_id (p_sys->conn);
236     xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
237                          p_sys->cmap, scr->root, p_sys->vid);
238
239     /* Check shared memory support */
240     p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
241     if (p_sys->shm)
242     {
243         xcb_shm_query_version_cookie_t ck;
244         xcb_shm_query_version_reply_t *r;
245         xcb_generic_error_t *err;
246
247         ck = xcb_shm_query_version (p_sys->conn);
248         r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
249         if (!r)
250         {
251             msg_Err (vout, "shared memory (MIT-SHM) not available");
252             msg_Warn (vout, "display will be slow");
253             p_sys->shm = false;
254         }
255     }
256
257     /* Get window */
258     /* FIXME: WTH to put as initial width/height values??? */
259     p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
260                                         &(unsigned){ 0 }, &(unsigned){ 0 });
261     if (p_sys->embed == NULL)
262     {
263         msg_Err (vout, "parent window not available");
264         goto error;
265     }
266
267     vout->pf_init = Init;
268     vout->pf_end = Deinit;
269     vout->pf_render = Render;
270     vout->pf_display = Display;
271     vout->pf_manage = Manage;
272     return VLC_SUCCESS;
273
274 error:
275     Close (obj);
276     return VLC_EGENERIC;
277 }
278
279
280 /**
281  * Disconnect from the X server.
282  */
283 static void Close (vlc_object_t *obj)
284 {
285     vout_thread_t *vout = (vout_thread_t *)obj;
286     vout_sys_t *p_sys = vout->p_sys;
287
288     if (p_sys->embed)
289         vout_ReleaseWindow (p_sys->embed);
290     /* colormap is garbage-ollected by X (?) */
291     if (p_sys->conn)
292         xcb_disconnect (p_sys->conn);
293     free (p_sys);
294 }
295
296 struct picture_sys_t
297 {
298     xcb_connection_t *conn; /* Shared connection to X server */
299     xcb_image_t *image;  /* Picture buffer */
300     xcb_image_t *native; /* Rendered picture buffer (in X server format) */
301     xcb_shm_seg_t segment; /* Shared memory segment X ID */
302 };
303
304 #define SHM_ERR ((void *)(intptr_t)(-1))
305
306 static int PictureInit (vout_thread_t *vout, picture_t *pic)
307 {
308     vout_sys_t *p_sys = vout->p_sys;
309     picture_sys_t *priv = malloc (sizeof (*p_sys));
310
311     if (priv == NULL)
312         return VLC_ENOMEM;
313
314     assert (pic->i_status == FREE_PICTURE);
315     vout_InitPicture (vout, pic, vout->output.i_chroma,
316                       vout->output.i_width, vout->output.i_height,
317                       vout->output.i_aspect);
318
319     void *shm = SHM_ERR;
320     const size_t size = pic->p->i_pitch * pic->p->i_lines;
321
322     if (p_sys->shm)
323     {   /* Allocate shared memory segment */
324         int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
325
326         if (id == -1)
327         {
328             msg_Err (vout, "shared memory allocation error: %m");
329             goto error;
330         }
331
332         /* Attach the segment to VLC */
333         shm = shmat (id, NULL, 0 /* read/write */);
334         if (shm == SHM_ERR)
335         {
336             msg_Err (vout, "shared memory attachment error: %m");
337             shmctl (id, IPC_RMID, 0);
338             goto error;
339         }
340
341         /* Attach the segment to X */
342         xcb_void_cookie_t ck;
343
344         priv->segment = xcb_generate_id (p_sys->conn);
345         ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
346         shmctl (id, IPC_RMID, 0);
347
348         if (CheckError (vout, "shared memory server-side error", ck))
349         {
350             msg_Info (vout, "using buggy X (remote) server? SSH?");
351             shmdt (shm);
352             shm = SHM_ERR;
353         }
354     }
355     else
356         priv->segment = 0;
357
358     const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
359     /* FIXME: anyway to getthing more intuitive than that?? */
360
361     xcb_image_t *img;
362     img = xcb_image_create (real_width, pic->p->i_lines,
363                             XCB_IMAGE_FORMAT_Z_PIXMAP, p_sys->pad,
364                             p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
365 #ifdef WORDS_BIGENDIAN
366                             XCB_IMAGE_ORDER_MSB_FIRST,
367 #else
368                             XCB_IMAGE_ORDER_LSB_FIRST,
369 #endif
370                             XCB_IMAGE_ORDER_MSB_FIRST,
371                             NULL,
372                             (shm != SHM_ERR) ? size : 0,
373                             (shm != SHM_ERR) ? shm : NULL);
374
375     if (img == NULL)
376     {
377         if (shm != SHM_ERR)
378             xcb_shm_detach (p_sys->conn, priv->segment);
379         goto error;
380     }
381     if (shm != SHM_ERR && xcb_image_native (p_sys->conn, img, 0) == NULL)
382         msg_Warn (vout, "incompatible X server image format");
383
384     priv->conn = p_sys->conn;
385     priv->image = img;
386     priv->native = NULL;
387     pic->p_sys = priv;
388     pic->p->p_pixels = img->data;
389     pic->i_status = DESTROYED_PICTURE;
390     pic->i_type = DIRECT_PICTURE;
391     return VLC_SUCCESS;
392
393 error:
394     if (shm != SHM_ERR)
395         shmdt (shm);
396     free (priv);
397     return VLC_EGENERIC;
398 }
399
400
401 /**
402  * Release picture private data
403  */
404 static void PictureDeinit (picture_t *pic)
405 {
406     struct picture_sys_t *p_sys = pic->p_sys;
407
408     if (p_sys->segment != 0)
409     {
410         xcb_shm_detach (p_sys->conn, p_sys->segment);
411         shmdt (p_sys->image->data);
412     }
413     if ((p_sys->native != NULL) && (p_sys->native != p_sys->image))
414         xcb_image_destroy (p_sys->native);
415     xcb_image_destroy (p_sys->image);
416     free (p_sys);
417 }
418
419 /**
420  * Allocate drawable window and picture buffers.
421  */
422 static int Init (vout_thread_t *vout)
423 {
424     vout_sys_t *p_sys = vout->p_sys;
425     const xcb_screen_t *screen = p_sys->screen;
426     unsigned x, y, width, height;
427
428     I_OUTPUTPICTURES = 0;
429
430     /* Determine parent window and size */
431     if (vout->b_fullscreen)
432     {
433         p_sys->parent = screen->root;
434         width = screen->width_in_pixels;
435         height = screen->height_in_pixels;
436     }
437     else
438     {
439         p_sys->parent = p_sys->embed->handle.xid;
440
441         /* Subscribe to parent window resize events */
442         const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
443         xcb_change_window_attributes (p_sys->conn, p_sys->parent,
444                                       XCB_CW_EVENT_MASK, &value);
445
446         xcb_get_geometry_cookie_t ck;
447         ck = xcb_get_geometry (p_sys->conn, p_sys->parent);
448
449         xcb_get_geometry_reply_t *geo;
450         xcb_generic_error_t *err;
451         geo = xcb_get_geometry_reply (p_sys->conn, ck, &err);
452         width = geo->width;
453         height = geo->height;
454         free (geo);
455     }
456
457     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
458
459     /* FIXME: I don't get the subtlety between output and fmt_out here */
460     vout->fmt_out.i_visible_width = width;
461     vout->fmt_out.i_visible_height = height;
462     vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
463
464     vout->output.i_width = vout->fmt_out.i_width =
465         width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
466     vout->output.i_height = vout->fmt_out.i_height =
467         height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
468     vout->fmt_out.i_x_offset =
469         width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
470     p_vout->fmt_out.i_y_offset =
471         height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
472
473     assert (height > 0);
474     vout->output.i_aspect = vout->fmt_out.i_aspect =
475         width * VOUT_ASPECT_FACTOR / height;
476
477     /* Create window */
478     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
479                         | XCB_CW_COLORMAP;
480     const uint32_t values[] = {
481         /* XCB_CW_BACK_PIXEL */
482         screen->black_pixel,
483         /* XCB_CW_EVENT_MASK */
484         XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
485         XCB_EVENT_MASK_POINTER_MOTION,
486         /* XCB_CW_COLORMAP */
487         p_sys->cmap,
488     };
489     xcb_void_cookie_t c;
490     xcb_window_t window = xcb_generate_id (p_sys->conn);
491
492     c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
493                                    p_sys->parent, x, y, width, height, 0,
494                                    XCB_WINDOW_CLASS_INPUT_OUTPUT,
495                                    p_sys->vid, mask, values);
496     if (CheckError (vout, "cannot create X11 window", c))
497         goto error;
498     p_sys->window = window;
499     msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
500     xcb_map_window (p_sys->conn, window);
501
502     /* Create graphic context (I wonder why the heck do we need this) */
503     p_sys->gc = xcb_generate_id (p_sys->conn);
504     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
505     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
506     xcb_flush (p_sys->conn);
507
508     /* Allocate picture buffers */
509     I_OUTPUTPICTURES = 0;
510     for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
511     {
512         picture_t *pic = vout->p_picture + index;
513
514         if (index > sizeof (vout->p_picture) / sizeof (pic))
515             break;
516         if (pic->i_status != FREE_PICTURE)
517             continue;
518         if (PictureInit (vout, pic))
519             break;
520         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
521     }
522
523     return VLC_SUCCESS;
524
525 error:
526     Deinit (vout);
527     return VLC_EGENERIC;
528 }
529
530 /**
531  * Free picture buffers.
532  */
533 static void Deinit (vout_thread_t *vout)
534 {
535     vout_sys_t *p_sys = vout->p_sys;
536
537     for (int i = 0; i < I_OUTPUTPICTURES; i++)
538         PictureDeinit (PP_OUTPUTPICTURE[i]);
539
540     xcb_unmap_window (p_sys->conn, p_sys->window);
541     xcb_destroy_window (p_sys->conn, p_sys->window);
542 }
543
544 /**
545  * Prepares an image ahead of display.
546  */
547 static void Render (vout_thread_t *vout, picture_t *pic)
548 {
549     vout_sys_t *p_sys = vout->p_sys;
550     picture_sys_t *priv = pic->p_sys;
551
552     if ((priv->native != NULL) && (priv->native != priv->image))
553         xcb_image_destroy (priv->native);
554     priv->native = xcb_image_native (p_sys->conn, priv->image, 1);
555 }
556
557
558 /**
559  * Sends an image to the X server.
560  */
561 static void Display (vout_thread_t *vout, picture_t *pic)
562 {
563     vout_sys_t *p_sys = vout->p_sys;
564     picture_sys_t *priv = pic->p_sys;
565     xcb_image_t *img = priv->image, *native = priv->native;
566
567     if ((native == img) && (img->base == NULL))
568     {
569         xcb_shm_segment_info_t info = {
570             .shmseg = priv->segment,
571             .shmid = -1, /* lost track of it, unimportant */
572             .shmaddr = img->data,
573         };
574
575         xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
576                         0, 0, 0, 0, img->width, img->height, 0);
577     }
578     else
579     if (native != NULL)
580         xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, native, 0, 0, 0);
581     xcb_flush (p_sys->conn);
582 }
583
584 /**
585  * Process incoming X events.
586  */
587 static int Manage (vout_thread_t *vout)
588 {
589     vout_sys_t *p_sys = vout->p_sys;
590     xcb_generic_event_t *ev;
591
592     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
593         ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
594
595     if (xcb_connection_has_error (p_sys->conn))
596     {
597         msg_Err (vout, "X server failure");
598         return VLC_EGENERIC;
599     }
600     return VLC_SUCCESS;
601 }