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/win/scoped_hdc.h" | 8 #include "base/win/scoped_hdc.h" |
9 #include "ui/gfx/display.h" | 9 #include "ui/gfx/display.h" |
10 #include "ui/gfx/geometry/point_conversions.h" | 10 #include "ui/gfx/geometry/point_conversions.h" |
11 #include "ui/gfx/geometry/rect_conversions.h" | 11 #include "ui/gfx/geometry/rect_conversions.h" |
12 #include "ui/gfx/geometry/size_conversions.h" | 12 #include "ui/gfx/geometry/size_conversions.h" |
| 13 #include "ui/gfx/screen.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 const float kDefaultDPI = 96.f; | 17 const float kDefaultDPI = 96.f; |
17 | 18 |
18 float g_device_scale_factor = 0.f; | 19 float g_device_scale_factor = 0.f; |
19 | 20 |
20 float GetUnforcedDeviceScaleFactor() { | 21 float GetUnforcedDeviceScaleFactor() { |
21 return g_device_scale_factor ? | 22 if (g_device_scale_factor) |
22 g_device_scale_factor : | 23 return g_device_scale_factor; |
23 static_cast<float>(gfx::GetDPI().width()) / kDefaultDPI; | 24 |
| 25 static float device_scale_factor = 0.0f; |
| 26 if (!device_scale_factor) { |
| 27 base::win::ScopedGetDC screen_dc(NULL); |
| 28 int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); |
| 29 DCHECK(dpi_x == GetDeviceCaps(screen_dc, LOGPIXELSY)); |
| 30 device_scale_factor = static_cast<float>(dpi_x) / kDefaultDPI; |
| 31 } |
| 32 return device_scale_factor; |
24 } | 33 } |
25 | 34 |
26 } // namespace | 35 } // namespace |
27 | 36 |
28 namespace gfx { | 37 namespace gfx { |
29 | 38 |
30 void SetDefaultDeviceScaleFactor(float scale) { | 39 void SetDefaultDeviceScaleFactor(float scale) { |
31 DCHECK_NE(0.f, scale); | 40 DCHECK_NE(0.f, scale); |
32 g_device_scale_factor = scale; | 41 g_device_scale_factor = scale; |
33 } | 42 } |
34 | 43 |
35 Size GetDPI() { | 44 int GetDPIFromScalingFactor(float device_scaling_factor) { |
36 static int dpi_x = 0; | 45 return kDefaultDPI * device_scaling_factor; |
37 static int dpi_y = 0; | |
38 static bool should_initialize = true; | |
39 | |
40 if (should_initialize) { | |
41 should_initialize = false; | |
42 base::win::ScopedGetDC screen_dc(NULL); | |
43 // This value is safe to cache for the life time of the app since the | |
44 // user must logout to change the DPI setting. This value also applies | |
45 // to all screens. | |
46 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | |
47 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | |
48 } | |
49 return Size(dpi_x, dpi_y); | |
50 } | 46 } |
51 | 47 |
52 float GetDPIScale() { | 48 float GetDPIScale() { |
53 if (gfx::Display::HasForceDeviceScaleFactor()) | 49 if (gfx::Display::HasForceDeviceScaleFactor()) |
54 return gfx::Display::GetForcedDeviceScaleFactor(); | 50 return gfx::Display::GetForcedDeviceScaleFactor(); |
55 float dpi_scale = GetUnforcedDeviceScaleFactor(); | 51 float dpi_scale = GetUnforcedDeviceScaleFactor(); |
56 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; | 52 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; |
57 } | 53 } |
58 | 54 |
59 namespace win { | 55 namespace win { |
60 | 56 |
61 Point ScreenToDIPPoint(const Point& pixel_point) { | |
62 return ScaleToFlooredPoint(pixel_point, 1.0f / GetDPIScale()); | |
63 } | |
64 | |
65 Point DIPToScreenPoint(const Point& dip_point) { | |
66 return ScaleToFlooredPoint(dip_point, GetDPIScale()); | |
67 } | |
68 | |
69 Rect ScreenToDIPRect(const Rect& pixel_bounds) { | |
70 // It's important we scale the origin and size separately. If we instead | |
71 // calculated the size from the floored origin and ceiled right the size could | |
72 // vary depending upon where the two points land. That would cause problems | |
73 // for the places this code is used (in particular mapping from native window | |
74 // bounds to DIPs). | |
75 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), | |
76 ScreenToDIPSize(pixel_bounds.size())); | |
77 } | |
78 | |
79 Rect DIPToScreenRect(const Rect& dip_bounds) { | |
80 // See comment in ScreenToDIPRect for why we calculate size like this. | |
81 return Rect(DIPToScreenPoint(dip_bounds.origin()), | |
82 DIPToScreenSize(dip_bounds.size())); | |
83 } | |
84 | |
85 Size ScreenToDIPSize(const Size& size_in_pixels) { | |
86 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. | |
87 return ScaleToCeiledSize(size_in_pixels, 1.0f / GetDPIScale()); | |
88 } | |
89 | |
90 Size DIPToScreenSize(const Size& dip_size) { | |
91 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. | |
92 return ScaleToCeiledSize(dip_size, GetDPIScale()); | |
93 } | |
94 | |
95 int GetSystemMetricsInDIP(int metric) { | 57 int GetSystemMetricsInDIP(int metric) { |
96 // The system metrics always reflect the system DPI, not whatever scale we've | 58 // The system metrics always reflect the system DPI, not whatever scale we've |
97 // forced or decided to use. | 59 // forced or decided to use. |
98 return static_cast<int>( | 60 return static_cast<int>( |
99 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); | 61 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); |
100 } | 62 } |
101 | 63 |
102 } // namespace win | 64 } // namespace win |
103 } // namespace gfx | 65 } // namespace gfx |
OLD | NEW |