[Mesa-dev] [RFC v4 21/23] vulkan/wsi/wayland: Add support for zwp_dmabuf

Louis-Francis Ratté-Boulianne lfrb at collabora.com
Mon Oct 16 07:04:31 UTC 2017


From: Daniel Stone <daniels at collabora.com>

zwp_linux_dmabuf_v1 lets us use multi-planar images and buffer
modifiers.

Signed-off-by: Daniel Stone <daniels at collabora.com>
---
 src/vulkan/Makefile.am              |  10 +++
 src/vulkan/Makefile.sources         |   4 +-
 src/vulkan/wsi/wsi_common_wayland.c | 139 +++++++++++++++++++++++++++++++-----
 3 files changed, 136 insertions(+), 17 deletions(-)

diff --git a/src/vulkan/Makefile.am b/src/vulkan/Makefile.am
index 8766952eaf..d7e1db3c29 100644
--- a/src/vulkan/Makefile.am
+++ b/src/vulkan/Makefile.am
@@ -67,6 +67,16 @@ wsi/wayland-drm-client-protocol.h : $(WL_DRM_XML)
 	$(MKDIR_GEN)
 	$(AM_V_GEN)$(WAYLAND_SCANNER) client-header < $< > $@
 
+WL_DMABUF_XML = $(WAYLAND_PROTOCOLS_DATADIR)/unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml
+
+wsi/linux-dmabuf-unstable-v1-protocol.c : $(WL_DMABUF_XML)
+	$(MKDIR_GEN)
+	$(AM_V_GEN)$(WAYLAND_SCANNER) code < $< > $@
+
+wsi/linux-dmabuf-unstable-v1-client-protocol.h : $(WL_DMABUF_XML)
+	$(MKDIR_GEN)
+	$(AM_V_GEN)$(WAYLAND_SCANNER) client-header < $< > $@
+
 if HAVE_PLATFORM_WAYLAND
 AM_CPPFLAGS += \
 	-I$(top_builddir)/src/vulkan/wsi \
diff --git a/src/vulkan/Makefile.sources b/src/vulkan/Makefile.sources
index 2cf7218e92..95685caab7 100644
--- a/src/vulkan/Makefile.sources
+++ b/src/vulkan/Makefile.sources
@@ -9,7 +9,9 @@ VULKAN_WSI_WAYLAND_FILES := \
 
 VULKAN_WSI_WAYLAND_GENERATED_FILES := \
 	wsi/wayland-drm-protocol.c \
-	wsi/wayland-drm-client-protocol.h
+	wsi/wayland-drm-client-protocol.h \
+	wsi/linux-dmabuf-unstable-v1-protocol.c \
+	wsi/linux-dmabuf-unstable-v1-client-protocol.h
 
 VULKAN_WSI_X11_FILES := \
 	wsi/wsi_common_x11.c \
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index 0ab73f0957..b4dd2d7ff0 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -31,9 +31,12 @@
 #include <string.h>
 #include <pthread.h>
 
+#include <drm_fourcc.h>
+
 #include "vk_util.h"
 #include "wsi_common_wayland.h"
 #include "wayland-drm-client-protocol.h"
+#include "linux-dmabuf-unstable-v1-client-protocol.h"
 
 #include <util/hash_table.h>
 #include <util/u_vector.h>
@@ -52,11 +55,17 @@ struct wsi_wl_display {
    struct wl_display *                          wl_display_wrapper;
    struct wl_event_queue *                      queue;
    struct wl_drm *                              drm;
+   struct zwp_linux_dmabuf_v1 *                 dmabuf;
 
    struct wsi_wayland *wsi_wl;
    /* Vector of VkFormats supported */
    struct u_vector                            formats;
 
+   struct {
+      struct u_vector                           argb8888;
+      struct u_vector                           xrgb8888;
+   } modifiers;
+
    uint32_t                                     capabilities;
 
    /* Only used for displays created by wsi_wl_display_create */
@@ -223,6 +232,49 @@ static const struct wl_drm_listener drm_listener = {
 };
 
 static void
+dmabuf_handle_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
+                     uint32_t format)
+{
+   /* Formats are implicitly advertised by the modifier event, so we ignore
+    * them here. */
+}
+
+static void
+dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
+                       uint32_t format, uint32_t modifier_hi,
+                       uint32_t modifier_lo)
+{
+   struct wsi_wl_display *display = data;
+   uint64_t *mod = NULL;
+
+   if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
+       modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
+      return;
+
+   switch (format) {
+   case WL_DRM_FORMAT_ARGB8888:
+      mod = u_vector_add(&display->modifiers.argb8888);
+      break;
+   case WL_DRM_FORMAT_XRGB8888:
+      mod = u_vector_add(&display->modifiers.xrgb8888);
+      break;
+   default:
+      break;
+   }
+
+   if (!mod)
+      return;
+
+   *mod = (uint64_t) modifier_hi << 32;
+   *mod |= (uint64_t) (modifier_lo & 0xffffffff);
+}
+
+static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
+   dmabuf_handle_format,
+   dmabuf_handle_modifier,
+};
+
+static void
 registry_handle_global(void *data, struct wl_registry *registry,
                        uint32_t name, const char *interface, uint32_t version)
 {
@@ -236,6 +288,11 @@ registry_handle_global(void *data, struct wl_registry *registry,
 
       if (display->drm)
          wl_drm_add_listener(display->drm, &drm_listener, display);
+   } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
+      display->dmabuf =
+         wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface, 3);
+      zwp_linux_dmabuf_v1_add_listener(display->dmabuf, &dmabuf_listener,
+                                       display);
    }
 }
 
@@ -276,7 +333,9 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
    display->wl_display = wl_display;
 
    if (get_format_list) {
-      if (!u_vector_init(&display->formats, sizeof(VkFormat), 8)) {
+      if (!u_vector_init(&display->formats, sizeof(VkFormat), 8) ||
+          !u_vector_init(&display->modifiers.argb8888, sizeof(uint64_t), 32) ||
+          !u_vector_init(&display->modifiers.xrgb8888, sizeof(uint64_t), 32)) {
          result = VK_ERROR_OUT_OF_HOST_MEMORY;
          goto fail;
       }
@@ -725,31 +784,79 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
                   const VkAllocationCallbacks* pAllocator)
 {
    VkDevice vk_device = chain->base.device;
+   struct wsi_wl_display *display = chain->display;
+   uint64_t *modifiers = NULL;
+   int num_modifiers = 0;
+   int num_tranches = 0;
    VkResult result;
 
+   if (display->dmabuf) {
+      switch (chain->drm_format) {
+      case WL_DRM_FORMAT_ARGB8888:
+         modifiers = u_vector_tail(&display->modifiers.argb8888);
+         num_modifiers = u_vector_length(&display->modifiers.argb8888);
+         break;
+      case WL_DRM_FORMAT_XRGB8888:
+         modifiers = u_vector_tail(&display->modifiers.xrgb8888);
+         num_modifiers = u_vector_length(&display->modifiers.xrgb8888);
+         break;
+      default:
+         break;
+      }
+   }
+
+   if (num_modifiers > 0)
+      num_tranches = 1;
+
    result = chain->base.image_fns->create_wsi_image(vk_device,
                                                     pCreateInfo,
                                                     pAllocator,
                                                     false,
-                                                    NULL,
-                                                    NULL,
-                                                    0,
+                                                    &modifiers,
+                                                    &num_modifiers,
+                                                    num_tranches,
                                                     &image->base);
    if (result != VK_SUCCESS)
       return result;
 
-   /* Without passing modifiers, we can't have multi-plane RGB images. */
-   assert(image->base.num_planes == 1);
-
-   image->buffer = wl_drm_create_prime_buffer(chain->drm_wrapper,
-                                              image->base.fds[0], /* name */
-                                              chain->extent.width,
-                                              chain->extent.height,
-                                              chain->drm_format,
-                                              image->base.offsets[0],
-                                              image->base.row_pitches[0],
-                                              0, 0, 0, 0 /* unused */);
-   close(image->base.fds[0]);
+   if (display->dmabuf && image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
+      struct zwp_linux_buffer_params_v1 *params =
+         zwp_linux_dmabuf_v1_create_params(display->dmabuf);
+      wl_proxy_set_queue((struct wl_proxy *) params, chain->queue);
+
+      for (int i = 0; i < image->base.num_planes; i++) {
+         zwp_linux_buffer_params_v1_add(params,
+                                        image->base.fds[i],
+                                        i,
+                                        image->base.offsets[i],
+                                        image->base.row_pitches[i],
+                                        image->base.drm_modifier >> 32,
+                                        image->base.drm_modifier & 0xffffffff);
+         close(image->base.fds[i]);
+      }
+
+      image->buffer =
+         zwp_linux_buffer_params_v1_create_immed(params,
+                                                 chain->extent.width,
+                                                 chain->extent.height,
+                                                 chain->drm_format,
+                                                 0);
+      zwp_linux_buffer_params_v1_destroy(params);
+   } else {
+      /* Without passing modifiers, we can't have multi-plane RGB images. */
+      assert(image->base.num_planes == 1);
+
+      image->buffer =
+         wl_drm_create_prime_buffer(chain->drm_wrapper,
+                                    image->base.fds[0], /* name */
+                                    chain->extent.width,
+                                    chain->extent.height,
+                                    chain->drm_format,
+                                    image->base.offsets[0],
+                                    image->base.row_pitches[0],
+                                    0, 0, 0, 0 /* unused */);
+      close(image->base.fds[0]);
+   }
 
    if (!image->buffer)
       goto fail_image;
-- 
2.13.0



More information about the mesa-dev mailing list