From a76e58aa724a1848f5407dfbf6aa5bc2b2173f38 Mon Sep 17 00:00:00 2001 From: vii Date: Sun, 29 Nov 2026 10:36:37 -0600 Subject: [PATCH] fix(vulkan): destroy old swapchains in the background once the driver is slow After an inline vkDestroySwapchainKHR takes 1 s or more, later destroys go to the teardown worker so resizes stop freezing the render thread. --- CHANGELOG.md | 3 + .../mocktail/graphics/swapchain_retirement.h | 60 +++++++ src/graphics/bionic_vulkan_loader_adapter.cc | 119 +++++++++++++- src/graphics/swapchain_retirement.cc | 81 ++++++++++ stubs/CMakeLists.txt | 1 + tests/CMakeLists.txt | 12 ++ tests/swapchain_retirement_test.cc | 153 ++++++++++++++++++ 7 files changed, 424 insertions(+), 5 deletions(-) create mode 100644 include/mocktail/graphics/swapchain_retirement.h create mode 100644 src/graphics/swapchain_retirement.cc create mode 100644 tests/swapchain_retirement_test.cc diff --git a/CHANGELOG.md b/CHANGELOG.md index e5fa8193..113ec9f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ Release notes on GitHub are taken from the matching section here. those requests with "BrowserTrackerID is missing or invalid"; Nightcap now sends its browser tracker. - Games that size their UI by screen size, such as Racket Rivals, no longer show a tiny phone layout. Nightcap reported a 9-inch screen; it now reports the monitor's size at 96 dpi. +- With Vulkan on NVIDIA, resizing the window no longer freezes for about 22 seconds each time on setups + where the driver is slow to destroy the old swapchain. After the first slow destroy, Nightcap destroys + old swapchains in the background. - On FreeBSD, Roblox downloads no longer fail with "error adding trust anchors" in a Fedora userspace. - On FreeBSD, sound plays through `/dev/dsp` without an `asound.conf`, once `alsa-plugins-oss` is installed in the Linux userspace. An existing `~/.asoundrc` or `/etc/asound.conf` is still used. diff --git a/include/mocktail/graphics/swapchain_retirement.h b/include/mocktail/graphics/swapchain_retirement.h new file mode 100644 index 00000000..4b9d35c7 --- /dev/null +++ b/include/mocktail/graphics/swapchain_retirement.h @@ -0,0 +1,60 @@ +#ifndef MOCKTAIL_GRAPHICS_SWAPCHAIN_RETIREMENT_H_ +#define MOCKTAIL_GRAPHICS_SWAPCHAIN_RETIREMENT_H_ + +#include + +#include +#include +#include +#include + +namespace mocktail::graphics { + +struct HeldSwapchain { + VkDevice device = VK_NULL_HANDLE; + VkSwapchainKHR swapchain = VK_NULL_HANDLE; + VkSurfaceKHR surface = VK_NULL_HANDLE; + // A copy: the game's callbacks struct need not outlive its destroy call. + bool has_allocator = false; + VkAllocationCallbacks allocator{}; + const VkAllocationCallbacks* Allocator() const { + return has_allocator ? &allocator : nullptr; + } + explicit operator bool() const { return swapchain != VK_NULL_HANDLE; } +}; + +// Decides when a destroyed swapchain may go to the background worker. NVIDIA's +// X11 driver can block ~22 s in vkDestroySwapchainKHR. A swapchain the game +// destroys while it still owns the window is held and passed as oldSwapchain +// to the next create, so the window is free while the worker destroys it. +// Off until Enable(), so drivers that destroy promptly keep the old path. +class SwapchainRetirement final { + public: + void Enable() { enabled_.store(true, std::memory_order_relaxed); } + bool Enabled() const { return enabled_.load(std::memory_order_relaxed); } + // Drops the bookkeeping for a swapchain destroyed in place. + void Forget(VkSwapchainKHR swapchain); + void Created(VkSwapchainKHR swapchain, VkSurfaceKHR surface, + VkSwapchainKHR old_swapchain); + // A failed create still retires its oldSwapchain. + void Retired(VkSwapchainKHR old_swapchain); + // Returns what to destroy now; a swapchain still owning the window is held. + HeldSwapchain Destroyed(VkDevice device, VkSwapchainKHR swapchain, + const VkAllocationCallbacks* allocator); + HeldSwapchain TakeForCreate(VkDevice device, VkSurfaceKHR surface, + VkSwapchainKHR* old_swapchain); + HeldSwapchain ReleaseIfSurface(VkSurfaceKHR surface); + HeldSwapchain ReleaseIfDevice(VkDevice device); + HeldSwapchain Release(); + + private: + std::mutex mutex_; + std::unordered_map surfaces_; + std::unordered_set retired_; + HeldSwapchain held_; + std::atomic enabled_{false}; +}; + +} // namespace mocktail::graphics + +#endif diff --git a/src/graphics/bionic_vulkan_loader_adapter.cc b/src/graphics/bionic_vulkan_loader_adapter.cc index 7dfa556a..768fa43f 100644 --- a/src/graphics/bionic_vulkan_loader_adapter.cc +++ b/src/graphics/bionic_vulkan_loader_adapter.cc @@ -22,6 +22,7 @@ #include "mocktail/graphics/android_vulkan_wsi_adapter.h" #include "mocktail/graphics/chrome_trace_writer.h" #include "mocktail/graphics/deferred_teardown.h" +#include "mocktail/graphics/swapchain_retirement.h" #include "mocktail/graphics/vulkan_object_reuse.h" #include "mocktail/graphics/vulkan_driver_pin.h" #include "mocktail/graphics/present_mode_policy.h" @@ -244,6 +245,11 @@ mocktail::graphics::DeferredTeardown& Teardown() { return teardown; } +mocktail::graphics::SwapchainRetirement& Retirement() { + static mocktail::graphics::SwapchainRetirement retirement; + return retirement; +} + void NoteSwapchainResult(VkResult result, const char* call) { if (result != VK_ERROR_OUT_OF_DATE_KHR && result != VK_ERROR_SURFACE_LOST_KHR) { @@ -1694,6 +1700,31 @@ void DestroyStaleKeptDevice(VkDevice device, PFN_vkDestroyDevice host_destroy, }); } +void PostSwapchainDestroy(const mocktail::graphics::HeldSwapchain& held) { + if (!held) { + return; + } + const auto host_destroy = reinterpret_cast( + HostDeviceProc(held.device, "vkDestroySwapchainKHR")); + if (host_destroy == nullptr) { + return; + } + void* const library = + HoldLibraryOf(reinterpret_cast(host_destroy)); + Teardown().Post([held, host_destroy, library] { + const std::uint64_t start_ns = MonotonicNanos(); + host_destroy(held.device, held.swapchain, held.Allocator()); + const std::uint64_t elapsed_ms = (MonotonicNanos() - start_ns) / 1000000; + if (elapsed_ms >= 100) { + std::fprintf(stderr, + " [vulkan] old swapchain destroyed in the background " + "after %llu ms\n", + static_cast(elapsed_ms)); + } + ReleaseLibrary(library); + }); +} + } // namespace extern "C" { @@ -1869,9 +1900,28 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateAndroidSurfaceKHR( VKAPI_ATTR void VKAPI_CALL vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks* allocator) { - if (EnsureInitialized()) { + if (!EnsureInitialized()) { + return; + } + PostSwapchainDestroy(Retirement().ReleaseIfSurface(surface)); + const auto host_destroy = reinterpret_cast( + HostInstanceProc(instance, "vkDestroySurfaceKHR")); + if (host_destroy == nullptr || !Retirement().Enabled() || + !Teardown().Busy()) { State().android_wsi.DestroySurface(instance, surface, allocator); + return; } + // Its swapchain is still being destroyed in the background. + const bool has_allocator = allocator != nullptr; + const VkAllocationCallbacks allocator_copy = + has_allocator ? *allocator : VkAllocationCallbacks{}; + void* const library = + HoldLibraryOf(reinterpret_cast(host_destroy)); + Teardown().Post([instance, surface, has_allocator, allocator_copy, + host_destroy, library] { + host_destroy(instance, surface, has_allocator ? &allocator_copy : nullptr); + ReleaseLibrary(library); + }); } VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties( @@ -2298,6 +2348,7 @@ VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* allocator) { const auto host_destroy = reinterpret_cast( HostDeviceProc(device, "vkDestroyDevice")); + PostSwapchainDestroy(Retirement().ReleaseIfDevice(device)); State().text_overlay.DestroyDevice(device); if (State().etc2.EncoderBuilding(device)) { std::fprintf(stderr, @@ -2324,6 +2375,12 @@ vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* allocator) { } else { State().etc2.DropParkedGpuDecoder(device); if (host_destroy != nullptr) { + if (Retirement().Enabled() && Teardown().Busy()) { + std::fprintf(stderr, + " [vulkan] waiting for background destroys before " + "destroying the device\n"); + Teardown().Drain(); // Its swapchains may still be on the worker. + } host_destroy(device, allocator); } } @@ -2372,10 +2429,12 @@ VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue2( VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( VkDevice device, const VkSwapchainCreateInfoKHR* create_info, const VkAllocationCallbacks* allocator, VkSwapchainKHR* swapchain) { + VulkanCallObservation observation("vkCreateSwapchainKHR"); const auto host_create = reinterpret_cast( HostDeviceProc(device, "vkCreateSwapchainKHR")); if (host_create == nullptr || create_info == nullptr || swapchain == nullptr) { + observation.SetResult(VK_ERROR_INITIALIZATION_FAILED); return VK_ERROR_INITIALIZATION_FAILED; } VkSwapchainCreateInfoKHR host_info = *create_info; @@ -2434,6 +2493,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( if (explicit_mode && !mode_selected) { std::fprintf(stderr, " [vulkan] cannot resolve present_mode=%s for surface\n", mocktail::graphics::PresentModePolicyName(present_policy)); + observation.SetResult(VK_ERROR_INITIALIZATION_FAILED); return VK_ERROR_INITIALIZATION_FAILED; } VkSurfaceCapabilitiesKHR capabilities{}; @@ -2459,15 +2519,45 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( } } } + const mocktail::graphics::HeldSwapchain held = + Retirement().TakeForCreate(device, create_info->surface, + &host_info.oldSwapchain); + const bool retires_held = held && host_info.oldSwapchain == held.swapchain; + if (!retires_held) { + PostSwapchainDestroy(held); + } + bool first_attempt = true; const VkResult result = mocktail::graphics::CreateSwapchainWithPresentModeFallback( host_info, create_info->presentMode, [&](const VkSwapchainCreateInfoKHR& attempt) { - return host_create(device, &attempt, allocator, swapchain); + VkSwapchainCreateInfoKHR info = attempt; + if (!first_attempt) { + info.oldSwapchain = VK_NULL_HANDLE; // Retired by the failure. + } + first_attempt = false; + VkResult created = host_create(device, &info, allocator, swapchain); + if (created == VK_ERROR_NATIVE_WINDOW_IN_USE_KHR && + Retirement().Enabled() && Teardown().Busy()) { + std::fprintf(stderr, + " [vulkan] waiting for the old swapchain's " + "background destroy to free the window\n"); + Teardown().Drain(); + info.oldSwapchain = VK_NULL_HANDLE; + created = host_create(device, &info, allocator, swapchain); + } + return created; }); + if (retires_held) { + PostSwapchainDestroy(held); // Retired even when the create failed. + } + observation.SetResult(result); if (result != VK_SUCCESS) { + Retirement().Retired(create_info->oldSwapchain); return result; } + Retirement().Created(*swapchain, create_info->surface, + create_info->oldSwapchain); const auto host_get_images = reinterpret_cast( HostDeviceProc(device, "vkGetSwapchainImagesKHR")); std::uint32_t image_count = 0; @@ -2491,11 +2581,30 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( VKAPI_ATTR void VKAPI_CALL vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* allocator) { + if (swapchain == VK_NULL_HANDLE) { + return; + } + State().text_overlay.DestroySwapchain(device, swapchain); + if (Retirement().Enabled()) { + PostSwapchainDestroy(Retirement().Destroyed(device, swapchain, allocator)); + return; + } + Retirement().Forget(swapchain); const auto host_destroy = reinterpret_cast( HostDeviceProc(device, "vkDestroySwapchainKHR")); - State().text_overlay.DestroySwapchain(device, swapchain); - if (host_destroy != nullptr) { - host_destroy(device, swapchain, allocator); + if (host_destroy == nullptr) { + return; + } + const std::uint64_t start_ns = MonotonicNanos(); + host_destroy(device, swapchain, allocator); + const std::uint64_t elapsed_ms = (MonotonicNanos() - start_ns) / 1000000; + // A healthy driver takes a few ms; the slow NVIDIA case takes ~22 s. + if (elapsed_ms >= 1000) { + Retirement().Enable(); + std::fprintf(stderr, + " [vulkan] vkDestroySwapchainKHR took %llu ms; destroying " + "old swapchains in the background from now on\n", + static_cast(elapsed_ms)); } } diff --git a/src/graphics/swapchain_retirement.cc b/src/graphics/swapchain_retirement.cc new file mode 100644 index 00000000..d596b4eb --- /dev/null +++ b/src/graphics/swapchain_retirement.cc @@ -0,0 +1,81 @@ +#include "mocktail/graphics/swapchain_retirement.h" + +#include + +namespace mocktail::graphics { + +void SwapchainRetirement::Created(VkSwapchainKHR swapchain, + VkSurfaceKHR surface, + VkSwapchainKHR old_swapchain) { + std::lock_guard lock(mutex_); + surfaces_[swapchain] = surface; + if (old_swapchain != VK_NULL_HANDLE) { + retired_.insert(old_swapchain); + } +} + +void SwapchainRetirement::Retired(VkSwapchainKHR old_swapchain) { + if (old_swapchain == VK_NULL_HANDLE) { + return; + } + std::lock_guard lock(mutex_); + retired_.insert(old_swapchain); +} + +void SwapchainRetirement::Forget(VkSwapchainKHR swapchain) { + std::lock_guard lock(mutex_); + surfaces_.erase(swapchain); + retired_.erase(swapchain); +} + +HeldSwapchain SwapchainRetirement::Destroyed( + VkDevice device, VkSwapchainKHR swapchain, + const VkAllocationCallbacks* allocator) { + std::lock_guard lock(mutex_); + HeldSwapchain destroyed; + destroyed.device = device; + destroyed.swapchain = swapchain; + if (allocator != nullptr) { + destroyed.has_allocator = true; + destroyed.allocator = *allocator; + } + if (const auto found = surfaces_.find(swapchain); found != surfaces_.end()) { + destroyed.surface = found->second; + surfaces_.erase(found); + } + if (retired_.erase(swapchain) != 0) { + return destroyed; + } + return std::exchange(held_, destroyed); +} + +HeldSwapchain SwapchainRetirement::TakeForCreate(VkDevice device, + VkSurfaceKHR surface, + VkSwapchainKHR* old_swapchain) { + std::lock_guard lock(mutex_); + if (held_ && held_.device == device && held_.surface == surface && + old_swapchain != nullptr && + *old_swapchain == VK_NULL_HANDLE) { + *old_swapchain = held_.swapchain; + } + return std::exchange(held_, HeldSwapchain{}); +} + +HeldSwapchain SwapchainRetirement::ReleaseIfSurface(VkSurfaceKHR surface) { + std::lock_guard lock(mutex_); + return held_.surface == surface ? std::exchange(held_, HeldSwapchain{}) + : HeldSwapchain{}; +} + +HeldSwapchain SwapchainRetirement::ReleaseIfDevice(VkDevice device) { + std::lock_guard lock(mutex_); + return held_.device == device ? std::exchange(held_, HeldSwapchain{}) + : HeldSwapchain{}; +} + +HeldSwapchain SwapchainRetirement::Release() { + std::lock_guard lock(mutex_); + return std::exchange(held_, HeldSwapchain{}); +} + +} // namespace mocktail::graphics diff --git a/stubs/CMakeLists.txt b/stubs/CMakeLists.txt index 5e4e8063..511e9491 100644 --- a/stubs/CMakeLists.txt +++ b/stubs/CMakeLists.txt @@ -127,6 +127,7 @@ add_library(stub_vulkan SHARED ${CMAKE_SOURCE_DIR}/src/graphics/bionic_vulkan_loader_adapter.cc ${CMAKE_SOURCE_DIR}/src/graphics/chrome_trace_writer.cc ${CMAKE_SOURCE_DIR}/src/graphics/deferred_teardown.cc + ${CMAKE_SOURCE_DIR}/src/graphics/swapchain_retirement.cc ${CMAKE_SOURCE_DIR}/src/graphics/vulkan_object_reuse.cc ${CMAKE_SOURCE_DIR}/src/graphics/vulkan_text_overlay_compositor.cc ${CMAKE_SOURCE_DIR}/src/graphics/vulkan_etc2_emulation.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 44291eec..2a56b596 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -891,6 +891,17 @@ target_link_libraries(vulkan_object_reuse_test PRIVATE GTest::gtest_main Vulkan::Headers) mocktail_apply_compile_options(vulkan_object_reuse_test) +add_executable(swapchain_retirement_test + swapchain_retirement_test.cc + ${PROJECT_SOURCE_DIR}/src/graphics/swapchain_retirement.cc +) +target_include_directories(swapchain_retirement_test PRIVATE + ${PROJECT_SOURCE_DIR}/include +) +target_link_libraries(swapchain_retirement_test PRIVATE GTest::gtest_main + Vulkan::Headers) +mocktail_apply_compile_options(swapchain_retirement_test) + add_executable(deferred_teardown_test deferred_teardown_test.cc ${PROJECT_SOURCE_DIR}/src/graphics/deferred_teardown.cc @@ -1559,6 +1570,7 @@ gtest_discover_tests(chrome_trace_writer_test) gtest_discover_tests(vulkan_etc2_emulation_test) gtest_discover_tests(vulkan_etc2_gpu_emulation_test) gtest_discover_tests(deferred_teardown_test) +gtest_discover_tests(swapchain_retirement_test) gtest_discover_tests(vulkan_object_reuse_test) gtest_discover_tests(log_redaction_test) gtest_discover_tests(vulkan_driver_pin_test) diff --git a/tests/swapchain_retirement_test.cc b/tests/swapchain_retirement_test.cc new file mode 100644 index 00000000..f04276c9 --- /dev/null +++ b/tests/swapchain_retirement_test.cc @@ -0,0 +1,153 @@ +#include "mocktail/graphics/swapchain_retirement.h" + +#include +#include + +#include + +namespace { + +using mocktail::graphics::HeldSwapchain; +using mocktail::graphics::SwapchainRetirement; + +template +Handle Fake(std::uintptr_t value) { + return reinterpret_cast(value); +} + +const VkDevice kDevice = Fake(0x10); +const VkDevice kOtherDevice = Fake(0x11); +const VkSurfaceKHR kSurface = Fake(0x20); +const VkSurfaceKHR kOtherSurface = Fake(0x21); +const VkSwapchainKHR kFirst = Fake(0x30); +const VkSwapchainKHR kSecond = Fake(0x31); + +TEST(SwapchainRetirementTest, StaysOffUntilEnabled) { + SwapchainRetirement retirement; + EXPECT_FALSE(retirement.Enabled()); + retirement.Enable(); + EXPECT_TRUE(retirement.Enabled()); +} + +TEST(SwapchainRetirementTest, ForgetsASwapchainDestroyedInPlace) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + retirement.Created(kSecond, kSurface, kFirst); + retirement.Forget(kFirst); + + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + EXPECT_EQ(retirement.Release().surface, VK_NULL_HANDLE); +} + +TEST(SwapchainRetirementTest, HoldsALiveSwapchainUntilTheNextCreateRetiresIt) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + VkSwapchainKHR old_swapchain = VK_NULL_HANDLE; + const HeldSwapchain held = + retirement.TakeForCreate(kDevice, kSurface, &old_swapchain); + EXPECT_EQ(old_swapchain, kFirst); + EXPECT_EQ(held.swapchain, kFirst); + EXPECT_EQ(held.surface, kSurface); + EXPECT_FALSE(retirement.Release()); +} + +TEST(SwapchainRetirementTest, DestroysASwapchainTheGameAlreadyRetired) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + retirement.Created(kSecond, kSurface, kFirst); + + const HeldSwapchain now = retirement.Destroyed(kDevice, kFirst, nullptr); + EXPECT_EQ(now.swapchain, kFirst); + EXPECT_FALSE(retirement.Release()); +} + +TEST(SwapchainRetirementTest, KeepsTheGamesOwnOldSwapchain) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + VkSwapchainKHR old_swapchain = kSecond; + const HeldSwapchain held = + retirement.TakeForCreate(kDevice, kSurface, &old_swapchain); + EXPECT_EQ(old_swapchain, kSecond); + EXPECT_EQ(held.swapchain, kFirst); +} + +TEST(SwapchainRetirementTest, DoesNotRetireIntoAnotherDevice) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + VkSwapchainKHR old_swapchain = VK_NULL_HANDLE; + const HeldSwapchain held = + retirement.TakeForCreate(kOtherDevice, kSurface, &old_swapchain); + EXPECT_EQ(old_swapchain, VK_NULL_HANDLE); + EXPECT_EQ(held.swapchain, kFirst); +} + +TEST(SwapchainRetirementTest, DoesNotRetireIntoAnotherSurface) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + VkSwapchainKHR old_swapchain = VK_NULL_HANDLE; + const HeldSwapchain held = + retirement.TakeForCreate(kDevice, kOtherSurface, &old_swapchain); + EXPECT_EQ(old_swapchain, VK_NULL_HANDLE); + EXPECT_EQ(held.swapchain, kFirst); +} + +TEST(SwapchainRetirementTest, DestroysASwapchainRetiredByAFailedCreate) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + retirement.Retired(kFirst); + + EXPECT_EQ(retirement.Destroyed(kDevice, kFirst, nullptr).swapchain, kFirst); + EXPECT_FALSE(retirement.Release()); +} + +TEST(SwapchainRetirementTest, CopiesTheAllocator) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + int user_data = 0; + { + VkAllocationCallbacks callbacks{}; + callbacks.pUserData = &user_data; + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, &callbacks)); + } + const HeldSwapchain held = retirement.Release(); + ASSERT_NE(held.Allocator(), nullptr); + EXPECT_EQ(held.Allocator()->pUserData, &user_data); + EXPECT_EQ(HeldSwapchain{}.Allocator(), nullptr); +} + +TEST(SwapchainRetirementTest, HoldsOnlyTheLatestSwapchain) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + retirement.Created(kSecond, kOtherSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + const HeldSwapchain released = retirement.Destroyed(kDevice, kSecond, nullptr); + EXPECT_EQ(released.swapchain, kFirst); + EXPECT_EQ(retirement.Release().swapchain, kSecond); +} + +TEST(SwapchainRetirementTest, ReleasesWhenItsSurfaceOrDeviceGoes) { + SwapchainRetirement retirement; + retirement.Created(kFirst, kSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kFirst, nullptr)); + + EXPECT_FALSE(retirement.ReleaseIfSurface(kOtherSurface)); + EXPECT_FALSE(retirement.ReleaseIfDevice(kOtherDevice)); + EXPECT_EQ(retirement.ReleaseIfSurface(kSurface).swapchain, kFirst); + + retirement.Created(kSecond, kSurface, VK_NULL_HANDLE); + EXPECT_FALSE(retirement.Destroyed(kDevice, kSecond, nullptr)); + EXPECT_EQ(retirement.ReleaseIfDevice(kDevice).swapchain, kSecond); + EXPECT_FALSE(retirement.Release()); +} + +} // namespace