Detect <xcb/shm.h> for building XCB surface support

Building with VK_USE_PLATFORM_XCB_KHR enabled requires the <xcb/shm.h>
header. Note that we dynamically check whether the MIT-SHM extension is
actually available on the target system before making use of it, but we
need the header to be present on the build machine. A warning about
installing the libxcb-shm0-dev package gets generated if it's not.

Also, the header include order was fixed and clang-format reapplied.

Bug: b/209030951
Change-Id: I6a6889c9ee64e3b8aa2a56a15a3aeff534268638
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/64950
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7049995..deb0c05 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -111,10 +111,10 @@
     check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
 
     include(CheckIncludeFiles)
-    CHECK_INCLUDE_FILES("xcb/xcb.h" HAVE_XCB_H)
+    CHECK_INCLUDE_FILES("xcb/xcb.h;xcb/shm.h" HAVE_XCB_H)
     if(NOT HAVE_XCB_H)
-        message(WARNING "xcb/xcb.h was not found. Install the libx11-xcb-dev package "
-                        "to build with WSI support for XCB surfaces.")
+        message(WARNING "xcb/xcb.h or xcb/shm.h was not found. Install the libx11-xcb-dev and "
+                        "libxcb-shm0-dev packages to build with WSI support for XCB surfaces.")
     endif()
 endif()
 
diff --git a/src/WSI/XcbSurfaceKHR.cpp b/src/WSI/XcbSurfaceKHR.cpp
index 1a4102a..3af964b 100644
--- a/src/WSI/XcbSurfaceKHR.cpp
+++ b/src/WSI/XcbSurfaceKHR.cpp
@@ -45,13 +45,13 @@
 }
 
 XcbSurfaceKHR::XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR *pCreateInfo, void *mem)
-	: connection(pCreateInfo->connection)
-	, window(pCreateInfo->window)
+    : connection(pCreateInfo->connection)
+    , window(pCreateInfo->window)
 {
 	ASSERT(isSupported());
 
 	gc = libXCB->xcb_generate_id(connection);
-	uint32_t values[2] = { 0, 0xffffffff };
+	uint32_t values[2] = { 0, 0xFFFFFFFF };
 	libXCB->xcb_create_gc(connection, gc, window, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
 
 	auto shmQuery = libXCB->xcb_get_extension_data(connection, libXCB->xcb_shm_id);
@@ -89,7 +89,7 @@
 
 VkResult XcbSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
 {
-	if (surfaceLost)
+	if(surfaceLost)
 	{
 		return VK_ERROR_SURFACE_LOST_KHR;
 	}
@@ -110,12 +110,14 @@
 	return VK_SUCCESS;
 }
 
-void* XcbSurfaceKHR::allocateImageMemory(PresentImage *image, const VkMemoryAllocateInfo &allocateInfo)
+void *XcbSurfaceKHR::allocateImageMemory(PresentImage *image, const VkMemoryAllocateInfo &allocateInfo)
 {
-	if (!mitSHM)
+	if(!mitSHM)
+	{
 		return nullptr;
+	}
 
-	SHMPixmap& pixmap = pixmaps[image];
+	SHMPixmap &pixmap = pixmaps[image];
 	int shmid = shmget(IPC_PRIVATE, allocateInfo.allocationSize, IPC_CREAT | SHM_R | SHM_W);
 	pixmap.shmaddr = shmat(shmid, 0, 0);
 	pixmap.shmseg = libXCB->xcb_generate_id(connection);
@@ -129,20 +131,20 @@
 
 	pixmap.pixmap = libXCB->xcb_generate_id(connection);
 	libXCB->xcb_shm_create_pixmap(
-		connection,
-		pixmap.pixmap,
-		window,
-		width, height,
-		windowDepth,
-		pixmap.shmseg,
-		0);
+	    connection,
+	    pixmap.pixmap,
+	    window,
+	    width, height,
+	    windowDepth,
+	    pixmap.shmseg,
+	    0);
 
 	return pixmap.shmaddr;
 }
 
 void XcbSurfaceKHR::releaseImageMemory(PresentImage *image)
 {
-	if (mitSHM)
+	if(mitSHM)
 	{
 		auto it = pixmaps.find(image);
 		assert(it != pixmaps.end());
@@ -179,7 +181,8 @@
 		return VK_ERROR_OUT_OF_DATE_KHR;
 	}
 
-	if (!mitSHM) {
+	if(!mitSHM)
+	{
 		// TODO: Convert image if not RGB888.
 		int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
 		int bytesPerPixel = static_cast<int>(image->getImage()->getFormat(VK_IMAGE_ASPECT_COLOR_BIT).bytes());
@@ -187,17 +190,17 @@
 		auto buffer = reinterpret_cast<uint8_t *>(image->getImageMemory()->getOffsetPointer(0));
 		size_t bufferSize = extent.height * stride;
 		libXCB->xcb_put_image(
-			connection,
-			XCB_IMAGE_FORMAT_Z_PIXMAP,
-			window,
-			gc,
-			width,
-			extent.height,
-			0, 0,  // dst x, y
-			0,     // left_pad
-			depth,
-			bufferSize,  // data_len
-			buffer       // data
+		    connection,
+		    XCB_IMAGE_FORMAT_Z_PIXMAP,
+		    window,
+		    gc,
+		    width,
+		    extent.height,
+		    0, 0,  // dst x, y
+		    0,     // left_pad
+		    depth,
+		    bufferSize,  // data_len
+		    buffer       // data
 		);
 	}
 	else
@@ -205,14 +208,14 @@
 		auto it = pixmaps.find(image);
 		assert(it != pixmaps.end());
 		libXCB->xcb_copy_area(
-			connection,
-			it->second.pixmap,
-			window,
-			gc,
-			0, 0,  // src x, y
-			0, 0,  // dst x, y
-			extent.width,
-			extent.height);
+		    connection,
+		    it->second.pixmap,
+		    window,
+		    gc,
+		    0, 0,  // src x, y
+		    0, 0,  // dst x, y
+		    extent.width,
+		    extent.height);
 	}
 	libXCB->xcb_flush(connection);
 
diff --git a/src/WSI/XcbSurfaceKHR.hpp b/src/WSI/XcbSurfaceKHR.hpp
index a6892d4..eae9257 100644
--- a/src/WSI/XcbSurfaceKHR.hpp
+++ b/src/WSI/XcbSurfaceKHR.hpp
@@ -18,9 +18,10 @@
 #include "VkSurfaceKHR.hpp"
 #include "Vulkan/VkObject.hpp"
 
-#include <vulkan/vulkan_xcb.h>
-#include <xcb/xcb.h>
 #include <xcb/shm.h>
+#include <xcb/xcb.h>
+// XCB headers must be included before the Vulkan header.
+#include <vulkan/vulkan_xcb.h>
 
 #include <unordered_map>
 
@@ -38,7 +39,7 @@
 
 	VkResult getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const override;
 
-	virtual void* allocateImageMemory(PresentImage *image, const VkMemoryAllocateInfo &allocateInfo) override;
+	virtual void *allocateImageMemory(PresentImage *image, const VkMemoryAllocateInfo &allocateInfo) override;
 	virtual void releaseImageMemory(PresentImage *image) override;
 	virtual void attachImage(PresentImage *image) override;
 	virtual void detachImage(PresentImage *image) override;
@@ -51,9 +52,10 @@
 	xcb_gcontext_t gc = XCB_NONE;
 	int windowDepth = 0;
 	mutable bool surfaceLost = false;
-	struct SHMPixmap {
-  		xcb_shm_seg_t shmseg = XCB_NONE;
-  		void *shmaddr = nullptr;
+	struct SHMPixmap
+	{
+		xcb_shm_seg_t shmseg = XCB_NONE;
+		void *shmaddr = nullptr;
 		xcb_pixmap_t pixmap = XCB_NONE;
 	};
 	std::unordered_map<PresentImage *, SHMPixmap> pixmaps;
diff --git a/src/WSI/libXCB.cpp b/src/WSI/libXCB.cpp
index 60be41e..212884c 100644
--- a/src/WSI/libXCB.cpp
+++ b/src/WSI/libXCB.cpp
@@ -54,7 +54,7 @@
 			libxcb = RTLD_DEFAULT;
 		}
 		else
-                {
+		{
 			libxcb = loadLibrary("libxcb.so.1");
 		}
 
@@ -63,7 +63,7 @@
 			libshm = RTLD_DEFAULT;
 		}
 		else
-                {
+		{
 			libshm = loadLibrary("libxcb-shm.so.0");
 		}
 
diff --git a/src/WSI/libXCB.hpp b/src/WSI/libXCB.hpp
index 50a943c..ad957ca 100644
--- a/src/WSI/libXCB.hpp
+++ b/src/WSI/libXCB.hpp
@@ -34,10 +34,10 @@
 	xcb_void_cookie_t (*xcb_free_pixmap)(xcb_connection_t *conn, xcb_pixmap_t pixmap);
 	xcb_query_extension_reply_t *(*xcb_get_extension_data)(xcb_connection_t *c, xcb_extension_t *extension) = nullptr;
 
-	xcb_shm_query_version_cookie_t 	(*xcb_shm_query_version)(xcb_connection_t *c);
+	xcb_shm_query_version_cookie_t (*xcb_shm_query_version)(xcb_connection_t *c);
 	xcb_shm_query_version_reply_t *(*xcb_shm_query_version_reply)(xcb_connection_t *c, xcb_shm_query_version_cookie_t cookie, xcb_generic_error_t **e);
 	xcb_void_cookie_t (*xcb_shm_attach)(xcb_connection_t *c, xcb_shm_seg_t shmseg, uint32_t shmid, uint8_t read_only);
-	xcb_void_cookie_t (*xcb_shm_detach)(xcb_connection_t * 	c, xcb_shm_seg_t shmseg);
+	xcb_void_cookie_t (*xcb_shm_detach)(xcb_connection_t *c, xcb_shm_seg_t shmseg);
 	xcb_void_cookie_t (*xcb_shm_create_pixmap)(xcb_connection_t *c, xcb_pixmap_t pid, xcb_drawable_t drawable, uint16_t width, uint16_t height, uint8_t depth, xcb_shm_seg_t shmseg, uint32_t offset);
 	xcb_extension_t *xcb_shm_id;
 };