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"
26 #if defined(_EGL_OS_UNIX)
28 #include <sys/types.h>
35 * Wrappers for dlopen/dlclose()
37 #if defined(_EGL_OS_WINDOWS)
40 /* XXX Need to decide how to do dynamic name lookup on Windows */
41 static const char *DefaultDriverNames
[] = {
45 typedef HMODULE lib_handle
;
48 open_library(const char *filename
)
50 return LoadLibrary(filename
);
54 close_library(HMODULE lib
)
67 #elif defined(_EGL_OS_UNIX)
70 static const char *DefaultDriverNames
[] = {
76 typedef void * lib_handle
;
79 open_library(const char *filename
)
81 return dlopen(filename
, RTLD_LAZY
);
85 close_library(void *lib
)
101 #define NUM_PROBE_CACHE_SLOTS 8
103 EGLint keys
[NUM_PROBE_CACHE_SLOTS
];
104 const void *values
[NUM_PROBE_CACHE_SLOTS
];
109 * Open the named driver and find its bootstrap function: _eglMain().
112 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
115 _EGLMain_t mainFunc
= NULL
;
116 const char *error
= "unknown error";
120 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
121 lib
= open_library(driverPath
);
123 #if defined(_EGL_OS_WINDOWS)
126 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
127 #elif defined(_EGL_OS_UNIX)
133 /* direct cast gives a warning when compiled with -pedantic */
134 tmp
.ptr
= dlsym(lib
, "_eglMain");
145 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
147 if (!getenv("EGL_DRIVER"))
148 _eglLog(_EGL_WARNING
,
149 "The driver can be overridden by setting EGL_DRIVER");
154 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
167 * Load the named driver.
170 _eglLoadDriver(const char *path
, const char *args
)
174 _EGLDriver
*drv
= NULL
;
176 mainFunc
= _eglOpenLibrary(path
, &lib
);
180 drv
= mainFunc(args
);
188 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", path
);
189 drv
->Name
= "UNNAMED";
192 drv
->Path
= _eglstrdup(path
);
193 drv
->Args
= (args
) ? _eglstrdup(args
) : NULL
;
194 if (!drv
->Path
|| (args
&& !drv
->Args
)) {
196 free((char *) drv
->Path
);
198 free((char *) drv
->Args
);
205 drv
->LibHandle
= lib
;
212 * Match a display to a preloaded driver.
214 * The matching is done by finding the driver with the highest score.
217 _eglMatchDriver(_EGLDisplay
*dpy
)
219 _EGLDriver
*best_drv
= NULL
;
220 EGLint best_score
= -1, i
;
223 * this function is called after preloading and the drivers never change
226 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
227 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
230 score
= (drv
->Probe
) ? drv
->Probe(drv
, dpy
) : 0;
231 if (score
> best_score
) {
233 _eglLog(_EGL_DEBUG
, "driver %s has higher score than %s",
234 drv
->Name
, best_drv
->Name
);
250 * A loader function for use with _eglPreloadForEach. The loader data is the
251 * filename of the driver. This function stops on the first valid driver.
254 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
258 const char *filename
= (const char *) loader_data
;
259 size_t flen
= strlen(filename
);
261 /* make a full path */
262 if (len
+ flen
+ 2 > sizeof(path
))
265 memcpy(path
, dir
, len
);
268 memcpy(path
+ len
, filename
, flen
);
272 if (library_suffix() == NULL
|| strstr(path
, library_suffix()))
273 drv
= _eglLoadDriver(path
, NULL
);
275 const char *suffix
= library_suffix();
276 size_t slen
= strlen(suffix
);
278 EGLBoolean need_suffix
;
280 p
= filename
+ flen
- slen
;
281 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
282 if (need_suffix
&& len
+ slen
+ 1 <= sizeof(path
)) {
283 strcpy(path
+ len
, suffix
);
284 drv
= _eglLoadDriver(path
, NULL
);
292 /* remember the driver and stop */
293 _eglGlobal
.Drivers
[_eglGlobal
.NumDrivers
++] = drv
;
299 * Run the preload function on each driver directory and return the number of
302 * The process may end prematurely if the callback function returns false.
305 _eglPreloadForEach(const char *search_path
,
306 EGLBoolean (*loader
)(const char *, size_t, void *),
309 const char *cur
, *next
;
311 EGLint num_drivers
= _eglGlobal
.NumDrivers
;
315 next
= strchr(cur
, ':');
316 len
= (next
) ? next
- cur
: strlen(cur
);
318 if (!loader(cur
, len
, loader_data
))
321 cur
= (next
) ? next
+ 1 : NULL
;
324 return (_eglGlobal
.NumDrivers
- num_drivers
);
329 * Return a list of colon-separated driver directories.
332 _eglGetSearchPath(void)
334 static const char *search_path
;
336 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
338 static char buffer
[1024];
342 p
= getenv("EGL_DRIVERS_PATH");
343 #if defined(_EGL_OS_UNIX)
344 if (p
&& (geteuid() != getuid() || getegid() != getgid())) {
346 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
349 #endif /* _EGL_OS_UNIX */
352 ret
= _eglsnprintf(buffer
, sizeof(buffer
),
353 "%s:%s", p
, _EGL_DRIVER_SEARCH_DIR
);
354 if (ret
> 0 && ret
< sizeof(buffer
))
355 search_path
= buffer
;
359 search_path
= _EGL_DRIVER_SEARCH_DIR
;
369 * Preload a user driver.
371 * A user driver can be specified by EGL_DRIVER.
374 _eglPreloadUserDriver(void)
376 const char *search_path
= _eglGetSearchPath();
379 env
= getenv("EGL_DRIVER");
380 #if defined(_EGL_OS_UNIX)
381 if (env
&& strchr(env
, '/')) {
383 if ((geteuid() != getuid() || getegid() != getgid())) {
385 "ignore EGL_DRIVER for setuid/setgid binaries");
389 #endif /* _EGL_OS_UNIX */
393 if (!_eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
)) {
394 _eglLog(_EGL_WARNING
, "EGL_DRIVER is set to an invalid driver");
405 * This function loads the driver modules and creates the corresponding
406 * _EGLDriver objects.
409 _eglPreloadDrivers(void)
413 /* protect the preloading process */
414 _eglLockMutex(_eglGlobal
.Mutex
);
416 /* already preloaded */
417 if (_eglGlobal
.NumDrivers
) {
418 _eglUnlockMutex(_eglGlobal
.Mutex
);
422 loaded
= _eglPreloadUserDriver();
424 _eglUnlockMutex(_eglGlobal
.Mutex
);
430 * Unload preloaded drivers.
433 _eglUnloadDrivers(void)
437 /* this is called at atexit time */
438 for (i
= 0; i
< _eglGlobal
.NumDrivers
; i
++) {
439 _EGLDriver
*drv
= _eglGlobal
.Drivers
[i
];
440 lib_handle handle
= drv
->LibHandle
;
443 free((char *) drv
->Path
);
445 free((char *) drv
->Args
);
452 close_library(handle
);
453 _eglGlobal
.Drivers
[i
] = NULL
;
456 _eglGlobal
.NumDrivers
= 0;
460 _eglLoadDefaultDriver(EGLDisplay dpy
, EGLint
*major
, EGLint
*minor
)
462 _EGLDriver
*drv
= NULL
;
466 _eglLockMutex(_eglGlobal
.Mutex
);
468 for (i
= 0; i
< ARRAY_SIZE(DefaultDriverNames
); i
++) {
469 _eglPreloadForEach(_eglGetSearchPath(),
470 _eglLoaderFile
, (void *) DefaultDriverNames
[i
]);
471 if (_eglGlobal
.NumDrivers
== 0)
473 drv
= _eglGlobal
.Drivers
[0];
475 _eglUnlockMutex(_eglGlobal
.Mutex
);
476 ok
= drv
->API
.Initialize(drv
, dpy
, major
, minor
);
477 _eglLockMutex(_eglGlobal
.Mutex
);
484 _eglUnlockMutex(_eglGlobal
.Mutex
);
486 return _eglGlobal
.NumDrivers
> 0 ? drv
: NULL
;
491 * Plug all the available fallback routines into the given driver's
495 _eglInitDriverFallbacks(_EGLDriver
*drv
)
497 /* If a pointer is set to NULL, then the device driver _really_ has
500 drv
->API
.Initialize
= NULL
;
501 drv
->API
.Terminate
= NULL
;
503 drv
->API
.GetConfigs
= _eglGetConfigs
;
504 drv
->API
.ChooseConfig
= _eglChooseConfig
;
505 drv
->API
.GetConfigAttrib
= _eglGetConfigAttrib
;
507 drv
->API
.CreateContext
= _eglCreateContext
;
508 drv
->API
.DestroyContext
= _eglDestroyContext
;
509 drv
->API
.MakeCurrent
= _eglMakeCurrent
;
510 drv
->API
.QueryContext
= _eglQueryContext
;
512 drv
->API
.CreateWindowSurface
= _eglCreateWindowSurface
;
513 drv
->API
.CreatePixmapSurface
= _eglCreatePixmapSurface
;
514 drv
->API
.CreatePbufferSurface
= _eglCreatePbufferSurface
;
515 drv
->API
.DestroySurface
= _eglDestroySurface
;
516 drv
->API
.QuerySurface
= _eglQuerySurface
;
517 drv
->API
.SurfaceAttrib
= _eglSurfaceAttrib
;
518 drv
->API
.BindTexImage
= _eglBindTexImage
;
519 drv
->API
.ReleaseTexImage
= _eglReleaseTexImage
;
520 drv
->API
.SwapInterval
= _eglSwapInterval
;
521 drv
->API
.SwapBuffers
= _eglSwapBuffers
;
522 drv
->API
.CopyBuffers
= _eglCopyBuffers
;
524 drv
->API
.QueryString
= _eglQueryString
;
525 drv
->API
.WaitClient
= _eglWaitClient
;
526 drv
->API
.WaitNative
= _eglWaitNative
;
528 #ifdef EGL_MESA_screen_surface
529 drv
->API
.ChooseModeMESA
= _eglChooseModeMESA
;
530 drv
->API
.GetModesMESA
= _eglGetModesMESA
;
531 drv
->API
.GetModeAttribMESA
= _eglGetModeAttribMESA
;
532 drv
->API
.GetScreensMESA
= _eglGetScreensMESA
;
533 drv
->API
.CreateScreenSurfaceMESA
= _eglCreateScreenSurfaceMESA
;
534 drv
->API
.ShowScreenSurfaceMESA
= _eglShowScreenSurfaceMESA
;
535 drv
->API
.ScreenPositionMESA
= _eglScreenPositionMESA
;
536 drv
->API
.QueryScreenMESA
= _eglQueryScreenMESA
;
537 drv
->API
.QueryScreenSurfaceMESA
= _eglQueryScreenSurfaceMESA
;
538 drv
->API
.QueryScreenModeMESA
= _eglQueryScreenModeMESA
;
539 drv
->API
.QueryModeStringMESA
= _eglQueryModeStringMESA
;
540 #endif /* EGL_MESA_screen_surface */
542 #ifdef EGL_VERSION_1_2
543 drv
->API
.CreatePbufferFromClientBuffer
= _eglCreatePbufferFromClientBuffer
;
544 #endif /* EGL_VERSION_1_2 */
546 #ifdef EGL_KHR_image_base
547 drv
->API
.CreateImageKHR
= _eglCreateImageKHR
;
548 drv
->API
.DestroyImageKHR
= _eglDestroyImageKHR
;
549 #endif /* EGL_KHR_image_base */
554 * Invoke a callback function on each EGL search path.
556 * The first argument of the callback function is the name of the search path.
557 * The second argument is the length of the name.
560 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
563 const char *search_path
= _eglGetSearchPath();
564 _eglPreloadForEach(search_path
, callback
, callback_data
);
569 * Set the probe cache at the given key.
571 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
572 * by multiple drivers.
575 _eglSetProbeCache(EGLint key
, const void *val
)
579 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
580 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
584 assert(idx
< NUM_PROBE_CACHE_SLOTS
);
586 _eglProbeCache
.keys
[idx
] = key
;
587 _eglProbeCache
.values
[idx
] = val
;
592 * Return the probe cache at the given key.
595 _eglGetProbeCache(EGLint key
)
599 for (idx
= 0; idx
< NUM_PROBE_CACHE_SLOTS
; idx
++) {
600 if (!_eglProbeCache
.keys
[idx
] || _eglProbeCache
.keys
[idx
] == key
)
604 return (idx
< NUM_PROBE_CACHE_SLOTS
&& _eglProbeCache
.keys
[idx
] == key
) ?
605 _eglProbeCache
.values
[idx
] : NULL
;