Fixed bug 3714 - Windows: SDL_WINDOW_FULLSCREEN_DESKTOP broken on 3 monitor setup w/ DPI scaling
authorSam Lantinga
Fri, 11 Aug 2017 10:18:45 -0700
changeset 112180060bcf673e8
parent 11217 d9c3f934bd0e
child 11219 f94279990934
Fixed bug 3714 - Windows: SDL_WINDOW_FULLSCREEN_DESKTOP broken on 3 monitor setup w/ DPI scaling

Eric Wasylishen 2017-07-26 18:42:58 UTC
I set up an (admittedly exotic) 3-monitor setup, and when I enter fullscreen-desktop on the middle display (#2), the SDL window is off center. (covers half of monitor #2 and most of monitor #3).

The displays are arranged from left to right:

Display #1 (main): 2880x1800, 200% scaling
Display #2: 1920x1200, 150% scaling
Display #3: 1920x1080, 100% scaling

SDL display bounds:
INFO: Bounds: 1440x900 at 0,0
INFO: Bounds: 1281x801 at 1921,0 (these are incorrect)
INFO: Bounds: 1920x1080 at 4800,0

Correct bounds reported by calling EnumDisplayMonitors and printing the LPRECT param of the callback:
1440x900 at (0, 0)
1280x800 at (2880, 0)
1920x1080 at (4800, 0)

It seems like you need 3 displays to reproduce this, and the left two need DPI scaling, and the 3rd display needs to have a different scale factor than the others.

Related: https://bugzilla.libsdl.org/show_bug.cgi?id=3709

SDL: current hg (11235:43c7baa53681)
Windows 10, Version 10.0.15063 Build 15063
Tested with testdraw2 and testgl2, and pressing alt+enter to enter fullscreen desktop.

This patch reworks SDL_windowsmodes.c to use EnumDisplayMonitors instead of EnumDisplayDevices, so we always have an HMONITOR for each SDL display.

With access to an HMONITOR, we can get the monitor bounds in virtual screen coordinates the proper way, by calling GetMonitorInfo. (whereas the original code was doing some calculations - e.g. "data->DeviceMode.dmPosition.x * data->ScaleX" - to try to get virtual screen coordinates. These worked in simple cases, but failed in more complex cases like this bug)

The one potential problem with my patch is, the ChangeDisplaySettingsEx docs say that you're supposed to get the display name from EnumDisplayDevices, but I'm getting the display name from GetMonitorInfo now.
src/video/windows/SDL_windowsmodes.c
src/video/windows/SDL_windowsmodes.h
     1.1 --- a/src/video/windows/SDL_windowsmodes.c	Fri Aug 11 10:05:45 2017 -0700
     1.2 +++ b/src/video/windows/SDL_windowsmodes.c	Fri Aug 11 10:18:45 2017 -0700
     1.3 @@ -24,51 +24,14 @@
     1.4  
     1.5  #include "SDL_windowsvideo.h"
     1.6  #include "../../../include/SDL_assert.h"
     1.7 +#include "../../../include/SDL_log.h"
     1.8  
     1.9  /* Windows CE compatibility */
    1.10  #ifndef CDS_FULLSCREEN
    1.11  #define CDS_FULLSCREEN 0
    1.12  #endif
    1.13  
    1.14 -typedef struct _WIN_GetMonitorDPIData {
    1.15 -    SDL_VideoData *vid_data;
    1.16 -    SDL_DisplayMode *mode;
    1.17 -    SDL_DisplayModeData *mode_data;
    1.18 -} WIN_GetMonitorDPIData;
    1.19 -
    1.20 -static BOOL CALLBACK
    1.21 -WIN_GetMonitorDPI(HMONITOR hMonitor,
    1.22 -                  HDC      hdcMonitor,
    1.23 -                  LPRECT   lprcMonitor,
    1.24 -                  LPARAM   dwData)
    1.25 -{
    1.26 -    WIN_GetMonitorDPIData *data = (WIN_GetMonitorDPIData*) dwData;
    1.27 -    UINT hdpi, vdpi;
    1.28 -
    1.29 -    if (data->vid_data->GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &hdpi, &vdpi) == S_OK &&
    1.30 -        hdpi > 0 &&
    1.31 -        vdpi > 0) {
    1.32 -        float hsize, vsize;
    1.33 -        
    1.34 -        data->mode_data->HorzDPI = (float)hdpi;
    1.35 -        data->mode_data->VertDPI = (float)vdpi;
    1.36 -
    1.37 -        // Figure out the monitor size and compute the diagonal DPI.
    1.38 -        hsize = data->mode->w / data->mode_data->HorzDPI;
    1.39 -        vsize = data->mode->h / data->mode_data->VertDPI;
    1.40 -        
    1.41 -        data->mode_data->DiagDPI = SDL_ComputeDiagonalDPI( data->mode->w,
    1.42 -                                                           data->mode->h,
    1.43 -                                                           hsize,
    1.44 -                                                           vsize );
    1.45 -
    1.46 -        // We can only handle one DPI per display mode so end the enumeration.
    1.47 -        return FALSE;
    1.48 -    }
    1.49 -
    1.50 -    // We didn't get DPI information so keep going.
    1.51 -    return TRUE;
    1.52 -}
    1.53 +/* #define DEBUG_MODES */
    1.54  
    1.55  static void
    1.56  WIN_UpdateDisplayMode(_THIS, LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
    1.57 @@ -89,39 +52,8 @@
    1.58          int logical_width = GetDeviceCaps( hdc, HORZRES );
    1.59          int logical_height = GetDeviceCaps( hdc, VERTRES );
    1.60  
    1.61 -        data->ScaleX = (float)logical_width / data->DeviceMode.dmPelsWidth;
    1.62 -        data->ScaleY = (float)logical_height / data->DeviceMode.dmPelsHeight;
    1.63          mode->w = logical_width;
    1.64          mode->h = logical_height;
    1.65 -
    1.66 -        // WIN_GetMonitorDPI needs mode->w and mode->h
    1.67 -        // so only call after those are set.
    1.68 -        if (vid_data->GetDpiForMonitor) {
    1.69 -            WIN_GetMonitorDPIData dpi_data;
    1.70 -            RECT monitor_rect;
    1.71 -
    1.72 -            dpi_data.vid_data = vid_data;
    1.73 -            dpi_data.mode = mode;
    1.74 -            dpi_data.mode_data = data;
    1.75 -            monitor_rect.left = data->DeviceMode.dmPosition.x;
    1.76 -            monitor_rect.top = data->DeviceMode.dmPosition.y;
    1.77 -            monitor_rect.right = monitor_rect.left + 1;
    1.78 -            monitor_rect.bottom = monitor_rect.top + 1;
    1.79 -            EnumDisplayMonitors(NULL, &monitor_rect, WIN_GetMonitorDPI, (LPARAM)&dpi_data);
    1.80 -        } else {
    1.81 -            // We don't have the Windows 8.1 routine so just
    1.82 -            // get system DPI.
    1.83 -            data->HorzDPI = (float)GetDeviceCaps( hdc, LOGPIXELSX );
    1.84 -            data->VertDPI = (float)GetDeviceCaps( hdc, LOGPIXELSY );
    1.85 -            if (data->HorzDPI == data->VertDPI) {
    1.86 -                data->DiagDPI = data->HorzDPI;
    1.87 -            } else {
    1.88 -                data->DiagDPI = SDL_ComputeDiagonalDPI( mode->w,
    1.89 -                                                        mode->h,
    1.90 -                                                        (float)GetDeviceCaps( hdc, HORZSIZE ) / 25.4f,
    1.91 -                                                        (float)GetDeviceCaps( hdc, VERTSIZE ) / 25.4f );
    1.92 -            }
    1.93 -        }
    1.94          
    1.95          SDL_zero(bmi_data);
    1.96          bmi = (LPBITMAPINFO) bmi_data;
    1.97 @@ -199,13 +131,6 @@
    1.98      mode->driverdata = data;
    1.99      data->DeviceMode = devmode;
   1.100  
   1.101 -    /* Default basic information */
   1.102 -    data->ScaleX = 1.0f;
   1.103 -    data->ScaleY = 1.0f;
   1.104 -    data->DiagDPI = 0.0f;
   1.105 -    data->HorzDPI = 0.0f;
   1.106 -    data->VertDPI = 0.0f;
   1.107 -
   1.108      mode->format = SDL_PIXELFORMAT_UNKNOWN;
   1.109      mode->w = data->DeviceMode.dmPelsWidth;
   1.110      mode->h = data->DeviceMode.dmPelsHeight;
   1.111 @@ -217,7 +142,7 @@
   1.112  }
   1.113  
   1.114  static SDL_bool
   1.115 -WIN_AddDisplay(_THIS, LPTSTR DeviceName)
   1.116 +WIN_AddDisplay(_THIS, HMONITOR hMonitor, const MONITORINFOEX *info)
   1.117  {
   1.118      SDL_VideoDisplay display;
   1.119      SDL_DisplayData *displaydata;
   1.120 @@ -225,9 +150,10 @@
   1.121      DISPLAY_DEVICE device;
   1.122  
   1.123  #ifdef DEBUG_MODES
   1.124 -    printf("Display: %s\n", WIN_StringToUTF8(DeviceName));
   1.125 +    SDL_Log("Display: %s\n", WIN_StringToUTF8(info->szDevice));
   1.126  #endif
   1.127 -    if (!WIN_GetDisplayMode(_this, DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
   1.128 +
   1.129 +    if (!WIN_GetDisplayMode(_this, info->szDevice, ENUM_CURRENT_SETTINGS, &mode)) {
   1.130          return SDL_FALSE;
   1.131      }
   1.132  
   1.133 @@ -235,12 +161,13 @@
   1.134      if (!displaydata) {
   1.135          return SDL_FALSE;
   1.136      }
   1.137 -    SDL_memcpy(displaydata->DeviceName, DeviceName,
   1.138 +    SDL_memcpy(displaydata->DeviceName, info->szDevice,
   1.139                 sizeof(displaydata->DeviceName));
   1.140 +    displaydata->MonitorHandle = hMonitor;
   1.141  
   1.142      SDL_zero(display);
   1.143      device.cb = sizeof(device);
   1.144 -    if (EnumDisplayDevices(DeviceName, 0, &device, 0)) {
   1.145 +    if (EnumDisplayDevices(info->szDevice, 0, &device, 0)) {
   1.146          display.name = WIN_StringToUTF8(device.DeviceString);
   1.147      }
   1.148      display.desktop_mode = mode;
   1.149 @@ -251,63 +178,53 @@
   1.150      return SDL_TRUE;
   1.151  }
   1.152  
   1.153 +typedef struct _WIN_AddDisplaysData {
   1.154 +    SDL_VideoDevice *video_device;
   1.155 +    SDL_bool want_primary;
   1.156 +} WIN_AddDisplaysData;
   1.157 +
   1.158 +static BOOL CALLBACK
   1.159 +WIN_AddDisplaysCallback(HMONITOR hMonitor,
   1.160 +                        HDC      hdcMonitor,
   1.161 +                        LPRECT   lprcMonitor,
   1.162 +                        LPARAM   dwData)
   1.163 +{
   1.164 +    WIN_AddDisplaysData *data = (WIN_AddDisplaysData*)dwData;
   1.165 +    MONITORINFOEX info;
   1.166 +
   1.167 +    SDL_zero(info);
   1.168 +    info.cbSize = sizeof(info);
   1.169 +
   1.170 +    if (GetMonitorInfo(hMonitor, (LPMONITORINFO)&info) != 0) {
   1.171 +        const SDL_bool is_primary = ((info.dwFlags & MONITORINFOF_PRIMARY) == MONITORINFOF_PRIMARY);
   1.172 +
   1.173 +        if (is_primary == data->want_primary) {
   1.174 +            WIN_AddDisplay(data->video_device, hMonitor, &info);
   1.175 +        }
   1.176 +    }
   1.177 +
   1.178 +    // continue enumeration
   1.179 +    return TRUE;
   1.180 +}
   1.181 +
   1.182 +static void
   1.183 +WIN_AddDisplays(_THIS)
   1.184 +{
   1.185 +    WIN_AddDisplaysData callback_data;
   1.186 +    callback_data.video_device = _this;
   1.187 +
   1.188 +    callback_data.want_primary = SDL_TRUE;
   1.189 +    EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
   1.190 +
   1.191 +    callback_data.want_primary = SDL_FALSE;
   1.192 +    EnumDisplayMonitors(NULL, NULL, WIN_AddDisplaysCallback, (LPARAM)&callback_data);
   1.193 +}
   1.194 +
   1.195  int
   1.196  WIN_InitModes(_THIS)
   1.197  {
   1.198 -    int pass;
   1.199 -    DWORD i, j, count;
   1.200 -    DISPLAY_DEVICE device;
   1.201 -
   1.202 -    device.cb = sizeof(device);
   1.203 -
   1.204 -    /* Get the primary display in the first pass */
   1.205 -    for (pass = 0; pass < 2; ++pass) {
   1.206 -        for (i = 0; ; ++i) {
   1.207 -            TCHAR DeviceName[32];
   1.208 +    WIN_AddDisplays(_this);
   1.209  
   1.210 -            if (!EnumDisplayDevices(NULL, i, &device, 0)) {
   1.211 -                break;
   1.212 -            }
   1.213 -            if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
   1.214 -                continue;
   1.215 -            }
   1.216 -            if (pass == 0) {
   1.217 -                if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
   1.218 -                    continue;
   1.219 -                }
   1.220 -            } else {
   1.221 -                if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
   1.222 -                    continue;
   1.223 -                }
   1.224 -            }
   1.225 -            SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
   1.226 -#ifdef DEBUG_MODES
   1.227 -            printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
   1.228 -#endif
   1.229 -            count = 0;
   1.230 -            for (j = 0; ; ++j) {
   1.231 -                if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
   1.232 -                    break;
   1.233 -                }
   1.234 -                if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
   1.235 -                    continue;
   1.236 -                }
   1.237 -                if (pass == 0) {
   1.238 -                    if (!(device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)) {
   1.239 -                        continue;
   1.240 -                    }
   1.241 -                } else {
   1.242 -                    if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
   1.243 -                        continue;
   1.244 -                    }
   1.245 -                }
   1.246 -                count += WIN_AddDisplay(_this, device.DeviceName);
   1.247 -            }
   1.248 -            if (count == 0) {
   1.249 -                WIN_AddDisplay(_this, DeviceName);
   1.250 -            }
   1.251 -        }
   1.252 -    }
   1.253      if (_this->num_displays == 0) {
   1.254          return SDL_SetError("No displays available");
   1.255      }
   1.256 @@ -317,67 +234,104 @@
   1.257  int
   1.258  WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
   1.259  {
   1.260 -    SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->current_mode.driverdata;
   1.261 +    const SDL_DisplayData *data = (const SDL_DisplayData *)display->driverdata;
   1.262 +    MONITORINFO minfo;
   1.263 +    BOOL rc;
   1.264 +
   1.265 +    SDL_zero(minfo);
   1.266 +    minfo.cbSize = sizeof(MONITORINFO);
   1.267 +    rc = GetMonitorInfo(data->MonitorHandle, &minfo);
   1.268  
   1.269 -    rect->x = (int)SDL_ceil(data->DeviceMode.dmPosition.x * data->ScaleX);
   1.270 -    rect->y = (int)SDL_ceil(data->DeviceMode.dmPosition.y * data->ScaleY);
   1.271 -    rect->w = (int)SDL_ceil(data->DeviceMode.dmPelsWidth * data->ScaleX);
   1.272 -    rect->h = (int)SDL_ceil(data->DeviceMode.dmPelsHeight * data->ScaleY);
   1.273 +    if (!rc) {
   1.274 +        return SDL_SetError("Couldn't find monitor data");
   1.275 +    }
   1.276 +
   1.277 +    rect->x = minfo.rcMonitor.left;
   1.278 +    rect->y = minfo.rcMonitor.top;
   1.279 +    rect->w = minfo.rcMonitor.right - minfo.rcMonitor.left;
   1.280 +    rect->h = minfo.rcMonitor.bottom - minfo.rcMonitor.top;
   1.281  
   1.282      return 0;
   1.283  }
   1.284  
   1.285  int
   1.286 -WIN_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
   1.287 +WIN_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi_out, float * hdpi_out, float * vdpi_out)
   1.288  {
   1.289 -    SDL_DisplayModeData *data = (SDL_DisplayModeData *) display->current_mode.driverdata;
   1.290 +    const SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
   1.291 +    const SDL_VideoData *videodata = (SDL_VideoData *)display->device->driverdata;
   1.292 +    float hdpi = 0, vdpi = 0, ddpi = 0;
   1.293 +    
   1.294 +    if (videodata->GetDpiForMonitor) {
   1.295 +        UINT hdpi_uint, vdpi_uint;
   1.296 +        // Windows 8.1+ codepath
   1.297 +        if (videodata->GetDpiForMonitor(displaydata->MonitorHandle, MDT_EFFECTIVE_DPI, &hdpi_uint, &vdpi_uint) == S_OK) {
   1.298 +            // GetDpiForMonitor docs promise to return the same hdpi/vdpi
   1.299 +            hdpi = (float)hdpi_uint;
   1.300 +            vdpi = (float)hdpi_uint;
   1.301 +            ddpi = (float)hdpi_uint;
   1.302 +        } else {
   1.303 +            return SDL_SetError("GetDpiForMonitor failed");
   1.304 +        }
   1.305 +    } else {
   1.306 +        // Window 8.0 and below: same DPI for all monitors.
   1.307 +        HDC hdc;
   1.308 +        int hdpi_int, vdpi_int, hpoints, vpoints, hpix, vpix;
   1.309 +        float hinches, vinches;
   1.310  
   1.311 -    if (ddpi) {
   1.312 -        *ddpi = data->DiagDPI;
   1.313 -    }
   1.314 -    if (hdpi) {
   1.315 -        *hdpi = data->HorzDPI;
   1.316 -    }
   1.317 -    if (vdpi) {
   1.318 -        *vdpi = data->VertDPI;
   1.319 +        hdc = GetDC(NULL);
   1.320 +        if (hdc == NULL) {
   1.321 +            return SDL_SetError("GetDC failed");
   1.322 +        }
   1.323 +        hdpi_int = GetDeviceCaps(hdc, LOGPIXELSX);
   1.324 +        vdpi_int = GetDeviceCaps(hdc, LOGPIXELSY);
   1.325 +        ReleaseDC(NULL, hdc);
   1.326 +
   1.327 +        hpoints = GetSystemMetrics(SM_CXVIRTUALSCREEN);
   1.328 +        vpoints = GetSystemMetrics(SM_CYVIRTUALSCREEN);
   1.329 +
   1.330 +        hpix = MulDiv(hpoints, hdpi_int, 96);
   1.331 +        vpix = MulDiv(vpoints, vdpi_int, 96);
   1.332 +
   1.333 +        hinches = (float)hpoints / 96.0f;
   1.334 +        vinches = (float)vpoints / 96.0f;
   1.335 +
   1.336 +        hdpi = (float)hdpi_int;
   1.337 +        vdpi = (float)vdpi_int;
   1.338 +        ddpi = SDL_ComputeDiagonalDPI(hpix, vpix, hinches, vinches);
   1.339      }
   1.340  
   1.341 -    return data->DiagDPI != 0.0f ? 0 : SDL_SetError("Couldn't get DPI");
   1.342 +    if (ddpi_out) {
   1.343 +        *ddpi_out = ddpi;
   1.344 +    }
   1.345 +    if (hdpi_out) {
   1.346 +        *hdpi_out = hdpi;
   1.347 +    }
   1.348 +    if (vdpi_out) {
   1.349 +        *vdpi_out = vdpi;
   1.350 +    }
   1.351 +
   1.352 +    return ddpi != 0.0f ? 0 : SDL_SetError("Couldn't get DPI");
   1.353  }
   1.354  
   1.355  int
   1.356  WIN_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
   1.357  {
   1.358 -    const SDL_DisplayModeData *data = (const SDL_DisplayModeData *) display->current_mode.driverdata;
   1.359 -    const DEVMODE *pDevMode = &data->DeviceMode;
   1.360 -    POINT pt = {
   1.361 -        /* !!! FIXME: no scale, right? */
   1.362 -        (LONG) (pDevMode->dmPosition.x + (pDevMode->dmPelsWidth / 2)),
   1.363 -        (LONG) (pDevMode->dmPosition.y + (pDevMode->dmPelsHeight / 2))
   1.364 -    };
   1.365 -    HMONITOR hmon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
   1.366 +    const SDL_DisplayData *data = (const SDL_DisplayData *)display->driverdata;
   1.367      MONITORINFO minfo;
   1.368 -    const RECT *work;
   1.369 -    BOOL rc = FALSE;
   1.370 -
   1.371 -    SDL_assert(hmon != NULL);
   1.372 +    BOOL rc;
   1.373  
   1.374 -    if (hmon != NULL) {
   1.375 -        SDL_zero(minfo);
   1.376 -        minfo.cbSize = sizeof (MONITORINFO);
   1.377 -        rc = GetMonitorInfo(hmon, &minfo);
   1.378 -        SDL_assert(rc);
   1.379 -    }
   1.380 +    SDL_zero(minfo);
   1.381 +    minfo.cbSize = sizeof(MONITORINFO);
   1.382 +    rc = GetMonitorInfo(data->MonitorHandle, &minfo);
   1.383  
   1.384      if (!rc) {
   1.385          return SDL_SetError("Couldn't find monitor data");
   1.386      }
   1.387  
   1.388 -    work = &minfo.rcWork;
   1.389 -    rect->x = (int)SDL_ceil(work->left * data->ScaleX);
   1.390 -    rect->y = (int)SDL_ceil(work->top * data->ScaleY);
   1.391 -    rect->w = (int)SDL_ceil((work->right - work->left) * data->ScaleX);
   1.392 -    rect->h = (int)SDL_ceil((work->bottom - work->top) * data->ScaleY);
   1.393 +    rect->x = minfo.rcWork.left;
   1.394 +    rect->y = minfo.rcWork.top;
   1.395 +    rect->w = minfo.rcWork.right - minfo.rcWork.left;
   1.396 +    rect->h = minfo.rcWork.bottom - minfo.rcWork.top;
   1.397  
   1.398      return 0;
   1.399  }
     2.1 --- a/src/video/windows/SDL_windowsmodes.h	Fri Aug 11 10:05:45 2017 -0700
     2.2 +++ b/src/video/windows/SDL_windowsmodes.h	Fri Aug 11 10:18:45 2017 -0700
     2.3 @@ -26,16 +26,12 @@
     2.4  typedef struct
     2.5  {
     2.6      TCHAR DeviceName[32];
     2.7 +    HMONITOR MonitorHandle;
     2.8  } SDL_DisplayData;
     2.9  
    2.10  typedef struct
    2.11  {
    2.12      DEVMODE DeviceMode;
    2.13 -    float ScaleX;
    2.14 -    float ScaleY;
    2.15 -    float DiagDPI;
    2.16 -    float HorzDPI;
    2.17 -    float VertDPI;
    2.18  } SDL_DisplayModeData;
    2.19  
    2.20  extern int WIN_InitModes(_THIS);