]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
XCB/X11: rework and fix color depth selection
[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
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 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 <xcb/xcb.h>
31 #include <xcb/shm.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout_display.h>
36 #include <vlc_picture_pool.h>
37
38 #include "xcb_vlc.h"
39
40 #define SHM_TEXT N_("Use shared memory")
41 #define SHM_LONGTEXT N_( \
42     "Use shared memory to communicate between VLC and the X server.")
43
44 static int  Open (vlc_object_t *);
45 static void Close (vlc_object_t *);
46
47 /*
48  * Module descriptor
49  */
50 vlc_module_begin ()
51     set_shortname (N_("X11"))
52     set_description (N_("X11 video output (XCB)"))
53     set_category (CAT_VIDEO)
54     set_subcategory (SUBCAT_VIDEO_VOUT)
55     set_capability ("vout display", 75)
56     set_callbacks (Open, Close)
57     add_shortcut ("xcb-x11")
58     add_shortcut ("x11")
59
60     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
61 vlc_module_end ()
62
63 /* It must be large enough to absorb the server display jitter but it is
64  * useless to used a too large value, direct rendering cannot be used with
65  * xcb x11
66  */
67 #define MAX_PICTURES (3)
68
69 struct vout_display_sys_t
70 {
71     xcb_connection_t *conn;
72     vout_window_t *embed; /* VLC window (when windowed) */
73
74     xcb_cursor_t cursor; /* blank cursor */
75     xcb_window_t window; /* drawable X window */
76     xcb_gcontext_t gc; /* context to put images */
77     bool shm; /* whether to use MIT-SHM */
78     bool visible; /* whether to draw */
79     uint8_t bpp; /* bits per pixel */
80     uint8_t pad; /* scanline pad */
81     uint8_t depth; /* useful bits per pixel */
82     uint8_t byte_order; /* server byte order */
83
84     picture_pool_t *pool; /* picture pool */
85     picture_resource_t resource[MAX_PICTURES];
86 };
87
88 static picture_pool_t *Pool (vout_display_t *, unsigned);
89 static void Display (vout_display_t *, picture_t *);
90 static int Control (vout_display_t *, int, va_list);
91 static void Manage (vout_display_t *);
92
93 static void ResetPictures (vout_display_t *);
94
95 static const xcb_depth_t *FindDepth (const xcb_screen_t *scr,
96                                      uint_fast8_t depth)
97 {
98     xcb_depth_t *d = NULL;
99     for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
100          it.rem > 0 && d == NULL;
101          xcb_depth_next (&it))
102     {
103         if (it.data->depth == depth)
104             d = it.data;
105     }
106
107     return d;
108 }
109
110
111 /**
112  * Probe the X server.
113  */
114 static int Open (vlc_object_t *obj)
115 {
116     vout_display_t *vd = (vout_display_t *)obj;
117     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
118     if (p_sys == NULL)
119         return VLC_ENOMEM;
120
121     vd->sys = p_sys;
122     p_sys->pool = NULL;
123
124     /* Get window, connect to X server */
125     const xcb_screen_t *scr;
126     p_sys->embed = GetWindow (vd, &p_sys->conn, &scr, &(uint8_t){ 0 });
127     if (p_sys->embed == NULL)
128     {
129         free (p_sys);
130         return VLC_EGENERIC;
131     }
132
133     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
134     p_sys->byte_order = setup->image_byte_order;
135
136     /* Determine our pixel format */
137     xcb_visualid_t vid = 0;
138     p_sys->depth = 0;
139
140     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
141              *end = fmt + xcb_setup_pixmap_formats_length (setup);
142          fmt < end;
143          fmt++)
144     {
145         if (fmt->depth <= p_sys->depth)
146             continue; /* no better than earlier format */
147
148         video_format_t fmt_pic = vd->fmt;
149
150         /* Check that the pixmap format is supported by VLC. */
151         switch (fmt->depth)
152         {
153           case 32:
154             if (fmt->bits_per_pixel != 32)
155                 continue;
156 #ifdef FIXED_VLC_RGBA_MASK
157             fmt_pic.i_chroma = VLC_CODEC_RGBA;
158             break;
159 #else
160             msg_Dbg (vd, "X11 visual with alpha-channel not supported");
161             continue;
162 #endif
163           case 24:
164             if (fmt->bits_per_pixel == 32)
165                 fmt_pic.i_chroma = VLC_CODEC_RGB32;
166             else if (fmt->bits_per_pixel == 24)
167                 fmt_pic.i_chroma = VLC_CODEC_RGB24;
168             else
169                 continue;
170             break;
171           case 16:
172             if (fmt->bits_per_pixel != 16)
173                 continue;
174             fmt_pic.i_chroma = VLC_CODEC_RGB16;
175             break;
176           case 15:
177             if (fmt->bits_per_pixel != 16)
178                 continue;
179             fmt_pic.i_chroma = VLC_CODEC_RGB15;
180             break;
181           case 8:
182             if (fmt->bits_per_pixel != 8)
183                 continue;
184             fmt_pic.i_chroma = VLC_CODEC_RGB8;
185             break;
186           default:
187             continue;
188         }
189
190         /* VLC pads lines to 16 pixels internally */
191         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
192             continue;
193
194         /* Byte sex is a non-issue for 8-bits. It can be worked around with
195          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
196         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
197             continue;
198
199         /* Check that the selected screen supports this depth */
200         const xcb_depth_t *d = FindDepth (scr, fmt->depth);
201         if (d == NULL)
202             continue;
203
204         /* Find a visual type for the selected depth */
205         const xcb_visualtype_t *vt = xcb_depth_visuals (d);
206
207         /* First try True Color class */
208         for (int i = xcb_depth_visuals_length (d); i > 0; i--)
209         {
210             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
211             {
212                 fmt_pic.i_rmask = vt->red_mask;
213                 fmt_pic.i_gmask = vt->green_mask;
214                 fmt_pic.i_bmask = vt->blue_mask;
215                 goto found_visual;
216             }
217             vt++;
218         }
219         /* Then try Static Gray class */
220         if (fmt->depth == 8)
221             for (int i = xcb_depth_visuals_length (d); i > 0 && !vid; i--)
222             {
223                 if (vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
224                     goto found_grey;
225                 vt++;
226             }
227
228         continue; /* Fail: unusable pixel format */
229
230     found_grey:
231        fmt_pic.i_chroma = VLC_CODEC_GREY;
232     found_visual:
233         p_sys->bpp = fmt->bits_per_pixel;
234         p_sys->pad = fmt->scanline_pad;
235         p_sys->depth = fmt->depth;
236         vd->fmt = fmt_pic;
237         vid = vt->visual_id;
238     }
239
240     if (!vid)
241     {
242         msg_Err (obj, "no supported pixel format & visual");
243         goto error;
244     }
245
246     msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32, vid);
247     msg_Dbg (vd, " %"PRIu8" bits depth", p_sys->depth);
248     msg_Dbg (vd, " %"PRIu8" bits per pixel", p_sys->bpp);
249     msg_Dbg (vd, " %"PRIu8" bits line pad", p_sys->pad);
250
251     /* Create colormap (needed to select non-default visual) */
252     xcb_colormap_t cmap;
253     if (vid != scr->root_visual)
254     {
255         cmap = xcb_generate_id (p_sys->conn);
256         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
257                              cmap, scr->root, vid);
258     }
259     else
260         cmap = scr->default_colormap;
261
262     /* Create window */
263     unsigned width, height;
264     if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
265         goto error;
266
267     p_sys->window = xcb_generate_id (p_sys->conn);
268     p_sys->gc = xcb_generate_id (p_sys->conn);
269     xcb_pixmap_t pixmap = xcb_generate_id (p_sys->conn);
270     {
271         const uint32_t mask =
272             XCB_CW_BACK_PIXMAP |
273             XCB_CW_BACK_PIXEL |
274             XCB_CW_BORDER_PIXMAP |
275             XCB_CW_BORDER_PIXEL |
276             XCB_CW_EVENT_MASK |
277             XCB_CW_COLORMAP;
278         const uint32_t values[] = {
279             /* XCB_CW_BACK_PIXMAP */
280             pixmap,
281             /* XCB_CW_BACK_PIXEL */
282             scr->black_pixel,
283             /* XCB_CW_BORDER_PIXMAP */
284             pixmap,
285             /* XCB_CW_BORDER_PIXEL */
286             scr->black_pixel,
287             /* XCB_CW_EVENT_MASK */
288             XCB_EVENT_MASK_VISIBILITY_CHANGE,
289             /* XCB_CW_COLORMAP */
290             cmap,
291         };
292         xcb_void_cookie_t c;
293
294         xcb_create_pixmap (p_sys->conn, p_sys->depth, pixmap, scr->root, 1, 1);
295         c = xcb_create_window_checked (p_sys->conn, p_sys->depth,
296                                        p_sys->window,
297                                        p_sys->embed->handle.xid, 0, 0,
298                                        width, height, 0,
299                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
300                                        vid, mask, values);
301         xcb_map_window (p_sys->conn, p_sys->window);
302         /* Create graphic context (I wonder why the heck do we need this) */
303         xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
304
305         if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
306             goto error;
307     }
308     msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
309     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
310     p_sys->cursor = CreateBlankCursor (p_sys->conn, scr);
311
312     p_sys->visible = false;
313
314     CheckSHM (obj, p_sys->conn, &p_sys->shm);
315
316     /* */
317     vout_display_info_t info = vd->info;
318     info.has_pictures_invalid = true;
319     info.has_event_thread = true;
320
321     /* Setup vout_display_t once everything is fine */
322     vd->info = info;
323
324     vd->pool = Pool;
325     vd->prepare = NULL;
326     vd->display = Display;
327     vd->control = Control;
328     vd->manage = Manage;
329
330     /* */
331     bool is_fullscreen = vd->cfg->is_fullscreen;
332     if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
333         is_fullscreen = false;
334     vout_display_SendEventFullscreen (vd, is_fullscreen);
335     vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
336
337     return VLC_SUCCESS;
338
339 error:
340     Close (obj);
341     return VLC_EGENERIC;
342 }
343
344
345 /**
346  * Disconnect from the X server.
347  */
348 static void Close (vlc_object_t *obj)
349 {
350     vout_display_t *vd = (vout_display_t *)obj;
351     vout_display_sys_t *p_sys = vd->sys;
352
353     ResetPictures (vd);
354
355     /* show the default cursor */
356     xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
357                                   &(uint32_t) { XCB_CURSOR_NONE });
358     xcb_flush (p_sys->conn);
359
360     /* colormap, window and context are garbage-collected by X */
361     xcb_disconnect (p_sys->conn);
362     vout_display_DeleteWindow (vd, p_sys->embed);
363     free (p_sys);
364 }
365
366 /**
367  * Return a direct buffer
368  */
369 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
370 {
371     vout_display_sys_t *p_sys = vd->sys;
372     (void)requested_count;
373
374     if (!p_sys->pool)
375     {
376         vout_display_place_t place;
377
378         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
379
380         /* */
381         const uint32_t values[] = { place.x, place.y, place.width, place.height };
382         xcb_configure_window (p_sys->conn, p_sys->window,
383                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
384                               XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
385                               values);
386
387         picture_t *pic = picture_NewFromFormat (&vd->fmt);
388         if (!pic)
389             return NULL;
390
391         assert (pic->i_planes == 1);
392         memset (p_sys->resource, 0, sizeof(p_sys->resource));
393
394         unsigned count;
395         picture_t *pic_array[MAX_PICTURES];
396         for (count = 0; count < MAX_PICTURES; count++)
397         {
398             picture_resource_t *res = &p_sys->resource[count];
399
400             res->p->i_lines = pic->p->i_lines;
401             res->p->i_pitch = pic->p->i_pitch;
402             if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
403                                       p_sys->conn, p_sys->shm))
404                 break;
405             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
406             if (!pic_array[count])
407             {
408                 PictureResourceFree (res, p_sys->conn);
409                 memset (res, 0, sizeof(*res));
410                 break;
411             }
412         }
413         picture_Release (pic);
414
415         if (count == 0)
416             return NULL;
417
418         p_sys->pool = picture_pool_New (count, pic_array);
419         /* TODO release picture resources if NULL */
420         xcb_flush (p_sys->conn);
421     }
422
423     return p_sys->pool;
424 }
425
426 /**
427  * Sends an image to the X server.
428  */
429 static void Display (vout_display_t *vd, picture_t *pic)
430 {
431     vout_display_sys_t *p_sys = vd->sys;
432     xcb_shm_seg_t segment = pic->p_sys->segment;
433     xcb_void_cookie_t ck;
434
435     if (!p_sys->visible)
436         goto out;
437     if (segment != 0)
438         ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_sys->gc,
439           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
440          /* real height */ pic->p->i_lines,
441                    /* x */ vd->fmt.i_x_offset,
442                    /* y */ vd->fmt.i_y_offset,
443                /* width */ vd->fmt.i_visible_width,
444               /* height */ vd->fmt.i_visible_height,
445                            0, 0, p_sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
446                            0, segment, 0);
447     else
448     {
449         const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
450         const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
451
452         ck = xcb_put_image_checked (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
453                        p_sys->window, p_sys->gc,
454                        pic->p->i_pitch / pic->p->i_pixel_pitch,
455                        lines, -vd->fmt.i_x_offset, 0, 0, p_sys->depth,
456                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
457     }
458
459     /* Wait for reply. This makes sure that the X server gets CPU time to
460      * display the picture. xcb_flush() is *not* sufficient: especially with
461      * shared memory the PUT requests are so short that many of them can fit in
462      * X11 socket output buffer before the kernel preempts VLC. */
463     xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
464     if (e != NULL)
465     {
466         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
467         free (e);
468     }
469
470     /* FIXME might be WAY better to wait in some case (be carefull with
471      * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
472      * vout_display wrapper. */
473 out:
474     picture_Release (pic);
475 }
476
477 static int Control (vout_display_t *vd, int query, va_list ap)
478 {
479     vout_display_sys_t *p_sys = vd->sys;
480
481     switch (query)
482     {
483     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
484     {
485         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
486         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
487     }
488
489     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
490     {
491         const vout_display_cfg_t *p_cfg =
492             (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
493         const bool is_forced = (bool)va_arg (ap, int);
494
495         if (is_forced
496          && vout_window_SetSize (p_sys->embed,
497                                  p_cfg->display.width,
498                                  p_cfg->display.height))
499             return VLC_EGENERIC;
500
501         vout_display_place_t place;
502         vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
503
504         if (place.width  != vd->fmt.i_visible_width ||
505             place.height != vd->fmt.i_visible_height)
506         {
507             vout_display_SendEventPicturesInvalid (vd);
508             return VLC_SUCCESS;
509         }
510
511         /* Move the picture within the window */
512         const uint32_t values[] = { place.x, place.y };
513         xcb_configure_window (p_sys->conn, p_sys->window,
514                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
515                               values);
516         return VLC_SUCCESS;
517     }
518     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
519     {
520         unsigned state = va_arg (ap, unsigned);
521         return vout_window_SetState (p_sys->embed, state);
522     }
523
524     case VOUT_DISPLAY_CHANGE_ZOOM:
525     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
526     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
527     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
528         /* I am not sure it is always necessary, but it is way simpler ... */
529         vout_display_SendEventPicturesInvalid (vd);
530         return VLC_SUCCESS;
531
532     case VOUT_DISPLAY_RESET_PICTURES:
533     {
534         ResetPictures (vd);
535
536         vout_display_place_t place;
537         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
538
539         vd->fmt.i_width  = vd->source.i_width  * place.width  / vd->source.i_visible_width;
540         vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
541
542         vd->fmt.i_visible_width  = place.width;
543         vd->fmt.i_visible_height = place.height;
544         vd->fmt.i_x_offset = vd->source.i_x_offset * place.width  / vd->source.i_visible_width;
545         vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
546         return VLC_SUCCESS;
547     }
548
549     /* Hide the mouse. It will be send when
550      * vout_display_t::info.b_hide_mouse is false */
551     case VOUT_DISPLAY_HIDE_MOUSE:
552         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
553                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
554         return VLC_SUCCESS;
555
556     default:
557         msg_Err (vd, "Unknown request in XCB vout display");
558         return VLC_EGENERIC;
559     }
560 }
561
562 static void Manage (vout_display_t *vd)
563 {
564     vout_display_sys_t *p_sys = vd->sys;
565
566     ManageEvent (vd, p_sys->conn, &p_sys->visible);
567 }
568
569 static void ResetPictures (vout_display_t *vd)
570 {
571     vout_display_sys_t *p_sys = vd->sys;
572
573     if (!p_sys->pool)
574         return;
575
576     for (unsigned i = 0; i < MAX_PICTURES; i++)
577     {
578         picture_resource_t *res = &p_sys->resource[i];
579
580         if (!res->p->p_pixels)
581             break;
582         PictureResourceFree (res, p_sys->conn);
583     }
584     picture_pool_Delete (p_sys->pool);
585     p_sys->pool = NULL;
586 }