From d7399e33c10b430ed0f82c76bfaac118be2907d9 Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Sun, 26 Jun 2022 15:26:18 +0530 Subject: [PATCH] Avoid waiting on mutex in `PresentationEngine::Present` We want to block on the host thread during presentation while the host surface isn't present to implicitly pause the game, this can end up being fairly costly as it involves locking the `PresentationEngine` mutex which can lead to a lot of contention with the presentation thread. This fixes the issue by polling if there is a surface and only if there isn't then doing the wait as it isn't mandatory to wait always, we'll eventually run into the guest thread stalling. --- app/src/main/cpp/skyline/gpu/presentation_engine.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/cpp/skyline/gpu/presentation_engine.cpp b/app/src/main/cpp/skyline/gpu/presentation_engine.cpp index dfe4de98..39043d4e 100644 --- a/app/src/main/cpp/skyline/gpu/presentation_engine.cpp +++ b/app/src/main/cpp/skyline/gpu/presentation_engine.cpp @@ -374,8 +374,11 @@ namespace skyline::gpu { } u64 PresentationEngine::Present(const std::shared_ptr &texture, i64 timestamp, i64 swapInterval, AndroidRect crop, NativeWindowScalingMode scalingMode, NativeWindowTransform transform, skyline::service::hosbinder::AndroidFence fence, const std::function& presentCallback) { - std::unique_lock lock(mutex); - surfaceCondition.wait(lock, [this]() { return vkSurface.has_value(); }); + if (!vkSurface.has_value()) { + // We want this function to generally (not necessarily always) block when a surface is not present to implicitly pause the game + std::unique_lock lock(mutex); + surfaceCondition.wait(lock, [this]() { return vkSurface.has_value(); }); + } presentQueue.Push(PresentableFrame{ texture,