X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fvulkan%2Fwsi%2Fwsi_common_wayland.c;h=8290170c9ec38b232559b81125aea769f936864f;hb=61c3feb38dcae3feb1176426903ca8e0046edd12;hp=76917442fb1b9ed6cd548ba38172dc1f5ffb20d0;hpb=4fa0ca80eeeac813affcbb0129ed61f1534d8df0;p=mesa.git diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c index 76917442fb1..8290170c9ec 100644 --- a/src/vulkan/wsi/wsi_common_wayland.c +++ b/src/vulkan/wsi/wsi_common_wayland.c @@ -22,7 +22,6 @@ */ #include -#include #include #include @@ -32,20 +31,27 @@ #include #include +#include "vk_util.h" +#include "wsi_common_private.h" #include "wsi_common_wayland.h" +#include "wayland-drm-client-protocol.h" #include #include #define typed_memcpy(dest, src, count) ({ \ - static_assert(sizeof(*src) == sizeof(*dest), ""); \ + STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \ memcpy((dest), (src), (count) * sizeof(*(src))); \ }) struct wsi_wayland; struct wsi_wl_display { - struct wl_display * 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 wsi_wayland *wsi_wl; @@ -53,19 +59,18 @@ struct wsi_wl_display { struct u_vector formats; uint32_t capabilities; + + /* Only used for displays created by wsi_wl_display_create */ + uint32_t refcount; }; struct wsi_wayland { struct wsi_interface base; + struct wsi_device *wsi; + const VkAllocationCallbacks *alloc; VkPhysicalDevice physical_device; - - pthread_mutex_t mutex; - /* Hash table of wl_display -> wsi_wl_display mappings */ - struct hash_table * displays; - - const struct wsi_callbacks *cbs; }; static void @@ -80,7 +85,7 @@ wsi_wl_display_add_vk_format(struct wsi_wl_display *display, VkFormat format) /* Don't add formats that aren't renderable. */ VkFormatProperties props; - display->wsi_wl->cbs->get_phys_device_format_properties(display->wsi_wl->physical_device, + display->wsi_wl->wsi->GetPhysicalDeviceFormatProperties(display->wsi_wl->physical_device, format, &props); if (!(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) return; @@ -93,7 +98,6 @@ wsi_wl_display_add_vk_format(struct wsi_wl_display *display, VkFormat format) static void drm_handle_device(void *data, struct wl_drm *drm, const char *name) { - fprintf(stderr, "wl_drm.device(%s)\n", name); } static uint32_t @@ -144,6 +148,8 @@ 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) + return; switch (wl_format) { #if 0 @@ -245,105 +251,147 @@ static const struct wl_registry_listener registry_listener = { }; static void -wsi_wl_display_destroy(struct wsi_wayland *wsi, struct wsi_wl_display *display) +wsi_wl_display_finish(struct wsi_wl_display *display) { + assert(display->refcount == 0); + u_vector_finish(&display->formats); if (display->drm) wl_drm_destroy(display->drm); - vk_free(wsi->alloc, display); + if (display->wl_display_wrapper) + wl_proxy_wrapper_destroy(display->wl_display_wrapper); + if (display->queue) + wl_event_queue_destroy(display->queue); } -static struct wsi_wl_display * -wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display) +static VkResult +wsi_wl_display_init(struct wsi_wayland *wsi_wl, + struct wsi_wl_display *display, + struct wl_display *wl_display, + bool get_format_list) { - struct wsi_wl_display *display = - vk_alloc(wsi->alloc, sizeof(*display), 8, - VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); - if (!display) - return NULL; - + VkResult result = VK_SUCCESS; memset(display, 0, sizeof(*display)); - display->display = wl_display; - display->wsi_wl = wsi; + display->wsi_wl = wsi_wl; + display->wl_display = wl_display; + + if (get_format_list) { + if (!u_vector_init(&display->formats, sizeof(VkFormat), 8)) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; + goto fail; + } + } + + display->queue = wl_display_create_queue(wl_display); + if (!display->queue) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; + goto fail; + } - if (!u_vector_init(&display->formats, sizeof(VkFormat), 8)) + display->wl_display_wrapper = wl_proxy_create_wrapper(wl_display); + if (!display->wl_display_wrapper) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; goto fail; + } + + wl_proxy_set_queue((struct wl_proxy *) display->wl_display_wrapper, + display->queue); - struct wl_registry *registry = wl_display_get_registry(wl_display); - if (!registry) - return NULL; + struct wl_registry *registry = + wl_display_get_registry(display->wl_display_wrapper); + if (!registry) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; + goto fail; + } wl_registry_add_listener(registry, ®istry_listener, display); - /* Round-rip to get the wl_drm global */ - wl_display_roundtrip(wl_display); + /* Round-trip to get the wl_drm global */ + wl_display_roundtrip_queue(display->wl_display, display->queue); - if (!display->drm) - goto fail; + if (!display->drm) { + result = VK_ERROR_SURFACE_LOST_KHR; + goto fail_registry; + } - /* Round-rip to get wl_drm formats and capabilities */ - wl_display_roundtrip(wl_display); + /* Round-trip to get wl_drm formats and capabilities */ + wl_display_roundtrip_queue(display->wl_display, display->queue); /* We need prime support */ - if (!(display->capabilities & WL_DRM_CAPABILITY_PRIME)) - goto fail; + if (!(display->capabilities & WL_DRM_CAPABILITY_PRIME)) { + result = VK_ERROR_SURFACE_LOST_KHR; + goto fail_registry; + } /* We don't need this anymore */ wl_registry_destroy(registry); - return display; + display->refcount = 0; -fail: + return VK_SUCCESS; + +fail_registry: if (registry) wl_registry_destroy(registry); - wsi_wl_display_destroy(wsi, display); - return NULL; +fail: + wsi_wl_display_finish(display); + return result; } -static struct wsi_wl_display * -wsi_wl_get_display(struct wsi_device *wsi_device, - struct wl_display *wl_display) +static VkResult +wsi_wl_display_create(struct wsi_wayland *wsi, struct wl_display *wl_display, + struct wsi_wl_display **display_out) { - struct wsi_wayland *wsi = - (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; - - pthread_mutex_lock(&wsi->mutex); + struct wsi_wl_display *display = + vk_alloc(wsi->alloc, sizeof(*display), 8, + VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); + if (!display) + return VK_ERROR_OUT_OF_HOST_MEMORY; - struct hash_entry *entry = _mesa_hash_table_search(wsi->displays, - wl_display); - if (!entry) { - /* We're about to make a bunch of blocking calls. Let's drop the - * mutex for now so we don't block up too badly. - */ - pthread_mutex_unlock(&wsi->mutex); + VkResult result = wsi_wl_display_init(wsi, display, wl_display, true); + if (result != VK_SUCCESS) { + vk_free(wsi->alloc, display); + return result; + } - struct wsi_wl_display *display = wsi_wl_display_create(wsi, wl_display); - if (!display) - return NULL; + display->refcount++; + *display_out = display; - pthread_mutex_lock(&wsi->mutex); + return result; +} - entry = _mesa_hash_table_search(wsi->displays, wl_display); - if (entry) { - /* Oops, someone raced us to it */ - wsi_wl_display_destroy(wsi, display); - } else { - entry = _mesa_hash_table_insert(wsi->displays, wl_display, display); - } - } +static struct wsi_wl_display * +wsi_wl_display_ref(struct wsi_wl_display *display) +{ + display->refcount++; + return display; +} - pthread_mutex_unlock(&wsi->mutex); +static void +wsi_wl_display_unref(struct wsi_wl_display *display) +{ + if (display->refcount-- > 1) + return; - return entry->data; + struct wsi_wayland *wsi = display->wsi_wl; + wsi_wl_display_finish(display); + vk_free(wsi->alloc, display); } VkBool32 wsi_wl_get_presentation_support(struct wsi_device *wsi_device, struct wl_display *wl_display) { - return wsi_wl_get_display(wsi_device, wl_display) != NULL; + struct wsi_wayland *wsi = + (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; + + struct wsi_wl_display display; + int ret = wsi_wl_display_init(wsi, &display, wl_display, false); + wsi_wl_display_finish(&display); + + return ret == 0; } static VkResult @@ -351,6 +399,7 @@ wsi_wl_surface_get_support(VkIcdSurfaceBase *surface, struct wsi_device *wsi_device, const VkAllocationCallbacks *alloc, uint32_t queueFamilyIndex, + int local_fd, VkBool32* pSupported) { *pSupported = true; @@ -379,7 +428,8 @@ wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface, caps->currentExtent = (VkExtent2D) { -1, -1 }; caps->minImageExtent = (VkExtent2D) { 1, 1 }; - caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX }; + /* This is the maximum supported size on Intel */ + caps->maxImageExtent = (VkExtent2D) { 1 << 14, 1 << 14 }; caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; caps->maxImageArrayLayers = 1; @@ -397,6 +447,16 @@ wsi_wl_surface_get_capabilities(VkIcdSurfaceBase *surface, return VK_SUCCESS; } +static VkResult +wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase *surface, + const void *info_next, + VkSurfaceCapabilities2KHR* caps) +{ + assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR); + + return wsi_wl_surface_get_capabilities(surface, &caps->surfaceCapabilities); +} + static VkResult wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface, struct wsi_device *wsi_device, @@ -404,31 +464,56 @@ wsi_wl_surface_get_formats(VkIcdSurfaceBase *icd_surface, VkSurfaceFormatKHR* pSurfaceFormats) { VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface; - struct wsi_wl_display *display = - wsi_wl_get_display(wsi_device, surface->display); - if (!display) - return VK_ERROR_OUT_OF_HOST_MEMORY; + struct wsi_wayland *wsi = + (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; - uint32_t count = u_vector_length(&display->formats); + struct wsi_wl_display display; + if (wsi_wl_display_init(wsi, &display, surface->display, true)) + return VK_ERROR_SURFACE_LOST_KHR; - if (pSurfaceFormats == NULL) { - *pSurfaceFormatCount = count; - return VK_SUCCESS; + VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount); + + VkFormat *disp_fmt; + 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; + } } - assert(*pSurfaceFormatCount >= count); - *pSurfaceFormatCount = count; + wsi_wl_display_finish(&display); - VkFormat *f; - u_vector_foreach(f, &display->formats) { - *(pSurfaceFormats++) = (VkSurfaceFormatKHR) { - .format = *f, - /* TODO: We should get this from the compositor somehow */ - .colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR, - }; + return vk_outarray_status(&out); +} + +static VkResult +wsi_wl_surface_get_formats2(VkIcdSurfaceBase *icd_surface, + struct wsi_device *wsi_device, + const void *info_next, + uint32_t* pSurfaceFormatCount, + VkSurfaceFormat2KHR* pSurfaceFormats) +{ + VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface; + struct wsi_wayland *wsi = + (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; + + struct wsi_wl_display display; + if (wsi_wl_display_init(wsi, &display, surface->display, true)) + return VK_ERROR_SURFACE_LOST_KHR; + + VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount); + + VkFormat *disp_fmt; + 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; + } } - return VK_SUCCESS; + wsi_wl_display_finish(&display); + + return vk_outarray_status(&out); } static VkResult @@ -441,11 +526,13 @@ wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface, return VK_SUCCESS; } - assert(*pPresentModeCount >= ARRAY_SIZE(present_modes)); + *pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes)); typed_memcpy(pPresentModes, present_modes, *pPresentModeCount); - *pPresentModeCount = ARRAY_SIZE(present_modes); - return VK_SUCCESS; + if (*pPresentModeCount < ARRAY_SIZE(present_modes)) + return VK_INCOMPLETE; + else + return VK_SUCCESS; } VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator, @@ -463,14 +550,13 @@ VkResult wsi_create_wl_surface(const VkAllocationCallbacks *pAllocator, surface->display = pCreateInfo->display; surface->surface = pCreateInfo->surface; - *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base); + *pSurface = VkIcdSurfaceBase_to_handle(&surface->base); return VK_SUCCESS; } struct wsi_wl_image { - VkImage image; - VkDeviceMemory memory; + struct wsi_image base; struct wl_buffer * buffer; bool busy; }; @@ -478,9 +564,12 @@ struct wsi_wl_image { struct wsi_wl_swapchain { struct wsi_swapchain base; - struct wsi_wl_display * display; - struct wl_event_queue * queue; + struct wsi_wl_display *display; + struct wl_surface * surface; + uint32_t surface_version; + struct wl_drm * drm_wrapper; + struct wl_callback * frame; VkExtent2D extent; VkFormat vk_format; @@ -489,28 +578,15 @@ struct wsi_wl_swapchain { VkPresentModeKHR present_mode; bool fifo_ready; - uint32_t image_count; struct wsi_wl_image images[0]; }; -static VkResult -wsi_wl_swapchain_get_images(struct wsi_swapchain *wsi_chain, - uint32_t *pCount, VkImage *pSwapchainImages) +static struct wsi_image * +wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain *wsi_chain, + uint32_t image_index) { struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_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] = chain->images[i].image; - - *pCount = chain->image_count; - - return VK_SUCCESS; + return &chain->images[image_index].base; } static VkResult @@ -521,8 +597,8 @@ wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain, { struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain; - int ret = wl_display_dispatch_queue_pending(chain->display->display, - chain->queue); + 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. @@ -531,7 +607,7 @@ wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain, return VK_ERROR_OUT_OF_DATE_KHR; while (1) { - for (uint32_t i = 0; i < chain->image_count; i++) { + for (uint32_t i = 0; i < chain->base.image_count; i++) { if (!chain->images[i].busy) { /* We found a non-busy image */ *image_index = i; @@ -543,8 +619,8 @@ 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->display, - chain->queue); + int ret = wl_display_roundtrip_queue(chain->display->wl_display, + chain->display->queue); if (ret < 0) return VK_ERROR_OUT_OF_DATE_KHR; } @@ -555,6 +631,7 @@ frame_handle_done(void *data, struct wl_callback *callback, uint32_t serial) { struct wsi_wl_swapchain *chain = data; + chain->frame = NULL; chain->fifo_ready = true; wl_callback_destroy(callback); @@ -566,33 +643,45 @@ static const struct wl_callback_listener frame_listener = { static VkResult wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain, - uint32_t image_index) + uint32_t image_index, + const VkPresentRegionKHR *damage) { struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain; if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR) { while (!chain->fifo_ready) { - int ret = wl_display_dispatch_queue(chain->display->display, - chain->queue); + int ret = wl_display_dispatch_queue(chain->display->wl_display, + chain->display->queue); if (ret < 0) return VK_ERROR_OUT_OF_DATE_KHR; } } - assert(image_index < chain->image_count); + assert(image_index < chain->base.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->surface_version >= 4 && damage && + damage->pRectangles && damage->rectangleCount > 0) { + for (unsigned i = 0; i < damage->rectangleCount; i++) { + const VkRectLayerKHR *rect = &damage->pRectangles[i]; + assert(rect->layer == 0); + wl_surface_damage_buffer(chain->surface, + rect->offset.x, rect->offset.y, + rect->extent.width, rect->extent.height); + } + } else { + wl_surface_damage(chain->surface, 0, 0, INT32_MAX, INT32_MAX); + } if (chain->base.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->frame = wl_surface_frame(chain->surface); + wl_callback_add_listener(chain->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); + wl_display_flush(chain->display->wl_display); return VK_SUCCESS; } @@ -617,46 +706,34 @@ wsi_wl_image_init(struct wsi_wl_swapchain *chain, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks* pAllocator) { - 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, - &image->image, - &image->memory, - &size, - &offset, - &row_pitch, - &fd); + + result = wsi_create_native_image(&chain->base, pCreateInfo, &image->base); if (result != VK_SUCCESS) return result; - image->buffer = wl_drm_create_prime_buffer(chain->display->drm, - fd, /* name */ + /* Without passing modifiers, we can't have multi-plane RGB images. */ + assert(image->base.num_planes == 1); + + image->buffer = wl_drm_create_prime_buffer(chain->drm_wrapper, + image->base.fds[0], /* name */ chain->extent.width, chain->extent.height, chain->drm_format, - offset, - row_pitch, + image->base.offsets[0], + image->base.row_pitches[0], 0, 0, 0, 0 /* unused */); - wl_display_roundtrip(chain->display->display); - close(fd); + close(image->base.fds[0]); if (!image->buffer) goto fail_image; - wl_proxy_set_queue((struct wl_proxy *)image->buffer, chain->queue); wl_buffer_add_listener(image->buffer, &buffer_listener, image); return VK_SUCCESS; fail_image: - chain->base.image_fns->free_wsi_image(vk_device, pAllocator, - image->image, image->memory); + wsi_destroy_image(&chain->base, &image->base); return result; } @@ -667,13 +744,25 @@ wsi_wl_swapchain_destroy(struct wsi_swapchain *wsi_chain, { struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain; - for (uint32_t i = 0; i < chain->image_count; i++) { - if (chain->images[i].buffer) - chain->base.image_fns->free_wsi_image(chain->base.device, pAllocator, - chain->images[i].image, - chain->images[i].memory); + for (uint32_t i = 0; i < chain->base.image_count; i++) { + if (chain->images[i].buffer) { + wl_buffer_destroy(chain->images[i].buffer); + wsi_destroy_image(&chain->base, &chain->images[i].base); + } } + if (chain->frame) + wl_callback_destroy(chain->frame); + if (chain->surface) + wl_proxy_wrapper_destroy(chain->surface); + if (chain->drm_wrapper) + wl_proxy_wrapper_destroy(chain->drm_wrapper); + + if (chain->display) + wsi_wl_display_unref(chain->display); + + wsi_swapchain_finish(&chain->base); + vk_free(pAllocator, chain); return VK_SUCCESS; @@ -683,12 +772,14 @@ static VkResult wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, VkDevice device, struct wsi_device *wsi_device, + int local_fd, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, - const struct wsi_image_fns *image_fns, struct wsi_swapchain **swapchain_out) { VkIcdSurfaceWayland *surface = (VkIcdSurfaceWayland *)icd_surface; + struct wsi_wayland *wsi = + (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; struct wsi_wl_swapchain *chain; VkResult result; @@ -702,46 +793,68 @@ wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface, if (chain == NULL) return VK_ERROR_OUT_OF_HOST_MEMORY; + result = wsi_swapchain_init(wsi_device, &chain->base, device, + pCreateInfo, pAllocator); + if (result != VK_SUCCESS) { + vk_free(pAllocator, chain); + return result; + } + + /* Mark a bunch of stuff as NULL. This way we can just call + * destroy_swapchain for cleanup. + */ + for (uint32_t i = 0; i < num_images; i++) + chain->images[i].buffer = NULL; + chain->surface = NULL; + chain->drm_wrapper = NULL; + chain->frame = NULL; + bool alpha = pCreateInfo->compositeAlpha == VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR; - chain->base.device = device; chain->base.destroy = wsi_wl_swapchain_destroy; - chain->base.get_images = wsi_wl_swapchain_get_images; + 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.image_fns = image_fns; chain->base.present_mode = pCreateInfo->presentMode; - chain->surface = surface->surface; + chain->base.image_count = num_images; chain->extent = pCreateInfo->imageExtent; chain->vk_format = pCreateInfo->imageFormat; chain->drm_format = wl_drm_format_for_vk_format(chain->vk_format, alpha); - 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; + if (pCreateInfo->oldSwapchain) { + /* If we have an oldSwapchain parameter, copy the display struct over + * from the old one so we don't have to fully re-initialize it. + */ + struct wsi_wl_swapchain *old_chain = (void *)pCreateInfo->oldSwapchain; + chain->display = wsi_wl_display_ref(old_chain->display); + } else { + chain->display = NULL; + result = wsi_wl_display_create(wsi, surface->display, &chain->display); + if (result != VK_SUCCESS) + goto fail; + } - chain->display = wsi_wl_get_display(wsi_device, - surface->display); - if (!chain->display) { - result = VK_ERROR_INITIALIZATION_FAILED; + chain->surface = wl_proxy_create_wrapper(surface->surface); + if (!chain->surface) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; goto fail; } + wl_proxy_set_queue((struct wl_proxy *) chain->surface, + chain->display->queue); + chain->surface_version = wl_proxy_get_version((void *)surface->surface); - chain->queue = wl_display_create_queue(chain->display->display); - if (!chain->queue) { - result = VK_ERROR_INITIALIZATION_FAILED; + chain->drm_wrapper = wl_proxy_create_wrapper(chain->display->drm); + if (!chain->drm_wrapper) { + result = VK_ERROR_OUT_OF_HOST_MEMORY; goto fail; } + wl_proxy_set_queue((struct wl_proxy *) chain->drm_wrapper, + chain->display->queue); - for (uint32_t i = 0; i < chain->image_count; i++) { + chain->fifo_ready = true; + + for (uint32_t i = 0; i < chain->base.image_count; i++) { result = wsi_wl_image_init(chain, &chain->images[i], pCreateInfo, pAllocator); if (result != VK_SUCCESS) @@ -762,8 +875,7 @@ fail: VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device, const VkAllocationCallbacks *alloc, - VkPhysicalDevice physical_device, - const struct wsi_callbacks *cbs) + VkPhysicalDevice physical_device) { struct wsi_wayland *wsi; VkResult result; @@ -777,29 +889,13 @@ wsi_wl_init_wsi(struct wsi_device *wsi_device, wsi->physical_device = physical_device; wsi->alloc = alloc; - wsi->cbs = cbs; - int ret = pthread_mutex_init(&wsi->mutex, NULL); - if (ret != 0) { - if (ret == ENOMEM) { - result = VK_ERROR_OUT_OF_HOST_MEMORY; - } else { - /* FINISHME: Choose a better error. */ - result = 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_OUT_OF_HOST_MEMORY; - goto fail_mutex; - } + wsi->wsi = wsi_device; wsi->base.get_support = wsi_wl_surface_get_support; wsi->base.get_capabilities = wsi_wl_surface_get_capabilities; + wsi->base.get_capabilities2 = wsi_wl_surface_get_capabilities2; wsi->base.get_formats = wsi_wl_surface_get_formats; + wsi->base.get_formats2 = wsi_wl_surface_get_formats2; wsi->base.get_present_modes = wsi_wl_surface_get_present_modes; wsi->base.create_swapchain = wsi_wl_surface_create_swapchain; @@ -807,11 +903,6 @@ wsi_wl_init_wsi(struct wsi_device *wsi_device, return VK_SUCCESS; -fail_mutex: - pthread_mutex_destroy(&wsi->mutex); - -fail_alloc: - vk_free(alloc, wsi); fail: wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND] = NULL; @@ -824,16 +915,8 @@ wsi_wl_finish_wsi(struct wsi_device *wsi_device, { struct wsi_wayland *wsi = (struct wsi_wayland *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_WAYLAND]; + if (!wsi) + return; - if (wsi) { - struct hash_entry *entry; - hash_table_foreach(wsi->displays, entry) - wsi_wl_display_destroy(wsi, entry->data); - - _mesa_hash_table_destroy(wsi->displays, NULL); - - pthread_mutex_destroy(&wsi->mutex); - - vk_free(alloc, wsi); - } + vk_free(alloc, wsi); }