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 "ui/display/chromeos/output_configurator.h"
31 class AcceleratorControllerTest
;
32 class DisplayController
;
36 class DisplayManagerTestApi
;
37 class SystemGestureEventFilterTest
;
40 class DisplayLayoutStore
;
42 // DisplayManager maintains the current display configurations,
43 // and notifies observers when configuration changes.
45 // TODO(oshima): Make this non internal.
46 class ASH_EXPORT DisplayManager
47 #if defined(OS_CHROMEOS)
48 : public ui::OutputConfigurator::SoftwareMirroringController
52 class ASH_EXPORT Delegate
{
54 virtual ~Delegate() {}
56 // Create or updates the non desktop window with |display_info|.
57 virtual void CreateOrUpdateNonDesktopDisplay(
58 const DisplayInfo
& display_info
) = 0;
60 // Closes the mirror window if exists.
61 virtual void CloseNonDesktopDisplay() = 0;
63 // Called before and after the display configuration changes.
64 // When |clear_focus| is true, the implementation should
65 // deactivate the active window and set the focus window to NULL.
66 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
67 virtual void PostDisplayConfigurationChange() = 0;
70 // How the second display will be used.
71 // 1) EXTENDED mode extends the desktop to the second dislpay.
72 // 2) MIRRORING mode copies the content of the primary display to
73 // the 2nd display. (Software Mirroring).
74 // 3) In VIRTUAL_KEYBOARD mode, the 2nd display is used as a
75 // dedicated display for virtual keyboard, and it is not
76 // recognized as a part of desktop.
77 enum SecondDisplayMode
{
83 // Returns the list of possible UI scales for the display.
84 static std::vector
<float> GetScalesForDisplay(const DisplayInfo
& info
);
86 // Returns next valid UI scale.
87 static float GetNextUIScale(const DisplayInfo
& info
, bool up
);
89 // Updates the bounds of the display given by |secondary_display_id|
90 // according to |layout|.
91 static void UpdateDisplayBoundsForLayoutById(
92 const DisplayLayout
& layout
,
93 const gfx::Display
& primary_display
,
94 int64 secondary_display_id
);
97 virtual ~DisplayManager();
99 DisplayLayoutStore
* layout_store() {
100 return layout_store_
.get();
103 gfx::Screen
* screen() {
107 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
109 // When set to true, the MonitorManager calls OnDisplayBoundsChanged
110 // even if the display's bounds didn't change. Used to swap primary
112 void set_force_bounds_changed(bool force_bounds_changed
) {
113 force_bounds_changed_
= force_bounds_changed
;
116 // Returns the display id of the first display in the outupt list.
117 int64
first_display_id() const { return first_display_id_
; }
119 // Initializes displays using command line flag. Returns false
120 // if no command line flag was provided.
121 bool InitFromCommandLine();
123 // Initialize default display.
124 void InitDefaultDisplay();
126 // True if the given |display| is currently connected.
127 bool IsActiveDisplay(const gfx::Display
& display
) const;
129 // True if there is an internal display.
130 bool HasInternalDisplay() const;
132 bool IsInternalDisplayId(int64 id
) const;
134 // Returns the display layout used for current displays.
135 DisplayLayout
GetCurrentDisplayLayout();
137 // Returns the current display pair.
138 DisplayIdPair
GetCurrentDisplayIdPair() const;
140 // Sets the layout for the current display pair. The |layout| specifies
141 // the locaion of the secondary display relative to the primary.
142 void SetLayoutForCurrentDisplays(
143 const DisplayLayout
& layout_relative_to_primary
);
145 // Returns display for given |id|;
146 const gfx::Display
& GetDisplayForId(int64 id
) const;
148 // Finds the display that contains |point| in screeen coordinates.
149 // Returns invalid display if there is no display that can satisfy
151 const gfx::Display
& FindDisplayContainingPoint(
152 const gfx::Point
& point_in_screen
) const;
154 // Sets the work area's |insets| to the display given by |display_id|.
155 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
157 // Registers the overscan insets for the display of the specified ID. Note
158 // that the insets size should be specified in DIP size. It also triggers the
159 // display's bounds change.
160 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
162 // Sets the display's rotation.
163 void SetDisplayRotation(int64 display_id
, gfx::Display::Rotation rotation
);
165 // Sets the display's ui scale.
166 void SetDisplayUIScale(int64 display_id
, float ui_scale
);
168 // Sets the display's resolution.
169 void SetDisplayResolution(int64 display_id
, const gfx::Size
& resolution
);
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 ui::ColorCalibrationProfile color_profile
);
180 // Returns the display's selected mode.
181 bool GetSelectedModeForDisplayId(int64 display_id
,
182 DisplayMode
* mode_out
) const;
184 // Tells if the virtual resolution feature is enabled.
185 bool IsDisplayUIScalingEnabled() const;
187 // Returns the current overscan insets for the specified |display_id|.
188 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
190 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
192 // Sets the color calibration of the display to |profile|.
193 void SetColorCalibrationProfile(int64 display_id
,
194 ui::ColorCalibrationProfile profile
);
196 // Called when display configuration has changed. The new display
197 // configurations is passed as a vector of Display object, which
198 // contains each display's new infomration.
199 void OnNativeDisplaysChanged(
200 const std::vector
<DisplayInfo
>& display_info_list
);
202 // Updates the internal display data and notifies observers about the changes.
203 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
205 // Updates current displays using current |display_info_|.
206 void UpdateDisplays();
208 // Returns the display at |index|. The display at 0 is
209 // no longer considered "primary".
210 const gfx::Display
& GetDisplayAt(size_t index
) const;
212 const gfx::Display
& GetPrimaryDisplayCandidate() const;
214 // Returns the logical number of displays. This returns 1
215 // when displays are mirrored.
216 size_t GetNumDisplays() const;
218 const std::vector
<gfx::Display
>& displays() const { return displays_
; }
220 // Returns the number of connected displays. This returns 2
221 // when displays are mirrored.
222 size_t num_connected_displays() const { return num_connected_displays_
; }
224 // Returns the mirroring status.
225 bool IsMirrored() const;
226 int64
mirrored_display_id() const { return mirrored_display_id_
; }
228 // Returns the display object that is not a part of desktop.
229 const gfx::Display
& non_desktop_display() const {
230 return non_desktop_display_
;
233 // Retuns the display info associated with |display_id|.
234 const DisplayInfo
& GetDisplayInfo(int64 display_id
) const;
236 // Returns the human-readable name for the display |id|.
237 std::string
GetDisplayNameForId(int64 id
);
239 // Returns the display id that is capable of UI scaling. On device,
240 // this returns internal display's ID if its device scale factor is 2,
241 // or invalid ID if such internal display doesn't exist. On linux
242 // desktop, this returns the first display ID.
243 int64
GetDisplayIdForUIScaling() const;
245 // Change the mirror mode.
246 void SetMirrorMode(bool mirrored
);
248 // Used to emulate display change when run in a desktop environment instead
250 void AddRemoveDisplay();
251 void ToggleDisplayScaleFactor();
253 // SoftwareMirroringController override:
254 #if defined(OS_CHROMEOS)
255 virtual void SetSoftwareMirroring(bool enabled
) OVERRIDE
;
257 bool software_mirroring_enabled() const {
258 return second_display_mode_
== MIRRORING
;
261 bool virtual_keyboard_root_window_enabled() const {
262 return second_display_mode_
== VIRTUAL_KEYBOARD
;
265 // Sets/gets second display mode.
266 void SetSecondDisplayMode(SecondDisplayMode mode
);
267 SecondDisplayMode
second_display_mode() const {
268 return second_display_mode_
;
271 // Update the bounds of the display given by |display_id|.
272 bool UpdateDisplayBounds(int64 display_id
,
273 const gfx::Rect
& new_bounds
);
275 // Creates mirror window if the software mirror mode is enabled.
276 // This is used only for bootstrap.
277 void CreateMirrorWindowIfAny();
279 // Create a screen instance to be used during shutdown.
280 void CreateScreenForShutdown() const;
283 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
284 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
285 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
286 NativeDisplaysChangedAfterPrimaryChange
);
287 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
288 friend class ash::AcceleratorControllerTest
;
289 friend class test::DisplayManagerTestApi
;
290 friend class test::SystemGestureEventFilterTest
;
291 friend class DisplayManagerTest
;
293 typedef std::vector
<gfx::Display
> DisplayList
;
295 void set_change_display_upon_host_resize(bool value
) {
296 change_display_upon_host_resize_
= value
;
299 gfx::Display
* FindDisplayForId(int64 id
);
301 // Add the mirror display's display info if the software based
302 // mirroring is in use.
303 void AddMirrorDisplayInfoIfAny(std::vector
<DisplayInfo
>* display_info_list
);
305 // Inserts and update the DisplayInfo according to the overscan
306 // state. Note that The DisplayInfo stored in the |internal_display_info_|
307 // can be different from |new_info| (due to overscan state), so
308 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
310 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
312 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
313 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
315 // Creates a display object from the DisplayInfo for |display_id|.
316 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
318 // Updates the bounds of the secondary display in |display_list|
319 // using the layout registered for the display pair and set the
320 // index of display updated to |updated_index|. Returns true
321 // if the secondary display's bounds has been changed from current
322 // value, or false otherwise.
323 bool UpdateSecondaryDisplayBoundsForLayout(DisplayList
* display_list
,
324 size_t* updated_index
) const;
326 static void UpdateDisplayBoundsForLayout(
327 const DisplayLayout
& layout
,
328 const gfx::Display
& primary_display
,
329 gfx::Display
* secondary_display
);
331 Delegate
* delegate_
; // not owned.
333 scoped_ptr
<ScreenAsh
> screen_ash_
;
334 // This is to have an accessor without ScreenAsh definition.
335 gfx::Screen
* screen_
;
337 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
339 int64 first_display_id_
;
341 // List of current active displays.
342 DisplayList displays_
;
344 int num_connected_displays_
;
346 bool force_bounds_changed_
;
348 // The mapping from the display ID to its internal data.
349 std::map
<int64
, DisplayInfo
> display_info_
;
351 // Selected display modes for displays. Key is the displays' ID.
352 std::map
<int64
, DisplayMode
> display_modes_
;
354 // When set to true, the host window's resize event updates
355 // the display's size. This is set to true when running on
356 // desktop environment (for debugging) so that resizing the host
357 // window will update the display properly. This is set to false
358 // on device as well as during the unit tests.
359 bool change_display_upon_host_resize_
;
361 SecondDisplayMode second_display_mode_
;
362 int64 mirrored_display_id_
;
363 gfx::Display non_desktop_display_
;
365 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
368 } // namespace internal
371 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_