egl: fix wrong argument. Use loader_data instead of loader
[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 loader function for use with _eglPreloadForEach. The loader data is the
265 * filename of the driver. This function stops on the first valid driver.
266 */
267 static EGLBoolean
268 _eglLoaderFile(const char *dir, size_t len, void *loader_data)
269 {
270 _EGLDriver *drv;
271 char path[1024];
272 const char *filename = (const char *) loader_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 loader function for use with _eglPreloadForEach. The loader data is the
312 * pattern (prefix) of the files to look for.
313 */
314 static EGLBoolean
315 _eglLoaderPattern(const char *dir, size_t len, void *loader_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 *) loader_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 if (suffix) {
351 p = dirent->d_name + dirent_len - suffix_len;
352 if (p < dirent->d_name || strcmp(p, suffix) != 0)
353 continue;
354 }
355
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;
362 }
363 }
364
365 closedir(dirp);
366
367 return EGL_TRUE;
368 #else /* _EGL_PLATFORM_POSIX */
369 /* stop immediately */
370 return EGL_FALSE;
371 #endif
372 }
373
374
375 /**
376 * Run the preload function on each driver directory and return the number of
377 * drivers loaded.
378 *
379 * The process may end prematurely if the callback function returns false.
380 */
381 static EGLint
382 _eglPreloadForEach(const char *search_path,
383 EGLBoolean (*loader)(const char *, size_t, void *),
384 void *loader_data)
385 {
386 const char *cur, *next;
387 size_t len;
388 EGLint num_drivers = _eglGlobal.NumDrivers;
389
390 cur = search_path;
391 while (cur) {
392 next = strchr(cur, ':');
393 len = (next) ? next - cur : strlen(cur);
394
395 if (!loader(cur, len, loader_data))
396 break;
397
398 cur = (next) ? next + 1 : NULL;
399 }
400
401 return (_eglGlobal.NumDrivers - num_drivers);
402 }
403
404
405 /**
406 * Return a list of colon-separated driver directories.
407 */
408 static const char *
409 _eglGetSearchPath(void)
410 {
411 static const char *search_path;
412
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;
418
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;
425 }
426 #endif /* _EGL_PLATFORM_POSIX */
427
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;
433 }
434 }
435 if (!search_path)
436 search_path = _EGL_DRIVER_SEARCH_DIR;
437 #else
438 search_path = "";
439 #endif
440
441 return search_path;
442 }
443
444
445 /**
446 * Preload a user driver.
447 *
448 * A user driver can be specified by EGL_DRIVER.
449 */
450 static EGLBoolean
451 _eglPreloadUserDriver(void)
452 {
453 const char *search_path = _eglGetSearchPath();
454 char *env;
455
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;
464 }
465 }
466 #endif /* _EGL_PLATFORM_POSIX */
467 if (!env)
468 return EGL_FALSE;
469
470 if (!_eglPreloadForEach(search_path, _eglLoaderFile, (void *) env)) {
471 _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
472 return EGL_FALSE;
473 }
474
475 return EGL_TRUE;
476 }
477
478
479 /**
480 * Preload display drivers.
481 *
482 * Display drivers are a set of drivers that support a certain display system.
483 * The display system may be specified by EGL_DISPLAY.
484 *
485 * FIXME This makes libEGL a memory hog if an user driver is not specified and
486 * there are many display drivers.
487 */
488 static EGLBoolean
489 _eglPreloadDisplayDrivers(void)
490 {
491 const char *dpy;
492 char prefix[32];
493 int ret;
494
495 dpy = getenv("EGL_DISPLAY");
496 if (!dpy || !dpy[0])
497 dpy = _EGL_DEFAULT_DISPLAY;
498 if (!dpy || !dpy[0])
499 return EGL_FALSE;
500
501 ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
502 if (ret < 0 || ret >= sizeof(prefix))
503 return EGL_FALSE;
504
505 return (_eglPreloadForEach(_eglGetSearchPath(),
506 _eglLoaderPattern, (void *) prefix) > 0);
507 }
508
509
510 /**
511 * Preload the default driver.
512 */
513 static EGLBoolean
514 _eglPreloadDefaultDriver(void)
515 {
516 return (_eglPreloadForEach(_eglGetSearchPath(),
517 _eglLoaderFile, (void *) DefaultDriverName) > 0);
518 }
519
520
521 /**
522 * Preload drivers.
523 *
524 * This function loads the driver modules and creates the corresponding
525 * _EGLDriver objects.
526 */
527 EGLBoolean
528 _eglPreloadDrivers(void)
529 {
530 EGLBoolean loaded;
531
532 /* already preloaded */
533 if (_eglGlobal.NumDrivers)
534 return EGL_TRUE;
535
536 loaded = (_eglPreloadUserDriver() ||
537 _eglPreloadDisplayDrivers() ||
538 _eglPreloadDefaultDriver());
539
540 return loaded;
541 }
542
543
544 /**
545 * Unload preloaded drivers.
546 */
547 void
548 _eglUnloadDrivers(void)
549 {
550 EGLint i;
551 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
552 _EGLDriver *drv = _eglGlobal.Drivers[i];
553 lib_handle handle = drv->LibHandle;
554
555 if (drv->Path)
556 free((char *) drv->Path);
557 if (drv->Args)
558 free((char *) drv->Args);
559
560 /* destroy driver */
561 if (drv->Unload)
562 drv->Unload(drv);
563
564 if (handle)
565 close_library(handle);
566 _eglGlobal.Drivers[i] = NULL;
567 }
568
569 _eglGlobal.NumDrivers = 0;
570 }
571
572
573 /**
574 * Plug all the available fallback routines into the given driver's
575 * dispatch table.
576 */
577 void
578 _eglInitDriverFallbacks(_EGLDriver *drv)
579 {
580 /* If a pointer is set to NULL, then the device driver _really_ has
581 * to implement it.
582 */
583 drv->API.Initialize = NULL;
584 drv->API.Terminate = NULL;
585
586 drv->API.GetConfigs = _eglGetConfigs;
587 drv->API.ChooseConfig = _eglChooseConfig;
588 drv->API.GetConfigAttrib = _eglGetConfigAttrib;
589
590 drv->API.CreateContext = _eglCreateContext;
591 drv->API.DestroyContext = _eglDestroyContext;
592 drv->API.MakeCurrent = _eglMakeCurrent;
593 drv->API.QueryContext = _eglQueryContext;
594
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;
606
607 drv->API.QueryString = _eglQueryString;
608 drv->API.WaitClient = _eglWaitClient;
609 drv->API.WaitNative = _eglWaitNative;
610
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 */
624
625 #ifdef EGL_VERSION_1_2
626 drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
627 #endif /* EGL_VERSION_1_2 */
628
629 #ifdef EGL_KHR_image_base
630 drv->API.CreateImageKHR = _eglCreateImageKHR;
631 drv->API.DestroyImageKHR = _eglDestroyImageKHR;
632 #endif /* EGL_KHR_image_base */
633 }
634
635
636 /**
637 * Set the probe cache at the given key.
638 *
639 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
640 * by multiple drivers.
641 */
642 void
643 _eglSetProbeCache(EGLint key, const void *val)
644 {
645 EGLint idx;
646
647 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
648 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
649 break;
650 }
651 assert(key > 0);
652 assert(idx < NUM_PROBE_CACHE_SLOTS);
653
654 _eglProbeCache.keys[idx] = key;
655 _eglProbeCache.values[idx] = val;
656 }
657
658
659 /**
660 * Return the probe cache at the given key.
661 */
662 const void *
663 _eglGetProbeCache(EGLint key)
664 {
665 EGLint idx;
666
667 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
668 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
669 break;
670 }
671
672 return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
673 _eglProbeCache.values[idx] : NULL;
674 }