#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) ({ \
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;
}
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;