OSDN Git Service

wayland: Fix uninitialized registry
[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->handle) {
149         dlclose(wl_drm_ctx->handle);
150         wl_drm_ctx->handle = NULL;
151     }
152
153     if (drm_state) {
154         if (drm_state->fd >= 0) {
155             close(drm_state->fd);
156             drm_state->fd = -1;
157         }
158         free(ctx->drm_state);
159         ctx->drm_state = NULL;
160     }
161 }
162
163 static void
164 registry_handle_global(
165     void               *data,
166     struct wl_registry *registry,
167     uint32_t            id,
168     const char         *interface,
169     uint32_t            version
170 )
171 {
172     struct va_wayland_drm_context *wl_drm_ctx = data;
173
174     if (strcmp(interface, "wl_drm") == 0) {
175         wl_drm_ctx->drm =
176             wl_registry_bind(wl_drm_ctx->registry, id, wl_drm_ctx->drm_interface, 2);
177     }
178 }
179
180 static const struct wl_registry_listener registry_listener = {
181     registry_handle_global,
182     NULL,
183 };
184
185 bool
186 va_wayland_drm_create(VADisplayContextP pDisplayContext)
187 {
188     VADriverContextP const ctx = pDisplayContext->pDriverContext;
189     struct va_wayland_drm_context *wl_drm_ctx;
190     struct drm_state *drm_state;
191     struct VADriverVTableWayland *vtable = ctx->vtable_wayland;
192
193     wl_drm_ctx = malloc(sizeof(*wl_drm_ctx));
194     if (!wl_drm_ctx)
195         return false;
196     wl_drm_ctx->base.destroy            = va_wayland_drm_destroy;
197     wl_drm_ctx->handle                  = NULL;
198     wl_drm_ctx->drm                     = NULL;
199     wl_drm_ctx->drm_interface           = NULL;
200     wl_drm_ctx->registry                = NULL;
201     wl_drm_ctx->is_authenticated        = 0;
202     pDisplayContext->opaque             = wl_drm_ctx;
203     pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;
204
205     drm_state = calloc(1, sizeof(struct drm_state));
206     if (!drm_state)
207         return false;
208     drm_state->fd        = -1;
209     drm_state->auth_type = 0;
210     ctx->drm_state       = drm_state;
211     vtable->has_prime_sharing = 0;
212
213     wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME, RTLD_LAZY|RTLD_LOCAL);
214     if (!wl_drm_ctx->handle) {
215         wl_drm_ctx->handle = dlopen(LIBWAYLAND_DRM_NAME_FALLBACK, RTLD_LAZY|RTLD_LOCAL);
216         if (!wl_drm_ctx->handle)
217             return false;
218     }
219
220     wl_drm_ctx->drm_interface =
221         dlsym(wl_drm_ctx->handle, "wl_drm_interface");
222     if (!wl_drm_ctx->drm_interface)
223         return false;
224
225     wl_drm_ctx->registry = wl_display_get_registry(ctx->native_dpy);
226     wl_registry_add_listener(wl_drm_ctx->registry, &registry_listener, wl_drm_ctx);
227     wl_display_roundtrip(ctx->native_dpy);
228
229     /* registry_handle_global should have been called by the
230      * wl_display_roundtrip above
231      */
232
233     if (!wl_drm_ctx->drm)
234         return false;
235
236     wl_drm_add_listener(wl_drm_ctx->drm, &drm_listener, pDisplayContext);
237     wl_display_roundtrip(ctx->native_dpy);
238     if (drm_state->fd < 0)
239         return false;
240
241     wl_display_roundtrip(ctx->native_dpy);
242     if (!wl_drm_ctx->is_authenticated)
243         return false;
244     return true;
245 }