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; m_StreamConfig.height = m_Preferences->height;
int x, y, width, 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 // Create a hidden window to use for decoder initialization tests
SDL_Window* testWindow = StreamUtils::createTestWindow(); SDL_Window* testWindow = StreamUtils::createTestWindow();
@@ -1324,7 +1324,8 @@ private:
Session* m_Session; 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& width, int& height)
{ {
int displayIndex = 0; int displayIndex = 0;
@@ -1381,21 +1382,35 @@ void Session::getWindowDimensions(int& x, int& y,
SDL_zero(usableBounds); 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 // Use the usable display bounds if they are valid
if (usableBounds.w != 0 && if (usableBounds.w != 0 &&
WMUtils::hasDisplayUsableBounds() && WMUtils::hasDisplayUsableBounds() &&
m_StreamConfig.width <= ((int)(usableBounds.w * 0.95f)) && desiredWidth <= ((int)(usableBounds.w * 0.95f)) &&
m_StreamConfig.height <= ((int)(usableBounds.h * 0.95f))) { desiredHeight <= ((int)(usableBounds.h * 0.95f))) {
// We fit pixel-perfect in the usable area, so don't scale at all // We fit pixel-perfect in the usable area, so don't scale at all
width = m_StreamConfig.width; width = desiredWidth;
height = m_StreamConfig.height; 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 // Otherwise, use 80% of usable bounds and preserve aspect ratio
SDL_Rect src, dst; SDL_Rect src, dst;
src.x = src.y = dst.x = dst.y = 0; src.x = src.y = dst.x = dst.y = 0;
src.w = m_StreamConfig.width; src.w = desiredWidth;
src.h = m_StreamConfig.height; src.h = desiredHeight;
dst.w = ((int)(usableBounds.w * 0.80f)) & ~0x1; // even width dst.w = ((int)(usableBounds.w * 0.80f)) & ~0x1; // even width
dst.h = ((int)(usableBounds.h * 0.80f)) & ~0x1; // even height 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); StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
// Only scale the size down, never up // 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; width = dst.w;
height = dst.h; height = dst.h;
} }
else { else {
width = m_StreamConfig.width; width = desiredWidth;
height = m_StreamConfig.height; height = desiredHeight;
} }
} }
else { else {
// Oh well, hope for the best // Oh well, hope for the best
width = m_StreamConfig.width; width = desiredWidth;
height = m_StreamConfig.height; height = desiredHeight;
} }
x = y = SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex); 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. // around the issue by manually resetting the position and size here.
if (!fullScreen && m_FullScreenFlag == SDL_WINDOW_FULLSCREEN) { if (!fullScreen && m_FullScreenFlag == SDL_WINDOW_FULLSCREEN) {
int x, y, width, height; 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_SetWindowSize(m_Window, width, height);
SDL_SetWindowPosition(m_Window, x, y); SDL_SetWindowPosition(m_Window, x, y);
} }
@@ -1824,7 +1839,8 @@ void Session::exec()
QCoreApplication::sendPostedEvents(); QCoreApplication::sendPostedEvents();
int x, y, width, height; 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 #ifdef STEAM_LINK
// We need a little delay before creating the window or we will trigger some kind // We need a little delay before creating the window or we will trigger some kind
+2 -1
View File
@@ -163,7 +163,8 @@ private:
int getAudioRendererCapabilities(int audioConfiguration); int getAudioRendererCapabilities(int audioConfiguration);
void getWindowDimensions(int& x, int& y, void getWindowDimensions(int desiredWidth, int desiredHeight, bool onlyPixelPerfect,
int& x, int& y,
int& width, int& height); int& width, int& height);
void toggleFullscreen(); void toggleFullscreen();