gstvaapidisplay_drm.c 12.7 KB
Newer Older
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
/*
 *  gstvaapidisplay_drm.c - VA/DRM display abstraction
 *
 *  Copyright (C) 2012 Intel Corporation
 *
 *  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.
 *
 *  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
 *  Lesser General Public License for more details.
 *
 *  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
 */

/**
 * SECTION:gstvaapidisplay_drm
 * @short_description: VA/DRM display abstraction
 */

#include "sysdeps.h"
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libudev.h>
#include <xf86drm.h>
#include <va/va_drm.h>
#include "gstvaapiutils.h"
#include "gstvaapidisplay_priv.h"
#include "gstvaapidisplay_drm.h"
#include "gstvaapidisplay_drm_priv.h"

#define DEBUG 1
#include "gstvaapidebug.h"

#define NAME_PREFIX "DRM:"
#define NAME_PREFIX_LENGTH 4

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

static gboolean
compare_device_path(gconstpointer a, gconstpointer b, gpointer user_data)
{
    const gchar *cached_name = a;
    const gchar *tested_name = b;

    if (!cached_name || !is_device_path(cached_name))
        return FALSE;
    g_return_val_if_fail(tested_name && is_device_path(tested_name), FALSE);

    cached_name += NAME_PREFIX_LENGTH;
    tested_name += NAME_PREFIX_LENGTH;
    return strcmp(cached_name, tested_name) == 0;
}

/* Get default device path. Actually, the first match in the DRM subsystem */
static const gchar *
68
get_default_device_path(GstVaapiDisplay *display)
69
{
70 71
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
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
    const gchar *syspath, *devpath;
    struct udev *udev = NULL;
    struct udev_device *device, *parent;
    struct udev_enumerate *e = NULL;
    struct udev_list_entry *l;
    int fd;

    if (!priv->device_path_default) {
        udev = udev_new();
        if (!udev)
            goto end;

        e = udev_enumerate_new(udev);
        if (!e)
            goto end;

        udev_enumerate_add_match_subsystem(e, "drm");
        udev_enumerate_scan_devices(e);
        udev_list_entry_foreach(l, udev_enumerate_get_list_entry(e)) {
            syspath = udev_list_entry_get_name(l);
            device  = udev_device_new_from_syspath(udev, syspath);
            parent  = udev_device_get_parent(device);
            if (strcmp(udev_device_get_subsystem(parent), "pci") != 0) {
                udev_device_unref(device);
                continue;
            }

            devpath = udev_device_get_devnode(device);
            fd = open(devpath, O_RDWR|O_CLOEXEC);
            if (fd < 0) {
                udev_device_unref(device);
                continue;
            }

            priv->device_path_default = g_strdup(devpath);
            close(fd);
            udev_device_unref(device);
            break;
        }

    end:
        if (e)
            udev_enumerate_unref(e);
        if (udev)
            udev_unref(udev);
    }
    return priv->device_path_default;
}

/* Reconstruct a device path without our prefix */
static const gchar *
123
get_device_path(GstVaapiDisplay *display)
124
{
125 126 127
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
    const gchar *device_path = priv->device_path;
128 129 130 131 132 133 134 135 136 137 138 139 140

    if (!device_path)
        return NULL;

    g_return_val_if_fail(is_device_path(device_path), NULL);

    device_path += NAME_PREFIX_LENGTH;
    if (*device_path == '\0')
        return NULL;
    return device_path;
}

/* Mangle device path with our prefix */
141 142
static gboolean
set_device_path(GstVaapiDisplay *display, const gchar *device_path)
143
{
144 145
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
146 147 148 149 150 151 152

    g_free(priv->device_path);
    priv->device_path = NULL;

    if (!device_path) {
        device_path = get_default_device_path(display);
        if (!device_path)
153
            return FALSE;
154 155
    }
    priv->device_path = g_strdup_printf("%s%s", NAME_PREFIX, device_path);
156
    return priv->device_path != NULL;
157 158 159
}

/* Set device path from file descriptor */
160 161
static gboolean
set_device_path_from_fd(GstVaapiDisplay *display, gint drm_device)
162
{
163 164
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
165 166 167 168 169 170
    const gchar *busid, *path, *str;
    gsize busid_length, path_length;
    struct udev *udev = NULL;
    struct udev_device *device;
    struct udev_enumerate *e = NULL;
    struct udev_list_entry *l;
171
    gboolean success = FALSE;
172 173 174 175 176

    g_free(priv->device_path);
    priv->device_path = NULL;

    if (drm_device < 0)
177
        goto end;
178 179 180

    busid = drmGetBusid(drm_device);
    if (!busid)
181
        goto end;
182
    if (strncmp(busid, "pci:", 4) != 0)
183
        goto end;
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
    busid += 4;
    busid_length = strlen(busid);

    udev = udev_new();
    if (!udev)
        goto end;

    e = udev_enumerate_new(udev);
    if (!e)
        goto end;

    udev_enumerate_add_match_subsystem(e, "drm");
    udev_enumerate_scan_devices(e);
    udev_list_entry_foreach(l, udev_enumerate_get_list_entry(e)) {
        path = udev_list_entry_get_name(l);
        str  = strstr(path, busid);
        if (!str || str <= path || str[-1] != '/')
            continue;

        path_length = strlen(path);
        if (str + busid_length >= path + path_length)
            continue;
        if (strncmp(&str[busid_length], "/drm/card", 9) != 0)
            continue;

        device = udev_device_new_from_syspath(udev, path);
        if (!device)
            continue;

        path = udev_device_get_devnode(device);
        priv->device_path = g_strdup_printf("%s%s", NAME_PREFIX, path);
        udev_device_unref(device);
        break;
    }
218
    success = TRUE;
219 220 221 222 223 224

end:
    if (e)
        udev_enumerate_unref(e);
    if (udev)
        udev_unref(udev);
225
    return success;
226 227
}

228 229 230
static gboolean
gst_vaapi_display_drm_bind_display(GstVaapiDisplay *display,
    gpointer native_display)
231
{
232 233
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
234

235 236
    priv->drm_device = GPOINTER_TO_INT(native_display);
    priv->use_foreign_display = TRUE;
237

238 239 240
    if (!set_device_path_from_fd(display, priv->drm_device))
        return FALSE;
    return TRUE;
241 242
}

243 244
static gboolean
gst_vaapi_display_drm_open_display(GstVaapiDisplay *display, const gchar *name)
245
{
246 247 248
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
    GstVaapiDisplayCache *cache;
249 250
    const GstVaapiDisplayInfo *info;

251 252
    cache = gst_vaapi_display_get_cache();
    g_return_val_if_fail(cache != NULL, FALSE);
253

254 255
    if (!set_device_path(display, name))
        return FALSE;
256

257 258 259 260 261 262 263 264
    info = gst_vaapi_display_cache_lookup_by_name(cache, priv->device_path,
        compare_device_path, NULL);
    if (info) {
        priv->drm_device = GPOINTER_TO_INT(info->native_display);
        priv->use_foreign_display = TRUE;
    }
    else {
        priv->drm_device = open(get_device_path(display), O_RDWR|O_CLOEXEC);
265 266
        if (priv->drm_device < 0)
            return FALSE;
267
        priv->use_foreign_display = FALSE;
268 269 270 271 272 273 274 275
    }
    return TRUE;
}

static void
gst_vaapi_display_drm_close_display(GstVaapiDisplay *display)
{
    GstVaapiDisplayDRMPrivate * const priv =
276
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
277 278

    if (priv->drm_device >= 0) {
279
        if (!priv->use_foreign_display)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
            close(priv->drm_device);
        priv->drm_device = -1;
    }

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

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

static gboolean
296 297
gst_vaapi_display_drm_get_display_info(GstVaapiDisplay *display,
    GstVaapiDisplayInfo *info)
298 299
{
    GstVaapiDisplayDRMPrivate * const priv =
300
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);
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
    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;
    cached_info = gst_vaapi_display_cache_lookup_by_native_display(
        cache, GINT_TO_POINTER(priv->drm_device));
    if (cached_info) {
        *info = *cached_info;
        return TRUE;
    }

    /* Otherwise, create VA display if there is none already */
    info->native_display = GINT_TO_POINTER(priv->drm_device);
    info->display_name   = priv->device_path;
    if (!info->va_display) {
        info->va_display = vaGetDisplayDRM(priv->drm_device);
        if (!info->va_display)
            return FALSE;
        info->display_type = GST_VAAPI_DISPLAY_TYPE_DRM;
    }
    return TRUE;
}

327 328 329 330 331 332 333 334 335
static void
gst_vaapi_display_drm_init(GstVaapiDisplay *display)
{
    GstVaapiDisplayDRMPrivate * const priv =
        GST_VAAPI_DISPLAY_DRM_PRIVATE(display);

    priv->drm_device = -1;
}

336 337 338
static void
gst_vaapi_display_drm_class_init(GstVaapiDisplayDRMClass *klass)
{
339 340
    GstVaapiMiniObjectClass * const object_class =
        GST_VAAPI_MINI_OBJECT_CLASS(klass);
341 342
    GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);

343
    gst_vaapi_display_class_init(&klass->parent_class);
344

345 346 347
    object_class->size          = sizeof(GstVaapiDisplayDRM);
    dpy_class->init             = gst_vaapi_display_drm_init;
    dpy_class->bind_display     = gst_vaapi_display_drm_bind_display;
348 349 350 351 352
    dpy_class->open_display     = gst_vaapi_display_drm_open_display;
    dpy_class->close_display    = gst_vaapi_display_drm_close_display;
    dpy_class->get_display      = gst_vaapi_display_drm_get_display_info;
}

353 354
static inline const GstVaapiDisplayClass *
gst_vaapi_display_drm_class(void)
355
{
356 357
    static GstVaapiDisplayDRMClass g_class;
    static gsize g_class_init = FALSE;
358

359 360 361 362 363
    if (g_once_init_enter(&g_class_init)) {
        gst_vaapi_display_drm_class_init(&g_class);
        g_once_init_leave(&g_class_init, TRUE);
    }
    return GST_VAAPI_DISPLAY_CLASS(&g_class);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
}

/**
 * gst_vaapi_display_drm_new:
 * @device_path: the DRM device path
 *
 * Opens an DRM file descriptor using @device_path and returns a newly
 * allocated #GstVaapiDisplay object. The DRM display will be cloed
 * when the reference count of the object reaches zero.
 *
 * If @device_path is NULL, the DRM device path will be automatically
 * determined as the first positive match in the list of available DRM
 * devices.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
GstVaapiDisplay *
gst_vaapi_display_drm_new(const gchar *device_path)
{
383 384
    return gst_vaapi_display_new(gst_vaapi_display_drm_class(),
        GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME, (gpointer)device_path);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

/**
 * gst_vaapi_display_drm_new_with_device:
 * @device: an open DRM device (file descriptor)
 *
 * Creates a #GstVaapiDisplay based on the open DRM @device. The
 * caller still owns the device file descriptor and must call close()
 * when all #GstVaapiDisplay references are released. Doing so too
 * early can yield undefined behaviour.
 *
 * Return value: a newly allocated #GstVaapiDisplay object
 */
GstVaapiDisplay *
gst_vaapi_display_drm_new_with_device(gint device)
{
    g_return_val_if_fail(device >= 0, NULL);

403 404
    return gst_vaapi_display_new(gst_vaapi_display_drm_class(),
        GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY, GINT_TO_POINTER(device));
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
}

/**
 * gst_vaapi_display_drm_get_device:
 * @display: a #GstVaapiDisplayDRM
 *
 * Returns the underlying DRM device file descriptor that was created
 * by gst_vaapi_display_drm_new() or that was bound from
 * gst_vaapi_display_drm_new_with_device().
 *
 * Return value: the DRM file descriptor attached to @display
 */
gint
gst_vaapi_display_drm_get_device(GstVaapiDisplayDRM *display)
{
420
    g_return_val_if_fail(display != NULL, -1);
421

422
    return GST_VAAPI_DISPLAY_DRM_DEVICE(display);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}

/**
 * gst_vaapi_display_drm_get_device_path:
 * @display: a #GstVaapiDisplayDRM
 *
 * Returns the underlying DRM device path name was created by
 * gst_vaapi_display_drm_new() or that was bound from
 * gst_vaapi_display_drm_new_with_device().
 *
 * Note: the #GstVaapiDisplayDRM object owns the resulting string, so
 * it shall not be deallocated.
 *
 * Return value: the DRM device path name attached to @display
 */
const gchar *
gst_vaapi_display_drm_get_device_path(GstVaapiDisplayDRM *display)
{
441
    g_return_val_if_fail(display != NULL, NULL);
442

443
    return get_device_path(GST_VAAPI_DISPLAY_CAST(display));
444
}