2 * Functions for choosing and opening/loading device drivers.
11 #include "eglstring.h"
12 #include "egldefines.h"
13 #include "egldisplay.h"
14 #include "egldriver.h"
18 #if defined(_EGL_OS_UNIX)
20 #include <sys/types.h>
26 typedef struct _egl_module
{
32 static _EGL_DECLARE_MUTEX(_eglModuleMutex
);
33 static _EGLArray
*_eglModules
;
37 * Wrappers for dlopen/dlclose()
39 #if defined(_EGL_OS_WINDOWS)
42 typedef HMODULE lib_handle
;
45 open_library(const char *filename
)
47 return LoadLibrary(filename
);
51 close_library(HMODULE lib
)
64 #elif defined(_EGL_OS_UNIX)
67 typedef void * lib_handle
;
70 open_library(const char *filename
)
72 return dlopen(filename
, RTLD_LAZY
);
76 close_library(void *lib
)
93 * Open the named driver and find its bootstrap function: _eglMain().
96 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
99 _EGLMain_t mainFunc
= NULL
;
100 const char *error
= "unknown error";
104 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
105 lib
= open_library(driverPath
);
107 #if defined(_EGL_OS_WINDOWS)
110 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
111 #elif defined(_EGL_OS_UNIX)
117 /* direct cast gives a warning when compiled with -pedantic */
118 tmp
.ptr
= dlsym(lib
, "_eglMain");
129 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
131 if (!getenv("EGL_DRIVER"))
132 _eglLog(_EGL_WARNING
,
133 "The driver can be overridden by setting EGL_DRIVER");
138 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
151 * Load a module and create the driver object.
154 _eglLoadModule(_EGLModule
*mod
)
160 mainFunc
= _eglOpenLibrary(mod
->Path
, &lib
);
164 drv
= mainFunc(NULL
);
172 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", mod
->Path
);
173 drv
->Name
= "UNNAMED";
176 mod
->Handle
= (void *) lib
;
187 _eglUnloadModule(_EGLModule
*mod
)
189 /* destroy the driver */
190 if (mod
->Driver
&& mod
->Driver
->Unload
)
191 mod
->Driver
->Unload(mod
->Driver
);
193 close_library(mod
->Handle
);
201 * Add a module to the module array.
204 _eglAddModule(const char *path
)
210 _eglModules
= _eglCreateArray("Module", 8);
215 /* find duplicates */
216 for (i
= 0; i
< _eglModules
->Size
; i
++) {
217 mod
= _eglModules
->Elements
[i
];
218 if (strcmp(mod
->Path
, path
) == 0)
222 /* allocate a new one */
223 mod
= calloc(1, sizeof(*mod
));
225 mod
->Path
= _eglstrdup(path
);
232 _eglAppendArray(_eglModules
, (void *) mod
);
233 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Path
);
244 _eglFreeModule(void *module
)
246 _EGLModule
*mod
= (_EGLModule
*) module
;
248 _eglUnloadModule(mod
);
255 * A loader function for use with _eglPreloadForEach. The loader data is the
256 * filename of the driver. This function stops on the first valid driver.
259 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
262 const char *filename
= (const char *) loader_data
;
263 size_t flen
= strlen(filename
);
265 /* make a full path */
266 if (len
+ flen
+ 2 > sizeof(path
))
269 memcpy(path
, dir
, len
);
272 memcpy(path
+ len
, filename
, flen
);
276 if (library_suffix()) {
277 const char *suffix
= library_suffix();
278 size_t slen
= strlen(suffix
);
280 EGLBoolean need_suffix
;
282 p
= filename
+ flen
- slen
;
283 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
286 if (len
+ slen
+ 1 > sizeof(path
))
288 strcpy(path
+ len
, suffix
);
292 #if defined(_EGL_OS_UNIX)
293 /* check if the file exists */
294 if (access(path
, F_OK
))
305 * A loader function for use with _eglPreloadForEach. The loader data is the
306 * pattern (prefix) of the files to look for.
309 _eglLoaderPattern(const char *dir
, size_t len
, void *loader_data
)
311 #if defined(_EGL_OS_UNIX)
312 const char *prefix
, *suffix
;
313 size_t prefix_len
, suffix_len
;
315 struct dirent
*dirent
;
318 if (len
+ 2 > sizeof(path
))
321 memcpy(path
, dir
, len
);
326 dirp
= opendir(path
);
330 prefix
= (const char *) loader_data
;
331 prefix_len
= strlen(prefix
);
332 suffix
= library_suffix();
333 suffix_len
= (suffix
) ? strlen(suffix
) : 0;
335 while ((dirent
= readdir(dirp
))) {
336 size_t dirent_len
= strlen(dirent
->d_name
);
339 /* match the prefix */
340 if (strncmp(dirent
->d_name
, prefix
, prefix_len
) != 0)
342 /* match the suffix */
344 p
= dirent
->d_name
+ dirent_len
- suffix_len
;
345 if (p
< dirent
->d_name
|| strcmp(p
, suffix
) != 0)
349 /* make a full path and add it to the module array */
350 if (len
+ dirent_len
+ 1 <= sizeof(path
)) {
351 strcpy(path
+ len
, dirent
->d_name
);
359 #else /* _EGL_OS_UNIX */
360 /* stop immediately */
367 * Run the callback function on each driver directory.
369 * The process may end prematurely if the callback function returns false.
372 _eglPreloadForEach(const char *search_path
,
373 EGLBoolean (*loader
)(const char *, size_t, void *),
376 const char *cur
, *next
;
381 next
= strchr(cur
, ':');
382 len
= (next
) ? next
- cur
: strlen(cur
);
384 if (!loader(cur
, len
, loader_data
))
387 cur
= (next
) ? next
+ 1 : NULL
;
393 * Return a list of colon-separated driver directories.
396 _eglGetSearchPath(void)
398 static char search_path
[1024];
400 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
401 if (search_path
[0] == '\0') {
402 char *buf
= search_path
;
403 size_t len
= sizeof(search_path
);
408 #if defined(_EGL_OS_UNIX)
409 use_env
= (geteuid() == getuid() && getegid() == getgid());
419 /* extract the dirname from EGL_DRIVER */
420 p
= getenv("EGL_DRIVER");
421 if (p
&& strchr(p
, dir_sep
)) {
422 ret
= _eglsnprintf(buf
, len
, "%s", p
);
423 if (ret
> 0 && ret
< len
) {
424 p
= strrchr(buf
, dir_sep
);
432 /* append EGL_DRIVERS_PATH */
433 p
= getenv("EGL_DRIVERS_PATH");
435 ret
= _eglsnprintf(buf
, len
, "%s:", p
);
436 if (ret
> 0 && ret
< len
) {
444 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
447 ret
= _eglsnprintf(buf
, len
, "%s", _EGL_DRIVER_SEARCH_DIR
);
448 if (ret
< 0 || ret
>= len
)
449 search_path
[0] = '\0';
451 _eglLog(_EGL_DEBUG
, "EGL search path is %s", search_path
);
453 #endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
460 * Add the user driver to the module array.
462 * The user driver is specified by EGL_DRIVER.
465 _eglAddUserDriver(void)
467 const char *search_path
= _eglGetSearchPath();
470 env
= getenv("EGL_DRIVER");
471 #if defined(_EGL_OS_UNIX)
472 if (env
&& strchr(env
, '/')) {
474 if ((geteuid() != getuid() || getegid() != getgid())) {
476 "ignore EGL_DRIVER for setuid/setgid binaries");
480 #endif /* _EGL_OS_UNIX */
482 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
487 * Add default drivers to the module array.
490 _eglAddDefaultDrivers(void)
492 const char *search_path
= _eglGetSearchPath();
494 #if defined(_EGL_OS_WINDOWS)
495 const char *DefaultDriverNames
[] = {
498 #elif defined(_EGL_OS_UNIX)
499 const char *DefaultDriverNames
[] = {
506 for (i
= 0; i
< ARRAY_SIZE(DefaultDriverNames
); i
++) {
507 void *name
= (void *) DefaultDriverNames
[i
];
508 _eglPreloadForEach(search_path
, _eglLoaderFile
, name
);
514 * Add drivers to the module array. Drivers will be loaded as they are matched
523 /* the order here decides the priorities of the drivers */
525 _eglAddDefaultDrivers();
526 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern
, (void *) "egl_");
528 return (_eglModules
!= NULL
);
533 * Match a display to a driver. The display is initialized unless use_probe is
536 * The matching is done by finding the first driver that can initialize the
537 * display, or when use_probe is true, the driver with highest score.
540 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean use_probe
)
543 _EGLDriver
*best_drv
= NULL
;
544 EGLint best_score
= 0;
545 EGLint major
, minor
, i
;
547 _eglLockMutex(&_eglModuleMutex
);
549 if (!_eglAddDrivers()) {
550 _eglUnlockMutex(&_eglModuleMutex
);
554 /* match the loaded modules */
555 for (i
= 0; i
< _eglModules
->Size
; i
++) {
556 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
561 EGLint score
= (mod
->Driver
->Probe
) ?
562 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
563 if (score
> best_score
) {
564 best_drv
= mod
->Driver
;
569 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
)) {
570 best_drv
= mod
->Driver
;
575 if (best_score
>= 100)
579 /* load more modules */
581 EGLint first_unloaded
= i
;
583 while (i
< _eglModules
->Size
) {
584 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
585 assert(!mod
->Driver
);
587 if (!_eglLoadModule(mod
)) {
588 /* remove invalid modules */
589 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
594 best_score
= (mod
->Driver
->Probe
) ?
595 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
598 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
))
602 if (best_score
> 0) {
603 best_drv
= mod
->Driver
;
604 /* loaded modules come before unloaded ones */
605 if (first_unloaded
!= i
) {
606 void *tmp
= _eglModules
->Elements
[i
];
607 _eglModules
->Elements
[i
] =
608 _eglModules
->Elements
[first_unloaded
];
609 _eglModules
->Elements
[first_unloaded
] = tmp
;
614 _eglUnloadModule(mod
);
620 _eglUnlockMutex(&_eglModuleMutex
);
623 _eglLog(_EGL_DEBUG
, "the best driver is %s (score %d)",
624 best_drv
->Name
, best_score
);
626 dpy
->Driver
= best_drv
;
627 dpy
->Initialized
= EGL_TRUE
;
628 dpy
->APImajor
= major
;
629 dpy
->APIminor
= minor
;
637 __eglMustCastToProperFunctionPointerType
638 _eglGetDriverProc(const char *procname
)
641 _EGLProc proc
= NULL
;
644 /* load the driver for the default display */
645 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
646 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
647 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
651 for (i
= 0; i
< _eglModules
->Size
; i
++) {
652 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
656 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
666 * Unload all drivers.
669 _eglUnloadDrivers(void)
671 /* this is called at atexit time */
673 #if defined(_EGL_OS_UNIX)
674 _eglDestroyArray(_eglModules
, _eglFreeModule
);
675 #elif defined(_EGL_OS_WINDOWS)
676 /* XXX Windows unloads DLLs before atexit */
677 _eglDestroyArray(_eglModules
, NULL
);
685 * Invoke a callback function on each EGL search path.
687 * The first argument of the callback function is the name of the search path.
688 * The second argument is the length of the name.
691 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
694 const char *search_path
= _eglGetSearchPath();
695 _eglPreloadForEach(search_path
, callback
, callback_data
);