- RADV_FROM_HANDLE(radv_instance, instance, _instance);
-
- assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR);
-
- VkIcdSurfaceWayland *surface;
-
- surface = vk_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_WAYLAND;
- surface->display = pCreateInfo->display;
- surface->surface = pCreateInfo->surface;
-
- *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
-
- return VK_SUCCESS;
-}
-
-struct wsi_wl_image {
- struct radv_image * image;
- struct radv_device_memory * memory;
- struct wl_buffer * buffer;
- bool busy;
-};
-
-struct wsi_wl_swapchain {
- struct radv_swapchain base;
-
- struct wsi_wl_display * display;
- struct wl_event_queue * queue;
- struct wl_surface * surface;
-
- VkExtent2D extent;
- VkFormat vk_format;
- uint32_t drm_format;
-
- VkPresentModeKHR present_mode;
- bool fifo_ready;
-
- uint32_t image_count;
- struct wsi_wl_image images[0];
-};
-
-static VkResult
-wsi_wl_swapchain_get_images(struct radv_swapchain *radv_chain,
- uint32_t *pCount, VkImage *pSwapchainImages)
-{
- struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)radv_chain;
-
- if (pSwapchainImages == NULL) {
- *pCount = chain->image_count;
- return VK_SUCCESS;
- }
-
- assert(chain->image_count <= *pCount);
- for (uint32_t i = 0; i < chain->image_count; i++)
- pSwapchainImages[i] = radv_image_to_handle(chain->images[i].image);
-
- *pCount = chain->image_count;
-
- return VK_SUCCESS;
-}
-
-static VkResult
-wsi_wl_swapchain_acquire_next_image(struct radv_swapchain *radv_chain,
- uint64_t timeout,
- VkSemaphore semaphore,
- uint32_t *image_index)
-{
- struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)radv_chain;
-
- int ret = wl_display_dispatch_queue_pending(chain->display->display,
- chain->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(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 */
- *image_index = i;
- chain->images[i].busy = true;
- return VK_SUCCESS;
- }
- }
-
- /* 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->display,
- chain->queue);
- if (ret < 0)
- return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
- }
-}
-
-static void
-frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial)
-{
- struct wsi_wl_swapchain *chain = data;
-
- chain->fifo_ready = true;
-
- wl_callback_destroy(callback);
-}
-
-static const struct wl_callback_listener frame_listener = {
- frame_handle_done,
-};
-
-static VkResult
-wsi_wl_swapchain_queue_present(struct radv_swapchain *radv_chain,
- struct radv_queue *queue,
- uint32_t image_index)
-{
- struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)radv_chain;
-
- if (chain->present_mode == VK_PRESENT_MODE_FIFO_KHR) {
- while (!chain->fifo_ready) {
- int ret = wl_display_dispatch_queue(chain->display->display,
- chain->queue);
- if (ret < 0)
- return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
- }
- }
-
- assert(image_index < chain->image_count);
- wl_surface_attach(chain->surface, chain->images[image_index].buffer, 0, 0);
- wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX);
-
- if (chain->present_mode == VK_PRESENT_MODE_FIFO_KHR) {
- struct wl_callback *frame = wl_surface_frame(chain->surface);
- wl_proxy_set_queue((struct wl_proxy *)frame, chain->queue);
- wl_callback_add_listener(frame, &frame_listener, chain);
- chain->fifo_ready = false;
- }
-
- chain->images[image_index].busy = true;
- wl_surface_commit(chain->surface);
- wl_display_flush(chain->display->display);
-
- return VK_SUCCESS;
-}
-
-static void
-wsi_wl_image_finish(struct wsi_wl_swapchain *chain, struct wsi_wl_image *image,
- const VkAllocationCallbacks* pAllocator)
-{
- VkDevice vk_device = radv_device_to_handle(chain->base.device);
- radv_FreeMemory(vk_device, radv_device_memory_to_handle(image->memory),
- pAllocator);
- radv_DestroyImage(vk_device, radv_image_to_handle(image->image),
- pAllocator);
-}
-
-static void
-buffer_handle_release(void *data, struct wl_buffer *buffer)
-{
- struct wsi_wl_image *image = data;
-
- assert(image->buffer == buffer);
-
- image->busy = false;
-}
-
-static const struct wl_buffer_listener buffer_listener = {
- buffer_handle_release,
-};
-
-static VkResult
-wsi_wl_image_init(struct wsi_wl_swapchain *chain,
- struct wsi_wl_image *image,
- const VkSwapchainCreateInfoKHR *pCreateInfo,
- const VkAllocationCallbacks* pAllocator)
-{
- VkDevice vk_device = radv_device_to_handle(chain->base.device);
- VkResult result;
- bool bret;
- VkImage vk_image;
- struct radeon_surf *surface;
- int fd;
- result = radv_image_create(vk_device,
- &(struct radv_image_create_info) {
- .vk_info =
- &(VkImageCreateInfo) {
- .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
- .imageType = VK_IMAGE_TYPE_2D,
- .format = chain->vk_format,
- .extent = {
- .width = chain->extent.width,
- .height = chain->extent.height,
- .depth = 1
- },
- .mipLevels = 1,
- .arrayLayers = 1,
- .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 |
- pCreateInfo->imageUsage),
- .flags = 0,
- },
- .scanout = true},
- pAllocator,
- &vk_image);
-
- if (result != VK_SUCCESS)
- return result;
-
- image->image = radv_image_from_handle(vk_image);
- assert(vk_format_is_color(image->image->vk_format));
-
- VkDeviceMemory vk_memory;
- result = radv_AllocateMemory(vk_device,
- &(VkMemoryAllocateInfo) {
- .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
- .allocationSize = image->image->size,
- .memoryTypeIndex = 0,
- },
- pAllocator,
- &vk_memory);
-
- if (result != VK_SUCCESS)
- goto fail_image;
-
- image->memory = radv_device_memory_from_handle(vk_memory);
-
- result = radv_BindImageMemory(vk_device, vk_image, vk_memory, 0);
-
- if (result != VK_SUCCESS)
- goto fail_mem;
-
- bret = chain->base.device->ws->buffer_get_fd(chain->base.device->ws,
- image->memory->bo, &fd);
- if (bret == false)
- goto fail_mem;
-
- {
- struct radeon_bo_metadata metadata;
- radv_init_metadata(chain->base.device, image->image, &metadata);
- chain->base.device->ws->buffer_set_metadata(image->memory->bo, &metadata);
- }
- surface = &image->image->surface;
-
- image->buffer = wl_drm_create_prime_buffer(chain->display->drm,
- fd, /* name */
- chain->extent.width,
- chain->extent.height,
- chain->drm_format,
- surface->level[0].offset,
- surface->level[0].pitch_bytes,
- 0, 0, 0, 0 /* unused */);
- wl_display_roundtrip(chain->display->display);
- close(fd);
-
- wl_proxy_set_queue((struct wl_proxy *)image->buffer, chain->queue);
- wl_buffer_add_listener(image->buffer, &buffer_listener, image);
-
- return VK_SUCCESS;
-
-fail_mem:
- radv_FreeMemory(vk_device, vk_memory, pAllocator);
-fail_image:
- radv_DestroyImage(vk_device, vk_image, pAllocator);
-
- return result;
-}
-
-static VkResult
-wsi_wl_swapchain_destroy(struct radv_swapchain *radv_chain,
- const VkAllocationCallbacks *pAllocator)
-{
- struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)radv_chain;
-
- for (uint32_t i = 0; i < chain->image_count; i++) {
- if (chain->images[i].buffer)
- wsi_wl_image_finish(chain, &chain->images[i], pAllocator);
- }
-
- vk_free2(&chain->base.device->alloc, pAllocator, chain);
-
- return VK_SUCCESS;
-}
-
-static VkResult
-wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
- struct radv_device *device,
- const VkSwapchainCreateInfoKHR* pCreateInfo,
- const VkAllocationCallbacks* pAllocator,
- struct radv_swapchain **swapchain_out)
-{
- VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface;
- struct wsi_wl_swapchain *chain;
- VkResult result;
-
- assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
-
- int num_images = pCreateInfo->minImageCount;
-
- assert(num_images >= MIN_NUM_IMAGES);
-
- /* 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 = vk_alloc2(&device->alloc, pAllocator, size, 8,
- VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
- if (chain == NULL)
- return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
-
- chain->base.device = device;
- chain->base.destroy = wsi_wl_swapchain_destroy;
- chain->base.get_images = wsi_wl_swapchain_get_images;
- chain->base.acquire_next_image = wsi_wl_swapchain_acquire_next_image;
- chain->base.queue_present = wsi_wl_swapchain_queue_present;
-
- chain->surface = surface->surface;
- chain->extent = pCreateInfo->imageExtent;
- chain->vk_format = pCreateInfo->imageFormat;
- chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, false);
-
- chain->present_mode = pCreateInfo->presentMode;
- chain->fifo_ready = true;
-
- chain->image_count = num_images;
-
- /* Mark a bunch of stuff as NULL. This way we can just call
- * destroy_swapchain for cleanup.
- */
- for (uint32_t i = 0; i < chain->image_count; i++)
- chain->images[i].buffer = NULL;
- chain->queue = NULL;
-
- chain->display = wsi_wl_get_display(&device->instance->physicalDevice,
- surface->display);
- if (!chain->display) {
- result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
- goto fail;
- }
-
- chain->queue = wl_display_create_queue(chain->display->display);
- if (!chain->queue) {
- result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
- goto fail;
- }
-
- for (uint32_t i = 0; i < chain->image_count; i++) {
- result = wsi_wl_image_init(chain, &chain->images[i],
- pCreateInfo, pAllocator);
- if (result != VK_SUCCESS)
- goto fail;
- chain->images[i].busy = false;
- }
-
- *swapchain_out = &chain->base;
-
- return VK_SUCCESS;
-
-fail:
- wsi_wl_swapchain_destroy(&chain->base, pAllocator);
-
- return result;
-}
-
-VkResult
-radv_wl_init_wsi(struct radv_physical_device *device)
-{
- struct wsi_wayland *wsi;
- VkResult result;
-
- wsi = vk_alloc(&device->instance->alloc, sizeof(*wsi), 8,
- VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
- if (!wsi) {
- result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
- goto fail;
- }
-
- wsi->physical_device = device;
-
- int ret = pthread_mutex_init(&wsi->mutex, NULL);
- if (ret != 0) {
- if (ret == ENOMEM) {
- result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
- } else {
- /* FINISHME: Choose a better error. */
- result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
- }
-
- goto fail_alloc;
- }
-
- wsi->displays = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
- _mesa_key_pointer_equal);
- if (!wsi->displays) {
- result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
- goto fail_mutex;
- }
-
- wsi->base.get_support = wsi_wl_surface_get_support;
- wsi->base.get_capabilities = wsi_wl_surface_get_capabilities;
- wsi->base.get_formats = wsi_wl_surface_get_formats;
- wsi->base.get_present_modes = wsi_wl_surface_get_present_modes;
- wsi->base.create_swapchain = wsi_wl_surface_create_swapchain;
-
- device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = &wsi->base;
-
- return VK_SUCCESS;
-
-fail_mutex:
- pthread_mutex_destroy(&wsi->mutex);
-
-fail_alloc:
- vk_free(&device->instance->alloc, wsi);
-fail:
- device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL;
-
- return result;
-}
-
-void
-radv_wl_finish_wsi(struct radv_physical_device *device)
-{
- struct wsi_wayland *wsi =
- (struct wsi_wayland *)device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND];
-
- if (wsi) {
- _mesa_hash_table_destroy(wsi->displays, NULL);