]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
1f52ce7ea9a2b3dbc328ed7881f22d36e8fc7aa8
[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 /**
96  * Probe the X server.
97  */
98 static int Open (vlc_object_t *obj)
99 {
100     vout_display_t *vd = (vout_display_t *)obj;
101     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
102     if (p_sys == NULL)
103         return VLC_ENOMEM;
104
105     vd->sys = p_sys;
106     p_sys->pool = NULL;
107
108     /* Get window, connect to X server */
109     const xcb_screen_t *scr;
110     p_sys->embed = GetWindow (vd, &p_sys->conn, &scr, &p_sys->depth);
111     if (p_sys->embed == NULL)
112     {
113         free (p_sys);
114         return VLC_EGENERIC;
115     }
116
117     const xcb_setup_t *setup = xcb_get_setup (p_sys->conn);
118     p_sys->byte_order = setup->image_byte_order;
119
120     /* */
121     video_format_t fmt_pic = vd->fmt;
122
123     /* Check that the selected screen supports this depth */
124     xcb_depth_t *d = NULL;
125     for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
126          it.rem > 0 && d == NULL;
127          xcb_depth_next (&it))
128         if (it.data->depth == p_sys->depth)
129             d = it.data;
130     if (d == NULL)
131     {
132         msg_Err (obj, "unexpected color depth msimatch");
133         goto error; /* WTH? depth not supported on screen */
134     }
135
136     /* Find a visual type for the selected depth */
137     xcb_visualid_t vid = 0;
138     bool gray = true;
139     const xcb_visualtype_t *vt = xcb_depth_visuals (d);
140     for (int i = xcb_depth_visuals_length (d); i > 0; i--)
141     {
142         if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
143         {
144             vid = vt->visual_id;
145             fmt_pic.i_rmask = vt->red_mask;
146             fmt_pic.i_gmask = vt->green_mask;
147             fmt_pic.i_bmask = vt->blue_mask;
148             gray = false;
149             break;
150         }
151         if (p_sys->depth == 8 && vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY
152          && vid == 0)
153         {
154             vid = vt->visual_id;
155         }
156     }
157
158     if (!vid)
159     {
160         msg_Err (obj, "unexpected visual type mismatch");
161         goto error;
162     }
163     msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32" (depth: %"PRIu8")", vid,
164              p_sys->depth);
165
166     /* Determine our pixel format */
167     p_sys->bpp = 0;
168     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
169              *end = fmt + xcb_setup_pixmap_formats_length (setup);
170          fmt < end && !p_sys->bpp; fmt++)
171     {
172         if (fmt->depth != p_sys->depth)
173             continue; /* Wrong depth! */
174
175         /* Check that the pixmap format is supported by VLC. */
176         switch (fmt->depth)
177         {
178           case 32:
179             if (fmt->bits_per_pixel != 32)
180                 continue;
181 #ifdef FIXED_VLC_RGBA_MASK
182             fmt_pic.i_chroma = VLC_CODEC_RGBA;
183             break;
184 #else
185             msg_Dbg (vd, "X11 visual with alpha-channel not supported");
186             continue;
187 #endif
188           case 24:
189             if (fmt->bits_per_pixel == 32)
190                 fmt_pic.i_chroma = VLC_CODEC_RGB32;
191             else if (fmt->bits_per_pixel == 24)
192                 fmt_pic.i_chroma = VLC_CODEC_RGB24;
193             else
194                 continue;
195             break;
196           case 16:
197             if (fmt->bits_per_pixel != 16)
198                 continue;
199             fmt_pic.i_chroma = VLC_CODEC_RGB16;
200             break;
201           case 15:
202             if (fmt->bits_per_pixel != 16)
203                 continue;
204             fmt_pic.i_chroma = VLC_CODEC_RGB15;
205             break;
206           case 8:
207             if (fmt->bits_per_pixel != 8)
208                 continue;
209             fmt_pic.i_chroma = gray ? VLC_CODEC_GREY : VLC_CODEC_RGB8;
210             break;
211           default:
212             continue;
213         }
214         if ((fmt->bits_per_pixel << 4) % fmt->scanline_pad)
215             continue; /* VLC pads lines to 16 pixels internally */
216
217         /* Byte sex is a non-issue for 8-bits. It can be worked around with
218          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
219         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
220             continue;
221
222         p_sys->bpp = fmt->bits_per_pixel;
223         p_sys->pad = fmt->scanline_pad;
224         msg_Dbg (vd, " %"PRIu8" bits per pixels, %"PRIu8" bits line pad",
225                  p_sys->bpp, p_sys->pad);
226     }
227
228     if (!p_sys->bpp)
229     {
230         msg_Err (vd, "no supported pixmap formats");
231         goto error;
232     }
233
234     /* Create colormap (needed to select non-default visual) */
235     xcb_colormap_t cmap;
236     if (vid != scr->root_visual)
237     {
238         cmap = xcb_generate_id (p_sys->conn);
239         xcb_create_colormap (p_sys->conn, XCB_COLORMAP_ALLOC_NONE,
240                              cmap, scr->root, vid);
241     }
242     else
243         cmap = scr->default_colormap;
244
245     /* Create window */
246     unsigned width, height;
247     if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
248         goto error;
249
250     p_sys->window = xcb_generate_id (p_sys->conn);
251     p_sys->gc = xcb_generate_id (p_sys->conn);
252     xcb_pixmap_t pixmap = xcb_generate_id (p_sys->conn);
253     {
254         const uint32_t mask =
255             XCB_CW_BACK_PIXMAP |
256             XCB_CW_BACK_PIXEL |
257             XCB_CW_BORDER_PIXMAP |
258             XCB_CW_BORDER_PIXEL |
259             XCB_CW_EVENT_MASK |
260             XCB_CW_COLORMAP;
261         const uint32_t values[] = {
262             /* XCB_CW_BACK_PIXMAP */
263             pixmap,
264             /* XCB_CW_BACK_PIXEL */
265             scr->black_pixel,
266             /* XCB_CW_BORDER_PIXMAP */
267             pixmap,
268             /* XCB_CW_BORDER_PIXEL */
269             scr->black_pixel,
270             /* XCB_CW_EVENT_MASK */
271             XCB_EVENT_MASK_VISIBILITY_CHANGE,
272             /* XCB_CW_COLORMAP */
273             cmap,
274         };
275         xcb_void_cookie_t c;
276
277         xcb_create_pixmap (p_sys->conn, p_sys->depth, pixmap, scr->root, 1, 1);
278         c = xcb_create_window_checked (p_sys->conn, p_sys->depth,
279                                        p_sys->window,
280                                        p_sys->embed->handle.xid, 0, 0,
281                                        width, height, 0,
282                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
283                                        vid, mask, values);
284         xcb_map_window (p_sys->conn, p_sys->window);
285         /* Create graphic context (I wonder why the heck do we need this) */
286         xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
287
288         if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
289             goto error;
290     }
291     msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
292     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
293     p_sys->cursor = CreateBlankCursor (p_sys->conn, scr);
294
295     p_sys->visible = false;
296
297     CheckSHM (obj, p_sys->conn, &p_sys->shm);
298
299     /* */
300     vout_display_info_t info = vd->info;
301     info.has_pictures_invalid = true;
302     info.has_event_thread = true;
303
304     /* Setup vout_display_t once everything is fine */
305     vd->fmt = fmt_pic;
306     vd->info = info;
307
308     vd->pool = Pool;
309     vd->prepare = NULL;
310     vd->display = Display;
311     vd->control = Control;
312     vd->manage = Manage;
313
314     /* */
315     bool is_fullscreen = vd->cfg->is_fullscreen;
316     if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
317         is_fullscreen = false;
318     vout_display_SendEventFullscreen (vd, is_fullscreen);
319     vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
320
321     return VLC_SUCCESS;
322
323 error:
324     Close (obj);
325     return VLC_EGENERIC;
326 }
327
328
329 /**
330  * Disconnect from the X server.
331  */
332 static void Close (vlc_object_t *obj)
333 {
334     vout_display_t *vd = (vout_display_t *)obj;
335     vout_display_sys_t *p_sys = vd->sys;
336
337     ResetPictures (vd);
338
339     /* show the default cursor */
340     xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
341                                   &(uint32_t) { XCB_CURSOR_NONE });
342     xcb_flush (p_sys->conn);
343
344     /* colormap, window and context are garbage-collected by X */
345     xcb_disconnect (p_sys->conn);
346     vout_display_DeleteWindow (vd, p_sys->embed);
347     free (p_sys);
348 }
349
350 /**
351  * Return a direct buffer
352  */
353 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
354 {
355     vout_display_sys_t *p_sys = vd->sys;
356     (void)requested_count;
357
358     if (!p_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 (p_sys->conn, p_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         memset (p_sys->resource, 0, sizeof(p_sys->resource));
377
378         unsigned count;
379         picture_t *pic_array[MAX_PICTURES];
380         for (count = 0; count < MAX_PICTURES; count++)
381         {
382             picture_resource_t *res = &p_sys->resource[count];
383
384             res->p->i_lines = pic->p->i_lines;
385             res->p->i_pitch = pic->p->i_pitch;
386             if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
387                                       p_sys->conn, p_sys->shm))
388                 break;
389             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
390             if (!pic_array[count])
391             {
392                 PictureResourceFree (res, p_sys->conn);
393                 memset (res, 0, sizeof(*res));
394                 break;
395             }
396         }
397         picture_Release (pic);
398
399         if (count == 0)
400             return NULL;
401
402         p_sys->pool = picture_pool_New (count, pic_array);
403         /* TODO release picture resources if NULL */
404         xcb_flush (p_sys->conn);
405     }
406
407     return p_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)
414 {
415     vout_display_sys_t *p_sys = vd->sys;
416     xcb_shm_seg_t segment = pic->p_sys->segment;
417     xcb_void_cookie_t ck;
418
419     if (!p_sys->visible)
420         goto out;
421     if (segment != 0)
422         ck = xcb_shm_put_image_checked (p_sys->conn, p_sys->window, p_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, p_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 (p_sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
437                        p_sys->window, p_sys->gc,
438                        pic->p->i_pitch / pic->p->i_pixel_pitch,
439                        lines, -vd->fmt.i_x_offset, 0, 0, p_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 (p_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 }
460
461 static int Control (vout_display_t *vd, int query, va_list ap)
462 {
463     vout_display_sys_t *p_sys = vd->sys;
464
465     switch (query)
466     {
467     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
468     {
469         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
470         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
471     }
472
473     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
474     {
475         const vout_display_cfg_t *p_cfg =
476             (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
477         const bool is_forced = (bool)va_arg (ap, int);
478
479         if (is_forced
480          && vout_window_SetSize (p_sys->embed,
481                                  p_cfg->display.width,
482                                  p_cfg->display.height))
483             return VLC_EGENERIC;
484
485         vout_display_place_t place;
486         vout_display_PlacePicture (&place, &vd->source, p_cfg, false);
487
488         if (place.width  != vd->fmt.i_visible_width ||
489             place.height != vd->fmt.i_visible_height)
490         {
491             vout_display_SendEventPicturesInvalid (vd);
492             return VLC_SUCCESS;
493         }
494
495         /* Move the picture within the window */
496         const uint32_t values[] = { place.x, place.y };
497         xcb_configure_window (p_sys->conn, p_sys->window,
498                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
499                               values);
500         return VLC_SUCCESS;
501     }
502     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
503     {
504         unsigned state = va_arg (ap, unsigned);
505         return vout_window_SetState (p_sys->embed, state);
506     }
507
508     case VOUT_DISPLAY_CHANGE_ZOOM:
509     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
510     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
511     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
512         /* I am not sure it is always necessary, but it is way simpler ... */
513         vout_display_SendEventPicturesInvalid (vd);
514         return VLC_SUCCESS;
515
516     case VOUT_DISPLAY_RESET_PICTURES:
517     {
518         ResetPictures (vd);
519
520         vout_display_place_t place;
521         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
522
523         vd->fmt.i_width  = vd->source.i_width  * place.width  / vd->source.i_visible_width;
524         vd->fmt.i_height = vd->source.i_height * place.height / vd->source.i_visible_height;
525
526         vd->fmt.i_visible_width  = place.width;
527         vd->fmt.i_visible_height = place.height;
528         vd->fmt.i_x_offset = vd->source.i_x_offset * place.width  / vd->source.i_visible_width;
529         vd->fmt.i_y_offset = vd->source.i_y_offset * place.height / vd->source.i_visible_height;
530         return VLC_SUCCESS;
531     }
532
533     /* Hide the mouse. It will be send when
534      * vout_display_t::info.b_hide_mouse is false */
535     case VOUT_DISPLAY_HIDE_MOUSE:
536         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
537                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
538         return VLC_SUCCESS;
539
540     default:
541         msg_Err (vd, "Unknown request in XCB vout display");
542         return VLC_EGENERIC;
543     }
544 }
545
546 static void Manage (vout_display_t *vd)
547 {
548     vout_display_sys_t *p_sys = vd->sys;
549
550     ManageEvent (vd, p_sys->conn, &p_sys->visible);
551 }
552
553 static void ResetPictures (vout_display_t *vd)
554 {
555     vout_display_sys_t *p_sys = vd->sys;
556
557     if (!p_sys->pool)
558         return;
559
560     for (unsigned i = 0; i < MAX_PICTURES; i++)
561     {
562         picture_resource_t *res = &p_sys->resource[i];
563
564         if (!res->p->p_pixels)
565             break;
566         PictureResourceFree (res, p_sys->conn);
567     }
568     picture_pool_Delete (p_sys->pool);
569     p_sys->pool = NULL;
570 }