Refactor FrameBuffer state.

This mainly groups the state that is used for generating a new blit
routine into a second BlitState structure 'updateState'. It also
allowed for the FrameBuffer's own parameters to not have a 'dest'
prefix. Also, 'locked' was renamed to 'framebuffer', and 'target' to
'renderbuffer'.

Change-Id: I64e26f0b06f9f4419b8ca67e6fbb0dee8272898a
Reviewed-on: https://swiftshader-review.googlesource.com/11510
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Main/FrameBuffer.cpp b/src/Main/FrameBuffer.cpp
index 96b8e02..29371db 100644
--- a/src/Main/FrameBuffer.cpp
+++ b/src/Main/FrameBuffer.cpp
@@ -27,7 +27,7 @@
 #include <cutils/properties.h>
 #endif
 
-#define ASYNCHRONOUS_BLIT 0   // FIXME: Currently leads to rare race conditions
+#define ASYNCHRONOUS_BLIT false   // FIXME: Currently leads to rare race conditions
 
 namespace sw
 {
@@ -40,30 +40,18 @@
 	{
 		this->topLeftOrigin = topLeftOrigin;
 
-		locked = nullptr;
+		framebuffer = nullptr;
 
 		this->width = width;
 		this->height = height;
-		destFormat = FORMAT_X8R8G8B8;
-		sourceFormat = FORMAT_X8R8G8B8;
+		format = FORMAT_X8R8G8B8;
 		stride = 0;
 
-		if(forceWindowed)
-		{
-			fullscreen = false;
-		}
-
-		windowed = !fullscreen;
+		windowed = !fullscreen || forceWindowed;
 
 		blitFunction = nullptr;
 		blitRoutine = nullptr;
-
-		blitState.width = 0;
-		blitState.height = 0;
-		blitState.destFormat = FORMAT_X8R8G8B8;
-		blitState.sourceFormat = FORMAT_X8R8G8B8;
-		blitState.cursorWidth = 0;
-		blitState.cursorHeight = 0;
+		blitState = {};
 
 		if(ASYNCHRONOUS_BLIT)
 		{
@@ -86,21 +74,6 @@
 		delete blitRoutine;
 	}
 
-	int FrameBuffer::getWidth() const
-	{
-		return width;
-	}
-
-	int FrameBuffer::getHeight() const
-	{
-		return height;
-	}
-
-	int FrameBuffer::getStride() const
-	{
-		return stride;
-	}
-
 	void FrameBuffer::setCursorImage(sw::Surface *cursorImage)
 	{
 		if(cursorImage)
@@ -130,7 +103,7 @@
 		cursor.positionY = y;
 	}
 
-	void FrameBuffer::copy(void *source, Format format, size_t stride)
+	void FrameBuffer::copy(void *source, Format sourceFormat, size_t sourceStride)
 	{
 		if(!source)
 		{
@@ -142,15 +115,22 @@
 			return;
 		}
 
-		sourceFormat = format;
+		updateState = {};
+		updateState.width = width;
+		updateState.height = height;
+		updateState.destFormat = format;
+		updateState.sourceFormat = sourceFormat;
+		updateState.stride = topLeftOrigin ? (int)sourceStride : -(int)sourceStride;
+		updateState.cursorWidth = cursor.width;
+		updateState.cursorHeight = cursor.height;
 
 		if(topLeftOrigin)
 		{
-			target = source;
+			renderbuffer = source;
 		}
 		else
 		{
-			target = (byte*)source + (height - 1) * stride;
+			renderbuffer = (byte*)source + (height - 1) * sourceStride;
 		}
 
 		cursor.x = cursor.positionX - cursor.hotspotX;
@@ -173,25 +153,16 @@
 
 	void FrameBuffer::copyLocked()
 	{
-		BlitState update = {};
-		update.width = width;
-		update.height = height;
-		update.destFormat = destFormat;
-		update.sourceFormat = sourceFormat;
-		update.stride = stride;
-		update.cursorWidth = cursor.width;
-		update.cursorHeight = cursor.height;
-
-		if(memcmp(&blitState, &update, sizeof(BlitState)) != 0)
+		if(memcmp(&blitState, &updateState, sizeof(BlitState)) != 0)
 		{
-			blitState = update;
+			blitState = updateState;
 			delete blitRoutine;
 
 			blitRoutine = copyRoutine(blitState);
 			blitFunction = (void(*)(void*, void*, Cursor*))blitRoutine->getEntry();
 		}
 
-		blitFunction(locked, target, &cursor);
+		blitFunction(framebuffer, renderbuffer, &cursor);
 	}
 
 	Routine *FrameBuffer::copyRoutine(const BlitState &state)
diff --git a/src/Main/FrameBuffer.hpp b/src/Main/FrameBuffer.hpp
index a18a0b1..f0e00a4 100644
--- a/src/Main/FrameBuffer.hpp
+++ b/src/Main/FrameBuffer.hpp
@@ -41,10 +41,6 @@
 
 		virtual ~FrameBuffer() = 0;
 
-		int getWidth() const;
-		int getHeight() const;
-		int getStride() const;
-
 		virtual void flip(void *source, Format sourceFormat, size_t sourceStride) = 0;
 		virtual void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) = 0;
 
@@ -58,22 +54,22 @@
 		static Routine *copyRoutine(const BlitState &state);
 
 	protected:
-		void copy(void *source, Format format, size_t stride);
-		int width;
-		int height;
-		Format sourceFormat;
-		Format destFormat;
-		int stride;
+		void copy(void *source, Format sourceFormat, size_t sourceStride);
+
 		bool windowed;
 
-		void *locked;   // Video memory back buffer
+		void *framebuffer;   // Native window buffer.
+		int width;
+		int height;
+		int stride;
+		Format format;
 
 	private:
 		void copyLocked();
 
 		static void threadFunction(void *parameters);
 
-		void *target;   // Render target buffer
+		void *renderbuffer;   // Render target buffer.
 
 		struct Cursor
 		{
@@ -92,7 +88,8 @@
 
 		void (*blitFunction)(void *dst, void *src, Cursor *cursor);
 		Routine *blitRoutine;
-		BlitState blitState;
+		BlitState blitState;     // State of the current blitRoutine.
+		BlitState updateState;   // State of the routine to be generated.
 
 		static void blend(const BlitState &state, const Pointer<Byte> &d, const Pointer<Byte> &s, const Pointer<Byte> &c);
 
diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp
index a12ae94..5bac870 100644
--- a/src/Main/FrameBufferAndroid.cpp
+++ b/src/Main/FrameBufferAndroid.cpp
@@ -67,9 +67,9 @@
 
 		if(buffer)
 		{
-			if(locked)
+			if(framebuffer)
 			{
-				locked = nullptr;
+				framebuffer = nullptr;
 				unlock();
 			}
 
@@ -86,7 +86,7 @@
 
 		if(GrallocModule::getInstance()->lock(buffer->handle,
 		                 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
-		                 0, 0, buffer->width, buffer->height, &locked) != 0)
+		                 0, 0, buffer->width, buffer->height, &framebuffer) != 0)
 		{
 			ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
 			return nullptr;
@@ -101,26 +101,26 @@
 
 		switch(buffer->format)
 		{
-		case HAL_PIXEL_FORMAT_RGB_565:   destFormat = FORMAT_R5G6B5; break;
-		case HAL_PIXEL_FORMAT_RGBA_8888: destFormat = FORMAT_A8B8G8R8; break;
+		case HAL_PIXEL_FORMAT_RGB_565:   format = FORMAT_R5G6B5; break;
+		case HAL_PIXEL_FORMAT_RGBA_8888: format = FORMAT_A8B8G8R8; break;
 #if ANDROID_PLATFORM_SDK_VERSION > 16
-		case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: destFormat = FORMAT_X8B8G8R8; break;
+		case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: format = FORMAT_X8B8G8R8; break;
 #endif
-		case HAL_PIXEL_FORMAT_RGBX_8888: destFormat = FORMAT_X8B8G8R8; break;
-		case HAL_PIXEL_FORMAT_BGRA_8888: destFormat = FORMAT_A8R8G8B8; break;
+		case HAL_PIXEL_FORMAT_RGBX_8888: format = FORMAT_X8B8G8R8; break;
+		case HAL_PIXEL_FORMAT_BGRA_8888: format = FORMAT_A8R8G8B8; break;
 		case HAL_PIXEL_FORMAT_RGB_888:
 			// Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
 			ALOGE("Unsupported frame buffer format RGB_888"); ASSERT(false);
-			destFormat = FORMAT_R8G8B8;   // Wrong component order.
+			format = FORMAT_R8G8B8;   // Wrong component order.
 			break;
 		default:
 			ALOGE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
-			destFormat = FORMAT_NULL;
+			format = FORMAT_NULL;
 			break;
 		}
 
-		stride = buffer->stride * Surface::bytes(destFormat);
-		return locked;
+		stride = buffer->stride * Surface::bytes(format);
+		return framebuffer;
 	}
 
 	void FrameBufferAndroid::unlock()
@@ -131,7 +131,7 @@
 			return;
 		}
 
-		locked = nullptr;
+		framebuffer = nullptr;
 
 		if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
 		{
diff --git a/src/Main/FrameBufferDD.cpp b/src/Main/FrameBufferDD.cpp
index 36570bd..1f87bdf 100644
--- a/src/Main/FrameBufferDD.cpp
+++ b/src/Main/FrameBufferDD.cpp
@@ -38,7 +38,7 @@
 		frontBuffer = 0;
 		backBuffer = 0;
 
-		locked = 0;
+		framebuffer = nullptr;
 
 		ddraw = LoadLibrary("ddraw.dll");
 		DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress(ddraw, "DirectDrawCreate");
@@ -106,13 +106,13 @@
 
 			switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
 			{
-			case 32: destFormat = FORMAT_X8R8G8B8; break;
-			case 24: destFormat = FORMAT_R8G8B8;   break;
-			case 16: destFormat = FORMAT_R5G6B5;   break;
-			default: destFormat = FORMAT_NULL;     break;
+			case 32: format = FORMAT_X8R8G8B8; break;
+			case 24: format = FORMAT_R8G8B8;   break;
+			case 16: format = FORMAT_R5G6B5;   break;
+			default: format = FORMAT_NULL;     break;
 			}
 
-			if((result != DD_OK && result != DDERR_PRIMARYSURFACEALREADYEXISTS) || (destFormat == FORMAT_NULL))
+			if((result != DD_OK && result != DDERR_PRIMARYSURFACEALREADYEXISTS) || (format == FORMAT_NULL))
 			{
 				assert(!"Failed to initialize graphics: Incompatible display mode.");
 			}
@@ -206,17 +206,17 @@
 
 		do
 		{
-			destFormat = FORMAT_X8R8G8B8;
+			format = FORMAT_X8R8G8B8;
 			result = directDraw->SetDisplayMode(width, height, 32);
 
 			if(result == DDERR_INVALIDMODE)
 			{
-				destFormat = FORMAT_R8G8B8;
+				format = FORMAT_R8G8B8;
 				result = directDraw->SetDisplayMode(width, height, 24);
 
 				if(result == DDERR_INVALIDMODE)
 				{
-					destFormat = FORMAT_R5G6B5;
+					format = FORMAT_R5G6B5;
 					result = directDraw->SetDisplayMode(width, height, 16);
 
 					if(result == DDERR_INVALIDMODE)
@@ -404,14 +404,14 @@
 
 	void *FrameBufferDD::lock()
 	{
-		if(locked)
+		if(framebuffer)
 		{
-			return locked;
+			return framebuffer;
 		}
 
 		if(!readySurfaces())
 		{
-			return 0;
+			return nullptr;
 		}
 
 		DDSURFACEDESC DDSD;
@@ -425,21 +425,21 @@
 			height = DDSD.dwHeight;
 			stride = DDSD.lPitch;
 
-			locked = DDSD.lpSurface;
+			framebuffer = DDSD.lpSurface;
 
-			return locked;
+			return framebuffer;
 		}
 
-		return 0;
+		return nullptr;
 	}
 
 	void FrameBufferDD::unlock()
 	{
-		if(!locked || !backBuffer) return;
+		if(!framebuffer || !backBuffer) return;
 
 		backBuffer->Unlock(0);
 
-		locked = 0;
+		framebuffer = nullptr;
 	}
 
 	void FrameBufferDD::drawText(int x, int y, const char *string, ...)
diff --git a/src/Main/FrameBufferGDI.cpp b/src/Main/FrameBufferGDI.cpp
index 551a476..2668e10 100644
--- a/src/Main/FrameBufferGDI.cpp
+++ b/src/Main/FrameBufferGDI.cpp
@@ -37,7 +37,7 @@
 
 		init(this->windowHandle);
 
-		destFormat = FORMAT_X8R8G8B8;
+		format = FORMAT_X8R8G8B8;
 	}
 
 	FrameBufferGDI::~FrameBufferGDI()
@@ -64,7 +64,7 @@
 	{
 		stride = width * 4;
 
-		return locked;
+		return framebuffer;
 	}
 
 	void FrameBufferGDI::unlock()
@@ -146,7 +146,7 @@
 		bitmapInfo.bmiHeader.biWidth = width;
 		bitmapInfo.bmiHeader.biCompression = BI_RGB;
 
-		bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &locked, 0, 0);
+		bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &framebuffer, 0, 0);
 		SelectObject(bitmapContext, bitmap);
 
 		updateBounds(window);
diff --git a/src/Main/FrameBufferOSX.mm b/src/Main/FrameBufferOSX.mm
index 3a0cfc7..c99d11a 100644
--- a/src/Main/FrameBufferOSX.mm
+++ b/src/Main/FrameBufferOSX.mm
@@ -25,7 +25,7 @@
 		: FrameBuffer(width, height, false, false), width(width), height(height),
 		  layer(layer), buffer(nullptr), provider(nullptr), currentImage(nullptr)
 	{
-		destFormat = sw::FORMAT_X8B8G8R8;
+		format = sw::FORMAT_X8B8G8R8;
 		int bufferSize = width * height * 4 * sizeof(uint8_t);
 		buffer = new uint8_t[bufferSize];
 		provider = CGDataProviderCreateWithData(nullptr, buffer, bufferSize, nullptr);
@@ -72,13 +72,13 @@
 	void *FrameBufferOSX::lock()
 	{
 		stride = width * 4 * sizeof(uint8_t);
-		locked = buffer;
-		return locked;
+		framebuffer = buffer;
+		return framebuffer;
 	};
 
 	void FrameBufferOSX::unlock()
 	{
-		locked = nullptr;
+		framebuffer = nullptr;
 	};
 }
 
diff --git a/src/Main/FrameBufferOzone.cpp b/src/Main/FrameBufferOzone.cpp
index a9de407..3261f30 100644
--- a/src/Main/FrameBufferOzone.cpp
+++ b/src/Main/FrameBufferOzone.cpp
@@ -18,9 +18,9 @@
 {
 	FrameBufferOzone::FrameBufferOzone(intptr_t display, intptr_t window, int width, int height) : FrameBuffer(width, height, false, false)
 	{
-		buffer = sw::Surface::create(width, height, 1, destFormat, nullptr,
-		                             sw::Surface::pitchB(width, destFormat, true),
-		                             sw::Surface::sliceB(width, height, destFormat, true));
+		buffer = sw::Surface::create(width, height, 1, format, nullptr,
+		                             sw::Surface::pitchB(width, format, true),
+		                             sw::Surface::sliceB(width, height, format, true));
 	}
 
 	FrameBufferOzone::~FrameBufferOzone()
@@ -30,16 +30,16 @@
 
 	void *FrameBufferOzone::lock()
 	{
-		locked = buffer->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);
+		framebuffer = buffer->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);
 
-		return locked;
+		return framebuffer;
 	}
 
 	void FrameBufferOzone::unlock()
 	{
 		buffer->unlockInternal();
 
-		locked = nullptr;
+		framebuffer = nullptr;
 	}
 
 	void FrameBufferOzone::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
diff --git a/src/Main/FrameBufferX11.cpp b/src/Main/FrameBufferX11.cpp
index 365c638..2e50f42 100644
--- a/src/Main/FrameBufferX11.cpp
+++ b/src/Main/FrameBufferX11.cpp
@@ -120,14 +120,14 @@
 	void *FrameBufferX11::lock()
 	{
 		stride = x_image->bytes_per_line;
-		locked = buffer;
+		framebuffer = buffer;
 
-		return locked;
+		return framebuffer;
 	}
 
 	void FrameBufferX11::unlock()
 	{
-		locked = nullptr;
+		framebuffer = nullptr;
 	}
 
 	void FrameBufferX11::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
diff --git a/src/OpenGL/libGL/Image.cpp b/src/OpenGL/libGL/Image.cpp
index 8126de2..4547d92 100644
--- a/src/OpenGL/libGL/Image.cpp
+++ b/src/OpenGL/libGL/Image.cpp
@@ -33,7 +33,7 @@
 			return texture->getResource();
 		}
 
-		return 0;
+		return nullptr;
 	}
 
 	Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, GLenum format, GLenum type)
@@ -141,7 +141,7 @@
 
 	void Image::unbind()
 	{
-		parentTexture = 0;
+		parentTexture = nullptr;
 
 		release();
 	}