OSDN Git Service

wayland: Cleanup wl_registry in va_wayland_drm_destroy
[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_drm              *drm;
50     struct wl_registry         *registry;
51     void                       *drm_interface;
52     unsigned int                is_authenticated        : 1;
53 } VADisplayContextWaylandDRM;
54
55 static void
56 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
57 {
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;
62     drm_magic_t magic;
63     struct stat st;
64
65     if (stat(device, &st) < 0) {
66         va_wayland_error("failed to identify %s: %s (errno %d)",
67                          device, strerror(errno), errno);
68         return;
69     }
70
71     if (!S_ISCHR(st.st_mode)) {
72         va_wayland_error("%s is not a device", device);
73         return;
74     }
75
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);
80         return;
81     }
82
83     drmGetMagic(drm_state->fd, &magic);
84     wl_drm_authenticate(wl_drm_ctx->drm, magic);
85 }
86
87 static void
88 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
89 {
90 }
91
92 static void
93 drm_handle_authenticated(void *data, struct wl_drm *drm)
94 {
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;
99
100     wl_drm_ctx->is_authenticated = 1;
101     drm_state->auth_type         = VA_DRM_AUTH_CUSTOM;
102 }
103
104 static void
105 drm_handle_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
106 {
107     VADisplayContextP const pDisplayContext = data;
108     VADriverContextP const ctx = pDisplayContext->pDriverContext;
109     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
110
111     vtable->has_prime_sharing = !!(value & WL_DRM_CAPABILITY_PRIME);
112 }
113
114 static const struct wl_drm_listener drm_listener = {
115     drm_handle_device,
116     drm_handle_format,
117     drm_handle_authenticated,
118     drm_handle_capabilities,
119 };
120
121 static VAStatus
122 va_DisplayContextGetDriverName(
123     VADisplayContextP pDisplayContext,
124     char            **driver_name_ptr
125 )
126 {
127     VADriverContextP const ctx = pDisplayContext->pDriverContext;
128
129     return VA_DRM_GetDriverName(ctx, driver_name_ptr);
130 }
131
132 void
133 va_wayland_drm_destroy(VADisplayContextP pDisplayContext)
134 {
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;
139
140     vtable->has_prime_sharing = 0;
141
142     if (wl_drm_ctx->drm) {
143         wl_drm_destroy(wl_drm_ctx->drm);
144         wl_drm_ctx->drm = NULL;
145     }
146     wl_drm_ctx->is_authenticated = 0;
147
148     if (wl_drm_ctx->registry) {
149         wl_registry_destroy(wl_drm_ctx->registry);
150         wl_drm_ctx->registry = NULL;
151     }
152
153     if (wl_drm_ctx->handle) {
154         dlclose(wl_drm_ctx->handle);
155         wl_drm_ctx->handle = NULL;
156     }
157
158     if (drm_state) {
159         if (drm_state->fd >= 0) {
160             close(drm_state->fd);
161             drm_state->fd = -1;
162         }
163         free(ctx->drm_state);
164         ctx->drm_state = NULL;
165     }
166 }
167
168 static void
169 registry_handle_global(
170     void               *data,
171     struct wl_registry *registry,
172     uint32_t            id,
173     const char         *interface,
174     uint32_t            version
175 )
176 {
177     struct va_wayland_drm_context *wl_drm_ctx = data;
178
179     if (strcmp(interface, "wl_drm") == 0) {
180         wl_drm_ctx->drm =
181             wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface, 2);
182     }
183 }
184
185 static const struct wl_registry_listener registry_listener = {
186     registry_handle_global,
187     NULL,
188 };
189
190 bool
191 va_wayland_drm_create(VADisplayContextP pDisplayContext)
192 {
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;
197
198     wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
199     if (!wl_drm_ctx)
200         return false;
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;
209
210     drm_state = calloc(1, sizeof(struct drm_state));
211     if (!drm_state)
212         return false;
213     drm_state->fd        = -1;
214     drm_state->auth_type = 0;
215     ctx->drm_state       = drm_state;
216     vtable->has_prime_sharing = 0;
217
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)
222             return false;
223     }
224
225     wl_drm_ctx->drm_interface =
226         dlsym(wl_drm_ctx->handle, "wl_drm_interface");
227     if (!wl_drm_ctx->drm_interface)
228         return false;
229
230     wl_drm_ctx->registry = wl_display_get_registry(ctx->native_dpy);
231     wl_registry_add_listener(wl_drm_ctx->registry, &registry_listener, wl_drm_ctx);
232     wl_display_roundtrip(ctx->native_dpy);
233
234     /* registry_handle_global should have been called by the
235      * wl_display_roundtrip above
236      */
237
238     if (!wl_drm_ctx->drm)
239         return false;
240
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)
244         return false;
245
246     wl_display_roundtrip(ctx->native_dpy);
247     if (!wl_drm_ctx->is_authenticated)
248         return false;
249     return true;
250 }