Removed some dependencies on old threading class

- Added separate mutexes for both caches in the Blitter and used
  std::mutex instead of MutexLock.
- Removed some now unused inclusions/forward declaration from the
  Context class and fixed the fallout of doing that in other files.
- Also moved SwiftConfig to std::thread/std::mutex
- Removed unused inclusions of System/Thread.hpp where possible.

Bug b/132280877

Change-Id: Ic1a992ee3161c141ec1a16471420955c6309f58f
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31031
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Alexis Hétu <sugoi@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Device/Blitter.cpp b/src/Device/Blitter.cpp
index 957f819..ce4a2b0 100644
--- a/src/Device/Blitter.cpp
+++ b/src/Device/Blitter.cpp
@@ -25,16 +25,16 @@
 
 namespace sw
 {
-	Blitter::Blitter()
+	Blitter::Blitter() :
+		blitMutex(),
+		blitCache(1024),
+		cornerUpdateMutex(),
+		cornerUpdateCache(64) // We only need one of these per format
 	{
-		blitCache = new RoutineCache<State>(1024);
-		cornerUpdateCache = new RoutineCache<State>(64); // We only need one of these per format
 	}
 
 	Blitter::~Blitter()
 	{
-		delete blitCache;
-		delete cornerUpdateCache;
 	}
 
 	void Blitter::clear(void *pixel, vk::Format format, vk::Image *dest, const vk::Format& viewFormat, const VkImageSubresourceRange& subresourceRange, const VkRect2D* renderArea)
@@ -52,7 +52,7 @@
 		}
 
 		State state(format, dstFormat, 1, dest->getSampleCountFlagBits(), { 0xF });
-		Routine *blitRoutine = getRoutine(state);
+		Routine *blitRoutine = getBlitRoutine(state);
 		if(!blitRoutine)
 		{
 			return;
@@ -1531,10 +1531,10 @@
 		return function("BlitRoutine");
 	}
 
-	Routine *Blitter::getRoutine(const State &state)
+	Routine *Blitter::getBlitRoutine(const State &state)
 	{
-		criticalSection.lock();
-		Routine *blitRoutine = blitCache->query(state);
+		std::unique_lock<std::mutex> lock(blitMutex);
+		Routine *blitRoutine = blitCache.query(state);
 
 		if(!blitRoutine)
 		{
@@ -1542,19 +1542,37 @@
 
 			if(!blitRoutine)
 			{
-				criticalSection.unlock();
 				UNIMPLEMENTED("blitRoutine");
 				return nullptr;
 			}
 
-			blitCache->add(state, blitRoutine);
+			blitCache.add(state, blitRoutine);
 		}
 
-		criticalSection.unlock();
-
 		return blitRoutine;
 	}
 
+	Routine *Blitter::getCornerUpdateRoutine(const State &state)
+	{
+		std::unique_lock<std::mutex> lock(cornerUpdateMutex);
+		Routine *cornerUpdateRoutine = cornerUpdateCache.query(state);
+
+		if(!cornerUpdateRoutine)
+		{
+			cornerUpdateRoutine = generateCornerUpdate(state);
+
+			if(!cornerUpdateRoutine)
+			{
+				UNIMPLEMENTED("cornerUpdateRoutine");
+				return nullptr;
+			}
+
+			cornerUpdateCache.add(state, cornerUpdateRoutine);
+		}
+
+		return cornerUpdateRoutine;
+	}
+
 	void Blitter::blitToBuffer(const vk::Image *src, VkImageSubresourceLayers subresource, VkOffset3D offset, VkExtent3D extent, uint8_t *dst, int bufferRowPitch, int bufferSlicePitch)
 	{
 		auto aspect = static_cast<VkImageAspectFlagBits>(subresource.aspectMask);
@@ -1562,7 +1580,7 @@
 		State state(format, format.getNonQuadLayoutFormat(), VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT,
 					{false, false});
 
-		Routine *blitRoutine = getRoutine(state);
+		Routine *blitRoutine = getBlitRoutine(state);
 		if(!blitRoutine)
 		{
 			return;
@@ -1628,7 +1646,7 @@
 		State state(format.getNonQuadLayoutFormat(), format, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT,
 					{false, false});
 
-		Routine *blitRoutine = getRoutine(state);
+		Routine *blitRoutine = getBlitRoutine(state);
 		if(!blitRoutine)
 		{
 			return;
@@ -1735,7 +1753,7 @@
 		                    (static_cast<uint32_t>(region.srcOffsets[1].y) > srcExtent.height) ||
 		                    (doFilter && ((x0 < 0.5f) || (y0 < 0.5f)));
 
-		Routine *blitRoutine = getRoutine(state);
+		Routine *blitRoutine = getBlitRoutine(state);
 		if(!blitRoutine)
 		{
 			return;
@@ -1933,25 +1951,12 @@
 			UNIMPLEMENTED("Multi-sampled cube: %d samples", static_cast<int>(samples));
 		}
 
-		criticalSection.lock();
-		Routine *cornerUpdateRoutine = cornerUpdateCache->query(state);
-
+		Routine *cornerUpdateRoutine = getCornerUpdateRoutine(state);
 		if(!cornerUpdateRoutine)
 		{
-			cornerUpdateRoutine = generateCornerUpdate(state);
-
-			if(!cornerUpdateRoutine)
-			{
-				criticalSection.unlock();
-				UNIMPLEMENTED("cornerUpdateRoutine");
-				return;
-			}
-
-			cornerUpdateCache->add(state, cornerUpdateRoutine);
+			return;
 		}
 
-		criticalSection.unlock();
-
 		void(*cornerUpdateFunction)(const CubeBorderData *data) = (void(*)(const CubeBorderData*))cornerUpdateRoutine->getEntry();
 
 		VkExtent3D extent = image->getMipLevelExtent(aspect, subresourceLayers.mipLevel);
diff --git a/src/Device/Blitter.hpp b/src/Device/Blitter.hpp
index 513bf86..99feb22 100644
--- a/src/Device/Blitter.hpp
+++ b/src/Device/Blitter.hpp
@@ -133,8 +133,9 @@
 		static Int ComputeOffset(Int &x, Int &y, Int &pitchB, int bytes, bool quadLayout);
 		static Float4 LinearToSRGB(Float4 &color);
 		static Float4 sRGBtoLinear(Float4 &color);
-		Routine *getRoutine(const State &state);
+		Routine *getBlitRoutine(const State &state);
 		Routine *generate(const State &state);
+		Routine *getCornerUpdateRoutine(const State &state);
 		Routine *generateCornerUpdate(const State& state);
 		void computeCubeCorner(Pointer<Byte>& layer, Int& x0, Int& x1, Int& y0, Int& y1, Int& pitchB, const State& state);
 
@@ -142,9 +143,10 @@
 	                      const VkImageSubresourceLayers& dstSubresourceLayers, Edge dstEdge,
 	                      const VkImageSubresourceLayers& srcSubresourceLayers, Edge srcEdge);
 
-		RoutineCache<State> *blitCache;
-		RoutineCache<State> *cornerUpdateCache;
-		std::mutex criticalSection;
+		std::mutex blitMutex;
+		RoutineCache<State> blitCache; // guarded by blitMutex
+		std::mutex cornerUpdateMutex;
+		RoutineCache<State> cornerUpdateCache; // guarded by cornerUpdateMutex
 	};
 }
 
diff --git a/src/Device/Config.cpp b/src/Device/Config.cpp
index 9e2e918..a155044 100644
--- a/src/Device/Config.cpp
+++ b/src/Device/Config.cpp
@@ -14,7 +14,6 @@
 
 #include "Config.hpp"
 
-#include "System/Thread.hpp"
 #include "System/Timer.hpp"
 
 namespace sw
diff --git a/src/Device/Context.hpp b/src/Device/Context.hpp
index 250e313..107ebe0 100644
--- a/src/Device/Context.hpp
+++ b/src/Device/Context.hpp
@@ -17,14 +17,10 @@
 
 #include "Vulkan/VkConfig.h"
 #include "Vulkan/VkDescriptorSet.hpp"
-#include "Sampler.hpp"
+#include "Config.hpp"
 #include "Stream.hpp"
-#include "Point.hpp"
-#include "Vertex.hpp"
 #include "System/Types.hpp"
 
-#include <Vulkan/VkConfig.h>
-
 namespace vk
 {
 	class DescriptorSet;
@@ -34,14 +30,7 @@
 
 namespace sw
 {
-	struct Sampler;
-	class PixelShader;
-	class VertexShader;
 	class SpirvShader;
-	struct Triangle;
-	struct Primitive;
-	struct Vertex;
-	class Resource;
 
 	enum In   // Default input stream semantic
 	{
diff --git a/src/Device/PixelProcessor.hpp b/src/Device/PixelProcessor.hpp
index 232ae47..0ab9d88 100644
--- a/src/Device/PixelProcessor.hpp
+++ b/src/Device/PixelProcessor.hpp
@@ -15,6 +15,7 @@
 #ifndef sw_PixelProcessor_hpp
 #define sw_PixelProcessor_hpp
 
+#include "Color.hpp"
 #include "Context.hpp"
 #include "RoutineCache.hpp"
 
@@ -24,6 +25,7 @@
 	class Rasterizer;
 	struct Texture;
 	struct DrawData;
+	struct Primitive;
 
 	class PixelProcessor
 	{
diff --git a/src/Device/SwiftConfig.cpp b/src/Device/SwiftConfig.cpp
index 5e1c2f9..7db5021 100644
--- a/src/Device/SwiftConfig.cpp
+++ b/src/Device/SwiftConfig.cpp
@@ -111,9 +111,8 @@
 
 	void SwiftConfig::getConfiguration(Configuration &configuration)
 	{
-		criticalSection.lock();
+		std::unique_lock<std::mutex> lock(criticalSection);
 		configuration = config;
-		criticalSection.unlock();
 	}
 
 	void SwiftConfig::serverRoutine(void *parameters)
@@ -186,7 +185,7 @@
 			{
 				if(match(&request, " ") || match(&request, "/ "))
 				{
-					criticalSection.lock();
+					std::unique_lock<std::mutex> lock(criticalSection);
 
 					const char *postData = strstr(request, "\r\n\r\n");
 					postData = postData ? postData + 4 : 0;
@@ -214,7 +213,7 @@
 						destroyServer();
 					}
 
-					criticalSection.unlock();
+					lock.unlock();
 
 					return send(clientSocket, OK, page());
 				}
diff --git a/src/Device/VertexProcessor.hpp b/src/Device/VertexProcessor.hpp
index 025c165..811ac32 100644
--- a/src/Device/VertexProcessor.hpp
+++ b/src/Device/VertexProcessor.hpp
@@ -18,6 +18,7 @@
 #include "Matrix.hpp"
 #include "Context.hpp"
 #include "RoutineCache.hpp"
+#include "Vertex.hpp"
 #include "Pipeline/SpirvShader.hpp"
 
 namespace sw
diff --git a/src/Vulkan/VkCommandBuffer.hpp b/src/Vulkan/VkCommandBuffer.hpp
index 5e59a79..df99cd8 100644
--- a/src/Vulkan/VkCommandBuffer.hpp
+++ b/src/Vulkan/VkCommandBuffer.hpp
@@ -18,6 +18,7 @@
 #include "VkConfig.h"
 #include "VkObject.hpp"
 #include "VkDescriptorSet.hpp"
+#include "Device/Color.hpp"
 #include "Device/Context.hpp"
 #include <memory>
 #include <vector>