Implement an OpenGL 2.1 prototype.

Bug 18962347

Change-Id: I9a7b07647b1b3f561dd9e4597670e63641b155a8
Reviewed-on: https://swiftshader-review.googlesource.com/1810
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/libGL/Image.cpp b/src/OpenGL/libGL/Image.cpp
index 4168d82..deb3df8 100644
--- a/src/OpenGL/libGL/Image.cpp
+++ b/src/OpenGL/libGL/Image.cpp
@@ -16,7 +16,11 @@
 #include "../common/debug.h"
 #include "Common/Thread.hpp"
 
-#include <GLES2/gl2ext.h>
+#define _GDI32_
+#include <windows.h>
+#include <GL/GL.h>
+#define GL_GLEXT_PROTOTYPES
+#include <GL/glext.h>
 
 namespace gl
 {
@@ -31,15 +35,16 @@
 	}
 
 	Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, GLenum format, GLenum type)
-		: parentTexture(parentTexture)
-		, egl::Image(getParentResource(parentTexture), width, height, 1, format, type, selectInternalFormat(format, type))
+		: parentTexture(parentTexture), width(width), height(height), format(format), type(type)
+		, internalFormat(selectInternalFormat(format, type)), multiSampleDepth(1)
+		, sw::Surface(getParentResource(parentTexture), width, height, 1, selectInternalFormat(format, type), true, true)
 	{
 		referenceCount = 1;
 	}
 
 	Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, sw::Format internalFormat, int multiSampleDepth, bool lockable, bool renderTarget)
-		: parentTexture(parentTexture)
-		, egl::Image(getParentResource(parentTexture), width, height, multiSampleDepth, internalFormat, lockable, renderTarget)
+		: parentTexture(parentTexture), width(width), height(height), internalFormat(internalFormat), format(0 /*GL_NONE*/), type(0 /*GL_NONE*/), multiSampleDepth(multiSampleDepth)
+		, sw::Surface(getParentResource(parentTexture), width, height, multiSampleDepth, internalFormat, lockable, renderTarget)
 	{
 		referenceCount = 1;
 	}
@@ -49,6 +54,51 @@
 		ASSERT(referenceCount == 0);
 	}
 
+	void *Image::lock(unsigned int left, unsigned int top, sw::Lock lock)
+	{
+		return lockExternal(left, top, 0, lock, sw::PUBLIC);
+	}
+
+	unsigned int Image::getPitch() const
+	{
+		return getExternalPitchB();
+	}
+
+	void Image::unlock()
+	{
+		unlockExternal();
+	}
+
+	int Image::getWidth()
+	{
+		return width;
+	}
+	
+	int Image::getHeight()
+	{
+		return height;
+	}
+
+	GLenum Image::getFormat()
+	{
+		return format;
+	}
+	
+	GLenum Image::getType()
+	{
+		return type;
+	}
+	
+	sw::Format Image::getInternalFormat()
+	{
+		return internalFormat;
+	}
+	
+	int Image::getMultiSampleDepth()
+	{
+		return multiSampleDepth;
+	}
+
 	void Image::addRef()
 	{
 		if(parentTexture)
@@ -73,39 +123,35 @@
 
 		if(referenceCount == 0)
 		{
-			ASSERT(!shared);   // Should still hold a reference if eglDestroyImage hasn't been called
 			delete this;
 		}
 	}
 
-	void Image::unbind(const egl::Texture *parent)
+	void Image::unbind()
 	{
-		if(parentTexture == parent)
-		{
-			parentTexture = 0;
-		}
+		parentTexture = 0;
 
 		release();
 	}
 
 	sw::Format Image::selectInternalFormat(GLenum format, GLenum type)
 	{
-		if(format == GL_ETC1_RGB8_OES)
-		{
-			return sw::FORMAT_ETC1;
-		}
-		else
+        if(type == GL_NONE && format == GL_NONE)
+        {
+            return sw::FORMAT_NULL;
+        }
+        else
 		#if S3TC_SUPPORT
 		if(format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||
 		   format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
 		{
 			return sw::FORMAT_DXT1;
 		}
-		else if(format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE)
+		else if(format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)
 		{
 			return sw::FORMAT_DXT3;
 		}
-		else if(format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE)
+		else if(format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
 		{
 			return sw::FORMAT_DXT5;
 		}
@@ -115,7 +161,7 @@
 		{
 			return sw::FORMAT_A32B32G32R32F;
 		}
-		else if(type == GL_HALF_FLOAT_OES)
+		else if(type == GL_HALF_FLOAT)
 		{
 			return sw::FORMAT_A16B16G16R16F;
 		}
@@ -151,9 +197,9 @@
 			}
 			else UNREACHABLE();
 		}
-		else if(type == GL_UNSIGNED_INT_24_8_OES)
+		else if(type == GL_UNSIGNED_INT_24_8_EXT)
 		{
-			if(format == GL_DEPTH_STENCIL_OES)
+			if(format == GL_DEPTH_STENCIL_EXT)
 			{
 				return sw::FORMAT_D32FS8_TEXTURE;
 			}
@@ -171,6 +217,11 @@
 		{
 			return sw::FORMAT_X8R8G8B8;
 		}
+        else if(type == GL_UNSIGNED_INT_8_8_8_8_REV)
+        {
+            return sw::FORMAT_A8R8G8B8;
+        }
+
 		else UNREACHABLE();
 
 		return sw::FORMAT_A8R8G8B8;
@@ -186,6 +237,7 @@
 			switch(type)
 			{
 			case GL_UNSIGNED_BYTE:
+            case GL_UNSIGNED_INT_8_8_8_8_REV:
 				switch(format)
 				{
 				case GL_ALPHA:
@@ -258,7 +310,7 @@
 				default: UNREACHABLE();
 				}
 				break;
-			  case GL_HALF_FLOAT_OES:
+			  case GL_HALF_FLOAT:
 				switch(format)
 				{
 				// float textures are converted to RGBA, not BGRA
@@ -286,7 +338,7 @@
 			case GL_UNSIGNED_INT:
 				loadD32ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
 				break;
-			case GL_UNSIGNED_INT_24_8_OES:
+			case GL_UNSIGNED_INT_24_8_EXT:
 				loadD24S8ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
 				break;
 			default: UNREACHABLE();