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