vulkan/wsi/wayland: implement acquire timeout
[mesa.git] / src / vulkan / wsi / wsi_common_wayland.c
index 3113cba3079643a7f3c7cc01b9bbd3e6cbbf2348..438c2f50da39e57fe5d88fa554954252b824bf0f 100644 (file)
@@ -30,6 +30,7 @@
 #include <errno.h>
 #include <string.h>
 #include <pthread.h>
+#include <poll.h>
 
 #include "drm-uapi/drm_fourcc.h"
 
@@ -40,6 +41,7 @@
 #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_wayland;
 
+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;
    /* Actually a proxy wrapper around the event queue */
    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 wsi_wl_display_drm                    drm;
+   struct wsi_wl_display_dmabuf                 dmabuf;
 
-   struct {
-      struct u_vector                           argb8888;
-      struct u_vector                           xrgb8888;
-   } modifiers;
+   struct wsi_wayland *wsi_wl;
 
-   uint32_t                                     capabilities;
+   /* 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;
@@ -228,10 +240,10 @@ static void
 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
@@ -244,7 +256,7 @@ drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t capabilities)
 {
    struct wsi_wl_display *display = data;
 
-   display->capabilities = capabilities;
+   display->drm.capabilities = capabilities;
 }
 
 static const struct wl_drm_listener drm_listener = {
@@ -271,7 +283,7 @@ dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
    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) &&
@@ -280,10 +292,12 @@ dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
 
    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;
@@ -308,18 +322,18 @@ registry_handle_global(void *data, struct wl_registry *registry,
    struct wsi_wl_display *display = data;
 
    if (strcmp(interface, "wl_drm") == 0) {
-      assert(display->drm == NULL);
+      assert(display->drm.wl_drm == NULL);
 
       assert(version >= 2);
-      display->drm = wl_registry_bind(registry, name, &wl_drm_interface, 2);
-
-      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 =
+      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->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);
    }
 }
 
@@ -338,13 +352,14 @@ wsi_wl_display_finish(struct wsi_wl_display *display)
 {
    assert(display->refcount == 0);
 
-   u_vector_finish(&display->formats);
-   u_vector_finish(&display->modifiers.argb8888);
-   u_vector_finish(&display->modifiers.xrgb8888);
-   if (display->drm)
-      wl_drm_destroy(display->drm);
-   if (display->dmabuf)
-      zwp_linux_dmabuf_v1_destroy(display->dmabuf);
+   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.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)
@@ -364,9 +379,12 @@ 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) ||
-          !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;
       }
@@ -396,19 +414,22 @@ wsi_wl_display_init(struct wsi_wayland *wsi_wl,
 
    wl_registry_add_listener(registry, &registry_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) {
-      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);
 
-   /* 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->capabilities & WL_DRM_CAPABILITY_PRIME)) {
+   if (!display->formats) {
       result = VK_ERROR_SURFACE_LOST_KHR;
       goto fail_registry;
    }
@@ -586,7 +607,7 @@ wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface,
    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;
@@ -616,7 +637,7 @@ wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface,
    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;
@@ -735,17 +756,23 @@ wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
                                     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 */
@@ -755,11 +782,44 @@ wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
          }
       }
 
-      /* 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, &current_time);
+      if (timespec_after(&current_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, &current_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;
    }
@@ -858,11 +918,11 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
 
    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++) {
@@ -990,7 +1050,7 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
    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;
@@ -1024,14 +1084,15 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
    /* 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;
@@ -1049,7 +1110,10 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
     * wl_drm.
     */
    if (!chain->num_drm_modifiers) {
-      chain->drm_wrapper = wl_proxy_create_wrapper(chain->display->drm);
+      assert(chain->display->drm.wl_drm);
+
+      chain->drm_wrapper =
+         wl_proxy_create_wrapper(chain->display->drm.wl_drm);
       if (!chain->drm_wrapper) {
          result = VK_ERROR_OUT_OF_HOST_MEMORY;
          goto fail;