OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/display/win/dpi.h" | 5 #include "ui/display/win/dpi.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
7 #include <windows.h> | 9 #include <windows.h> |
8 | 10 |
11 #include "base/metrics/histogram.h" | |
Ilya Sherman
2016/07/16 01:31:24
nit: You shouldn't need this include -- just the s
Bret
2016/07/18 17:41:48
Done.
| |
12 #include "base/metrics/sparse_histogram.h" | |
9 #include "base/win/scoped_hdc.h" | 13 #include "base/win/scoped_hdc.h" |
10 #include "ui/display/display.h" | 14 #include "ui/display/display.h" |
11 | 15 |
12 namespace display { | 16 namespace display { |
13 namespace win { | 17 namespace win { |
14 | 18 |
15 namespace { | 19 namespace { |
16 | 20 |
17 const float kDefaultDPI = 96.f; | 21 const float kDefaultDPI = 96.f; |
18 | 22 |
19 float g_device_scale_factor = 0.f; | 23 float g_device_scale_factor = 0.f; |
20 | 24 |
25 void RecordDeviceScale(); | |
26 | |
21 gfx::Size GetDPI() { | 27 gfx::Size GetDPI() { |
22 static int dpi_x = 0; | 28 static int dpi_x = 0; |
23 static int dpi_y = 0; | 29 static int dpi_y = 0; |
24 static bool should_initialize = true; | 30 static bool should_initialize = true; |
25 | 31 |
26 if (should_initialize) { | 32 if (should_initialize) { |
27 should_initialize = false; | 33 should_initialize = false; |
28 base::win::ScopedGetDC screen_dc(NULL); | 34 base::win::ScopedGetDC screen_dc(NULL); |
29 // This value is safe to cache for the life time of the app since the | 35 // This value is safe to cache for the life time of the app since the |
30 // user must logout to change the DPI setting. This value also applies | 36 // user must logout to change the DPI setting. This value also applies |
31 // to all screens. | 37 // to all screens. |
32 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | 38 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); |
33 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | 39 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); |
40 RecordDeviceScale(); | |
34 } | 41 } |
35 return gfx::Size(dpi_x, dpi_y); | 42 return gfx::Size(dpi_x, dpi_y); |
36 } | 43 } |
37 | 44 |
38 float GetUnforcedDeviceScaleFactor() { | 45 float GetUnforcedDeviceScaleFactor() { |
39 return g_device_scale_factor | 46 return g_device_scale_factor ? g_device_scale_factor |
40 ? g_device_scale_factor | 47 : GetScalingFactorFromDPI(GetDPI().width()); |
41 : GetScalingFactorFromDPI(GetDPI().width()); | 48 } |
49 | |
50 void RecordDeviceScale() { | |
51 // Clamp the reported value so that if it's wildly out-of-band we won't send | |
52 // it to the backend. | |
53 const int scale = std::min( | |
54 std::max(std::floor(GetUnforcedDeviceScaleFactor() * 100), 0.f), 1000.f); | |
Ilya Sherman
2016/07/16 01:31:24
nit: Somewhere, please add some explicit code to c
Bret
2016/07/18 17:41:48
Done.
| |
55 UMA_HISTOGRAM_SPARSE_SLOWLY("UI.DeviceScale", scale); | |
42 } | 56 } |
43 | 57 |
44 } // namespace | 58 } // namespace |
45 | 59 |
46 void SetDefaultDeviceScaleFactor(float scale) { | 60 void SetDefaultDeviceScaleFactor(float scale) { |
47 DCHECK_NE(0.f, scale); | 61 DCHECK_NE(0.f, scale); |
48 g_device_scale_factor = scale; | 62 g_device_scale_factor = scale; |
49 } | 63 } |
50 | 64 |
51 float GetDPIScale() { | 65 float GetDPIScale() { |
(...skipping 13 matching lines...) Expand all Loading... | |
65 | 79 |
66 int GetSystemMetricsInDIP(int metric) { | 80 int GetSystemMetricsInDIP(int metric) { |
67 // The system metrics always reflect the system DPI, not whatever scale we've | 81 // The system metrics always reflect the system DPI, not whatever scale we've |
68 // forced or decided to use. | 82 // forced or decided to use. |
69 return static_cast<int>( | 83 return static_cast<int>( |
70 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); | 84 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); |
71 } | 85 } |
72 | 86 |
73 } // namespace win | 87 } // namespace win |
74 } // namespace display | 88 } // namespace display |
OLD | NEW |