1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/**
* Functions for choosing and opening/loading device drivers.
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "eglconfig.h"
#include "eglcontext.h"
#include "egldefines.h"
#include "egldisplay.h"
#include "egldriver.h"
#include "eglglobals.h"
#include "eglcurrent.h"
#include "egllog.h"
#include "eglmisc.h"
#include "eglmode.h"
#include "eglscreen.h"
#include "eglstring.h"
#include "eglsurface.h"
#include "eglimage.h"
#if defined(_EGL_PLATFORM_POSIX)
#include <dlfcn.h>
#include <sys/types.h>
#include <dirent.h>
#endif
/**
* Wrappers for dlopen/dlclose()
*/
#if defined(_EGL_PLATFORM_WINDOWS)
/* XXX Need to decide how to do dynamic name lookup on Windows */
static const char DefaultDriverName[] = "TBD";
typedef HMODULE lib_handle;
static HMODULE
open_library(const char *filename)
{
return LoadLibrary(filename);
}
static void
close_library(HMODULE lib)
{
FreeLibrary(lib);
}
static const char *
library_suffix(void)
{
return "dll";
}
static EGLBoolean
make_library_path(char *buf, unsigned int size, const char *name)
{
EGLBoolean need_suffix;
const char *suffix = ".dll";
int ret;
need_suffix = (strchr(name, '.') == NULL);
ret = snprintf(buf, size, "%s%s", name, (need_suffix) ? suffix : "");
return ((unsigned int) ret < size);
}
#elif defined(_EGL_PLATFORM_POSIX)
static const char DefaultDriverName[] = "egl_glx";
typedef void * lib_handle;
static void *
open_library(const char *filename)
{
return dlopen(filename, RTLD_LAZY);
}
static void
close_library(void *lib)
{
dlclose(lib);
}
static const char *
library_suffix(void)
{
return "so";
}
static EGLBoolean
make_library_path(char *buf, unsigned int size, const char *name)
{
EGLBoolean need_dir, need_suffix;
const char *suffix = ".so";
int ret;
need_dir = (strchr(name, '/') == NULL);
need_suffix = (strchr(name, '.') == NULL);
ret = snprintf(buf, size, "%s%s%s",
(need_dir) ? _EGL_DRIVER_SEARCH_DIR"/" : "", name,
(need_suffix) ? suffix : "");
return ((unsigned int) ret < size);
}
#else /* _EGL_PLATFORM_NO_OS */
static const char DefaultDriverName[] = "builtin";
typedef void *lib_handle;
static INLINE void *
open_library(const char *filename)
{
return (void *) filename;
}
static INLINE void
close_library(void *lib)
{
}
static const char *
library_suffix(void)
{
return NULL;
}
static EGLBoolean
make_library_path(char *buf, unsigned int size, const char *name)
{
int ret = snprintf(buf, size, name);
return ((unsigned int) ret < size);
}
#endif
#define NUM_PROBE_CACHE_SLOTS 8
static struct {
EGLint keys[NUM_PROBE_CACHE_SLOTS];
const void *values[NUM_PROBE_CACHE_SLOTS];
} _eglProbeCache;
/**
* Open the named driver and find its bootstrap function: _eglMain().
*/
static _EGLMain_t
_eglOpenLibrary(const char *driverPath, lib_handle *handle)
{
lib_handle lib;
_EGLMain_t mainFunc = NULL;
const char *error = "unknown error";
assert(driverPath);
_eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
lib = open_library(driverPath);
#if defined(_EGL_PLATFORM_WINDOWS)
/* XXX untested */
if (lib)
mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
#elif defined(_EGL_PLATFORM_POSIX)
if (lib) {
mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
if (!mainFunc)
error = dlerror();
}
else {
error = dlerror();
}
#else /* _EGL_PLATFORM_NO_OS */
/* must be the default driver name */
if (strcmp(driverPath, DefaultDriverName) == 0)
mainFunc = (_EGLMain_t) _eglMain;
else
error = "not builtin driver";
#endif
if (!lib) {
_eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
driverPath, error);
if (!getenv("EGL_DRIVER"))
_eglLog(_EGL_WARNING,
"The driver can be overridden by setting EGL_DRIVER");
return NULL;
}
if (!mainFunc) {
_eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
driverPath, error);
if (lib)
close_library(lib);
return NULL;
}
*handle = lib;
return mainFunc;
}
/**
* Load the named driver.
*/
static _EGLDriver *
_eglLoadDriver(const char *path, const char *args)
{
_EGLMain_t mainFunc;
lib_handle lib;
_EGLDriver *drv = NULL;
mainFunc = _eglOpenLibrary(path, &lib);
if (!mainFunc)
return NULL;
drv = mainFunc(args);
if (!drv) {
if (lib)
close_library(lib);
return NULL;
}
if (!drv->Name) {
_eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
drv->Name = "UNNAMED";
}
drv->Path = _eglstrdup(path);
drv->Args = (args) ? _eglstrdup(args) : NULL;
if (!drv->Path || (args && !drv->Args)) {
if (drv->Path)
free((char *) drv->Path);
if (drv->Args)
free((char *) drv->Args);
drv->Unload(drv);
if (lib)
close_library(lib);
return NULL;
}
drv->LibHandle = lib;
return drv;
}
/**
* Match a display to a preloaded driver.
*
* The matching is done by finding the driver with the highest score.
*/
_EGLDriver *
_eglMatchDriver(_EGLDisplay *dpy)
{
_EGLDriver *best_drv = NULL;
EGLint best_score = -1, i;
for (i = 0; i < _eglGlobal.NumDrivers; i++) {
_EGLDriver *drv = _eglGlobal.Drivers[i];
EGLint score;
score = (drv->Probe) ? drv->Probe(drv, dpy) : 0;
if (score > best_score) {
if (best_drv) {
_eglLog(_EGL_DEBUG, "driver %s has higher score than %s",
drv->Name, best_drv->Name);
}
best_drv = drv;
best_score = score;
/* perfect match */
if (score >= 100)
break;
}
}
return best_drv;
}
/**
* Preload a user driver.
*
* A user driver can be specified by EGL_DRIVER.
*/
static EGLBoolean
_eglPreloadUserDriver(void)
{
#if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
_EGLDriver *drv;
char path[1024];
char *env;
env = getenv("EGL_DRIVER");
if (!env)
return EGL_FALSE;
if (!make_library_path(path, sizeof(path), env))
return EGL_FALSE;
drv = _eglLoadDriver(path, NULL);
if (!drv) {
_eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
return EGL_FALSE;
}
_eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
return EGL_TRUE;
#else /* _EGL_PLATFORM_POSIX || _EGL_PLATFORM_WINDOWS */
return EGL_FALSE;
#endif
}
/**
* Preload display drivers.
*
* Display drivers are a set of drivers that support a certain display system.
* The display system may be specified by EGL_DISPLAY.
*
* FIXME This makes libEGL a memory hog if an user driver is not specified and
* there are many display drivers.
*/
static EGLBoolean
_eglPreloadDisplayDrivers(void)
{
#if defined(_EGL_PLATFORM_POSIX)
const char *dpy, *suffix;
char path[1024], prefix[32];
DIR *dirp;
struct dirent *dirent;
dpy = getenv("EGL_DISPLAY");
if (!dpy || !dpy[0])
dpy = _EGL_DEFAULT_DISPLAY;
if (!dpy || !dpy[0])
return EGL_FALSE;
snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
suffix = library_suffix();
dirp = opendir(_EGL_DRIVER_SEARCH_DIR);
if (!dirp)
return EGL_FALSE;
while ((dirent = readdir(dirp))) {
_EGLDriver *drv;
const char *p;
/* match the prefix */
if (strncmp(dirent->d_name, prefix, strlen(prefix)) != 0)
continue;
/* match the suffix */
p = strrchr(dirent->d_name, '.');
if ((p && !suffix) || (!p && suffix))
continue;
else if (p && suffix && strcmp(p + 1, suffix) != 0)
continue;
snprintf(path, sizeof(path),
_EGL_DRIVER_SEARCH_DIR"/%s", dirent->d_name);
drv = _eglLoadDriver(path, NULL);
if (drv)
_eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
}
closedir(dirp);
return (_eglGlobal.NumDrivers > 0);
#else /* _EGL_PLATFORM_POSIX */
return EGL_FALSE;
#endif
}
/**
* Preload the default driver.
*/
static EGLBoolean
_eglPreloadDefaultDriver(void)
{
_EGLDriver *drv;
char path[1024];
if (!make_library_path(path, sizeof(path), DefaultDriverName))
return EGL_FALSE;
drv = _eglLoadDriver(path, NULL);
if (!drv)
return EGL_FALSE;
_eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
return EGL_TRUE;
}
/**
* Preload drivers.
*
* This function loads the driver modules and creates the corresponding
* _EGLDriver objects.
*/
EGLBoolean
_eglPreloadDrivers(void)
{
EGLBoolean loaded;
/* already preloaded */
if (_eglGlobal.NumDrivers)
return EGL_TRUE;
loaded = (_eglPreloadUserDriver() ||
_eglPreloadDisplayDrivers() ||
_eglPreloadDefaultDriver());
return loaded;
}
/**
* Unload preloaded drivers.
*/
void
_eglUnloadDrivers(void)
{
EGLint i;
for (i = 0; i < _eglGlobal.NumDrivers; i++) {
_EGLDriver *drv = _eglGlobal.Drivers[i];
lib_handle handle = drv->LibHandle;
if (drv->Path)
free((char *) drv->Path);
if (drv->Args)
free((char *) drv->Args);
/* destroy driver */
if (drv->Unload)
drv->Unload(drv);
if (handle)
close_library(handle);
_eglGlobal.Drivers[i] = NULL;
}
_eglGlobal.NumDrivers = 0;
}
/**
* Plug all the available fallback routines into the given driver's
* dispatch table.
*/
void
_eglInitDriverFallbacks(_EGLDriver *drv)
{
/* If a pointer is set to NULL, then the device driver _really_ has
* to implement it.
*/
drv->API.Initialize = NULL;
drv->API.Terminate = NULL;
drv->API.GetConfigs = _eglGetConfigs;
drv->API.ChooseConfig = _eglChooseConfig;
drv->API.GetConfigAttrib = _eglGetConfigAttrib;
drv->API.CreateContext = _eglCreateContext;
drv->API.DestroyContext = _eglDestroyContext;
drv->API.MakeCurrent = _eglMakeCurrent;
drv->API.QueryContext = _eglQueryContext;
drv->API.CreateWindowSurface = _eglCreateWindowSurface;
drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
drv->API.DestroySurface = _eglDestroySurface;
drv->API.QuerySurface = _eglQuerySurface;
drv->API.SurfaceAttrib = _eglSurfaceAttrib;
drv->API.BindTexImage = _eglBindTexImage;
drv->API.ReleaseTexImage = _eglReleaseTexImage;
drv->API.SwapInterval = _eglSwapInterval;
drv->API.SwapBuffers = _eglSwapBuffers;
drv->API.CopyBuffers = _eglCopyBuffers;
drv->API.QueryString = _eglQueryString;
drv->API.WaitClient = _eglWaitClient;
drv->API.WaitNative = _eglWaitNative;
#ifdef EGL_MESA_screen_surface
drv->API.ChooseModeMESA = _eglChooseModeMESA;
drv->API.GetModesMESA = _eglGetModesMESA;
drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
drv->API.GetScreensMESA = _eglGetScreensMESA;
drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
drv->API.QueryScreenMESA = _eglQueryScreenMESA;
drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
#endif /* EGL_MESA_screen_surface */
#ifdef EGL_VERSION_1_2
drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
#endif /* EGL_VERSION_1_2 */
#ifdef EGL_KHR_image_base
drv->API.CreateImageKHR = _eglCreateImageKHR;
drv->API.DestroyImageKHR = _eglDestroyImageKHR;
#endif /* EGL_KHR_image_base */
}
/**
* Set the probe cache at the given key.
*
* A key, instead of a _EGLDriver, is used to allow the probe cache to be share
* by multiple drivers.
*/
void
_eglSetProbeCache(EGLint key, const void *val)
{
EGLint idx;
for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
break;
}
assert(key > 0);
assert(idx < NUM_PROBE_CACHE_SLOTS);
_eglProbeCache.keys[idx] = key;
_eglProbeCache.values[idx] = val;
}
/**
* Return the probe cache at the given key.
*/
const void *
_eglGetProbeCache(EGLint key)
{
EGLint idx;
for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
break;
}
return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
_eglProbeCache.values[idx] : NULL;
}
|