vk/wsi: Move to a clallback system for the entire WSI implementation
authorJason Ekstrand <jason.ekstrand@intel.com>
Fri, 4 Sep 2015 18:14:45 +0000 (11:14 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Sat, 5 Sep 2015 00:55:42 +0000 (17:55 -0700)
We do this for two reasons: First, because it allows us to simplify WSI and
compiling in/out support for a particular platform is as simple as calling
or not calling the platform-specific init function.  Second, the
implementation gives us a place for a given chunk of the WSI to stash
stuff in the instance.

src/vulkan/anv_device.c
src/vulkan/anv_private.h
src/vulkan/anv_wsi.c
src/vulkan/anv_wsi.h
src/vulkan/anv_wsi_x11.c

index 0df1bbeda3c8fe854eba04f6f634a276813001d0..c1758de537cc6614e4f9a045fff8a6d49cba5773 100644 (file)
@@ -139,6 +139,8 @@ VkResult anv_CreateInstance(
 
    VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
 
+   anv_init_wsi(instance);
+
    *pInstance = anv_instance_to_handle(instance);
 
    return VK_SUCCESS;
@@ -149,6 +151,8 @@ VkResult anv_DestroyInstance(
 {
    ANV_FROM_HANDLE(anv_instance, instance, _instance);
 
+   anv_finish_wsi(instance);
+
    VG(VALGRIND_DESTROY_MEMPOOL(instance));
 
    _mesa_locale_fini();
index 8be2aa2bee176d34ba5091c27ab7244fb2cc2326..0c7d5e8a536ebdb6cbc830adf2225f892396c434 100644 (file)
@@ -343,8 +343,13 @@ struct anv_instance {
     uint32_t                                    apiVersion;
     uint32_t                                    physicalDeviceCount;
     struct anv_physical_device                  physicalDevice;
+
+    struct anv_wsi_implementation *             wsi_impl[VK_PLATFORM_NUM_WSI];
 };
 
+VkResult anv_init_wsi(struct anv_instance *instance);
+void anv_finish_wsi(struct anv_instance *instance);
+
 struct anv_meta_state {
    struct {
       VkPipeline                                pipeline;
index 404c905d419cf78efcc3a02d322ae73f915a5dde..24379337a5e4ed1f54009620700b4cc7ca84e88f 100644 (file)
 
 #include "anv_wsi.h"
 
+VkResult
+anv_init_wsi(struct anv_instance *instance)
+{
+   memset(instance->wsi_impl, 0, sizeof(instance->wsi_impl));
+   return anv_x11_init_wsi(instance);
+}
+
+void
+anv_finish_wsi(struct anv_instance *instance)
+{
+   anv_x11_finish_wsi(instance);
+}
+
 VkResult
 anv_GetPhysicalDeviceSurfaceSupportWSI(
     VkPhysicalDevice                        physicalDevice,
@@ -30,14 +43,23 @@ anv_GetPhysicalDeviceSurfaceSupportWSI(
     const VkSurfaceDescriptionWSI*          pSurfaceDescription,
     VkBool32*                               pSupported)
 {
+   ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
+
    assert(pSurfaceDescription->sType ==
           VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI);
 
    VkSurfaceDescriptionWindowWSI *window = (void *)pSurfaceDescription;
 
-   *pSupported = window->platform == VK_PLATFORM_XCB_WSI;
+   struct anv_wsi_implementation *impl =
+      physical_device->instance->wsi_impl[window->platform];
 
-   return VK_SUCCESS;
+   if (impl) {
+      return impl->get_window_supported(impl, physical_device,
+                                        window, pSupported);
+   } else {
+      *pSupported = false;
+      return VK_SUCCESS;
+   }
 }
 
 VkResult
@@ -55,13 +77,13 @@ anv_GetSurfaceInfoWSI(
    VkSurfaceDescriptionWindowWSI *window =
       (VkSurfaceDescriptionWindowWSI *)pSurfaceDescription;
 
-   switch (window->platform) {
-   case VK_PLATFORM_XCB_WSI:
-      return anv_x11_get_surface_info(device, window, infoType,
-                                      pDataSize, pData);
-   default:
-      return vk_error(VK_ERROR_INVALID_VALUE);
-   }
+   struct anv_wsi_implementation *impl =
+      device->instance->wsi_impl[window->platform];
+
+   assert(impl);
+
+   return impl->get_surface_info(impl, device, window, infoType,
+                                 pDataSize, pData);
 }
 
 VkResult
@@ -79,14 +101,12 @@ anv_CreateSwapChainWSI(
    VkSurfaceDescriptionWindowWSI *window =
       (VkSurfaceDescriptionWindowWSI *)pCreateInfo->pSurfaceDescription;
 
-   switch (window->platform) {
-   case VK_PLATFORM_XCB_WSI:
-      result = anv_x11_create_swap_chain(device, pCreateInfo,
-                                         (void *)&swap_chain);
-      break;
-   default:
-      return vk_error(VK_ERROR_INVALID_VALUE);
-   }
+   struct anv_wsi_implementation *impl =
+      device->instance->wsi_impl[window->platform];
+
+   assert(impl);
+
+   result = impl->create_swap_chain(impl, device, pCreateInfo, &swap_chain);
 
    if (result == VK_SUCCESS)
       *pSwapChain = anv_swap_chain_to_handle(swap_chain);
index 3a5fba13a564201429c33faaa19f2cbab28d939c..3ee3fcee56db452dbda6ff48dc7c84dea8b88788 100644 (file)
@@ -42,10 +42,21 @@ struct anv_swap_chain {
 
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_swap_chain, VkSwapChainWSI)
 
-VkResult anv_x11_get_surface_info(struct anv_device *device,
-                                  VkSurfaceDescriptionWindowWSI *window,
-                                  VkSurfaceInfoTypeWSI infoType,
-                                  size_t* pDataSize, void* pData);
-VkResult anv_x11_create_swap_chain(struct anv_device *device,
-                                   const VkSwapChainCreateInfoWSI *pCreateInfo,
-                                   struct anv_swap_chain **swap_chain);
+struct anv_wsi_implementation {
+   VkResult (*get_window_supported)(struct anv_wsi_implementation *impl,
+                                    struct anv_physical_device *physical_device,
+                                    const VkSurfaceDescriptionWindowWSI *window,
+                                    VkBool32 *pSupported);
+   VkResult (*get_surface_info)(struct anv_wsi_implementation *impl,
+                                struct anv_device *device,
+                                VkSurfaceDescriptionWindowWSI *window,
+                                VkSurfaceInfoTypeWSI infoType,
+                                size_t* pDataSize, void* pData);
+   VkResult (*create_swap_chain)(struct anv_wsi_implementation *impl,
+                                 struct anv_device *device,
+                                 const VkSwapChainCreateInfoWSI *pCreateInfo,
+                                 struct anv_swap_chain **swap_chain);
+};
+
+VkResult anv_x11_init_wsi(struct anv_instance *instance);
+void anv_x11_finish_wsi(struct anv_instance *instance);
index 54260f56ad944853891b13b5abb513d0a2bd8111..212c01be0b9ffc30ee4aed00d3875ba16fe1c73b 100644 (file)
@@ -37,11 +37,22 @@ static const VkSurfacePresentModePropertiesWSI present_modes[] = {
    { VK_PRESENT_MODE_MAILBOX_WSI },
 };
 
-VkResult
-anv_x11_get_surface_info(struct anv_device *device,
-                         VkSurfaceDescriptionWindowWSI *window,
-                         VkSurfaceInfoTypeWSI infoType,
-                         size_t* pDataSize, void* pData)
+static VkResult
+x11_get_window_supported(struct anv_wsi_implementation *impl,
+                         struct anv_physical_device *physical_device,
+                         const VkSurfaceDescriptionWindowWSI *window,
+                         VkBool32 *pSupported)
+{
+   *pSupported = true;
+   stub_return(VK_SUCCESS);
+}
+
+static VkResult
+x11_get_surface_info(struct anv_wsi_implementation *impl,
+                     struct anv_device *device,
+                     VkSurfaceDescriptionWindowWSI *window,
+                     VkSurfaceInfoTypeWSI infoType,
+                     size_t* pDataSize, void* pData)
 {
    if (pDataSize == NULL)
       return vk_error(VK_ERROR_INVALID_POINTER);
@@ -192,10 +203,11 @@ x11_destroy_swap_chain(struct anv_swap_chain *chain)
    return VK_SUCCESS;
 }
 
-VkResult
-anv_x11_create_swap_chain(struct anv_device *device,
-                          const VkSwapChainCreateInfoWSI *pCreateInfo,
-                          struct anv_swap_chain **swap_chain_out)
+static VkResult
+x11_create_swap_chain(struct anv_wsi_implementation *impl,
+                      struct anv_device *device,
+                      const VkSwapChainCreateInfoWSI *pCreateInfo,
+                      struct anv_swap_chain **swap_chain_out)
 {
    struct x11_swap_chain *chain;
    xcb_void_cookie_t cookie;
@@ -335,3 +347,28 @@ anv_x11_create_swap_chain(struct anv_device *device,
  fail:
    return result;
 }
+
+VkResult
+anv_x11_init_wsi(struct anv_instance *instance)
+{
+   struct anv_wsi_implementation *impl;
+
+   impl = anv_instance_alloc(instance, sizeof(*impl), 8,
+                             VK_SYSTEM_ALLOC_TYPE_INTERNAL);
+   if (!impl)
+      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
+
+   impl->get_window_supported = x11_get_window_supported;
+   impl->get_surface_info = x11_get_surface_info;
+   impl->create_swap_chain = x11_create_swap_chain;
+
+   instance->wsi_impl[VK_PLATFORM_XCB_WSI] = impl;
+
+   return VK_SUCCESS;
+}
+
+void
+anv_x11_finish_wsi(struct anv_instance *instance)
+{
+   anv_instance_free(instance, instance->wsi_impl[VK_PLATFORM_XCB_WSI]);
+}