v3dv: hook up WSI support

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>
diff --git a/src/broadcom/vulkan/meson.build b/src/broadcom/vulkan/meson.build
index 39de45c..840459d 100644
--- a/src/broadcom/vulkan/meson.build
+++ b/src/broadcom/vulkan/meson.build
@@ -68,6 +68,7 @@
   'v3dv_queue.c',
   'v3dv_uniforms.c',
   'v3dv_util.c',
+  'v3dv_wsi.c',
   'v3d_tiling.c',
 )
 
@@ -90,17 +91,27 @@
   idep_vulkan_util,
 ]
 
+if with_platform_x11
+  v3dv_deps += dep_xcb_dri3
+  v3dv_flags += [
+    '-DVK_USE_PLATFORM_XCB_KHR',
+    '-DVK_USE_PLATFORM_XLIB_KHR',
+  ]
+  libv3dv_files += files('v3dv_wsi_x11.c')
+endif
+
 libvulkan_broadcom = shared_library(
   'vulkan_broadcom',
   [libv3dv_files, v3dv_entrypoints, v3dv_extensions_c, v3dv_extensions_h, sha1_h],
   include_directories : [
-    inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_broadcom, inc_compiler, inc_util,
+    inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_broadcom, inc_compiler, inc_util, inc_vulkan_wsi,
   ],
   link_with : [
     libbroadcom_cle,
     libbroadcom_v3d,
     # For glsl_type_singleton_init_or_ref:
     libglsl,
+    libvulkan_wsi,
   ],
   dependencies : v3dv_deps,
   c_args : v3dv_flags,
diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c
index fef0100..7ff4921 100644
--- a/src/broadcom/vulkan/v3dv_device.c
+++ b/src/broadcom/vulkan/v3dv_device.c
@@ -342,6 +342,15 @@
 
    device->options.merge_jobs = getenv("V3DV_NO_MERGE_JOBS") == NULL;
 
+   result = v3dv_wsi_init(device);
+   if (result != VK_SUCCESS) {
+      vk_error(instance, result);
+      goto fail;
+   }
+
+   v3dv_physical_device_get_supported_extensions(device,
+                                                 &device->supported_extensions);
+
 fail:
    close(fd);
    if (master_fd != -1)
diff --git a/src/broadcom/vulkan/v3dv_extensions.py b/src/broadcom/vulkan/v3dv_extensions.py
index e1fded3..131537f 100644
--- a/src/broadcom/vulkan/v3dv_extensions.py
+++ b/src/broadcom/vulkan/v3dv_extensions.py
@@ -59,10 +59,17 @@
 MAX_API_VERSION = None # Computed later
 
 EXTENSIONS = [
+    Extension('VK_KHR_display',                          23, 'VK_USE_PLATFORM_DISPLAY_KHR'),
     Extension('VK_KHR_external_memory',                   1, True),
     Extension('VK_KHR_external_memory_capabilities',      1, True),
     Extension('VK_KHR_external_memory_fd',                1, True),
     Extension('VK_KHR_get_physical_device_properties2',   1, True),
+    Extension('VK_KHR_get_surface_capabilities2',         1, 'V3DV_HAS_SURFACE'),
+    Extension('VK_KHR_surface',                          25, 'V3DV_HAS_SURFACE'),
+    Extension('VK_KHR_swapchain',                        68, 'V3DV_HAS_SURFACE'),
+    Extension('VK_KHR_wayland_surface',                   6, 'VK_USE_PLATFORM_WAYLAND_KHR'),
+    Extension('VK_KHR_xcb_surface',                       6, 'VK_USE_PLATFORM_XCB_KHR'),
+    Extension('VK_KHR_xlib_surface',                      6, 'VK_USE_PLATFORM_XLIB_KHR'),
     Extension('VK_EXT_debug_report',                      9, True),
     Extension('VK_EXT_external_memory_dma_buf',           1, True),
     Extension('VK_EXT_image_drm_format_modifier',         1, False),
diff --git a/src/broadcom/vulkan/v3dv_formats.c b/src/broadcom/vulkan/v3dv_formats.c
index 93fcc5d..803544e 100644
--- a/src/broadcom/vulkan/v3dv_formats.c
+++ b/src/broadcom/vulkan/v3dv_formats.c
@@ -60,9 +60,11 @@
 #define SWIZ_000X	SWIZ(0, 0, 0, X)
 
 static const struct v3dv_format format_table[] = {
-   FORMAT(R8G8B8A8_UNORM,          RGBA8,       RGBA8,       SWIZ_XYZW, 16),
-   FORMAT(B8G8R8A8_UNORM,          RGBA8,       RGBA8,       SWIZ_ZYXW, 16),
-   FORMAT(R32G32B32A32_SFLOAT,     RGBA32F,     RGBA32F,     SWIZ_XYZW, 32),
+   FORMAT(R8G8B8A8_SRGB,           SRGB8_ALPHA8, RGBA8,       SWIZ_XYZW, 16),
+   FORMAT(B8G8R8A8_SRGB,           SRGB8_ALPHA8, RGBA8,       SWIZ_ZYXW, 16),
+   FORMAT(R8G8B8A8_UNORM,          RGBA8,        RGBA8,       SWIZ_XYZW, 16),
+   FORMAT(B8G8R8A8_UNORM,          RGBA8,        RGBA8,       SWIZ_ZYXW, 16),
+   FORMAT(R32G32B32A32_SFLOAT,     RGBA32F,      RGBA32F,     SWIZ_XYZW, 32),
 };
 
 const struct v3dv_format *
diff --git a/src/broadcom/vulkan/v3dv_private.h b/src/broadcom/vulkan/v3dv_private.h
index ba3b451..b390c1b 100644
--- a/src/broadcom/vulkan/v3dv_private.h
+++ b/src/broadcom/vulkan/v3dv_private.h
@@ -84,6 +84,7 @@
  * good enough.
  */
 #include "util/u_box.h"
+#include "wsi_common.h"
 
 #include "broadcom/cle/v3dx_pack.h"
 
@@ -138,6 +139,8 @@
    int32_t master_fd;
    uint8_t pipeline_cache_uuid[VK_UUID_SIZE];
 
+   struct wsi_device wsi_device;
+
    VkPhysicalDeviceMemoryProperties memory;
 
    struct v3d_device_info devinfo;
@@ -152,6 +155,9 @@
    } options;
 };
 
+VkResult v3dv_wsi_init(struct v3dv_physical_device *physical_device);
+void v3dv_wsi_finish(struct v3dv_physical_device *physical_device);
+
 struct v3dv_app_info {
    const char *app_name;
    uint32_t app_version;
diff --git a/src/broadcom/vulkan/v3dv_wsi.c b/src/broadcom/vulkan/v3dv_wsi.c
new file mode 100644
index 0000000..5716f6a
--- /dev/null
+++ b/src/broadcom/vulkan/v3dv_wsi.c
@@ -0,0 +1,279 @@
+/*
+ * Copyright © 2020 Raspberry Pi
+ * based on intel anv code:
+ * Copyright © 2015 Intel Corporation
+
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "v3dv_private.h"
+#include "drm-uapi/drm_fourcc.h"
+#include "vk_format_info.h"
+#include "vk_util.h"
+#include "wsi_common.h"
+
+static PFN_vkVoidFunction
+v3dv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, physical_device, physicalDevice);
+   return v3dv_lookup_entrypoint(&physical_device->devinfo, pName);
+}
+
+VkResult
+v3dv_wsi_init(struct v3dv_physical_device *physical_device)
+{
+   VkResult result;
+
+   result = wsi_device_init(&physical_device->wsi_device,
+                            v3dv_physical_device_to_handle(physical_device),
+                            v3dv_wsi_proc_addr,
+                            &physical_device->instance->alloc,
+                            physical_device->master_fd,
+                            NULL, false);
+   if (result != VK_SUCCESS)
+      return result;
+
+   physical_device->wsi_device.supports_modifiers = true;
+
+   return VK_SUCCESS;
+}
+
+void
+v3dv_wsi_finish(struct v3dv_physical_device *physical_device)
+{
+   wsi_device_finish(&physical_device->wsi_device,
+                     &physical_device->instance->alloc);
+}
+
+void v3dv_DestroySurfaceKHR(
+    VkInstance                                   _instance,
+    VkSurfaceKHR                                 _surface,
+    const VkAllocationCallbacks*                 pAllocator)
+{
+   V3DV_FROM_HANDLE(v3dv_instance, instance, _instance);
+   ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
+
+   if (!surface)
+      return;
+
+   vk_free2(&instance->alloc, pAllocator, surface);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfaceSupportKHR(
+    VkPhysicalDevice                            physicalDevice,
+    uint32_t                                    queueFamilyIndex,
+    VkSurfaceKHR                                surface,
+    VkBool32*                                   pSupported)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_support(&device->wsi_device,
+                                         queueFamilyIndex,
+                                         surface,
+                                         pSupported);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
+    VkPhysicalDevice                            physicalDevice,
+    VkSurfaceKHR                                surface,
+    VkSurfaceCapabilitiesKHR*                   pSurfaceCapabilities)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_capabilities(&device->wsi_device,
+                                              surface,
+                                              pSurfaceCapabilities);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfaceCapabilities2KHR(
+    VkPhysicalDevice                            physicalDevice,
+    const VkPhysicalDeviceSurfaceInfo2KHR*      pSurfaceInfo,
+    VkSurfaceCapabilities2KHR*                  pSurfaceCapabilities)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_capabilities2(&device->wsi_device,
+                                               pSurfaceInfo,
+                                               pSurfaceCapabilities);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfaceFormatsKHR(
+    VkPhysicalDevice                            physicalDevice,
+    VkSurfaceKHR                                surface,
+    uint32_t*                                   pSurfaceFormatCount,
+    VkSurfaceFormatKHR*                         pSurfaceFormats)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_formats(&device->wsi_device, surface,
+                                         pSurfaceFormatCount, pSurfaceFormats);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfaceFormats2KHR(
+    VkPhysicalDevice                            physicalDevice,
+    const VkPhysicalDeviceSurfaceInfo2KHR*      pSurfaceInfo,
+    uint32_t*                                   pSurfaceFormatCount,
+    VkSurfaceFormat2KHR*                        pSurfaceFormats)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
+                                          pSurfaceFormatCount, pSurfaceFormats);
+}
+
+VkResult v3dv_GetPhysicalDeviceSurfacePresentModesKHR(
+    VkPhysicalDevice                            physicalDevice,
+    VkSurfaceKHR                                surface,
+    uint32_t*                                   pPresentModeCount,
+    VkPresentModeKHR*                           pPresentModes)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_surface_present_modes(&device->wsi_device, surface,
+                                               pPresentModeCount,
+                                               pPresentModes);
+}
+
+VkResult v3dv_CreateSwapchainKHR(
+    VkDevice                                     _device,
+    const VkSwapchainCreateInfoKHR*              pCreateInfo,
+    const VkAllocationCallbacks*                 pAllocator,
+    VkSwapchainKHR*                              pSwapchain)
+{
+   V3DV_FROM_HANDLE(v3dv_device, device, _device);
+   struct wsi_device *wsi_device = &device->instance->physicalDevice.wsi_device;
+   const VkAllocationCallbacks *alloc;
+
+   if (pAllocator)
+     alloc = pAllocator;
+   else
+     alloc = &device->alloc;
+
+   return wsi_common_create_swapchain(wsi_device, _device,
+                                      pCreateInfo, alloc, pSwapchain);
+}
+
+void v3dv_DestroySwapchainKHR(
+    VkDevice                                     _device,
+    VkSwapchainKHR                               swapchain,
+    const VkAllocationCallbacks*                 pAllocator)
+{
+   V3DV_FROM_HANDLE(v3dv_device, device, _device);
+   const VkAllocationCallbacks *alloc;
+
+   if (pAllocator)
+     alloc = pAllocator;
+   else
+     alloc = &device->alloc;
+
+   wsi_common_destroy_swapchain(_device, swapchain, alloc);
+}
+
+VkResult v3dv_GetSwapchainImagesKHR(
+    VkDevice                                     device,
+    VkSwapchainKHR                               swapchain,
+    uint32_t*                                    pSwapchainImageCount,
+    VkImage*                                     pSwapchainImages)
+{
+   return wsi_common_get_images(swapchain,
+                                pSwapchainImageCount,
+                                pSwapchainImages);
+}
+
+VkResult v3dv_AcquireNextImageKHR(
+    VkDevice                                     device,
+    VkSwapchainKHR                               swapchain,
+    uint64_t                                     timeout,
+    VkSemaphore                                  semaphore,
+    VkFence                                      fence,
+    uint32_t*                                    pImageIndex)
+{
+   VkAcquireNextImageInfoKHR acquire_info = {
+      .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
+      .swapchain = swapchain,
+      .timeout = timeout,
+      .semaphore = semaphore,
+      .fence = fence,
+      .deviceMask = 0,
+   };
+
+   return v3dv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
+}
+
+VkResult v3dv_AcquireNextImage2KHR(
+    VkDevice                                     _device,
+    const VkAcquireNextImageInfoKHR*             pAcquireInfo,
+    uint32_t*                                    pImageIndex)
+{
+   V3DV_FROM_HANDLE(v3dv_device, device, _device);
+   struct v3dv_physical_device *pdevice = &device->instance->physicalDevice;
+
+   return wsi_common_acquire_next_image2(&pdevice->wsi_device, _device,
+                                         pAcquireInfo, pImageIndex);
+}
+
+VkResult v3dv_QueuePresentKHR(
+    VkQueue                                  _queue,
+    const VkPresentInfoKHR*                  pPresentInfo)
+{
+   V3DV_FROM_HANDLE(v3dv_queue, queue, _queue);
+   struct v3dv_physical_device *pdevice =
+      &queue->device->instance->physicalDevice;
+
+   return wsi_common_queue_present(&pdevice->wsi_device,
+                                   v3dv_device_to_handle(queue->device),
+                                   _queue, 0,
+                                   pPresentInfo);
+}
+
+VkResult v3dv_GetDeviceGroupPresentCapabilitiesKHR(
+    VkDevice                                    device,
+    VkDeviceGroupPresentCapabilitiesKHR*        pCapabilities)
+{
+   memset(pCapabilities->presentMask, 0,
+          sizeof(pCapabilities->presentMask));
+   pCapabilities->presentMask[0] = 0x1;
+   pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
+
+   return VK_SUCCESS;
+}
+
+VkResult v3dv_GetDeviceGroupSurfacePresentModesKHR(
+    VkDevice                                    device,
+    VkSurfaceKHR                                surface,
+    VkDeviceGroupPresentModeFlagsKHR*           pModes)
+{
+   *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
+
+   return VK_SUCCESS;
+}
+
+VkResult v3dv_GetPhysicalDevicePresentRectanglesKHR(
+    VkPhysicalDevice                            physicalDevice,
+    VkSurfaceKHR                                surface,
+    uint32_t*                                   pRectCount,
+    VkRect2D*                                   pRects)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_common_get_present_rectangles(&device->wsi_device,
+                                            surface,
+                                            pRectCount, pRects);
+}
diff --git a/src/broadcom/vulkan/v3dv_wsi_x11.c b/src/broadcom/vulkan/v3dv_wsi_x11.c
new file mode 100644
index 0000000..4421dc3
--- /dev/null
+++ b/src/broadcom/vulkan/v3dv_wsi_x11.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2020 Raspberry Pi
+ *
+ * based mostly on anv driver which is:
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <X11/Xlib-xcb.h>
+#include <X11/xshmfence.h>
+#include <xcb/xcb.h>
+#include <xcb/dri3.h>
+#include <xcb/present.h>
+
+#include "wsi_common_x11.h"
+#include "v3dv_private.h"
+
+VkBool32 v3dv_GetPhysicalDeviceXcbPresentationSupportKHR(
+   VkPhysicalDevice                            physicalDevice,
+   uint32_t                                    queueFamilyIndex,
+   xcb_connection_t*                           connection,
+   xcb_visualid_t                              visual_id)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_get_physical_device_xcb_presentation_support(
+             &device->wsi_device,
+             queueFamilyIndex,
+             connection, visual_id);
+}
+
+VkBool32 v3dv_GetPhysicalDeviceXlibPresentationSupportKHR(
+   VkPhysicalDevice                            physicalDevice,
+   uint32_t                                    queueFamilyIndex,
+   Display*                                    dpy,
+   VisualID                                    visualID)
+{
+   V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
+
+   return wsi_get_physical_device_xcb_presentation_support(
+             &device->wsi_device,
+             queueFamilyIndex,
+             XGetXCBConnection(dpy), visualID);
+}
+
+VkResult v3dv_CreateXcbSurfaceKHR(
+   VkInstance                                  _instance,
+   const VkXcbSurfaceCreateInfoKHR*            pCreateInfo,
+   const VkAllocationCallbacks*                pAllocator,
+   VkSurfaceKHR*                               pSurface)
+{
+   V3DV_FROM_HANDLE(v3dv_instance, instance, _instance);
+   const VkAllocationCallbacks *alloc;
+   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
+
+   if (pAllocator)
+      alloc = pAllocator;
+   else
+      alloc = &instance->alloc;
+
+   return wsi_create_xcb_surface(alloc, pCreateInfo, pSurface);
+}
+
+VkResult v3dv_CreateXlibSurfaceKHR(
+   VkInstance                                  _instance,
+   const VkXlibSurfaceCreateInfoKHR*           pCreateInfo,
+   const VkAllocationCallbacks*                pAllocator,
+   VkSurfaceKHR*                               pSurface)
+{
+   V3DV_FROM_HANDLE(v3dv_instance, instance, _instance);
+   const VkAllocationCallbacks *alloc;
+
+   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
+
+   if (pAllocator)
+      alloc = pAllocator;
+   else
+      alloc = &instance->alloc;
+
+   return wsi_create_xlib_surface(alloc, pCreateInfo, pSurface);
+}