vulkan/wsi/wayland: Add support for zwp_dmabuf

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

Signed-off-by: Daniel Stone <daniels@collabora.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
Daniel Stone 2018-02-09 15:43:30 -08:00
parent c757fd2852
commit bfa22266cd
4 changed files with 141 additions and 14 deletions

View file

@ -71,6 +71,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 \

View file

@ -11,7 +11,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 \

View file

@ -54,6 +54,8 @@ if with_platform_wayland
files_vulkan_wsi += [
wayland_drm_client_protocol_h,
wayland_drm_protocol_c,
linux_dmabuf_unstable_v1_client_protocol_h,
linux_dmabuf_unstable_v1_protocol_c,
]
endif

View file

@ -31,10 +31,13 @@
#include <string.h>
#include <pthread.h>
#include <drm_fourcc.h>
#include "vk_util.h"
#include "wsi_common_private.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>
@ -53,11 +56,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,53 @@ static const struct wl_drm_listener drm_listener = {
drm_handle_capabilities,
};
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 we're not fetching formats, don't fetch modifiers either. */
if (display->formats.element_size == 0)
return;
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)
@ -237,6 +293,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);
}
}
@ -277,7 +338,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;
}
@ -706,25 +769,75 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
const VkSwapchainCreateInfoKHR *pCreateInfo,
const VkAllocationCallbacks* pAllocator)
{
struct wsi_wl_display *display = chain->display;
const uint64_t *modifiers = NULL;
uint32_t num_modifiers = 0;
VkResult result;
if (display->dmabuf && chain->base.wsi->supports_modifiers) {
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;
}
}
result = wsi_create_native_image(&chain->base, pCreateInfo,
0, NULL, NULL, &image->base);
num_modifiers > 0 ? 1 : 0,
&num_modifiers, &modifiers,
&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);
if (image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
/* Only request modifiers if we have dmabuf, else it must be implicit. */
assert(display->dmabuf);
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]);
struct zwp_linux_buffer_params_v1 *params =
zwp_linux_dmabuf_v1_create_params(display->dmabuf);
wl_proxy_set_queue((struct wl_proxy *) params, chain->display->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;