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 struct DisplayConfigureRequest
;
34 struct GammaRampRGBEntry
;
35 class DisplayLayoutManager
;
37 class DisplaySnapshot
;
38 class NativeDisplayDelegate
;
39 class UpdateDisplayConfigurationTask
;
41 // This class interacts directly with the system display configurator.
42 class DISPLAY_EXPORT DisplayConfigurator
: public NativeDisplayObserver
{
44 typedef uint64_t ContentProtectionClientId
;
45 static const ContentProtectionClientId kInvalidClientId
= 0;
47 typedef base::Callback
<void(bool)> ConfigurationCallback
;
49 typedef base::Callback
<void(bool /* success */)> EnableProtectionCallback
;
51 struct QueryProtectionResponse
{
52 // True if the query succeeded, false otherwise.
55 // The type of connected display links, which is a bitmask of
56 // DisplayConnectionType values.
57 uint32_t link_mask
= 0;
59 // The desired protection methods, which is a bitmask of the
60 // ContentProtectionMethod values.
61 uint32_t protection_mask
= 0;
64 typedef base::Callback
<void(const QueryProtectionResponse
&)>
65 QueryProtectionCallback
;
67 typedef std::vector
<DisplaySnapshot
*> DisplayStateList
;
69 // Mapping a display_id to a protection request bitmask.
70 typedef std::map
<int64_t, uint32_t> ContentProtections
;
74 virtual ~Observer() {}
76 // Called after the display mode has been changed. |display| contains the
77 // just-applied configuration. Note that the X server is no longer grabbed
78 // when this method is called, so the actual configuration could've changed
80 virtual void OnDisplayModeChanged(
81 const DisplayStateList
& displays
) {}
83 // Called after a display mode change attempt failed. |displays| contains
84 // displays that are detected when failed.
85 // |failed_new_state| is the new state which the system failed to enter.
86 virtual void OnDisplayModeChangeFailed(
87 const DisplayStateList
& displays
,
88 MultipleDisplayState failed_new_state
) {}
91 // Interface for classes that make decisions about which display state
93 class StateController
{
95 virtual ~StateController() {}
97 // Called when displays are detected.
98 virtual MultipleDisplayState
GetStateForDisplayIds(
99 const std::vector
<int64_t>& display_ids
) const = 0;
101 // Queries the resolution (|size|) in pixels to select display mode for the
103 virtual bool GetResolutionForDisplayId(int64_t display_id
,
104 gfx::Size
* size
) const = 0;
107 // Interface for classes that implement software based mirroring.
108 class SoftwareMirroringController
{
110 virtual ~SoftwareMirroringController() {}
112 // Called when the hardware mirroring failed.
113 virtual void SetSoftwareMirroring(bool enabled
) = 0;
114 virtual bool SoftwareMirroringEnabled() const = 0;
117 // Helper class used by tests.
120 TestApi(DisplayConfigurator
* configurator
) : configurator_(configurator
) {}
123 // If |configure_timer_| is started, stops the timer, runs
124 // ConfigureDisplays(), and returns true; returns false otherwise.
125 bool TriggerConfigureTimeout() WARN_UNUSED_RESULT
;
128 DisplayConfigurator
* configurator_
; // not owned
130 DISALLOW_COPY_AND_ASSIGN(TestApi
);
133 // Flags that can be passed to SetDisplayPower().
134 static const int kSetDisplayPowerNoFlags
;
135 // Configure displays even if the passed-in state matches |power_state_|.
136 static const int kSetDisplayPowerForceProbe
;
137 // Do not change the state if multiple displays are connected or if the
138 // only connected display is external.
139 static const int kSetDisplayPowerOnlyIfSingleInternalDisplay
;
141 // Gap between screens so cursor at bottom of active display doesn't
142 // partially appear on top of inactive display. Higher numbers guard
143 // against larger cursors, but also waste more memory.
144 // For simplicity, this is hard-coded to avoid the complexity of always
145 // determining the DPI of the screen and rationalizing which screen we
146 // need to use for the DPI calculation.
147 // See crbug.com/130188 for initial discussion.
148 static const int kVerticalGap
= 60;
150 // Returns the mode within |display| that matches the given size with highest
151 // refresh rate. Returns None if no matching display was found.
152 static const DisplayMode
* FindDisplayModeMatchingSize(
153 const DisplaySnapshot
& display
,
154 const gfx::Size
& size
);
156 DisplayConfigurator();
157 ~DisplayConfigurator() override
;
159 MultipleDisplayState
display_state() const { return current_display_state_
; }
160 chromeos::DisplayPowerState
requested_power_state() const {
161 return requested_power_state_
;
163 const gfx::Size
framebuffer_size() const { return framebuffer_size_
; }
164 const std::vector
<DisplaySnapshot
*>& cached_displays() const {
165 return cached_displays_
;
168 // Called when an external process no longer needs to control the display
169 // and Chrome can take control.
172 // Called when an external process needs to control the display and thus
173 // Chrome should relinquish it.
174 void RelinquishControl();
176 void set_state_controller(StateController
* controller
) {
177 state_controller_
= controller
;
179 void set_mirroring_controller(SoftwareMirroringController
* controller
) {
180 mirroring_controller_
= controller
;
183 // Replaces |native_display_delegate_| with the delegate passed in and sets
184 // |configure_display_| to true. Should be called before Init().
185 void SetDelegateForTesting(
186 scoped_ptr
<NativeDisplayDelegate
> display_delegate
);
188 // Sets the initial value of |power_state_|. Must be called before Start().
189 void SetInitialDisplayPower(chromeos::DisplayPowerState power_state
);
191 // Initialization, must be called right after constructor.
192 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
193 void Init(bool is_panel_fitting_enabled
);
195 // Does initial configuration of displays during startup.
196 // If |background_color_argb| is non zero and there are multiple displays,
197 // DisplayConfigurator sets the background color of X's RootWindow to this
199 void ForceInitialConfigure(uint32_t background_color_argb
);
201 // Stop handling display configuration events/requests.
202 void PrepareForExit();
204 // Called when powerd notifies us that some set of displays should be turned
205 // on or off. This requires enabling or disabling the CRTC associated with
206 // the display(s) in question so that the low power state is engaged.
207 // |flags| contains bitwise-or-ed kSetDisplayPower* values. After the
208 // configuration finishes |callback| is called with the status of the
210 void SetDisplayPower(chromeos::DisplayPowerState power_state
,
212 const ConfigurationCallback
& callback
);
214 // Force switching the display mode to |new_state|. Returns false if
215 // switching failed (possibly because |new_state| is invalid for the
216 // current set of connected displays).
217 void SetDisplayMode(MultipleDisplayState new_state
);
219 // NativeDisplayDelegate::Observer overrides:
220 void OnConfigurationChanged() override
;
222 void AddObserver(Observer
* observer
);
223 void RemoveObserver(Observer
* observer
);
225 // Sets all the displays into pre-suspend mode; usually this means
226 // configure them for their resume state. This allows faster resume on
227 // machines where display configuration is slow. On completion of the display
228 // configuration |callback| is executed.
229 void SuspendDisplays(const ConfigurationCallback
& callback
);
231 // Reprobes displays to handle changes made while the system was
233 void ResumeDisplays();
235 // Registers a client for display protection and requests a client id. Returns
236 // 0 if requesting failed.
237 ContentProtectionClientId
RegisterContentProtectionClient();
239 // Unregisters the client.
240 void UnregisterContentProtectionClient(ContentProtectionClientId client_id
);
242 // Queries link status and protection status. |callback| is used to respond
244 void QueryContentProtectionStatus(ContentProtectionClientId client_id
,
246 const QueryProtectionCallback
& callback
);
248 // Requests the desired protection methods.
249 // |protection_mask| is the desired protection methods, which is a bitmask
250 // of the ContentProtectionMethod values.
251 // Returns true when the protection request has been made.
252 void EnableContentProtection(ContentProtectionClientId client_id
,
254 uint32_t protection_mask
,
255 const EnableProtectionCallback
& callback
);
257 // Checks the available color profiles for |display_id| and fills the result
259 std::vector
<ui::ColorCalibrationProfile
> GetAvailableColorCalibrationProfiles(
262 // Updates the color calibration to |new_profile|.
263 bool SetColorCalibrationProfile(int64_t display_id
,
264 ui::ColorCalibrationProfile new_profile
);
266 // Sets the gamma ramp for |display_id| to the values in |lut|.
267 bool SetGammaRamp(int64_t display_id
,
268 const std::vector
<GammaRampRGBEntry
>& lut
);
271 class DisplayLayoutManagerImpl
;
273 // Mapping a client to its protection request.
274 typedef std::map
<ContentProtectionClientId
, ContentProtections
>
277 // Performs platform specific delegate initialization.
278 scoped_ptr
<NativeDisplayDelegate
> CreatePlatformNativeDisplayDelegate();
280 // Configures displays. Invoked by |configure_timer_|.
281 void ConfigureDisplays();
283 // Restores |requested_power_state_| after the system has resumed,
284 // additionally forcing a probe. Invoked by |configure_timer_|.
285 void RestoreRequestedPowerStateAfterResume();
287 // Notifies observers about an attempted state change.
288 void NotifyObservers(bool success
, MultipleDisplayState attempted_state
);
290 // Returns the display state that should be used with |cached_displays_| while
292 MultipleDisplayState
ChooseDisplayState(
293 chromeos::DisplayPowerState power_state
) const;
295 // Applies display protections according to requests.
296 bool ApplyProtections(const ContentProtections
& requests
);
298 // If |configuration_task_| isn't initialized, initializes it and starts the
299 // configuration task.
300 void RunPendingConfiguration();
302 // Callback for |configuration_taks_|. When the configuration process finishes
303 // this is called with the result (|success|) and the updated display state.
304 void OnConfigured(bool success
,
305 const std::vector
<DisplaySnapshot
*>& displays
,
306 const gfx::Size
& framebuffer_size
,
307 MultipleDisplayState new_display_state
,
308 chromeos::DisplayPowerState new_power_state
);
310 // Helps in identifying if a configuration task needs to be scheduled.
311 // Return true if any of the |requested_*| parameters have been updated. False
313 bool ShouldRunConfigurationTask() const;
315 // Helper functions which will call the callbacks in
316 // |in_progress_configuration_callbacks_| and
317 // |queued_configuration_callbacks_| and clear the lists after. |success| is
318 // the configuration status used when calling the callbacks.
319 void CallAndClearInProgressCallbacks(bool success
);
320 void CallAndClearQueuedCallbacks(bool success
);
322 // Content protection callbacks called by the tasks when they finish. These
323 // are responsible for destroying the task, replying to the caller that made
324 // the task and starting the a new content protection task if one is queued.
325 void OnContentProtectionQueried(
326 ContentProtectionClientId client_id
,
328 QueryContentProtectionTask::Response response
);
329 void OnContentProtectionEnabled(ContentProtectionClientId client_id
,
331 uint32_t desired_method_mask
,
333 void OnContentProtectionClientUnregistered(bool success
);
335 StateController
* state_controller_
;
336 SoftwareMirroringController
* mirroring_controller_
;
337 scoped_ptr
<NativeDisplayDelegate
> native_display_delegate_
;
339 // Used to enable modes which rely on panel fitting.
340 bool is_panel_fitting_enabled_
;
342 // This is detected by the constructor to determine whether or not we should
343 // be enabled. If we aren't running on Chrome OS, we can't assume that the
344 // Xrandr X11 extension or the Ozone underlying display hotplug system are
346 // If this flag is set to false, any attempts to change the display
347 // configuration to immediately fail without changing the state.
348 bool configure_display_
;
350 // Current configuration state.
351 MultipleDisplayState current_display_state_
;
352 chromeos::DisplayPowerState current_power_state_
;
354 // Pending requests. These values are used when triggering the next display
357 // Stores the user requested state or INVALID if nothing was requested.
358 MultipleDisplayState requested_display_state_
;
360 // Stores the requested power state.
361 chromeos::DisplayPowerState requested_power_state_
;
363 // True if |requested_power_state_| has been changed due to a user request.
364 bool requested_power_state_change_
;
366 // Bitwise-or value of the |kSetDisplayPower*| flags defined above.
367 int requested_power_flags_
;
369 // List of callbacks from callers waiting for the display configuration to
370 // start/finish. Note these callbacks belong to the pending request, not a
371 // request currently active.
372 std::vector
<ConfigurationCallback
> queued_configuration_callbacks_
;
374 // List of callbacks belonging to the currently running display configuration
376 std::vector
<ConfigurationCallback
> in_progress_configuration_callbacks_
;
378 std::queue
<base::Closure
> content_protection_tasks_
;
379 std::queue
<QueryProtectionCallback
> query_protection_callbacks_
;
380 std::queue
<EnableProtectionCallback
> enable_protection_callbacks_
;
382 // True if the caller wants to force the display configuration process.
383 bool force_configure_
;
385 // Most-recently-used display configuration. Note that the actual
386 // configuration changes asynchronously.
387 DisplayStateList cached_displays_
;
389 // Most-recently-used framebuffer size.
390 gfx::Size framebuffer_size_
;
392 ObserverList
<Observer
> observers_
;
394 // The timer to delay configuring displays. This is used to aggregate multiple
395 // display configuration events when they are reported in short time spans.
396 // See comment for NativeDisplayEventDispatcherX11 for more details.
397 base::OneShotTimer
<DisplayConfigurator
> configure_timer_
;
399 // Id for next display protection client.
400 ContentProtectionClientId next_display_protection_client_id_
;
402 // Display protection requests of each client.
403 ProtectionRequests client_protection_requests_
;
405 // Display controlled by an external entity.
406 bool display_externally_controlled_
;
408 // Whether the displays are currently suspended.
409 bool displays_suspended_
;
411 scoped_ptr
<DisplayLayoutManager
> layout_manager_
;
413 scoped_ptr
<UpdateDisplayConfigurationTask
> configuration_task_
;
415 // This must be the last variable.
416 base::WeakPtrFactory
<DisplayConfigurator
> weak_ptr_factory_
;
418 DISALLOW_COPY_AND_ASSIGN(DisplayConfigurator
);
423 #endif // UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_