From 9741bcacd9cb92f0a4d7ef749c642d9d8b82842a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 11 Oct 2026 16:55:36 +0200 Subject: [PATCH] iOS: Apply size changes between frames, fixing a glitch when rotating UIKit lays out at the new size from the main thread, often while a frame is being drawn, and the swapchain was only resized after presenting. So the first frame of a rotation was laid out for one size and drawn at the other. The view controller now hands the new size to the render thread, and NativeFrame resizes before drawing instead of after presenting. Co-Authored-By: Claude Opus 5.5 --- UI/NativeApp.cpp | 54 +++++++++++++++++++--------------------- ios/ViewController.mm | 57 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index cdc5528810..f841e9e236 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1159,6 +1159,29 @@ void NativeFrame(GraphicsContext *graphicsContext) { g_audioBackend->FrameUpdate(g_Config.bAutoSwitchAudioDevice); } + // Resize before drawing anything, so the first frame at a new size isn't drawn into the old backbuffer. + const bool resizedThisFrame = resized; + if (resized) { + INFO_LOG(Log::G3D, "Resized flag set - recalculating bounds"); + resized = false; + + if (uiContext) { + uiContext->SetBounds(Bounds(0, 0, g_display.dp_xres, g_display.dp_yres)); + } + + graphicsContext->Resize(); + g_screenManager->resized(); + + // TODO: Move this to the GraphicsContext objects for each backend. + // (Android used to be left out here, since every size change there was a full graphics restart, + // and DeviceRestore covers it. Rotation and window resizing are in-place now.) +#if !PPSSPP_PLATFORM(WINDOWS) + PSP_CoreParameter().pixelWidth = g_display.pixel_xres; + PSP_CoreParameter().pixelHeight = g_display.pixel_yres; + System_PostUIMessage(UIMessage::GPU_DISPLAY_RESIZED); +#endif + } + // NOTE: We must begin the frame before update, so we can do texture size queries and stuff in Measure etc. Draw::DebugFlags debugFlags = Draw::DebugFlags::NONE; if ((DebugOverlay)g_Config.iDebugOverlay == DebugOverlay::GPU_PROFILE) @@ -1317,35 +1340,8 @@ void NativeFrame(GraphicsContext *graphicsContext) { g_draw->Present(g_frameTiming.PresentMode()); - if (resized) { - INFO_LOG(Log::G3D, "Resized flag set - recalculating bounds"); - resized = false; - - if (uiContext) { - uiContext->SetBounds(Bounds(0, 0, g_display.dp_xres, g_display.dp_yres)); - - // OSX 10.6 and SDL 1.2 bug. -#if defined(__APPLE__) - static int dp_xres_old = g_display.dp_xres; - if (g_display.dp_xres != dp_xres_old) { - dp_xres_old = g_display.dp_xres; - } -#endif - } - - graphicsContext->Resize(); - g_screenManager->resized(); - - // TODO: Move this to the GraphicsContext objects for each backend. - // (Android used to be left out here, since every size change there was a full graphics restart, - // and DeviceRestore covers it. Rotation and window resizing are in-place now.) -#if !PPSSPP_PLATFORM(WINDOWS) - PSP_CoreParameter().pixelWidth = g_display.pixel_xres; - PSP_CoreParameter().pixelHeight = g_display.pixel_yres; - System_PostUIMessage(UIMessage::GPU_DISPLAY_RESIZED); -#endif - } else { - // INFO_LOG(Log::G3D, "Polling graphics context"); + // Not done if we resized at the start of the frame, the swapchain is new. + if (!resizedThisFrame) { graphicsContext->Poll(); } diff --git a/ios/ViewController.mm b/ios/ViewController.mm index 49416a4485..0f295754d7 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -6,6 +6,8 @@ #import #import #include +#include +#include #include #include "Common/GPU/GraphicsContext.h" #include "Common/GPU/Vulkan/VulkanGraphicsContext.h" @@ -74,6 +76,34 @@ static std::atomic exitRenderLoop; static std::atomic renderLoopRunning; static std::thread g_renderLoopThread; +// A size change from the main thread, for the render thread to apply between frames. Changing +// g_display while a frame is being drawn gives a frame laid out for one size and drawn at another. +struct PendingResolution { + float pixelWidth; + float pixelHeight; + float dpiScale; + float uiScale; +}; +static std::mutex g_pendingResolutionLock; +static std::optional g_pendingResolution; + +static void ApplyResolution(const PendingResolution &res) { + g_display.Recalculate(res.pixelWidth, res.pixelHeight, res.dpiScale, res.dpiScale, res.uiScale); + // NativeFrame updates the PSP_CoreParameter() size along with the rest. + NativeResized(); +} + +static void ApplyPendingResolution() { + std::optional res; + { + std::lock_guard guard(g_pendingResolutionLock); + res.swap(g_pendingResolution); + } + if (res) { + ApplyResolution(*res); + } +} + static void VulkanRenderLoop(GraphicsContext *graphicsContext, CAMetalLayer *metalLayer) { // Only a provisional name, RunGraphicsLoop renames the thread once it knows its role. SetCurrentThreadName("RenderLoop"); @@ -88,6 +118,7 @@ static void VulkanRenderLoop(GraphicsContext *graphicsContext, CAMetalLayer *met } const auto frame = [](GraphicsContext *graphicsContext) { + ApplyPendingResolution(); NativeFrame(graphicsContext); return !exitRenderLoop; }; @@ -685,20 +716,26 @@ extern float g_safeInsetBottom; CGSize size = view.bounds.size; float dpi = (IS_IPAD() ? 200.0f : 150.0f) * scale; - const float dpi_scale_x = 240.0f / dpi; - const float dpi_scale_y = 240.0f / dpi; - - g_display.Recalculate(size.width * scale, size.height * scale, - dpi_scale_x, dpi_scale_y, - UIScaleFactorToMultiplier(g_Config.iUIScaleFactor)); + PendingResolution res{ + (float)(size.width * scale), (float)(size.height * scale), + 240.0f / dpi, + UIScaleFactorToMultiplier(g_Config.iUIScaleFactor), + }; [view setContentScaleFactor:scale]; - // NativeFrame updates the PSP_CoreParameter() size along with the rest. - NativeResized(); + { + std::lock_guard guard(g_pendingResolutionLock); + if (renderLoopRunning) { + // Applied by the render thread before its next frame. + g_pendingResolution = res; + } else { + g_pendingResolution.reset(); + ApplyResolution(res); + } + } - NSLog(@"Updated display resolution: (%d, %d) @%.1fx", - g_display.pixel_xres, g_display.pixel_yres, (float)scale); + NSLog(@"Updated display resolution: (%d, %d) @%.1fx", (int)res.pixelWidth, (int)res.pixelHeight, (float)scale); } @end