mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2024-11-27 12:14:10 +08:00
egl: Simplify the "driver" interface
"Driver" isn't a great word for what this layer is, it's effectively a build-time choice about what OS you're targeting. Despite that both of the extant backends totally ignore the display argument, the old code would only set up the backend relative to a display. That causes problems! One problem is it means eglGetProcAddress can generate X or Wayland protocol when it tries to connect to a default display so it can call into the backend, which is, you know, completely bonkers. Any other EGL API that doesn't reference a display, like EGL_EXT_device_query, would have the same issue. Fortunately this is a problem that can be solved with the delete key. Reviewed-by: Eric Anholt <eric@anholt.net> Signed-off-by: Adam Jackson <ajax@redhat.com>
This commit is contained in:
parent
15e208c4cc
commit
b174a1ae72
@ -130,16 +130,6 @@ mesa/demos repository.</p>
|
||||
runtime</p>
|
||||
|
||||
<dl>
|
||||
<dt><code>EGL_DRIVER</code></dt>
|
||||
<dd>
|
||||
|
||||
<p>This variable specifies a full path to or the name of an EGL driver. It
|
||||
forces the specified EGL driver to be loaded. It comes in handy when one wants
|
||||
to test a specific driver. This variable is ignored for setuid/setgid
|
||||
binaries.</p>
|
||||
|
||||
</dd>
|
||||
|
||||
<dt><code>EGL_PLATFORM</code></dt>
|
||||
<dd>
|
||||
|
||||
|
@ -3193,12 +3193,10 @@ dri2_load(_EGLDriver *drv)
|
||||
* Create a new _EGLDriver object and init its dispatch table.
|
||||
*/
|
||||
_EGLDriver *
|
||||
_eglBuiltInDriverDRI2(const char *args)
|
||||
_eglBuiltInDriver(void)
|
||||
{
|
||||
struct dri2_egl_driver *dri2_drv;
|
||||
|
||||
(void) args;
|
||||
|
||||
dri2_drv = calloc(1, sizeof *dri2_drv);
|
||||
if (!dri2_drv)
|
||||
return NULL;
|
||||
|
@ -322,7 +322,7 @@ haiku_unload(_EGLDriver* drv)
|
||||
*/
|
||||
extern "C"
|
||||
_EGLDriver*
|
||||
_eglBuiltInDriverHaiku(const char *args)
|
||||
_eglBuiltInDriver(void)
|
||||
{
|
||||
CALLED();
|
||||
|
||||
|
@ -44,232 +44,32 @@
|
||||
#include "egldriver.h"
|
||||
#include "egllog.h"
|
||||
|
||||
typedef struct _egl_module {
|
||||
char *Name;
|
||||
_EGLMain_t BuiltIn;
|
||||
_EGLDriver *Driver;
|
||||
} _EGLModule;
|
||||
|
||||
static mtx_t _eglModuleMutex = _MTX_INITIALIZER_NP;
|
||||
static _EGLArray *_eglModules;
|
||||
static _EGLDriver *_eglDriver;
|
||||
|
||||
const struct {
|
||||
const char *name;
|
||||
_EGLMain_t main;
|
||||
} _eglBuiltInDrivers[] = {
|
||||
#ifdef _EGL_BUILT_IN_DRIVER_DRI2
|
||||
{ "egl_dri2", _eglBuiltInDriverDRI2 },
|
||||
#endif
|
||||
#ifdef _EGL_BUILT_IN_DRIVER_HAIKU
|
||||
{ "egl_haiku", _eglBuiltInDriverHaiku },
|
||||
#endif
|
||||
};
|
||||
|
||||
/**
|
||||
* Load a module and create the driver object.
|
||||
*/
|
||||
static EGLBoolean
|
||||
_eglLoadModule(_EGLModule *mod)
|
||||
static _EGLDriver *
|
||||
_eglGetDriver(void)
|
||||
{
|
||||
_EGLDriver *drv;
|
||||
mtx_lock(&_eglModuleMutex);
|
||||
|
||||
if (mod->Driver)
|
||||
return EGL_TRUE;
|
||||
if (!_eglDriver)
|
||||
_eglDriver = _eglBuiltInDriver();
|
||||
|
||||
if (!mod->BuiltIn)
|
||||
return EGL_FALSE;
|
||||
mtx_unlock(&_eglModuleMutex);
|
||||
|
||||
drv = mod->BuiltIn(NULL);
|
||||
if (!drv || !drv->Name)
|
||||
return EGL_FALSE;
|
||||
|
||||
mod->Driver = drv;
|
||||
|
||||
return EGL_TRUE;
|
||||
return _eglDriver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unload a module.
|
||||
*/
|
||||
static void
|
||||
_eglUnloadModule(_EGLModule *mod)
|
||||
{
|
||||
/* destroy the driver */
|
||||
if (mod->Driver && mod->Driver->Unload)
|
||||
mod->Driver->Unload(mod->Driver);
|
||||
|
||||
mod->Driver = NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a module to the module array.
|
||||
*/
|
||||
static _EGLModule *
|
||||
_eglAddModule(const char *name)
|
||||
{
|
||||
_EGLModule *mod;
|
||||
EGLint i;
|
||||
|
||||
if (!_eglModules) {
|
||||
_eglModules = _eglCreateArray("Module", 8);
|
||||
if (!_eglModules)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* find duplicates */
|
||||
for (i = 0; i < _eglModules->Size; i++) {
|
||||
mod = _eglModules->Elements[i];
|
||||
if (strcmp(mod->Name, name) == 0)
|
||||
return mod;
|
||||
}
|
||||
|
||||
/* allocate a new one */
|
||||
mod = calloc(1, sizeof(*mod));
|
||||
if (mod) {
|
||||
mod->Name = strdup(name);
|
||||
if (!mod->Name) {
|
||||
free(mod);
|
||||
mod = NULL;
|
||||
}
|
||||
}
|
||||
if (mod) {
|
||||
_eglAppendArray(_eglModules, (void *) mod);
|
||||
_eglLog(_EGL_DEBUG, "added %s to module array", mod->Name);
|
||||
}
|
||||
|
||||
return mod;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Free a module.
|
||||
*/
|
||||
static void
|
||||
_eglFreeModule(void *module)
|
||||
{
|
||||
_EGLModule *mod = (_EGLModule *) module;
|
||||
|
||||
_eglUnloadModule(mod);
|
||||
free(mod->Name);
|
||||
free(mod);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add the user driver to the module array.
|
||||
*
|
||||
* The user driver is specified by EGL_DRIVER.
|
||||
*/
|
||||
static EGLBoolean
|
||||
_eglAddUserDriver(void)
|
||||
{
|
||||
char *env;
|
||||
|
||||
env = getenv("EGL_DRIVER");
|
||||
if (env) {
|
||||
EGLint i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(_eglBuiltInDrivers); i++) {
|
||||
if (!strcmp(_eglBuiltInDrivers[i].name, env)) {
|
||||
_EGLModule *mod = _eglAddModule(env);
|
||||
if (mod)
|
||||
mod->BuiltIn = _eglBuiltInDrivers[i].main;
|
||||
|
||||
return EGL_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EGL_FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add built-in drivers to the module array.
|
||||
*/
|
||||
static void
|
||||
_eglAddBuiltInDrivers(void)
|
||||
{
|
||||
_EGLModule *mod;
|
||||
EGLint i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(_eglBuiltInDrivers); i++) {
|
||||
mod = _eglAddModule(_eglBuiltInDrivers[i].name);
|
||||
if (mod)
|
||||
mod->BuiltIn = _eglBuiltInDrivers[i].main;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add drivers to the module array. Drivers will be loaded as they are matched
|
||||
* to displays.
|
||||
*/
|
||||
static EGLBoolean
|
||||
_eglAddDrivers(void)
|
||||
{
|
||||
if (_eglModules)
|
||||
return EGL_TRUE;
|
||||
|
||||
if (!_eglAddUserDriver()) {
|
||||
/*
|
||||
* Add other drivers only when EGL_DRIVER is not set. The order here
|
||||
* decides the priorities.
|
||||
*/
|
||||
_eglAddBuiltInDrivers();
|
||||
}
|
||||
|
||||
return (_eglModules != NULL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A helper function for _eglMatchDriver. It finds the first driver that can
|
||||
* initialize the display and return.
|
||||
*/
|
||||
static _EGLDriver *
|
||||
_eglMatchAndInitialize(_EGLDisplay *dpy)
|
||||
{
|
||||
_EGLDriver *drv = NULL;
|
||||
EGLint i = 0;
|
||||
if (_eglGetDriver())
|
||||
if (_eglDriver->API.Initialize(_eglDriver, dpy))
|
||||
return _eglDriver;
|
||||
|
||||
if (!_eglAddDrivers()) {
|
||||
_eglLog(_EGL_WARNING, "failed to find any driver");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (dpy->Driver) {
|
||||
drv = dpy->Driver;
|
||||
/* no re-matching? */
|
||||
if (!drv->API.Initialize(drv, dpy))
|
||||
drv = NULL;
|
||||
return drv;
|
||||
}
|
||||
|
||||
while (i < _eglModules->Size) {
|
||||
_EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
|
||||
|
||||
if (!_eglLoadModule(mod)) {
|
||||
/* remove invalid modules */
|
||||
_eglEraseArray(_eglModules, i, _eglFreeModule);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mod->Driver->API.Initialize(mod->Driver, dpy)) {
|
||||
drv = mod->Driver;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return drv;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Match a display to a driver. The display is initialized unless test_only is
|
||||
* true. The matching is done by finding the first driver that can initialize
|
||||
@ -282,8 +82,6 @@ _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
|
||||
|
||||
assert(!dpy->Initialized);
|
||||
|
||||
mtx_lock(&_eglModuleMutex);
|
||||
|
||||
/* set options */
|
||||
dpy->Options.TestOnly = test_only;
|
||||
dpy->Options.UseFallback = EGL_FALSE;
|
||||
@ -294,8 +92,6 @@ _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
|
||||
best_drv = _eglMatchAndInitialize(dpy);
|
||||
}
|
||||
|
||||
mtx_unlock(&_eglModuleMutex);
|
||||
|
||||
if (best_drv) {
|
||||
_eglLog(_EGL_DEBUG, "the best driver is %s%s",
|
||||
best_drv->Name, (test_only) ? " (test only) " : "");
|
||||
@ -308,35 +104,15 @@ _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
|
||||
return best_drv;
|
||||
}
|
||||
|
||||
|
||||
__eglMustCastToProperFunctionPointerType
|
||||
_eglGetDriverProc(const char *procname)
|
||||
{
|
||||
EGLint i;
|
||||
_EGLProc proc = NULL;
|
||||
if (_eglGetDriver())
|
||||
return _eglDriver->API.GetProcAddress(_eglDriver, procname);
|
||||
|
||||
if (!_eglModules) {
|
||||
/* load the driver for the default display */
|
||||
EGLDisplay egldpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
_EGLDisplay *dpy = _eglLookupDisplay(egldpy);
|
||||
if (!dpy || !_eglMatchDriver(dpy, EGL_TRUE))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < _eglModules->Size; i++) {
|
||||
_EGLModule *mod = (_EGLModule *) _eglModules->Elements[i];
|
||||
|
||||
if (!mod->Driver)
|
||||
break;
|
||||
proc = mod->Driver->API.GetProcAddress(mod->Driver, procname);
|
||||
if (proc)
|
||||
break;
|
||||
}
|
||||
|
||||
return proc;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unload all drivers.
|
||||
*/
|
||||
@ -344,8 +120,8 @@ void
|
||||
_eglUnloadDrivers(void)
|
||||
{
|
||||
/* this is called at atexit time */
|
||||
if (_eglModules) {
|
||||
_eglDestroyArray(_eglModules, _eglFreeModule);
|
||||
_eglModules = NULL;
|
||||
}
|
||||
if (_eglDriver && _eglDriver->Unload)
|
||||
_eglDriver->Unload(_eglDriver);
|
||||
|
||||
_eglDriver = NULL;
|
||||
}
|
||||
|
@ -91,12 +91,8 @@ struct _egl_driver
|
||||
};
|
||||
|
||||
|
||||
extern _EGLDriver *
|
||||
_eglBuiltInDriverDRI2(const char *args);
|
||||
|
||||
|
||||
extern _EGLDriver*
|
||||
_eglBuiltInDriverHaiku(const char* args);
|
||||
_eglBuiltInDriver(void);
|
||||
|
||||
|
||||
extern _EGLDriver *
|
||||
|
Loading…
Reference in New Issue
Block a user