summaryrefslogtreecommitdiff
path: root/src/egl/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/egl/main')
-rw-r--r--src/egl/main/Makefile2
-rw-r--r--src/egl/main/eglapi.c1
-rw-r--r--src/egl/main/eglcompiler.h33
-rw-r--r--src/egl/main/eglconfig.c2
-rw-r--r--src/egl/main/eglcontext.c106
-rw-r--r--src/egl/main/eglcontext.h17
-rw-r--r--src/egl/main/eglcurrent.c125
-rw-r--r--src/egl/main/eglcurrent.h8
-rw-r--r--src/egl/main/egldisplay.c114
-rw-r--r--src/egl/main/egldisplay.h26
-rw-r--r--src/egl/main/eglglobals.c66
-rw-r--r--src/egl/main/eglglobals.h17
-rw-r--r--src/egl/main/eglmutex.h52
-rw-r--r--src/egl/main/eglsurface.c5
-rw-r--r--src/egl/main/eglsurface.h19
15 files changed, 428 insertions, 165 deletions
diff --git a/src/egl/main/Makefile b/src/egl/main/Makefile
index 7cab005214..ab61d68f2b 100644
--- a/src/egl/main/Makefile
+++ b/src/egl/main/Makefile
@@ -20,6 +20,7 @@ HEADERS = \
eglhash.h \
eglmisc.h \
eglmode.h \
+ eglmutex.h \
eglscreen.h \
eglstring.h \
eglsurface.h \
@@ -74,6 +75,7 @@ install: default
clean:
-rm -f *.o *.so*
-rm -f core.*
+ -rm -f depend depend.bak
depend: $(SOURCES) $(HEADERS)
diff --git a/src/egl/main/eglapi.c b/src/egl/main/eglapi.c
index f0a6f7f935..fde6b7316c 100644
--- a/src/egl/main/eglapi.c
+++ b/src/egl/main/eglapi.c
@@ -50,7 +50,6 @@ EGLDisplay EGLAPIENTRY
eglGetDisplay(NativeDisplayType nativeDisplay)
{
_EGLDisplay *dpy;
- _eglInitGlobals();
dpy = _eglFindDisplay(nativeDisplay);
if (!dpy) {
dpy = _eglNewDisplay(nativeDisplay);
diff --git a/src/egl/main/eglcompiler.h b/src/egl/main/eglcompiler.h
index 0b19afedfd..6b639b75c6 100644
--- a/src/egl/main/eglcompiler.h
+++ b/src/egl/main/eglcompiler.h
@@ -3,6 +3,39 @@
/**
+ * Get standard integer types
+ */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
+# include <stdint.h>
+#elif defined(_MSC_VER)
+ typedef __int8 int8_t;
+ typedef unsigned __int8 uint8_t;
+ typedef __int16 int16_t;
+ typedef unsigned __int16 uint16_t;
+# ifndef __eglplatform_h_
+ typedef __int32 int32_t;
+# endif
+ typedef unsigned __int32 uint32_t;
+ typedef __int64 int64_t;
+ typedef unsigned __int64 uint64_t;
+
+# if defined(_WIN64)
+ typedef __int64 intptr_t;
+ typedef unsigned __int64 uintptr_t;
+# else
+ typedef __int32 intptr_t;
+ typedef unsigned __int32 uintptr_t;
+# endif
+
+# define INT64_C(__val) __val##i64
+# define UINT64_C(__val) __val##ui64
+#else
+/* hope the best instead of adding a bunch of ifdef's */
+# include <stdint.h>
+#endif
+
+
+/**
* Function inlining
*/
#if defined(__GNUC__)
diff --git a/src/egl/main/eglconfig.c b/src/egl/main/eglconfig.c
index f2f32585c7..bbc585b55e 100644
--- a/src/egl/main/eglconfig.c
+++ b/src/egl/main/eglconfig.c
@@ -34,7 +34,7 @@ void
_eglInitConfig(_EGLConfig *config, EGLint id)
{
memset(config, 0, sizeof(*config));
- config->Handle = (EGLConfig) id;
+ config->Handle = (EGLConfig) _eglUIntToPointer((unsigned int) id);
_eglSetConfigAttrib(config, EGL_CONFIG_ID, id);
_eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGB, EGL_DONT_CARE);
_eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGBA, EGL_DONT_CARE);
diff --git a/src/egl/main/eglcontext.c b/src/egl/main/eglcontext.c
index 9ab4286d3a..88de60d69b 100644
--- a/src/egl/main/eglcontext.c
+++ b/src/egl/main/eglcontext.c
@@ -96,7 +96,7 @@ _eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
_EGLContext *context = _eglLookupContext(ctx);
if (context) {
_eglUnlinkContext(context);
- if (!context->IsBound)
+ if (!_eglIsContextBound(context))
free(context);
return EGL_TRUE;
}
@@ -146,10 +146,11 @@ _eglQueryContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx,
* Then, the driver will do its device-dependent Make-Current stuff.
*/
EGLBoolean
-_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
+_eglMakeCurrent(_EGLDriver *drv, EGLDisplay display, EGLSurface d,
EGLSurface r, EGLContext context)
{
_EGLThreadInfo *t = _eglGetCurrentThread();
+ _EGLDisplay *dpy = _eglLookupDisplay(display);
_EGLContext *ctx = _eglLookupContext(context);
_EGLSurface *draw = _eglLookupSurface(d);
_EGLSurface *read = _eglLookupSurface(r);
@@ -160,21 +161,23 @@ _eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
if (_eglIsCurrentThreadDummy())
return _eglError(EGL_BAD_ALLOC, "eglMakeCurrent");
+ if (dpy == NULL)
+ return _eglError(EGL_BAD_DISPLAY, "eglMakeCurrent");
- /* error checking */
if (ctx) {
+ /* error checking */
+ if (ctx->Binding && ctx->Binding != t)
+ return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
if (draw == NULL || read == NULL) {
- _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
- return EGL_FALSE;
- }
- if (draw->Config != ctx->Config) {
- _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
- return EGL_FALSE;
- }
- if (read->Config != ctx->Config) {
- _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
- return EGL_FALSE;
+ EGLint err = (d == EGL_NO_SURFACE || r == EGL_NO_SURFACE)
+ ? EGL_BAD_MATCH : EGL_BAD_SURFACE;
+ return _eglError(err, "eglMakeCurrent");
}
+ if (draw->Config != ctx->Config || read->Config != ctx->Config)
+ return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ if ((draw->Binding && draw->Binding->Binding != t) ||
+ (read->Binding && read->Binding->Binding != t))
+ return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
#ifdef EGL_VERSION_1_4
/* OpenGL and OpenGL ES are conflicting */
@@ -194,6 +197,10 @@ _eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
apiIndex = _eglConvertApiToIndex(ctx->ClientAPI);
}
else {
+ if (context != EGL_NO_CONTEXT)
+ return _eglError(EGL_BAD_CONTEXT, "eglMakeCurrent");
+ if (draw != NULL || read != NULL)
+ return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
apiIndex = t->CurrentAPIIndex;
}
@@ -201,60 +208,47 @@ _eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
if (oldContext) {
oldDrawSurface = oldContext->DrawSurface;
oldReadSurface = oldContext->ReadSurface;
- }
+ assert(oldDrawSurface);
+ assert(oldReadSurface);
- /*
- * check if the old context or surfaces need to be deleted
- */
- if (oldDrawSurface != NULL) {
- oldDrawSurface->IsBound = EGL_FALSE;
+ /* break old bindings */
+ t->CurrentContexts[apiIndex] = NULL;
+ oldContext->Binding = NULL;
+ oldContext->DrawSurface = NULL;
+ oldContext->ReadSurface = NULL;
+ oldDrawSurface->Binding = NULL;
+ oldReadSurface->Binding = NULL;
+
+ /*
+ * check if the old context or surfaces need to be deleted
+ * FIXME They are linked so that they can be unlinked. This is ugly.
+ */
if (!_eglIsSurfaceLinked(oldDrawSurface)) {
- /* make sure we don't try to rebind a deleted surface */
- if (draw == oldDrawSurface || draw == oldReadSurface) {
- draw = NULL;
- }
- /* really delete surface now */
- drv->API.DestroySurface(drv, dpy, oldDrawSurface->Handle);
+ assert(draw != oldDrawSurface && read != oldDrawSurface);
+ drv->API.DestroySurface(drv, display,
+ _eglLinkSurface(oldDrawSurface, dpy));
}
- }
- if (oldReadSurface != NULL && oldReadSurface != oldDrawSurface) {
- oldReadSurface->IsBound = EGL_FALSE;
- if (!_eglIsSurfaceLinked(oldReadSurface)) {
- /* make sure we don't try to rebind a deleted surface */
- if (read == oldDrawSurface || read == oldReadSurface) {
- read = NULL;
- }
- /* really delete surface now */
- drv->API.DestroySurface(drv, dpy, oldReadSurface->Handle);
+ if (oldReadSurface != oldDrawSurface &&
+ !_eglIsSurfaceLinked(oldReadSurface)) {
+ assert(draw != oldReadSurface && read != oldReadSurface);
+ drv->API.DestroySurface(drv, display,
+ _eglLinkSurface(oldReadSurface, dpy));
}
- }
- if (oldContext != NULL) {
- oldContext->IsBound = EGL_FALSE;
if (!_eglIsContextLinked(oldContext)) {
- /* make sure we don't try to rebind a deleted context */
- if (ctx == oldContext) {
- ctx = NULL;
- }
- /* really delete context now */
- drv->API.DestroyContext(drv, dpy, _eglGetContextHandle(oldContext));
+ assert(ctx != oldContext);
+ drv->API.DestroyContext(drv, display,
+ _eglLinkContext(oldContext, dpy));
}
}
+ /* build new bindings */
if (ctx) {
- /* check read/draw again, in case we deleted them above */
- if (draw == NULL || read == NULL) {
- _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
- return EGL_FALSE;
- }
+ t->CurrentContexts[apiIndex] = ctx;
+ ctx->Binding = t;
ctx->DrawSurface = draw;
ctx->ReadSurface = read;
- ctx->IsBound = EGL_TRUE;
- draw->IsBound = EGL_TRUE;
- read->IsBound = EGL_TRUE;
- t->CurrentContexts[apiIndex] = ctx;
- }
- else {
- t->CurrentContexts[apiIndex] = NULL;
+ draw->Binding = ctx;
+ read->Binding = ctx;
}
return EGL_TRUE;
diff --git a/src/egl/main/eglcontext.h b/src/egl/main/eglcontext.h
index 2fb28d38b9..4276c0980e 100644
--- a/src/egl/main/eglcontext.h
+++ b/src/egl/main/eglcontext.h
@@ -15,12 +15,12 @@ struct _egl_context
_EGLDisplay *Display;
_EGLContext *Next;
- _EGLConfig *Config;
-
+ /* The bound status of the context */
+ _EGLThreadInfo *Binding;
_EGLSurface *DrawSurface;
_EGLSurface *ReadSurface;
- EGLBoolean IsBound;
+ _EGLConfig *Config;
EGLint ClientAPI; /**< EGL_OPENGL_ES_API, EGL_OPENGL_API, EGL_OPENVG_API */
EGLint ClientVersion; /**< 1 = OpenGLES 1.x, 2 = OpenGLES 2.x */
@@ -51,4 +51,15 @@ _eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface rea
extern EGLBoolean
_eglCopyContextMESA(_EGLDriver *drv, EGLDisplay dpy, EGLContext source, EGLContext dest, EGLint mask);
+
+/**
+ * Return true if the context is bound to a thread.
+ */
+static INLINE EGLBoolean
+_eglIsContextBound(_EGLContext *ctx)
+{
+ return (ctx->Binding != NULL);
+}
+
+
#endif /* EGLCONTEXT_INCLUDED */
diff --git a/src/egl/main/eglcurrent.c b/src/egl/main/eglcurrent.c
index 96152db19f..4431f964f6 100644
--- a/src/egl/main/eglcurrent.c
+++ b/src/egl/main/eglcurrent.c
@@ -3,61 +3,127 @@
#include "eglcurrent.h"
#include "eglcontext.h"
#include "egllog.h"
+#include "eglmutex.h"
+#include "eglglobals.h"
+/* This should be kept in sync with _eglInitThreadInfo() */
+#define _EGL_THREAD_INFO_INITIALIZER \
+ { EGL_SUCCESS, { NULL }, 1 }
+
/* a fallback thread info to guarantee that every thread always has one */
-static _EGLThreadInfo dummy_thread;
+static _EGLThreadInfo dummy_thread = _EGL_THREAD_INFO_INITIALIZER;
#ifdef GLX_USE_TLS
static __thread const _EGLThreadInfo *_egl_TSD;
__attribute__ ((tls_model("initial-exec")));
-static INLINE EGLBoolean _eglInitTSD(void) { return EGL_TRUE; }
-static INLINE void _eglFiniTSD(void) { }
-static INLINE void _eglSetTSD(const _EGLThreadInfo *t) { _egl_TSD = t; }
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+{
+ _egl_TSD = t;
+}
static INLINE _EGLThreadInfo *_eglGetTSD(void)
{
return (_EGLThreadInfo *) _egl_TSD;
}
+static INLINE void _eglFiniTSD(void)
+{
+}
+
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
+{
+ /* TODO destroy TSD */
+ (void) dtor;
+ (void) _eglFiniTSD;
+ return EGL_TRUE;
+}
+
#elif PTHREADS
#include <pthread.h>
+static _EGL_DECLARE_MUTEX(_egl_TSDMutex);
+static EGLBoolean _egl_TSDInitialized;
static pthread_key_t _egl_TSD;
+static void (*_egl_FreeTSD)(_EGLThreadInfo *);
-static INLINE EGLBoolean _eglInitTSD(void)
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
{
- return (pthread_key_create(&_egl_TSD, NULL) == 0);
+ pthread_setspecific(_egl_TSD, (const void *) t);
}
-static INLINE void _eglFiniTSD(void)
+static INLINE _EGLThreadInfo *_eglGetTSD(void)
{
- pthread_key_delete(_egl_TSD);
+ return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD);
}
-static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+static INLINE void _eglFiniTSD(void)
{
- pthread_setspecific(_egl_TSD, (const void *) t);
+ _eglLockMutex(&_egl_TSDMutex);
+ if (_egl_TSDInitialized) {
+ _EGLThreadInfo *t = _eglGetTSD();
+
+ _egl_TSDInitialized = EGL_FALSE;
+ if (t && _egl_FreeTSD)
+ _egl_FreeTSD((void *) t);
+ pthread_key_delete(_egl_TSD);
+ }
+ _eglUnlockMutex(&_egl_TSDMutex);
}
-static INLINE _EGLThreadInfo *_eglGetTSD(void)
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
{
- return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD);
+ if (!_egl_TSDInitialized) {
+ _eglLockMutex(&_egl_TSDMutex);
+
+ /* check again after acquiring lock */
+ if (!_egl_TSDInitialized) {
+ if (pthread_key_create(&_egl_TSD, (void (*)(void *)) dtor) != 0) {
+ _eglUnlockMutex(&_egl_TSDMutex);
+ return EGL_FALSE;
+ }
+ _egl_FreeTSD = dtor;
+ _eglAddAtExitCall(_eglFiniTSD);
+ _egl_TSDInitialized = EGL_TRUE;
+ }
+
+ _eglUnlockMutex(&_egl_TSDMutex);
+ }
+
+ return EGL_TRUE;
}
#else /* PTHREADS */
static const _EGLThreadInfo *_egl_TSD;
+static void (*_egl_FreeTSD)(_EGLThreadInfo *);
-static INLINE EGLBoolean _eglInitTSD(void) { return EGL_TRUE; }
-static INLINE void _eglFiniTSD(void) { }
-static INLINE void _eglSetTSD(const _EGLThreadInfo *t) { _egl_TSD = t; }
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+{
+ _egl_TSD = t;
+}
static INLINE _EGLThreadInfo *_eglGetTSD(void)
{
return (_EGLThreadInfo *) _egl_TSD;
}
+
+static INLINE void _eglFiniTSD(void)
+{
+ if (_egl_FreeTSD && _egl_TSD)
+ _egl_FreeTSD((_EGLThreadInfo *) _egl_TSD);
+}
+
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
+{
+ if (!_egl_FreeTSD && dtor) {
+ _egl_FreeTSD = dtor;
+ _eglAddAtExitCall(_eglFiniTSD);
+ }
+ return EGL_TRUE;
+}
+
#endif /* !PTHREADS */
@@ -98,24 +164,17 @@ _eglDestroyThreadInfo(_EGLThreadInfo *t)
/**
- * Initialize "current thread" management.
+ * Make sure TSD is initialized and return current value.
*/
-EGLBoolean
-_eglInitCurrent(void)
+static INLINE _EGLThreadInfo *
+_eglCheckedGetTSD(void)
{
- _eglInitThreadInfo(&dummy_thread);
- return _eglInitTSD();
-}
-
+ if (_eglInitTSD(&_eglDestroyThreadInfo) != EGL_TRUE) {
+ _eglLog(_EGL_FATAL, "failed to initialize \"current\" system");
+ return NULL;
+ }
-/**
- * Finish "current thread" management.
- */
-void
-_eglFiniCurrent(void)
-{
- /* TODO trace and release all threads... */
- _eglFiniTSD();
+ return _eglGetTSD();
}
@@ -129,7 +188,7 @@ _eglFiniCurrent(void)
_EGLThreadInfo *
_eglGetCurrentThread(void)
{
- _EGLThreadInfo *t = _eglGetTSD();
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
if (!t) {
t = _eglCreateThreadInfo();
_eglSetTSD(t);
@@ -145,7 +204,7 @@ _eglGetCurrentThread(void)
void
_eglDestroyCurrentThread(void)
{
- _EGLThreadInfo *t = _eglGetTSD();
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
if (t) {
_eglDestroyThreadInfo(t);
_eglSetTSD(NULL);
@@ -162,7 +221,7 @@ _eglDestroyCurrentThread(void)
EGLBoolean
_eglIsCurrentThreadDummy(void)
{
- _EGLThreadInfo *t = _eglGetTSD();
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
return (!t || t == &dummy_thread);
}
diff --git a/src/egl/main/eglcurrent.h b/src/egl/main/eglcurrent.h
index f9fdf7bd0f..8eb241029e 100644
--- a/src/egl/main/eglcurrent.h
+++ b/src/egl/main/eglcurrent.h
@@ -20,14 +20,6 @@ struct _egl_thread_info
};
-extern EGLBoolean
-_eglInitCurrent(void);
-
-
-extern void
-_eglFiniCurrent(void);
-
-
/**
* Return true if a client API enum can be converted to an index.
*/
diff --git a/src/egl/main/egldisplay.c b/src/egl/main/egldisplay.c
index 89de609d0b..feae1d6040 100644
--- a/src/egl/main/egldisplay.c
+++ b/src/egl/main/egldisplay.c
@@ -1,4 +1,3 @@
-
/**
* Functions related to EGLDisplay.
*/
@@ -13,6 +12,67 @@
#include "eglglobals.h"
#include "eglhash.h"
#include "eglstring.h"
+#include "eglmutex.h"
+#include "egllog.h"
+
+
+static _EGL_DECLARE_MUTEX(_eglDisplayInitMutex);
+static _EGLHashtable *_eglDisplayHash;
+/* TODO surface hash table should be per-display */
+static _EGLHashtable *_eglSurfaceHash;
+
+
+/**
+ * Finish display management.
+ */
+static void
+_eglFiniDisplay(void)
+{
+ _eglLockMutex(&_eglDisplayInitMutex);
+ if (_eglDisplayHash) {
+ EGLuint key = _eglHashFirstEntry(_eglDisplayHash);
+
+ while (key) {
+ _EGLDisplay *dpy = (_EGLDisplay *)
+ _eglHashLookup(_eglDisplayHash, key);
+ assert(dpy);
+
+ if (dpy->ContextList || dpy->SurfaceList)
+ _eglLog(_EGL_DEBUG, "Display %u is destroyed with resources", key);
+
+ _eglCleanupDisplay(dpy);
+ free(dpy);
+
+ key = _eglHashNextEntry(_eglDisplayHash, key);
+ }
+
+ _eglDeleteHashTable(_eglDisplayHash);
+ _eglDisplayHash = NULL;
+ _eglDeleteHashTable(_eglSurfaceHash);
+ _eglSurfaceHash = NULL;
+ }
+ _eglUnlockMutex(&_eglDisplayInitMutex);
+}
+
+
+/* This can be avoided if hash table can be statically initialized */
+static INLINE void
+_eglInitDisplay(void)
+{
+ if (!_eglDisplayHash) {
+ _eglLockMutex(&_eglDisplayInitMutex);
+
+ /* check again after acquiring lock */
+ if (!_eglDisplayHash) {
+ _eglDisplayHash = _eglNewHashTable();
+ _eglSurfaceHash = _eglNewHashTable();
+
+ _eglAddAtExitCall(_eglFiniDisplay);
+ }
+
+ _eglUnlockMutex(&_eglDisplayInitMutex);
+ }
+}
/**
@@ -31,6 +91,9 @@ _eglNewDisplay(NativeDisplayType nativeDisplay)
dpy->Xdpy = (Display *) nativeDisplay;
#endif
+ _eglInitDisplay();
+ dpy->SurfaceHash = _eglSurfaceHash;
+
dpy->DriverName = _eglChooseDriver(dpy);
if (!dpy->DriverName) {
free(dpy);
@@ -49,11 +112,14 @@ EGLDisplay
_eglLinkDisplay(_EGLDisplay *dpy)
{
EGLuint key;
- key = _eglHashGenKey(_eglGlobal.Displays);
+
+ _eglInitDisplay();
+
+ key = _eglHashGenKey(_eglDisplayHash);
assert(key);
/* "link" the display to the hash table */
- _eglHashInsert(_eglGlobal.Displays, key, dpy);
- dpy->Handle = (EGLDisplay) key;
+ _eglHashInsert(_eglDisplayHash, key, dpy);
+ dpy->Handle = (EGLDisplay) _eglUIntToPointer(key);
return dpy->Handle;
}
@@ -66,7 +132,11 @@ _eglLinkDisplay(_EGLDisplay *dpy)
void
_eglUnlinkDisplay(_EGLDisplay *dpy)
{
- _eglHashRemove(_eglGlobal.Displays, (EGLuint) dpy->Handle);
+ EGLuint key = _eglPointerToUInt((void *) dpy->Handle);
+
+ _eglInitDisplay();
+
+ _eglHashRemove(_eglDisplayHash, key);
dpy->Handle = EGL_NO_DISPLAY;
}
@@ -83,7 +153,7 @@ _eglGetDisplayHandle(_EGLDisplay *display)
return EGL_NO_DISPLAY;
}
-
+
/**
* Lookup a handle to find the linked display.
* Return NULL if the handle has no corresponding linked display.
@@ -91,8 +161,11 @@ _eglGetDisplayHandle(_EGLDisplay *display)
_EGLDisplay *
_eglLookupDisplay(EGLDisplay dpy)
{
- EGLuint key = (EGLuint) dpy;
- return (_EGLDisplay *) _eglHashLookup(_eglGlobal.Displays, key);
+ EGLuint key = _eglPointerToUInt((void *) dpy);
+
+ _eglInitDisplay();
+
+ return (_EGLDisplay *) _eglHashLookup(_eglDisplayHash, key);
}
@@ -103,17 +176,20 @@ _eglLookupDisplay(EGLDisplay dpy)
_EGLDisplay *
_eglFindDisplay(NativeDisplayType nativeDisplay)
{
- EGLuint key = _eglHashFirstEntry(_eglGlobal.Displays);
+ EGLuint key;
+
+ _eglInitDisplay();
/* Walk the hash table. Should switch to list if it is a problem. */
+ key = _eglHashFirstEntry(_eglDisplayHash);
while (key) {
_EGLDisplay *dpy = (_EGLDisplay *)
- _eglHashLookup(_eglGlobal.Displays, key);
+ _eglHashLookup(_eglDisplayHash, key);
assert(dpy);
if (dpy->NativeDisplay == nativeDisplay)
return dpy;
- key = _eglHashNextEntry(_eglGlobal.Displays, key);
+ key = _eglHashNextEntry(_eglDisplayHash, key);
}
return NULL;
@@ -224,7 +300,7 @@ _eglUnlinkContext(_EGLContext *ctx)
EGLContext
_eglGetContextHandle(_EGLContext *ctx)
{
- return (EGLContext) (ctx && ctx->Display) ? ctx : EGL_NO_CONTEXT;
+ return (EGLContext) ((ctx && ctx->Display) ? ctx : EGL_NO_CONTEXT);
}
@@ -253,11 +329,11 @@ _eglLinkSurface(_EGLSurface *surf, _EGLDisplay *dpy)
surf->Next = dpy->SurfaceList;
dpy->SurfaceList = surf;
- key = _eglHashGenKey(_eglGlobal.Surfaces);
+ key = _eglHashGenKey(dpy->SurfaceHash);
assert(key);
- _eglHashInsert(_eglGlobal.Surfaces, key, surf);
+ _eglHashInsert(dpy->SurfaceHash, key, surf);
- surf->Handle = (EGLSurface) key;
+ surf->Handle = (EGLSurface) _eglUIntToPointer(key);
return surf->Handle;
}
@@ -270,8 +346,9 @@ void
_eglUnlinkSurface(_EGLSurface *surf)
{
_EGLSurface *prev;
+ EGLuint key = _eglPointerToUInt((void *) surf->Handle);
- _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surf->Handle);
+ _eglHashRemove(surf->Display->SurfaceHash, key);
surf->Handle = EGL_NO_SURFACE;
prev = surf->Display->SurfaceList;
@@ -314,7 +391,6 @@ _eglGetSurfaceHandle(_EGLSurface *surface)
_EGLSurface *
_eglLookupSurface(EGLSurface surf)
{
- _EGLSurface *c = (_EGLSurface *) _eglHashLookup(_eglGlobal.Surfaces,
- (EGLuint) surf);
- return c;
+ EGLuint key = _eglPointerToUInt((void *) surf);
+ return (_EGLSurface *) _eglHashLookup(_eglSurfaceHash, key);
}
diff --git a/src/egl/main/egldisplay.h b/src/egl/main/egldisplay.h
index 372ed3cd79..70c59ef5e4 100644
--- a/src/egl/main/egldisplay.h
+++ b/src/egl/main/egldisplay.h
@@ -6,6 +6,7 @@
#endif
#include "egltypedefs.h"
+#include "eglhash.h"
struct _egl_display
@@ -26,6 +27,10 @@ struct _egl_display
/* lists of linked contexts and surface */
_EGLContext *ContextList;
_EGLSurface *SurfaceList;
+
+ /* hash table to map surfaces to handles */
+ _EGLHashtable *SurfaceHash;
+
#ifdef _EGL_PLATFORM_X
Display *Xdpy;
#endif
@@ -125,4 +130,25 @@ _eglIsSurfaceLinked(_EGLSurface *surf)
}
+/**
+ * Cast an unsigned int to a pointer.
+ */
+static INLINE void *
+_eglUIntToPointer(unsigned int v)
+{
+ return (void *) ((uintptr_t) v);
+}
+
+
+/**
+ * Cast a pointer to an unsigned int. The pointer must be one that is
+ * returned by _eglUIntToPointer.
+ */
+static INLINE unsigned int
+_eglPointerToUInt(const void *p)
+{
+ return (unsigned int) ((uintptr_t) p);
+}
+
+
#endif /* EGLDISPLAY_INCLUDED */
diff --git a/src/egl/main/eglglobals.c b/src/egl/main/eglglobals.c
index 55de394ef5..e93b48e03b 100644
--- a/src/egl/main/eglglobals.c
+++ b/src/egl/main/eglglobals.c
@@ -1,41 +1,53 @@
#include <stdlib.h>
+#include <assert.h>
#include "eglglobals.h"
+#include "egldisplay.h"
#include "egllog.h"
+#include "eglmutex.h"
-struct _egl_global _eglGlobal =
-{
- EGL_FALSE
-};
-/**
- * Init the fields in the _eglGlobal struct
- * May be safely called more than once.
- */
-void
-_eglInitGlobals(void)
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+
+static _EGL_DECLARE_MUTEX(_eglGlobalMutex);
+struct _egl_global _eglGlobal =
{
- if (!_eglGlobal.Initialized) {
- _eglGlobal.Displays = _eglNewHashTable();
- _eglGlobal.Surfaces = _eglNewHashTable();
- _eglGlobal.FreeScreenHandle = 1;
- _eglGlobal.Initialized = EGL_TRUE;
+ &_eglGlobalMutex, /* Mutex */
+ 1, /* FreeScreenHandle */
+ 0x0, /* ClientAPIsMask */
+ { 0x0 }, /* ClientAPIs */
+ 0, /* NumDrivers */
+ { NULL }, /* Drivers */
+ 0, /* NumAtExitCalls */
+ { NULL }, /* AtExitCalls */
+};
- _eglGlobal.ClientAPIsMask = 0x0;
- if (!_eglInitCurrent())
- _eglLog(_EGL_FATAL, "failed to initialize \"current\" system");
- }
+static void
+_eglAtExit(void)
+{
+ EGLint i;
+ for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
+ _eglGlobal.AtExitCalls[i]();
}
-/**
- * Should call this via an atexit handler.
- */
void
-_eglDestroyGlobals(void)
+_eglAddAtExitCall(void (*func)(void))
{
- _eglFiniCurrent();
- /* XXX TODO walk over table entries, deleting each */
- _eglDeleteHashTable(_eglGlobal.Displays);
- _eglDeleteHashTable(_eglGlobal.Surfaces);
+ if (func) {
+ static EGLBoolean registered = EGL_FALSE;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ if (!registered) {
+ atexit(_eglAtExit);
+ registered = EGL_TRUE;
+ }
+
+ assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
+ _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+ }
}
diff --git a/src/egl/main/eglglobals.h b/src/egl/main/eglglobals.h
index a9443cfbdd..1e2c674263 100644
--- a/src/egl/main/eglglobals.h
+++ b/src/egl/main/eglglobals.h
@@ -4,6 +4,7 @@
#include "egltypedefs.h"
#include "eglhash.h"
#include "eglcurrent.h"
+#include "eglmutex.h"
/**
@@ -11,12 +12,7 @@
*/
struct _egl_global
{
- EGLBoolean Initialized;
-
- /* these are private to egldisplay.c */
- _EGLHashtable *Displays;
- _EGLHashtable *Surfaces;
-
+ _EGLMutex *Mutex;
EGLScreenMESA FreeScreenHandle;
/* bitmaks of supported APIs (supported by _some_ driver) */
@@ -26,6 +22,9 @@ struct _egl_global
EGLint NumDrivers;
_EGLDriver *Drivers[10];
+
+ EGLint NumAtExitCalls;
+ void (*AtExitCalls[10])(void);
};
@@ -33,11 +32,7 @@ extern struct _egl_global _eglGlobal;
extern void
-_eglInitGlobals(void);
-
-
-extern void
-_eglDestroyGlobals(void);
+_eglAddAtExitCall(void (*func)(void));
#endif /* EGLGLOBALS_INCLUDED */
diff --git a/src/egl/main/eglmutex.h b/src/egl/main/eglmutex.h
new file mode 100644
index 0000000000..29faba0f24
--- /dev/null
+++ b/src/egl/main/eglmutex.h
@@ -0,0 +1,52 @@
+#ifndef EGLMUTEX_INCLUDED
+#define EGLMUTEX_INCLUDED
+
+#include "eglcompiler.h"
+
+#ifdef PTHREADS
+#include <pthread.h>
+
+typedef pthread_mutex_t _EGLMutex;
+
+static INLINE void _eglInitMutex(_EGLMutex *m)
+{
+ pthread_mutex_init(m, NULL);
+}
+
+static INLINE void
+_eglDestroyMutex(_EGLMutex *m)
+{
+ pthread_mutex_destroy(m);
+}
+
+static INLINE void
+_eglLockMutex(_EGLMutex *m)
+{
+ pthread_mutex_lock(m);
+}
+
+static INLINE void
+_eglUnlockMutex(_EGLMutex *m)
+{
+ pthread_mutex_unlock(m);
+}
+
+#define _EGL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#define _EGL_DECLARE_MUTEX(m) \
+ _EGLMutex m = _EGL_MUTEX_INITIALIZER
+
+#else
+
+typedef int _EGLMutex;
+static INLINE void _eglInitMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglDestroyMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglLockMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglUnlockMutex(_EGLMutex *m) { (void) m; }
+
+#define _EGL_MUTEX_INITIALIZER 0
+#define _EGL_DECLARE_MUTEX(m) \
+ _EGLMutex m = _EGL_MUTEX_INITIALIZER
+
+#endif
+
+#endif /* EGLMUTEX_INCLUDED */
diff --git a/src/egl/main/eglsurface.c b/src/egl/main/eglsurface.c
index 9821e63628..3947051127 100644
--- a/src/egl/main/eglsurface.c
+++ b/src/egl/main/eglsurface.c
@@ -26,7 +26,8 @@ _eglInitSurface(_EGLDriver *drv, _EGLSurface *surf, EGLint type,
{
const char *func;
EGLint width = 0, height = 0, largest = 0;
- EGLint texFormat = 0, texTarget = 0, mipmapTex = 0;
+ EGLint texFormat = EGL_NO_TEXTURE, texTarget = EGL_NO_TEXTURE;
+ EGLint mipmapTex = EGL_FALSE;
EGLint renderBuffer = EGL_BACK_BUFFER;
#ifdef EGL_VERSION_1_2
EGLint colorspace = EGL_COLORSPACE_sRGB;
@@ -413,7 +414,7 @@ _eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
_EGLSurface *surf = _eglLookupSurface(surface);
if (surf) {
_eglUnlinkSurface(surf);
- if (!surf->IsBound)
+ if (!_eglIsSurfaceBound(surf))
free(surf);
return EGL_TRUE;
}
diff --git a/src/egl/main/eglsurface.h b/src/egl/main/eglsurface.h
index f9413eb9d7..8864176844 100644
--- a/src/egl/main/eglsurface.h
+++ b/src/egl/main/eglsurface.h
@@ -15,12 +15,12 @@ struct _egl_surface
_EGLSurface *Next;
EGLSurface Handle;
- _EGLConfig *Config;
-
- /* May need reference counting here */
- EGLBoolean IsBound;
+ /* The bound status of the surface */
+ _EGLContext *Binding;
EGLBoolean BoundToTexture;
+ _EGLConfig *Config;
+
EGLint Type; /* one of EGL_WINDOW_BIT, EGL_PIXMAP_BIT or EGL_PBUFFER_BIT */
EGLint Width, Height;
EGLint TextureFormat, TextureTarget;
@@ -100,5 +100,16 @@ _eglCreatePbufferFromClientBuffer(_EGLDriver *drv, EGLDisplay dpy,
#endif /* EGL_VERSION_1_2 */
+/**
+ * Return true if the surface is bound to a thread.
+ * A surface bound to a texutre is not considered bound by
+ * this function.
+ */
+static INLINE EGLBoolean
+_eglIsSurfaceBound(_EGLSurface *surf)
+{
+ return (surf->Binding != NULL);
+}
+
#endif /* EGLSURFACE_INCLUDED */