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.
171 static const char createNewScreenName
[] = __DRI_CREATE_NEW_SCREEN_STRING
;
175 * Try to \c dlopen the named driver.
177 * This function adds the "_dri.so" suffix to the driver name and searches the
178 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
179 * order to find the driver.
181 * \param driverName - a name like "tdfx", "i810", "mga", etc.
184 * A handle from \c dlopen, or \c NULL if driver file not found.
186 static __DRIdriver
*OpenDriver(const char *driverName
)
188 void *glhandle
= NULL
;
189 char *libPaths
= NULL
;
194 /* First, search Drivers list to see if we've already opened this driver */
195 for (driver
= Drivers
; driver
; driver
= driver
->next
) {
196 if (strcmp(driver
->name
, driverName
) == 0) {
202 /* Attempt to make sure libGL symbols will be visible to the driver */
203 glhandle
= dlopen("libGL.so.1", RTLD_NOW
| RTLD_GLOBAL
);
205 if (geteuid() == getuid()) {
206 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
207 libPaths
= getenv("LIBGL_DRIVERS_PATH");
209 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
212 libPaths
= DEFAULT_DRIVER_DIR
;
214 for ( i
= 0 ; ExtractDir(i
, libPaths
, 1000, libDir
) != 0 ; i
++ ) {
215 char realDriverName
[200];
219 /* If TLS support is enabled, try to open the TLS version of the driver
220 * binary first. If that fails, try the non-TLS version.
223 snprintf(realDriverName
, 200, "%s/tls/%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 snprintf(realDriverName
, 200, "%s/%s_dri.so", libDir
, driverName
);
230 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
231 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
234 if ( handle
!= NULL
) {
235 /* allocate __DRIdriver struct */
236 driver
= (__DRIdriver
*) Xmalloc(sizeof(__DRIdriver
));
238 break; /* out of memory! */
239 /* init the struct */
240 driver
->name
= __glXstrdup(driverName
);
244 break; /* out of memory! */
247 driver
->createNewScreenFunc
= (PFNCREATENEWSCREENFUNC
)
248 dlsym(handle
, createNewScreenName
);
250 if ( driver
->createNewScreenFunc
== NULL
) {
251 /* If the driver doesn't have this symbol then something's
252 * really, really wrong.
254 ErrorMessageF("%s not defined in %s_dri.so!\n"
255 "Your driver may be too old for this libGL.\n",
256 createNewScreenName
, driverName
);
262 driver
->handle
= handle
;
263 /* put at head of linked list */
264 driver
->next
= Drivers
;
269 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
274 ErrorMessageF("unable to load driver: %s_dri.so\n", driverName
);
284 * Given a display pointer and screen number, determine the name of
285 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
286 * Return True for success, False for failure.
288 static Bool
GetDriverName(Display
*dpy
, int scrNum
, char **driverName
)
292 int driverMajor
, driverMinor
, driverPatch
;
296 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrNum
, &directCapable
)) {
297 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
300 if (!directCapable
) {
301 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
305 b
= XF86DRIGetClientDriverName(dpy
, scrNum
, &driverMajor
, &driverMinor
,
306 &driverPatch
, driverName
);
308 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum
);
312 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
313 driverMajor
, driverMinor
, driverPatch
, *driverName
, scrNum
);
320 * Given a display pointer and screen number, return a __DRIdriver handle.
321 * Return NULL if anything goes wrong.
323 __DRIdriver
*driGetDriver(Display
*dpy
, int scrNum
)
326 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
328 ret
= OpenDriver(driverName
);
338 * Exported function for querying the DRI driver for a given screen.
340 * The returned char pointer points to a static array that will be
341 * overwritten by subsequent calls.
343 PUBLIC
const char *glXGetScreenDriver (Display
*dpy
, int scrNum
) {
346 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
350 len
= strlen (driverName
);
353 memcpy (ret
, driverName
, len
+1);
362 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
364 * The returned char pointer points directly into the driver. Therefore
365 * it should be treated as a constant.
367 * If the driver was not found or does not support configuration NULL is
370 * Note: The driver remains opened after this function returns.
372 PUBLIC
const char *glXGetDriverConfig (const char *driverName
) {
373 __DRIdriver
*driver
= OpenDriver (driverName
);
375 return dlsym (driver
->handle
, "__driConfigOptions");
381 /* Called from __glXFreeDisplayPrivate.
383 static void driDestroyDisplay(Display
*dpy
, void *private)
385 __DRIdisplayPrivate
*pdpyp
= (__DRIdisplayPrivate
*)private;
388 const int numScreens
= ScreenCount(dpy
);
390 for (i
= 0; i
< numScreens
; i
++) {
391 if (pdpyp
->libraryHandles
[i
]) {
392 __DRIdriver
*driver
, *prev
;
394 /* Remove driver from Drivers list */
395 for (prev
= NULL
, driver
= Drivers
; driver
;
396 prev
= driver
, driver
= driver
->next
) {
397 if (driver
->handle
== pdpyp
->libraryHandles
[i
]) {
399 prev
->next
= driver
->next
;
401 Drivers
= driver
->next
;
409 dlclose(pdpyp
->libraryHandles
[i
]);
412 Xfree(pdpyp
->libraryHandles
);
419 * Allocate, initialize and return a __DRIdisplayPrivate object.
420 * This is called from __glXInitialize() when we are given a new
423 void *driCreateDisplay(Display
*dpy
, __DRIdisplay
*pdisp
)
425 const int numScreens
= ScreenCount(dpy
);
426 __DRIdisplayPrivate
*pdpyp
;
427 int eventBase
, errorBase
;
428 int major
, minor
, patch
;
431 /* Initialize these fields to NULL in case we fail.
432 * If we don't do this we may later get segfaults trying to free random
433 * addresses when the display is closed.
435 pdisp
->private = NULL
;
436 pdisp
->destroyDisplay
= NULL
;
438 if (!XF86DRIQueryExtension(dpy
, &eventBase
, &errorBase
)) {
442 if (!XF86DRIQueryVersion(dpy
, &major
, &minor
, &patch
)) {
446 pdpyp
= (__DRIdisplayPrivate
*)Xmalloc(sizeof(__DRIdisplayPrivate
));
451 pdpyp
->driMajor
= major
;
452 pdpyp
->driMinor
= minor
;
453 pdpyp
->driPatch
= patch
;
455 pdisp
->destroyDisplay
= driDestroyDisplay
;
457 /* allocate array of pointers to createNewScreen funcs */
458 pdisp
->createNewScreen
= (PFNCREATENEWSCREENFUNC
*)
459 Xmalloc(numScreens
* sizeof(void *));
460 if (!pdisp
->createNewScreen
) {
465 /* allocate array of library handles */
466 pdpyp
->libraryHandles
= (void **) Xmalloc(numScreens
* sizeof(void*));
467 if (!pdpyp
->libraryHandles
) {
468 Xfree(pdisp
->createNewScreen
);
473 /* dynamically discover DRI drivers for all screens, saving each
474 * driver's "__driCreateScreen" function pointer. That's the bootstrap
475 * entrypoint for all DRI drivers.
477 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
478 __DRIdriver
*driver
= driGetDriver(dpy
, scrn
);
480 pdisp
->createNewScreen
[scrn
] = driver
->createNewScreenFunc
;
481 pdpyp
->libraryHandles
[scrn
] = driver
->handle
;
484 pdisp
->createNewScreen
[scrn
] = NULL
;
485 pdpyp
->libraryHandles
[scrn
] = NULL
;
489 return (void *)pdpyp
;
492 #endif /* GLX_DIRECT_RENDERING */