OSDN Git Service

552f2432309a470c30b60ee0f28d4b9f60d70244
[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
59     if (stat(device, &st) < 0) {
60         va_wayland_error("failed to identify %s: %s (errno %d)",
61                          device, strerror(errno), errno);
62         return;
63     }
64
65     if (!S_ISCHR(st.st_mode)) {
66         va_wayland_error("%s is not a device", device);
67         return;
68     }
69
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);
74         return;
75     }
76
77     drmGetMagic(drm_state->fd, &magic);
78     wl_drm_authenticate(wl_drm_ctx->drm, magic);
79 }
80
81 static void
82 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
83 {
84 }
85
86 static void
87 drm_handle_authenticated(void *data, struct wl_drm *drm)
88 {
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;
93
94     wl_drm_ctx->is_authenticated = 1;
95     drm_state->auth_type         = VA_DRM_AUTH_CUSTOM;
96 }
97
98 static void
99 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
100 {
101     VADisplayContextP const pDisplayContext = data;
102     VADriverContextP const ctx = pDisplayContext->pDriverContext;
103     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
104
105     vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
106 }
107
108 static const struct wl_drm_listener drm_listener = {
109     drm_handle_device,
110     drm_handle_format,
111     drm_handle_authenticated,
112     drm_handle_capabilities,
113 };
114
115 static VAStatus
116 va_DisplayContextGetDriverName(
117     VADisplayContextP pDisplayContext,
118     char            **driver_name_ptr
119 )
120 {
121     VADriverContextP const ctx = pDisplayContext->pDriverContext;
122
123     return VA_DRM_GetDriverName(ctx, driver_name_ptr);
124 }
125
126 void
127 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
128 {
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;
133
134     vtable->has_prime_sharing = 0;
135
136     wl_drm_ctx->is_authenticated = 0;
137
138     if (drm_state) {
139         if (drm_state->fd >= 0) {
140             close(drm_state->fd);
141             drm_state->fd = -1;
142         }
143         free(ctx->drm_state);
144         ctx->drm_state = NULL;
145     }
146 }
147
148 static void
149 registry_handle_global(
150     void               *data,
151     struct wl_registry *registry,
152     uint32_t            name,
153     const char         *interface,
154     uint32_t            version
155 )
156 {
157     struct va_wayland_drm_context *wl_drm_ctx = data;
158
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
162          */
163         wl_drm_ctx->drm_name = name;
164         wl_drm_ctx->drm =
165             wl_registry_bind(wl_drm_ctx->registry, name, &wl_drm_interface,
166                              (version < 2) ? version : 2);
167     }
168 }
169
170 static void
171 registry_handle_global_remove(
172     void               *data,
173     struct wl_registry *registry,
174     uint32_t            name
175 )
176 {
177     struct va_wayland_drm_context *wl_drm_ctx = data;
178
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;
182     }
183 }
184
185 static const struct wl_registry_listener registry_listener = {
186     registry_handle_global,
187     registry_handle_global_remove
188 };
189
190 static bool
191 wayland_roundtrip_queue(struct wl_display *display,
192                          struct wl_event_queue *queue)
193 {
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);
197         return false;
198     } else {
199         return true;
200     }
201 }
202
203 bool
204 va_wayland_drm_create(VADisplayContextP pDisplayContext)
205 {
206     bool result = false;
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;
212
213     wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
214     if (!wl_drm_ctx) {
215         va_wayland_error("could not allocate wl_drm_ctx");
216         goto end;
217     }
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;
225
226     drm_state = calloc(1, sizeof(struct drm_state));
227     if (!drm_state) {
228         va_wayland_error("could not allocate drm_state");
229         goto end;
230     }
231     drm_state->fd        = -1;
232     drm_state->auth_type = 0;
233     ctx->drm_state       = drm_state;
234     vtable->has_prime_sharing = 0;
235
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
241      * same queue.
242      */
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");
246         goto end;
247     }
248
249     wrapped_display = wl_proxy_create_wrapper(ctx->native_dpy);
250     if (!wrapped_display) {
251         va_wayland_error("could not create Wayland proxy wrapper");
252         goto end;
253     }
254
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");
260         goto end;
261     }
262     if (wl_registry_add_listener(wl_drm_ctx->registry, &registry_listener, wl_drm_ctx) != 0) {
263         va_wayland_error("could not add listener to wl_registry");
264         goto end;
265     }
266     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
267         goto end;
268
269     /* registry_handle_global should have been called by the
270      * wl_display_roundtrip_queue above
271      */
272
273     /* Do not print an error, the compositor might just not support wl_drm */
274     if (!wl_drm_ctx->drm)
275         goto end;
276
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");
279         goto end;
280     }
281     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
282         goto end;
283     if (drm_state->fd < 0) {
284         va_wayland_error("did not get DRM device");
285         goto end;
286     }
287
288     if (!wayland_roundtrip_queue(ctx->native_dpy, wl_drm_ctx->queue))
289         goto end;
290
291     if (!wl_drm_ctx->is_authenticated) {
292         va_wayland_error("Wayland compositor did not respond to DRM authentication");
293         goto end;
294     }
295
296     result = true;
297
298 end:
299     if (wrapped_display) {
300         wl_proxy_wrapper_destroy(wrapped_display);
301         wrapped_display = NULL;
302     }
303
304     if (wl_drm_ctx) {
305         if (wl_drm_ctx->drm) {
306             wl_drm_destroy(wl_drm_ctx->drm);
307             wl_drm_ctx->drm = NULL;
308         }
309
310         if (wl_drm_ctx->registry) {
311             wl_registry_destroy(wl_drm_ctx->registry);
312             wl_drm_ctx->registry = NULL;
313         }
314
315         if (wl_drm_ctx->queue) {
316             wl_event_queue_destroy(wl_drm_ctx->queue);
317             wl_drm_ctx->queue = NULL;
318         }
319     }
320
321     return result;
322 }