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_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/geometry/size_conversions.h"
16 #include "ui/gfx/geometry/size_f.h"
19 #include "ui/aura/window_tree_host.h"
20 #include "ui/gfx/win/dpi.h"
26 bool use_125_dsf_for_ui_scaling
= false;
28 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
29 // Returns true when |bounds| is found.
30 bool GetDisplayBounds(
31 const std::string
& spec
, gfx::Rect
* bounds
, float* device_scale_factor
) {
36 if (sscanf(spec
.c_str(), "%dx%d*%f",
37 &width
, &height
, device_scale_factor
) >= 2 ||
38 sscanf(spec
.c_str(), "%d+%d-%dx%d*%f", &x
, &y
, &width
, &height
,
39 device_scale_factor
) >= 4) {
40 bounds
->SetRect(x
, y
, width
, height
);
46 // Display mode list is sorted by:
47 // * the area in pixels in ascending order
48 // * refresh rate in descending order
49 struct DisplayModeSorter
{
50 explicit DisplayModeSorter(bool is_internal
) : is_internal(is_internal
) {}
52 bool operator()(const DisplayMode
& a
, const DisplayMode
& b
) {
53 gfx::Size size_a_dip
= a
.GetSizeInDIP(is_internal
);
54 gfx::Size size_b_dip
= b
.GetSizeInDIP(is_internal
);
55 if (size_a_dip
.GetArea() == size_b_dip
.GetArea())
56 return (a
.refresh_rate
> b
.refresh_rate
);
57 return (size_a_dip
.GetArea() < size_b_dip
.GetArea());
65 DisplayMode::DisplayMode()
70 device_scale_factor(1.0f
) {}
72 DisplayMode::DisplayMode(const gfx::Size
& size
,
77 refresh_rate(refresh_rate
),
78 interlaced(interlaced
),
81 device_scale_factor(1.0f
) {}
83 gfx::Size
DisplayMode::GetSizeInDIP(bool is_internal
) const {
84 gfx::SizeF
size_dip(size
);
85 size_dip
.Scale(ui_scale
);
86 // DSF=1.25 is special. The screen is drawn with DSF=1.25 in some mode but it
87 // doesn't affect the screen size computation.
88 if (!(use_125_dsf_for_ui_scaling
&& is_internal
) ||
89 device_scale_factor
!= 1.25f
) {
90 size_dip
.Scale(1.0f
/ device_scale_factor
);
92 return gfx::ToFlooredSize(size_dip
);
95 bool DisplayMode::IsEquivalent(const DisplayMode
& other
) const {
96 const float kEpsilon
= 0.0001f
;
97 return size
== other
.size
&&
98 std::abs(ui_scale
- other
.ui_scale
) < kEpsilon
&&
99 std::abs(device_scale_factor
- other
.device_scale_factor
) < kEpsilon
;
103 DisplayInfo
DisplayInfo::CreateFromSpec(const std::string
& spec
) {
104 return CreateFromSpecWithID(spec
, gfx::Display::kInvalidDisplayID
);
108 void DisplayInfo::SetUse125DSFForUIScaling(bool enable
) {
109 use_125_dsf_for_ui_scaling
= enable
;
113 DisplayInfo
DisplayInfo::CreateFromSpecWithID(const std::string
& spec
,
115 // Use larger than max int to catch overflow early.
116 static int64 synthesized_display_id
= 2200000000LL;
119 gfx::Rect
bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
121 // Default bounds for a display.
122 const int kDefaultHostWindowX
= 200;
123 const int kDefaultHostWindowY
= 200;
124 const int kDefaultHostWindowWidth
= 1366;
125 const int kDefaultHostWindowHeight
= 768;
126 gfx::Rect
bounds_in_native(kDefaultHostWindowX
, kDefaultHostWindowY
,
127 kDefaultHostWindowWidth
, kDefaultHostWindowHeight
);
129 std::string main_spec
= spec
;
131 float ui_scale
= 1.0f
;
132 std::vector
<std::string
> parts
;
133 if (Tokenize(main_spec
, "@", &parts
) == 2) {
134 double scale_in_double
= 0;
135 if (base::StringToDouble(parts
[1], &scale_in_double
))
136 ui_scale
= scale_in_double
;
137 main_spec
= parts
[0];
140 size_t count
= Tokenize(main_spec
, "/", &parts
);
141 gfx::Display::Rotation
rotation(gfx::Display::ROTATE_0
);
142 bool has_overscan
= false;
144 main_spec
= parts
[0];
146 std::string options
= parts
[1];
147 for (size_t i
= 0; i
< options
.size(); ++i
) {
153 case 'r': // rotate 90 degrees to 'right'.
154 rotation
= gfx::Display::ROTATE_90
;
156 case 'u': // 180 degrees, 'u'pside-down.
157 rotation
= gfx::Display::ROTATE_180
;
159 case 'l': // rotate 90 degrees to 'left'.
160 rotation
= gfx::Display::ROTATE_270
;
167 float device_scale_factor
= 1.0f
;
168 if (!GetDisplayBounds(main_spec
, &bounds_in_native
, &device_scale_factor
)) {
170 device_scale_factor
= gfx::GetDPIScale();
174 std::vector
<DisplayMode
> display_modes
;
175 if (Tokenize(main_spec
, "#", &parts
) == 2) {
176 size_t native_mode
= 0;
177 int largest_area
= -1;
178 float highest_refresh_rate
= -1.0f
;
179 main_spec
= parts
[0];
180 std::string resolution_list
= parts
[1];
181 count
= Tokenize(resolution_list
, "|", &parts
);
182 for (size_t i
= 0; i
< count
; ++i
) {
184 gfx::Rect mode_bounds
;
185 std::vector
<std::string
> resolution
;
186 Tokenize(parts
[i
], "%", &resolution
);
187 if (GetDisplayBounds(
188 resolution
[0], &mode_bounds
, &mode
.device_scale_factor
)) {
189 mode
.size
= mode_bounds
.size();
190 if (resolution
.size() > 1)
191 sscanf(resolution
[1].c_str(), "%f", &mode
.refresh_rate
);
192 if (mode
.size
.GetArea() >= largest_area
&&
193 mode
.refresh_rate
> highest_refresh_rate
) {
194 // Use mode with largest area and highest refresh rate as native.
195 largest_area
= mode
.size
.GetArea();
196 highest_refresh_rate
= mode
.refresh_rate
;
199 display_modes
.push_back(mode
);
202 display_modes
[native_mode
].native
= true;
205 if (id
== gfx::Display::kInvalidDisplayID
)
206 id
= synthesized_display_id
++;
207 DisplayInfo
display_info(
208 id
, base::StringPrintf("Display-%d", static_cast<int>(id
)), has_overscan
);
209 display_info
.set_device_scale_factor(device_scale_factor
);
210 display_info
.SetRotation(rotation
, gfx::Display::ROTATION_SOURCE_ACTIVE
);
211 display_info
.set_configured_ui_scale(ui_scale
);
212 display_info
.SetBounds(bounds_in_native
);
213 display_info
.SetDisplayModes(display_modes
);
215 // To test the overscan, it creates the default 5% overscan.
217 int width
= bounds_in_native
.width() / device_scale_factor
/ 40;
218 int height
= bounds_in_native
.height() / device_scale_factor
/ 40;
219 display_info
.SetOverscanInsets(gfx::Insets(height
, width
, height
, width
));
220 display_info
.UpdateDisplaySize();
223 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info
.ToString()
224 << ", spec=" << spec
;
228 DisplayInfo::DisplayInfo()
229 : id_(gfx::Display::kInvalidDisplayID
),
230 has_overscan_(false),
231 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
233 device_scale_factor_(1.0f
),
234 overscan_insets_in_dip_(0, 0, 0, 0),
235 configured_ui_scale_(1.0f
),
237 is_aspect_preserving_scaling_(false),
238 clear_overscan_insets_(false),
239 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
242 DisplayInfo::DisplayInfo(int64 id
,
243 const std::string
& name
,
247 has_overscan_(has_overscan
),
248 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
250 device_scale_factor_(1.0f
),
251 overscan_insets_in_dip_(0, 0, 0, 0),
252 configured_ui_scale_(1.0f
),
254 is_aspect_preserving_scaling_(false),
255 clear_overscan_insets_(false),
256 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
259 DisplayInfo::~DisplayInfo() {
262 void DisplayInfo::SetRotation(gfx::Display::Rotation rotation
,
263 gfx::Display::RotationSource source
) {
264 rotations_
[source
] = rotation
;
265 rotations_
[gfx::Display::ROTATION_SOURCE_ACTIVE
] = rotation
;
268 gfx::Display::Rotation
DisplayInfo::GetActiveRotation() const {
269 return GetRotation(gfx::Display::ROTATION_SOURCE_ACTIVE
);
272 gfx::Display::Rotation
DisplayInfo::GetRotation(
273 gfx::Display::RotationSource source
) const {
274 if (rotations_
.find(source
) == rotations_
.end())
275 return gfx::Display::ROTATE_0
;
276 return rotations_
.at(source
);
279 void DisplayInfo::Copy(const DisplayInfo
& native_info
) {
280 DCHECK(id_
== native_info
.id_
);
281 name_
= native_info
.name_
;
282 has_overscan_
= native_info
.has_overscan_
;
284 touch_support_
= native_info
.touch_support_
;
285 touch_device_id_
= native_info
.touch_device_id_
;
286 device_scale_factor_
= native_info
.device_scale_factor_
;
287 DCHECK(!native_info
.bounds_in_native_
.IsEmpty());
288 bounds_in_native_
= native_info
.bounds_in_native_
;
289 size_in_pixel_
= native_info
.size_in_pixel_
;
290 is_aspect_preserving_scaling_
= native_info
.is_aspect_preserving_scaling_
;
291 display_modes_
= native_info
.display_modes_
;
292 available_color_profiles_
= native_info
.available_color_profiles_
;
294 // Rotation, ui_scale, color_profile and overscan are given by preference,
295 // or unit tests. Don't copy if this native_info came from
296 // DisplayChangeObserver.
297 if (!native_info
.native()) {
298 // Update the overscan_insets_in_dip_ either if the inset should be
299 // cleared, or has non empty insts.
300 if (native_info
.clear_overscan_insets())
301 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
302 else if (!native_info
.overscan_insets_in_dip_
.empty())
303 overscan_insets_in_dip_
= native_info
.overscan_insets_in_dip_
;
305 rotations_
= native_info
.rotations_
;
306 configured_ui_scale_
= native_info
.configured_ui_scale_
;
307 color_profile_
= native_info
.color_profile();
311 void DisplayInfo::SetBounds(const gfx::Rect
& new_bounds_in_native
) {
312 bounds_in_native_
= new_bounds_in_native
;
313 size_in_pixel_
= new_bounds_in_native
.size();
317 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
318 if (Use125DSFRorUIScaling() && device_scale_factor_
== 1.25f
)
319 return (configured_ui_scale_
== 0.8f
) ? 1.25f
: 1.0f
;
320 if (device_scale_factor_
== configured_ui_scale_
)
322 return device_scale_factor_
;
325 float DisplayInfo::GetEffectiveUIScale() const {
326 if (Use125DSFRorUIScaling() && device_scale_factor_
== 1.25f
)
327 return (configured_ui_scale_
== 0.8f
) ? 1.0f
: configured_ui_scale_
;
328 if (device_scale_factor_
== configured_ui_scale_
)
330 return configured_ui_scale_
;
333 void DisplayInfo::UpdateDisplaySize() {
334 size_in_pixel_
= bounds_in_native_
.size();
335 if (!overscan_insets_in_dip_
.empty()) {
336 gfx::Insets insets_in_pixel
=
337 overscan_insets_in_dip_
.Scale(device_scale_factor_
);
338 size_in_pixel_
.Enlarge(-insets_in_pixel
.width(), -insets_in_pixel
.height());
340 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
343 if (GetActiveRotation() == gfx::Display::ROTATE_90
||
344 GetActiveRotation() == gfx::Display::ROTATE_270
) {
345 size_in_pixel_
.SetSize(size_in_pixel_
.height(), size_in_pixel_
.width());
347 gfx::SizeF
size_f(size_in_pixel_
);
348 size_f
.Scale(GetEffectiveUIScale());
349 size_in_pixel_
= gfx::ToFlooredSize(size_f
);
352 void DisplayInfo::SetOverscanInsets(const gfx::Insets
& insets_in_dip
) {
353 overscan_insets_in_dip_
= insets_in_dip
;
356 gfx::Insets
DisplayInfo::GetOverscanInsetsInPixel() const {
357 return overscan_insets_in_dip_
.Scale(device_scale_factor_
);
360 void DisplayInfo::SetDisplayModes(
361 const std::vector
<DisplayMode
>& display_modes
) {
362 display_modes_
= display_modes
;
363 std::sort(display_modes_
.begin(), display_modes_
.end(),
364 DisplayModeSorter(id_
== gfx::Display::InternalDisplayId()));
367 gfx::Size
DisplayInfo::GetNativeModeSize() const {
368 for (size_t i
= 0; i
< display_modes_
.size(); ++i
) {
369 if (display_modes_
[i
].native
)
370 return display_modes_
[i
].size
;
376 std::string
DisplayInfo::ToString() const {
377 int rotation_degree
= static_cast<int>(GetActiveRotation()) * 90;
378 return base::StringPrintf(
379 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
380 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
381 "touch-device-id=%d",
382 static_cast<long long int>(id_
),
383 bounds_in_native_
.ToString().c_str(),
384 size_in_pixel_
.ToString().c_str(),
385 device_scale_factor_
,
386 overscan_insets_in_dip_
.ToString().c_str(),
388 configured_ui_scale_
,
389 touch_support_
== gfx::Display::TOUCH_SUPPORT_AVAILABLE
391 : touch_support_
== gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
397 std::string
DisplayInfo::ToFullString() const {
398 std::string display_modes_str
;
399 std::vector
<DisplayMode
>::const_iterator iter
= display_modes_
.begin();
400 for (; iter
!= display_modes_
.end(); ++iter
) {
401 if (!display_modes_str
.empty())
402 display_modes_str
+= ",";
403 base::StringAppendF(&display_modes_str
,
408 iter
->interlaced
? 'I' : 'P',
409 iter
->native
? "(N)" : "");
411 return ToString() + ", display_modes==" + display_modes_str
;
414 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile
) {
415 if (IsColorProfileAvailable(profile
))
416 color_profile_
= profile
;
419 bool DisplayInfo::IsColorProfileAvailable(
420 ui::ColorCalibrationProfile profile
) const {
421 return std::find(available_color_profiles_
.begin(),
422 available_color_profiles_
.end(),
423 profile
) != available_color_profiles_
.end();
426 bool DisplayInfo::Use125DSFRorUIScaling() const {
427 return use_125_dsf_for_ui_scaling
&& id_
== gfx::Display::InternalDisplayId();