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