1 // Copyright (c) 2013 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.
9 #include "ash/display/display_info.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "ui/gfx/display.h"
16 #include "ui/gfx/geometry/size_conversions.h"
17 #include "ui/gfx/geometry/size_f.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/gfx/win/dpi.h"
27 bool use_125_dsf_for_ui_scaling
= false;
29 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
30 // Returns true when |bounds| is found.
31 bool GetDisplayBounds(
32 const std::string
& spec
, gfx::Rect
* bounds
, float* device_scale_factor
) {
37 if (sscanf(spec
.c_str(), "%dx%d*%f",
38 &width
, &height
, device_scale_factor
) >= 2 ||
39 sscanf(spec
.c_str(), "%d+%d-%dx%d*%f", &x
, &y
, &width
, &height
,
40 device_scale_factor
) >= 4) {
41 bounds
->SetRect(x
, y
, width
, height
);
47 // Display mode list is sorted by:
48 // * the area in pixels in ascending order
49 // * refresh rate in descending order
50 struct DisplayModeSorter
{
51 explicit DisplayModeSorter(bool is_internal
) : is_internal(is_internal
) {}
53 bool operator()(const DisplayMode
& a
, const DisplayMode
& b
) {
54 gfx::Size size_a_dip
= a
.GetSizeInDIP(is_internal
);
55 gfx::Size size_b_dip
= b
.GetSizeInDIP(is_internal
);
56 if (size_a_dip
.GetArea() == size_b_dip
.GetArea())
57 return (a
.refresh_rate
> b
.refresh_rate
);
58 return (size_a_dip
.GetArea() < size_b_dip
.GetArea());
66 DisplayMode::DisplayMode()
71 device_scale_factor(1.0f
) {}
73 DisplayMode::DisplayMode(const gfx::Size
& size
,
78 refresh_rate(refresh_rate
),
79 interlaced(interlaced
),
82 device_scale_factor(1.0f
) {}
84 gfx::Size
DisplayMode::GetSizeInDIP(bool is_internal
) const {
85 gfx::SizeF
size_dip(size
);
86 size_dip
.Scale(ui_scale
);
87 // DSF=1.25 is special. The screen is drawn with DSF=1.25 in some mode but it
88 // doesn't affect the screen size computation.
89 if (!(use_125_dsf_for_ui_scaling
&& is_internal
) ||
90 device_scale_factor
!= 1.25f
) {
91 size_dip
.Scale(1.0f
/ device_scale_factor
);
93 return gfx::ToFlooredSize(size_dip
);
96 bool DisplayMode::IsEquivalent(const DisplayMode
& other
) const {
97 const float kEpsilon
= 0.0001f
;
98 return size
== other
.size
&&
99 std::abs(ui_scale
- other
.ui_scale
) < kEpsilon
&&
100 std::abs(device_scale_factor
- other
.device_scale_factor
) < kEpsilon
;
104 DisplayInfo
DisplayInfo::CreateFromSpec(const std::string
& spec
) {
105 return CreateFromSpecWithID(spec
, gfx::Display::kInvalidDisplayID
);
109 void DisplayInfo::SetUse125DSFForUIScaling(bool enable
) {
110 use_125_dsf_for_ui_scaling
= enable
;
114 DisplayInfo
DisplayInfo::CreateFromSpecWithID(const std::string
& spec
,
116 // Use larger than max int to catch overflow early.
117 static int64 synthesized_display_id
= 2200000000LL;
120 gfx::Rect
bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
122 // Default bounds for a display.
123 const int kDefaultHostWindowX
= 200;
124 const int kDefaultHostWindowY
= 200;
125 const int kDefaultHostWindowWidth
= 1366;
126 const int kDefaultHostWindowHeight
= 768;
127 gfx::Rect
bounds_in_native(kDefaultHostWindowX
, kDefaultHostWindowY
,
128 kDefaultHostWindowWidth
, kDefaultHostWindowHeight
);
130 std::string main_spec
= spec
;
132 float ui_scale
= 1.0f
;
133 std::vector
<std::string
> parts
= base::SplitString(
134 main_spec
, "@", base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
135 if (parts
.size() == 2) {
136 double scale_in_double
= 0;
137 if (base::StringToDouble(parts
[1], &scale_in_double
))
138 ui_scale
= scale_in_double
;
139 main_spec
= parts
[0];
142 parts
= base::SplitString(main_spec
, "/", base::KEEP_WHITESPACE
,
143 base::SPLIT_WANT_NONEMPTY
);
144 gfx::Display::Rotation
rotation(gfx::Display::ROTATE_0
);
145 bool has_overscan
= false;
146 if (!parts
.empty()) {
147 main_spec
= parts
[0];
148 if (parts
.size() >= 2) {
149 std::string options
= parts
[1];
150 for (size_t i
= 0; i
< options
.size(); ++i
) {
156 case 'r': // rotate 90 degrees to 'right'.
157 rotation
= gfx::Display::ROTATE_90
;
159 case 'u': // 180 degrees, 'u'pside-down.
160 rotation
= gfx::Display::ROTATE_180
;
162 case 'l': // rotate 90 degrees to 'left'.
163 rotation
= gfx::Display::ROTATE_270
;
170 float device_scale_factor
= 1.0f
;
171 if (!GetDisplayBounds(main_spec
, &bounds_in_native
, &device_scale_factor
)) {
173 device_scale_factor
= gfx::GetDPIScale();
177 std::vector
<DisplayMode
> display_modes
;
178 parts
= base::SplitString(main_spec
, "#", base::KEEP_WHITESPACE
,
179 base::SPLIT_WANT_NONEMPTY
);
180 if (parts
.size() == 2) {
181 size_t native_mode
= 0;
182 int largest_area
= -1;
183 float highest_refresh_rate
= -1.0f
;
184 main_spec
= parts
[0];
185 std::string resolution_list
= parts
[1];
186 parts
= base::SplitString(resolution_list
, "|", base::KEEP_WHITESPACE
,
187 base::SPLIT_WANT_NONEMPTY
);
188 for (size_t i
= 0; i
< parts
.size(); ++i
) {
190 gfx::Rect mode_bounds
;
191 std::vector
<std::string
> resolution
= base::SplitString(
192 parts
[i
], "%", base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
193 if (GetDisplayBounds(
194 resolution
[0], &mode_bounds
, &mode
.device_scale_factor
)) {
195 mode
.size
= mode_bounds
.size();
196 if (resolution
.size() > 1)
197 sscanf(resolution
[1].c_str(), "%f", &mode
.refresh_rate
);
198 if (mode
.size
.GetArea() >= largest_area
&&
199 mode
.refresh_rate
> highest_refresh_rate
) {
200 // Use mode with largest area and highest refresh rate as native.
201 largest_area
= mode
.size
.GetArea();
202 highest_refresh_rate
= mode
.refresh_rate
;
205 display_modes
.push_back(mode
);
208 display_modes
[native_mode
].native
= true;
211 if (id
== gfx::Display::kInvalidDisplayID
)
212 id
= synthesized_display_id
++;
213 DisplayInfo
display_info(
214 id
, base::StringPrintf("Display-%d", static_cast<int>(id
)), has_overscan
);
215 display_info
.set_device_scale_factor(device_scale_factor
);
216 display_info
.SetRotation(rotation
, gfx::Display::ROTATION_SOURCE_ACTIVE
);
217 display_info
.set_configured_ui_scale(ui_scale
);
218 display_info
.SetBounds(bounds_in_native
);
219 display_info
.SetDisplayModes(display_modes
);
221 // To test the overscan, it creates the default 5% overscan.
223 int width
= bounds_in_native
.width() / device_scale_factor
/ 40;
224 int height
= bounds_in_native
.height() / device_scale_factor
/ 40;
225 display_info
.SetOverscanInsets(gfx::Insets(height
, width
, height
, width
));
226 display_info
.UpdateDisplaySize();
229 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info
.ToString()
230 << ", spec=" << spec
;
234 DisplayInfo::DisplayInfo()
235 : id_(gfx::Display::kInvalidDisplayID
),
236 has_overscan_(false),
237 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
239 device_scale_factor_(1.0f
),
240 overscan_insets_in_dip_(0, 0, 0, 0),
241 configured_ui_scale_(1.0f
),
243 is_aspect_preserving_scaling_(false),
244 clear_overscan_insets_(false),
245 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
248 DisplayInfo::DisplayInfo(int64 id
,
249 const std::string
& name
,
253 has_overscan_(has_overscan
),
254 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
256 device_scale_factor_(1.0f
),
257 overscan_insets_in_dip_(0, 0, 0, 0),
258 configured_ui_scale_(1.0f
),
260 is_aspect_preserving_scaling_(false),
261 clear_overscan_insets_(false),
262 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
265 DisplayInfo::~DisplayInfo() {
268 void DisplayInfo::SetRotation(gfx::Display::Rotation rotation
,
269 gfx::Display::RotationSource source
) {
270 rotations_
[source
] = rotation
;
271 rotations_
[gfx::Display::ROTATION_SOURCE_ACTIVE
] = rotation
;
274 gfx::Display::Rotation
DisplayInfo::GetActiveRotation() const {
275 return GetRotation(gfx::Display::ROTATION_SOURCE_ACTIVE
);
278 gfx::Display::Rotation
DisplayInfo::GetRotation(
279 gfx::Display::RotationSource source
) const {
280 if (rotations_
.find(source
) == rotations_
.end())
281 return gfx::Display::ROTATE_0
;
282 return rotations_
.at(source
);
285 void DisplayInfo::Copy(const DisplayInfo
& native_info
) {
286 DCHECK(id_
== native_info
.id_
);
287 name_
= native_info
.name_
;
288 has_overscan_
= native_info
.has_overscan_
;
290 touch_support_
= native_info
.touch_support_
;
291 touch_device_id_
= native_info
.touch_device_id_
;
292 device_scale_factor_
= native_info
.device_scale_factor_
;
293 DCHECK(!native_info
.bounds_in_native_
.IsEmpty());
294 bounds_in_native_
= native_info
.bounds_in_native_
;
295 size_in_pixel_
= native_info
.size_in_pixel_
;
296 is_aspect_preserving_scaling_
= native_info
.is_aspect_preserving_scaling_
;
297 display_modes_
= native_info
.display_modes_
;
298 available_color_profiles_
= native_info
.available_color_profiles_
;
300 // Rotation, ui_scale, color_profile and overscan are given by preference,
301 // or unit tests. Don't copy if this native_info came from
302 // DisplayChangeObserver.
303 if (!native_info
.native()) {
304 // Update the overscan_insets_in_dip_ either if the inset should be
305 // cleared, or has non empty insts.
306 if (native_info
.clear_overscan_insets())
307 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
308 else if (!native_info
.overscan_insets_in_dip_
.empty())
309 overscan_insets_in_dip_
= native_info
.overscan_insets_in_dip_
;
311 rotations_
= native_info
.rotations_
;
312 configured_ui_scale_
= native_info
.configured_ui_scale_
;
313 color_profile_
= native_info
.color_profile();
317 void DisplayInfo::SetBounds(const gfx::Rect
& new_bounds_in_native
) {
318 bounds_in_native_
= new_bounds_in_native
;
319 size_in_pixel_
= new_bounds_in_native
.size();
323 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
324 if (Use125DSFRorUIScaling() && device_scale_factor_
== 1.25f
)
325 return (configured_ui_scale_
== 0.8f
) ? 1.25f
: 1.0f
;
326 if (device_scale_factor_
== configured_ui_scale_
)
328 return device_scale_factor_
;
331 float DisplayInfo::GetEffectiveUIScale() const {
332 if (Use125DSFRorUIScaling() && device_scale_factor_
== 1.25f
)
333 return (configured_ui_scale_
== 0.8f
) ? 1.0f
: configured_ui_scale_
;
334 if (device_scale_factor_
== configured_ui_scale_
)
336 return configured_ui_scale_
;
339 void DisplayInfo::UpdateDisplaySize() {
340 size_in_pixel_
= bounds_in_native_
.size();
341 if (!overscan_insets_in_dip_
.empty()) {
342 gfx::Insets insets_in_pixel
=
343 overscan_insets_in_dip_
.Scale(device_scale_factor_
);
344 size_in_pixel_
.Enlarge(-insets_in_pixel
.width(), -insets_in_pixel
.height());
346 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
349 if (GetActiveRotation() == gfx::Display::ROTATE_90
||
350 GetActiveRotation() == gfx::Display::ROTATE_270
) {
351 size_in_pixel_
.SetSize(size_in_pixel_
.height(), size_in_pixel_
.width());
353 gfx::SizeF
size_f(size_in_pixel_
);
354 size_f
.Scale(GetEffectiveUIScale());
355 size_in_pixel_
= gfx::ToFlooredSize(size_f
);
358 void DisplayInfo::SetOverscanInsets(const gfx::Insets
& insets_in_dip
) {
359 overscan_insets_in_dip_
= insets_in_dip
;
362 gfx::Insets
DisplayInfo::GetOverscanInsetsInPixel() const {
363 return overscan_insets_in_dip_
.Scale(device_scale_factor_
);
366 void DisplayInfo::SetDisplayModes(
367 const std::vector
<DisplayMode
>& display_modes
) {
368 display_modes_
= display_modes
;
369 std::sort(display_modes_
.begin(), display_modes_
.end(),
370 DisplayModeSorter(id_
== gfx::Display::InternalDisplayId()));
373 gfx::Size
DisplayInfo::GetNativeModeSize() const {
374 for (size_t i
= 0; i
< display_modes_
.size(); ++i
) {
375 if (display_modes_
[i
].native
)
376 return display_modes_
[i
].size
;
382 std::string
DisplayInfo::ToString() const {
383 int rotation_degree
= static_cast<int>(GetActiveRotation()) * 90;
384 return base::StringPrintf(
385 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
386 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
387 "touch-device-id=%d",
388 static_cast<long long int>(id_
),
389 bounds_in_native_
.ToString().c_str(),
390 size_in_pixel_
.ToString().c_str(),
391 device_scale_factor_
,
392 overscan_insets_in_dip_
.ToString().c_str(),
394 configured_ui_scale_
,
395 touch_support_
== gfx::Display::TOUCH_SUPPORT_AVAILABLE
397 : touch_support_
== gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
403 std::string
DisplayInfo::ToFullString() const {
404 std::string display_modes_str
;
405 std::vector
<DisplayMode
>::const_iterator iter
= display_modes_
.begin();
406 for (; iter
!= display_modes_
.end(); ++iter
) {
407 if (!display_modes_str
.empty())
408 display_modes_str
+= ",";
409 base::StringAppendF(&display_modes_str
,
414 iter
->interlaced
? 'I' : 'P',
415 iter
->native
? "(N)" : "");
417 return ToString() + ", display_modes==" + display_modes_str
;
420 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile
) {
421 if (IsColorProfileAvailable(profile
))
422 color_profile_
= profile
;
425 bool DisplayInfo::IsColorProfileAvailable(
426 ui::ColorCalibrationProfile profile
) const {
427 return std::find(available_color_profiles_
.begin(),
428 available_color_profiles_
.end(),
429 profile
) != available_color_profiles_
.end();
432 bool DisplayInfo::Use125DSFRorUIScaling() const {
433 return use_125_dsf_for_ui_scaling
&& id_
== gfx::Display::InternalDisplayId();