Fix crash on eglDestroyContext.

BUG=18211761

Change-Id: I7baf09e59330aad390c1fab37d7d3f1321faa3fc
Reviewed-on: https://swiftshader-review.googlesource.com/1310
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/GLES2/libEGL/libEGL.cpp b/src/GLES2/libEGL/libEGL.cpp
index b5fbe43..5d1eda2 100644
--- a/src/GLES2/libEGL/libEGL.cpp
+++ b/src/GLES2/libEGL/libEGL.cpp
@@ -839,6 +839,19 @@
         egl::Display *display = static_cast<egl::Display*>(dpy);

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

 

+		if(ctx != EGL_NO_CONTEXT || draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE)

+		{

+			if(!validateDisplay(display))

+			{

+				return EGL_FALSE;

+			}

+		}

+

+		if(ctx == EGL_NO_CONTEXT && (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE))

+		{

+			return error(EGL_BAD_MATCH, EGL_FALSE);

+		}

+

         if(ctx != EGL_NO_CONTEXT && !validateContext(display, context))

         {

             return EGL_FALSE;

@@ -850,6 +863,11 @@
             return EGL_FALSE;

         }

 

+		if((draw != EGL_NO_SURFACE) ^ (read != EGL_NO_SURFACE))

+		{

+			return error(EGL_BAD_MATCH, EGL_FALSE);

+		}

+

         if(draw != read)

         {

             UNIMPLEMENTED();   // FIXME

diff --git a/src/GLES2/libGLES_CM/Context.cpp b/src/GLES2/libGLES_CM/Context.cpp
index 55530ce..664a046 100644
--- a/src/GLES2/libGLES_CM/Context.cpp
+++ b/src/GLES2/libGLES_CM/Context.cpp
@@ -255,11 +255,6 @@
 

 void Context::destroy()

 {

-	if(this == getContext())

-	{

-		makeCurrent(0);

-	}

-

 	delete this;

 }

 

diff --git a/src/GLES2/libGLESv2/Context.cpp b/src/GLES2/libGLESv2/Context.cpp
index 6c05383..55dfd06 100644
--- a/src/GLES2/libGLESv2/Context.cpp
+++ b/src/GLES2/libGLESv2/Context.cpp
@@ -267,11 +267,6 @@
 

 void Context::destroy()

 {

-	if(this == getContext())

-	{

-		makeCurrent(0);

-	}

-

 	delete this;

 }