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