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->registry) {
149 wl_registry_destroy(wl_drm_ctx->registry);
150 wl_drm_ctx->registry = NULL;
153 if (wl_drm_ctx->handle) {
154 dlclose(wl_drm_ctx->handle);
155 wl_drm_ctx->handle = NULL;
159 if (drm_state->fd >= 0) {
160 close(drm_state->fd);
163 free(ctx->drm_state);
164 ctx->drm_state = NULL;
169 registry_handle_global(
171 struct wl_registry *registry,
173 const char *interface,
177 struct va_wayland_drm_context *wl_drm_ctx = data;
179 if (strcmp(interface, "wl_drm") == 0) {
181 wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface, 2);
185 static const struct wl_registry_listener registry_listener = {
186 registry_handle_global,
191 va_wayland_drm_create(VADisplayContextP pDisplayContext)
193 VADriverContextP const ctx = pDisplayContext->pDriverContext;
194 struct va_wayland_drm_context *wl_drm_ctx;
195 struct drm_state *drm_state;
196 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
198 wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
201 wl_drm_ctx->base.destroy = va_wayland_drm_destroy;
202 wl_drm_ctx->handle = NULL;
203 wl_drm_ctx->drm = NULL;
204 wl_drm_ctx->drm_interface = NULL;
205 wl_drm_ctx->registry = NULL;
206 wl_drm_ctx->is_authenticated = 0;
207 pDisplayContext->opaque = wl_drm_ctx;
208 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
210 drm_state = calloc(1, sizeof(struct drm_state));
214 drm_state->auth_type = 0;
215 ctx->drm_state = drm_state;
216 vtable->has_prime_sharing = 0;
218 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME, RTLD_LAZY|RTLD_LOCAL);
219 if (!wl_drm_ctx->handle) {
220 wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME_FALLBACK, RTLD_LAZY|RTLD_LOCAL);
221 if (!wl_drm_ctx->handle)
225 wl_drm_ctx->drm_interface =
226 dlsym(wl_drm_ctx->handle, "wl_drm_interface");
227 if (!wl_drm_ctx->drm_interface)
230 wl_drm_ctx->registry = wl_display_get_registry(ctx->native_dpy);
231 wl_registry_add_listener(wl_drm_ctx->registry, ®istry_listener, wl_drm_ctx);
232 wl_display_roundtrip(ctx->native_dpy);
234 /* registry_handle_global should have been called by the
235 * wl_display_roundtrip above
238 if (!wl_drm_ctx->drm)
241 wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext);
242 wl_display_roundtrip(ctx->native_dpy);
243 if (drm_state->fd < 0)
246 wl_display_roundtrip(ctx->native_dpy);
247 if (!wl_drm_ctx->is_authenticated)