sdl2-compat: SDL 2.0 DPI calculation uses the display content scale

From ca1c0aa443ba1550248b0b76c77f3b2aeda359d7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 19 May 2023 06:30:19 -0700
Subject: [PATCH] SDL 2.0 DPI calculation uses the display content scale

---
 src/sdl2_compat.c | 19 ++++++++++---------
 src/sdl3_syms.h   |  1 +
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 2ea3d3e..ef8716e 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -3789,26 +3789,27 @@ SDL_GetNumDisplayModes(int displayIndex)
 DECLSPEC int SDLCALL
 SDL_GetDisplayDPI(int displayIndex, float *ddpi, float *hdpi, float *vdpi)
 {
-    const SDL_DisplayMode *dp = SDL3_GetDesktopDisplayMode(Display_IndexToID(displayIndex));
-    float val;
-    if (dp == NULL) {
-        return -1;
+    float content_scale = SDL3_GetDisplayContentScale(Display_IndexToID(displayIndex));
+    float dpi;
+
+    if (content_scale == 0.0f) {
+        content_scale = 1.0f;
     }
 
 #if defined(__ANDROID__) || defined(__IOS__)
-    val = dp->pixel_density * 160.0f;
+    dpi = content_scale * 160.0f;
 #else
-    val = dp->pixel_density * 96.0f;
+    dpi = content_scale * 96.0f;
 #endif
 
     if (hdpi) {
-        *hdpi = val;
+        *hdpi = dpi;
     }
     if (vdpi) {
-        *vdpi = val;
+        *vdpi = dpi;
     }
     if (ddpi) {
-        *ddpi = val;
+        *ddpi = dpi;
     }
     return 0;
 }
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 5d604c8..6eb5e9c 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -478,6 +478,7 @@ SDL3_SYM_PASSTHROUGH(const char*,GetVideoDriver,(int a),(a),return)
 SDL3_SYM_PASSTHROUGH(const char*,GetCurrentVideoDriver,(void),(),return)
 SDL3_SYM(const char*,GetDisplayName,(SDL_DisplayID a),(a),return)
 SDL3_SYM(int,GetDisplayBounds,(SDL_DisplayID a, SDL_Rect *b),(a,b),return)
+SDL3_SYM(float,GetDisplayContentScale,(SDL_DisplayID a),(a),return)
 SDL3_SYM(const SDL_DisplayMode*,GetDesktopDisplayMode,(SDL_DisplayID a),(a),return)
 SDL3_SYM(const SDL_DisplayMode*,GetCurrentDisplayMode,(SDL_DisplayID a),(a),return)
 SDL3_SYM(int,SetWindowFullscreenMode,(SDL_Window *a, const SDL_DisplayMode *b),(a,b),return)