]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/x11.c
XCB: flush when changing the cursor
[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", 100)
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 /* This must be large enough to absorb the server display jitter.
59  * But excessively large value is useless as direct rendering cannot be used
60  * with XCB X11. */
61 #define MAX_PICTURES (3)
62
63 struct vout_display_sys_t
64 {
65     xcb_connection_t *conn;
66     vout_window_t *embed; /* VLC window */
67
68     xcb_cursor_t cursor; /* blank cursor */
69     xcb_window_t window; /* drawable X window */
70     xcb_gcontext_t gc; /* context to put images */
71     bool shm; /* whether to use MIT-SHM */
72     bool visible; /* whether to draw */
73     uint8_t depth; /* useful bits per pixel */
74
75     picture_pool_t *pool; /* picture pool */
76     picture_resource_t resource[MAX_PICTURES];
77 };
78
79 static picture_pool_t *Pool (vout_display_t *, unsigned);
80 static void Display (vout_display_t *, picture_t *, subpicture_t *subpicture);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
83
84 static void ResetPictures (vout_display_t *);
85
86 static const xcb_depth_t *FindDepth (const xcb_screen_t *scr,
87                                      uint_fast8_t depth)
88 {
89     xcb_depth_t *d = NULL;
90     for (xcb_depth_iterator_t it = xcb_screen_allowed_depths_iterator (scr);
91          it.rem > 0 && d == NULL;
92          xcb_depth_next (&it))
93     {
94         if (it.data->depth == depth)
95             d = it.data;
96     }
97
98     return d;
99 }
100
101
102 /**
103  * Probe the X server.
104  */
105 static int Open (vlc_object_t *obj)
106 {
107     vout_display_t *vd = (vout_display_t *)obj;
108     vout_display_sys_t *sys = malloc (sizeof (*sys));
109     if (unlikely(sys == NULL))
110         return VLC_ENOMEM;
111
112     vd->sys = sys;
113     sys->pool = NULL;
114
115     /* Get window, connect to X server */
116     xcb_connection_t *conn;
117     const xcb_screen_t *scr;
118     sys->embed = GetWindow (vd, &conn, &scr, &(uint8_t){ 0 });
119     if (sys->embed == NULL)
120     {
121         free (sys);
122         return VLC_EGENERIC;
123     }
124     sys->conn = conn;
125
126     const xcb_setup_t *setup = xcb_get_setup (conn);
127
128     /* Determine our pixel format */
129     xcb_visualid_t vid;
130     sys->depth = 0;
131
132     for (const xcb_format_t *fmt = xcb_setup_pixmap_formats (setup),
133              *end = fmt + xcb_setup_pixmap_formats_length (setup);
134          fmt < end;
135          fmt++)
136     {
137         if (fmt->depth <= sys->depth)
138             continue; /* no better than earlier format */
139
140         video_format_t fmt_pic = vd->fmt;
141
142         /* Check that the pixmap format is supported by VLC. */
143         switch (fmt->depth)
144         {
145           case 32:
146             if (fmt->bits_per_pixel != 32)
147                 continue;
148 #ifdef FIXED_VLC_RGBA_MASK
149             fmt_pic.i_chroma = VLC_CODEC_RGBA;
150             break;
151 #else
152             msg_Dbg (vd, "X11 visual with alpha-channel not supported");
153             continue;
154 #endif
155           case 24:
156             if (fmt->bits_per_pixel == 32)
157                 fmt_pic.i_chroma = VLC_CODEC_RGB32;
158             else if (fmt->bits_per_pixel == 24)
159                 fmt_pic.i_chroma = VLC_CODEC_RGB24;
160             else
161                 continue;
162             break;
163           case 16:
164             if (fmt->bits_per_pixel != 16)
165                 continue;
166             fmt_pic.i_chroma = VLC_CODEC_RGB16;
167             break;
168           case 15:
169             if (fmt->bits_per_pixel != 16)
170                 continue;
171             fmt_pic.i_chroma = VLC_CODEC_RGB15;
172             break;
173           case 8:
174             if (fmt->bits_per_pixel != 8)
175                 continue;
176             fmt_pic.i_chroma = VLC_CODEC_RGB8;
177             break;
178           default:
179             continue;
180         }
181
182         /* Byte sex is a non-issue for 8-bits. It can be worked around with
183          * RGB masks for 24-bits. Too bad for 15-bits and 16-bits. */
184         if (fmt->bits_per_pixel == 16 && setup->image_byte_order != ORDER)
185             continue;
186
187         /* Make sure the X server is sane */
188         assert (fmt->bits_per_pixel > 0);
189         if (unlikely(fmt->scanline_pad % fmt->bits_per_pixel))
190             continue;
191
192         /* Check that the selected screen supports this depth */
193         const xcb_depth_t *d = FindDepth (scr, fmt->depth);
194         if (d == NULL)
195             continue;
196
197         /* Find a visual type for the selected depth */
198         const xcb_visualtype_t *vt = xcb_depth_visuals (d);
199
200         /* First try True Color class */
201         for (int i = xcb_depth_visuals_length (d); i > 0; i--)
202         {
203             if (vt->_class == XCB_VISUAL_CLASS_TRUE_COLOR)
204             {
205                 fmt_pic.i_rmask = vt->red_mask;
206                 fmt_pic.i_gmask = vt->green_mask;
207                 fmt_pic.i_bmask = vt->blue_mask;
208             found_visual:
209                 vd->fmt = fmt_pic;
210                 vid = vt->visual_id;
211                 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32, vid);
212                 sys->depth = fmt->depth;
213                 msg_Dbg (vd, " %"PRIu8" bits depth", sys->depth);
214                 msg_Dbg (vd, " %"PRIu8" bits per pixel", fmt->bits_per_pixel);
215                 msg_Dbg (vd, " %"PRIu8" bits line pad", fmt->scanline_pad);
216                 goto found_format;
217             }
218             vt++;
219         }
220
221         /* Then try Static Gray class */
222         if (fmt->depth != 8)
223             continue;
224         vt = xcb_depth_visuals (d);
225         for (int i = xcb_depth_visuals_length (d); i > 0 && !vid; i--)
226         {
227             if (vt->_class == XCB_VISUAL_CLASS_STATIC_GRAY)
228             {
229                 fmt_pic.i_chroma = VLC_CODEC_GREY;
230                 goto found_visual;
231             }
232             vt++;
233         }
234     }
235
236     msg_Err (obj, "no supported pixel format & visual");
237     goto error;
238
239 found_format:;
240     /* Create colormap (needed to select non-default visual) */
241     xcb_colormap_t cmap;
242     if (vid != scr->root_visual)
243     {
244         cmap = xcb_generate_id (conn);
245         xcb_create_colormap (conn, XCB_COLORMAP_ALLOC_NONE,
246                              cmap, scr->root, vid);
247     }
248     else
249         cmap = scr->default_colormap;
250
251     /* Create window */
252     unsigned width, height;
253     if (GetWindowSize (sys->embed, conn, &width, &height))
254         goto error;
255
256     sys->window = xcb_generate_id (conn);
257     sys->gc = xcb_generate_id (conn);
258     xcb_pixmap_t pixmap = xcb_generate_id (conn);
259     {
260         const uint32_t mask =
261             XCB_CW_BACK_PIXMAP |
262             XCB_CW_BACK_PIXEL |
263             XCB_CW_BORDER_PIXMAP |
264             XCB_CW_BORDER_PIXEL |
265             XCB_CW_EVENT_MASK |
266             XCB_CW_COLORMAP;
267         const uint32_t values[] = {
268             /* XCB_CW_BACK_PIXMAP */
269             pixmap,
270             /* XCB_CW_BACK_PIXEL */
271             scr->black_pixel,
272             /* XCB_CW_BORDER_PIXMAP */
273             pixmap,
274             /* XCB_CW_BORDER_PIXEL */
275             scr->black_pixel,
276             /* XCB_CW_EVENT_MASK */
277             XCB_EVENT_MASK_VISIBILITY_CHANGE,
278             /* XCB_CW_COLORMAP */
279             cmap,
280         };
281         xcb_void_cookie_t c;
282
283         xcb_create_pixmap (conn, sys->depth, pixmap, scr->root, 1, 1);
284         c = xcb_create_window_checked (conn, sys->depth, sys->window,
285                                        sys->embed->handle.xid, 0, 0,
286                                        width, height, 0,
287                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
288                                        vid, mask, values);
289         xcb_map_window (conn, sys->window);
290         /* Create graphic context (I wonder why the heck do we need this) */
291         xcb_create_gc (conn, sys->gc, sys->window, 0, NULL);
292
293         if (CheckError (vd, conn, "cannot create X11 window", c))
294             goto error;
295     }
296     msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
297     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, sys->gc);
298
299     sys->cursor = CreateBlankCursor (conn, scr);
300     sys->visible = false;
301     sys->shm = CheckSHM (obj, conn);
302
303
304     /* Setup vout_display_t once everything is fine */
305     vd->info.has_pictures_invalid = true;
306     vd->info.has_event_thread = true;
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 (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 *sys = vd->sys;
336
337     ResetPictures (vd);
338
339     /* show the default cursor */
340     xcb_change_window_attributes (sys->conn, sys->embed->handle.xid, XCB_CW_CURSOR,
341                                   &(uint32_t) { XCB_CURSOR_NONE });
342     xcb_flush (sys->conn);
343
344     /* colormap, window and context are garbage-collected by X */
345     xcb_disconnect (sys->conn);
346     vout_display_DeleteWindow (vd, sys->embed);
347     free (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 *sys = vd->sys;
356     (void)requested_count;
357
358     if (sys->pool)
359         return sys->pool;
360
361     vout_display_place_t place;
362
363     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
364
365     /* */
366     const uint32_t values[] = { place.x, place.y, place.width, place.height };
367     xcb_configure_window (sys->conn, sys->window,
368                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
369                           XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
370                           values);
371
372     picture_t *pic = picture_NewFromFormat (&vd->fmt);
373     if (!pic)
374         return NULL;
375
376     assert (pic->i_planes == 1);
377     memset (sys->resource, 0, sizeof(sys->resource));
378
379     unsigned count;
380     picture_t *pic_array[MAX_PICTURES];
381     for (count = 0; count < MAX_PICTURES; count++)
382     {
383         picture_resource_t *res = &sys->resource[count];
384
385         res->p->i_lines = pic->p->i_lines;
386         res->p->i_pitch = pic->p->i_pitch;
387         if (PictureResourceAlloc (vd, res, res->p->i_pitch * res->p->i_lines,
388                                   sys->conn, sys->shm))
389             break;
390         pic_array[count] = picture_NewFromResource (&vd->fmt, res);
391         if (!pic_array[count])
392         {
393             PictureResourceFree (res, sys->conn);
394             memset (res, 0, sizeof(*res));
395             break;
396         }
397     }
398     picture_Release (pic);
399
400     if (count == 0)
401         return NULL;
402
403     sys->pool = picture_pool_New (count, pic_array);
404     /* TODO release picture resources if NULL */
405     xcb_flush (sys->conn);
406     return sys->pool;
407 }
408
409 /**
410  * Sends an image to the X server.
411  */
412 static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
413 {
414     vout_display_sys_t *sys = vd->sys;
415     xcb_shm_seg_t segment = pic->p_sys->segment;
416     xcb_void_cookie_t ck;
417
418     if (!sys->visible)
419         goto out;
420     if (segment != 0)
421         ck = xcb_shm_put_image_checked (sys->conn, sys->window, sys->gc,
422           /* real width */ pic->p->i_pitch / pic->p->i_pixel_pitch,
423          /* real height */ pic->p->i_lines,
424                    /* x */ vd->fmt.i_x_offset,
425                    /* y */ vd->fmt.i_y_offset,
426                /* width */ vd->fmt.i_visible_width,
427               /* height */ vd->fmt.i_visible_height,
428                            0, 0, sys->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
429                            0, segment, 0);
430     else
431     {
432         const size_t offset = vd->fmt.i_y_offset * pic->p->i_pitch;
433         const unsigned lines = pic->p->i_lines - vd->fmt.i_y_offset;
434
435         ck = xcb_put_image_checked (sys->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
436                        sys->window, sys->gc,
437                        pic->p->i_pitch / pic->p->i_pixel_pitch,
438                        lines, -vd->fmt.i_x_offset, 0, 0, sys->depth,
439                        pic->p->i_pitch * lines, pic->p->p_pixels + offset);
440     }
441
442     /* Wait for reply. This makes sure that the X server gets CPU time to
443      * display the picture. xcb_flush() is *not* sufficient: especially with
444      * shared memory the PUT requests are so short that many of them can fit in
445      * X11 socket output buffer before the kernel preempts VLC. */
446     xcb_generic_error_t *e = xcb_request_check (sys->conn, ck);
447     if (e != NULL)
448     {
449         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
450         free (e);
451     }
452
453     /* FIXME might be WAY better to wait in some case (be carefull with
454      * VOUT_DISPLAY_RESET_PICTURES if done) + does not work with
455      * vout_display wrapper. */
456 out:
457     picture_Release (pic);
458     (void)subpicture;
459 }
460
461 static int Control (vout_display_t *vd, int query, va_list ap)
462 {
463     vout_display_sys_t *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 (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 (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 (sys->conn, 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 (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 (sys->conn, sys->embed->handle.xid,
537                                   XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
538         xcb_flush (sys->conn);
539         return VLC_SUCCESS;
540
541     default:
542         msg_Err (vd, "Unknown request in XCB vout display");
543         return VLC_EGENERIC;
544     }
545 }
546
547 static void Manage (vout_display_t *vd)
548 {
549     vout_display_sys_t *sys = vd->sys;
550
551     ManageEvent (vd, sys->conn, &sys->visible);
552 }
553
554 static void ResetPictures (vout_display_t *vd)
555 {
556     vout_display_sys_t *sys = vd->sys;
557
558     if (!sys->pool)
559         return;
560
561     for (unsigned i = 0; i < MAX_PICTURES; i++)
562     {
563         picture_resource_t *res = &sys->resource[i];
564
565         if (!res->p->p_pixels)
566             break;
567         PictureResourceFree (res, sys->conn);
568     }
569     picture_pool_Delete (sys->pool);
570     sys->pool = NULL;
571 }