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

Side by Side Diff: ui/gfx/dpi.cc

Issue 659883002: Enable hidpi on Linux, refactor a bit on Windows to share Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: constants Created 6 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/dpi.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "ui/gfx/display.h"
11 #include "ui/gfx/point_conversions.h"
12 #include "ui/gfx/rect_conversions.h"
13 #include "ui/gfx/size_conversions.h"
14
15 namespace {
16
17 int kDefaultDPI = 96;
18
19 float g_device_scale_factor = 0.0f;
20
21 float GetUnforcedDeviceScaleFactor() {
22 // If the global device scale factor is initialized use it. This is to ensure
23 // we use the same scale factor across all callsites. We don't use the
24 // GetDeviceScaleFactor function here because it fires a DCHECK if the
25 // g_device_scale_factor global is 0.
26 if (g_device_scale_factor)
27 return g_device_scale_factor;
28 return static_cast<float>(gfx::GetDPI().width()) /
29 static_cast<float>(kDefaultDPI);
30 }
31
32 } // namespace
33
34
35 namespace gfx {
36
37 void InitDeviceScaleFactor(float scale) {
38 DCHECK_NE(0.0f, scale);
39 g_device_scale_factor = std::max(1.f, scale);
40 }
41
42 float GetDeviceScaleFactor() {
43 DCHECK_NE(0.0f, g_device_scale_factor);
44 return g_device_scale_factor;
45 }
46
47 bool IsDeviceScaleFactorSet() {
48 return g_device_scale_factor != 0.0f;
49 }
50
51 void ForceHighDPISupportForTesting(float scale) {
52 g_device_scale_factor = scale;
53 }
54
55 bool IsInHighDPIMode() {
56 return GetDPIScale() > 1.0;
57 }
58
59 float GetDPIScale() {
60 if (IsHighDPIEnabled()) {
61 if (gfx::Display::HasForceDeviceScaleFactor())
62 return gfx::Display::GetForcedDeviceScaleFactor();
63 float dpi_scale = GetUnforcedDeviceScaleFactor();
64 if (dpi_scale <= 1.25) {
65 // Force 125% and below to 100% scale. We do this to maintain previous
66 // (non-DPI-aware) behavior where only the font size was boosted.
67 dpi_scale = 1.0;
68 }
69 return dpi_scale;
70 }
71 return 1.0;
72 }
73
74 Point ScreenToDIPPoint(const Point& pixel_point) {
75 return ToFlooredPoint(ScalePoint(pixel_point,
76 1.0f / GetDeviceScaleFactor()));
77 }
78
79 Point DIPToScreenPoint(const Point& dip_point) {
80 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor()));
81 }
82
83 Rect ScreenToDIPRect(const Rect& pixel_bounds) {
84 // It's important we scale the origin and size separately. If we instead
85 // calculated the size from the floored origin and ceiled right the size could
86 // vary depending upon where the two points land. That would cause problems
87 // for the places this code is used (in particular mapping from native window
88 // bounds to DIPs).
89 return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
90 ScreenToDIPSize(pixel_bounds.size()));
91 }
92
93 Rect DIPToScreenRect(const Rect& dip_bounds) {
94 // See comment in ScreenToDIPRect for why we calculate size like this.
95 return Rect(DIPToScreenPoint(dip_bounds.origin()),
96 DIPToScreenSize(dip_bounds.size()));
97 }
98
99 Size ScreenToDIPSize(const Size& size_in_pixels) {
100 // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
101 return ToCeiledSize(
102 ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor()));
103 }
104
105 Size DIPToScreenSize(const Size& dip_size) {
106 // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
107 return ToCeiledSize(ScaleSize(dip_size, GetDeviceScaleFactor()));
108 }
109
110 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698