From 0d7a1831b69a7dedd1b040bea4884c7268ddfc01 Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Sat, 1 Jul 2023 02:07:38 +0800 Subject: [PATCH 1/6] sdl: support HiDPI on wayland --- Core/Core.cpp | 2 +- SDL/SDLMain.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Core/Core.cpp b/Core/Core.cpp index daf858a30b..690776ea66 100644 --- a/Core/Core.cpp +++ b/Core/Core.cpp @@ -167,7 +167,7 @@ bool UpdateScreenScale(int width, int height) { float g_logical_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_LOGICAL_DPI); g_display.dpi_scale_x = g_logical_dpi / g_display.dpi; g_display.dpi_scale_y = g_logical_dpi / g_display.dpi; -#elif PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP) +#elif (PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)) || PPSSPP_PLATFORM(LINUX) g_display.dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI); g_display.dpi_scale_x = 96.0f / g_display.dpi; g_display.dpi_scale_y = 96.0f / g_display.dpi; diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 955592a492..45a3376e6c 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -64,6 +64,12 @@ SDLJoystick *joystick = NULL; #include "SDLGLGraphicsContext.h" #include "SDLVulkanGraphicsContext.h" +#if PPSSPP_PLATFORM(MAC) +#include "SDL2/SDL_vulkan.h" +#else +#include "SDL_vulkan.h" +#endif + #if PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) #include "UI/DarwinFileSystemServices.h" #endif @@ -85,6 +91,7 @@ static bool g_RestartRequested = false; static int g_DesktopWidth = 0; static int g_DesktopHeight = 0; +static float g_DesktopDPI = 1.0f; static float g_RefreshRate = 60.f; static int g_sampleRate = 44100; @@ -173,6 +180,20 @@ static void StopSDLAudioDevice() { } } +static void UpdateScreenDPI(SDL_Window *window) { + int drawable_width, window_width; + SDL_GetWindowSize(window, &window_width, NULL); + + if (g_Config.iGPUBackend == (int)GPUBackend::OPENGL) + SDL_GL_GetDrawableSize(window, &drawable_width, NULL); + else if (g_Config.iGPUBackend == (int)GPUBackend::VULKAN) + SDL_Vulkan_GetDrawableSize(window, &drawable_width, NULL); + + // Round up a little otherwise there would be a gap sometimes + // in fractional scaling + g_DesktopDPI = ((float) drawable_width + 0.5) / window_width; +} + // Simple implementations of System functions @@ -495,6 +516,8 @@ float System_GetPropertyFloat(SystemProperty prop) { switch (prop) { case SYSPROP_DISPLAY_REFRESH_RATE: return g_RefreshRate; + case SYSPROP_DISPLAY_DPI: + return g_DesktopDPI * 96.0; case SYSPROP_DISPLAY_SAFE_INSET_LEFT: case SYSPROP_DISPLAY_SAFE_INSET_RIGHT: case SYSPROP_DISPLAY_SAFE_INSET_TOP: @@ -782,7 +805,7 @@ int main(int argc, char *argv[]) { #elif defined(USING_FBDEV) || PPSSPP_PLATFORM(SWITCH) mode |= SDL_WINDOW_FULLSCREEN_DESKTOP; #else - mode |= SDL_WINDOW_RESIZABLE; + mode |= SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; #endif if (mode & SDL_WINDOW_FULLSCREEN_DESKTOP) { @@ -910,6 +933,8 @@ int main(int argc, char *argv[]) { #endif } + UpdateScreenDPI(window); + bool useEmuThread = g_Config.iGPUBackend == (int)GPUBackend::OPENGL; SDL_SetWindowTitle(window, (app_name_nice + " " + PPSSPP_GIT_VERSION).c_str()); @@ -1009,8 +1034,8 @@ int main(int argc, char *argv[]) { } SDL_Event event, touchEvent; while (SDL_PollEvent(&event)) { - float mx = event.motion.x * g_display.dpi_scale_x; - float my = event.motion.y * g_display.dpi_scale_y; + float mx = event.motion.x; + float my = event.motion.y; switch (event.type) { case SDL_QUIT: @@ -1025,6 +1050,11 @@ int main(int argc, char *argv[]) { int new_width = event.window.data1; int new_height = event.window.data2; + // The size given by SDL is in point-units, convert these to + // pixels before passing to UpdateScreenScale() + int new_width_px = new_width * g_DesktopDPI; + int new_height_px = new_height * g_DesktopDPI; + windowHidden = false; Core_NotifyWindowHidden(windowHidden); @@ -1032,12 +1062,16 @@ int main(int argc, char *argv[]) { bool fullscreen = (window_flags & SDL_WINDOW_FULLSCREEN); // This one calls NativeResized if the size changed. - UpdateScreenScale(new_width, new_height); + UpdateScreenScale(new_width_px, new_height_px); // Set variable here in case fullscreen was toggled by hotkey if (g_Config.UseFullScreen() != fullscreen) { g_Config.bFullScreen = fullscreen; g_Config.iForceFullScreen = -1; + } else { + // It is possible for the monitor to change DPI, so recalculate + // DPI on each resize event. + UpdateScreenDPI(window); } if (!g_Config.bFullScreen) { From a5fb1a346ce5ad23e349f906c6a13b8d2ebf00d0 Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Sat, 1 Jul 2023 22:50:06 +0800 Subject: [PATCH 2/6] sdl: fix --dpi option after HiDPI rework --- SDL/SDLMain.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 45a3376e6c..e8097098f6 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -92,6 +92,7 @@ static bool g_RestartRequested = false; static int g_DesktopWidth = 0; static int g_DesktopHeight = 0; static float g_DesktopDPI = 1.0f; +static float g_ForcedDPI = 0.0f; // if this is 0.0f, use g_DesktopDPI static float g_RefreshRate = 60.f; static int g_sampleRate = 44100; @@ -191,7 +192,7 @@ static void UpdateScreenDPI(SDL_Window *window) { // Round up a little otherwise there would be a gap sometimes // in fractional scaling - g_DesktopDPI = ((float) drawable_width + 0.5) / window_width; + g_DesktopDPI = ((float) drawable_width + 1.0f) / window_width; } // Simple implementations of System functions @@ -517,7 +518,7 @@ float System_GetPropertyFloat(SystemProperty prop) { case SYSPROP_DISPLAY_REFRESH_RATE: return g_RefreshRate; case SYSPROP_DISPLAY_DPI: - return g_DesktopDPI * 96.0; + return (g_ForcedDPI == 0.0f ? g_DesktopDPI : g_ForcedDPI) * 96.0; case SYSPROP_DISPLAY_SAFE_INSET_LEFT: case SYSPROP_DISPLAY_SAFE_INSET_RIGHT: case SYSPROP_DISPLAY_SAFE_INSET_TOP: @@ -713,7 +714,7 @@ int main(int argc, char *argv[]) { int set_yres = -1; bool portrait = false; bool set_ipad = false; - float set_dpi = 1.0f; + float set_dpi = 0.0f; float set_scale = 1.0f; // Produce a new set of arguments with the ones we skip. @@ -824,8 +825,6 @@ int main(int argc, char *argv[]) { g_Config.bFullScreen = false; } - set_dpi = 1.0f / set_dpi; - if (set_ipad) { g_display.pixel_xres = 1024; g_display.pixel_yres = 768; @@ -840,9 +839,8 @@ int main(int argc, char *argv[]) { if (set_yres > 0) { g_display.pixel_yres = set_yres; } - float dpi_scale = 1.0f; if (set_dpi > 0) { - dpi_scale = set_dpi; + g_ForcedDPI = set_dpi; } // Mac / Linux @@ -893,18 +891,6 @@ int main(int argc, char *argv[]) { if (g_Config.iWindowHeight > 0) h = g_Config.iWindowHeight; } - g_display.pixel_xres = w; - g_display.pixel_yres = h; - g_display.dp_xres = (float)g_display.pixel_xres * dpi_scale; - g_display.dp_yres = (float)g_display.pixel_yres * dpi_scale; - - g_display.pixel_in_dps_x = (float)g_display.pixel_xres / g_display.dp_xres; - g_display.pixel_in_dps_y = (float)g_display.pixel_yres / g_display.dp_yres; - g_display.dpi_scale_x = g_display.dp_xres / (float)g_display.pixel_xres; - g_display.dpi_scale_y = g_display.dp_yres / (float)g_display.pixel_yres; - g_display.dpi_scale_real_x = g_display.dpi_scale_x; - g_display.dpi_scale_real_y = g_display.dpi_scale_y; - // g_display.Print(); GraphicsContext *graphicsContext = nullptr; SDL_Window *window = nullptr; @@ -935,6 +921,10 @@ int main(int argc, char *argv[]) { UpdateScreenDPI(window); + float dpi_scale = 1.0f / (g_ForcedDPI == 0.0f ? g_DesktopDPI : g_ForcedDPI); + + UpdateScreenScale(w, h); + bool useEmuThread = g_Config.iGPUBackend == (int)GPUBackend::OPENGL; SDL_SetWindowTitle(window, (app_name_nice + " " + PPSSPP_GIT_VERSION).c_str()); @@ -1034,8 +1024,13 @@ int main(int argc, char *argv[]) { } SDL_Event event, touchEvent; while (SDL_PollEvent(&event)) { - float mx = event.motion.x; - float my = event.motion.y; + // We have to juggle around 3 kinds of "DPI spaces" if a logical DPI is + // provided (through --dpi, it is equal to system DPI if unspecified): + // - SDL gives us motion events in "system DPI" points + // - UpdateScreenScale expects pixels, so in a way "96 DPI" points + // - The UI code expects motion events in "logical DPI" points + float mx = event.motion.x * g_DesktopDPI * g_display.dpi_scale_x; + float my = event.motion.y * g_DesktopDPI * g_display.dpi_scale_x; switch (event.type) { case SDL_QUIT: From 805821e01e3548e2aed6a2ced874d62134b5afb9 Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Sat, 22 Jul 2023 21:35:21 +0800 Subject: [PATCH 3/6] Core: decouple UpdateScreenScale from preprocessor defines --- Core/Core.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Core/Core.cpp b/Core/Core.cpp index 690776ea66..5fce2079d0 100644 --- a/Core/Core.cpp +++ b/Core/Core.cpp @@ -162,20 +162,19 @@ static bool IsWindowSmall(int pixelWidth, int pixelHeight) { // TODO: Feels like this belongs elsewhere. bool UpdateScreenScale(int width, int height) { bool smallWindow; -#if defined(USING_QT_UI) - g_display.dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI); + float g_logical_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_LOGICAL_DPI); + g_display.dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI); + + if (g_display.dpi < 0.0f) { + g_display.dpi = 96.0f; + } + if (g_logical_dpi < 0.0f) { + g_logical_dpi = 96.0f; + } + g_display.dpi_scale_x = g_logical_dpi / g_display.dpi; g_display.dpi_scale_y = g_logical_dpi / g_display.dpi; -#elif (PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)) || PPSSPP_PLATFORM(LINUX) - g_display.dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI); - g_display.dpi_scale_x = 96.0f / g_display.dpi; - g_display.dpi_scale_y = 96.0f / g_display.dpi; -#else - g_display.dpi = 96.0f; - g_display.dpi_scale_x = 1.0f; - g_display.dpi_scale_y = 1.0f; -#endif g_display.dpi_scale_real_x = g_display.dpi_scale_x; g_display.dpi_scale_real_y = g_display.dpi_scale_y; From b23c59676bdcac7f72484a18b83683f3eca98b1e Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Tue, 25 Jul 2023 22:54:12 +0800 Subject: [PATCH 4/6] SDL: scale touch events by logical DPI too --- SDL/SDLMain.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index e8097098f6..d3ac76f0e9 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -1165,8 +1165,8 @@ int main(int argc, char *argv[]) { SDL_GetWindowSize(window, &w, &h); TouchInput input; input.id = event.tfinger.fingerId; - input.x = event.tfinger.x * w; - input.y = event.tfinger.y * h; + input.x = event.tfinger.x * w * g_DesktopDPI * g_display.dpi_scale_x; + input.y = event.tfinger.y * h * g_DesktopDPI * g_display.dpi_scale_x; input.flags = TOUCH_MOVE; input.timestamp = event.tfinger.timestamp; NativeTouch(input); @@ -1177,8 +1177,8 @@ int main(int argc, char *argv[]) { SDL_GetWindowSize(window, &w, &h); TouchInput input; input.id = event.tfinger.fingerId; - input.x = event.tfinger.x * w; - input.y = event.tfinger.y * h; + input.x = event.tfinger.x * w * g_DesktopDPI * g_display.dpi_scale_x; + input.y = event.tfinger.y * h * g_DesktopDPI * g_display.dpi_scale_x; input.flags = TOUCH_DOWN; input.timestamp = event.tfinger.timestamp; NativeTouch(input); @@ -1195,8 +1195,8 @@ int main(int argc, char *argv[]) { SDL_GetWindowSize(window, &w, &h); TouchInput input; input.id = event.tfinger.fingerId; - input.x = event.tfinger.x * w; - input.y = event.tfinger.y * h; + input.x = event.tfinger.x * w * g_DesktopDPI * g_display.dpi_scale_x; + input.y = event.tfinger.y * h * g_DesktopDPI * g_display.dpi_scale_x; input.flags = TOUCH_UP; input.timestamp = event.tfinger.timestamp; NativeTouch(input); From eda5f355bd1a69e012f9c4295ad599404fa8a2b7 Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Sat, 29 Jul 2023 18:55:22 +0800 Subject: [PATCH 5/6] Add NSHighResolutionCapable --- macOS/Info.plist | 2 ++ macOS/InfoGold.plist | 2 ++ 2 files changed, 4 insertions(+) diff --git a/macOS/Info.plist b/macOS/Info.plist index c4f448c261..03a318124a 100644 --- a/macOS/Info.plist +++ b/macOS/Info.plist @@ -90,6 +90,8 @@ CSResourcesFileMapped + NSHighResolutionCapable + NSHumanReadableCopyright UTExportedTypeDeclarations diff --git a/macOS/InfoGold.plist b/macOS/InfoGold.plist index cd816e2501..2bec2d6606 100644 --- a/macOS/InfoGold.plist +++ b/macOS/InfoGold.plist @@ -90,6 +90,8 @@ CSResourcesFileMapped + NSHighResolutionCapable + NSHumanReadableCopyright UTExportedTypeDeclarations From 7221e39a031403f1845cde3695ba9dc1d2c2e429 Mon Sep 17 00:00:00 2001 From: Hoe Hao Cheng Date: Wed, 2 Aug 2023 22:30:07 +0800 Subject: [PATCH 6/6] sdl: scale window dimensions by DPI on initialization --- SDL/SDLMain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index d3ac76f0e9..f01fa8c02d 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -923,7 +923,7 @@ int main(int argc, char *argv[]) { float dpi_scale = 1.0f / (g_ForcedDPI == 0.0f ? g_DesktopDPI : g_ForcedDPI); - UpdateScreenScale(w, h); + UpdateScreenScale(w * g_DesktopDPI, h * g_DesktopDPI); bool useEmuThread = g_Config.iGPUBackend == (int)GPUBackend::OPENGL;