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>
42 #include "glxclient.h"
48 #include <sys/types.h>
58 #ifdef BUILT_IN_DRI_DRIVER
60 extern void *__driCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
61 int numConfigs
, __GLXvisualConfig
*config
);
64 #else /* BUILT_IN_DRI_DRIVER */
67 #ifndef DEFAULT_DRIVER_DIR
68 /* this is normally defined in the Imakefile */
69 #define DEFAULT_DRIVER_DIR "/usr/X11R6/lib/modules/dri"
72 static __DRIdriver
*Drivers
= NULL
;
79 static void InfoMessageF(const char *f
, ...)
84 if ((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) {
85 fprintf(stderr
, "libGL: ");
87 vfprintf(stderr
, f
, args
);
92 static void ErrorMessageF(const char *f
, ...)
96 if (getenv("LIBGL_DEBUG")) {
97 fprintf(stderr
, "libGL error: ");
99 vfprintf(stderr
, f
, args
);
106 * We'll save a pointer to this function when we couldn't find a
107 * direct rendering driver for a given screen.
109 static void *DummyCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
110 int numConfigs
, __GLXvisualConfig
*config
)
123 * Extract the ith directory path out of a colon-separated list of paths. No
124 * more than \c dirLen characters, including the terminating \c NUL, will be
127 * \param index Index of path to extract (starting at zero)
128 * \param paths The colon-separated list of paths
129 * \param dirLen Maximum length of result to store in \c dir
130 * \param dir Buffer to hold the extracted directory path
133 * The number of characters that would have been written to \c dir had there
134 * been enough room. This does not include the terminating \c NUL. When
135 * extraction fails, zero will be returned.
138 * It seems like this function could be rewritten to use \c strchr.
141 ExtractDir(int index
, const char *paths
, int dirLen
, char *dir
)
144 const char *start
, *end
;
154 else if (*start
== 0) {
155 /* end of string and couldn't find ith colon */
164 while (*start
== ':')
167 /* find next colon, or end of string */
169 while (*end
!= ':' && *end
!= 0) {
173 /* copy string between <start> and <end> into result string */
175 if (len
> dirLen
- 1)
177 strncpy(dir
, start
, len
);
180 return( end
- start
);
185 * Try to \c dlopen the named driver.
187 * This function adds the "_dri.so" suffix to the driver name and searches the
188 * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
189 * order to find the driver.
191 * \param driverName - a name like "tdfx", "i810", "mga", etc.
194 * A handle from \c dlopen, or \c NULL if driver file not found.
196 static __DRIdriver
*OpenDriver(const char *driverName
)
198 char *libPaths
= NULL
;
203 /* First, search Drivers list to see if we've already opened this driver */
204 for (driver
= Drivers
; driver
; driver
= driver
->next
) {
205 if (strcmp(driver
->name
, driverName
) == 0) {
211 if (geteuid() == getuid()) {
212 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
213 libPaths
= getenv("LIBGL_DRIVERS_PATH");
215 libPaths
= getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
218 libPaths
= DEFAULT_DRIVER_DIR
;
220 for ( i
= 0 ; ExtractDir(i
, libPaths
, 1000, libDir
) != 0 ; i
++ ) {
221 char realDriverName
[200];
225 /* If TLS support is enabled, try to open the TLS version of the driver
226 * binary first. If that fails, try the non-TLS version.
229 snprintf(realDriverName
, 200, "%s/tls/%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 snprintf(realDriverName
, 200, "%s/%s_dri.so", libDir
, driverName
);
236 InfoMessageF("OpenDriver: trying %s\n", realDriverName
);
237 handle
= dlopen(realDriverName
, RTLD_NOW
| RTLD_GLOBAL
);
240 if ( handle
!= NULL
) {
241 /* allocate __DRIdriver struct */
242 driver
= (__DRIdriver
*) Xmalloc(sizeof(__DRIdriver
));
244 return NULL
; /* out of memory! */
245 /* init the struct */
246 driver
->name
= __glXstrdup(driverName
);
249 return NULL
; /* out of memory! */
252 driver
->createScreenFunc
= (CreateScreenFunc
)
253 dlsym(handle
, "__driCreateScreen");
254 driver
->createNewScreenFunc
= (CreateNewScreenFunc
)
255 dlsym(handle
, "__driCreateNewScreen");
257 if ( (driver
->createScreenFunc
== NULL
)
258 && (driver
->createNewScreenFunc
== NULL
) ) {
259 /* If the driver doesn't have this symbol then something's
260 * really, really wrong.
262 ErrorMessageF("Neither __driCreateScreen or __driCreateNewScreen "
263 "are defined in %s_dri.so!\n", driverName
);
268 driver
->handle
= handle
;
269 /* put at head of linked list */
270 driver
->next
= Drivers
;
275 ErrorMessageF("dlopen %s failed (%s)\n", realDriverName
, dlerror());
279 ErrorMessageF("unable to find driver: %s_dri.so\n", driverName
);
285 * Given a display pointer and screen number, determine the name of
286 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
287 * Return True for success, False for failure.
289 static Bool
GetDriverName(Display
*dpy
, int scrNum
, char **driverName
)
293 int driverMajor
, driverMinor
, driverPatch
;
297 if (!XF86DRIQueryDirectRenderingCapable(dpy
, scrNum
, &directCapable
)) {
298 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
301 if (!directCapable
) {
302 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
306 b
= XF86DRIGetClientDriverName(dpy
, scrNum
, &driverMajor
, &driverMinor
,
307 &driverPatch
, driverName
);
309 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum
);
313 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
314 driverMajor
, driverMinor
, driverPatch
, *driverName
, scrNum
);
321 * Given a display pointer and screen number, return a __DRIdriver handle.
322 * Return NULL if anything goes wrong.
324 __DRIdriver
*driGetDriver(Display
*dpy
, int scrNum
)
327 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
329 ret
= OpenDriver(driverName
);
339 * Exported function for querying the DRI driver for a given screen.
341 * The returned char pointer points to a static array that will be
342 * overwritten by subsequent calls.
344 const char *glXGetScreenDriver (Display
*dpy
, int scrNum
) {
347 if (GetDriverName(dpy
, scrNum
, &driverName
)) {
351 len
= strlen (driverName
);
354 memcpy (ret
, driverName
, len
+1);
363 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
365 * The returned char pointer points directly into the driver. Therefore
366 * it should be treated as a constant.
368 * If the driver was not found or does not support configuration NULL is
371 * Note: The driver remains opened after this function returns.
373 const char *glXGetDriverConfig (const char *driverName
) {
374 __DRIdriver
*driver
= OpenDriver (driverName
);
376 return dlsym (driver
->handle
, "__driConfigOptions");
382 #endif /* BUILT_IN_DRI_DRIVER */
385 /* This function isn't currently used.
387 static void driDestroyDisplay(Display
*dpy
, void *private)
389 __DRIdisplayPrivate
*pdpyp
= (__DRIdisplayPrivate
*)private;
392 const int numScreens
= ScreenCount(dpy
);
394 for (i
= 0; i
< numScreens
; i
++) {
395 if (pdpyp
->libraryHandles
[i
])
396 dlclose(pdpyp
->libraryHandles
[i
]);
398 Xfree(pdpyp
->libraryHandles
);
405 * Allocate, initialize and return a __DRIdisplayPrivate object.
406 * This is called from __glXInitialize() when we are given a new
409 void *driCreateDisplay(Display
*dpy
, __DRIdisplay
*pdisp
)
411 const int numScreens
= ScreenCount(dpy
);
412 __DRIdisplayPrivate
*pdpyp
;
413 int eventBase
, errorBase
;
414 int major
, minor
, patch
;
417 /* Initialize these fields to NULL in case we fail.
418 * If we don't do this we may later get segfaults trying to free random
419 * addresses when the display is closed.
421 pdisp
->private = NULL
;
422 pdisp
->destroyDisplay
= NULL
;
423 pdisp
->createScreen
= NULL
;
425 if (!XF86DRIQueryExtension(dpy
, &eventBase
, &errorBase
)) {
429 if (!XF86DRIQueryVersion(dpy
, &major
, &minor
, &patch
)) {
433 pdpyp
= (__DRIdisplayPrivate
*)Xmalloc(sizeof(__DRIdisplayPrivate
));
438 pdpyp
->driMajor
= major
;
439 pdpyp
->driMinor
= minor
;
440 pdpyp
->driPatch
= patch
;
442 pdisp
->destroyDisplay
= driDestroyDisplay
;
444 /* allocate array of pointers to createScreen funcs */
445 pdisp
->createScreen
= (CreateScreenFunc
*) Xmalloc(numScreens
* sizeof(void *));
446 if (!pdisp
->createScreen
) {
451 /* allocate array of pointers to createScreen funcs */
452 pdisp
->createNewScreen
= (CreateNewScreenFunc
*) Xmalloc(numScreens
* sizeof(void *));
453 if (!pdisp
->createNewScreen
) {
454 Xfree(pdisp
->createScreen
);
459 /* allocate array of library handles */
460 pdpyp
->libraryHandles
= (void **) Xmalloc(numScreens
* sizeof(void*));
461 if (!pdpyp
->libraryHandles
) {
462 Xfree(pdisp
->createNewScreen
);
463 Xfree(pdisp
->createScreen
);
468 #ifdef BUILT_IN_DRI_DRIVER
469 /* we'll statically bind to the built-in __driCreateScreen function */
470 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
471 pdisp
->createScreen
[scrn
] = __driCreateScreen
;
472 pdisp
->createNewScreen
[scrn
] = NULL
;
473 pdpyp
->libraryHandles
[scrn
] = NULL
;
477 /* dynamically discover DRI drivers for all screens, saving each
478 * driver's "__driCreateScreen" function pointer. That's the bootstrap
479 * entrypoint for all DRI drivers.
481 for (scrn
= 0; scrn
< numScreens
; scrn
++) {
482 __DRIdriver
*driver
= driGetDriver(dpy
, scrn
);
484 pdisp
->createScreen
[scrn
] = driver
->createScreenFunc
;
485 pdisp
->createNewScreen
[scrn
] = driver
->createNewScreenFunc
;
486 pdpyp
->libraryHandles
[scrn
] = driver
->handle
;
489 pdisp
->createScreen
[scrn
] = DummyCreateScreen
;
490 pdisp
->createNewScreen
[scrn
] = NULL
;
491 pdpyp
->libraryHandles
[scrn
] = NULL
;
496 return (void *)pdpyp
;
499 #endif /* GLX_DIRECT_RENDERING */