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 "egldefines.h"
43 #include "egldisplay.h"
44 #include "egldriver.h"
47 typedef struct _egl_module
{
53 static mtx_t _eglModuleMutex
= _MTX_INITIALIZER_NP
;
54 static _EGLArray
*_eglModules
;
59 } _eglBuiltInDrivers
[] = {
60 #ifdef _EGL_BUILT_IN_DRIVER_DRI2
61 { "egl_dri2", _eglBuiltInDriverDRI2
},
63 #ifdef _EGL_BUILT_IN_DRIVER_HAIKU
64 { "egl_haiku", _eglBuiltInDriverHaiku
},
70 * Load a module and create the driver object.
73 _eglLoadModule(_EGLModule
*mod
)
83 drv
= mod
->BuiltIn(NULL
);
84 if (!drv
|| !drv
->Name
)
97 _eglUnloadModule(_EGLModule
*mod
)
99 #if defined(_EGL_OS_UNIX)
100 /* destroy the driver */
101 if (mod
->Driver
&& mod
->Driver
->Unload
)
102 mod
->Driver
->Unload(mod
->Driver
);
104 #elif defined(_EGL_OS_WINDOWS)
105 /* XXX Windows unloads DLLs before atexit */
113 * Add a module to the module array.
116 _eglAddModule(const char *name
)
122 _eglModules
= _eglCreateArray("Module", 8);
127 /* find duplicates */
128 for (i
= 0; i
< _eglModules
->Size
; i
++) {
129 mod
= _eglModules
->Elements
[i
];
130 if (strcmp(mod
->Name
, name
) == 0)
134 /* allocate a new one */
135 mod
= calloc(1, sizeof(*mod
));
137 mod
->Name
= strdup(name
);
144 _eglAppendArray(_eglModules
, (void *) mod
);
145 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Name
);
156 _eglFreeModule(void *module
)
158 _EGLModule
*mod
= (_EGLModule
*) module
;
160 _eglUnloadModule(mod
);
167 * Add the user driver to the module array.
169 * The user driver is specified by EGL_DRIVER.
172 _eglAddUserDriver(void)
176 env
= getenv("EGL_DRIVER");
180 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
181 if (!strcmp(_eglBuiltInDrivers
[i
].name
, env
)) {
182 _EGLModule
*mod
= _eglAddModule(env
);
184 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
196 * Add built-in drivers to the module array.
199 _eglAddBuiltInDrivers(void)
204 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
205 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
207 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
213 * Add drivers to the module array. Drivers will be loaded as they are matched
222 if (!_eglAddUserDriver()) {
224 * Add other drivers only when EGL_DRIVER is not set. The order here
225 * decides the priorities.
227 _eglAddBuiltInDrivers();
230 return (_eglModules
!= NULL
);
235 * A helper function for _eglMatchDriver. It finds the first driver that can
236 * initialize the display and return.
239 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
241 _EGLDriver
*drv
= NULL
;
244 if (!_eglAddDrivers()) {
245 _eglLog(_EGL_WARNING
, "failed to find any driver");
251 /* no re-matching? */
252 if (!drv
->API
.Initialize(drv
, dpy
))
257 while (i
< _eglModules
->Size
) {
258 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
260 if (!_eglLoadModule(mod
)) {
261 /* remove invalid modules */
262 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
266 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
280 * Match a display to a driver. The display is initialized unless test_only is
281 * true. The matching is done by finding the first driver that can initialize
285 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
287 _EGLDriver
*best_drv
;
289 assert(!dpy
->Initialized
);
291 mtx_lock(&_eglModuleMutex
);
294 dpy
->Options
.TestOnly
= test_only
;
295 dpy
->Options
.UseFallback
= EGL_FALSE
;
297 best_drv
= _eglMatchAndInitialize(dpy
);
299 dpy
->Options
.UseFallback
= EGL_TRUE
;
300 best_drv
= _eglMatchAndInitialize(dpy
);
303 mtx_unlock(&_eglModuleMutex
);
306 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
307 best_drv
->Name
, (test_only
) ? " (test only) " : "");
309 dpy
->Driver
= best_drv
;
310 dpy
->Initialized
= EGL_TRUE
;
318 __eglMustCastToProperFunctionPointerType
319 _eglGetDriverProc(const char *procname
)
322 _EGLProc proc
= NULL
;
325 /* load the driver for the default display */
326 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
327 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
328 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
332 for (i
= 0; i
< _eglModules
->Size
; i
++) {
333 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
337 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
347 * Unload all drivers.
350 _eglUnloadDrivers(void)
352 /* this is called at atexit time */
354 _eglDestroyArray(_eglModules
, _eglFreeModule
);