cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ash / display / display_info.cc
blob45b047fa1e6acda1a60c2438e2bfa82ca5c39fb0
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.
5 #include <stdio.h>
6 #include <string>
7 #include <vector>
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"
19 #if defined(OS_WIN)
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/gfx/win/dpi.h"
22 #endif
24 namespace ash {
25 namespace {
27 // Use larger than max int to catch overflow early.
28 const int64 kSynthesizedDisplayIdStart = 2200000000LL;
30 int64 synthesized_display_id = kSynthesizedDisplayIdStart;
32 bool use_125_dsf_for_ui_scaling = true;
34 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
35 // Returns true when |bounds| is found.
36 bool GetDisplayBounds(
37 const std::string& spec, gfx::Rect* bounds, float* device_scale_factor) {
38 int width = 0;
39 int height = 0;
40 int x = 0;
41 int y = 0;
42 if (sscanf(spec.c_str(), "%dx%d*%f",
43 &width, &height, device_scale_factor) >= 2 ||
44 sscanf(spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
45 device_scale_factor) >= 4) {
46 bounds->SetRect(x, y, width, height);
47 return true;
49 return false;
52 // Display mode list is sorted by:
53 // * the area in pixels in ascending order
54 // * refresh rate in descending order
55 struct DisplayModeSorter {
56 explicit DisplayModeSorter(bool is_internal) : is_internal(is_internal) {}
58 bool operator()(const DisplayMode& a, const DisplayMode& b) {
59 gfx::Size size_a_dip = a.GetSizeInDIP(is_internal);
60 gfx::Size size_b_dip = b.GetSizeInDIP(is_internal);
61 if (size_a_dip.GetArea() == size_b_dip.GetArea())
62 return (a.refresh_rate > b.refresh_rate);
63 return (size_a_dip.GetArea() < size_b_dip.GetArea());
66 bool is_internal;
69 } // namespace
71 DisplayMode::DisplayMode()
72 : refresh_rate(0.0f),
73 interlaced(false),
74 native(false),
75 ui_scale(1.0f),
76 device_scale_factor(1.0f) {}
78 DisplayMode::DisplayMode(const gfx::Size& size,
79 float refresh_rate,
80 bool interlaced,
81 bool native)
82 : size(size),
83 refresh_rate(refresh_rate),
84 interlaced(interlaced),
85 native(native),
86 ui_scale(1.0f),
87 device_scale_factor(1.0f) {}
89 gfx::Size DisplayMode::GetSizeInDIP(bool is_internal) const {
90 gfx::SizeF size_dip(size);
91 size_dip.Scale(ui_scale);
92 // DSF=1.25 is special on internal display. The screen is drawn with DSF=1.25
93 // but it doesn't affect the screen size computation.
94 if (use_125_dsf_for_ui_scaling && is_internal && device_scale_factor == 1.25f)
95 return gfx::ToFlooredSize(size_dip);
96 size_dip.Scale(1.0f / device_scale_factor);
97 return gfx::ToFlooredSize(size_dip);
100 bool DisplayMode::IsEquivalent(const DisplayMode& other) const {
101 const float kEpsilon = 0.0001f;
102 return size == other.size &&
103 std::abs(ui_scale - other.ui_scale) < kEpsilon &&
104 std::abs(device_scale_factor - other.device_scale_factor) < kEpsilon;
107 // satic
108 DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
109 return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
112 // static
113 DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
114 int64 id) {
115 #if defined(OS_WIN)
116 gfx::Rect bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
117 #else
118 // Default bounds for a display.
119 const int kDefaultHostWindowX = 200;
120 const int kDefaultHostWindowY = 200;
121 const int kDefaultHostWindowWidth = 1366;
122 const int kDefaultHostWindowHeight = 768;
123 gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
124 kDefaultHostWindowWidth, kDefaultHostWindowHeight);
125 #endif
126 std::string main_spec = spec;
128 float ui_scale = 1.0f;
129 std::vector<std::string> parts = base::SplitString(
130 main_spec, "@", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
131 if (parts.size() == 2) {
132 double scale_in_double = 0;
133 if (base::StringToDouble(parts[1], &scale_in_double))
134 ui_scale = scale_in_double;
135 main_spec = parts[0];
138 parts = base::SplitString(main_spec, "/", base::KEEP_WHITESPACE,
139 base::SPLIT_WANT_NONEMPTY);
140 gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
141 bool has_overscan = false;
142 if (!parts.empty()) {
143 main_spec = parts[0];
144 if (parts.size() >= 2) {
145 std::string options = parts[1];
146 for (size_t i = 0; i < options.size(); ++i) {
147 char c = options[i];
148 switch (c) {
149 case 'o':
150 has_overscan = true;
151 break;
152 case 'r': // rotate 90 degrees to 'right'.
153 rotation = gfx::Display::ROTATE_90;
154 break;
155 case 'u': // 180 degrees, 'u'pside-down.
156 rotation = gfx::Display::ROTATE_180;
157 break;
158 case 'l': // rotate 90 degrees to 'left'.
159 rotation = gfx::Display::ROTATE_270;
160 break;
166 float device_scale_factor = 1.0f;
167 if (!GetDisplayBounds(main_spec, &bounds_in_native, &device_scale_factor)) {
168 #if defined(OS_WIN)
169 device_scale_factor = gfx::GetDPIScale();
170 #endif
173 std::vector<DisplayMode> display_modes;
174 parts = base::SplitString(main_spec, "#", base::KEEP_WHITESPACE,
175 base::SPLIT_WANT_NONEMPTY);
176 if (parts.size() == 2) {
177 size_t native_mode = 0;
178 int largest_area = -1;
179 float highest_refresh_rate = -1.0f;
180 main_spec = parts[0];
181 std::string resolution_list = parts[1];
182 parts = base::SplitString(resolution_list, "|", base::KEEP_WHITESPACE,
183 base::SPLIT_WANT_NONEMPTY);
184 for (size_t i = 0; i < parts.size(); ++i) {
185 DisplayMode mode;
186 gfx::Rect mode_bounds;
187 std::vector<std::string> resolution = base::SplitString(
188 parts[i], "%", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
189 if (GetDisplayBounds(
190 resolution[0], &mode_bounds, &mode.device_scale_factor)) {
191 mode.size = mode_bounds.size();
192 if (resolution.size() > 1)
193 sscanf(resolution[1].c_str(), "%f", &mode.refresh_rate);
194 if (mode.size.GetArea() >= largest_area &&
195 mode.refresh_rate > highest_refresh_rate) {
196 // Use mode with largest area and highest refresh rate as native.
197 largest_area = mode.size.GetArea();
198 highest_refresh_rate = mode.refresh_rate;
199 native_mode = i;
201 display_modes.push_back(mode);
204 display_modes[native_mode].native = true;
207 if (id == gfx::Display::kInvalidDisplayID)
208 id = synthesized_display_id++;
209 DisplayInfo display_info(
210 id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
211 display_info.set_device_scale_factor(device_scale_factor);
212 display_info.SetRotation(rotation, gfx::Display::ROTATION_SOURCE_ACTIVE);
213 display_info.set_configured_ui_scale(ui_scale);
214 display_info.SetBounds(bounds_in_native);
215 display_info.SetDisplayModes(display_modes);
217 // To test the overscan, it creates the default 5% overscan.
218 if (has_overscan) {
219 int width = bounds_in_native.width() / device_scale_factor / 40;
220 int height = bounds_in_native.height() / device_scale_factor / 40;
221 display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
222 display_info.UpdateDisplaySize();
225 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
226 << ", spec=" << spec;
227 return display_info;
230 // static
231 void DisplayInfo::SetUse125DSFForUIScalingForTest(bool enable) {
232 use_125_dsf_for_ui_scaling = enable;
235 DisplayInfo::DisplayInfo()
236 : id_(gfx::Display::kInvalidDisplayID),
237 has_overscan_(false),
238 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),
242 native_(false),
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,
250 bool has_overscan)
251 : id_(id),
252 name_(name),
253 has_overscan_(has_overscan),
254 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
255 device_scale_factor_(1.0f),
256 overscan_insets_in_dip_(0, 0, 0, 0),
257 configured_ui_scale_(1.0f),
258 native_(false),
259 is_aspect_preserving_scaling_(false),
260 clear_overscan_insets_(false),
261 color_profile_(ui::COLOR_PROFILE_STANDARD) {
264 DisplayInfo::~DisplayInfo() {
267 void DisplayInfo::SetRotation(gfx::Display::Rotation rotation,
268 gfx::Display::RotationSource source) {
269 rotations_[source] = rotation;
270 rotations_[gfx::Display::ROTATION_SOURCE_ACTIVE] = rotation;
273 gfx::Display::Rotation DisplayInfo::GetActiveRotation() const {
274 return GetRotation(gfx::Display::ROTATION_SOURCE_ACTIVE);
277 gfx::Display::Rotation DisplayInfo::GetRotation(
278 gfx::Display::RotationSource source) const {
279 if (rotations_.find(source) == rotations_.end())
280 return gfx::Display::ROTATE_0;
281 return rotations_.at(source);
284 void DisplayInfo::Copy(const DisplayInfo& native_info) {
285 DCHECK(id_ == native_info.id_);
286 name_ = native_info.name_;
287 has_overscan_ = native_info.has_overscan_;
289 touch_support_ = native_info.touch_support_;
290 input_devices_ = native_info.input_devices_;
291 device_scale_factor_ = native_info.device_scale_factor_;
292 DCHECK(!native_info.bounds_in_native_.IsEmpty());
293 bounds_in_native_ = native_info.bounds_in_native_;
294 size_in_pixel_ = native_info.size_in_pixel_;
295 is_aspect_preserving_scaling_ = native_info.is_aspect_preserving_scaling_;
296 display_modes_ = native_info.display_modes_;
297 available_color_profiles_ = native_info.available_color_profiles_;
299 // Rotation, ui_scale, color_profile and overscan are given by preference,
300 // or unit tests. Don't copy if this native_info came from
301 // DisplayChangeObserver.
302 if (!native_info.native()) {
303 // Update the overscan_insets_in_dip_ either if the inset should be
304 // cleared, or has non empty insts.
305 if (native_info.clear_overscan_insets())
306 overscan_insets_in_dip_.Set(0, 0, 0, 0);
307 else if (!native_info.overscan_insets_in_dip_.empty())
308 overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
310 rotations_ = native_info.rotations_;
311 configured_ui_scale_ = native_info.configured_ui_scale_;
312 color_profile_ = native_info.color_profile();
316 void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
317 bounds_in_native_ = new_bounds_in_native;
318 size_in_pixel_ = new_bounds_in_native.size();
319 UpdateDisplaySize();
322 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
323 if (Use125DSFForUIScaling() && device_scale_factor_ == 1.25f)
324 return (configured_ui_scale_ == 0.8f) ? 1.25f : 1.0f;
325 if (device_scale_factor_ == configured_ui_scale_)
326 return 1.0f;
327 return device_scale_factor_;
330 float DisplayInfo::GetEffectiveUIScale() const {
331 if (Use125DSFForUIScaling() && device_scale_factor_ == 1.25f)
332 return (configured_ui_scale_ == 0.8f) ? 1.0f : configured_ui_scale_;
333 if (device_scale_factor_ == configured_ui_scale_)
334 return 1.0f;
335 return configured_ui_scale_;
338 void DisplayInfo::UpdateDisplaySize() {
339 size_in_pixel_ = bounds_in_native_.size();
340 if (!overscan_insets_in_dip_.empty()) {
341 gfx::Insets insets_in_pixel =
342 overscan_insets_in_dip_.Scale(device_scale_factor_);
343 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
344 } else {
345 overscan_insets_in_dip_.Set(0, 0, 0, 0);
348 if (GetActiveRotation() == gfx::Display::ROTATE_90 ||
349 GetActiveRotation() == gfx::Display::ROTATE_270) {
350 size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
352 gfx::SizeF size_f(size_in_pixel_);
353 size_f.Scale(GetEffectiveUIScale());
354 size_in_pixel_ = gfx::ToFlooredSize(size_f);
357 void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
358 overscan_insets_in_dip_ = insets_in_dip;
361 gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
362 return overscan_insets_in_dip_.Scale(device_scale_factor_);
365 void DisplayInfo::SetDisplayModes(
366 const std::vector<DisplayMode>& display_modes) {
367 display_modes_ = display_modes;
368 std::sort(display_modes_.begin(), display_modes_.end(),
369 DisplayModeSorter(gfx::Display::IsInternalDisplayId(id_)));
372 gfx::Size DisplayInfo::GetNativeModeSize() const {
373 for (size_t i = 0; i < display_modes_.size(); ++i) {
374 if (display_modes_[i].native)
375 return display_modes_[i].size;
378 return gfx::Size();
381 std::string DisplayInfo::ToString() const {
382 int rotation_degree = static_cast<int>(GetActiveRotation()) * 90;
383 std::string devices_str;
385 for (size_t i = 0; i < input_devices_.size(); ++i) {
386 devices_str += base::IntToString(input_devices_[i]);
387 if (i != input_devices_.size() - 1)
388 devices_str += ", ";
391 std::string result = base::StringPrintf(
392 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
393 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
394 "input_devices=[%s]",
395 static_cast<long long int>(id_), bounds_in_native_.ToString().c_str(),
396 size_in_pixel_.ToString().c_str(), device_scale_factor_,
397 overscan_insets_in_dip_.ToString().c_str(), rotation_degree,
398 configured_ui_scale_,
399 touch_support_ == gfx::Display::TOUCH_SUPPORT_AVAILABLE
400 ? "yes"
401 : touch_support_ == gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
402 ? "no"
403 : "unknown",
404 devices_str.c_str());
406 return result;
409 std::string DisplayInfo::ToFullString() const {
410 std::string display_modes_str;
411 std::vector<DisplayMode>::const_iterator iter = display_modes_.begin();
412 for (; iter != display_modes_.end(); ++iter) {
413 if (!display_modes_str.empty())
414 display_modes_str += ",";
415 base::StringAppendF(&display_modes_str,
416 "(%dx%d@%f%c%s)",
417 iter->size.width(),
418 iter->size.height(),
419 iter->refresh_rate,
420 iter->interlaced ? 'I' : 'P',
421 iter->native ? "(N)" : "");
423 return ToString() + ", display_modes==" + display_modes_str;
426 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile) {
427 if (IsColorProfileAvailable(profile))
428 color_profile_ = profile;
431 bool DisplayInfo::IsColorProfileAvailable(
432 ui::ColorCalibrationProfile profile) const {
433 return std::find(available_color_profiles_.begin(),
434 available_color_profiles_.end(),
435 profile) != available_color_profiles_.end();
438 bool DisplayInfo::Use125DSFForUIScaling() const {
439 return use_125_dsf_for_ui_scaling && gfx::Display::IsInternalDisplayId(id_);
442 void DisplayInfo::AddInputDevice(int id) {
443 input_devices_.push_back(id);
446 void DisplayInfo::ClearInputDevices() {
447 input_devices_.clear();
450 void ResetDisplayIdForTest() {
451 synthesized_display_id = kSynthesizedDisplayIdStart;
454 } // namespace ash