Retrieve the current context and display from EGL.

BUG=18110152

Change-Id: Iedddab96b5958b1daef6a41bb968af358afd6561
Reviewed-on: https://swiftshader-review.googlesource.com/1254
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/GLES2/libEGL/Context.hpp b/src/GLES2/libEGL/Context.hpp
index df23e8f..b4a811b 100644
--- a/src/GLES2/libEGL/Context.hpp
+++ b/src/GLES2/libEGL/Context.hpp
@@ -15,9 +15,11 @@
 {

 public:

 	virtual void destroy() = 0;

+	virtual void makeCurrent(Surface *surface) = 0;

 	virtual void bindTexImage(Surface *surface) = 0;

 	virtual EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel) = 0;

 	virtual Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel) = 0;

+	virtual int getClientVersion() = 0;

 };

 }

 

diff --git a/src/GLES2/libEGL/Surface.cpp b/src/GLES2/libEGL/Surface.cpp
index f1ad266..8492e6b 100644
--- a/src/GLES2/libEGL/Surface.cpp
+++ b/src/GLES2/libEGL/Surface.cpp
@@ -19,6 +19,7 @@
 #include "Display.h"

 #include "Texture2D.hpp"

 #include "Image.hpp"

+#include "Context.hpp"

 #include "common/debug.h"

 #include "Main/FrameBuffer.hpp"

 

@@ -304,7 +305,7 @@
 

         if(static_cast<egl::Surface*>(getCurrentDrawSurface()) == this)

         {

-            gl2::makeCurrent(static_cast<egl::Context*>(getCurrentContext()), static_cast<egl::Display*>(getCurrentDisplay()), this);

+			static_cast<egl::Context*>(getCurrentContext())->makeCurrent(this);

         }

 

         return true;

diff --git a/src/GLES2/libEGL/libEGL.cpp b/src/GLES2/libEGL/libEGL.cpp
index f30d181..628a581 100644
--- a/src/GLES2/libEGL/libEGL.cpp
+++ b/src/GLES2/libEGL/libEGL.cpp
@@ -868,7 +868,10 @@
         egl::setCurrentReadSurface(read);

 		egl::setCurrentContext(ctx);

 

-        gl2::makeCurrent(context, display, static_cast<egl::Surface*>(draw));

+		if(context)

+		{

+			context->makeCurrent(static_cast<egl::Surface*>(draw));

+		}

 

         return success(EGL_TRUE);

     }

diff --git a/src/GLES2/libEGL/main.cpp b/src/GLES2/libEGL/main.cpp
index 8d8224a..a257083 100644
--- a/src/GLES2/libEGL/main.cpp
+++ b/src/GLES2/libEGL/main.cpp
@@ -90,7 +90,6 @@
 	

     libGLESv2 = loadLibrary(libGLESv2_lib);

     gl2::createContext = (egl::Context *(*)(const egl::Config*, const egl::Context*))getProcAddress(libGLESv2, "glCreateContext");

-    gl2::makeCurrent = (void (*)(egl::Context*, egl::Display*, egl::Surface*))getProcAddress(libGLESv2, "glMakeCurrent");

     gl2::getProcAddress = (__eglMustCastToProperFunctionPointerType (*)(const char*))getProcAddress(libGLESv2, "glGetProcAddress");

     gl2::createBackBuffer = (egl::Image *(*)(int, int, const egl::Config*))getProcAddress(libGLESv2, "createBackBuffer");

 	gl2::createDepthStencil = (egl::Image *(*)(unsigned int, unsigned int, sw::Format, int, bool))getProcAddress(libGLESv2, "createDepthStencil");

@@ -261,7 +260,6 @@
 namespace gl2

 {

 	egl::Context *(*createContext)(const egl::Config *config, const egl::Context *shareContext) = 0;

-	void (*makeCurrent)(egl::Context *context, egl::Display *display, egl::Surface *surface) = 0;

 	__eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname) = 0;

 	egl::Image *(*createBackBuffer)(int width, int height, const egl::Config *config) = 0;

 	egl::Image *(*createDepthStencil)(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard) = 0;

diff --git a/src/GLES2/libEGL/main.h b/src/GLES2/libEGL/main.h
index 9f54007..25fcd15 100644
--- a/src/GLES2/libEGL/main.h
+++ b/src/GLES2/libEGL/main.h
@@ -86,7 +86,6 @@
 namespace gl2
 {
 	extern egl::Context *(*createContext)(const egl::Config *config, const egl::Context *shareContext);
-	extern void (*makeCurrent)(egl::Context *context, egl::Display *display, egl::Surface *surface);
 	extern __eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname);
 	extern egl::Image *(*createBackBuffer)(int width, int height, const egl::Config *config);
 	extern egl::Image *(*createDepthStencil)(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard);
diff --git a/src/GLES2/libGLES_CM/Context.cpp b/src/GLES2/libGLES_CM/Context.cpp
index 1e83169..a4f7a2b 100644
--- a/src/GLES2/libGLES_CM/Context.cpp
+++ b/src/GLES2/libGLES_CM/Context.cpp
@@ -174,17 +174,7 @@
     mResourceManager->release();

 }

 

-void Context::destroy()

-{

-	if(this == gl::getContext())

-	{

-		gl::makeCurrent(NULL, NULL, NULL);

-	}

-

-	delete this;

-}

-

-void Context::makeCurrent(egl::Display *display, egl::Surface *surface)

+void Context::makeCurrent(egl::Surface *surface)

 {

     if(!mHasBeenCurrent)

     {

@@ -227,6 +217,21 @@
     markAllStateDirty();

 }

 

+void Context::destroy()

+{

+	if(this == getContext())

+	{

+		makeCurrent(0);

+	}

+

+	delete this;

+}

+

+int Context::getClientVersion()

+{

+	return 1;

+}

+

 // This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.

 void Context::markAllStateDirty()

 {

@@ -2316,9 +2321,4 @@
 	{

 		return new gl::Context(config, shareContext);

 	}

-

-	void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)

-	{

-		gl::makeCurrent(context, display, surface);

-	}

 }

diff --git a/src/GLES2/libGLES_CM/Context.h b/src/GLES2/libGLES_CM/Context.h
index 653ce11..962a410 100644
--- a/src/GLES2/libGLES_CM/Context.h
+++ b/src/GLES2/libGLES_CM/Context.h
@@ -224,9 +224,9 @@
 public:

     Context(const egl::Config *config, const Context *shareContext);

 

+	virtual void makeCurrent(egl::Surface *surface);

 	virtual void destroy();

-

-    void makeCurrent(egl::Display *display, egl::Surface *surface);

+	virtual int getClientVersion();

 

     void markAllStateDirty();

 

diff --git a/src/GLES2/libGLES_CM/libGLES_CM.def b/src/GLES2/libGLES_CM/libGLES_CM.def
index dee995f..ea03026 100644
--- a/src/GLES2/libGLES_CM/libGLES_CM.def
+++ b/src/GLES2/libGLES_CM/libGLES_CM.def
@@ -147,7 +147,6 @@
 
     ; EGL dependencies
     glCreateContext                 @144
-    glMakeCurrent                   @146
     glGetProcAddress                @148
 
 	createFrameBuffer               @172
diff --git a/src/GLES2/libGLES_CM/main.cpp b/src/GLES2/libGLES_CM/main.cpp
index fa20507..050a29d 100644
--- a/src/GLES2/libGLES_CM/main.cpp
+++ b/src/GLES2/libGLES_CM/main.cpp
@@ -16,10 +16,9 @@
 #include "Framebuffer.h"

 #include "libEGL/Surface.h"

 #include "Common/Thread.hpp"

+#include "Common/SharedLibrary.hpp"

 #include "common/debug.h"

 

-static sw::Thread::LocalStorageKey currentTLS = TLS_OUT_OF_INDEXES;

-

 #if !defined(_MSC_VER)

 #define CONSTRUCTOR __attribute__((constructor))

 #define DESTRUCTOR __attribute__((destructor))

@@ -31,44 +30,30 @@
 static void glAttachThread()

 {

     TRACE("()");

-

-	gl::Current *current = new gl::Current;

-

-    if(current)

-    {

-		sw::Thread::setLocalStorage(currentTLS, current);

-

-        current->context = NULL;

-        current->display = NULL;

-    }

 }

 

 static void glDetachThread()

 {

     TRACE("()");

-

-	gl::Current *current = (gl::Current*)sw::Thread::getLocalStorage(currentTLS);

-

-    if(current)

-    {

-        delete current;

-    }

 }

 

 CONSTRUCTOR static bool glAttachProcess()

 {

     TRACE("()");

 

-	currentTLS = sw::Thread::allocateLocalStorageKey();

-

-    if(currentTLS == TLS_OUT_OF_INDEXES)

-    {

-        return false;

-    }

-

     glAttachThread();

 

-    return true;

+	#if defined(_WIN32)

+	const char *libEGL_lib = "libEGL.dll";

+	#else

+	const char *libEGL_lib = "libEGL.so.1";

+	#endif

+

+	libEGL = loadLibrary(libEGL_lib);

+	egl::getCurrentContext = (egl::Context *(*)())getProcAddress(libEGL, "eglGetCurrentContext");

+	egl::getCurrentDisplay = (egl::Display *(*)())getProcAddress(libEGL, "eglGetCurrentDisplay");

+

+    return libEGL != 0;

 }

 

 DESTRUCTOR static void glDetachProcess()

@@ -76,8 +61,7 @@
     TRACE("()");

 

 	glDetachThread();

-

-    sw::Thread::freeLocalStorageKey(currentTLS);

+	freeLibrary(libEGL);

 }

 

 #if defined(_WIN32)

@@ -107,43 +91,21 @@
 

 namespace gl

 {

-static gl::Current *getCurrent(void)

+gl::Context *getContext()

 {

-	Current *current = (Current*)sw::Thread::getLocalStorage(currentTLS);

-

-	if(!current)

+	egl::Context *context = egl::getCurrentContext();

+	

+	if(context && context->getClientVersion() == 1)

 	{

-		glAttachThread();

+		return static_cast<gl::Context*>(context);

 	}

-

-	return (Current*)sw::Thread::getLocalStorage(currentTLS);

-}

-

-void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface)

-{

-    Current *current = getCurrent();

-

-    current->context = context;

-    current->display = display;

-

-    if(context && display && surface)

-    {

-        context->makeCurrent(display, surface);

-    }

-}

-

-Context *getContext()

-{

-    Current *current = getCurrent();

-

-    return current->context;

+	

+	return 0;

 }

 

 egl::Display *getDisplay()

 {

-    Current *current = getCurrent();

-

-    return current->display;

+    return egl::getCurrentDisplay();

 }

 

 Device *getDevice()

@@ -187,3 +149,11 @@
         }

     }

 }

+

+namespace egl

+{

+	egl::Context *(*getCurrentContext)() = 0;

+	egl::Display *(*getCurrentDisplay)() = 0;

+}

+

+void *libEGL = 0;   // Handle to the libEGL module
\ No newline at end of file
diff --git a/src/GLES2/libGLES_CM/main.h b/src/GLES2/libGLES_CM/main.h
index cc3cfb1..8aefa7e 100644
--- a/src/GLES2/libGLES_CM/main.h
+++ b/src/GLES2/libGLES_CM/main.h
@@ -26,17 +26,8 @@
 
 namespace gl
 {
-	struct Current
-	{
-		Context *context;
-		egl::Display *display;
-	};
-
-	void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface);
-
 	Context *getContext();
 	egl::Display *getDisplay();
-
 	Device *getDevice();
 }
 
@@ -50,4 +41,13 @@
     return returnValue;
 }
 
+// libEGL dependencies
+namespace egl
+{
+	extern egl::Context *(*getCurrentContext)();
+	extern egl::Display *(*getCurrentDisplay)();
+}
+
+extern void *libEGL;   // Handle to the libEGL module
+
 #endif   // LIBGLES_CM_MAIN_H_
diff --git a/src/GLES2/libGLESv2/Context.cpp b/src/GLES2/libGLESv2/Context.cpp
index b265d19..9ef1f0b 100644
--- a/src/GLES2/libGLESv2/Context.cpp
+++ b/src/GLES2/libGLESv2/Context.cpp
@@ -222,17 +222,7 @@
     mResourceManager->release();

 }

 

-void Context::destroy()

-{

-	if(this == gl2::getContext())

-	{

-		gl2::makeCurrent(NULL, NULL, NULL);

-	}

-

-	delete this;

-}

-

-void Context::makeCurrent(egl::Display *display, egl::Surface *surface)

+void Context::makeCurrent(egl::Surface *surface)

 {

     if(!mHasBeenCurrent)

     {

@@ -275,6 +265,21 @@
     markAllStateDirty();

 }

 

+void Context::destroy()

+{

+	if(this == getContext())

+	{

+		makeCurrent(0);

+	}

+

+	delete this;

+}

+

+int Context::getClientVersion()

+{

+	return 2;

+}

+

 // This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.

 void Context::markAllStateDirty()

 {

@@ -3153,9 +3158,4 @@
 	{

 		return new gl2::Context(config, shareContext);

 	}

-

-	void glMakeCurrent(gl2::Context *context, egl::Display *display, egl::Surface *surface)

-	{

-		gl2::makeCurrent(context, display, surface);

-	}

 }

diff --git a/src/GLES2/libGLESv2/Context.h b/src/GLES2/libGLESv2/Context.h
index fd18df3..7eb8c90 100644
--- a/src/GLES2/libGLESv2/Context.h
+++ b/src/GLES2/libGLESv2/Context.h
@@ -242,9 +242,9 @@
 public:

     Context(const egl::Config *config, const Context *shareContext);

 

+	virtual void makeCurrent(egl::Surface *surface);

 	virtual void destroy();

-

-    void makeCurrent(egl::Display *display, egl::Surface *surface);

+	virtual int getClientVersion();

 

     void markAllStateDirty();

 

diff --git a/src/GLES2/libGLESv2/libGLESv2.def b/src/GLES2/libGLESv2/libGLESv2.def
index 277f507..c3b133e 100644
--- a/src/GLES2/libGLESv2/libGLESv2.def
+++ b/src/GLES2/libGLESv2/libGLESv2.def
@@ -170,7 +170,6 @@
 
     ; EGL dependencies
     glCreateContext                 @144
-    glMakeCurrent                   @146
     glGetProcAddress                @148
 
 	createFrameBuffer               @172
diff --git a/src/GLES2/libGLESv2/main.cpp b/src/GLES2/libGLESv2/main.cpp
index 43f8e38..a6a0d48 100644
--- a/src/GLES2/libGLESv2/main.cpp
+++ b/src/GLES2/libGLESv2/main.cpp
@@ -16,10 +16,9 @@
 #include "Framebuffer.h"

 #include "libEGL/Surface.h"

 #include "Common/Thread.hpp"

+#include "Common/SharedLibrary.hpp"

 #include "common/debug.h"

 

-static sw::Thread::LocalStorageKey currentTLS = TLS_OUT_OF_INDEXES;

-

 #if !defined(_MSC_VER)

 #define CONSTRUCTOR __attribute__((constructor))

 #define DESTRUCTOR __attribute__((destructor))

@@ -31,44 +30,30 @@
 static void glAttachThread()

 {

     TRACE("()");

-

-	gl2::Current *current = new gl2::Current;

-

-    if(current)

-    {

-		sw::Thread::setLocalStorage(currentTLS, current);

-

-        current->context = NULL;

-        current->display = NULL;

-    }

 }

 

 static void glDetachThread()

 {

     TRACE("()");

-

-	gl2::Current *current = (gl2::Current*)sw::Thread::getLocalStorage(currentTLS);

-

-    if(current)

-    {

-        delete current;

-    }

 }

 

 CONSTRUCTOR static bool glAttachProcess()

 {

     TRACE("()");

 

-	currentTLS = sw::Thread::allocateLocalStorageKey();

-

-    if(currentTLS == TLS_OUT_OF_INDEXES)

-    {

-        return false;

-    }

-

     glAttachThread();

 

-    return true;

+    #if defined(_WIN32)

+	const char *libEGL_lib = "libEGL.dll";

+	#else

+	const char *libEGL_lib = "libEGL.so.1";

+	#endif

+

+	libEGL = loadLibrary(libEGL_lib);

+	egl::getCurrentContext = (egl::Context *(*)())getProcAddress(libEGL, "eglGetCurrentContext");

+	egl::getCurrentDisplay = (egl::Display *(*)())getProcAddress(libEGL, "eglGetCurrentDisplay");

+

+    return libEGL != 0;

 }

 

 DESTRUCTOR static void glDetachProcess()

@@ -76,8 +61,7 @@
     TRACE("()");

 

 	glDetachThread();

-

-    sw::Thread::freeLocalStorageKey(currentTLS);

+	freeLibrary(libEGL);

 }

 

 #if defined(_WIN32)

@@ -107,43 +91,21 @@
 

 namespace gl2

 {

-static gl2::Current *getCurrent(void)

+gl2::Context *getContext()

 {

-	Current *current = (Current*)sw::Thread::getLocalStorage(currentTLS);

-

-	if(!current)

+	egl::Context *context = egl::getCurrentContext();

+	

+	if(context && context->getClientVersion() == 2)

 	{

-		glAttachThread();

+		return static_cast<gl2::Context*>(context);

 	}

-

-	return (Current*)sw::Thread::getLocalStorage(currentTLS);

-}

-

-void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface)

-{

-    Current *current = getCurrent();

-

-    current->context = context;

-    current->display = display;

-

-    if(context && display && surface)

-    {

-        context->makeCurrent(display, surface);

-    }

-}

-

-Context *getContext()

-{

-    Current *current = getCurrent();

-

-    return current->context;

+	

+	return 0;

 }

 

 egl::Display *getDisplay()

 {

-    Current *current = getCurrent();

-

-    return current->display;

+    return egl::getCurrentDisplay();

 }

 

 Device *getDevice()

@@ -187,3 +149,11 @@
         }

     }

 }

+

+namespace egl

+{

+	egl::Context *(*getCurrentContext)() = 0;

+	egl::Display *(*getCurrentDisplay)() = 0;

+}

+

+void *libEGL = 0;   // Handle to the libEGL module
\ No newline at end of file
diff --git a/src/GLES2/libGLESv2/main.h b/src/GLES2/libGLESv2/main.h
index ca7672a..99c8596 100644
--- a/src/GLES2/libGLESv2/main.h
+++ b/src/GLES2/libGLESv2/main.h
@@ -25,17 +25,8 @@
 
 namespace gl2
 {
-	struct Current
-	{
-		Context *context;
-		egl::Display *display;
-	};
-
-	void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface);
-
 	Context *getContext();
 	egl::Display *getDisplay();
-
 	Device *getDevice();
 }
 
@@ -49,4 +40,13 @@
     return returnValue;
 }
 
+// libEGL dependencies
+namespace egl
+{
+	extern egl::Context *(*getCurrentContext)();
+	extern egl::Display *(*getCurrentDisplay)();
+}
+
+extern void *libEGL;   // Handle to the libEGL module
+
 #endif   // LIBGLESV2_MAIN_H_