Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Side by Side Diff: chromeos/display/output_configurator.h

Issue 187073002: Refactoring display configuration state to allow generic state objects (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ordering Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #ifndef CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ 5 #ifndef CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_
6 #define CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ 6 #define CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/event_types.h" 13 #include "base/event_types.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h" 15 #include "base/observer_list.h"
16 #include "base/timer/timer.h" 16 #include "base/timer/timer.h"
17 #include "chromeos/chromeos_export.h" 17 #include "chromeos/chromeos_export.h"
18 #include "chromeos/display/native_display_observer.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 18 #include "third_party/cros_system_api/dbus/service_constants.h"
19 #include "ui/display/chromeos/native_display_observer.h"
20 #include "ui/display/display_constants.h" 20 #include "ui/display/display_constants.h"
21 21
22 // Forward declarations for Xlib and Xrandr. 22 namespace gfx {
23 // This is so unused X definitions don't pollute the namespace. 23 class Point;
24 typedef unsigned long XID; 24 class Size;
25 typedef XID RROutput; 25 }
26 typedef XID RRCrtc; 26
27 typedef XID RRMode; 27 namespace ui {
28 class DisplayMode;
29 class DisplaySnapshot;
30 class NativeDisplayDelegate;
31 }
28 32
29 namespace chromeos { 33 namespace chromeos {
30 34
31 class NativeDisplayDelegate;
32
33 // This class interacts directly with the underlying Xrandr API to manipulate 35 // This class interacts directly with the underlying Xrandr API to manipulate
34 // CTRCs and Outputs. 36 // CTRCs and Outputs.
35 class CHROMEOS_EXPORT OutputConfigurator 37 class CHROMEOS_EXPORT OutputConfigurator : public ui::NativeDisplayObserver {
36 : public NativeDisplayObserver {
37 public: 38 public:
38 typedef uint64_t OutputProtectionClientId; 39 typedef uint64_t OutputProtectionClientId;
39 static const OutputProtectionClientId kInvalidClientId = 0; 40 static const OutputProtectionClientId kInvalidClientId = 0;
40 41
41 struct ModeInfo {
42 ModeInfo();
43 ModeInfo(int width, int height, bool interlaced, float refresh_rate);
44
45 int width;
46 int height;
47 bool interlaced;
48 float refresh_rate;
49 };
50
51 typedef std::map<RRMode, ModeInfo> ModeInfoMap;
52
53 struct CoordinateTransformation { 42 struct CoordinateTransformation {
54 // Initialized to the identity transformation. 43 // Initialized to the identity transformation.
55 CoordinateTransformation(); 44 CoordinateTransformation();
56 45
57 float x_scale; 46 float x_scale;
58 float x_offset; 47 float x_offset;
59 float y_scale; 48 float y_scale;
60 float y_offset; 49 float y_offset;
61 }; 50 };
62 51
63 // Information about an output's current state. 52 struct DisplayState {
64 struct OutputSnapshot { 53 DisplayState();
65 OutputSnapshot();
66 ~OutputSnapshot();
67 54
68 RROutput output; 55 ui::DisplaySnapshot* display; // Not owned.
69
70 // CRTC that should be used for this output. Not necessarily the CRTC
71 // that XRandR reports is currently being used.
72 RRCrtc crtc;
73
74 // Mode currently being used by the output.
75 RRMode current_mode;
76
77 // "Best" mode supported by the output.
78 RRMode native_mode;
79
80 // Mode used when displaying the same desktop on multiple outputs.
81 RRMode mirror_mode;
82
83 // User-selected mode for the output.
84 RRMode selected_mode;
85
86 // Output's origin on the framebuffer.
87 int x;
88 int y;
89
90 // Output's physical dimensions.
91 uint64 width_mm;
92 uint64 height_mm;
93
94 bool is_aspect_preserving_scaling;
95
96 // The type of output.
97 ui::OutputType type;
98
99 // Map from mode IDs to details about the corresponding modes.
100 ModeInfoMap mode_infos;
101 56
102 // XInput device ID or 0 if this output isn't a touchscreen. 57 // XInput device ID or 0 if this output isn't a touchscreen.
103 int touch_device_id; 58 int touch_device_id;
104 59
105 CoordinateTransformation transform; 60 CoordinateTransformation transform;
106 61
107 // Display id for this output. 62 // User-selected mode for the output.
108 int64 display_id; 63 const ui::DisplayMode* selected_mode;
109 64
110 bool has_display_id; 65 // Mode used when displaying the same desktop on multiple outputs.
66 const ui::DisplayMode* mirror_mode;
67 };
111 68
112 // This output's index in the array returned by XRandR. Stable even as 69 typedef std::vector<DisplayState> DisplayStateList;
113 // outputs are connected or disconnected.
114 int index;
115 };
116 70
117 class Observer { 71 class Observer {
118 public: 72 public:
119 virtual ~Observer() {} 73 virtual ~Observer() {}
120 74
121 // Called after the display mode has been changed. |output| contains the 75 // Called after the display mode has been changed. |output| contains the
122 // just-applied configuration. Note that the X server is no longer grabbed 76 // just-applied configuration. Note that the X server is no longer grabbed
123 // when this method is called, so the actual configuration could've changed 77 // when this method is called, so the actual configuration could've changed
124 // already. 78 // already.
125 virtual void OnDisplayModeChanged( 79 virtual void OnDisplayModeChanged(
126 const std::vector<OutputSnapshot>& outputs) {} 80 const std::vector<DisplayState>& outputs) {}
127 81
128 // Called after a display mode change attempt failed. |failed_new_state| is 82 // Called after a display mode change attempt failed. |failed_new_state| is
129 // the new state which the system failed to enter. 83 // the new state which the system failed to enter.
130 virtual void OnDisplayModeChangeFailed(ui::OutputState failed_new_state) {} 84 virtual void OnDisplayModeChangeFailed(ui::OutputState failed_new_state) {}
131 }; 85 };
132 86
133 // Interface for classes that make decisions about which output state 87 // Interface for classes that make decisions about which output state
134 // should be used. 88 // should be used.
135 class StateController { 89 class StateController {
136 public: 90 public:
137 virtual ~StateController() {} 91 virtual ~StateController() {}
138 92
139 // Called when displays are detected. 93 // Called when displays are detected.
140 virtual ui::OutputState GetStateForDisplayIds( 94 virtual ui::OutputState GetStateForDisplayIds(
141 const std::vector<int64>& display_ids) const = 0; 95 const std::vector<int64>& display_ids) const = 0;
142 96
143 // Queries the resolution (|width|x|height|) in pixels 97 // Queries the resolution (|size|) in pixels to select output mode for the
144 // to select output mode for the given display id. 98 // given display id.
145 virtual bool GetResolutionForDisplayId(int64 display_id, 99 virtual bool GetResolutionForDisplayId(int64 display_id,
146 int* width, 100 gfx::Size* size) const = 0;
147 int* height) const = 0;
148 }; 101 };
149 102
150 // Interface for classes that implement software based mirroring. 103 // Interface for classes that implement software based mirroring.
151 class SoftwareMirroringController { 104 class SoftwareMirroringController {
152 public: 105 public:
153 virtual ~SoftwareMirroringController() {} 106 virtual ~SoftwareMirroringController() {}
154 107
155 // Called when the hardware mirroring failed. 108 // Called when the hardware mirroring failed.
156 virtual void SetSoftwareMirroring(bool enabled) = 0; 109 virtual void SetSoftwareMirroring(bool enabled) = 0;
157 }; 110 };
158 111
159 class TouchscreenDelegate { 112 class TouchscreenDelegate {
160 public: 113 public:
161 virtual ~TouchscreenDelegate() {} 114 virtual ~TouchscreenDelegate() {}
162 115
163 // Searches for touchscreens among input devices, 116 // Searches for touchscreens among input devices,
164 // and tries to match them up to screens in |outputs|. 117 // and tries to match them up to screens in |outputs|.
165 // |outputs| is an array of detected screens. 118 // |outputs| is an array of detected screens.
166 // If a touchscreen with same resolution as an output's native mode 119 // If a touchscreen with same resolution as an output's native mode
167 // is detected, its id will be stored in this output. 120 // is detected, its id will be stored in this output.
168 virtual void AssociateTouchscreens( 121 virtual void AssociateTouchscreens(std::vector<DisplayState>* outputs) = 0;
169 std::vector<OutputSnapshot>* outputs) = 0;
170 122
171 // Configures XInput's Coordinate Transformation Matrix property. 123 // Configures XInput's Coordinate Transformation Matrix property.
172 // |touch_device_id| the ID of the touchscreen device to configure. 124 // |touch_device_id| the ID of the touchscreen device to configure.
173 // |ctm| contains the desired transformation parameters. The offsets 125 // |ctm| contains the desired transformation parameters. The offsets
174 // in it should be normalized so that 1 corresponds to the X or Y axis 126 // in it should be normalized so that 1 corresponds to the X or Y axis
175 // size for the corresponding offset. 127 // size for the corresponding offset.
176 virtual void ConfigureCTM(int touch_device_id, 128 virtual void ConfigureCTM(int touch_device_id,
177 const CoordinateTransformation& ctm) = 0; 129 const CoordinateTransformation& ctm) = 0;
178 }; 130 };
179 131
(...skipping 24 matching lines...) Expand all
204 156
205 // Gap between screens so cursor at bottom of active display doesn't 157 // Gap between screens so cursor at bottom of active display doesn't
206 // partially appear on top of inactive display. Higher numbers guard 158 // partially appear on top of inactive display. Higher numbers guard
207 // against larger cursors, but also waste more memory. 159 // against larger cursors, but also waste more memory.
208 // For simplicity, this is hard-coded to avoid the complexity of always 160 // For simplicity, this is hard-coded to avoid the complexity of always
209 // determining the DPI of the screen and rationalizing which screen we 161 // determining the DPI of the screen and rationalizing which screen we
210 // need to use for the DPI calculation. 162 // need to use for the DPI calculation.
211 // See crbug.com/130188 for initial discussion. 163 // See crbug.com/130188 for initial discussion.
212 static const int kVerticalGap = 60; 164 static const int kVerticalGap = 60;
213 165
214 // Returns a pointer to the ModeInfo struct in |output| corresponding to
215 // |mode|, or NULL if the struct isn't present.
216 static const ModeInfo* GetModeInfo(const OutputSnapshot& output,
217 RRMode mode);
218
219 // Returns the mode within |output| that matches the given size with highest 166 // Returns the mode within |output| that matches the given size with highest
220 // refresh rate. Returns None if no matching output was found. 167 // refresh rate. Returns None if no matching output was found.
221 static RRMode FindOutputModeMatchingSize(const OutputSnapshot& output, 168 static const ui::DisplayMode* FindDisplayModeMatchingSize(
222 int width, 169 const ui::DisplaySnapshot& output,
223 int height); 170 const gfx::Size& size);
224 171
225 OutputConfigurator(); 172 OutputConfigurator();
226 virtual ~OutputConfigurator(); 173 virtual ~OutputConfigurator();
227 174
228 ui::OutputState output_state() const { return output_state_; } 175 ui::OutputState output_state() const { return output_state_; }
229 DisplayPowerState power_state() const { return power_state_; } 176 DisplayPowerState power_state() const { return power_state_; }
230 const std::vector<OutputSnapshot>& cached_outputs() const { 177 const std::vector<DisplayState>& cached_outputs() const {
231 return cached_outputs_; 178 return cached_outputs_;
232 } 179 }
233 180
234 void set_state_controller(StateController* controller) { 181 void set_state_controller(StateController* controller) {
235 state_controller_ = controller; 182 state_controller_ = controller;
236 } 183 }
237 void set_mirroring_controller(SoftwareMirroringController* controller) { 184 void set_mirroring_controller(SoftwareMirroringController* controller) {
238 mirroring_controller_ = controller; 185 mirroring_controller_ = controller;
239 } 186 }
240 187
241 // Replaces |native_display_delegate_| with |delegate| and sets 188 // Replaces |native_display_delegate_| with |delegate| and sets
242 // |configure_display_| to true. Should be called before Init(). 189 // |configure_display_| to true. Should be called before Init().
243 void SetNativeDisplayDelegateForTesting( 190 void SetNativeDisplayDelegateForTesting(
244 scoped_ptr<NativeDisplayDelegate> delegate); 191 scoped_ptr<ui::NativeDisplayDelegate> delegate);
245 192
246 void SetTouchscreenDelegateForTesting( 193 void SetTouchscreenDelegateForTesting(
247 scoped_ptr<TouchscreenDelegate> delegate); 194 scoped_ptr<TouchscreenDelegate> delegate);
248 195
249 // Sets the initial value of |power_state_|. Must be called before Start(). 196 // Sets the initial value of |power_state_|. Must be called before Start().
250 void SetInitialDisplayPower(DisplayPowerState power_state); 197 void SetInitialDisplayPower(DisplayPowerState power_state);
251 198
252 // Initialization, must be called right after constructor. 199 // Initialization, must be called right after constructor.
253 // |is_panel_fitting_enabled| indicates hardware panel fitting support. 200 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
254 void Init(bool is_panel_fitting_enabled); 201 void Init(bool is_panel_fitting_enabled);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 // outputs' |mirror_mode| fields by looking for a mode in |internal_output| 283 // outputs' |mirror_mode| fields by looking for a mode in |internal_output|
337 // and |external_output| having the same resolution. Returns false if a shared 284 // and |external_output| having the same resolution. Returns false if a shared
338 // mode wasn't found or created. 285 // mode wasn't found or created.
339 // 286 //
340 // |try_panel_fitting| allows creating a panel-fitting mode for 287 // |try_panel_fitting| allows creating a panel-fitting mode for
341 // |internal_output| instead of only searching for a matching mode (note that 288 // |internal_output| instead of only searching for a matching mode (note that
342 // it may lead to a crash if |internal_info| is not capable of panel fitting). 289 // it may lead to a crash if |internal_info| is not capable of panel fitting).
343 // 290 //
344 // |preserve_aspect| limits the search/creation only to the modes having the 291 // |preserve_aspect| limits the search/creation only to the modes having the
345 // native aspect ratio of |external_output|. 292 // native aspect ratio of |external_output|.
346 bool FindMirrorMode(OutputSnapshot* internal_output, 293 bool FindMirrorMode(DisplayState* internal_output,
347 OutputSnapshot* external_output, 294 DisplayState* external_output,
348 bool try_panel_fitting, 295 bool try_panel_fitting,
349 bool preserve_aspect); 296 bool preserve_aspect);
350 297
351 // Configures outputs. 298 // Configures outputs.
352 void ConfigureOutputs(); 299 void ConfigureOutputs();
353 300
354 // Notifies observers about an attempted state change. 301 // Notifies observers about an attempted state change.
355 void NotifyObservers(bool success, ui::OutputState attempted_state); 302 void NotifyObservers(bool success, ui::OutputState attempted_state);
356 303
357 // Switches to the state specified in |output_state| and |power_state|. 304 // Switches to the state specified in |output_state| and |power_state|.
(...skipping 10 matching lines...) Expand all
368 // |cached_outputs_| and returns true. 315 // |cached_outputs_| and returns true.
369 bool EnterState(ui::OutputState output_state, DisplayPowerState power_state); 316 bool EnterState(ui::OutputState output_state, DisplayPowerState power_state);
370 317
371 // Returns the output state that should be used with |cached_outputs_| while 318 // Returns the output state that should be used with |cached_outputs_| while
372 // in |power_state|. 319 // in |power_state|.
373 ui::OutputState ChooseOutputState(DisplayPowerState power_state) const; 320 ui::OutputState ChooseOutputState(DisplayPowerState power_state) const;
374 321
375 // Computes the relevant transformation for mirror mode. 322 // Computes the relevant transformation for mirror mode.
376 // |output| is the output on which mirror mode is being applied. 323 // |output| is the output on which mirror mode is being applied.
377 // Returns the transformation or identity if computations fail. 324 // Returns the transformation or identity if computations fail.
378 CoordinateTransformation GetMirrorModeCTM( 325 CoordinateTransformation GetMirrorModeCTM(const DisplayState& output);
379 const OutputConfigurator::OutputSnapshot& output);
380 326
381 // Computes the relevant transformation for extended mode. 327 // Computes the relevant transformation for extended mode. |output| is the
382 // |output| is the output on which extended mode is being applied. 328 // output on which extended mode is being applied. |new_origin| is the
383 // |width| and |height| are the width and height of the combined framebuffer. 329 // position of the output on the framebuffer. |framebuffer_size| is the
330 // size of the combined framebuffer.
384 // Returns the transformation or identity if computations fail. 331 // Returns the transformation or identity if computations fail.
385 CoordinateTransformation GetExtendedModeCTM( 332 CoordinateTransformation GetExtendedModeCTM(
386 const OutputConfigurator::OutputSnapshot& output, 333 const DisplayState& output,
387 int framebuffer_width, 334 const gfx::Point& new_origin,
388 int frame_buffer_height); 335 const gfx::Size& framebuffer_size);
389 336
390 // Returns the ratio between mirrored mode area and native mode area: 337 // Returns the ratio between mirrored mode area and native mode area:
391 // (mirror_mode_width * mirrow_mode_height) / (native_width * native_height) 338 // (mirror_mode_width * mirrow_mode_height) / (native_width * native_height)
392 float GetMirroredDisplayAreaRatio( 339 float GetMirroredDisplayAreaRatio(const DisplayState& output);
393 const OutputConfigurator::OutputSnapshot& output);
394 340
395 // Applies output protections according to requests. 341 // Applies output protections according to requests.
396 bool ApplyProtections(const DisplayProtections& requests); 342 bool ApplyProtections(const DisplayProtections& requests);
397 343
398 StateController* state_controller_; 344 StateController* state_controller_;
399 SoftwareMirroringController* mirroring_controller_; 345 SoftwareMirroringController* mirroring_controller_;
400 scoped_ptr<NativeDisplayDelegate> native_display_delegate_; 346 scoped_ptr<ui::NativeDisplayDelegate> native_display_delegate_;
401 scoped_ptr<TouchscreenDelegate> touchscreen_delegate_; 347 scoped_ptr<TouchscreenDelegate> touchscreen_delegate_;
402 348
403 // Used to enable modes which rely on panel fitting. 349 // Used to enable modes which rely on panel fitting.
404 bool is_panel_fitting_enabled_; 350 bool is_panel_fitting_enabled_;
405 351
406 // Key of the map is the touch display's id, and the value of the map is the 352 // Key of the map is the touch display's id, and the value of the map is the
407 // touch display's area ratio in mirror mode defined as : 353 // touch display's area ratio in mirror mode defined as :
408 // mirror_mode_area / native_mode_area. 354 // mirror_mode_area / native_mode_area.
409 // This is used for scaling touch event's radius when the touch display is in 355 // This is used for scaling touch event's radius when the touch display is in
410 // mirror mode : 356 // mirror mode :
411 // new_touch_radius = sqrt(area_ratio) * old_touch_radius 357 // new_touch_radius = sqrt(area_ratio) * old_touch_radius
412 std::map<int, float> mirrored_display_area_ratio_map_; 358 std::map<int, float> mirrored_display_area_ratio_map_;
413 359
414 // This is detected by the constructor to determine whether or not we should 360 // This is detected by the constructor to determine whether or not we should
415 // be enabled. If we aren't running on ChromeOS, we can't assume that the 361 // be enabled. If we aren't running on ChromeOS, we can't assume that the
416 // Xrandr X11 extension is supported. 362 // Xrandr X11 extension is supported.
417 // If this flag is set to false, any attempts to change the output 363 // If this flag is set to false, any attempts to change the output
418 // configuration to immediately fail without changing the state. 364 // configuration to immediately fail without changing the state.
419 bool configure_display_; 365 bool configure_display_;
420 366
421 // The current display state. 367 // The current display state.
422 ui::OutputState output_state_; 368 ui::OutputState output_state_;
423 369
424 // The current power state. 370 // The current power state.
425 DisplayPowerState power_state_; 371 DisplayPowerState power_state_;
426 372
427 // Most-recently-used output configuration. Note that the actual 373 // Most-recently-used output configuration. Note that the actual
428 // configuration changes asynchronously. 374 // configuration changes asynchronously.
429 std::vector<OutputSnapshot> cached_outputs_; 375 DisplayStateList cached_outputs_;
430 376
431 ObserverList<Observer> observers_; 377 ObserverList<Observer> observers_;
432 378
433 // The timer to delay configuring outputs. See also the comments in 379 // The timer to delay configuring outputs. See also the comments in
434 // Dispatch(). 380 // Dispatch().
435 scoped_ptr<base::OneShotTimer<OutputConfigurator> > configure_timer_; 381 scoped_ptr<base::OneShotTimer<OutputConfigurator> > configure_timer_;
436 382
437 // Id for next output protection client. 383 // Id for next output protection client.
438 OutputProtectionClientId next_output_protection_client_id_; 384 OutputProtectionClientId next_output_protection_client_id_;
439 385
440 // Output protection requests of each client. 386 // Output protection requests of each client.
441 ProtectionRequests client_protection_requests_; 387 ProtectionRequests client_protection_requests_;
442 388
443 DISALLOW_COPY_AND_ASSIGN(OutputConfigurator); 389 DISALLOW_COPY_AND_ASSIGN(OutputConfigurator);
444 }; 390 };
445 391
446 typedef std::vector<OutputConfigurator::OutputSnapshot> OutputSnapshotList;
447
448 } // namespace chromeos 392 } // namespace chromeos
449 393
450 #endif // CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ 394 #endif // CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698