1 // Copyright 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 "ash/display/display_change_observer_chromeos.h"
12 #include "ash/ash_switches.h"
13 #include "ash/display/display_info.h"
14 #include "ash/display/display_layout_store.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/shell.h"
17 #include "base/command_line.h"
18 #include "base/logging.h"
19 #include "chromeos/display/output_util.h"
20 #include "grit/ash_strings.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/compositor/dip_util.h"
23 #include "ui/gfx/display.h"
28 using chromeos::OutputConfigurator;
32 // The DPI threshold to detect high density screen.
33 // Higher DPI than this will use device_scale_factor=2.
34 const unsigned int kHighDensityDPIThreshold = 160;
37 const float kInchInMm = 25.4f;
39 // A list of bogus sizes in mm that X detects that should be ignored.
40 // See crbug.com/136533. The first element maintains the minimum
41 // size required to be valid size.
42 const unsigned long kInvalidDisplaySizeList[][2] = {
49 // Resolution list are sorted by the area in pixels and the larger
51 struct ResolutionSorter {
52 bool operator()(const Resolution& a, const Resolution& b) {
53 return a.size.width() * a.size.height() > b.size.width() * b.size.height();
60 bool DisplayChangeObserver::ShouldIgnoreSize(unsigned long mm_width,
61 unsigned long mm_height) {
62 // Ignore if the reported display is smaller than minimum size.
63 if (mm_width <= kInvalidDisplaySizeList[0][0] ||
64 mm_height <= kInvalidDisplaySizeList[0][1]) {
65 LOG(WARNING) << "Smaller than minimum display size";
68 for (unsigned long i = 1 ; i < arraysize(kInvalidDisplaySizeList); ++i) {
69 const unsigned long* size = kInvalidDisplaySizeList[i];
70 if (mm_width == size[0] && mm_height == size[1]) {
71 LOG(WARNING) << "Black listed display size detected:"
72 << size[0] << "x" << size[1];
80 std::vector<Resolution> DisplayChangeObserver::GetResolutionList(
81 const OutputConfigurator::OutputSnapshot& output) {
82 typedef std::map<std::pair<int,int>, Resolution> ResolutionMap;
83 ResolutionMap resolution_map;
85 for (std::map<RRMode, OutputConfigurator::ModeInfo>::const_iterator it =
86 output.mode_infos.begin(); it != output.mode_infos.end(); ++it) {
87 const OutputConfigurator::ModeInfo& mode_info = it->second;
88 const std::pair<int, int> size(mode_info.width, mode_info.height);
89 const Resolution resolution(gfx::Size(mode_info.width, mode_info.height),
90 mode_info.interlaced);
92 // Add the resolution if it isn't already present and override interlaced
93 // resolutions with non-interlaced ones.
94 ResolutionMap::iterator resolution_it = resolution_map.find(size);
95 if (resolution_it == resolution_map.end())
96 resolution_map.insert(std::make_pair(size, resolution));
97 else if (resolution_it->second.interlaced && !resolution.interlaced)
98 resolution_it->second = resolution;
101 std::vector<Resolution> resolution_list;
102 for (ResolutionMap::const_iterator iter = resolution_map.begin();
103 iter != resolution_map.end();
105 resolution_list.push_back(iter->second);
107 std::sort(resolution_list.begin(), resolution_list.end(), ResolutionSorter());
108 return resolution_list;
111 DisplayChangeObserver::DisplayChangeObserver() {
112 Shell::GetInstance()->AddShellObserver(this);
115 DisplayChangeObserver::~DisplayChangeObserver() {
116 Shell::GetInstance()->RemoveShellObserver(this);
119 chromeos::OutputState DisplayChangeObserver::GetStateForDisplayIds(
120 const std::vector<int64>& display_ids) const {
121 if (CommandLine::ForCurrentProcess()->HasSwitch(
122 switches::kAshForceMirrorMode)) {
123 return chromeos::STATE_DUAL_MIRROR;
126 CHECK_EQ(2U, display_ids.size());
127 DisplayIdPair pair = std::make_pair(display_ids[0], display_ids[1]);
128 DisplayLayout layout = Shell::GetInstance()->display_manager()->
129 layout_store()->GetRegisteredDisplayLayout(pair);
130 return layout.mirrored ?
131 chromeos::STATE_DUAL_MIRROR : chromeos::STATE_DUAL_EXTENDED;
134 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id,
137 gfx::Size resolution;
138 if (!Shell::GetInstance()->display_manager()->
139 GetSelectedResolutionForDisplayId(display_id, &resolution)) {
143 *width = resolution.width();
144 *height = resolution.height();
148 void DisplayChangeObserver::OnDisplayModeChanged(
149 const std::vector<OutputConfigurator::OutputSnapshot>& outputs) {
150 std::vector<DisplayInfo> displays;
152 for (size_t i = 0; i < outputs.size(); ++i) {
153 const OutputConfigurator::OutputSnapshot& output = outputs[i];
155 if (output.is_internal &&
156 gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID) {
157 // Fall back to output index. crbug.com/180100
158 gfx::Display::SetInternalDisplayId(
159 output.display_id == gfx::Display::kInvalidDisplayID ? output.index :
163 const OutputConfigurator::ModeInfo* mode_info =
164 OutputConfigurator::GetModeInfo(output, output.current_mode);
168 float device_scale_factor = 1.0f;
169 if (!ShouldIgnoreSize(output.width_mm, output.height_mm) &&
170 (kInchInMm * mode_info->width / output.width_mm) >
171 kHighDensityDPIThreshold) {
172 device_scale_factor = 2.0f;
174 gfx::Rect display_bounds(
175 output.x, output.y, mode_info->width, mode_info->height);
177 std::vector<Resolution> resolutions;
178 if (!output.is_internal)
179 resolutions = GetResolutionList(output);
181 std::string name = output.is_internal ?
182 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) :
183 chromeos::GetDisplayName(output.output);
185 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
187 bool has_overscan = false;
188 chromeos::GetOutputOverscanFlag(output.output, &has_overscan);
190 int64 id = output.display_id;
191 if (id == gfx::Display::kInvalidDisplayID || ids.find(id) != ids.end())
195 displays.push_back(DisplayInfo(id, name, has_overscan));
196 displays.back().set_device_scale_factor(device_scale_factor);
197 displays.back().SetBounds(display_bounds);
198 displays.back().set_native(true);
199 displays.back().set_resolutions(resolutions);
200 displays.back().set_touch_support(
201 output.touch_device_id == 0 ? gfx::Display::TOUCH_SUPPORT_UNAVAILABLE :
202 gfx::Display::TOUCH_SUPPORT_AVAILABLE);
205 // DisplayManager can be null during the boot.
206 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays);
209 void DisplayChangeObserver::OnAppTerminating() {
211 // Stop handling display configuration events once the shutdown
212 // process starts. crbug.com/177014.
213 Shell::GetInstance()->output_configurator()->Stop();
217 } // namespace internal