Re-land: C++ readability review
[chromium-blink-merge.git] / ui / display / chromeos / display_configurator.h
blob88eab83b4db146f787a5fd377878e792c76ed0a6
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_
8 #include <stdint.h>
10 #include <map>
11 #include <string>
12 #include <vector>
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"
25 namespace gfx {
26 class Point;
27 class Size;
30 namespace ui {
31 struct DisplayConfigureRequest;
32 class DisplayMode;
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 {
39 public:
40 typedef uint64_t ContentProtectionClientId;
41 static const ContentProtectionClientId kInvalidClientId = 0;
43 typedef base::Callback<void(bool)> ConfigurationCallback;
45 typedef std::vector<DisplaySnapshot*> DisplayStateList;
47 class Observer {
48 public:
49 virtual ~Observer() {}
51 // Called after the display mode has been changed. |display| contains the
52 // just-applied configuration. Note that the X server is no longer grabbed
53 // when this method is called, so the actual configuration could've changed
54 // already.
55 virtual void OnDisplayModeChanged(
56 const DisplayStateList& displays) {}
58 // Called after a display mode change attempt failed. |displays| contains
59 // displays that are detected when failed.
60 // |failed_new_state| is the new state which the system failed to enter.
61 virtual void OnDisplayModeChangeFailed(
62 const DisplayStateList& displays,
63 MultipleDisplayState failed_new_state) {}
66 // Interface for classes that make decisions about which display state
67 // should be used.
68 class StateController {
69 public:
70 virtual ~StateController() {}
72 // Called when displays are detected.
73 virtual MultipleDisplayState GetStateForDisplayIds(
74 const std::vector<int64_t>& display_ids) const = 0;
76 // Queries the resolution (|size|) in pixels to select display mode for the
77 // given display id.
78 virtual bool GetResolutionForDisplayId(int64_t display_id,
79 gfx::Size* size) const = 0;
82 // Interface for classes that implement software based mirroring.
83 class SoftwareMirroringController {
84 public:
85 virtual ~SoftwareMirroringController() {}
87 // Called when the hardware mirroring failed.
88 virtual void SetSoftwareMirroring(bool enabled) = 0;
89 virtual bool SoftwareMirroringEnabled() const = 0;
92 class DisplayLayoutManager {
93 public:
94 virtual ~DisplayLayoutManager() {}
96 virtual SoftwareMirroringController* GetSoftwareMirroringController()
97 const = 0;
99 virtual StateController* GetStateController() const = 0;
101 // Returns the current display state.
102 virtual MultipleDisplayState GetDisplayState() const = 0;
104 // Returns the current power state.
105 virtual chromeos::DisplayPowerState GetPowerState() const = 0;
107 // Based on the given |displays|, display state and power state, it will
108 // create display configuration requests which will then be used to
109 // configure the hardware. The requested configuration is stored in
110 // |requests| and |framebuffer_size|.
111 virtual bool GetDisplayLayout(
112 const std::vector<DisplaySnapshot*>& displays,
113 MultipleDisplayState new_display_state,
114 chromeos::DisplayPowerState new_power_state,
115 std::vector<DisplayConfigureRequest>* requests,
116 gfx::Size* framebuffer_size) const = 0;
119 // Helper class used by tests.
120 class TestApi {
121 public:
122 TestApi(DisplayConfigurator* configurator) : configurator_(configurator) {}
123 ~TestApi() {}
125 // If |configure_timer_| is started, stops the timer, runs
126 // ConfigureDisplays(), and returns true; returns false otherwise.
127 bool TriggerConfigureTimeout() WARN_UNUSED_RESULT;
129 private:
130 DisplayConfigurator* configurator_; // not owned
132 DISALLOW_COPY_AND_ASSIGN(TestApi);
135 // Flags that can be passed to SetDisplayPower().
136 static const int kSetDisplayPowerNoFlags;
137 // Configure displays even if the passed-in state matches |power_state_|.
138 static const int kSetDisplayPowerForceProbe;
139 // Do not change the state if multiple displays are connected or if the
140 // only connected display is external.
141 static const int kSetDisplayPowerOnlyIfSingleInternalDisplay;
143 // Gap between screens so cursor at bottom of active display doesn't
144 // partially appear on top of inactive display. Higher numbers guard
145 // against larger cursors, but also waste more memory.
146 // For simplicity, this is hard-coded to avoid the complexity of always
147 // determining the DPI of the screen and rationalizing which screen we
148 // need to use for the DPI calculation.
149 // See crbug.com/130188 for initial discussion.
150 static const int kVerticalGap = 60;
152 // Returns the mode within |display| that matches the given size with highest
153 // refresh rate. Returns None if no matching display was found.
154 static const DisplayMode* FindDisplayModeMatchingSize(
155 const DisplaySnapshot& display,
156 const gfx::Size& size);
158 DisplayConfigurator();
159 ~DisplayConfigurator() override;
161 MultipleDisplayState display_state() const { return current_display_state_; }
162 chromeos::DisplayPowerState requested_power_state() const {
163 return requested_power_state_;
165 const gfx::Size framebuffer_size() const { return framebuffer_size_; }
166 const std::vector<DisplaySnapshot*>& cached_displays() const {
167 return cached_displays_;
170 // Called when an external process no longer needs to control the display
171 // and Chrome can take control.
172 void TakeControl();
174 // Called when an external process needs to control the display and thus
175 // Chrome should relinquish it.
176 void RelinquishControl();
178 void set_state_controller(StateController* controller) {
179 state_controller_ = controller;
181 void set_mirroring_controller(SoftwareMirroringController* controller) {
182 mirroring_controller_ = controller;
185 // Replaces |native_display_delegate_| with the delegate passed in and sets
186 // |configure_display_| to true. Should be called before Init().
187 void SetDelegateForTesting(
188 scoped_ptr<NativeDisplayDelegate> display_delegate);
190 // Sets the initial value of |power_state_|. Must be called before Start().
191 void SetInitialDisplayPower(chromeos::DisplayPowerState power_state);
193 // Initialization, must be called right after constructor.
194 // |is_panel_fitting_enabled| indicates hardware panel fitting support.
195 void Init(bool is_panel_fitting_enabled);
197 // Does initial configuration of displays during startup.
198 // If |background_color_argb| is non zero and there are multiple displays,
199 // DisplayConfigurator sets the background color of X's RootWindow to this
200 // color.
201 void ForceInitialConfigure(uint32_t background_color_argb);
203 // Stop handling display configuration events/requests.
204 void PrepareForExit();
206 // Called when powerd notifies us that some set of displays should be turned
207 // on or off. This requires enabling or disabling the CRTC associated with
208 // the display(s) in question so that the low power state is engaged.
209 // |flags| contains bitwise-or-ed kSetDisplayPower* values. After the
210 // configuration finishes |callback| is called with the status of the
211 // operation.
212 void SetDisplayPower(chromeos::DisplayPowerState power_state,
213 int flags,
214 const ConfigurationCallback& callback);
216 // Force switching the display mode to |new_state|. Returns false if
217 // switching failed (possibly because |new_state| is invalid for the
218 // current set of connected displays).
219 void SetDisplayMode(MultipleDisplayState new_state);
221 // NativeDisplayDelegate::Observer overrides:
222 void OnConfigurationChanged() override;
224 void AddObserver(Observer* observer);
225 void RemoveObserver(Observer* observer);
227 // Sets all the displays into pre-suspend mode; usually this means
228 // configure them for their resume state. This allows faster resume on
229 // machines where display configuration is slow.
230 void SuspendDisplays();
232 // Reprobes displays to handle changes made while the system was
233 // suspended.
234 void ResumeDisplays();
236 // Registers a client for display protection and requests a client id. Returns
237 // 0 if requesting failed.
238 ContentProtectionClientId RegisterContentProtectionClient();
240 // Unregisters the client.
241 void UnregisterContentProtectionClient(ContentProtectionClientId client_id);
243 // Queries link status and protection status.
244 // |link_mask| is the type of connected display links, which is a bitmask of
245 // DisplayConnectionType values. |protection_mask| is the desired protection
246 // methods, which is a bitmask of the ContentProtectionMethod values.
247 // Returns true on success.
248 bool QueryContentProtectionStatus(ContentProtectionClientId client_id,
249 int64_t display_id,
250 uint32_t* link_mask,
251 uint32_t* protection_mask);
253 // Requests the desired protection methods.
254 // |protection_mask| is the desired protection methods, which is a bitmask
255 // of the ContentProtectionMethod values.
256 // Returns true when the protection request has been made.
257 bool EnableContentProtection(ContentProtectionClientId client_id,
258 int64_t display_id,
259 uint32_t desired_protection_mask);
261 // Checks the available color profiles for |display_id| and fills the result
262 // into |profiles|.
263 std::vector<ui::ColorCalibrationProfile> GetAvailableColorCalibrationProfiles(
264 int64_t display_id);
266 // Updates the color calibration to |new_profile|.
267 bool SetColorCalibrationProfile(int64_t display_id,
268 ui::ColorCalibrationProfile new_profile);
270 private:
271 class DisplayLayoutManagerImpl;
273 // Mapping a display_id to a protection request bitmask.
274 typedef std::map<int64_t, uint32_t> ContentProtections;
275 // Mapping a client to its protection request.
276 typedef std::map<ContentProtectionClientId, ContentProtections>
277 ProtectionRequests;
279 // Performs platform specific delegate initialization.
280 scoped_ptr<NativeDisplayDelegate> CreatePlatformNativeDisplayDelegate();
282 // Configures displays. Invoked by |configure_timer_|.
283 void ConfigureDisplays();
285 // Restores |requested_power_state_| after the system has resumed,
286 // additionally forcing a probe. Invoked by |configure_timer_|.
287 void RestoreRequestedPowerStateAfterResume();
289 // Notifies observers about an attempted state change.
290 void NotifyObservers(bool success, MultipleDisplayState attempted_state);
292 // Returns the display state that should be used with |cached_displays_| while
293 // in |power_state|.
294 MultipleDisplayState ChooseDisplayState(
295 chromeos::DisplayPowerState power_state) const;
297 // Returns true if in either hardware or software mirroring mode.
298 bool IsMirroring() const;
300 // Applies display protections according to requests.
301 bool ApplyProtections(const ContentProtections& requests);
303 // If |configuration_task_| isn't initialized, initializes it and starts the
304 // configuration task.
305 void RunPendingConfiguration();
307 // Callback for |configuration_taks_|. When the configuration process finishes
308 // this is called with the result (|success|) and the updated display state.
309 void OnConfigured(bool success,
310 const std::vector<DisplaySnapshot*>& displays,
311 const gfx::Size& framebuffer_size,
312 MultipleDisplayState new_display_state,
313 chromeos::DisplayPowerState new_power_state);
315 // Helps in identifying if a configuration task needs to be scheduled.
316 // Return true if any of the |requested_*| parameters have been updated. False
317 // otherwise.
318 bool ShouldRunConfigurationTask() const;
320 // Helper functions which will call the callbacks in
321 // |in_progress_configuration_callbacks_| and
322 // |queued_configuration_callbacks_| and clear the lists after. |success| is
323 // the configuration status used when calling the callbacks.
324 void CallAndClearInProgressCallbacks(bool success);
325 void CallAndClearQueuedCallbacks(bool success);
327 StateController* state_controller_;
328 SoftwareMirroringController* mirroring_controller_;
329 scoped_ptr<NativeDisplayDelegate> native_display_delegate_;
331 // Used to enable modes which rely on panel fitting.
332 bool is_panel_fitting_enabled_;
334 // This is detected by the constructor to determine whether or not we should
335 // be enabled. If we aren't running on Chrome OS, we can't assume that the
336 // Xrandr X11 extension or the Ozone underlying display hotplug system are
337 // supported.
338 // If this flag is set to false, any attempts to change the display
339 // configuration to immediately fail without changing the state.
340 bool configure_display_;
342 // Current configuration state.
343 MultipleDisplayState current_display_state_;
344 chromeos::DisplayPowerState current_power_state_;
346 // Pending requests. These values are used when triggering the next display
347 // configuration.
349 // Stores the user requested state or INVALID if nothing was requested.
350 MultipleDisplayState requested_display_state_;
352 // Stores the requested power state.
353 chromeos::DisplayPowerState requested_power_state_;
355 // True if |requested_power_state_| has been changed due to a user request.
356 bool requested_power_state_change_;
358 // Bitwise-or value of the |kSetDisplayPower*| flags defined above.
359 int requested_power_flags_;
361 // List of callbacks from callers waiting for the display configuration to
362 // start/finish. Note these callbacks belong to the pending request, not a
363 // request currently active.
364 std::vector<ConfigurationCallback> queued_configuration_callbacks_;
366 // List of callbacks belonging to the currently running display configuration
367 // task.
368 std::vector<ConfigurationCallback> in_progress_configuration_callbacks_;
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 // Whether the displays are currently suspended.
397 bool displays_suspended_;
399 scoped_ptr<DisplayLayoutManager> layout_manager_;
401 scoped_ptr<UpdateDisplayConfigurationTask> configuration_task_;
403 // This must be the last variable.
404 base::WeakPtrFactory<DisplayConfigurator> weak_ptr_factory_;
406 DISALLOW_COPY_AND_ASSIGN(DisplayConfigurator);
409 } // namespace ui
411 #endif // UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_