Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / mesa / src / egl / main / egldriver.c
blob8a01459929654494259744621432c2f5f9be3b0f
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.
6 * All Rights Reserved.
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
18 * of the Software.
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 **************************************************************************/
31 /**
32 * Functions for choosing and opening/loading device drivers.
35 #include <assert.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 #include "eglstring.h"
41 #include "egldefines.h"
42 #include "egldisplay.h"
43 #include "egldriver.h"
44 #include "egllog.h"
45 #include "eglmutex.h"
47 #if defined(_EGL_OS_UNIX)
48 #include <dlfcn.h>
49 #include <sys/types.h>
50 #include <dirent.h>
51 #include <unistd.h>
52 #endif
55 typedef struct _egl_module {
56 char *Path;
57 _EGLMain_t BuiltIn;
58 void *Handle;
59 _EGLDriver *Driver;
60 } _EGLModule;
62 static _EGL_DECLARE_MUTEX(_eglModuleMutex);
63 static _EGLArray *_eglModules;
65 const struct {
66 const char *name;
67 _EGLMain_t main;
68 } _eglBuiltInDrivers[] = {
69 #ifdef _EGL_BUILT_IN_DRIVER_GALLIUM
70 { "egl_gallium", _eglBuiltInDriverGALLIUM },
71 #endif
72 #ifdef _EGL_BUILT_IN_DRIVER_DRI2
73 { "egl_dri2", _eglBuiltInDriverDRI2 },
74 #endif
75 #ifdef _EGL_BUILT_IN_DRIVER_GLX
76 { "egl_glx", _eglBuiltInDriverGLX },
77 #endif
78 { NULL, NULL }
82 /**
83 * Wrappers for dlopen/dlclose()
85 #if defined(_EGL_OS_WINDOWS)
88 typedef HMODULE lib_handle;
90 static HMODULE
91 open_library(const char *filename)
93 return LoadLibrary(filename);
96 static void
97 close_library(HMODULE lib)
99 FreeLibrary(lib);
103 static const char *
104 library_suffix(void)
106 return ".dll";
110 #elif defined(_EGL_OS_AROS)
112 typedef void * lib_handle;
114 static void
115 close_library(void *lib)
117 /* No-Op */
120 #elif defined(_EGL_OS_UNIX)
123 typedef void * lib_handle;
125 static void *
126 open_library(const char *filename)
128 return dlopen(filename, RTLD_LAZY);
131 static void
132 close_library(void *lib)
134 dlclose(lib);
138 static const char *
139 library_suffix(void)
141 return ".so";
145 #endif
149 * Open the named driver and find its bootstrap function: _eglMain().
151 static _EGLMain_t
152 _eglOpenLibrary(const char *driverPath, lib_handle *handle)
154 #if !defined(_EGL_OS_AROS)
155 lib_handle lib;
156 _EGLMain_t mainFunc = NULL;
157 const char *error = "unknown error";
159 assert(driverPath);
161 _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
162 lib = open_library(driverPath);
164 #if defined(_EGL_OS_WINDOWS)
165 /* XXX untested */
166 if (lib)
167 mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
168 #elif defined(_EGL_OS_UNIX)
169 if (lib) {
170 union {
171 _EGLMain_t func;
172 void *ptr;
173 } tmp = { NULL };
174 /* direct cast gives a warning when compiled with -pedantic */
175 tmp.ptr = dlsym(lib, "_eglMain");
176 mainFunc = tmp.func;
177 if (!mainFunc)
178 error = dlerror();
180 else {
181 error = dlerror();
183 #endif
185 if (!lib) {
186 _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
187 driverPath, error);
188 return NULL;
191 if (!mainFunc) {
192 _eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
193 driverPath, error);
194 if (lib)
195 close_library(lib);
196 return NULL;
199 *handle = lib;
200 return mainFunc;
201 #else
202 return NULL;
203 #endif
208 * Load a module and create the driver object.
210 static EGLBoolean
211 _eglLoadModule(_EGLModule *mod)
213 _EGLMain_t mainFunc;
214 lib_handle lib;
215 _EGLDriver *drv;
217 if (mod->Driver)
218 return EGL_TRUE;
220 if (mod->BuiltIn) {
221 lib = (lib_handle) NULL;
222 mainFunc = mod->BuiltIn;
224 else {
225 mainFunc = _eglOpenLibrary(mod->Path, &lib);
226 if (!mainFunc)
227 return EGL_FALSE;
230 drv = mainFunc(NULL);
231 if (!drv) {
232 if (lib)
233 close_library(lib);
234 return EGL_FALSE;
237 if (!drv->Name) {
238 _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", mod->Path);
239 drv->Name = "UNNAMED";
242 mod->Handle = (void *) lib;
243 mod->Driver = drv;
245 return EGL_TRUE;
250 * Unload a module.
252 static void
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.
264 #if 0
265 if (mod->Handle)
266 close_library(mod->Handle);
267 #endif
268 #elif defined(_EGL_OS_WINDOWS)
269 /* XXX Windows unloads DLLs before atexit */
270 #endif
272 mod->Driver = NULL;
273 mod->Handle = NULL;
278 * Add a module to the module array.
280 static _EGLModule *
281 _eglAddModule(const char *path)
283 _EGLModule *mod;
284 EGLint i;
286 if (!_eglModules) {
287 _eglModules = _eglCreateArray("Module", 8);
288 if (!_eglModules)
289 return NULL;
292 /* find duplicates */
293 for (i = 0; i < _eglModules->Size; i++) {
294 mod = _eglModules->Elements[i];
295 if (strcmp(mod->Path, path) == 0)
296 return mod;
299 /* allocate a new one */
300 mod = calloc(1, sizeof(*mod));
301 if (mod) {
302 mod->Path = _eglstrdup(path);
303 if (!mod->Path) {
304 free(mod);
305 mod = NULL;
308 if (mod) {
309 _eglAppendArray(_eglModules, (void *) mod);
310 _eglLog(_EGL_DEBUG, "added %s to module array", mod->Path);
313 return mod;
318 * Free a module.
320 static void
321 _eglFreeModule(void *module)
323 _EGLModule *mod = (_EGLModule *) module;
325 _eglUnloadModule(mod);
326 free(mod->Path);
327 free(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.
336 static EGLBoolean
337 _eglLoaderFile(const char *dir, size_t len, void *loader_data)
339 char path[1024];
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))
345 return EGL_TRUE;
346 if (len) {
347 memcpy(path, dir, len);
348 path[len++] = '/';
350 memcpy(path + len, filename, flen);
351 len += flen;
352 path[len] = '\0';
354 if (library_suffix()) {
355 const char *suffix = library_suffix();
356 size_t slen = strlen(suffix);
357 const char *p;
358 EGLBoolean need_suffix;
360 p = filename + flen - slen;
361 need_suffix = (p < filename || strcmp(p, suffix) != 0);
362 if (need_suffix) {
363 /* overflow */
364 if (len + slen + 1 > sizeof(path))
365 return EGL_TRUE;
366 strcpy(path + len, suffix);
370 #if defined(_EGL_OS_UNIX)
371 /* check if the file exists */
372 if (access(path, F_OK))
373 return EGL_TRUE;
374 #endif
376 _eglAddModule(path);
378 return EGL_TRUE;
383 * Run the callback function on each driver directory.
385 * The process may end prematurely if the callback function returns false.
387 static void
388 _eglPreloadForEach(const char *search_path,
389 EGLBoolean (*loader)(const char *, size_t, void *),
390 void *loader_data)
392 const char *cur, *next;
393 size_t len;
395 cur = search_path;
396 while (cur) {
397 next = strchr(cur, ':');
398 len = (next) ? next - cur : strlen(cur);
400 if (!loader(cur, len, loader_data))
401 break;
403 cur = (next) ? next + 1 : NULL;
409 * Return a list of colon-separated driver directories.
411 static const char *
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);
420 EGLBoolean use_env;
421 char dir_sep;
422 int ret;
424 #if defined(_EGL_OS_UNIX)
425 use_env = (geteuid() == getuid() && getegid() == getgid());
426 dir_sep = '/';
427 #else
428 use_env = EGL_TRUE;
429 dir_sep = '\\';
430 #endif
432 if (use_env) {
433 char *p;
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);
441 *p++ = ':';
443 len -= p - buf;
444 buf = p;
448 /* append EGL_DRIVERS_PATH */
449 p = getenv("EGL_DRIVERS_PATH");
450 if (p) {
451 ret = _eglsnprintf(buf, len, "%s:", p);
452 if (ret > 0 && ret < len) {
453 buf += ret;
454 len -= ret;
458 else {
459 _eglLog(_EGL_DEBUG,
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) */
471 return search_path;
476 * Add the user driver to the module array.
478 * The user driver is specified by EGL_DRIVER.
480 static EGLBoolean
481 _eglAddUserDriver(void)
483 const char *search_path = _eglGetSearchPath();
484 char *env;
485 size_t name_len = 0;
487 env = getenv("EGL_DRIVER");
488 #if defined(_EGL_OS_UNIX)
489 if (env && strchr(env, '/')) {
490 search_path = "";
491 if ((geteuid() != getuid() || getegid() != getgid())) {
492 _eglLog(_EGL_DEBUG,
493 "ignore EGL_DRIVER for setuid/setgid binaries");
494 env = NULL;
497 else if (env) {
498 char *suffix = strchr(env, '.');
499 name_len = (suffix) ? suffix - env : strlen(env);
501 #else
502 if (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
509 * filesystem.
511 if (name_len) {
512 _EGLModule *mod;
513 EGLint i;
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);
519 if (mod)
520 mod->BuiltIn = _eglBuiltInDrivers[i].main;
522 return EGL_TRUE;
527 /* otherwise, treat env as a path */
528 if (env) {
529 _eglPreloadForEach(search_path, _eglLoaderFile, (void *) env);
531 return EGL_TRUE;
534 return EGL_FALSE;
539 * Add egl_gallium to the module array.
541 static void
542 _eglAddGalliumDriver(void)
544 #ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
545 void *external = (void *) "egl_gallium";
546 _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile, external);
547 #endif
549 #endif /* !defined(_EGL_OS_AROS) */
552 * Add built-in drivers to the module array.
554 static void
555 _eglAddBuiltInDrivers(void)
557 _EGLModule *mod;
558 EGLint i;
560 for (i = 0; _eglBuiltInDrivers[i].name; i++) {
561 mod = _eglAddModule(_eglBuiltInDrivers[i].name);
562 if (mod)
563 mod->BuiltIn = _eglBuiltInDrivers[i].main;
569 * Add drivers to the module array. Drivers will be loaded as they are matched
570 * to displays.
572 static EGLBoolean
573 _eglAddDrivers(void)
575 if (_eglModules)
576 return EGL_TRUE;
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();
587 #else
588 _eglAddBuiltInDrivers();
589 #endif
591 return (_eglModules != NULL);
596 * A helper function for _eglMatchDriver. It finds the first driver that can
597 * initialize the display and return.
599 static _EGLDriver *
600 _eglMatchAndInitialize(_EGLDisplay *dpy)
602 _EGLDriver *drv = NULL;
603 EGLint i = 0;
605 if (!_eglAddDrivers()) {
606 _eglLog(_EGL_WARNING, "failed to find any driver");
607 return NULL;
610 if (dpy->Driver) {
611 drv = dpy->Driver;
612 /* no re-matching? */
613 if (!drv->API.Initialize(drv, dpy))
614 drv = NULL;
615 return drv;
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);
624 continue;
627 if (mod->Driver->API.Initialize(mod->Driver, dpy)) {
628 drv = mod->Driver;
629 break;
631 else {
632 i++;
636 return drv;
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
643 * the display.
645 _EGLDriver *
646 _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
648 _EGLDriver *best_drv;
650 assert(!dpy->Initialized);
652 _eglLockMutex(&_eglModuleMutex);
654 /* set options */
655 dpy->Options.TestOnly = test_only;
656 dpy->Options.UseFallback = EGL_FALSE;
658 best_drv = _eglMatchAndInitialize(dpy);
659 if (!best_drv) {
660 dpy->Options.UseFallback = EGL_TRUE;
661 best_drv = _eglMatchAndInitialize(dpy);
664 _eglUnlockMutex(&_eglModuleMutex);
666 if (best_drv) {
667 _eglLog(_EGL_DEBUG, "the best driver is %s%s",
668 best_drv->Name, (test_only) ? " (test only) " : "");
669 if (!test_only) {
670 dpy->Driver = best_drv;
671 dpy->Initialized = EGL_TRUE;
675 return best_drv;
679 __eglMustCastToProperFunctionPointerType
680 _eglGetDriverProc(const char *procname)
682 EGLint i;
683 _EGLProc proc = NULL;
685 if (!_eglModules) {
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))
690 return NULL;
693 for (i = 0; i < _eglModules->Size; i++) {
694 _EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
696 if (!mod->Driver)
697 break;
698 proc = mod->Driver->API.GetProcAddress(mod->Driver, procname);
699 if (proc)
700 break;
703 return proc;
708 * Unload all drivers.
710 void
711 _eglUnloadDrivers(void)
713 /* this is called at atexit time */
714 if (_eglModules) {
715 _eglDestroyArray(_eglModules, _eglFreeModule);
716 _eglModules = NULL;
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.
727 void
728 _eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
729 void *callback_data)
731 const char *search_path = _eglGetSearchPath();
732 _eglPreloadForEach(search_path, callback, callback_data);
734 #endif