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 /* destroy the driver */
100 if (mod
->Driver
&& mod
->Driver
->Unload
)
101 mod
->Driver
->Unload(mod
->Driver
);
108 * Add a module to the module array.
111 _eglAddModule(const char *name
)
117 _eglModules
= _eglCreateArray("Module", 8);
122 /* find duplicates */
123 for (i
= 0; i
< _eglModules
->Size
; i
++) {
124 mod
= _eglModules
->Elements
[i
];
125 if (strcmp(mod
->Name
, name
) == 0)
129 /* allocate a new one */
130 mod
= calloc(1, sizeof(*mod
));
132 mod
->Name
= strdup(name
);
139 _eglAppendArray(_eglModules
, (void *) mod
);
140 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Name
);
151 _eglFreeModule(void *module
)
153 _EGLModule
*mod
= (_EGLModule
*) module
;
155 _eglUnloadModule(mod
);
162 * Add the user driver to the module array.
164 * The user driver is specified by EGL_DRIVER.
167 _eglAddUserDriver(void)
171 env
= getenv("EGL_DRIVER");
175 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
176 if (!strcmp(_eglBuiltInDrivers
[i
].name
, env
)) {
177 _EGLModule
*mod
= _eglAddModule(env
);
179 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
191 * Add built-in drivers to the module array.
194 _eglAddBuiltInDrivers(void)
199 for (i
= 0; _eglBuiltInDrivers
[i
].name
; i
++) {
200 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
202 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
208 * Add drivers to the module array. Drivers will be loaded as they are matched
217 if (!_eglAddUserDriver()) {
219 * Add other drivers only when EGL_DRIVER is not set. The order here
220 * decides the priorities.
222 _eglAddBuiltInDrivers();
225 return (_eglModules
!= NULL
);
230 * A helper function for _eglMatchDriver. It finds the first driver that can
231 * initialize the display and return.
234 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
236 _EGLDriver
*drv
= NULL
;
239 if (!_eglAddDrivers()) {
240 _eglLog(_EGL_WARNING
, "failed to find any driver");
246 /* no re-matching? */
247 if (!drv
->API
.Initialize(drv
, dpy
))
252 while (i
< _eglModules
->Size
) {
253 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
255 if (!_eglLoadModule(mod
)) {
256 /* remove invalid modules */
257 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
261 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
275 * Match a display to a driver. The display is initialized unless test_only is
276 * true. The matching is done by finding the first driver that can initialize
280 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
282 _EGLDriver
*best_drv
;
284 assert(!dpy
->Initialized
);
286 mtx_lock(&_eglModuleMutex
);
289 dpy
->Options
.TestOnly
= test_only
;
290 dpy
->Options
.UseFallback
= EGL_FALSE
;
292 best_drv
= _eglMatchAndInitialize(dpy
);
294 dpy
->Options
.UseFallback
= EGL_TRUE
;
295 best_drv
= _eglMatchAndInitialize(dpy
);
298 mtx_unlock(&_eglModuleMutex
);
301 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
302 best_drv
->Name
, (test_only
) ? " (test only) " : "");
304 dpy
->Driver
= best_drv
;
305 dpy
->Initialized
= EGL_TRUE
;
313 __eglMustCastToProperFunctionPointerType
314 _eglGetDriverProc(const char *procname
)
317 _EGLProc proc
= NULL
;
320 /* load the driver for the default display */
321 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
322 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
323 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
327 for (i
= 0; i
< _eglModules
->Size
; i
++) {
328 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
332 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
342 * Unload all drivers.
345 _eglUnloadDrivers(void)
347 /* this is called at atexit time */
349 _eglDestroyArray(_eglModules
, _eglFreeModule
);