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