From afe84a2c35642e81724ecd4e9089afd52967620a Mon Sep 17 00:00:00 2001
From: Milan Jaros <milan.jaros@vsb.cz>
Date: Mon, 23 May 2022 14:35:12 -0500
Subject: [PATCH] c/client: Swapchain for Win32 GL

Co-authored-by: Ryan Pavlik <ryan.pavlik@collabora.com>
Co-authored-by: Jakob Bornecrantz <jakob@collabora.com>
---
 .../client/comp_gl_memobj_swapchain.c         | 28 ++++++++++++++++---
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/xrt/compositor/client/comp_gl_memobj_swapchain.c b/src/xrt/compositor/client/comp_gl_memobj_swapchain.c
index 2556f0d56..00020e90e 100644
--- a/src/xrt/compositor/client/comp_gl_memobj_swapchain.c
+++ b/src/xrt/compositor/client/comp_gl_memobj_swapchain.c
@@ -1,4 +1,4 @@
-// Copyright 2019-2021, Collabora, Ltd.
+// Copyright 2019-2022, Collabora, Ltd.
 // SPDX-License-Identifier: BSL-1.0
 /*!
  * @file
@@ -35,6 +35,7 @@ client_gl_memobj_swapchain(struct xrt_swapchain *xsc)
 	return (struct client_gl_memobj_swapchain *)xsc;
 }
 
+
 /*
  *
  * Swapchain functions.
@@ -72,16 +73,33 @@ client_gl_memobj_swapchain_destroy(struct xrt_swapchain *xsc)
 	free(sc);
 }
 
+static bool
+client_gl_memobj_swapchain_import(GLuint memory, size_t size, xrt_graphics_buffer_handle_t handle)
+{
+#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
+	glImportMemoryFdEXT(memory, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, handle);
+	return true;
+#elif defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
+	glImportMemoryWin32HandleEXT(memory, size, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, handle);
+	return true;
+#else
+	(void)memory;
+	(void)size;
+	(void)handle;
+	return false;
+#endif
+}
+
 struct xrt_swapchain *
 client_gl_memobj_swapchain_create(struct xrt_compositor *xc,
                                   const struct xrt_swapchain_create_info *info,
                                   struct xrt_swapchain_native *xscn,
                                   struct client_gl_swapchain **out_cglsc)
 {
-#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD)
 	struct client_gl_compositor *c = client_gl_compositor(xc);
 	(void)c;
 
+#if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_FD) || defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE)
 	if (xscn == NULL) {
 		return NULL;
 	}
@@ -110,8 +128,10 @@ client_gl_memobj_swapchain_create(struct xrt_compositor *xc,
 
 		GLint dedicated = xscn->images[i].use_dedicated_allocation ? GL_TRUE : GL_FALSE;
 		glMemoryObjectParameterivEXT(sc->memory[i], GL_DEDICATED_MEMORY_OBJECT_EXT, &dedicated);
-		glImportMemoryFdEXT(sc->memory[i], xscn->images[i].size, GL_HANDLE_TYPE_OPAQUE_FD_EXT,
-		                    xscn->images[i].handle);
+
+		if (!client_gl_memobj_swapchain_import(sc->memory[i], xscn->images[i].size, xscn->images[i].handle)) {
+			continue;
+		}
 
 		// We have consumed this now, make sure it's not freed again.
 		xscn->images[i].handle = XRT_GRAPHICS_BUFFER_HANDLE_INVALID;