Mesa (main): vulkan/wsi/wl: add wl_shm support for lavapipe.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jul 12 20:56:20 UTC 2021


Module: Mesa
Branch: main
Commit: 6b36f35734a9ffa2c6fde475933eb46d7f4fb6f4
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=6b36f35734a9ffa2c6fde475933eb46d7f4fb6f4

Author: Dave Airlie <airlied at redhat.com>
Date:   Mon Jul 12 10:29:28 2021 +1000

vulkan/wsi/wl: add wl_shm support for lavapipe.

This adds swrast support for rendering from lavapipe.

Reviewed-by: Daniel Stone <daniels at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11819>

---

 src/vulkan/wsi/wsi_common_wayland.c | 164 +++++++++++++++++++++++++++++++++---
 1 file changed, 153 insertions(+), 11 deletions(-)

diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index 67786e22881..b37163de04c 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <pthread.h>
 #include <poll.h>
+#include <sys/mman.h>
 
 #include "drm-uapi/drm_fourcc.h"
 
@@ -44,9 +45,15 @@
 #include <util/hash_table.h>
 #include <util/timespec.h>
 #include <util/u_vector.h>
+#include <util/anon_file.h>
 
 struct wsi_wayland;
 
+struct wsi_wl_display_swrast {
+   struct wl_shm *                              wl_shm;
+   struct u_vector                              formats;
+};
+
 struct wsi_wl_display_drm {
    struct wl_drm *                              wl_drm;
    struct u_vector                              formats;
@@ -69,6 +76,7 @@ struct wsi_wl_display {
    struct wl_display *                          wl_display_wrapper;
    struct wl_event_queue *                      queue;
 
+   struct wsi_wl_display_swrast                 swrast;
    struct wsi_wl_display_drm                    drm;
    struct wsi_wl_display_dmabuf                 dmabuf;
 
@@ -79,6 +87,8 @@ struct wsi_wl_display {
 
    /* Only used for displays created by wsi_wl_display_create */
    uint32_t                                     refcount;
+
+   bool sw;
 };
 
 struct wsi_wayland {
@@ -218,6 +228,40 @@ wsi_wl_display_add_wl_format(struct wsi_wl_display *display,
    }
 }
 
+static void
+wsi_wl_display_add_wl_shm_format(struct wsi_wl_display *display,
+                                 struct u_vector *formats,
+                                 uint32_t wl_shm_format)
+{
+   switch (wl_shm_format) {
+   case WL_SHM_FORMAT_XBGR8888:
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_R8G8B8_SRGB);
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_R8G8B8_UNORM);
+      FALLTHROUGH;
+   case WL_SHM_FORMAT_ABGR8888:
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_R8G8B8A8_SRGB);
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_R8G8B8A8_UNORM);
+      break;
+   case WL_SHM_FORMAT_XRGB8888:
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_B8G8R8_SRGB);
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_B8G8R8_UNORM);
+      FALLTHROUGH;
+   case WL_SHM_FORMAT_ARGB8888:
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_B8G8R8A8_SRGB);
+      wsi_wl_display_add_vk_format(display, formats,
+                                   VK_FORMAT_B8G8R8A8_UNORM);
+      break;
+   }
+}
+
+
 static void
 drm_handle_device(void *data, struct wl_drm *drm, const char *name)
 {
@@ -272,6 +316,23 @@ wl_drm_format_for_vk_format(VkFormat vk_format, bool alpha)
    }
 }
 
+static uint32_t
+wl_shm_format_for_vk_format(VkFormat vk_format, bool alpha)
+{
+   switch (vk_format) {
+   case VK_FORMAT_R8G8B8A8_UNORM:
+   case VK_FORMAT_R8G8B8A8_SRGB:
+      return alpha ? WL_SHM_FORMAT_ABGR8888 : WL_SHM_FORMAT_XBGR8888;
+   case VK_FORMAT_B8G8R8A8_UNORM:
+   case VK_FORMAT_B8G8R8A8_SRGB:
+      return alpha ? WL_SHM_FORMAT_ARGB8888 : WL_SHM_FORMAT_XRGB8888;
+
+   default:
+      assert(!"Unsupported Vulkan format");
+      return 0;
+   }
+}
+
 static void
 drm_handle_format(void *data, struct wl_drm *drm, uint32_t wl_format)
 {
@@ -353,12 +414,31 @@ static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
    dmabuf_handle_modifier,
 };
 
+static void
+shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
+{
+   struct wsi_wl_display *display = data;
+   wsi_wl_display_add_wl_shm_format(display, &display->swrast.formats, format);
+}
+
+static const struct wl_shm_listener shm_listener = {
+   .format = shm_handle_format
+};
+
 static void
 registry_handle_global(void *data, struct wl_registry *registry,
                        uint32_t name, const char *interface, uint32_t version)
 {
    struct wsi_wl_display *display = data;
 
+   if (display->sw) {
+      if (strcmp(interface, "wl_shm") == 0) {
+         display->swrast.wl_shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
+         wl_shm_add_listener(display->swrast.wl_shm, &shm_listener, display);
+      }
+      return;
+   }
+
    if (strcmp(interface, "wl_drm") == 0) {
       assert(display->drm.wl_drm == NULL);
 
@@ -389,10 +469,13 @@ wsi_wl_display_finish(struct wsi_wl_display *display)
 {
    assert(display->refcount == 0);
 
+   u_vector_finish(&display->swrast.formats);
    u_vector_finish(&display->drm.formats);
    u_vector_finish(&display->dmabuf.formats);
    u_vector_finish(&display->dmabuf.modifiers.argb8888);
    u_vector_finish(&display->dmabuf.modifiers.xrgb8888);
+   if (display->swrast.wl_shm)
+      wl_shm_destroy(display->swrast.wl_shm);
    if (display->drm.wl_drm)
       wl_drm_destroy(display->drm.wl_drm);
    if (display->dmabuf.wl_dmabuf)
@@ -407,16 +490,18 @@ static VkResult
 wsi_wl_display_init(struct wsi_wayland *wsi_wl,
                     struct wsi_wl_display *display,
                     struct wl_display *wl_display,
-                    bool get_format_list)
+                    bool get_format_list, bool sw)
 {
    VkResult result = VK_SUCCESS;
    memset(display, 0, sizeof(*display));
 
    display->wsi_wl = wsi_wl;
    display->wl_display = wl_display;
+   display->sw = sw;
 
    if (get_format_list) {
-      if (!u_vector_init(&display->drm.formats, sizeof(VkFormat), 8) ||
+      if (!u_vector_init(&display->swrast.formats, sizeof(VkFormat), 8) ||
+          !u_vector_init(&display->drm.formats, sizeof(VkFormat), 8) ||
           !u_vector_init(&display->dmabuf.formats, sizeof(VkFormat), 8) ||
           !u_vector_init(&display->dmabuf.modifiers.argb8888,
                          sizeof(uint64_t), 32) ||
@@ -455,7 +540,7 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
    wl_display_roundtrip_queue(display->wl_display, display->queue);
 
    /* Round-trip again to get formats, modifiers and capabilities */
-   if (display->drm.wl_drm || display->dmabuf.wl_dmabuf)
+   if (display->drm.wl_drm || display->dmabuf.wl_dmabuf || display->swrast.wl_shm)
       wl_display_roundtrip_queue(display->wl_display, display->queue);
 
    if (wsi_wl->wsi->force_bgra8_unorm_first) {
@@ -474,7 +559,9 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
    }
 
    /* Prefer the linux-dmabuf protocol if available */
-   if (display->dmabuf.wl_dmabuf) {
+   if (display->sw)
+      display->formats = &display->swrast.formats;
+   else if (display->dmabuf.wl_dmabuf) {
       display->formats = &display->dmabuf.formats;
    } else if (display->drm.wl_drm &&
        (display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
@@ -505,6 +592,7 @@ fail:
 
 static VkResult
 wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
+                      bool sw,
                       struct wsi_wl_display **display_out)
 {
    struct wsi_wl_display *display =
@@ -513,7 +601,8 @@ wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display,
    if (!display)
       return VK_ERROR_OUT_OF_HOST_MEMORY;
 
-   VkResult result = wsi_wl_display_init(wsi, display, wl_display, true);
+   VkResult result = wsi_wl_display_init(wsi, display, wl_display, true,
+                                         sw);
    if (result != VK_SUCCESS) {
       vk_free(wsi->alloc, display);
       return result;
@@ -551,7 +640,8 @@ wsi_wl_get_presentation_support(struct wsi_device *wsi_device,
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
 
    struct wsi_wl_display display;
-   VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false);
+   VkResult ret = wsi_wl_display_init(wsi, &display, wl_display, false,
+                                      wsi_device->sw);
    if (ret == VK_SUCCESS)
       wsi_wl_display_finish(&display);
 
@@ -654,7 +744,8 @@ wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
 
    struct wsi_wl_display display;
-   if (wsi_wl_display_init(wsi, &display, surface->display, true))
+   if (wsi_wl_display_init(wsi, &display, surface->display, true,
+                           wsi_device->sw))
       return VK_ERROR_SURFACE_LOST_KHR;
 
    VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
@@ -684,7 +775,8 @@ wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
       (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
 
    struct wsi_wl_display display;
-   if (wsi_wl_display_init(wsi, &display, surface->display, true))
+   if (wsi_wl_display_init(wsi, &display, surface->display, true,
+                           wsi_device->sw))
       return VK_ERROR_SURFACE_LOST_KHR;
 
    VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
@@ -764,6 +856,8 @@ struct wsi_wl_image {
    struct wsi_image                             base;
    struct wl_buffer *                           buffer;
    bool                                         busy;
+   void *data_ptr;
+   uint32_t data_size;
 };
 
 struct wsi_wl_swapchain {
@@ -778,6 +872,7 @@ struct wsi_wl_swapchain {
    VkExtent2D                                   extent;
    VkFormat                                     vk_format;
    uint32_t                                     drm_format;
+   uint32_t                                     shm_format;
 
    uint32_t                                     num_drm_modifiers;
    const uint64_t *                             drm_modifiers;
@@ -895,6 +990,23 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
 {
    struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
 
+   if (chain->display->sw) {
+      struct wsi_wl_image *image = &chain->images[image_index];
+      void *dptr = image->data_ptr;
+      void *sptr;
+      chain->base.wsi->MapMemory(chain->base.device,
+                                 image->base.memory,
+                                 0, 0, 0, &sptr);
+
+      for (unsigned r = 0; r < chain->extent.height; r++) {
+         memcpy(dptr, sptr, image->base.row_pitches[0]);
+         dptr += image->base.row_pitches[0];
+         sptr += image->base.row_pitches[0];
+      }
+      chain->base.wsi->UnmapMemory(chain->base.device,
+                                   image->base.memory);
+
+   }
    if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) {
       while (!chain->fifo_ready) {
          int ret = wl_display_dispatch_queue(chain->display->wl_display,
@@ -964,7 +1076,31 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
    if (result != VK_SUCCESS)
       return result;
 
-   if (display->dmabuf.wl_dmabuf) {
+   if (display->sw) {
+      int fd, stride;
+
+      stride = image->base.row_pitches[0];
+      image->data_size = stride * chain->extent.height;
+
+      /* Create a shareable buffer */
+      fd = os_create_anonymous_file(image->data_size, NULL);
+      if (fd < 0)
+         goto fail_image;
+
+      image->data_ptr = mmap(NULL, image->data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+      if (image->data_ptr == MAP_FAILED) {
+         close(fd);
+         goto fail_image;
+      }
+      /* Share it in a wl_buffer */
+      struct wl_shm_pool *pool = wl_shm_create_pool(display->swrast.wl_shm, fd, image->data_size);
+      wl_proxy_set_queue((struct wl_proxy *)pool, display->queue);
+      image->buffer = wl_shm_pool_create_buffer(pool, 0, chain->extent.width,
+                                                chain->extent.height, stride,
+                                                chain->shm_format);
+      wl_shm_pool_destroy(pool);
+      close(fd);
+   } else if (display->dmabuf.wl_dmabuf) {
       struct zwp_linux_buffer_params_v1 *params =
          zwp_linux_dmabuf_v1_create_params(display->dmabuf.wl_dmabuf);
       if (!params)
@@ -1028,6 +1164,8 @@ wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
       if (chain->images[i].buffer) {
          wl_buffer_destroy(chain->images[i].buffer);
          wsi_destroy_image(&chain->base, &chain->images[i].base);
+         if (chain->images[i].data_ptr)
+            munmap(chain->images[i].data_ptr, chain->images[i].data_size);
       }
    }
 
@@ -1096,7 +1234,10 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
    chain->base.image_count = num_images;
    chain->extent = pCreateInfo->imageExtent;
    chain->vk_format = pCreateInfo->imageFormat;
-   chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
+   if (wsi_device->sw)
+      chain->shm_format = wl_shm_format_for_vk_format(chain->vk_format, alpha);
+   else
+      chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha);
 
    if (pCreateInfo->oldSwapchain) {
       /* If we have an oldSwapchain parameter, copy the display struct over
@@ -1106,7 +1247,8 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
       chain->display = wsi_wl_display_ref(old_chain->display);
    } else {
       chain->display = NULL;
-      result = wsi_wl_display_create(wsi, surface->display, &chain->display);
+      result = wsi_wl_display_create(wsi, surface->display,
+                                     wsi_device->sw, &chain->display);
       if (result != VK_SUCCESS)
          goto fail;
    }



More information about the mesa-commit mailing list