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 const char *search_path
;
400 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
402 static char buffer
[1024];
406 p
= getenv("EGL_DRIVERS_PATH");
407 #if defined(_EGL_OS_UNIX)
408 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
410 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
413 #endif /* _EGL_OS_UNIX */
416 ret
= _eglsnprintf(buffer
, sizeof(buffer
),
417 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
418 if (ret
> 0 && ret
< sizeof(buffer
))
419 search_path
= buffer
;
423 search_path
= _EGL_DRIVER_SEARCH_DIR
;
433 * Add the user driver to the module array.
435 * The user driver is specified by EGL_DRIVER.
438 _eglAddUserDriver(void)
440 const char *search_path
= _eglGetSearchPath();
443 env
= getenv("EGL_DRIVER");
444 #if defined(_EGL_OS_UNIX)
445 if (env
&& strchr(env
, '/')) {
447 if ((geteuid() != getuid() || getegid() != getgid())) {
449 "ignore EGL_DRIVER for setuid/setgid binaries");
453 #endif /* _EGL_OS_UNIX */
455 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
460 * Add default drivers to the module array.
463 _eglAddDefaultDrivers(void)
465 const char *search_path
= _eglGetSearchPath();
467 #if defined(_EGL_OS_WINDOWS)
468 const char *DefaultDriverNames
[] = {
471 #elif defined(_EGL_OS_UNIX)
472 const char *DefaultDriverNames
[] = {
479 for (i
= 0; i
< ARRAY_SIZE(DefaultDriverNames
); i
++) {
480 void *name
= (void *) DefaultDriverNames
[i
];
481 _eglPreloadForEach(search_path
, _eglLoaderFile
, name
);
487 * Add drivers to the module array. Drivers will be loaded as they are matched
496 /* the order here decides the priorities of the drivers */
498 _eglAddDefaultDrivers();
499 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern
, (void *) "egl_");
501 return (_eglModules
!= NULL
);
506 * Match a display to a driver. The display is initialized unless use_probe is
509 * The matching is done by finding the first driver that can initialize the
510 * display, or when use_probe is true, the driver with highest score.
513 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean use_probe
)
516 _EGLDriver
*best_drv
= NULL
;
517 EGLint best_score
= 0;
518 EGLint major
, minor
, i
;
520 _eglLockMutex(&_eglModuleMutex
);
522 if (!_eglAddDrivers()) {
523 _eglUnlockMutex(&_eglModuleMutex
);
527 /* match the loaded modules */
528 for (i
= 0; i
< _eglModules
->Size
; i
++) {
529 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
534 EGLint score
= (mod
->Driver
->Probe
) ?
535 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
536 if (score
> best_score
) {
537 best_drv
= mod
->Driver
;
542 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
)) {
543 best_drv
= mod
->Driver
;
548 if (best_score
>= 100)
552 /* load more modules */
554 EGLint first_unloaded
= i
;
556 while (i
< _eglModules
->Size
) {
557 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
558 assert(!mod
->Driver
);
560 if (!_eglLoadModule(mod
)) {
561 /* remove invalid modules */
562 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
567 best_score
= (mod
->Driver
->Probe
) ?
568 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
571 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
))
575 if (best_score
> 0) {
576 best_drv
= mod
->Driver
;
577 /* loaded modules come before unloaded ones */
578 if (first_unloaded
!= i
) {
579 void *tmp
= _eglModules
->Elements
[i
];
580 _eglModules
->Elements
[i
] =
581 _eglModules
->Elements
[first_unloaded
];
582 _eglModules
->Elements
[first_unloaded
] = tmp
;
587 _eglUnloadModule(mod
);
593 _eglUnlockMutex(&_eglModuleMutex
);
596 _eglLog(_EGL_DEBUG
, "the best driver is %s (score %d)",
597 best_drv
->Name
, best_score
);
599 dpy
->Driver
= best_drv
;
600 dpy
->Initialized
= EGL_TRUE
;
601 dpy
->APImajor
= major
;
602 dpy
->APIminor
= minor
;
610 __eglMustCastToProperFunctionPointerType
611 _eglGetDriverProc(const char *procname
)
614 _EGLProc proc
= NULL
;
617 /* load the driver for the default display */
618 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
619 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
620 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
624 for (i
= 0; i
< _eglModules
->Size
; i
++) {
625 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
629 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
639 * Unload all drivers.
642 _eglUnloadDrivers(void)
644 /* this is called at atexit time */
646 #if defined(_EGL_OS_UNIX)
647 _eglDestroyArray(_eglModules
, _eglFreeModule
);
648 #elif defined(_EGL_OS_WINDOWS)
649 /* XXX Windows unloads DLLs before atexit */
650 _eglDestroyArray(_eglModules
, NULL
);
658 * Invoke a callback function on each EGL search path.
660 * The first argument of the callback function is the name of the search path.
661 * The second argument is the length of the name.
664 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
667 const char *search_path
= _eglGetSearchPath();
668 _eglPreloadForEach(search_path
, callback
, callback_data
);