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

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: Fixed Other Unit Tests - Moved Inner Classes Outside Created 4 years, 11 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 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"
11 #include "ui/gfx/geometry/rect_conversions.h"
12 #include "ui/gfx/geometry/size_conversions.h"
13 10
14 namespace { 11 namespace {
15 12
16 const float kDefaultDPI = 96.f; 13 const float kDefaultDPI = 96.f;
17 14
18 float g_device_scale_factor = 0.f; 15 float g_device_scale_factor = 0.f;
19 16
20 float GetUnforcedDeviceScaleFactor() { 17 float GetUnforcedDeviceScaleFactor() {
21 return g_device_scale_factor ? 18 if (g_device_scale_factor)
22 g_device_scale_factor : 19 return g_device_scale_factor;
23 static_cast<float>(gfx::GetDPI().width()) / kDefaultDPI; 20
21 static float device_scale_factor = 0.0f;
22 if (!device_scale_factor) {
23 base::win::ScopedGetDC screen_dc(NULL);
24 int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
25 DCHECK(dpi_x == GetDeviceCaps(screen_dc, LOGPIXELSY));
26 device_scale_factor = static_cast<float>(dpi_x) / kDefaultDPI;
27 }
28 return device_scale_factor;
24 } 29 }
25 30
26 } // namespace 31 } // namespace
27 32
28 namespace gfx { 33 namespace gfx {
29 34
30 void SetDefaultDeviceScaleFactor(float scale) { 35 void SetDefaultDeviceScaleFactor(float scale) {
31 DCHECK_NE(0.f, scale); 36 DCHECK_NE(0.f, scale);
32 g_device_scale_factor = scale; 37 g_device_scale_factor = scale;
33 } 38 }
34 39
35 Size GetDPI() { 40 int GetDPIFromScalingFactor(float device_scaling_factor) {
36 static int dpi_x = 0; 41 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 } 42 }
51 43
52 float GetDPIScale() { 44 float GetDPIScale() {
53 if (gfx::Display::HasForceDeviceScaleFactor()) 45 if (gfx::Display::HasForceDeviceScaleFactor())
54 return gfx::Display::GetForcedDeviceScaleFactor(); 46 return gfx::Display::GetForcedDeviceScaleFactor();
55 float dpi_scale = GetUnforcedDeviceScaleFactor(); 47 float dpi_scale = GetUnforcedDeviceScaleFactor();
56 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; 48 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale;
57 } 49 }
58 50
59 namespace win { 51 namespace win {
60 52
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) { 53 int GetSystemMetricsInDIP(int metric) {
96 // The system metrics always reflect the system DPI, not whatever scale we've 54 // The system metrics always reflect the system DPI, not whatever scale we've
97 // forced or decided to use. 55 // forced or decided to use.
98 return static_cast<int>( 56 return static_cast<int>(
99 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); 57 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor()));
100 } 58 }
101 59
102 } // namespace win 60 } // namespace win
103 } // namespace gfx 61 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698