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"
20 #include "eglscreen.h"
21 #include "eglstring.h"
22 #include "eglsurface.h"
26 #if defined(_EGL_OS_UNIX)
28 #include <sys/types.h>
34 typedef struct _egl_module
{
40 static _EGL_DECLARE_MUTEX(_eglModuleMutex
);
41 static _EGLArray
*_eglModules
;
45 * Wrappers for dlopen/dlclose()
47 #if defined(_EGL_OS_WINDOWS)
50 typedef HMODULE lib_handle
;
53 open_library(const char *filename
)
55 return LoadLibrary(filename
);
59 close_library(HMODULE lib
)
72 #elif defined(_EGL_OS_UNIX)
75 typedef void * lib_handle
;
78 open_library(const char *filename
)
80 return dlopen(filename
, RTLD_LAZY
);
84 close_library(void *lib
)
101 * Open the named driver and find its bootstrap function: _eglMain().
104 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
107 _EGLMain_t mainFunc
= NULL
;
108 const char *error
= "unknown error";
112 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
113 lib
= open_library(driverPath
);
115 #if defined(_EGL_OS_WINDOWS)
118 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
119 #elif defined(_EGL_OS_UNIX)
125 /* direct cast gives a warning when compiled with -pedantic */
126 tmp
.ptr
= dlsym(lib
, "_eglMain");
137 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
139 if (!getenv("EGL_DRIVER"))
140 _eglLog(_EGL_WARNING
,
141 "The driver can be overridden by setting EGL_DRIVER");
146 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
159 * Load a module and create the driver object.
162 _eglLoadModule(_EGLModule
*mod
)
168 mainFunc
= _eglOpenLibrary(mod
->Path
, &lib
);
172 drv
= mainFunc(NULL
);
180 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", mod
->Path
);
181 drv
->Name
= "UNNAMED";
184 mod
->Handle
= (void *) lib
;
195 _eglUnloadModule(_EGLModule
*mod
)
197 /* destroy the driver */
198 if (mod
->Driver
&& mod
->Driver
->Unload
)
199 mod
->Driver
->Unload(mod
->Driver
);
201 close_library(mod
->Handle
);
209 * Add a module to the module array.
212 _eglAddModule(const char *path
)
218 _eglModules
= _eglCreateArray("Module", 8);
223 /* find duplicates */
224 for (i
= 0; i
< _eglModules
->Size
; i
++) {
225 mod
= _eglModules
->Elements
[i
];
226 if (strcmp(mod
->Path
, path
) == 0)
230 /* allocate a new one */
231 mod
= calloc(1, sizeof(*mod
));
233 mod
->Path
= _eglstrdup(path
);
240 _eglAppendArray(_eglModules
, (void *) mod
);
241 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Path
);
252 _eglFreeModule(void *module
)
254 _EGLModule
*mod
= (_EGLModule
*) module
;
256 _eglUnloadModule(mod
);
263 * A loader function for use with _eglPreloadForEach. The loader data is the
264 * filename of the driver. This function stops on the first valid driver.
267 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
270 const char *filename
= (const char *) loader_data
;
271 size_t flen
= strlen(filename
);
273 /* make a full path */
274 if (len
+ flen
+ 2 > sizeof(path
))
277 memcpy(path
, dir
, len
);
280 memcpy(path
+ len
, filename
, flen
);
284 if (library_suffix()) {
285 const char *suffix
= library_suffix();
286 size_t slen
= strlen(suffix
);
288 EGLBoolean need_suffix
;
290 p
= filename
+ flen
- slen
;
291 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
294 if (len
+ slen
+ 1 > sizeof(path
))
296 strcpy(path
+ len
, suffix
);
300 #if defined(_EGL_OS_UNIX)
301 /* check if the file exists */
302 if (access(path
, F_OK
))
313 * A loader function for use with _eglPreloadForEach. The loader data is the
314 * pattern (prefix) of the files to look for.
317 _eglLoaderPattern(const char *dir
, size_t len
, void *loader_data
)
319 #if defined(_EGL_OS_UNIX)
320 const char *prefix
, *suffix
;
321 size_t prefix_len
, suffix_len
;
323 struct dirent
*dirent
;
326 if (len
+ 2 > sizeof(path
))
329 memcpy(path
, dir
, len
);
334 dirp
= opendir(path
);
338 prefix
= (const char *) loader_data
;
339 prefix_len
= strlen(prefix
);
340 suffix
= library_suffix();
341 suffix_len
= (suffix
) ? strlen(suffix
) : 0;
343 while ((dirent
= readdir(dirp
))) {
344 size_t dirent_len
= strlen(dirent
->d_name
);
347 /* match the prefix */
348 if (strncmp(dirent
->d_name
, prefix
, prefix_len
) != 0)
350 /* match the suffix */
352 p
= dirent
->d_name
+ dirent_len
- suffix_len
;
353 if (p
< dirent
->d_name
|| strcmp(p
, suffix
) != 0)
357 /* make a full path and add it to the module array */
358 if (len
+ dirent_len
+ 1 <= sizeof(path
)) {
359 strcpy(path
+ len
, dirent
->d_name
);
367 #else /* _EGL_OS_UNIX */
368 /* stop immediately */
375 * Run the callback function on each driver directory.
377 * The process may end prematurely if the callback function returns false.
380 _eglPreloadForEach(const char *search_path
,
381 EGLBoolean (*loader
)(const char *, size_t, void *),
384 const char *cur
, *next
;
389 next
= strchr(cur
, ':');
390 len
= (next
) ? next
- cur
: strlen(cur
);
392 if (!loader(cur
, len
, loader_data
))
395 cur
= (next
) ? next
+ 1 : NULL
;
401 * Return a list of colon-separated driver directories.
404 _eglGetSearchPath(void)
406 static const char *search_path
;
408 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
410 static char buffer
[1024];
414 p
= getenv("EGL_DRIVERS_PATH");
415 #if defined(_EGL_OS_UNIX)
416 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
418 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
421 #endif /* _EGL_OS_UNIX */
424 ret
= _eglsnprintf(buffer
, sizeof(buffer
),
425 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
426 if (ret
> 0 && ret
< sizeof(buffer
))
427 search_path
= buffer
;
431 search_path
= _EGL_DRIVER_SEARCH_DIR
;
441 * Add the user driver to the module array.
443 * The user driver is specified by EGL_DRIVER.
446 _eglAddUserDriver(void)
448 const char *search_path
= _eglGetSearchPath();
451 env
= getenv("EGL_DRIVER");
452 #if defined(_EGL_OS_UNIX)
453 if (env
&& strchr(env
, '/')) {
455 if ((geteuid() != getuid() || getegid() != getgid())) {
457 "ignore EGL_DRIVER for setuid/setgid binaries");
461 #endif /* _EGL_OS_UNIX */
463 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
468 * Add default drivers to the module array.
471 _eglAddDefaultDrivers(void)
473 const char *search_path
= _eglGetSearchPath();
475 #if defined(_EGL_OS_WINDOWS)
476 const char *DefaultDriverNames
[] = {
479 #elif defined(_EGL_OS_UNIX)
480 const char *DefaultDriverNames
[] = {
487 for (i
= 0; i
< ARRAY_SIZE(DefaultDriverNames
); i
++) {
488 void *name
= (void *) DefaultDriverNames
[i
];
489 _eglPreloadForEach(search_path
, _eglLoaderFile
, name
);
495 * Add drivers to the module array. Drivers will be loaded as they are matched
504 /* the order here decides the priorities of the drivers */
506 _eglAddDefaultDrivers();
507 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern
, (void *) "egl_");
509 return (_eglModules
!= NULL
);
514 * Match a display to a driver. The display is initialized unless use_probe is
517 * The matching is done by finding the first driver that can initialize the
518 * display, or when use_probe is true, the driver with highest score.
521 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean use_probe
)
524 _EGLDriver
*best_drv
= NULL
;
525 EGLint best_score
= 0;
526 EGLint major
, minor
, i
;
528 _eglLockMutex(&_eglModuleMutex
);
530 if (!_eglAddDrivers()) {
531 _eglUnlockMutex(&_eglModuleMutex
);
535 /* match the loaded modules */
536 for (i
= 0; i
< _eglModules
->Size
; i
++) {
537 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
542 EGLint score
= (mod
->Driver
->Probe
) ?
543 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
544 if (score
> best_score
) {
545 best_drv
= mod
->Driver
;
550 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
)) {
551 best_drv
= mod
->Driver
;
556 if (best_score
>= 100)
560 /* load more modules */
562 EGLint first_unloaded
= i
;
564 while (i
< _eglModules
->Size
) {
565 mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
566 assert(!mod
->Driver
);
568 if (!_eglLoadModule(mod
)) {
569 /* remove invalid modules */
570 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
575 best_score
= (mod
->Driver
->Probe
) ?
576 mod
->Driver
->Probe(mod
->Driver
, dpy
) : 1;
579 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
, &major
, &minor
))
583 if (best_score
> 0) {
584 best_drv
= mod
->Driver
;
585 /* loaded modules come before unloaded ones */
586 if (first_unloaded
!= i
) {
587 void *tmp
= _eglModules
->Elements
[i
];
588 _eglModules
->Elements
[i
] =
589 _eglModules
->Elements
[first_unloaded
];
590 _eglModules
->Elements
[first_unloaded
] = tmp
;
595 _eglUnloadModule(mod
);
601 _eglUnlockMutex(&_eglModuleMutex
);
604 _eglLog(_EGL_DEBUG
, "the best driver is %s (score %d)",
605 best_drv
->Name
, best_score
);
607 dpy
->Driver
= best_drv
;
608 dpy
->Initialized
= EGL_TRUE
;
609 dpy
->APImajor
= major
;
610 dpy
->APIminor
= minor
;
618 __eglMustCastToProperFunctionPointerType
619 _eglGetDriverProc(const char *procname
)
622 _EGLProc proc
= NULL
;
625 /* load the driver for the default display */
626 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
627 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
628 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
632 for (i
= 0; i
< _eglModules
->Size
; i
++) {
633 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
637 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
647 * Unload all drivers.
650 _eglUnloadDrivers(void)
652 /* this is called at atexit time */
654 #if defined(_EGL_OS_UNIX)
655 _eglDestroyArray(_eglModules
, _eglFreeModule
);
656 #elif defined(_EGL_OS_WINDOWS)
657 /* XXX Windows unloads DLLs before atexit */
658 _eglDestroyArray(_eglModules
, NULL
);
666 * Plug all the available fallback routines into the given driver's
670 _eglInitDriverFallbacks(_EGLDriver
*drv
)
672 /* If a pointer is set to NULL, then the device driver _really_ has
675 drv
->API
.Initialize
= NULL
;
676 drv
->API
.Terminate
= NULL
;
678 drv
->API
.GetConfigs
= _eglGetConfigs
;
679 drv
->API
.ChooseConfig
= _eglChooseConfig
;
680 drv
->API
.GetConfigAttrib
= _eglGetConfigAttrib
;
682 drv
->API
.CreateContext
= _eglCreateContext
;
683 drv
->API
.DestroyContext
= _eglDestroyContext
;
684 drv
->API
.MakeCurrent
= _eglMakeCurrent
;
685 drv
->API
.QueryContext
= _eglQueryContext
;
687 drv
->API
.CreateWindowSurface
= _eglCreateWindowSurface
;
688 drv
->API
.CreatePixmapSurface
= _eglCreatePixmapSurface
;
689 drv
->API
.CreatePbufferSurface
= _eglCreatePbufferSurface
;
690 drv
->API
.DestroySurface
= _eglDestroySurface
;
691 drv
->API
.QuerySurface
= _eglQuerySurface
;
692 drv
->API
.SurfaceAttrib
= _eglSurfaceAttrib
;
693 drv
->API
.BindTexImage
= _eglBindTexImage
;
694 drv
->API
.ReleaseTexImage
= _eglReleaseTexImage
;
695 drv
->API
.SwapInterval
= _eglSwapInterval
;
696 drv
->API
.SwapBuffers
= _eglSwapBuffers
;
697 drv
->API
.CopyBuffers
= _eglCopyBuffers
;
699 drv
->API
.QueryString
= _eglQueryString
;
700 drv
->API
.WaitClient
= _eglWaitClient
;
701 drv
->API
.WaitNative
= _eglWaitNative
;
703 #ifdef EGL_MESA_screen_surface
704 drv
->API
.ChooseModeMESA
= _eglChooseModeMESA
;
705 drv
->API
.GetModesMESA
= _eglGetModesMESA
;
706 drv
->API
.GetModeAttribMESA
= _eglGetModeAttribMESA
;
707 drv
->API
.GetScreensMESA
= _eglGetScreensMESA
;
708 drv
->API
.CreateScreenSurfaceMESA
= _eglCreateScreenSurfaceMESA
;
709 drv
->API
.ShowScreenSurfaceMESA
= _eglShowScreenSurfaceMESA
;
710 drv
->API
.ScreenPositionMESA
= _eglScreenPositionMESA
;
711 drv
->API
.QueryScreenMESA
= _eglQueryScreenMESA
;
712 drv
->API
.QueryScreenSurfaceMESA
= _eglQueryScreenSurfaceMESA
;
713 drv
->API
.QueryScreenModeMESA
= _eglQueryScreenModeMESA
;
714 drv
->API
.QueryModeStringMESA
= _eglQueryModeStringMESA
;
715 #endif /* EGL_MESA_screen_surface */
717 #ifdef EGL_VERSION_1_2
718 drv
->API
.CreatePbufferFromClientBuffer
= _eglCreatePbufferFromClientBuffer
;
719 #endif /* EGL_VERSION_1_2 */
721 #ifdef EGL_KHR_image_base
722 drv
->API
.CreateImageKHR
= _eglCreateImageKHR
;
723 drv
->API
.DestroyImageKHR
= _eglDestroyImageKHR
;
724 #endif /* EGL_KHR_image_base */
729 * Invoke a callback function on each EGL search path.
731 * The first argument of the callback function is the name of the search path.
732 * The second argument is the length of the name.
735 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
738 const char *search_path
= _eglGetSearchPath();
739 _eglPreloadForEach(search_path
, callback
, callback_data
);