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