2 * Functions for choosing and opening/loading device drivers.
11 #include "eglstring.h"
12 #include "eglconfig.h"
13 #include "eglcontext.h"
14 #include "egldefines.h"
15 #include "egldisplay.h"
16 #include "egldriver.h"
17 #include "eglglobals.h"
21 #include "eglscreen.h"
22 #include "eglstring.h"
23 #include "eglsurface.h"
27 #if defined(_EGL_OS_UNIX)
29 #include <sys/types.h>
35 typedef struct _egl_module
{
41 static _EGL_DECLARE_MUTEX(_eglModuleMutex
);
42 static _EGLArray
*_eglModules
;
46 * Wrappers for dlopen/dlclose()
48 #if defined(_EGL_OS_WINDOWS)
51 typedef HMODULE lib_handle
;
54 open_library(const char *filename
)
56 return LoadLibrary(filename
);
60 close_library(HMODULE lib
)
73 #elif defined(_EGL_OS_UNIX)
76 typedef void * lib_handle
;
79 open_library(const char *filename
)
81 return dlopen(filename
, RTLD_LAZY
);
85 close_library(void *lib
)
102 * Open the named driver and find its bootstrap function: _eglMain().
105 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
108 _EGLMain_t mainFunc
= NULL
;
109 const char *error
= "unknown error";
113 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
114 lib
= open_library(driverPath
);
116 #if defined(_EGL_OS_WINDOWS)
119 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
120 #elif defined(_EGL_OS_UNIX)
126 /* direct cast gives a warning when compiled with -pedantic */
127 tmp
.ptr
= dlsym(lib
, "_eglMain");
138 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
140 if (!getenv("EGL_DRIVER"))
141 _eglLog(_EGL_WARNING
,
142 "The driver can be overridden by setting EGL_DRIVER");
147 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
160 * Load a module and create the driver object.
163 _eglLoadModule(_EGLModule
*mod
)
169 mainFunc
= _eglOpenLibrary(mod
->Path
, &lib
);
173 drv
= mainFunc(NULL
);
181 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", mod
->Path
);
182 drv
->Name
= "UNNAMED";
185 mod
->Handle
= (void *) lib
;
196 _eglUnloadModule(_EGLModule
*mod
)
198 /* destroy the driver */
199 if (mod
->Driver
&& mod
->Driver
->Unload
)
200 mod
->Driver
->Unload(mod
->Driver
);
202 close_library(mod
->Handle
);
210 * Add a module to the module array.
213 _eglAddModule(const char *path
)
219 _eglModules
= _eglCreateArray("Module", 8);
224 /* find duplicates */
225 for (i
= 0; i
< _eglModules
->Size
; i
++) {
226 mod
= _eglModules
->Elements
[i
];
227 if (strcmp(mod
->Path
, path
) == 0)
231 /* allocate a new one */
232 mod
= calloc(1, sizeof(*mod
));
234 mod
->Path
= _eglstrdup(path
);
241 _eglAppendArray(_eglModules
, (void *) mod
);
242 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Path
);
253 _eglFreeModule(void *module
)
255 _EGLModule
*mod
= (_EGLModule
*) module
;
257 _eglUnloadModule(mod
);
265 * A loader function for use with _eglPreloadForEach. The loader data is the
266 * filename of the driver. This function stops on the first valid driver.
269 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
272 const char *filename
= (const char *) loader_data
;
273 size_t flen
= strlen(filename
);
275 /* make a full path */
276 if (len
+ flen
+ 2 > sizeof(path
))
279 memcpy(path
, dir
, len
);
282 memcpy(path
+ len
, filename
, flen
);
286 if (library_suffix()) {
287 const char *suffix
= library_suffix();
288 size_t slen
= strlen(suffix
);
290 EGLBoolean need_suffix
;
292 p
= filename
+ flen
- slen
;
293 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
296 if (len
+ slen
+ 1 > sizeof(path
))
298 strcpy(path
+ len
, suffix
);
302 #if defined(_EGL_OS_UNIX)
303 /* check if the file exists */
304 if (access(path
, F_OK
))
315 * A loader function for use with _eglPreloadForEach. The loader data is the
316 * pattern (prefix) of the files to look for.
319 _eglLoaderPattern(const char *dir
, size_t len
, void *loader_data
)
321 #if defined(_EGL_OS_UNIX)
322 const char *prefix
, *suffix
;
323 size_t prefix_len
, suffix_len
;
325 struct dirent
*dirent
;
328 if (len
+ 2 > sizeof(path
))
331 memcpy(path
, dir
, len
);
336 dirp
= opendir(path
);
340 prefix
= (const char *) loader_data
;
341 prefix_len
= strlen(prefix
);
342 suffix
= library_suffix();
343 suffix_len
= (suffix
) ? strlen(suffix
) : 0;
345 while ((dirent
= readdir(dirp
))) {
346 size_t dirent_len
= strlen(dirent
->d_name
);
349 /* match the prefix */
350 if (strncmp(dirent
->d_name
, prefix
, prefix_len
) != 0)
352 /* match the suffix */
354 p
= dirent
->d_name
+ dirent_len
- suffix_len
;
355 if (p
< dirent
->d_name
|| strcmp(p
, suffix
) != 0)
359 /* make a full path and add it to the module array */
360 if (len
+ dirent_len
+ 1 <= sizeof(path
)) {
361 strcpy(path
+ len
, dirent
->d_name
);
369 #else /* _EGL_OS_UNIX */
370 /* stop immediately */
377 * Run the callback function on each driver directory.
379 * The process may end prematurely if the callback function returns false.
382 _eglPreloadForEach(const char *search_path
,
383 EGLBoolean (*loader
)(const char *, size_t, void *),
386 const char *cur
, *next
;
391 next
= strchr(cur
, ':');
392 len
= (next
) ? next
- cur
: strlen(cur
);
394 if (!loader(cur
, len
, loader_data
))
397 cur
= (next
) ? next
+ 1 : NULL
;
403 * Return a list of colon-separated driver directories.
406 _eglGetSearchPath(void)
408 static const char *search_path
;
410 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
412 static char buffer
[1024];
416 p
= getenv("EGL_DRIVERS_PATH");
417 #if defined(_EGL_OS_UNIX)
418 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
420 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
423 #endif /* _EGL_OS_UNIX */
426 ret
= _eglsnprintf(buffer
, sizeof(buffer
),
427 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
428 if (ret
> 0 && ret
< sizeof(buffer
))
429 search_path
= buffer
;
433 search_path
= _EGL_DRIVER_SEARCH_DIR
;
443 * Add the user driver to the module array.
445 * The user driver is specified by EGL_DRIVER.
448 _eglAddUserDriver(void)
450 const char *search_path
= _eglGetSearchPath();
453 env
= getenv("EGL_DRIVER");
454 #if defined(_EGL_OS_UNIX)
455 if (env
&& strchr(env
, '/')) {
457 if ((geteuid() != getuid() || getegid() != getgid())) {
459 "ignore EGL_DRIVER for setuid/setgid binaries");
463 #endif /* _EGL_OS_UNIX */
465 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
470 * Add default drivers to the module array.
473 _eglAddDefaultDrivers(void)
475 const char *search_path
= _eglGetSearchPath();
477 #if defined(_EGL_OS_WINDOWS)
478 const char *DefaultDriverNames
[] = {
481 #elif defined(_EGL_OS_UNIX)
482 const char *DefaultDriverNames
[] = {
489 for (i
= 0; i
< ARRAY_SIZE(DefaultDriverNames
); i
++) {
490 void *name
= (void *) DefaultDriverNames
[i
];
491 _eglPreloadForEach(search_path
, _eglLoaderFile
, name
);
497 * Add drivers to the module array. Drivers will be loaded as they are matched
506 /* the order here decides the priorities of the drivers */
508 _eglAddDefaultDrivers();
509 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern
, (void *) "egl_");
511 return (_eglModules
!= NULL
);
516 * Match a display to a driver. The display is initialized unless use_probe is
519 * The matching is done by finding the first driver that can initialize the
520 * display, or when use_probe is true, the driver with highest score.
523 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean use_probe
)
526 _EGLDriver
*best_drv
= NULL
;
527 EGLint best_score
= 0;
528 EGLint major
, minor
, i
;
530 _eglLockMutex(&_eglModuleMutex
);
532 if (!_eglAddDrivers()) {
533 _eglUnlockMutex(&_eglModuleMutex
);
537 /* match the loaded modules */
538 for (i
= 0; i
< _eglModules
->Size
; i
++) {
539 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
544 EGLint score
= (mod
->Driver
->Probe
) ?
545 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
546 if (score
> best_score
) {
547 best_drv
= mod
->Driver
;
552 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
)) {
553 best_drv
= mod
->Driver
;
558 if (best_score
>= 100)
562 /* load more modules */
564 EGLint first_unloaded
= i
;
566 while (i
< _eglModules
->Size
) {
567 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
568 assert(!mod
->Driver
);
570 if (!_eglLoadModule(mod
)) {
571 /* remove invalid modules */
572 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
577 best_score
= (mod
->Driver
->Probe
) ?
578 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
581 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
))
585 if (best_score
> 0) {
586 best_drv
= mod
->Driver
;
587 /* loaded modules come before unloaded ones */
588 if (first_unloaded
!= i
) {
589 void *tmp
= _eglModules
->Elements
[i
];
590 _eglModules
->Elements
[i
] =
591 _eglModules
->Elements
[first_unloaded
];
592 _eglModules
->Elements
[first_unloaded
] = tmp
;
597 _eglUnloadModule(mod
);
603 _eglUnlockMutex(&_eglModuleMutex
);
606 _eglLog(_EGL_DEBUG
, "the best driver is %s (score %d)",
607 best_drv
->Name
, best_score
);
609 dpy
->Driver
= best_drv
;
610 dpy
->Initialized
= EGL_TRUE
;
611 dpy
->APImajor
= major
;
612 dpy
->APIminor
= minor
;
620 __eglMustCastToProperFunctionPointerType
621 _eglGetDriverProc(const char *procname
)
624 _EGLProc proc
= NULL
;
627 /* load the driver for the default display */
628 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
629 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
630 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
634 for (i
= 0; i
< _eglModules
->Size
; i
++) {
635 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
639 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
649 * Unload all drivers.
652 _eglUnloadDrivers(void)
654 /* this is called at atexit time */
656 #if defined(_EGL_OS_UNIX)
657 _eglDestroyArray(_eglModules
, _eglFreeModule
);
658 #elif defined(_EGL_OS_WINDOWS)
659 /* XXX Windows unloads DLLs before atexit */
660 _eglDestroyArray(_eglModules
, NULL
);
668 * Plug all the available fallback routines into the given driver's
672 _eglInitDriverFallbacks(_EGLDriver
*drv
)
674 /* If a pointer is set to NULL, then the device driver _really_ has
677 drv
->API
.Initialize
= NULL
;
678 drv
->API
.Terminate
= NULL
;
680 drv
->API
.GetConfigs
= _eglGetConfigs
;
681 drv
->API
.ChooseConfig
= _eglChooseConfig
;
682 drv
->API
.GetConfigAttrib
= _eglGetConfigAttrib
;
684 drv
->API
.CreateContext
= _eglCreateContext
;
685 drv
->API
.DestroyContext
= _eglDestroyContext
;
686 drv
->API
.MakeCurrent
= _eglMakeCurrent
;
687 drv
->API
.QueryContext
= _eglQueryContext
;
689 drv
->API
.CreateWindowSurface
= _eglCreateWindowSurface
;
690 drv
->API
.CreatePixmapSurface
= _eglCreatePixmapSurface
;
691 drv
->API
.CreatePbufferSurface
= _eglCreatePbufferSurface
;
692 drv
->API
.DestroySurface
= _eglDestroySurface
;
693 drv
->API
.QuerySurface
= _eglQuerySurface
;
694 drv
->API
.SurfaceAttrib
= _eglSurfaceAttrib
;
695 drv
->API
.BindTexImage
= _eglBindTexImage
;
696 drv
->API
.ReleaseTexImage
= _eglReleaseTexImage
;
697 drv
->API
.SwapInterval
= _eglSwapInterval
;
698 drv
->API
.SwapBuffers
= _eglSwapBuffers
;
699 drv
->API
.CopyBuffers
= _eglCopyBuffers
;
701 drv
->API
.QueryString
= _eglQueryString
;
702 drv
->API
.WaitClient
= _eglWaitClient
;
703 drv
->API
.WaitNative
= _eglWaitNative
;
705 #ifdef EGL_MESA_screen_surface
706 drv
->API
.ChooseModeMESA
= _eglChooseModeMESA
;
707 drv
->API
.GetModesMESA
= _eglGetModesMESA
;
708 drv
->API
.GetModeAttribMESA
= _eglGetModeAttribMESA
;
709 drv
->API
.GetScreensMESA
= _eglGetScreensMESA
;
710 drv
->API
.CreateScreenSurfaceMESA
= _eglCreateScreenSurfaceMESA
;
711 drv
->API
.ShowScreenSurfaceMESA
= _eglShowScreenSurfaceMESA
;
712 drv
->API
.ScreenPositionMESA
= _eglScreenPositionMESA
;
713 drv
->API
.QueryScreenMESA
= _eglQueryScreenMESA
;
714 drv
->API
.QueryScreenSurfaceMESA
= _eglQueryScreenSurfaceMESA
;
715 drv
->API
.QueryScreenModeMESA
= _eglQueryScreenModeMESA
;
716 drv
->API
.QueryModeStringMESA
= _eglQueryModeStringMESA
;
717 #endif /* EGL_MESA_screen_surface */
719 #ifdef EGL_VERSION_1_2
720 drv
->API
.CreatePbufferFromClientBuffer
= _eglCreatePbufferFromClientBuffer
;
721 #endif /* EGL_VERSION_1_2 */
723 #ifdef EGL_KHR_image_base
724 drv
->API
.CreateImageKHR
= _eglCreateImageKHR
;
725 drv
->API
.DestroyImageKHR
= _eglDestroyImageKHR
;
726 #endif /* EGL_KHR_image_base */
731 * Invoke a callback function on each EGL search path.
733 * The first argument of the callback function is the name of the search path.
734 * The second argument is the length of the name.
737 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
740 const char *search_path
= _eglGetSearchPath();
741 _eglPreloadForEach(search_path
, callback
, callback_data
);