Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: ui/gfx/win/dpi.cc

Issue 1426933002: Refactor Windows DPI Point, Rect, and Size for Multiple Monitor DPI Awareness (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR Feedback Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/win/dpi.cc
diff --git a/ui/gfx/win/dpi.cc b/ui/gfx/win/dpi.cc
index c0c44d612b80a91e736f6d9eedcd2ba2a7a33fb3..8a4e4d606b1921d9c488338958de83adc6ced671 100644
--- a/ui/gfx/win/dpi.cc
+++ b/ui/gfx/win/dpi.cc
@@ -10,6 +10,7 @@
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
+#include "ui/gfx/screen.h"
namespace {
@@ -17,15 +18,6 @@ int kDefaultDPI = 96;
float g_device_scale_factor = 0.0f;
-float GetUnforcedDeviceScaleFactor() {
- // If the global device scale factor is initialized use it. This is to ensure
- // we use the same scale factor across all callsites.
- if (g_device_scale_factor)
- return g_device_scale_factor;
- return static_cast<float>(gfx::GetDPI().width()) /
- static_cast<float>(kDefaultDPI);
-}
-
} // namespace
namespace gfx {
@@ -35,70 +27,35 @@ void InitDeviceScaleFactor(float scale) {
g_device_scale_factor = scale;
}
-Size GetDPI() {
- static int dpi_x = 0;
- static int dpi_y = 0;
- static bool should_initialize = true;
-
- if (should_initialize) {
- should_initialize = false;
- base::win::ScopedGetDC screen_dc(NULL);
- // This value is safe to cache for the life time of the app since the
- // user must logout to change the DPI setting. This value also applies
- // to all screens.
- dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
- dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY);
- }
- return Size(dpi_x, dpi_y);
+int GetDPIFromScalingFactor(float device_scaling_factor) {
+ return static_cast<float>(kDefaultDPI) * device_scaling_factor;
}
float GetDPIScale() {
if (gfx::Display::HasForceDeviceScaleFactor())
return gfx::Display::GetForcedDeviceScaleFactor();
- float dpi_scale = GetUnforcedDeviceScaleFactor();
- if (dpi_scale <= 1.25) {
- // Force 125% and below to 100% scale. We do this to maintain previous
- // (non-DPI-aware) behavior where only the font size was boosted.
- dpi_scale = 1.0;
- }
- return dpi_scale;
-}
-namespace win {
-
-Point ScreenToDIPPoint(const Point& pixel_point) {
- return ScaleToFlooredPoint(pixel_point, 1.0f / GetDPIScale());
-}
-
-Point DIPToScreenPoint(const Point& dip_point) {
- return ScaleToFlooredPoint(dip_point, GetDPIScale());
-}
-
-Rect ScreenToDIPRect(const Rect& pixel_bounds) {
- // It's important we scale the origin and size separately. If we instead
- // calculated the size from the floored origin and ceiled right the size could
- // vary depending upon where the two points land. That would cause problems
- // for the places this code is used (in particular mapping from native window
- // bounds to DIPs).
- return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
- ScreenToDIPSize(pixel_bounds.size()));
-}
+ if (g_device_scale_factor)
+ return g_device_scale_factor;
-Rect DIPToScreenRect(const Rect& dip_bounds) {
- // See comment in ScreenToDIPRect for why we calculate size like this.
- return Rect(DIPToScreenPoint(dip_bounds.origin()),
- DIPToScreenSize(dip_bounds.size()));
-}
+ static float device_scale_factor = 0.0f;
+ if (!device_scale_factor) {
+ base::win::ScopedGetDC screen_dc(NULL);
+ int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
+ DCHECK(dpi_x == GetDeviceCaps(screen_dc, LOGPIXELSX));
+ device_scale_factor =
+ static_cast<float>(dpi_x) / static_cast<float>(kDefaultDPI);
+ if (device_scale_factor <= 1.25) {
+ // Force 125% and below to 100% scale. We do this to maintain previous
+ // (non-DPI-aware) behavior where only the font size was boosted.
+ device_scale_factor = 1.0;
+ }
+ }
-Size ScreenToDIPSize(const Size& size_in_pixels) {
- // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
- return ScaleToCeiledSize(size_in_pixels, 1.0f / GetDPIScale());
+ return device_scale_factor;
}
-Size DIPToScreenSize(const Size& dip_size) {
- // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
- return ScaleToCeiledSize(dip_size, GetDPIScale());
-}
+namespace win {
int GetSystemMetricsInDIP(int metric) {
return static_cast<int>(GetSystemMetrics(metric) / GetDPIScale() + 0.5);

Powered by Google App Engine
This is Rietveld 408576698