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 "ui/base/layout.h" | |
12 #include "base/win/registry.h" | 11 #include "base/win/registry.h" |
13 #include "ui/gfx/display.h" | 12 #include "ui/gfx/display.h" |
14 #include "ui/gfx/switches.h" | 13 #include "ui/gfx/switches.h" |
15 #include "ui/gfx/point_conversions.h" | 14 #include "ui/gfx/point_conversions.h" |
16 #include "ui/gfx/rect_conversions.h" | 15 #include "ui/gfx/rect_conversions.h" |
17 #include "ui/gfx/size_conversions.h" | 16 #include "ui/gfx/size_conversions.h" |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 int kDefaultDPIX = 96; | 20 int kDefaultDPIX = 96; |
22 int kDefaultDPIY = 96; | 21 int kDefaultDPIY = 96; |
23 | 22 |
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 switches::kHighDPISupport)) { | |
29 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
30 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::GetImageScale(ui::GetSupportedScaleFactor(scale)); | |
44 return scale; | |
45 } | |
46 return 1.0f; | |
47 } | |
48 | |
49 BOOL IsProcessDPIAwareWrapper() { | 23 BOOL IsProcessDPIAwareWrapper() { |
50 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); | 24 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); |
51 IsProcessDPIAwarePtr is_process_dpi_aware_func = | 25 IsProcessDPIAwarePtr is_process_dpi_aware_func = |
52 reinterpret_cast<IsProcessDPIAwarePtr>( | 26 reinterpret_cast<IsProcessDPIAwarePtr>( |
53 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); | 27 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); |
54 if (is_process_dpi_aware_func) | 28 if (is_process_dpi_aware_func) |
55 return is_process_dpi_aware_func(); | 29 return is_process_dpi_aware_func(); |
56 return FALSE; | 30 return FALSE; |
57 } | 31 } |
58 | 32 |
| 33 float g_device_scale_factor = 0.0f; |
| 34 |
59 } // namespace | 35 } // namespace |
60 | 36 |
61 namespace gfx { | 37 namespace gfx { |
62 | 38 |
| 39 void InitDeviceScaleFactor(float scale) { |
| 40 DCHECK_NE(0.0f, scale); |
| 41 g_device_scale_factor = scale; |
| 42 } |
| 43 |
63 Size GetDPI() { | 44 Size GetDPI() { |
64 static int dpi_x = 0; | 45 static int dpi_x = 0; |
65 static int dpi_y = 0; | 46 static int dpi_y = 0; |
66 static bool should_initialize = true; | 47 static bool should_initialize = true; |
67 | 48 |
68 if (should_initialize) { | 49 if (should_initialize) { |
69 should_initialize = false; | 50 should_initialize = false; |
70 base::win::ScopedGetDC screen_dc(NULL); | 51 base::win::ScopedGetDC screen_dc(NULL); |
71 // This value is safe to cache for the life time of the app since the | 52 // 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 | 53 // user must logout to change the DPI setting. This value also applies |
73 // to all screens. | 54 // to all screens. |
74 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | 55 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); |
75 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | 56 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); |
76 } | 57 } |
77 return Size(dpi_x, dpi_y); | 58 return Size(dpi_x, dpi_y); |
78 } | 59 } |
79 | 60 |
80 float GetDPIScale() { | 61 float GetDPIScale() { |
81 if (IsHighDPIEnabled()) { | 62 if (IsHighDPIEnabled()) { |
82 return static_cast<float>(GetDPI().width()) / | 63 return static_cast<float>(GetDPI().width()) / |
83 static_cast<float>(kDefaultDPIX); | 64 static_cast<float>(kDefaultDPIX); |
84 } | 65 } |
85 return 1.0; | 66 return 1.0; |
86 } | 67 } |
87 | 68 |
| 69 bool IsHighDPIEnabled() { |
| 70 // Default is disabled. |
| 71 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 72 switches::kHighDPISupport)) { |
| 73 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 74 switches::kHighDPISupport).compare("1") == 0; |
| 75 } |
| 76 return false; |
| 77 } |
| 78 |
88 bool IsInHighDPIMode() { | 79 bool IsInHighDPIMode() { |
89 return GetDPIScale() > 1.0; | 80 return GetDPIScale() > 1.0; |
90 } | 81 } |
91 | 82 |
92 void EnableHighDPISupport() { | 83 void EnableHighDPISupport() { |
93 if (IsHighDPIEnabled()) { | 84 if (IsHighDPIEnabled()) { |
94 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); | 85 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); |
95 SetProcessDPIAwarePtr set_process_dpi_aware_func = | 86 SetProcessDPIAwarePtr set_process_dpi_aware_func = |
96 reinterpret_cast<SetProcessDPIAwarePtr>( | 87 reinterpret_cast<SetProcessDPIAwarePtr>( |
97 GetProcAddress(GetModuleHandleA("user32.dll"), | 88 GetProcAddress(GetModuleHandleA("user32.dll"), |
98 "SetProcessDPIAware")); | 89 "SetProcessDPIAware")); |
99 if (set_process_dpi_aware_func) | 90 if (set_process_dpi_aware_func) |
100 set_process_dpi_aware_func(); | 91 set_process_dpi_aware_func(); |
101 } | 92 } |
102 } | 93 } |
103 | 94 |
104 namespace win { | 95 namespace win { |
105 | 96 |
106 float GetDeviceScaleFactor() { | 97 float GetDeviceScaleFactor() { |
107 static const float device_scale_factor = GetDeviceScaleFactorImpl(); | 98 DCHECK_NE(0.0f, g_device_scale_factor); |
108 return device_scale_factor; | 99 return g_device_scale_factor; |
109 } | 100 } |
110 | 101 |
111 Point ScreenToDIPPoint(const Point& pixel_point) { | 102 Point ScreenToDIPPoint(const Point& pixel_point) { |
112 return ToFlooredPoint(ScalePoint(pixel_point, 1.0f / GetDeviceScaleFactor())); | 103 return ToFlooredPoint(ScalePoint(pixel_point, 1.0f / GetDeviceScaleFactor())); |
113 } | 104 } |
114 | 105 |
115 Point DIPToScreenPoint(const Point& dip_point) { | 106 Point DIPToScreenPoint(const Point& dip_point) { |
116 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); | 107 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); |
117 } | 108 } |
118 | 109 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 double GetUndocumentedDPITouchScale() { | 157 double GetUndocumentedDPITouchScale() { |
167 static double scale = | 158 static double scale = |
168 (base::win::GetVersion() < base::win::VERSION_WIN8_1) ? | 159 (base::win::GetVersion() < base::win::VERSION_WIN8_1) ? |
169 GetUndocumentedDPIScale() : 1.0; | 160 GetUndocumentedDPIScale() : 1.0; |
170 return scale; | 161 return scale; |
171 } | 162 } |
172 | 163 |
173 | 164 |
174 } // namespace win | 165 } // namespace win |
175 } // namespace gfx | 166 } // namespace gfx |
OLD | NEW |