SDL: Set Fixed Scale Factor for VisionOS (#10222)

From a16ff651e8b631d2d960809f30c24e84f6aa8ed3 Mon Sep 17 00:00:00 2001
From: Giovanni Petrantoni <[EMAIL REDACTED]>
Date: Thu, 11 Jul 2024 12:56:50 +0800
Subject: [PATCH] Set Fixed Scale Factor for VisionOS (#10222)

---
 src/video/uikit/SDL_uikitmetalview.m | 12 ++++++++++--
 src/video/uikit/SDL_uikitwindow.m    |  8 ++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/video/uikit/SDL_uikitmetalview.m b/src/video/uikit/SDL_uikitmetalview.m
index 71be59e49c770..ab7942143d911 100644
--- a/src/video/uikit/SDL_uikitmetalview.m
+++ b/src/video/uikit/SDL_uikitmetalview.m
@@ -79,16 +79,24 @@ SDL_MetalView UIKit_Metal_CreateView(SDL_VideoDevice *_this, SDL_Window *window)
         CGFloat scale = 1.0;
         SDL_uikitmetalview *metalview;
 
-#ifndef SDL_PLATFORM_VISIONOS
         if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
             /* Set the scale to the natural scale factor of the screen - then
              * the backing dimensions of the Metal view will match the pixel
              * dimensions of the screen rather than the dimensions in points
              * yielding high resolution on retine displays.
              */
+#ifndef SDL_PLATFORM_VISIONOS
             scale = data.uiwindow.screen.nativeScale;
-        }
+#else
+            // VisionOS doesn't use the concept of "nativeScale" like other iOS devices.
+            // We use a fixed scale factor of 2.0 to achieve better pixel density.
+            // This is because VisionOS presents a virtual 1280x720 "screen", but we need
+            // to render at a higher resolution for optimal visual quality.
+            // TODO: Consider making this configurable or determining it dynamically
+            // based on the specific visionOS device capabilities.
+            scale = 2.0;
 #endif
+        }
 
         metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds
                                                         scale:scale];
diff --git a/src/video/uikit/SDL_uikitwindow.m b/src/video/uikit/SDL_uikitwindow.m
index b443695d6bd19..395c01322a2ff 100644
--- a/src/video/uikit/SDL_uikitwindow.m
+++ b/src/video/uikit/SDL_uikitwindow.m
@@ -368,11 +368,15 @@ void UIKit_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int
         CGSize size = view.bounds.size;
         CGFloat scale = 1.0;
 
-#ifndef SDL_PLATFORM_VISIONOS
+
         if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
+#ifndef SDL_PLATFORM_VISIONOS
             scale = windata.uiwindow.screen.nativeScale;
-        }
+#else
+            scale = 2.0;
 #endif
+        }
+
 
         /* Integer truncation of fractional values matches SDL_uikitmetalview and
          * SDL_uikitopenglview. */