Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions include/mocktail/graphics/swapchain_retirement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef MOCKTAIL_GRAPHICS_SWAPCHAIN_RETIREMENT_H_
#define MOCKTAIL_GRAPHICS_SWAPCHAIN_RETIREMENT_H_

#include <vulkan/vulkan.h>

#include <atomic>
#include <mutex>
#include <unordered_map>
#include <unordered_set>

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<VkSwapchainKHR, VkSurfaceKHR> surfaces_;
std::unordered_set<VkSwapchainKHR> retired_;
HeldSwapchain held_;
std::atomic<bool> enabled_{false};
};

} // namespace mocktail::graphics

#endif
119 changes: 114 additions & 5 deletions src/graphics/bionic_vulkan_loader_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<PFN_vkDestroySwapchainKHR>(
HostDeviceProc(held.device, "vkDestroySwapchainKHR"));
if (host_destroy == nullptr) {
return;
}
void* const library =
HoldLibraryOf(reinterpret_cast<const void*>(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<unsigned long long>(elapsed_ms));
}
ReleaseLibrary(library);
});
}

} // namespace

extern "C" {
Expand Down Expand Up @@ -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<PFN_vkDestroySurfaceKHR>(
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<const void*>(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(
Expand Down Expand Up @@ -2298,6 +2348,7 @@ VKAPI_ATTR void VKAPI_CALL
vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* allocator) {
const auto host_destroy = reinterpret_cast<PFN_vkDestroyDevice>(
HostDeviceProc(device, "vkDestroyDevice"));
PostSwapchainDestroy(Retirement().ReleaseIfDevice(device));
State().text_overlay.DestroyDevice(device);
if (State().etc2.EncoderBuilding(device)) {
std::fprintf(stderr,
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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<PFN_vkCreateSwapchainKHR>(
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;
Expand Down Expand Up @@ -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{};
Expand All @@ -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<PFN_vkGetSwapchainImagesKHR>(
HostDeviceProc(device, "vkGetSwapchainImagesKHR"));
std::uint32_t image_count = 0;
Expand All @@ -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<PFN_vkDestroySwapchainKHR>(
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<unsigned long long>(elapsed_ms));
}
}

Expand Down
81 changes: 81 additions & 0 deletions src/graphics/swapchain_retirement.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "mocktail/graphics/swapchain_retirement.h"

#include <utility>

namespace mocktail::graphics {

void SwapchainRetirement::Created(VkSwapchainKHR swapchain,
VkSurfaceKHR surface,
VkSwapchainKHR old_swapchain) {
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
retired_.insert(old_swapchain);
}

void SwapchainRetirement::Forget(VkSwapchainKHR swapchain) {
std::lock_guard<std::mutex> lock(mutex_);
surfaces_.erase(swapchain);
retired_.erase(swapchain);
}

HeldSwapchain SwapchainRetirement::Destroyed(
VkDevice device, VkSwapchainKHR swapchain,
const VkAllocationCallbacks* allocator) {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(mutex_);
return held_.surface == surface ? std::exchange(held_, HeldSwapchain{})
: HeldSwapchain{};
}

HeldSwapchain SwapchainRetirement::ReleaseIfDevice(VkDevice device) {
std::lock_guard<std::mutex> lock(mutex_);
return held_.device == device ? std::exchange(held_, HeldSwapchain{})
: HeldSwapchain{};
}

HeldSwapchain SwapchainRetirement::Release() {
std::lock_guard<std::mutex> lock(mutex_);
return std::exchange(held_, HeldSwapchain{});
}

} // namespace mocktail::graphics
1 change: 1 addition & 0 deletions stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading