| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/win/dpi.h" | 5 #include "ui/gfx/win/dpi.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/win/scoped_hdc.h" | 9 #include "base/win/scoped_hdc.h" |
| 10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| 11 #include "base/win/registry.h" | 11 #include "base/win/registry.h" |
| 12 #include "ui/gfx/display.h" | 12 #include "ui/gfx/display.h" |
| 13 #include "ui/gfx/switches.h" | 13 #include "ui/gfx/switches.h" |
| 14 #include "ui/gfx/point_conversions.h" | 14 #include "ui/gfx/point_conversions.h" |
| 15 #include "ui/gfx/rect_conversions.h" | 15 #include "ui/gfx/rect_conversions.h" |
| 16 #include "ui/gfx/size_conversions.h" | 16 #include "ui/gfx/size_conversions.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 int kDefaultDPIX = 96; | 20 int kDefaultDPIX = 96; |
| 21 int kDefaultDPIY = 96; | 21 int kDefaultDPIY = 96; |
| 22 | 22 |
| 23 const wchar_t kRegistryProfilePath[] = L"SOFTWARE\\Google\\Chrome\\Profile"; |
| 24 const wchar_t kHighDPISupportW[] = L"high-dpi-support"; |
| 25 |
| 26 bool force_highdpi_for_testing = false; |
| 27 |
| 23 BOOL IsProcessDPIAwareWrapper() { | 28 BOOL IsProcessDPIAwareWrapper() { |
| 24 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); | 29 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); |
| 25 IsProcessDPIAwarePtr is_process_dpi_aware_func = | 30 IsProcessDPIAwarePtr is_process_dpi_aware_func = |
| 26 reinterpret_cast<IsProcessDPIAwarePtr>( | 31 reinterpret_cast<IsProcessDPIAwarePtr>( |
| 27 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); | 32 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); |
| 28 if (is_process_dpi_aware_func) | 33 if (is_process_dpi_aware_func) |
| 29 return is_process_dpi_aware_func(); | 34 return is_process_dpi_aware_func(); |
| 30 return FALSE; | 35 return FALSE; |
| 31 } | 36 } |
| 32 | 37 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 BOOL SetProcessDPIAwareWrapper() { | 96 BOOL SetProcessDPIAwareWrapper() { |
| 92 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); | 97 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); |
| 93 SetProcessDPIAwarePtr set_process_dpi_aware_func = | 98 SetProcessDPIAwarePtr set_process_dpi_aware_func = |
| 94 reinterpret_cast<SetProcessDPIAwarePtr>( | 99 reinterpret_cast<SetProcessDPIAwarePtr>( |
| 95 GetProcAddress(GetModuleHandleA("user32.dll"), | 100 GetProcAddress(GetModuleHandleA("user32.dll"), |
| 96 "SetProcessDPIAware")); | 101 "SetProcessDPIAware")); |
| 97 return set_process_dpi_aware_func && | 102 return set_process_dpi_aware_func && |
| 98 set_process_dpi_aware_func(); | 103 set_process_dpi_aware_func(); |
| 99 } | 104 } |
| 100 | 105 |
| 106 DWORD ReadRegistryValue(HKEY root, |
| 107 const wchar_t* base_key, |
| 108 const wchar_t* value_name, |
| 109 DWORD default_value) { |
| 110 base::win::RegKey reg_key(HKEY_CURRENT_USER, |
| 111 base_key, |
| 112 KEY_QUERY_VALUE); |
| 113 DWORD value; |
| 114 if (reg_key.Valid() && |
| 115 reg_key.ReadValueDW(value_name, &value) == ERROR_SUCCESS) { |
| 116 return value; |
| 117 } |
| 118 return default_value; |
| 119 } |
| 120 |
| 101 } // namespace | 121 } // namespace |
| 102 | 122 |
| 103 namespace gfx { | 123 namespace gfx { |
| 104 | 124 |
| 105 float GetModernUIScale() { | 125 float GetModernUIScale() { |
| 106 return GetModernUIScaleWrapper(); | 126 return GetModernUIScaleWrapper(); |
| 107 } | 127 } |
| 108 | 128 |
| 109 void InitDeviceScaleFactor(float scale) { | 129 void InitDeviceScaleFactor(float scale) { |
| 110 DCHECK_NE(0.0f, scale); | 130 DCHECK_NE(0.0f, scale); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 130 | 150 |
| 131 float GetDPIScale() { | 151 float GetDPIScale() { |
| 132 if (IsHighDPIEnabled()) { | 152 if (IsHighDPIEnabled()) { |
| 133 return gfx::Display::HasForceDeviceScaleFactor() ? | 153 return gfx::Display::HasForceDeviceScaleFactor() ? |
| 134 gfx::Display::GetForcedDeviceScaleFactor() : | 154 gfx::Display::GetForcedDeviceScaleFactor() : |
| 135 GetUnforcedDeviceScaleFactor(); | 155 GetUnforcedDeviceScaleFactor(); |
| 136 } | 156 } |
| 137 return 1.0; | 157 return 1.0; |
| 138 } | 158 } |
| 139 | 159 |
| 160 void ForceHighDPISupportForTesting(float scale) { |
| 161 force_highdpi_for_testing = true; |
| 162 g_device_scale_factor = scale; |
| 163 } |
| 164 |
| 140 bool IsHighDPIEnabled() { | 165 bool IsHighDPIEnabled() { |
| 166 // Flag stored in HKEY_CURRENT_USER\SOFTWARE\\Google\\Chrome\\Profile, |
| 167 // under the DWORD value high-dpi-support. |
| 141 // Default is disabled. | 168 // Default is disabled. |
| 142 if (CommandLine::ForCurrentProcess()->HasSwitch( | 169 static DWORD value = ReadRegistryValue( |
| 143 switches::kHighDPISupport)) { | 170 HKEY_CURRENT_USER, kRegistryProfilePath, |
| 144 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 171 kHighDPISupportW, FALSE); |
| 145 switches::kHighDPISupport).compare("1") == 0; | 172 return force_highdpi_for_testing || (value == 1); |
| 146 } | |
| 147 return false; | |
| 148 } | 173 } |
| 149 | 174 |
| 150 bool IsInHighDPIMode() { | 175 bool IsInHighDPIMode() { |
| 151 return GetDPIScale() > 1.0; | 176 return GetDPIScale() > 1.0; |
| 152 } | 177 } |
| 153 | 178 |
| 154 void EnableHighDPISupport() { | 179 void EnableHighDPISupport() { |
| 155 if (IsHighDPIEnabled() && | 180 if (IsHighDPIEnabled() && |
| 156 (base::win::GetVersion() < base::win::VERSION_WIN8_1)) { | 181 !SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) { |
| 157 if (!SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) { | 182 SetProcessDPIAwareWrapper(); |
| 158 SetProcessDPIAwareWrapper(); | |
| 159 } | |
| 160 } | 183 } |
| 161 } | 184 } |
| 162 | 185 |
| 163 namespace win { | 186 namespace win { |
| 164 | 187 |
| 165 float GetDeviceScaleFactor() { | 188 float GetDeviceScaleFactor() { |
| 166 DCHECK_NE(0.0f, g_device_scale_factor); | 189 DCHECK_NE(0.0f, g_device_scale_factor); |
| 167 return g_device_scale_factor; | 190 return g_device_scale_factor; |
| 168 } | 191 } |
| 169 | 192 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 251 |
| 229 double GetUndocumentedDPITouchScale() { | 252 double GetUndocumentedDPITouchScale() { |
| 230 static double scale = | 253 static double scale = |
| 231 (base::win::GetVersion() < base::win::VERSION_WIN8_1) ? | 254 (base::win::GetVersion() < base::win::VERSION_WIN8_1) ? |
| 232 GetUndocumentedDPIScale() : 1.0; | 255 GetUndocumentedDPIScale() : 1.0; |
| 233 return scale; | 256 return scale; |
| 234 } | 257 } |
| 235 | 258 |
| 236 } // namespace win | 259 } // namespace win |
| 237 } // namespace gfx | 260 } // namespace gfx |
| OLD | NEW |