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

Side by Side 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, 1 month 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 unified diff | Download patch
OLDNEW
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 int kDefaultDPI = 96; 17 int kDefaultDPI = 96;
17 18
18 float g_device_scale_factor = 0.0f; 19 float g_device_scale_factor = 0.0f;
19 20
20 float GetUnforcedDeviceScaleFactor() {
21 // If the global device scale factor is initialized use it. This is to ensure
22 // we use the same scale factor across all callsites.
23 if (g_device_scale_factor)
24 return g_device_scale_factor;
25 return static_cast<float>(gfx::GetDPI().width()) /
26 static_cast<float>(kDefaultDPI);
27 }
28
29 } // namespace 21 } // namespace
30 22
31 namespace gfx { 23 namespace gfx {
32 24
33 void InitDeviceScaleFactor(float scale) { 25 void InitDeviceScaleFactor(float scale) {
34 DCHECK_NE(0.0f, scale); 26 DCHECK_NE(0.0f, scale);
35 g_device_scale_factor = scale; 27 g_device_scale_factor = scale;
36 } 28 }
37 29
38 Size GetDPI() { 30 int GetDPIFromScalingFactor(float device_scaling_factor) {
39 static int dpi_x = 0; 31 return static_cast<float>(kDefaultDPI) * device_scaling_factor;
40 static int dpi_y = 0;
41 static bool should_initialize = true;
42
43 if (should_initialize) {
44 should_initialize = false;
45 base::win::ScopedGetDC screen_dc(NULL);
46 // This value is safe to cache for the life time of the app since the
47 // user must logout to change the DPI setting. This value also applies
48 // to all screens.
49 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
50 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY);
51 }
52 return Size(dpi_x, dpi_y);
53 } 32 }
54 33
55 float GetDPIScale() { 34 float GetDPIScale() {
56 if (gfx::Display::HasForceDeviceScaleFactor()) 35 if (gfx::Display::HasForceDeviceScaleFactor())
57 return gfx::Display::GetForcedDeviceScaleFactor(); 36 return gfx::Display::GetForcedDeviceScaleFactor();
58 float dpi_scale = GetUnforcedDeviceScaleFactor(); 37
59 if (dpi_scale <= 1.25) { 38 if (g_device_scale_factor)
60 // Force 125% and below to 100% scale. We do this to maintain previous 39 return g_device_scale_factor;
61 // (non-DPI-aware) behavior where only the font size was boosted. 40
62 dpi_scale = 1.0; 41 static float device_scale_factor = 0.0f;
42 if (!device_scale_factor) {
43 base::win::ScopedGetDC screen_dc(NULL);
44 int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
45 DCHECK(dpi_x == GetDeviceCaps(screen_dc, LOGPIXELSX));
46 device_scale_factor =
47 static_cast<float>(dpi_x) / static_cast<float>(kDefaultDPI);
48 if (device_scale_factor <= 1.25) {
49 // Force 125% and below to 100% scale. We do this to maintain previous
50 // (non-DPI-aware) behavior where only the font size was boosted.
51 device_scale_factor = 1.0;
52 }
63 } 53 }
64 return dpi_scale; 54
55 return device_scale_factor;
65 } 56 }
66 57
67 namespace win { 58 namespace win {
68 59
69 Point ScreenToDIPPoint(const Point& pixel_point) {
70 return ScaleToFlooredPoint(pixel_point, 1.0f / GetDPIScale());
71 }
72
73 Point DIPToScreenPoint(const Point& dip_point) {
74 return ScaleToFlooredPoint(dip_point, GetDPIScale());
75 }
76
77 Rect ScreenToDIPRect(const Rect& pixel_bounds) {
78 // It's important we scale the origin and size separately. If we instead
79 // calculated the size from the floored origin and ceiled right the size could
80 // vary depending upon where the two points land. That would cause problems
81 // for the places this code is used (in particular mapping from native window
82 // bounds to DIPs).
83 return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
84 ScreenToDIPSize(pixel_bounds.size()));
85 }
86
87 Rect DIPToScreenRect(const Rect& dip_bounds) {
88 // See comment in ScreenToDIPRect for why we calculate size like this.
89 return Rect(DIPToScreenPoint(dip_bounds.origin()),
90 DIPToScreenSize(dip_bounds.size()));
91 }
92
93 Size ScreenToDIPSize(const Size& size_in_pixels) {
94 // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
95 return ScaleToCeiledSize(size_in_pixels, 1.0f / GetDPIScale());
96 }
97
98 Size DIPToScreenSize(const Size& dip_size) {
99 // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
100 return ScaleToCeiledSize(dip_size, GetDPIScale());
101 }
102
103 int GetSystemMetricsInDIP(int metric) { 60 int GetSystemMetricsInDIP(int metric) {
104 return static_cast<int>(GetSystemMetrics(metric) / GetDPIScale() + 0.5); 61 return static_cast<int>(GetSystemMetrics(metric) / GetDPIScale() + 0.5);
105 } 62 }
106 63
107 } // namespace win 64 } // namespace win
108 } // namespace gfx 65 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698