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