Skip to content

Commit

Permalink
Support PMv2 DPI awareness, add SDL_HINT_WINDOWS_DPI_AWARENESS
Browse files Browse the repository at this point in the history
The hint allows setting a specific DPI awareness ("unaware", "system", "permonitor", "permonitorv2").

This is the first part of High-DPI support on Windows ( #2119 ).
It doesn't implement a virtualized SDL coordinate system, which will be
addressed in a later commit. (This hint could be useful for SDL apps
that want 1 SDL unit = 1 pixel, though.)

Detecting and behaving correctly under per-monitor V2
(calling AdjustWindowRectExForDpi where needed) should fix the
following issues:

#3286
#4712
  • Loading branch information
ericwa authored and slouken committed Jun 11, 2022
1 parent 81d3add commit 51ebefe
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 8 deletions.
30 changes: 30 additions & 0 deletions include/SDL_hints.h
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,36 @@ extern "C" {
*/
#define SDL_HINT_WINDOWS_USE_D3D9EX "SDL_WINDOWS_USE_D3D9EX"

/**
* \brief Controls whether SDL will declare the process to be DPI aware.
*
* This hint must be set before initializing the video subsystem.
*
* The main purpose of declaring DPI awareness is to disable OS bitmap scaling of SDL windows on monitors with
* a DPI scale factor.
*
* This hint is equivalent to requesting DPI awareness via external means (e.g. calling SetProcessDpiAwarenessContext)
* and does not cause SDL to use a virtualized coordinate system, so it will generally give you 1 SDL coordinate = 1 pixel
* even on high-DPI displays.
*
* For more information, see:
* https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows
*
* This variable can be set to the following values:
* "" - Do not change the DPI awareness (default).
* "unaware" - Declare the process as DPI unaware. (Windows 8.1 and later).
* "system" - Request system DPI awareness. (Vista and later).
* "permonitor" - Request per-monitor DPI awareness. (Windows 8.1 and later).
* "permonitorv2" - Request per-monitor V2 DPI awareness. (Windows 10, version 1607 and later).
* The most visible difference from "permonitor" is that window title bar will be scaled
* to the visually correct size when dragging between monitors with different scale factors.
* This is the preferred DPI awareness level.
*
* If the requested DPI awareness is not available on the currently running OS, SDL will try to request the best
* available match.
*/
#define SDL_HINT_WINDOWS_DPI_AWARENESS "SDL_WINDOWS_DPI_AWARENESS"

/**
* \brief A variable controlling whether the window frame and title bar are interactive when the cursor is hidden
*
Expand Down
136 changes: 135 additions & 1 deletion src/video/windows/SDL_windowsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "../../events/SDL_touch_c.h"
#include "../../events/scancodes_windows.h"
#include "SDL_hints.h"
#include "SDL_log.h"

/* Dropfile support */
#include <shellapi.h>
Expand All @@ -52,6 +53,8 @@
#include "wmmsg.h"
#endif

/* #define HIGHDPI_DEBUG */

/* Masks for processing the windows KEYDOWN and KEYUP messages */
#define REPEATED_KEYMASK (1<<30)
#define EXTENDED_KEYMASK (1<<24)
Expand Down Expand Up @@ -86,6 +89,12 @@
#ifndef WM_UNICHAR
#define WM_UNICHAR 0x0109
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
#ifndef WM_GETDPISCALEDSIZE
#define WM_GETDPISCALEDSIZE 0x02E4
#endif

#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(x) (((x) >= 0xd800) && ((x) <= 0xdbff))
Expand Down Expand Up @@ -1084,7 +1093,12 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
size.bottom = h;
size.right = w;

AdjustWindowRectEx(&size, style, menu, 0);
if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
UINT dpi = data->videodata->GetDpiForWindow(hwnd);
data->videodata->AdjustWindowRectExForDpi(&size, style, menu, 0, dpi);
} else {
AdjustWindowRectEx(&size, style, menu, 0);
}
w = size.right - size.left;
h = size.bottom - size.top;
}
Expand Down Expand Up @@ -1154,6 +1168,12 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
h = rect.bottom - rect.top;
SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w, h);

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_WINDOWPOSCHANGED: Windows client rect (pixels): (%d, %d) (%d x %d)\tSDL client rect: (%d, %d) (%d x %d)\tGetDpiForWindow: %d",
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
x, y, w, h, data->videodata->GetDpiForWindow ? (int)data->videodata->GetDpiForWindow(data->hwnd) : 0);
#endif

/* Forces a WM_PAINT event */
InvalidateRect(hwnd, NULL, FALSE);

Expand Down Expand Up @@ -1399,6 +1419,120 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
}
break;

case WM_GETDPISCALEDSIZE:
/* Windows 10 Creators Update+ */
/* Documented as only being sent to windows that are per-monitor V2 DPI aware. */
if (data->videodata->GetDpiForWindow && data->videodata->AdjustWindowRectExForDpi) {
/* Windows expects applications to scale their window rects linearly
when dragging between monitors with different DPI's.
e.g. a 100x100 window dragged to a 200% scaled monitor
becomes 200x200.
For SDL, we instead want the client size to scale linearly.
This is not the same as the window rect scaling linearly,
because Windows doesn't scale the non-client area (titlebar etc.)
linearly. So, we need to handle this message to request custom
scaling. */

const int nextDPI = (int)wParam;
const int prevDPI = (int)data->videodata->GetDpiForWindow(hwnd);
SIZE *sizeInOut = (SIZE *)lParam;

int frame_w, frame_h;
int query_client_w_win, query_client_h_win;

const DWORD style = GetWindowLong(hwnd, GWL_STYLE);
const BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_GETDPISCALEDSIZE: current DPI: %d potential DPI: %d input size: (%dx%d)",
prevDPI, nextDPI, sizeInOut->cx, sizeInOut->cy);
#endif

/* Subtract the window frame size that would have been used at prevDPI */
{
RECT rect = {0};

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
data->videodata->AdjustWindowRectExForDpi(&rect, style, menu, 0, prevDPI);
}

frame_w = -rect.left + rect.right;
frame_h = -rect.top + rect.bottom;

query_client_w_win = sizeInOut->cx - frame_w;
query_client_h_win = sizeInOut->cy - frame_h;
}

/* Add the window frame size that would be used at nextDPI */
{
RECT rect = { 0, 0, query_client_w_win, query_client_h_win };

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
data->videodata->AdjustWindowRectExForDpi(&rect, style, menu, 0, nextDPI);
}

/* This is supposed to control the suggested rect param of WM_DPICHANGED */
sizeInOut->cx = rect.right - rect.left;
sizeInOut->cy = rect.bottom - rect.top;
}

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_GETDPISCALEDSIZE: output size: (%dx%d)", sizeInOut->cx, sizeInOut->cy);
#endif
return TRUE;
}
break;

case WM_DPICHANGED:
/* Windows 8.1+ */
{
const int newDPI = HIWORD(wParam);
RECT* const suggestedRect = (RECT*)lParam;
int w, h;

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: to %d\tsuggested rect: (%d, %d), (%dx%d)\n", newDPI,
suggestedRect->left, suggestedRect->top, suggestedRect->right - suggestedRect->left, suggestedRect->bottom - suggestedRect->top);
#endif

/* DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 means that
WM_GETDPISCALEDSIZE will have been called, so we can use suggestedRect. */
if (WIN_IsPerMonitorV2DPIAware(SDL_GetVideoDevice())) {
w = suggestedRect->right - suggestedRect->left;
h = suggestedRect->bottom - suggestedRect->top;
} else {
RECT rect = { 0, 0, data->window->w, data->window->h };
const DWORD style = GetWindowLong(hwnd, GWL_STYLE);
const BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);

if (!(data->window->flags & SDL_WINDOW_BORDERLESS)) {
AdjustWindowRectEx(&rect, style, menu, 0);
}

w = rect.right - rect.left;
h = rect.bottom - rect.top;
}

#ifdef HIGHDPI_DEBUG
SDL_Log("WM_DPICHANGED: current SDL window size: (%dx%d)\tcalling SetWindowPos: (%d, %d), (%dx%d)\n",
data->window->w, data->window->h,
suggestedRect->left, suggestedRect->top, w, h);
#endif

data->expected_resize = SDL_TRUE;
SetWindowPos(hwnd,
NULL,
suggestedRect->left,
suggestedRect->top,
w,
h,
SWP_NOZORDER | SWP_NOACTIVATE);
data->expected_resize = SDL_FALSE;
return 0;
}
break;
}

/* If there's a window proc, assume it's going to handle messages */
Expand Down
116 changes: 116 additions & 0 deletions src/video/windows/SDL_windowsvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,24 @@ WIN_CreateDevice(int devindex)
data->CloseTouchInputHandle = (BOOL (WINAPI *)(HTOUCHINPUT)) SDL_LoadFunction(data->userDLL, "CloseTouchInputHandle");
data->GetTouchInputInfo = (BOOL (WINAPI *)(HTOUCHINPUT, UINT, PTOUCHINPUT, int)) SDL_LoadFunction(data->userDLL, "GetTouchInputInfo");
data->RegisterTouchWindow = (BOOL (WINAPI *)(HWND, ULONG)) SDL_LoadFunction(data->userDLL, "RegisterTouchWindow");
data->SetProcessDPIAware = (BOOL (WINAPI *)(void)) SDL_LoadFunction(data->userDLL, "SetProcessDPIAware");
data->SetProcessDpiAwarenessContext = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT)) SDL_LoadFunction(data->userDLL, "SetProcessDpiAwarenessContext");
data->SetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT)) SDL_LoadFunction(data->userDLL, "SetThreadDpiAwarenessContext");
data->GetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(void)) SDL_LoadFunction(data->userDLL, "GetThreadDpiAwarenessContext");
data->GetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT)) SDL_LoadFunction(data->userDLL, "GetAwarenessFromDpiAwarenessContext");
data->EnableNonClientDpiScaling = (BOOL (WINAPI *)(HWND)) SDL_LoadFunction(data->userDLL, "EnableNonClientDpiScaling");
data->AdjustWindowRectExForDpi = (BOOL (WINAPI *)(LPRECT, DWORD, BOOL, DWORD, UINT)) SDL_LoadFunction(data->userDLL, "AdjustWindowRectExForDpi");
data->GetDpiForWindow = (UINT (WINAPI *)(HWND)) SDL_LoadFunction(data->userDLL, "GetDpiForWindow");
data->AreDpiAwarenessContextsEqual = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT, DPI_AWARENESS_CONTEXT)) SDL_LoadFunction(data->userDLL, "AreDpiAwarenessContextsEqual");
data->IsValidDpiAwarenessContext = (BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT)) SDL_LoadFunction(data->userDLL, "IsValidDpiAwarenessContext");
} else {
SDL_ClearError();
}

data->shcoreDLL = SDL_LoadObject("SHCORE.DLL");
if (data->shcoreDLL) {
data->GetDpiForMonitor = (HRESULT (WINAPI *)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *)) SDL_LoadFunction(data->shcoreDLL, "GetDpiForMonitor");
data->SetProcessDpiAwareness = (HRESULT (WINAPI *)(PROCESS_DPI_AWARENESS)) SDL_LoadFunction(data->shcoreDLL, "SetProcessDpiAwareness");
} else {
SDL_ClearError();
}
Expand Down Expand Up @@ -233,11 +244,103 @@ VideoBootStrap WINDOWS_bootstrap = {
"windows", "SDL Windows video driver", WIN_CreateDevice
};

static BOOL
WIN_DeclareDPIAwareUnaware(_THIS)
{
SDL_VideoData* data = (SDL_VideoData*)_this->driverdata;

if (data->SetProcessDpiAwarenessContext) {
return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
} else if (data->SetProcessDpiAwareness) {
/* Windows 8.1 */
return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_DPI_UNAWARE));
}
return FALSE;
}

static BOOL
WIN_DeclareDPIAwareSystem(_THIS)
{
SDL_VideoData* data = (SDL_VideoData*)_this->driverdata;

if (data->SetProcessDpiAwarenessContext) {
/* Windows 10, version 1607 */
return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
} else if (data->SetProcessDpiAwareness) {
/* Windows 8.1 */
return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE));
} else if (data->SetProcessDPIAware) {
/* Windows Vista */
return data->SetProcessDPIAware();
}
return FALSE;
}

static BOOL
WIN_DeclareDPIAwarePerMonitor(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

if (data->SetProcessDpiAwarenessContext) {
/* Windows 10, version 1607 */
return data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
} else if (data->SetProcessDpiAwareness) {
/* Windows 8.1 */
return SUCCEEDED(data->SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE));
} else {
/* Older OS: fall back to system DPI aware */
return WIN_DeclareDPIAwareSystem(_this);
}
return FALSE;
}

static BOOL
WIN_DeclareDPIAwarePerMonitorV2(_THIS)
{
SDL_VideoData* data = (SDL_VideoData*)_this->driverdata;

/* Declare DPI aware(may have been done in external code or a manifest, as well) */
if (data->SetProcessDpiAwarenessContext) {
/* Windows 10, version 1607 */

/* NOTE: SetThreadDpiAwarenessContext doesn't work here with OpenGL - the OpenGL contents
end up still getting OS scaled. (tested on Windows 10 21H1 19043.1348, NVIDIA 496.49) */
if (data->SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) {
return TRUE;
} else {
return WIN_DeclareDPIAwarePerMonitor(_this);
}
} else {
/* Older OS: fall back to per-monitor (or system) */
return WIN_DeclareDPIAwarePerMonitor(_this);
}
}

static void
WIN_InitDPIAwareness(_THIS)
{
const char* hint = SDL_GetHint(SDL_HINT_WINDOWS_DPI_AWARENESS);

if (hint != NULL) {
if (SDL_strcmp(hint, "permonitorv2") == 0) {
WIN_DeclareDPIAwarePerMonitorV2(_this);
} else if (SDL_strcmp(hint, "permonitor") == 0) {
WIN_DeclareDPIAwarePerMonitor(_this);
} else if (SDL_strcmp(hint, "system") == 0) {
WIN_DeclareDPIAwareSystem(_this);
} else if (SDL_strcmp(hint, "unaware") == 0) {
WIN_DeclareDPIAwareUnaware(_this);
}
}
}

int
WIN_VideoInit(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

WIN_InitDPIAwareness(_this);

if (WIN_InitModes(_this) < 0) {
return -1;
}
Expand Down Expand Up @@ -473,6 +576,19 @@ SDL_DXGIGetOutputInfo(int displayIndex, int *adapterIndex, int *outputIndex)
#endif
}

SDL_bool
WIN_IsPerMonitorV2DPIAware(_THIS)
{
SDL_VideoData* data = (SDL_VideoData*) _this->driverdata;

if (data->AreDpiAwarenessContextsEqual && data->GetThreadDpiAwarenessContext) {
/* Windows 10, version 1607 */
return (SDL_bool)data->AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
data->GetThreadDpiAwarenessContext());
}
return SDL_FALSE;
}

#endif /* SDL_VIDEO_DRIVER_WINDOWS */

/* vim: set ts=4 sw=4 expandtab: */
Loading

0 comments on commit 51ebefe

Please sign in to comment.