1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Functions for choosing and opening/loading device drivers.
41 #include "eglstring.h"
42 #include "egldefines.h"
43 #include "egldisplay.h"
44 #include "egldriver.h"
48 #if defined(_EGL_OS_UNIX)
50 #include <sys/types.h>
56 typedef struct _egl_module {
63 static _EGL_DECLARE_MUTEX(_eglModuleMutex);
64 static _EGLArray *_eglModules;
69 } _eglBuiltInDrivers[] = {
70 #ifdef _EGL_BUILT_IN_DRIVER_GALLIUM
71 { "egl_gallium", _eglBuiltInDriverGALLIUM },
73 #ifdef _EGL_BUILT_IN_DRIVER_DRI2
74 { "egl_dri2", _eglBuiltInDriverDRI2 },
76 #ifdef _EGL_BUILT_IN_DRIVER_GLX
77 { "egl_glx", _eglBuiltInDriverGLX },
83 * Wrappers for dlopen/dlclose()
85 #if defined(_EGL_OS_WINDOWS)
88 typedef HMODULE lib_handle;
91 open_library(const char *filename)
93 return LoadLibrary(filename);
97 close_library(HMODULE lib)
110 #elif defined(_EGL_OS_UNIX)
113 typedef void * lib_handle;
116 open_library(const char *filename)
118 return dlopen(filename, RTLD_LAZY);
122 close_library(void *lib)
139 * Open the named driver and find its bootstrap function: _eglMain().
142 _eglOpenLibrary(const char *driverPath, lib_handle *handle)
145 _EGLMain_t mainFunc = NULL;
146 const char *error = "unknown error";
150 _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
151 lib = open_library(driverPath);
153 #if defined(_EGL_OS_WINDOWS)
156 mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
157 #elif defined(_EGL_OS_UNIX)
163 /* direct cast gives a warning when compiled with -pedantic */
164 tmp.ptr = dlsym(lib, "_eglMain");
175 _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
181 _eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
194 * Load a module and create the driver object.
197 _eglLoadModule(_EGLModule *mod)
207 lib = (lib_handle) NULL;
208 mainFunc = mod->BuiltIn;
211 mainFunc = _eglOpenLibrary(mod->Path, &lib);
216 drv = mainFunc(NULL);
224 _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", mod->Path);
225 drv->Name = "UNNAMED";
228 mod->Handle = (void *) lib;
239 _eglUnloadModule(_EGLModule *mod)
241 #if defined(_EGL_OS_UNIX)
242 /* destroy the driver */
243 if (mod->Driver && mod->Driver->Unload)
244 mod->Driver->Unload(mod->Driver);
247 * XXX At this point (atexit), the module might be the last reference to
248 * libEGL. Closing the module might unmap libEGL and give problems.
252 close_library(mod->Handle);
254 #elif defined(_EGL_OS_WINDOWS)
255 /* XXX Windows unloads DLLs before atexit */
264 * Add a module to the module array.
267 _eglAddModule(const char *path)
273 _eglModules = _eglCreateArray("Module", 8);
278 /* find duplicates */
279 for (i = 0; i < _eglModules->Size; i++) {
280 mod = _eglModules->Elements[i];
281 if (strcmp(mod->Path, path) == 0)
285 /* allocate a new one */
286 mod = calloc(1, sizeof(*mod));
288 mod->Path = _eglstrdup(path);
295 _eglAppendArray(_eglModules, (void *) mod);
296 _eglLog(_EGL_DEBUG, "added %s to module array", mod->Path);
307 _eglFreeModule(void *module)
309 _EGLModule *mod = (_EGLModule *) module;
311 _eglUnloadModule(mod);
318 * A loader function for use with _eglPreloadForEach. The loader data is the
319 * filename of the driver. This function stops on the first valid driver.
322 _eglLoaderFile(const char *dir, size_t len, void *loader_data)
325 const char *filename = (const char *) loader_data;
326 size_t flen = strlen(filename);
328 /* make a full path */
329 if (len + flen + 2 > sizeof(path))
332 memcpy(path, dir, len);
335 memcpy(path + len, filename, flen);
339 if (library_suffix()) {
340 const char *suffix = library_suffix();
341 size_t slen = strlen(suffix);
343 EGLBoolean need_suffix;
345 p = filename + flen - slen;
346 need_suffix = (p < filename || strcmp(p, suffix) != 0);
349 if (len + slen + 1 > sizeof(path))
351 strcpy(path + len, suffix);
355 #if defined(_EGL_OS_UNIX)
356 /* check if the file exists */
357 if (access(path, F_OK))
368 * Run the callback function on each driver directory.
370 * The process may end prematurely if the callback function returns false.
373 _eglPreloadForEach(const char *search_path,
374 EGLBoolean (*loader)(const char *, size_t, void *),
377 const char *cur, *next;
382 next = strchr(cur, ':');
383 len = (next) ? next - cur : strlen(cur);
385 if (!loader(cur, len, loader_data))
388 cur = (next) ? next + 1 : NULL;
394 * Return a list of colon-separated driver directories.
397 _eglGetSearchPath(void)
399 static char search_path[1024];
401 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
402 if (search_path[0] == '\0') {
403 char *buf = search_path;
404 size_t len = sizeof(search_path);
409 #if defined(_EGL_OS_UNIX)
410 use_env = (geteuid() == getuid() && getegid() == getgid());
420 /* extract the dirname from EGL_DRIVER */
421 p = getenv("EGL_DRIVER");
422 if (p && strchr(p, dir_sep)) {
423 ret = _eglsnprintf(buf, len, "%s", p);
424 if (ret > 0 && ret < len) {
425 p = strrchr(buf, dir_sep);
433 /* append EGL_DRIVERS_PATH */
434 p = getenv("EGL_DRIVERS_PATH");
436 ret = _eglsnprintf(buf, len, "%s:", p);
437 if (ret > 0 && ret < len) {
445 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
448 ret = _eglsnprintf(buf, len, "%s", _EGL_DRIVER_SEARCH_DIR);
449 if (ret < 0 || ret >= len)
450 search_path[0] = '\0';
452 _eglLog(_EGL_DEBUG, "EGL search path is %s", search_path);
454 #endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
461 * Add the user driver to the module array.
463 * The user driver is specified by EGL_DRIVER.
466 _eglAddUserDriver(void)
468 const char *search_path = _eglGetSearchPath();
472 env = getenv("EGL_DRIVER");
473 #if defined(_EGL_OS_UNIX)
474 if (env && strchr(env, '/')) {
476 if ((geteuid() != getuid() || getegid() != getgid())) {
478 "ignore EGL_DRIVER for setuid/setgid binaries");
483 char *suffix = strchr(env, '.');
484 name_len = (suffix) ? suffix - env : strlen(env);
488 name_len = strlen(env);
489 #endif /* _EGL_OS_UNIX */
492 * Try built-in drivers first if we know the driver name. This makes sure
493 * we do not load the outdated external driver that is still on the
500 for (i = 0; _eglBuiltInDrivers[i].name; i++) {
501 if (strlen(_eglBuiltInDrivers[i].name) == name_len &&
502 !strncmp(_eglBuiltInDrivers[i].name, env, name_len)) {
503 mod = _eglAddModule(env);
505 mod->BuiltIn = _eglBuiltInDrivers[i].main;
512 /* otherwise, treat env as a path */
514 _eglPreloadForEach(search_path, _eglLoaderFile, (void *) env);
524 * Add egl_gallium to the module array.
527 _eglAddGalliumDriver(void)
529 #ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
530 void *external = (void *) "egl_gallium";
531 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile, external);
537 * Add built-in drivers to the module array.
540 _eglAddBuiltInDrivers(void)
545 for (i = 0; _eglBuiltInDrivers[i].name; i++) {
546 mod = _eglAddModule(_eglBuiltInDrivers[i].name);
548 mod->BuiltIn = _eglBuiltInDrivers[i].main;
554 * Add drivers to the module array. Drivers will be loaded as they are matched
563 if (!_eglAddUserDriver()) {
565 * Add other drivers only when EGL_DRIVER is not set. The order here
566 * decides the priorities.
568 _eglAddGalliumDriver();
569 _eglAddBuiltInDrivers();
572 return (_eglModules != NULL);
577 * A helper function for _eglMatchDriver. It finds the first driver that can
578 * initialize the display and return.
581 _eglMatchAndInitialize(_EGLDisplay *dpy)
583 _EGLDriver *drv = NULL;
586 if (!_eglAddDrivers()) {
587 _eglLog(_EGL_WARNING, "failed to find any driver");
593 /* no re-matching? */
594 if (!drv->API.Initialize(drv, dpy))
599 while (i < _eglModules->Size) {
600 _EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
602 if (!_eglLoadModule(mod)) {
603 /* remove invalid modules */
604 _eglEraseArray(_eglModules, i, _eglFreeModule);
608 if (mod->Driver->API.Initialize(mod->Driver, dpy)) {
622 * Match a display to a driver. The display is initialized unless test_only is
623 * true. The matching is done by finding the first driver that can initialize
627 _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
629 _EGLDriver *best_drv;
631 assert(!dpy->Initialized);
633 _eglLockMutex(&_eglModuleMutex);
636 dpy->Options.TestOnly = test_only;
637 dpy->Options.UseFallback = EGL_FALSE;
639 best_drv = _eglMatchAndInitialize(dpy);
641 dpy->Options.UseFallback = EGL_TRUE;
642 best_drv = _eglMatchAndInitialize(dpy);
645 _eglUnlockMutex(&_eglModuleMutex);
648 _eglLog(_EGL_DEBUG, "the best driver is %s%s",
649 best_drv->Name, (test_only) ? " (test only) " : "");
651 dpy->Driver = best_drv;
652 dpy->Initialized = EGL_TRUE;
660 __eglMustCastToProperFunctionPointerType
661 _eglGetDriverProc(const char *procname)
664 _EGLProc proc = NULL;
667 /* load the driver for the default display */
668 EGLDisplay egldpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
669 _EGLDisplay *dpy = _eglLookupDisplay(egldpy);
670 if (!dpy || !_eglMatchDriver(dpy, EGL_TRUE))
674 for (i = 0; i < _eglModules->Size; i++) {
675 _EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
679 proc = mod->Driver->API.GetProcAddress(mod->Driver, procname);
689 * Unload all drivers.
692 _eglUnloadDrivers(void)
694 /* this is called at atexit time */
696 _eglDestroyArray(_eglModules, _eglFreeModule);
703 * Invoke a callback function on each EGL search path.
705 * The first argument of the callback function is the name of the search path.
706 * The second argument is the length of the name.
709 _eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
712 const char *search_path = _eglGetSearchPath();
713 _eglPreloadForEach(search_path, callback, callback_data);