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>
42 #include "glxclient.h"
48 #include <sys/types.h>
59 #ifndef DEFAULT_DRIVER_DIR
60 /* this is normally defined in the Imakefile */
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
);
84 static void ErrorMessageF(const char *f
, ...)
88 if (getenv("LIBGL_DEBUG")) {
89 fprintf(stderr
, "libGL error: ");
91 vfprintf(stderr
, f
, args
);
98 * Extract the ith directory path out of a colon-separated list of paths. No
99 * more than \c dirLen characters, including the terminating \c NUL, will be
102 * \param index Index of path to extract (starting at zero)
103 * \param paths The colon-separated list of paths
104 * \param dirLen Maximum length of result to store in \c dir
105 * \param dir Buffer to hold the extracted directory path
108 * The number of characters that would have been written to \c dir had there
109 * been enough room. This does not include the terminating \c NUL. When
110 * extraction fails, zero will be returned.
113 * It seems like this function could be rewritten to use \c strchr.
116 ExtractDir(int index
, const char *paths
, int dirLen
, char *dir
)
119 const char *start
, *end
;
129 else if (*start
== 0) {
130 /* end of string and couldn't find ith colon */
139 while (*start
== ':')
142 /* find next colon, or end of string */
144 while (*end
!= ':' && *end
!= 0) {
148 /* copy string between <start> and <end> into result string */
150 if (len
> dirLen
- 1)
152 strncpy(dir
, start
, len
);
155 return( end
- start
);
160 * Versioned name of the expected \c __driCreateNewScreen function.
162 * The version of the last incompatible loader/driver inteface change is
163 * appended to the name of the \c __driCreateNewScreen function. This
164 * prevents loaders from trying to load drivers that are too old.
167 * Create a macro or something so that this is automatically updated.
169 static const char createNewScreenName
[] = "__driCreateNewScreen_20050727";
173 * Try to \c dlopen the named driver.
175 * This function adds the "_dri.so" suffix to the driver name and searches the
176 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
177 * order to find the driver.
179 * \param driverName - a name like "tdfx", "i810", "mga", etc.
182 * A handle from \c dlopen, or \c NULL if driver file not found.
184 static __DRIdriver
*OpenDriver(const char *driverName
)
186 char *libPaths
= NULL
;
191 /* First, search Drivers list to see if we've already opened this driver */
192 for (driver
= Drivers
; driver
; driver
= driver
->next
) {
193 if (strcmp(driver
->name
, driverName
) == 0) {
199 if (geteuid() == getuid()) {
200 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
201 libPaths
= getenv("LIBGL_DRIVERS_PATH");
203 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
206 libPaths
= DEFAULT_DRIVER_DIR
;
208 for ( i
= 0 ; ExtractDir(i
, libPaths
, 1000, libDir
) != 0 ; i
++ ) {
209 char realDriverName
[200];
213 /* If TLS support is enabled, try to open the TLS version of the driver
214 * binary first. If that fails, try the non-TLS version.
217 snprintf(realDriverName
, 200, "%s/tls/%s_dri.so", libDir
, driverName
);
218 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
219 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
222 if ( handle
== NULL
) {
223 snprintf(realDriverName
, 200, "%s/%s_dri.so", libDir
, driverName
);
224 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
225 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
228 if ( handle
!= NULL
) {
229 /* allocate __DRIdriver struct */
230 driver
= (__DRIdriver
*) Xmalloc(sizeof(__DRIdriver
));
232 return NULL
; /* out of memory! */
233 /* init the struct */
234 driver
->name
= __glXstrdup(driverName
);
237 return NULL
; /* out of memory! */
240 driver
->createNewScreenFunc
= (PFNCREATENEWSCREENFUNC
)
241 dlsym(handle
, createNewScreenName
);
243 if ( driver
->createNewScreenFunc
== NULL
) {
244 /* If the driver doesn't have this symbol then something's
245 * really, really wrong.
247 ErrorMessageF("%s not defined in %s_dri.so!\n"
248 "Your driver may be too old for this libGL.\n",
249 createNewScreenName
, driverName
);
254 driver
->handle
= handle
;
255 /* put at head of linked list */
256 driver
->next
= Drivers
;
261 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
265 ErrorMessageF("unable to find driver: %s_dri.so\n", driverName
);
271 * Given a display pointer and screen number, determine the name of
272 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
273 * Return True for success, False for failure.
275 static Bool
GetDriverName(Display
*dpy
, int scrNum
, char **driverName
)
279 int driverMajor
, driverMinor
, driverPatch
;
283 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrNum
, &directCapable
)) {
284 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
287 if (!directCapable
) {
288 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
292 b
= XF86DRIGetClientDriverName(dpy
, scrNum
, &driverMajor
, &driverMinor
,
293 &driverPatch
, driverName
);
295 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum
);
299 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
300 driverMajor
, driverMinor
, driverPatch
, *driverName
, scrNum
);
307 * Given a display pointer and screen number, return a __DRIdriver handle.
308 * Return NULL if anything goes wrong.
310 __DRIdriver
*driGetDriver(Display
*dpy
, int scrNum
)
313 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
315 ret
= OpenDriver(driverName
);
325 * Exported function for querying the DRI driver for a given screen.
327 * The returned char pointer points to a static array that will be
328 * overwritten by subsequent calls.
330 const char *glXGetScreenDriver (Display
*dpy
, int scrNum
) {
333 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
337 len
= strlen (driverName
);
340 memcpy (ret
, driverName
, len
+1);
349 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
351 * The returned char pointer points directly into the driver. Therefore
352 * it should be treated as a constant.
354 * If the driver was not found or does not support configuration NULL is
357 * Note: The driver remains opened after this function returns.
359 const char *glXGetDriverConfig (const char *driverName
) {
360 __DRIdriver
*driver
= OpenDriver (driverName
);
362 return dlsym (driver
->handle
, "__driConfigOptions");
368 /* This function isn't currently used.
370 static void driDestroyDisplay(Display
*dpy
, void *private)
372 __DRIdisplayPrivate
*pdpyp
= (__DRIdisplayPrivate
*)private;
375 const int numScreens
= ScreenCount(dpy
);
377 for (i
= 0; i
< numScreens
; i
++) {
378 if (pdpyp
->libraryHandles
[i
])
379 dlclose(pdpyp
->libraryHandles
[i
]);
381 Xfree(pdpyp
->libraryHandles
);
388 * Allocate, initialize and return a __DRIdisplayPrivate object.
389 * This is called from __glXInitialize() when we are given a new
392 void *driCreateDisplay(Display
*dpy
, __DRIdisplay
*pdisp
)
394 const int numScreens
= ScreenCount(dpy
);
395 __DRIdisplayPrivate
*pdpyp
;
396 int eventBase
, errorBase
;
397 int major
, minor
, patch
;
400 /* Initialize these fields to NULL in case we fail.
401 * If we don't do this we may later get segfaults trying to free random
402 * addresses when the display is closed.
404 pdisp
->private = NULL
;
405 pdisp
->destroyDisplay
= NULL
;
407 if (!XF86DRIQueryExtension(dpy
, &eventBase
, &errorBase
)) {
411 if (!XF86DRIQueryVersion(dpy
, &major
, &minor
, &patch
)) {
415 pdpyp
= (__DRIdisplayPrivate
*)Xmalloc(sizeof(__DRIdisplayPrivate
));
420 pdpyp
->driMajor
= major
;
421 pdpyp
->driMinor
= minor
;
422 pdpyp
->driPatch
= patch
;
424 pdisp
->destroyDisplay
= driDestroyDisplay
;
426 /* allocate array of pointers to createNewScreen funcs */
427 pdisp
->createNewScreen
= (PFNCREATENEWSCREENFUNC
*)
428 Xmalloc(numScreens
* sizeof(void *));
429 if (!pdisp
->createNewScreen
) {
434 /* allocate array of library handles */
435 pdpyp
->libraryHandles
= (void **) Xmalloc(numScreens
* sizeof(void*));
436 if (!pdpyp
->libraryHandles
) {
437 Xfree(pdisp
->createNewScreen
);
442 /* dynamically discover DRI drivers for all screens, saving each
443 * driver's "__driCreateScreen" function pointer. That's the bootstrap
444 * entrypoint for all DRI drivers.
446 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
447 __DRIdriver
*driver
= driGetDriver(dpy
, scrn
);
449 pdisp
->createNewScreen
[scrn
] = driver
->createNewScreenFunc
;
450 pdpyp
->libraryHandles
[scrn
] = driver
->handle
;
453 pdisp
->createNewScreen
[scrn
] = NULL
;
454 pdpyp
->libraryHandles
[scrn
] = NULL
;
458 return (void *)pdpyp
;
461 #endif /* GLX_DIRECT_RENDERING */