1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 _EGLMutex _eglModuleMutex
= _EGL_MUTEX_INITIALIZER
;
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
},
80 * Wrappers for dlopen/dlclose()
82 #if defined(_EGL_OS_WINDOWS)
85 typedef HMODULE lib_handle
;
88 open_library(const char *filename
)
90 return LoadLibrary(filename
);
94 close_library(HMODULE lib
)
107 #elif defined(_EGL_OS_UNIX)
110 typedef void * lib_handle
;
113 open_library(const char *filename
)
115 return dlopen(filename
, RTLD_LAZY
);
119 close_library(void *lib
)
136 * Open the named driver and find its bootstrap function: _eglMain().
139 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
142 _EGLMain_t mainFunc
= NULL
;
143 const char *error
= "unknown error";
147 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
148 lib
= open_library(driverPath
);
150 #if defined(_EGL_OS_WINDOWS)
153 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
154 #elif defined(_EGL_OS_UNIX)
160 /* direct cast gives a warning when compiled with -pedantic */
161 tmp
.ptr
= dlsym(lib
, "_eglMain");
172 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
178 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
191 * Load a module and create the driver object.
194 _eglLoadModule(_EGLModule
*mod
)
204 lib
= (lib_handle
) NULL
;
205 mainFunc
= mod
->BuiltIn
;
208 mainFunc
= _eglOpenLibrary(mod
->Path
, &lib
);
213 drv
= mainFunc(NULL
);
221 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", mod
->Path
);
222 drv
->Name
= "UNNAMED";
225 mod
->Handle
= (void *) lib
;
236 _eglUnloadModule(_EGLModule
*mod
)
238 #if defined(_EGL_OS_UNIX)
239 /* destroy the driver */
240 if (mod
->Driver
&& mod
->Driver
->Unload
)
241 mod
->Driver
->Unload(mod
->Driver
);
244 * XXX At this point (atexit), the module might be the last reference to
245 * libEGL. Closing the module might unmap libEGL and give problems.
249 close_library(mod
->Handle
);
251 #elif defined(_EGL_OS_WINDOWS)
252 /* XXX Windows unloads DLLs before atexit */
261 * Add a module to the module array.
264 _eglAddModule(const char *path
)
270 _eglModules
= _eglCreateArray("Module", 8);
275 /* find duplicates */
276 for (i
= 0; i
< _eglModules
->Size
; i
++) {
277 mod
= _eglModules
->Elements
[i
];
278 if (strcmp(mod
->Path
, path
) == 0)
282 /* allocate a new one */
283 mod
= calloc(1, sizeof(*mod
));
285 mod
->Path
= _eglstrdup(path
);
292 _eglAppendArray(_eglModules
, (void *) mod
);
293 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Path
);
304 _eglFreeModule(void *module
)
306 _EGLModule
*mod
= (_EGLModule
*) module
;
308 _eglUnloadModule(mod
);
315 * A loader function for use with _eglPreloadForEach. The loader data is the
316 * filename of the driver. This function stops on the first valid driver.
319 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
322 const char *filename
= (const char *) loader_data
;
323 size_t flen
= strlen(filename
);
325 /* make a full path */
326 if (len
+ flen
+ 2 > sizeof(path
))
329 memcpy(path
, dir
, len
);
332 memcpy(path
+ len
, filename
, flen
);
336 if (library_suffix()) {
337 const char *suffix
= library_suffix();
338 size_t slen
= strlen(suffix
);
340 EGLBoolean need_suffix
;
342 p
= filename
+ flen
- slen
;
343 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
346 if (len
+ slen
+ 1 > sizeof(path
))
348 strcpy(path
+ len
, suffix
);
352 #if defined(_EGL_OS_UNIX)
353 /* check if the file exists */
354 if (access(path
, F_OK
))
365 * Run the callback function on each driver directory.
367 * The process may end prematurely if the callback function returns false.
370 _eglPreloadForEach(const char *search_path
,
371 EGLBoolean (*loader
)(const char *, size_t, void *),
374 const char *cur
, *next
;
379 next
= strchr(cur
, ':');
380 len
= (next
) ? next
- cur
: strlen(cur
);
382 if (!loader(cur
, len
, loader_data
))
385 cur
= (next
) ? next
+ 1 : NULL
;
391 * Return a list of colon-separated driver directories.
394 _eglGetSearchPath(void)
396 static char search_path
[1024];
398 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
399 if (search_path
[0] == '\0') {
400 char *buf
= search_path
;
401 size_t len
= sizeof(search_path
);
406 #if defined(_EGL_OS_UNIX)
407 use_env
= (geteuid() == getuid() && getegid() == getgid());
417 /* extract the dirname from EGL_DRIVER */
418 p
= getenv("EGL_DRIVER");
419 if (p
&& strchr(p
, dir_sep
)) {
420 ret
= _eglsnprintf(buf
, len
, "%s", p
);
421 if (ret
> 0 && ret
< len
) {
422 p
= strrchr(buf
, dir_sep
);
430 /* append EGL_DRIVERS_PATH */
431 p
= getenv("EGL_DRIVERS_PATH");
433 ret
= _eglsnprintf(buf
, len
, "%s:", p
);
434 if (ret
> 0 && ret
< len
) {
442 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
445 ret
= _eglsnprintf(buf
, len
, "%s", _EGL_DRIVER_SEARCH_DIR
);
446 if (ret
< 0 || ret
>= len
)
447 search_path
[0] = '\0';
449 _eglLog(_EGL_DEBUG
, "EGL search path is %s", search_path
);
451 #endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
458 * Add the user driver to the module array.
460 * The user driver is specified by EGL_DRIVER.
463 _eglAddUserDriver(void)
465 const char *search_path
= _eglGetSearchPath();
469 env
= getenv("EGL_DRIVER");
470 #if defined(_EGL_OS_UNIX)
471 if (env
&& strchr(env
, '/')) {
473 if ((geteuid() != getuid() || getegid() != getgid())) {
475 "ignore EGL_DRIVER for setuid/setgid binaries");
480 char *suffix
= strchr(env
, '.');
481 name_len
= (suffix
) ? suffix
- env
: strlen(env
);
485 name_len
= strlen(env
);
486 #endif /* _EGL_OS_UNIX */
489 * Try built-in drivers first if we know the driver name. This makes sure
490 * we do not load the outdated external driver that is still on the
497 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
498 if (strlen(_eglBuiltInDrivers
[i
].name
) == name_len
&&
499 !strncmp(_eglBuiltInDrivers
[i
].name
, env
, name_len
)) {
500 mod
= _eglAddModule(env
);
502 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
509 /* otherwise, treat env as a path */
511 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
521 * Add egl_gallium to the module array.
524 _eglAddGalliumDriver(void)
526 #ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
527 void *external
= (void *) "egl_gallium";
528 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile
, external
);
534 * Add built-in drivers to the module array.
537 _eglAddBuiltInDrivers(void)
542 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
543 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
545 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
551 * Add drivers to the module array. Drivers will be loaded as they are matched
560 if (!_eglAddUserDriver()) {
562 * Add other drivers only when EGL_DRIVER is not set. The order here
563 * decides the priorities.
565 _eglAddGalliumDriver();
566 _eglAddBuiltInDrivers();
569 return (_eglModules
!= NULL
);
574 * A helper function for _eglMatchDriver. It finds the first driver that can
575 * initialize the display and return.
578 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
580 _EGLDriver
*drv
= NULL
;
583 if (!_eglAddDrivers()) {
584 _eglLog(_EGL_WARNING
, "failed to find any driver");
590 /* no re-matching? */
591 if (!drv
->API
.Initialize(drv
, dpy
))
596 while (i
< _eglModules
->Size
) {
597 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
599 if (!_eglLoadModule(mod
)) {
600 /* remove invalid modules */
601 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
605 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
619 * Match a display to a driver. The display is initialized unless test_only is
620 * true. The matching is done by finding the first driver that can initialize
624 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
626 _EGLDriver
*best_drv
;
628 assert(!dpy
->Initialized
);
630 _eglLockMutex(&_eglModuleMutex
);
633 dpy
->Options
.TestOnly
= test_only
;
634 dpy
->Options
.UseFallback
= EGL_FALSE
;
636 best_drv
= _eglMatchAndInitialize(dpy
);
638 dpy
->Options
.UseFallback
= EGL_TRUE
;
639 best_drv
= _eglMatchAndInitialize(dpy
);
642 _eglUnlockMutex(&_eglModuleMutex
);
645 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
646 best_drv
->Name
, (test_only
) ? " (test only) " : "");
648 dpy
->Driver
= best_drv
;
649 dpy
->Initialized
= EGL_TRUE
;
657 __eglMustCastToProperFunctionPointerType
658 _eglGetDriverProc(const char *procname
)
661 _EGLProc proc
= NULL
;
664 /* load the driver for the default display */
665 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
666 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
667 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
671 for (i
= 0; i
< _eglModules
->Size
; i
++) {
672 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
676 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
686 * Unload all drivers.
689 _eglUnloadDrivers(void)
691 /* this is called at atexit time */
693 _eglDestroyArray(_eglModules
, _eglFreeModule
);
700 * Invoke a callback function on each EGL search path.
702 * The first argument of the callback function is the name of the search path.
703 * The second argument is the length of the name.
706 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
709 const char *search_path
= _eglGetSearchPath();
710 _eglPreloadForEach(search_path
, callback
, callback_data
);