Make Win32SurfaceKHR::present() more robust

Some tests are flaky on Windows due to surface lost errors.
Instead of asserting, this CL returns VK_ERROR_SURFACE_LOST_KHR
when "hwnd" is no longer valid, which may give a chance for the
application to recreate a window and try again, rather than
immediately crash.

Bug: chromium:1271664
Change-Id: I30f458404dfc9af7db8f25ae2dfe4a636979ba5f
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/60108
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Alexis Hétu <sugoi@google.com>
Commit-Queue: Alexis Hétu <sugoi@google.com>
diff --git a/src/WSI/Win32SurfaceKHR.cpp b/src/WSI/Win32SurfaceKHR.cpp
index bc82f6e..cf495c8 100644
--- a/src/WSI/Win32SurfaceKHR.cpp
+++ b/src/WSI/Win32SurfaceKHR.cpp
@@ -20,18 +20,19 @@
 #include <string.h>
 
 namespace {
-VkExtent2D getWindowSize(HWND hwnd)
+VkResult getWindowSize(HWND hwnd, VkExtent2D &windowSize)
 {
-	ASSERT(IsWindow(hwnd) == TRUE);
-
 	RECT clientRect = {};
-	BOOL status = GetClientRect(hwnd, &clientRect);
-	ASSERT(status != 0);
+	if(!IsWindow(hwnd) || !GetClientRect(hwnd, &clientRect))
+	{
+		windowSize = { 0, 0 };
+		return VK_ERROR_SURFACE_LOST_KHR;
+	}
 
-	int windowWidth = clientRect.right - clientRect.left;
-	int windowHeight = clientRect.bottom - clientRect.top;
+	windowSize = { static_cast<uint32_t>(clientRect.right - clientRect.left),
+		           static_cast<uint32_t>(clientRect.bottom - clientRect.top) };
 
-	return { static_cast<uint32_t>(windowWidth), static_cast<uint32_t>(windowHeight) };
+	return VK_SUCCESS;
 }
 }  // namespace
 
@@ -62,20 +63,12 @@
 {
 	setCommonSurfaceCapabilities(pSurfaceCapabilities);
 
-	if(!IsWindow(hwnd))
-	{
-		VkExtent2D extent = { 0, 0 };
-		pSurfaceCapabilities->currentExtent = extent;
-		pSurfaceCapabilities->minImageExtent = extent;
-		pSurfaceCapabilities->maxImageExtent = extent;
-		return VK_ERROR_SURFACE_LOST_KHR;
-	}
-
-	VkExtent2D extent = getWindowSize(hwnd);
+	VkExtent2D extent;
+	VkResult result = getWindowSize(hwnd, extent);
 	pSurfaceCapabilities->currentExtent = extent;
 	pSurfaceCapabilities->minImageExtent = extent;
 	pSurfaceCapabilities->maxImageExtent = extent;
-	return VK_SUCCESS;
+	return result;
 }
 
 void Win32SurfaceKHR::attachImage(PresentImage *image)
@@ -93,7 +86,11 @@
 VkResult Win32SurfaceKHR::present(PresentImage *image)
 {
 	// Recreate frame buffer in case window size has changed
-	lazyCreateFrameBuffer();
+	VkResult result = lazyCreateFrameBuffer();
+	if(result != VK_SUCCESS)
+	{
+		return result;
+	}
 
 	if(!framebuffer)
 	{
@@ -115,12 +112,19 @@
 	return VK_SUCCESS;
 }
 
-void Win32SurfaceKHR::lazyCreateFrameBuffer()
+VkResult Win32SurfaceKHR::lazyCreateFrameBuffer()
 {
-	auto currWindowExtent = getWindowSize(hwnd);
+	VkExtent2D currWindowExtent;
+	VkResult result = getWindowSize(hwnd, currWindowExtent);
+	if(result != VK_SUCCESS)
+	{
+		destroyFrameBuffer();
+		return result;
+	}
+
 	if(currWindowExtent.width == windowExtent.width && currWindowExtent.height == windowExtent.height)
 	{
-		return;
+		return VK_SUCCESS;
 	}
 
 	windowExtent = currWindowExtent;
@@ -132,7 +136,7 @@
 
 	if(windowExtent.width == 0 || windowExtent.height == 0)
 	{
-		return;
+		return VK_SUCCESS;
 	}
 
 	BITMAPINFO bitmapInfo = {};
@@ -151,6 +155,8 @@
 	int status = GetObject(bitmap, sizeof(BITMAP), &header);
 	ASSERT(status != 0);
 	bitmapRowPitch = static_cast<int>(header.bmWidthBytes);
+
+	return VK_SUCCESS;
 }
 
 void Win32SurfaceKHR::destroyFrameBuffer()
diff --git a/src/WSI/Win32SurfaceKHR.hpp b/src/WSI/Win32SurfaceKHR.hpp
index 32cfaec..254cbcb 100644
--- a/src/WSI/Win32SurfaceKHR.hpp
+++ b/src/WSI/Win32SurfaceKHR.hpp
@@ -45,7 +45,7 @@
 	VkResult present(PresentImage *image) override;
 
 private:
-	void lazyCreateFrameBuffer();
+	VkResult lazyCreateFrameBuffer();
 	void destroyFrameBuffer();
 
 	const HWND hwnd;