]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
XCB: do not render, we always use the native server format
[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     uint8_t byte_order; /* server byte order */
89 };
90
91 static int Init (vout_thread_t *);
92 static void Deinit (vout_thread_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     p_sys->byte_order = setup->image_byte_order;
140
141     /* Get the preferred screen */
142     xcb_screen_t *scr = xcb_aux_get_screen (p_sys->conn, snum);
143     p_sys->screen = scr;
144     assert (p_sys->screen);
145     msg_Dbg (vout, "using screen %d", snum);
146
147     /* Determine our video format. Normally, this is done in pf_init(), but
148      * this plugin always uses the same format for a given X11 screen. */
149     uint8_t depth = 0;
150     bool gray = true;
151     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
152              *end = fmt + xcb_setup_pixmap_formats_length (setup);
153          fmt < end; fmt++)
154     {
155         vlc_fourcc_t chroma = 0;
156
157         if (fmt->depth < depth)
158             continue; /* We already found a better format! */
159
160         /* Check that the pixmap format is supported by VLC. */
161         switch (fmt->depth)
162         {
163           case 24:
164             if (fmt->bits_per_pixel == 32)
165                 chroma = VLC_FOURCC ('R', 'V', '3', '2');
166             else if (fmt->bits_per_pixel == 24)
167                 chroma = VLC_FOURCC ('R', 'V', '2', '4');
168             else
169                 continue;
170             break;
171           case 16:
172             if (fmt->bits_per_pixel != 16)
173                 continue;
174             chroma = VLC_FOURCC ('R', 'V', '1', '6');
175             break;
176           case 15:
177             if (fmt->bits_per_pixel != 16)
178                 continue;
179             chroma = VLC_FOURCC ('R', 'V', '1', '5');
180             break;
181           case 8:
182             if (fmt->bits_per_pixel != 8)
183                 continue;
184             chroma = VLC_FOURCC ('R', 'G', 'B', '2');
185             break;
186           default:
187             continue;
188         }
189         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
190             continue; /* VLC pads lines to 16 pixels internally */
191
192         /* Byte sex is a non-issue for 8-bits. It can be worked around with
193          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
194 #ifdef WORDS_BIGENDIAN
195 # define ORDER XCB_IMAGE_ORDER_MSB_FIRST
196 #else
197 # define ORDER XCB_IMAGE_ORDER_LSB_FIRST
198 #endif
199         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
200             continue;
201
202         /* Check that the selected screen supports this depth */
203         xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
204         while (it.rem > 0 && it.data->depth != fmt->depth)
205              xcb_depth_next (&it);
206         if (!it.rem)
207             continue; /* Depth not supported on this screen */
208
209         /* Find a visual type for the selected depth */
210         const xcb_visualtype_t *vt = xcb_depth_visuals (it.data);
211         xcb_visualid_t vid = 0;
212         for (int i = xcb_depth_visuals_length (it.data); (i > 0) && !vid; i--)
213         {
214             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
215             {
216                 msg_Dbg (vout,
217                          "using TrueColor %"PRIu8"-bits visual ID 0x%0"PRIx32,
218                          fmt->depth, vt->visual_id);
219                 vid = vt->visual_id;
220                 gray = false;
221                 break;
222             }
223             if (fmt->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
224             {
225                 if (!gray)
226                     continue; /* Prefer color over gray scale */
227                 vid = vt->visual_id;
228                 chroma = VLC_FOURCC ('G', 'R', 'E', 'Y');
229                 msg_Dbg (vout,
230                          "using static gray 8-bits visual ID 0x%x"PRIx32,
231                          vt->visual_id);
232                 break;
233             }
234         }
235
236         if (!vid)
237             continue; /* The screen does not *really* support this depth */
238
239         vout->fmt_out.i_chroma = vout->output.i_chroma = chroma;
240         if (!gray)
241         {
242             vout->output.i_rmask = vt->red_mask;
243             vout->output.i_gmask = vt->green_mask;
244             vout->output.i_bmask = vt->blue_mask;
245         }
246         p_sys->vid = vid;
247         p_sys->bpp = fmt->bits_per_pixel;
248         p_sys->pad = fmt->scanline_pad;
249
250         depth = fmt->depth;
251     }
252
253     if (depth == 0)
254     {
255         msg_Err (vout, "no supported pixmap formats");
256         goto error;
257     }
258
259     msg_Dbg (vout, "using %"PRIu8" bits per pixels (line pad: %"PRIu8")",
260              p_sys->bpp, p_sys->pad);
261
262     /* Create colormap (needed to select non-default visual) */
263     if (p_sys->vid != scr->root_visual)
264     {
265         p_sys->cmap = xcb_generate_id (p_sys->conn);
266         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
267                              p_sys->cmap, scr->root, p_sys->vid);
268     }
269     else
270         p_sys->cmap = scr->default_colormap;
271
272     /* Check shared memory support */
273     p_sys->shm = var_CreateGetBool (vout, "x11-shm") > 0;
274     if (p_sys->shm)
275     {
276         xcb_shm_query_version_cookie_t ck;
277         xcb_shm_query_version_reply_t *r;
278         xcb_generic_error_t *err;
279
280         ck = xcb_shm_query_version (p_sys->conn);
281         r = xcb_shm_query_version_reply (p_sys->conn, ck, &err);
282         if (!r)
283         {
284             msg_Err (vout, "shared memory (MIT-SHM) not available");
285             msg_Warn (vout, "display will be slow");
286             p_sys->shm = false;
287         }
288     }
289
290     /* Get window */
291     /* FIXME: WTH to put as initial width/height values??? */
292     p_sys->embed = vout_RequestXWindow (vout, &(int){ 0 }, &(int){ 0 },
293                                         &(unsigned){ 0 }, &(unsigned){ 0 });
294     if (p_sys->embed == NULL)
295     {
296         msg_Err (vout, "parent window not available");
297         goto error;
298     }
299
300     vout->pf_init = Init;
301     vout->pf_end = Deinit;
302     vout->pf_display = Display;
303     vout->pf_manage = Manage;
304     return VLC_SUCCESS;
305
306 error:
307     Close (obj);
308     return VLC_EGENERIC;
309 }
310
311
312 /**
313  * Disconnect from the X server.
314  */
315 static void Close (vlc_object_t *obj)
316 {
317     vout_thread_t *vout = (vout_thread_t *)obj;
318     vout_sys_t *p_sys = vout->p_sys;
319
320     if (p_sys->embed)
321         vout_ReleaseWindow (p_sys->embed);
322     /* colormap is garbage-ollected by X (?) */
323     if (p_sys->conn)
324         xcb_disconnect (p_sys->conn);
325     free (p_sys);
326 }
327
328 struct picture_sys_t
329 {
330     xcb_connection_t *conn; /* Shared connection to X server */
331     xcb_image_t *image;  /* Picture buffer */
332     xcb_shm_seg_t segment; /* Shared memory segment X ID */
333 };
334
335 #define SHM_ERR ((void *)(intptr_t)(-1))
336
337 static int PictureInit (vout_thread_t *vout, picture_t *pic)
338 {
339     vout_sys_t *p_sys = vout->p_sys;
340     picture_sys_t *priv = malloc (sizeof (*p_sys));
341
342     if (priv == NULL)
343         return VLC_ENOMEM;
344
345     assert (pic->i_status == FREE_PICTURE);
346     vout_InitPicture (vout, pic, vout->output.i_chroma,
347                       vout->output.i_width, vout->output.i_height,
348                       vout->output.i_aspect);
349
350     void *shm = SHM_ERR;
351     const size_t size = pic->p->i_pitch * pic->p->i_lines;
352
353     if (p_sys->shm)
354     {   /* Allocate shared memory segment */
355         int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
356
357         if (id == -1)
358         {
359             msg_Err (vout, "shared memory allocation error: %m");
360             goto error;
361         }
362
363         /* Attach the segment to VLC */
364         shm = shmat (id, NULL, 0 /* read/write */);
365         if (shm == SHM_ERR)
366         {
367             msg_Err (vout, "shared memory attachment error: %m");
368             shmctl (id, IPC_RMID, 0);
369             goto error;
370         }
371
372         /* Attach the segment to X */
373         xcb_void_cookie_t ck;
374
375         priv->segment = xcb_generate_id (p_sys->conn);
376         ck = xcb_shm_attach_checked (p_sys->conn, priv->segment, id, 1);
377         shmctl (id, IPC_RMID, 0);
378
379         if (CheckError (vout, "shared memory server-side error", ck))
380         {
381             msg_Info (vout, "using buggy X (remote) server? SSH?");
382             shmdt (shm);
383             shm = SHM_ERR;
384         }
385     }
386     else
387         priv->segment = 0;
388
389     const unsigned real_width = pic->p->i_pitch / (p_sys->bpp >> 3);
390     /* FIXME: anyway to getthing more intuitive than that?? */
391
392     xcb_image_t *img;
393     img = xcb_image_create (real_width, pic->p->i_lines,
394                             XCB_IMAGE_FORMAT_Z_PIXMAP, p_sys->pad,
395                             p_sys->screen->root_depth, p_sys->bpp, p_sys->bpp,
396                             p_sys->byte_order, XCB_IMAGE_ORDER_MSB_FIRST,
397                             NULL,
398                             (shm != SHM_ERR) ? size : 0,
399                             (shm != SHM_ERR) ? shm : NULL);
400
401     if (img == NULL)
402     {
403         if (shm != SHM_ERR)
404             xcb_shm_detach (p_sys->conn, priv->segment);
405         goto error;
406     }
407
408     priv->conn = p_sys->conn;
409     priv->image = img;
410     pic->p_sys = priv;
411     pic->p->p_pixels = img->data;
412     pic->i_status = DESTROYED_PICTURE;
413     pic->i_type = DIRECT_PICTURE;
414     return VLC_SUCCESS;
415
416 error:
417     if (shm != SHM_ERR)
418         shmdt (shm);
419     free (priv);
420     return VLC_EGENERIC;
421 }
422
423
424 /**
425  * Release picture private data
426  */
427 static void PictureDeinit (picture_t *pic)
428 {
429     struct picture_sys_t *p_sys = pic->p_sys;
430
431     if (p_sys->segment != 0)
432     {
433         xcb_shm_detach (p_sys->conn, p_sys->segment);
434         shmdt (p_sys->image->data);
435     }
436     xcb_image_destroy (p_sys->image);
437     free (p_sys);
438 }
439
440 /**
441  * Allocate drawable window and picture buffers.
442  */
443 static int Init (vout_thread_t *vout)
444 {
445     vout_sys_t *p_sys = vout->p_sys;
446     const xcb_screen_t *screen = p_sys->screen;
447     unsigned x, y, width, height;
448
449     I_OUTPUTPICTURES = 0;
450
451     /* Determine parent window and size */
452     if (vout->b_fullscreen)
453     {
454         p_sys->parent = screen->root;
455         width = screen->width_in_pixels;
456         height = screen->height_in_pixels;
457     }
458     else
459     {
460         p_sys->parent = p_sys->embed->handle.xid;
461
462         /* Subscribe to parent window resize events */
463         const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
464         xcb_change_window_attributes (p_sys->conn, p_sys->parent,
465                                       XCB_CW_EVENT_MASK, &value);
466
467         xcb_get_geometry_cookie_t ck;
468         ck = xcb_get_geometry (p_sys->conn, p_sys->parent);
469
470         xcb_get_geometry_reply_t *geo;
471         xcb_generic_error_t *err;
472         geo = xcb_get_geometry_reply (p_sys->conn, ck, &err);
473         width = geo->width;
474         height = geo->height;
475         free (geo);
476     }
477
478     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
479
480     /* FIXME: I don't get the subtlety between output and fmt_out here */
481     vout->fmt_out.i_visible_width = width;
482     vout->fmt_out.i_visible_height = height;
483     vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
484
485     vout->output.i_width = vout->fmt_out.i_width =
486         width * vout->fmt_in.i_width / vout->fmt_in.i_visible_width;
487     vout->output.i_height = vout->fmt_out.i_height =
488         height * vout->fmt_in.i_height / vout->fmt_in.i_visible_height;
489     vout->fmt_out.i_x_offset =
490         width * vout->fmt_in.i_x_offset / vout->fmt_in.i_visible_width;
491     p_vout->fmt_out.i_y_offset =
492         height * vout->fmt_in.i_y_offset / vout->fmt_in.i_visible_height;
493
494     assert (height > 0);
495     vout->output.i_aspect = vout->fmt_out.i_aspect =
496         width * VOUT_ASPECT_FACTOR / height;
497
498     /* Create window */
499     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK
500                         | XCB_CW_COLORMAP;
501     const uint32_t values[] = {
502         /* XCB_CW_BACK_PIXEL */
503         screen->black_pixel,
504         /* XCB_CW_EVENT_MASK */
505         XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
506         XCB_EVENT_MASK_POINTER_MOTION,
507         /* XCB_CW_COLORMAP */
508         p_sys->cmap,
509     };
510     xcb_void_cookie_t c;
511     xcb_window_t window = xcb_generate_id (p_sys->conn);
512
513     c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
514                                    p_sys->parent, x, y, width, height, 0,
515                                    XCB_WINDOW_CLASS_INPUT_OUTPUT,
516                                    p_sys->vid, mask, values);
517     if (CheckError (vout, "cannot create X11 window", c))
518         goto error;
519     p_sys->window = window;
520     msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
521     xcb_map_window (p_sys->conn, window);
522
523     /* Create graphic context (I wonder why the heck do we need this) */
524     p_sys->gc = xcb_generate_id (p_sys->conn);
525     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
526     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
527     xcb_flush (p_sys->conn);
528
529     /* Allocate picture buffers */
530     I_OUTPUTPICTURES = 0;
531     for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
532     {
533         picture_t *pic = vout->p_picture + index;
534
535         if (index > sizeof (vout->p_picture) / sizeof (pic))
536             break;
537         if (pic->i_status != FREE_PICTURE)
538             continue;
539         if (PictureInit (vout, pic))
540             break;
541         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
542     }
543
544     return VLC_SUCCESS;
545
546 error:
547     Deinit (vout);
548     return VLC_EGENERIC;
549 }
550
551 /**
552  * Free picture buffers.
553  */
554 static void Deinit (vout_thread_t *vout)
555 {
556     vout_sys_t *p_sys = vout->p_sys;
557
558     for (int i = 0; i < I_OUTPUTPICTURES; i++)
559         PictureDeinit (PP_OUTPUTPICTURE[i]);
560
561     xcb_unmap_window (p_sys->conn, p_sys->window);
562     xcb_destroy_window (p_sys->conn, p_sys->window);
563 }
564
565 /**
566  * Sends an image to the X server.
567  */
568 static void Display (vout_thread_t *vout, picture_t *pic)
569 {
570     vout_sys_t *p_sys = vout->p_sys;
571     picture_sys_t *priv = pic->p_sys;
572     xcb_image_t *img = priv->image;
573
574     if (img->base == NULL)
575     {
576         xcb_shm_segment_info_t info = {
577             .shmseg = priv->segment,
578             .shmid = -1, /* lost track of it, unimportant */
579             .shmaddr = img->data,
580         };
581
582         xcb_image_shm_put (p_sys->conn, p_sys->window, p_sys->gc, img, info,
583                         0, 0, 0, 0, img->width, img->height, 0);
584     }
585     else
586         xcb_image_put (p_sys->conn, p_sys->window, p_sys->gc, img, 0, 0, 0);
587     xcb_flush (p_sys->conn);
588 }
589
590 /**
591  * Process incoming X events.
592  */
593 static int Manage (vout_thread_t *vout)
594 {
595     vout_sys_t *p_sys = vout->p_sys;
596     xcb_generic_event_t *ev;
597
598     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
599         ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
600
601     if (xcb_connection_has_error (p_sys->conn))
602     {
603         msg_Err (vout, "X server failure");
604         return VLC_EGENERIC;
605     }
606     return VLC_SUCCESS;
607 }