OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_x11.h" | |
6 | |
7 #include <algorithm> | |
8 #include <map> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
12 #include <X11/extensions/Xrandr.h> | |
13 | |
14 #include "ash/ash_switches.h" | |
15 #include "ash/display/display_info.h" | |
16 #include "ash/display/display_layout_store.h" | |
17 #include "ash/display/display_manager.h" | |
18 #include "ash/display/display_util_x11.h" | |
19 #include "ash/shell.h" | |
20 #include "base/command_line.h" | |
21 #include "base/message_loop/message_pump_aurax11.h" | |
22 #include "chromeos/display/output_util.h" | |
23 #include "grit/ash_strings.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "ui/compositor/dip_util.h" | |
26 #include "ui/gfx/display.h" | |
27 | |
28 namespace ash { | |
29 namespace internal { | |
30 | |
31 using chromeos::OutputConfigurator; | |
32 | |
33 namespace { | |
34 | |
35 // The DPI threshold to detect high density screen. | |
36 // Higher DPI than this will use device_scale_factor=2. | |
37 const unsigned int kHighDensityDPIThreshold = 160; | |
38 | |
39 // 1 inch in mm. | |
40 const float kInchInMm = 25.4f; | |
41 | |
42 int64 GetDisplayId(XID output, size_t output_index) { | |
43 int64 display_id; | |
44 if (chromeos::GetDisplayId(output, output_index, &display_id)) | |
45 return display_id; | |
46 return gfx::Display::kInvalidDisplayID; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 DisplayChangeObserverX11::DisplayChangeObserverX11() | |
52 : xdisplay_(base::MessagePumpAuraX11::GetDefaultXDisplay()), | |
53 x_root_window_(DefaultRootWindow(xdisplay_)), | |
54 xrandr_event_base_(0) { | |
55 int error_base_ignored; | |
56 XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored); | |
57 | |
58 Shell::GetInstance()->AddShellObserver(this); | |
59 } | |
60 | |
61 DisplayChangeObserverX11::~DisplayChangeObserverX11() { | |
62 Shell::GetInstance()->RemoveShellObserver(this); | |
63 } | |
64 | |
65 chromeos::OutputState DisplayChangeObserverX11::GetStateForDisplayIds( | |
66 const std::vector<int64>& display_ids) const { | |
67 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
68 switches::kAshForceMirrorMode)) { | |
69 return chromeos::STATE_DUAL_MIRROR; | |
70 } | |
71 | |
72 CHECK_EQ(2U, display_ids.size()); | |
73 DisplayIdPair pair = std::make_pair(display_ids[0], display_ids[1]); | |
74 DisplayLayout layout = Shell::GetInstance()->display_manager()-> | |
75 layout_store()->GetRegisteredDisplayLayout(pair); | |
76 return layout.mirrored ? | |
77 chromeos::STATE_DUAL_MIRROR : chromeos::STATE_DUAL_EXTENDED; | |
78 } | |
79 | |
80 bool DisplayChangeObserverX11::GetResolutionForDisplayId(int64 display_id, | |
81 int* width, | |
82 int* height) const { | |
83 | |
84 gfx::Size resolution; | |
85 if (!Shell::GetInstance()->display_manager()-> | |
86 GetSelectedResolutionForDisplayId(display_id, &resolution)) { | |
87 return false; | |
88 } | |
89 | |
90 *width = resolution.width(); | |
91 *height = resolution.height(); | |
92 return true; | |
93 } | |
94 | |
95 void DisplayChangeObserverX11::OnDisplayModeChanged( | |
96 const std::vector<OutputConfigurator::OutputSnapshot>& outputs) { | |
97 // TODO(derat): Use |outputs| instead of re-fetching information. | |
98 XRRScreenResources* screen_resources = | |
99 XRRGetScreenResources(xdisplay_, x_root_window_); | |
100 std::map<XID, XRRCrtcInfo*> crtc_info_map; | |
101 | |
102 for (int c = 0; c < screen_resources->ncrtc; c++) { | |
103 XID crtc_id = screen_resources->crtcs[c]; | |
104 XRRCrtcInfo *crtc_info = | |
105 XRRGetCrtcInfo(xdisplay_, screen_resources, crtc_id); | |
106 crtc_info_map[crtc_id] = crtc_info; | |
107 } | |
108 | |
109 std::vector<DisplayInfo> displays; | |
110 std::set<int64> ids; | |
111 for (int output_index = 0; output_index < screen_resources->noutput; | |
112 output_index++) { | |
113 XID output = screen_resources->outputs[output_index]; | |
114 XRROutputInfo *output_info = | |
115 XRRGetOutputInfo(xdisplay_, screen_resources, output); | |
116 | |
117 const bool is_internal = chromeos::IsInternalOutputName( | |
118 std::string(output_info->name)); | |
119 | |
120 if (is_internal && | |
121 gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID) { | |
122 int64 id = GetDisplayId(output, output_index); | |
123 // Fallback to output index. crbug.com/180100 | |
124 gfx::Display::SetInternalDisplayId( | |
125 id == gfx::Display::kInvalidDisplayID ? output_index : id); | |
126 } | |
127 | |
128 if (output_info->connection != RR_Connected) { | |
129 XRRFreeOutputInfo(output_info); | |
130 continue; | |
131 } | |
132 const XRRCrtcInfo* crtc_info = crtc_info_map[output_info->crtc]; | |
133 if (!crtc_info) { | |
134 LOG(WARNING) << "Crtc not found for output: output_index=" | |
135 << output_index; | |
136 continue; | |
137 } | |
138 const XRRModeInfo* mode = | |
139 chromeos::FindXRRModeInfo(screen_resources, crtc_info->mode); | |
140 if (!mode) { | |
141 LOG(WARNING) << "Could not find a mode for the output: output_index=" | |
142 << output_index; | |
143 continue; | |
144 } | |
145 | |
146 float device_scale_factor = 1.0f; | |
147 if (!ShouldIgnoreSize(output_info->mm_width, output_info->mm_height) && | |
148 (kInchInMm * mode->width / output_info->mm_width) > | |
149 kHighDensityDPIThreshold) { | |
150 device_scale_factor = 2.0f; | |
151 } | |
152 gfx::Rect display_bounds( | |
153 crtc_info->x, crtc_info->y, mode->width, mode->height); | |
154 | |
155 std::vector<Resolution> resolutions; | |
156 if (!is_internal) | |
157 resolutions = GetResolutionList(screen_resources, output_info); | |
158 | |
159 XRRFreeOutputInfo(output_info); | |
160 | |
161 std::string name = is_internal ? | |
162 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) : | |
163 chromeos::GetDisplayName(output); | |
164 if (name.empty()) | |
165 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); | |
166 | |
167 bool has_overscan = false; | |
168 chromeos::GetOutputOverscanFlag(output, &has_overscan); | |
169 | |
170 int64 id = GetDisplayId(output, output_index); | |
171 | |
172 // If ID is invalid or there is an duplicate, just use output index. | |
173 if (id == gfx::Display::kInvalidDisplayID || ids.find(id) != ids.end()) | |
174 id = output_index; | |
175 ids.insert(id); | |
176 | |
177 displays.push_back(DisplayInfo(id, name, has_overscan)); | |
178 displays.back().set_device_scale_factor(device_scale_factor); | |
179 displays.back().SetBounds(display_bounds); | |
180 displays.back().set_native(true); | |
181 displays.back().set_resolutions(resolutions); | |
182 } | |
183 | |
184 // Free all allocated resources. | |
185 for (std::map<XID, XRRCrtcInfo*>::const_iterator iter = crtc_info_map.begin(); | |
186 iter != crtc_info_map.end(); ++iter) { | |
187 XRRFreeCrtcInfo(iter->second); | |
188 } | |
189 XRRFreeScreenResources(screen_resources); | |
190 | |
191 // DisplayManager can be null during the boot. | |
192 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays); | |
193 } | |
194 | |
195 void DisplayChangeObserverX11::OnAppTerminating() { | |
196 #if defined(USE_ASH) | |
197 // Stop handling display configuration events once the shutdown | |
198 // process starts. crbug.com/177014. | |
199 Shell::GetInstance()->output_configurator()->Stop(); | |
200 #endif | |
201 } | |
202 | |
203 } // namespace internal | |
204 } // namespace ash | |
OLD | NEW |