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 typedef struct va_wayland_drm_context {
41 struct va_wayland_context base;
42 struct wl_event_queue *queue;
45 struct wl_registry *registry;
46 unsigned int is_authenticated : 1;
47 } VADisplayContextWaylandDRM;
50 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
52 VADisplayContextP const pDisplayContext = data;
53 VADriverContextP const ctx = pDisplayContext->pDriverContext;
54 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
55 struct drm_state * const drm_state = ctx->drm_state;
59 if (stat(device, &st) < 0) {
60 va_wayland_error("failed to identify %s: %s (errno %d)",
61 device, strerror(errno), errno);
65 if (!S_ISCHR(st.st_mode)) {
66 va_wayland_error("%s is not a device", device);
70 drm_state->fd = open(device, O_RDWR);
71 if (drm_state->fd < 0) {
72 va_wayland_error("failed to open %s: %s (errno %d)",
73 device, strerror(errno), errno);
77 drmGetMagic(drm_state->fd, &magic);
78 wl_drm_authenticate(wl_drm_ctx->drm, magic);
82 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
87 drm_handle_authenticated(void *data, struct wl_drm *drm)
89 VADisplayContextP const pDisplayContext = data;
90 VADriverContextP const ctx = pDisplayContext->pDriverContext;
91 VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
92 struct drm_state * const drm_state = ctx->drm_state;
94 wl_drm_ctx->is_authenticated = 1;
95 drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
99 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
101 VADisplayContextP const pDisplayContext = data;
102 VADriverContextP const ctx = pDisplayContext->pDriverContext;
103 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
105 vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
108 static const struct wl_drm_listener drm_listener = {
111 drm_handle_authenticated,
112 drm_handle_capabilities,
116 va_DisplayContextGetDriverName(
117 VADisplayContextP pDisplayContext,
118 char **driver_name_ptr
121 VADriverContextP const ctx = pDisplayContext->pDriverContext;
123 return VA_DRM_GetDriverName(ctx, driver_name_ptr);
127 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
129 VADriverContextP const ctx = pDisplayContext->pDriverContext;
130 struct va_wayland_drm_context * const wl_drm_ctx = pDisplayContext->opaque;
131 struct drm_state * const drm_state = ctx->drm_state;
132 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
134 vtable->has_prime_sharing = 0;
136 wl_drm_ctx->is_authenticated = 0;
139 if (drm_state->fd >= 0) {
140 close(drm_state->fd);
143 free(ctx->drm_state);
144 ctx->drm_state = NULL;
149 registry_handle_global(
151 struct wl_registry *registry,
153 const char *interface,
157 struct va_wayland_drm_context *wl_drm_ctx = data;
159 if (strcmp(interface, "wl_drm") == 0) {
160 /* bind to at most version 2, but also support version 1 if
161 * compositor does not have v2
163 wl_drm_ctx->drm_name = name;
165 wl_registry_bind(wl_drm_ctx->registry, name, &wl_drm_interface,
166 (version < 2) ? version : 2);
171 registry_handle_global_remove(
173 struct wl_registry *registry,
177 struct va_wayland_drm_context *wl_drm_ctx = data;
179 if (wl_drm_ctx->drm && name == wl_drm_ctx->drm_name) {
180 wl_drm_destroy(wl_drm_ctx->drm);
181 wl_drm_ctx->drm = NULL;
185 static const struct wl_registry_listener registry_listener = {
186 registry_handle_global,
187 registry_handle_global_remove
191 wayland_roundtrip_queue(struct wl_display *display,
192 struct wl_event_queue *queue)
194 if (wl_display_roundtrip_queue(display, queue) < 0) {
195 int err = wl_display_get_error(display);
196 va_wayland_error("Wayland roundtrip error: %s (errno %d)", strerror(err), err);
204 va_wayland_drm_create(VADisplayContextP pDisplayContext)
207 VADriverContextP const ctx = pDisplayContext->pDriverContext;
208 struct va_wayland_drm_context *wl_drm_ctx;
209 struct drm_state *drm_state;
210 struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
211 struct wl_display *wrapped_display = NULL;
213 wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
215 va_wayland_error("could not allocate wl_drm_ctx");
218 wl_drm_ctx->base.destroy = va_wayland_drm_destroy;
219 wl_drm_ctx->queue = NULL;
220 wl_drm_ctx->drm = NULL;
221 wl_drm_ctx->registry = NULL;
222 wl_drm_ctx->is_authenticated = 0;
223 pDisplayContext->opaque = wl_drm_ctx;
224 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
226 drm_state = calloc(1, sizeof(struct drm_state));
228 va_wayland_error("could not allocate drm_state");
232 drm_state->auth_type = 0;
233 ctx->drm_state = drm_state;
234 vtable->has_prime_sharing = 0;
236 /* Use wrapped wl_display with private event queue to prevent
237 * thread safety issues with applications that e.g. run an event pump
238 * parallel to libva initialization.
239 * Using the default queue, events might get lost and crashes occur
240 * because wl_display_roundtrip is not thread-safe with respect to the
243 wl_drm_ctx->queue = wl_display_create_queue(ctx->native_dpy);
244 if (!wl_drm_ctx->queue) {
245 va_wayland_error("could not create Wayland event queue");
249 wrapped_display = wl_proxy_create_wrapper(ctx->native_dpy);
250 if (!wrapped_display) {
251 va_wayland_error("could not create Wayland proxy wrapper");
255 /* All created objects will inherit this queue */
256 wl_proxy_set_queue((struct wl_proxy *) wrapped_display, wl_drm_ctx->queue);
257 wl_drm_ctx->registry = wl_display_get_registry(wrapped_display);
258 if (!wl_drm_ctx->registry) {
259 va_wayland_error("could not create wl_registry");
262 if (wl_registry_add_listener(wl_drm_ctx->registry, ®istry_listener, wl_drm_ctx) != 0) {
263 va_wayland_error("could not add listener to wl_registry");
266 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
269 /* registry_handle_global should have been called by the
270 * wl_display_roundtrip_queue above
273 /* Do not print an error, the compositor might just not support wl_drm */
274 if (!wl_drm_ctx->drm)
277 if (wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext) != 0) {
278 va_wayland_error("could not add listener to wl_drm");
281 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
283 if (drm_state->fd < 0) {
284 va_wayland_error("did not get DRM device");
288 if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
291 if (!wl_drm_ctx->is_authenticated) {
292 va_wayland_error("Wayland compositor did not respond to DRM authentication");
299 if (wrapped_display) {
300 wl_proxy_wrapper_destroy(wrapped_display);
301 wrapped_display = NULL;
305 if (wl_drm_ctx->drm) {
306 wl_drm_destroy(wl_drm_ctx->drm);
307 wl_drm_ctx->drm = NULL;
310 if (wl_drm_ctx->registry) {
311 wl_registry_destroy(wl_drm_ctx->registry);
312 wl_drm_ctx->registry = NULL;
315 if (wl_drm_ctx->queue) {
316 wl_event_queue_destroy(wl_drm_ctx->queue);
317 wl_drm_ctx->queue = NULL;