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;
50 struct wl_registry *registry;
52 unsigned int is_authenticated : 1;
53 } VADisplayContextWaylandDRM;
56 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
58 VADisplayContextP const pDisplayContext = data;
59 VADriverContextP const ctx = pDisplayContext->pDriverContext;
60 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
61 struct drm_state * const drm_state = ctx->drm_state;
65 if (stat(device, &st) < 0) {
66 va_wayland_error("failed to identify %s: %s (errno %d)",
67 device, strerror(errno), errno);
71 if (!S_ISCHR(st.st_mode)) {
72 va_wayland_error("%s is not a device", device);
76 drm_state->fd = open(device, O_RDWR);
77 if (drm_state->fd < 0) {
78 va_wayland_error("failed to open %s: %s (errno %d)",
79 device, strerror(errno), errno);
83 drmGetMagic(drm_state->fd, &magic);
84 wl_drm_authenticate(wl_drm_ctx->drm, magic);
88 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
93 drm_handle_authenticated(void *data, struct wl_drm *drm)
95 VADisplayContextP const pDisplayContext = data;
96 VADriverContextP const ctx = pDisplayContext->pDriverContext;
97 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
98 struct drm_state * const drm_state = ctx->drm_state;
100 wl_drm_ctx->is_authenticated = 1;
101 drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
105 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
107 VADisplayContextP const pDisplayContext = data;
108 VADriverContextP const ctx = pDisplayContext->pDriverContext;
109 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
111 vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
114 static const struct wl_drm_listener drm_listener = {
117 drm_handle_authenticated,
118 drm_handle_capabilities,
122 va_DisplayContextGetDriverName(
123 VADisplayContextP pDisplayContext,
124 char **driver_name_ptr
127 VADriverContextP const ctx = pDisplayContext->pDriverContext;
129 return VA_DRM_GetDriverName(ctx, driver_name_ptr);
133 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
135 VADriverContextP const ctx = pDisplayContext->pDriverContext;
136 struct va_wayland_drm_context * const wl_drm_ctx = pDisplayContext->opaque;
137 struct drm_state * const drm_state = ctx->drm_state;
138 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
140 vtable->has_prime_sharing = 0;
142 if (wl_drm_ctx->drm) {
143 wl_drm_destroy(wl_drm_ctx->drm);
144 wl_drm_ctx->drm = NULL;
146 wl_drm_ctx->is_authenticated = 0;
148 if (wl_drm_ctx->handle) {
149 dlclose(wl_drm_ctx->handle);
150 wl_drm_ctx->handle = NULL;
154 if (drm_state->fd >= 0) {
155 close(drm_state->fd);
158 free(ctx->drm_state);
159 ctx->drm_state = NULL;
164 registry_handle_global(
166 struct wl_registry *registry,
168 const char *interface,
172 struct va_wayland_drm_context *wl_drm_ctx = data;
174 if (strcmp(interface, "wl_drm") == 0) {
176 wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface, 2);
180 static const struct wl_registry_listener registry_listener = {
181 registry_handle_global,
186 va_wayland_drm_create(VADisplayContextP pDisplayContext)
188 VADriverContextP const ctx = pDisplayContext->pDriverContext;
189 struct va_wayland_drm_context *wl_drm_ctx;
190 struct drm_state *drm_state;
191 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
193 wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
196 wl_drm_ctx->base.destroy = va_wayland_drm_destroy;
197 wl_drm_ctx->handle = NULL;
198 wl_drm_ctx->drm = NULL;
199 wl_drm_ctx->drm_interface = NULL;
200 wl_drm_ctx->registry = NULL;
201 wl_drm_ctx->is_authenticated = 0;
202 pDisplayContext->opaque = wl_drm_ctx;
203 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
205 drm_state = calloc(1, sizeof(struct drm_state));
209 drm_state->auth_type = 0;
210 ctx->drm_state = drm_state;
211 vtable->has_prime_sharing = 0;
213 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME, RTLD_LAZY|RTLD_LOCAL);
214 if (!wl_drm_ctx->handle) {
215 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME_FALLBACK, RTLD_LAZY|RTLD_LOCAL);
216 if (!wl_drm_ctx->handle)
220 wl_drm_ctx->drm_interface =
221 dlsym(wl_drm_ctx->handle, "wl_drm_interface");
222 if (!wl_drm_ctx->drm_interface)
225 wl_drm_ctx->registry = wl_display_get_registry(ctx->native_dpy);
226 wl_registry_add_listener(wl_drm_ctx->registry, ®istry_listener, wl_drm_ctx);
227 wl_display_roundtrip(ctx->native_dpy);
229 /* registry_handle_global should have been called by the
230 * wl_display_roundtrip above
233 if (!wl_drm_ctx->drm)
236 wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext);
237 wl_display_roundtrip(ctx->native_dpy);
238 if (drm_state->fd < 0)
241 wl_display_roundtrip(ctx->native_dpy);
242 if (!wl_drm_ctx->is_authenticated)