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 DisplayLayoutStore
;
42 class MouseWarpController
;
45 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
49 class DisplayManagerTestApi
;
50 class SystemGestureEventFilterTest
;
53 // DisplayManager maintains the current display configurations,
54 // and notifies observers when configuration changes.
56 // TODO(oshima): Make this non internal.
57 class ASH_EXPORT DisplayManager
58 #if defined(OS_CHROMEOS)
59 : public ui::DisplayConfigurator::SoftwareMirroringController
63 class ASH_EXPORT Delegate
{
65 virtual ~Delegate() {}
67 // Create or updates the mirroring window with |display_info_list|.
68 virtual void CreateOrUpdateMirroringDisplay(
69 const DisplayInfoList
& display_info_list
) = 0;
71 // Closes the mirror window if not necessary.
72 virtual void CloseMirroringDisplayIfNotNecessary() = 0;
74 // Called before and after the display configuration changes.
75 // When |clear_focus| is true, the implementation should
76 // deactivate the active window and set the focus window to NULL.
77 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
78 virtual void PostDisplayConfigurationChange() = 0;
81 typedef std::vector
<gfx::Display
> DisplayList
;
83 // How the second display will be used.
84 // 1) EXTENDED mode extends the desktop to the second dislpay.
85 // 2) MIRRORING mode copies the content of the primary display to
86 // the 2nd display. (Software Mirroring).
87 // 3) UNIFIED mode creates single desktop across multiple displays.
88 enum MultiDisplayMode
{
94 // The display ID for a virtual display assigned to a unified desktop.
95 static int64 kUnifiedDisplayId
;
98 #if defined(OS_CHROMEOS)
99 ~DisplayManager() override
;
101 virtual ~DisplayManager();
104 DisplayLayoutStore
* layout_store() {
105 return layout_store_
.get();
108 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
110 // When set to true, the DisplayManager calls OnDisplayMetricsChanged
111 // even if the display's bounds didn't change. Used to swap primary
113 void set_force_bounds_changed(bool force_bounds_changed
) {
114 force_bounds_changed_
= force_bounds_changed
;
117 // Returns the display id of the first display in the outupt list.
118 int64
first_display_id() const { return first_display_id_
; }
120 // Initializes displays using command line flag. Returns false
121 // if no command line flag was provided.
122 bool InitFromCommandLine();
124 // Initialize default display.
125 void InitDefaultDisplay();
127 // Initializes font related params that depends on display
129 void RefreshFontParams();
131 // Returns the display layout used for current displays.
132 DisplayLayout
GetCurrentDisplayLayout();
134 // Returns the current display pair.
135 DisplayIdPair
GetCurrentDisplayIdPair() const;
137 // Sets the layout for the current display pair. The |layout| specifies
138 // the locaion of the secondary display relative to the primary.
139 void SetLayoutForCurrentDisplays(
140 const DisplayLayout
& layout_relative_to_primary
);
142 // Returns display for given |id|;
143 const gfx::Display
& GetDisplayForId(int64 id
) const;
145 // Finds the display that contains |point| in screeen coordinates.
146 // Returns invalid display if there is no display that can satisfy
148 const gfx::Display
& FindDisplayContainingPoint(
149 const gfx::Point
& point_in_screen
) const;
151 // Sets the work area's |insets| to the display given by |display_id|.
152 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
154 // Registers the overscan insets for the display of the specified ID. Note
155 // that the insets size should be specified in DIP size. It also triggers the
156 // display's bounds change.
157 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
159 // Sets the display's rotation for the given |source|. The new |rotation| will
160 // also become active.
161 void SetDisplayRotation(int64 display_id
,
162 gfx::Display::Rotation rotation
,
163 gfx::Display::RotationSource source
);
165 // Sets the external display's configuration, including resolution change,
166 // ui-scale change, and device scale factor change. Returns true if it changes
167 // the display resolution so that the caller needs to show a notification in
168 // case the new resolution actually doesn't work.
169 bool SetDisplayMode(int64 display_id
, const DisplayMode
& display_mode
);
171 // Register per display properties. |overscan_insets| is NULL if
172 // the display has no custom overscan insets.
173 void RegisterDisplayProperty(int64 display_id
,
174 gfx::Display::Rotation rotation
,
176 const gfx::Insets
* overscan_insets
,
177 const gfx::Size
& resolution_in_pixels
,
178 float device_scale_factor
,
179 ui::ColorCalibrationProfile color_profile
);
181 // Register stored rotation properties for the internal display.
182 void RegisterDisplayRotationProperties(bool rotation_lock
,
183 gfx::Display::Rotation rotation
);
185 // Returns the stored rotation lock preference if it has been loaded,
187 bool registered_internal_display_rotation_lock() const {
188 return registered_internal_display_rotation_lock_
;
191 // Returns the stored rotation preference for the internal display if it has
192 // been loaded, otherwise |gfx::Display::Rotate_0|.
193 gfx::Display::Rotation
registered_internal_display_rotation() const {
194 return registered_internal_display_rotation_
;
197 // Returns the display mode of |display_id| which is currently used.
198 DisplayMode
GetActiveModeForDisplayId(int64 display_id
) const;
200 // Returns the display's selected mode. This returns false and doesn't
201 // set |mode_out| if the display mode is in default.
202 bool GetSelectedModeForDisplayId(int64 display_id
,
203 DisplayMode
* mode_out
) const;
205 // Tells if the virtual resolution feature is enabled.
206 bool IsDisplayUIScalingEnabled() const;
208 // Returns the current overscan insets for the specified |display_id|.
209 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
211 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
213 // Sets the color calibration of the display to |profile|.
214 void SetColorCalibrationProfile(int64 display_id
,
215 ui::ColorCalibrationProfile profile
);
217 // Called when display configuration has changed. The new display
218 // configurations is passed as a vector of Display object, which
219 // contains each display's new infomration.
220 void OnNativeDisplaysChanged(
221 const std::vector
<DisplayInfo
>& display_info_list
);
223 // Updates the internal display data and notifies observers about the changes.
224 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
226 // Updates current displays using current |display_info_|.
227 void UpdateDisplays();
229 // Returns the display at |index|. The display at 0 is
230 // no longer considered "primary".
231 const gfx::Display
& GetDisplayAt(size_t index
) const;
233 const gfx::Display
& GetPrimaryDisplayCandidate() const;
235 // Returns the logical number of displays. This returns 1
236 // when displays are mirrored.
237 size_t GetNumDisplays() const;
239 const DisplayList
& active_display_list() const {
240 return active_display_list_
;
243 // Returns the number of connected displays. This returns 2
244 // when displays are mirrored.
245 size_t num_connected_displays() const { return num_connected_displays_
; }
247 // Returns the mirroring status.
248 bool IsInMirrorMode() const;
249 int64
mirroring_display_id() const { return mirroring_display_id_
; }
250 const DisplayList
& software_mirroring_display_list() const {
251 return software_mirroring_display_list_
;
254 // Sets/gets if the unified desktop feature is enabled.
255 void SetUnifiedDesktopEnabled(bool enabled
);
256 bool unified_desktop_enabled() const { return unified_desktop_enabled_
; }
258 // Returns true if it's in unified desktop mode.
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 default multi display mode.
292 void SetDefaultMultiDisplayModeForCurrentDisplays(MultiDisplayMode mode
);
293 MultiDisplayMode
current_default_multi_display_mode() const {
294 return current_default_multi_display_mode_
;
297 // Sets multi display mode.
298 void SetMultiDisplayMode(MultiDisplayMode 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 chromeos::DisplayNotificationsTest
;
337 friend class test::AshTestBase
;
338 friend class test::DisplayManagerTestApi
;
339 friend class test::SystemGestureEventFilterTest
;
341 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
343 bool software_mirroring_enabled() const {
344 return multi_display_mode_
== MIRRORING
;
347 void set_change_display_upon_host_resize(bool value
) {
348 change_display_upon_host_resize_
= value
;
351 // Creates software mirroring display related information. The display
352 // used to mirror the content is removed from the |display_info_list|.
353 void CreateSoftwareMirroringDisplayInfo(DisplayInfoList
* display_info_list
);
355 gfx::Display
* FindDisplayForId(int64 id
);
357 // Add the mirror display's display info if the software based
358 // mirroring is in use.
359 void AddMirrorDisplayInfoIfAny(DisplayInfoList
* display_info_list
);
361 // Inserts and update the DisplayInfo according to the overscan
362 // state. Note that The DisplayInfo stored in the |internal_display_info_|
363 // can be different from |new_info| (due to overscan state), so
364 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
366 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
368 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
369 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
371 // Creates a display object from the DisplayInfo for |display_id|.
372 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
374 // Creates a display object from the DisplayInfo for |display_id| for
375 // mirroring. The size of the display will be scaled using |scale|
376 // with the offset using |origin|.
377 gfx::Display
CreateMirroringDisplayFromDisplayInfoById(
379 const gfx::Point
& origin
,
382 // Updates the bounds of all non-primary displays in |display_list| and
383 // append the indices of displays updated to |updated_indices|.
384 // When the size of |display_list| equals 2, the bounds are updated using
385 // the layout registered for the display pair. For more than 2 displays,
386 // the bounds are updated using horizontal layout.
387 // Returns true if any of the non-primary display's bounds has been changed
388 // from current value, or false otherwise.
389 bool UpdateNonPrimaryDisplayBoundsForLayout(
390 DisplayList
* display_list
, std::vector
<size_t>* updated_indices
) const;
392 void CreateMirrorWindowIfAny();
394 void RunPendingTasksForTest();
396 static void UpdateDisplayBoundsForLayout(
397 const DisplayLayout
& layout
,
398 const gfx::Display
& primary_display
,
399 gfx::Display
* secondary_display
);
401 Delegate
* delegate_
; // not owned.
403 scoped_ptr
<ScreenAsh
> screen_
;
405 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
407 int64 first_display_id_
;
409 // List of current active displays.
410 DisplayList active_display_list_
;
412 int num_connected_displays_
;
414 bool force_bounds_changed_
;
416 // The mapping from the display ID to its internal data.
417 std::map
<int64
, DisplayInfo
> display_info_
;
419 // Selected display modes for displays. Key is the displays' ID.
420 std::map
<int64
, DisplayMode
> display_modes_
;
422 // When set to true, the host window's resize event updates
423 // the display's size. This is set to true when running on
424 // desktop environment (for debugging) so that resizing the host
425 // window will update the display properly. This is set to false
426 // on device as well as during the unit tests.
427 bool change_display_upon_host_resize_
;
429 MultiDisplayMode multi_display_mode_
;
430 MultiDisplayMode current_default_multi_display_mode_
;
432 int64 mirroring_display_id_
;
433 DisplayList software_mirroring_display_list_
;
435 // User preference for rotation lock of the internal display.
436 bool registered_internal_display_rotation_lock_
;
438 // User preference for the rotation of the internal display.
439 gfx::Display::Rotation registered_internal_display_rotation_
;
441 bool unified_desktop_enabled_
;
443 base::WeakPtrFactory
<DisplayManager
> weak_ptr_factory_
;
445 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
450 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_