OSDN Git Service

8e2269517060ab04133cdd7ab1032a0abcaaea3e
[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 typedef struct va_wayland_drm_context {
41     struct va_wayland_context   base;
42     struct wl_event_queue      *queue;
43     struct wl_drm              *drm;
44     uint32_t                    drm_name;
45     struct wl_registry         *registry;
46     unsigned int                is_authenticated        : 1;
47 } VADisplayContextWaylandDRM;
48
49 static void
50 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
51 {
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;
56     drm_magic_t magic;
57     struct stat st;
58     int fd = -1;
59
60     fd = open(device, O_RDWR);
61     if (fd < 0) {
62         va_wayland_error("failed to open %s: %s (errno %d)",
63                          device, strerror(errno), errno);
64         return;
65     }
66
67     if (fstat(fd, &st) < 0) {
68         va_wayland_error("failed to identify %s: %s (errno %d)",
69                          device, strerror(errno), errno);
70         close(fd);
71         return;
72     }
73
74     if (!S_ISCHR(st.st_mode)) {
75         va_wayland_error("%s is not a device", device);
76         close(fd);
77         return;
78     }
79
80     drm_state->fd = fd;
81     drmGetMagic(drm_state->fd, &magic);
82     wl_drm_authenticate(wl_drm_ctx->drm, magic);
83 }
84
85 static void
86 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
87 {
88 }
89
90 static void
91 drm_handle_authenticated(void *data, struct wl_drm *drm)
92 {
93     VADisplayContextP const pDisplayContext = data;
94     VADriverContextP const ctx = pDisplayContext->pDriverContext;
95     VADisplayContextWaylandDRM * const wl_drm_ctx = pDisplayContext->opaque;
96     struct drm_state * const drm_state = ctx->drm_state;
97
98     wl_drm_ctx->is_authenticated = 1;
99     drm_state->auth_type         = VA_DRM_AUTH_CUSTOM;
100 }
101
102 static void
103 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
104 {
105     VADisplayContextP const pDisplayContext = data;
106     VADriverContextP const ctx = pDisplayContext->pDriverContext;
107     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
108
109     vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
110 }
111
112 static const struct wl_drm_listener drm_listener = {
113     drm_handle_device,
114     drm_handle_format,
115     drm_handle_authenticated,
116     drm_handle_capabilities,
117 };
118
119 static VAStatus
120 va_DisplayContextGetDriverName(
121     VADisplayContextP pDisplayContext,
122     char            **driver_name_ptr
123 )
124 {
125     VADriverContextP const ctx = pDisplayContext->pDriverContext;
126
127     return VA_DRM_GetDriverName(ctx, driver_name_ptr);
128 }
129
130 void
131 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
132 {
133     VADriverContextP const ctx = pDisplayContext->pDriverContext;
134     struct va_wayland_drm_context * const wl_drm_ctx = pDisplayContext->opaque;
135     struct drm_state * const drm_state = ctx->drm_state;
136     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
137
138     vtable->has_prime_sharing = 0;
139
140     wl_drm_ctx->is_authenticated = 0;
141
142     if (drm_state) {
143         if (drm_state->fd >= 0) {
144             close(drm_state->fd);
145             drm_state->fd = -1;
146         }
147         free(ctx->drm_state);
148         ctx->drm_state = NULL;
149     }
150 }
151
152 static void
153 registry_handle_global(
154     void               *data,
155     struct wl_registry *registry,
156     uint32_t            name,
157     const char         *interface,
158     uint32_t            version
159 )
160 {
161     VADisplayContextP const pDisplayContext = data;
162     struct va_wayland_drm_context * const wl_drm_ctx = pDisplayContext->opaque;
163
164     if (strcmp(interface, "wl_drm") == 0) {
165         /* bind to at most version 2, but also support version 1 if
166          * compositor does not have v2
167          */
168         wl_drm_ctx->drm_name = name;
169         wl_drm_ctx->drm =
170             wl_registry_bind(wl_drm_ctx->registry, name, &wl_drm_interface,
171                              (version < 2) ? version : 2);
172
173         if (wl_drm_ctx->drm
174             && wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext) != 0) {
175             va_wayland_error("could not add listener to wl_drm");
176             wl_drm_destroy(wl_drm_ctx->drm);
177             wl_drm_ctx->drm = NULL;
178         }
179     }
180 }
181
182 static void
183 registry_handle_global_remove(
184     void               *data,
185     struct wl_registry *registry,
186     uint32_t            name
187 )
188 {
189     struct va_wayland_drm_context *wl_drm_ctx = data;
190
191     if (wl_drm_ctx->drm && name == wl_drm_ctx->drm_name) {
192         wl_drm_destroy(wl_drm_ctx->drm);
193         wl_drm_ctx->drm = NULL;
194     }
195 }
196
197 static const struct wl_registry_listener registry_listener = {
198     registry_handle_global,
199     registry_handle_global_remove
200 };
201
202 static bool
203 wayland_roundtrip_queue(struct wl_display *display,
204                          struct wl_event_queue *queue)
205 {
206     if (wl_display_roundtrip_queue(display, queue) < 0) {
207         int err = wl_display_get_error(display);
208         va_wayland_error("Wayland roundtrip error: %s (errno %d)", strerror(err), err);
209         return false;
210     } else {
211         return true;
212     }
213 }
214
215 bool
216 va_wayland_drm_create(VADisplayContextP pDisplayContext)
217 {
218     bool result = false;
219     VADriverContextP const ctx = pDisplayContext->pDriverContext;
220     struct va_wayland_drm_context *wl_drm_ctx;
221     struct drm_state *drm_state;
222     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
223     struct wl_display *wrapped_display = NULL;
224
225     wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
226     if (!wl_drm_ctx) {
227         va_wayland_error("could not allocate wl_drm_ctx");
228         goto end;
229     }
230     wl_drm_ctx->base.destroy            = va_wayland_drm_destroy;
231     wl_drm_ctx->queue                   = NULL;
232     wl_drm_ctx->drm                     = NULL;
233     wl_drm_ctx->registry                = NULL;
234     wl_drm_ctx->is_authenticated        = 0;
235     pDisplayContext->opaque             = wl_drm_ctx;
236     pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;
237
238     drm_state = calloc(1, sizeof(struct drm_state));
239     if (!drm_state) {
240         va_wayland_error("could not allocate drm_state");
241         goto end;
242     }
243     drm_state->fd        = -1;
244     drm_state->auth_type = 0;
245     ctx->drm_state       = drm_state;
246     vtable->has_prime_sharing = 0;
247
248     /* Use wrapped wl_display with private event queue to prevent
249      * thread safety issues with applications that e.g. run an event pump
250      * parallel to libva initialization.
251      * Using the default queue, events might get lost and crashes occur
252      * because wl_display_roundtrip is not thread-safe with respect to the
253      * same queue.
254      */
255     wl_drm_ctx->queue = wl_display_create_queue(ctx->native_dpy);
256     if (!wl_drm_ctx->queue) {
257         va_wayland_error("could not create Wayland event queue");
258         goto end;
259     }
260
261     wrapped_display = wl_proxy_create_wrapper(ctx->native_dpy);
262     if (!wrapped_display) {
263         va_wayland_error("could not create Wayland proxy wrapper");
264         goto end;
265     }
266
267     /* All created objects will inherit this queue */
268     wl_proxy_set_queue((struct wl_proxy *) wrapped_display, wl_drm_ctx->queue);
269     wl_drm_ctx->registry = wl_display_get_registry(wrapped_display);
270     if (!wl_drm_ctx->registry) {
271         va_wayland_error("could not create wl_registry");
272         goto end;
273     }
274     if (wl_registry_add_listener(wl_drm_ctx->registry, &registry_listener, pDisplayContext) != 0) {
275         va_wayland_error("could not add listener to wl_registry");
276         goto end;
277     }
278     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
279         goto end;
280
281     /* registry_handle_global should have been called by the
282      * wl_display_roundtrip_queue above
283      */
284
285     /* Do not print an error, the compositor might just not support wl_drm */
286     if (!wl_drm_ctx->drm)
287         goto end;
288     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
289         goto end;
290     if (drm_state->fd < 0) {
291         va_wayland_error("did not get DRM device");
292         goto end;
293     }
294
295     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
296         goto end;
297
298     if (!wl_drm_ctx->is_authenticated) {
299         va_wayland_error("Wayland compositor did not respond to DRM authentication");
300         goto end;
301     }
302
303     result = true;
304
305 end:
306     if (wrapped_display) {
307         wl_proxy_wrapper_destroy(wrapped_display);
308         wrapped_display = NULL;
309     }
310
311     if (wl_drm_ctx) {
312         if (wl_drm_ctx->drm) {
313             wl_drm_destroy(wl_drm_ctx->drm);
314             wl_drm_ctx->drm = NULL;
315         }
316
317         if (wl_drm_ctx->registry) {
318             wl_registry_destroy(wl_drm_ctx->registry);
319             wl_drm_ctx->registry = NULL;
320         }
321
322         if (wl_drm_ctx->queue) {
323             wl_event_queue_destroy(wl_drm_ctx->queue);
324             wl_drm_ctx->queue = NULL;
325         }
326     }
327
328     return result;
329 }