2 * va_wayland_drm.c - Wayland/DRM helpers
4 * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
34 #include "va_drmcommon.h"
35 #include "drm/va_drm_utils.h"
36 #include "va_wayland_drm.h"
37 #include "va_wayland_private.h"
38 #include "wayland-drm-client-protocol.h"
40 /* XXX: Wayland/DRM support currently lives in Mesa libEGL.so.* library */
41 /* First try the soname of a glvnd enabled mesa build */
42 #define LIBWAYLAND_DRM_NAME "libEGL_mesa.so.0"
43 /* Then fallback to plain libEGL.so.1 (which might not be mesa) */
44 #define LIBWAYLAND_DRM_NAME_FALLBACK "libEGL.so.1"
46 typedef struct va_wayland_drm_context {
47 struct va_wayland_context base;
49 struct wl_event_queue *queue;
51 struct wl_registry *registry;
53 unsigned int is_authenticated : 1;
54 } VADisplayContextWaylandDRM;
57 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
59 VADisplayContextP const pDisplayContext = data;
60 VADriverContextP const ctx = pDisplayContext->pDriverContext;
61 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
62 struct drm_state * const drm_state = ctx->drm_state;
66 if (stat(device, &st) < 0) {
67 va_wayland_error("failed to identify %s: %s (errno %d)",
68 device, strerror(errno), errno);
72 if (!S_ISCHR(st.st_mode)) {
73 va_wayland_error("%s is not a device", device);
77 drm_state->fd = open(device, O_RDWR);
78 if (drm_state->fd < 0) {
79 va_wayland_error("failed to open %s: %s (errno %d)",
80 device, strerror(errno), errno);
84 drmGetMagic(drm_state->fd, &magic);
85 wl_drm_authenticate(wl_drm_ctx->drm, magic);
89 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
94 drm_handle_authenticated(void *data, struct wl_drm *drm)
96 VADisplayContextP const pDisplayContext = data;
97 VADriverContextP const ctx = pDisplayContext->pDriverContext;
98 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
99 struct drm_state * const drm_state = ctx->drm_state;
101 wl_drm_ctx->is_authenticated = 1;
102 drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
106 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
108 VADisplayContextP const pDisplayContext = data;
109 VADriverContextP const ctx = pDisplayContext->pDriverContext;
110 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
112 vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
115 static const struct wl_drm_listener drm_listener = {
118 drm_handle_authenticated,
119 drm_handle_capabilities,
123 va_DisplayContextGetDriverName(
124 VADisplayContextP pDisplayContext,
125 char **driver_name_ptr
128 VADriverContextP const ctx = pDisplayContext->pDriverContext;
130 return VA_DRM_GetDriverName(ctx, driver_name_ptr);
134 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
136 VADriverContextP const ctx = pDisplayContext->pDriverContext;
137 struct va_wayland_drm_context * const wl_drm_ctx = pDisplayContext->opaque;
138 struct drm_state * const drm_state = ctx->drm_state;
139 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
141 vtable->has_prime_sharing = 0;
143 if (wl_drm_ctx->drm) {
144 wl_drm_destroy(wl_drm_ctx->drm);
145 wl_drm_ctx->drm = NULL;
147 wl_drm_ctx->is_authenticated = 0;
149 if (wl_drm_ctx->registry) {
150 wl_registry_destroy(wl_drm_ctx->registry);
151 wl_drm_ctx->registry = NULL;
154 if (wl_drm_ctx->queue) {
155 wl_event_queue_destroy(wl_drm_ctx->queue);
156 wl_drm_ctx->queue = NULL;
159 if (wl_drm_ctx->handle) {
160 dlclose(wl_drm_ctx->handle);
161 wl_drm_ctx->handle = NULL;
165 if (drm_state->fd >= 0) {
166 close(drm_state->fd);
169 free(ctx->drm_state);
170 ctx->drm_state = NULL;
175 registry_handle_global(
177 struct wl_registry *registry,
179 const char *interface,
183 struct va_wayland_drm_context *wl_drm_ctx = data;
185 if (strcmp(interface, "wl_drm") == 0) {
186 /* bind to at most version 2, but also support version 1 if
187 * compositor does not have v2
190 wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface,
191 (version < 2) ? version : 2);
195 static const struct wl_registry_listener registry_listener = {
196 registry_handle_global,
201 wayland_roundtrip_queue(struct wl_display *display,
202 struct wl_event_queue *queue)
204 if (wl_display_roundtrip_queue(display, queue) < 0) {
205 int err = wl_display_get_error(display);
206 va_wayland_error("Wayland roundtrip error: %s (errno %d)", strerror(err), err);
214 va_wayland_drm_create(VADisplayContextP pDisplayContext)
216 VADriverContextP const ctx = pDisplayContext->pDriverContext;
217 struct va_wayland_drm_context *wl_drm_ctx;
218 struct drm_state *drm_state;
219 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
220 struct wl_display *wrapped_display;
222 wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
224 va_wayland_error("could not allocate wl_drm_ctx");
227 wl_drm_ctx->base.destroy = va_wayland_drm_destroy;
228 wl_drm_ctx->handle = NULL;
229 wl_drm_ctx->queue = NULL;
230 wl_drm_ctx->drm = NULL;
231 wl_drm_ctx->drm_interface = NULL;
232 wl_drm_ctx->registry = NULL;
233 wl_drm_ctx->is_authenticated = 0;
234 pDisplayContext->opaque = wl_drm_ctx;
235 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
237 drm_state = calloc(1, sizeof(struct drm_state));
239 va_wayland_error("could not allocate drm_state");
243 drm_state->auth_type = 0;
244 ctx->drm_state = drm_state;
245 vtable->has_prime_sharing = 0;
247 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME, RTLD_LAZY|RTLD_LOCAL);
248 if (!wl_drm_ctx->handle) {
249 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME_FALLBACK, RTLD_LAZY|RTLD_LOCAL);
250 if (!wl_drm_ctx->handle)
254 wl_drm_ctx->drm_interface =
255 dlsym(wl_drm_ctx->handle, "wl_drm_interface");
256 if (!wl_drm_ctx->drm_interface) {
257 va_wayland_error("wl_drm_interface not found in library");
261 /* Use wrapped wl_display with private event queue to prevent
262 * thread safety issues with applications that e.g. run an event pump
263 * parallel to libva initialization.
264 * Using the default queue, events might get lost and crashes occur
265 * because wl_display_roundtrip is not thread-safe with respect to the
268 wl_drm_ctx->queue = wl_display_create_queue(ctx->native_dpy);
269 if (!wl_drm_ctx->queue) {
270 va_wayland_error("could not create Wayland event queue");
274 wrapped_display = wl_proxy_create_wrapper(ctx->native_dpy);
275 if (!wrapped_display) {
276 va_wayland_error("could not create Wayland proxy wrapper");
280 /* All created objects will inherit this queue */
281 wl_proxy_set_queue((struct wl_proxy *) wrapped_display, wl_drm_ctx->queue);
282 wl_drm_ctx->registry = wl_display_get_registry(wrapped_display);
283 wl_proxy_wrapper_destroy(wrapped_display);
284 wl_registry_add_listener(wl_drm_ctx->registry, ®istry_listener, wl_drm_ctx);
285 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
288 /* registry_handle_global should have been called by the
289 * wl_display_roundtrip_queue above
292 /* Do not print an error, the compositor might just not support wl_drm */
293 if (!wl_drm_ctx->drm)
296 wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext);
297 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
299 if (drm_state->fd < 0) {
300 va_wayland_error("did not get DRM device");
304 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
307 if (!wl_drm_ctx->is_authenticated) {
308 va_wayland_error("Wayland compositor did not respond to DRM authentication");