1 // Copyright 2014 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 "ui/display/util/display_util.h"
6
7 #include "base/logging.h"
8
9 namespace ui {
10
11 namespace {
12
13 // A list of bogus sizes in mm that should be ignored.
14 // See crbug.com/136533. The first element maintains the minimum
15 // size required to be valid size.
16 const int kInvalidDisplaySizeList[][2] = {
17 {40, 30},
18 {50, 40},
19 {160, 90},
20 {160, 100},
21 };
22
23 // The DPI threshold to detect high density screen.
24 // Higher DPI than this will use device_scale_factor=2.
25 const unsigned int kHighDensityDPIThresholdSmall = 170;
26
27 // The HiDPI threshold for large (usually external) monitors. Lower threshold
28 // makes sense for large monitors, because such monitors should be located
29 // farther from the user's face usually. See http://crbug.com/348279
30 const unsigned int kHighDensityDPIThresholdLarge = 150;
31
32 // The width threshold in mm for "large" monitors.
33 const int kLargeDisplayWidthThresholdMM = 500;
34
35 // 1 inch in mm.
36 const float kInchInMm = 25.4f;
37
38 } // namespace
39
IsDisplaySizeBlackListed(const gfx::Size & physical_size)40 bool IsDisplaySizeBlackListed(const gfx::Size& physical_size) {
41 // Ignore if the reported display is smaller than minimum size.
42 if (physical_size.width() <= kInvalidDisplaySizeList[0][0] ||
43 physical_size.height() <= kInvalidDisplaySizeList[0][1]) {
44 VLOG(1) << "Smaller than minimum display size";
45 return true;
46 }
47 for (size_t i = 1; i < arraysize(kInvalidDisplaySizeList); ++i) {
48 const gfx::Size size(kInvalidDisplaySizeList[i][0],
49 kInvalidDisplaySizeList[i][1]);
50 if (physical_size == size) {
51 VLOG(1) << "Black listed display size detected:" << size.ToString();
52 return true;
53 }
54 }
55 return false;
56 }
57
GetScaleFactor(const gfx::Size & physical_size_in_mm,const gfx::Size & screen_size_in_pixels)58 float GetScaleFactor(const gfx::Size& physical_size_in_mm,
59 const gfx::Size& screen_size_in_pixels) {
60 if (IsDisplaySizeBlackListed(physical_size_in_mm))
61 return 1.0f;
62
63 const unsigned int dpi = (kInchInMm * screen_size_in_pixels.width() /
64 physical_size_in_mm.width());
65 const unsigned int threshold =
66 (physical_size_in_mm.width() >= kLargeDisplayWidthThresholdMM) ?
67 kHighDensityDPIThresholdLarge : kHighDensityDPIThresholdSmall;
68 return (dpi > threshold) ? 2.0f : 1.0f;
69 }
70
71 } // namespace ui
72