| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/dpi_win.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/win/scoped_hdc.h" | |
| 10 #include "base/win/windows_version.h" | |
| 11 #include "ui/base/layout.h" | |
| 12 #include "base/win/registry.h" | |
| 13 #include "ui/gfx/display.h" | |
| 14 #include "ui/gfx/switches.h" | |
| 15 #include "ui/gfx/point_conversions.h" | |
| 16 #include "ui/gfx/rect_conversions.h" | |
| 17 #include "ui/gfx/size_conversions.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 int kDefaultDPIX = 96; | |
| 22 int kDefaultDPIY = 96; | |
| 23 | |
| 24 // Tests to see if the command line flag "--high-dpi-support" is set. | |
| 25 bool IsHighDPIEnabled() { | |
| 26 // Default is disabled. | |
| 27 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 28 gfx::switches::kHighDPISupport)) { | |
| 29 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 30 gfx::switches::kHighDPISupport).compare("1") == 0; | |
| 31 } | |
| 32 return false; | |
| 33 } | |
| 34 | |
| 35 // Gets the device scale factor. If support is enabled, this will return the | |
| 36 // best available scale based on the screen's pixel density. This can be | |
| 37 // affected (overridden) by --force-device-scale-factor=x | |
| 38 float GetDeviceScaleFactorImpl() { | |
| 39 if (IsHighDPIEnabled()) { | |
| 40 float scale = gfx::Display::HasForceDeviceScaleFactor() ? | |
| 41 gfx::Display::GetForcedDeviceScaleFactor() : gfx::GetDPIScale(); | |
| 42 // Quantize to nearest supported scale factor. | |
| 43 scale = ui::GetScaleFactorScale(ui::GetScaleFactorFromScale(scale)); | |
| 44 return scale; | |
| 45 } | |
| 46 return 1.0f; | |
| 47 } | |
| 48 | |
| 49 BOOL IsProcessDPIAwareWrapper() { | |
| 50 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); | |
| 51 IsProcessDPIAwarePtr is_process_dpi_aware_func = | |
| 52 reinterpret_cast<IsProcessDPIAwarePtr>( | |
| 53 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); | |
| 54 if (is_process_dpi_aware_func) | |
| 55 return is_process_dpi_aware_func(); | |
| 56 return FALSE; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 namespace gfx { | |
| 62 | |
| 63 Size GetDPI() { | |
| 64 static int dpi_x = 0; | |
| 65 static int dpi_y = 0; | |
| 66 static bool should_initialize = true; | |
| 67 | |
| 68 if (should_initialize) { | |
| 69 should_initialize = false; | |
| 70 base::win::ScopedGetDC screen_dc(NULL); | |
| 71 // This value is safe to cache for the life time of the app since the | |
| 72 // user must logout to change the DPI setting. This value also applies | |
| 73 // to all screens. | |
| 74 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | |
| 75 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | |
| 76 } | |
| 77 return Size(dpi_x, dpi_y); | |
| 78 } | |
| 79 | |
| 80 float GetDPIScale() { | |
| 81 if (IsHighDPIEnabled()) { | |
| 82 return static_cast<float>(GetDPI().width()) / | |
| 83 static_cast<float>(kDefaultDPIX); | |
| 84 } | |
| 85 return 1.0; | |
| 86 } | |
| 87 | |
| 88 bool IsInHighDPIMode() { | |
| 89 return GetDPIScale() > 1.0; | |
| 90 } | |
| 91 | |
| 92 void EnableHighDPISupport() { | |
| 93 if (IsHighDPIEnabled()) { | |
| 94 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); | |
| 95 SetProcessDPIAwarePtr set_process_dpi_aware_func = | |
| 96 reinterpret_cast<SetProcessDPIAwarePtr>( | |
| 97 GetProcAddress(GetModuleHandleA("user32.dll"), | |
| 98 "SetProcessDPIAware")); | |
| 99 if (set_process_dpi_aware_func) | |
| 100 set_process_dpi_aware_func(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 namespace win { | |
| 105 | |
| 106 float GetDeviceScaleFactor() { | |
| 107 static const float device_scale_factor = GetDeviceScaleFactorImpl(); | |
| 108 return device_scale_factor; | |
| 109 } | |
| 110 | |
| 111 Point ScreenToDIPPoint(const Point& pixel_point) { | |
| 112 return ToFlooredPoint(ScalePoint(pixel_point, 1.0f / GetDeviceScaleFactor())); | |
| 113 } | |
| 114 | |
| 115 Point DIPToScreenPoint(const Point& dip_point) { | |
| 116 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); | |
| 117 } | |
| 118 | |
| 119 Rect ScreenToDIPRect(const Rect& pixel_bounds) { | |
| 120 // TODO(kevers): Switch to non-deprecated method for float to int conversions. | |
| 121 return ToFlooredRectDeprecated( | |
| 122 ScaleRect(pixel_bounds, 1.0f / GetDeviceScaleFactor())); | |
| 123 } | |
| 124 | |
| 125 Rect DIPToScreenRect(const Rect& dip_bounds) { | |
| 126 // TODO(kevers): Switch to non-deprecated method for float to int conversions. | |
| 127 return ToFlooredRectDeprecated( | |
| 128 ScaleRect(dip_bounds, GetDeviceScaleFactor())); | |
| 129 } | |
| 130 | |
| 131 Size ScreenToDIPSize(const Size& size_in_pixels) { | |
| 132 return ToFlooredSize( | |
| 133 ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); | |
| 134 } | |
| 135 | |
| 136 Size DIPToScreenSize(const Size& dip_size) { | |
| 137 return ToFlooredSize(ScaleSize(dip_size, GetDeviceScaleFactor())); | |
| 138 } | |
| 139 | |
| 140 int GetSystemMetricsInDIP(int metric) { | |
| 141 return static_cast<int>(GetSystemMetrics(metric) / | |
| 142 GetDeviceScaleFactor() + 0.5); | |
| 143 } | |
| 144 | |
| 145 double GetUndocumentedDPIScale() { | |
| 146 // TODO(girard): Remove this code when chrome is DPIAware. | |
| 147 static double scale = -1.0; | |
| 148 if (scale == -1.0) { | |
| 149 scale = 1.0; | |
| 150 if (!IsProcessDPIAwareWrapper()) { | |
| 151 base::win::RegKey key(HKEY_CURRENT_USER, | |
| 152 L"Control Panel\\Desktop\\WindowMetrics", | |
| 153 KEY_QUERY_VALUE); | |
| 154 if (key.Valid()) { | |
| 155 DWORD value = 0; | |
| 156 if (key.ReadValueDW(L"AppliedDPI", &value) == ERROR_SUCCESS) { | |
| 157 scale = static_cast<double>(value) / kDefaultDPIX; | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 return scale; | |
| 163 } | |
| 164 | |
| 165 | |
| 166 double GetUndocumentedDPITouchScale() { | |
| 167 static double scale = | |
| 168 (base::win::GetVersion() < base::win::VERSION_WIN8_1) ? | |
| 169 GetUndocumentedDPIScale() : 1.0; | |
| 170 return scale; | |
| 171 } | |
| 172 | |
| 173 | |
| 174 } // namespace win | |
| 175 } // namespace gfx | |
| OLD | NEW |