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