[Mesa-dev] [RFC v3 09/23] vulkan/wsi: Add wsi_image_base structure

Louis-Francis Ratté-Boulianne lfrb at collabora.com
Thu Sep 28 07:54:49 UTC 2017


From: Daniel Stone <daniels at collabora.com>

This is used to hold information about the allocated image, rather than
an ever-growing function argument list.

Signed-off-by: Daniel Stone <daniels at collabora.com>
---
 src/amd/vulkan/radv_wsi.c           | 31 ++++++++++------------
 src/intel/vulkan/anv_wsi.c          | 25 +++++++-----------
 src/vulkan/wsi/wsi_common.h         | 19 ++++++++------
 src/vulkan/wsi/wsi_common_wayland.c | 31 +++++++---------------
 src/vulkan/wsi/wsi_common_x11.c     | 52 ++++++++++++++-----------------------
 5 files changed, 65 insertions(+), 93 deletions(-)

diff --git a/src/amd/vulkan/radv_wsi.c b/src/amd/vulkan/radv_wsi.c
index c9d4bbce8b..a790ea979b 100644
--- a/src/amd/vulkan/radv_wsi.c
+++ b/src/amd/vulkan/radv_wsi.c
@@ -144,11 +144,7 @@ radv_wsi_image_create(VkDevice device_h,
 		      const VkAllocationCallbacks* pAllocator,
 		      bool needs_linear_copy,
 		      bool linear,
-		      VkImage *image_p,
-		      VkDeviceMemory *memory_p,
-		      uint32_t *size,
-		      uint32_t *offset,
-		      uint32_t *row_pitch, int *fd_p)
+		      struct wsi_image_base *wsi_image)
 {
 	VkResult result = VK_SUCCESS;
 	struct radeon_surf *surface;
@@ -216,20 +212,22 @@ radv_wsi_image_create(VkDevice device_h,
 		RADV_FROM_HANDLE(radv_device_memory, memory, memory_h);
 		if (!radv_get_memory_fd(device, memory, &fd))
 			goto fail_alloc_memory;
-		*fd_p = fd;
+		wsi_image->fd = fd;
 	}
 
 	surface = &image->surface;
 
-	*image_p = image_h;
-	*memory_p = memory_h;
-	*size = image->size;
-	*offset = image->offset;
-
+	wsi_image->image = image_h;
+	wsi_image->memory = memory_h;
+	wsi_image->size = image->size;
+	wsi_image->offset = image->offset;
 	if (device->physical_device->rad_info.chip_class >= GFX9)
-		*row_pitch = surface->u.gfx9.surf_pitch * surface->bpe;
+		wsi_image->row_pitch =
+			surface->u.gfx9.surf_pitch * surface->bpe;
 	else
-		*row_pitch = surface->u.legacy.level[0].nblk_x * surface->bpe;
+		wsi_image->row_pitch =
+			surface->u.legacy.level[0].nblk_x * surface->bpe;
+
 	return VK_SUCCESS;
  fail_alloc_memory:
 	radv_FreeMemory(device_h, memory_h, pAllocator);
@@ -243,12 +241,11 @@ fail_create_image:
 static void
 radv_wsi_image_free(VkDevice device,
 		    const VkAllocationCallbacks* pAllocator,
-		    VkImage image_h,
-		    VkDeviceMemory memory_h)
+		    struct wsi_image_base *wsi_image)
 {
-	radv_DestroyImage(device, image_h, pAllocator);
+	radv_DestroyImage(device, wsi_image->image, pAllocator);
 
-	radv_FreeMemory(device, memory_h, pAllocator);
+	radv_FreeMemory(device, wsi_image->memory, pAllocator);
 }
 
 static const struct wsi_image_fns radv_wsi_image_fns = {
diff --git a/src/intel/vulkan/anv_wsi.c b/src/intel/vulkan/anv_wsi.c
index c3e5dc1870..d9481d1f1c 100644
--- a/src/intel/vulkan/anv_wsi.c
+++ b/src/intel/vulkan/anv_wsi.c
@@ -174,11 +174,7 @@ anv_wsi_image_create(VkDevice device_h,
                      const VkAllocationCallbacks* pAllocator,
                      bool different_gpu,
                      bool linear,
-                     VkImage *image_p,
-                     VkDeviceMemory *memory_p,
-                     uint32_t *size,
-                     uint32_t *offset,
-                     uint32_t *row_pitch, int *fd_p)
+                     struct wsi_image_base *wsi_image)
 {
    struct anv_device *device = anv_device_from_handle(device_h);
    VkImage image_h;
@@ -243,7 +239,6 @@ anv_wsi_image_create(VkDevice device_h,
    struct anv_surface *surface = &image->color_surface;
    assert(surface->isl.tiling == ISL_TILING_X);
 
-   *row_pitch = surface->isl.row_pitch;
    int ret = anv_gem_set_tiling(device, memory->bo->gem_handle,
                                 surface->isl.row_pitch, I915_TILING_X);
    if (ret) {
@@ -263,11 +258,12 @@ anv_wsi_image_create(VkDevice device_h,
       goto fail_alloc_memory;
    }
 
-   *image_p = image_h;
-   *memory_p = memory_h;
-   *fd_p = fd;
-   *size = image->size;
-   *offset = image->offset;
+   wsi_image->image = image_h;
+   wsi_image->memory = memory_h;
+   wsi_image->fd = fd;
+   wsi_image->size = image->size;
+   wsi_image->offset = image->offset;
+   wsi_image->row_pitch = surface->isl.row_pitch;
    return VK_SUCCESS;
 fail_alloc_memory:
    anv_FreeMemory(device_h, memory_h, pAllocator);
@@ -280,12 +276,11 @@ fail_create_image:
 static void
 anv_wsi_image_free(VkDevice device,
                    const VkAllocationCallbacks* pAllocator,
-                   VkImage image_h,
-                   VkDeviceMemory memory_h)
+                   struct wsi_image_base *wsi_image)
 {
-   anv_DestroyImage(device, image_h, pAllocator);
+   anv_DestroyImage(device, wsi_image->image, pAllocator);
 
-   anv_FreeMemory(device, memory_h, pAllocator);
+   anv_FreeMemory(device, wsi_image->memory, pAllocator);
 }
 
 static const struct wsi_image_fns anv_wsi_image_fns = {
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h
index 8166b7dd34..2a9092479d 100644
--- a/src/vulkan/wsi/wsi_common.h
+++ b/src/vulkan/wsi/wsi_common.h
@@ -30,6 +30,15 @@
 #include <vulkan/vulkan.h>
 #include <vulkan/vk_icd.h>
 
+struct wsi_image_base {
+   VkImage image;
+   VkDeviceMemory memory;
+   uint32_t size;
+   uint32_t offset;
+   uint32_t row_pitch;
+   int fd;
+};
+
 struct wsi_device;
 struct wsi_image_fns {
    VkResult (*create_wsi_image)(VkDevice device_h,
@@ -37,16 +46,10 @@ struct wsi_image_fns {
                                 const VkAllocationCallbacks *pAllocator,
                                 bool needs_linear_copy,
                                 bool linear,
-                                VkImage *image_p,
-                                VkDeviceMemory *memory_p,
-                                uint32_t *size_p,
-                                uint32_t *offset_p,
-                                uint32_t *row_pitch_p,
-                                int *fd_p);
+                                struct wsi_image_base *image_p);
    void (*free_wsi_image)(VkDevice device,
                           const VkAllocationCallbacks *pAllocator,
-                          VkImage image_h,
-                          VkDeviceMemory memory_h);
+                          struct wsi_image_base *image);
 };
 
 struct wsi_swapchain {
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index 4c94cd60a5..495e7068b4 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -556,8 +556,7 @@ VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator,
 }
 
 struct wsi_wl_image {
-   VkImage image;
-   VkDeviceMemory memory;
+   struct wsi_image_base                        base;
    struct wl_buffer *                           buffer;
    bool                                         busy;
 };
@@ -603,7 +602,7 @@ wsi_wl_swapchain_get_images(struct wsi_swapchain *wsi_chain,
    }
 
    for (uint32_t i = 0; i < ret_count; i++)
-      pSwapchainImages[i] = chain->images[i].image;
+      pSwapchainImages[i] = chain->images[i].base.image;
 
    return result;
 }
@@ -727,33 +726,25 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
 {
    VkDevice vk_device = chain->base.device;
    VkResult result;
-   int fd;
-   uint32_t size;
-   uint32_t row_pitch;
-   uint32_t offset;
+
    result = chain->base.image_fns->create_wsi_image(vk_device,
                                                     pCreateInfo,
                                                     pAllocator,
                                                     false,
                                                     false,
-                                                    &image->image,
-                                                    &image->memory,
-                                                    &size,
-                                                    &offset,
-                                                    &row_pitch,
-                                                    &fd);
+                                                    &image->base);
    if (result != VK_SUCCESS)
       return result;
 
    image->buffer = wl_drm_create_prime_buffer(chain->drm_wrapper,
-                                              fd, /* name */
+                                              image->base.fd, /* name */
                                               chain->extent.width,
                                               chain->extent.height,
                                               chain->drm_format,
-                                              offset,
-                                              row_pitch,
+                                              image->base.offset,
+                                              image->base.row_pitch,
                                               0, 0, 0, 0 /* unused */);
-   close(fd);
+   close(image->base.fd);
 
    if (!image->buffer)
       goto fail_image;
@@ -763,8 +754,7 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain,
    return VK_SUCCESS;
 
 fail_image:
-   chain->base.image_fns->free_wsi_image(vk_device, pAllocator,
-                                         image->image, image->memory);
+   chain->base.image_fns->free_wsi_image(vk_device, pAllocator, &image->base);
 
    return result;
 }
@@ -779,8 +769,7 @@ wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain,
       if (chain->images[i].buffer) {
          wl_buffer_destroy(chain->images[i].buffer);
          chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
-                                               chain->images[i].image,
-                                               chain->images[i].memory);
+                                               &chain->images[i].base);
       }
    }
 
diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c
index ecdaf91434..580a200d73 100644
--- a/src/vulkan/wsi/wsi_common_x11.c
+++ b/src/vulkan/wsi/wsi_common_x11.c
@@ -615,10 +615,8 @@ VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
 }
 
 struct x11_image {
-   VkImage image;
-   VkImage linear_image; // for prime
-   VkDeviceMemory memory;
-   VkDeviceMemory linear_memory; // for prime
+   struct wsi_image_base                     base;
+   struct wsi_image_base                     linear_base;
    xcb_pixmap_t                              pixmap;
    bool                                      busy;
    struct xshmfence *                        shm_fence;
@@ -670,7 +668,7 @@ x11_get_images(struct wsi_swapchain *anv_chain,
    }
 
    for (uint32_t i = 0; i < ret_count; i++)
-      pSwapchainImages[i] = chain->images[i].image;
+      pSwapchainImages[i] = chain->images[i].base.image;
 
    return result;
 }
@@ -680,8 +678,8 @@ x11_get_image_and_linear(struct wsi_swapchain *drv_chain,
                          int imageIndex, VkImage *image, VkImage *linear_image)
 {
    struct x11_swapchain *chain = (struct x11_swapchain *)drv_chain;
-   *image = chain->images[imageIndex].image;
-   *linear_image = chain->images[imageIndex].linear_image;
+   *image = chain->images[imageIndex].base.image;
+   *linear_image = chain->images[imageIndex].linear_base.image;
 }
 
 static VkResult
@@ -959,23 +957,14 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
 {
    xcb_void_cookie_t cookie;
    VkResult result;
-   uint32_t row_pitch;
-   uint32_t offset;
    uint32_t bpp = 32;
-   int fd;
-   uint32_t size;
 
    result = chain->base.image_fns->create_wsi_image(device_h,
                                                     pCreateInfo,
                                                     pAllocator,
                                                     chain->base.needs_linear_copy,
                                                     false,
-                                                    &image->image,
-                                                    &image->memory,
-                                                    &size,
-                                                    &offset,
-                                                    &row_pitch,
-                                                    &fd);
+                                                    &image->base);
    if (result != VK_SUCCESS)
       return result;
 
@@ -985,31 +974,31 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
                                                        pAllocator,
                                                        chain->base.needs_linear_copy,
                                                        true,
-                                                       &image->linear_image,
-                                                       &image->linear_memory,
-                                                       &size,
-                                                       &offset,
-                                                       &row_pitch,
-                                                       &fd);
+                                                       &image->linear_base);
+
       if (result != VK_SUCCESS) {
          chain->base.image_fns->free_wsi_image(device_h, pAllocator,
-                                               image->image, image->memory);
+                                               &image->base);
          return result;
       }
    }
 
    image->pixmap = xcb_generate_id(chain->conn);
 
+   struct wsi_image_base *image_ws =
+      chain->base.needs_linear_copy ? &image->linear_base : &image->base;
    cookie =
       xcb_dri3_pixmap_from_buffer_checked(chain->conn,
                                           image->pixmap,
                                           chain->window,
-                                          size,
+                                          image_ws->size,
                                           pCreateInfo->imageExtent.width,
                                           pCreateInfo->imageExtent.height,
-                                          row_pitch,
-                                          chain->depth, bpp, fd);
+                                          image_ws->row_pitch,
+                                          chain->depth, bpp,
+                                          image_ws->fd);
    xcb_discard_reply(chain->conn, cookie.sequence);
+   image_ws->fd = -1; /* XCB has now taken ownership of the FD */
 
    int fence_fd = xshmfence_alloc_shm();
    if (fence_fd < 0)
@@ -1040,10 +1029,9 @@ fail_pixmap:
 
    if (chain->base.needs_linear_copy) {
       chain->base.image_fns->free_wsi_image(device_h, pAllocator,
-                                            image->linear_image, image->linear_memory);
+                                            &image->linear_base);
    }
-   chain->base.image_fns->free_wsi_image(device_h, pAllocator,
-                                         image->image, image->memory);
+   chain->base.image_fns->free_wsi_image(device_h, pAllocator, &image->base);
 
    return result;
 }
@@ -1064,10 +1052,10 @@ x11_image_finish(struct x11_swapchain *chain,
 
    if (chain->base.needs_linear_copy) {
       chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
-                                            image->linear_image, image->linear_memory);
+                                            &image->linear_base);
    }
    chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator,
-                                        image->image, image->memory);
+                                         &image->base);
 }
 
 static VkResult
-- 
2.13.0



More information about the mesa-dev mailing list