File: display.c

package info (click to toggle)
vlc 2.2.7-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 191,124 kB
  • sloc: ansic: 356,116; cpp: 94,295; objc: 34,063; sh: 6,765; makefile: 4,272; xml: 1,538; asm: 1,251; python: 240; perl: 77; sed: 16
file content (721 lines) | stat: -rw-r--r-- 23,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/**
 * @file display.c
 * @brief VDPAU video display module for VLC media player
 */
/*****************************************************************************
 * Copyright © 2009-2013 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, 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 <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>
#include <vlc_picture_pool.h>
#include <vlc_xlib.h>

#include "vlc_vdpau.h"
#include "events.h"

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

vlc_module_begin()
    set_shortname(N_("VDPAU"))
    set_description(N_("VDPAU output"))
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
    set_capability("vout display", 300)
    set_callbacks(Open, Close)

    add_shortcut("vdpau", "xid")
vlc_module_end()

struct vout_display_sys_t
{
    xcb_connection_t *conn; /**< XCB connection */
    vout_window_t *embed; /**< parent window */
    vdp_t *vdp; /**< VDPAU back-end */
    picture_t *current; /**< Currently visible picture */

    xcb_window_t window; /**< target window (owned by VDPAU back-end) */
    xcb_cursor_t cursor; /**< blank cursor */
    VdpDevice device; /**< VDPAU device handle */
    VdpPresentationQueueTarget target; /**< VDPAU presentation queue target */
    VdpPresentationQueue queue; /**< VDPAU presentation queue */
    VdpRGBAFormat rgb_fmt; /**< Output surface format */

    picture_pool_t *pool; /**< pictures pool */
};

static void pictureSys_DestroyVDPAU(picture_sys_t *psys)
{
    vdp_output_surface_destroy(psys->vdp, psys->surface);
    vdp_release_x11(psys->vdp);
    free(psys);
}

static void PictureDestroyVDPAU(picture_t *pic)
{
    pictureSys_DestroyVDPAU(pic->p_sys);
    free(pic);
}

static VdpStatus picture_NewVDPAU(vdp_t *vdp, VdpRGBAFormat rgb_fmt,
                                  const video_format_t *restrict fmt,
                                  picture_t **restrict picp)
{
    picture_sys_t *psys = malloc(sizeof (*psys));
    if (unlikely(psys == NULL))
        return VDP_STATUS_RESOURCES;

    psys->vdp = vdp_hold_x11(vdp, &psys->device);

    VdpStatus err = vdp_output_surface_create(psys->vdp, psys->device,
                          rgb_fmt, fmt->i_visible_width, fmt->i_visible_height,
                                              &psys->surface);
    if (err != VDP_STATUS_OK)
    {
        vdp_release_x11(psys->vdp);
        free(psys);
        return err;
    }

    picture_resource_t res = {
        .p_sys = psys,
        .pf_destroy = PictureDestroyVDPAU,
    };

    picture_t *pic = picture_NewFromResource(fmt, &res);
    if (unlikely(pic == NULL))
    {
        pictureSys_DestroyVDPAU(psys);
        return VDP_STATUS_RESOURCES;
    }
    *picp = pic;
    return VDP_STATUS_OK;
}

static picture_pool_t *PoolAlloc(vout_display_t *vd, unsigned requested_count)
{
    vout_display_sys_t *sys = vd->sys;
    picture_t *pics[requested_count];

    unsigned count = 0;
    while (count < requested_count)
    {
        VdpStatus err = picture_NewVDPAU(sys->vdp, sys->rgb_fmt, &vd->fmt,
                                         pics + count);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s creation failure: %s", "output surface",
                    vdp_get_error_string(sys->vdp, err));
            break;
        }
        count++;
    }
    sys->current = NULL;

    if (count == 0)
        return NULL;

    picture_pool_t *pool = picture_pool_New(count, pics);
    if (unlikely(pool == NULL))
        while (count > 0)
            picture_Release(pics[--count]);
    return pool;
}

static void PoolFree(vout_display_t *vd, picture_pool_t *pool)
{
    vout_display_sys_t *sys = vd->sys;

    if (sys->current != NULL)
        picture_Release(sys->current);
    picture_pool_Delete(pool);
}

static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
{
    vout_display_sys_t *sys = vd->sys;

    if (sys->pool == NULL)
        sys->pool = PoolAlloc(vd, requested_count);
    return sys->pool;
}

static void RenderRegion(vout_display_t *vd, VdpOutputSurface target,
                         const subpicture_t *subpic,
                         const subpicture_region_t *reg)
{
    vout_display_sys_t *sys = vd->sys;
    VdpBitmapSurface surface;
#ifdef WORDS_BIGENDIAN
    VdpRGBAFormat fmt = VDP_RGBA_FORMAT_B8G8R8A8;
#else
    VdpRGBAFormat fmt = VDP_RGBA_FORMAT_R8G8B8A8;
#endif
    VdpStatus err;

    /* Create GPU surface for sub-picture */
    err = vdp_bitmap_surface_create(sys->vdp, sys->device, fmt,
        reg->fmt.i_visible_width, reg->fmt.i_visible_height, VDP_FALSE,
                                    &surface);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(vd, "%s creation failure: %s", "bitmap surface",
                vdp_get_error_string(sys->vdp, err));
        return;
    }

    /* Upload sub-picture to GPU surface */
    picture_t *pic = reg->p_picture;
    const void *data = pic->p[0].p_pixels;
    uint32_t pitch = pic->p[0].i_pitch;

    err = vdp_bitmap_surface_put_bits_native(sys->vdp, surface, &data, &pitch,
                                             NULL);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(vd, "subpicture upload failure: %s",
                vdp_get_error_string(sys->vdp, err));
        goto out;
    }

    /* Render onto main surface */
    VdpRect area = {
        reg->i_x * vd->fmt.i_visible_width
            / subpic->i_original_picture_width,
        reg->i_y * vd->fmt.i_visible_height
            / subpic->i_original_picture_height,
        (reg->i_x + reg->fmt.i_visible_width) * vd->fmt.i_visible_width
            / subpic->i_original_picture_width,
        (reg->i_y + reg->fmt.i_visible_height) * vd->fmt.i_visible_height
            / subpic->i_original_picture_height,
    };
    VdpColor color = { 1.f, 1.f, 1.f,
        reg->i_alpha * subpic->i_alpha / 65535.f };
    VdpOutputSurfaceRenderBlendState state = {
        .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
        .blend_factor_source_color =
            VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
        .blend_factor_destination_color =
            VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
        .blend_factor_source_alpha =
            VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ZERO,
        .blend_factor_destination_alpha =
            VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
        .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
        .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
        .blend_constant = { 0.f, 0.f, 0.f, 0.f },
    };

    err = vdp_output_surface_render_bitmap_surface(sys->vdp, target, &area,
                                             surface, NULL, &color, &state, 0);
    if (err != VDP_STATUS_OK)
        msg_Err(vd, "blending failure: %s",
                vdp_get_error_string(sys->vdp, err));

out:/* Destroy GPU surface */
    vdp_bitmap_surface_destroy(sys->vdp, surface);
}

static void Queue(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
{
    vout_display_sys_t *sys = vd->sys;
    VdpOutputSurface surface = pic->p_sys->surface;
    VdpStatus err;

    VdpPresentationQueueStatus status;
    VdpTime ts;
    err = vdp_presentation_queue_query_surface_status(sys->vdp, sys->queue,
                                                      surface, &status, &ts);
    if (err == VDP_STATUS_OK && status != VDP_PRESENTATION_QUEUE_STATUS_IDLE)
        msg_Dbg(vd, "surface status: %u", status);

    if (subpic != NULL)
        for (subpicture_region_t *r = subpic->p_region; r != NULL;
             r = r->p_next)
            RenderRegion(vd, surface, subpic, r);

    /* Compute picture presentation time */
    mtime_t now = mdate();
    VdpTime pts;

    err = vdp_presentation_queue_get_time(sys->vdp, sys->queue, &pts);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(vd, "presentation queue time failure: %s",
                vdp_get_error_string(sys->vdp, err));
        if (err == VDP_STATUS_DISPLAY_PREEMPTED)
            vout_display_SendEventPicturesInvalid(vd);
        return;
    }

    mtime_t delay = pic->date - now;
    if (delay < 0)
        delay = 0; /* core bug: date is not updated during pause */
    if (unlikely(delay > CLOCK_FREQ))
    {   /* We would get stuck if the delay was too long. */
        msg_Dbg(vd, "picture date corrupt: delay of %"PRId64" us", delay);
        delay = CLOCK_FREQ / 50;
    }
    pts += delay * 1000;

    /* Queue picture */
    err = vdp_presentation_queue_display(sys->vdp, sys->queue, surface, 0, 0,
                                         pts);
    if (err != VDP_STATUS_OK)
        msg_Err(vd, "presentation queue display failure: %s",
                vdp_get_error_string(sys->vdp, err));
}

static void Wait(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;

    picture_t *current = sys->current;
    if (current != NULL)
    {
        picture_sys_t *psys = current->p_sys;
        VdpTime pts;
        VdpStatus err;

        err = vdp_presentation_queue_block_until_surface_idle(sys->vdp,
                                              sys->queue, psys->surface, &pts);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "presentation queue blocking error: %s",
                    vdp_get_error_string(sys->vdp, err));
            picture_Release(pic);
            return;
        }
        picture_Release(current);
    }

    sys->current = pic;

    /* We already dealt with the subpicture in the Queue phase, so it's safe to
       delete at this point */
    if (subpicture)
        subpicture_Delete(subpicture);
}

static int Control(vout_display_t *vd, int query, va_list ap)
{
    vout_display_sys_t *sys = vd->sys;

    switch (query)
    {
    case VOUT_DISPLAY_HIDE_MOUSE:
        xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
                                    XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
        break;
    case VOUT_DISPLAY_RESET_PICTURES:
    {
        msg_Dbg(vd, "resetting pictures");
        if (sys->pool != NULL)
        {
            PoolFree(vd, sys->pool);
            sys->pool = NULL;
        }

        const video_format_t *src= &vd->source;
        video_format_t *fmt = &vd->fmt;
        vout_display_place_t place;

        vout_display_PlacePicture(&place, src, vd->cfg, false);

        fmt->i_width = src->i_width * place.width / src->i_visible_width;
        fmt->i_height = src->i_height * place.height / src->i_visible_height;
        fmt->i_visible_width  = place.width;
        fmt->i_visible_height = place.height;
        fmt->i_x_offset = src->i_x_offset * place.width / src->i_visible_width;
        fmt->i_y_offset = src->i_y_offset * place.height / src->i_visible_height;

        const uint32_t values[] = { place.x, place.y,
                                    place.width, place.height, };
        xcb_configure_window(sys->conn, sys->window,
                             XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
                             XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
                             values);
        break;
    }
    case VOUT_DISPLAY_CHANGE_FULLSCREEN:
    {
        const vout_display_cfg_t *c = va_arg(ap, const vout_display_cfg_t *);
        return vout_window_SetFullScreen(sys->embed, c->is_fullscreen);
    }
    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
    {
        unsigned state = va_arg(ap, unsigned);
        return vout_window_SetState(sys->embed, state);
    }
    case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
    {
        const vout_display_cfg_t *cfg = va_arg(ap, const vout_display_cfg_t *);
        bool forced = va_arg(ap, int);
        if (forced)
        {
            vout_window_SetSize(sys->embed,
                                cfg->display.width, cfg->display.height);
            return VLC_EGENERIC; /* Always fail. See x11.c for rationale. */
        }

        vout_display_place_t place;
        vout_display_PlacePicture(&place, &vd->source, cfg, false);
        if (place.width  != vd->fmt.i_visible_width
         || place.height != vd->fmt.i_visible_height)
        {
            vout_display_SendEventPicturesInvalid(vd);
            return VLC_SUCCESS;
        }

        const uint32_t values[] = { place.x, place.y,
                                    place.width, place.height, };
        xcb_configure_window(sys->conn, sys->window,
                             XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
                             XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
                             values);
        break;
    }
    case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
    case VOUT_DISPLAY_CHANGE_ZOOM:
    case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
    case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
        vout_display_SendEventPicturesInvalid (vd);
        return VLC_SUCCESS;
    default:
        msg_Err(vd, "unknown control request %d", query);
        return VLC_EGENERIC;
    }
    xcb_flush (sys->conn);
    return VLC_SUCCESS;
}

static void Manage(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;
    bool visible;

    XCB_Manage(vd, sys->conn, &visible);
}

static int xcb_screen_num(xcb_connection_t *conn, const xcb_screen_t *screen)
{
    const xcb_setup_t *setup = xcb_get_setup(conn);
    unsigned snum = 0;

    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator(setup);
         i.rem > 0; xcb_screen_next(&i))
    {
        if (i.data->root == screen->root)
            return snum;
        snum++;
    }
    return -1;
}

static int Open(vlc_object_t *obj)
{
    if (!vlc_xlib_init(obj))
        return VLC_EGENERIC;

    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *sys = malloc(sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    const xcb_screen_t *screen;
    uint16_t width, height;
    sys->embed = XCB_parent_Create(vd, &sys->conn, &screen, &width, &height);
    if (sys->embed == NULL)
    {
        free(sys);
        return VLC_EGENERIC;
    }

    /* Load the VDPAU back-end and create a device instance */
    VdpStatus err = vdp_get_x11(sys->embed->display.x11,
                                xcb_screen_num(sys->conn, screen),
                                &sys->vdp, &sys->device);
    if (err != VDP_STATUS_OK)
    {
        msg_Dbg(obj, "device creation failure: error %d", (int)err);
        xcb_disconnect(sys->conn);
        vout_display_DeleteWindow(vd, sys->embed);
        free(sys);
        return VLC_EGENERIC;
    }

    const char *info;
    if (vdp_get_information_string(sys->vdp, &info) == VDP_STATUS_OK)
        msg_Dbg(vd, "using back-end %s", info);

    /* Check source format */
    video_format_t fmt;
    VdpChromaType chroma;
    VdpYCbCrFormat format;

    video_format_ApplyRotation(&fmt, &vd->fmt);

    if (fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_420
     || fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_422)
        ;
    else
    if (vlc_fourcc_to_vdp_ycc(fmt.i_chroma, &chroma, &format))
    {
        uint32_t w, h;
        VdpBool ok;

        err = vdp_video_surface_query_capabilities(sys->vdp, sys->device,
                                                   chroma, &ok, &w, &h);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s", "video surface",
                    vdp_get_error_string(sys->vdp, err));
            goto error;
        }
        if (!ok || w < fmt.i_width || h < fmt.i_height)
        {
            msg_Err(vd, "source video %s not supported", "chroma type");
            goto error;
        }

        err = vdp_video_surface_query_get_put_bits_y_cb_cr_capabilities(
                                   sys->vdp, sys->device, chroma, format, &ok);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s", "video surface",
                    vdp_get_error_string(sys->vdp, err));
            goto error;
        }
        if (!ok)
        {
            msg_Err(vd, "source video %s not supported", "YCbCr format");
            goto error;
        }
    }
    else
        goto error;

    /* Check video mixer capabilities */
    {
        uint32_t min, max;

        err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
                    sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
                                                          &min, &max);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s",
                    "video mixer surface width",
                    vdp_get_error_string(sys->vdp, err));
            goto error;
        }
        if (min > fmt.i_width || fmt.i_width > max)
        {
            msg_Err(vd, "source video %s not supported", "width");
            goto error;
        }

        err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
                   sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
                                                          &min, &max);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s",
                    "video mixer surface height",
                    vdp_get_error_string(sys->vdp, err));
            goto error;
        }
        if (min > fmt.i_height || fmt.i_height > max)
        {
            msg_Err(vd, "source video %s not supported", "height");
            goto error;
        }
    }
    fmt.i_chroma = VLC_CODEC_VDPAU_OUTPUT;

    /* Select surface format */
    static const VdpRGBAFormat rgb_fmts[] = {
        VDP_RGBA_FORMAT_R10G10B10A2, VDP_RGBA_FORMAT_B10G10R10A2,
        VDP_RGBA_FORMAT_B8G8R8A8, VDP_RGBA_FORMAT_R8G8B8A8,
    };
    unsigned i;

    for (i = 0; i < sizeof (rgb_fmts) / sizeof (rgb_fmts[0]); i++)
    {
        uint32_t w, h;
        VdpBool ok;

        err = vdp_output_surface_query_capabilities(sys->vdp, sys->device,
                                                    rgb_fmts[i], &ok, &w, &h);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s", "output surface",
                    vdp_get_error_string(sys->vdp, err));
            continue;
        }
        /* NOTE: Wrong! No warranties that zoom <= 100%! */
        if (!ok || w < fmt.i_width || h < fmt.i_height)
            continue;

        sys->rgb_fmt = rgb_fmts[i];
        msg_Dbg(vd, "using RGBA format %u", sys->rgb_fmt);
        break;
    }
    if (i == sizeof (rgb_fmts) / sizeof (rgb_fmts[0]))
    {
        msg_Err(vd, "no supported output surface format");
        goto error;
    }

    /* VDPAU-X11 requires a window dedicated to the back-end */
    {
        xcb_pixmap_t pix = xcb_generate_id(sys->conn);
        xcb_create_pixmap(sys->conn, screen->root_depth, pix,
                          screen->root, 1, 1);

        uint32_t mask =
            XCB_CW_BACK_PIXMAP | XCB_CW_BACK_PIXEL |
            XCB_CW_BORDER_PIXMAP | XCB_CW_BORDER_PIXEL |
            XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
        const uint32_t values[] = {
            pix, screen->black_pixel, pix, screen->black_pixel,
            XCB_EVENT_MASK_VISIBILITY_CHANGE, screen->default_colormap
        };
        vout_display_place_t place;

        vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
        sys->window = xcb_generate_id(sys->conn);

        xcb_void_cookie_t c =
            xcb_create_window_checked(sys->conn, screen->root_depth,
                sys->window, sys->embed->handle.xid, place.x, place.y,
                place.width, place.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
                screen->root_visual, mask, values);
        if (XCB_error_Check(vd, sys->conn, "window creation failure", c))
            goto error;
        msg_Dbg(vd, "using X11 window 0x%08"PRIx32, sys->window);
        xcb_map_window(sys->conn, sys->window);
    }

    /* Check bitmap capabilities (for SPU) */
    const vlc_fourcc_t *spu_chromas = NULL;
    {
#ifdef WORDS_BIGENDIAN
        static const vlc_fourcc_t subpicture_chromas[] = { VLC_CODEC_ARGB, 0 };
#else
        static const vlc_fourcc_t subpicture_chromas[] = { VLC_CODEC_RGBA, 0 };
#endif
        uint32_t w, h;
        VdpBool ok;

        err = vdp_bitmap_surface_query_capabilities(sys->vdp, sys->device,
                                        VDP_RGBA_FORMAT_R8G8B8A8, &ok, &w, &h);
        if (err != VDP_STATUS_OK)
        {
            msg_Err(vd, "%s capabilities query failure: %s", "output surface",
                    vdp_get_error_string(sys->vdp, err));
            ok = VDP_FALSE;
        }
        if (ok)
            spu_chromas = subpicture_chromas;
    }

    /* Initialize VDPAU queue */
    err = vdp_presentation_queue_target_create_x11(sys->vdp, sys->device,
                                                   sys->window, &sys->target);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(vd, "%s creation failure: %s", "presentation queue target",
                vdp_get_error_string(sys->vdp, err));
        goto error;
    }

    err = vdp_presentation_queue_create(sys->vdp, sys->device, sys->target,
                                        &sys->queue);
    if (err != VDP_STATUS_OK)
    {
        msg_Err(vd, "%s creation failure: %s", "presentation queue",
                vdp_get_error_string(sys->vdp, err));
        vdp_presentation_queue_target_destroy(sys->vdp, sys->target);
        goto error;
    }

    sys->cursor = XCB_cursor_Create(sys->conn, screen);
    sys->pool = NULL;

    /* */
    vd->sys = sys;
    vd->info.has_pictures_invalid = true;
    vd->info.has_event_thread = true;
    vd->info.subpicture_chromas = spu_chromas;
    vd->fmt = fmt;

    vd->pool = Pool;
    vd->prepare = Queue;
    vd->display = Wait;
    vd->control = Control;
    vd->manage = Manage;

    /* */
    bool is_fullscreen = vd->cfg->is_fullscreen;
    if (is_fullscreen && vout_window_SetFullScreen(sys->embed, true))
        is_fullscreen = false;
    vout_display_SendEventFullscreen(vd, is_fullscreen);
    vout_display_SendEventDisplaySize(vd, width, height, is_fullscreen);

    return VLC_SUCCESS;

error:
    vdp_release_x11(sys->vdp);
    xcb_disconnect(sys->conn);
    vout_display_DeleteWindow(vd, sys->embed);
    free(sys);
    return VLC_EGENERIC;
}

static void Close(vlc_object_t *obj)
{
    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *sys = vd->sys;

    /* Restore cursor explicitly (parent window connection will survive) */
    xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
                               XCB_CW_CURSOR, &(uint32_t) { XCB_CURSOR_NONE });
    xcb_flush(sys->conn);

    vdp_presentation_queue_destroy(sys->vdp, sys->queue);
    vdp_presentation_queue_target_destroy(sys->vdp, sys->target);

    if (sys->pool != NULL)
        PoolFree(vd, sys->pool);

    vdp_release_x11(sys->vdp);
    xcb_disconnect(sys->conn);
    vout_display_DeleteWindow(vd, sys->embed);
    free(sys);
}