xvideo.c 25.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
/**
 * @file xvideo.c
 * @brief X C Bindings video output module for VLC media player
 */
/*****************************************************************************
 * Copyright © 2009 Rémi Denis-Courmont
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
12
13
14
15
16
17
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
18
 * You should have received a copy of the GNU General Public
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <assert.h>

#include <xcb/xcb.h>
#include <xcb/shm.h>
#include <xcb/xv.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
36
37
#include <vlc_vout_display.h>
#include <vlc_picture_pool.h>
38
#include <vlc_dialog.h>
39
40
41

#include "xcb_vlc.h"

42
43
44
45
46
#define ADAPTOR_TEXT N_("XVideo adaptor number")
#define ADAPTOR_LONGTEXT N_( \
    "XVideo hardware adaptor to use. By default, VLC will " \
    "use the first functional adaptor.")

47
48
49
50
51
52
53
54
55
56
57
58
#define SHM_TEXT N_("Use shared memory")
#define SHM_LONGTEXT N_( \
    "Use shared memory to communicate between VLC and the X server.")

static int  Open (vlc_object_t *);
static void Close (vlc_object_t *);

/*
 * Module descriptor
 */
vlc_module_begin ()
    set_shortname (N_("XVideo"))
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
59
    set_description (N_("XVideo output (XCB)"))
60
61
    set_category (CAT_VIDEO)
    set_subcategory (SUBCAT_VIDEO_VOUT)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
    set_capability ("vout display", 155)
63
64
    set_callbacks (Open, Close)

65
66
    add_integer ("xvideo-adaptor", -1, NULL,
                 ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
67
    add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
68
        add_deprecated_alias ("xvideo-shm")
69
    add_shortcut ("xcb-xv")
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
70
    add_shortcut ("xv")
71
    add_shortcut ("xvideo")
72
73
vlc_module_end ()

74
75
76
#define MAX_PICTURES (VOUT_MAX_PICTURES)

struct vout_display_sys_t
77
78
79
80
{
    xcb_connection_t *conn;
    vout_window_t *embed;/* VLC window */

81
    xcb_cursor_t cursor; /* blank cursor */
82
83
84
85
86
87
    xcb_window_t window; /* drawable X window */
    xcb_gcontext_t gc;   /* context to put images */
    xcb_xv_port_t port;  /* XVideo port */
    uint32_t id;         /* XVideo format */
    uint16_t width;      /* display width */
    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
94
95
96
    xcb_xv_query_image_attributes_reply_t *att;
    picture_pool_t *pool; /* picture pool */
    picture_resource_t resource[MAX_PICTURES];
};
97

98
static picture_pool_t *Pool (vout_display_t *, unsigned);
99
100
101
static void Display (vout_display_t *, picture_t *);
static int Control (vout_display_t *, int, va_list);
static void Manage (vout_display_t *);
102
103
104
105

/**
 * Check that the X server supports the XVideo extension.
 */
106
static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
107
108
109
110
111
{
    xcb_xv_query_extension_reply_t *r;
    xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
    bool ok = false;

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    else
    if (r->major != 2)
        msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
                 r->major, r->minor);
    else
    if (r->minor < 2)
        msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
                 r->major, r->minor);
    else
    {
        msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
                 r->major, r->minor);
        ok = true;
    }
    free (r);
131
132
133
    return ok;
}

134
static vlc_fourcc_t ParseFormat (vout_display_t *vd,
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
                                 const xcb_xv_image_format_info_t *restrict f)
{
    if (f->byte_order != ORDER && f->bpp != 8)
        return 0; /* Argh! */

    switch (f->type)
    {
      case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
        switch (f->num_planes)
        {
          case 1:
            switch (f->bpp)
            {
              case 32:
                if (f->depth == 24)
150
                    return VLC_CODEC_RGB32;
151
152
                if (f->depth == 32)
                    return 0; /* ARGB -> VLC cannot do that currently */
153
154
155
                break;
              case 24:
                if (f->depth == 24)
156
                    return VLC_CODEC_RGB24;
157
158
159
                break;
              case 16:
                if (f->depth == 16)
160
                    return VLC_CODEC_RGB16;
161
                if (f->depth == 15)
162
                    return VLC_CODEC_RGB15;
163
164
165
                break;
              case 8:
                if (f->depth == 8)
166
                    return VLC_CODEC_RGB8;
167
168
169
170
                break;
            }
            break;
        }
171
        msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
172
                 f->id, f->guid);
173
        msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
                 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
        break;

      case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
        if (f->u_sample_bits != f->v_sample_bits
         || f->vhorz_u_period != f->vhorz_v_period
         || f->vvert_u_period != f->vvert_v_period
         || f->y_sample_bits != 8 || f->u_sample_bits != 8
         || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
            goto bad;
        switch (f->num_planes)
        {
          case 1:
            switch (f->bpp)
            {
              /*untested: case 24:
                if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
191
                    return VLC_CODEC_I444;
192
193
194
195
196
                break;*/
              case 16:
                if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
                {
                    if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
197
                        return VLC_CODEC_YUYV;
198
                    if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
199
                        return VLC_CODEC_UYVY;
200
201
202
203
204
205
206
207
208
209
210
                }
                break;
            }
            break;
          case 3:
            switch (f->bpp)
            {
              case 12:
                if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
                {
                    if (!strcmp ((const char *)f->vcomp_order, "YVU"))
211
                        return VLC_CODEC_YV12;
212
                    if (!strcmp ((const char *)f->vcomp_order, "YUV"))
213
                        return VLC_CODEC_I420;
214
215
216
217
218
                }
            }
            break;
        }
    bad:
219
        msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
220
                 f->guid);
221
        msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
222
223
                 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
                 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
224
        msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
225
226
227
                 "%"PRIu32"/%"PRIu32"/%"PRIu32,
                 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
                 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
228
        msg_Warn (vd, " order: %.32s", f->vcomp_order);
229
230
231
232
233
234
        break;
    }
    return 0;
}


235
static const xcb_xv_image_format_info_t *
236
237
238
FindFormat (vout_display_t *vd,
            vlc_fourcc_t chroma, const video_format_t *fmt,
            xcb_xv_port_t port,
239
240
241
            const xcb_xv_list_image_formats_reply_t *list,
            xcb_xv_query_image_attributes_reply_t **restrict pa)
{
242
    xcb_connection_t *conn = vd->sys->conn;
243
244
    const xcb_xv_image_format_info_t *f, *end;

245
#ifndef XCB_XV_OLD
246
    f = xcb_xv_list_image_formats_format (list);
247
248
249
#else
    f = (xcb_xv_image_format_info_t *) (list + 1);
#endif
250
251
252
    end = f + xcb_xv_list_image_formats_format_length (list);
    for (; f < end; f++)
    {
253
        if (chroma != ParseFormat (vd, f))
254
255
            continue;

256
257
        /* VLC pads scanline to 16 pixels internally */
        unsigned width = (fmt->i_width + 15) & ~15;
258
        unsigned height = (fmt->i_height + 15) & ~15;
259
260
261
        xcb_xv_query_image_attributes_reply_t *i;
        i = xcb_xv_query_image_attributes_reply (conn,
            xcb_xv_query_image_attributes (conn, port, f->id,
262
                                           width, height), NULL);
263
264
265
        if (i == NULL)
            continue;

266
        if (i->width != width || i->height != height)
267
        {
268
269
            msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
                      fmt->i_width, fmt->i_height,
270
                      i->width, i->height);
271
272
273
274
275
276
277
278
279
280
281
282
283
            var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
            if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
            {
                dialog_FatalWait (vd, _("Video acceleration not available"),
                    _("Your video output acceleration driver does not support "
                      "the required resolution: %ux%u pixels. The maximum "
                      "supported resolution is %"PRIu32"x%"PRIu32".\n"
                      "Video output acceleration will be disabled. However, "
                      "rendering videos with overly large resolution "
                      "may cause severe performance degration."),
                                  width, height, i->width, i->height);
                var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
            }
284
285
286
287
288
289
290
291
292
            free (i);
            continue;
        }
        *pa = i;
        return f;
    }
    return NULL;
}

293
294
295
296
297
298
299
300

/**
 * Probe the X server.
 */
static int Open (vlc_object_t *obj)
{
    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
301
302
303

    if (!var_CreateGetBool (obj, "overlay"))
        return VLC_EGENERIC;
304
305
306
307
308
309
    if (p_sys == NULL)
        return VLC_ENOMEM;

    vd->sys = p_sys;

    /* Connect to X */
310
311
312
    xcb_connection_t *conn;
    const xcb_screen_t *screen;
    uint8_t depth;
313
    p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
314
    if (p_sys->embed == NULL)
315
316
317
318
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
319

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
320
    p_sys->conn = conn;
321
322
    p_sys->att = NULL;
    p_sys->pool = NULL;
323
    p_sys->swap_uv = false;
324

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
325
    if (!CheckXVideo (vd, conn))
326
327
    {
        msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
328
        goto error;
329
330
    }

331
    p_sys->window = xcb_generate_id (conn);
332

333
    /* Cache adaptors infos */
334
335
    xcb_xv_query_adaptors_reply_t *adaptors =
        xcb_xv_query_adaptors_reply (conn,
336
            xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
337
    if (adaptors == NULL)
338
339
        goto error;

340
341
    int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");

342
343
344
    /* */
    video_format_t fmt = vd->fmt;
    bool found_adaptor = false;
345
346

    xcb_xv_adaptor_info_iterator_t it;
347
    for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
348
         it.rem > 0 && !found_adaptor;
349
350
351
         xcb_xv_adaptor_info_next (&it))
    {
        const xcb_xv_adaptor_info_t *a = it.data;
352
        char *name;
353

354
355
356
357
358
359
        if (forced_adaptor != -1 && forced_adaptor != 0)
        {
            forced_adaptor--;
            continue;
        }

360
361
362
        if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
            continue;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
363
364
365
        xcb_xv_list_image_formats_reply_t *r =
            xcb_xv_list_image_formats_reply (conn,
                xcb_xv_list_image_formats (conn, a->base_id), NULL);
366
367
368
        if (r == NULL)
            continue;

369
370
        /* Look for an image format */
        const xcb_xv_image_format_info_t *xfmt = NULL;
371
        const vlc_fourcc_t *chromas, chromas_default[] = {
372
            fmt.i_chroma,
373
            VLC_CODEC_RGB32,
374
            VLC_CODEC_RGB24,
375
            VLC_CODEC_RGB16,
376
            VLC_CODEC_RGB15,
377
            VLC_CODEC_YUYV,
Laurent Aimar's avatar
Laurent Aimar committed
378
            0
379
        };
Laurent Aimar's avatar
Laurent Aimar committed
380
381
382
383
384
        if (vlc_fourcc_IsYUV (fmt.i_chroma))
            chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
        else
            chromas = chromas_default;

385
        vlc_fourcc_t chroma;
386
        for (size_t i = 0; chromas[i]; i++)
387
        {
388
            chroma = chromas[i];
389
390
391
392
393
394
395
396
397
398

            /* Oink oink! */
            if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
             && a->name_size >= 4
             && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
            {
                msg_Dbg (vd, "skipping slow I420 format");
                continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
            }

399
            xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
400
401
402
            if (xfmt != NULL)
            {
                p_sys->id = xfmt->id;
403
404
405
406
                p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
                                                                chroma);
                if (!p_sys->swap_uv)
                    fmt.i_chroma = chroma;
407
408
409
410
411
412
413
414
                if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
                {
                    fmt.i_rmask = xfmt->red_mask;
                    fmt.i_gmask = xfmt->green_mask;
                    fmt.i_bmask = xfmt->blue_mask;
                }
                break;
            }
415
        }
416
        free (r);
417
        if (xfmt == NULL) /* No acceptable image formats */
418
            continue;
419

420
421
422
423
424
        /* Grab a port */
        for (unsigned i = 0; i < a->num_ports; i++)
        {
             xcb_xv_port_t port = a->base_id + i;
             xcb_xv_grab_port_reply_t *gr =
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
425
426
                 xcb_xv_grab_port_reply (conn,
                     xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
427
428
429
430
431
432
             uint8_t result = gr ? gr->result : 0xff;

             free (gr);
             if (result == 0)
             {
                 p_sys->port = port;
433
                 goto grabbed_port;
434
435
436
             }
             msg_Dbg (vd, "cannot grab port %"PRIu32, port);
        }
437
        continue; /* No usable port */
438

439
    grabbed_port:
440
        /* Found port - initialize selected format */
441
442
443
444
445
446
        name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
        if (name != NULL)
        {
            msg_Dbg (vd, "using adaptor %s", name);
            free (name);
        }
447
448
        msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
        msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
449
450
451
452
453

        /* Look for an X11 visual, create a window */
        xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
        for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
        {
454
455
456
            if (f->depth != depth)
                continue; /* this would fail anyway */

457
458
459
460
461
462
            const uint32_t mask =
                /* XCB_CW_EVENT_MASK */
                XCB_EVENT_MASK_VISIBILITY_CHANGE;
            xcb_void_cookie_t c;

            c = xcb_create_window_checked (conn, f->depth, p_sys->window,
463
                 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
464
465
466
467
468
469
470
471
472
473
474
475
                 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual,
                 XCB_CW_EVENT_MASK, &mask);

            if (!CheckError (vd, conn, "cannot create X11 window", c))
            {
                msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
                         " (depth: %"PRIu8")", f->visual, f->depth);
                msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
                goto created_window;
            }
        }
        xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
476
        msg_Dbg (vd, "no usable X11 visual");
477
478
479
480
481
        continue; /* No workable XVideo format (visual/depth) */

    created_window:
        found_adaptor = true;
        break;
482
    }
483
    free (adaptors);
484
485
486
487
488
    if (!found_adaptor)
    {
        msg_Err (vd, "no available XVideo adaptor");
        goto error;
    }
489
    else
490
    {
491
        xcb_map_window (conn, p_sys->window);
492

493
494
495
496
497
498
499
        vout_display_place_t place;

        vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
        p_sys->width  = place.width;
        p_sys->height = place.height;

        /* */
500
501
502
        const uint32_t values[] = {
            place.x, place.y, place.width, place.height };
        xcb_configure_window (conn, p_sys->window,
503
                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
504
505
                              XCB_CONFIG_WINDOW_WIDTH |
                              XCB_CONFIG_WINDOW_HEIGHT,
506
                              values);
507
    }
508
    p_sys->visible = false;
509

510
    /* Create graphic context */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
511
512
    p_sys->gc = xcb_generate_id (conn);
    xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
513
    msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
514

515
516
517
    /* Create cursor */
    p_sys->cursor = CreateBlankCursor (conn, screen);

518
    CheckSHM (obj, conn, &p_sys->shm);
519

520
521
522
    /* */
    vout_display_info_t info = vd->info;
    info.has_pictures_invalid = false;
523

524
525
526
    /* Setup vout_display_t once everything is fine */
    vd->fmt = fmt;
    vd->info = info;
527

528
    vd->pool = Pool;
529
530
531
532
    vd->prepare = NULL;
    vd->display = Display;
    vd->control = Control;
    vd->manage = Manage;
533

534
    /* */
535
    vout_display_SendEventFullscreen (vd, false);
536
    unsigned width, height;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
537
    if (!GetWindowSize (p_sys->embed, conn, &width, &height))
538
        vout_display_SendEventDisplaySize (vd, width, height, false);
539
540

    return VLC_SUCCESS;
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

error:
    Close (obj);
    return VLC_EGENERIC;
}


/**
 * Disconnect from the X server.
 */
static void Close (vlc_object_t *obj)
{
    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *p_sys = vd->sys;

    if (p_sys->pool)
    {
        for (unsigned i = 0; i < MAX_PICTURES; i++)
        {
            picture_resource_t *res = &p_sys->resource[i];

            if (!res->p->p_pixels)
                break;
564
            PictureResourceFree (res, NULL);
565
566
567
568
        }
        picture_pool_Delete (p_sys->pool);
    }

569
570
571
572
573
    /* show the default cursor */
    xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
                                  &(uint32_t) { XCB_CURSOR_NONE });
    xcb_flush (p_sys->conn);

574
575
    free (p_sys->att);
    xcb_disconnect (p_sys->conn);
576
    vout_display_DeleteWindow (vd, p_sys->embed);
577
    free (p_sys);
578
579
580
}

/**
581
 * Return a direct buffer
582
 */
583
static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
584
{
585
    vout_display_sys_t *p_sys = vd->sys;
586
    (void)requested_count;
587
588
589
590
591

    if (!p_sys->pool)
    {
        memset (p_sys->resource, 0, sizeof(p_sys->resource));

592
593
        const uint32_t *pitches =
            xcb_xv_query_image_attributes_pitches (p_sys->att);
594
595
596
597
598
599
600
601
602
603
        const uint32_t *offsets =
            xcb_xv_query_image_attributes_offsets (p_sys->att);
        p_sys->data_size = p_sys->att->data_size;

        unsigned count;
        picture_t *pic_array[MAX_PICTURES];
        for (count = 0; count < MAX_PICTURES; count++)
        {
            picture_resource_t *res = &p_sys->resource[count];

604
            for (int i = 0; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
605
            {
606
607
608
609
                res->p[i].i_lines =
                    ((i + 1 < p_sys->att->num_planes ? offsets[i+1] :
                                                       p_sys->data_size) - offsets[i]) / pitches[i];
                res->p[i].i_pitch = pitches[i];
610
611
612
613
614
615
616
            }
            if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
                                      p_sys->conn, p_sys->shm))
                break;

            /* Allocate further planes as specified by XVideo */
            /* We assume that offsets[0] is zero */
617
            for (int i = 1; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
618
                res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
619
            if (p_sys->swap_uv)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
620
621
622
623
624
625
            {   /* YVU: swap U and V planes */
                uint8_t *buf = res->p[2].p_pixels;
                res->p[2].p_pixels = res->p[1].p_pixels;
                res->p[1].p_pixels = buf;
            }

626
627
628
629
630
631
632
633
634
635
636
637
638
            pic_array[count] = picture_NewFromResource (&vd->fmt, res);
            if (!pic_array[count])
            {
                PictureResourceFree (res, p_sys->conn);
                memset (res, 0, sizeof(*res));
                break;
            }
        }

        if (count == 0)
            return NULL;

        p_sys->pool = picture_pool_New (count, pic_array);
639
        /* TODO release picture resources if NULL */
640
641
        xcb_flush (p_sys->conn);
    }
642

643
    return p_sys->pool;
644
645
646
647
648
}

/**
 * Sends an image to the X server.
 */
649
static void Display (vout_display_t *vd, picture_t *pic)
650
{
651
652
    vout_display_sys_t *p_sys = vd->sys;
    xcb_shm_seg_t segment = pic->p_sys->segment;
653
    xcb_void_cookie_t ck;
654

655
656
    if (!p_sys->visible)
        goto out;
657
    if (segment)
658
659
        ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
                              p_sys->window, p_sys->gc, segment, p_sys->id, 0,
660
661
662
663
664
                   /* Src: */ vd->source.i_x_offset,
                              vd->source.i_y_offset,
                              vd->source.i_visible_width,
                              vd->source.i_visible_height,
                   /* Dst: */ 0, 0, p_sys->width, p_sys->height,
665
                /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
666
                              pic->p->i_lines, false);
667
    else
668
        ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
669
                          p_sys->gc, p_sys->id,
670
671
672
673
                          vd->source.i_x_offset,
                          vd->source.i_y_offset,
                          vd->source.i_visible_width,
                          vd->source.i_visible_height,
674
                          0, 0, p_sys->width, p_sys->height,
675
                          pic->p->i_pitch / pic->p->i_pixel_pitch,
676
                          pic->p->i_lines,
677
                          p_sys->data_size, pic->p->p_pixels);
678

679
680
681
682
683
684
685
    /* Wait for reply. See x11.c for rationale. */
    xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
    if (e != NULL)
    {
        msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
        free (e);
    }
686
out:
687
    picture_Release (pic);
688
689
}

690
static int Control (vout_display_t *vd, int query, va_list ap)
691
{
692
    vout_display_sys_t *p_sys = vd->sys;
693

694
    switch (query)
695
    {
696
697
698
699
700
701
    case VOUT_DISPLAY_CHANGE_FULLSCREEN:
    {
        const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
        return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
    }

702
703
704
705
706
707
708
709
    case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
    case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
    case VOUT_DISPLAY_CHANGE_ZOOM:
    case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
    case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
    {
        const vout_display_cfg_t *cfg;
        const video_format_t *source;
710
        bool is_forced = false;
711

712
713
714
715
716
717
718
719
720
721
        if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
         || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
        {
            source = (const video_format_t *)va_arg (ap, const video_format_t *);
            cfg = vd->cfg;
        }
        else
        {
            source = &vd->source;
            cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
722
723
            if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
                is_forced = (bool)va_arg (ap, int);
724
        }
725

726
727
        /* */
        if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
728
         && is_forced
729
730
731
732
733
         && (cfg->display.width  != vd->cfg->display.width
           ||cfg->display.height != vd->cfg->display.height)
         && vout_window_SetSize (p_sys->embed,
                                  cfg->display.width,
                                  cfg->display.height))
734
            return VLC_EGENERIC;
735

736
737
738
739
740
741
742
743
744
745
746
747
748
749
        vout_display_place_t place;
        vout_display_PlacePicture (&place, source, cfg, false);
        p_sys->width  = place.width;
        p_sys->height = place.height;

        /* Move the picture within the window */
        const uint32_t values[] = { place.x, place.y,
                                    place.width, place.height, };
        xcb_configure_window (p_sys->conn, p_sys->window,
                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
                            | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
                              values);
        xcb_flush (p_sys->conn);
        return VLC_SUCCESS;
750
    }
751
    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
752
    {
753
        unsigned state = va_arg (ap, unsigned);
754
        return vout_window_SetState (p_sys->embed, state);
755
    }
756
757
758

    /* Hide the mouse. It will be send when
     * vout_display_t::info.b_hide_mouse is false */
759
    case VOUT_DISPLAY_HIDE_MOUSE:
760
        xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
761
762
                                  XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
        return VLC_SUCCESS;
763
764
    case VOUT_DISPLAY_RESET_PICTURES:
        assert(0);
765
    default:
766
        msg_Err (vd, "Unknown request in XCB vout display");
767
768
        return VLC_EGENERIC;
    }
769
}
770

771
772
773
774
static void Manage (vout_display_t *vd)
{
    vout_display_sys_t *p_sys = vd->sys;

775
    ManageEvent (vd, p_sys->conn, &p_sys->visible);
776
}
777