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)
147 mainFunc
= (_EGLMain_t
) dlsym(lib
, "_eglMain");
154 #else /* _EGL_PLATFORM_NO_OS */
155 /* must be the default driver name */
156 if (strcmp(driverPath
, DefaultDriverName
) == 0)
157 mainFunc
= (_EGLMain_t
) _eglMain
;
159 error
= "not builtin driver";
163 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
165 if (!getenv("EGL_DRIVER"))
166 _eglLog(_EGL_WARNING
,
167 "The driver can be overridden by setting EGL_DRIVER");
172 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
185 * Load the named driver.
188 _eglLoadDriver(const char *path
, const char *args
)
192 _EGLDriver
*drv
= NULL
;
194 mainFunc
= _eglOpenLibrary(path
, &lib
);
198 drv
= mainFunc(args
);
206 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", path
);
207 drv
->Name
= "UNNAMED";
210 drv
->Path
= _eglstrdup(path
);
211 drv
->Args
= (args
) ? _eglstrdup(args
) : NULL
;
212 if (!drv
->Path
|| (args
&& !drv
->Args
)) {
214 free((char *) drv
->Path
);
216 free((char *) drv
->Args
);
223 drv
->LibHandle
= lib
;
230 * Match a display to a preloaded driver.
232 * The matching is done by finding the driver with the highest score.
235 _eglMatchDriver(_EGLDisplay
*dpy
)
237 _EGLDriver
*best_drv
= NULL
;
238 EGLint best_score
= -1, i
;
240 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
241 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
244 score
= (drv
->Probe
) ? drv
->Probe(drv
, dpy
) : 0;
245 if (score
> best_score
) {
247 _eglLog(_EGL_DEBUG
, "driver %s has higher score than %s",
248 drv
->Name
, best_drv
->Name
);
264 * A loader function for use with _eglPreloadForEach. The loader data is the
265 * filename of the driver. This function stops on the first valid driver.
268 _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 drv
= _eglLoadDriver(path
, NULL
);
287 /* fix the path and load again */
288 if (!drv
&& library_suffix()) {
289 const char *suffix
= library_suffix();
290 size_t slen
= strlen(suffix
);
292 EGLBoolean need_suffix
;
294 p
= filename
+ flen
- slen
;
295 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
296 if (need_suffix
&& len
+ slen
+ 1 <= sizeof(path
)) {
297 strcpy(path
+ len
, suffix
);
298 drv
= _eglLoadDriver(path
, NULL
);
304 /* remember the driver and stop */
305 _eglGlobal
.Drivers
[_eglGlobal
.NumDrivers
++] = drv
;
311 * A loader function for use with _eglPreloadForEach. The loader data is the
312 * pattern (prefix) of the files to look for.
315 _eglLoaderPattern(const char *dir
, size_t len
, void *loader_data
)
317 #if defined(_EGL_PLATFORM_POSIX)
318 const char *prefix
, *suffix
;
319 size_t prefix_len
, suffix_len
;
321 struct dirent
*dirent
;
324 if (len
+ 2 > sizeof(path
))
327 memcpy(path
, dir
, len
);
332 dirp
= opendir(path
);
336 prefix
= (const char *) loader_data
;
337 prefix_len
= strlen(prefix
);
338 suffix
= library_suffix();
339 suffix_len
= (suffix
) ? strlen(suffix
) : 0;
341 while ((dirent
= readdir(dirp
))) {
343 size_t dirent_len
= strlen(dirent
->d_name
);
346 /* match the prefix */
347 if (strncmp(dirent
->d_name
, prefix
, prefix_len
) != 0)
349 /* match the suffix */
351 p
= dirent
->d_name
+ dirent_len
- suffix_len
;
352 if (p
< dirent
->d_name
|| strcmp(p
, suffix
) != 0)
356 /* make a full path and load the driver */
357 if (len
+ dirent_len
+ 1 <= sizeof(path
)) {
358 strcpy(path
+ len
, dirent
->d_name
);
359 drv
= _eglLoadDriver(path
, NULL
);
361 _eglGlobal
.Drivers
[_eglGlobal
.NumDrivers
++] = drv
;
368 #else /* _EGL_PLATFORM_POSIX */
369 /* stop immediately */
376 * Run the preload function on each driver directory and return the number of
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
;
388 EGLint num_drivers
= _eglGlobal
.NumDrivers
;
392 next
= strchr(cur
, ':');
393 len
= (next
) ? next
- cur
: strlen(cur
);
395 if (!loader(cur
, len
, loader
))
398 cur
= (next
) ? next
+ 1 : NULL
;
401 return (_eglGlobal
.NumDrivers
- num_drivers
);
406 * Return a list of colon-separated driver directories.
409 _eglGetSearchPath(void)
411 static const char *search_path
;
413 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
415 static char buffer
[1024];
419 p
= getenv("EGL_DRIVERS_PATH");
420 #if defined(_EGL_PLATFORM_POSIX)
421 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
423 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
426 #endif /* _EGL_PLATFORM_POSIX */
429 ret
= snprintf(buffer
, sizeof(buffer
),
430 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
431 if (ret
> 0 && ret
< sizeof(buffer
))
432 search_path
= buffer
;
436 search_path
= _EGL_DRIVER_SEARCH_DIR
;
446 * Preload a user driver.
448 * A user driver can be specified by EGL_DRIVER.
451 _eglPreloadUserDriver(void)
453 const char *search_path
= _eglGetSearchPath();
456 env
= getenv("EGL_DRIVER");
457 #if defined(_EGL_PLATFORM_POSIX)
458 if (env
&& strchr(env
, '/')) {
460 if ((geteuid() != getuid() || getegid() != getgid())) {
462 "ignore EGL_DRIVER for setuid/setgid binaries");
466 #endif /* _EGL_PLATFORM_POSIX */
470 if (!_eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
)) {
471 _eglLog(_EGL_WARNING
, "EGL_DRIVER is set to an invalid driver");
480 * Preload display drivers.
482 * Display drivers are a set of drivers that support a certain display system.
483 * The display system may be specified by EGL_DISPLAY.
485 * FIXME This makes libEGL a memory hog if an user driver is not specified and
486 * there are many display drivers.
489 _eglPreloadDisplayDrivers(void)
495 dpy
= getenv("EGL_DISPLAY");
497 dpy
= _EGL_DEFAULT_DISPLAY
;
501 ret
= snprintf(prefix
, sizeof(prefix
), "egl_%s_", dpy
);
502 if (ret
< 0 || ret
>= sizeof(prefix
))
505 return (_eglPreloadForEach(_eglGetSearchPath(),
506 _eglLoaderPattern
, (void *) prefix
) > 0);
511 * Preload the default driver.
514 _eglPreloadDefaultDriver(void)
516 return (_eglPreloadForEach(_eglGetSearchPath(),
517 _eglLoaderFile
, (void *) DefaultDriverName
) > 0);
524 * This function loads the driver modules and creates the corresponding
525 * _EGLDriver objects.
528 _eglPreloadDrivers(void)
532 /* already preloaded */
533 if (_eglGlobal
.NumDrivers
)
536 loaded
= (_eglPreloadUserDriver() ||
537 _eglPreloadDisplayDrivers() ||
538 _eglPreloadDefaultDriver());
545 * Unload preloaded drivers.
548 _eglUnloadDrivers(void)
551 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
552 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
553 lib_handle handle
= drv
->LibHandle
;
556 free((char *) drv
->Path
);
558 free((char *) drv
->Args
);
565 close_library(handle
);
566 _eglGlobal
.Drivers
[i
] = NULL
;
569 _eglGlobal
.NumDrivers
= 0;
574 * Plug all the available fallback routines into the given driver's
578 _eglInitDriverFallbacks(_EGLDriver
*drv
)
580 /* If a pointer is set to NULL, then the device driver _really_ has
583 drv
->API
.Initialize
= NULL
;
584 drv
->API
.Terminate
= NULL
;
586 drv
->API
.GetConfigs
= _eglGetConfigs
;
587 drv
->API
.ChooseConfig
= _eglChooseConfig
;
588 drv
->API
.GetConfigAttrib
= _eglGetConfigAttrib
;
590 drv
->API
.CreateContext
= _eglCreateContext
;
591 drv
->API
.DestroyContext
= _eglDestroyContext
;
592 drv
->API
.MakeCurrent
= _eglMakeCurrent
;
593 drv
->API
.QueryContext
= _eglQueryContext
;
595 drv
->API
.CreateWindowSurface
= _eglCreateWindowSurface
;
596 drv
->API
.CreatePixmapSurface
= _eglCreatePixmapSurface
;
597 drv
->API
.CreatePbufferSurface
= _eglCreatePbufferSurface
;
598 drv
->API
.DestroySurface
= _eglDestroySurface
;
599 drv
->API
.QuerySurface
= _eglQuerySurface
;
600 drv
->API
.SurfaceAttrib
= _eglSurfaceAttrib
;
601 drv
->API
.BindTexImage
= _eglBindTexImage
;
602 drv
->API
.ReleaseTexImage
= _eglReleaseTexImage
;
603 drv
->API
.SwapInterval
= _eglSwapInterval
;
604 drv
->API
.SwapBuffers
= _eglSwapBuffers
;
605 drv
->API
.CopyBuffers
= _eglCopyBuffers
;
607 drv
->API
.QueryString
= _eglQueryString
;
608 drv
->API
.WaitClient
= _eglWaitClient
;
609 drv
->API
.WaitNative
= _eglWaitNative
;
611 #ifdef EGL_MESA_screen_surface
612 drv
->API
.ChooseModeMESA
= _eglChooseModeMESA
;
613 drv
->API
.GetModesMESA
= _eglGetModesMESA
;
614 drv
->API
.GetModeAttribMESA
= _eglGetModeAttribMESA
;
615 drv
->API
.GetScreensMESA
= _eglGetScreensMESA
;
616 drv
->API
.CreateScreenSurfaceMESA
= _eglCreateScreenSurfaceMESA
;
617 drv
->API
.ShowScreenSurfaceMESA
= _eglShowScreenSurfaceMESA
;
618 drv
->API
.ScreenPositionMESA
= _eglScreenPositionMESA
;
619 drv
->API
.QueryScreenMESA
= _eglQueryScreenMESA
;
620 drv
->API
.QueryScreenSurfaceMESA
= _eglQueryScreenSurfaceMESA
;
621 drv
->API
.QueryScreenModeMESA
= _eglQueryScreenModeMESA
;
622 drv
->API
.QueryModeStringMESA
= _eglQueryModeStringMESA
;
623 #endif /* EGL_MESA_screen_surface */
625 #ifdef EGL_VERSION_1_2
626 drv
->API
.CreatePbufferFromClientBuffer
= _eglCreatePbufferFromClientBuffer
;
627 #endif /* EGL_VERSION_1_2 */
629 #ifdef EGL_KHR_image_base
630 drv
->API
.CreateImageKHR
= _eglCreateImageKHR
;
631 drv
->API
.DestroyImageKHR
= _eglDestroyImageKHR
;
632 #endif /* EGL_KHR_image_base */
637 * Set the probe cache at the given key.
639 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
640 * by multiple drivers.
643 _eglSetProbeCache(EGLint key
, const void *val
)
647 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
648 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
652 assert(idx
< NUM_PROBE_CACHE_SLOTS
);
654 _eglProbeCache
.keys
[idx
] = key
;
655 _eglProbeCache
.values
[idx
] = val
;
660 * Return the probe cache at the given key.
663 _eglGetProbeCache(EGLint key
)
667 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
668 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
672 return (idx
< NUM_PROBE_CACHE_SLOTS
&& _eglProbeCache
.keys
[idx
] == key
) ?
673 _eglProbeCache
.values
[idx
] : NULL
;