• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/x/x11_util.h"
23 #include "ui/compositor/dip_util.h"
24 #include "ui/gfx/display.h"
25 
26 namespace ash {
27 namespace internal {
28 
29 using chromeos::OutputConfigurator;
30 
31 namespace {
32 
33 // The DPI threshold to detect high density screen.
34 // Higher DPI than this will use device_scale_factor=2.
35 const unsigned int kHighDensityDPIThreshold = 160;
36 
37 // 1 inch in mm.
38 const float kInchInMm = 25.4f;
39 
40 // Resolution list are sorted by the area in pixels and the larger
41 // one comes first.
42 struct ResolutionSorter {
operator ()ash::internal::__anonae71a85a0111::ResolutionSorter43   bool operator()(const Resolution& a, const Resolution& b) {
44     return a.size.width() * a.size.height() > b.size.width() * b.size.height();
45   }
46 };
47 
48 }  // namespace
49 
50 // static
GetResolutionList(const OutputConfigurator::OutputSnapshot & output)51 std::vector<Resolution> DisplayChangeObserver::GetResolutionList(
52     const OutputConfigurator::OutputSnapshot& output) {
53   typedef std::map<std::pair<int,int>, Resolution> ResolutionMap;
54   ResolutionMap resolution_map;
55 
56   for (std::map<RRMode, OutputConfigurator::ModeInfo>::const_iterator it =
57        output.mode_infos.begin(); it != output.mode_infos.end(); ++it) {
58     const OutputConfigurator::ModeInfo& mode_info = it->second;
59     const std::pair<int, int> size(mode_info.width, mode_info.height);
60     const Resolution resolution(gfx::Size(mode_info.width, mode_info.height),
61                                 mode_info.interlaced);
62 
63     // Add the resolution if it isn't already present and override interlaced
64     // resolutions with non-interlaced ones.
65     ResolutionMap::iterator resolution_it = resolution_map.find(size);
66     if (resolution_it == resolution_map.end())
67       resolution_map.insert(std::make_pair(size, resolution));
68     else if (resolution_it->second.interlaced && !resolution.interlaced)
69       resolution_it->second = resolution;
70   }
71 
72   std::vector<Resolution> resolution_list;
73   for (ResolutionMap::const_iterator iter = resolution_map.begin();
74        iter != resolution_map.end();
75        ++iter) {
76     resolution_list.push_back(iter->second);
77   }
78   std::sort(resolution_list.begin(), resolution_list.end(), ResolutionSorter());
79   return resolution_list;
80 }
81 
DisplayChangeObserver()82 DisplayChangeObserver::DisplayChangeObserver() {
83   Shell::GetInstance()->AddShellObserver(this);
84 }
85 
~DisplayChangeObserver()86 DisplayChangeObserver::~DisplayChangeObserver() {
87   Shell::GetInstance()->RemoveShellObserver(this);
88 }
89 
GetStateForDisplayIds(const std::vector<int64> & display_ids) const90 chromeos::OutputState DisplayChangeObserver::GetStateForDisplayIds(
91     const std::vector<int64>& display_ids) const {
92   if (CommandLine::ForCurrentProcess()->HasSwitch(
93           switches::kAshForceMirrorMode)) {
94     return chromeos::STATE_DUAL_MIRROR;
95   }
96 
97   CHECK_EQ(2U, display_ids.size());
98   DisplayIdPair pair = std::make_pair(display_ids[0], display_ids[1]);
99   DisplayLayout layout = Shell::GetInstance()->display_manager()->
100       layout_store()->GetRegisteredDisplayLayout(pair);
101   return layout.mirrored ?
102       chromeos::STATE_DUAL_MIRROR : chromeos::STATE_DUAL_EXTENDED;
103 }
104 
GetResolutionForDisplayId(int64 display_id,int * width,int * height) const105 bool DisplayChangeObserver::GetResolutionForDisplayId(int64 display_id,
106                                                       int* width,
107                                                       int* height) const {
108   gfx::Size resolution;
109   if (!Shell::GetInstance()->display_manager()->
110       GetSelectedResolutionForDisplayId(display_id, &resolution)) {
111     return false;
112   }
113 
114   *width = resolution.width();
115   *height = resolution.height();
116   return true;
117 }
118 
OnDisplayModeChanged(const std::vector<OutputConfigurator::OutputSnapshot> & outputs)119 void DisplayChangeObserver::OnDisplayModeChanged(
120     const std::vector<OutputConfigurator::OutputSnapshot>& outputs) {
121   std::vector<DisplayInfo> displays;
122   std::set<int64> ids;
123   for (size_t i = 0; i < outputs.size(); ++i) {
124     const OutputConfigurator::OutputSnapshot& output = outputs[i];
125 
126     if (output.is_internal &&
127         gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID) {
128       // Fall back to output index. crbug.com/180100
129       gfx::Display::SetInternalDisplayId(
130           output.display_id == gfx::Display::kInvalidDisplayID ? output.index :
131           output.display_id);
132     }
133 
134     const OutputConfigurator::ModeInfo* mode_info =
135         OutputConfigurator::GetModeInfo(output, output.current_mode);
136     if (!mode_info)
137       continue;
138 
139     float device_scale_factor = 1.0f;
140     if (!ui::IsXDisplaySizeBlackListed(output.width_mm, output.height_mm) &&
141         (kInchInMm * mode_info->width / output.width_mm) >
142         kHighDensityDPIThreshold) {
143       device_scale_factor = 2.0f;
144     }
145     gfx::Rect display_bounds(
146         output.x, output.y, mode_info->width, mode_info->height);
147 
148     std::vector<Resolution> resolutions;
149     if (!output.is_internal)
150       resolutions = GetResolutionList(output);
151 
152     std::string name = output.is_internal ?
153         l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) :
154         chromeos::GetDisplayName(output.output);
155     if (name.empty())
156       name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
157 
158     bool has_overscan = false;
159     chromeos::GetOutputOverscanFlag(output.output, &has_overscan);
160 
161     int64 id = output.display_id;
162     if (id == gfx::Display::kInvalidDisplayID || ids.find(id) != ids.end())
163       id = output.index;
164     ids.insert(id);
165 
166     displays.push_back(DisplayInfo(id, name, has_overscan));
167     displays.back().set_device_scale_factor(device_scale_factor);
168     displays.back().SetBounds(display_bounds);
169     displays.back().set_native(true);
170     displays.back().set_resolutions(resolutions);
171     displays.back().set_touch_support(
172         output.touch_device_id == 0 ? gfx::Display::TOUCH_SUPPORT_UNAVAILABLE :
173                                       gfx::Display::TOUCH_SUPPORT_AVAILABLE);
174   }
175 
176   // DisplayManager can be null during the boot.
177   Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays);
178 }
179 
OnAppTerminating()180 void DisplayChangeObserver::OnAppTerminating() {
181 #if defined(USE_ASH)
182   // Stop handling display configuration events once the shutdown
183   // process starts. crbug.com/177014.
184   Shell::GetInstance()->output_configurator()->Stop();
185 #endif
186 }
187 
188 }  // namespace internal
189 }  // namespace ash
190