2 * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "va_backend.h"
34 #include <sys/types.h>
39 static VADisplayContextP pDisplayContexts = NULL;
41 static void va_errorMessage(const char *msg, ...)
45 fprintf(stderr, "libva error: ");
47 vfprintf(stderr, msg, args);
51 static void va_infoMessage(const char *msg, ...)
55 fprintf(stderr, "libva: ");
57 vfprintf(stderr, msg, args);
61 static int va_DisplayContextIsValid (
62 VADisplayContextP pDisplayContext
65 VADisplayContextP ctx = pDisplayContexts;
69 if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
76 static void va_DisplayContextDestroy (
77 VADisplayContextP pDisplayContext
80 VADisplayContextP *ctx = &pDisplayContexts;
82 /* Throw away pDisplayContext */
85 if (*ctx == pDisplayContext)
87 *ctx = pDisplayContext->pNext;
88 pDisplayContext->pNext = NULL;
91 ctx = &((*ctx)->pNext);
93 free(pDisplayContext->pDriverContext);
94 free(pDisplayContext);
98 static VAStatus va_DRI2GetDriverName (
99 VADisplayContextP pDisplayContext,
103 VADriverContextP ctx = pDisplayContext->pDriverContext;
104 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
105 int eventBase, errorBase;
112 if (!VA_DRI2QueryExtension(ctx->x11_dpy, &eventBase, &errorBase)) {
113 va_infoMessage("DRI2 extension isn't present\n");
114 return VA_STATUS_ERROR_UNKNOWN;
117 if (!VA_DRI2QueryVersion(ctx->x11_dpy, &driver_major, &driver_minor)) {
118 va_errorMessage("VA_DRI2QueryVersion failed\n");
119 return VA_STATUS_ERROR_UNKNOWN;
122 if (!VA_DRI2Connect(ctx->x11_dpy, RootWindow(ctx->x11_dpy, ctx->x11_screen),
123 driver_name, &device_name)) {
124 va_infoMessage("DRI2 isn't enabled, fallback to DRI1\n");
125 return VA_STATUS_ERROR_UNKNOWN;
128 va_infoMessage("VA_DRI2Connect: %d.%d.%d %s (screen %d)\n",
129 driver_major, driver_minor, driver_patch, *driver_name, ctx->x11_screen);
132 return VA_STATUS_SUCCESS;
135 static VAStatus va_DRIGetDriverName (
136 VADisplayContextP pDisplayContext,
140 VADriverContextP ctx = pDisplayContext->pDriverContext;
141 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
142 int eventBase, errorBase;
148 char *x_driver_name = NULL;
150 if (!VA_DRIQueryExtension(ctx->x11_dpy, &eventBase, &errorBase)) {
151 va_errorMessage("VA_DRIQueryExtension failed\n");
152 return VA_STATUS_ERROR_UNKNOWN;
157 result = VA_DRIQueryDirectRenderingCapable(ctx->x11_dpy, ctx->x11_screen, &direct_capable);
160 va_errorMessage("VA_DRIQueryDirectRenderingCapable failed\n");
165 result = direct_capable;
168 va_errorMessage("VA_DRIQueryDirectRenderingCapable returned false\n");
173 result = VA_DRIGetClientDriverName(ctx->x11_dpy, ctx->x11_screen, &driver_major, &driver_minor,
174 &driver_patch, &x_driver_name);
177 va_errorMessage("VA_DRIGetClientDriverName returned false\n");
182 vaStatus = VA_STATUS_SUCCESS;
183 va_infoMessage("VA_DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
184 driver_major, driver_minor, driver_patch, x_driver_name, ctx->x11_screen);
186 *driver_name = strdup(x_driver_name);
189 XFree(x_driver_name);
194 static VAStatus va_DisplayContextGetDriverName (
195 VADisplayContextP pDisplayContext,
199 VADriverContextP ctx = pDisplayContext->pDriverContext;
200 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
206 char *x_driver_name = NULL;
211 vaStatus = va_DRI2GetDriverName(pDisplayContext, driver_name);
212 if (vaStatus != VA_STATUS_SUCCESS)
213 vaStatus = va_DRIGetDriverName(pDisplayContext, driver_name);
215 if ((vaStatus == VA_STATUS_SUCCESS)
216 && geteuid() == getuid())
218 /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
219 if (getenv("LIBVA_DRIVER_NAME"))
221 /* For easier debugging */
225 *driver_name = strdup(getenv("LIBVA_DRIVER_NAME"));
226 return VA_STATUS_SUCCESS;
233 int vaDisplayIsValid(VADisplay dpy)
235 VADisplayContextP tmp=NULL;
236 VADisplayContextP pDisplayContext = pDisplayContexts;
238 while (pDisplayContext)
240 if (pDisplayContext == (VADisplayContextP)dpy)
242 tmp = (VADisplay)pDisplayContext;
245 pDisplayContext = pDisplayContext->pNext;
251 return tmp->vaIsValid(pDisplayContext);
255 VADisplay vaGetDisplay (
256 Display *native_dpy /* implementation specific */
259 VADisplay dpy = NULL;
260 VADisplayContextP pDisplayContext = pDisplayContexts;
265 while (pDisplayContext)
267 if (pDisplayContext->pDriverContext &&
268 pDisplayContext->pDriverContext->x11_dpy == native_dpy)
270 dpy = (VADisplay)pDisplayContext;
273 pDisplayContext = pDisplayContext->pNext;
278 /* create new entry */
279 VADriverContextP pDriverContext;
280 pDisplayContext = calloc(1, sizeof(*pDisplayContext));
281 pDriverContext = calloc(1, sizeof(*pDriverContext));
282 if (pDisplayContext && pDriverContext)
284 pDriverContext->old_pNext = (void *)(unsigned long)0xdeadbeef;
285 pDriverContext->x11_dpy = native_dpy;
286 pDisplayContext->pNext = pDisplayContexts;
287 pDisplayContext->pDriverContext = pDriverContext;
288 pDisplayContext->vaIsValid = va_DisplayContextIsValid;
289 pDisplayContext->vaDestroy = va_DisplayContextDestroy;
290 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
291 pDisplayContexts = pDisplayContext;
292 dpy = (VADisplay)pDisplayContext;
297 free(pDisplayContext);
299 free(pDriverContext);