Eliminate the glBindTexImage dependency.

BUG=18110152

Change-Id: I7c7e8aa5ff52de794aacef557769f130d039c1a5
Reviewed-on: https://swiftshader-review.googlesource.com/1242
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
new file mode 100644
index 0000000..d8ce02d
--- /dev/null
+++ b/src/GLES2/libEGL/Context.hpp
@@ -0,0 +1,16 @@
+#ifndef egl_Context_hpp

+#define egl_Context_hpp

+

+namespace egl

+{

+class Surface;

+

+class Context

+{

+public:

+	virtual void destroy() = 0;

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

+};

+}

+

+#endif   // egl_Context_hpp

diff --git a/src/GLES2/libEGL/Display.cpp b/src/GLES2/libEGL/Display.cpp
index 5346ad5..b80ef75 100644
--- a/src/GLES2/libEGL/Display.cpp
+++ b/src/GLES2/libEGL/Display.cpp
@@ -17,8 +17,9 @@
 

 #include "main.h"

 #include "libGLESv2/mathutil.h"

-#include "libGLESv2/Context.h"

 #include "libGLESv2/Device.hpp"

+#include "libEGL/Surface.h"

+#include "libEGL/Context.hpp"

 #include "common/debug.h"

 

 #include <algorithm>

@@ -404,7 +405,7 @@
     return success(surface);

 }

 

-EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)

+EGLContext Display::createContext(EGLConfig configHandle, const egl::Context *shareContext)

 {

     if(!mDevice)

     {

@@ -416,7 +417,7 @@
 

     const egl::Config *config = mConfigSet.get(configHandle);

 

-    gl::Context *context = gl::createContext(config, shareContext);

+    egl::Context *context = gl::createContext(config, shareContext);

     mContextSet.insert(context);

 

     return context;

@@ -428,7 +429,7 @@
     mSurfaceSet.erase(surface);

 }

 

-void Display::destroyContext(gl::Context *context)

+void Display::destroyContext(egl::Context *context)

 {

     context->destroy();

     mContextSet.erase(context);

@@ -444,7 +445,7 @@
     return mConfigSet.get(config) != NULL;

 }

 

-bool Display::isValidContext(gl::Context *context)

+bool Display::isValidContext(egl::Context *context)

 {

     return mContextSet.find(context) != mContextSet.end();

 }

diff --git a/src/GLES2/libEGL/Display.h b/src/GLES2/libEGL/Display.h
index 651cc93..808223d 100644
--- a/src/GLES2/libEGL/Display.h
+++ b/src/GLES2/libEGL/Display.h
@@ -17,18 +17,19 @@
 #define INCLUDE_DISPLAY_H_

 

 #include "Config.h"

-#include "Surface.h"

 

 #include <set>

 

 namespace gl

 {

-class Context;

 class Device;

 }

 

 namespace egl

 {

+	class Surface;

+	class Context;

+

 	class Display

 	{

 	public:

@@ -44,15 +45,15 @@
 

 		EGLSurface createWindowSurface(EGLNativeWindowType window, EGLConfig config, const EGLint *attribList);

 		EGLSurface createOffscreenSurface(EGLConfig config, const EGLint *attribList);

-		EGLContext createContext(EGLConfig configHandle, const gl::Context *shareContext);

+		EGLContext createContext(EGLConfig configHandle, const Context *shareContext);

 

-		void destroySurface(egl::Surface *surface);

-		void destroyContext(gl::Context *context);

+		void destroySurface(Surface *surface);

+		void destroyContext(Context *context);

 

 		bool isInitialized() const;

 		bool isValidConfig(EGLConfig config);

-		bool isValidContext(gl::Context *context);

-		bool isValidSurface(egl::Surface *surface);

+		bool isValidContext(Context *context);

+		bool isValidSurface(Surface *surface);

 		bool isValidWindow(EGLNativeWindowType window);

 		bool hasExistingWindowSurface(EGLNativeWindowType window);

 

@@ -80,7 +81,7 @@
 

 		ConfigSet mConfigSet;

 

-		typedef std::set<gl::Context*> ContextSet;

+		typedef std::set<Context*> ContextSet;

 		ContextSet mContextSet;

 

 		bool createDevice();

diff --git a/src/GLES2/libEGL/libEGL.cpp b/src/GLES2/libEGL/libEGL.cpp
index 521c982..49ee1d6 100644
--- a/src/GLES2/libEGL/libEGL.cpp
+++ b/src/GLES2/libEGL/libEGL.cpp
@@ -13,6 +13,7 @@
 

 #include "main.h"

 #include "Display.h"

+#include "Surface.h"

 #include "libGLESv2/Context.h"

 #include "libGLESv2/Texture.h"

 #include "common/debug.h"

@@ -50,7 +51,7 @@
     return true;

 }

 

-static bool validateContext(egl::Display *display, gl::Context *context)

+static bool validateContext(egl::Display *display, egl::Context *context)

 {

     if(!validateDisplay(display))

     {

@@ -661,7 +662,12 @@
             return error(EGL_BAD_MATCH, EGL_FALSE);

         }

 

-        gl::bindTexImage(eglSurface);

+		egl::Context *context = static_cast<egl::Context*>(egl::getCurrentContext());

+

+		if(context)

+		{

+			context->bindTexImage(eglSurface);

+		}

 

         return success(EGL_TRUE);

     }

@@ -787,7 +793,7 @@
             return EGL_NO_CONTEXT;

         }

 

-        EGLContext context = display->createContext(config, static_cast<gl::Context*>(share_context));

+        EGLContext context = display->createContext(config, static_cast<egl::Context*>(share_context));

 

         return success(context);

     }

@@ -806,7 +812,7 @@
     try

     {

         egl::Display *display = static_cast<egl::Display*>(dpy);

-        gl::Context *context = static_cast<gl::Context*>(ctx);

+        egl::Context *context = static_cast<egl::Context*>(ctx);

 

         if(!validateContext(display, context))

         {

diff --git a/src/GLES2/libEGL/libEGL.vcxproj b/src/GLES2/libEGL/libEGL.vcxproj
index bced187..203aea5 100644
--- a/src/GLES2/libEGL/libEGL.vcxproj
+++ b/src/GLES2/libEGL/libEGL.vcxproj
@@ -161,6 +161,7 @@
     <ClInclude Include="..\include\EGL\eglext.h" />

     <ClInclude Include="..\include\EGL\eglplatform.h" />

     <ClInclude Include="Config.h" />

+    <ClInclude Include="Context.hpp" />

     <ClInclude Include="Display.h" />

     <ClInclude Include="main.h" />

     <ClInclude Include="resource.h" />

diff --git a/src/GLES2/libEGL/libEGL.vcxproj.filters b/src/GLES2/libEGL/libEGL.vcxproj.filters
index 44a6a98..b395738 100644
--- a/src/GLES2/libEGL/libEGL.vcxproj.filters
+++ b/src/GLES2/libEGL/libEGL.vcxproj.filters
@@ -58,6 +58,9 @@
     <ClInclude Include="..\common\debug.h">

       <Filter>Header Files</Filter>

     </ClInclude>

+    <ClInclude Include="Context.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

   </ItemGroup>

   <ItemGroup>

     <ResourceCompile Include="libEGL.rc" />

diff --git a/src/GLES2/libEGL/main.cpp b/src/GLES2/libEGL/main.cpp
index 9e0483a..c90e1f4 100644
--- a/src/GLES2/libEGL/main.cpp
+++ b/src/GLES2/libEGL/main.cpp
@@ -89,7 +89,7 @@
 	

     libGLESv2 = loadLibrary(libGLESv2_lib);

     gl::createDevice = (gl::Device*(*)())getProcAddress(libGLESv2, "createDevice");

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

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

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

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

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

@@ -260,7 +260,7 @@
 namespace gl

 {

 	Device *(*createDevice)() = 0;

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

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

 	void (*bindTexImage)(egl::Surface *surface) = 0;

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

 	Context *(*getCurrentContext)() = 0;

diff --git a/src/GLES2/libEGL/main.h b/src/GLES2/libEGL/main.h
index 6f67abd..01a6873 100644
--- a/src/GLES2/libEGL/main.h
+++ b/src/GLES2/libEGL/main.h
@@ -72,6 +72,7 @@
 	class Config;
 	class Surface;
 	class Display;
+	class Context;
 }
 
 namespace sw
@@ -87,8 +88,7 @@
 	class Image;
 
 	extern Device *(*createDevice)();
-	extern Context *(*createContext)(const egl::Config *config, const Context *shareContext);
-	extern void (*bindTexImage)(egl::Surface *surface);
+	extern egl::Context *(*createContext)(const egl::Config *config, const egl::Context *shareContext);
 	extern void (*makeCurrent)(Context *context, egl::Display *display, egl::Surface *surface);
 	extern __eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname);
 	extern Image *(*createBackBuffer)(int width, int height, const egl::Config *config);
diff --git a/src/GLES2/libGLES_CM/Context.cpp b/src/GLES2/libGLES_CM/Context.cpp
index f909a4c..1a09617 100644
--- a/src/GLES2/libGLES_CM/Context.cpp
+++ b/src/GLES2/libGLES_CM/Context.cpp
@@ -26,6 +26,7 @@
 #include "VertexDataManager.h"

 #include "IndexDataManager.h"

 #include "libEGL/Display.h"

+#include "libEGL/Surface.h"

 #include "Common/Half.hpp"

 

 #undef near

@@ -2217,6 +2218,16 @@
     mVertexDataManager->dirtyCurrentValue(index);

 }

 

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

+{

+	gl::Texture2D *textureObject = getTexture2D();

+

+    if(textureObject)

+    {

+		textureObject->bindTexImage(surface);

+	}

+}

+

 }

 

 // Exported functions for use by EGL

diff --git a/src/GLES2/libGLES_CM/Context.h b/src/GLES2/libGLES_CM/Context.h
index 2aa880b..ee4d38f 100644
--- a/src/GLES2/libGLES_CM/Context.h
+++ b/src/GLES2/libGLES_CM/Context.h
@@ -15,6 +15,7 @@
 #ifndef LIBGLES_CM_CONTEXT_H_

 #define LIBGLES_CM_CONTEXT_H_

 

+#include "libEGL/Context.hpp"

 #include "ResourceManager.h"

 #include "HandleAllocator.h"

 #include "RefCountObject.h"

@@ -217,7 +218,7 @@
     GLint packAlignment;

 };

 

-class Context

+class Context : public egl::Context

 {

 public:

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

@@ -366,6 +367,8 @@
 

     static int getSupportedMultiSampleDepth(sw::Format format, int requested);

 

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

+

 private:

 	virtual ~Context();

 

diff --git a/src/GLES2/libGLES_CM/Texture.cpp b/src/GLES2/libGLES_CM/Texture.cpp
index c3765e1..6150b1c 100644
--- a/src/GLES2/libGLES_CM/Texture.cpp
+++ b/src/GLES2/libGLES_CM/Texture.cpp
@@ -20,6 +20,7 @@
 #include "Framebuffer.h"

 #include "Device.hpp"

 #include "libEGL/Display.h"

+#include "libEGL/Surface.h"

 #include "common/debug.h"

 

 #include <algorithm>

diff --git a/src/GLES2/libGLES_CM/libGLES_CM.cpp b/src/GLES2/libGLES_CM/libGLES_CM.cpp
index d8c81b8..650b93f 100644
--- a/src/GLES2/libGLES_CM/libGLES_CM.cpp
+++ b/src/GLES2/libGLES_CM/libGLES_CM.cpp
@@ -3697,31 +3697,6 @@
     return NULL;

 }

 

-void glBindTexImage(egl::Surface *surface)

-{

-    TRACE("(egl::Surface* surface = 0x%0.8p)",

-          surface);

-

-    try

-    {

-        gl::Context *context = gl::getContext();

-

-        if(context)

-        {

-            gl::Texture2D *textureObject = context->getTexture2D();

-

-            if(textureObject)

-            {

-                textureObject->bindTexImage(surface);

-            }

-        }

-    }

-    catch(std::bad_alloc&)

-    {

-        return error(GL_OUT_OF_MEMORY);

-    }

-}

-

 void GL_APIENTRY Register(const char *licenseKey)

 {

 	RegisterLicenseKey(licenseKey);

diff --git a/src/GLES2/libGLES_CM/libGLES_CM.def b/src/GLES2/libGLES_CM/libGLES_CM.def
index faf6203..e5bc3a5 100644
--- a/src/GLES2/libGLES_CM/libGLES_CM.def
+++ b/src/GLES2/libGLES_CM/libGLES_CM.def
@@ -149,7 +149,6 @@
     glCreateContext                 @144
     glMakeCurrent                   @146
     glGetProcAddress                @148
-    glBindTexImage                  @158
 
 	createFrameBuffer               @172
 	createBackBuffer                @173
diff --git a/src/GLES2/libGLESv2/Context.cpp b/src/GLES2/libGLESv2/Context.cpp
index c775e81..4953e6e 100644
--- a/src/GLES2/libGLESv2/Context.cpp
+++ b/src/GLES2/libGLESv2/Context.cpp
@@ -29,6 +29,7 @@
 #include "VertexDataManager.h"

 #include "IndexDataManager.h"

 #include "libEGL/Display.h"

+#include "libEGL/Surface.h"

 #include "Common/Half.hpp"

 

 #undef near

@@ -3030,6 +3031,16 @@
     }

 }

 

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

+{

+	gl::Texture2D *textureObject = getTexture2D();

+

+    if(textureObject)

+    {

+		textureObject->bindTexImage(surface);

+	}

+}

+

 }

 

 // Exported functions for use by EGL

diff --git a/src/GLES2/libGLESv2/Context.h b/src/GLES2/libGLESv2/Context.h
index 206b35f..aefd7bd 100644
--- a/src/GLES2/libGLESv2/Context.h
+++ b/src/GLES2/libGLESv2/Context.h
@@ -15,6 +15,7 @@
 #ifndef LIBGLESV2_CONTEXT_H_

 #define LIBGLESV2_CONTEXT_H_

 

+#include "libEGL/Context.hpp"

 #include "ResourceManager.h"

 #include "HandleAllocator.h"

 #include "RefCountObject.h"

@@ -235,7 +236,7 @@
     GLint packAlignment;

 };

 

-class Context

+class Context : public egl::Context

 {

 public:

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

@@ -418,6 +419,8 @@
                          GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,

                          GLbitfield mask);

 

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

+

 private:

 	virtual ~Context();

 

diff --git a/src/GLES2/libGLESv2/Texture.cpp b/src/GLES2/libGLESv2/Texture.cpp
index 25dbdb1..7c65b88 100644
--- a/src/GLES2/libGLESv2/Texture.cpp
+++ b/src/GLES2/libGLESv2/Texture.cpp
@@ -20,6 +20,7 @@
 #include "Framebuffer.h"

 #include "Device.hpp"

 #include "libEGL/Display.h"

+#include "libEGL/Surface.h"

 #include "common/debug.h"

 

 #include <algorithm>

diff --git a/src/GLES2/libGLESv2/libGLESv2.cpp b/src/GLES2/libGLESv2/libGLESv2.cpp
index 2ea6070..60c11a9 100644
--- a/src/GLES2/libGLESv2/libGLESv2.cpp
+++ b/src/GLES2/libGLESv2/libGLESv2.cpp
@@ -6182,31 +6182,6 @@
     return NULL;

 }

 

-void glBindTexImage(egl::Surface *surface)

-{

-    TRACE("(egl::Surface* surface = 0x%0.8p)",

-          surface);

-

-    try

-    {

-        gl::Context *context = gl::getContext();

-

-        if(context)

-        {

-            gl::Texture2D *textureObject = context->getTexture2D();

-

-            if(textureObject)

-            {

-                textureObject->bindTexImage(surface);

-            }

-        }

-    }

-    catch(std::bad_alloc&)

-    {

-        return error(GL_OUT_OF_MEMORY);

-    }

-}

-

 void GL_APIENTRY Register(const char *licenseKey)

 {

 	RegisterLicenseKey(licenseKey);

diff --git a/src/GLES2/libGLESv2/libGLESv2.def b/src/GLES2/libGLESv2/libGLESv2.def
index c6dfa6e..93a7450 100644
--- a/src/GLES2/libGLESv2/libGLESv2.def
+++ b/src/GLES2/libGLESv2/libGLESv2.def
@@ -172,7 +172,6 @@
     glCreateContext                 @144
     glMakeCurrent                   @146
     glGetProcAddress                @148
-    glBindTexImage                  @158
 
 	createFrameBuffer               @172
 	createBackBuffer                @173