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"
32 class AcceleratorControllerTest
;
33 class DisplayController
;
34 class DisplayLayoutStore
;
39 class DisplayManagerTestApi
;
40 class SystemGestureEventFilterTest
;
43 // DisplayManager maintains the current display configurations,
44 // and notifies observers when configuration changes.
46 // TODO(oshima): Make this non internal.
47 class ASH_EXPORT DisplayManager
48 #if defined(OS_CHROMEOS)
49 : public ui::DisplayConfigurator::SoftwareMirroringController
53 class ASH_EXPORT Delegate
{
55 virtual ~Delegate() {}
57 // Create or updates the mirroring window with |display_info|.
58 virtual void CreateOrUpdateMirroringDisplay(
59 const DisplayInfo
& display_info
) = 0;
61 // Closes the mirror window if exists.
62 virtual void CloseMirroringDisplay() = 0;
64 // Called before and after the display configuration changes.
65 // When |clear_focus| is true, the implementation should
66 // deactivate the active window and set the focus window to NULL.
67 virtual void PreDisplayConfigurationChange(bool clear_focus
) = 0;
68 virtual void PostDisplayConfigurationChange() = 0;
71 // How the second display will be used.
72 // 1) EXTENDED mode extends the desktop to the second dislpay.
73 // 2) MIRRORING mode copies the content of the primary display to
74 // the 2nd display. (Software Mirroring).
75 enum SecondDisplayMode
{
80 // Returns the list of possible UI scales for the display.
81 static std::vector
<float> GetScalesForDisplay(const DisplayInfo
& info
);
83 // Returns next valid UI scale.
84 static float GetNextUIScale(const DisplayInfo
& info
, bool up
);
86 // Updates the bounds of the display given by |secondary_display_id|
87 // according to |layout|.
88 static void UpdateDisplayBoundsForLayoutById(
89 const DisplayLayout
& layout
,
90 const gfx::Display
& primary_display
,
91 int64 secondary_display_id
);
94 virtual ~DisplayManager();
96 DisplayLayoutStore
* layout_store() {
97 return layout_store_
.get();
100 gfx::Screen
* screen() {
104 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
106 // When set to true, the MonitorManager calls OnDisplayMetricsChanged
107 // even if the display's bounds didn't change. Used to swap primary
109 void set_force_bounds_changed(bool force_bounds_changed
) {
110 force_bounds_changed_
= force_bounds_changed
;
113 // Returns the display id of the first display in the outupt list.
114 int64
first_display_id() const { return first_display_id_
; }
116 // Initializes displays using command line flag. Returns false
117 // if no command line flag was provided.
118 bool InitFromCommandLine();
120 // Initialize default display.
121 void InitDefaultDisplay();
123 // Initializes font related params that depends on display
125 void InitFontParams();
127 // True if the given |display| is currently connected.
128 bool IsActiveDisplay(const gfx::Display
& display
) const;
130 // True if there is an internal display.
131 bool HasInternalDisplay() const;
133 bool IsInternalDisplayId(int64 id
) const;
135 // Returns the display layout used for current displays.
136 DisplayLayout
GetCurrentDisplayLayout();
138 // Returns the current display pair.
139 DisplayIdPair
GetCurrentDisplayIdPair() const;
141 // Sets the layout for the current display pair. The |layout| specifies
142 // the locaion of the secondary display relative to the primary.
143 void SetLayoutForCurrentDisplays(
144 const DisplayLayout
& layout_relative_to_primary
);
146 // Returns display for given |id|;
147 const gfx::Display
& GetDisplayForId(int64 id
) const;
149 // Finds the display that contains |point| in screeen coordinates.
150 // Returns invalid display if there is no display that can satisfy
152 const gfx::Display
& FindDisplayContainingPoint(
153 const gfx::Point
& point_in_screen
) const;
155 // Sets the work area's |insets| to the display given by |display_id|.
156 bool UpdateWorkAreaOfDisplay(int64 display_id
, const gfx::Insets
& insets
);
158 // Registers the overscan insets for the display of the specified ID. Note
159 // that the insets size should be specified in DIP size. It also triggers the
160 // display's bounds change.
161 void SetOverscanInsets(int64 display_id
, const gfx::Insets
& insets_in_dip
);
163 // Sets the display's rotation.
164 void SetDisplayRotation(int64 display_id
, gfx::Display::Rotation rotation
);
166 // Sets the display's ui scale.
167 // TODO(mukai): remove this and merge into SetDisplayMode.
168 void SetDisplayUIScale(int64 display_id
, float ui_scale
);
170 // Sets the display's resolution.
171 // TODO(mukai): remove this and merge into SetDisplayMode.
172 void SetDisplayResolution(int64 display_id
, const gfx::Size
& resolution
);
174 // Sets the external display's configuration, including resolution change,
175 // ui-scale change, and device scale factor change. Returns true if it changes
176 // the display resolution so that the caller needs to show a notification in
177 // case the new resolution actually doesn't work.
178 bool SetDisplayMode(int64 display_id
, const DisplayMode
& display_mode
);
180 // Register per display properties. |overscan_insets| is NULL if
181 // the display has no custom overscan insets.
182 void RegisterDisplayProperty(int64 display_id
,
183 gfx::Display::Rotation rotation
,
185 const gfx::Insets
* overscan_insets
,
186 const gfx::Size
& resolution_in_pixels
,
187 float device_scale_factor
,
188 ui::ColorCalibrationProfile color_profile
);
190 // Register stored rotation properties for the internal display.
191 void RegisterDisplayRotationProperties(bool rotation_lock
,
192 gfx::Display::Rotation rotation
);
194 // Returns the stored rotation lock preference if it has been loaded,
196 bool registered_internal_display_rotation_lock() const {
197 return registered_internal_display_rotation_lock_
;
200 // Returns the stored rotation preference for the internal display if it has
201 // been loaded, otherwise |gfx::Display::Rotate_0|.
202 gfx::Display::Rotation
registered_internal_display_rotation() const {
203 return registered_internal_display_rotation_
;
206 // Returns the display mode of |display_id| which is currently used.
207 DisplayMode
GetActiveModeForDisplayId(int64 display_id
) const;
209 // Returns the display's selected mode. This returns false and doesn't
210 // set |mode_out| if the display mode is in default.
211 bool GetSelectedModeForDisplayId(int64 display_id
,
212 DisplayMode
* mode_out
) const;
214 // Tells if the virtual resolution feature is enabled.
215 bool IsDisplayUIScalingEnabled() const;
217 // Returns the current overscan insets for the specified |display_id|.
218 // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
220 gfx::Insets
GetOverscanInsets(int64 display_id
) const;
222 // Sets the color calibration of the display to |profile|.
223 void SetColorCalibrationProfile(int64 display_id
,
224 ui::ColorCalibrationProfile profile
);
226 // Called when display configuration has changed. The new display
227 // configurations is passed as a vector of Display object, which
228 // contains each display's new infomration.
229 void OnNativeDisplaysChanged(
230 const std::vector
<DisplayInfo
>& display_info_list
);
232 // Updates the internal display data and notifies observers about the changes.
233 void UpdateDisplays(const std::vector
<DisplayInfo
>& display_info_list
);
235 // Updates current displays using current |display_info_|.
236 void UpdateDisplays();
238 // Returns the display at |index|. The display at 0 is
239 // no longer considered "primary".
240 const gfx::Display
& GetDisplayAt(size_t index
) const;
242 const gfx::Display
& GetPrimaryDisplayCandidate() const;
244 // Returns the logical number of displays. This returns 1
245 // when displays are mirrored.
246 size_t GetNumDisplays() const;
248 const std::vector
<gfx::Display
>& displays() const { return displays_
; }
250 // Returns the number of connected displays. This returns 2
251 // when displays are mirrored.
252 size_t num_connected_displays() const { return num_connected_displays_
; }
254 // Returns the mirroring status.
255 bool IsMirrored() const;
256 int64
mirrored_display_id() const { return mirrored_display_id_
; }
258 // Returns the display used for software mirrroring.
259 const gfx::Display
& mirroring_display() const {
260 return mirroring_display_
;
263 // Retuns the display info associated with |display_id|.
264 const DisplayInfo
& GetDisplayInfo(int64 display_id
) const;
266 // Returns the human-readable name for the display |id|.
267 std::string
GetDisplayNameForId(int64 id
);
269 // Returns the display id that is capable of UI scaling. On device,
270 // this returns internal display's ID if its device scale factor is 2,
271 // or invalid ID if such internal display doesn't exist. On linux
272 // desktop, this returns the first display ID.
273 int64
GetDisplayIdForUIScaling() const;
275 // Change the mirror mode.
276 void SetMirrorMode(bool mirrored
);
278 // Used to emulate display change when run in a desktop environment instead
280 void AddRemoveDisplay();
281 void ToggleDisplayScaleFactor();
283 // SoftwareMirroringController override:
284 #if defined(OS_CHROMEOS)
285 virtual void SetSoftwareMirroring(bool enabled
) override
;
286 virtual bool SoftwareMirroringEnabled() const override
;
288 bool software_mirroring_enabled() const {
289 return second_display_mode_
== MIRRORING
;
292 // Sets/gets second display mode.
293 void SetSecondDisplayMode(SecondDisplayMode mode
);
294 SecondDisplayMode
second_display_mode() const {
295 return second_display_mode_
;
298 // Update the bounds of the display given by |display_id|.
299 bool UpdateDisplayBounds(int64 display_id
,
300 const gfx::Rect
& new_bounds
);
302 // Creates mirror window asynchronously if the software mirror mode
304 void CreateMirrorWindowAsyncIfAny();
306 // Create a screen instance to be used during shutdown.
307 void CreateScreenForShutdown() const;
310 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest
, ConvertPoint
);
311 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, TestNativeDisplaysChanged
);
312 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
,
313 NativeDisplaysChangedAfterPrimaryChange
);
314 FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest
, AutomaticOverscanInsets
);
315 friend class AcceleratorControllerTest
;
316 friend class DisplayManagerTest
;
317 friend class test::AshTestBase
;
318 friend class test::DisplayManagerTestApi
;
319 friend class test::SystemGestureEventFilterTest
;
321 typedef std::vector
<gfx::Display
> DisplayList
;
323 void set_change_display_upon_host_resize(bool value
) {
324 change_display_upon_host_resize_
= value
;
327 gfx::Display
* FindDisplayForId(int64 id
);
329 // Add the mirror display's display info if the software based
330 // mirroring is in use.
331 void AddMirrorDisplayInfoIfAny(std::vector
<DisplayInfo
>* display_info_list
);
333 // Inserts and update the DisplayInfo according to the overscan
334 // state. Note that The DisplayInfo stored in the |internal_display_info_|
335 // can be different from |new_info| (due to overscan state), so
336 // you must use |GetDisplayInfo| to get the correct DisplayInfo for
338 void InsertAndUpdateDisplayInfo(const DisplayInfo
& new_info
);
340 // Called when the display info is updated through InsertAndUpdateDisplayInfo.
341 void OnDisplayInfoUpdated(const DisplayInfo
& display_info
);
343 // Creates a display object from the DisplayInfo for |display_id|.
344 gfx::Display
CreateDisplayFromDisplayInfoById(int64 display_id
);
346 // Updates the bounds of the secondary display in |display_list|
347 // using the layout registered for the display pair and set the
348 // index of display updated to |updated_index|. Returns true
349 // if the secondary display's bounds has been changed from current
350 // value, or false otherwise.
351 bool UpdateSecondaryDisplayBoundsForLayout(DisplayList
* display_list
,
352 size_t* updated_index
) const;
354 void CreateMirrorWindowIfAny();
356 bool HasSoftwareMirroringDisplay();
358 static void UpdateDisplayBoundsForLayout(
359 const DisplayLayout
& layout
,
360 const gfx::Display
& primary_display
,
361 gfx::Display
* secondary_display
);
363 Delegate
* delegate_
; // not owned.
365 scoped_ptr
<ScreenAsh
> screen_ash_
;
366 // This is to have an accessor without ScreenAsh definition.
367 gfx::Screen
* screen_
;
369 scoped_ptr
<DisplayLayoutStore
> layout_store_
;
371 int64 first_display_id_
;
373 // List of current active displays.
374 DisplayList displays_
;
376 int num_connected_displays_
;
378 bool force_bounds_changed_
;
380 // The mapping from the display ID to its internal data.
381 std::map
<int64
, DisplayInfo
> display_info_
;
383 // Selected display modes for displays. Key is the displays' ID.
384 std::map
<int64
, DisplayMode
> display_modes_
;
386 // When set to true, the host window's resize event updates
387 // the display's size. This is set to true when running on
388 // desktop environment (for debugging) so that resizing the host
389 // window will update the display properly. This is set to false
390 // on device as well as during the unit tests.
391 bool change_display_upon_host_resize_
;
393 SecondDisplayMode second_display_mode_
;
394 int64 mirrored_display_id_
;
395 gfx::Display mirroring_display_
;
397 // User preference for rotation lock of the internal display.
398 bool registered_internal_display_rotation_lock_
;
400 // User preference for the rotation of the internal display.
401 gfx::Display::Rotation registered_internal_display_rotation_
;
403 base::WeakPtrFactory
<DisplayManager
> weak_ptr_factory_
;
405 DISALLOW_COPY_AND_ASSIGN(DisplayManager
);
410 #endif // ASH_DISPLAY_DISPLAY_MANAGER_H_