Commit 01e20bb7 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

screen_win.cc cleanup part 7

Change function signatures.  This is mostly using Optional to
better convey "function returns a value or failure", but also uses a
struct in one spot to change outparams to multiple return values.

This change exposed what I think was previously a bug, where
EnumMonitorForDisplayInfoCallback() would use
GetDefaultMonitorPhysicalPixelsPerInch() if it could not get an
appropriate pointer device, but would use an empty vector if
GetPixelsPerInchForPointerDevice() failed.  Using Optional for its
helpers makes it easy to use the former in both cases, which seems
more correct.

Bug: none
Change-Id: Ie9df3da798e1c5b448b57963929955193a2a255e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2109262
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752684}
parent e54ab10e
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_functions.h"
#include "base/numerics/ranges.h" #include "base/numerics/ranges.h"
#include "base/optional.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/win/win_util.h" #include "base/win/win_util.h"
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
...@@ -40,9 +41,9 @@ namespace { ...@@ -40,9 +41,9 @@ namespace {
ScreenWin* g_instance = nullptr; ScreenWin* g_instance = nullptr;
// Gets the DPI for a particular monitor. // Gets the DPI for a particular monitor.
int GetPerMonitorDPI(HMONITOR monitor) { base::Optional<int> GetPerMonitorDPI(HMONITOR monitor) {
if (!base::win::IsProcessPerMonitorDpiAware()) if (!base::win::IsProcessPerMonitorDpiAware())
return 0; return base::nullopt;
static auto get_dpi_for_monitor_func = []() { static auto get_dpi_for_monitor_func = []() {
const HMODULE shcore_dll = ::LoadLibrary(L"shcore.dll"); const HMODULE shcore_dll = ::LoadLibrary(L"shcore.dll");
...@@ -54,7 +55,7 @@ int GetPerMonitorDPI(HMONITOR monitor) { ...@@ -54,7 +55,7 @@ int GetPerMonitorDPI(HMONITOR monitor) {
if (!get_dpi_for_monitor_func || if (!get_dpi_for_monitor_func ||
!SUCCEEDED( !SUCCEEDED(
get_dpi_for_monitor_func(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y))) get_dpi_for_monitor_func(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y)))
return 0; return base::nullopt;
DCHECK_EQ(dpi_x, dpi_y); DCHECK_EQ(dpi_x, dpi_y);
return int{dpi_x}; return int{dpi_x};
...@@ -77,8 +78,9 @@ float GetMonitorScaleFactor(HMONITOR monitor, ...@@ -77,8 +78,9 @@ float GetMonitorScaleFactor(HMONITOR monitor,
if (Display::HasForceDeviceScaleFactor()) if (Display::HasForceDeviceScaleFactor())
return Display::GetForcedDeviceScaleFactor(); return Display::GetForcedDeviceScaleFactor();
const int dpi = GetPerMonitorDPI(monitor); const auto dpi = GetPerMonitorDPI(monitor);
return dpi ? GetScaleFactorForDPI(dpi, include_accessibility) : GetDPIScale(); return dpi ? GetScaleFactorForDPI(dpi.value(), include_accessibility)
: GetDPIScale();
} }
std::vector<DISPLAYCONFIG_PATH_INFO> GetPathInfos() { std::vector<DISPLAYCONFIG_PATH_INFO> GetPathInfos() {
...@@ -102,12 +104,12 @@ std::vector<DISPLAYCONFIG_PATH_INFO> GetPathInfos() { ...@@ -102,12 +104,12 @@ std::vector<DISPLAYCONFIG_PATH_INFO> GetPathInfos() {
return {}; return {};
} }
bool GetPathInfo(HMONITOR monitor, DISPLAYCONFIG_PATH_INFO* path_info) { base::Optional<DISPLAYCONFIG_PATH_INFO> GetPathInfo(HMONITOR monitor) {
// Get the monitor name. // Get the monitor name.
MONITORINFOEX monitor_info = {}; MONITORINFOEX monitor_info = {};
monitor_info.cbSize = sizeof(monitor_info); monitor_info.cbSize = sizeof(monitor_info);
if (!GetMonitorInfo(monitor, &monitor_info)) if (!GetMonitorInfo(monitor, &monitor_info))
return false; return base::nullopt;
// Look for a path info with a matching name. // Look for a path info with a matching name.
std::vector<DISPLAYCONFIG_PATH_INFO> path_infos = GetPathInfos(); std::vector<DISPLAYCONFIG_PATH_INFO> path_infos = GetPathInfos();
...@@ -118,22 +120,19 @@ bool GetPathInfo(HMONITOR monitor, DISPLAYCONFIG_PATH_INFO* path_info) { ...@@ -118,22 +120,19 @@ bool GetPathInfo(HMONITOR monitor, DISPLAYCONFIG_PATH_INFO* path_info) {
device_name.header.adapterId = info.sourceInfo.adapterId; device_name.header.adapterId = info.sourceInfo.adapterId;
device_name.header.id = info.sourceInfo.id; device_name.header.id = info.sourceInfo.id;
if ((DisplayConfigGetDeviceInfo(&device_name.header) == ERROR_SUCCESS) && if ((DisplayConfigGetDeviceInfo(&device_name.header) == ERROR_SUCCESS) &&
(wcscmp(monitor_info.szDevice, device_name.viewGdiDeviceName) == 0)) { (wcscmp(monitor_info.szDevice, device_name.viewGdiDeviceName) == 0))
*path_info = info; return info;
return true;
}
} }
return false; return base::nullopt;
} }
float GetMonitorSDRWhiteLevel(HMONITOR monitor) { float GetMonitorSDRWhiteLevel(HMONITOR monitor) {
DISPLAYCONFIG_PATH_INFO path_info = {}; if (auto path_info = GetPathInfo(monitor)) {
if (GetPathInfo(monitor, &path_info)) {
DISPLAYCONFIG_SDR_WHITE_LEVEL white_level = {}; DISPLAYCONFIG_SDR_WHITE_LEVEL white_level = {};
white_level.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL; white_level.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL;
white_level.header.size = sizeof(white_level); white_level.header.size = sizeof(white_level);
white_level.header.adapterId = path_info.targetInfo.adapterId; white_level.header.adapterId = path_info->targetInfo.adapterId;
white_level.header.id = path_info.targetInfo.id; white_level.header.id = path_info->targetInfo.id;
if (DisplayConfigGetDeviceInfo(&white_level.header) == ERROR_SUCCESS) if (DisplayConfigGetDeviceInfo(&white_level.header) == ERROR_SUCCESS)
return white_level.SDRWhiteLevel * 80.0 / 1000.0; return white_level.SDRWhiteLevel * 80.0 / 1000.0;
} }
...@@ -156,17 +155,17 @@ Display::Rotation OrientationToRotation(DWORD orientation) { ...@@ -156,17 +155,17 @@ Display::Rotation OrientationToRotation(DWORD orientation) {
} }
} }
void GetDisplaySettingsForDevice(const wchar_t* device_name, struct DisplaySettings {
Display::Rotation* rotation, Display::Rotation rotation;
int* frequency) { int frequency;
*rotation = Display::ROTATE_0; };
*frequency = 0; DisplaySettings GetDisplaySettingsForDevice(const wchar_t* device_name) {
DEVMODE mode = {}; DEVMODE mode = {};
mode.dmSize = sizeof(mode); mode.dmSize = sizeof(mode);
if (!::EnumDisplaySettings(device_name, ENUM_CURRENT_SETTINGS, &mode)) if (!::EnumDisplaySettings(device_name, ENUM_CURRENT_SETTINGS, &mode))
return; return {Display::ROTATE_0, 0};
*rotation = OrientationToRotation(mode.dmDisplayOrientation); return {OrientationToRotation(mode.dmDisplayOrientation),
*frequency = mode.dmDisplayFrequency; mode.dmDisplayFrequency};
} }
std::vector<DisplayInfo> FindAndRemoveTouchingDisplayInfos( std::vector<DisplayInfo> FindAndRemoveTouchingDisplayInfos(
...@@ -334,14 +333,15 @@ MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) { ...@@ -334,14 +333,15 @@ MONITORINFOEX MonitorInfoFromHMONITOR(HMONITOR monitor) {
return monitor_info; return monitor_info;
} }
gfx::Vector2dF GetPixelsPerInchForPointerDevice(HANDLE source_device) { base::Optional<gfx::Vector2dF> GetPixelsPerInchForPointerDevice(
HANDLE source_device) {
static const auto get_pointer_device_rects = static const auto get_pointer_device_rects =
reinterpret_cast<decltype(&::GetPointerDeviceRects)>( reinterpret_cast<decltype(&::GetPointerDeviceRects)>(
base::win::GetUser32FunctionPointer("GetPointerDeviceRects")); base::win::GetUser32FunctionPointer("GetPointerDeviceRects"));
RECT device_rect, screen_rect; RECT device_rect, screen_rect;
if (!get_pointer_device_rects || if (!get_pointer_device_rects ||
!get_pointer_device_rects(source_device, &device_rect, &screen_rect)) !get_pointer_device_rects(source_device, &device_rect, &screen_rect))
return gfx::Vector2dF(); return base::nullopt;
const gfx::RectF device{gfx::Rect(device_rect)}; const gfx::RectF device{gfx::Rect(device_rect)};
const gfx::RectF screen{gfx::Rect(screen_rect)}; const gfx::RectF screen{gfx::Rect(screen_rect)};
...@@ -358,28 +358,28 @@ gfx::Vector2dF GetDefaultMonitorPhysicalPixelsPerInch() { ...@@ -358,28 +358,28 @@ gfx::Vector2dF GetDefaultMonitorPhysicalPixelsPerInch() {
return gfx::Vector2dF(default_dpi, default_dpi); return gfx::Vector2dF(default_dpi, default_dpi);
} }
// Retrieve PPI for |monitor| based on touch pointer device handles. // Retrieves PPI for |monitor| based on touch pointer device handles. Returns
gfx::Vector2dF GetMonitorPixelsPerInch(HMONITOR monitor) { // nullopt if a pointer device for |monitor| can't be found.
base::Optional<gfx::Vector2dF> GetMonitorPixelsPerInch(HMONITOR monitor) {
static const auto get_pointer_devices = static const auto get_pointer_devices =
reinterpret_cast<decltype(&::GetPointerDevices)>( reinterpret_cast<decltype(&::GetPointerDevices)>(
base::win::GetUser32FunctionPointer("GetPointerDevices")); base::win::GetUser32FunctionPointer("GetPointerDevices"));
gfx::Vector2dF pixels_per_inch = GetDefaultMonitorPhysicalPixelsPerInch();
uint32_t pointer_device_count = 0; uint32_t pointer_device_count = 0;
if (!get_pointer_devices || if (!get_pointer_devices ||
!get_pointer_devices(&pointer_device_count, nullptr) || !get_pointer_devices(&pointer_device_count, nullptr) ||
(pointer_device_count == 0)) (pointer_device_count == 0))
return pixels_per_inch; return base::nullopt;
std::vector<POINTER_DEVICE_INFO> pointer_devices(pointer_device_count); std::vector<POINTER_DEVICE_INFO> pointer_devices(pointer_device_count);
if (!get_pointer_devices(&pointer_device_count, pointer_devices.data())) if (!get_pointer_devices(&pointer_device_count, pointer_devices.data()))
return pixels_per_inch; return base::nullopt;
for (const auto& device : pointer_devices) { for (const auto& device : pointer_devices) {
if (device.pointerDeviceType == POINTER_DEVICE_TYPE_TOUCH && if (device.pointerDeviceType == POINTER_DEVICE_TYPE_TOUCH &&
device.monitor == monitor) device.monitor == monitor)
return GetPixelsPerInchForPointerDevice(device.device); return GetPixelsPerInchForPointerDevice(device.device);
} }
return pixels_per_inch; return base::nullopt;
} }
BOOL CALLBACK EnumMonitorForDisplayInfoCallback(HMONITOR monitor, BOOL CALLBACK EnumMonitorForDisplayInfoCallback(HMONITOR monitor,
...@@ -387,17 +387,18 @@ BOOL CALLBACK EnumMonitorForDisplayInfoCallback(HMONITOR monitor, ...@@ -387,17 +387,18 @@ BOOL CALLBACK EnumMonitorForDisplayInfoCallback(HMONITOR monitor,
LPRECT rect, LPRECT rect,
LPARAM data) { LPARAM data) {
const MONITORINFOEX monitor_info = MonitorInfoFromHMONITOR(monitor); const MONITORINFOEX monitor_info = MonitorInfoFromHMONITOR(monitor);
Display::Rotation rotation; const auto display_settings =
int display_frequency; GetDisplaySettingsForDevice(monitor_info.szDevice);
GetDisplaySettingsForDevice(monitor_info.szDevice, &rotation, const gfx::Vector2dF pixels_per_inch =
&display_frequency); GetMonitorPixelsPerInch(monitor).value_or(
gfx::Vector2dF pixels_per_inch = GetMonitorPixelsPerInch(monitor); GetDefaultMonitorPhysicalPixelsPerInch());
auto* display_infos = reinterpret_cast<std::vector<DisplayInfo>*>(data); auto* display_infos = reinterpret_cast<std::vector<DisplayInfo>*>(data);
DCHECK(display_infos); DCHECK(display_infos);
display_infos->emplace_back(monitor_info, GetMonitorScaleFactor(monitor), display_infos->emplace_back(monitor_info, GetMonitorScaleFactor(monitor),
GetMonitorSDRWhiteLevel(monitor), rotation, GetMonitorSDRWhiteLevel(monitor),
display_frequency, pixels_per_inch); display_settings.rotation,
display_settings.frequency, pixels_per_inch);
return TRUE; return TRUE;
} }
...@@ -571,8 +572,8 @@ int ScreenWin::GetDPIForHWND(HWND hwnd) { ...@@ -571,8 +572,8 @@ int ScreenWin::GetDPIForHWND(HWND hwnd) {
return GetDPIFromScalingFactor(Display::GetForcedDeviceScaleFactor()); return GetDPIFromScalingFactor(Display::GetForcedDeviceScaleFactor());
const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
int dpi = GetPerMonitorDPI(monitor); return GetPerMonitorDPI(monitor).value_or(
return dpi ? dpi : display::win::internal::GetDefaultSystemDPI(); display::win::internal::GetDefaultSystemDPI());
} }
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment