Fix an image destruction deadlock.

The EGL image extension allows a texture image to be shared with another
texture. We previously prevented destroying them as long as they are shared
but the parent texture (which owns the resource lock for all its images)
could be destroyed before the EGL image is destroyed, causing a deadlock
when trying to acquire the resource lock. The situation was aggravated by
increasing the parent texture's reference count with the image reference
count (i.e. not the 'owning' texture's reference count). This fix ensures
that the parent texture can only destroy the images it owns.

Bug 18680652

Change-Id: I7fa09d181e695cbc287e77874bdb88f24c22cbd8
Reviewed-on: https://swiftshader-review.googlesource.com/1601
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/OpenGL/libGLESv2/Image.cpp b/src/OpenGL/libGLESv2/Image.cpp
index 0c18f4b..97b08d4 100644
--- a/src/OpenGL/libGLESv2/Image.cpp
+++ b/src/OpenGL/libGLESv2/Image.cpp
@@ -53,7 +53,7 @@
 	{
 		if(parentTexture)
 		{
-			parentTexture->addRef();
+			return parentTexture->addRef();
 		}
 
 		sw::atomicIncrement(&referenceCount);
@@ -63,7 +63,7 @@
 	{
 		if(parentTexture)
 		{
-			parentTexture->release();
+			return parentTexture->release();
 		}
 
 		if(referenceCount > 0)
@@ -71,15 +71,19 @@
 			sw::atomicDecrement(&referenceCount);
 		}
 
-		if(referenceCount == 0 && !shared)
+		if(referenceCount == 0)
 		{
+			ASSERT(!shared);   // Should still hold a reference if eglDestroyImage hasn't been called
 			delete this;
 		}
 	}
 
-	void Image::unbind()
+	void Image::unbind(const egl::Texture *parent)
 	{
-		parentTexture = 0;
+		if(parentTexture == parent)
+		{
+			parentTexture = 0;
+		}
 
 		release();
 	}