OSDN Git Service

Display *x11_dpy ==> void *native_dpy for other window system
[android-x86/hardware-intel-common-libva.git] / va / 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 #define _GNU_SOURCE 1
26 #include "config.h"
27 #include "va.h"
28 #include "va_backend.h"
29 #include "va_x11.h"
30 #include "va_dri.h"
31 #include "va_dri2.h"
32 #include "va_dricommon.h"
33 #include "va_nvctrl.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <errno.h>
43
44 static VADisplayContextP pDisplayContexts = NULL;
45
46 static int va_DisplayContextIsValid (
47     VADisplayContextP pDisplayContext
48 )
49 {
50     VADisplayContextP ctx = pDisplayContexts;
51
52     while (ctx)
53     {
54         if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
55             return 1;
56         ctx = ctx->pNext;
57     }
58     return 0;
59 }
60
61 static void va_DisplayContextDestroy (
62     VADisplayContextP pDisplayContext
63 )
64 {
65     VADisplayContextP *ctx = &pDisplayContexts;
66
67     /* Throw away pDisplayContext */
68     while (*ctx)
69     {
70         if (*ctx == pDisplayContext)
71         {
72             *ctx = pDisplayContext->pNext;
73             pDisplayContext->pNext = NULL;
74             break;
75         }
76         ctx = &((*ctx)->pNext);
77     }
78     free(pDisplayContext->pDriverContext->dri_state);
79     free(pDisplayContext->pDriverContext);
80     free(pDisplayContext);
81 }
82
83
84 static VAStatus va_DRI2GetDriverName (
85     VADisplayContextP pDisplayContext,
86     char **driver_name
87 )
88 {
89     VADriverContextP ctx = pDisplayContext->pDriverContext;
90
91     if (!isDRI2Connected(ctx, driver_name))
92         return VA_STATUS_ERROR_UNKNOWN;
93
94     return VA_STATUS_SUCCESS;
95 }
96
97 static VAStatus va_DRIGetDriverName (
98     VADisplayContextP pDisplayContext,
99     char **driver_name
100 )
101 {
102     VADriverContextP ctx = pDisplayContext->pDriverContext;
103
104     if (!isDRI1Connected(ctx, driver_name))
105         return VA_STATUS_ERROR_UNKNOWN;
106
107     return VA_STATUS_SUCCESS;
108 }
109
110 static VAStatus va_NVCTRL_GetDriverName (
111     VADisplayContextP pDisplayContext,
112     char **driver_name
113 )
114 {
115     VADriverContextP ctx = pDisplayContext->pDriverContext;
116     int direct_capable, driver_major, driver_minor, driver_patch;
117     Bool result;
118
119     result = VA_NVCTRLQueryDirectRenderingCapable((Display *)ctx->native_dpy, ctx->x11_screen,
120                                                   &direct_capable);
121     if (!result || !direct_capable)
122         return VA_STATUS_ERROR_UNKNOWN;
123
124     result = VA_NVCTRLGetClientDriverName((Display *)ctx->native_dpy, ctx->x11_screen,
125                                           &driver_major, &driver_minor,
126                                           &driver_patch, driver_name);
127     if (!result)
128         return VA_STATUS_ERROR_UNKNOWN;
129
130     return VA_STATUS_SUCCESS;
131 }
132
133 static VAStatus va_DisplayContextGetDriverName (
134     VADisplayContextP pDisplayContext,
135     char **driver_name
136 )
137 {
138     VAStatus vaStatus;
139     char *driver_name_env;
140
141     if (driver_name)
142         *driver_name = NULL;
143
144     if ((driver_name_env = getenv("LIBVA_DRIVER_NAME")) != NULL
145         && geteuid() == getuid())
146     {
147         /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
148         *driver_name = strdup(driver_name_env);
149         return VA_STATUS_SUCCESS;
150     }
151
152     vaStatus = va_DRI2GetDriverName(pDisplayContext, driver_name);
153     if (vaStatus != VA_STATUS_SUCCESS)
154         vaStatus = va_DRIGetDriverName(pDisplayContext, driver_name);
155     if (vaStatus != VA_STATUS_SUCCESS)
156         vaStatus = va_NVCTRL_GetDriverName(pDisplayContext, driver_name);
157    
158     return vaStatus;
159 }
160
161
162 VADisplay vaGetDisplay (
163     Display *native_dpy /* implementation specific */
164 )
165 {
166   VADisplay dpy = NULL;
167   VADisplayContextP pDisplayContext = pDisplayContexts;
168
169   if (!native_dpy)
170       return NULL;
171
172   while (pDisplayContext)
173   {
174       if (pDisplayContext->pDriverContext &&
175           pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
176       {
177           dpy = (VADisplay)pDisplayContext;
178           break;
179       }
180       pDisplayContext = pDisplayContext->pNext;
181   }
182
183   if (!dpy)
184   {
185       /* create new entry */
186       VADriverContextP pDriverContext;
187       struct dri_state *dri_state;
188       pDisplayContext = calloc(1, sizeof(*pDisplayContext));
189       pDriverContext  = calloc(1, sizeof(*pDriverContext));
190       dri_state       = calloc(1, sizeof(*dri_state));
191       if (pDisplayContext && pDriverContext && dri_state)
192       {
193           pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
194
195           pDriverContext->native_dpy       = (void *)native_dpy;
196           pDisplayContext->pNext           = pDisplayContexts;
197           pDisplayContext->pDriverContext  = pDriverContext;
198           pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
199           pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
200           pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
201           pDisplayContexts                 = pDisplayContext;
202           pDriverContext->dri_state        = dri_state;
203           dpy                              = (VADisplay)pDisplayContext;
204       }
205       else
206       {
207           if (pDisplayContext)
208               free(pDisplayContext);
209           if (pDriverContext)
210               free(pDriverContext);
211           if (dri_state)
212               free(dri_state);
213       }
214   }
215   
216   return dpy;
217 }