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.
28 #include "va_backend.h"
32 #include "va_dricommon.h"
37 #include <sys/types.h>
42 static VADisplayContextP pDisplayContexts = NULL;
44 static void va_errorMessage(const char *msg, ...)
48 fprintf(stderr, "libva error: ");
50 vfprintf(stderr, msg, args);
54 static void va_infoMessage(const char *msg, ...)
58 fprintf(stderr, "libva: ");
60 vfprintf(stderr, msg, args);
64 static int va_DisplayContextIsValid (
65 VADisplayContextP pDisplayContext
68 VADisplayContextP ctx = pDisplayContexts;
72 if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
79 static void va_DisplayContextDestroy (
80 VADisplayContextP pDisplayContext
83 VADisplayContextP *ctx = &pDisplayContexts;
85 /* Throw away pDisplayContext */
88 if (*ctx == pDisplayContext)
90 *ctx = pDisplayContext->pNext;
91 pDisplayContext->pNext = NULL;
94 ctx = &((*ctx)->pNext);
96 free(pDisplayContext->pDriverContext);
97 free(pDisplayContext);
101 static VAStatus va_DRI2GetDriverName (
102 VADisplayContextP pDisplayContext,
106 VADriverContextP ctx = pDisplayContext->pDriverContext;
107 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
108 int eventBase, errorBase;
115 if (!VA_DRI2QueryExtension(ctx->x11_dpy, &eventBase, &errorBase)) {
116 va_infoMessage("DRI2 extension isn't present\n");
117 return VA_STATUS_ERROR_UNKNOWN;
120 if (!VA_DRI2QueryVersion(ctx->x11_dpy, &driver_major, &driver_minor)) {
121 va_errorMessage("VA_DRI2QueryVersion failed\n");
122 return VA_STATUS_ERROR_UNKNOWN;
125 if (!VA_DRI2Connect(ctx->x11_dpy, RootWindow(ctx->x11_dpy, ctx->x11_screen),
126 driver_name, &device_name)) {
127 va_infoMessage("DRI2 isn't enabled, fallback to DRI1\n");
128 return VA_STATUS_ERROR_UNKNOWN;
131 va_infoMessage("VA_DRI2Connect: %d.%d.%d %s (screen %d)\n",
132 driver_major, driver_minor, driver_patch, *driver_name, ctx->x11_screen);
135 return VA_STATUS_SUCCESS;
138 static VAStatus va_DRIGetDriverName (
139 VADisplayContextP pDisplayContext,
143 VADriverContextP ctx = pDisplayContext->pDriverContext;
144 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
145 int eventBase, errorBase;
151 char *x_driver_name = NULL;
153 if (!VA_DRIQueryExtension(ctx->x11_dpy, &eventBase, &errorBase)) {
154 va_errorMessage("VA_DRIQueryExtension failed\n");
155 return VA_STATUS_ERROR_UNKNOWN;
160 result = VA_DRIQueryDirectRenderingCapable(ctx->x11_dpy, ctx->x11_screen, &direct_capable);
163 va_errorMessage("VA_DRIQueryDirectRenderingCapable failed\n");
168 result = direct_capable;
171 va_errorMessage("VA_DRIQueryDirectRenderingCapable returned false\n");
176 result = VA_DRIGetClientDriverName(ctx->x11_dpy, ctx->x11_screen, &driver_major, &driver_minor,
177 &driver_patch, &x_driver_name);
180 va_errorMessage("VA_DRIGetClientDriverName returned false\n");
185 vaStatus = VA_STATUS_SUCCESS;
186 va_infoMessage("VA_DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
187 driver_major, driver_minor, driver_patch, x_driver_name, ctx->x11_screen);
189 *driver_name = strdup(x_driver_name);
192 XFree(x_driver_name);
197 static VAStatus va_DisplayContextGetDriverName (
198 VADisplayContextP pDisplayContext,
202 VADriverContextP ctx = pDisplayContext->pDriverContext;
203 VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
209 char *driver_name_env;
214 if ((driver_name_env = getenv("LIBVA_DRIVER_NAME")) != NULL
215 && geteuid() == getuid())
217 /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
218 *driver_name = strdup(driver_name_env);
219 return VA_STATUS_SUCCESS;
222 vaStatus = va_DRI2GetDriverName(pDisplayContext, driver_name);
223 if (vaStatus != VA_STATUS_SUCCESS)
224 vaStatus = va_DRIGetDriverName(pDisplayContext, driver_name);
229 int vaDisplayIsValid(VADisplay dpy)
231 VADisplayContextP tmp=NULL;
232 VADisplayContextP pDisplayContext = pDisplayContexts;
234 while (pDisplayContext)
236 if (pDisplayContext == (VADisplayContextP)dpy)
238 tmp = (VADisplay)pDisplayContext;
241 pDisplayContext = pDisplayContext->pNext;
247 return tmp->vaIsValid(pDisplayContext);
251 VADisplay vaGetDisplay (
252 Display *native_dpy /* implementation specific */
255 VADisplay dpy = NULL;
256 VADisplayContextP pDisplayContext = pDisplayContexts;
261 while (pDisplayContext)
263 if (pDisplayContext->pDriverContext &&
264 pDisplayContext->pDriverContext->x11_dpy == native_dpy)
266 dpy = (VADisplay)pDisplayContext;
269 pDisplayContext = pDisplayContext->pNext;
274 /* create new entry */
275 VADriverContextP pDriverContext;
276 pDisplayContext = calloc(1, sizeof(*pDisplayContext));
277 pDriverContext = calloc(1, sizeof(*pDriverContext));
278 if (pDisplayContext && pDriverContext)
280 pDriverContext->old_pNext = (void *)(unsigned long)0xdeadbeef;
281 pDriverContext->x11_dpy = native_dpy;
282 pDisplayContext->pNext = pDisplayContexts;
283 pDisplayContext->pDriverContext = pDriverContext;
284 pDisplayContext->vaIsValid = va_DisplayContextIsValid;
285 pDisplayContext->vaDestroy = va_DisplayContextDestroy;
286 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
287 pDisplayContexts = pDisplayContext;
288 pDriverContext->dri_state = calloc(1, sizeof(struct dri_state));
289 dpy = (VADisplay)pDisplayContext;
294 free(pDisplayContext);
296 free(pDriverContext);