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 "ui/gfx/display.h"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/display/output_configurator.h"
30 class AcceleratorControllerTest
;
31 class DisplayController
;
34 class DisplayManagerTestApi
;
35 class SystemGestureEventFilterTest
;
38 class DisplayLayoutStore
;
40 // DisplayManager maintains the current display configurations,
41 // and notifies observers when configuration changes.
43 // TODO(oshima): Make this non internal.
44 class ASH_EXPORT DisplayManager
45 #if defined(OS_CHROMEOS)
46 : public chromeos::OutputConfigurator::SoftwareMirroringController
50 class ASH_EXPORT Delegate
{
52 virtual ~Delegate() {}
54 // Create or updates the mirror window with |display_info|.
55 virtual void CreateOrUpdateMirrorWindow(
56 const DisplayInfo
& display_info
) = 0;
58 // Closes the mirror window if exists.
59 virtual void CloseMirrorWindow() = 0;
61 // Called before and after the display configuration changes.
62 virtual void PreDisplayConfigurationChange() = 0;
63 virtual void PostDisplayConfigurationChange() = 0;
66 // Returns the list of possible UI scales for the display.
67 static std::vector
<float> GetScalesForDisplay(const DisplayInfo
& info
);
69 // Returns next valid UI scale.
70 static float GetNextUIScale(const DisplayInfo
& info
, bool up
);
72 // Updates the bounds of the display given by |secondary_display_id|
73 // according to |layout|.
74 static void UpdateDisplayBoundsForLayoutById(
75 const DisplayLayout
& layout
,
76 const gfx::Display
& primary_display
,
77 int64 secondary_display_id
);
80 virtual ~DisplayManager();
82 DisplayLayoutStore
* layout_store() {
83 return layout_store_
.get();
86 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
88 // When set to true, the MonitorManager calls OnDisplayBoundsChanged
89 // even if the display's bounds didn't change. Used to swap primary
91 void set_force_bounds_changed(bool force_bounds_changed
) {
92 force_bounds_changed_
= force_bounds_changed
;
95 // Returns the display id of the first display in the outupt list.
96 int64
first_display_id() const { return first_display_id_
; }
98 // Initializes displays using command line flag, or uses
99 // defualt if no options are specified.
100 void InitFromCommandLine();
102 // True if the given |display| is currently connected.
103 bool IsActiveDisplay(const gfx::Display
& display
) const;
105 // True if there is an internal display.
106 bool HasInternalDisplay() const;
108 bool IsInternalDisplayId(int64 id
) const;
110 // Returns display for given |id|;
111 const gfx::Display
& GetDisplayForId(int64 id
) const;
113 // Finds the display that contains |point| in screeen coordinates.
114 // Returns invalid display if there is no display that can satisfy
116 const gfx::Display
& FindDisplayContainingPoint(
117 const gfx::Point
& point_in_screen
) const;
119 // Sets the work area's |insets| to the display given by |display_id|.
120 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
122 // Registers the overscan insets for the display of the specified ID. Note
123 // that the insets size should be specified in DIP size. It also triggers the
124 // display's bounds change.
125 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
127 // Sets the display's rotation.
128 void SetDisplayRotation(int64 display_id
, gfx::Display::Rotation rotation
);
130 // Sets the display's ui scale.
131 void SetDisplayUIScale(int64 display_id
, float ui_scale
);
133 // Register per display properties. |overscan_insets| is NULL if
134 // the display has no custom overscan insets.
135 void RegisterDisplayProperty(int64 display_id
,
136 gfx::Display::Rotation rotation
,
138 const gfx::Insets
* overscan_insets
);
140 // Tells if display rotation/ui scaling features are enabled.
141 bool IsDisplayRotationEnabled() const;
142 bool IsDisplayUIScalingEnabled() const;
144 // Returns the current overscan insets for the specified |display_id|.
145 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
147 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
149 // Called when display configuration has changed. The new display
150 // configurations is passed as a vector of Display object, which
151 // contains each display's new infomration.
152 void OnNativeDisplaysChanged(
153 const std::vector
<DisplayInfo
>& display_info_list
);
155 // Updates the internal display data and notifies observers about the changes.
156 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
158 // Updates current displays using current |display_info_|.
159 void UpdateDisplays();
161 // Returns the display at |index|. The display at 0 is
162 // no longer considered "primary".
163 const gfx::Display
& GetDisplayAt(size_t index
) const;
165 const gfx::Display
* GetPrimaryDisplayCandidate() const;
167 // Returns the logical number of displays. This returns 1
168 // when displays are mirrored.
169 size_t GetNumDisplays() const;
171 // Returns the number of connected displays. This returns 2
172 // when displays are mirrored.
173 size_t num_connected_displays() const { return num_connected_displays_
; }
175 // Returns the mirroring status.
176 bool IsMirrored() const;
177 const gfx::Display
& mirrored_display() const { return mirrored_display_
; }
179 // Retuns the display info associated with |display_id|.
180 const DisplayInfo
& GetDisplayInfo(int64 display_id
) const;
182 // Returns the human-readable name for the display |id|.
183 std::string
GetDisplayNameForId(int64 id
);
185 // Returns the display id that is capable of UI scaling. On device,
186 // this returns internal display's ID if its device scale factor is 2,
187 // or invalid ID if such internal display doesn't exist. On linux
188 // desktop, this returns the first display ID.
189 int64
GetDisplayIdForUIScaling() const;
191 // Change the mirror mode.
192 void SetMirrorMode(bool mirrored
);
194 // Used to emulate display change when run in a desktop environment instead
196 void AddRemoveDisplay();
197 void ToggleDisplayScaleFactor();
199 // SoftwareMirroringController override:
200 #if defined(OS_CHROMEOS)
201 virtual void SetSoftwareMirroring(bool enabled
) OVERRIDE
;
203 void SetSoftwareMirroring(bool enabled
);
206 // Update the bounds of the display given by |display_id|.
207 bool UpdateDisplayBounds(int64 display_id
,
208 const gfx::Rect
& new_bounds
);
210 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
211 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
212 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
213 NativeDisplaysChangedAfterPrimaryChange
);
214 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
215 friend class ash::AcceleratorControllerTest
;
216 friend class test::DisplayManagerTestApi
;
217 friend class test::SystemGestureEventFilterTest
;
218 friend class DisplayManagerTest
;
220 typedef std::vector
<gfx::Display
> DisplayList
;
222 void set_change_display_upon_host_resize(bool value
) {
223 change_display_upon_host_resize_
= value
;
226 gfx::Display
* FindDisplayForId(int64 id
);
228 // Add the mirror display's display info if the software based
229 // mirroring is in use.
230 void AddMirrorDisplayInfoIfAny(std::vector
<DisplayInfo
>* display_info_list
);
232 // Inserts and update the DisplayInfo according to the overscan
233 // state. Note that The DisplayInfo stored in the |internal_display_info_|
234 // can be different from |new_info| (due to overscan state), so
235 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
237 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
239 // Creates a display object from the DisplayInfo for |display_id|.
240 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
242 // Updates the bounds of the secondary display in |display_list|
243 // using the layout registered for the display pair and set the
244 // index of display updated to |updated_index|. Returns true
245 // if the secondary display's bounds has been changed from current
246 // value, or false otherwise.
247 bool UpdateSecondaryDisplayBoundsForLayout(DisplayList
* display_list
,
248 size_t* updated_index
) const;
250 static void UpdateDisplayBoundsForLayout(
251 const DisplayLayout
& layout
,
252 const gfx::Display
& primary_display
,
253 gfx::Display
* secondary_display
);
255 Delegate
* delegate_
; // not owned.
257 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
259 int64 first_display_id_
;
261 gfx::Display mirrored_display_
;
263 // List of current active dispays.
264 DisplayList displays_
;
266 int num_connected_displays_
;
268 bool force_bounds_changed_
;
270 // The mapping from the display ID to its internal data.
271 std::map
<int64
, DisplayInfo
> display_info_
;
273 // When set to true, the host window's resize event updates
274 // the display's size. This is set to true when running on
275 // desktop environment (for debugging) so that resizing the host
276 // window wil update the display properly. This is set to false
277 // on device as well as during the unit tests.
278 bool change_display_upon_host_resize_
;
280 bool software_mirroring_enabled_
;
282 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
285 } // namespace internal
288 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_