1 // Copyright 2014 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.
5 #ifndef UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_
6 #define UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_
14 #include "base/event_types.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/timer/timer.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
20 #include "ui/display/display_export.h"
21 #include "ui/display/types/display_constants.h"
22 #include "ui/display/types/native_display_observer.h"
23 #include "ui/gfx/geometry/size.h"
31 struct DisplayConfigureRequest
;
33 class DisplaySnapshot
;
34 class NativeDisplayDelegate
;
35 class UpdateDisplayConfigurationTask
;
37 // This class interacts directly with the system display configurator.
38 class DISPLAY_EXPORT DisplayConfigurator
: public NativeDisplayObserver
{
40 typedef uint64_t ContentProtectionClientId
;
41 static const ContentProtectionClientId kInvalidClientId
= 0;
46 DisplaySnapshot
* display
; // Not owned.
48 // User-selected mode for the display.
49 const DisplayMode
* selected_mode
;
51 // Mode used when displaying the same desktop on multiple displays.
52 const DisplayMode
* mirror_mode
;
55 typedef std::vector
<DisplayState
> DisplayStateList
;
59 virtual ~Observer() {}
61 // Called after the display mode has been changed. |display| contains the
62 // just-applied configuration. Note that the X server is no longer grabbed
63 // when this method is called, so the actual configuration could've changed
65 virtual void OnDisplayModeChanged(
66 const std::vector
<DisplayState
>& displays
) {}
68 // Called after a display mode change attempt failed. |failed_new_state| is
69 // the new state which the system failed to enter.
70 virtual void OnDisplayModeChangeFailed(
71 MultipleDisplayState failed_new_state
) {}
74 // Interface for classes that make decisions about which display state
76 class StateController
{
78 virtual ~StateController() {}
80 // Called when displays are detected.
81 virtual MultipleDisplayState
GetStateForDisplayIds(
82 const std::vector
<int64_t>& display_ids
) const = 0;
84 // Queries the resolution (|size|) in pixels to select display mode for the
86 virtual bool GetResolutionForDisplayId(int64_t display_id
,
87 gfx::Size
* size
) const = 0;
90 // Interface for classes that implement software based mirroring.
91 class SoftwareMirroringController
{
93 virtual ~SoftwareMirroringController() {}
95 // Called when the hardware mirroring failed.
96 virtual void SetSoftwareMirroring(bool enabled
) = 0;
97 virtual bool SoftwareMirroringEnabled() const = 0;
100 class DisplayLayoutManager
{
102 virtual ~DisplayLayoutManager() {}
104 virtual SoftwareMirroringController
* GetSoftwareMirroringController()
107 virtual StateController
* GetStateController() const = 0;
109 // Returns the current display state.
110 virtual MultipleDisplayState
GetDisplayState() const = 0;
112 // Returns the current power state.
113 virtual chromeos::DisplayPowerState
GetPowerState() const = 0;
115 // Parses the |displays| into a list of DisplayStates. This effectively adds
116 // |mirror_mode| and |selected_mode| to the returned results.
117 // TODO(dnicoara): This operation doesn't depend on state and could be done
118 // directly in |GetDisplayLayout()|. Though I need to make sure that there
119 // are no uses for those fields outside DisplayConfigurator.
120 virtual std::vector
<DisplayState
> ParseDisplays(
121 const std::vector
<DisplaySnapshot
*>& displays
) const = 0;
123 // Based on the given |displays|, display state and power state, it will
124 // create display configuration requests which will then be used to
125 // configure the hardware. The requested configuration is stored in
126 // |requests| and |framebuffer_size|.
127 virtual bool GetDisplayLayout(
128 const std::vector
<DisplayState
>& displays
,
129 MultipleDisplayState new_display_state
,
130 chromeos::DisplayPowerState new_power_state
,
131 std::vector
<DisplayConfigureRequest
>* requests
,
132 gfx::Size
* framebuffer_size
) const = 0;
135 // Helper class used by tests.
138 TestApi(DisplayConfigurator
* configurator
) : configurator_(configurator
) {}
141 // If |configure_timer_| is started, stops the timer, runs
142 // ConfigureDisplays(), and returns true; returns false otherwise.
143 bool TriggerConfigureTimeout() WARN_UNUSED_RESULT
;
146 DisplayConfigurator
* configurator_
; // not owned
148 DISALLOW_COPY_AND_ASSIGN(TestApi
);
151 // Flags that can be passed to SetDisplayPower().
152 static const int kSetDisplayPowerNoFlags
;
153 // Configure displays even if the passed-in state matches |power_state_|.
154 static const int kSetDisplayPowerForceProbe
;
155 // Do not change the state if multiple displays are connected or if the
156 // only connected display is external.
157 static const int kSetDisplayPowerOnlyIfSingleInternalDisplay
;
159 // Gap between screens so cursor at bottom of active display doesn't
160 // partially appear on top of inactive display. Higher numbers guard
161 // against larger cursors, but also waste more memory.
162 // For simplicity, this is hard-coded to avoid the complexity of always
163 // determining the DPI of the screen and rationalizing which screen we
164 // need to use for the DPI calculation.
165 // See crbug.com/130188 for initial discussion.
166 static const int kVerticalGap
= 60;
168 // Returns the mode within |display| that matches the given size with highest
169 // refresh rate. Returns None if no matching display was found.
170 static const DisplayMode
* FindDisplayModeMatchingSize(
171 const DisplaySnapshot
& display
,
172 const gfx::Size
& size
);
174 DisplayConfigurator();
175 ~DisplayConfigurator() override
;
177 MultipleDisplayState
display_state() const { return current_display_state_
; }
178 chromeos::DisplayPowerState
requested_power_state() const {
179 return requested_power_state_
;
181 const gfx::Size
framebuffer_size() const { return framebuffer_size_
; }
182 const std::vector
<DisplayState
>& cached_displays() const {
183 return cached_displays_
;
186 // Called when an external process no longer needs to control the display
187 // and Chrome can take control.
190 // Called when an external process needs to control the display and thus
191 // Chrome should relinquish it.
192 void RelinquishControl();
194 void set_state_controller(StateController
* controller
) {
195 state_controller_
= controller
;
197 void set_mirroring_controller(SoftwareMirroringController
* controller
) {
198 mirroring_controller_
= controller
;
201 // Replaces |native_display_delegate_| with the delegate passed in and sets
202 // |configure_display_| to true. Should be called before Init().
203 void SetDelegateForTesting(
204 scoped_ptr
<NativeDisplayDelegate
> display_delegate
);
206 // Sets the initial value of |power_state_|. Must be called before Start().
207 void SetInitialDisplayPower(chromeos::DisplayPowerState power_state
);
209 // Initialization, must be called right after constructor.
210 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
211 void Init(bool is_panel_fitting_enabled
);
213 // Does initial configuration of displays during startup.
214 // If |background_color_argb| is non zero and there are multiple displays,
215 // DisplayConfigurator sets the background color of X's RootWindow to this
217 void ForceInitialConfigure(uint32_t background_color_argb
);
219 // Stop handling display configuration events/requests.
220 void PrepareForExit();
222 // Called when powerd notifies us that some set of displays should be turned
223 // on or off. This requires enabling or disabling the CRTC associated with
224 // the display(s) in question so that the low power state is engaged.
225 // |flags| contains bitwise-or-ed kSetDisplayPower* values. Returns true if
226 // the system successfully enters (or was already in) |power_state|.
227 void SetDisplayPower(chromeos::DisplayPowerState power_state
, int flags
);
229 // Force switching the display mode to |new_state|. Returns false if
230 // switching failed (possibly because |new_state| is invalid for the
231 // current set of connected displays).
232 void SetDisplayMode(MultipleDisplayState new_state
);
234 // NativeDisplayDelegate::Observer overrides:
235 void OnConfigurationChanged() override
;
237 void AddObserver(Observer
* observer
);
238 void RemoveObserver(Observer
* observer
);
240 // Sets all the displays into pre-suspend mode; usually this means
241 // configure them for their resume state. This allows faster resume on
242 // machines where display configuration is slow.
243 void SuspendDisplays();
245 // Reprobes displays to handle changes made while the system was
247 void ResumeDisplays();
249 // Registers a client for display protection and requests a client id. Returns
250 // 0 if requesting failed.
251 ContentProtectionClientId
RegisterContentProtectionClient();
253 // Unregisters the client.
254 void UnregisterContentProtectionClient(ContentProtectionClientId client_id
);
256 // Queries link status and protection status.
257 // |link_mask| is the type of connected display links, which is a bitmask of
258 // DisplayConnectionType values. |protection_mask| is the desired protection
259 // methods, which is a bitmask of the ContentProtectionMethod values.
260 // Returns true on success.
261 bool QueryContentProtectionStatus(ContentProtectionClientId client_id
,
264 uint32_t* protection_mask
);
266 // Requests the desired protection methods.
267 // |protection_mask| is the desired protection methods, which is a bitmask
268 // of the ContentProtectionMethod values.
269 // Returns true when the protection request has been made.
270 bool EnableContentProtection(ContentProtectionClientId client_id
,
272 uint32_t desired_protection_mask
);
274 // Checks the available color profiles for |display_id| and fills the result
276 std::vector
<ui::ColorCalibrationProfile
> GetAvailableColorCalibrationProfiles(
279 // Updates the color calibration to |new_profile|.
280 bool SetColorCalibrationProfile(int64_t display_id
,
281 ui::ColorCalibrationProfile new_profile
);
284 class DisplayLayoutManagerImpl
;
286 // Mapping a display_id to a protection request bitmask.
287 typedef std::map
<int64_t, uint32_t> ContentProtections
;
288 // Mapping a client to its protection request.
289 typedef std::map
<ContentProtectionClientId
, ContentProtections
>
292 // Performs platform specific delegate initialization.
293 scoped_ptr
<NativeDisplayDelegate
> CreatePlatformNativeDisplayDelegate();
295 // Configures displays. Invoked by |configure_timer_|.
296 void ConfigureDisplays();
298 // Restores |requested_power_state_| after the system has resumed,
299 // additionally forcing a probe. Invoked by |configure_timer_|.
300 void RestoreRequestedPowerStateAfterResume();
302 // Notifies observers about an attempted state change.
303 void NotifyObservers(bool success
, MultipleDisplayState attempted_state
);
305 // Returns the display state that should be used with |cached_displays_| while
307 MultipleDisplayState
ChooseDisplayState(
308 chromeos::DisplayPowerState power_state
) const;
310 // Returns the ratio between mirrored mode area and native mode area:
311 // (mirror_mode_width * mirrow_mode_height) / (native_width * native_height)
312 float GetMirroredDisplayAreaRatio(const DisplayState
& display
);
314 // Returns true if in either hardware or software mirroring mode.
315 bool IsMirroring() const;
317 // Applies display protections according to requests.
318 bool ApplyProtections(const ContentProtections
& requests
);
320 // If |configuration_task_| isn't initialized, initializes it and starts the
321 // configuration task.
322 void RunPendingConfiguration();
324 // Callback for |configuration_taks_|. When the configuration process finishes
325 // this is called with the result (|success|) and the updated display state.
326 void OnConfigured(bool success
,
327 const std::vector
<DisplayState
>& displays
,
328 const gfx::Size
& framebuffer_size
,
329 MultipleDisplayState new_display_state
,
330 chromeos::DisplayPowerState new_power_state
);
332 // Helps in identifying if a configuration task needs to be scheduled.
333 // Return true if any of the |requested_*| parameters have been updated. False
335 bool ShouldRunConfigurationTask() const;
337 StateController
* state_controller_
;
338 SoftwareMirroringController
* mirroring_controller_
;
339 scoped_ptr
<NativeDisplayDelegate
> native_display_delegate_
;
341 // Used to enable modes which rely on panel fitting.
342 bool is_panel_fitting_enabled_
;
344 // This is detected by the constructor to determine whether or not we should
345 // be enabled. If we aren't running on ChromeOS, we can't assume that the
346 // Xrandr X11 extension is supported.
347 // If this flag is set to false, any attempts to change the display
348 // configuration to immediately fail without changing the state.
349 bool configure_display_
;
351 // Current configuration state.
352 MultipleDisplayState current_display_state_
;
353 chromeos::DisplayPowerState current_power_state_
;
355 // Pending requests. These values are used when triggering the next display
358 // Stores the user requested state or INVALID if nothing was requested.
359 MultipleDisplayState requested_display_state_
;
361 // Stores the requested power state.
362 chromeos::DisplayPowerState requested_power_state_
;
364 // True if |requested_power_state_| has been changed due to a user request.
365 bool requested_power_state_change_
;
367 // Bitwise-or value of the |kSetDisplayPower*| flags defined above.
368 int requested_power_flags_
;
370 // True if the caller wants to force the display configuration process.
371 bool force_configure_
;
373 // Most-recently-used display configuration. Note that the actual
374 // configuration changes asynchronously.
375 DisplayStateList cached_displays_
;
377 // Most-recently-used framebuffer size.
378 gfx::Size framebuffer_size_
;
380 ObserverList
<Observer
> observers_
;
382 // The timer to delay configuring displays. This is used to aggregate multiple
383 // display configuration events when they are reported in short time spans.
384 // See comment for NativeDisplayEventDispatcherX11 for more details.
385 base::OneShotTimer
<DisplayConfigurator
> configure_timer_
;
387 // Id for next display protection client.
388 ContentProtectionClientId next_display_protection_client_id_
;
390 // Display protection requests of each client.
391 ProtectionRequests client_protection_requests_
;
393 // Display controlled by an external entity.
394 bool display_externally_controlled_
;
396 scoped_ptr
<DisplayLayoutManager
> layout_manager_
;
398 scoped_ptr
<UpdateDisplayConfigurationTask
> configuration_task_
;
400 // This must be the last variable.
401 base::WeakPtrFactory
<DisplayConfigurator
> weak_ptr_factory_
;
403 DISALLOW_COPY_AND_ASSIGN(DisplayConfigurator
);
408 #endif // UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_