From 42970d1118f06ed931ad9d75ceec5b929228eb1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 13 Feb 2026 18:08:28 +0100 Subject: [PATCH] Modernize iOS screen resize code a little. --- ios/ViewController.mm | 4 ++-- ios/ViewControllerCommon.h | 2 +- ios/ViewControllerCommon.mm | 26 +++++++++++++++++++------- ios/ViewControllerMetal.mm | 15 +++++++++++---- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/ios/ViewController.mm b/ios/ViewController.mm index f555f5f435..c1d7a91282 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -313,8 +313,8 @@ void GLRenderLoop(IOSGLESContext *graphicsContext) { INFO_LOG(Log::System, "didBecomeActive begin"); + [self updateResolutionWithView:self.view]; [self runGLRenderLoop]; - [self updateResolution:[UIScreen mainScreen]]; INFO_LOG(Log::System, "didBecomeActive end"); @@ -383,8 +383,8 @@ void GLRenderLoop(IOSGLESContext *graphicsContext) { NSLog(@"Rotation finished"); // Reinitialize graphics context to match new size [self requestExitGLRenderLoop]; + [self updateResolutionWithView:self.view]; [self runGLRenderLoop]; - [self updateResolution:[UIScreen mainScreen]]; }]; } diff --git a/ios/ViewControllerCommon.h b/ios/ViewControllerCommon.h index 76e4e93b78..a4f83c6687 100644 --- a/ios/ViewControllerCommon.h +++ b/ios/ViewControllerCommon.h @@ -32,7 +32,7 @@ - (void)uiStateChanged; - (void)pickPhoto:(NSString *)saveFilename requestId:(int)requestId; -- (void)updateResolution:(UIScreen *)screen; +- (void)updateResolutionWithView:(UIView *)view; @end diff --git a/ios/ViewControllerCommon.mm b/ios/ViewControllerCommon.mm index 05cb249cf3..b7eae300bf 100644 --- a/ios/ViewControllerCommon.mm +++ b/ios/ViewControllerCommon.mm @@ -442,17 +442,28 @@ extern float g_safeInsetBottom; [self setNeedsStatusBarAppearanceUpdate]; } -- (void)updateResolution:(UIScreen *)screen { - float scale = screen.nativeScale; - CGSize size = screen.bounds.size; +- (void)updateResolutionWithView:(UIView *)view { + // 1. Get the scale from the window scene (safest for Metal) + CGFloat scale = 1.0; + if (view.window.windowScene) { + scale = view.window.windowScene.screen.nativeScale; + } else { + scale = [UITraitCollection currentTraitCollection].displayScale; + } + // 2. Get the ACTUAL bounds of the view (already corrected for orientation) + CGSize size = view.bounds.size; + + // Your existing logic 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)); - [[sharedViewController getView] setContentScaleFactor:scale]; + g_display.Recalculate(size.width * scale, size.height * scale, + dpi_scale_x, dpi_scale_y, + UIScaleFactorToMultiplier(g_Config.iUIScaleFactor)); + + [view setContentScaleFactor:scale]; // PSP native resize PSP_CoreParameter().pixelWidth = g_display.pixel_xres; @@ -460,7 +471,8 @@ extern float g_safeInsetBottom; NativeResized(); - NSLog(@"Updated display resolution: (%d, %d) @%.1fx", g_display.pixel_xres, g_display.pixel_yres, scale); + NSLog(@"Updated display resolution: (%d, %d) @%.1fx", + g_display.pixel_xres, g_display.pixel_yres, (float)scale); } @end diff --git a/ios/ViewControllerMetal.mm b/ios/ViewControllerMetal.mm index bd136f9e5e..70382a2a62 100644 --- a/ios/ViewControllerMetal.mm +++ b/ios/ViewControllerMetal.mm @@ -294,8 +294,8 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye // Spin up the emu thread. It will in turn spin up the Vulkan render thread // on its own. + [self updateResolutionWithView:self.view]; [self runVulkanRenderLoop]; - [self updateResolution:[UIScreen mainScreen]]; } - (void)willResignActive { @@ -345,7 +345,7 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye graphicsContext = new IOSVulkanContext(); - [self updateResolution:[UIScreen mainScreen]]; + [self updateResolutionWithView:self.view]; if (!graphicsContext->InitAPI()) { _assert_msg_(false, "Failed to init Vulkan"); @@ -365,7 +365,14 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; INFO_LOG(Log::G3D, "viewWillAppear"); - self.view.contentScaleFactor = UIScreen.mainScreen.nativeScale; + // This is to make sure we get 1:1 pixels. + UIWindowScene *scene = self.view.window.windowScene; + if (scene) { + self.view.contentScaleFactor = scene.screen.nativeScale; + } + if (@available(iOS 16.0, *)) { + [self setNeedsUpdateOfSupportedInterfaceOrientations]; + } } - (void)viewWillDisappear:(BOOL)animated { @@ -407,8 +414,8 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye NSLog(@"Rotation finished"); // Reinitialize graphics context to match new size [self requestExitVulkanRenderLoop]; + [self updateResolutionWithView:self.view]; [self runVulkanRenderLoop]; - [self updateResolution:[UIScreen mainScreen]]; }]; }