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.
40 #include "eglstring.h"
41 #include "egldefines.h"
42 #include "egldisplay.h"
43 #include "egldriver.h"
47 #if defined(_EGL_OS_UNIX)
49 #include <sys/types.h>
55 typedef struct _egl_module
{
62 static _EGL_DECLARE_MUTEX(_eglModuleMutex
);
63 static _EGLArray
*_eglModules
;
68 } _eglBuiltInDrivers
[] = {
69 #ifdef _EGL_BUILT_IN_DRIVER_GALLIUM
70 { "egl_gallium", _eglBuiltInDriverGALLIUM
},
72 #ifdef _EGL_BUILT_IN_DRIVER_DRI2
73 { "egl_dri2", _eglBuiltInDriverDRI2
},
75 #ifdef _EGL_BUILT_IN_DRIVER_GLX
76 { "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_AROS)
112 typedef void * lib_handle
;
115 close_library(void *lib
)
120 #elif defined(_EGL_OS_UNIX)
123 typedef void * lib_handle
;
126 open_library(const char *filename
)
128 return dlopen(filename
, RTLD_LAZY
);
132 close_library(void *lib
)
149 * Open the named driver and find its bootstrap function: _eglMain().
152 _eglOpenLibrary(const char *driverPath
, lib_handle
*handle
)
154 #if !defined(_EGL_OS_AROS)
156 _EGLMain_t mainFunc
= NULL
;
157 const char *error
= "unknown error";
161 _eglLog(_EGL_DEBUG
, "dlopen(%s)", driverPath
);
162 lib
= open_library(driverPath
);
164 #if defined(_EGL_OS_WINDOWS)
167 mainFunc
= (_EGLMain_t
) GetProcAddress(lib
, "_eglMain");
168 #elif defined(_EGL_OS_UNIX)
174 /* direct cast gives a warning when compiled with -pedantic */
175 tmp
.ptr
= dlsym(lib
, "_eglMain");
186 _eglLog(_EGL_WARNING
, "Could not open driver %s (%s)",
192 _eglLog(_EGL_WARNING
, "_eglMain not found in %s (%s)",
208 * Load a module and create the driver object.
211 _eglLoadModule(_EGLModule
*mod
)
221 lib
= (lib_handle
) NULL
;
222 mainFunc
= mod
->BuiltIn
;
225 mainFunc
= _eglOpenLibrary(mod
->Path
, &lib
);
230 drv
= mainFunc(NULL
);
238 _eglLog(_EGL_WARNING
, "Driver loaded from %s has no name", mod
->Path
);
239 drv
->Name
= "UNNAMED";
242 mod
->Handle
= (void *) lib
;
253 _eglUnloadModule(_EGLModule
*mod
)
255 #if defined(_EGL_OS_UNIX)
256 /* destroy the driver */
257 if (mod
->Driver
&& mod
->Driver
->Unload
)
258 mod
->Driver
->Unload(mod
->Driver
);
261 * XXX At this point (atexit), the module might be the last reference to
262 * libEGL. Closing the module might unmap libEGL and give problems.
266 close_library(mod
->Handle
);
268 #elif defined(_EGL_OS_WINDOWS)
269 /* XXX Windows unloads DLLs before atexit */
278 * Add a module to the module array.
281 _eglAddModule(const char *path
)
287 _eglModules
= _eglCreateArray("Module", 8);
292 /* find duplicates */
293 for (i
= 0; i
< _eglModules
->Size
; i
++) {
294 mod
= _eglModules
->Elements
[i
];
295 if (strcmp(mod
->Path
, path
) == 0)
299 /* allocate a new one */
300 mod
= calloc(1, sizeof(*mod
));
302 mod
->Path
= _eglstrdup(path
);
309 _eglAppendArray(_eglModules
, (void *) mod
);
310 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Path
);
321 _eglFreeModule(void *module
)
323 _EGLModule
*mod
= (_EGLModule
*) module
;
325 _eglUnloadModule(mod
);
331 #if !defined(_EGL_OS_AROS)
333 * A loader function for use with _eglPreloadForEach. The loader data is the
334 * filename of the driver. This function stops on the first valid driver.
337 _eglLoaderFile(const char *dir
, size_t len
, void *loader_data
)
340 const char *filename
= (const char *) loader_data
;
341 size_t flen
= strlen(filename
);
343 /* make a full path */
344 if (len
+ flen
+ 2 > sizeof(path
))
347 memcpy(path
, dir
, len
);
350 memcpy(path
+ len
, filename
, flen
);
354 if (library_suffix()) {
355 const char *suffix
= library_suffix();
356 size_t slen
= strlen(suffix
);
358 EGLBoolean need_suffix
;
360 p
= filename
+ flen
- slen
;
361 need_suffix
= (p
< filename
|| strcmp(p
, suffix
) != 0);
364 if (len
+ slen
+ 1 > sizeof(path
))
366 strcpy(path
+ len
, suffix
);
370 #if defined(_EGL_OS_UNIX)
371 /* check if the file exists */
372 if (access(path
, F_OK
))
383 * Run the callback function on each driver directory.
385 * The process may end prematurely if the callback function returns false.
388 _eglPreloadForEach(const char *search_path
,
389 EGLBoolean (*loader
)(const char *, size_t, void *),
392 const char *cur
, *next
;
397 next
= strchr(cur
, ':');
398 len
= (next
) ? next
- cur
: strlen(cur
);
400 if (!loader(cur
, len
, loader_data
))
403 cur
= (next
) ? next
+ 1 : NULL
;
409 * Return a list of colon-separated driver directories.
412 _eglGetSearchPath(void)
414 static char search_path
[1024];
416 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
417 if (search_path
[0] == '\0') {
418 char *buf
= search_path
;
419 size_t len
= sizeof(search_path
);
424 #if defined(_EGL_OS_UNIX)
425 use_env
= (geteuid() == getuid() && getegid() == getgid());
435 /* extract the dirname from EGL_DRIVER */
436 p
= getenv("EGL_DRIVER");
437 if (p
&& strchr(p
, dir_sep
)) {
438 ret
= _eglsnprintf(buf
, len
, "%s", p
);
439 if (ret
> 0 && ret
< len
) {
440 p
= strrchr(buf
, dir_sep
);
448 /* append EGL_DRIVERS_PATH */
449 p
= getenv("EGL_DRIVERS_PATH");
451 ret
= _eglsnprintf(buf
, len
, "%s:", p
);
452 if (ret
> 0 && ret
< len
) {
460 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
463 ret
= _eglsnprintf(buf
, len
, "%s", _EGL_DRIVER_SEARCH_DIR
);
464 if (ret
< 0 || ret
>= len
)
465 search_path
[0] = '\0';
467 _eglLog(_EGL_DEBUG
, "EGL search path is %s", search_path
);
469 #endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
476 * Add the user driver to the module array.
478 * The user driver is specified by EGL_DRIVER.
481 _eglAddUserDriver(void)
483 const char *search_path
= _eglGetSearchPath();
487 env
= getenv("EGL_DRIVER");
488 #if defined(_EGL_OS_UNIX)
489 if (env
&& strchr(env
, '/')) {
491 if ((geteuid() != getuid() || getegid() != getgid())) {
493 "ignore EGL_DRIVER for setuid/setgid binaries");
498 char *suffix
= strchr(env
, '.');
499 name_len
= (suffix
) ? suffix
- env
: strlen(env
);
503 name_len
= strlen(env
);
504 #endif /* _EGL_OS_UNIX */
507 * Try built-in drivers first if we know the driver name. This makes sure
508 * we do not load the outdated external driver that is still on the
515 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
516 if (strlen(_eglBuiltInDrivers
[i
].name
) == name_len
&&
517 !strncmp(_eglBuiltInDrivers
[i
].name
, env
, name_len
)) {
518 mod
= _eglAddModule(env
);
520 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
527 /* otherwise, treat env as a path */
529 _eglPreloadForEach(search_path
, _eglLoaderFile
, (void *) env
);
539 * Add egl_gallium to the module array.
542 _eglAddGalliumDriver(void)
544 #ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
545 void *external
= (void *) "egl_gallium";
546 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile
, external
);
549 #endif /* !defined(_EGL_OS_AROS) */
552 * Add built-in drivers to the module array.
555 _eglAddBuiltInDrivers(void)
560 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
561 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
563 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
569 * Add drivers to the module array. Drivers will be loaded as they are matched
578 #if !defined(_EGL_OS_AROS)
579 if (!_eglAddUserDriver()) {
581 * Add other drivers only when EGL_DRIVER is not set. The order here
582 * decides the priorities.
584 _eglAddGalliumDriver();
585 _eglAddBuiltInDrivers();
588 _eglAddBuiltInDrivers();
591 return (_eglModules
!= NULL
);
596 * A helper function for _eglMatchDriver. It finds the first driver that can
597 * initialize the display and return.
600 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
602 _EGLDriver
*drv
= NULL
;
605 if (!_eglAddDrivers()) {
606 _eglLog(_EGL_WARNING
, "failed to find any driver");
612 /* no re-matching? */
613 if (!drv
->API
.Initialize(drv
, dpy
))
618 while (i
< _eglModules
->Size
) {
619 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
621 if (!_eglLoadModule(mod
)) {
622 /* remove invalid modules */
623 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
627 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
641 * Match a display to a driver. The display is initialized unless test_only is
642 * true. The matching is done by finding the first driver that can initialize
646 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
648 _EGLDriver
*best_drv
;
650 assert(!dpy
->Initialized
);
652 _eglLockMutex(&_eglModuleMutex
);
655 dpy
->Options
.TestOnly
= test_only
;
656 dpy
->Options
.UseFallback
= EGL_FALSE
;
658 best_drv
= _eglMatchAndInitialize(dpy
);
660 dpy
->Options
.UseFallback
= EGL_TRUE
;
661 best_drv
= _eglMatchAndInitialize(dpy
);
664 _eglUnlockMutex(&_eglModuleMutex
);
667 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
668 best_drv
->Name
, (test_only
) ? " (test only) " : "");
670 dpy
->Driver
= best_drv
;
671 dpy
->Initialized
= EGL_TRUE
;
679 __eglMustCastToProperFunctionPointerType
680 _eglGetDriverProc(const char *procname
)
683 _EGLProc proc
= NULL
;
686 /* load the driver for the default display */
687 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
688 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
689 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
693 for (i
= 0; i
< _eglModules
->Size
; i
++) {
694 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
698 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
708 * Unload all drivers.
711 _eglUnloadDrivers(void)
713 /* this is called at atexit time */
715 _eglDestroyArray(_eglModules
, _eglFreeModule
);
720 #if !defined(_EGL_OS_AROS)
722 * Invoke a callback function on each EGL search path.
724 * The first argument of the callback function is the name of the search path.
725 * The second argument is the length of the name.
728 _eglSearchPathForEach(EGLBoolean (*callback
)(const char *, size_t, void *),
731 const char *search_path
= _eglGetSearchPath();
732 _eglPreloadForEach(search_path
, callback
, callback_data
);