egl: Rename _eglPreloadForEach callbacks.
[mesa/nouveau-pmpeg.git] / src / egl / main / egldriver.c
bloba8a8e302e48b55bf508486f6cba310f7efb4ae0a
1 /**
2 * Functions for choosing and opening/loading device drivers.
3 */
6 #include <assert.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "eglconfig.h"
11 #include "eglcontext.h"
12 #include "egldefines.h"
13 #include "egldisplay.h"
14 #include "egldriver.h"
15 #include "eglglobals.h"
16 #include "egllog.h"
17 #include "eglmisc.h"
18 #include "eglmode.h"
19 #include "eglscreen.h"
20 #include "eglstring.h"
21 #include "eglsurface.h"
22 #include "eglimage.h"
24 #if defined(_EGL_PLATFORM_POSIX)
25 #include <dlfcn.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #endif
32 /**
33 * Wrappers for dlopen/dlclose()
35 #if defined(_EGL_PLATFORM_WINDOWS)
38 /* XXX Need to decide how to do dynamic name lookup on Windows */
39 static const char DefaultDriverName[] = "TBD";
41 typedef HMODULE lib_handle;
43 static HMODULE
44 open_library(const char *filename)
46 return LoadLibrary(filename);
49 static void
50 close_library(HMODULE lib)
52 FreeLibrary(lib);
56 static const char *
57 library_suffix(void)
59 return ".dll";
63 #elif defined(_EGL_PLATFORM_POSIX)
66 static const char DefaultDriverName[] = "egl_glx";
68 typedef void * lib_handle;
70 static void *
71 open_library(const char *filename)
73 return dlopen(filename, RTLD_LAZY);
76 static void
77 close_library(void *lib)
79 dlclose(lib);
83 static const char *
84 library_suffix(void)
86 return ".so";
90 #else /* _EGL_PLATFORM_NO_OS */
93 static const char DefaultDriverName[] = "builtin";
95 typedef void *lib_handle;
97 static INLINE void *
98 open_library(const char *filename)
100 return (void *) filename;
103 static INLINE void
104 close_library(void *lib)
109 static const char *
110 library_suffix(void)
112 return NULL;
116 #endif
119 #define NUM_PROBE_CACHE_SLOTS 8
120 static struct {
121 EGLint keys[NUM_PROBE_CACHE_SLOTS];
122 const void *values[NUM_PROBE_CACHE_SLOTS];
123 } _eglProbeCache;
127 * Open the named driver and find its bootstrap function: _eglMain().
129 static _EGLMain_t
130 _eglOpenLibrary(const char *driverPath, lib_handle *handle)
132 lib_handle lib;
133 _EGLMain_t mainFunc = NULL;
134 const char *error = "unknown error";
136 assert(driverPath);
138 _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
139 lib = open_library(driverPath);
141 #if defined(_EGL_PLATFORM_WINDOWS)
142 /* XXX untested */
143 if (lib)
144 mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
145 #elif defined(_EGL_PLATFORM_POSIX)
146 if (lib) {
147 mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
148 if (!mainFunc)
149 error = dlerror();
151 else {
152 error = dlerror();
154 #else /* _EGL_PLATFORM_NO_OS */
155 /* must be the default driver name */
156 if (strcmp(driverPath, DefaultDriverName) == 0)
157 mainFunc = (_EGLMain_t) _eglMain;
158 else
159 error = "not builtin driver";
160 #endif
162 if (!lib) {
163 _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
164 driverPath, error);
165 if (!getenv("EGL_DRIVER"))
166 _eglLog(_EGL_WARNING,
167 "The driver can be overridden by setting EGL_DRIVER");
168 return NULL;
171 if (!mainFunc) {
172 _eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
173 driverPath, error);
174 if (lib)
175 close_library(lib);
176 return NULL;
179 *handle = lib;
180 return mainFunc;
185 * Load the named driver.
187 static _EGLDriver *
188 _eglLoadDriver(const char *path, const char *args)
190 _EGLMain_t mainFunc;
191 lib_handle lib;
192 _EGLDriver *drv = NULL;
194 mainFunc = _eglOpenLibrary(path, &lib);
195 if (!mainFunc)
196 return NULL;
198 drv = mainFunc(args);
199 if (!drv) {
200 if (lib)
201 close_library(lib);
202 return NULL;
205 if (!drv->Name) {
206 _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
207 drv->Name = "UNNAMED";
210 drv->Path = _eglstrdup(path);
211 drv->Args = (args) ? _eglstrdup(args) : NULL;
212 if (!drv->Path || (args && !drv->Args)) {
213 if (drv->Path)
214 free((char *) drv->Path);
215 if (drv->Args)
216 free((char *) drv->Args);
217 drv->Unload(drv);
218 if (lib)
219 close_library(lib);
220 return NULL;
223 drv->LibHandle = lib;
225 return drv;
230 * Match a display to a preloaded driver.
232 * The matching is done by finding the driver with the highest score.
234 _EGLDriver *
235 _eglMatchDriver(_EGLDisplay *dpy)
237 _EGLDriver *best_drv = NULL;
238 EGLint best_score = -1, i;
240 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
241 _EGLDriver *drv = _eglGlobal.Drivers[i];
242 EGLint score;
244 score = (drv->Probe) ? drv->Probe(drv, dpy) : 0;
245 if (score > best_score) {
246 if (best_drv) {
247 _eglLog(_EGL_DEBUG, "driver %s has higher score than %s",
248 drv->Name, best_drv->Name);
251 best_drv = drv;
252 best_score = score;
253 /* perfect match */
254 if (score >= 100)
255 break;
259 return best_drv;
264 * A loader function for use with _eglPreloadForEach. The loader data is the
265 * filename of the driver. This function stops on the first valid driver.
267 static EGLBoolean
268 _eglLoaderFile(const char *dir, size_t len, void *loader_data)
270 _EGLDriver *drv;
271 char path[1024];
272 const char *filename = (const char *) loader_data;
273 size_t flen = strlen(filename);
275 /* make a full path */
276 if (len + flen + 2 > sizeof(path))
277 return EGL_TRUE;
278 if (len) {
279 memcpy(path, dir, len);
280 path[len++] = '/';
282 memcpy(path + len, filename, flen);
283 len += flen;
284 path[len] = '\0';
286 drv = _eglLoadDriver(path, NULL);
287 /* fix the path and load again */
288 if (!drv && library_suffix()) {
289 const char *suffix = library_suffix();
290 size_t slen = strlen(suffix);
291 const char *p;
292 EGLBoolean need_suffix;
294 p = filename + flen - slen;
295 need_suffix = (p < filename || strcmp(p, suffix) != 0);
296 if (need_suffix && len + slen + 1 <= sizeof(path)) {
297 strcpy(path + len, suffix);
298 drv = _eglLoadDriver(path, NULL);
301 if (!drv)
302 return EGL_TRUE;
304 /* remember the driver and stop */
305 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
306 return EGL_FALSE;
311 * A loader function for use with _eglPreloadForEach. The loader data is the
312 * pattern (prefix) of the files to look for.
314 static EGLBoolean
315 _eglLoaderPattern(const char *dir, size_t len, void *loader_data)
317 #if defined(_EGL_PLATFORM_POSIX)
318 const char *prefix, *suffix;
319 size_t prefix_len, suffix_len;
320 DIR *dirp;
321 struct dirent *dirent;
322 char path[1024];
324 if (len + 2 > sizeof(path))
325 return EGL_TRUE;
326 if (len) {
327 memcpy(path, dir, len);
328 path[len++] = '/';
330 path[len] = '\0';
332 dirp = opendir(path);
333 if (!dirp)
334 return EGL_TRUE;
336 prefix = (const char *) loader_data;
337 prefix_len = strlen(prefix);
338 suffix = library_suffix();
339 suffix_len = (suffix) ? strlen(suffix) : 0;
341 while ((dirent = readdir(dirp))) {
342 _EGLDriver *drv;
343 size_t dirent_len = strlen(dirent->d_name);
344 const char *p;
346 /* match the prefix */
347 if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
348 continue;
349 /* match the suffix */
350 if (suffix) {
351 p = dirent->d_name + dirent_len - suffix_len;
352 if (p < dirent->d_name || strcmp(p, suffix) != 0)
353 continue;
356 /* make a full path and load the driver */
357 if (len + dirent_len + 1 <= sizeof(path)) {
358 strcpy(path + len, dirent->d_name);
359 drv = _eglLoadDriver(path, NULL);
360 if (drv)
361 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
365 closedir(dirp);
367 return EGL_TRUE;
368 #else /* _EGL_PLATFORM_POSIX */
369 /* stop immediately */
370 return EGL_FALSE;
371 #endif
376 * Run the preload function on each driver directory and return the number of
377 * drivers loaded.
379 * The process may end prematurely if the callback function returns false.
381 static EGLint
382 _eglPreloadForEach(const char *search_path,
383 EGLBoolean (*loader)(const char *, size_t, void *),
384 void *loader_data)
386 const char *cur, *next;
387 size_t len;
388 EGLint num_drivers = _eglGlobal.NumDrivers;
390 cur = search_path;
391 while (cur) {
392 next = strchr(cur, ':');
393 len = (next) ? next - cur : strlen(cur);
395 if (!loader(cur, len, loader))
396 break;
398 cur = (next) ? next + 1 : NULL;
401 return (_eglGlobal.NumDrivers - num_drivers);
406 * Return a list of colon-separated driver directories.
408 static const char *
409 _eglGetSearchPath(void)
411 static const char *search_path;
413 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
414 if (!search_path) {
415 static char buffer[1024];
416 const char *p;
417 int ret;
419 p = getenv("EGL_DRIVERS_PATH");
420 #if defined(_EGL_PLATFORM_POSIX)
421 if (p && (geteuid() != getuid() || getegid() != getgid())) {
422 _eglLog(_EGL_DEBUG,
423 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
424 p = NULL;
426 #endif /* _EGL_PLATFORM_POSIX */
428 if (p) {
429 ret = snprintf(buffer, sizeof(buffer),
430 "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
431 if (ret > 0 && ret < sizeof(buffer))
432 search_path = buffer;
435 if (!search_path)
436 search_path = _EGL_DRIVER_SEARCH_DIR;
437 #else
438 search_path = "";
439 #endif
441 return search_path;
446 * Preload a user driver.
448 * A user driver can be specified by EGL_DRIVER.
450 static EGLBoolean
451 _eglPreloadUserDriver(void)
453 const char *search_path = _eglGetSearchPath();
454 char *env;
456 env = getenv("EGL_DRIVER");
457 #if defined(_EGL_PLATFORM_POSIX)
458 if (env && strchr(env, '/')) {
459 search_path = "";
460 if ((geteuid() != getuid() || getegid() != getgid())) {
461 _eglLog(_EGL_DEBUG,
462 "ignore EGL_DRIVER for setuid/setgid binaries");
463 env = NULL;
466 #endif /* _EGL_PLATFORM_POSIX */
467 if (!env)
468 return EGL_FALSE;
470 if (!_eglPreloadForEach(search_path, _eglLoaderFile, (void *) env)) {
471 _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
472 return EGL_FALSE;
475 return EGL_TRUE;
480 * Preload display drivers.
482 * Display drivers are a set of drivers that support a certain display system.
483 * The display system may be specified by EGL_DISPLAY.
485 * FIXME This makes libEGL a memory hog if an user driver is not specified and
486 * there are many display drivers.
488 static EGLBoolean
489 _eglPreloadDisplayDrivers(void)
491 const char *dpy;
492 char prefix[32];
493 int ret;
495 dpy = getenv("EGL_DISPLAY");
496 if (!dpy || !dpy[0])
497 dpy = _EGL_DEFAULT_DISPLAY;
498 if (!dpy || !dpy[0])
499 return EGL_FALSE;
501 ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
502 if (ret < 0 || ret >= sizeof(prefix))
503 return EGL_FALSE;
505 return (_eglPreloadForEach(_eglGetSearchPath(),
506 _eglLoaderPattern, (void *) prefix) > 0);
511 * Preload the default driver.
513 static EGLBoolean
514 _eglPreloadDefaultDriver(void)
516 return (_eglPreloadForEach(_eglGetSearchPath(),
517 _eglLoaderFile, (void *) DefaultDriverName) > 0);
522 * Preload drivers.
524 * This function loads the driver modules and creates the corresponding
525 * _EGLDriver objects.
527 EGLBoolean
528 _eglPreloadDrivers(void)
530 EGLBoolean loaded;
532 /* already preloaded */
533 if (_eglGlobal.NumDrivers)
534 return EGL_TRUE;
536 loaded = (_eglPreloadUserDriver() ||
537 _eglPreloadDisplayDrivers() ||
538 _eglPreloadDefaultDriver());
540 return loaded;
545 * Unload preloaded drivers.
547 void
548 _eglUnloadDrivers(void)
550 EGLint i;
551 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
552 _EGLDriver *drv = _eglGlobal.Drivers[i];
553 lib_handle handle = drv->LibHandle;
555 if (drv->Path)
556 free((char *) drv->Path);
557 if (drv->Args)
558 free((char *) drv->Args);
560 /* destroy driver */
561 if (drv->Unload)
562 drv->Unload(drv);
564 if (handle)
565 close_library(handle);
566 _eglGlobal.Drivers[i] = NULL;
569 _eglGlobal.NumDrivers = 0;
574 * Plug all the available fallback routines into the given driver's
575 * dispatch table.
577 void
578 _eglInitDriverFallbacks(_EGLDriver *drv)
580 /* If a pointer is set to NULL, then the device driver _really_ has
581 * to implement it.
583 drv->API.Initialize = NULL;
584 drv->API.Terminate = NULL;
586 drv->API.GetConfigs = _eglGetConfigs;
587 drv->API.ChooseConfig = _eglChooseConfig;
588 drv->API.GetConfigAttrib = _eglGetConfigAttrib;
590 drv->API.CreateContext = _eglCreateContext;
591 drv->API.DestroyContext = _eglDestroyContext;
592 drv->API.MakeCurrent = _eglMakeCurrent;
593 drv->API.QueryContext = _eglQueryContext;
595 drv->API.CreateWindowSurface = _eglCreateWindowSurface;
596 drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
597 drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
598 drv->API.DestroySurface = _eglDestroySurface;
599 drv->API.QuerySurface = _eglQuerySurface;
600 drv->API.SurfaceAttrib = _eglSurfaceAttrib;
601 drv->API.BindTexImage = _eglBindTexImage;
602 drv->API.ReleaseTexImage = _eglReleaseTexImage;
603 drv->API.SwapInterval = _eglSwapInterval;
604 drv->API.SwapBuffers = _eglSwapBuffers;
605 drv->API.CopyBuffers = _eglCopyBuffers;
607 drv->API.QueryString = _eglQueryString;
608 drv->API.WaitClient = _eglWaitClient;
609 drv->API.WaitNative = _eglWaitNative;
611 #ifdef EGL_MESA_screen_surface
612 drv->API.ChooseModeMESA = _eglChooseModeMESA;
613 drv->API.GetModesMESA = _eglGetModesMESA;
614 drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
615 drv->API.GetScreensMESA = _eglGetScreensMESA;
616 drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
617 drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
618 drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
619 drv->API.QueryScreenMESA = _eglQueryScreenMESA;
620 drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
621 drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
622 drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
623 #endif /* EGL_MESA_screen_surface */
625 #ifdef EGL_VERSION_1_2
626 drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
627 #endif /* EGL_VERSION_1_2 */
629 #ifdef EGL_KHR_image_base
630 drv->API.CreateImageKHR = _eglCreateImageKHR;
631 drv->API.DestroyImageKHR = _eglDestroyImageKHR;
632 #endif /* EGL_KHR_image_base */
637 * Set the probe cache at the given key.
639 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
640 * by multiple drivers.
642 void
643 _eglSetProbeCache(EGLint key, const void *val)
645 EGLint idx;
647 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
648 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
649 break;
651 assert(key > 0);
652 assert(idx < NUM_PROBE_CACHE_SLOTS);
654 _eglProbeCache.keys[idx] = key;
655 _eglProbeCache.values[idx] = val;
660 * Return the probe cache at the given key.
662 const void *
663 _eglGetProbeCache(EGLint key)
665 EGLint idx;
667 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
668 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
669 break;
672 return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
673 _eglProbeCache.values[idx] : NULL;