Use correct window depth to create pixmap

using hardcoded window depth(32) to create pixmap causes
presenting problem with chrome menus, we need to query
window depth and use the correct depth to create pixmaps.

Bug: swiftshader:169
Change-Id: I5a6b01e05f1387621a7102ccf1914511eef4ad28
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/61029
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Peng Huang <penghuang@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
diff --git a/src/WSI/XcbSurfaceKHR.cpp b/src/WSI/XcbSurfaceKHR.cpp
index 186cf3a..ab55f66 100644
--- a/src/WSI/XcbSurfaceKHR.cpp
+++ b/src/WSI/XcbSurfaceKHR.cpp
@@ -50,14 +50,28 @@
 {
 	ASSERT(isSupported());
 
-	xcb_shm_query_version_cookie_t cookie = libXCB->xcb_shm_query_version(connection);
-	xcb_shm_query_version_reply_t* reply = libXCB->xcb_shm_query_version_reply(connection, cookie, nullptr);
-	mitSHM = reply && reply->shared_pixmaps;
-	free(reply);
-
 	gc = libXCB->xcb_generate_id(connection);
 	uint32_t values[2] = { 0, 0xffffffff };
 	libXCB->xcb_create_gc(connection, gc, window, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
+
+	auto shmCookie = libXCB->xcb_shm_query_version(connection);
+	auto geomCookie = libXCB->xcb_get_geometry(connection, window);
+
+	if (auto *reply = libXCB->xcb_shm_query_version_reply(connection, shmCookie, nullptr))
+	{
+		mitSHM = reply && reply->shared_pixmaps;
+		free(reply);
+	}
+
+	if (auto *reply = libXCB->xcb_get_geometry_reply(connection, geomCookie, nullptr))
+	{
+		windowDepth = reply->depth;
+		free(reply);
+	}
+	else
+	{
+		surfaceLost = true;
+	}
 }
 
 void XcbSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
@@ -72,12 +86,18 @@
 
 VkResult XcbSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
 {
+	if (surfaceLost)
+	{
+		return VK_ERROR_SURFACE_LOST_KHR;
+	}
+
 	setCommonSurfaceCapabilities(pSurfaceCapabilities);
 
 	VkExtent2D extent;
 	int depth;
 	if(!getWindowSizeAndDepth(connection, window, &extent, &depth))
 	{
+		surfaceLost = true;
 		return VK_ERROR_SURFACE_LOST_KHR;
 	}
 
@@ -103,7 +123,6 @@
 	int bytesPerPixel = static_cast<int>(image->getImage()->getFormat(VK_IMAGE_ASPECT_COLOR_BIT).bytes());
 	int width = stride / bytesPerPixel;
 	int height = allocateInfo.allocationSize / stride;
-	int depth = 32;
 
 	pixmap.pixmap = libXCB->xcb_generate_id(connection);
 	libXCB->xcb_shm_create_pixmap(
@@ -111,7 +130,7 @@
 		pixmap.pixmap,
 		window,
 		width, height,
-		depth,
+		windowDepth,
 		pixmap.shmseg,
 		0);
 
@@ -143,8 +162,10 @@
 {
 	VkExtent2D windowExtent;
 	int depth;
-	if(!getWindowSizeAndDepth(connection, window, &windowExtent, &depth))
+	// TODO(penghuang): getWindowSizeAndDepth() call needs a sync IPC, try to remove it.
+	if(surfaceLost || !getWindowSizeAndDepth(connection, window, &windowExtent, &depth))
 	{
+		surfaceLost = true;
 		return VK_ERROR_SURFACE_LOST_KHR;
 	}
 
@@ -175,7 +196,6 @@
 			bufferSize,  // data_len
 			buffer       // data
 		);
-		libXCB->xcb_flush(connection);
 	}
 	else
 	{
@@ -190,8 +210,8 @@
 			0, 0,  // dst x, y
 			extent.width,
 			extent.height);
-		libXCB->xcb_flush(connection);
 	}
+	libXCB->xcb_flush(connection);
 
 	return VK_SUCCESS;
 }
diff --git a/src/WSI/XcbSurfaceKHR.hpp b/src/WSI/XcbSurfaceKHR.hpp
index e01edb8..a6892d4 100644
--- a/src/WSI/XcbSurfaceKHR.hpp
+++ b/src/WSI/XcbSurfaceKHR.hpp
@@ -45,14 +45,16 @@
 	VkResult present(PresentImage *image) override;
 
 private:
-	xcb_connection_t *connection;
-	xcb_window_t window;
-	bool mitSHM;
-	xcb_gcontext_t gc;
+	xcb_connection_t *connection = nullptr;
+	xcb_window_t window = XCB_NONE;
+	bool mitSHM = false;
+	xcb_gcontext_t gc = XCB_NONE;
+	int windowDepth = 0;
+	mutable bool surfaceLost = false;
 	struct SHMPixmap {
-  		xcb_shm_seg_t shmseg;
-  		void *shmaddr;
-		xcb_pixmap_t pixmap;
+  		xcb_shm_seg_t shmseg = XCB_NONE;
+  		void *shmaddr = nullptr;
+		xcb_pixmap_t pixmap = XCB_NONE;
 	};
 	std::unordered_map<PresentImage *, SHMPixmap> pixmaps;
 	// std::unordered_map<PresentImage *, uint32_t> graphicsContexts;