OSDN Git Service

Apply the patch to split va and display/x11 from Gwenole Beauchesne [mailto:gbeauches...
[android-x86/hardware-intel-common-libva.git] / src / x11 / va_x11.c
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
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:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
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.
23  */
24
25 #include "config.h"
26 #include "va.h"
27 #include "va_backend.h"
28 #include "va_x11.h"
29 #include "va_dri.h"
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <string.h>
33
34 static VADisplayContextP pDisplayContexts = NULL;
35
36 static void va_errorMessage(const char *msg, ...)
37 {
38     va_list args;
39
40     fprintf(stderr, "libva error: ");
41     va_start(args, msg);
42     vfprintf(stderr, msg, args);
43     va_end(args);
44 }
45
46 static void va_infoMessage(const char *msg, ...)
47 {
48     va_list args;
49
50     fprintf(stderr, "libva: ");
51     va_start(args, msg);
52     vfprintf(stderr, msg, args);
53     va_end(args);
54 }
55
56 static int va_DisplayContextIsValid (
57     VADisplayContextP pDisplayContext
58 )
59 {
60     VADisplayContextP ctx = pDisplayContexts;
61
62     while (ctx)
63     {
64         if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
65             return 1;
66         ctx = ctx->pNext;
67     }
68     return 0;
69 }
70
71 static void va_DisplayContextDestroy (
72     VADisplayContextP pDisplayContext
73 )
74 {
75     VADisplayContextP *ctx = &pDisplayContexts;
76
77     /* Throw away pDisplayContext */
78     while (*ctx)
79     {
80         if (*ctx == pDisplayContext)
81         {
82             *ctx = pDisplayContext->pNext;
83             pDisplayContext->pNext = NULL;
84             break;
85         }
86         ctx = &((*ctx)->pNext);
87     }
88     free(pDisplayContext->pDriverContext);
89     free(pDisplayContext);
90 }
91
92 static VAStatus va_DisplayContextGetDriverName (
93     VADisplayContextP pDisplayContext,
94     char **driver_name
95 )
96 {
97     VADriverContextP ctx = pDisplayContext->pDriverContext;
98     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
99     int direct_capable;
100     int driver_major;
101     int driver_minor;
102     int driver_patch;
103     Bool result = True;
104     char *x_driver_name = NULL;
105
106     if (driver_name)
107         *driver_name = NULL;
108     if (geteuid() == getuid())
109     {
110         /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
111         if (getenv("LIBVA_DRIVER_NAME"))
112         {
113             /* For easier debugging */
114             *driver_name = strdup(getenv("LIBVA_DRIVER_NAME"));
115             return VA_STATUS_SUCCESS;
116         }
117     }
118     if (result)
119     {
120         result = VA_DRIQueryDirectRenderingCapable(ctx->x11_dpy, ctx->x11_screen, &direct_capable);
121         if (!result)
122         {
123             va_errorMessage("VA_DRIQueryDirectRenderingCapable failed\n");
124         }
125     }
126     if (result)
127     {
128         result = direct_capable;
129         if (!result)
130         {
131             va_errorMessage("VA_DRIQueryDirectRenderingCapable returned false\n");
132         }
133     }
134     if (result)
135     {
136         result = VA_DRIGetClientDriverName(ctx->x11_dpy, ctx->x11_screen, &driver_major, &driver_minor,
137                                             &driver_patch, &x_driver_name);
138         if (!result)
139         {
140             va_errorMessage("VA_DRIGetClientDriverName returned false\n");
141         }
142     }
143     if (result)
144     {
145         vaStatus = VA_STATUS_SUCCESS;
146         va_infoMessage("VA_DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
147              driver_major, driver_minor, driver_patch, x_driver_name, ctx->x11_screen);
148         if (driver_name)
149             *driver_name = strdup(x_driver_name);
150     }
151     if (x_driver_name)
152         XFree(x_driver_name);
153
154     return vaStatus;
155 }
156
157 VADisplay vaGetDisplay (
158     Display *native_dpy
159 )
160 {
161   VADisplay dpy = NULL;
162   VADisplayContextP pDisplayContext = pDisplayContexts;
163
164   if (!native_dpy)
165       return NULL;
166
167   while (pDisplayContext)
168   {
169       if (pDisplayContext->pDriverContext &&
170           pDisplayContext->pDriverContext->x11_dpy == native_dpy)
171       {
172           dpy = (VADisplay)pDisplayContext;
173           break;
174       }
175       pDisplayContext = pDisplayContext->pNext;
176   }
177
178   if (!dpy)
179   {
180       /* create new entry */
181       VADriverContextP pDriverContext;
182       pDisplayContext = calloc(1, sizeof(*pDisplayContext));
183       pDriverContext  = calloc(1, sizeof(*pDriverContext));
184       if (pDisplayContext && pDriverContext)
185       {
186           pDriverContext->old_pNext        = (void *)(unsigned long)0xdeadbeef;
187           pDriverContext->x11_dpy          = native_dpy;
188           pDisplayContext->pNext           = pDisplayContexts;
189           pDisplayContext->pDriverContext  = pDriverContext;
190           pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
191           pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
192           pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
193           pDisplayContexts                 = pDisplayContext;
194           dpy                              = (VADisplay)pDisplayContext;
195       }
196       else
197       {
198           if (pDisplayContext)
199               free(pDisplayContext);
200           if (pDriverContext)
201               free(pDriverContext);
202       }
203   }
204   
205   return dpy;
206 }