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