5cf9923979a0647e4bf672ebbee9b5cb20f7cb50
1 /**************************************************************************
3 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sub license, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial portions
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
27 /* $XFree86: xc/lib/GL/dri/dri_glx.c,v 1.14 2003/07/16 00:54:00 dawes Exp $ */
31 * Kevin E. Martin <kevin@precisioninsight.com>
32 * Brian Paul <brian@precisioninsight.com>
36 #ifdef GLX_DIRECT_RENDERING
39 #include <X11/Xlibint.h>
40 #include <X11/extensions/Xext.h>
41 #include <X11/extensions/extutil.h>
43 #include "glxclient.h"
49 #include <sys/types.h>
60 #ifndef DEFAULT_DRIVER_DIR
61 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
62 #define DEFAULT_DRIVER_DIR "/usr/X11R6/lib/modules/dri"
65 static __DRIdriver
*Drivers
= NULL
;
72 static void InfoMessageF(const char *f
, ...)
77 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
78 fprintf(stderr
, "libGL: ");
80 vfprintf(stderr
, f
, args
);
86 * Print error to stderr, unless LIBGL_DEBUG=="quiet".
88 static void ErrorMessageF(const char *f
, ...)
93 if ((env
= getenv("LIBGL_DEBUG")) && !strstr(env
, "quiet")) {
94 fprintf(stderr
, "libGL error: ");
96 vfprintf(stderr
, f
, args
);
103 * Extract the ith directory path out of a colon-separated list of paths. No
104 * more than \c dirLen characters, including the terminating \c NUL, will be
107 * \param index Index of path to extract (starting at zero)
108 * \param paths The colon-separated list of paths
109 * \param dirLen Maximum length of result to store in \c dir
110 * \param dir Buffer to hold the extracted directory path
113 * The number of characters that would have been written to \c dir had there
114 * been enough room. This does not include the terminating \c NUL. When
115 * extraction fails, zero will be returned.
118 * It seems like this function could be rewritten to use \c strchr.
121 ExtractDir(int index
, const char *paths
, int dirLen
, char *dir
)
124 const char *start
, *end
;
134 else if (*start
== 0) {
135 /* end of string and couldn't find ith colon */
144 while (*start
== ':')
147 /* find next colon, or end of string */
149 while (*end
!= ':' && *end
!= 0) {
153 /* copy string between <start> and <end> into result string */
155 if (len
> dirLen
- 1)
157 strncpy(dir
, start
, len
);
160 return( end
- start
);
165 * Versioned name of the expected \c __driCreateNewScreen function.
167 * The version of the last incompatible loader/driver inteface change is
168 * appended to the name of the \c __driCreateNewScreen function. This
169 * prevents loaders from trying to load drivers that are too old.
172 * Create a macro or something so that this is automatically updated.
174 static const char createNewScreenName
[] = "__driCreateNewScreen_20050727";
178 * Try to \c dlopen the named driver.
180 * This function adds the "_dri.so" suffix to the driver name and searches the
181 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
182 * order to find the driver.
184 * \param driverName - a name like "tdfx", "i810", "mga", etc.
187 * A handle from \c dlopen, or \c NULL if driver file not found.
189 static __DRIdriver
*OpenDriver(const char *driverName
)
191 void *glhandle
= NULL
;
192 char *libPaths
= NULL
;
197 /* First, search Drivers list to see if we've already opened this driver */
198 for (driver
= Drivers
; driver
; driver
= driver
->next
) {
199 if (strcmp(driver
->name
, driverName
) == 0) {
205 /* Attempt to make sure libGL symbols will be visible to the driver */
206 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
208 if (geteuid() == getuid()) {
209 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
210 libPaths
= getenv("LIBGL_DRIVERS_PATH");
212 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
215 libPaths
= DEFAULT_DRIVER_DIR
;
217 for ( i
= 0 ; ExtractDir(i
, libPaths
, 1000, libDir
) != 0 ; i
++ ) {
218 char realDriverName
[200];
222 /* If TLS support is enabled, try to open the TLS version of the driver
223 * binary first. If that fails, try the non-TLS version.
226 snprintf(realDriverName
, 200, "%s/tls/%s_dri.so", libDir
, driverName
);
227 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
228 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
231 if ( handle
== NULL
) {
232 snprintf(realDriverName
, 200, "%s/%s_dri.so", libDir
, driverName
);
233 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
234 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
237 if ( handle
!= NULL
) {
238 /* allocate __DRIdriver struct */
239 driver
= (__DRIdriver
*) Xmalloc(sizeof(__DRIdriver
));
241 break; /* out of memory! */
242 /* init the struct */
243 driver
->name
= __glXstrdup(driverName
);
247 break; /* out of memory! */
250 driver
->createNewScreenFunc
= (PFNCREATENEWSCREENFUNC
)
251 dlsym(handle
, createNewScreenName
);
253 if ( driver
->createNewScreenFunc
== NULL
) {
254 /* If the driver doesn't have this symbol then something's
255 * really, really wrong.
257 ErrorMessageF("%s not defined in %s_dri.so!\n"
258 "Your driver may be too old for this libGL.\n",
259 createNewScreenName
, driverName
);
265 driver
->handle
= handle
;
266 /* put at head of linked list */
267 driver
->next
= Drivers
;
272 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
277 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
287 * Given a display pointer and screen number, determine the name of
288 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
289 * Return True for success, False for failure.
291 static Bool
GetDriverName(Display
*dpy
, int scrNum
, char **driverName
)
295 int driverMajor
, driverMinor
, driverPatch
;
299 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrNum
, &directCapable
)) {
300 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
303 if (!directCapable
) {
304 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
308 b
= XF86DRIGetClientDriverName(dpy
, scrNum
, &driverMajor
, &driverMinor
,
309 &driverPatch
, driverName
);
311 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum
);
315 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
316 driverMajor
, driverMinor
, driverPatch
, *driverName
, scrNum
);
323 * Given a display pointer and screen number, return a __DRIdriver handle.
324 * Return NULL if anything goes wrong.
326 __DRIdriver
*driGetDriver(Display
*dpy
, int scrNum
)
329 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
331 ret
= OpenDriver(driverName
);
341 * Exported function for querying the DRI driver for a given screen.
343 * The returned char pointer points to a static array that will be
344 * overwritten by subsequent calls.
346 PUBLIC
const char *glXGetScreenDriver (Display
*dpy
, int scrNum
) {
349 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
353 len
= strlen (driverName
);
356 memcpy (ret
, driverName
, len
+1);
365 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
367 * The returned char pointer points directly into the driver. Therefore
368 * it should be treated as a constant.
370 * If the driver was not found or does not support configuration NULL is
373 * Note: The driver remains opened after this function returns.
375 PUBLIC
const char *glXGetDriverConfig (const char *driverName
) {
376 __DRIdriver
*driver
= OpenDriver (driverName
);
378 return dlsym (driver
->handle
, "__driConfigOptions");
384 /* Called from __glXFreeDisplayPrivate.
386 static void driDestroyDisplay(Display
*dpy
, void *private)
388 __DRIdisplayPrivate
*pdpyp
= (__DRIdisplayPrivate
*)private;
391 const int numScreens
= ScreenCount(dpy
);
393 for (i
= 0; i
< numScreens
; i
++) {
394 if (pdpyp
->libraryHandles
[i
]) {
395 __DRIdriver
*driver
, *prev
;
397 /* Remove driver from Drivers list */
398 for (prev
= NULL
, driver
= Drivers
; driver
;
399 prev
= driver
, driver
= driver
->next
) {
400 if (driver
->handle
== pdpyp
->libraryHandles
[i
]) {
402 prev
->next
= driver
->next
;
404 Drivers
= driver
->next
;
412 dlclose(pdpyp
->libraryHandles
[i
]);
415 Xfree(pdpyp
->libraryHandles
);
422 * Allocate, initialize and return a __DRIdisplayPrivate object.
423 * This is called from __glXInitialize() when we are given a new
426 void *driCreateDisplay(Display
*dpy
, __DRIdisplay
*pdisp
)
428 const int numScreens
= ScreenCount(dpy
);
429 __DRIdisplayPrivate
*pdpyp
;
430 int eventBase
, errorBase
;
431 int major
, minor
, patch
;
434 /* Initialize these fields to NULL in case we fail.
435 * If we don't do this we may later get segfaults trying to free random
436 * addresses when the display is closed.
438 pdisp
->private = NULL
;
439 pdisp
->destroyDisplay
= NULL
;
441 if (!XF86DRIQueryExtension(dpy
, &eventBase
, &errorBase
)) {
445 if (!XF86DRIQueryVersion(dpy
, &major
, &minor
, &patch
)) {
449 pdpyp
= (__DRIdisplayPrivate
*)Xmalloc(sizeof(__DRIdisplayPrivate
));
454 pdpyp
->driMajor
= major
;
455 pdpyp
->driMinor
= minor
;
456 pdpyp
->driPatch
= patch
;
458 pdisp
->destroyDisplay
= driDestroyDisplay
;
460 /* allocate array of pointers to createNewScreen funcs */
461 pdisp
->createNewScreen
= (PFNCREATENEWSCREENFUNC
*)
462 Xmalloc(numScreens
* sizeof(void *));
463 if (!pdisp
->createNewScreen
) {
468 /* allocate array of library handles */
469 pdpyp
->libraryHandles
= (void **) Xmalloc(numScreens
* sizeof(void*));
470 if (!pdpyp
->libraryHandles
) {
471 Xfree(pdisp
->createNewScreen
);
476 /* dynamically discover DRI drivers for all screens, saving each
477 * driver's "__driCreateScreen" function pointer. That's the bootstrap
478 * entrypoint for all DRI drivers.
480 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
481 __DRIdriver
*driver
= driGetDriver(dpy
, scrn
);
483 pdisp
->createNewScreen
[scrn
] = driver
->createNewScreenFunc
;
484 pdpyp
->libraryHandles
[scrn
] = driver
->handle
;
487 pdisp
->createNewScreen
[scrn
] = NULL
;
488 pdpyp
->libraryHandles
[scrn
] = NULL
;
492 return (void *)pdpyp
;
495 #endif /* GLX_DIRECT_RENDERING */