OSDN Git Service

wayland: Also support wl_drm version 1
[android-x86/hardware-intel-common-libva.git] / va / wayland / va_wayland_drm.c
1 /*
2  * va_wayland_drm.c - Wayland/DRM helpers
3  *
4  * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
5  *
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:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
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.
25  */
26
27 #include "sysdeps.h"
28 #include <unistd.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <dlfcn.h>
32 #include <sys/stat.h>
33 #include <xf86drm.h>
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"
39
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"
45
46 typedef struct va_wayland_drm_context {
47     struct va_wayland_context   base;
48     void                       *handle;
49     struct wl_event_queue      *queue;
50     struct wl_drm              *drm;
51     struct wl_registry         *registry;
52     void                       *drm_interface;
53     unsigned int                is_authenticated        : 1;
54 } VADisplayContextWaylandDRM;
55
56 static void
57 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
58 {
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;
63     drm_magic_t magic;
64     struct stat st;
65
66     if (stat(device, &st) < 0) {
67         va_wayland_error("failed to identify %s: %s (errno %d)",
68                          device, strerror(errno), errno);
69         return;
70     }
71
72     if (!S_ISCHR(st.st_mode)) {
73         va_wayland_error("%s is not a device", device);
74         return;
75     }
76
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);
81         return;
82     }
83
84     drmGetMagic(drm_state->fd, &magic);
85     wl_drm_authenticate(wl_drm_ctx->drm, magic);
86 }
87
88 static void
89 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
90 {
91 }
92
93 static void
94 drm_handle_authenticated(void *data, struct wl_drm *drm)
95 {
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;
100
101     wl_drm_ctx->is_authenticated = 1;
102     drm_state->auth_type         = VA_DRM_AUTH_CUSTOM;
103 }
104
105 static void
106 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
107 {
108     VADisplayContextP const pDisplayContext = data;
109     VADriverContextP const ctx = pDisplayContext->pDriverContext;
110     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
111
112     vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
113 }
114
115 static const struct wl_drm_listener drm_listener = {
116     drm_handle_device,
117     drm_handle_format,
118     drm_handle_authenticated,
119     drm_handle_capabilities,
120 };
121
122 static VAStatus
123 va_DisplayContextGetDriverName(
124     VADisplayContextP pDisplayContext,
125     char            **driver_name_ptr
126 )
127 {
128     VADriverContextP const ctx = pDisplayContext->pDriverContext;
129
130     return VA_DRM_GetDriverName(ctx, driver_name_ptr);
131 }
132
133 void
134 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
135 {
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;
140
141     vtable->has_prime_sharing = 0;
142
143     if (wl_drm_ctx->drm) {
144         wl_drm_destroy(wl_drm_ctx->drm);
145         wl_drm_ctx->drm = NULL;
146     }
147     wl_drm_ctx->is_authenticated = 0;
148
149     if (wl_drm_ctx->registry) {
150         wl_registry_destroy(wl_drm_ctx->registry);
151         wl_drm_ctx->registry = NULL;
152     }
153
154     if (wl_drm_ctx->queue) {
155         wl_event_queue_destroy(wl_drm_ctx->queue);
156         wl_drm_ctx->queue = NULL;
157     }
158
159     if (wl_drm_ctx->handle) {
160         dlclose(wl_drm_ctx->handle);
161         wl_drm_ctx->handle = NULL;
162     }
163
164     if (drm_state) {
165         if (drm_state->fd >= 0) {
166             close(drm_state->fd);
167             drm_state->fd = -1;
168         }
169         free(ctx->drm_state);
170         ctx->drm_state = NULL;
171     }
172 }
173
174 static void
175 registry_handle_global(
176     void               *data,
177     struct wl_registry *registry,
178     uint32_t            id,
179     const char         *interface,
180     uint32_t            version
181 )
182 {
183     struct va_wayland_drm_context *wl_drm_ctx = data;
184
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
188          */
189         wl_drm_ctx->drm =
190             wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface,
191                              (version < 2) ? version : 2);
192     }
193 }
194
195 static const struct wl_registry_listener registry_listener = {
196     registry_handle_global,
197     NULL,
198 };
199
200 static bool
201 wayland_roundtrip_queue(struct wl_display *display,
202                          struct wl_event_queue *queue)
203 {
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);
207         return false;
208     } else {
209         return true;
210     }
211 }
212
213 bool
214 va_wayland_drm_create(VADisplayContextP pDisplayContext)
215 {
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;
221
222     wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
223     if (!wl_drm_ctx) {
224         va_wayland_error("could not allocate wl_drm_ctx");
225         return false;
226     }
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;
236
237     drm_state = calloc(1, sizeof(struct drm_state));
238     if (!drm_state) {
239         va_wayland_error("could not allocate drm_state");
240         return false;
241     }
242     drm_state->fd        = -1;
243     drm_state->auth_type = 0;
244     ctx->drm_state       = drm_state;
245     vtable->has_prime_sharing = 0;
246
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)
251             return false;
252     }
253
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");
258         return false;
259     }
260
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
266      * same queue.
267      */
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");
271         return false;
272     }
273
274     wrapped_display = wl_proxy_create_wrapper(ctx->native_dpy);
275     if (!wrapped_display) {
276         va_wayland_error("could not create Wayland proxy wrapper");
277         return false;
278     }
279
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, &registry_listener, wl_drm_ctx);
285     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
286         return false;
287
288     /* registry_handle_global should have been called by the
289      * wl_display_roundtrip_queue above
290      */
291
292     /* Do not print an error, the compositor might just not support wl_drm */
293     if (!wl_drm_ctx->drm)
294         return false;
295
296     wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext);
297     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
298         return false;
299     if (drm_state->fd < 0) {
300         va_wayland_error("did not get DRM device");
301         return false;
302     }
303
304     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
305         return false;
306
307     if (!wl_drm_ctx->is_authenticated) {
308         va_wayland_error("Wayland compositor did not respond to DRM authentication");
309         return false;
310     }
311
312     return true;
313 }