| 1 |
/* |
| 2 |
* Copyright (C) 2010 Splitted-Desktop Systems. 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 "sysdeps.h" |
| 26 |
|
| 27 |
#ifdef HAVE_FGLRX |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <dlfcn.h> |
| 33 |
#include <X11/Xlib.h> |
| 34 |
#include "va_fglrx.h" |
| 35 |
|
| 36 |
#define ADL_OK 0 |
| 37 |
#define ADL_MAX_PATH 256 |
| 38 |
|
| 39 |
/* |
| 40 |
* Based on public AMD Display Library (ADL) SDK: |
| 41 |
* <http://developer.amd.com/gpu/adlsdk/Pages/default.aspx> |
| 42 |
*/ |
| 43 |
typedef struct AdapterInfo { |
| 44 |
int iSize; |
| 45 |
int iAdapterIndex; |
| 46 |
char strUDID[ADL_MAX_PATH]; |
| 47 |
int iBusNumber; |
| 48 |
int iDeviceNumber; |
| 49 |
int iFunctionNumber; |
| 50 |
int iVendorID; |
| 51 |
char strAdapterName[ADL_MAX_PATH]; |
| 52 |
char strDisplayName[ADL_MAX_PATH]; |
| 53 |
int iPresent; |
| 54 |
int iXScreenNum; |
| 55 |
int iDrvIndex; |
| 56 |
char strXScreenConfigName[ADL_MAX_PATH]; |
| 57 |
} AdapterInfo, *LPAdapterInfo; |
| 58 |
|
| 59 |
typedef struct XScreenInfo { |
| 60 |
int iXScreenNum; |
| 61 |
char strXScreenConfigName[ADL_MAX_PATH]; |
| 62 |
} XScreenInfo, *LPXScreenInfo; |
| 63 |
|
| 64 |
typedef void *(*ADL_MAIN_MALLOC_CALLBACK)(int); |
| 65 |
typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MAIN_MALLOC_CALLBACK, int); |
| 66 |
typedef int (*ADL_MAIN_CONTROL_DESTROY)(void); |
| 67 |
typedef int (*ADL_ADAPTER_NUMBEROFADAPTERS_GET)(int *); |
| 68 |
typedef int (*ADL_ADAPTER_ADAPTERINFO_GET)(LPAdapterInfo, int); |
| 69 |
typedef int (*ADL_ADAPTER_XSCREENINFO_GET)(LPXScreenInfo, int); |
| 70 |
|
| 71 |
static void *ADL_Main_Memory_Alloc(int iSize) |
| 72 |
{ |
| 73 |
return malloc(iSize); |
| 74 |
} |
| 75 |
|
| 76 |
static void ADL_Main_Memory_Free(void *arg) |
| 77 |
{ |
| 78 |
void ** const lpBuffer = arg; |
| 79 |
|
| 80 |
if (lpBuffer && *lpBuffer) { |
| 81 |
free(*lpBuffer); |
| 82 |
*lpBuffer = NULL; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static int get_display_name_length(const char *name) |
| 87 |
{ |
| 88 |
const char *m; |
| 89 |
|
| 90 |
if (!name) |
| 91 |
return 0; |
| 92 |
|
| 93 |
/* Strip out screen number */ |
| 94 |
m = strchr(name, ':'); |
| 95 |
if (m) { |
| 96 |
m = strchr(m, '.'); |
| 97 |
if (m) |
| 98 |
return m - name; |
| 99 |
} |
| 100 |
return strlen(name); |
| 101 |
} |
| 102 |
|
| 103 |
static int match_display_name(Display *x11_dpy, const char *display_name) |
| 104 |
{ |
| 105 |
Display *test_dpy; |
| 106 |
char *test_dpy_name, *x11_dpy_name; |
| 107 |
int test_dpy_namelen, x11_dpy_namelen; |
| 108 |
int m; |
| 109 |
|
| 110 |
test_dpy = XOpenDisplay(display_name); |
| 111 |
if (!test_dpy) |
| 112 |
return 0; |
| 113 |
|
| 114 |
test_dpy_name = XDisplayString(test_dpy); |
| 115 |
test_dpy_namelen = get_display_name_length(test_dpy_name); |
| 116 |
x11_dpy_name = XDisplayString(x11_dpy); |
| 117 |
x11_dpy_namelen = get_display_name_length(x11_dpy_name); |
| 118 |
|
| 119 |
m = (test_dpy_namelen == x11_dpy_namelen && |
| 120 |
(test_dpy_namelen == 0 || |
| 121 |
(test_dpy_namelen > 0 && |
| 122 |
strncmp(test_dpy_name, x11_dpy_name, test_dpy_namelen) == 0))); |
| 123 |
|
| 124 |
XCloseDisplay(test_dpy); |
| 125 |
return m; |
| 126 |
} |
| 127 |
|
| 128 |
static Bool VA_FGLRXGetClientDriverName(Display *dpy, int screen, char **clientDriverName) |
| 129 |
{ |
| 130 |
ADL_MAIN_CONTROL_CREATE ADL_Main_Control_Create; |
| 131 |
ADL_MAIN_CONTROL_DESTROY ADL_Main_Control_Destroy; |
| 132 |
ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get; |
| 133 |
ADL_ADAPTER_ADAPTERINFO_GET ADL_Adapter_AdapterInfo_Get; |
| 134 |
ADL_ADAPTER_XSCREENINFO_GET ADL_Adapter_XScreenInfo_Get; |
| 135 |
|
| 136 |
LPAdapterInfo lpAdapterInfo = NULL; |
| 137 |
LPXScreenInfo lpXScreenInfo = NULL; |
| 138 |
void *libadl_handle = NULL; |
| 139 |
Bool success = False; |
| 140 |
int is_adl_initialized = 0; |
| 141 |
int i, num_adapters, lpAdapterInfo_size, lpXScreenInfo_size; |
| 142 |
|
| 143 |
libadl_handle = dlopen("libatiadlxx.so", RTLD_LAZY | RTLD_GLOBAL); |
| 144 |
if (!libadl_handle) |
| 145 |
goto end; |
| 146 |
|
| 147 |
dlerror(); |
| 148 |
ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) |
| 149 |
dlsym(libadl_handle, "ADL_Main_Control_Create"); |
| 150 |
if (dlerror()) |
| 151 |
goto end; |
| 152 |
|
| 153 |
ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) |
| 154 |
dlsym(libadl_handle, "ADL_Main_Control_Destroy"); |
| 155 |
if (dlerror()) |
| 156 |
goto end; |
| 157 |
|
| 158 |
ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) |
| 159 |
dlsym(libadl_handle, "ADL_Adapter_NumberOfAdapters_Get"); |
| 160 |
if (dlerror()) |
| 161 |
goto end; |
| 162 |
|
| 163 |
ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) |
| 164 |
dlsym(libadl_handle, "ADL_Adapter_AdapterInfo_Get"); |
| 165 |
if (dlerror()) |
| 166 |
goto end; |
| 167 |
|
| 168 |
ADL_Adapter_XScreenInfo_Get = (ADL_ADAPTER_XSCREENINFO_GET) |
| 169 |
dlsym(libadl_handle, "ADL_Adapter_XScreenInfo_Get"); |
| 170 |
if (dlerror()) |
| 171 |
goto end; |
| 172 |
|
| 173 |
if (ADL_Main_Control_Create(ADL_Main_Memory_Alloc, 1) != ADL_OK) |
| 174 |
goto end; |
| 175 |
is_adl_initialized = 1; |
| 176 |
|
| 177 |
if (ADL_Adapter_NumberOfAdapters_Get(&num_adapters) != ADL_OK) |
| 178 |
goto end; |
| 179 |
if (num_adapters <= 0) |
| 180 |
goto end; |
| 181 |
|
| 182 |
lpAdapterInfo_size = num_adapters * sizeof(*lpAdapterInfo); |
| 183 |
lpAdapterInfo = ADL_Main_Memory_Alloc(lpAdapterInfo_size); |
| 184 |
if (!lpAdapterInfo) |
| 185 |
goto end; |
| 186 |
memset(lpAdapterInfo, 0, lpAdapterInfo_size); |
| 187 |
|
| 188 |
for (i = 0; i < num_adapters; i++) |
| 189 |
lpAdapterInfo[i].iSize = sizeof(lpAdapterInfo[i]); |
| 190 |
|
| 191 |
lpXScreenInfo_size = num_adapters * sizeof(*lpXScreenInfo); |
| 192 |
lpXScreenInfo = ADL_Main_Memory_Alloc(lpXScreenInfo_size); |
| 193 |
if (!lpXScreenInfo) |
| 194 |
goto end; |
| 195 |
memset(lpXScreenInfo, 0, lpXScreenInfo_size); |
| 196 |
|
| 197 |
if (ADL_Adapter_AdapterInfo_Get(lpAdapterInfo, lpAdapterInfo_size) != ADL_OK) |
| 198 |
goto end; |
| 199 |
|
| 200 |
if (ADL_Adapter_XScreenInfo_Get(lpXScreenInfo, lpXScreenInfo_size) != ADL_OK) |
| 201 |
goto end; |
| 202 |
|
| 203 |
for (i = 0; i < num_adapters; i++) { |
| 204 |
LPXScreenInfo const lpCurrXScreenInfo = &lpXScreenInfo[i]; |
| 205 |
LPAdapterInfo const lpCurrAdapterInfo = &lpAdapterInfo[i]; |
| 206 |
if (!lpCurrAdapterInfo->iPresent) |
| 207 |
continue; |
| 208 |
#if 0 |
| 209 |
printf("Adapter %d:\n", i); |
| 210 |
printf(" iAdapterIndex: %d\n", lpCurrAdapterInfo->iAdapterIndex); |
| 211 |
printf(" strUDID: '%s'\n", lpCurrAdapterInfo->strUDID); |
| 212 |
printf(" iBusNumber: %d\n", lpCurrAdapterInfo->iBusNumber); |
| 213 |
printf(" iDeviceNumber: %d\n", lpCurrAdapterInfo->iDeviceNumber); |
| 214 |
printf(" iFunctionNumber: %d\n", lpCurrAdapterInfo->iFunctionNumber); |
| 215 |
printf(" iVendorID: 0x%04x\n", lpCurrAdapterInfo->iVendorID); |
| 216 |
printf(" strAdapterName: '%s'\n", lpCurrAdapterInfo->strAdapterName); |
| 217 |
printf(" strDisplayName: '%s'\n", lpCurrAdapterInfo->strDisplayName); |
| 218 |
printf(" iPresent: %d\n", lpCurrAdapterInfo->iPresent); |
| 219 |
printf(" iXScreenNum: %d\n", lpCurrXScreenInfo->iXScreenNum); |
| 220 |
#endif |
| 221 |
if (screen == lpCurrXScreenInfo->iXScreenNum && |
| 222 |
match_display_name(dpy, lpCurrAdapterInfo->strDisplayName)) { |
| 223 |
*clientDriverName = strdup("fglrx"); |
| 224 |
success = !!(*clientDriverName); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
end: |
| 230 |
if (lpXScreenInfo) |
| 231 |
ADL_Main_Memory_Free(&lpXScreenInfo); |
| 232 |
if (lpAdapterInfo) |
| 233 |
ADL_Main_Memory_Free(&lpAdapterInfo); |
| 234 |
if (is_adl_initialized) |
| 235 |
ADL_Main_Control_Destroy(); |
| 236 |
if (libadl_handle) |
| 237 |
dlclose(libadl_handle); |
| 238 |
return success; |
| 239 |
} |
| 240 |
|
| 241 |
VAStatus va_FGLRX_GetDriverNames( |
| 242 |
VADisplayContextP pDisplayContext, |
| 243 |
char **drivers, |
| 244 |
unsigned *num_drivers |
| 245 |
) |
| 246 |
{ |
| 247 |
VADriverContextP ctx = pDisplayContext->pDriverContext; |
| 248 |
if (!VA_FGLRXGetClientDriverName(ctx->native_dpy, ctx->x11_screen, |
| 249 |
drivers)) |
| 250 |
return VA_STATUS_ERROR_UNKNOWN; |
| 251 |
|
| 252 |
*num_drivers = 1; |
| 253 |
return VA_STATUS_SUCCESS; |
| 254 |
} |
| 255 |
|
| 256 |
#endif |