#include <errno.h>
#include <string.h>
#include <pthread.h>
+#include <poll.h>
#include "drm-uapi/drm_fourcc.h"
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include <util/hash_table.h>
+#include <util/timespec.h>
#include <util/u_vector.h>
#define typed_memcpy(dest, src, count) ({ \
struct wsi_wl_display_drm {
struct wl_drm * wl_drm;
+ struct u_vector formats;
uint32_t capabilities;
};
+struct wsi_wl_display_dmabuf {
+ struct zwp_linux_dmabuf_v1 * wl_dmabuf;
+ struct u_vector formats;
+ struct {
+ struct u_vector argb8888;
+ struct u_vector xrgb8888;
+ } modifiers;
+};
+
struct wsi_wl_display {
/* The real wl_display */
struct wl_display * wl_display;
struct wl_event_queue * queue;
struct wsi_wl_display_drm drm;
- struct zwp_linux_dmabuf_v1 * dmabuf;
+ struct wsi_wl_display_dmabuf dmabuf;
struct wsi_wayland *wsi_wl;
- /* Vector of VkFormats supported */
- struct u_vector formats;
- struct {
- struct u_vector argb8888;
- struct u_vector xrgb8888;
- } modifiers;
+ /* Points to formats in wsi_wl_display_drm or wsi_wl_display_dmabuf */
+ struct u_vector * formats;
/* Only used for displays created by wsi_wl_display_create */
uint32_t refcount;
drm_handle_format(void *data, struct wl_drm *drm, uint32_t wl_format)
{
struct wsi_wl_display *display = data;
- if (display->formats.element_size == 0)
+ if (display->drm.formats.element_size == 0)
return;
- wsi_wl_display_add_wl_format(display, &display->formats, wl_format);
+ wsi_wl_display_add_wl_format(display, &display->drm.formats, wl_format);
}
static void
uint64_t *mod = NULL;
/* If we're not fetching formats, don't fetch modifiers either. */
- if (display->formats.element_size == 0)
+ if (display->dmabuf.formats.element_size == 0)
return;
if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
switch (format) {
case WL_DRM_FORMAT_ARGB8888:
- mod = u_vector_add(&display->modifiers.argb8888);
+ wsi_wl_display_add_wl_format(display, &display->dmabuf.formats, format);
+ mod = u_vector_add(&display->dmabuf.modifiers.argb8888);
break;
case WL_DRM_FORMAT_XRGB8888:
- mod = u_vector_add(&display->modifiers.xrgb8888);
+ wsi_wl_display_add_wl_format(display, &display->dmabuf.formats, format);
+ mod = u_vector_add(&display->dmabuf.modifiers.xrgb8888);
break;
default:
break;
display->drm.wl_drm =
wl_registry_bind(registry, name, &wl_drm_interface, 2);
wl_drm_add_listener(display->drm.wl_drm, &drm_listener, display);
- } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
- display->dmabuf =
+ } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3 &&
+ display->wsi_wl->wsi->supports_modifiers) {
+ display->dmabuf.wl_dmabuf =
wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface, 3);
- zwp_linux_dmabuf_v1_add_listener(display->dmabuf, &dmabuf_listener,
- display);
+ zwp_linux_dmabuf_v1_add_listener(display->dmabuf.wl_dmabuf,
+ &dmabuf_listener, display);
}
}
{
assert(display->refcount == 0);
- u_vector_finish(&display->formats);
- u_vector_finish(&display->modifiers.argb8888);
- u_vector_finish(&display->modifiers.xrgb8888);
+ 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->drm.wl_drm)
wl_drm_destroy(display->drm.wl_drm);
- if (display->dmabuf)
- zwp_linux_dmabuf_v1_destroy(display->dmabuf);
+ if (display->dmabuf.wl_dmabuf)
+ zwp_linux_dmabuf_v1_destroy(display->dmabuf.wl_dmabuf);
if (display->wl_display_wrapper)
wl_proxy_wrapper_destroy(display->wl_display_wrapper);
if (display->queue)
display->wl_display = wl_display;
if (get_format_list) {
- 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)) {
+ if (!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) ||
+ !u_vector_init(&display->dmabuf.modifiers.xrgb8888,
+ sizeof(uint64_t), 32)) {
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto fail;
}
wl_registry_add_listener(registry, ®istry_listener, display);
- /* Round-trip to get the wl_drm global */
+ /* Round-trip to get wl_drms and zwp_linux_dmabuf_v1 globals */
wl_display_roundtrip_queue(display->wl_display, display->queue);
- if (!display->drm.wl_drm) {
- result = VK_ERROR_SURFACE_LOST_KHR;
- goto fail_registry;
+ /* Round-trip again to get formats, modifiers and capabilities */
+ if (display->drm.wl_drm || display->dmabuf.wl_dmabuf)
+ wl_display_roundtrip_queue(display->wl_display, display->queue);
+
+ if (wsi_wl->wsi->force_bgra8_unorm_first) {
+ /* Find BGRA8_UNORM in the list and swap it to the first position if we
+ * can find it. Some apps get confused if SRGB is first in the list.
+ */
+ VkFormat *first_fmt = u_vector_head(display->formats);
+ VkFormat *iter_fmt;
+ u_vector_foreach(iter_fmt, display->formats) {
+ if (*iter_fmt == VK_FORMAT_B8G8R8A8_UNORM) {
+ *iter_fmt = *first_fmt;
+ *first_fmt = VK_FORMAT_B8G8R8A8_UNORM;
+ break;
+ }
+ }
}
- /* Round-trip to get wl_drm formats and capabilities */
- wl_display_roundtrip_queue(display->wl_display, display->queue);
+ /* We need prime support for wl_drm */
+ if (display->drm.wl_drm &&
+ (display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
+ display->formats = &display->drm.formats;
+ } else if (display->dmabuf.wl_dmabuf) {
+ display->formats = &display->dmabuf.formats;
+ }
- /* We need prime support */
- if (!(display->drm.capabilities & WL_DRM_CAPABILITY_PRIME)) {
+ if (!display->formats) {
result = VK_ERROR_SURFACE_LOST_KHR;
goto fail_registry;
}
VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
VkFormat *disp_fmt;
- u_vector_foreach(disp_fmt, &display.formats) {
+ u_vector_foreach(disp_fmt, display.formats) {
vk_outarray_append(&out, out_fmt) {
out_fmt->format = *disp_fmt;
out_fmt->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
VkFormat *disp_fmt;
- u_vector_foreach(disp_fmt, &display.formats) {
+ u_vector_foreach(disp_fmt, display.formats) {
vk_outarray_append(&out, out_fmt) {
out_fmt->surfaceFormat.format = *disp_fmt;
out_fmt->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
uint32_t *image_index)
{
struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
+ struct timespec start_time, end_time;
+ struct timespec rel_timeout;
+ int wl_fd = wl_display_get_fd(chain->display->wl_display);
- int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
- chain->display->queue);
- /* XXX: I'm not sure if out-of-date is the right error here. If
- * wl_display_dispatch_queue_pending fails it most likely means we got
- * kicked by the server so this seems more-or-less correct.
- */
- if (ret < 0)
- return VK_ERROR_OUT_OF_DATE_KHR;
+ timespec_from_nsec(&rel_timeout, info->timeout);
+
+ clock_gettime(CLOCK_MONOTONIC, &start_time);
+ timespec_add(&end_time, &rel_timeout, &start_time);
while (1) {
+ /* Try to dispatch potential events. */
+ int ret = wl_display_dispatch_queue_pending(chain->display->wl_display,
+ chain->display->queue);
+ if (ret < 0)
+ return VK_ERROR_OUT_OF_DATE_KHR;
+
+ /* Try to find a free image. */
for (uint32_t i = 0; i < chain->base.image_count; i++) {
if (!chain->images[i].busy) {
/* We found a non-busy image */
}
}
- /* This time we do a blocking dispatch because we can't go
- * anywhere until we get an event.
- */
- int ret = wl_display_roundtrip_queue(chain->display->wl_display,
- chain->display->queue);
+ /* Check for timeout. */
+ struct timespec current_time;
+ clock_gettime(CLOCK_MONOTONIC, ¤t_time);
+ if (timespec_after(¤t_time, &end_time))
+ return VK_NOT_READY;
+
+ /* Try to read events from the server. */
+ ret = wl_display_prepare_read_queue(chain->display->wl_display,
+ chain->display->queue);
+ if (ret < 0) {
+ /* Another thread might have read events for our queue already. Go
+ * back to dispatch them.
+ */
+ if (errno == EAGAIN)
+ continue;
+ return VK_ERROR_OUT_OF_DATE_KHR;
+ }
+
+ struct pollfd pollfd = {
+ .fd = wl_fd,
+ .events = POLLIN
+ };
+ timespec_sub(&rel_timeout, &end_time, ¤t_time);
+ ret = ppoll(&pollfd, 1, &rel_timeout, NULL);
+ if (ret <= 0) {
+ int lerrno = errno;
+ wl_display_cancel_read(chain->display->wl_display);
+ if (ret < 0) {
+ /* If ppoll() was interrupted, try again. */
+ if (lerrno == EINTR || lerrno == EAGAIN)
+ continue;
+ return VK_ERROR_OUT_OF_DATE_KHR;
+ }
+ assert(ret == 0);
+ continue;
+ }
+
+ ret = wl_display_read_events(chain->display->wl_display);
if (ret < 0)
return VK_ERROR_OUT_OF_DATE_KHR;
}
if (!chain->drm_wrapper) {
/* Only request modifiers if we have dmabuf, else it must be implicit. */
- assert(display->dmabuf);
+ assert(display->dmabuf.wl_dmabuf);
assert(image->base.drm_modifier != DRM_FORMAT_MOD_INVALID);
struct zwp_linux_buffer_params_v1 *params =
- zwp_linux_dmabuf_v1_create_params(display->dmabuf);
+ zwp_linux_dmabuf_v1_create_params(display->dmabuf.wl_dmabuf);
wl_proxy_set_queue((struct wl_proxy *) params, chain->display->queue);
for (int i = 0; i < image->base.num_planes; i++) {
chain->base.get_wsi_image = wsi_wl_swapchain_get_wsi_image;
chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
chain->base.queue_present = wsi_wl_swapchain_queue_present;
- chain->base.present_mode = pCreateInfo->presentMode;
+ chain->base.present_mode = wsi_swapchain_get_present_mode(wsi_device, pCreateInfo);
chain->base.image_count = num_images;
chain->extent = pCreateInfo->imageExtent;
chain->vk_format = pCreateInfo->imageFormat;
/* Use explicit DRM format modifiers when both the server and the driver
* support them.
*/
- if (chain->display->dmabuf && chain->base.wsi->supports_modifiers) {
+ if (chain->display->dmabuf.wl_dmabuf &&
+ chain->base.wsi->supports_modifiers) {
struct u_vector *modifiers;
switch (chain->drm_format) {
case WL_DRM_FORMAT_ARGB8888:
- modifiers = &chain->display->modifiers.argb8888;
+ modifiers = &chain->display->dmabuf.modifiers.argb8888;
break;
case WL_DRM_FORMAT_XRGB8888:
- modifiers = &chain->display->modifiers.xrgb8888;
+ modifiers = &chain->display->dmabuf.modifiers.xrgb8888;
break;
default:
modifiers = NULL;
* wl_drm.
*/
if (!chain->num_drm_modifiers) {
+ assert(chain->display->drm.wl_drm);
+
chain->drm_wrapper =
wl_proxy_create_wrapper(chain->display->drm.wl_drm);
if (!chain->drm_wrapper) {