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"
36 class AcceleratorControllerTest
;
37 class DisplayController
;
38 class DisplayLayoutStore
;
39 class MouseWarpController
;
42 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
46 class DisplayManagerTestApi
;
47 class SystemGestureEventFilterTest
;
50 // DisplayManager maintains the current display configurations,
51 // and notifies observers when configuration changes.
53 // TODO(oshima): Make this non internal.
54 class ASH_EXPORT DisplayManager
55 #if defined(OS_CHROMEOS)
56 : public ui::DisplayConfigurator::SoftwareMirroringController
60 class ASH_EXPORT Delegate
{
62 virtual ~Delegate() {}
64 // Create or updates the mirroring window with |display_info_list|.
65 virtual void CreateOrUpdateMirroringDisplay(
66 const DisplayInfoList
& display_info_list
) = 0;
68 // Closes the mirror window if not necessary.
69 virtual void CloseMirroringDisplayIfNotNecessary() = 0;
71 // Called before and after the display configuration changes.
72 // When |clear_focus| is true, the implementation should
73 // deactivate the active window and set the focus window to NULL.
74 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
75 virtual void PostDisplayConfigurationChange() = 0;
78 typedef std::vector
<gfx::Display
> DisplayList
;
80 // How the second display will be used.
81 // 1) EXTENDED mode extends the desktop to the second dislpay.
82 // 2) MIRRORING mode copies the content of the primary display to
83 // the 2nd display. (Software Mirroring).
84 // 3) UNIFIED mode creates single desktop across multiple displays.
85 enum MultiDisplayMode
{
91 // The display ID for a virtual display assigned to a unified desktop.
92 static int64 kUnifiedDisplayId
;
95 #if defined(OS_CHROMEOS)
96 ~DisplayManager() override
;
98 virtual ~DisplayManager();
101 DisplayLayoutStore
* layout_store() {
102 return layout_store_
.get();
105 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
107 // When set to true, the DisplayManager calls OnDisplayMetricsChanged
108 // even if the display's bounds didn't change. Used to swap primary
110 void set_force_bounds_changed(bool force_bounds_changed
) {
111 force_bounds_changed_
= force_bounds_changed
;
114 // Returns the display id of the first display in the outupt list.
115 int64
first_display_id() const { return first_display_id_
; }
117 // Initializes displays using command line flag. Returns false
118 // if no command line flag was provided.
119 bool InitFromCommandLine();
121 // Initialize default display.
122 void InitDefaultDisplay();
124 // Initializes font related params that depends on display
126 void RefreshFontParams();
128 // Returns the display layout used for current displays.
129 DisplayLayout
GetCurrentDisplayLayout();
131 // Returns the current display pair.
132 DisplayIdPair
GetCurrentDisplayIdPair() const;
134 // Sets the layout for the current display pair. The |layout| specifies
135 // the locaion of the secondary display relative to the primary.
136 void SetLayoutForCurrentDisplays(
137 const DisplayLayout
& layout_relative_to_primary
);
139 // Returns display for given |id|;
140 const gfx::Display
& GetDisplayForId(int64 id
) const;
142 // Finds the display that contains |point| in screeen coordinates.
143 // Returns invalid display if there is no display that can satisfy
145 const gfx::Display
& FindDisplayContainingPoint(
146 const gfx::Point
& point_in_screen
) const;
148 // Sets the work area's |insets| to the display given by |display_id|.
149 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
151 // Registers the overscan insets for the display of the specified ID. Note
152 // that the insets size should be specified in DIP size. It also triggers the
153 // display's bounds change.
154 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
156 // Sets the display's rotation for the given |source|. The new |rotation| will
157 // also become active.
158 void SetDisplayRotation(int64 display_id
,
159 gfx::Display::Rotation rotation
,
160 gfx::Display::RotationSource source
);
162 // Sets the display's ui scale. Returns true if it's successful, or
163 // false otherwise. TODO(mukai): remove this and merge into
165 bool SetDisplayUIScale(int64 display_id
, float ui_scale
);
167 // Sets the display's resolution.
168 // TODO(mukai): remove this and merge into SetDisplayMode.
169 void SetDisplayResolution(int64 display_id
, const gfx::Size
& resolution
);
171 // Sets the external display's configuration, including resolution change,
172 // ui-scale change, and device scale factor change. Returns true if it changes
173 // the display resolution so that the caller needs to show a notification in
174 // case the new resolution actually doesn't work.
175 bool SetDisplayMode(int64 display_id
, const DisplayMode
& display_mode
);
177 // Register per display properties. |overscan_insets| is NULL if
178 // the display has no custom overscan insets.
179 void RegisterDisplayProperty(int64 display_id
,
180 gfx::Display::Rotation rotation
,
182 const gfx::Insets
* overscan_insets
,
183 const gfx::Size
& resolution_in_pixels
,
184 float device_scale_factor
,
185 ui::ColorCalibrationProfile color_profile
);
187 // Register stored rotation properties for the internal display.
188 void RegisterDisplayRotationProperties(bool rotation_lock
,
189 gfx::Display::Rotation rotation
);
191 // Returns the stored rotation lock preference if it has been loaded,
193 bool registered_internal_display_rotation_lock() const {
194 return registered_internal_display_rotation_lock_
;
197 // Returns the stored rotation preference for the internal display if it has
198 // been loaded, otherwise |gfx::Display::Rotate_0|.
199 gfx::Display::Rotation
registered_internal_display_rotation() const {
200 return registered_internal_display_rotation_
;
203 // Returns the display mode of |display_id| which is currently used.
204 DisplayMode
GetActiveModeForDisplayId(int64 display_id
) const;
206 // Returns the display's selected mode. This returns false and doesn't
207 // set |mode_out| if the display mode is in default.
208 bool GetSelectedModeForDisplayId(int64 display_id
,
209 DisplayMode
* mode_out
) const;
211 // Tells if the virtual resolution feature is enabled.
212 bool IsDisplayUIScalingEnabled() const;
214 // Returns the current overscan insets for the specified |display_id|.
215 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
217 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
219 // Sets the color calibration of the display to |profile|.
220 void SetColorCalibrationProfile(int64 display_id
,
221 ui::ColorCalibrationProfile profile
);
223 // Called when display configuration has changed. The new display
224 // configurations is passed as a vector of Display object, which
225 // contains each display's new infomration.
226 void OnNativeDisplaysChanged(
227 const std::vector
<DisplayInfo
>& display_info_list
);
229 // Updates the internal display data and notifies observers about the changes.
230 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
232 // Updates current displays using current |display_info_|.
233 void UpdateDisplays();
235 // Returns the display at |index|. The display at 0 is
236 // no longer considered "primary".
237 const gfx::Display
& GetDisplayAt(size_t index
) const;
239 const gfx::Display
& GetPrimaryDisplayCandidate() const;
241 // Returns the logical number of displays. This returns 1
242 // when displays are mirrored.
243 size_t GetNumDisplays() const;
245 const DisplayList
& active_display_list() const {
246 return active_display_list_
;
249 // Returns the number of connected displays. This returns 2
250 // when displays are mirrored.
251 size_t num_connected_displays() const { return num_connected_displays_
; }
253 // Returns the mirroring status.
254 bool IsInMirrorMode() const;
255 int64
mirroring_display_id() const { return mirroring_display_id_
; }
256 const DisplayList
& software_mirroring_display_list() const {
257 return software_mirroring_display_list_
;
259 bool IsInUnifiedMode() const;
261 // Returns the display used for software mirrroring. Returns invalid
262 // display if not found.
263 const gfx::Display
GetMirroringDisplayById(int64 id
) const;
265 // Retuns the display info associated with |display_id|.
266 const DisplayInfo
& GetDisplayInfo(int64 display_id
) const;
268 // Returns the human-readable name for the display |id|.
269 std::string
GetDisplayNameForId(int64 id
);
271 // Returns the display id that is capable of UI scaling. On device,
272 // this returns internal display's ID if its device scale factor is 2,
273 // or invalid ID if such internal display doesn't exist. On linux
274 // desktop, this returns the first display ID.
275 int64
GetDisplayIdForUIScaling() const;
277 // Change the mirror mode.
278 void SetMirrorMode(bool mirrored
);
280 // Used to emulate display change when run in a desktop environment instead
282 void AddRemoveDisplay();
283 void ToggleDisplayScaleFactor();
285 // SoftwareMirroringController override:
286 #if defined(OS_CHROMEOS)
287 void SetSoftwareMirroring(bool enabled
) override
;
288 bool SoftwareMirroringEnabled() const override
;
291 // Sets/gets multi display mode.
292 void SetMultiDisplayMode(MultiDisplayMode mode
);
294 // Sets/gets default multi display mode.
295 void SetDefaultMultiDisplayMode(MultiDisplayMode mode
);
296 MultiDisplayMode
default_multi_display_mode() const {
297 return default_multi_display_mode_
;
300 // Reconfigure display configuration using the same
301 // physical display. TODO(oshima): Refactor and move this
302 // impl to |SetDefaultMultiDisplayMode|.
303 void ReconfigureDisplays();
305 // Update the bounds of the display given by |display_id|.
306 bool UpdateDisplayBounds(int64 display_id
,
307 const gfx::Rect
& new_bounds
);
309 // Creates mirror window asynchronously if the software mirror mode
311 void CreateMirrorWindowAsyncIfAny();
313 // Creates a MouseWarpController for the current display
314 // configuration. |drag_source| is the window where dragging
315 // started, or nullptr otherwise.
316 scoped_ptr
<MouseWarpController
> CreateMouseWarpController(
317 aura::Window
* drag_source
) const;
319 // Create a screen instance to be used during shutdown.
320 void CreateScreenForShutdown() const;
322 // A unit test may change the internal display id (which never happens on
323 // a real device). This will update the mode list for internal display
324 // for this test scenario.
325 void UpdateInternalDisplayModeListForTest();
328 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
329 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
330 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
331 NativeDisplaysChangedAfterPrimaryChange
);
332 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
333 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, Rotate
);
334 friend class AcceleratorControllerTest
;
335 friend class DisplayManagerTest
;
336 friend class test::AshTestBase
;
337 friend class test::DisplayManagerTestApi
;
338 friend class test::SystemGestureEventFilterTest
;
340 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
342 bool software_mirroring_enabled() const {
343 return multi_display_mode_
== MIRRORING
;
346 void set_change_display_upon_host_resize(bool value
) {
347 change_display_upon_host_resize_
= value
;
350 // Creates software mirroring display related information. The display
351 // used to mirror the content is removed from the |display_info_list|.
352 void CreateSoftwareMirroringDisplayInfo(DisplayInfoList
* display_info_list
);
354 gfx::Display
* FindDisplayForId(int64 id
);
356 // Add the mirror display's display info if the software based
357 // mirroring is in use.
358 void AddMirrorDisplayInfoIfAny(DisplayInfoList
* display_info_list
);
360 // Inserts and update the DisplayInfo according to the overscan
361 // state. Note that The DisplayInfo stored in the |internal_display_info_|
362 // can be different from |new_info| (due to overscan state), so
363 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
365 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
367 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
368 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
370 // Creates a display object from the DisplayInfo for |display_id|.
371 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
373 // Updates the bounds of all non-primary displays in |display_list| and
374 // append the indices of displays updated to |updated_indices|.
375 // When the size of |display_list| equals 2, the bounds are updated using
376 // the layout registered for the display pair. For more than 2 displays,
377 // the bounds are updated using horizontal layout.
378 // Returns true if any of the non-primary display's bounds has been changed
379 // from current value, or false otherwise.
380 bool UpdateNonPrimaryDisplayBoundsForLayout(
381 DisplayList
* display_list
, std::vector
<size_t>* updated_indices
) const;
383 void CreateMirrorWindowIfAny();
385 void RunPendingTasksForTest();
387 static void UpdateDisplayBoundsForLayout(
388 const DisplayLayout
& layout
,
389 const gfx::Display
& primary_display
,
390 gfx::Display
* secondary_display
);
392 Delegate
* delegate_
; // not owned.
394 scoped_ptr
<ScreenAsh
> screen_
;
396 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
398 int64 first_display_id_
;
400 // List of current active displays.
401 DisplayList active_display_list_
;
403 int num_connected_displays_
;
405 bool force_bounds_changed_
;
407 // The mapping from the display ID to its internal data.
408 std::map
<int64
, DisplayInfo
> display_info_
;
410 // Selected display modes for displays. Key is the displays' ID.
411 std::map
<int64
, DisplayMode
> display_modes_
;
413 // When set to true, the host window's resize event updates
414 // the display's size. This is set to true when running on
415 // desktop environment (for debugging) so that resizing the host
416 // window will update the display properly. This is set to false
417 // on device as well as during the unit tests.
418 bool change_display_upon_host_resize_
;
420 MultiDisplayMode multi_display_mode_
;
421 MultiDisplayMode default_multi_display_mode_
;
423 int64 mirroring_display_id_
;
424 DisplayList software_mirroring_display_list_
;
426 // User preference for rotation lock of the internal display.
427 bool registered_internal_display_rotation_lock_
;
429 // User preference for the rotation of the internal display.
430 gfx::Display::Rotation registered_internal_display_rotation_
;
432 base::WeakPtrFactory
<DisplayManager
> weak_ptr_factory_
;
434 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
439 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_