| 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 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const float kDefaultDPI = 96.f; | 16 const float kDefaultDPI = 96.f; |
| 17 | 17 |
| 18 float g_device_scale_factor = 0.f; | 18 float g_device_scale_factor = 0.f; |
| 19 | 19 |
| 20 float GetUnforcedDeviceScaleFactor() { | 20 gfx::Size GetDPI() { |
| 21 return g_device_scale_factor ? | |
| 22 g_device_scale_factor : | |
| 23 static_cast<float>(gfx::GetDPI().width()) / kDefaultDPI; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 namespace gfx { | |
| 29 | |
| 30 void SetDefaultDeviceScaleFactor(float scale) { | |
| 31 DCHECK_NE(0.f, scale); | |
| 32 g_device_scale_factor = scale; | |
| 33 } | |
| 34 | |
| 35 Size GetDPI() { | |
| 36 static int dpi_x = 0; | 21 static int dpi_x = 0; |
| 37 static int dpi_y = 0; | 22 static int dpi_y = 0; |
| 38 static bool should_initialize = true; | 23 static bool should_initialize = true; |
| 39 | 24 |
| 40 if (should_initialize) { | 25 if (should_initialize) { |
| 41 should_initialize = false; | 26 should_initialize = false; |
| 42 base::win::ScopedGetDC screen_dc(NULL); | 27 base::win::ScopedGetDC screen_dc(NULL); |
| 43 // This value is safe to cache for the life time of the app since the | 28 // 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 | 29 // user must logout to change the DPI setting. This value also applies |
| 45 // to all screens. | 30 // to all screens. |
| 46 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | 31 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); |
| 47 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | 32 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); |
| 48 } | 33 } |
| 49 return Size(dpi_x, dpi_y); | 34 return gfx::Size(dpi_x, dpi_y); |
| 35 } |
| 36 |
| 37 float GetUnforcedDeviceScaleFactor() { |
| 38 return g_device_scale_factor ? |
| 39 g_device_scale_factor : |
| 40 static_cast<float>(GetDPI().width()) / kDefaultDPI; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 namespace gfx { |
| 46 |
| 47 void SetDefaultDeviceScaleFactor(float scale) { |
| 48 DCHECK_NE(0.f, scale); |
| 49 g_device_scale_factor = scale; |
| 50 } | 50 } |
| 51 | 51 |
| 52 float GetDPIScale() { | 52 float GetDPIScale() { |
| 53 if (gfx::Display::HasForceDeviceScaleFactor()) | 53 if (gfx::Display::HasForceDeviceScaleFactor()) |
| 54 return gfx::Display::GetForcedDeviceScaleFactor(); | 54 return gfx::Display::GetForcedDeviceScaleFactor(); |
| 55 float dpi_scale = GetUnforcedDeviceScaleFactor(); | 55 float dpi_scale = GetUnforcedDeviceScaleFactor(); |
| 56 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; | 56 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; |
| 57 } | 57 } |
| 58 | 58 |
| 59 namespace win { | 59 int GetDPIFromScalingFactor(float device_scaling_factor) { |
| 60 | 60 return kDefaultDPI * device_scaling_factor; |
| 61 Point ScreenToDIPPoint(const Point& pixel_point) { | |
| 62 return ScaleToFlooredPoint(pixel_point, 1.0f / GetDPIScale()); | |
| 63 } | 61 } |
| 64 | 62 |
| 65 Point DIPToScreenPoint(const Point& dip_point) { | 63 namespace win { |
| 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 | 64 |
| 95 int GetSystemMetricsInDIP(int metric) { | 65 int GetSystemMetricsInDIP(int metric) { |
| 96 // The system metrics always reflect the system DPI, not whatever scale we've | 66 // The system metrics always reflect the system DPI, not whatever scale we've |
| 97 // forced or decided to use. | 67 // forced or decided to use. |
| 98 return static_cast<int>( | 68 return static_cast<int>( |
| 99 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); | 69 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); |
| 100 } | 70 } |
| 101 | 71 |
| 102 } // namespace win | 72 } // namespace win |
| 103 } // namespace gfx | 73 } // namespace gfx |
| OLD | NEW |