Scale initial window size for DPI on Windows and X11

This commit is contained in:
Cameron Gutman committed 2026-11-29 23:44:00 -06:00
1 parent fd0fc5a808
commit 990229fbb8
2 files changed
+34 -17

No files matched your search

+32 -16
View File
@@ -666,7 +666,7 @@ bool Session::initialize(QQuickWindow* qtWindow)
m_StreamConfig.height = m_Preferences->height;
int x, y, width, height;
getWindowDimensions(x, y, width, height);
getWindowDimensions(m_StreamConfig.width, m_StreamConfig.height, false, x, y, width, height);
// Create a hidden window to use for decoder initialization tests
SDL_Window* testWindow = StreamUtils::createTestWindow();
@@ -1324,7 +1324,8 @@ private:
Session* m_Session;
};
void Session::getWindowDimensions(int& x, int& y,
void Session::getWindowDimensions(int desiredWidth, int desiredHeight, bool onlyPixelPerfect,
int& x, int& y,
int& width, int& height)
{
int displayIndex = 0;
@@ -1381,21 +1382,35 @@ void Session::getWindowDimensions(int& x, int& y,
SDL_zero(usableBounds);
}
QString videoDriver(SDL_GetCurrentVideoDriver());
if (videoDriver != "cocoa" && videoDriver != "wayland") {
// The window size is in physical pixels, so we must adjust for DPI scaling ourselves
float ddpi;
if (SDL_GetDisplayDPI(displayIndex, &ddpi, nullptr, nullptr) == 0) {
// Don't allow scaling below 50% to avoid bogus DPI EDID data
float scale = SDL_max(ddpi, 0.48f) / 96.0f;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "DPI scaling factor: %.02f", scale);
desiredWidth = (int)SDL_roundf(desiredWidth * scale) & ~0x1; // even width
desiredHeight = (int)SDL_roundf(desiredHeight * scale) & ~0x1; // even height
}
}
// Use the usable display bounds if they are valid
if (usableBounds.w != 0 &&
WMUtils::hasDisplayUsableBounds() &&
m_StreamConfig.width <= ((int)(usableBounds.w * 0.95f)) &&
m_StreamConfig.height <= ((int)(usableBounds.h * 0.95f))) {
desiredWidth <= ((int)(usableBounds.w * 0.95f)) &&
desiredHeight <= ((int)(usableBounds.h * 0.95f))) {
// We fit pixel-perfect in the usable area, so don't scale at all
width = m_StreamConfig.width;
height = m_StreamConfig.height;
width = desiredWidth;
height = desiredHeight;
}
else if (usableBounds.w != 0 || SDL_GetDisplayBounds(displayIndex, &usableBounds) == 0) {
else if (!onlyPixelPerfect && (usableBounds.w != 0 || SDL_GetDisplayBounds(displayIndex, &usableBounds) == 0)) {
// Otherwise, use 80% of usable bounds and preserve aspect ratio
SDL_Rect src, dst;
src.x = src.y = dst.x = dst.y = 0;
src.w = m_StreamConfig.width;
src.h = m_StreamConfig.height;
src.w = desiredWidth;
src.h = desiredHeight;
dst.w = ((int)(usableBounds.w * 0.80f)) & ~0x1; // even width
dst.h = ((int)(usableBounds.h * 0.80f)) & ~0x1; // even height
@@ -1403,19 +1418,19 @@ void Session::getWindowDimensions(int& x, int& y,
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
// Only scale the size down, never up
if (m_StreamConfig.width >= dst.w || m_StreamConfig.height >= dst.h) {
if (desiredWidth >= dst.w || desiredHeight >= dst.h) {
width = dst.w;
height = dst.h;
}
else {
width = m_StreamConfig.width;
height = m_StreamConfig.height;
width = desiredWidth;
height = desiredHeight;
}
}
else {
// Oh well, hope for the best
width = m_StreamConfig.width;
height = m_StreamConfig.height;
width = desiredWidth;
height = desiredHeight;
}
x = y = SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex);
@@ -1559,7 +1574,7 @@ void Session::toggleFullscreen()
// around the issue by manually resetting the position and size here.
if (!fullScreen && m_FullScreenFlag == SDL_WINDOW_FULLSCREEN) {
int x, y, width, height;
getWindowDimensions(x, y, width, height);
getWindowDimensions(m_StreamConfig.width, m_StreamConfig.height, false, x, y, width, height);
SDL_SetWindowSize(m_Window, width, height);
SDL_SetWindowPosition(m_Window, x, y);
}
@@ -1824,7 +1839,8 @@ void Session::exec()
QCoreApplication::sendPostedEvents();
int x, y, width, height;
getWindowDimensions(x, y, width, height);
getWindowDimensions(m_StreamConfig.width, m_StreamConfig.height, false, x, y, width, height);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Initial window dimensions: %dx%d", width, height);
#ifdef STEAM_LINK
// We need a little delay before creating the window or we will trigger some kind