Use gl::Object to reference count egl::Image.

Bug 26851951

Change-Id: I5b594bdd34e6fa1074d5439ee2c09f6731839dee
Reviewed-on: https://swiftshader-review.googlesource.com/4534
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 2b4d97e..e4a1c87 100644
--- a/src/OpenGL/common/Image.cpp
+++ b/src/OpenGL/common/Image.cpp
@@ -1132,7 +1132,7 @@
 
 	Image::~Image()
 	{
-		ASSERT(referenceCount == 0);
+		ASSERT(!shared);
 	}
 
 	void Image::addRef()
@@ -1141,8 +1141,8 @@
 		{
 			return parentTexture->addRef();
 		}
-		int newCount = sw::atomicIncrement(&referenceCount);
-		LOGLOCK("%s image=%p referenceCount=%d", __FUNCTION__, this, newCount);
+
+		Object::addRef();
 	}
 
 	void Image::release()
@@ -1152,20 +1152,14 @@
 			return parentTexture->release();
 		}
 
-		int newCount = sw::atomicDecrement(&referenceCount);
-		LOGLOCK("%s image=%p referenceCount=%d", __FUNCTION__, this, newCount);
-		if (newCount == 0)
-		{
-			ASSERT(!shared);   // Should still hold a reference if eglDestroyImage hasn't been called
-			delete this;
-		}
+		Object::release();
 	}
 
 	void Image::unbind(const egl::Texture *parent)
 	{
 		if(parentTexture == parent)
 		{
-			parentTexture = 0;
+			parentTexture = nullptr;
 		}
 
 		release();
diff --git a/src/OpenGL/common/Image.hpp b/src/OpenGL/common/Image.hpp
index 9d060ca..7d98fea 100644
--- a/src/OpenGL/common/Image.hpp
+++ b/src/OpenGL/common/Image.hpp
@@ -36,7 +36,7 @@
 	return texture ? texture->getResource() : nullptr;

 }

 

-class Image : public sw::Surface

+class Image : public sw::Surface, public gl::Object

 {

 public:

 	Image(Texture *parentTexture, GLsizei width, GLsizei height, GLenum format, GLenum type)

@@ -45,7 +45,7 @@
 		  parentTexture(parentTexture)

 	{

 		shared = false;

-		referenceCount = 1;

+		Object::addRef();

 	}

 

 	Image(Texture *parentTexture, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int pitchP = 0)

@@ -54,7 +54,7 @@
 		  parentTexture(parentTexture)

 	{

 		shared = false;

-		referenceCount = 1;

+		Object::addRef();

 	}

 

 	Image(GLsizei width, GLsizei height, sw::Format internalFormat, int multiSampleDepth, bool lockable, bool renderTarget)

@@ -62,7 +62,7 @@
 		  width(width), height(height), format(0 /*GL_NONE*/), type(0 /*GL_NONE*/), internalFormat(internalFormat), depth(multiSampleDepth), parentTexture(nullptr)

 	{

 		shared = false;

-		referenceCount = 1;

+		Object::addRef();

 	}

 

 	GLsizei getWidth() const

@@ -137,8 +137,8 @@
 	void loadImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const UnpackInfo& unpackInfo, const void *input);

 	void loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize, const void *pixels);

 

-	virtual void addRef();

-	virtual void release();

+	void addRef() override;

+	void release() override;

 	virtual void unbind(const Texture *parent);   // Break parent ownership and release

 

 	virtual void destroyShared()   // Release a shared image

@@ -160,8 +160,6 @@
 

 	egl::Texture *parentTexture;

 

-	volatile int referenceCount;

-

 	virtual ~Image();

 

 	void loadD24S8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer);