OLD | NEW |
| (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/display.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "build/build_config.h" | |
14 #include "ui/gfx/geometry/insets.h" | |
15 #include "ui/gfx/geometry/point_conversions.h" | |
16 #include "ui/gfx/geometry/point_f.h" | |
17 #include "ui/gfx/geometry/size_conversions.h" | |
18 #include "ui/gfx/switches.h" | |
19 | |
20 namespace gfx { | |
21 namespace { | |
22 | |
23 // This variable tracks whether the forced device scale factor switch needs to | |
24 // be read from the command line, i.e. if it is set to -1 then the command line | |
25 // is checked. | |
26 int g_has_forced_device_scale_factor = -1; | |
27 | |
28 // This variable caches the forced device scale factor value which is read off | |
29 // the command line. If the cache is invalidated by setting this variable to | |
30 // -1.0, we read the forced device scale factor again. | |
31 float g_forced_device_scale_factor = -1.0; | |
32 | |
33 bool HasForceDeviceScaleFactorImpl() { | |
34 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
35 switches::kForceDeviceScaleFactor); | |
36 } | |
37 | |
38 float GetForcedDeviceScaleFactorImpl() { | |
39 double scale_in_double = 1.0; | |
40 if (HasForceDeviceScaleFactorImpl()) { | |
41 std::string value = | |
42 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
43 switches::kForceDeviceScaleFactor); | |
44 if (!base::StringToDouble(value, &scale_in_double)) { | |
45 LOG(ERROR) << "Failed to parse the default device scale factor:" << value; | |
46 scale_in_double = 1.0; | |
47 } | |
48 } | |
49 return static_cast<float>(scale_in_double); | |
50 } | |
51 | |
52 int64_t internal_display_id_ = -1; | |
53 | |
54 } // namespace | |
55 | |
56 // static | |
57 float Display::GetForcedDeviceScaleFactor() { | |
58 if (g_forced_device_scale_factor < 0) | |
59 g_forced_device_scale_factor = GetForcedDeviceScaleFactorImpl(); | |
60 return g_forced_device_scale_factor; | |
61 } | |
62 | |
63 // static | |
64 bool Display::HasForceDeviceScaleFactor() { | |
65 if (g_has_forced_device_scale_factor == -1) | |
66 g_has_forced_device_scale_factor = HasForceDeviceScaleFactorImpl(); | |
67 return !!g_has_forced_device_scale_factor; | |
68 } | |
69 | |
70 // static | |
71 void Display::ResetForceDeviceScaleFactorForTesting() { | |
72 g_has_forced_device_scale_factor = -1; | |
73 g_forced_device_scale_factor = -1.0; | |
74 } | |
75 | |
76 Display::Display() | |
77 : id_(kInvalidDisplayID), | |
78 device_scale_factor_(GetForcedDeviceScaleFactor()), | |
79 rotation_(ROTATE_0), | |
80 touch_support_(TOUCH_SUPPORT_UNKNOWN) { | |
81 } | |
82 | |
83 Display::Display(const Display& other) = default; | |
84 | |
85 Display::Display(int64_t id) | |
86 : id_(id), | |
87 device_scale_factor_(GetForcedDeviceScaleFactor()), | |
88 rotation_(ROTATE_0), | |
89 touch_support_(TOUCH_SUPPORT_UNKNOWN) {} | |
90 | |
91 Display::Display(int64_t id, const gfx::Rect& bounds) | |
92 : id_(id), | |
93 bounds_(bounds), | |
94 work_area_(bounds), | |
95 device_scale_factor_(GetForcedDeviceScaleFactor()), | |
96 rotation_(ROTATE_0), | |
97 touch_support_(TOUCH_SUPPORT_UNKNOWN) { | |
98 #if defined(USE_AURA) | |
99 SetScaleAndBounds(device_scale_factor_, bounds); | |
100 #endif | |
101 } | |
102 | |
103 Display::~Display() { | |
104 } | |
105 | |
106 int Display::RotationAsDegree() const { | |
107 switch (rotation_) { | |
108 case ROTATE_0: | |
109 return 0; | |
110 case ROTATE_90: | |
111 return 90; | |
112 case ROTATE_180: | |
113 return 180; | |
114 case ROTATE_270: | |
115 return 270; | |
116 } | |
117 | |
118 NOTREACHED(); | |
119 return 0; | |
120 } | |
121 | |
122 void Display::SetRotationAsDegree(int rotation) { | |
123 switch (rotation) { | |
124 case 0: | |
125 rotation_ = ROTATE_0; | |
126 break; | |
127 case 90: | |
128 rotation_ = ROTATE_90; | |
129 break; | |
130 case 180: | |
131 rotation_ = ROTATE_180; | |
132 break; | |
133 case 270: | |
134 rotation_ = ROTATE_270; | |
135 break; | |
136 default: | |
137 // We should not reach that but we will just ignore the call if we do. | |
138 NOTREACHED(); | |
139 } | |
140 } | |
141 | |
142 Insets Display::GetWorkAreaInsets() const { | |
143 return gfx::Insets(work_area_.y() - bounds_.y(), | |
144 work_area_.x() - bounds_.x(), | |
145 bounds_.bottom() - work_area_.bottom(), | |
146 bounds_.right() - work_area_.right()); | |
147 } | |
148 | |
149 void Display::SetScaleAndBounds( | |
150 float device_scale_factor, | |
151 const gfx::Rect& bounds_in_pixel) { | |
152 Insets insets = bounds_.InsetsFrom(work_area_); | |
153 if (!HasForceDeviceScaleFactor()) { | |
154 #if defined(OS_MACOSX) | |
155 // Unless an explicit scale factor was provided for testing, ensure the | |
156 // scale is integral. | |
157 device_scale_factor = static_cast<int>(device_scale_factor); | |
158 #endif | |
159 device_scale_factor_ = device_scale_factor; | |
160 } | |
161 device_scale_factor_ = std::max(1.0f, device_scale_factor_); | |
162 bounds_ = gfx::Rect(gfx::ScaleToFlooredPoint(bounds_in_pixel.origin(), | |
163 1.0f / device_scale_factor_), | |
164 gfx::ScaleToFlooredSize(bounds_in_pixel.size(), | |
165 1.0f / device_scale_factor_)); | |
166 UpdateWorkAreaFromInsets(insets); | |
167 } | |
168 | |
169 void Display::SetSize(const gfx::Size& size_in_pixel) { | |
170 gfx::Point origin = bounds_.origin(); | |
171 #if defined(USE_AURA) | |
172 origin = gfx::ScaleToFlooredPoint(origin, device_scale_factor_); | |
173 #endif | |
174 SetScaleAndBounds(device_scale_factor_, gfx::Rect(origin, size_in_pixel)); | |
175 } | |
176 | |
177 void Display::UpdateWorkAreaFromInsets(const gfx::Insets& insets) { | |
178 work_area_ = bounds_; | |
179 work_area_.Inset(insets); | |
180 } | |
181 | |
182 gfx::Size Display::GetSizeInPixel() const { | |
183 return gfx::ScaleToFlooredSize(size(), device_scale_factor_); | |
184 } | |
185 | |
186 std::string Display::ToString() const { | |
187 return base::StringPrintf( | |
188 "Display[%lld] bounds=%s, workarea=%s, scale=%f, %s", | |
189 static_cast<long long int>(id_), | |
190 bounds_.ToString().c_str(), | |
191 work_area_.ToString().c_str(), | |
192 device_scale_factor_, | |
193 IsInternal() ? "internal" : "external"); | |
194 } | |
195 | |
196 bool Display::IsInternal() const { | |
197 return is_valid() && (id_ == internal_display_id_); | |
198 } | |
199 | |
200 // static | |
201 int64_t Display::InternalDisplayId() { | |
202 DCHECK_NE(kInvalidDisplayID, internal_display_id_); | |
203 return internal_display_id_; | |
204 } | |
205 | |
206 // static | |
207 void Display::SetInternalDisplayId(int64_t internal_display_id) { | |
208 internal_display_id_ = internal_display_id; | |
209 } | |
210 | |
211 // static | |
212 bool Display::IsInternalDisplayId(int64_t display_id) { | |
213 DCHECK_NE(kInvalidDisplayID, display_id); | |
214 return HasInternalDisplay() && internal_display_id_ == display_id; | |
215 } | |
216 | |
217 // static | |
218 bool Display::HasInternalDisplay() { | |
219 return internal_display_id_ != kInvalidDisplayID; | |
220 } | |
221 | |
222 } // namespace gfx | |
OLD | NEW |