egl: Add EGL_DRIVERS_PATH environment variable.
[mesa.git] / src / egl / main / egldriver.c
1 /**
2 * Functions for choosing and opening/loading device drivers.
3 */
4
5
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"
23
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
30
31
32 /**
33 * Wrappers for dlopen/dlclose()
34 */
35 #if defined(_EGL_PLATFORM_WINDOWS)
36
37
38 /* XXX Need to decide how to do dynamic name lookup on Windows */
39 static const char DefaultDriverName[] = "TBD";
40
41 typedef HMODULE lib_handle;
42
43 static HMODULE
44 open_library(const char *filename)
45 {
46 return LoadLibrary(filename);
47 }
48
49 static void
50 close_library(HMODULE lib)
51 {
52 FreeLibrary(lib);
53 }
54
55
56 static const char *
57 library_suffix(void)
58 {
59 return ".dll";
60 }
61
62
63 #elif defined(_EGL_PLATFORM_POSIX)
64
65
66 static const char DefaultDriverName[] = "egl_glx";
67
68 typedef void * lib_handle;
69
70 static void *
71 open_library(const char *filename)
72 {
73 return dlopen(filename, RTLD_LAZY);
74 }
75
76 static void
77 close_library(void *lib)
78 {
79 dlclose(lib);
80 }
81
82
83 static const char *
84 library_suffix(void)
85 {
86 return ".so";
87 }
88
89
90 #else /* _EGL_PLATFORM_NO_OS */
91
92
93 static const char DefaultDriverName[] = "builtin";
94
95 typedef void *lib_handle;
96
97 static INLINE void *
98 open_library(const char *filename)
99 {
100 return (void *) filename;
101 }
102
103 static INLINE void
104 close_library(void *lib)
105 {
106 }
107
108
109 static const char *
110 library_suffix(void)
111 {
112 return NULL;
113 }
114
115
116 #endif
117
118
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;
124
125
126 /**
127 * Open the named driver and find its bootstrap function: _eglMain().
128 */
129 static _EGLMain_t
130 _eglOpenLibrary(const char *driverPath, lib_handle *handle)
131 {
132 lib_handle lib;
133 _EGLMain_t mainFunc = NULL;
134 const char *error = "unknown error";
135
136 assert(driverPath);
137
138 _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
139 lib = open_library(driverPath);
140
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();
150 }
151 else {
152 error = dlerror();
153 }
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
161
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;
169 }
170
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;
177 }
178
179 *handle = lib;
180 return mainFunc;
181 }
182
183
184 /**
185 * Load the named driver.
186 */
187 static _EGLDriver *
188 _eglLoadDriver(const char *path, const char *args)
189 {
190 _EGLMain_t mainFunc;
191 lib_handle lib;
192 _EGLDriver *drv = NULL;
193
194 mainFunc = _eglOpenLibrary(path, &lib);
195 if (!mainFunc)
196 return NULL;
197
198 drv = mainFunc(args);
199 if (!drv) {
200 if (lib)
201 close_library(lib);
202 return NULL;
203 }
204
205 if (!drv->Name) {
206 _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
207 drv->Name = "UNNAMED";
208 }
209
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;
221 }
222
223 drv->LibHandle = lib;
224
225 return drv;
226 }
227
228
229 /**
230 * Match a display to a preloaded driver.
231 *
232 * The matching is done by finding the driver with the highest score.
233 */
234 _EGLDriver *
235 _eglMatchDriver(_EGLDisplay *dpy)
236 {
237 _EGLDriver *best_drv = NULL;
238 EGLint best_score = -1, i;
239
240 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
241 _EGLDriver *drv = _eglGlobal.Drivers[i];
242 EGLint score;
243
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);
249 }
250
251 best_drv = drv;
252 best_score = score;
253 /* perfect match */
254 if (score >= 100)
255 break;
256 }
257 }
258
259 return best_drv;
260 }
261
262
263 /**
264 * A preload function for use with _eglPreloadForEach. The preload data is the
265 * filename of the driver. This function stops on the first valid driver.
266 */
267 static EGLBoolean
268 _eglPreloadFile(const char *dir, size_t len, void *preload_data)
269 {
270 _EGLDriver *drv;
271 char path[1024];
272 const char *filename = (const char *) preload_data;
273 size_t flen = strlen(filename);
274
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++] = '/';
281 }
282 memcpy(path + len, filename, flen);
283 len += flen;
284 path[len] = '\0';
285
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;
293
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);
299 }
300 }
301 if (!drv)
302 return EGL_TRUE;
303
304 /* remember the driver and stop */
305 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
306 return EGL_FALSE;
307 }
308
309
310 /**
311 * A preload function for use with _eglPreloadForEach. The preload data is the
312 * pattern (prefix) of the files to look for.
313 */
314 static EGLBoolean
315 _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
316 {
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];
323
324 if (len + 2 > sizeof(path))
325 return EGL_TRUE;
326 if (len) {
327 memcpy(path, dir, len);
328 path[len++] = '/';
329 }
330 path[len] = '\0';
331
332 dirp = opendir(path);
333 if (!dirp)
334 return EGL_TRUE;
335
336 prefix = (const char *) preload_data;
337 prefix_len = strlen(prefix);
338 suffix = library_suffix();
339 suffix_len = (suffix) ? strlen(suffix) : 0;
340
341 while ((dirent = readdir(dirp))) {
342 _EGLDriver *drv;
343 size_t dirent_len = strlen(dirent->d_name);
344 const char *p;
345
346 /* match the prefix */
347 if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
348 continue;
349 /* match the suffix */
350 p = dirent->d_name + dirent_len - suffix_len;
351 if (p < dirent->d_name || strcmp(p, suffix) != 0)
352 continue;
353
354 /* make a full path and load the driver */
355 if (len + dirent_len + 1 <= sizeof(path)) {
356 strcpy(path + len, dirent->d_name);
357 drv = _eglLoadDriver(path, NULL);
358 if (drv)
359 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
360 }
361 }
362
363 closedir(dirp);
364
365 return EGL_TRUE;
366 #else /* _EGL_PLATFORM_POSIX */
367 /* stop immediately */
368 return EGL_FALSE;
369 #endif
370 }
371
372
373 /**
374 * Run the preload function on each driver directory and return the number of
375 * drivers loaded.
376 *
377 * The process may end prematurely if the callback function returns false.
378 */
379 static EGLint
380 _eglPreloadForEach(const char *search_path,
381 EGLBoolean (*preload)(const char *, size_t, void *),
382 void *preload_data)
383 {
384 const char *cur, *next;
385 size_t len;
386 EGLint num_drivers = _eglGlobal.NumDrivers;
387
388 cur = search_path;
389 while (cur) {
390 next = strchr(cur, ':');
391 len = (next) ? next - cur : strlen(cur);
392
393 if (!preload(cur, len, preload_data))
394 break;
395
396 cur = (next) ? next + 1 : NULL;
397 }
398
399 return (_eglGlobal.NumDrivers - num_drivers);
400 }
401
402
403 /**
404 * Return a list of colon-separated driver directories.
405 */
406 static const char *
407 _eglGetSearchPath(void)
408 {
409 static const char *search_path;
410
411 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
412 if (!search_path) {
413 static char buffer[1024];
414 const char *p;
415 int ret;
416
417 p = getenv("EGL_DRIVERS_PATH");
418 if (p) {
419 ret = snprintf(buffer, sizeof(buffer),
420 "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
421 if (ret > 0 && ret < sizeof(buffer))
422 search_path = buffer;
423 }
424 }
425 if (!search_path)
426 search_path = _EGL_DRIVER_SEARCH_DIR;
427 #else
428 search_path = "";
429 #endif
430
431 return search_path;
432 }
433
434
435 /**
436 * Preload a user driver.
437 *
438 * A user driver can be specified by EGL_DRIVER.
439 */
440 static EGLBoolean
441 _eglPreloadUserDriver(void)
442 {
443 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
444 const char *search_path = _eglGetSearchPath();
445 char *env;
446
447 env = getenv("EGL_DRIVER");
448 #if defined(_EGL_PLATFORM_POSIX)
449 if (env && strchr(env, '/'))
450 search_path = "";
451 #endif
452 if (!env)
453 return EGL_FALSE;
454
455 if (!_eglPreloadForEach(search_path, _eglPreloadFile, (void *) env)) {
456 _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
457 return EGL_FALSE;
458 }
459
460 return EGL_TRUE;
461 #else /* _EGL_PLATFORM_POSIX || _EGL_PLATFORM_WINDOWS */
462 return EGL_FALSE;
463 #endif
464 }
465
466
467 /**
468 * Preload display drivers.
469 *
470 * Display drivers are a set of drivers that support a certain display system.
471 * The display system may be specified by EGL_DISPLAY.
472 *
473 * FIXME This makes libEGL a memory hog if an user driver is not specified and
474 * there are many display drivers.
475 */
476 static EGLBoolean
477 _eglPreloadDisplayDrivers(void)
478 {
479 #if defined(_EGL_PLATFORM_POSIX)
480 const char *dpy;
481 char prefix[32];
482 int ret;
483
484 dpy = getenv("EGL_DISPLAY");
485 if (!dpy || !dpy[0])
486 dpy = _EGL_DEFAULT_DISPLAY;
487 if (!dpy || !dpy[0])
488 return EGL_FALSE;
489
490 ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
491 if (ret < 0 || ret >= sizeof(prefix))
492 return EGL_FALSE;
493
494 return (_eglPreloadForEach(_eglGetSearchPath(),
495 _eglPreloadPattern, (void *) prefix) > 0);
496 #else /* _EGL_PLATFORM_POSIX */
497 return EGL_FALSE;
498 #endif
499 }
500
501
502 /**
503 * Preload the default driver.
504 */
505 static EGLBoolean
506 _eglPreloadDefaultDriver(void)
507 {
508 return (_eglPreloadForEach(_eglGetSearchPath(),
509 _eglPreloadFile, (void *) DefaultDriverName) > 0);
510 }
511
512
513 /**
514 * Preload drivers.
515 *
516 * This function loads the driver modules and creates the corresponding
517 * _EGLDriver objects.
518 */
519 EGLBoolean
520 _eglPreloadDrivers(void)
521 {
522 EGLBoolean loaded;
523
524 /* already preloaded */
525 if (_eglGlobal.NumDrivers)
526 return EGL_TRUE;
527
528 loaded = (_eglPreloadUserDriver() ||
529 _eglPreloadDisplayDrivers() ||
530 _eglPreloadDefaultDriver());
531
532 return loaded;
533 }
534
535
536 /**
537 * Unload preloaded drivers.
538 */
539 void
540 _eglUnloadDrivers(void)
541 {
542 EGLint i;
543 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
544 _EGLDriver *drv = _eglGlobal.Drivers[i];
545 lib_handle handle = drv->LibHandle;
546
547 if (drv->Path)
548 free((char *) drv->Path);
549 if (drv->Args)
550 free((char *) drv->Args);
551
552 /* destroy driver */
553 if (drv->Unload)
554 drv->Unload(drv);
555
556 if (handle)
557 close_library(handle);
558 _eglGlobal.Drivers[i] = NULL;
559 }
560
561 _eglGlobal.NumDrivers = 0;
562 }
563
564
565 /**
566 * Plug all the available fallback routines into the given driver's
567 * dispatch table.
568 */
569 void
570 _eglInitDriverFallbacks(_EGLDriver *drv)
571 {
572 /* If a pointer is set to NULL, then the device driver _really_ has
573 * to implement it.
574 */
575 drv->API.Initialize = NULL;
576 drv->API.Terminate = NULL;
577
578 drv->API.GetConfigs = _eglGetConfigs;
579 drv->API.ChooseConfig = _eglChooseConfig;
580 drv->API.GetConfigAttrib = _eglGetConfigAttrib;
581
582 drv->API.CreateContext = _eglCreateContext;
583 drv->API.DestroyContext = _eglDestroyContext;
584 drv->API.MakeCurrent = _eglMakeCurrent;
585 drv->API.QueryContext = _eglQueryContext;
586
587 drv->API.CreateWindowSurface = _eglCreateWindowSurface;
588 drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
589 drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
590 drv->API.DestroySurface = _eglDestroySurface;
591 drv->API.QuerySurface = _eglQuerySurface;
592 drv->API.SurfaceAttrib = _eglSurfaceAttrib;
593 drv->API.BindTexImage = _eglBindTexImage;
594 drv->API.ReleaseTexImage = _eglReleaseTexImage;
595 drv->API.SwapInterval = _eglSwapInterval;
596 drv->API.SwapBuffers = _eglSwapBuffers;
597 drv->API.CopyBuffers = _eglCopyBuffers;
598
599 drv->API.QueryString = _eglQueryString;
600 drv->API.WaitClient = _eglWaitClient;
601 drv->API.WaitNative = _eglWaitNative;
602
603 #ifdef EGL_MESA_screen_surface
604 drv->API.ChooseModeMESA = _eglChooseModeMESA;
605 drv->API.GetModesMESA = _eglGetModesMESA;
606 drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
607 drv->API.GetScreensMESA = _eglGetScreensMESA;
608 drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
609 drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
610 drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
611 drv->API.QueryScreenMESA = _eglQueryScreenMESA;
612 drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
613 drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
614 drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
615 #endif /* EGL_MESA_screen_surface */
616
617 #ifdef EGL_VERSION_1_2
618 drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
619 #endif /* EGL_VERSION_1_2 */
620
621 #ifdef EGL_KHR_image_base
622 drv->API.CreateImageKHR = _eglCreateImageKHR;
623 drv->API.DestroyImageKHR = _eglDestroyImageKHR;
624 #endif /* EGL_KHR_image_base */
625 }
626
627
628 /**
629 * Set the probe cache at the given key.
630 *
631 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
632 * by multiple drivers.
633 */
634 void
635 _eglSetProbeCache(EGLint key, const void *val)
636 {
637 EGLint idx;
638
639 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
640 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
641 break;
642 }
643 assert(key > 0);
644 assert(idx < NUM_PROBE_CACHE_SLOTS);
645
646 _eglProbeCache.keys[idx] = key;
647 _eglProbeCache.values[idx] = val;
648 }
649
650
651 /**
652 * Return the probe cache at the given key.
653 */
654 const void *
655 _eglGetProbeCache(EGLint key)
656 {
657 EGLint idx;
658
659 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
660 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
661 break;
662 }
663
664 return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
665 _eglProbeCache.values[idx] : NULL;
666 }