1/*
2 * va_wayland_emgd.c - Wayland/EMGD helpers
3 *
4 * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27#include "sysdeps.h"
28#include <unistd.h>
29#include <dlfcn.h>
30#include "va_drmcommon.h"
31#include "va_wayland_emgd.h"
32#include "va_wayland_private.h"
33
34/* XXX: Wayland/EMGD support currently lives in libwayland-emgd.so.* library */
35#define LIBWAYLAND_EMGD_NAME "libwayland-emgd.so.1"
36
37typedef struct va_wayland_emgd_context {
38    struct va_wayland_context   base;
39    void                       *handle;
40    struct wl_emgd             *emgd;
41    void                       *emgd_interface;
42    unsigned int                is_created      : 1;
43    struct wl_registry         *registry;
44} VADisplayContextWaylandEMGD;
45
46static inline void
47wl_emgd_destroy(struct wl_emgd *emgd)
48{
49    wl_proxy_destroy((struct wl_proxy *)emgd);
50}
51
52static VAStatus
53va_DisplayContextGetDriverName(
54    VADisplayContextP pDisplayContext,
55    char            **driver_name_ptr
56)
57{
58    *driver_name_ptr = strdup("emgd");
59    return VA_STATUS_SUCCESS;
60}
61
62void
63va_wayland_emgd_destroy(VADisplayContextP pDisplayContext)
64{
65    VADriverContextP const ctx = pDisplayContext->pDriverContext;
66    VADisplayContextWaylandEMGD * const wl_emgd_ctx = pDisplayContext->opaque;
67    struct drm_state * const drm_state = ctx->drm_state;
68
69    if (wl_emgd_ctx->emgd) {
70        wl_emgd_destroy(wl_emgd_ctx->emgd);
71        wl_emgd_ctx->emgd = NULL;
72    }
73    wl_emgd_ctx->is_created = 0;
74
75    if (wl_emgd_ctx->handle) {
76        dlclose(wl_emgd_ctx->handle);
77        wl_emgd_ctx->handle = NULL;
78    }
79
80    if (drm_state) {
81        if (drm_state->fd >= 0) {
82            close(drm_state->fd);
83            drm_state->fd = -1;
84        }
85        free(ctx->drm_state);
86        ctx->drm_state = NULL;
87    }
88}
89
90static void
91registry_handle_global(
92    void               *data,
93    struct wl_registry *registry,
94    uint32_t            id,
95    const char         *interface,
96    uint32_t            version
97)
98{
99    VADisplayContextWaylandEMGD *wl_emgd_ctx = data;
100
101    if (strcmp(interface, "wl_emgd") == 0) {
102        wl_emgd_ctx->emgd =
103            wl_registry_bind(registry, id, wl_emgd_ctx->emgd_interface, 1);
104    }
105}
106
107static const struct wl_registry_listener registry_listener = {
108    registry_handle_global,
109    NULL,
110};
111
112bool
113va_wayland_emgd_create(VADisplayContextP pDisplayContext)
114{
115    VADriverContextP const ctx = pDisplayContext->pDriverContext;
116    VADisplayContextWaylandEMGD *wl_emgd_ctx;
117    struct drm_state *drm_state;
118    uint32_t id;
119
120    wl_emgd_ctx = malloc(sizeof(*wl_emgd_ctx));
121    if (!wl_emgd_ctx)
122        return false;
123    wl_emgd_ctx->base.destroy           = va_wayland_emgd_destroy;
124    wl_emgd_ctx->handle                 = NULL;
125    wl_emgd_ctx->emgd                   = NULL;
126    wl_emgd_ctx->emgd_interface         = NULL;
127    wl_emgd_ctx->is_created             = 0;
128    pDisplayContext->opaque             = wl_emgd_ctx;
129    pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;
130
131    drm_state = calloc(1, sizeof(struct drm_state));
132    if (!drm_state)
133        return false;
134    drm_state->fd        = -1;
135    drm_state->auth_type = 0;
136    ctx->drm_state       = drm_state;
137
138    wl_emgd_ctx->handle = dlopen(LIBWAYLAND_EMGD_NAME, RTLD_LAZY|RTLD_LOCAL);
139    if (!wl_emgd_ctx->handle)
140        return false;
141
142    wl_emgd_ctx->emgd_interface =
143        dlsym(wl_emgd_ctx->handle, "wl_emgd_interface");
144    if (!wl_emgd_ctx->emgd_interface)
145        return false;
146
147    wl_emgd_ctx->registry = wl_display_get_registry(ctx->native_dpy);
148    wl_registry_add_listener(wl_emgd_ctx->registry, &registry_listener, wl_emgd_ctx);
149    wl_display_roundtrip(ctx->native_dpy);
150
151    /* registry_handle_global should have been called by the
152     * wl_display_roundtrip above
153     */
154    if (!wl_emgd_ctx->emgd)
155        return false;
156    return true;
157}
158