wayland: add initial support for EMGD driver.
[profile/ivi/libva.git] / va / wayland / va_wayland_emgd.c
1 /*
2  * va_wayland_emgd.c - Wayland/EMGD 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 <dlfcn.h>
30 #include "va_drmcommon.h"
31 #include "va_wayland_emgd.h"
32 #include "va_wayland_private.h"
33
34 /* XXX: Wayland/EMGD support currently lives in libwayland-emgd.so.* library */
35 #define LIBWAYLAND_EMGD_NAME "libwayland-emgd.so.1"
36
37 typedef struct va_wayland_emgd_context {
38     struct va_wayland_context   base;
39     void                       *handle;
40     struct wl_emgd             *emgd;
41     void                       *emgd_interface;
42     unsigned int                is_created      : 1;
43 } VADisplayContextWaylandEMGD;
44
45 static inline void
46 wl_emgd_destroy(struct wl_emgd *emgd)
47 {
48     wl_proxy_destroy((struct wl_proxy *)emgd);
49 }
50
51 static VAStatus
52 va_DisplayContextGetDriverName(
53     VADisplayContextP pDisplayContext,
54     char            **driver_name_ptr
55 )
56 {
57     *driver_name_ptr = strdup("emgd");
58     return VA_STATUS_SUCCESS;
59 }
60
61 void
62 va_wayland_emgd_destroy(VADisplayContextP pDisplayContext)
63 {
64     VADriverContextP const ctx = pDisplayContext->pDriverContext;
65     VADisplayContextWaylandEMGD * const wl_emgd_ctx = pDisplayContext->opaque;
66     struct drm_state * const drm_state = ctx->drm_state;
67
68     if (wl_emgd_ctx->emgd) {
69         wl_emgd_destroy(wl_emgd_ctx->emgd);
70         wl_emgd_ctx->emgd = NULL;
71     }
72     wl_emgd_ctx->is_created = 0;
73
74     if (wl_emgd_ctx->handle) {
75         dlclose(wl_emgd_ctx->handle);
76         wl_emgd_ctx->handle = NULL;
77     }
78
79     if (drm_state) {
80         if (drm_state->fd >= 0) {
81             close(drm_state->fd);
82             drm_state->fd = -1;
83         }
84         free(ctx->drm_state);
85         ctx->drm_state = NULL;
86     }
87 }
88
89 bool
90 va_wayland_emgd_create(VADisplayContextP pDisplayContext)
91 {
92     VADriverContextP const ctx = pDisplayContext->pDriverContext;
93     VADisplayContextWaylandEMGD *wl_emgd_ctx;
94     struct drm_state *drm_state;
95     uint32_t id;
96
97     wl_emgd_ctx = malloc(sizeof(*wl_emgd_ctx));
98     if (!wl_emgd_ctx)
99         return false;
100     wl_emgd_ctx->base.destroy           = va_wayland_emgd_destroy;
101     wl_emgd_ctx->handle                 = NULL;
102     wl_emgd_ctx->emgd                   = NULL;
103     wl_emgd_ctx->emgd_interface         = NULL;
104     wl_emgd_ctx->is_created             = 0;
105     pDisplayContext->opaque             = wl_emgd_ctx;
106     pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;
107
108     drm_state = calloc(1, sizeof(struct drm_state));
109     if (!drm_state)
110         return false;
111     drm_state->fd        = -1;
112     drm_state->auth_type = 0;
113     ctx->drm_state       = drm_state;
114
115     id = wl_display_get_global(ctx->native_dpy, "wl_emgd", 1);
116     if (!id) {
117         wl_display_roundtrip(ctx->native_dpy);
118         id = wl_display_get_global(ctx->native_dpy, "wl_emgd", 1);
119         if (!id)
120             return false;
121     }
122
123     wl_emgd_ctx->handle = dlopen(LIBWAYLAND_EMGD_NAME, RTLD_LAZY|RTLD_LOCAL);
124     if (!wl_emgd_ctx->handle)
125         return false;
126
127     wl_emgd_ctx->emgd_interface =
128         dlsym(wl_emgd_ctx->handle, "wl_emgd_interface");
129     if (!wl_emgd_ctx->emgd_interface)
130         return false;
131
132     wl_emgd_ctx->emgd =
133         wl_display_bind(ctx->native_dpy, id, wl_emgd_ctx->emgd_interface);
134     if (!wl_emgd_ctx->emgd)
135         return false;
136     return true;
137 }