summaryrefslogtreecommitdiff
path: root/va/wayland/va_wayland_emgd.c
blob: fb8a136d313d70f79c9b19dfd97fa11b1f09c07d (plain)
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
/*
 * va_wayland_emgd.c - Wayland/EMGD helpers
 *
 * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "sysdeps.h"
#include <unistd.h>
#include <dlfcn.h>
#include "va_drmcommon.h"
#include "va_wayland_emgd.h"
#include "va_wayland_private.h"

/* XXX: Wayland/EMGD support currently lives in libwayland-emgd.so.* library */
#define LIBWAYLAND_EMGD_NAME "libwayland-emgd.so.1"

typedef struct va_wayland_emgd_context {
    struct va_wayland_context   base;
    void                       *handle;
    struct wl_emgd             *emgd;
    void                       *emgd_interface;
    unsigned int                is_created      : 1;
    struct wl_registry         *registry;
} VADisplayContextWaylandEMGD;

static inline void
wl_emgd_destroy(struct wl_emgd *emgd)
{
    wl_proxy_destroy((struct wl_proxy *)emgd);
}

static VAStatus
va_DisplayContextGetDriverName(
    VADisplayContextP pDisplayContext,
    char            **driver_name_ptr
)
{
    *driver_name_ptr = strdup("emgd");
    return VA_STATUS_SUCCESS;
}

void
va_wayland_emgd_destroy(VADisplayContextP pDisplayContext)
{
    VADriverContextP const ctx = pDisplayContext->pDriverContext;
    VADisplayContextWaylandEMGD * const wl_emgd_ctx = pDisplayContext->opaque;
    struct drm_state * const drm_state = ctx->drm_state;

    if (wl_emgd_ctx->emgd) {
        wl_emgd_destroy(wl_emgd_ctx->emgd);
        wl_emgd_ctx->emgd = NULL;
    }
    wl_emgd_ctx->is_created = 0;

    if (wl_emgd_ctx->handle) {
        dlclose(wl_emgd_ctx->handle);
        wl_emgd_ctx->handle = NULL;
    }

    if (drm_state) {
        if (drm_state->fd >= 0) {
            close(drm_state->fd);
            drm_state->fd = -1;
        }
        free(ctx->drm_state);
        ctx->drm_state = NULL;
    }
}

static void
registry_handle_global(
    void               *data,
    struct wl_registry *registry,
    uint32_t            id,
    const char         *interface,
    uint32_t            version
)
{
    VADisplayContextWaylandEMGD *wl_emgd_ctx = data;

    if (strcmp(interface, "wl_emgd") == 0) {
        wl_emgd_ctx->emgd =
            wl_registry_bind(registry, id, wl_emgd_ctx->emgd_interface, 1);
    }
}

static const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    NULL,
};

bool
va_wayland_emgd_create(VADisplayContextP pDisplayContext)
{
    VADriverContextP const ctx = pDisplayContext->pDriverContext;
    VADisplayContextWaylandEMGD *wl_emgd_ctx;
    struct drm_state *drm_state;

    wl_emgd_ctx = malloc(sizeof(*wl_emgd_ctx));
    if (!wl_emgd_ctx)
        return false;
    wl_emgd_ctx->base.destroy           = va_wayland_emgd_destroy;
    wl_emgd_ctx->handle                 = NULL;
    wl_emgd_ctx->emgd                   = NULL;
    wl_emgd_ctx->emgd_interface         = NULL;
    wl_emgd_ctx->is_created             = 0;
    pDisplayContext->opaque             = wl_emgd_ctx;
    pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;

    drm_state = calloc(1, sizeof(struct drm_state));
    if (!drm_state)
        return false;
    drm_state->fd        = -1;
    drm_state->auth_type = 0;
    ctx->drm_state       = drm_state;

    wl_emgd_ctx->handle = dlopen(LIBWAYLAND_EMGD_NAME, RTLD_LAZY|RTLD_LOCAL);
    if (!wl_emgd_ctx->handle)
        return false;

    wl_emgd_ctx->emgd_interface =
        dlsym(wl_emgd_ctx->handle, "wl_emgd_interface");
    if (!wl_emgd_ctx->emgd_interface)
        return false;

    wl_emgd_ctx->registry = wl_display_get_registry(ctx->native_dpy);
    wl_registry_add_listener(wl_emgd_ctx->registry, &registry_listener, wl_emgd_ctx);
    wl_display_roundtrip(ctx->native_dpy);

    /* registry_handle_global should have been called by the
     * wl_display_roundtrip above
     */
    if (!wl_emgd_ctx->emgd)
        return false;
    return true;
}