1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
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
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 **************************************************************************/
32 * Functions for choosing and opening/loading device drivers.
40 #include "c11/threads.h"
42 #include "eglstring.h"
43 #include "egldefines.h"
44 #include "egldisplay.h"
45 #include "egldriver.h"
48 #ifdef _EGL_BUILT_IN_DRIVER_HAIKU
49 _EGLDriver
* _eglBuiltInDriverHaiku(const char* args
);
52 typedef struct _egl_module
{
58 static mtx_t _eglModuleMutex
= _MTX_INITIALIZER_NP
;
59 static _EGLArray
*_eglModules
;
64 } _eglBuiltInDrivers
[] = {
65 #ifdef _EGL_BUILT_IN_DRIVER_DRI2
66 { "egl_dri2", _eglBuiltInDriverDRI2
},
68 #ifdef _EGL_BUILT_IN_DRIVER_HAIKU
69 { "egl_haiku", _eglBuiltInDriverHaiku
},
75 * Load a module and create the driver object.
78 _eglLoadModule(_EGLModule
*mod
)
88 drv
= mod
->BuiltIn(NULL
);
89 if (!drv
|| !drv
->Name
)
102 _eglUnloadModule(_EGLModule
*mod
)
104 #if defined(_EGL_OS_UNIX)
105 /* destroy the driver */
106 if (mod
->Driver
&& mod
->Driver
->Unload
)
107 mod
->Driver
->Unload(mod
->Driver
);
109 #elif defined(_EGL_OS_WINDOWS)
110 /* XXX Windows unloads DLLs before atexit */
118 * Add a module to the module array.
121 _eglAddModule(const char *name
)
127 _eglModules
= _eglCreateArray("Module", 8);
132 /* find duplicates */
133 for (i
= 0; i
< _eglModules
->Size
; i
++) {
134 mod
= _eglModules
->Elements
[i
];
135 if (strcmp(mod
->Name
, name
) == 0)
139 /* allocate a new one */
140 mod
= calloc(1, sizeof(*mod
));
142 mod
->Name
= _eglstrdup(name
);
149 _eglAppendArray(_eglModules
, (void *) mod
);
150 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Name
);
161 _eglFreeModule(void *module
)
163 _EGLModule
*mod
= (_EGLModule
*) module
;
165 _eglUnloadModule(mod
);
172 * Add the user driver to the module array.
174 * The user driver is specified by EGL_DRIVER.
177 _eglAddUserDriver(void)
181 env
= getenv("EGL_DRIVER");
185 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
186 if (!strcmp(_eglBuiltInDrivers
[i
].name
, env
)) {
187 _EGLModule
*mod
= _eglAddModule(env
);
189 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
201 * Add built-in drivers to the module array.
204 _eglAddBuiltInDrivers(void)
209 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
210 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
212 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
218 * Add drivers to the module array. Drivers will be loaded as they are matched
227 if (!_eglAddUserDriver()) {
229 * Add other drivers only when EGL_DRIVER is not set. The order here
230 * decides the priorities.
232 _eglAddBuiltInDrivers();
235 return (_eglModules
!= NULL
);
240 * A helper function for _eglMatchDriver. It finds the first driver that can
241 * initialize the display and return.
244 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
246 _EGLDriver
*drv
= NULL
;
249 if (!_eglAddDrivers()) {
250 _eglLog(_EGL_WARNING
, "failed to find any driver");
256 /* no re-matching? */
257 if (!drv
->API
.Initialize(drv
, dpy
))
262 while (i
< _eglModules
->Size
) {
263 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
265 if (!_eglLoadModule(mod
)) {
266 /* remove invalid modules */
267 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
271 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
285 * Match a display to a driver. The display is initialized unless test_only is
286 * true. The matching is done by finding the first driver that can initialize
290 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
292 _EGLDriver
*best_drv
;
294 assert(!dpy
->Initialized
);
296 mtx_lock(&_eglModuleMutex
);
299 dpy
->Options
.TestOnly
= test_only
;
300 dpy
->Options
.UseFallback
= EGL_FALSE
;
302 best_drv
= _eglMatchAndInitialize(dpy
);
304 dpy
->Options
.UseFallback
= EGL_TRUE
;
305 best_drv
= _eglMatchAndInitialize(dpy
);
308 mtx_unlock(&_eglModuleMutex
);
311 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
312 best_drv
->Name
, (test_only
) ? " (test only) " : "");
314 dpy
->Driver
= best_drv
;
315 dpy
->Initialized
= EGL_TRUE
;
323 __eglMustCastToProperFunctionPointerType
324 _eglGetDriverProc(const char *procname
)
327 _EGLProc proc
= NULL
;
330 /* load the driver for the default display */
331 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
332 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
333 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
337 for (i
= 0; i
< _eglModules
->Size
; i
++) {
338 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
342 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
352 * Unload all drivers.
355 _eglUnloadDrivers(void)
357 /* this is called at atexit time */
359 _eglDestroyArray(_eglModules
, _eglFreeModule
);