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_
15 #include "base/event_types.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/timer/timer.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21 #include "ui/display/chromeos/query_content_protection_task.h"
22 #include "ui/display/display_export.h"
23 #include "ui/display/types/display_constants.h"
24 #include "ui/display/types/native_display_observer.h"
25 #include "ui/gfx/geometry/size.h"
33 class DisplayLayoutManager
;
35 class DisplaySnapshot
;
36 class NativeDisplayDelegate
;
37 class UpdateDisplayConfigurationTask
;
39 // This class interacts directly with the system display configurator.
40 class DISPLAY_EXPORT DisplayConfigurator
: public NativeDisplayObserver
{
42 typedef uint64_t ContentProtectionClientId
;
43 static const ContentProtectionClientId kInvalidClientId
= 0;
45 typedef base::Callback
<void(bool)> ConfigurationCallback
;
47 typedef base::Callback
<void(bool /* success */)> EnableProtectionCallback
;
49 struct QueryProtectionResponse
{
50 // True if the query succeeded, false otherwise.
53 // The type of connected display links, which is a bitmask of
54 // DisplayConnectionType values.
55 uint32_t link_mask
= 0;
57 // The desired protection methods, which is a bitmask of the
58 // ContentProtectionMethod values.
59 uint32_t protection_mask
= 0;
62 typedef base::Callback
<void(const QueryProtectionResponse
&)>
63 QueryProtectionCallback
;
65 typedef std::vector
<DisplaySnapshot
*> DisplayStateList
;
67 // Mapping a display_id to a protection request bitmask.
68 typedef std::map
<int64_t, uint32_t> ContentProtections
;
72 virtual ~Observer() {}
74 // Called after the display mode has been changed. |display| contains the
75 // just-applied configuration. Note that the X server is no longer grabbed
76 // when this method is called, so the actual configuration could've changed
78 virtual void OnDisplayModeChanged(
79 const DisplayStateList
& displays
) {}
81 // Called after a display mode change attempt failed. |displays| contains
82 // displays that are detected when failed.
83 // |failed_new_state| is the new state which the system failed to enter.
84 virtual void OnDisplayModeChangeFailed(
85 const DisplayStateList
& displays
,
86 MultipleDisplayState failed_new_state
) {}
89 // Interface for classes that make decisions about which display state
91 class StateController
{
93 virtual ~StateController() {}
95 // Called when displays are detected.
96 virtual MultipleDisplayState
GetStateForDisplayIds(
97 const std::vector
<int64_t>& display_ids
) const = 0;
99 // Queries the resolution (|size|) in pixels to select display mode for the
101 virtual bool GetResolutionForDisplayId(int64_t display_id
,
102 gfx::Size
* size
) const = 0;
105 // Interface for classes that implement software based mirroring.
106 class SoftwareMirroringController
{
108 virtual ~SoftwareMirroringController() {}
110 // Called when the hardware mirroring failed.
111 virtual void SetSoftwareMirroring(bool enabled
) = 0;
112 virtual bool SoftwareMirroringEnabled() const = 0;
115 // Helper class used by tests.
118 TestApi(DisplayConfigurator
* configurator
) : configurator_(configurator
) {}
121 // If |configure_timer_| is started, stops the timer, runs
122 // ConfigureDisplays(), and returns true; returns false otherwise.
123 bool TriggerConfigureTimeout() WARN_UNUSED_RESULT
;
126 DisplayConfigurator
* configurator_
; // not owned
128 DISALLOW_COPY_AND_ASSIGN(TestApi
);
131 // Flags that can be passed to SetDisplayPower().
132 static const int kSetDisplayPowerNoFlags
;
133 // Configure displays even if the passed-in state matches |power_state_|.
134 static const int kSetDisplayPowerForceProbe
;
135 // Do not change the state if multiple displays are connected or if the
136 // only connected display is external.
137 static const int kSetDisplayPowerOnlyIfSingleInternalDisplay
;
139 // Gap between screens so cursor at bottom of active display doesn't
140 // partially appear on top of inactive display. Higher numbers guard
141 // against larger cursors, but also waste more memory.
142 // For simplicity, this is hard-coded to avoid the complexity of always
143 // determining the DPI of the screen and rationalizing which screen we
144 // need to use for the DPI calculation.
145 // See crbug.com/130188 for initial discussion.
146 static const int kVerticalGap
= 60;
148 // Returns the mode within |display| that matches the given size with highest
149 // refresh rate. Returns None if no matching display was found.
150 static const DisplayMode
* FindDisplayModeMatchingSize(
151 const DisplaySnapshot
& display
,
152 const gfx::Size
& size
);
154 DisplayConfigurator();
155 ~DisplayConfigurator() override
;
157 MultipleDisplayState
display_state() const { return current_display_state_
; }
158 chromeos::DisplayPowerState
requested_power_state() const {
159 return requested_power_state_
;
161 const gfx::Size
framebuffer_size() const { return framebuffer_size_
; }
162 const std::vector
<DisplaySnapshot
*>& cached_displays() const {
163 return cached_displays_
;
166 // Called when an external process no longer needs to control the display
167 // and Chrome can take control.
170 // Called when an external process needs to control the display and thus
171 // Chrome should relinquish it.
172 void RelinquishControl();
174 void set_state_controller(StateController
* controller
) {
175 state_controller_
= controller
;
177 void set_mirroring_controller(SoftwareMirroringController
* controller
) {
178 mirroring_controller_
= controller
;
181 // Replaces |native_display_delegate_| with the delegate passed in and sets
182 // |configure_display_| to true. Should be called before Init().
183 void SetDelegateForTesting(
184 scoped_ptr
<NativeDisplayDelegate
> display_delegate
);
186 // Sets the initial value of |power_state_|. Must be called before Start().
187 void SetInitialDisplayPower(chromeos::DisplayPowerState power_state
);
189 // Initialization, must be called right after constructor.
190 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
191 void Init(bool is_panel_fitting_enabled
);
193 // Does initial configuration of displays during startup.
194 // If |background_color_argb| is non zero and there are multiple displays,
195 // DisplayConfigurator sets the background color of X's RootWindow to this
197 void ForceInitialConfigure(uint32_t background_color_argb
);
199 // Stop handling display configuration events/requests.
200 void PrepareForExit();
202 // Called when powerd notifies us that some set of displays should be turned
203 // on or off. This requires enabling or disabling the CRTC associated with
204 // the display(s) in question so that the low power state is engaged.
205 // |flags| contains bitwise-or-ed kSetDisplayPower* values. After the
206 // configuration finishes |callback| is called with the status of the
208 void SetDisplayPower(chromeos::DisplayPowerState power_state
,
210 const ConfigurationCallback
& callback
);
212 // Force switching the display mode to |new_state|. Returns false if
213 // switching failed (possibly because |new_state| is invalid for the
214 // current set of connected displays).
215 void SetDisplayMode(MultipleDisplayState new_state
);
217 // NativeDisplayDelegate::Observer overrides:
218 void OnConfigurationChanged() override
;
220 void AddObserver(Observer
* observer
);
221 void RemoveObserver(Observer
* observer
);
223 // Sets all the displays into pre-suspend mode; usually this means
224 // configure them for their resume state. This allows faster resume on
225 // machines where display configuration is slow.
226 void SuspendDisplays();
228 // Reprobes displays to handle changes made while the system was
230 void ResumeDisplays();
232 // Registers a client for display protection and requests a client id. Returns
233 // 0 if requesting failed.
234 ContentProtectionClientId
RegisterContentProtectionClient();
236 // Unregisters the client.
237 void UnregisterContentProtectionClient(ContentProtectionClientId client_id
);
239 // Queries link status and protection status. |callback| is used to respond
241 void QueryContentProtectionStatus(ContentProtectionClientId client_id
,
243 const QueryProtectionCallback
& callback
);
245 // Requests the desired protection methods.
246 // |protection_mask| is the desired protection methods, which is a bitmask
247 // of the ContentProtectionMethod values.
248 // Returns true when the protection request has been made.
249 void EnableContentProtection(ContentProtectionClientId client_id
,
251 uint32_t protection_mask
,
252 const EnableProtectionCallback
& callback
);
254 // Checks the available color profiles for |display_id| and fills the result
256 std::vector
<ui::ColorCalibrationProfile
> GetAvailableColorCalibrationProfiles(
259 // Updates the color calibration to |new_profile|.
260 bool SetColorCalibrationProfile(int64_t display_id
,
261 ui::ColorCalibrationProfile new_profile
);
264 class DisplayLayoutManagerImpl
;
266 // Mapping a client to its protection request.
267 typedef std::map
<ContentProtectionClientId
, ContentProtections
>
270 // Performs platform specific delegate initialization.
271 scoped_ptr
<NativeDisplayDelegate
> CreatePlatformNativeDisplayDelegate();
273 // Configures displays. Invoked by |configure_timer_|.
274 void ConfigureDisplays();
276 // Restores |requested_power_state_| after the system has resumed,
277 // additionally forcing a probe. Invoked by |configure_timer_|.
278 void RestoreRequestedPowerStateAfterResume();
280 // Notifies observers about an attempted state change.
281 void NotifyObservers(bool success
, MultipleDisplayState attempted_state
);
283 // Returns the display state that should be used with |cached_displays_| while
285 MultipleDisplayState
ChooseDisplayState(
286 chromeos::DisplayPowerState power_state
) const;
288 // Applies display protections according to requests.
289 bool ApplyProtections(const ContentProtections
& requests
);
291 // If |configuration_task_| isn't initialized, initializes it and starts the
292 // configuration task.
293 void RunPendingConfiguration();
295 // Callback for |configuration_taks_|. When the configuration process finishes
296 // this is called with the result (|success|) and the updated display state.
297 void OnConfigured(bool success
,
298 const std::vector
<DisplaySnapshot
*>& displays
,
299 const gfx::Size
& framebuffer_size
,
300 MultipleDisplayState new_display_state
,
301 chromeos::DisplayPowerState new_power_state
);
303 // Helps in identifying if a configuration task needs to be scheduled.
304 // Return true if any of the |requested_*| parameters have been updated. False
306 bool ShouldRunConfigurationTask() const;
308 // Helper functions which will call the callbacks in
309 // |in_progress_configuration_callbacks_| and
310 // |queued_configuration_callbacks_| and clear the lists after. |success| is
311 // the configuration status used when calling the callbacks.
312 void CallAndClearInProgressCallbacks(bool success
);
313 void CallAndClearQueuedCallbacks(bool success
);
315 // Content protection callbacks called by the tasks when they finish. These
316 // are responsible for destroying the task, replying to the caller that made
317 // the task and starting the a new content protection task if one is queued.
318 void OnContentProtectionQueried(
319 ContentProtectionClientId client_id
,
321 QueryContentProtectionTask::Response response
);
322 void OnContentProtectionEnabled(ContentProtectionClientId client_id
,
324 uint32_t desired_method_mask
,
326 void OnContentProtectionClientUnregistered(bool success
);
328 StateController
* state_controller_
;
329 SoftwareMirroringController
* mirroring_controller_
;
330 scoped_ptr
<NativeDisplayDelegate
> native_display_delegate_
;
332 // Used to enable modes which rely on panel fitting.
333 bool is_panel_fitting_enabled_
;
335 // This is detected by the constructor to determine whether or not we should
336 // be enabled. If we aren't running on Chrome OS, we can't assume that the
337 // Xrandr X11 extension or the Ozone underlying display hotplug system are
339 // If this flag is set to false, any attempts to change the display
340 // configuration to immediately fail without changing the state.
341 bool configure_display_
;
343 // Current configuration state.
344 MultipleDisplayState current_display_state_
;
345 chromeos::DisplayPowerState current_power_state_
;
347 // Pending requests. These values are used when triggering the next display
350 // Stores the user requested state or INVALID if nothing was requested.
351 MultipleDisplayState requested_display_state_
;
353 // Stores the requested power state.
354 chromeos::DisplayPowerState requested_power_state_
;
356 // True if |requested_power_state_| has been changed due to a user request.
357 bool requested_power_state_change_
;
359 // Bitwise-or value of the |kSetDisplayPower*| flags defined above.
360 int requested_power_flags_
;
362 // List of callbacks from callers waiting for the display configuration to
363 // start/finish. Note these callbacks belong to the pending request, not a
364 // request currently active.
365 std::vector
<ConfigurationCallback
> queued_configuration_callbacks_
;
367 // List of callbacks belonging to the currently running display configuration
369 std::vector
<ConfigurationCallback
> in_progress_configuration_callbacks_
;
371 std::queue
<base::Closure
> content_protection_tasks_
;
372 std::queue
<QueryProtectionCallback
> query_protection_callbacks_
;
373 std::queue
<EnableProtectionCallback
> enable_protection_callbacks_
;
375 // True if the caller wants to force the display configuration process.
376 bool force_configure_
;
378 // Most-recently-used display configuration. Note that the actual
379 // configuration changes asynchronously.
380 DisplayStateList cached_displays_
;
382 // Most-recently-used framebuffer size.
383 gfx::Size framebuffer_size_
;
385 ObserverList
<Observer
> observers_
;
387 // The timer to delay configuring displays. This is used to aggregate multiple
388 // display configuration events when they are reported in short time spans.
389 // See comment for NativeDisplayEventDispatcherX11 for more details.
390 base::OneShotTimer
<DisplayConfigurator
> configure_timer_
;
392 // Id for next display protection client.
393 ContentProtectionClientId next_display_protection_client_id_
;
395 // Display protection requests of each client.
396 ProtectionRequests client_protection_requests_
;
398 // Display controlled by an external entity.
399 bool display_externally_controlled_
;
401 // Whether the displays are currently suspended.
402 bool displays_suspended_
;
404 scoped_ptr
<DisplayLayoutManager
> layout_manager_
;
406 scoped_ptr
<UpdateDisplayConfigurationTask
> configuration_task_
;
408 // This must be the last variable.
409 base::WeakPtrFactory
<DisplayConfigurator
> weak_ptr_factory_
;
411 DISALLOW_COPY_AND_ASSIGN(DisplayConfigurator
);
416 #endif // UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_