OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/display/display_change_observer_x11.h" | 5 #include "ash/display/display_change_observer_x11.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include <X11/extensions/Xrandr.h> | |
13 | |
14 #include "ash/ash_switches.h" | 12 #include "ash/ash_switches.h" |
15 #include "ash/display/display_info.h" | 13 #include "ash/display/display_info.h" |
16 #include "ash/display/display_layout_store.h" | 14 #include "ash/display/display_layout_store.h" |
17 #include "ash/display/display_manager.h" | 15 #include "ash/display/display_manager.h" |
18 #include "ash/display/display_util_x11.h" | 16 #include "ash/display/display_util_x11.h" |
19 #include "ash/shell.h" | 17 #include "ash/shell.h" |
20 #include "base/command_line.h" | 18 #include "base/command_line.h" |
21 #include "base/message_loop/message_pump_aurax11.h" | 19 #include "base/message_loop/message_pump_aurax11.h" |
22 #include "chromeos/display/output_util.h" | 20 #include "chromeos/display/output_util.h" |
23 #include "grit/ash_strings.h" | 21 #include "grit/ash_strings.h" |
24 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
25 #include "ui/compositor/dip_util.h" | 23 #include "ui/compositor/dip_util.h" |
26 #include "ui/gfx/display.h" | 24 #include "ui/gfx/display.h" |
27 | 25 |
28 namespace ash { | 26 namespace ash { |
29 namespace internal { | 27 namespace internal { |
30 | 28 |
31 using chromeos::OutputConfigurator; | 29 using chromeos::OutputConfigurator; |
32 | 30 |
33 namespace { | 31 namespace { |
34 | 32 |
35 // The DPI threshold to detect high density screen. | 33 // The DPI threshold to detect high density screen. |
36 // Higher DPI than this will use device_scale_factor=2. | 34 // Higher DPI than this will use device_scale_factor=2. |
37 const unsigned int kHighDensityDPIThreshold = 160; | 35 const unsigned int kHighDensityDPIThreshold = 160; |
38 | 36 |
39 // 1 inch in mm. | 37 // 1 inch in mm. |
40 const float kInchInMm = 25.4f; | 38 const float kInchInMm = 25.4f; |
41 | 39 |
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 | 40 } // namespace |
50 | 41 |
51 DisplayChangeObserverX11::DisplayChangeObserverX11() | 42 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); | 43 Shell::GetInstance()->AddShellObserver(this); |
59 } | 44 } |
60 | 45 |
61 DisplayChangeObserverX11::~DisplayChangeObserverX11() { | 46 DisplayChangeObserverX11::~DisplayChangeObserverX11() { |
62 Shell::GetInstance()->RemoveShellObserver(this); | 47 Shell::GetInstance()->RemoveShellObserver(this); |
63 } | 48 } |
64 | 49 |
65 chromeos::OutputState DisplayChangeObserverX11::GetStateForDisplayIds( | 50 chromeos::OutputState DisplayChangeObserverX11::GetStateForDisplayIds( |
66 const std::vector<int64>& display_ids) const { | 51 const std::vector<int64>& display_ids) const { |
67 if (CommandLine::ForCurrentProcess()->HasSwitch( | 52 if (CommandLine::ForCurrentProcess()->HasSwitch( |
(...skipping 19 matching lines...) Expand all Loading... | |
87 return false; | 72 return false; |
88 } | 73 } |
89 | 74 |
90 *width = resolution.width(); | 75 *width = resolution.width(); |
91 *height = resolution.height(); | 76 *height = resolution.height(); |
92 return true; | 77 return true; |
93 } | 78 } |
94 | 79 |
95 void DisplayChangeObserverX11::OnDisplayModeChanged( | 80 void DisplayChangeObserverX11::OnDisplayModeChanged( |
96 const std::vector<OutputConfigurator::OutputSnapshot>& outputs) { | 81 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; | 82 std::vector<DisplayInfo> displays; |
110 std::set<int64> ids; | 83 std::set<int64> ids; |
111 for (int output_index = 0; output_index < screen_resources->noutput; | 84 for (size_t output_index = 0; output_index < outputs.size(); ++output_index) { |
112 output_index++) { | 85 const OutputConfigurator::OutputSnapshot& output = outputs[output_index]; |
113 XID output = screen_resources->outputs[output_index]; | |
114 XRROutputInfo *output_info = | |
115 XRRGetOutputInfo(xdisplay_, screen_resources, output); | |
116 | 86 |
117 const bool is_internal = chromeos::IsInternalOutputName( | 87 if (output.is_internal && |
118 std::string(output_info->name)); | |
119 | |
120 if (is_internal && | |
121 gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID) { | 88 gfx::Display::InternalDisplayId() == gfx::Display::kInvalidDisplayID) { |
122 int64 id = GetDisplayId(output, output_index); | 89 // Fall back to output index. crbug.com/180100 |
123 // Fallback to output index. crbug.com/180100 | |
124 gfx::Display::SetInternalDisplayId( | 90 gfx::Display::SetInternalDisplayId( |
125 id == gfx::Display::kInvalidDisplayID ? output_index : id); | 91 output.display_id == gfx::Display::kInvalidDisplayID ? output_index : |
92 output.display_id); | |
oshima
2013/08/16 23:13:05
output_index is different from the index of output
Daniel Erat
2013/08/16 23:35:36
Thanks, I'll store the original index in the Outpu
oshima
2013/08/19 20:00:24
This doesn't seem to be in the CL?
Daniel Erat
2013/08/19 22:09:30
Whoops, sorry. Adding.
| |
126 } | 93 } |
127 | 94 |
128 if (output_info->connection != RR_Connected) { | 95 const OutputConfigurator::ModeInfo* mode_info = |
129 XRRFreeOutputInfo(output_info); | 96 OutputConfigurator::GetModeInfo(output, output.current_mode); |
97 if (!mode_info) | |
130 continue; | 98 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 | 99 |
146 float device_scale_factor = 1.0f; | 100 float device_scale_factor = 1.0f; |
147 if (!ShouldIgnoreSize(output_info->mm_width, output_info->mm_height) && | 101 if (!ShouldIgnoreSize(output.width_mm, output.height_mm) && |
148 (kInchInMm * mode->width / output_info->mm_width) > | 102 (kInchInMm * mode_info->width / output.width_mm) > |
149 kHighDensityDPIThreshold) { | 103 kHighDensityDPIThreshold) { |
150 device_scale_factor = 2.0f; | 104 device_scale_factor = 2.0f; |
151 } | 105 } |
152 gfx::Rect display_bounds( | 106 gfx::Rect display_bounds( |
153 crtc_info->x, crtc_info->y, mode->width, mode->height); | 107 output.x, output.y, mode_info->width, mode_info->height); |
154 | 108 |
155 std::vector<Resolution> resolutions; | 109 std::vector<Resolution> resolutions; |
156 if (!is_internal) | 110 if (!output.is_internal) |
157 resolutions = GetResolutionList(screen_resources, output_info); | 111 resolutions = GetResolutionList(output); |
158 | 112 |
159 XRRFreeOutputInfo(output_info); | 113 std::string name = output.is_internal ? |
160 | |
161 std::string name = is_internal ? | |
162 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) : | 114 l10n_util::GetStringUTF8(IDS_ASH_INTERNAL_DISPLAY_NAME) : |
163 chromeos::GetDisplayName(output); | 115 chromeos::GetDisplayName(output.output); |
164 if (name.empty()) | 116 if (name.empty()) |
165 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); | 117 name = l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); |
166 | 118 |
167 bool has_overscan = false; | 119 bool has_overscan = false; |
168 chromeos::GetOutputOverscanFlag(output, &has_overscan); | 120 chromeos::GetOutputOverscanFlag(output.output, &has_overscan); |
169 | 121 |
170 int64 id = GetDisplayId(output, output_index); | 122 int64 id = output.display_id; |
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()) | 123 if (id == gfx::Display::kInvalidDisplayID || ids.find(id) != ids.end()) |
174 id = output_index; | 124 id = output_index; |
oshima
2013/08/16 23:13:05
same here.
| |
175 ids.insert(id); | 125 ids.insert(id); |
176 | 126 |
177 displays.push_back(DisplayInfo(id, name, has_overscan)); | 127 displays.push_back(DisplayInfo(id, name, has_overscan)); |
178 displays.back().set_device_scale_factor(device_scale_factor); | 128 displays.back().set_device_scale_factor(device_scale_factor); |
179 displays.back().SetBounds(display_bounds); | 129 displays.back().SetBounds(display_bounds); |
180 displays.back().set_native(true); | 130 displays.back().set_native(true); |
181 displays.back().set_resolutions(resolutions); | 131 displays.back().set_resolutions(resolutions); |
182 } | 132 } |
183 | 133 |
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. | 134 // DisplayManager can be null during the boot. |
192 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays); | 135 Shell::GetInstance()->display_manager()->OnNativeDisplaysChanged(displays); |
193 } | 136 } |
194 | 137 |
195 void DisplayChangeObserverX11::OnAppTerminating() { | 138 void DisplayChangeObserverX11::OnAppTerminating() { |
196 #if defined(USE_ASH) | 139 #if defined(USE_ASH) |
197 // Stop handling display configuration events once the shutdown | 140 // Stop handling display configuration events once the shutdown |
198 // process starts. crbug.com/177014. | 141 // process starts. crbug.com/177014. |
199 Shell::GetInstance()->output_configurator()->Stop(); | 142 Shell::GetInstance()->output_configurator()->Stop(); |
200 #endif | 143 #endif |
201 } | 144 } |
202 | 145 |
203 } // namespace internal | 146 } // namespace internal |
204 } // namespace ash | 147 } // namespace ash |
OLD | NEW |