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),
179 DisplayInfo::DisplayInfo(int64 id,
180 const std::string& name,
184 has_overscan_(has_overscan),
185 rotation_(gfx::Display::ROTATE_0),
186 touch_support_(gfx::Display::TOUCH_SUPPORT_UNKNOWN),
187 device_scale_factor_(1.0f),
188 overscan_insets_in_dip_(0, 0, 0, 0),
189 configured_ui_scale_(1.0f),
193 DisplayInfo::~DisplayInfo() {
196 void DisplayInfo::Copy(const DisplayInfo& native_info) {
197 DCHECK(id_ == native_info.id_);
198 name_ = native_info.name_;
199 has_overscan_ = native_info.has_overscan_;
201 DCHECK(!native_info.bounds_in_native_.IsEmpty());
202 bounds_in_native_ = native_info.bounds_in_native_;
203 size_in_pixel_ = native_info.size_in_pixel_;
204 device_scale_factor_ = native_info.device_scale_factor_;
205 display_modes_ = native_info.display_modes_;
206 touch_support_ = native_info.touch_support_;
208 // Copy overscan_insets_in_dip_ if it's not empty. This is for test
209 // cases which use "/o" annotation which sets the overscan inset
210 // to native, and that overscan has to be propagated. This does not
211 // happen on the real environment.
212 if (!native_info.overscan_insets_in_dip_.empty())
213 overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
215 // Rotation_ and ui_scale_ are given by preference, or unit
216 // tests. Don't copy if this native_info came from
217 // DisplayChangeObserver.
218 if (!native_info.native()) {
219 rotation_ = native_info.rotation_;
220 configured_ui_scale_ = native_info.configured_ui_scale_;
222 // Don't copy insets as it may be given by preference. |rotation_|
223 // is treated as a native so that it can be specified in
227 void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
228 bounds_in_native_ = new_bounds_in_native;
229 size_in_pixel_ = new_bounds_in_native.size();
233 float DisplayInfo::GetEffectiveUIScale() const {
234 if (device_scale_factor_ == 2.0f && configured_ui_scale_ == 2.0f)
236 return configured_ui_scale_;
239 void DisplayInfo::UpdateDisplaySize() {
240 size_in_pixel_ = bounds_in_native_.size();
241 if (!overscan_insets_in_dip_.empty()) {
242 gfx::Insets insets_in_pixel =
243 overscan_insets_in_dip_.Scale(device_scale_factor_);
244 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
246 overscan_insets_in_dip_.Set(0, 0, 0, 0);
249 if (rotation_ == gfx::Display::ROTATE_90 ||
250 rotation_ == gfx::Display::ROTATE_270)
251 size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
252 gfx::SizeF size_f(size_in_pixel_);
253 size_f.Scale(GetEffectiveUIScale());
254 size_in_pixel_ = gfx::ToFlooredSize(size_f);
257 void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
258 overscan_insets_in_dip_ = insets_in_dip;
261 gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
262 return overscan_insets_in_dip_.Scale(device_scale_factor_);
265 std::string DisplayInfo::ToString() const {
266 int rotation_degree = static_cast<int>(rotation_) * 90;
267 return base::StringPrintf(
268 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
269 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s",
270 static_cast<long long int>(id_),
271 bounds_in_native_.ToString().c_str(),
272 size_in_pixel_.ToString().c_str(),
273 device_scale_factor_,
274 overscan_insets_in_dip_.ToString().c_str(),
276 configured_ui_scale_,
277 touch_support_ == gfx::Display::TOUCH_SUPPORT_AVAILABLE
279 : touch_support_ == gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
284 std::string DisplayInfo::ToFullString() const {
285 std::string display_modes_str;
286 std::vector<DisplayMode>::const_iterator iter = display_modes_.begin();
287 for (; iter != display_modes_.end(); ++iter) {
288 if (!display_modes_str.empty())
289 display_modes_str += ",";
290 base::StringAppendF(&display_modes_str,
295 iter->interlaced ? 'I' : 'P',
296 iter->native ? "(N)" : "");
298 return ToString() + ", display_modes==" + display_modes_str;
301 } // namespace internal