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

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: 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 typedef std::vector<const ui::DisplayMode*> DisplayModeList;
Daniel Erat 2014/03/05 01:56:12 doesn't look like you're using this anywhere in th
dnicoara 2014/03/05 17:29:25 Done. I was using this in some of the ash/ files
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 43
53 struct CoordinateTransformation { 44 struct CoordinateTransformation {
54 // Initialized to the identity transformation. 45 // Initialized to the identity transformation.
55 CoordinateTransformation(); 46 CoordinateTransformation();
56 47
57 float x_scale; 48 float x_scale;
58 float x_offset; 49 float x_offset;
59 float y_scale; 50 float y_scale;
60 float y_offset; 51 float y_offset;
61 }; 52 };
62 53
63 // Information about an output's current state. 54 struct InternalDisplayState {
Daniel Erat 2014/03/05 01:56:12 i think that the word "internal" here may be confu
dnicoara 2014/03/05 17:29:25 Done.
64 struct OutputSnapshot { 55 InternalDisplayState();
65 OutputSnapshot();
66 ~OutputSnapshot();
67 56
68 RROutput output; 57 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 58
102 // XInput device ID or 0 if this output isn't a touchscreen. 59 // XInput device ID or 0 if this output isn't a touchscreen.
103 int touch_device_id; 60 int touch_device_id;
104 61
105 CoordinateTransformation transform; 62 CoordinateTransformation transform;
106 63
107 // Display id for this output. 64 // User-selected mode for the output.
108 int64 display_id; 65 const ui::DisplayMode* selected_mode;
dnicoara 2014/03/05 00:06:34 Since this (and mirror_mode) is OutputConfigurator
109 66
110 bool has_display_id; 67 // Mode used when displaying the same desktop on multiple outputs.
111 68 const ui::DisplayMode* mirror_mode;
112 // This output's index in the array returned by XRandR. Stable even as
113 // outputs are connected or disconnected.
114 int index;
115 }; 69 };
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<InternalDisplayState>& 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 (|width|x|height|) in pixels
Daniel Erat 2014/03/05 01:56:12 nit: update this comment
dnicoara 2014/03/05 17:29:25 Done.
144 // to select output mode for the given display id. 98 // to select output mode for the 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(
169 std::vector<OutputSnapshot>* outputs) = 0; 122 std::vector<InternalDisplayState>* outputs) = 0;
170 123
171 // Configures XInput's Coordinate Transformation Matrix property. 124 // Configures XInput's Coordinate Transformation Matrix property.
172 // |touch_device_id| the ID of the touchscreen device to configure. 125 // |touch_device_id| the ID of the touchscreen device to configure.
173 // |ctm| contains the desired transformation parameters. The offsets 126 // |ctm| contains the desired transformation parameters. The offsets
174 // in it should be normalized so that 1 corresponds to the X or Y axis 127 // in it should be normalized so that 1 corresponds to the X or Y axis
175 // size for the corresponding offset. 128 // size for the corresponding offset.
176 virtual void ConfigureCTM(int touch_device_id, 129 virtual void ConfigureCTM(int touch_device_id,
177 const CoordinateTransformation& ctm) = 0; 130 const CoordinateTransformation& ctm) = 0;
178 }; 131 };
179 132
(...skipping 24 matching lines...) Expand all
204 157
205 // Gap between screens so cursor at bottom of active display doesn't 158 // Gap between screens so cursor at bottom of active display doesn't
206 // partially appear on top of inactive display. Higher numbers guard 159 // partially appear on top of inactive display. Higher numbers guard
207 // against larger cursors, but also waste more memory. 160 // against larger cursors, but also waste more memory.
208 // For simplicity, this is hard-coded to avoid the complexity of always 161 // For simplicity, this is hard-coded to avoid the complexity of always
209 // determining the DPI of the screen and rationalizing which screen we 162 // determining the DPI of the screen and rationalizing which screen we
210 // need to use for the DPI calculation. 163 // need to use for the DPI calculation.
211 // See crbug.com/130188 for initial discussion. 164 // See crbug.com/130188 for initial discussion.
212 static const int kVerticalGap = 60; 165 static const int kVerticalGap = 60;
213 166
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 167 // Returns the mode within |output| that matches the given size with highest
220 // refresh rate. Returns None if no matching output was found. 168 // refresh rate. Returns None if no matching output was found.
221 static RRMode FindOutputModeMatchingSize(const OutputSnapshot& output, 169 static const ui::DisplayMode* FindDisplayModeMatchingSize(
222 int width, 170 const ui::DisplaySnapshot& output,
223 int height); 171 const gfx::Size& size);
224 172
225 OutputConfigurator(); 173 OutputConfigurator();
226 virtual ~OutputConfigurator(); 174 virtual ~OutputConfigurator();
227 175
228 ui::OutputState output_state() const { return output_state_; } 176 ui::OutputState output_state() const { return output_state_; }
229 DisplayPowerState power_state() const { return power_state_; } 177 DisplayPowerState power_state() const { return power_state_; }
230 const std::vector<OutputSnapshot>& cached_outputs() const { 178 const std::vector<InternalDisplayState>& cached_outputs() const {
231 return cached_outputs_; 179 return cached_outputs_;
232 } 180 }
233 181
234 void set_state_controller(StateController* controller) { 182 void set_state_controller(StateController* controller) {
235 state_controller_ = controller; 183 state_controller_ = controller;
236 } 184 }
237 void set_mirroring_controller(SoftwareMirroringController* controller) { 185 void set_mirroring_controller(SoftwareMirroringController* controller) {
238 mirroring_controller_ = controller; 186 mirroring_controller_ = controller;
239 } 187 }
240 188
241 // Replaces |native_display_delegate_| with |delegate| and sets 189 // Replaces |native_display_delegate_| with |delegate| and sets
242 // |configure_display_| to true. Should be called before Init(). 190 // |configure_display_| to true. Should be called before Init().
243 void SetNativeDisplayDelegateForTesting( 191 void SetNativeDisplayDelegateForTesting(
244 scoped_ptr<NativeDisplayDelegate> delegate); 192 scoped_ptr<ui::NativeDisplayDelegate> delegate);
245 193
246 void SetTouchscreenDelegateForTesting( 194 void SetTouchscreenDelegateForTesting(
247 scoped_ptr<TouchscreenDelegate> delegate); 195 scoped_ptr<TouchscreenDelegate> delegate);
248 196
249 // Sets the initial value of |power_state_|. Must be called before Start(). 197 // Sets the initial value of |power_state_|. Must be called before Start().
250 void SetInitialDisplayPower(DisplayPowerState power_state); 198 void SetInitialDisplayPower(DisplayPowerState power_state);
251 199
252 // Initialization, must be called right after constructor. 200 // Initialization, must be called right after constructor.
253 // |is_panel_fitting_enabled| indicates hardware panel fitting support. 201 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
254 void Init(bool is_panel_fitting_enabled); 202 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| 284 // 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 285 // and |external_output| having the same resolution. Returns false if a shared
338 // mode wasn't found or created. 286 // mode wasn't found or created.
339 // 287 //
340 // |try_panel_fitting| allows creating a panel-fitting mode for 288 // |try_panel_fitting| allows creating a panel-fitting mode for
341 // |internal_output| instead of only searching for a matching mode (note that 289 // |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). 290 // it may lead to a crash if |internal_info| is not capable of panel fitting).
343 // 291 //
344 // |preserve_aspect| limits the search/creation only to the modes having the 292 // |preserve_aspect| limits the search/creation only to the modes having the
345 // native aspect ratio of |external_output|. 293 // native aspect ratio of |external_output|.
346 bool FindMirrorMode(OutputSnapshot* internal_output, 294 bool FindMirrorMode(InternalDisplayState* internal_output,
347 OutputSnapshot* external_output, 295 InternalDisplayState* external_output,
348 bool try_panel_fitting, 296 bool try_panel_fitting,
349 bool preserve_aspect); 297 bool preserve_aspect);
350 298
351 // Configures outputs. 299 // Configures outputs.
352 void ConfigureOutputs(); 300 void ConfigureOutputs();
353 301
354 // Notifies observers about an attempted state change. 302 // Notifies observers about an attempted state change.
355 void NotifyObservers(bool success, ui::OutputState attempted_state); 303 void NotifyObservers(bool success, ui::OutputState attempted_state);
356 304
357 // Switches to the state specified in |output_state| and |power_state|. 305 // Switches to the state specified in |output_state| and |power_state|.
(...skipping 10 matching lines...) Expand all
368 // |cached_outputs_| and returns true. 316 // |cached_outputs_| and returns true.
369 bool EnterState(ui::OutputState output_state, DisplayPowerState power_state); 317 bool EnterState(ui::OutputState output_state, DisplayPowerState power_state);
370 318
371 // Returns the output state that should be used with |cached_outputs_| while 319 // Returns the output state that should be used with |cached_outputs_| while
372 // in |power_state|. 320 // in |power_state|.
373 ui::OutputState ChooseOutputState(DisplayPowerState power_state) const; 321 ui::OutputState ChooseOutputState(DisplayPowerState power_state) const;
374 322
375 // Computes the relevant transformation for mirror mode. 323 // Computes the relevant transformation for mirror mode.
376 // |output| is the output on which mirror mode is being applied. 324 // |output| is the output on which mirror mode is being applied.
377 // Returns the transformation or identity if computations fail. 325 // Returns the transformation or identity if computations fail.
378 CoordinateTransformation GetMirrorModeCTM( 326 CoordinateTransformation GetMirrorModeCTM(const InternalDisplayState& output);
379 const OutputConfigurator::OutputSnapshot& output);
380 327
381 // Computes the relevant transformation for extended mode. 328 // Computes the relevant transformation for extended mode.
382 // |output| is the output on which extended mode is being applied. 329 // |output| is the output on which extended mode is being applied.
383 // |width| and |height| are the width and height of the combined framebuffer. 330 // |width| and |height| are the width and height of the combined framebuffer.
Daniel Erat 2014/03/05 01:56:12 nit: update this comment
dnicoara 2014/03/05 17:29:25 Done.
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 InternalDisplayState& 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 InternalDisplayState& 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 std::vector<InternalDisplayState> 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