Refactor FrameBuffer blit/flip source.

Pass a surface to the blit/flip functions, instead of a raw pointer.
This puts the FrameBuffer in control of locking and unlocking.

Change-Id: I55335b3beef8d7083aae7687bd25392964261bde
Reviewed-on: https://swiftshader-review.googlesource.com/4482
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/D3D8/Direct3DSwapChain8.cpp b/src/D3D8/Direct3DSwapChain8.cpp
index a49bf11..d0e3b97 100644
--- a/src/D3D8/Direct3DSwapChain8.cpp
+++ b/src/D3D8/Direct3DSwapChain8.cpp
@@ -81,13 +81,9 @@
 			profiler.nextFrame();
 		#endif
 
-		void *source = backBuffer[0]->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);   // FIXME: External
-		sw::Format format = backBuffer[0]->getInternalFormat();
-		int stride = backBuffer[0]->getInternalPitchB();
-
 		if(!sourceRect && !destRect)   // FIXME: More cases?
 		{
-			frameBuffer->flip(destWindowOverride, source, format, stride);
+			frameBuffer->flip(destWindowOverride, backBuffer[0]);
 		}
 		else   // TODO: Check for SWAPEFFECT_COPY
 		{
@@ -110,11 +106,9 @@
 				dRect.y1 = destRect->bottom;
 			}
 
-			frameBuffer->blit(destWindowOverride, source, sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr, format, stride);
+			frameBuffer->blit(destWindowOverride, backBuffer[0], sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr);
 		}
 
-		backBuffer[0]->unlockInternal();   // FIXME: External
-
 		return D3D_OK;
 	}
 
@@ -133,7 +127,7 @@
 		}
 
 		this->backBuffer[index]->AddRef();
-		*backBuffer = this->backBuffer[index]; 
+		*backBuffer = this->backBuffer[index];
 
 		return D3D_OK;
 	}
@@ -155,7 +149,7 @@
 		device->GetCreationParameters(&creationParameters);
 
 		HWND windowHandle = presentParameters->hDeviceWindow ? presentParameters->hDeviceWindow : creationParameters.hFocusWindow;
-			
+
 		int width = 0;
 		int height = 0;
 
@@ -222,7 +216,7 @@
 	{
 		return backBuffer[index]->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);   // FIXME: External
 	}
-	
+
 	void Direct3DSwapChain8::unlockBackBuffer(int index)
 	{
 		backBuffer[index]->unlockInternal();   // FIXME: External
diff --git a/src/D3D9/Direct3DSwapChain9.cpp b/src/D3D9/Direct3DSwapChain9.cpp
index 4b510aa..b446a9f 100644
--- a/src/D3D9/Direct3DSwapChain9.cpp
+++ b/src/D3D9/Direct3DSwapChain9.cpp
@@ -149,9 +149,6 @@
 		#endif
 
 		HWND window = destWindowOverride ? destWindowOverride : presentParameters.hDeviceWindow;
-		void *source = backBuffer[0]->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);   // FIXME: External
-		sw::Format format = backBuffer[0]->getInternalFormat();
-		int stride = backBuffer[0]->getInternalPitchB();
 
 		POINT point;
 		GetCursorPos(&point);
@@ -161,7 +158,7 @@
 
 		if(!sourceRect && !destRect)   // FIXME: More cases?
 		{
-			frameBuffer->flip(window, source, format, stride);
+			frameBuffer->flip(window, backBuffer[0]);
 		}
 		else   // FIXME: Check for SWAPEFFECT_COPY
 		{
@@ -184,11 +181,9 @@
 				dRect.y1 = destRect->bottom;
 			}
 
-			frameBuffer->blit(window, source, sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr, format, stride);
+			frameBuffer->blit(window, backBuffer[0], sourceRect ? &sRect : nullptr, destRect ? &dRect : nullptr);
 		}
 
-		backBuffer[0]->unlockInternal();   // FIXME: External
-
 		return D3D_OK;
 	}
 
diff --git a/src/Main/FrameBuffer.cpp b/src/Main/FrameBuffer.cpp
index ccf9083..82dff46 100644
--- a/src/Main/FrameBuffer.cpp
+++ b/src/Main/FrameBuffer.cpp
@@ -103,7 +103,7 @@
 		cursor.positionY = y;
 	}
 
-	void FrameBuffer::copy(void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBuffer::copy(sw::Surface *source)
 	{
 		if(!source)
 		{
@@ -115,23 +115,23 @@
 			return;
 		}
 
+		int sourceStride = source->getInternalPitchB();
+
 		updateState = {};
 		updateState.width = width;
 		updateState.height = height;
 		updateState.destFormat = format;
 		updateState.destStride = stride;
-		updateState.sourceFormat = sourceFormat;
-		updateState.sourceStride = topLeftOrigin ? (int)sourceStride : -(int)sourceStride;
+		updateState.sourceFormat = source->getInternalFormat();
+		updateState.sourceStride = topLeftOrigin ? sourceStride : -sourceStride;
 		updateState.cursorWidth = cursor.width;
 		updateState.cursorHeight = cursor.height;
 
-		if(topLeftOrigin)
+		renderbuffer = source->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);
+
+		if(!topLeftOrigin)
 		{
-			renderbuffer = source;
-		}
-		else
-		{
-			renderbuffer = (byte*)source + (height - 1) * sourceStride;
+			renderbuffer = (byte*)renderbuffer + (height - 1) * sourceStride;
 		}
 
 		cursor.x = cursor.positionX - cursor.hotspotX;
@@ -147,6 +147,7 @@
 			copyLocked();
 		}
 
+		source->unlockInternal();
 		unlock();
 
 		profiler.nextFrame();   // Assumes every copy() is a full frame
diff --git a/src/Main/FrameBuffer.hpp b/src/Main/FrameBuffer.hpp
index fbb2a79..dd539e1 100644
--- a/src/Main/FrameBuffer.hpp
+++ b/src/Main/FrameBuffer.hpp
@@ -42,8 +42,8 @@
 
 		virtual ~FrameBuffer() = 0;
 
-		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;
+		virtual void flip(sw::Surface *source) = 0;
+		virtual void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) = 0;
 
 		virtual void *lock() = 0;
 		virtual void unlock() = 0;
@@ -55,7 +55,7 @@
 		static Routine *copyRoutine(const BlitState &state);
 
 	protected:
-		void copy(void *source, Format sourceFormat, size_t sourceStride);
+		void copy(sw::Surface *source);
 
 		bool windowed;
 
diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp
index 5bac870..9b47171 100644
--- a/src/Main/FrameBufferAndroid.cpp
+++ b/src/Main/FrameBufferAndroid.cpp
@@ -61,9 +61,9 @@
 		nativeWindow->common.decRef(&nativeWindow->common);
 	}
 
-	void FrameBufferAndroid::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferAndroid::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		if(buffer)
 		{
diff --git a/src/Main/FrameBufferAndroid.hpp b/src/Main/FrameBufferAndroid.hpp
index 4400188..b71c32b 100644
--- a/src/Main/FrameBufferAndroid.hpp
+++ b/src/Main/FrameBufferAndroid.hpp
@@ -26,12 +26,12 @@
 	class FrameBufferAndroid : public FrameBuffer
 	{
 	public:
-		FrameBufferAndroid(ANativeWindow* window, int width, int height);
+		FrameBufferAndroid(ANativeWindow *window, int width, int height);
 
 		~FrameBufferAndroid() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override {blit(source, 0, 0, sourceFormat, sourceStride);};
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override {blit(source, nullptr, nullptr);};
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
@@ -39,8 +39,8 @@
 		bool setSwapRectangle(int l, int t, int w, int h);
 
 	private:
-		ANativeWindow* nativeWindow;
-		ANativeWindowBuffer* buffer;
+		ANativeWindow *nativeWindow;
+		ANativeWindowBuffer *buffer;
 	};
 }
 
diff --git a/src/Main/FrameBufferDD.cpp b/src/Main/FrameBufferDD.cpp
index 1f87bdf..46ed89f 100644
--- a/src/Main/FrameBufferDD.cpp
+++ b/src/Main/FrameBufferDD.cpp
@@ -250,9 +250,9 @@
 		updateBounds(windowHandle);
 	}
 
-	void FrameBufferDD::flip(void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBufferDD::flip(sw::Surface *source)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		if(!readySurfaces())
 		{
@@ -281,9 +281,9 @@
 		}
 	}
 
-	void FrameBufferDD::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferDD::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		if(!readySurfaces())
 		{
@@ -320,20 +320,20 @@
 		}
 	}
 
-	void FrameBufferDD::flip(HWND windowOverride, void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBufferDD::flip(HWND windowOverride, sw::Surface *source)
 	{
 		updateClipper(windowOverride);
 		updateBounds(windowOverride);
 
-		flip(source, sourceFormat, sourceStride);
+		flip(source);
 	}
 
-	void FrameBufferDD::blit(HWND windowOverride, void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferDD::blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
 		updateClipper(windowOverride);
 		updateBounds(windowOverride);
 
-		blit(source, sourceRect, destRect, sourceFormat, sourceStride);
+		blit(source, sourceRect, destRect);
 	}
 
 	void FrameBufferDD::screenshot(void *destBuffer)
diff --git a/src/Main/FrameBufferDD.hpp b/src/Main/FrameBufferDD.hpp
index efe6d68..22d76c9 100644
--- a/src/Main/FrameBufferDD.hpp
+++ b/src/Main/FrameBufferDD.hpp
@@ -28,11 +28,11 @@
 
 		~FrameBufferDD() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override;
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override;
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
-		void flip(HWND windowOverride, void *source, Format sourceFormat, size_t sourceStride) override;
-		void blit(HWND windowOverride, void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(HWND windowOverride, sw::Surface *source) override;
+		void blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
diff --git a/src/Main/FrameBufferGDI.cpp b/src/Main/FrameBufferGDI.cpp
index 2668e10..90a469e 100644
--- a/src/Main/FrameBufferGDI.cpp
+++ b/src/Main/FrameBufferGDI.cpp
@@ -71,14 +71,14 @@
 	{
 	}
 
-	void FrameBufferGDI::flip(void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBufferGDI::flip(sw::Surface *source)
 	{
-		blit(source, 0, 0, sourceFormat, sourceStride);
+		blit(source, nullptr, nullptr);
 	}
 
-	void FrameBufferGDI::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferGDI::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		int sourceLeft = sourceRect ? sourceRect->x0 : 0;
 		int sourceTop = sourceRect ? sourceRect->y0 : 0;
@@ -92,12 +92,12 @@
 		StretchBlt(windowContext, destLeft, destTop, destWidth, destHeight, bitmapContext, sourceLeft, sourceTop, sourceWidth, sourceHeight, SRCCOPY);
 	}
 
-	void FrameBufferGDI::flip(HWND windowOverride, void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBufferGDI::flip(HWND windowOverride, sw::Surface *source)
 	{
-		blit(windowOverride, source, 0, 0, sourceFormat, sourceStride);
+		blit(windowOverride, source, nullptr, nullptr);
 	}
 
-	void FrameBufferGDI::blit(HWND windowOverride, void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferGDI::blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
 		if(windowed && windowOverride != 0 && windowOverride != bitmapWindow)
 		{
@@ -105,7 +105,7 @@
 			init(windowOverride);
 		}
 
-		blit(source, sourceRect, destRect, sourceFormat, sourceStride);
+		blit(source, sourceRect, destRect);
 	}
 
 	void FrameBufferGDI::setGammaRamp(GammaRamp *gammaRamp, bool calibrate)
diff --git a/src/Main/FrameBufferGDI.hpp b/src/Main/FrameBufferGDI.hpp
index efe44a9..add2504 100644
--- a/src/Main/FrameBufferGDI.hpp
+++ b/src/Main/FrameBufferGDI.hpp
@@ -26,11 +26,11 @@
 
 		~FrameBufferGDI() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override;
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override;
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
-		void flip(HWND windowOverride, void *source, Format sourceFormat, size_t sourceStride) override;
-		void blit(HWND windowOverride, void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(HWND windowOverride, sw::Surface *source) override;
+		void blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
@@ -48,7 +48,7 @@
 		HDC windowContext;
 		HDC bitmapContext;
 		HWND bitmapWindow;
-		
+
 		HBITMAP bitmap;
 	};
 }
diff --git a/src/Main/FrameBufferOSX.hpp b/src/Main/FrameBufferOSX.hpp
index 41f95fe..07f8d63 100644
--- a/src/Main/FrameBufferOSX.hpp
+++ b/src/Main/FrameBufferOSX.hpp
@@ -29,8 +29,8 @@
 		FrameBufferOSX(CALayer *layer, int width, int height);
 		~FrameBufferOSX() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override;
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override;
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
diff --git a/src/Main/FrameBufferOSX.mm b/src/Main/FrameBufferOSX.mm
index c99d11a..6d58ae7 100644
--- a/src/Main/FrameBufferOSX.mm
+++ b/src/Main/FrameBufferOSX.mm
@@ -45,14 +45,14 @@
 		delete[] buffer;
 	}
 
-	void FrameBufferOSX::flip(void *source, Format sourceFormat, size_t sourceStride)
+	void FrameBufferOSX::flip(sw::Surface *source)
 	{
-		blit(source, nullptr, nullptr, sourceFormat, sourceStride);
+		blit(source, nullptr, nullptr);
 	}
 
-	void FrameBufferOSX::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferOSX::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		int bytesPerRow = width * 4 * sizeof(uint8_t);
 		CGImageRef image = CGImageCreate(width, height, 8, 32, bytesPerRow, colorspace, kCGBitmapByteOrder32Big, provider, nullptr, false, kCGRenderingIntentDefault);
diff --git a/src/Main/FrameBufferOzone.cpp b/src/Main/FrameBufferOzone.cpp
index 3261f30..c551a46 100644
--- a/src/Main/FrameBufferOzone.cpp
+++ b/src/Main/FrameBufferOzone.cpp
@@ -42,9 +42,9 @@
 		framebuffer = nullptr;
 	}
 
-	void FrameBufferOzone::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferOzone::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 	}
 }
 
diff --git a/src/Main/FrameBufferOzone.hpp b/src/Main/FrameBufferOzone.hpp
index 6843926..0dc9f60 100644
--- a/src/Main/FrameBufferOzone.hpp
+++ b/src/Main/FrameBufferOzone.hpp
@@ -26,8 +26,8 @@
 
 		~FrameBufferOzone() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override {blit(source, 0, 0, sourceFormat, sourceStride);};
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override {blit(source, nullptr, nullptr);};
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
diff --git a/src/Main/FrameBufferWin.hpp b/src/Main/FrameBufferWin.hpp
index e302195..15c1e0e 100644
--- a/src/Main/FrameBufferWin.hpp
+++ b/src/Main/FrameBufferWin.hpp
@@ -33,11 +33,11 @@
 
 		~FrameBufferWin() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override = 0;
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override = 0;
+		void flip(sw::Surface *source) override = 0;
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override = 0;
 
-		virtual void flip(HWND windowOverride, void *source, Format sourceFormat, size_t sourceStride) = 0;
-		virtual void blit(HWND windowOverride, void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) = 0;
+		virtual void flip(HWND windowOverride, sw::Surface *source) = 0;
+		virtual void blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect) = 0;
 
 		virtual void setGammaRamp(GammaRamp *gammaRamp, bool calibrate) = 0;
 		virtual void getGammaRamp(GammaRamp *gammaRamp) = 0;
diff --git a/src/Main/FrameBufferX11.cpp b/src/Main/FrameBufferX11.cpp
index 2e50f42..ca0bb4b 100644
--- a/src/Main/FrameBufferX11.cpp
+++ b/src/Main/FrameBufferX11.cpp
@@ -130,9 +130,9 @@
 		framebuffer = nullptr;
 	}
 
-	void FrameBufferX11::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
+	void FrameBufferX11::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
 	{
-		copy(source, sourceFormat, sourceStride);
+		copy(source);
 
 		if(!mit_shm)
 		{
diff --git a/src/Main/FrameBufferX11.hpp b/src/Main/FrameBufferX11.hpp
index ab0c205..f25487b 100644
--- a/src/Main/FrameBufferX11.hpp
+++ b/src/Main/FrameBufferX11.hpp
@@ -31,8 +31,8 @@
 
 		~FrameBufferX11() override;
 
-		void flip(void *source, Format sourceFormat, size_t sourceStride) override {blit(source, 0, 0, sourceFormat, sourceStride);};
-		void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) override;
+		void flip(sw::Surface *source) override {blit(source, nullptr, nullptr);};
+		void blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) override;
 
 		void *lock() override;
 		void unlock() override;
diff --git a/src/OpenGL/libEGL/Surface.cpp b/src/OpenGL/libEGL/Surface.cpp
index 4e05d5a..ad3bdd2 100644
--- a/src/OpenGL/libEGL/Surface.cpp
+++ b/src/OpenGL/libEGL/Surface.cpp
@@ -259,9 +259,7 @@
 {
 	if(backBuffer && frameBuffer)
 	{
-		void *source = backBuffer->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);
-		frameBuffer->flip(source, backBuffer->sw::Surface::getInternalFormat(), backBuffer->getInternalPitchB());
-		backBuffer->unlockInternal();
+		frameBuffer->flip(backBuffer);
 
 		checkForResize();
 	}
diff --git a/src/OpenGL/libGL/Surface.cpp b/src/OpenGL/libGL/Surface.cpp
index a6ec333..f5bfa0e 100644
--- a/src/OpenGL/libGL/Surface.cpp
+++ b/src/OpenGL/libGL/Surface.cpp
@@ -160,9 +160,7 @@
 {
 	if(backBuffer)
 	{
-		void *source = backBuffer->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);
-		frameBuffer->flip(source, backBuffer->Surface::getInternalFormat(), backBuffer->getInternalPitchB());
-		backBuffer->unlockInternal();
+		frameBuffer->flip(backBuffer);
 
 		checkForResize();
 	}