Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: ash/common/display/display_info.cc

Issue 2270553002: Move ash::DisplayInfo to ui (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/common/display/display_info.h ('k') | ash/common/display/display_info_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ash/common/display/display_info.h"
6
7 #include <stdio.h>
8
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "ui/display/display.h"
19 #include "ui/gfx/geometry/size_conversions.h"
20 #include "ui/gfx/geometry/size_f.h"
21
22 #if defined(OS_WIN)
23 #include <windows.h>
24 #include "ui/display/win/dpi.h"
25 #endif
26
27 namespace ash {
28 namespace {
29
30 // Use larger than max int to catch overflow early.
31 const int64_t kSynthesizedDisplayIdStart = 2200000000LL;
32
33 int64_t synthesized_display_id = kSynthesizedDisplayIdStart;
34
35 const float kDpi96 = 96.0;
36 bool use_125_dsf_for_ui_scaling = true;
37
38 // Check the content of |spec| and fill |bounds| and |device_scale_factor|.
39 // Returns true when |bounds| is found.
40 bool GetDisplayBounds(const std::string& spec,
41 gfx::Rect* bounds,
42 float* device_scale_factor) {
43 int width = 0;
44 int height = 0;
45 int x = 0;
46 int y = 0;
47 if (sscanf(spec.c_str(), "%dx%d*%f", &width, &height, device_scale_factor) >=
48 2 ||
49 sscanf(spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
50 device_scale_factor) >= 4) {
51 bounds->SetRect(x, y, width, height);
52 return true;
53 }
54 return false;
55 }
56
57 // Display mode list is sorted by:
58 // * the area in pixels in ascending order
59 // * refresh rate in descending order
60 struct ManagedDisplayModeSorter {
61 explicit ManagedDisplayModeSorter(bool is_internal)
62 : is_internal(is_internal) {}
63
64 bool operator()(const scoped_refptr<ManagedDisplayMode>& a,
65 const scoped_refptr<ManagedDisplayMode>& b) {
66 gfx::Size size_a_dip = a->GetSizeInDIP(is_internal);
67 gfx::Size size_b_dip = b->GetSizeInDIP(is_internal);
68 if (size_a_dip.GetArea() == size_b_dip.GetArea())
69 return (a->refresh_rate() > b->refresh_rate());
70 return (size_a_dip.GetArea() < size_b_dip.GetArea());
71 }
72
73 bool is_internal;
74 };
75
76 } // namespace
77
78 ManagedDisplayMode::ManagedDisplayMode()
79 : refresh_rate_(0.0f),
80 is_interlaced_(false),
81 native_(false),
82 ui_scale_(1.0f),
83 device_scale_factor_(1.0f) {}
84
85 ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size)
86 : size_(size),
87 refresh_rate_(0.0f),
88 is_interlaced_(false),
89 native_(false),
90 ui_scale_(1.0f),
91 device_scale_factor_(1.0f) {}
92
93 ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size,
94 float refresh_rate,
95 bool is_interlaced,
96 bool native)
97 : size_(size),
98 refresh_rate_(refresh_rate),
99 is_interlaced_(is_interlaced),
100 native_(native),
101 ui_scale_(1.0f),
102 device_scale_factor_(1.0f) {}
103
104 ManagedDisplayMode::~ManagedDisplayMode(){};
105
106 ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size,
107 float refresh_rate,
108 bool is_interlaced,
109 bool native,
110 float ui_scale,
111 float device_scale_factor)
112 : size_(size),
113 refresh_rate_(refresh_rate),
114 is_interlaced_(is_interlaced),
115 native_(native),
116 ui_scale_(ui_scale),
117 device_scale_factor_(device_scale_factor) {}
118
119 gfx::Size ManagedDisplayMode::GetSizeInDIP(bool is_internal) const {
120 gfx::SizeF size_dip(size_);
121 size_dip.Scale(ui_scale_);
122 // DSF=1.25 is special on internal display. The screen is drawn with DSF=1.25
123 // but it doesn't affect the screen size computation.
124 if (use_125_dsf_for_ui_scaling && is_internal &&
125 device_scale_factor_ == 1.25f)
126 return gfx::ToFlooredSize(size_dip);
127 size_dip.Scale(1.0f / device_scale_factor_);
128 return gfx::ToFlooredSize(size_dip);
129 }
130
131 bool ManagedDisplayMode::IsEquivalent(
132 const scoped_refptr<ManagedDisplayMode>& other) const {
133 const float kEpsilon = 0.0001f;
134 return size_ == other->size_ &&
135 std::abs(ui_scale_ - other->ui_scale_) < kEpsilon &&
136 std::abs(device_scale_factor_ - other->device_scale_factor_) <
137 kEpsilon;
138 }
139
140 // static
141 DisplayInfo DisplayInfo::CreateFromSpec(const std::string& spec) {
142 return CreateFromSpecWithID(spec, display::Display::kInvalidDisplayID);
143 }
144
145 // static
146 DisplayInfo DisplayInfo::CreateFromSpecWithID(const std::string& spec,
147 int64_t id) {
148 #if defined(OS_WIN)
149 gfx::Rect bounds_in_native(
150 gfx::Size(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)));
151 #else
152 // Default bounds for a display.
153 const int kDefaultHostWindowX = 200;
154 const int kDefaultHostWindowY = 200;
155 const int kDefaultHostWindowWidth = 1366;
156 const int kDefaultHostWindowHeight = 768;
157 gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
158 kDefaultHostWindowWidth, kDefaultHostWindowHeight);
159 #endif
160 std::string main_spec = spec;
161
162 float ui_scale = 1.0f;
163 std::vector<std::string> parts = base::SplitString(
164 main_spec, "@", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
165 if (parts.size() == 2) {
166 double scale_in_double = 0;
167 if (base::StringToDouble(parts[1], &scale_in_double))
168 ui_scale = scale_in_double;
169 main_spec = parts[0];
170 }
171
172 parts = base::SplitString(main_spec, "/", base::KEEP_WHITESPACE,
173 base::SPLIT_WANT_NONEMPTY);
174 display::Display::Rotation rotation(display::Display::ROTATE_0);
175 bool has_overscan = false;
176 if (!parts.empty()) {
177 main_spec = parts[0];
178 if (parts.size() >= 2) {
179 std::string options = parts[1];
180 for (size_t i = 0; i < options.size(); ++i) {
181 char c = options[i];
182 switch (c) {
183 case 'o':
184 has_overscan = true;
185 break;
186 case 'r': // rotate 90 degrees to 'right'.
187 rotation = display::Display::ROTATE_90;
188 break;
189 case 'u': // 180 degrees, 'u'pside-down.
190 rotation = display::Display::ROTATE_180;
191 break;
192 case 'l': // rotate 90 degrees to 'left'.
193 rotation = display::Display::ROTATE_270;
194 break;
195 }
196 }
197 }
198 }
199
200 float device_scale_factor = 1.0f;
201 if (!GetDisplayBounds(main_spec, &bounds_in_native, &device_scale_factor)) {
202 #if defined(OS_WIN)
203 device_scale_factor = display::win::GetDPIScale();
204 #endif
205 }
206
207 ManagedDisplayModeList display_modes;
208 parts = base::SplitString(main_spec, "#", base::KEEP_WHITESPACE,
209 base::SPLIT_WANT_NONEMPTY);
210 if (parts.size() == 2) {
211 size_t native_mode = 0;
212 int largest_area = -1;
213 float highest_refresh_rate = -1.0f;
214 main_spec = parts[0];
215 std::string resolution_list = parts[1];
216 parts = base::SplitString(resolution_list, "|", base::KEEP_WHITESPACE,
217 base::SPLIT_WANT_NONEMPTY);
218 for (size_t i = 0; i < parts.size(); ++i) {
219 gfx::Size size;
220 float refresh_rate = 0.0f;
221 bool is_interlaced = false;
222
223 gfx::Rect mode_bounds;
224 std::vector<std::string> resolution = base::SplitString(
225 parts[i], "%", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
226 if (GetDisplayBounds(resolution[0], &mode_bounds, &device_scale_factor)) {
227 size = mode_bounds.size();
228 if (resolution.size() > 1)
229 sscanf(resolution[1].c_str(), "%f", &refresh_rate);
230 if (size.GetArea() >= largest_area &&
231 refresh_rate > highest_refresh_rate) {
232 // Use mode with largest area and highest refresh rate as native.
233 largest_area = size.GetArea();
234 highest_refresh_rate = refresh_rate;
235 native_mode = i;
236 }
237 display_modes.push_back(make_scoped_refptr(
238 new ManagedDisplayMode(size, refresh_rate, is_interlaced, false,
239 1.0, device_scale_factor)));
240 }
241 }
242 scoped_refptr<ManagedDisplayMode> dm = display_modes[native_mode];
243 display_modes[native_mode] = new ManagedDisplayMode(
244 dm->size(), dm->refresh_rate(), dm->is_interlaced(), true,
245 dm->ui_scale(), dm->device_scale_factor());
246 }
247
248 if (id == display::Display::kInvalidDisplayID)
249 id = synthesized_display_id++;
250 DisplayInfo display_info(
251 id, base::StringPrintf("Display-%d", static_cast<int>(id)), has_overscan);
252 display_info.set_device_scale_factor(device_scale_factor);
253 display_info.SetRotation(rotation, display::Display::ROTATION_SOURCE_ACTIVE);
254 display_info.set_configured_ui_scale(ui_scale);
255 display_info.SetBounds(bounds_in_native);
256 display_info.SetManagedDisplayModes(display_modes);
257
258 // To test the overscan, it creates the default 5% overscan.
259 if (has_overscan) {
260 int width = bounds_in_native.width() / device_scale_factor / 40;
261 int height = bounds_in_native.height() / device_scale_factor / 40;
262 display_info.SetOverscanInsets(gfx::Insets(height, width, height, width));
263 display_info.UpdateDisplaySize();
264 }
265
266 DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
267 << ", spec=" << spec;
268 return display_info;
269 }
270
271 // static
272 void DisplayInfo::SetUse125DSFForUIScalingForTest(bool enable) {
273 use_125_dsf_for_ui_scaling = enable;
274 }
275
276 DisplayInfo::DisplayInfo()
277 : id_(display::Display::kInvalidDisplayID),
278 has_overscan_(false),
279 active_rotation_source_(display::Display::ROTATION_SOURCE_UNKNOWN),
280 touch_support_(display::Display::TOUCH_SUPPORT_UNKNOWN),
281 device_scale_factor_(1.0f),
282 device_dpi_(kDpi96),
283 overscan_insets_in_dip_(0, 0, 0, 0),
284 configured_ui_scale_(1.0f),
285 native_(false),
286 is_aspect_preserving_scaling_(false),
287 clear_overscan_insets_(false),
288 color_profile_(ui::COLOR_PROFILE_STANDARD) {}
289
290 DisplayInfo::DisplayInfo(int64_t id, const std::string& name, bool has_overscan)
291 : id_(id),
292 name_(name),
293 has_overscan_(has_overscan),
294 active_rotation_source_(display::Display::ROTATION_SOURCE_UNKNOWN),
295 touch_support_(display::Display::TOUCH_SUPPORT_UNKNOWN),
296 device_scale_factor_(1.0f),
297 device_dpi_(kDpi96),
298 overscan_insets_in_dip_(0, 0, 0, 0),
299 configured_ui_scale_(1.0f),
300 native_(false),
301 is_aspect_preserving_scaling_(false),
302 clear_overscan_insets_(false),
303 color_profile_(ui::COLOR_PROFILE_STANDARD) {}
304
305 DisplayInfo::DisplayInfo(const DisplayInfo& other) = default;
306
307 DisplayInfo::~DisplayInfo() {}
308
309 void DisplayInfo::SetRotation(display::Display::Rotation rotation,
310 display::Display::RotationSource source) {
311 rotations_[source] = rotation;
312 rotations_[display::Display::ROTATION_SOURCE_ACTIVE] = rotation;
313 active_rotation_source_ = source;
314 }
315
316 display::Display::Rotation DisplayInfo::GetActiveRotation() const {
317 return GetRotation(display::Display::ROTATION_SOURCE_ACTIVE);
318 }
319
320 display::Display::Rotation DisplayInfo::GetRotation(
321 display::Display::RotationSource source) const {
322 if (rotations_.find(source) == rotations_.end())
323 return display::Display::ROTATE_0;
324 return rotations_.at(source);
325 }
326
327 void DisplayInfo::Copy(const DisplayInfo& native_info) {
328 DCHECK(id_ == native_info.id_);
329 name_ = native_info.name_;
330 has_overscan_ = native_info.has_overscan_;
331
332 active_rotation_source_ = native_info.active_rotation_source_;
333 touch_support_ = native_info.touch_support_;
334 input_devices_ = native_info.input_devices_;
335 device_scale_factor_ = native_info.device_scale_factor_;
336 DCHECK(!native_info.bounds_in_native_.IsEmpty());
337 bounds_in_native_ = native_info.bounds_in_native_;
338 device_dpi_ = native_info.device_dpi_;
339 size_in_pixel_ = native_info.size_in_pixel_;
340 is_aspect_preserving_scaling_ = native_info.is_aspect_preserving_scaling_;
341 display_modes_ = native_info.display_modes_;
342 available_color_profiles_ = native_info.available_color_profiles_;
343 maximum_cursor_size_ = native_info.maximum_cursor_size_;
344
345 // Rotation, ui_scale, color_profile and overscan are given by preference,
346 // or unit tests. Don't copy if this native_info came from
347 // DisplayChangeObserver.
348 if (!native_info.native()) {
349 // Update the overscan_insets_in_dip_ either if the inset should be
350 // cleared, or has non empty insts.
351 if (native_info.clear_overscan_insets())
352 overscan_insets_in_dip_.Set(0, 0, 0, 0);
353 else if (!native_info.overscan_insets_in_dip_.IsEmpty())
354 overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;
355
356 rotations_ = native_info.rotations_;
357 configured_ui_scale_ = native_info.configured_ui_scale_;
358 color_profile_ = native_info.color_profile();
359 }
360 }
361
362 void DisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
363 bounds_in_native_ = new_bounds_in_native;
364 size_in_pixel_ = new_bounds_in_native.size();
365 UpdateDisplaySize();
366 }
367
368 float DisplayInfo::GetEffectiveDeviceScaleFactor() const {
369 if (Use125DSFForUIScaling() && device_scale_factor_ == 1.25f)
370 return (configured_ui_scale_ == 0.8f) ? 1.25f : 1.0f;
371 if (device_scale_factor_ == configured_ui_scale_)
372 return 1.0f;
373 return device_scale_factor_;
374 }
375
376 float DisplayInfo::GetEffectiveUIScale() const {
377 if (Use125DSFForUIScaling() && device_scale_factor_ == 1.25f)
378 return (configured_ui_scale_ == 0.8f) ? 1.0f : configured_ui_scale_;
379 if (device_scale_factor_ == configured_ui_scale_)
380 return 1.0f;
381 return configured_ui_scale_;
382 }
383
384 void DisplayInfo::UpdateDisplaySize() {
385 size_in_pixel_ = bounds_in_native_.size();
386 if (!overscan_insets_in_dip_.IsEmpty()) {
387 gfx::Insets insets_in_pixel =
388 overscan_insets_in_dip_.Scale(device_scale_factor_);
389 size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
390 } else {
391 overscan_insets_in_dip_.Set(0, 0, 0, 0);
392 }
393
394 if (GetActiveRotation() == display::Display::ROTATE_90 ||
395 GetActiveRotation() == display::Display::ROTATE_270) {
396 size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
397 }
398 gfx::SizeF size_f(size_in_pixel_);
399 size_f.Scale(GetEffectiveUIScale());
400 size_in_pixel_ = gfx::ToFlooredSize(size_f);
401 }
402
403 void DisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
404 overscan_insets_in_dip_ = insets_in_dip;
405 }
406
407 gfx::Insets DisplayInfo::GetOverscanInsetsInPixel() const {
408 return overscan_insets_in_dip_.Scale(device_scale_factor_);
409 }
410
411 void DisplayInfo::SetManagedDisplayModes(
412 const ManagedDisplayModeList& display_modes) {
413 display_modes_ = display_modes;
414 std::sort(
415 display_modes_.begin(), display_modes_.end(),
416 ManagedDisplayModeSorter(display::Display::IsInternalDisplayId(id_)));
417 }
418
419 gfx::Size DisplayInfo::GetNativeModeSize() const {
420 for (size_t i = 0; i < display_modes_.size(); ++i) {
421 if (display_modes_[i]->native())
422 return display_modes_[i]->size();
423 }
424 return gfx::Size();
425 }
426
427 std::string DisplayInfo::ToString() const {
428 int rotation_degree = static_cast<int>(GetActiveRotation()) * 90;
429 std::string devices_str;
430
431 for (size_t i = 0; i < input_devices_.size(); ++i) {
432 devices_str += base::IntToString(input_devices_[i]);
433 if (i != input_devices_.size() - 1)
434 devices_str += ", ";
435 }
436
437 std::string result = base::StringPrintf(
438 "DisplayInfo[%lld] native bounds=%s, size=%s, scale=%f, "
439 "overscan=%s, rotation=%d, ui-scale=%f, touchscreen=%s, "
440 "input_devices=[%s]",
441 static_cast<long long int>(id_), bounds_in_native_.ToString().c_str(),
442 size_in_pixel_.ToString().c_str(), device_scale_factor_,
443 overscan_insets_in_dip_.ToString().c_str(), rotation_degree,
444 configured_ui_scale_,
445 touch_support_ == display::Display::TOUCH_SUPPORT_AVAILABLE
446 ? "yes"
447 : touch_support_ == display::Display::TOUCH_SUPPORT_UNAVAILABLE
448 ? "no"
449 : "unknown",
450 devices_str.c_str());
451
452 return result;
453 }
454
455 std::string DisplayInfo::ToFullString() const {
456 std::string display_modes_str;
457 ManagedDisplayModeList::const_iterator iter = display_modes_.begin();
458 for (; iter != display_modes_.end(); ++iter) {
459 scoped_refptr<ManagedDisplayMode> m(*iter);
460 if (!display_modes_str.empty())
461 display_modes_str += ",";
462 base::StringAppendF(&display_modes_str, "(%dx%d@%f%c%s)", m->size().width(),
463 m->size().height(), m->refresh_rate(),
464 m->is_interlaced() ? 'I' : 'P',
465 m->native() ? "(N)" : "");
466 }
467 return ToString() + ", display_modes==" + display_modes_str;
468 }
469
470 void DisplayInfo::SetColorProfile(ui::ColorCalibrationProfile profile) {
471 if (IsColorProfileAvailable(profile))
472 color_profile_ = profile;
473 }
474
475 bool DisplayInfo::IsColorProfileAvailable(
476 ui::ColorCalibrationProfile profile) const {
477 return std::find(available_color_profiles_.begin(),
478 available_color_profiles_.end(),
479 profile) != available_color_profiles_.end();
480 }
481
482 bool DisplayInfo::Use125DSFForUIScaling() const {
483 return use_125_dsf_for_ui_scaling &&
484 display::Display::IsInternalDisplayId(id_);
485 }
486
487 void DisplayInfo::AddInputDevice(int id) {
488 input_devices_.push_back(id);
489 }
490
491 void DisplayInfo::ClearInputDevices() {
492 input_devices_.clear();
493 }
494
495 void ResetDisplayIdForTest() {
496 synthesized_display_id = kSynthesizedDisplayIdStart;
497 }
498
499 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/display/display_info.h ('k') | ash/common/display/display_info_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698