display_change_observer_chromeos.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#include "ash/display/display_change_observer_chromeos.h"
6
7#include <algorithm>
8#include <map>
9#include <set>
10#include <vector>
11
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"
24
25namespace ash {
26namespace internal {
27
28using chromeos::OutputConfigurator;
29
30namespace {
31
32// The DPI threshold to detect high density screen.
33// Higher DPI than this will use device_scale_factor=2.
34const unsigned int kHighDensityDPIThreshold = 160;
35
36// 1 inch in mm.
37const float kInchInMm = 25.4f;
38
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.
42const unsigned long kInvalidDisplaySizeList[][2] = {
43  {40, 30},
44  {50, 40},
45  {160, 90},
46  {160, 100},
47};
48
49// Resolution list are sorted by the area in pixels and the larger
50// one comes first.
51struct ResolutionSorter {
52  bool operator()(const Resolution& a, const Resolution& b) {
53    return a.size.width() * a.size.height() > b.size.width() * b.size.height();
54  }
55};
56
57}  // namespace
58
59// static
60bool 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";
66    return true;
67  }
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];
73      return true;
74    }
75  }
76  return false;
77}
78
79// static
80std::vector<Resolution> DisplayChangeObserver::GetResolutionList(
81    const OutputConfigurator::OutputSnapshot& output) {
82  typedef std::map<std::pair<int,int>, Resolution> ResolutionMap;
83  ResolutionMap resolution_map;
84
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);
91
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;
99  }
100
101  std::vector<Resolution> resolution_list;
102  for (ResolutionMap::const_iterator iter = resolution_map.begin();
103       iter != resolution_map.end();
104       ++iter) {
105    resolution_list.push_back(iter->second);
106  }
107  std::sort(resolution_list.begin(), resolution_list.end(), ResolutionSorter());
108  return resolution_list;
109}
110
111DisplayChangeObserver::DisplayChangeObserver() {
112  Shell::GetInstance()->AddShellObserver(this);
113}
114
115DisplayChangeObserver::~DisplayChangeObserver() {
116  Shell::GetInstance()->RemoveShellObserver(this);
117}
118
119chromeos::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;
124  }
125
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;
132}
133
134bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id,
135                                                      int* width,
136                                                      int* height) const {
137  gfx::Size resolution;
138  if (!Shell::GetInstance()->display_manager()->
139      GetSelectedResolutionForDisplayId(display_id, &resolution)) {
140    return false;
141  }
142
143  *width = resolution.width();
144  *height = resolution.height();
145  return true;
146}
147
148void DisplayChangeObserver::OnDisplayModeChanged(
149    const std::vector<OutputConfigurator::OutputSnapshot>& outputs) {
150  std::vector<DisplayInfo> displays;
151  std::set<int64> ids;
152  for (size_t i = 0; i < outputs.size(); ++i) {
153    const OutputConfigurator::OutputSnapshot& output = outputs[i];
154
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 :
160          output.display_id);
161    }
162
163    const OutputConfigurator::ModeInfo* mode_info =
164        OutputConfigurator::GetModeInfo(output, output.current_mode);
165    if (!mode_info)
166      continue;
167
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;
173    }
174    gfx::Rect display_bounds(
175        output.x, output.y, mode_info->width, mode_info->height);
176
177    std::vector<Resolution> resolutions;
178    if (!output.is_internal)
179      resolutions = GetResolutionList(output);
180
181    std::string name = output.is_internal ?
182        l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) :
183        chromeos::GetDisplayName(output.output);
184    if (name.empty())
185      name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
186
187    bool has_overscan = false;
188    chromeos::GetOutputOverscanFlag(output.output, &has_overscan);
189
190    int64 id = output.display_id;
191    if (id == gfx::Display::kInvalidDisplayID || ids.find(id) != ids.end())
192      id = output.index;
193    ids.insert(id);
194
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);
203  }
204
205  // DisplayManager can be null during the boot.
206  Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays);
207}
208
209void DisplayChangeObserver::OnAppTerminating() {
210#if defined(USE_ASH)
211  // Stop handling display configuration events once the shutdown
212  // process starts. crbug.com/177014.
213  Shell::GetInstance()->output_configurator()->Stop();
214#endif
215}
216
217}  // namespace internal
218}  // namespace ash
219