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.
5 #ifndef ASH_DISPLAY_DISPLAY_MANAGER_H_
6 #define ASH_DISPLAY_DISPLAY_MANAGER_H_
11 #include "ash/ash_export.h"
12 #include "ash/display/display_info.h"
13 #include "ash/display/display_layout.h"
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "ui/gfx/display.h"
20 #if defined(OS_CHROMEOS)
21 #include "ui/display/chromeos/display_configurator.h"
29 class DisplayNotificationsTest
;
40 class AcceleratorControllerTest
;
41 class DisplayController
;
42 class DisplayLayoutStore
;
43 class MouseWarpController
;
46 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
50 class DisplayManagerTestApi
;
51 class SystemGestureEventFilterTest
;
54 // DisplayManager maintains the current display configurations,
55 // and notifies observers when configuration changes.
57 // TODO(oshima): Make this non internal.
58 class ASH_EXPORT DisplayManager
59 #if defined(OS_CHROMEOS)
60 : public ui::DisplayConfigurator::SoftwareMirroringController
64 class ASH_EXPORT Delegate
{
66 virtual ~Delegate() {}
68 // Create or updates the mirroring window with |display_info_list|.
69 virtual void CreateOrUpdateMirroringDisplay(
70 const DisplayInfoList
& display_info_list
) = 0;
72 // Closes the mirror window if not necessary.
73 virtual void CloseMirroringDisplayIfNotNecessary() = 0;
75 // Called before and after the display configuration changes.
76 // When |clear_focus| is true, the implementation should
77 // deactivate the active window and set the focus window to NULL.
78 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
79 virtual void PostDisplayConfigurationChange() = 0;
82 typedef std::vector
<gfx::Display
> DisplayList
;
84 // How the second display will be used.
85 // 1) EXTENDED mode extends the desktop to the second dislpay.
86 // 2) MIRRORING mode copies the content of the primary display to
87 // the 2nd display. (Software Mirroring).
88 // 3) UNIFIED mode creates single desktop across multiple displays.
89 enum MultiDisplayMode
{
95 // The display ID for a virtual display assigned to a unified desktop.
96 static int64 kUnifiedDisplayId
;
99 #if defined(OS_CHROMEOS)
100 ~DisplayManager() override
;
102 virtual ~DisplayManager();
105 DisplayLayoutStore
* layout_store() {
106 return layout_store_
.get();
109 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
111 // When set to true, the DisplayManager calls OnDisplayMetricsChanged
112 // even if the display's bounds didn't change. Used to swap primary
114 void set_force_bounds_changed(bool force_bounds_changed
) {
115 force_bounds_changed_
= force_bounds_changed
;
118 // Returns the display id of the first display in the outupt list.
119 int64
first_display_id() const { return first_display_id_
; }
121 // Initializes displays using command line flag. Returns false
122 // if no command line flag was provided.
123 bool InitFromCommandLine();
125 // Initialize default display.
126 void InitDefaultDisplay();
128 // Initializes font related params that depends on display
130 void RefreshFontParams();
132 // Returns the display layout used for current displays.
133 DisplayLayout
GetCurrentDisplayLayout();
135 // Returns the current display pair.
136 DisplayIdPair
GetCurrentDisplayIdPair() const;
138 // Sets the layout for the current display pair. The |layout| specifies
139 // the locaion of the secondary display relative to the primary.
140 void SetLayoutForCurrentDisplays(
141 const DisplayLayout
& layout_relative_to_primary
);
143 // Returns display for given |id|;
144 const gfx::Display
& GetDisplayForId(int64 id
) const;
146 // Finds the display that contains |point| in screeen coordinates.
147 // Returns invalid display if there is no display that can satisfy
149 const gfx::Display
& FindDisplayContainingPoint(
150 const gfx::Point
& point_in_screen
) const;
152 // Sets the work area's |insets| to the display given by |display_id|.
153 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
155 // Registers the overscan insets for the display of the specified ID. Note
156 // that the insets size should be specified in DIP size. It also triggers the
157 // display's bounds change.
158 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
160 // Sets the display's rotation for the given |source|. The new |rotation| will
161 // also become active.
162 void SetDisplayRotation(int64 display_id
,
163 gfx::Display::Rotation rotation
,
164 gfx::Display::RotationSource source
);
166 // Sets the display's ui scale. Returns true if it's successful, or
167 // false otherwise. TODO(mukai): remove this and merge into
169 bool SetDisplayUIScale(int64 display_id
, float ui_scale
);
171 // Sets the display's resolution.
172 // TODO(mukai): remove this and merge into SetDisplayMode.
173 void SetDisplayResolution(int64 display_id
, const gfx::Size
& resolution
);
175 // Sets the external display's configuration, including resolution change,
176 // ui-scale change, and device scale factor change. Returns true if it changes
177 // the display resolution so that the caller needs to show a notification in
178 // case the new resolution actually doesn't work.
179 bool SetDisplayMode(int64 display_id
, const DisplayMode
& display_mode
);
181 // Register per display properties. |overscan_insets| is NULL if
182 // the display has no custom overscan insets.
183 void RegisterDisplayProperty(int64 display_id
,
184 gfx::Display::Rotation rotation
,
186 const gfx::Insets
* overscan_insets
,
187 const gfx::Size
& resolution_in_pixels
,
188 float device_scale_factor
,
189 ui::ColorCalibrationProfile color_profile
);
191 // Register stored rotation properties for the internal display.
192 void RegisterDisplayRotationProperties(bool rotation_lock
,
193 gfx::Display::Rotation rotation
);
195 // Returns the stored rotation lock preference if it has been loaded,
197 bool registered_internal_display_rotation_lock() const {
198 return registered_internal_display_rotation_lock_
;
201 // Returns the stored rotation preference for the internal display if it has
202 // been loaded, otherwise |gfx::Display::Rotate_0|.
203 gfx::Display::Rotation
registered_internal_display_rotation() const {
204 return registered_internal_display_rotation_
;
207 // Returns the display mode of |display_id| which is currently used.
208 DisplayMode
GetActiveModeForDisplayId(int64 display_id
) const;
210 // Returns the display's selected mode. This returns false and doesn't
211 // set |mode_out| if the display mode is in default.
212 bool GetSelectedModeForDisplayId(int64 display_id
,
213 DisplayMode
* mode_out
) const;
215 // Tells if the virtual resolution feature is enabled.
216 bool IsDisplayUIScalingEnabled() const;
218 // Returns the current overscan insets for the specified |display_id|.
219 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
221 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
223 // Sets the color calibration of the display to |profile|.
224 void SetColorCalibrationProfile(int64 display_id
,
225 ui::ColorCalibrationProfile profile
);
227 // Called when display configuration has changed. The new display
228 // configurations is passed as a vector of Display object, which
229 // contains each display's new infomration.
230 void OnNativeDisplaysChanged(
231 const std::vector
<DisplayInfo
>& display_info_list
);
233 // Updates the internal display data and notifies observers about the changes.
234 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
236 // Updates current displays using current |display_info_|.
237 void UpdateDisplays();
239 // Returns the display at |index|. The display at 0 is
240 // no longer considered "primary".
241 const gfx::Display
& GetDisplayAt(size_t index
) const;
243 const gfx::Display
& GetPrimaryDisplayCandidate() const;
245 // Returns the logical number of displays. This returns 1
246 // when displays are mirrored.
247 size_t GetNumDisplays() const;
249 const DisplayList
& active_display_list() const {
250 return active_display_list_
;
253 // Returns the number of connected displays. This returns 2
254 // when displays are mirrored.
255 size_t num_connected_displays() const { return num_connected_displays_
; }
257 // Returns the mirroring status.
258 bool IsInMirrorMode() const;
259 int64
mirroring_display_id() const { return mirroring_display_id_
; }
260 const DisplayList
& software_mirroring_display_list() const {
261 return software_mirroring_display_list_
;
263 bool IsInUnifiedMode() const;
265 // Returns the display used for software mirrroring. Returns invalid
266 // display if not found.
267 const gfx::Display
GetMirroringDisplayById(int64 id
) const;
269 // Retuns the display info associated with |display_id|.
270 const DisplayInfo
& GetDisplayInfo(int64 display_id
) const;
272 // Returns the human-readable name for the display |id|.
273 std::string
GetDisplayNameForId(int64 id
);
275 // Returns the display id that is capable of UI scaling. On device,
276 // this returns internal display's ID if its device scale factor is 2,
277 // or invalid ID if such internal display doesn't exist. On linux
278 // desktop, this returns the first display ID.
279 int64
GetDisplayIdForUIScaling() const;
281 // Change the mirror mode.
282 void SetMirrorMode(bool mirrored
);
284 // Used to emulate display change when run in a desktop environment instead
286 void AddRemoveDisplay();
287 void ToggleDisplayScaleFactor();
289 // SoftwareMirroringController override:
290 #if defined(OS_CHROMEOS)
291 void SetSoftwareMirroring(bool enabled
) override
;
292 bool SoftwareMirroringEnabled() const override
;
295 // Sets/gets multi display mode.
296 void SetMultiDisplayMode(MultiDisplayMode mode
);
298 // Sets/gets default multi display mode.
299 void SetDefaultMultiDisplayMode(MultiDisplayMode mode
);
300 MultiDisplayMode
default_multi_display_mode() const {
301 return default_multi_display_mode_
;
304 // Reconfigure display configuration using the same
305 // physical display. TODO(oshima): Refactor and move this
306 // impl to |SetDefaultMultiDisplayMode|.
307 void ReconfigureDisplays();
309 // Update the bounds of the display given by |display_id|.
310 bool UpdateDisplayBounds(int64 display_id
,
311 const gfx::Rect
& new_bounds
);
313 // Creates mirror window asynchronously if the software mirror mode
315 void CreateMirrorWindowAsyncIfAny();
317 // Creates a MouseWarpController for the current display
318 // configuration. |drag_source| is the window where dragging
319 // started, or nullptr otherwise.
320 scoped_ptr
<MouseWarpController
> CreateMouseWarpController(
321 aura::Window
* drag_source
) const;
323 // Create a screen instance to be used during shutdown.
324 void CreateScreenForShutdown() const;
326 // A unit test may change the internal display id (which never happens on
327 // a real device). This will update the mode list for internal display
328 // for this test scenario.
329 void UpdateInternalDisplayModeListForTest();
332 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
333 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
334 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
335 NativeDisplaysChangedAfterPrimaryChange
);
336 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
337 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, Rotate
);
338 friend class AcceleratorControllerTest
;
339 friend class DisplayManagerTest
;
340 friend class chromeos::DisplayNotificationsTest
;
341 friend class test::AshTestBase
;
342 friend class test::DisplayManagerTestApi
;
343 friend class test::SystemGestureEventFilterTest
;
345 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
347 bool software_mirroring_enabled() const {
348 return multi_display_mode_
== MIRRORING
;
351 void set_change_display_upon_host_resize(bool value
) {
352 change_display_upon_host_resize_
= value
;
355 // Creates software mirroring display related information. The display
356 // used to mirror the content is removed from the |display_info_list|.
357 void CreateSoftwareMirroringDisplayInfo(DisplayInfoList
* display_info_list
);
359 gfx::Display
* FindDisplayForId(int64 id
);
361 // Add the mirror display's display info if the software based
362 // mirroring is in use.
363 void AddMirrorDisplayInfoIfAny(DisplayInfoList
* display_info_list
);
365 // Inserts and update the DisplayInfo according to the overscan
366 // state. Note that The DisplayInfo stored in the |internal_display_info_|
367 // can be different from |new_info| (due to overscan state), so
368 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
370 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
372 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
373 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
375 // Creates a display object from the DisplayInfo for |display_id|.
376 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
378 // Updates the bounds of all non-primary displays in |display_list| and
379 // append the indices of displays updated to |updated_indices|.
380 // When the size of |display_list| equals 2, the bounds are updated using
381 // the layout registered for the display pair. For more than 2 displays,
382 // the bounds are updated using horizontal layout.
383 // Returns true if any of the non-primary display's bounds has been changed
384 // from current value, or false otherwise.
385 bool UpdateNonPrimaryDisplayBoundsForLayout(
386 DisplayList
* display_list
, std::vector
<size_t>* updated_indices
) const;
388 void CreateMirrorWindowIfAny();
390 void RunPendingTasksForTest();
392 static void UpdateDisplayBoundsForLayout(
393 const DisplayLayout
& layout
,
394 const gfx::Display
& primary_display
,
395 gfx::Display
* secondary_display
);
397 Delegate
* delegate_
; // not owned.
399 scoped_ptr
<ScreenAsh
> screen_
;
401 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
403 int64 first_display_id_
;
405 // List of current active displays.
406 DisplayList active_display_list_
;
408 int num_connected_displays_
;
410 bool force_bounds_changed_
;
412 // The mapping from the display ID to its internal data.
413 std::map
<int64
, DisplayInfo
> display_info_
;
415 // Selected display modes for displays. Key is the displays' ID.
416 std::map
<int64
, DisplayMode
> display_modes_
;
418 // When set to true, the host window's resize event updates
419 // the display's size. This is set to true when running on
420 // desktop environment (for debugging) so that resizing the host
421 // window will update the display properly. This is set to false
422 // on device as well as during the unit tests.
423 bool change_display_upon_host_resize_
;
425 MultiDisplayMode multi_display_mode_
;
426 MultiDisplayMode default_multi_display_mode_
;
428 int64 mirroring_display_id_
;
429 DisplayList software_mirroring_display_list_
;
431 // User preference for rotation lock of the internal display.
432 bool registered_internal_display_rotation_lock_
;
434 // User preference for the rotation of the internal display.
435 gfx::Display::Rotation registered_internal_display_rotation_
;
437 base::WeakPtrFactory
<DisplayManager
> weak_ptr_factory_
;
439 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
444 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_