xvideo.c 25.9 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", "xv", "xvideo", "xid")
70
71
vlc_module_end ()

72
#define MAX_PICTURES (128)
73
74

struct vout_display_sys_t
75
76
77
78
{
    xcb_connection_t *conn;
    vout_window_t *embed;/* VLC window */

79
    xcb_cursor_t cursor; /* blank cursor */
80
81
82
83
84
85
    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 */
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
92
93
94
    xcb_xv_query_image_attributes_reply_t *att;
    picture_pool_t *pool; /* picture pool */
    picture_resource_t resource[MAX_PICTURES];
};
95

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

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

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    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);
129
130
131
    return ok;
}

132
static vlc_fourcc_t ParseFormat (vout_display_t *vd,
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                                 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)
148
                    return VLC_CODEC_RGB32;
149
150
                if (f->depth == 32)
                    return 0; /* ARGB -> VLC cannot do that currently */
151
152
153
                break;
              case 24:
                if (f->depth == 24)
154
                    return VLC_CODEC_RGB24;
155
156
157
                break;
              case 16:
                if (f->depth == 16)
158
                    return VLC_CODEC_RGB16;
159
                if (f->depth == 15)
160
                    return VLC_CODEC_RGB15;
161
162
163
                break;
              case 8:
                if (f->depth == 8)
164
                    return VLC_CODEC_RGB8;
165
166
167
168
                break;
            }
            break;
        }
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
                 "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)
189
                    return VLC_CODEC_I444;
190
191
192
193
194
                break;*/
              case 16:
                if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
                {
                    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
200
201
202
203
204
205
206
207
208
                }
                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"))
209
                        return VLC_CODEC_YV12;
210
                    if (!strcmp ((const char *)f->vcomp_order, "YUV"))
211
                        return VLC_CODEC_I420;
212
213
214
215
216
                }
            }
            break;
        }
    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
221
                 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
                 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
222
        msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
223
224
225
                 "%"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);
226
        msg_Warn (vd, " order: %.32s", f->vcomp_order);
227
228
229
230
231
232
        break;
    }
    return 0;
}


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

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

254
        /* VLC pads scanline to 16 pixels internally */
255
256
        unsigned width = fmt->i_width;
        unsigned height = fmt->i_height;
257
258
259
        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,
260
                                           width, height), NULL);
261
262
263
        if (i == NULL)
            continue;

264
        if (i->width != width || i->height != height)
265
        {
266
267
            msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
                      fmt->i_width, fmt->i_height,
268
                      i->width, i->height);
269
270
271
272
273
274
275
276
277
278
279
280
281
            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);
            }
282
283
284
285
286
287
288
289
290
            free (i);
            continue;
        }
        *pa = i;
        return f;
    }
    return NULL;
}

291
292
293
294
295
296
297
298

/**
 * 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));
299

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
300
    if (!var_InheritBool (obj, "overlay"))
301
        return VLC_EGENERIC;
302
303
304
305
306
307
    if (p_sys == NULL)
        return VLC_ENOMEM;

    vd->sys = p_sys;

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

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
334
    xcb_xv_query_adaptors_reply_t *adaptors =
        xcb_xv_query_adaptors_reply (conn,
335
            xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
336
    if (adaptors == NULL)
337
338
        goto error;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
349
350
         xcb_xv_adaptor_info_next (&it))
    {
        const xcb_xv_adaptor_info_t *a = it.data;
351
        char *name;
352

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

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
362
363
364
        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);
365
366
367
        if (r == NULL)
            continue;

368
369
        /* Look for an image format */
        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,
Laurent Aimar's avatar
Laurent Aimar committed
377
            0
378
        };
Laurent Aimar's avatar
Laurent Aimar committed
379
380
381
382
383
        if (vlc_fourcc_IsYUV (fmt.i_chroma))
            chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
        else
            chromas = chromas_default;

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

            /* 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 */
            }

398
            xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
399
400
401
            if (xfmt != NULL)
            {
                p_sys->id = xfmt->id;
402
403
404
405
                p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
                                                                chroma);
                if (!p_sys->swap_uv)
                    fmt.i_chroma = chroma;
406
407
408
409
410
411
412
413
                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;
            }
414
        }
415
        free (r);
416
        if (xfmt == NULL) /* No acceptable image formats */
417
            continue;
418

419
420
421
422
423
        /* 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
424
425
                 xcb_xv_grab_port_reply (conn,
                     xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
426
427
428
429
430
431
             uint8_t result = gr ? gr->result : 0xff;

             free (gr);
             if (result == 0)
             {
                 p_sys->port = port;
432
                 goto grabbed_port;
433
             }
434
435
             msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
                      result);
436
        }
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
            if (f->depth != screen->root_depth)
455
456
                continue; /* this would fail anyway */

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
            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 list[] = {
                /* XCB_CW_BACK_PIXMAP */
                pixmap,
                /* XCB_CW_BACK_PIXEL */
                screen->black_pixel,
                /* XCB_CW_BORDER_PIXMAP */
                pixmap,
                /* XCB_CW_BORDER_PIXEL */
                screen->black_pixel,
473
                /* XCB_CW_EVENT_MASK */
474
475
476
477
478
                XCB_EVENT_MASK_VISIBILITY_CHANGE,
                /* XCB_CW_COLORMAP */
                screen->default_colormap,
            };

479
480
            xcb_void_cookie_t c;

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
487
488
489
490
491
492
493
494

            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);
495
        p_sys->port = 0;
496
        msg_Dbg (vd, "no usable X11 visual");
497
498
499
500
        continue; /* No workable XVideo format (visual/depth) */

    created_window:
        break;
501
    }
502
    free (adaptors);
503
    if (!p_sys->port)
504
505
506
507
    {
        msg_Err (vd, "no available XVideo adaptor");
        goto error;
    }
508
    else
509
    {
510
        xcb_map_window (conn, p_sys->window);
511

512
513
514
515
516
517
518
        vout_display_place_t place;

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

        /* */
519
520
521
        const uint32_t values[] = {
            place.x, place.y, place.width, place.height };
        xcb_configure_window (conn, p_sys->window,
522
                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
523
524
                              XCB_CONFIG_WINDOW_WIDTH |
                              XCB_CONFIG_WINDOW_HEIGHT,
525
                              values);
526
    }
527
    p_sys->visible = false;
528

529
    /* Create graphic context */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
530
531
    p_sys->gc = xcb_generate_id (conn);
    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
535
536
    /* Create cursor */
    p_sys->cursor = CreateBlankCursor (conn, screen);

537
    CheckSHM (obj, conn, &p_sys->shm);
538

539
540
541
    /* */
    vout_display_info_t info = vd->info;
    info.has_pictures_invalid = false;
542
    info.has_event_thread = true;
543

544
545
546
    /* Setup vout_display_t once everything is fine */
    vd->fmt = fmt;
    vd->info = info;
547

548
    vd->pool = Pool;
549
550
551
552
    vd->prepare = NULL;
    vd->display = Display;
    vd->control = Control;
    vd->manage = Manage;
553

554
    /* */
555
556
557
558
    bool is_fullscreen = vd->cfg->is_fullscreen;
    if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
        is_fullscreen = false;
    vout_display_SendEventFullscreen (vd, is_fullscreen);
559
    unsigned width, height;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

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;
587
            PictureResourceFree (res, NULL);
588
589
590
591
        }
        picture_pool_Delete (p_sys->pool);
    }

592
593
594
595
596
    /* 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);

597
598
    free (p_sys->att);
    xcb_disconnect (p_sys->conn);
599
    vout_display_DeleteWindow (vd, p_sys->embed);
600
    free (p_sys);
601
602
}

603
static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
604
{
605
606
    vout_display_sys_t *p_sys = vd->sys;

607
    memset (p_sys->resource, 0, sizeof(p_sys->resource));
608

609
610
611
612
    const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
    const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
    const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
    p_sys->data_size = p_sys->att->data_size;
613

614
615
    picture_t *pic_array[MAX_PICTURES];
    requested_count = __MIN(requested_count, MAX_PICTURES);
616

617
618
619
620
    unsigned count;
    for (count = 0; count < requested_count; count++)
    {
        picture_resource_t *res = &p_sys->resource[count];
621

622
623
624
625
        for (unsigned i = 0; i < num_planes; i++)
        {
            uint32_t data_size;
            data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
626

627
628
            res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
            res->p[i].i_pitch = pitches[i];
629
630
        }

631
632
633
634
635
636
637
638
        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 */
        for (unsigned i = 1; i < num_planes; i++)
            res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
639

640
641
642
643
644
645
646
647
648
649
650
651
652
653
        if (p_sys->swap_uv)
        {   /* 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;
        }

        pic_array[count] = picture_NewFromResource (&vd->fmt, res);
        if (!pic_array[count])
        {
            PictureResourceFree (res, p_sys->conn);
            memset (res, 0, sizeof(*res));
            break;
        }
654
    }
655

656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
    if (count == 0)
        return;

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

/**
 * Return a direct buffer
 */
static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
{
    vout_display_sys_t *p_sys = vd->sys;

    if (!p_sys->pool)
        PoolAlloc (vd, requested_count);

674
    return p_sys->pool;
675
676
677
678
679
}

/**
 * Sends an image to the X server.
 */
680
static void Display (vout_display_t *vd, picture_t *pic)
681
{
682
683
    vout_display_sys_t *p_sys = vd->sys;
    xcb_shm_seg_t segment = pic->p_sys->segment;
684
    xcb_void_cookie_t ck;
685

686
687
    if (!p_sys->visible)
        goto out;
688
    if (segment)
689
690
        ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
                              p_sys->window, p_sys->gc, segment, p_sys->id, 0,
691
692
693
694
695
                   /* 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,
696
                /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
697
                              pic->p->i_lines, false);
698
    else
699
        ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
700
                          p_sys->gc, p_sys->id,
701
702
703
704
                          vd->source.i_x_offset,
                          vd->source.i_y_offset,
                          vd->source.i_visible_width,
                          vd->source.i_visible_height,
705
                          0, 0, p_sys->width, p_sys->height,
706
                          pic->p->i_pitch / pic->p->i_pixel_pitch,
707
                          pic->p->i_lines,
708
                          p_sys->data_size, pic->p->p_pixels);
709

710
711
712
713
714
715
716
    /* 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);
    }
717
out:
718
    picture_Release (pic);
719
720
}

721
static int Control (vout_display_t *vd, int query, va_list ap)
722
{
723
    vout_display_sys_t *p_sys = vd->sys;
724

725
    switch (query)
726
    {
727
728
729
730
731
732
    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);
    }

733
734
735
736
737
738
739
740
    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;
741
        bool is_forced = false;
742

743
744
745
746
747
748
749
750
751
752
        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 *);
753
754
            if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
                is_forced = (bool)va_arg (ap, int);
755
        }
756

757
758
        /* */
        if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
759
         && is_forced
760
761
762
763
764
         && (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))
765
            return VLC_EGENERIC;
766

767
768
769
770
771
772
773
774
775
776
777
778
779
780
        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;
781
    }
782
    case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
783
    {
784
        unsigned state = va_arg (ap, unsigned);
785
        return vout_window_SetState (p_sys->embed, state);
786
    }
787
788
789

    /* Hide the mouse. It will be send when
     * vout_display_t::info.b_hide_mouse is false */
790
    case VOUT_DISPLAY_HIDE_MOUSE:
791
        xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
792
793
                                  XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
        return VLC_SUCCESS;
794
795
    case VOUT_DISPLAY_RESET_PICTURES:
        assert(0);
796
    default:
797
        msg_Err (vd, "Unknown request in XCB vout display");
798
799
        return VLC_EGENERIC;
    }
800
}
801

802
803
804
805
static void Manage (vout_display_t *vd)
{
    vout_display_sys_t *p_sys = vd->sys;

806
    ManageEvent (vd, p_sys->conn, &p_sys->visible);
807
}
808