Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / ash / display / display_info.cc
blob2d182715db87b1f60ab7a8f786f585dbd5d0179e
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 const float kDpi96 = 96.0;
33 bool use_125_dsf_for_ui_scaling = true;
35 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
36 // Returns true when |bounds| is found.
37 bool GetDisplayBounds(
38 const std::string& spec, gfx::Rect* bounds, float* device_scale_factor) {
39 int width = 0;
40 int height = 0;
41 int x = 0;
42 int y = 0;
43 if (sscanf(spec.c_str(), "%dx%d*%f",
44 &width, &height, device_scale_factor) >= 2 ||
45 sscanf(spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
46 device_scale_factor) >= 4) {
47 bounds->SetRect(x, y, width, height);
48 return true;
50 return false;
53 // Display mode list is sorted by:
54 // * the area in pixels in ascending order
55 // * refresh rate in descending order
56 struct DisplayModeSorter {
57 explicit DisplayModeSorter(bool is_internal) : is_internal(is_internal) {}
59 bool operator()(const DisplayMode& a, const DisplayMode& b) {
60 gfx::Size size_a_dip = a.GetSizeInDIP(is_internal);
61 gfx::Size size_b_dip = b.GetSizeInDIP(is_internal);
62 if (size_a_dip.GetArea() == size_b_dip.GetArea())
63 return (a.refresh_rate > b.refresh_rate);
64 return (size_a_dip.GetArea() < size_b_dip.GetArea());
67 bool is_internal;
70 } // namespace
72 DisplayMode::DisplayMode()
73 : refresh_rate(0.0f),
74 interlaced(false),
75 native(false),
76 ui_scale(1.0f),
77 device_scale_factor(1.0f) {}
79 DisplayMode::DisplayMode(const gfx::Size& size,
80 float refresh_rate,
81 bool interlaced,
82 bool native)
83 : size(size),
84 refresh_rate(refresh_rate),
85 interlaced(interlaced),
86 native(native),
87 ui_scale(1.0f),
88 device_scale_factor(1.0f) {}
90 gfx::Size DisplayMode::GetSizeInDIP(bool is_internal) const {
91 gfx::SizeF size_dip(size);
92 size_dip.Scale(ui_scale);
93 // DSF=1.25 is special on internal display. The screen is drawn with DSF=1.25
94 // but it doesn't affect the screen size computation.
95 if (use_125_dsf_for_ui_scaling && is_internal && device_scale_factor == 1.25f)
96 return gfx::ToFlooredSize(size_dip);
97 size_dip.Scale(1.0f / device_scale_factor);
98 return gfx::ToFlooredSize(size_dip);
101 bool DisplayMode::IsEquivalent(const DisplayMode& other) const {
102 const float kEpsilon = 0.0001f;
103 return size == other.size &&
104 std::abs(ui_scale - other.ui_scale) < kEpsilon &&
105 std::abs(device_scale_factor - other.device_scale_factor) < kEpsilon;
108 // satic
109 DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
110 return CreateFromSpecWithID(spec, gfx::Display::kInvalidDisplayID);
113 // static
114 DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
115 int64 id) {
116 #if defined(OS_WIN)
117 gfx::Rect bounds_in_native(aura::WindowTreeHost::GetNativeScreenSize());
118 #else
119 // Default bounds for a display.
120 const int kDefaultHostWindowX = 200;
121 const int kDefaultHostWindowY = 200;
122 const int kDefaultHostWindowWidth = 1366;
123 const int kDefaultHostWindowHeight = 768;
124 gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
125 kDefaultHostWindowWidth, kDefaultHostWindowHeight);
126 #endif
127 std::string main_spec = spec;
129 float ui_scale = 1.0f;
130 std::vector<std::string> parts = base::SplitString(
131 main_spec, "@", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
132 if (parts.size() == 2) {
133 double scale_in_double = 0;
134 if (base::StringToDouble(parts[1], &scale_in_double))
135 ui_scale = scale_in_double;
136 main_spec = parts[0];
139 parts = base::SplitString(main_spec, "/", base::KEEP_WHITESPACE,
140 base::SPLIT_WANT_NONEMPTY);
141 gfx::Display::Rotation rotation(gfx::Display::ROTATE_0);
142 bool has_overscan = false;
143 if (!parts.empty()) {
144 main_spec = parts[0];
145 if (parts.size() >= 2) {
146 std::string options = parts[1];
147 for (size_t i = 0; i < options.size(); ++i) {
148 char c = options[i];
149 switch (c) {
150 case 'o':
151 has_overscan = true;
152 break;
153 case 'r': // rotate 90 degrees to 'right'.
154 rotation = gfx::Display::ROTATE_90;
155 break;
156 case 'u': // 180 degrees, 'u'pside-down.
157 rotation = gfx::Display::ROTATE_180;
158 break;
159 case 'l': // rotate 90 degrees to 'left'.
160 rotation = gfx::Display::ROTATE_270;
161 break;
167 float device_scale_factor = 1.0f;
168 if (!GetDisplayBounds(main_spec, &bounds_in_native, &device_scale_factor)) {
169 #if defined(OS_WIN)
170 device_scale_factor = gfx::GetDPIScale();
171 #endif
174 std::vector<DisplayMode> display_modes;
175 parts = base::SplitString(main_spec, "#", base::KEEP_WHITESPACE,
176 base::SPLIT_WANT_NONEMPTY);
177 if (parts.size() == 2) {
178 size_t native_mode = 0;
179 int largest_area = -1;
180 float highest_refresh_rate = -1.0f;
181 main_spec = parts[0];
182 std::string resolution_list = parts[1];
183 parts = base::SplitString(resolution_list, "|", base::KEEP_WHITESPACE,
184 base::SPLIT_WANT_NONEMPTY);
185 for (size_t i = 0; i < parts.size(); ++i) {
186 DisplayMode mode;
187 gfx::Rect mode_bounds;
188 std::vector<std::string> resolution = base::SplitString(
189 parts[i], "%", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
190 if (GetDisplayBounds(
191 resolution[0], &mode_bounds, &mode.device_scale_factor)) {
192 mode.size = mode_bounds.size();
193 if (resolution.size() > 1)
194 sscanf(resolution[1].c_str(), "%f", &mode.refresh_rate);
195 if (mode.size.GetArea() >= largest_area &&
196 mode.refresh_rate > highest_refresh_rate) {
197 // Use mode with largest area and highest refresh rate as native.
198 largest_area = mode.size.GetArea();
199 highest_refresh_rate = mode.refresh_rate;
200 native_mode = i;
202 display_modes.push_back(mode);
205 display_modes[native_mode].native = true;
208 if (id == gfx::Display::kInvalidDisplayID)
209 id = synthesized_display_id++;
210 DisplayInfo display_info(
211 id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
212 display_info.set_device_scale_factor(device_scale_factor);
213 display_info.SetRotation(rotation, gfx::Display::ROTATION_SOURCE_ACTIVE);
214 display_info.set_configured_ui_scale(ui_scale);
215 display_info.SetBounds(bounds_in_native);
216 display_info.SetDisplayModes(display_modes);
218 // To test the overscan, it creates the default 5% overscan.
219 if (has_overscan) {
220 int width = bounds_in_native.width() / device_scale_factor / 40;
221 int height = bounds_in_native.height() / device_scale_factor / 40;
222 display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
223 display_info.UpdateDisplaySize();
226 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
227 << ", spec=" << spec;
228 return display_info;
231 // static
232 void DisplayInfo::SetUse125DSFForUIScalingForTest(bool enable) {
233 use_125_dsf_for_ui_scaling = enable;
236 DisplayInfo::DisplayInfo()
237 : id_(gfx::Display::kInvalidDisplayID),
238 has_overscan_(false),
239 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
240 device_scale_factor_(1.0f),
241 device_dpi_(kDpi96),
242 overscan_insets_in_dip_(0, 0, 0, 0),
243 configured_ui_scale_(1.0f),
244 native_(false),
245 is_aspect_preserving_scaling_(false),
246 clear_overscan_insets_(false),
247 color_profile_(ui::COLOR_PROFILE_STANDARD) {}
249 DisplayInfo::DisplayInfo(int64 id, const std::string& name, bool has_overscan)
250 : id_(id),
251 name_(name),
252 has_overscan_(has_overscan),
253 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
254 device_scale_factor_(1.0f),
255 device_dpi_(kDpi96),
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) {}
263 DisplayInfo::~DisplayInfo() {
266 void DisplayInfo::SetRotation(gfx::Display::Rotation rotation,
267 gfx::Display::RotationSource source) {
268 rotations_[source] = rotation;
269 rotations_[gfx::Display::ROTATION_SOURCE_ACTIVE] = rotation;
272 gfx::Display::Rotation DisplayInfo::GetActiveRotation() const {
273 return GetRotation(gfx::Display::ROTATION_SOURCE_ACTIVE);
276 gfx::Display::Rotation DisplayInfo::GetRotation(
277 gfx::Display::RotationSource source) const {
278 if (rotations_.find(source) == rotations_.end())
279 return gfx::Display::ROTATE_0;
280 return rotations_.at(source);
283 void DisplayInfo::Copy(const DisplayInfo& native_info) {
284 DCHECK(id_ == native_info.id_);
285 name_ = native_info.name_;
286 has_overscan_ = native_info.has_overscan_;
288 touch_support_ = native_info.touch_support_;
289 input_devices_ = native_info.input_devices_;
290 device_scale_factor_ = native_info.device_scale_factor_;
291 DCHECK(!native_info.bounds_in_native_.IsEmpty());
292 bounds_in_native_ = native_info.bounds_in_native_;
293 device_dpi_ = native_info.device_dpi_;
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