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/size_conversions.h"
16 #include "ui/gfx/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
);
48 DisplayMode::DisplayMode()
53 device_scale_factor(1.0f
) {}
55 DisplayMode::DisplayMode(const gfx::Size
& size
,
60 refresh_rate(refresh_rate
),
61 interlaced(interlaced
),
64 device_scale_factor(1.0f
) {}
66 gfx::Size
DisplayMode::GetSizeInDIP() const {
67 gfx::SizeF
size_dip(size
);
68 size_dip
.Scale(ui_scale
);
69 size_dip
.Scale(1.0f
/ device_scale_factor
);
70 return gfx::ToFlooredSize(size_dip
);
73 bool DisplayMode::IsEquivalent(const DisplayMode
& other
) const {
74 const float kEpsilon
= 0.0001f
;
75 return size
== other
.size
&&
76 std::abs(ui_scale
- other
.ui_scale
) < kEpsilon
&&
77 std::abs(device_scale_factor
- other
.device_scale_factor
) < kEpsilon
;
81 DisplayInfo
DisplayInfo::CreateFromSpec(const std::string
& spec
) {
82 return CreateFromSpecWithID(spec
, gfx::Display::kInvalidDisplayID
);
88 void DisplayInfo::SetUse125DSFForUIScaling(bool enable
) {
89 use_125_dsf_for_ui_scaling
= enable
;
93 DisplayInfo
DisplayInfo::CreateFromSpecWithID(const std::string
& spec
,
95 // Default bounds for a display.
96 const int kDefaultHostWindowX
= 200;
97 const int kDefaultHostWindowY
= 200;
98 const int kDefaultHostWindowWidth
= 1366;
99 const int kDefaultHostWindowHeight
= 768;
101 // Use larger than max int to catch overflow early.
102 static int64 synthesized_display_id
= 2200000000LL;
105 gfx::Rect
bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
107 gfx::Rect
bounds_in_native(kDefaultHostWindowX
, kDefaultHostWindowY
,
108 kDefaultHostWindowWidth
, kDefaultHostWindowHeight
);
110 std::string main_spec
= spec
;
112 float ui_scale
= 1.0f
;
113 std::vector
<std::string
> parts
;
114 if (Tokenize(main_spec
, "@", &parts
) == 2) {
115 double scale_in_double
= 0;
116 if (base::StringToDouble(parts
[1], &scale_in_double
))
117 ui_scale
= scale_in_double
;
118 main_spec
= parts
[0];
121 size_t count
= Tokenize(main_spec
, "/", &parts
);
122 gfx::Display::Rotation
rotation(gfx::Display::ROTATE_0
);
123 bool has_overscan
= false;
125 main_spec
= parts
[0];
127 std::string options
= parts
[1];
128 for (size_t i
= 0; i
< options
.size(); ++i
) {
134 case 'r': // rotate 90 degrees to 'right'.
135 rotation
= gfx::Display::ROTATE_90
;
137 case 'u': // 180 degrees, 'u'pside-down.
138 rotation
= gfx::Display::ROTATE_180
;
140 case 'l': // rotate 90 degrees to 'left'.
141 rotation
= gfx::Display::ROTATE_270
;
148 float device_scale_factor
= 1.0f
;
149 if (!GetDisplayBounds(main_spec
, &bounds_in_native
, &device_scale_factor
)) {
151 if (gfx::IsHighDPIEnabled()) {
152 device_scale_factor
= gfx::GetDPIScale();
157 std::vector
<DisplayMode
> display_modes
;
158 if (Tokenize(main_spec
, "#", &parts
) == 2) {
159 size_t native_mode
= 0;
160 int largest_area
= -1;
161 float highest_refresh_rate
= -1.0f
;
162 main_spec
= parts
[0];
163 std::string resolution_list
= parts
[1];
164 count
= Tokenize(resolution_list
, "|", &parts
);
165 for (size_t i
= 0; i
< count
; ++i
) {
167 gfx::Rect mode_bounds
;
168 std::vector
<std::string
> resolution
;
169 Tokenize(parts
[i
], "%", &resolution
);
170 if (GetDisplayBounds(
171 resolution
[0], &mode_bounds
, &mode
.device_scale_factor
)) {
172 mode
.size
= mode_bounds
.size();
173 if (resolution
.size() > 1)
174 sscanf(resolution
[1].c_str(), "%f", &mode
.refresh_rate
);
175 if (mode
.size
.GetArea() >= largest_area
&&
176 mode
.refresh_rate
> highest_refresh_rate
) {
177 // Use mode with largest area and highest refresh rate as native.
178 largest_area
= mode
.size
.GetArea();
179 highest_refresh_rate
= mode
.refresh_rate
;
182 display_modes
.push_back(mode
);
185 display_modes
[native_mode
].native
= true;
188 if (id
== gfx::Display::kInvalidDisplayID
)
189 id
= synthesized_display_id
++;
190 DisplayInfo
display_info(
191 id
, base::StringPrintf("Display-%d", static_cast<int>(id
)), has_overscan
);
192 display_info
.set_device_scale_factor(device_scale_factor
);
193 display_info
.set_rotation(rotation
);
194 display_info
.set_configured_ui_scale(ui_scale
);
195 display_info
.SetBounds(bounds_in_native
);
196 display_info
.set_display_modes(display_modes
);
198 // To test the overscan, it creates the default 5% overscan.
200 int width
= bounds_in_native
.width() / device_scale_factor
/ 40;
201 int height
= bounds_in_native
.height() / device_scale_factor
/ 40;
202 display_info
.SetOverscanInsets(gfx::Insets(height
, width
, height
, width
));
203 display_info
.UpdateDisplaySize();
206 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info
.ToString()
207 << ", spec=" << spec
;
211 DisplayInfo::DisplayInfo()
212 : id_(gfx::Display::kInvalidDisplayID
),
213 has_overscan_(false),
214 rotation_(gfx::Display::ROTATE_0
),
215 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
217 device_scale_factor_(1.0f
),
218 overscan_insets_in_dip_(0, 0, 0, 0),
219 configured_ui_scale_(1.0f
),
221 is_aspect_preserving_scaling_(false),
222 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
225 DisplayInfo::DisplayInfo(int64 id
,
226 const std::string
& name
,
230 has_overscan_(has_overscan
),
231 rotation_(gfx::Display::ROTATE_0
),
232 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
234 device_scale_factor_(1.0f
),
235 overscan_insets_in_dip_(0, 0, 0, 0),
236 configured_ui_scale_(1.0f
),
238 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
241 DisplayInfo::~DisplayInfo() {
244 void DisplayInfo::Copy(const DisplayInfo
& native_info
) {
245 DCHECK(id_
== native_info
.id_
);
246 name_
= native_info
.name_
;
247 has_overscan_
= native_info
.has_overscan_
;
249 DCHECK(!native_info
.bounds_in_native_
.IsEmpty());
250 bounds_in_native_
= native_info
.bounds_in_native_
;
251 size_in_pixel_
= native_info
.size_in_pixel_
;
252 device_scale_factor_
= native_info
.device_scale_factor_
;
253 display_modes_
= native_info
.display_modes_
;
254 touch_support_
= native_info
.touch_support_
;
255 touch_device_id_
= native_info
.touch_device_id_
;
257 // Copy overscan_insets_in_dip_ if it's not empty. This is for test
258 // cases which use "/o" annotation which sets the overscan inset
259 // to native, and that overscan has to be propagated. This does not
260 // happen on the real environment.
261 if (!native_info
.overscan_insets_in_dip_
.empty())
262 overscan_insets_in_dip_
= native_info
.overscan_insets_in_dip_
;
264 // Rotation_ and ui_scale_ color_profile_ are given by preference,
265 // or unit tests. Don't copy if this native_info came from
266 // DisplayChangeObserver.
267 if (!native_info
.native()) {
268 rotation_
= native_info
.rotation_
;
269 configured_ui_scale_
= native_info
.configured_ui_scale_
;
270 color_profile_
= native_info
.color_profile();
273 available_color_profiles_
= native_info
.available_color_profiles();
275 // Don't copy insets as it may be given by preference. |rotation_|
276 // is treated as a native so that it can be specified in
280 void DisplayInfo::SetBounds(const gfx::Rect
& new_bounds_in_native
) {
281 bounds_in_native_
= new_bounds_in_native
;
282 size_in_pixel_
= new_bounds_in_native
.size();
286 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
287 if (use_125_dsf_for_ui_scaling
&& device_scale_factor_
== 1.25f
)
288 return (configured_ui_scale_
== 0.8f
) ? 1.25f
: 1.0f
;
289 if (device_scale_factor_
== configured_ui_scale_
)
291 return device_scale_factor_
;
294 float DisplayInfo::GetEffectiveUIScale() const {
295 if (use_125_dsf_for_ui_scaling
&& device_scale_factor_
== 1.25f
)
296 return (configured_ui_scale_
== 0.8f
) ? 1.0f
: configured_ui_scale_
;
297 if (device_scale_factor_
== configured_ui_scale_
)
299 return configured_ui_scale_
;
302 void DisplayInfo::UpdateDisplaySize() {
303 size_in_pixel_
= bounds_in_native_
.size();
304 if (!overscan_insets_in_dip_
.empty()) {
305 gfx::Insets insets_in_pixel
=
306 overscan_insets_in_dip_
.Scale(device_scale_factor_
);
307 size_in_pixel_
.Enlarge(-insets_in_pixel
.width(), -insets_in_pixel
.height());
309 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
312 if (rotation_
== gfx::Display::ROTATE_90
||
313 rotation_
== gfx::Display::ROTATE_270
)
314 size_in_pixel_
.SetSize(size_in_pixel_
.height(), size_in_pixel_
.width());
315 gfx::SizeF
size_f(size_in_pixel_
);
316 size_f
.Scale(GetEffectiveUIScale());
317 size_in_pixel_
= gfx::ToFlooredSize(size_f
);
320 void DisplayInfo::SetOverscanInsets(const gfx::Insets
& insets_in_dip
) {
321 overscan_insets_in_dip_
= insets_in_dip
;
324 gfx::Insets
DisplayInfo::GetOverscanInsetsInPixel() const {
325 return overscan_insets_in_dip_
.Scale(device_scale_factor_
);
328 gfx::Size
DisplayInfo::GetNativeModeSize() const {
329 for (size_t i
= 0; i
< display_modes_
.size(); ++i
) {
330 if (display_modes_
[i
].native
)
331 return display_modes_
[i
].size
;
337 std::string
DisplayInfo::ToString() const {
338 int rotation_degree
= static_cast<int>(rotation_
) * 90;
339 return base::StringPrintf(
340 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
341 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
342 "touch-device-id=%d",
343 static_cast<long long int>(id_
),
344 bounds_in_native_
.ToString().c_str(),
345 size_in_pixel_
.ToString().c_str(),
346 device_scale_factor_
,
347 overscan_insets_in_dip_
.ToString().c_str(),
349 configured_ui_scale_
,
350 touch_support_
== gfx::Display::TOUCH_SUPPORT_AVAILABLE
352 : touch_support_
== gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
358 std::string
DisplayInfo::ToFullString() const {
359 std::string display_modes_str
;
360 std::vector
<DisplayMode
>::const_iterator iter
= display_modes_
.begin();
361 for (; iter
!= display_modes_
.end(); ++iter
) {
362 if (!display_modes_str
.empty())
363 display_modes_str
+= ",";
364 base::StringAppendF(&display_modes_str
,
369 iter
->interlaced
? 'I' : 'P',
370 iter
->native
? "(N)" : "");
372 return ToString() + ", display_modes==" + display_modes_str
;
375 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile
) {
376 if (IsColorProfileAvailable(profile
))
377 color_profile_
= profile
;
380 bool DisplayInfo::IsColorProfileAvailable(
381 ui::ColorCalibrationProfile profile
) const {
382 return std::find(available_color_profiles_
.begin(),
383 available_color_profiles_
.end(),
384 profile
) != available_color_profiles_
.end();