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
},
69 * Load a module and create the driver object.
72 _eglLoadModule(_EGLModule
*mod
)
82 drv
= mod
->BuiltIn(NULL
);
83 if (!drv
|| !drv
->Name
)
96 _eglUnloadModule(_EGLModule
*mod
)
98 /* destroy the driver */
99 if (mod
->Driver
&& mod
->Driver
->Unload
)
100 mod
->Driver
->Unload(mod
->Driver
);
107 * Add a module to the module array.
110 _eglAddModule(const char *name
)
116 _eglModules
= _eglCreateArray("Module", 8);
121 /* find duplicates */
122 for (i
= 0; i
< _eglModules
->Size
; i
++) {
123 mod
= _eglModules
->Elements
[i
];
124 if (strcmp(mod
->Name
, name
) == 0)
128 /* allocate a new one */
129 mod
= calloc(1, sizeof(*mod
));
131 mod
->Name
= strdup(name
);
138 _eglAppendArray(_eglModules
, (void *) mod
);
139 _eglLog(_EGL_DEBUG
, "added %s to module array", mod
->Name
);
150 _eglFreeModule(void *module
)
152 _EGLModule
*mod
= (_EGLModule
*) module
;
154 _eglUnloadModule(mod
);
161 * Add the user driver to the module array.
163 * The user driver is specified by EGL_DRIVER.
166 _eglAddUserDriver(void)
170 env
= getenv("EGL_DRIVER");
174 for (i
= 0; i
< ARRAY_SIZE(_eglBuiltInDrivers
); i
++) {
175 if (!strcmp(_eglBuiltInDrivers
[i
].name
, env
)) {
176 _EGLModule
*mod
= _eglAddModule(env
);
178 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
190 * Add built-in drivers to the module array.
193 _eglAddBuiltInDrivers(void)
198 for (i
= 0; i
< ARRAY_SIZE(_eglBuiltInDrivers
); i
++) {
199 mod
= _eglAddModule(_eglBuiltInDrivers
[i
].name
);
201 mod
->BuiltIn
= _eglBuiltInDrivers
[i
].main
;
207 * Add drivers to the module array. Drivers will be loaded as they are matched
216 if (!_eglAddUserDriver()) {
218 * Add other drivers only when EGL_DRIVER is not set. The order here
219 * decides the priorities.
221 _eglAddBuiltInDrivers();
224 return (_eglModules
!= NULL
);
229 * A helper function for _eglMatchDriver. It finds the first driver that can
230 * initialize the display and return.
233 _eglMatchAndInitialize(_EGLDisplay
*dpy
)
235 _EGLDriver
*drv
= NULL
;
238 if (!_eglAddDrivers()) {
239 _eglLog(_EGL_WARNING
, "failed to find any driver");
245 /* no re-matching? */
246 if (!drv
->API
.Initialize(drv
, dpy
))
251 while (i
< _eglModules
->Size
) {
252 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
254 if (!_eglLoadModule(mod
)) {
255 /* remove invalid modules */
256 _eglEraseArray(_eglModules
, i
, _eglFreeModule
);
260 if (mod
->Driver
->API
.Initialize(mod
->Driver
, dpy
)) {
274 * Match a display to a driver. The display is initialized unless test_only is
275 * true. The matching is done by finding the first driver that can initialize
279 _eglMatchDriver(_EGLDisplay
*dpy
, EGLBoolean test_only
)
281 _EGLDriver
*best_drv
;
283 assert(!dpy
->Initialized
);
285 mtx_lock(&_eglModuleMutex
);
288 dpy
->Options
.TestOnly
= test_only
;
289 dpy
->Options
.UseFallback
= EGL_FALSE
;
291 best_drv
= _eglMatchAndInitialize(dpy
);
293 dpy
->Options
.UseFallback
= EGL_TRUE
;
294 best_drv
= _eglMatchAndInitialize(dpy
);
297 mtx_unlock(&_eglModuleMutex
);
300 _eglLog(_EGL_DEBUG
, "the best driver is %s%s",
301 best_drv
->Name
, (test_only
) ? " (test only) " : "");
303 dpy
->Driver
= best_drv
;
304 dpy
->Initialized
= EGL_TRUE
;
312 __eglMustCastToProperFunctionPointerType
313 _eglGetDriverProc(const char *procname
)
316 _EGLProc proc
= NULL
;
319 /* load the driver for the default display */
320 EGLDisplay egldpy
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
321 _EGLDisplay
*dpy
= _eglLookupDisplay(egldpy
);
322 if (!dpy
|| !_eglMatchDriver(dpy
, EGL_TRUE
))
326 for (i
= 0; i
< _eglModules
->Size
; i
++) {
327 _EGLModule
*mod
= (_EGLModule
*) _eglModules
->Elements
[i
];
331 proc
= mod
->Driver
->API
.GetProcAddress(mod
->Driver
, procname
);
341 * Unload all drivers.
344 _eglUnloadDrivers(void)
346 /* this is called at atexit time */
348 _eglDestroyArray(_eglModules
, _eglFreeModule
);