gstvaapidisplay_x11.c 14.1 KB
Newer Older
gb's avatar
gb committed
1 2 3
/*
 *  gstvaapidisplay_x11.c - VA/X11 display abstraction
 *
4
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5
 *  Copyright (C) 2011-2012 Intel Corporation
gb's avatar
gb committed
6
 *
gb's avatar
gb committed
7 8 9 10
 *  This library 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.
gb's avatar
gb committed
11
 *
gb's avatar
gb committed
12
 *  This library is distributed in the hope that it will be useful,
gb's avatar
gb committed
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
gb's avatar
gb committed
14 15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
gb's avatar
gb committed
16
 *
gb's avatar
gb committed
17 18 19 20
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
gb's avatar
gb committed
21 22
 */

gb's avatar
gb committed
23
/**
gb's avatar
gb committed
24 25
 * SECTION:gstvaapidisplay_x11
 * @short_description: VA/X11 display abstraction
gb's avatar
gb committed
26 27
 */

28
#include "sysdeps.h"
29
#include <string.h>
gb's avatar
gb committed
30
#include "gstvaapiutils.h"
31
#include "gstvaapidisplay_priv.h"
32 33
#include "gstvaapidisplay_x11.h"
#include "gstvaapidisplay_x11_priv.h"
gb's avatar
gb committed
34

35 36 37 38
#ifdef HAVE_XRANDR
# include <X11/extensions/Xrandr.h>
#endif

gb's avatar
gb committed
39
#define DEBUG 1
gb's avatar
gb committed
40
#include "gstvaapidebug.h"
gb's avatar
gb committed
41

42 43 44 45 46 47 48 49 50
#define NAME_PREFIX "X11:"
#define NAME_PREFIX_LENGTH 4

static inline gboolean
is_display_name(const gchar *display_name)
{
    return strncmp(display_name, NAME_PREFIX, NAME_PREFIX_LENGTH) == 0;
}

51 52 53 54 55 56 57 58 59 60 61 62 63
static inline const gchar *
get_default_display_name(void)
{
    static const gchar *g_display_name;

    if (!g_display_name)
        g_display_name = getenv("DISPLAY");
    return g_display_name;
}

static gboolean
compare_display_name(gconstpointer a, gconstpointer b, gpointer user_data)
{
64 65 66
    const gchar *cached_name = a, *cached_name_end;
    const gchar *tested_name = b, *tested_name_end;
    guint cached_name_length, tested_name_length;
67

68 69 70 71 72 73 74 75
    if (!cached_name || !is_display_name(cached_name))
        return FALSE;
    g_return_val_if_fail(tested_name && is_display_name(tested_name), FALSE);

    cached_name += NAME_PREFIX_LENGTH;
    cached_name_end = strchr(cached_name, ':');
    if (cached_name_end)
        cached_name_length = cached_name_end - cached_name;
76
    else
77
        cached_name_length = strlen(cached_name);
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92
    tested_name += NAME_PREFIX_LENGTH;
    tested_name_end = strchr(tested_name, ':');
    if (tested_name_end)
        tested_name_length = tested_name_end - tested_name;
    else
        tested_name_length = strlen(tested_name);

    if (cached_name_length != tested_name_length)
        return FALSE;
    if (strncmp(cached_name, tested_name, cached_name_length) != 0)
        return FALSE;

    /* XXX: handle screen number? */
    return TRUE;
93 94
}

95 96
/* Reconstruct a display name without our prefix */
static const gchar *
97
get_display_name(GstVaapiDisplayX11 *display)
98
{
99 100
    GstVaapiDisplayX11Private * const priv = &display->priv;
    const gchar *display_name = priv->display_name;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    if (!display_name)
        return NULL;

    if (is_display_name(display_name)) {
        display_name += NAME_PREFIX_LENGTH;
        if (*display_name == '\0')
            return NULL;
        return display_name;
    }

    /* XXX: this should not happen */
    g_assert(0 && "display name without prefix");
    return display_name;
}

/* Mangle display name with our prefix */
118
static gboolean
119 120
set_display_name(GstVaapiDisplayX11 *display, const gchar *display_name)
{
121
    GstVaapiDisplayX11Private * const priv = &display->priv;
122 123 124

    g_free(priv->display_name);

125 126 127 128 129 130
    if (!display_name) {
        display_name = get_default_display_name();
        if (!display_name)
            display_name = "";
    }
    priv->display_name = g_strdup_printf("%s%s", NAME_PREFIX, display_name);
131
    return priv->display_name != NULL;
132 133
}

134
/* Set synchronous behavious on the underlying X11 display */
gb's avatar
gb committed
135 136 137
static void
set_synchronous(GstVaapiDisplayX11 *display, gboolean synchronous)
{
138
    GstVaapiDisplayX11Private * const priv = &display->priv;
gb's avatar
gb committed
139 140 141

    if (priv->synchronous != synchronous) {
        priv->synchronous = synchronous;
142 143
        if (priv->x11_display) {
            GST_VAAPI_DISPLAY_LOCK(display);
gb's avatar
gb committed
144
            XSynchronize(priv->x11_display, synchronous);
145 146
            GST_VAAPI_DISPLAY_UNLOCK(display);
        }
gb's avatar
gb committed
147 148 149
    }
}

150
/* Check whether XRANDR extension is available */
151
static void
152
check_xrandr(GstVaapiDisplayX11 *display)
gb's avatar
gb committed
153
{
154 155 156 157
#ifdef HAVE_XRANDR
    GstVaapiDisplayX11Private * const priv =
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
    int evt_base, err_base;
gb's avatar
gb committed
158

159 160 161
    priv->use_xrandr = XRRQueryExtension(priv->x11_display,
        &evt_base, &err_base);
#endif
gb's avatar
gb committed
162 163
}

164 165 166
static gboolean
gst_vaapi_display_x11_bind_display(GstVaapiDisplay *base_display,
    gpointer native_display)
167
{
168 169 170 171 172 173 174
    GstVaapiDisplayX11 * const display =
        GST_VAAPI_DISPLAY_X11_CAST(base_display);
    GstVaapiDisplayX11Private * const priv = &display->priv;

    priv->x11_display = native_display;
    priv->x11_screen = DefaultScreen(native_display);
    priv->use_foreign_display = TRUE;
175

176
    check_xrandr(display);
177

178 179 180
    if (!set_display_name(display, XDisplayString(priv->x11_display)))
        return FALSE;
    return TRUE;
181 182
}

183
static gboolean
184 185
gst_vaapi_display_x11_open_display(GstVaapiDisplay *base_display,
    const gchar *name)
186
{
187 188 189 190 191 192 193 194
    GstVaapiDisplayX11 * const display =
        GST_VAAPI_DISPLAY_X11_CAST(base_display);
    GstVaapiDisplayX11Private * const priv = &display->priv;
    GstVaapiDisplayCache *cache;
    const GstVaapiDisplayInfo *info;

    cache = gst_vaapi_display_get_cache();
    g_return_val_if_fail(cache != NULL, FALSE);
195

196 197 198 199 200 201 202 203 204 205
    if (!set_display_name(display, name))
        return FALSE;

    info = gst_vaapi_display_cache_lookup_by_name(cache, priv->display_name,
        compare_display_name, NULL);
    if (info) {
        priv->x11_display = info->native_display;
        priv->use_foreign_display = TRUE;
    }
    else {
206
        priv->x11_display = XOpenDisplay(get_display_name(display));
207 208
        if (!priv->x11_display)
            return FALSE;
209
        priv->use_foreign_display = FALSE;
210
    }
211
    priv->x11_screen = DefaultScreen(priv->x11_display);
212

213
    check_xrandr(display);
gb's avatar
gb committed
214
    return TRUE;
215 216 217 218 219 220
}

static void
gst_vaapi_display_x11_close_display(GstVaapiDisplay *display)
{
    GstVaapiDisplayX11Private * const priv =
221
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
222 223

    if (priv->x11_display) {
224
        if (!priv->use_foreign_display)
225
            XCloseDisplay(priv->x11_display);
226 227 228 229 230 231 232 233 234
        priv->x11_display = NULL;
    }

    if (priv->display_name) {
        g_free(priv->display_name);
        priv->display_name = NULL;
    }
}

235 236 237 238
static void
gst_vaapi_display_x11_sync(GstVaapiDisplay *display)
{
    GstVaapiDisplayX11Private * const priv =
239
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
240

gb's avatar
gb committed
241 242
    if (priv->x11_display) {
        GST_VAAPI_DISPLAY_LOCK(display);
243
        XSync(priv->x11_display, False);
gb's avatar
gb committed
244 245
        GST_VAAPI_DISPLAY_UNLOCK(display);
    }
246 247 248 249 250 251
}

static void
gst_vaapi_display_x11_flush(GstVaapiDisplay *display)
{
    GstVaapiDisplayX11Private * const priv =
252
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
253

gb's avatar
gb committed
254 255
    if (priv->x11_display) {
        GST_VAAPI_DISPLAY_LOCK(display);
256
        XFlush(priv->x11_display);
gb's avatar
gb committed
257 258
        GST_VAAPI_DISPLAY_UNLOCK(display);
    }
259 260
}

261 262 263 264 265
static gboolean
gst_vaapi_display_x11_get_display_info(
    GstVaapiDisplay     *display,
    GstVaapiDisplayInfo *info
)
266
{
267
    GstVaapiDisplayX11Private * const priv =
268
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
269 270 271 272 273 274 275
    GstVaapiDisplayCache *cache;
    const GstVaapiDisplayInfo *cached_info;

    /* Return any cached info even if child has its own VA display */
    cache = gst_vaapi_display_get_cache();
    if (!cache)
        return FALSE;
276 277
    cached_info = gst_vaapi_display_cache_lookup_by_native_display(
        cache, priv->x11_display);
278 279 280 281 282 283 284 285 286 287 288 289
    if (cached_info) {
        *info = *cached_info;
        return TRUE;
    }

    /* Otherwise, create VA display if there is none already */
    info->native_display = priv->x11_display;
    info->display_name   = priv->display_name;
    if (!info->va_display) {
        info->va_display = vaGetDisplay(priv->x11_display);
        if (!info->va_display)
            return FALSE;
290
        info->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
291 292
    }
    return TRUE;
293 294
}

gb's avatar
gb committed
295 296 297 298 299 300 301
static void
gst_vaapi_display_x11_get_size(
    GstVaapiDisplay *display,
    guint           *pwidth,
    guint           *pheight
)
{
302
    GstVaapiDisplayX11Private * const priv =
303
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
gb's avatar
gb committed
304 305 306 307 308 309 310 311 312 313 314

    if (!priv->x11_display)
        return;

    if (pwidth)
        *pwidth = DisplayWidth(priv->x11_display, priv->x11_screen);

    if (pheight)
        *pheight = DisplayHeight(priv->x11_display, priv->x11_screen);
}

315 316 317 318 319 320 321 322
static void
gst_vaapi_display_x11_get_size_mm(
    GstVaapiDisplay *display,
    guint           *pwidth,
    guint           *pheight
)
{
    GstVaapiDisplayX11Private * const priv =
323
        GST_VAAPI_DISPLAY_X11_PRIVATE(display);
324
    guint width_mm, height_mm;
325 326 327 328

    if (!priv->x11_display)
        return;

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
    width_mm  = DisplayWidthMM(priv->x11_display, priv->x11_screen);
    height_mm = DisplayHeightMM(priv->x11_display, priv->x11_screen);

#ifdef HAVE_XRANDR
    /* XXX: fix up physical size if the display is rotated */
    if (priv->use_xrandr) {
        XRRScreenConfiguration *xrr_config = NULL;
        XRRScreenSize *xrr_sizes;
        Window win;
        int num_xrr_sizes, size_id, screen;
        Rotation rotation;

        do {
            win    = DefaultRootWindow(priv->x11_display);
            screen = XRRRootToScreen(priv->x11_display, win);

            xrr_config = XRRGetScreenInfo(priv->x11_display, win);
            if (!xrr_config)
                break;

            size_id = XRRConfigCurrentConfiguration(xrr_config, &rotation);
            if (rotation == RR_Rotate_0 || rotation == RR_Rotate_180)
                break;

            xrr_sizes = XRRSizes(priv->x11_display, screen, &num_xrr_sizes);
            if (!xrr_sizes || size_id >= num_xrr_sizes)
                break;

            width_mm  = xrr_sizes[size_id].mheight;
            height_mm = xrr_sizes[size_id].mwidth;
        } while (0);
        if (xrr_config)
            XRRFreeScreenConfigInfo(xrr_config);
    }
#endif

365
    if (pwidth)
366
        *pwidth = width_mm;
367 368

    if (pheight)
369
        *pheight = height_mm;
370 371
}

372
void
gb's avatar
gb committed
373 374
gst_vaapi_display_x11_class_init(GstVaapiDisplayX11Class *klass)
{
375 376
    GstVaapiMiniObjectClass * const object_class =
        GST_VAAPI_MINI_OBJECT_CLASS(klass);
377
    GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
gb's avatar
gb committed
378

379
    gst_vaapi_display_class_init(&klass->parent_class);
380

381 382
    object_class->size          = sizeof(GstVaapiDisplayX11);
    dpy_class->bind_display     = gst_vaapi_display_x11_bind_display;
383 384
    dpy_class->open_display     = gst_vaapi_display_x11_open_display;
    dpy_class->close_display    = gst_vaapi_display_x11_close_display;
385 386
    dpy_class->sync             = gst_vaapi_display_x11_sync;
    dpy_class->flush            = gst_vaapi_display_x11_flush;
387
    dpy_class->get_display      = gst_vaapi_display_x11_get_display_info;
gb's avatar
gb committed
388
    dpy_class->get_size         = gst_vaapi_display_x11_get_size;
389
    dpy_class->get_size_mm      = gst_vaapi_display_x11_get_size_mm;
gb's avatar
gb committed
390 391
}

392 393
static inline const GstVaapiDisplayClass *
gst_vaapi_display_x11_class(void)
gb's avatar
gb committed
394
{
395 396 397 398 399 400 401 402
    static GstVaapiDisplayX11Class g_class;
    static gsize g_class_init = FALSE;

    if (g_once_init_enter(&g_class_init)) {
        gst_vaapi_display_x11_class_init(&g_class);
        g_once_init_leave(&g_class_init, TRUE);
    }
    return GST_VAAPI_DISPLAY_CLASS(&g_class);
gb's avatar
gb committed
403 404
}

gb's avatar
gb committed
405 406 407 408 409 410 411 412 413 414
/**
 * gst_vaapi_display_x11_new:
 * @display_name: the X11 display name
 *
 * Opens an X11 #Display using @display_name and returns a newly
 * allocated #GstVaapiDisplay object. The X11 display will be cloed
 * when the reference count of the object reaches zero.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
415 416
GstVaapiDisplay *
gst_vaapi_display_x11_new(const gchar *display_name)
gb's avatar
gb committed
417
{
418 419
    return gst_vaapi_display_new(gst_vaapi_display_x11_class(),
        GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME, (gpointer)display_name);
gb's avatar
gb committed
420 421
}

gb's avatar
gb committed
422 423 424 425 426 427 428 429 430 431 432
/**
 * gst_vaapi_display_x11_new_with_display:
 * @x11_display: an X11 #Display
 *
 * Creates a #GstVaapiDisplay based on the X11 @x11_display
 * display. The caller still owns the display and must call
 * XCloseDisplay() when all #GstVaapiDisplay references are
 * released. Doing so too early can yield undefined behaviour.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
433 434 435
GstVaapiDisplay *
gst_vaapi_display_x11_new_with_display(Display *x11_display)
{
436 437
    g_return_val_if_fail(x11_display, NULL);

438 439
    return gst_vaapi_display_new(gst_vaapi_display_x11_class(),
        GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY, x11_display);
440 441
}

gb's avatar
gb committed
442 443 444 445 446 447 448 449 450 451
/**
 * gst_vaapi_display_x11_get_display:
 * @display: a #GstVaapiDisplayX11
 *
 * Returns the underlying X11 #Display that was created by
 * gst_vaapi_display_x11_new() or that was bound from
 * gst_vaapi_display_x11_new_with_display().
 *
 * Return value: the X11 #Display attached to @display
 */
452 453
Display *
gst_vaapi_display_x11_get_display(GstVaapiDisplayX11 *display)
gb's avatar
gb committed
454
{
455
    g_return_val_if_fail(display != NULL, NULL);
gb's avatar
gb committed
456

457
    return GST_VAAPI_DISPLAY_XDISPLAY(display);
gb's avatar
gb committed
458
}
459 460 461 462 463 464 465 466 467 468 469 470 471 472

/**
 * gst_vaapi_display_x11_get_screen:
 * @display: a #GstVaapiDisplayX11
 *
 * Returns the default X11 screen that was created by
 * gst_vaapi_display_x11_new() or that was bound from
 * gst_vaapi_display_x11_new_with_display().
 *
 * Return value: the X11 #Display attached to @display
 */
int
gst_vaapi_display_x11_get_screen(GstVaapiDisplayX11 *display)
{
473
    g_return_val_if_fail(display != NULL, -1);
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    return GST_VAAPI_DISPLAY_XSCREEN(display);
}

/**
 * gst_vaapi_display_x11_set_synchronous:
 * @display: a #GstVaapiDisplayX11
 * @synchronous: boolean value that indicates whether to enable or
 *   disable synchronization
 *
 * If @synchronous is %TRUE, gst_vaapi_display_x11_set_synchronous()
 * turns on synchronous behaviour on the underlying X11
 * display. Otherwise, synchronous behaviour is disabled if
 * @synchronous is %FALSE.
 */
void
gst_vaapi_display_x11_set_synchronous(GstVaapiDisplayX11 *display,
    gboolean synchronous)
{
493
    g_return_if_fail(display != NULL);
494 495

    set_synchronous(display, synchronous);
496
}