Refactor Image creation.

Customize construction for each use case.

Bug 26851951

Change-Id: Ic10166bbfeaf11e800fec2a6470446b76e49b825
Reviewed-on: https://swiftshader-review.googlesource.com/4710
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/common/Image.hpp b/src/OpenGL/common/Image.hpp
index 7d98fea..609a8c4 100644
--- a/src/OpenGL/common/Image.hpp
+++ b/src/OpenGL/common/Image.hpp
@@ -31,16 +31,12 @@
 GLsizei ComputePitch(GLsizei width, GLenum format, GLenum type, GLint alignment);

 GLsizei ComputeCompressedSize(GLsizei width, GLsizei height, GLenum format);

 

-static inline sw::Resource *getParentResource(egl::Texture *texture)

-{

-	return texture ? texture->getResource() : nullptr;

-}

-

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

 {

 public:

+	// 2D texture image

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

-		: sw::Surface(getParentResource(parentTexture), width, height, 1, SelectInternalFormat(format, type), true, true),

+		: sw::Surface(parentTexture->getResource(), width, height, 1, SelectInternalFormat(format, type), true, true),

 		  width(width), height(height), format(format), type(type), internalFormat(SelectInternalFormat(format, type)), depth(1),

 		  parentTexture(parentTexture)

 	{

@@ -48,8 +44,9 @@
 		Object::addRef();

 	}

 

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

-		: sw::Surface(getParentResource(parentTexture), width, height, depth, SelectInternalFormat(format, type), true, true, pitchP),

+	// 3D texture image

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

+		: sw::Surface(parentTexture->getResource(), width, height, depth, SelectInternalFormat(format, type), true, true),

 		  width(width), height(height), format(format), type(type), internalFormat(SelectInternalFormat(format, type)), depth(depth),

 		  parentTexture(parentTexture)

 	{

@@ -57,9 +54,21 @@
 		Object::addRef();

 	}

 

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

-		: sw::Surface(nullptr, width, height, multiSampleDepth, internalFormat, lockable, renderTarget),

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

+	// Native EGL image

+	Image(GLsizei width, GLsizei height, GLenum format, GLenum type, int pitchP)

+		: sw::Surface(nullptr, width, height, depth, SelectInternalFormat(format, type), true, true, pitchP),

+		  width(width), height(height), format(format), type(type), internalFormat(SelectInternalFormat(format, type)), depth(depth),

+		  parentTexture(nullptr)

+	{

+		shared = true;

+		Object::addRef();

+	}

+

+	// Render target

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

+		: sw::Surface(nullptr, width, height, multiSampleDepth, internalFormat, lockable, true),

+		  width(width), height(height), format(0 /*GL_NONE*/), type(0 /*GL_NONE*/), internalFormat(internalFormat), depth(multiSampleDepth),

+		  parentTexture(nullptr)

 	{

 		shared = false;

 		Object::addRef();

@@ -206,14 +215,13 @@
 {

 public:

 	explicit AndroidNativeImage(ANativeWindowBuffer *nativeBuffer)

-		: egl::Image(0, nativeBuffer->width, nativeBuffer->height, 1,

+		: egl::Image(nativeBuffer->width, nativeBuffer->height,

 		             GLPixelFormatFromAndroid(nativeBuffer->format),

 		             GLPixelTypeFromAndroid(nativeBuffer->format),

 		             nativeBuffer->stride),

 		  nativeBuffer(nativeBuffer)

 	{

 		nativeBuffer->common.incRef(&nativeBuffer->common);

-		markShared();

 	}

 

 private:

diff --git a/src/OpenGL/libGLES_CM/Device.cpp b/src/OpenGL/libGLES_CM/Device.cpp
index 10fcf5a..8355e11 100644
--- a/src/OpenGL/libGLES_CM/Device.cpp
+++ b/src/OpenGL/libGLES_CM/Device.cpp
@@ -223,7 +223,7 @@
 		if(height > OUTLINE_RESOLUTION)

 		{

 			ERR("Invalid parameters: %dx%d", width, height);

-			return 0;

+			return nullptr;

 		}

 

 		bool lockable = true;

@@ -251,12 +251,12 @@
 			UNREACHABLE(format);

 		}

 

-		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 		if(!surface)

 		{

 			ERR("Out of memory");

-			return 0;

+			return nullptr;

 		}

 

 		return surface;

@@ -267,15 +267,15 @@
 		if(height > OUTLINE_RESOLUTION)

 		{

 			ERR("Invalid parameters: %dx%d", width, height);

-			return 0;

+			return nullptr;

 		}

 

-		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 		if(!surface)

 		{

 			ERR("Out of memory");

-			return 0;

+			return nullptr;

 		}

 

 		return surface;

diff --git a/src/OpenGL/libGLES_CM/Texture.cpp b/src/OpenGL/libGLES_CM/Texture.cpp
index 0741b03..9ee8989 100644
--- a/src/OpenGL/libGLES_CM/Texture.cpp
+++ b/src/OpenGL/libGLES_CM/Texture.cpp
@@ -333,12 +333,12 @@
 {

 	for(int i = 0; i < MIPMAP_LEVELS; i++)

 	{

-		image[i] = 0;

+		image[i] = nullptr;

 	}

 

-    mSurface = NULL;

+    mSurface = nullptr;

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 	mProxyRefs = 0;

 }

 

@@ -351,7 +351,7 @@
 		if(image[i])

 		{

 			image[i]->unbind(this);

-			image[i] = 0;

+			image[i] = nullptr;

 		}

 	}

 

@@ -359,11 +359,11 @@
 

     if(mSurface)

     {

-        mSurface->setBoundTexture(NULL);

-        mSurface = NULL;

+        mSurface->setBoundTexture(nullptr);

+        mSurface = nullptr;

     }

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 }

 

 // We need to maintain a count of references to renderbuffers acting as

@@ -383,7 +383,7 @@
 

     if(mProxyRefs == 0)

 	{

-		mColorbufferProxy = NULL;

+		mColorbufferProxy = nullptr;

 	}

 }

 

@@ -478,7 +478,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 

@@ -495,7 +495,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 }

@@ -721,10 +721,10 @@
 {

     if(target != GL_TEXTURE_2D)

     {

-        return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);

+        return error(GL_INVALID_OPERATION, (Renderbuffer*)nullptr);

     }

 

-    if(mColorbufferProxy == NULL)

+    if(!mColorbufferProxy)

     {

         mColorbufferProxy = new Renderbuffer(name, new RenderbufferTexture2D(this));

     }

@@ -786,10 +786,10 @@
 {

 	if(config)

 	{

-		return new egl::Image(width, height, config->mRenderTargetFormat, config->mSamples, false, true);

+		return new egl::Image(width, height, config->mRenderTargetFormat, config->mSamples, false);

 	}

 

-	return 0;

+	return nullptr;

 }

 

 egl::Image *createDepthStencil(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard)

@@ -825,12 +825,12 @@
 		UNREACHABLE(format);

 	}

 

-	egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+	egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 	if(!surface)

 	{

 		ERR("Out of memory");

-		return 0;

+		return nullptr;

 	}

 

 	return surface;

diff --git a/src/OpenGL/libGLESv2/Device.cpp b/src/OpenGL/libGLESv2/Device.cpp
index 7e7d93d..b594bf1 100644
--- a/src/OpenGL/libGLESv2/Device.cpp
+++ b/src/OpenGL/libGLESv2/Device.cpp
@@ -143,13 +143,13 @@
 	}

 

 	Device::~Device()

-	{		

+	{

 		if(depthStencil)

 		{

 			depthStencil->release();

 			depthStencil = nullptr;

 		}

-		

+

 		for(int i = 0; i < RENDERTARGETS; ++i)

 		{

 			if(renderTarget[i])

@@ -219,7 +219,7 @@
 

 		int x0(0), y0(0), width(0), height(0);

 		getScissoredRegion(depthStencil, x0, y0, width, height);

-			

+

 		depthStencil->clearDepthBuffer(z, x0, y0, width, height);

 	}

 

@@ -241,9 +241,9 @@
 		if(height > OUTLINE_RESOLUTION)

 		{

 			ERR("Invalid parameters: %dx%d", width, height);

-			return 0;

+			return nullptr;

 		}

-		

+

 		bool lockable = true;

 

 		switch(format)

@@ -269,12 +269,12 @@
 			UNREACHABLE(format);

 		}

 

-		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 		if(!surface)

 		{

 			ERR("Out of memory");

-			return 0;

+			return nullptr;

 		}

 

 		return surface;

@@ -285,17 +285,17 @@
 		if(height > OUTLINE_RESOLUTION)

 		{

 			ERR("Invalid parameters: %dx%d", width, height);

-			return 0;

+			return nullptr;

 		}

 

-		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+		egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 		if(!surface)

 		{

 			ERR("Out of memory");

-			return 0;

+			return nullptr;

 		}

-		

+

 		return surface;

 	}

 

@@ -405,7 +405,7 @@
 			vertexShaderConstantF[startRegister + i][2] = constantData[i * 4 + 2];

 			vertexShaderConstantF[startRegister + i][3] = constantData[i * 4 + 3];

 		}

-			

+

 		vertexShaderConstantsFDirty = max(startRegister + count, vertexShaderConstantsFDirty);

 		vertexShaderDirty = true;   // Reload DEF constants

 	}

@@ -471,7 +471,7 @@
 			ERR("Invalid parameters");

 			return false;

 		}

-		

+

 		int sWidth = source->getWidth();

 		int sHeight = source->getHeight();

 		int dWidth = dest->getWidth();

@@ -732,7 +732,7 @@
 				{

 					Renderer::setVertexShaderConstantF(0, vertexShaderConstantF[0], vertexShaderConstantsFDirty);

 				}

-		

+

 				Renderer::setVertexShader(vertexShader);   // Loads shader constants set with DEF

 				vertexShaderConstantsFDirty = vertexShader->dirtyConstantsF;   // Shader DEF'ed constants are dirty

 			}

@@ -744,7 +744,7 @@
 			vertexShaderDirty = false;

 		}

 	}

-	

+

 	bool Device::bindViewport()

 	{

 		if(viewport.width <= 0 || viewport.height <= 0)

@@ -764,7 +764,7 @@
 			scissor.x1 = scissorRect.x1;

 			scissor.y0 = scissorRect.y0;

 			scissor.y1 = scissorRect.y1;

-			

+

 			setScissor(scissor);

 		}

 		else

@@ -774,7 +774,7 @@
 			scissor.x1 = viewport.x0 + viewport.width;

 			scissor.y0 = viewport.y0;

 			scissor.y1 = viewport.y0 + viewport.height;

-			

+

 			for(int i = 0; i < RENDERTARGETS; ++i)

 			{

 				if(renderTarget[i])

@@ -804,7 +804,7 @@
 		view.height = (float)viewport.height;

 		view.minZ = viewport.minZ;

 		view.maxZ = viewport.maxZ;

-		

+

 		Renderer::setViewport(view);

 

 		return true;

diff --git a/src/OpenGL/libGLESv2/Texture.cpp b/src/OpenGL/libGLESv2/Texture.cpp
index 7c216b9..68ef704 100644
--- a/src/OpenGL/libGLESv2/Texture.cpp
+++ b/src/OpenGL/libGLESv2/Texture.cpp
@@ -167,7 +167,7 @@
     {

         return false;

     }

-    

+

 	if(mMaxAnisotropy != textureMaxAnisotropy)

     {

         mMaxAnisotropy = textureMaxAnisotropy;

@@ -470,7 +470,7 @@
 bool Texture::copy(egl::Image *source, const sw::SliceRect &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, GLint zoffset, egl::Image *dest)

 {

     Device *device = getDevice();

-	

+

     sw::SliceRect destRect(xoffset, yoffset, xoffset + (sourceRect.x1 - sourceRect.x0), yoffset + (sourceRect.y1 - sourceRect.y0), zoffset);

     bool success = device->stretchRect(source, &sourceRect, dest, &destRect, false);

 

@@ -504,12 +504,12 @@
 {

 	for(int i = 0; i < MIPMAP_LEVELS; i++)

 	{

-		image[i] = 0;

+		image[i] = nullptr;

 	}

 

-    mSurface = NULL;

+    mSurface = nullptr;

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 	mProxyRefs = 0;

 }

 

@@ -522,7 +522,7 @@
 		if(image[i])

 		{

 			image[i]->unbind(this);

-			image[i] = 0;

+			image[i] = nullptr;

 		}

 	}

 

@@ -530,15 +530,15 @@
 

     if(mSurface)

     {

-        mSurface->setBoundTexture(NULL);

-        mSurface = NULL;

+        mSurface->setBoundTexture(nullptr);

+        mSurface = nullptr;

     }

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 }

 

-// We need to maintain a count of references to renderbuffers acting as 

-// proxies for this texture, so that we do not attempt to use a pointer 

+// We need to maintain a count of references to renderbuffers acting as

+// proxies for this texture, so that we do not attempt to use a pointer

 // to a renderbuffer proxy which has been deleted.

 void Texture2D::addProxyRef(const Renderbuffer *proxy)

 {

@@ -554,7 +554,7 @@
 

     if(mProxyRefs == 0)

 	{

-		mColorbufferProxy = NULL;

+		mColorbufferProxy = nullptr;

 	}

 }

 

@@ -649,7 +649,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 

@@ -666,7 +666,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 }

@@ -878,7 +878,7 @@
 	}

 

     unsigned int q = log2(std::max(image[0]->getWidth(), image[0]->getHeight()));

-    

+

 	for(unsigned int i = 1; i <= q; i++)

     {

 		if(image[i])

@@ -906,10 +906,10 @@
 {

     if(target != GL_TEXTURE_2D)

     {

-        return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);

+        return error(GL_INVALID_OPERATION, (Renderbuffer*)nullptr);

     }

 

-    if(mColorbufferProxy == NULL)

+    if(!mColorbufferProxy)

     {

         mColorbufferProxy = new Renderbuffer(name, new RenderbufferTexture2D(this, level));

     }

@@ -954,13 +954,13 @@
 	{

 		for(int i = 0; i < MIPMAP_LEVELS; i++)

 		{

-			image[f][i] = 0;

+			image[f][i] = nullptr;

 		}

 	}

 

 	for(int f = 0; f < 6; f++)

     {

-        mFaceProxies[f] = NULL;

+        mFaceProxies[f] = nullptr;

         mFaceProxyRefs[f] = 0;

 	}

 }

@@ -976,7 +976,7 @@
 			if(image[f][i])

 			{

 				image[f][i]->unbind(this);

-				image[f][i] = 0;

+				image[f][i] = nullptr;

 			}

 		}

 	}

@@ -985,14 +985,14 @@
 

     for(int i = 0; i < 6; i++)

     {

-        mFaceProxies[i] = NULL;

+        mFaceProxies[i] = nullptr;

     }

 }

 

-// We need to maintain a count of references to renderbuffers acting as 

-// proxies for this texture, so that the texture is not deleted while 

+// We need to maintain a count of references to renderbuffers acting as

+// proxies for this texture, so that the texture is not deleted while

 // proxy references still exist. If the reference count drops to zero,

-// we set our proxy pointer NULL, so that a new attempt at referencing

+// we set our proxy pointer null, so that a new attempt at referencing

 // will cause recreation.

 void TextureCubeMap::addProxyRef(const Renderbuffer *proxy)

 {

@@ -1018,7 +1018,7 @@
 

             if(mFaceProxyRefs[f] == 0)

 			{

-				mFaceProxies[f] = NULL;

+				mFaceProxies[f] = nullptr;

 			}

 		}

     }

@@ -1270,7 +1270,7 @@
 

 		sw::SliceRect sourceRect(x, y, x + width, y + height, 0);

 		sourceRect.clip(0, 0, renderbuffer->getWidth(), renderbuffer->getHeight());

-        

+

 		copy(renderTarget, sourceRect, sizedInternalFormat, 0, 0, 0, image[face][level]);

     }

 

@@ -1361,12 +1361,12 @@
 {

     if(!IsCubemapTextureTarget(target))

     {

-        return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);

+        return error(GL_INVALID_OPERATION, (Renderbuffer*)nullptr);

     }

 

     int face = CubeFaceIndex(target);

 

-    if(mFaceProxies[face] == NULL)

+    if(!mFaceProxies[face])

     {

         mFaceProxies[face] = new Renderbuffer(name, new RenderbufferTextureCubeMap(this, target, level));

     }

@@ -1378,7 +1378,7 @@
 {

     ASSERT(IsCubemapTextureTarget(target));

     ASSERT(level < IMPLEMENTATION_MAX_TEXTURE_LEVELS);

-    

+

 	int face = CubeFaceIndex(target);

 

 	if(image[face][level])

@@ -1408,12 +1408,12 @@
 {

 	for(int i = 0; i < MIPMAP_LEVELS; i++)

 	{

-		image[i] = 0;

+		image[i] = nullptr;

 	}

 

-	mSurface = NULL;

+	mSurface = nullptr;

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 	mProxyRefs = 0;

 }

 

@@ -1426,7 +1426,7 @@
 		if(image[i])

 		{

 			image[i]->unbind(this);

-			image[i] = 0;

+			image[i] = nullptr;

 		}

 	}

 

@@ -1434,15 +1434,15 @@
 

 	if(mSurface)

 	{

-		mSurface->setBoundTexture(NULL);

-		mSurface = NULL;

+		mSurface->setBoundTexture(nullptr);

+		mSurface = nullptr;

 	}

 

-	mColorbufferProxy = NULL;

+	mColorbufferProxy = nullptr;

 }

 

-// We need to maintain a count of references to renderbuffers acting as 

-// proxies for this texture, so that we do not attempt to use a pointer 

+// We need to maintain a count of references to renderbuffers acting as

+// proxies for this texture, so that we do not attempt to use a pointer

 // to a renderbuffer proxy which has been deleted.

 void Texture3D::addProxyRef(const Renderbuffer *proxy)

 {

@@ -1458,7 +1458,7 @@
 

 	if(mProxyRefs == 0)

 	{

-		mColorbufferProxy = NULL;

+		mColorbufferProxy = nullptr;

 	}

 }

 

@@ -1555,7 +1555,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 

@@ -1572,7 +1572,7 @@
 		if(image[level])

 		{

 			image[level]->unbind(this);

-			image[level] = 0;

+			image[level] = nullptr;

 		}

 	}

 }

@@ -1819,10 +1819,10 @@
 {

 	if(target != getTarget())

 	{

-		return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);

+		return error(GL_INVALID_OPERATION, (Renderbuffer*)nullptr);

 	}

 

-	if(mColorbufferProxy == NULL)

+	if(!mColorbufferProxy)

 	{

 		mColorbufferProxy = new Renderbuffer(name, new RenderbufferTexture3D(this, level, layer));

 	}

@@ -1935,10 +1935,10 @@
 {

 	if(config)

 	{

-		return new egl::Image(width, height, config->mRenderTargetFormat, config->mSamples, false, true);

+		return new egl::Image(width, height, config->mRenderTargetFormat, config->mSamples, false);

 	}

 

-	return 0;

+	return nullptr;

 }

 

 egl::Image *createDepthStencil(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard)

@@ -1948,7 +1948,7 @@
 		ERR("Invalid parameters: %dx%d", width, height);

 		return 0;

 	}

-		

+

 	bool lockable = true;

 

 	switch(format)

@@ -1974,12 +1974,12 @@
 		UNREACHABLE(format);

 	}

 

-	egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable, true);

+	egl::Image *surface = new egl::Image(width, height, format, multiSampleDepth, lockable);

 

 	if(!surface)

 	{

 		ERR("Out of memory");

-		return 0;

+		return nullptr;

 	}

 

 	return surface;