2 * Functions for choosing and opening/loading device drivers.
10 #include "eglconfig.h"
11 #include "eglcontext.h"
12 #include "egldefines.h"
13 #include "egldisplay.h"
14 #include "egldriver.h"
15 #include "eglglobals.h"
19 #include "eglscreen.h"
20 #include "eglstring.h"
21 #include "eglsurface.h"
24 #if defined(_EGL_PLATFORM_POSIX)
26 #include <sys/types.h>
33 * Wrappers for dlopen/dlclose()
35 #if defined(_EGL_PLATFORM_WINDOWS)
38 /* XXX Need to decide how to do dynamic name lookup on Windows */
39 static const char DefaultDriverName
[] = "TBD";
41 typedef HMODULE lib_handle
;
44 open_library(const char *filename
)
46 return LoadLibrary(filename
);
50 close_library(HMODULE lib
)
63 #elif defined(_EGL_PLATFORM_POSIX)
66 static const char DefaultDriverName
[] = "egl_glx";
68 typedef void * lib_handle
;
71 open_library(const char *filename
)
73 return dlopen(filename
, RTLD_LAZY
);
77 close_library(void *lib
)
90 #else /* _EGL_PLATFORM_NO_OS */
93 static const char DefaultDriverName
[] = "builtin";
95 typedef void *lib_handle
;
98 open_library(const char *filename
)
100 return (void *) filename
;
104 close_library(void *lib
)
119 #define NUM_PROBE_CACHE_SLOTS 8
121 EGLint keys
[NUM_PROBE_CACHE_SLOTS
];
122 const void *values
[NUM_PROBE_CACHE_SLOTS
];
127 * Open the named driver and find its bootstrap function: _eglMain().
130 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
133 _EGLMain_t mainFunc
= NULL
;
134 const char *error
= "unknown error";
138 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
139 lib
= open_library(driverPath
);
141 #if defined(_EGL_PLATFORM_WINDOWS)
144 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
145 #elif defined(_EGL_PLATFORM_POSIX)
151 /* direct cast gives a warning when compiled with -pedantic */
152 tmp
.ptr
= dlsym(lib
, "_eglMain");
160 #else /* _EGL_PLATFORM_NO_OS */
161 /* must be the default driver name */
162 if (strcmp(driverPath
, DefaultDriverName
) == 0)
163 mainFunc
= (_EGLMain_t
) _eglMain
;
165 error
= "not builtin driver";
169 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
171 if (!getenv("EGL_DRIVER"))
172 _eglLog(_EGL_WARNING
,
173 "The driver can be overridden by setting EGL_DRIVER");
178 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
191 * Load the named driver.
194 _eglLoadDriver(const char *path
, const char *args
)
198 _EGLDriver
*drv
= NULL
;
200 mainFunc
= _eglOpenLibrary(path
, &lib
);
204 drv
= mainFunc(args
);
212 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", path
);
213 drv
->Name
= "UNNAMED";
216 drv
->Path
= _eglstrdup(path
);
217 drv
->Args
= (args
) ? _eglstrdup(args
) : NULL
;
218 if (!drv
->Path
|| (args
&& !drv
->Args
)) {
220 free((char *) drv
->Path
);
222 free((char *) drv
->Args
);
229 drv
->LibHandle
= lib
;
236 * Match a display to a preloaded driver.
238 * The matching is done by finding the driver with the highest score.
241 _eglMatchDriver(_EGLDisplay
*dpy
)
243 _EGLDriver
*best_drv
= NULL
;
244 EGLint best_score
= -1, i
;
247 * this function is called after preloading and the drivers never change
250 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
251 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
254 score
= (drv
->Probe
) ? drv
->Probe(drv
, dpy
) : 0;
255 if (score
> best_score
) {
257 _eglLog(_EGL_DEBUG
, "driver %s has higher score than %s",
258 drv
->Name
, best_drv
->Name
);
274 * A loader function for use with _eglPreloadForEach. The loader data is the
275 * filename of the driver. This function stops on the first valid driver.
278 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
282 const char *filename
= (const char *) loader_data
;
283 size_t flen
= strlen(filename
);
285 /* make a full path */
286 if (len
+ flen
+ 2 > sizeof(path
))
289 memcpy(path
, dir
, len
);
292 memcpy(path
+ len
, filename
, flen
);
296 drv
= _eglLoadDriver(path
, NULL
);
297 /* fix the path and load again */
298 if (!drv
&& library_suffix()) {
299 const char *suffix
= library_suffix();
300 size_t slen
= strlen(suffix
);
302 EGLBoolean need_suffix
;
304 p
= filename
+ flen
- slen
;
305 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
306 if (need_suffix
&& len
+ slen
+ 1 <= sizeof(path
)) {
307 strcpy(path
+ len
, suffix
);
308 drv
= _eglLoadDriver(path
, NULL
);
314 /* remember the driver and stop */
315 _eglGlobal
.Drivers
[_eglGlobal
.NumDrivers
++] = drv
;
321 * A loader function for use with _eglPreloadForEach. The loader data is the
322 * pattern (prefix) of the files to look for.
325 _eglLoaderPattern(const char *dir
, size_t len
, void *loader_data
)
327 #if defined(_EGL_PLATFORM_POSIX)
328 const char *prefix
, *suffix
;
329 size_t prefix_len
, suffix_len
;
331 struct dirent
*dirent
;
334 if (len
+ 2 > sizeof(path
))
337 memcpy(path
, dir
, len
);
342 dirp
= opendir(path
);
346 prefix
= (const char *) loader_data
;
347 prefix_len
= strlen(prefix
);
348 suffix
= library_suffix();
349 suffix_len
= (suffix
) ? strlen(suffix
) : 0;
351 while ((dirent
= readdir(dirp
))) {
353 size_t dirent_len
= strlen(dirent
->d_name
);
356 /* match the prefix */
357 if (strncmp(dirent
->d_name
, prefix
, prefix_len
) != 0)
359 /* match the suffix */
361 p
= dirent
->d_name
+ dirent_len
- suffix_len
;
362 if (p
< dirent
->d_name
|| strcmp(p
, suffix
) != 0)
366 /* make a full path and load the driver */
367 if (len
+ dirent_len
+ 1 <= sizeof(path
)) {
368 strcpy(path
+ len
, dirent
->d_name
);
369 drv
= _eglLoadDriver(path
, NULL
);
371 _eglGlobal
.Drivers
[_eglGlobal
.NumDrivers
++] = drv
;
378 #else /* _EGL_PLATFORM_POSIX */
379 /* stop immediately */
386 * Run the preload function on each driver directory and return the number of
389 * The process may end prematurely if the callback function returns false.
392 _eglPreloadForEach(const char *search_path
,
393 EGLBoolean (*loader
)(const char *, size_t, void *),
396 const char *cur
, *next
;
398 EGLint num_drivers
= _eglGlobal
.NumDrivers
;
402 next
= strchr(cur
, ':');
403 len
= (next
) ? next
- cur
: strlen(cur
);
405 if (!loader(cur
, len
, loader_data
))
408 cur
= (next
) ? next
+ 1 : NULL
;
411 return (_eglGlobal
.NumDrivers
- num_drivers
);
416 * Return a list of colon-separated driver directories.
419 _eglGetSearchPath(void)
421 static const char *search_path
;
423 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
425 static char buffer
[1024];
429 p
= getenv("EGL_DRIVERS_PATH");
430 #if defined(_EGL_PLATFORM_POSIX)
431 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
433 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
436 #endif /* _EGL_PLATFORM_POSIX */
439 ret
= snprintf(buffer
, sizeof(buffer
),
440 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
441 if (ret
> 0 && ret
< sizeof(buffer
))
442 search_path
= buffer
;
446 search_path
= _EGL_DRIVER_SEARCH_DIR
;
456 * Preload a user driver.
458 * A user driver can be specified by EGL_DRIVER.
461 _eglPreloadUserDriver(void)
463 const char *search_path
= _eglGetSearchPath();
466 env
= getenv("EGL_DRIVER");
467 #if defined(_EGL_PLATFORM_POSIX)
468 if (env
&& strchr(env
, '/')) {
470 if ((geteuid() != getuid() || getegid() != getgid())) {
472 "ignore EGL_DRIVER for setuid/setgid binaries");
476 #endif /* _EGL_PLATFORM_POSIX */
480 if (!_eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
)) {
481 _eglLog(_EGL_WARNING
, "EGL_DRIVER is set to an invalid driver");
490 * Preload display drivers.
492 * Display drivers are a set of drivers that support a certain display system.
493 * The display system may be specified by EGL_DISPLAY.
495 * FIXME This makes libEGL a memory hog if an user driver is not specified and
496 * there are many display drivers.
499 _eglPreloadDisplayDrivers(void)
505 dpy
= getenv("EGL_DISPLAY");
507 dpy
= _EGL_DEFAULT_DISPLAY
;
511 ret
= snprintf(prefix
, sizeof(prefix
), "egl_%s_", dpy
);
512 if (ret
< 0 || ret
>= sizeof(prefix
))
515 return (_eglPreloadForEach(_eglGetSearchPath(),
516 _eglLoaderPattern
, (void *) prefix
) > 0);
521 * Preload the default driver.
524 _eglPreloadDefaultDriver(void)
526 return (_eglPreloadForEach(_eglGetSearchPath(),
527 _eglLoaderFile
, (void *) DefaultDriverName
) > 0);
534 * This function loads the driver modules and creates the corresponding
535 * _EGLDriver objects.
538 _eglPreloadDrivers(void)
542 /* protect the preloading process */
543 _eglLockMutex(_eglGlobal
.Mutex
);
545 /* already preloaded */
546 if (_eglGlobal
.NumDrivers
) {
547 _eglUnlockMutex(_eglGlobal
.Mutex
);
551 loaded
= (_eglPreloadUserDriver() ||
552 _eglPreloadDisplayDrivers() ||
553 _eglPreloadDefaultDriver());
555 _eglUnlockMutex(_eglGlobal
.Mutex
);
562 * Unload preloaded drivers.
565 _eglUnloadDrivers(void)
569 /* this is called at atexit time */
570 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
571 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
572 lib_handle handle
= drv
->LibHandle
;
575 free((char *) drv
->Path
);
577 free((char *) drv
->Args
);
584 close_library(handle
);
585 _eglGlobal
.Drivers
[i
] = NULL
;
588 _eglGlobal
.NumDrivers
= 0;
593 * Plug all the available fallback routines into the given driver's
597 _eglInitDriverFallbacks(_EGLDriver
*drv
)
599 /* If a pointer is set to NULL, then the device driver _really_ has
602 drv
->API
.Initialize
= NULL
;
603 drv
->API
.Terminate
= NULL
;
605 drv
->API
.GetConfigs
= _eglGetConfigs
;
606 drv
->API
.ChooseConfig
= _eglChooseConfig
;
607 drv
->API
.GetConfigAttrib
= _eglGetConfigAttrib
;
609 drv
->API
.CreateContext
= _eglCreateContext
;
610 drv
->API
.DestroyContext
= _eglDestroyContext
;
611 drv
->API
.MakeCurrent
= _eglMakeCurrent
;
612 drv
->API
.QueryContext
= _eglQueryContext
;
614 drv
->API
.CreateWindowSurface
= _eglCreateWindowSurface
;
615 drv
->API
.CreatePixmapSurface
= _eglCreatePixmapSurface
;
616 drv
->API
.CreatePbufferSurface
= _eglCreatePbufferSurface
;
617 drv
->API
.DestroySurface
= _eglDestroySurface
;
618 drv
->API
.QuerySurface
= _eglQuerySurface
;
619 drv
->API
.SurfaceAttrib
= _eglSurfaceAttrib
;
620 drv
->API
.BindTexImage
= _eglBindTexImage
;
621 drv
->API
.ReleaseTexImage
= _eglReleaseTexImage
;
622 drv
->API
.SwapInterval
= _eglSwapInterval
;
623 drv
->API
.SwapBuffers
= _eglSwapBuffers
;
624 drv
->API
.CopyBuffers
= _eglCopyBuffers
;
626 drv
->API
.QueryString
= _eglQueryString
;
627 drv
->API
.WaitClient
= _eglWaitClient
;
628 drv
->API
.WaitNative
= _eglWaitNative
;
630 #ifdef EGL_MESA_screen_surface
631 drv
->API
.ChooseModeMESA
= _eglChooseModeMESA
;
632 drv
->API
.GetModesMESA
= _eglGetModesMESA
;
633 drv
->API
.GetModeAttribMESA
= _eglGetModeAttribMESA
;
634 drv
->API
.GetScreensMESA
= _eglGetScreensMESA
;
635 drv
->API
.CreateScreenSurfaceMESA
= _eglCreateScreenSurfaceMESA
;
636 drv
->API
.ShowScreenSurfaceMESA
= _eglShowScreenSurfaceMESA
;
637 drv
->API
.ScreenPositionMESA
= _eglScreenPositionMESA
;
638 drv
->API
.QueryScreenMESA
= _eglQueryScreenMESA
;
639 drv
->API
.QueryScreenSurfaceMESA
= _eglQueryScreenSurfaceMESA
;
640 drv
->API
.QueryScreenModeMESA
= _eglQueryScreenModeMESA
;
641 drv
->API
.QueryModeStringMESA
= _eglQueryModeStringMESA
;
642 #endif /* EGL_MESA_screen_surface */
644 #ifdef EGL_VERSION_1_2
645 drv
->API
.CreatePbufferFromClientBuffer
= _eglCreatePbufferFromClientBuffer
;
646 #endif /* EGL_VERSION_1_2 */
648 #ifdef EGL_KHR_image_base
649 drv
->API
.CreateImageKHR
= _eglCreateImageKHR
;
650 drv
->API
.DestroyImageKHR
= _eglDestroyImageKHR
;
651 #endif /* EGL_KHR_image_base */
656 * Invoke a callback function on each EGL search path.
658 * The first argument of the callback function is the name of the search path.
659 * The second argument is the length of the name.
662 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
665 const char *search_path
= _eglGetSearchPath();
666 _eglPreloadForEach(search_path
, callback
, callback_data
);
671 * Set the probe cache at the given key.
673 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
674 * by multiple drivers.
677 _eglSetProbeCache(EGLint key
, const void *val
)
681 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
682 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
686 assert(idx
< NUM_PROBE_CACHE_SLOTS
);
688 _eglProbeCache
.keys
[idx
] = key
;
689 _eglProbeCache
.values
[idx
] = val
;
694 * Return the probe cache at the given key.
697 _eglGetProbeCache(EGLint key
)
701 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
702 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
706 return (idx
< NUM_PROBE_CACHE_SLOTS
&& _eglProbeCache
.keys
[idx
] == key
) ?
707 _eglProbeCache
.values
[idx
] : NULL
;