OSDN Git Service

Move driver context allocation to common code
[android-x86/hardware-intel-common-libva.git] / va / wayland / va_wayland.c
1 /*
2  * va_wayland.c - Wayland API
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 <stdarg.h>
29 #include "va_wayland.h"
30 #include "va_wayland_drm.h"
31 #include "va_wayland_emgd.h"
32 #include "va_wayland_private.h"
33 #include "va_backend.h"
34 #include "va_backend_wayland.h"
35 #include "va_internal.h"
36
37 static inline VADriverContextP
38 get_driver_context(VADisplay dpy)
39 {
40     if (!vaDisplayIsValid(dpy))
41         return NULL;
42     return ((VADisplayContextP)dpy)->pDriverContext;
43 }
44
45 void
46 va_wayland_error(const char *format, ...)
47 {
48     va_list args;
49
50     va_start(args, format);
51     fprintf(stderr, "VA error: wayland: ");
52     vfprintf(stderr, format, args);
53     fprintf(stderr, "\n");
54     va_end(args);
55 }
56
57 static int
58 va_DisplayContextIsValid(VADisplayContextP pDisplayContext)
59 {
60     VADriverContextP const pDriverContext = pDisplayContext->pDriverContext;
61
62     return (pDriverContext &&
63             pDriverContext->display_type == VA_DISPLAY_WAYLAND);
64 }
65
66 static void
67 va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
68 {
69     VADriverContextP pDriverContext;
70     VADisplayContextWaylandP pDisplayContextWl;
71
72     if (!pDisplayContext)
73         return;
74
75     pDisplayContextWl = pDisplayContext->opaque;
76     if (pDisplayContextWl && pDisplayContextWl->destroy)
77         pDisplayContextWl->destroy(pDisplayContext);
78
79     pDriverContext = pDisplayContext->pDriverContext;
80     if (pDriverContext) {
81         free(pDriverContext->vtable_wayland);
82         pDriverContext->vtable_wayland = NULL;
83         free(pDriverContext);
84         pDisplayContext->pDriverContext = NULL;
85     }
86
87     free(pDisplayContext->opaque);
88     pDisplayContext->opaque = NULL;
89     free(pDisplayContext);
90 }
91
92 static VAStatus
93 va_DisplayContextGetDriverName(VADisplayContextP pDisplayContext, char **name)
94 {
95     *name = NULL;
96     return VA_STATUS_ERROR_UNKNOWN;
97 }
98
99 /* -------------------------------------------------------------------------- */
100 /* --- Public interface                                                   --- */
101 /* -------------------------------------------------------------------------- */
102
103 struct va_wayland_backend {
104     VADisplayContextCreateFunc  create;
105     VADisplayContextDestroyFunc destroy;
106 };
107
108 static const struct va_wayland_backend g_backends[] = {
109     { va_wayland_drm_create,
110       va_wayland_drm_destroy },
111     { va_wayland_emgd_create,
112       va_wayland_emgd_destroy },
113     { NULL, }
114 };
115
116 VADisplay
117 vaGetDisplayWl(struct wl_display *display)
118 {
119     VADisplayContextP pDisplayContext = NULL;
120     VADriverContextP pDriverContext;
121     struct VADriverVTableWayland *vtable;
122     unsigned int i;
123
124     pDisplayContext = va_newDisplayContext();
125     if (!pDisplayContext)
126         return NULL;
127
128     pDisplayContext->vaIsValid          = va_DisplayContextIsValid;
129     pDisplayContext->vaDestroy          = va_DisplayContextDestroy;
130     pDisplayContext->vaGetDriverName    = va_DisplayContextGetDriverName;
131
132     pDriverContext = va_newDriverContext(pDisplayContext);
133     if (!pDriverContext)
134         goto error;
135
136     pDriverContext->native_dpy          = display;
137     pDriverContext->display_type        = VA_DISPLAY_WAYLAND;
138
139     vtable = calloc(1, sizeof(*vtable));
140     if (!vtable)
141         goto error;
142     pDriverContext->vtable_wayland      = vtable;
143
144     vtable->version                     = VA_WAYLAND_API_VERSION;
145
146     for (i = 0; g_backends[i].create != NULL; i++) {
147         if (g_backends[i].create(pDisplayContext))
148             break;
149         g_backends[i].destroy(pDisplayContext);
150     }
151
152     return (VADisplay)pDisplayContext;
153
154 error:
155     va_DisplayContextDestroy(pDisplayContext);
156     return NULL;
157 }
158
159 VAStatus
160 vaGetSurfaceBufferWl(
161     VADisplay           dpy,
162     VASurfaceID         surface,
163     unsigned int        flags,
164     struct wl_buffer  **out_buffer
165 )
166 {
167     VADriverContextP const ctx = get_driver_context(dpy);
168
169     if (!ctx)
170         return VA_STATUS_ERROR_INVALID_DISPLAY;
171     if (!ctx->vtable_wayland || !ctx->vtable_wayland->vaGetSurfaceBufferWl)
172         return VA_STATUS_ERROR_UNIMPLEMENTED;
173     return ctx->vtable_wayland->vaGetSurfaceBufferWl(ctx, surface, flags,
174                                                      out_buffer);
175 }
176
177 VAStatus
178 vaGetImageBufferWl(
179     VADisplay           dpy,
180     VAImageID           image,
181     unsigned int        flags,
182     struct wl_buffer  **out_buffer
183 )
184 {
185     VADriverContextP const ctx = get_driver_context(dpy);
186
187     if (!ctx)
188         return VA_STATUS_ERROR_INVALID_DISPLAY;
189     if (!ctx->vtable_wayland || !ctx->vtable_wayland->vaGetImageBufferWl)
190         return VA_STATUS_ERROR_UNIMPLEMENTED;
191     return ctx->vtable_wayland->vaGetImageBufferWl(ctx, image, flags,
192                                                    out_buffer);
193 }