Bug 1350643 - Part 6.1: Extract getting DPI logic from LogToPhysFactor & SystemScaleFactor to provide new functions MonitorDPI & SystemDPI. r=jimm

MozReview-Commit-ID: E9mXvFMM3pA

--HG--
extra : rebase_source : 1cc0f80c41054d6400d03b663f5b0037fbdb4cad
This commit is contained in:
Samael Wang 2017-06-16 01:11:41 +08:00
parent a4e6b1337a
commit b75d05055a
2 changed files with 36 additions and 18 deletions

View File

@ -549,27 +549,31 @@ WinUtils::Log(const char *fmt, ...)
}
// static
double
WinUtils::SystemScaleFactor()
float
WinUtils::SystemDPI()
{
// The result of GetDeviceCaps won't change dynamically, as it predates
// per-monitor DPI and support for on-the-fly resolution changes.
// Therefore, we only need to look it up once.
static double systemScale = 0;
if (systemScale == 0) {
static float dpi = 0;
if (dpi <= 0) {
HDC screenDC = GetDC(nullptr);
systemScale = GetDeviceCaps(screenDC, LOGPIXELSY) / 96.0;
dpi = GetDeviceCaps(screenDC, LOGPIXELSY);
ReleaseDC(nullptr, screenDC);
if (systemScale == 0) {
// Bug 1012487 - This can occur when the Screen DC is used off the
// main thread on windows. For now just assume a 100% DPI for this
// drawing call.
// XXX - fixme!
return 1.0;
}
}
return systemScale;
// Bug 1012487 - dpi can be 0 when the Screen DC is used off the
// main thread on windows. For now just assume a 100% DPI for this
// drawing call.
// XXX - fixme!
return dpi > 0 ? dpi : 96;
}
// static
double
WinUtils::SystemScaleFactor()
{
return SystemDPI() / 96.0;
}
#if WINVER < 0x603
@ -622,17 +626,25 @@ WinUtils::IsPerMonitorDPIAware()
}
/* static */
double
WinUtils::LogToPhysFactor(HMONITOR aMonitor)
float
WinUtils::MonitorDPI(HMONITOR aMonitor)
{
if (IsPerMonitorDPIAware()) {
UINT dpiX, dpiY = 96;
sGetDpiForMonitor(aMonitor ? aMonitor : GetPrimaryMonitor(),
MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
return dpiY / 96.0;
return dpiY;
}
return SystemScaleFactor();
// We're not per-monitor aware, use system DPI instead.
return SystemDPI();
}
/* static */
double
WinUtils::LogToPhysFactor(HMONITOR aMonitor)
{
return MonitorDPI(aMonitor) / 96.0;
}
/* static */

View File

@ -208,6 +208,12 @@ public:
static double SystemScaleFactor();
static bool IsPerMonitorDPIAware();
/**
* Get the DPI of the given monitor if it's per-monitor DPI aware, otherwise
* return the system DPI.
*/
static float MonitorDPI(HMONITOR aMonitor);
static float SystemDPI();
/**
* Functions to convert between logical pixels as used by most Windows APIs
* and physical (device) pixels.