anv/blorp: Add a gcd_pow2_u64 helper and use it for buffer alignments
[mesa.git] / src / intel / vulkan / anv_wsi_x11.c
index aa7e6ebe3e777ab8e3dfbf4bd235f74c48e80cec..7c6ef9783391af732170a3773b1f478b53c281c7 100644 (file)
  * IN THE SOFTWARE.
  */
 
+#include <X11/Xlib-xcb.h>
+#include <X11/xshmfence.h>
 #include <xcb/xcb.h>
 #include <xcb/dri3.h>
 #include <xcb/present.h>
 
 #include "anv_wsi.h"
 
+#include "vk_format_info.h"
 #include "util/hash_table.h"
 
 struct wsi_x11_connection {
@@ -43,13 +46,14 @@ struct wsi_x11 {
 };
 
 static struct wsi_x11_connection *
-wsi_x11_connection_create(struct anv_instance *instance, xcb_connection_t *conn)
+wsi_x11_connection_create(struct anv_physical_device *device,
+                          xcb_connection_t *conn)
 {
    xcb_query_extension_cookie_t dri3_cookie, pres_cookie;
    xcb_query_extension_reply_t *dri3_reply, *pres_reply;
 
    struct wsi_x11_connection *wsi_conn =
-      anv_alloc(&instance->alloc, sizeof(*wsi_conn), 8,
+      anv_alloc(&device->instance->alloc, sizeof(*wsi_conn), 8,
                 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
    if (!wsi_conn)
       return NULL;
@@ -62,7 +66,7 @@ wsi_x11_connection_create(struct anv_instance *instance, xcb_connection_t *conn)
    if (dri3_reply == NULL || pres_reply == NULL) {
       free(dri3_reply);
       free(pres_reply);
-      anv_free(&instance->alloc, wsi_conn);
+      anv_free(&device->instance->alloc, wsi_conn);
       return NULL;
    }
 
@@ -76,17 +80,18 @@ wsi_x11_connection_create(struct anv_instance *instance, xcb_connection_t *conn)
 }
 
 static void
-wsi_x11_connection_destroy(struct anv_instance *instance,
+wsi_x11_connection_destroy(struct anv_physical_device *device,
                            struct wsi_x11_connection *conn)
 {
-   anv_free(&instance->alloc, conn);
+   anv_free(&device->instance->alloc, conn);
 }
 
 static struct wsi_x11_connection *
-wsi_x11_get_connection(struct anv_instance *instance, xcb_connection_t *conn)
+wsi_x11_get_connection(struct anv_physical_device *device,
+                       xcb_connection_t *conn)
 {
    struct wsi_x11 *wsi =
-      (struct wsi_x11 *)instance->wsi[VK_ICD_WSI_PLATFORM_XCB];
+      (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
 
    pthread_mutex_lock(&wsi->mutex);
 
@@ -98,14 +103,14 @@ wsi_x11_get_connection(struct anv_instance *instance, xcb_connection_t *conn)
       pthread_mutex_unlock(&wsi->mutex);
 
       struct wsi_x11_connection *wsi_conn =
-         wsi_x11_connection_create(instance, conn);
+         wsi_x11_connection_create(device, conn);
 
       pthread_mutex_lock(&wsi->mutex);
 
       entry = _mesa_hash_table_search(wsi->connections, conn);
       if (entry) {
          /* Oops, someone raced us to it */
-         wsi_x11_connection_destroy(instance, wsi_conn);
+         wsi_x11_connection_destroy(device, wsi_conn);
       } else {
          entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
       }
@@ -235,7 +240,7 @@ VkBool32 anv_GetPhysicalDeviceXcbPresentationSupportKHR(
    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
 
    struct wsi_x11_connection *wsi_conn =
-      wsi_x11_get_connection(device->instance, connection);
+      wsi_x11_get_connection(device, connection);
 
    if (!wsi_conn->has_dri3) {
       fprintf(stderr, "vulkan: No DRI3 support\n");
@@ -252,16 +257,47 @@ VkBool32 anv_GetPhysicalDeviceXcbPresentationSupportKHR(
    return true;
 }
 
+VkBool32 anv_GetPhysicalDeviceXlibPresentationSupportKHR(
+    VkPhysicalDevice                            physicalDevice,
+    uint32_t                                    queueFamilyIndex,
+    Display*                                    dpy,
+    VisualID                                    visualID)
+{
+   return anv_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice,
+                                                         queueFamilyIndex,
+                                                         XGetXCBConnection(dpy),
+                                                         visualID);
+}
+
+static xcb_connection_t*
+x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
+{
+   if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
+      return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
+   else
+      return ((VkIcdSurfaceXcb *)icd_surface)->connection;
+}
+
+static xcb_window_t
+x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
+{
+   if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
+      return ((VkIcdSurfaceXlib *)icd_surface)->window;
+   else
+      return ((VkIcdSurfaceXcb *)icd_surface)->window;
+}
+
 static VkResult
 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
                         struct anv_physical_device *device,
                         uint32_t queueFamilyIndex,
                         VkBool32* pSupported)
 {
-   VkIcdSurfaceXcb *surface = (VkIcdSurfaceXcb *)icd_surface;
+   xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
+   xcb_window_t window = x11_surface_get_window(icd_surface);
 
    struct wsi_x11_connection *wsi_conn =
-      wsi_x11_get_connection(device->instance, surface->connection);
+      wsi_x11_get_connection(device, conn);
    if (!wsi_conn)
       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 
@@ -272,8 +308,7 @@ x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
    }
 
    unsigned visual_depth;
-   if (!get_visualtype_for_window(surface->connection, surface->window,
-                                  &visual_depth)) {
+   if (!get_visualtype_for_window(conn, window, &visual_depth)) {
       *pSupported = false;
       return VK_SUCCESS;
    }
@@ -292,22 +327,22 @@ x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
                              struct anv_physical_device *device,
                              VkSurfaceCapabilitiesKHR *caps)
 {
-   VkIcdSurfaceXcb *surface = (VkIcdSurfaceXcb *)icd_surface;
+   xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
+   xcb_window_t window = x11_surface_get_window(icd_surface);
    xcb_get_geometry_cookie_t geom_cookie;
    xcb_generic_error_t *err;
    xcb_get_geometry_reply_t *geom;
    unsigned visual_depth;
 
-   geom_cookie = xcb_get_geometry(surface->connection, surface->window);
+   geom_cookie = xcb_get_geometry(conn, window);
 
    /* This does a round-trip.  This is why we do get_geometry first and
     * wait to read the reply until after we have a visual.
     */
    xcb_visualtype_t *visual =
-      get_visualtype_for_window(surface->connection, surface->window,
-                                &visual_depth);
+      get_visualtype_for_window(conn, window, &visual_depth);
 
-   geom = xcb_get_geometry_reply(surface->connection, geom_cookie, &err);
+   geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
    if (geom) {
       VkExtent2D extent = { geom->width, geom->height };
       caps->currentExtent = extent;
@@ -416,12 +451,39 @@ VkResult anv_CreateXcbSurfaceKHR(
    return VK_SUCCESS;
 }
 
+VkResult anv_CreateXlibSurfaceKHR(
+    VkInstance                                  _instance,
+    const VkXlibSurfaceCreateInfoKHR*           pCreateInfo,
+    const VkAllocationCallbacks*                pAllocator,
+    VkSurfaceKHR*                               pSurface)
+{
+   ANV_FROM_HANDLE(anv_instance, instance, _instance);
+
+   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
+
+   VkIcdSurfaceXlib *surface;
+
+   surface = anv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
+                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+   if (surface == NULL)
+      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
+
+   surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
+   surface->dpy = pCreateInfo->dpy;
+   surface->window = pCreateInfo->window;
+
+   *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
+
+   return VK_SUCCESS;
+}
+
 struct x11_image {
    struct anv_image *                        image;
    struct anv_device_memory *                memory;
    xcb_pixmap_t                              pixmap;
-   xcb_get_geometry_cookie_t                 geom_cookie;
    bool                                      busy;
+   struct xshmfence *                        shm_fence;
+   uint32_t                                  sync_fence;
 };
 
 struct x11_swapchain {
@@ -432,7 +494,12 @@ struct x11_swapchain {
    xcb_gc_t                                     gc;
    VkExtent2D                                   extent;
    uint32_t                                     image_count;
-   uint32_t                                     next_image;
+
+   xcb_present_event_t                          event_id;
+   xcb_special_event_t *                        special_event;
+   uint64_t                                     send_sbc;
+   uint32_t                                     stamp;
+
    struct x11_image                             images[0];
 };
 
@@ -456,6 +523,42 @@ x11_get_images(struct anv_swapchain *anv_chain,
    return VK_SUCCESS;
 }
 
+static VkResult
+x11_handle_dri3_present_event(struct x11_swapchain *chain,
+                              xcb_present_generic_event_t *event)
+{
+   switch (event->evtype) {
+   case XCB_PRESENT_CONFIGURE_NOTIFY: {
+      xcb_present_configure_notify_event_t *config = (void *) event;
+
+      if (config->width != chain->extent.width ||
+          config->height != chain->extent.height)
+         return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
+
+      break;
+   }
+
+   case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
+      xcb_present_idle_notify_event_t *idle = (void *) event;
+
+      for (unsigned i = 0; i < chain->image_count; i++) {
+         if (chain->images[i].pixmap == idle->pixmap) {
+            chain->images[i].busy = false;
+            break;
+         }
+      }
+
+      break;
+   }
+
+   case XCB_PRESENT_COMPLETE_NOTIFY:
+   default:
+      break;
+   }
+
+   return VK_SUCCESS;
+}
+
 static VkResult
 x11_acquire_next_image(struct anv_swapchain *anv_chain,
                        uint64_t timeout,
@@ -463,30 +566,28 @@ x11_acquire_next_image(struct anv_swapchain *anv_chain,
                        uint32_t *image_index)
 {
    struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
-   struct x11_image *image = &chain->images[chain->next_image];
-
-   if (image->busy) {
-      xcb_generic_error_t *err;
-      xcb_get_geometry_reply_t *geom =
-         xcb_get_geometry_reply(chain->conn, image->geom_cookie, &err);
-      if (!geom) {
-         free(err);
-         return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
+
+   while (1) {
+      for (uint32_t i = 0; i < chain->image_count; i++) {
+         if (!chain->images[i].busy) {
+            /* We found a non-busy image */
+            xshmfence_await(chain->images[i].shm_fence);
+            *image_index = i;
+            return VK_SUCCESS;
+         }
       }
 
-      if (geom->width != chain->extent.width ||
-          geom->height != chain->extent.height) {
-         free(geom);
+      xcb_flush(chain->conn);
+      xcb_generic_event_t *event =
+         xcb_wait_for_special_event(chain->conn, chain->special_event);
+      if (!event)
          return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
-      }
-      free(geom);
 
-      image->busy = false;
+      VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
+      free(event);
+      if (result != VK_SUCCESS)
+         return result;
    }
-
-   *image_index = chain->next_image;
-   chain->next_image = (chain->next_image + 1) % chain->image_count;
-   return VK_SUCCESS;
 }
 
 static VkResult
@@ -499,19 +600,33 @@ x11_queue_present(struct anv_swapchain *anv_chain,
 
    assert(image_index < chain->image_count);
 
-   xcb_void_cookie_t cookie;
+   uint32_t options = XCB_PRESENT_OPTION_NONE;
 
-   cookie = xcb_copy_area(chain->conn,
-                          image->pixmap,
-                          chain->window,
-                          chain->gc,
-                          0, 0,
-                          0, 0,
-                          chain->extent.width,
-                          chain->extent.height);
-   xcb_discard_reply(chain->conn, cookie.sequence);
+   int64_t target_msc = 0;
+   int64_t divisor = 0;
+   int64_t remainder = 0;
+
+   options |= XCB_PRESENT_OPTION_ASYNC;
+
+   xshmfence_reset(image->shm_fence);
 
-   image->geom_cookie = xcb_get_geometry(chain->conn, chain->window);
+   xcb_void_cookie_t cookie =
+      xcb_present_pixmap(chain->conn,
+                         chain->window,
+                         image->pixmap,
+                         (uint32_t) chain->send_sbc,
+                         0,                                    /* valid */
+                         0,                                    /* update */
+                         0,                                    /* x_off */
+                         0,                                    /* y_off */
+                         XCB_NONE,                             /* target_crtc */
+                         XCB_NONE,
+                         image->sync_fence,
+                         options,
+                         target_msc,
+                         divisor,
+                         remainder, 0, NULL);
+   xcb_discard_reply(chain->conn, cookie.sequence);
    image->busy = true;
 
    xcb_flush(chain->conn);
@@ -525,6 +640,7 @@ x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
                const VkAllocationCallbacks* pAllocator,
                struct x11_image *image)
 {
+   xcb_void_cookie_t cookie;
    VkResult result;
 
    VkImage image_h;
@@ -547,7 +663,8 @@ x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
          .samples = 1,
          /* FIXME: Need a way to use X tiling to allow scanout */
          .tiling = VK_IMAGE_TILING_OPTIMAL,
-         .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
+         .usage = (pCreateInfo->imageUsage |
+                   VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
          .flags = 0,
       }},
       NULL,
@@ -556,7 +673,7 @@ x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
       return result;
 
    image->image = anv_image_from_handle(image_h);
-   assert(anv_format_is_color(image->image->format));
+   assert(vk_format_is_color(image->image->vk_format));
 
    VkDeviceMemory memory_h;
    result = anv_AllocateMemory(anv_device_to_handle(device),
@@ -599,7 +716,7 @@ x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
    uint32_t depth = 24;
    image->pixmap = xcb_generate_id(chain->conn);
 
-   xcb_void_cookie_t cookie =
+   cookie =
       xcb_dri3_pixmap_from_buffer_checked(chain->conn,
                                           image->pixmap,
                                           chain->window,
@@ -608,13 +725,35 @@ x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
                                           pCreateInfo->imageExtent.height,
                                           surface->isl.row_pitch,
                                           depth, bpp, fd);
+   xcb_discard_reply(chain->conn, cookie.sequence);
 
-   image->busy = false;
+   int fence_fd = xshmfence_alloc_shm();
+   if (fence_fd < 0)
+      goto fail_pixmap;
 
-   xcb_discard_reply(chain->conn, cookie.sequence);
+   image->shm_fence = xshmfence_map_shm(fence_fd);
+   if (image->shm_fence == NULL)
+      goto fail_shmfence_alloc;
+
+   image->sync_fence = xcb_generate_id(chain->conn);
+   xcb_dri3_fence_from_fd(chain->conn,
+                          image->pixmap,
+                          image->sync_fence,
+                          false,
+                          fence_fd);
+
+   image->busy = false;
+   xshmfence_trigger(image->shm_fence);
 
    return VK_SUCCESS;
 
+fail_shmfence_alloc:
+   close(fence_fd);
+
+fail_pixmap:
+   cookie = xcb_free_pixmap(chain->conn, image->pixmap);
+   xcb_discard_reply(chain->conn, cookie.sequence);
+
 fail_alloc_memory:
    anv_FreeMemory(anv_device_to_handle(chain->base.device),
                   anv_device_memory_to_handle(image->memory), pAllocator);
@@ -631,10 +770,13 @@ x11_image_finish(struct x11_swapchain *chain,
                  const VkAllocationCallbacks* pAllocator,
                  struct x11_image *image)
 {
-   if (image->busy)
-      xcb_discard_reply(chain->conn, image->geom_cookie.sequence);
+   xcb_void_cookie_t cookie;
+
+   cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
+   xcb_discard_reply(chain->conn, cookie.sequence);
+   xshmfence_unmap_shm(image->shm_fence);
 
-   xcb_void_cookie_t cookie = xcb_free_pixmap(chain->conn, image->pixmap);
+   cookie = xcb_free_pixmap(chain->conn, image->pixmap);
    xcb_discard_reply(chain->conn, cookie.sequence);
 
    anv_DestroyImage(anv_device_to_handle(chain->base.device),
@@ -653,6 +795,8 @@ x11_swapchain_destroy(struct anv_swapchain *anv_chain,
    for (uint32_t i = 0; i < chain->image_count; i++)
       x11_image_finish(chain, pAllocator, &chain->images[i]);
 
+   xcb_unregister_for_special_event(chain->conn, chain->special_event);
+
    anv_free2(&chain->base.device->alloc, pAllocator, chain);
 
    return VK_SUCCESS;
@@ -665,14 +809,22 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
                              const VkAllocationCallbacks* pAllocator,
                              struct anv_swapchain **swapchain_out)
 {
-   VkIcdSurfaceXcb *surface = (VkIcdSurfaceXcb *)icd_surface;
    struct x11_swapchain *chain;
    xcb_void_cookie_t cookie;
    VkResult result;
 
+   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
+
    int num_images = pCreateInfo->minImageCount;
 
-   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
+   /* For true mailbox mode, we need at least 4 images:
+    *  1) One to scan out from
+    *  2) One to have queued for scan-out
+    *  3) One to be currently held by the Wayland compositor
+    *  4) One to render to
+    */
+   if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
+      num_images = MAX2(num_images, 4);
 
    size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
    chain = anv_alloc2(&device->alloc, pAllocator, size, 8,
@@ -686,17 +838,29 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
    chain->base.acquire_next_image = x11_acquire_next_image;
    chain->base.queue_present = x11_queue_present;
 
-   chain->conn = surface->connection;
-   chain->window = surface->window;
+   chain->conn = x11_surface_get_connection(icd_surface);
+   chain->window = x11_surface_get_window(icd_surface);
    chain->extent = pCreateInfo->imageExtent;
    chain->image_count = num_images;
-   chain->next_image = 0;
+
+   chain->event_id = xcb_generate_id(chain->conn);
+   xcb_present_select_input(chain->conn, chain->event_id, chain->window,
+                            XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
+                            XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
+                            XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
+
+   /* Create an XCB event queue to hold present events outside of the usual
+    * application event queue
+    */
+   chain->special_event =
+      xcb_register_for_special_xge(chain->conn, &xcb_present_id,
+                                   chain->event_id, NULL);
 
    chain->gc = xcb_generate_id(chain->conn);
    if (!chain->gc) {
       /* FINISHME: Choose a better error. */
       result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
-      goto fail_alloc;
+      goto fail_register;
    }
 
    cookie = xcb_create_gc(chain->conn,
@@ -722,19 +886,21 @@ fail_init_images:
    for (uint32_t j = 0; j < image; j++)
       x11_image_finish(chain, pAllocator, &chain->images[j]);
 
-fail_alloc:
+fail_register:
+   xcb_unregister_for_special_event(chain->conn, chain->special_event);
+
    anv_free2(&device->alloc, pAllocator, chain);
 
    return result;
 }
 
 VkResult
-anv_x11_init_wsi(struct anv_instance *instance)
+anv_x11_init_wsi(struct anv_physical_device *device)
 {
    struct wsi_x11 *wsi;
    VkResult result;
 
-   wsi = anv_alloc(&instance->alloc, sizeof(*wsi), 8,
+   wsi = anv_alloc(&device->instance->alloc, sizeof(*wsi), 8,
                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
    if (!wsi) {
       result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
@@ -766,31 +932,33 @@ anv_x11_init_wsi(struct anv_instance *instance)
    wsi->base.get_present_modes = x11_surface_get_present_modes;
    wsi->base.create_swapchain = x11_surface_create_swapchain;
 
-   instance->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
+   device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
+   device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
 
    return VK_SUCCESS;
 
 fail_mutex:
    pthread_mutex_destroy(&wsi->mutex);
 fail_alloc:
-   anv_free(&instance->alloc, wsi);
+   anv_free(&device->instance->alloc, wsi);
 fail:
-   instance->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
+   device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
+   device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
 
    return result;
 }
 
 void
-anv_x11_finish_wsi(struct anv_instance *instance)
+anv_x11_finish_wsi(struct anv_physical_device *device)
 {
    struct wsi_x11 *wsi =
-      (struct wsi_x11 *)instance->wsi[VK_ICD_WSI_PLATFORM_XCB];
+      (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
 
    if (wsi) {
       _mesa_hash_table_destroy(wsi->connections, NULL);
 
       pthread_mutex_destroy(&wsi->mutex);
 
-      anv_free(&instance->alloc, wsi);
+      anv_free(&device->instance->alloc, wsi);
    }
 }