Blame SOURCES/mesa-vk-wsi-sw-fixes.patch

d14a95
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
d14a95
index e2a7d337ecf..bc4d87611e0 100644
d14a95
--- a/src/vulkan/wsi/wsi_common_wayland.c
d14a95
+++ b/src/vulkan/wsi/wsi_common_wayland.c
d14a95
@@ -31,6 +31,7 @@
d14a95
 #include <string.h>
d14a95
 #include <pthread.h>
d14a95
 #include <poll.h>
d14a95
+#include <sys/mman.h>
d14a95
 
d14a95
 #include "drm-uapi/drm_fourcc.h"
d14a95
 
d14a95
@@ -44,9 +45,15 @@
d14a95
 #include <util/hash_table.h>
d14a95
 #include <util/timespec.h>
d14a95
 #include <util/u_vector.h>
d14a95
+#include <util/anon_file.h>
d14a95
 
d14a95
 struct wsi_wayland;
d14a95
 
d14a95
+struct wsi_wl_display_swrast {
d14a95
+   struct wl_shm *                              wl_shm;
d14a95
+   struct u_vector                              formats;
d14a95
+};
d14a95
+
d14a95
 struct wsi_wl_display_drm {
d14a95
    struct wl_drm *                              wl_drm;
d14a95
    struct u_vector                              formats;
d14a95
@@ -69,6 +76,7 @@ struct wsi_wl_display {
d14a95
    struct wl_display *                          wl_display_wrapper;
d14a95
    struct wl_event_queue *                      queue;
d14a95
 
d14a95
+   struct wsi_wl_display_swrast                 swrast;
d14a95
    struct wsi_wl_display_drm                    drm;
d14a95
    struct wsi_wl_display_dmabuf                 dmabuf;
d14a95
 
d14a95
@@ -79,6 +87,8 @@ struct wsi_wl_display {
d14a95
 
d14a95
    /* Only used for displays created by wsi_wl_display_create */
d14a95
    uint32_t                                     refcount;
d14a95
+
d14a95
+   bool sw;
d14a95
 };
d14a95
 
d14a95
 struct wsi_wayland {
d14a95
@@ -183,6 +193,40 @@ wsi_wl_display_add_wl_format(struct wsi_wl_display *display,
d14a95
    }
d14a95
 }
d14a95
 
d14a95
+static void
d14a95
+wsi_wl_display_add_wl_shm_format(struct wsi_wl_display *display,
d14a95
+                                 struct u_vector *formats,
d14a95
+                                 uint32_t wl_shm_format)
d14a95
+{
d14a95
+   switch (wl_shm_format) {
d14a95
+   case WL_SHM_FORMAT_XBGR8888:
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_R8G8B8_SRGB);
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_R8G8B8_UNORM);
d14a95
+      FALLTHROUGH;
d14a95
+   case WL_SHM_FORMAT_ABGR8888:
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_R8G8B8A8_SRGB);
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_R8G8B8A8_UNORM);
d14a95
+      break;
d14a95
+   case WL_SHM_FORMAT_XRGB8888:
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_B8G8R8_SRGB);
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_B8G8R8_UNORM);
d14a95
+      FALLTHROUGH;
d14a95
+   case WL_SHM_FORMAT_ARGB8888:
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_B8G8R8A8_SRGB);
d14a95
+      wsi_wl_display_add_vk_format(display, formats,
d14a95
+                                   VK_FORMAT_B8G8R8A8_UNORM);
d14a95
+      break;
d14a95
+   }
d14a95
+}
d14a95
+
d14a95
+
d14a95
 static void
d14a95
 drm_handle_device(void *data, struct wl_drm *drm, const char *name)
d14a95
 {
d14a95
@@ -232,6 +276,23 @@ wl_drm_format_for_vk_format(VkFormat vk_format, bool alpha)
d14a95
    }
d14a95
 }
d14a95
 
d14a95
+static uint32_t
d14a95
+wl_shm_format_for_vk_format(VkFormat vk_format, bool alpha)
d14a95
+{
d14a95
+   switch (vk_format) {
d14a95
+   case VK_FORMAT_R8G8B8A8_UNORM:
d14a95
+   case VK_FORMAT_R8G8B8A8_SRGB:
d14a95
+      return alpha ? WL_SHM_FORMAT_ABGR8888 : WL_SHM_FORMAT_XBGR8888;
d14a95
+   case VK_FORMAT_B8G8R8A8_UNORM:
d14a95
+   case VK_FORMAT_B8G8R8A8_SRGB:
d14a95
+      return alpha ? WL_SHM_FORMAT_ARGB8888 : WL_SHM_FORMAT_XRGB8888;
d14a95
+
d14a95
+   default:
d14a95
+      assert(!"Unsupported Vulkan format");
d14a95
+      return 0;
d14a95
+   }
d14a95
+}
d14a95
+
d14a95
 static void
d14a95
 drm_handle_format(void *data, struct wl_drm *drm, uint32_t wl_format)
d14a95
 {
d14a95
@@ -311,12 +372,34 @@ static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
d14a95
    dmabuf_handle_modifier,
d14a95
 };
d14a95
 
d14a95
+static void
d14a95
+shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
d14a95
+{
d14a95
+   struct wsi_wl_display *display = data;
d14a95
+   if (display->swrast.formats.element_size == 0)
d14a95
+      return;
d14a95
+
d14a95
+   wsi_wl_display_add_wl_shm_format(display, &display->swrast.formats, format);
d14a95
+}
d14a95
+
d14a95
+static const struct wl_shm_listener shm_listener = {
d14a95
+   .format = shm_handle_format
d14a95
+};
d14a95
+
d14a95
 static void
d14a95
 registry_handle_global(void *data, struct wl_registry *registry,
d14a95
                        uint32_t name, const char *interface, uint32_t version)
d14a95
 {
d14a95
    struct wsi_wl_display *display = data;
d14a95
 
d14a95
+   if (display->sw) {
d14a95
+      if (strcmp(interface, "wl_shm") == 0) {
d14a95
+         display->swrast.wl_shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
d14a95
+         wl_shm_add_listener(display->swrast.wl_shm, &shm_listener, display);
d14a95
+      }
d14a95
+      return;
d14a95
+   }
d14a95
+
d14a95
    if (strcmp(interface, "wl_drm") == 0) {
d14a95
       assert(display->drm.wl_drm == NULL);
d14a95
 
d14a95
@@ -348,10 +431,13 @@ wsi_wl_display_finish(struct wsi_wl_display *display)
d14a95
 {
d14a95
    assert(display->refcount == 0);
d14a95
 
d14a95
+   u_vector_finish(&display->swrast.formats);
d14a95
    u_vector_finish(&display->drm.formats);
d14a95
    u_vector_finish(&display->dmabuf.formats);
d14a95
    u_vector_finish(&display->dmabuf.modifiers.argb8888);
d14a95
    u_vector_finish(&display->dmabuf.modifiers.xrgb8888);
d14a95
+   if (display->swrast.wl_shm)
d14a95
+      wl_shm_destroy(display->swrast.wl_shm);
d14a95
    if (display->drm.wl_drm)
d14a95
       wl_drm_destroy(display->drm.wl_drm);
d14a95
    if (display->dmabuf.wl_dmabuf)
d14a95
@@ -366,16 +452,18 @@ static VkResult
d14a95
 wsi_wl_display_init(struct wsi_wayland *wsi_wl,
d14a95
                     struct wsi_wl_display *display,
d14a95
                     struct wl_display *wl_display,
d14a95
-                    bool get_format_list)
d14a95
+                    bool get_format_list, bool sw)
d14a95
 {
d14a95
    VkResult result = VK_SUCCESS;
d14a95
    memset(display, 0, sizeof(*display));
d14a95
 
d14a95
    display->wsi_wl = wsi_wl;
d14a95
    display->wl_display = wl_display;
d14a95
+   display->sw = sw;
d14a95
 
d14a95
    if (get_format_list) {
d14a95
-      if (!u_vector_init(&display->drm.formats, sizeof(VkFormat), 8) ||
d14a95
+      if (!u_vector_init(&display->swrast.formats, sizeof(VkFormat), 8) ||
d14a95
+          !u_vector_init(&display->drm.formats, sizeof(VkFormat), 8) ||
d14a95
           !u_vector_init(&display->dmabuf.formats, sizeof(VkFormat), 8) ||
d14a95
           !u_vector_init(&display->dmabuf.modifiers.argb8888,
d14a95
                          sizeof(uint64_t), 32) ||
d14a95
@@ -414,7 +502,7 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
d14a95
    wl_display_roundtrip_queue(display->wl_display, display->queue);
d14a95
 
d14a95
    /* Round-trip again to get formats, modifiers and capabilities */
d14a95
-   if (display->drm.wl_drm || display->dmabuf.wl_dmabuf)
d14a95
+   if (display->drm.wl_drm || display->dmabuf.wl_dmabuf || display->swrast.wl_shm)
d14a95
       wl_display_roundtrip_queue(display->wl_display, display->queue);
d14a95
 
d14a95
    if (wsi_wl->wsi->force_bgra8_unorm_first) {
d14a95
@@ -432,8 +520,10 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
d14a95
       }
d14a95
    }
d14a95
 
d14a95
+   if (display->sw)
d14a95
+      display->formats = &display->swrast.formats;
d14a95
    /* We need prime support for wl_drm */
d14a95
-   if (display->drm.wl_drm &&
d14a95
+   else if (display->drm.wl_drm &&
d14a95
        (display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
d14a95
       display->formats = &display->drm.formats;
d14a95
    } else if (display->dmabuf.wl_dmabuf) {
d14a95
@@ -463,6 +553,7 @@ fail:
d14a95
 
d14a95
 static VkResult
d14a95
 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
d14a95
+                      bool sw,
d14a95
                       struct wsi_wl_display **display_out)
d14a95
 {
d14a95
    struct wsi_wl_display *display =
d14a95
@@ -471,7 +562,8 @@ wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
d14a95
    if (!display)
d14a95
       return VK_ERROR_OUT_OF_HOST_MEMORY;
d14a95
 
d14a95
-   VkResult result = wsi_wl_display_init(wsi, display, wl_display, true);
d14a95
+   VkResult result = wsi_wl_display_init(wsi, display, wl_display, true,
d14a95
+                                         sw);
d14a95
    if (result != VK_SUCCESS) {
d14a95
       vk_free(wsi->alloc, display);
d14a95
       return result;
d14a95
@@ -509,7 +601,8 @@ wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
d14a95
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
d14a95
 
d14a95
    struct wsi_wl_display display;
d14a95
-   VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false);
d14a95
+   VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false,
d14a95
+                                      wsi_device->sw);
d14a95
    if (ret == VK_SUCCESS)
d14a95
       wsi_wl_display_finish(&display);
d14a95
 
d14a95
@@ -612,7 +705,8 @@ wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
d14a95
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
d14a95
 
d14a95
    struct wsi_wl_display display;
d14a95
-   if (wsi_wl_display_init(wsi, &display, surface->display, true))
d14a95
+   if (wsi_wl_display_init(wsi, &display, surface->display, true,
d14a95
+                           wsi_device->sw))
d14a95
       return VK_ERROR_SURFACE_LOST_KHR;
d14a95
 
d14a95
    VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
d14a95
@@ -642,7 +736,8 @@ wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
d14a95
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
d14a95
 
d14a95
    struct wsi_wl_display display;
d14a95
-   if (wsi_wl_display_init(wsi, &display, surface->display, true))
d14a95
+   if (wsi_wl_display_init(wsi, &display, surface->display, true,
d14a95
+                           wsi_device->sw))
d14a95
       return VK_ERROR_SURFACE_LOST_KHR;
d14a95
 
d14a95
    VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
d14a95
@@ -722,10 +817,12 @@ struct wsi_wl_image {
d14a95
    struct wsi_image                             base;
d14a95
    struct wl_buffer *                           buffer;
d14a95
    bool                                         busy;
d14a95
+   void *                                       data_ptr;
d14a95
+   uint32_t                                     data_size;
d14a95
 };
d14a95
 
d14a95
 struct wsi_wl_swapchain {
d14a95
-   struct wsi_swapchain                        base;
d14a95
+   struct wsi_swapchain                         base;
d14a95
 
d14a95
    struct wsi_wl_display                        *display;
d14a95
 
d14a95
@@ -742,6 +839,7 @@ struct wsi_wl_swapchain {
d14a95
    VkExtent2D                                   extent;
d14a95
    VkFormat                                     vk_format;
d14a95
    uint32_t                                     drm_format;
d14a95
+   uint32_t                                     shm_format;
d14a95
 
d14a95
    uint32_t                                     num_drm_modifiers;
d14a95
    const uint64_t *                             drm_modifiers;
d14a95
@@ -859,6 +957,23 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
d14a95
 {
d14a95
    struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
d14a95
 
d14a95
+   if (chain->display->sw) {
d14a95
+      struct wsi_wl_image *image = &chain->images[image_index];
d14a95
+      void *dptr = image->data_ptr;
d14a95
+      void *sptr;
d14a95
+      chain->base.wsi->MapMemory(chain->base.device,
d14a95
+                                 image->base.memory,
d14a95
+                                 0, 0, 0, &sptr);
d14a95
+
d14a95
+      for (unsigned r = 0; r < chain->extent.height; r++) {
d14a95
+         memcpy(dptr, sptr, image->base.row_pitches[0]);
d14a95
+         dptr += image->base.row_pitches[0];
d14a95
+         sptr += image->base.row_pitches[0];
d14a95
+      }
d14a95
+      chain->base.wsi->UnmapMemory(chain->base.device,
d14a95
+                                   image->base.memory);
d14a95
+
d14a95
+   }
d14a95
    if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
d14a95
       while (!chain->fifo_ready) {
d14a95
          int ret = wl_display_dispatch_queue(chain->display->wl_display,
d14a95
@@ -928,7 +1043,31 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
d14a95
    if (result != VK_SUCCESS)
d14a95
       return result;
d14a95
 
d14a95
-   if (!chain->drm_wrapper) {
d14a95
+   if (display->sw) {
d14a95
+      int fd, stride;
d14a95
+
d14a95
+      stride = image->base.row_pitches[0];
d14a95
+      image->data_size = stride * chain->extent.height;
d14a95
+
d14a95
+      /* Create a shareable buffer */
d14a95
+      fd = os_create_anonymous_file(image->data_size, NULL);
d14a95
+      if (fd < 0)
d14a95
+         goto fail_image;
d14a95
+
d14a95
+      image->data_ptr = mmap(NULL, image->data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
d14a95
+      if (image->data_ptr == MAP_FAILED) {
d14a95
+         close(fd);
d14a95
+         goto fail_image;
d14a95
+      }
d14a95
+      /* Share it in a wl_buffer */
d14a95
+      struct wl_shm_pool *pool = wl_shm_create_pool(display->swrast.wl_shm, fd, image->data_size);
d14a95
+      wl_proxy_set_queue((struct wl_proxy *)pool, display->queue);
d14a95
+      image->buffer = wl_shm_pool_create_buffer(pool, 0, chain->extent.width,
d14a95
+                                                chain->extent.height, stride,
d14a95
+                                                chain->shm_format);
d14a95
+      wl_shm_pool_destroy(pool);
d14a95
+      close(fd);
d14a95
+   } else if (!chain->drm_wrapper) {
d14a95
       /* Only request modifiers if we have dmabuf, else it must be implicit. */
d14a95
       assert(display->dmabuf.wl_dmabuf);
d14a95
       assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
d14a95
@@ -995,6 +1134,8 @@ wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
d14a95
       if (chain->images[i].buffer) {
d14a95
          wl_buffer_destroy(chain->images[i].buffer);
d14a95
          wsi_destroy_image(&chain->base, &chain->images[i].base);
d14a95
+         if (chain->images[i].data_ptr)
d14a95
+            munmap(chain->images[i].data_ptr, chain->images[i].data_size);
d14a95
       }
d14a95
    }
d14a95
 
d14a95
@@ -1049,8 +1190,10 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
d14a95
    /* Mark a bunch of stuff as NULL.  This way we can just call
d14a95
     * destroy_swapchain for cleanup.
d14a95
     */
d14a95
-   for (uint32_t i = 0; i < num_images; i++)
d14a95
+   for (uint32_t i = 0; i < num_images; i++) {
d14a95
       chain->images[i].buffer = NULL;
d14a95
+      chain->images[i].data_ptr = NULL;
d14a95
+   }
d14a95
    chain->surface = NULL;
d14a95
    chain->drm_wrapper = NULL;
d14a95
    chain->frame = NULL;
d14a95
@@ -1066,7 +1209,10 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
d14a95
    chain->base.image_count = num_images;
d14a95
    chain->extent = pCreateInfo->imageExtent;
d14a95
    chain->vk_format = pCreateInfo->imageFormat;
d14a95
-   chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
d14a95
+   if (wsi_device->sw)
d14a95
+      chain->shm_format = wl_shm_format_for_vk_format(chain->vk_format, alpha);
d14a95
+   else
d14a95
+      chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
d14a95
 
d14a95
    if (pCreateInfo->oldSwapchain) {
d14a95
       /* If we have an oldSwapchain parameter, copy the display struct over
d14a95
@@ -1076,7 +1222,8 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
d14a95
       chain->display = wsi_wl_display_ref(old_chain->display);
d14a95
    } else {
d14a95
       chain->display = NULL;
d14a95
-      result = wsi_wl_display_create(wsi, surface->display, &chain->display);
d14a95
+      result = wsi_wl_display_create(wsi, surface->display,
d14a95
+                                     wsi_device->sw, &chain->display);
d14a95
       if (result != VK_SUCCESS)
d14a95
          goto fail;
d14a95
    }
d14a95
diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c
d14a95
index 54769b81ccc..fa0c3d997dc 100644
d14a95
--- a/src/vulkan/wsi/wsi_common_x11.c
d14a95
+++ b/src/vulkan/wsi/wsi_common_x11.c
d14a95
@@ -439,8 +439,10 @@ VkBool32 wsi_get_physical_device_xcb_presentation_support(
d14a95
    if (!wsi_conn)
d14a95
       return false;
d14a95
 
d14a95
-   if (!wsi_x11_check_for_dri3(wsi_conn))
d14a95
-      return false;
d14a95
+   if (!wsi_device->sw) {
d14a95
+      if (!wsi_x11_check_for_dri3(wsi_conn))
d14a95
+         return false;
d14a95
+   }
d14a95
 
d14a95
    unsigned visual_depth;
d14a95
    if (!connection_get_visualtype(connection, visual_id, &visual_depth))
d14a95
@@ -484,9 +486,11 @@ x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
d14a95
    if (!wsi_conn)
d14a95
       return VK_ERROR_OUT_OF_HOST_MEMORY;
d14a95
 
d14a95
-   if (!wsi_x11_check_for_dri3(wsi_conn)) {
d14a95
-      *pSupported = false;
d14a95
-      return VK_SUCCESS;
d14a95
+   if (!wsi_device->sw) {
d14a95
+      if (!wsi_x11_check_for_dri3(wsi_conn)) {
d14a95
+         *pSupported = false;
d14a95
+         return VK_SUCCESS;
d14a95
+      }
d14a95
    }
d14a95
 
d14a95
    unsigned visual_depth;