| 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 #ifndef CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ | |
| 6 #define CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/event_types.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "base/timer/timer.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" | |
| 20 #include "ui/display/display_constants.h" | |
| 21 | |
| 22 // Forward declarations for Xlib and Xrandr. | |
| 23 // This is so unused X definitions don't pollute the namespace. | |
| 24 typedef unsigned long XID; | |
| 25 typedef XID RROutput; | |
| 26 typedef XID RRCrtc; | |
| 27 typedef XID RRMode; | |
| 28 | |
| 29 namespace chromeos { | |
| 30 | |
| 31 class NativeDisplayDelegate; | |
| 32 | |
| 33 // This class interacts directly with the underlying Xrandr API to manipulate | |
| 34 // CTRCs and Outputs. | |
| 35 class CHROMEOS_EXPORT OutputConfigurator | |
| 36 : public NativeDisplayObserver { | |
| 37 public: | |
| 38 typedef uint64_t OutputProtectionClientId; | |
| 39 static const OutputProtectionClientId kInvalidClientId = 0; | |
| 40 | |
| 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 { | |
| 54 // Initialized to the identity transformation. | |
| 55 CoordinateTransformation(); | |
| 56 | |
| 57 float x_scale; | |
| 58 float x_offset; | |
| 59 float y_scale; | |
| 60 float y_offset; | |
| 61 }; | |
| 62 | |
| 63 // Information about an output's current state. | |
| 64 struct OutputSnapshot { | |
| 65 OutputSnapshot(); | |
| 66 ~OutputSnapshot(); | |
| 67 | |
| 68 RROutput output; | |
| 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 | |
| 102 // XInput device ID or 0 if this output isn't a touchscreen. | |
| 103 int touch_device_id; | |
| 104 | |
| 105 CoordinateTransformation transform; | |
| 106 | |
| 107 // Display id for this output. | |
| 108 int64 display_id; | |
| 109 | |
| 110 bool has_display_id; | |
| 111 | |
| 112 // This output's index in the array returned by XRandR. Stable even as | |
| 113 // outputs are connected or disconnected. | |
| 114 int index; | |
| 115 }; | |
| 116 | |
| 117 class Observer { | |
| 118 public: | |
| 119 virtual ~Observer() {} | |
| 120 | |
| 121 // Called after the display mode has been changed. |output| contains the | |
| 122 // 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 | |
| 124 // already. | |
| 125 virtual void OnDisplayModeChanged( | |
| 126 const std::vector<OutputSnapshot>& outputs) {} | |
| 127 | |
| 128 // Called after a display mode change attempt failed. |failed_new_state| is | |
| 129 // the new state which the system failed to enter. | |
| 130 virtual void OnDisplayModeChangeFailed(ui::OutputState failed_new_state) {} | |
| 131 }; | |
| 132 | |
| 133 // Interface for classes that make decisions about which output state | |
| 134 // should be used. | |
| 135 class StateController { | |
| 136 public: | |
| 137 virtual ~StateController() {} | |
| 138 | |
| 139 // Called when displays are detected. | |
| 140 virtual ui::OutputState GetStateForDisplayIds( | |
| 141 const std::vector<int64>& display_ids) const = 0; | |
| 142 | |
| 143 // Queries the resolution (|width|x|height|) in pixels | |
| 144 // to select output mode for the given display id. | |
| 145 virtual bool GetResolutionForDisplayId(int64 display_id, | |
| 146 int* width, | |
| 147 int* height) const = 0; | |
| 148 }; | |
| 149 | |
| 150 // Interface for classes that implement software based mirroring. | |
| 151 class SoftwareMirroringController { | |
| 152 public: | |
| 153 virtual ~SoftwareMirroringController() {} | |
| 154 | |
| 155 // Called when the hardware mirroring failed. | |
| 156 virtual void SetSoftwareMirroring(bool enabled) = 0; | |
| 157 }; | |
| 158 | |
| 159 class TouchscreenDelegate { | |
| 160 public: | |
| 161 virtual ~TouchscreenDelegate() {} | |
| 162 | |
| 163 // Searches for touchscreens among input devices, | |
| 164 // and tries to match them up to screens in |outputs|. | |
| 165 // |outputs| is an array of detected screens. | |
| 166 // If a touchscreen with same resolution as an output's native mode | |
| 167 // is detected, its id will be stored in this output. | |
| 168 virtual void AssociateTouchscreens( | |
| 169 std::vector<OutputSnapshot>* outputs) = 0; | |
| 170 | |
| 171 // Configures XInput's Coordinate Transformation Matrix property. | |
| 172 // |touch_device_id| the ID of the touchscreen device to configure. | |
| 173 // |ctm| contains the desired transformation parameters. The offsets | |
| 174 // in it should be normalized so that 1 corresponds to the X or Y axis | |
| 175 // size for the corresponding offset. | |
| 176 virtual void ConfigureCTM(int touch_device_id, | |
| 177 const CoordinateTransformation& ctm) = 0; | |
| 178 }; | |
| 179 | |
| 180 // Helper class used by tests. | |
| 181 class TestApi { | |
| 182 public: | |
| 183 TestApi(OutputConfigurator* configurator) | |
| 184 : configurator_(configurator) {} | |
| 185 ~TestApi() {} | |
| 186 | |
| 187 // If |configure_timer_| is started, stops the timer, runs | |
| 188 // ConfigureOutputs(), and returns true; returns false otherwise. | |
| 189 bool TriggerConfigureTimeout(); | |
| 190 | |
| 191 private: | |
| 192 OutputConfigurator* configurator_; // not owned | |
| 193 | |
| 194 DISALLOW_COPY_AND_ASSIGN(TestApi); | |
| 195 }; | |
| 196 | |
| 197 // Flags that can be passed to SetDisplayPower(). | |
| 198 static const int kSetDisplayPowerNoFlags = 0; | |
| 199 // Configure displays even if the passed-in state matches |power_state_|. | |
| 200 static const int kSetDisplayPowerForceProbe = 1 << 0; | |
| 201 // Do not change the state if multiple displays are connected or if the | |
| 202 // only connected display is external. | |
| 203 static const int kSetDisplayPowerOnlyIfSingleInternalDisplay = 1 << 1; | |
| 204 | |
| 205 // Gap between screens so cursor at bottom of active display doesn't | |
| 206 // partially appear on top of inactive display. Higher numbers guard | |
| 207 // against larger cursors, but also waste more memory. | |
| 208 // For simplicity, this is hard-coded to avoid the complexity of always | |
| 209 // determining the DPI of the screen and rationalizing which screen we | |
| 210 // need to use for the DPI calculation. | |
| 211 // See crbug.com/130188 for initial discussion. | |
| 212 static const int kVerticalGap = 60; | |
| 213 | |
| 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 | |
| 220 // refresh rate. Returns None if no matching output was found. | |
| 221 static RRMode FindOutputModeMatchingSize(const OutputSnapshot& output, | |
| 222 int width, | |
| 223 int height); | |
| 224 | |
| 225 OutputConfigurator(); | |
| 226 virtual ~OutputConfigurator(); | |
| 227 | |
| 228 ui::OutputState output_state() const { return output_state_; } | |
| 229 DisplayPowerState power_state() const { return power_state_; } | |
| 230 const std::vector<OutputSnapshot>& cached_outputs() const { | |
| 231 return cached_outputs_; | |
| 232 } | |
| 233 | |
| 234 void set_state_controller(StateController* controller) { | |
| 235 state_controller_ = controller; | |
| 236 } | |
| 237 void set_mirroring_controller(SoftwareMirroringController* controller) { | |
| 238 mirroring_controller_ = controller; | |
| 239 } | |
| 240 | |
| 241 // Replaces |native_display_delegate_| with |delegate| and sets | |
| 242 // |configure_display_| to true. Should be called before Init(). | |
| 243 void SetNativeDisplayDelegateForTesting( | |
| 244 scoped_ptr<NativeDisplayDelegate> delegate); | |
| 245 | |
| 246 void SetTouchscreenDelegateForTesting( | |
| 247 scoped_ptr<TouchscreenDelegate> delegate); | |
| 248 | |
| 249 // Sets the initial value of |power_state_|. Must be called before Start(). | |
| 250 void SetInitialDisplayPower(DisplayPowerState power_state); | |
| 251 | |
| 252 // Initialization, must be called right after constructor. | |
| 253 // |is_panel_fitting_enabled| indicates hardware panel fitting support. | |
| 254 void Init(bool is_panel_fitting_enabled); | |
| 255 | |
| 256 // Does initial configuration of displays during startup. | |
| 257 // If |background_color_argb| is non zero and there are multiple displays, | |
| 258 // OutputConfigurator sets the background color of X's RootWindow to this | |
| 259 // color. | |
| 260 void ForceInitialConfigure(uint32 background_color_argb); | |
| 261 | |
| 262 // Stop handling display configuration events/requests. | |
| 263 void PrepareForExit(); | |
| 264 | |
| 265 // Called when powerd notifies us that some set of displays should be turned | |
| 266 // on or off. This requires enabling or disabling the CRTC associated with | |
| 267 // the display(s) in question so that the low power state is engaged. | |
| 268 // |flags| contains bitwise-or-ed kSetDisplayPower* values. | |
| 269 bool SetDisplayPower(DisplayPowerState power_state, int flags); | |
| 270 | |
| 271 // Force switching the display mode to |new_state|. Returns false if | |
| 272 // switching failed (possibly because |new_state| is invalid for the | |
| 273 // current set of connected outputs). | |
| 274 bool SetDisplayMode(ui::OutputState new_state); | |
| 275 | |
| 276 // NativeDisplayDelegate::Observer overrides: | |
| 277 virtual void OnConfigurationChanged() OVERRIDE; | |
| 278 | |
| 279 void AddObserver(Observer* observer); | |
| 280 void RemoveObserver(Observer* observer); | |
| 281 | |
| 282 // Sets all the displays into pre-suspend mode; usually this means | |
| 283 // configure them for their resume state. This allows faster resume on | |
| 284 // machines where display configuration is slow. | |
| 285 void SuspendDisplays(); | |
| 286 | |
| 287 // Reprobes displays to handle changes made while the system was | |
| 288 // suspended. | |
| 289 void ResumeDisplays(); | |
| 290 | |
| 291 const std::map<int, float>& GetMirroredDisplayAreaRatioMap() { | |
| 292 return mirrored_display_area_ratio_map_; | |
| 293 } | |
| 294 | |
| 295 // Registers a client for output protection and requests a client id. Returns | |
| 296 // 0 if requesting failed. | |
| 297 OutputProtectionClientId RegisterOutputProtectionClient(); | |
| 298 | |
| 299 // Unregisters the client. | |
| 300 void UnregisterOutputProtectionClient(OutputProtectionClientId client_id); | |
| 301 | |
| 302 // Queries link status and protection status. | |
| 303 // |link_mask| is the type of connected output links, which is a bitmask of | |
| 304 // OutputType values. |protection_mask| is the desired protection methods, | |
| 305 // which is a bitmask of the OutputProtectionMethod values. | |
| 306 // Returns true on success. | |
| 307 bool QueryOutputProtectionStatus( | |
| 308 OutputProtectionClientId client_id, | |
| 309 int64 display_id, | |
| 310 uint32_t* link_mask, | |
| 311 uint32_t* protection_mask); | |
| 312 | |
| 313 // Requests the desired protection methods. | |
| 314 // |protection_mask| is the desired protection methods, which is a bitmask | |
| 315 // of the OutputProtectionMethod values. | |
| 316 // Returns true when the protection request has been made. | |
| 317 bool EnableOutputProtection( | |
| 318 OutputProtectionClientId client_id, | |
| 319 int64 display_id, | |
| 320 uint32_t desired_protection_mask); | |
| 321 | |
| 322 private: | |
| 323 // Mapping a display_id to a protection request bitmask. | |
| 324 typedef std::map<int64, uint32_t> DisplayProtections; | |
| 325 // Mapping a client to its protection request. | |
| 326 typedef std::map<OutputProtectionClientId, | |
| 327 DisplayProtections> ProtectionRequests; | |
| 328 | |
| 329 // Updates |cached_outputs_| to contain currently-connected outputs. Calls | |
| 330 // |delegate_->GetOutputs()| and then does additional work, like finding the | |
| 331 // mirror mode and setting user-preferred modes. Note that the server must be | |
| 332 // grabbed via |delegate_->GrabServer()| first. | |
| 333 void UpdateCachedOutputs(); | |
| 334 | |
| 335 // Helper method for UpdateCachedOutputs() that initializes the passed-in | |
| 336 // 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 | |
| 338 // mode wasn't found or created. | |
| 339 // | |
| 340 // |try_panel_fitting| allows creating a panel-fitting mode for | |
| 341 // |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). | |
| 343 // | |
| 344 // |preserve_aspect| limits the search/creation only to the modes having the | |
| 345 // native aspect ratio of |external_output|. | |
| 346 bool FindMirrorMode(OutputSnapshot* internal_output, | |
| 347 OutputSnapshot* external_output, | |
| 348 bool try_panel_fitting, | |
| 349 bool preserve_aspect); | |
| 350 | |
| 351 // Configures outputs. | |
| 352 void ConfigureOutputs(); | |
| 353 | |
| 354 // Notifies observers about an attempted state change. | |
| 355 void NotifyObservers(bool success, ui::OutputState attempted_state); | |
| 356 | |
| 357 // Switches to the state specified in |output_state| and |power_state|. | |
| 358 // If the hardware mirroring failed and |mirroring_controller_| is set, | |
| 359 // it switches to |STATE_DUAL_EXTENDED| and calls |SetSoftwareMirroring()| | |
| 360 // to enable software based mirroring. | |
| 361 // On success, updates |output_state_|, |power_state_|, and |cached_outputs_| | |
| 362 // and returns true. | |
| 363 bool EnterStateOrFallBackToSoftwareMirroring(ui::OutputState output_state, | |
| 364 DisplayPowerState power_state); | |
| 365 | |
| 366 // Switches to the state specified in |output_state| and |power_state|. | |
| 367 // On success, updates |output_state_|, |power_state_|, and | |
| 368 // |cached_outputs_| and returns true. | |
| 369 bool EnterState(ui::OutputState output_state, DisplayPowerState power_state); | |
| 370 | |
| 371 // Returns the output state that should be used with |cached_outputs_| while | |
| 372 // in |power_state|. | |
| 373 ui::OutputState ChooseOutputState(DisplayPowerState power_state) const; | |
| 374 | |
| 375 // Computes the relevant transformation for mirror mode. | |
| 376 // |output| is the output on which mirror mode is being applied. | |
| 377 // Returns the transformation or identity if computations fail. | |
| 378 CoordinateTransformation GetMirrorModeCTM( | |
| 379 const OutputConfigurator::OutputSnapshot& output); | |
| 380 | |
| 381 // Computes the relevant transformation for extended mode. | |
| 382 // |output| is the output on which extended mode is being applied. | |
| 383 // |width| and |height| are the width and height of the combined framebuffer. | |
| 384 // Returns the transformation or identity if computations fail. | |
| 385 CoordinateTransformation GetExtendedModeCTM( | |
| 386 const OutputConfigurator::OutputSnapshot& output, | |
| 387 int framebuffer_width, | |
| 388 int frame_buffer_height); | |
| 389 | |
| 390 // Returns the ratio between mirrored mode area and native mode area: | |
| 391 // (mirror_mode_width * mirrow_mode_height) / (native_width * native_height) | |
| 392 float GetMirroredDisplayAreaRatio( | |
| 393 const OutputConfigurator::OutputSnapshot& output); | |
| 394 | |
| 395 // Applies output protections according to requests. | |
| 396 bool ApplyProtections(const DisplayProtections& requests); | |
| 397 | |
| 398 StateController* state_controller_; | |
| 399 SoftwareMirroringController* mirroring_controller_; | |
| 400 scoped_ptr<NativeDisplayDelegate> native_display_delegate_; | |
| 401 scoped_ptr<TouchscreenDelegate> touchscreen_delegate_; | |
| 402 | |
| 403 // Used to enable modes which rely on panel fitting. | |
| 404 bool is_panel_fitting_enabled_; | |
| 405 | |
| 406 // 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 : | |
| 408 // mirror_mode_area / native_mode_area. | |
| 409 // This is used for scaling touch event's radius when the touch display is in | |
| 410 // mirror mode : | |
| 411 // new_touch_radius = sqrt(area_ratio) * old_touch_radius | |
| 412 std::map<int, float> mirrored_display_area_ratio_map_; | |
| 413 | |
| 414 // 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 | |
| 416 // Xrandr X11 extension is supported. | |
| 417 // If this flag is set to false, any attempts to change the output | |
| 418 // configuration to immediately fail without changing the state. | |
| 419 bool configure_display_; | |
| 420 | |
| 421 // The current display state. | |
| 422 ui::OutputState output_state_; | |
| 423 | |
| 424 // The current power state. | |
| 425 DisplayPowerState power_state_; | |
| 426 | |
| 427 // Most-recently-used output configuration. Note that the actual | |
| 428 // configuration changes asynchronously. | |
| 429 std::vector<OutputSnapshot> cached_outputs_; | |
| 430 | |
| 431 ObserverList<Observer> observers_; | |
| 432 | |
| 433 // The timer to delay configuring outputs. See also the comments in | |
| 434 // Dispatch(). | |
| 435 scoped_ptr<base::OneShotTimer<OutputConfigurator> > configure_timer_; | |
| 436 | |
| 437 // Id for next output protection client. | |
| 438 OutputProtectionClientId next_output_protection_client_id_; | |
| 439 | |
| 440 // Output protection requests of each client. | |
| 441 ProtectionRequests client_protection_requests_; | |
| 442 | |
| 443 DISALLOW_COPY_AND_ASSIGN(OutputConfigurator); | |
| 444 }; | |
| 445 | |
| 446 typedef std::vector<OutputConfigurator::OutputSnapshot> OutputSnapshotList; | |
| 447 | |
| 448 } // namespace chromeos | |
| 449 | |
| 450 #endif // CHROMEOS_DISPLAY_OUTPUT_CONFIGURATOR_H_ | |
| OLD | NEW |