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 **************************************************************************/
30 * Kevin E. Martin <kevin@precisioninsight.com>
31 * Brian Paul <brian@precisioninsight.com>
35 #ifdef GLX_DIRECT_RENDERING
38 #include <X11/Xlibint.h>
39 #include <X11/extensions/Xext.h>
40 #include <X11/extensions/extutil.h>
42 #include "glxclient.h"
48 #include <sys/types.h>
59 #ifndef DEFAULT_DRIVER_DIR
60 /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
61 #define DEFAULT_DRIVER_DIR "/usr/X11R6/lib/modules/dri"
64 static __DRIdriver
*Drivers
= NULL
;
71 static void InfoMessageF(const char *f
, ...)
76 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
77 fprintf(stderr
, "libGL: ");
79 vfprintf(stderr
, f
, args
);
85 * Print error to stderr, unless LIBGL_DEBUG=="quiet".
87 static void ErrorMessageF(const char *f
, ...)
92 if ((env
= getenv("LIBGL_DEBUG")) && !strstr(env
, "quiet")) {
93 fprintf(stderr
, "libGL error: ");
95 vfprintf(stderr
, f
, args
);
102 * Extract the ith directory path out of a colon-separated list of paths. No
103 * more than \c dirLen characters, including the terminating \c NUL, will be
106 * \param index Index of path to extract (starting at zero)
107 * \param paths The colon-separated list of paths
108 * \param dirLen Maximum length of result to store in \c dir
109 * \param dir Buffer to hold the extracted directory path
112 * The number of characters that would have been written to \c dir had there
113 * been enough room. This does not include the terminating \c NUL. When
114 * extraction fails, zero will be returned.
117 * It seems like this function could be rewritten to use \c strchr.
120 ExtractDir(int index
, const char *paths
, int dirLen
, char *dir
)
123 const char *start
, *end
;
133 else if (*start
== 0) {
134 /* end of string and couldn't find ith colon */
143 while (*start
== ':')
146 /* find next colon, or end of string */
148 while (*end
!= ':' && *end
!= 0) {
152 /* copy string between <start> and <end> into result string */
154 if (len
> dirLen
- 1)
156 strncpy(dir
, start
, len
);
159 return( end
- start
);
164 * Versioned name of the expected \c __driCreateNewScreen function.
166 * The version of the last incompatible loader/driver inteface change is
167 * appended to the name of the \c __driCreateNewScreen function. This
168 * prevents loaders from trying to load drivers that are too old.
171 * Create a macro or something so that this is automatically updated.
173 static const char createNewScreenName
[] = "__driCreateNewScreen_20050727";
177 * Try to \c dlopen the named driver.
179 * This function adds the "_dri.so" suffix to the driver name and searches the
180 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
181 * order to find the driver.
183 * \param driverName - a name like "tdfx", "i810", "mga", etc.
186 * A handle from \c dlopen, or \c NULL if driver file not found.
188 static __DRIdriver
*OpenDriver(const char *driverName
)
190 void *glhandle
= NULL
;
191 char *libPaths
= NULL
;
196 /* First, search Drivers list to see if we've already opened this driver */
197 for (driver
= Drivers
; driver
; driver
= driver
->next
) {
198 if (strcmp(driver
->name
, driverName
) == 0) {
204 /* Attempt to make sure libGL symbols will be visible to the driver */
205 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
207 if (geteuid() == getuid()) {
208 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
209 libPaths
= getenv("LIBGL_DRIVERS_PATH");
211 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
214 libPaths
= DEFAULT_DRIVER_DIR
;
216 for ( i
= 0 ; ExtractDir(i
, libPaths
, 1000, libDir
) != 0 ; i
++ ) {
217 char realDriverName
[200];
221 /* If TLS support is enabled, try to open the TLS version of the driver
222 * binary first. If that fails, try the non-TLS version.
225 snprintf(realDriverName
, 200, "%s/tls/%s_dri.so", libDir
, driverName
);
226 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
227 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
230 if ( handle
== NULL
) {
231 snprintf(realDriverName
, 200, "%s/%s_dri.so", libDir
, driverName
);
232 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
233 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
236 if ( handle
!= NULL
) {
237 /* allocate __DRIdriver struct */
238 driver
= (__DRIdriver
*) Xmalloc(sizeof(__DRIdriver
));
240 break; /* out of memory! */
241 /* init the struct */
242 driver
->name
= __glXstrdup(driverName
);
246 break; /* out of memory! */
249 driver
->createNewScreenFunc
= (PFNCREATENEWSCREENFUNC
)
250 dlsym(handle
, createNewScreenName
);
252 if ( driver
->createNewScreenFunc
== NULL
) {
253 /* If the driver doesn't have this symbol then something's
254 * really, really wrong.
256 ErrorMessageF("%s not defined in %s_dri.so!\n"
257 "Your driver may be too old for this libGL.\n",
258 createNewScreenName
, driverName
);
264 driver
->handle
= handle
;
265 /* put at head of linked list */
266 driver
->next
= Drivers
;
271 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
276 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
286 * Given a display pointer and screen number, determine the name of
287 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
288 * Return True for success, False for failure.
290 static Bool
GetDriverName(Display
*dpy
, int scrNum
, char **driverName
)
294 int driverMajor
, driverMinor
, driverPatch
;
298 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrNum
, &directCapable
)) {
299 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
302 if (!directCapable
) {
303 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
307 b
= XF86DRIGetClientDriverName(dpy
, scrNum
, &driverMajor
, &driverMinor
,
308 &driverPatch
, driverName
);
310 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum
);
314 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
315 driverMajor
, driverMinor
, driverPatch
, *driverName
, scrNum
);
322 * Given a display pointer and screen number, return a __DRIdriver handle.
323 * Return NULL if anything goes wrong.
325 __DRIdriver
*driGetDriver(Display
*dpy
, int scrNum
)
328 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
330 ret
= OpenDriver(driverName
);
340 * Exported function for querying the DRI driver for a given screen.
342 * The returned char pointer points to a static array that will be
343 * overwritten by subsequent calls.
345 PUBLIC
const char *glXGetScreenDriver (Display
*dpy
, int scrNum
) {
348 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
352 len
= strlen (driverName
);
355 memcpy (ret
, driverName
, len
+1);
364 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
366 * The returned char pointer points directly into the driver. Therefore
367 * it should be treated as a constant.
369 * If the driver was not found or does not support configuration NULL is
372 * Note: The driver remains opened after this function returns.
374 PUBLIC
const char *glXGetDriverConfig (const char *driverName
) {
375 __DRIdriver
*driver
= OpenDriver (driverName
);
377 return dlsym (driver
->handle
, "__driConfigOptions");
383 /* Called from __glXFreeDisplayPrivate.
385 static void driDestroyDisplay(Display
*dpy
, void *private)
387 __DRIdisplayPrivate
*pdpyp
= (__DRIdisplayPrivate
*)private;
390 const int numScreens
= ScreenCount(dpy
);
392 for (i
= 0; i
< numScreens
; i
++) {
393 if (pdpyp
->libraryHandles
[i
]) {
394 __DRIdriver
*driver
, *prev
;
396 /* Remove driver from Drivers list */
397 for (prev
= NULL
, driver
= Drivers
; driver
;
398 prev
= driver
, driver
= driver
->next
) {
399 if (driver
->handle
== pdpyp
->libraryHandles
[i
]) {
401 prev
->next
= driver
->next
;
403 Drivers
= driver
->next
;
411 dlclose(pdpyp
->libraryHandles
[i
]);
414 Xfree(pdpyp
->libraryHandles
);
421 * Allocate, initialize and return a __DRIdisplayPrivate object.
422 * This is called from __glXInitialize() when we are given a new
425 void *driCreateDisplay(Display
*dpy
, __DRIdisplay
*pdisp
)
427 const int numScreens
= ScreenCount(dpy
);
428 __DRIdisplayPrivate
*pdpyp
;
429 int eventBase
, errorBase
;
430 int major
, minor
, patch
;
433 /* Initialize these fields to NULL in case we fail.
434 * If we don't do this we may later get segfaults trying to free random
435 * addresses when the display is closed.
437 pdisp
->private = NULL
;
438 pdisp
->destroyDisplay
= NULL
;
440 if (!XF86DRIQueryExtension(dpy
, &eventBase
, &errorBase
)) {
444 if (!XF86DRIQueryVersion(dpy
, &major
, &minor
, &patch
)) {
448 pdpyp
= (__DRIdisplayPrivate
*)Xmalloc(sizeof(__DRIdisplayPrivate
));
453 pdpyp
->driMajor
= major
;
454 pdpyp
->driMinor
= minor
;
455 pdpyp
->driPatch
= patch
;
457 pdisp
->destroyDisplay
= driDestroyDisplay
;
459 /* allocate array of pointers to createNewScreen funcs */
460 pdisp
->createNewScreen
= (PFNCREATENEWSCREENFUNC
*)
461 Xmalloc(numScreens
* sizeof(void *));
462 if (!pdisp
->createNewScreen
) {
467 /* allocate array of library handles */
468 pdpyp
->libraryHandles
= (void **) Xmalloc(numScreens
* sizeof(void*));
469 if (!pdpyp
->libraryHandles
) {
470 Xfree(pdisp
->createNewScreen
);
475 /* dynamically discover DRI drivers for all screens, saving each
476 * driver's "__driCreateScreen" function pointer. That's the bootstrap
477 * entrypoint for all DRI drivers.
479 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
480 __DRIdriver
*driver
= driGetDriver(dpy
, scrn
);
482 pdisp
->createNewScreen
[scrn
] = driver
->createNewScreenFunc
;
483 pdpyp
->libraryHandles
[scrn
] = driver
->handle
;
486 pdisp
->createNewScreen
[scrn
] = NULL
;
487 pdpyp
->libraryHandles
[scrn
] = NULL
;
491 return (void *)pdpyp
;
494 #endif /* GLX_DIRECT_RENDERING */