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/display_configurator.h"
31 class AcceleratorControllerTest
;
32 class DisplayController
;
33 class DisplayLayoutStore
;
37 class DisplayManagerTestApi
;
38 class SystemGestureEventFilterTest
;
41 // DisplayManager maintains the current display configurations,
42 // and notifies observers when configuration changes.
44 // TODO(oshima): Make this non internal.
45 class ASH_EXPORT DisplayManager
46 #if defined(OS_CHROMEOS)
47 : public ui::DisplayConfigurator::SoftwareMirroringController
51 class ASH_EXPORT Delegate
{
53 virtual ~Delegate() {}
55 // Create or updates the non desktop window with |display_info|.
56 virtual void CreateOrUpdateNonDesktopDisplay(
57 const DisplayInfo
& display_info
) = 0;
59 // Closes the mirror window if exists.
60 virtual void CloseNonDesktopDisplay() = 0;
62 // Called before and after the display configuration changes.
63 // When |clear_focus| is true, the implementation should
64 // deactivate the active window and set the focus window to NULL.
65 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
66 virtual void PostDisplayConfigurationChange() = 0;
69 // How the second display will be used.
70 // 1) EXTENDED mode extends the desktop to the second dislpay.
71 // 2) MIRRORING mode copies the content of the primary display to
72 // the 2nd display. (Software Mirroring).
73 // 3) In VIRTUAL_KEYBOARD mode, the 2nd display is used as a
74 // dedicated display for virtual keyboard, and it is not
75 // recognized as a part of desktop.
76 enum SecondDisplayMode
{
82 // Returns the list of possible UI scales for the display.
83 static std::vector
<float> GetScalesForDisplay(const DisplayInfo
& info
);
85 // Returns next valid UI scale.
86 static float GetNextUIScale(const DisplayInfo
& info
, bool up
);
88 // Updates the bounds of the display given by |secondary_display_id|
89 // according to |layout|.
90 static void UpdateDisplayBoundsForLayoutById(
91 const DisplayLayout
& layout
,
92 const gfx::Display
& primary_display
,
93 int64 secondary_display_id
);
96 virtual ~DisplayManager();
98 DisplayLayoutStore
* layout_store() {
99 return layout_store_
.get();
102 gfx::Screen
* screen() {
106 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
108 // When set to true, the MonitorManager calls OnDisplayMetricsChanged
109 // even if the display's bounds didn't change. Used to swap primary
111 void set_force_bounds_changed(bool force_bounds_changed
) {
112 force_bounds_changed_
= force_bounds_changed
;
115 // Returns the display id of the first display in the outupt list.
116 int64
first_display_id() const { return first_display_id_
; }
118 // Initializes displays using command line flag. Returns false
119 // if no command line flag was provided.
120 bool InitFromCommandLine();
122 // Initialize default display.
123 void InitDefaultDisplay();
125 // Initializes font related params that depends on display
127 void InitFontParams();
129 // True if the given |display| is currently connected.
130 bool IsActiveDisplay(const gfx::Display
& display
) const;
132 // True if there is an internal display.
133 bool HasInternalDisplay() const;
135 bool IsInternalDisplayId(int64 id
) const;
137 // Returns the display layout used for current displays.
138 DisplayLayout
GetCurrentDisplayLayout();
140 // Returns the current display pair.
141 DisplayIdPair
GetCurrentDisplayIdPair() const;
143 // Sets the layout for the current display pair. The |layout| specifies
144 // the locaion of the secondary display relative to the primary.
145 void SetLayoutForCurrentDisplays(
146 const DisplayLayout
& layout_relative_to_primary
);
148 // Returns display for given |id|;
149 const gfx::Display
& GetDisplayForId(int64 id
) const;
151 // Finds the display that contains |point| in screeen coordinates.
152 // Returns invalid display if there is no display that can satisfy
154 const gfx::Display
& FindDisplayContainingPoint(
155 const gfx::Point
& point_in_screen
) const;
157 // Sets the work area's |insets| to the display given by |display_id|.
158 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
160 // Registers the overscan insets for the display of the specified ID. Note
161 // that the insets size should be specified in DIP size. It also triggers the
162 // display's bounds change.
163 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
165 // Sets the display's rotation.
166 void SetDisplayRotation(int64 display_id
, gfx::Display::Rotation rotation
);
168 // Sets the display's ui scale.
169 // TODO(mukai): remove this and merge into SetDisplayMode.
170 void SetDisplayUIScale(int64 display_id
, float ui_scale
);
172 // Sets the display's resolution.
173 // TODO(mukai): remove this and merge into SetDisplayMode.
174 void SetDisplayResolution(int64 display_id
, const gfx::Size
& resolution
);
176 // Sets the external display's configuration, including resolution change,
177 // ui-scale change, and device scale factor change. Returns true if it changes
178 // the display resolution so that the caller needs to show a notification in
179 // case the new resolution actually doesn't work.
180 bool SetDisplayMode(int64 display_id
, const DisplayMode
& display_mode
);
182 // Register per display properties. |overscan_insets| is NULL if
183 // the display has no custom overscan insets.
184 void RegisterDisplayProperty(int64 display_id
,
185 gfx::Display::Rotation rotation
,
187 const gfx::Insets
* overscan_insets
,
188 const gfx::Size
& resolution_in_pixels
,
189 float device_scale_factor
,
190 ui::ColorCalibrationProfile color_profile
);
192 // Register stored rotation properties for the internal display.
193 void RegisterDisplayRotationProperties(bool rotation_lock
,
194 gfx::Display::Rotation rotation
);
196 // Returns the stored rotation lock preference if it has been loaded,
198 bool registered_internal_display_rotation_lock() const {
199 return registered_internal_display_rotation_lock_
;
202 // Returns the stored rotation preference for the internal display if it has
203 // been loaded, otherwise |gfx::Display::Rotate_0|.
204 gfx::Display::Rotation
registered_internal_display_rotation() const {
205 return registered_internal_display_rotation_
;
208 // Returns the display mode of |display_id| which is currently used.
209 DisplayMode
GetActiveModeForDisplayId(int64 display_id
) const;
211 // Returns the display's selected mode. This returns false and doesn't
212 // set |mode_out| if the display mode is in default.
213 bool GetSelectedModeForDisplayId(int64 display_id
,
214 DisplayMode
* mode_out
) const;
216 // Tells if the virtual resolution feature is enabled.
217 bool IsDisplayUIScalingEnabled() const;
219 // Returns the current overscan insets for the specified |display_id|.
220 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
222 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
224 // Sets the color calibration of the display to |profile|.
225 void SetColorCalibrationProfile(int64 display_id
,
226 ui::ColorCalibrationProfile profile
);
228 // Called when display configuration has changed. The new display
229 // configurations is passed as a vector of Display object, which
230 // contains each display's new infomration.
231 void OnNativeDisplaysChanged(
232 const std::vector
<DisplayInfo
>& display_info_list
);
234 // Updates the internal display data and notifies observers about the changes.
235 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
237 // Updates current displays using current |display_info_|.
238 void UpdateDisplays();
240 // Returns the display at |index|. The display at 0 is
241 // no longer considered "primary".
242 const gfx::Display
& GetDisplayAt(size_t index
) const;
244 const gfx::Display
& GetPrimaryDisplayCandidate() const;
246 // Returns the logical number of displays. This returns 1
247 // when displays are mirrored.
248 size_t GetNumDisplays() const;
250 const std::vector
<gfx::Display
>& displays() const { return displays_
; }
252 // Returns the number of connected displays. This returns 2
253 // when displays are mirrored.
254 size_t num_connected_displays() const { return num_connected_displays_
; }
256 // Returns the mirroring status.
257 bool IsMirrored() const;
258 int64
mirrored_display_id() const { return mirrored_display_id_
; }
260 // Returns the display object that is not a part of desktop.
261 const gfx::Display
& non_desktop_display() const {
262 return non_desktop_display_
;
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 virtual void SetSoftwareMirroring(bool enabled
) OVERRIDE
;
288 virtual bool SoftwareMirroringEnabled() const OVERRIDE
;
290 bool software_mirroring_enabled() const {
291 return second_display_mode_
== MIRRORING
;
294 bool virtual_keyboard_root_window_enabled() const {
295 return second_display_mode_
== VIRTUAL_KEYBOARD
;
298 // Sets/gets second display mode.
299 void SetSecondDisplayMode(SecondDisplayMode mode
);
300 SecondDisplayMode
second_display_mode() const {
301 return second_display_mode_
;
304 // Update the bounds of the display given by |display_id|.
305 bool UpdateDisplayBounds(int64 display_id
,
306 const gfx::Rect
& new_bounds
);
308 // Creates mirror window if the software mirror mode is enabled.
309 // This is used only for bootstrap.
310 void CreateMirrorWindowIfAny();
312 // Create a screen instance to be used during shutdown.
313 void CreateScreenForShutdown() const;
316 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
317 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
318 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
319 NativeDisplaysChangedAfterPrimaryChange
);
320 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
321 friend class ash::AcceleratorControllerTest
;
322 friend class test::DisplayManagerTestApi
;
323 friend class test::SystemGestureEventFilterTest
;
324 friend class DisplayManagerTest
;
326 typedef std::vector
<gfx::Display
> DisplayList
;
328 void set_change_display_upon_host_resize(bool value
) {
329 change_display_upon_host_resize_
= value
;
332 gfx::Display
* FindDisplayForId(int64 id
);
334 // Add the mirror display's display info if the software based
335 // mirroring is in use.
336 void AddMirrorDisplayInfoIfAny(std::vector
<DisplayInfo
>* display_info_list
);
338 // Inserts and update the DisplayInfo according to the overscan
339 // state. Note that The DisplayInfo stored in the |internal_display_info_|
340 // can be different from |new_info| (due to overscan state), so
341 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
343 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
345 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
346 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
348 // Creates a display object from the DisplayInfo for |display_id|.
349 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
351 // Updates the bounds of the secondary display in |display_list|
352 // using the layout registered for the display pair and set the
353 // index of display updated to |updated_index|. Returns true
354 // if the secondary display's bounds has been changed from current
355 // value, or false otherwise.
356 bool UpdateSecondaryDisplayBoundsForLayout(DisplayList
* display_list
,
357 size_t* updated_index
) const;
359 static void UpdateDisplayBoundsForLayout(
360 const DisplayLayout
& layout
,
361 const gfx::Display
& primary_display
,
362 gfx::Display
* secondary_display
);
364 Delegate
* delegate_
; // not owned.
366 scoped_ptr
<ScreenAsh
> screen_ash_
;
367 // This is to have an accessor without ScreenAsh definition.
368 gfx::Screen
* screen_
;
370 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
372 int64 first_display_id_
;
374 // List of current active displays.
375 DisplayList displays_
;
377 int num_connected_displays_
;
379 bool force_bounds_changed_
;
381 // The mapping from the display ID to its internal data.
382 std::map
<int64
, DisplayInfo
> display_info_
;
384 // Selected display modes for displays. Key is the displays' ID.
385 std::map
<int64
, DisplayMode
> display_modes_
;
387 // When set to true, the host window's resize event updates
388 // the display's size. This is set to true when running on
389 // desktop environment (for debugging) so that resizing the host
390 // window will update the display properly. This is set to false
391 // on device as well as during the unit tests.
392 bool change_display_upon_host_resize_
;
394 SecondDisplayMode second_display_mode_
;
395 int64 mirrored_display_id_
;
396 gfx::Display non_desktop_display_
;
398 // User preference for rotation lock of the internal display.
399 bool registered_internal_display_rotation_lock_
;
401 // User preference for the rotation of the internal display.
402 gfx::Display::Rotation registered_internal_display_rotation_
;
404 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
409 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_