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 *x_driver_name = NULL;
214 vaStatus = va_DRI2GetDriverName(pDisplayContext, driver_name);
215 if (vaStatus != VA_STATUS_SUCCESS)
216 vaStatus = va_DRIGetDriverName(pDisplayContext, driver_name);
218 if ((vaStatus == VA_STATUS_SUCCESS)
219 && geteuid() == getuid())
221 /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
222 if (getenv("LIBVA_DRIVER_NAME"))
224 /* For easier debugging */
228 *driver_name = strdup(getenv("LIBVA_DRIVER_NAME"));
229 return VA_STATUS_SUCCESS;
236 int vaDisplayIsValid(VADisplay dpy)
238 VADisplayContextP tmp=NULL;
239 VADisplayContextP pDisplayContext = pDisplayContexts;
241 while (pDisplayContext)
243 if (pDisplayContext == (VADisplayContextP)dpy)
245 tmp = (VADisplay)pDisplayContext;
248 pDisplayContext = pDisplayContext->pNext;
254 return tmp->vaIsValid(pDisplayContext);
258 VADisplay vaGetDisplay (
259 Display *native_dpy /* implementation specific */
262 VADisplay dpy = NULL;
263 VADisplayContextP pDisplayContext = pDisplayContexts;
268 while (pDisplayContext)
270 if (pDisplayContext->pDriverContext &&
271 pDisplayContext->pDriverContext->x11_dpy == native_dpy)
273 dpy = (VADisplay)pDisplayContext;
276 pDisplayContext = pDisplayContext->pNext;
281 /* create new entry */
282 VADriverContextP pDriverContext;
283 pDisplayContext = calloc(1, sizeof(*pDisplayContext));
284 pDriverContext = calloc(1, sizeof(*pDriverContext));
285 if (pDisplayContext && pDriverContext)
287 pDriverContext->old_pNext = (void *)(unsigned long)0xdeadbeef;
288 pDriverContext->x11_dpy = native_dpy;
289 pDisplayContext->pNext = pDisplayContexts;
290 pDisplayContext->pDriverContext = pDriverContext;
291 pDisplayContext->vaIsValid = va_DisplayContextIsValid;
292 pDisplayContext->vaDestroy = va_DisplayContextDestroy;
293 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
294 pDisplayContexts = pDisplayContext;
295 pDriverContext->dri_state = calloc(1, sizeof(struct dri_state));
296 dpy = (VADisplay)pDisplayContext;
301 free(pDisplayContext);
303 free(pDriverContext);