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 DisplayMode::DisplayMode()
27 : refresh_rate(0.0f
), interlaced(false), native(false) {}
29 DisplayMode::DisplayMode(const gfx::Size
& size
,
34 refresh_rate(refresh_rate
),
35 interlaced(interlaced
),
39 DisplayInfo
DisplayInfo::CreateFromSpec(const std::string
& spec
) {
40 return CreateFromSpecWithID(spec
, gfx::Display::kInvalidDisplayID
);
44 DisplayInfo
DisplayInfo::CreateFromSpecWithID(const std::string
& spec
,
46 // Default bounds for a display.
47 const int kDefaultHostWindowX
= 200;
48 const int kDefaultHostWindowY
= 200;
49 const int kDefaultHostWindowWidth
= 1366;
50 const int kDefaultHostWindowHeight
= 768;
52 // Use larger than max int to catch overflow early.
53 static int64 synthesized_display_id
= 2200000000LL;
56 gfx::Rect
bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
58 gfx::Rect
bounds_in_native(kDefaultHostWindowX
, kDefaultHostWindowY
,
59 kDefaultHostWindowWidth
, kDefaultHostWindowHeight
);
61 std::string main_spec
= spec
;
63 float ui_scale
= 1.0f
;
64 std::vector
<std::string
> parts
;
65 if (Tokenize(main_spec
, "@", &parts
) == 2) {
66 double scale_in_double
= 0;
67 if (base::StringToDouble(parts
[1], &scale_in_double
))
68 ui_scale
= scale_in_double
;
72 size_t count
= Tokenize(main_spec
, "/", &parts
);
73 gfx::Display::Rotation
rotation(gfx::Display::ROTATE_0
);
74 bool has_overscan
= false;
78 std::string options
= parts
[1];
79 for (size_t i
= 0; i
< options
.size(); ++i
) {
85 case 'r': // rotate 90 degrees to 'right'.
86 rotation
= gfx::Display::ROTATE_90
;
88 case 'u': // 180 degrees, 'u'pside-down.
89 rotation
= gfx::Display::ROTATE_180
;
91 case 'l': // rotate 90 degrees to 'left'.
92 rotation
= gfx::Display::ROTATE_270
;
99 int x
= 0, y
= 0, width
, height
;
100 float device_scale_factor
= 1.0f
;
101 if (sscanf(main_spec
.c_str(), "%dx%d*%f",
102 &width
, &height
, &device_scale_factor
) >= 2 ||
103 sscanf(main_spec
.c_str(), "%d+%d-%dx%d*%f", &x
, &y
, &width
, &height
,
104 &device_scale_factor
) >= 4) {
105 bounds_in_native
.SetRect(x
, y
, width
, height
);
108 if (gfx::IsHighDPIEnabled()) {
109 device_scale_factor
= gfx::GetModernUIScale();
114 std::vector
<DisplayMode
> display_modes
;
115 if (Tokenize(main_spec
, "#", &parts
) == 2) {
116 size_t native_mode
= 0;
117 int largest_area
= -1;
118 float highest_refresh_rate
= -1.0f
;
119 main_spec
= parts
[0];
120 std::string resolution_list
= parts
[1];
121 count
= Tokenize(resolution_list
, "|", &parts
);
122 for (size_t i
= 0; i
< count
; ++i
) {
123 std::string resolution
= parts
[i
];
125 float refresh_rate
= 0.0f
;
126 if (sscanf(resolution
.c_str(),
130 &refresh_rate
) >= 2) {
131 if (width
* height
>= largest_area
&&
132 refresh_rate
> highest_refresh_rate
) {
133 // Use mode with largest area and highest refresh rate as native.
134 largest_area
= width
* height
;
135 highest_refresh_rate
= refresh_rate
;
138 display_modes
.push_back(
139 DisplayMode(gfx::Size(width
, height
), refresh_rate
, false, false));
142 display_modes
[native_mode
].native
= true;
145 if (id
== gfx::Display::kInvalidDisplayID
)
146 id
= synthesized_display_id
++;
147 DisplayInfo
display_info(
148 id
, base::StringPrintf("Display-%d", static_cast<int>(id
)), has_overscan
);
149 display_info
.set_device_scale_factor(device_scale_factor
);
150 display_info
.set_rotation(rotation
);
151 display_info
.set_configured_ui_scale(ui_scale
);
152 display_info
.SetBounds(bounds_in_native
);
153 display_info
.set_display_modes(display_modes
);
155 // To test the overscan, it creates the default 5% overscan.
157 int width
= bounds_in_native
.width() / device_scale_factor
/ 40;
158 int height
= bounds_in_native
.height() / device_scale_factor
/ 40;
159 display_info
.SetOverscanInsets(gfx::Insets(height
, width
, height
, width
));
160 display_info
.UpdateDisplaySize();
163 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info
.ToString()
164 << ", spec=" << spec
;
168 DisplayInfo::DisplayInfo()
169 : id_(gfx::Display::kInvalidDisplayID
),
170 has_overscan_(false),
171 rotation_(gfx::Display::ROTATE_0
),
172 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
173 device_scale_factor_(1.0f
),
174 overscan_insets_in_dip_(0, 0, 0, 0),
175 configured_ui_scale_(1.0f
),
177 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
180 DisplayInfo::DisplayInfo(int64 id
,
181 const std::string
& name
,
185 has_overscan_(has_overscan
),
186 rotation_(gfx::Display::ROTATE_0
),
187 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN
),
188 device_scale_factor_(1.0f
),
189 overscan_insets_in_dip_(0, 0, 0, 0),
190 configured_ui_scale_(1.0f
),
192 color_profile_(ui::COLOR_PROFILE_STANDARD
) {
195 DisplayInfo::~DisplayInfo() {
198 void DisplayInfo::Copy(const DisplayInfo
& native_info
) {
199 DCHECK(id_
== native_info
.id_
);
200 name_
= native_info
.name_
;
201 has_overscan_
= native_info
.has_overscan_
;
203 DCHECK(!native_info
.bounds_in_native_
.IsEmpty());
204 bounds_in_native_
= native_info
.bounds_in_native_
;
205 size_in_pixel_
= native_info
.size_in_pixel_
;
206 device_scale_factor_
= native_info
.device_scale_factor_
;
207 display_modes_
= native_info
.display_modes_
;
208 touch_support_
= native_info
.touch_support_
;
210 // Copy overscan_insets_in_dip_ if it's not empty. This is for test
211 // cases which use "/o" annotation which sets the overscan inset
212 // to native, and that overscan has to be propagated. This does not
213 // happen on the real environment.
214 if (!native_info
.overscan_insets_in_dip_
.empty())
215 overscan_insets_in_dip_
= native_info
.overscan_insets_in_dip_
;
217 // Rotation_ and ui_scale_ color_profile_ are given by preference,
218 // or unit tests. Don't copy if this native_info came from
219 // DisplayChangeObserver.
220 if (!native_info
.native()) {
221 rotation_
= native_info
.rotation_
;
222 configured_ui_scale_
= native_info
.configured_ui_scale_
;
223 color_profile_
= native_info
.color_profile();
226 available_color_profiles_
= native_info
.available_color_profiles();
228 // Don't copy insets as it may be given by preference. |rotation_|
229 // is treated as a native so that it can be specified in
233 void DisplayInfo::SetBounds(const gfx::Rect
& new_bounds_in_native
) {
234 bounds_in_native_
= new_bounds_in_native
;
235 size_in_pixel_
= new_bounds_in_native
.size();
239 float DisplayInfo::GetEffectiveUIScale() const {
240 if (device_scale_factor_
== 2.0f
&& configured_ui_scale_
== 2.0f
)
242 return configured_ui_scale_
;
245 void DisplayInfo::UpdateDisplaySize() {
246 size_in_pixel_
= bounds_in_native_
.size();
247 if (!overscan_insets_in_dip_
.empty()) {
248 gfx::Insets insets_in_pixel
=
249 overscan_insets_in_dip_
.Scale(device_scale_factor_
);
250 size_in_pixel_
.Enlarge(-insets_in_pixel
.width(), -insets_in_pixel
.height());
252 overscan_insets_in_dip_
.Set(0, 0, 0, 0);
255 if (rotation_
== gfx::Display::ROTATE_90
||
256 rotation_
== gfx::Display::ROTATE_270
)
257 size_in_pixel_
.SetSize(size_in_pixel_
.height(), size_in_pixel_
.width());
258 gfx::SizeF
size_f(size_in_pixel_
);
259 size_f
.Scale(GetEffectiveUIScale());
260 size_in_pixel_
= gfx::ToFlooredSize(size_f
);
263 void DisplayInfo::SetOverscanInsets(const gfx::Insets
& insets_in_dip
) {
264 overscan_insets_in_dip_
= insets_in_dip
;
267 gfx::Insets
DisplayInfo::GetOverscanInsetsInPixel() const {
268 return overscan_insets_in_dip_
.Scale(device_scale_factor_
);
271 std::string
DisplayInfo::ToString() const {
272 int rotation_degree
= static_cast<int>(rotation_
) * 90;
273 return base::StringPrintf(
274 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
275 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s",
276 static_cast<long long int>(id_
),
277 bounds_in_native_
.ToString().c_str(),
278 size_in_pixel_
.ToString().c_str(),
279 device_scale_factor_
,
280 overscan_insets_in_dip_
.ToString().c_str(),
282 configured_ui_scale_
,
283 touch_support_
== gfx::Display::TOUCH_SUPPORT_AVAILABLE
285 : touch_support_
== gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
290 std::string
DisplayInfo::ToFullString() const {
291 std::string display_modes_str
;
292 std::vector
<DisplayMode
>::const_iterator iter
= display_modes_
.begin();
293 for (; iter
!= display_modes_
.end(); ++iter
) {
294 if (!display_modes_str
.empty())
295 display_modes_str
+= ",";
296 base::StringAppendF(&display_modes_str
,
301 iter
->interlaced
? 'I' : 'P',
302 iter
->native
? "(N)" : "");
304 return ToString() + ", display_modes==" + display_modes_str
;
307 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile
) {
308 if (IsColorProfileAvailable(profile
))
309 color_profile_
= profile
;
312 bool DisplayInfo::IsColorProfileAvailable(
313 ui::ColorCalibrationProfile profile
) const {
314 return std::find(available_color_profiles_
.begin(),
315 available_color_profiles_
.end(),
316 profile
) != available_color_profiles_
.end();
319 } // namespace internal