display_manager.h revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
5#ifndef ASH_DISPLAY_DISPLAY_MANAGER_H_
6#define ASH_DISPLAY_DISPLAY_MANAGER_H_
7
8#include <string>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "ash/display/display_info.h"
13#include "base/compiler_specific.h"
14#include "base/gtest_prod_util.h"
15#include "ui/aura/root_window_observer.h"
16#include "ui/aura/window.h"
17#include "ui/gfx/display.h"
18
19#if defined(OS_CHROMEOS)
20#include "chromeos/display/output_configurator.h"
21#endif
22
23namespace gfx {
24class Display;
25class Insets;
26class Rect;
27}
28
29namespace ash {
30class AcceleratorControllerTest;
31namespace test {
32class DisplayManagerTestApi;
33class SystemGestureEventFilterTest;
34}
35namespace internal {
36
37// DisplayManager maintains the current display configurations,
38// and notifies observers when configuration changes.
39// This is exported for unittest.
40//
41// TODO(oshima): Make this non internal.
42class ASH_EXPORT DisplayManager :
43#if defined(OS_CHROMEOS)
44      public chromeos::OutputConfigurator::SoftwareMirroringController,
45#endif
46      public aura::RootWindowObserver {
47 public:
48  DisplayManager();
49  virtual ~DisplayManager();
50
51  // Returns the list of possible UI scales for the display.
52  static std::vector<float> GetScalesForDisplay(const DisplayInfo& info);
53
54  // Returns next valid UI scale.
55  static float GetNextUIScale(const DisplayInfo& info, bool up);
56
57  // When set to true, the MonitorManager calls OnDisplayBoundsChanged
58  // even if the display's bounds didn't change. Used to swap primary
59  // display.
60  void set_force_bounds_changed(bool force_bounds_changed) {
61    force_bounds_changed_ = force_bounds_changed;
62  }
63
64  // Returns the display id of the first display in the outupt list.
65  int64 first_display_id() const { return first_display_id_; }
66
67  // True if the given |display| is currently connected.
68  bool IsActiveDisplay(const gfx::Display& display) const;
69
70  // True if there is an internal display.
71  bool HasInternalDisplay() const;
72
73  bool IsInternalDisplayId(int64 id) const;
74
75  bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
76                                            const gfx::Insets& insets);
77
78  // Returns display for given |id|;
79  const gfx::Display& GetDisplayForId(int64 id) const;
80
81  // Finds the display that contains |point| in screeen coordinates.
82  // Returns invalid display if there is no display that can satisfy
83  // the condition.
84  const gfx::Display& FindDisplayContainingPoint(
85      const gfx::Point& point_in_screen) const;
86
87  // Registers the overscan insets for the display of the specified ID. Note
88  // that the insets size should be specified in DIP size. It also triggers the
89  // display's bounds change.
90  void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
91
92  // Clears the overscan insets
93  void ClearCustomOverscanInsets(int64 display_id);
94
95  // Sets the display's rotation.
96  void SetDisplayRotation(int64 display_id, gfx::Display::Rotation rotation);
97
98  // Sets the display's ui scale.
99  void SetDisplayUIScale(int64 display_id, float ui_scale);
100
101  // Register per display properties. |overscan_insets| is NULL if
102  // the display has no custom overscan insets.
103  void RegisterDisplayProperty(int64 display_id,
104                               gfx::Display::Rotation rotation,
105                               float ui_scale,
106                               const gfx::Insets* overscan_insets);
107
108  // Tells if display rotation/ui scaling features are enabled.
109  bool IsDisplayRotationEnabled() const;
110  bool IsDisplayUIScalingEnabled() const;
111
112  // Returns the current overscan insets for the specified |display_id|.
113  // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
114  // the display.
115  gfx::Insets GetOverscanInsets(int64 display_id) const;
116
117  // Called when display configuration has changed. The new display
118  // configurations is passed as a vector of Display object, which
119  // contains each display's new infomration.
120  void OnNativeDisplaysChanged(
121      const std::vector<DisplayInfo>& display_info_list);
122
123  // Updates the internal display data and notifies observers about the changes.
124  void UpdateDisplays(const std::vector<DisplayInfo>& display_info_list);
125
126  // Updates current displays using current |display_info_|.
127  void UpdateDisplays();
128
129  // Obsoleted: Do not use in new code.
130  // Returns the display at |index|. The display at 0 is
131  // no longer considered "primary".
132  gfx::Display* GetDisplayAt(size_t index);
133
134  const gfx::Display* GetPrimaryDisplayCandidate() const;
135
136  // Returns the logical number of displays. This returns 1
137  // when displays are mirrored.
138  size_t GetNumDisplays() const;
139
140  // Returns the number of connected displays. This returns 2
141  // when displays are mirrored.
142  size_t num_connected_displays() const { return num_connected_displays_; }
143
144  // Returns the mirroring status.
145  bool IsMirrored() const;
146  const gfx::Display& mirrored_display() const { return mirrored_display_; }
147
148  // Returns the display object nearest given |window|.
149  const gfx::Display& GetDisplayNearestPoint(
150      const gfx::Point& point) const;
151
152  // Returns the display object nearest given |point|.
153  const gfx::Display& GetDisplayNearestWindow(
154      const aura::Window* window) const;
155
156  // Returns the display that most closely intersects |match_rect|.
157  const gfx::Display& GetDisplayMatching(
158      const gfx::Rect& match_rect)const;
159
160  // Retuns the display info associated with |display_id|.
161  const DisplayInfo& GetDisplayInfo(int64 display_id) const;
162
163  // Returns the human-readable name for the display |id|.
164  std::string GetDisplayNameForId(int64 id);
165
166  // Returns the display id that is capable of UI scaling. On device,
167  // this returns internal display's ID if its device scale factor is 2,
168  // or invalid ID if such internal display doesn't exist. On linux
169  // desktop, this returns the first display ID.
170  int64 GetDisplayIdForUIScaling() const;
171
172  // Change the mirror mode.
173  void SetMirrorMode(bool mirrored);
174
175  // Used to emulate display change when run in a desktop environment instead
176  // of on a device.
177  void AddRemoveDisplay();
178  void ToggleDisplayScaleFactor();
179
180  // RootWindowObserver overrides:
181  virtual void OnRootWindowResized(const aura::RootWindow* root,
182                                   const gfx::Size& new_size) OVERRIDE;
183
184  // SoftwareMirroringController override:
185#if defined(OS_CHROMEOS)
186  virtual void SetSoftwareMirroring(bool enabled) OVERRIDE;
187#else
188  void SetSoftwareMirroring(bool enabled);
189#endif
190
191 private:
192  FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, ConvertPoint);
193  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, TestNativeDisplaysChanged);
194  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest,
195                           NativeDisplaysChangedAfterPrimaryChange);
196  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, AutomaticOverscanInsets);
197  friend class ash::AcceleratorControllerTest;
198  friend class test::DisplayManagerTestApi;
199  friend class DisplayManagerTest;
200  friend class test::SystemGestureEventFilterTest;
201
202  typedef std::vector<gfx::Display> DisplayList;
203
204  void set_change_display_upon_host_resize(bool value) {
205    change_display_upon_host_resize_ = value;
206  }
207
208  void Init();
209
210  gfx::Display& FindDisplayForRootWindow(const aura::RootWindow* root);
211  gfx::Display& FindDisplayForId(int64 id);
212
213  // Add the mirror display's display info if the software based
214  // mirroring is in use.
215  void AddMirrorDisplayInfoIfAny(std::vector<DisplayInfo>* display_info_list);
216
217  // Refer to |CreateDisplayFromSpec| API for the format of |spec|.
218  void AddDisplayFromSpec(const std::string& spec);
219
220  // Inserts and update the DisplayInfo according to the overscan
221  // state. Note that The DisplayInfo stored in the |internal_display_info_|
222  // can be different from |new_info| (due to overscan state), so
223  // you must use |GetDisplayInfo| to get the correct DisplayInfo for
224  // a display.
225  void InsertAndUpdateDisplayInfo(const DisplayInfo& new_info);
226
227  // Creates a display object from the DisplayInfo for |display_id|.
228  gfx::Display CreateDisplayFromDisplayInfoById(int64 display_id);
229
230  int64 first_display_id_;
231
232  gfx::Display mirrored_display_;
233
234  // List of current active dispays.
235  DisplayList displays_;
236
237  int num_connected_displays_;
238
239  bool force_bounds_changed_;
240
241  // The mapping from the display ID to its internal data.
242  std::map<int64, DisplayInfo> display_info_;
243
244  // When set to true, the host window's resize event updates
245  // the display's size. This is set to true when running on
246  // desktop environment (for debugging) so that resizing the host
247  // window wil update the display properly. This is set to false
248  // on device as well as during the unit tests.
249  bool change_display_upon_host_resize_;
250
251  bool software_mirroring_enabled_;
252
253  DISALLOW_COPY_AND_ASSIGN(DisplayManager);
254};
255
256extern const aura::WindowProperty<int64>* const kDisplayIdKey;
257
258}  // namespace internal
259}  // namespace ash
260
261#endif  // ASH_DISPLAY_DISPLAY_MANAGER_H_
262