]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/xvideo.c
XCB/XVideo: minor simplification
[vlc] / modules / video_output / xcb / xvideo.c
1 /**
2  * @file xvideo.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 #include <xcb/xv.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
38 #include <vlc_dialog.h>
39
40 #include "xcb_vlc.h"
41
42 #define ADAPTOR_TEXT N_("XVideo adaptor number")
43 #define ADAPTOR_LONGTEXT N_( \
44     "XVideo hardware adaptor to use. By default, VLC will " \
45     "use the first functional adaptor.")
46
47 #define SHM_TEXT N_("Use shared memory")
48 #define SHM_LONGTEXT N_( \
49     "Use shared memory to communicate between VLC and the X server.")
50
51 static int  Open (vlc_object_t *);
52 static void Close (vlc_object_t *);
53
54 /*
55  * Module descriptor
56  */
57 vlc_module_begin ()
58     set_shortname (N_("XVideo"))
59     set_description (N_("XVideo output (XCB)"))
60     set_category (CAT_VIDEO)
61     set_subcategory (SUBCAT_VIDEO_VOUT)
62     set_capability ("vout display", 155)
63     set_callbacks (Open, Close)
64
65     add_integer ("xvideo-adaptor", -1, NULL,
66                  ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
67     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
68         add_deprecated_alias ("xvideo-shm")
69     add_shortcut ("xcb-xv", "xv", "xvideo", "xid")
70 vlc_module_end ()
71
72 #define MAX_PICTURES (128)
73
74 struct vout_display_sys_t
75 {
76     xcb_connection_t *conn;
77     vout_window_t *embed;/* VLC window */
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     xcb_xv_port_t port;  /* XVideo port */
83     uint32_t id;         /* XVideo format */
84     uint16_t width;      /* display width */
85     uint16_t height;     /* display height */
86     uint32_t data_size;  /* picture byte size (for non-SHM) */
87     bool     swap_uv;    /* U/V pointer must be swapped in a picture */
88     bool shm;            /* whether to use MIT-SHM */
89     bool visible;        /* whether it makes sense to draw at all */
90
91     xcb_xv_query_image_attributes_reply_t *att;
92     picture_pool_t *pool; /* picture pool */
93     picture_resource_t resource[MAX_PICTURES];
94 };
95
96 static picture_pool_t *Pool (vout_display_t *, unsigned);
97 static void Display (vout_display_t *, picture_t *);
98 static int Control (vout_display_t *, int, va_list);
99 static void Manage (vout_display_t *);
100
101 /**
102  * Check that the X server supports the XVideo extension.
103  */
104 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
105 {
106     xcb_xv_query_extension_reply_t *r;
107     xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
108     bool ok = false;
109
110     /* We need XVideo 2.2 for PutImage */
111     r = xcb_xv_query_extension_reply (conn, ck, NULL);
112     if (r == NULL)
113         msg_Dbg (vd, "XVideo extension not available");
114     else
115     if (r->major != 2)
116         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
117                  r->major, r->minor);
118     else
119     if (r->minor < 2)
120         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
121                  r->major, r->minor);
122     else
123     {
124         msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
125                  r->major, r->minor);
126         ok = true;
127     }
128     free (r);
129     return ok;
130 }
131
132 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
133                                  const xcb_xv_image_format_info_t *restrict f)
134 {
135     if (f->byte_order != ORDER && f->bpp != 8)
136         return 0; /* Argh! */
137
138     switch (f->type)
139     {
140       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
141         switch (f->num_planes)
142         {
143           case 1:
144             switch (f->bpp)
145             {
146               case 32:
147                 if (f->depth == 24)
148                     return VLC_CODEC_RGB32;
149                 if (f->depth == 32)
150                     return 0; /* ARGB -> VLC cannot do that currently */
151                 break;
152               case 24:
153                 if (f->depth == 24)
154                     return VLC_CODEC_RGB24;
155                 break;
156               case 16:
157                 if (f->depth == 16)
158                     return VLC_CODEC_RGB16;
159                 if (f->depth == 15)
160                     return VLC_CODEC_RGB15;
161                 break;
162               case 8:
163                 if (f->depth == 8)
164                     return VLC_CODEC_RGB8;
165                 break;
166             }
167             break;
168         }
169         msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
170                  f->id, f->guid);
171         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
172                  "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
173         break;
174
175       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
176         if (f->u_sample_bits != f->v_sample_bits
177          || f->vhorz_u_period != f->vhorz_v_period
178          || f->vvert_u_period != f->vvert_v_period
179          || f->y_sample_bits != 8 || f->u_sample_bits != 8
180          || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
181             goto bad;
182         switch (f->num_planes)
183         {
184           case 1:
185             switch (f->bpp)
186             {
187               /*untested: case 24:
188                 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
189                     return VLC_CODEC_I444;
190                 break;*/
191               case 16:
192                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
193                 {
194                     if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
195                         return VLC_CODEC_YUYV;
196                     if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
197                         return VLC_CODEC_UYVY;
198                 }
199                 break;
200             }
201             break;
202           case 3:
203             switch (f->bpp)
204             {
205               case 12:
206                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
207                 {
208                     if (!strcmp ((const char *)f->vcomp_order, "YVU"))
209                         return VLC_CODEC_YV12;
210                     if (!strcmp ((const char *)f->vcomp_order, "YUV"))
211                         return VLC_CODEC_I420;
212                 }
213             }
214             break;
215         }
216     bad:
217         msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
218                  f->guid);
219         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
220                  "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
221                  f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
222         msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
223                  "%"PRIu32"/%"PRIu32"/%"PRIu32,
224                  f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
225                  f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
226         msg_Warn (vd, " order: %.32s", f->vcomp_order);
227         break;
228     }
229     return 0;
230 }
231
232
233 static const xcb_xv_image_format_info_t *
234 FindFormat (vout_display_t *vd,
235             vlc_fourcc_t chroma, const video_format_t *fmt,
236             xcb_xv_port_t port,
237             const xcb_xv_list_image_formats_reply_t *list,
238             xcb_xv_query_image_attributes_reply_t **restrict pa)
239 {
240     xcb_connection_t *conn = vd->sys->conn;
241     const xcb_xv_image_format_info_t *f, *end;
242
243 #ifndef XCB_XV_OLD
244     f = xcb_xv_list_image_formats_format (list);
245 #else
246     f = (xcb_xv_image_format_info_t *) (list + 1);
247 #endif
248     end = f + xcb_xv_list_image_formats_format_length (list);
249     for (; f < end; f++)
250     {
251         if (chroma != ParseFormat (vd, f))
252             continue;
253
254         /* VLC pads scanline to 16 pixels internally */
255         unsigned width = (fmt->i_width + 15) & ~15;
256         unsigned height = (fmt->i_height + 15) & ~15;
257         xcb_xv_query_image_attributes_reply_t *i;
258         i = xcb_xv_query_image_attributes_reply (conn,
259             xcb_xv_query_image_attributes (conn, port, f->id,
260                                            width, height), NULL);
261         if (i == NULL)
262             continue;
263
264         if (i->width != width || i->height != height)
265         {
266             msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
267                       fmt->i_width, fmt->i_height,
268                       i->width, i->height);
269             var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
270             if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
271             {
272                 dialog_FatalWait (vd, _("Video acceleration not available"),
273                     _("Your video output acceleration driver does not support "
274                       "the required resolution: %ux%u pixels. The maximum "
275                       "supported resolution is %"PRIu32"x%"PRIu32".\n"
276                       "Video output acceleration will be disabled. However, "
277                       "rendering videos with overly large resolution "
278                       "may cause severe performance degration."),
279                                   width, height, i->width, i->height);
280                 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
281             }
282             free (i);
283             continue;
284         }
285         *pa = i;
286         return f;
287     }
288     return NULL;
289 }
290
291
292 /**
293  * Probe the X server.
294  */
295 static int Open (vlc_object_t *obj)
296 {
297     vout_display_t *vd = (vout_display_t *)obj;
298     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
299
300     if (!var_InheritBool (obj, "overlay"))
301         return VLC_EGENERIC;
302     if (p_sys == NULL)
303         return VLC_ENOMEM;
304
305     vd->sys = p_sys;
306
307     /* Connect to X */
308     xcb_connection_t *conn;
309     const xcb_screen_t *screen;
310     uint8_t depth;
311     p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
312     if (p_sys->embed == NULL)
313     {
314         free (p_sys);
315         return VLC_EGENERIC;
316     }
317
318     p_sys->conn = conn;
319     p_sys->att = NULL;
320     p_sys->pool = NULL;
321     p_sys->swap_uv = false;
322
323     if (!CheckXVideo (vd, conn))
324     {
325         msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
326         goto error;
327     }
328
329     p_sys->window = xcb_generate_id (conn);
330     xcb_pixmap_t pixmap = xcb_generate_id (conn);
331
332     /* Cache adaptors infos */
333     xcb_xv_query_adaptors_reply_t *adaptors =
334         xcb_xv_query_adaptors_reply (conn,
335             xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
336     if (adaptors == NULL)
337         goto error;
338
339     int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
340
341     /* */
342     video_format_t fmt = vd->fmt;
343     p_sys->port = 0;
344
345     xcb_xv_adaptor_info_iterator_t it;
346     for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
347          it.rem > 0;
348          xcb_xv_adaptor_info_next (&it))
349     {
350         const xcb_xv_adaptor_info_t *a = it.data;
351         char *name;
352
353         if (forced_adaptor != -1 && forced_adaptor != 0)
354         {
355             forced_adaptor--;
356             continue;
357         }
358
359         if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
360             continue;
361
362         xcb_xv_list_image_formats_reply_t *r =
363             xcb_xv_list_image_formats_reply (conn,
364                 xcb_xv_list_image_formats (conn, a->base_id), NULL);
365         if (r == NULL)
366             continue;
367
368         /* Look for an image format */
369         const xcb_xv_image_format_info_t *xfmt = NULL;
370         const vlc_fourcc_t *chromas, chromas_default[] = {
371             fmt.i_chroma,
372             VLC_CODEC_RGB32,
373             VLC_CODEC_RGB24,
374             VLC_CODEC_RGB16,
375             VLC_CODEC_RGB15,
376             VLC_CODEC_YUYV,
377             0
378         };
379         if (vlc_fourcc_IsYUV (fmt.i_chroma))
380             chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
381         else
382             chromas = chromas_default;
383
384         vlc_fourcc_t chroma;
385         for (size_t i = 0; chromas[i]; i++)
386         {
387             chroma = chromas[i];
388
389             /* Oink oink! */
390             if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
391              && a->name_size >= 4
392              && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
393             {
394                 msg_Dbg (vd, "skipping slow I420 format");
395                 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
396             }
397
398             xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
399             if (xfmt != NULL)
400             {
401                 p_sys->id = xfmt->id;
402                 p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
403                                                                 chroma);
404                 if (!p_sys->swap_uv)
405                     fmt.i_chroma = chroma;
406                 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
407                 {
408                     fmt.i_rmask = xfmt->red_mask;
409                     fmt.i_gmask = xfmt->green_mask;
410                     fmt.i_bmask = xfmt->blue_mask;
411                 }
412                 break;
413             }
414         }
415         free (r);
416         if (xfmt == NULL) /* No acceptable image formats */
417             continue;
418
419         /* Grab a port */
420         for (unsigned i = 0; i < a->num_ports; i++)
421         {
422              xcb_xv_port_t port = a->base_id + i;
423              xcb_xv_grab_port_reply_t *gr =
424                  xcb_xv_grab_port_reply (conn,
425                      xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
426              uint8_t result = gr ? gr->result : 0xff;
427
428              free (gr);
429              if (result == 0)
430              {
431                  p_sys->port = port;
432                  goto grabbed_port;
433              }
434              msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
435                       result);
436         }
437         continue; /* No usable port */
438
439     grabbed_port:
440         /* Found port - initialize selected format */
441         name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
442         if (name != NULL)
443         {
444             msg_Dbg (vd, "using adaptor %s", name);
445             free (name);
446         }
447         msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
448         msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
449
450         /* Look for an X11 visual, create a window */
451         xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
452         for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
453         {
454             if (f->depth != screen->root_depth)
455                 continue; /* this would fail anyway */
456
457             uint32_t mask =
458                 XCB_CW_BACK_PIXMAP |
459                 XCB_CW_BACK_PIXEL |
460                 XCB_CW_BORDER_PIXMAP |
461                 XCB_CW_BORDER_PIXEL |
462                 XCB_CW_EVENT_MASK |
463                 XCB_CW_COLORMAP;
464             const uint32_t list[] = {
465                 /* XCB_CW_BACK_PIXMAP */
466                 pixmap,
467                 /* XCB_CW_BACK_PIXEL */
468                 screen->black_pixel,
469                 /* XCB_CW_BORDER_PIXMAP */
470                 pixmap,
471                 /* XCB_CW_BORDER_PIXEL */
472                 screen->black_pixel,
473                 /* XCB_CW_EVENT_MASK */
474                 XCB_EVENT_MASK_VISIBILITY_CHANGE,
475                 /* XCB_CW_COLORMAP */
476                 screen->default_colormap,
477             };
478
479             xcb_void_cookie_t c;
480
481             xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
482             c = xcb_create_window_checked (conn, f->depth, p_sys->window,
483                  p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
484                  XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
485
486             if (!CheckError (vd, conn, "cannot create X11 window", c))
487             {
488                 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
489                          " (depth: %"PRIu8")", f->visual, f->depth);
490                 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
491                 goto created_window;
492             }
493         }
494         xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
495         p_sys->port = 0;
496         msg_Dbg (vd, "no usable X11 visual");
497         continue; /* No workable XVideo format (visual/depth) */
498
499     created_window:
500         break;
501     }
502     free (adaptors);
503     if (!p_sys->port)
504     {
505         msg_Err (vd, "no available XVideo adaptor");
506         goto error;
507     }
508     else
509     {
510         xcb_map_window (conn, p_sys->window);
511
512         vout_display_place_t place;
513
514         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
515         p_sys->width  = place.width;
516         p_sys->height = place.height;
517
518         /* */
519         const uint32_t values[] = {
520             place.x, place.y, place.width, place.height };
521         xcb_configure_window (conn, p_sys->window,
522                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
523                               XCB_CONFIG_WINDOW_WIDTH |
524                               XCB_CONFIG_WINDOW_HEIGHT,
525                               values);
526     }
527     p_sys->visible = false;
528
529     /* Create graphic context */
530     p_sys->gc = xcb_generate_id (conn);
531     xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
532     msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
533
534     /* Create cursor */
535     p_sys->cursor = CreateBlankCursor (conn, screen);
536
537     CheckSHM (obj, conn, &p_sys->shm);
538
539     /* */
540     vout_display_info_t info = vd->info;
541     info.has_pictures_invalid = false;
542     info.has_event_thread = true;
543
544     /* Setup vout_display_t once everything is fine */
545     vd->fmt = fmt;
546     vd->info = info;
547
548     vd->pool = Pool;
549     vd->prepare = NULL;
550     vd->display = Display;
551     vd->control = Control;
552     vd->manage = Manage;
553
554     /* */
555     bool is_fullscreen = vd->cfg->is_fullscreen;
556     if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
557         is_fullscreen = false;
558     vout_display_SendEventFullscreen (vd, is_fullscreen);
559     unsigned width, height;
560     if (!GetWindowSize (p_sys->embed, conn, &width, &height))
561         vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
562
563     return VLC_SUCCESS;
564
565 error:
566     Close (obj);
567     return VLC_EGENERIC;
568 }
569
570
571 /**
572  * Disconnect from the X server.
573  */
574 static void Close (vlc_object_t *obj)
575 {
576     vout_display_t *vd = (vout_display_t *)obj;
577     vout_display_sys_t *p_sys = vd->sys;
578
579     if (p_sys->pool)
580     {
581         for (unsigned i = 0; i < MAX_PICTURES; i++)
582         {
583             picture_resource_t *res = &p_sys->resource[i];
584
585             if (!res->p->p_pixels)
586                 break;
587             PictureResourceFree (res, NULL);
588         }
589         picture_pool_Delete (p_sys->pool);
590     }
591
592     /* show the default cursor */
593     xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
594                                   &(uint32_t) { XCB_CURSOR_NONE });
595     xcb_flush (p_sys->conn);
596
597     free (p_sys->att);
598     xcb_disconnect (p_sys->conn);
599     vout_display_DeleteWindow (vd, p_sys->embed);
600     free (p_sys);
601 }
602
603 /**
604  * Return a direct buffer
605  */
606 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
607 {
608     vout_display_sys_t *p_sys = vd->sys;
609
610     if (!p_sys->pool)
611     {
612         memset (p_sys->resource, 0, sizeof(p_sys->resource));
613
614         const uint32_t *pitches =
615             xcb_xv_query_image_attributes_pitches (p_sys->att);
616         const uint32_t *offsets =
617             xcb_xv_query_image_attributes_offsets (p_sys->att);
618         p_sys->data_size = p_sys->att->data_size;
619
620         unsigned count;
621         picture_t *pic_array[MAX_PICTURES];
622         for (count = 0; count < requested_count; count++)
623         {
624             if (count >= MAX_PICTURES)
625                 break;
626             picture_resource_t *res = &p_sys->resource[count];
627
628             for (int i = 0; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
629             {
630                 res->p[i].i_lines =
631                     ((i + 1 < p_sys->att->num_planes ? offsets[i+1] :
632                                                        p_sys->data_size) - offsets[i]) / pitches[i];
633                 res->p[i].i_pitch = pitches[i];
634             }
635             if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
636                                       p_sys->conn, p_sys->shm))
637                 break;
638
639             /* Allocate further planes as specified by XVideo */
640             /* We assume that offsets[0] is zero */
641             for (int i = 1; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
642                 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
643             if (p_sys->swap_uv)
644             {   /* YVU: swap U and V planes */
645                 uint8_t *buf = res->p[2].p_pixels;
646                 res->p[2].p_pixels = res->p[1].p_pixels;
647                 res->p[1].p_pixels = buf;
648             }
649
650             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
651             if (!pic_array[count])
652             {
653                 PictureResourceFree (res, p_sys->conn);
654                 memset (res, 0, sizeof(*res));
655                 break;
656             }
657         }
658
659         if (count == 0)
660             return NULL;
661
662         p_sys->pool = picture_pool_New (count, pic_array);
663         /* TODO release picture resources if NULL */
664         xcb_flush (p_sys->conn);
665     }
666
667     return p_sys->pool;
668 }
669
670 /**
671  * Sends an image to the X server.
672  */
673 static void Display (vout_display_t *vd, picture_t *pic)
674 {
675     vout_display_sys_t *p_sys = vd->sys;
676     xcb_shm_seg_t segment = pic->p_sys->segment;
677     xcb_void_cookie_t ck;
678
679     if (!p_sys->visible)
680         goto out;
681     if (segment)
682         ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
683                               p_sys->window, p_sys->gc, segment, p_sys->id, 0,
684                    /* Src: */ vd->source.i_x_offset,
685                               vd->source.i_y_offset,
686                               vd->source.i_visible_width,
687                               vd->source.i_visible_height,
688                    /* Dst: */ 0, 0, p_sys->width, p_sys->height,
689                 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
690                               pic->p->i_lines, false);
691     else
692         ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
693                           p_sys->gc, p_sys->id,
694                           vd->source.i_x_offset,
695                           vd->source.i_y_offset,
696                           vd->source.i_visible_width,
697                           vd->source.i_visible_height,
698                           0, 0, p_sys->width, p_sys->height,
699                           pic->p->i_pitch / pic->p->i_pixel_pitch,
700                           pic->p->i_lines,
701                           p_sys->data_size, pic->p->p_pixels);
702
703     /* Wait for reply. See x11.c for rationale. */
704     xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
705     if (e != NULL)
706     {
707         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
708         free (e);
709     }
710 out:
711     picture_Release (pic);
712 }
713
714 static int Control (vout_display_t *vd, int query, va_list ap)
715 {
716     vout_display_sys_t *p_sys = vd->sys;
717
718     switch (query)
719     {
720     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
721     {
722         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
723         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
724     }
725
726     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
727     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
728     case VOUT_DISPLAY_CHANGE_ZOOM:
729     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
730     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
731     {
732         const vout_display_cfg_t *cfg;
733         const video_format_t *source;
734         bool is_forced = false;
735
736         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
737          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
738         {
739             source = (const video_format_t *)va_arg (ap, const video_format_t *);
740             cfg = vd->cfg;
741         }
742         else
743         {
744             source = &vd->source;
745             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
746             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
747                 is_forced = (bool)va_arg (ap, int);
748         }
749
750         /* */
751         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
752          && is_forced
753          && (cfg->display.width  != vd->cfg->display.width
754            ||cfg->display.height != vd->cfg->display.height)
755          && vout_window_SetSize (p_sys->embed,
756                                   cfg->display.width,
757                                   cfg->display.height))
758             return VLC_EGENERIC;
759
760         vout_display_place_t place;
761         vout_display_PlacePicture (&place, source, cfg, false);
762         p_sys->width  = place.width;
763         p_sys->height = place.height;
764
765         /* Move the picture within the window */
766         const uint32_t values[] = { place.x, place.y,
767                                     place.width, place.height, };
768         xcb_configure_window (p_sys->conn, p_sys->window,
769                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
770                             | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
771                               values);
772         xcb_flush (p_sys->conn);
773         return VLC_SUCCESS;
774     }
775     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
776     {
777         unsigned state = va_arg (ap, unsigned);
778         return vout_window_SetState (p_sys->embed, state);
779     }
780
781     /* Hide the mouse. It will be send when
782      * vout_display_t::info.b_hide_mouse is false */
783     case VOUT_DISPLAY_HIDE_MOUSE:
784         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
785                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
786         return VLC_SUCCESS;
787     case VOUT_DISPLAY_RESET_PICTURES:
788         assert(0);
789     default:
790         msg_Err (vd, "Unknown request in XCB vout display");
791         return VLC_EGENERIC;
792     }
793 }
794
795 static void Manage (vout_display_t *vd)
796 {
797     vout_display_sys_t *p_sys = vd->sys;
798
799     ManageEvent (vd, p_sys->conn, &p_sys->visible);
800 }
801