Fix reference counting of texture images.

- Only unbind (orphan and release) images when the texture destructs.
- Let images hold just one reference to the parent texture.
- Check if textures and images only reference each other and garbage collect.

Bug 26851951

Change-Id: I2b0bcc283bf545d948e91288c531eac7cc14d122
Reviewed-on: https://swiftshader-review.googlesource.com/4711
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/common/Image.cpp b/src/OpenGL/common/Image.cpp
index 73a8656..1396a52 100644
--- a/src/OpenGL/common/Image.cpp
+++ b/src/OpenGL/common/Image.cpp
@@ -1132,27 +1132,29 @@
 
 	Image::~Image()
 	{
-		ASSERT(!shared);
-	}
-
-	void Image::addRef()
-	{
 		if(parentTexture)
 		{
-			return parentTexture->addRef();
+			parentTexture->release();
 		}
 
-		Object::addRef();
+		ASSERT(!shared);
 	}
 
 	void Image::release()
 	{
-		if(parentTexture)
-		{
-			return parentTexture->release();
-		}
+		int refs = dereference();
 
-		Object::release();
+		if(refs > 0)
+		{
+			if(parentTexture)
+			{
+				parentTexture->sweep();
+			}
+		}
+		else
+		{
+			delete this;
+		}
 	}
 
 	void Image::unbind(const egl::Texture *parent)