Return the requested GL context version.

BUG=18110152

Change-Id: Ia2a04777e6303cd9f2e53a735455c8c7921b1b13
diff --git a/src/GLES2/libEGL/Display.cpp b/src/GLES2/libEGL/Display.cpp
index f066d07..f50fab1 100644
--- a/src/GLES2/libEGL/Display.cpp
+++ b/src/GLES2/libEGL/Display.cpp
@@ -383,14 +383,38 @@
     return success(surface);

 }

 

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

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

 {

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

+	egl::Context *context = 0;

 

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

-    mContextSet.insert(context);

+	if(clientVersion == 1 && config->mRenderableType & EGL_OPENGL_ES_BIT)

+	{

+		if(gl::createContext != 0)

+		{

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

+		}

+	}

+	else if(clientVersion == 2 && config->mRenderableType & EGL_OPENGL_ES2_BIT)

+	{

+		if(gl2::createContext != 0)

+		{

+			context = gl2::createContext(config, shareContext);

+		}

+	}

+	else

+	{

+		return error(EGL_BAD_CONFIG, EGL_NO_CONTEXT);

+	}

 

-    return context;

+	if(!context)

+	{

+		return error(EGL_BAD_ALLOC, EGL_NO_CONTEXT);

+	}

+

+	mContextSet.insert(context);

+

+    return success(context);;

 }

 

 void Display::destroySurface(egl::Surface *surface)

diff --git a/src/GLES2/libEGL/Display.h b/src/GLES2/libEGL/Display.h
index f06f776..ff1d7c8 100644
--- a/src/GLES2/libEGL/Display.h
+++ b/src/GLES2/libEGL/Display.h
@@ -40,7 +40,7 @@
 

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

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

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

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

 

 		void destroySurface(Surface *surface);

 		void destroyContext(Context *context);

diff --git a/src/GLES2/libEGL/libEGL.cpp b/src/GLES2/libEGL/libEGL.cpp
index 628a581..b76a420 100644
--- a/src/GLES2/libEGL/libEGL.cpp
+++ b/src/GLES2/libEGL/libEGL.cpp
@@ -765,15 +765,14 @@
 

     try

     {

-        // Get the requested client version (default is 1) and check it is two.

-        EGLint client_version = 1;

+        EGLint clientVersion = 1;

         if(attrib_list)

         {

             for(const EGLint* attribute = attrib_list; attribute[0] != EGL_NONE; attribute += 2)

             {

                 if(attribute[0] == EGL_CONTEXT_CLIENT_VERSION)

                 {

-                    client_version = attribute[1];

+                    clientVersion = attribute[1];

                 }

                 else

                 {

@@ -781,12 +780,7 @@
                 }

             }

         }

-

-        if(client_version != 2)

-        {

-            return error(EGL_BAD_CONFIG, EGL_NO_CONTEXT);

-        }

-

+		

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

 

         if(!validateConfig(display, config))

@@ -794,9 +788,7 @@
             return EGL_NO_CONTEXT;

         }

 

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

-

-        return success(context);

+        return display->createContext(config, static_cast<egl::Context*>(share_context), clientVersion);

     }

     catch(std::bad_alloc&)

     {

diff --git a/src/GLES2/libEGL/main.cpp b/src/GLES2/libEGL/main.cpp
index a257083..d77776f 100644
--- a/src/GLES2/libEGL/main.cpp
+++ b/src/GLES2/libEGL/main.cpp
@@ -83,6 +83,19 @@
 	eglAttachThread();

 

 	#if defined(_WIN32)

+	const char *libGLES_CM_lib = "libGLES_CM.dll";

+	#else

+	const char *libGLES_CM_lib = "libGLES_CM.so.1";

+	#endif

+	

+    libGLES_CM = loadLibrary(libGLES_CM_lib);

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

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

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

+	gl::createDepthStencil = (egl::Image *(*)(unsigned int, unsigned int, sw::Format, int, bool))getProcAddress(libGLES_CM, "createDepthStencil");

+    gl::createFrameBuffer = (sw::FrameBuffer *(*)(EGLNativeDisplayType, EGLNativeWindowType, int, int))getProcAddress(libGLES_CM, "createFrameBuffer");

+

+	#if defined(_WIN32)

 	const char *libGLESv2_lib = "libGLESv2.dll";

 	#else

 	const char *libGLESv2_lib = "libGLESv2.so.2";

@@ -95,7 +108,7 @@
 	gl2::createDepthStencil = (egl::Image *(*)(unsigned int, unsigned int, sw::Format, int, bool))getProcAddress(libGLESv2, "createDepthStencil");

     gl2::createFrameBuffer = (sw::FrameBuffer *(*)(EGLNativeDisplayType, EGLNativeWindowType, int, int))getProcAddress(libGLESv2, "createFrameBuffer");

 

-	return libGLESv2 != 0;

+	return libGLES_CM != 0 || libGLESv2 != 0;

 }

 

 DESTRUCTOR static void eglDetachProcess()

@@ -257,6 +270,15 @@
     }

 }

 

+namespace gl

+{

+	egl::Context *(*createContext)(const egl::Config *config, const egl::Context *shareContext) = 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;

+	sw::FrameBuffer *(*createFrameBuffer)(EGLNativeDisplayType display, EGLNativeWindowType window, int width, int height) = 0;

+}

+

 namespace gl2

 {

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

@@ -266,4 +288,5 @@
 	sw::FrameBuffer *(*createFrameBuffer)(EGLNativeDisplayType display, EGLNativeWindowType window, int width, int height) = 0;

 }

 

+void *libGLES_CM = 0;   // Handle to the libGLES_CM module

 void *libGLESv2 = 0;   // Handle to the libGLESv2 module

diff --git a/src/GLES2/libEGL/main.h b/src/GLES2/libEGL/main.h
index 25fcd15..faf7a56 100644
--- a/src/GLES2/libEGL/main.h
+++ b/src/GLES2/libEGL/main.h
@@ -82,6 +82,16 @@
 	enum Format : unsigned char;
 }
 
+// libGLES_CM dependencies
+namespace gl
+{
+	extern egl::Context *(*createContext)(const egl::Config *config, const egl::Context *shareContext);
+	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);
+	extern sw::FrameBuffer *(*createFrameBuffer)(EGLNativeDisplayType display, EGLNativeWindowType window, int width, int height);
+}
+
 // libGLESv2 dependencies
 namespace gl2
 {
@@ -92,6 +102,7 @@
 	extern sw::FrameBuffer *(*createFrameBuffer)(EGLNativeDisplayType display, EGLNativeWindowType window, int width, int height);
 }
 
-extern void *libGLESv2;   // Handle to the libGLESv2 module
+extern void *libGLES_CM;   // Handle to the libGLES_CM module
+extern void *libGLESv2;    // Handle to the libGLESv2 module
 
 #endif  // LIBEGL_MAIN_H_