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