display_manager.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright (c) 2012 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef ASH_DISPLAY_DISPLAY_MANAGER_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define ASH_DISPLAY_DISPLAY_MANAGER_H_
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <string>
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <vector>
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ash/ash_export.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ash/display/display_info.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/compiler_specific.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/gtest_prod_util.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/aura/root_window_observer.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/aura/window.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace gfx {
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Display;
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Insets;
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Rect;
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace ash {
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass AcceleratorControllerTest;
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace test {
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass DisplayManagerTestApi;
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass SystemGestureEventFilterTest;
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace internal {
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// DisplayManager maintains the current display configurations,
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// and notifies observers when configuration changes.
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// This is exported for unittest.
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// TODO(oshima): Make this non internal.
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ASH_EXPORT DisplayManager : public aura::RootWindowObserver {
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public:
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DisplayManager();
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual ~DisplayManager();
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Used to emulate display change when run in a desktop environment instead
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // of on a device.
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static void CycleDisplay();
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static void ToggleDisplayScaleFactor();
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
47  // Returns next valid UI scale.
48  static float GetNextUIScale(const DisplayInfo& info, bool up);
49
50  // When set to true, the MonitorManager calls OnDisplayBoundsChanged
51  // even if the display's bounds didn't change. Used to swap primary
52  // display.
53  void set_force_bounds_changed(bool force_bounds_changed) {
54    force_bounds_changed_ = force_bounds_changed;
55  }
56
57  // Returns the display id of the first display in the outupt list.
58  int64 first_display_id() const { return first_display_id_; }
59
60  // True if the given |display| is currently connected.
61  bool IsActiveDisplay(const gfx::Display& display) const;
62
63  // True if there is an internal display.
64  bool HasInternalDisplay() const;
65
66  bool IsInternalDisplayId(int64 id) const;
67
68  bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
69                                            const gfx::Insets& insets);
70
71  // Returns display for given |id|;
72  const gfx::Display& GetDisplayForId(int64 id) const;
73
74  // Finds the display that contains |point| in screeen coordinates.
75  // Returns invalid display if there is no display that can satisfy
76  // the condition.
77  const gfx::Display& FindDisplayContainingPoint(
78      const gfx::Point& point_in_screen) const;
79
80  // Registers the overscan insets for the display of the specified ID. Note
81  // that the insets size should be specified in DIP size. It also triggers the
82  // display's bounds change.
83  void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
84
85  // Clears the overscan insets
86  void ClearCustomOverscanInsets(int64 display_id);
87
88  // Sets the display's rotation.
89  void SetDisplayRotation(int64 display_id, gfx::Display::Rotation rotation);
90
91  // Sets the display's ui scale.
92  void SetDisplayUIScale(int64 display_id, float ui_scale);
93
94  // Register per display properties. |overscan_insets| is NULL if
95  // the display has no custom overscan insets.
96  void RegisterDisplayProperty(int64 display_id,
97                               gfx::Display::Rotation rotation,
98                               float ui_scale,
99                               const gfx::Insets* overscan_insets);
100
101  // Tells if display rotation/ui scaling features are enabled.
102  bool IsDisplayRotationEnabled() const;
103  bool IsDisplayUIScalingEnabled() const;
104
105  // Returns the current overscan insets for the specified |display_id|.
106  // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
107  // the display.
108  gfx::Insets GetOverscanInsets(int64 display_id) const;
109
110  // Called when display configuration has changed. The new display
111  // configurations is passed as a vector of Display object, which
112  // contains each display's new infomration.
113  void OnNativeDisplaysChanged(
114      const std::vector<DisplayInfo>& display_info_list);
115
116  // Updates the internal display data and notifies observers about the changes.
117  void UpdateDisplays(const std::vector<DisplayInfo>& display_info_list);
118
119  // Updates current displays using current |display_info_|.
120  void UpdateDisplays();
121
122  // Obsoleted: Do not use in new code.
123  // Returns the display at |index|. The display at 0 is
124  // no longer considered "primary".
125  gfx::Display* GetDisplayAt(size_t index);
126
127  const gfx::Display* GetPrimaryDisplayCandidate() const;
128
129  // Returns the logical number of displays. This returns 1
130  // when displays are mirrored.
131  size_t GetNumDisplays() const;
132
133  // Returns the number of connected displays. This returns 2
134  // when displays are mirrored.
135  size_t num_connected_displays() const { return num_connected_displays_; }
136
137  // Returns the mirroring status.
138  bool IsMirrored() const;
139  int64 mirrored_display_id() const { return mirrored_display_id_; }
140
141  // Returns the display object nearest given |window|.
142  const gfx::Display& GetDisplayNearestPoint(
143      const gfx::Point& point) const;
144
145  // Returns the display object nearest given |point|.
146  const gfx::Display& GetDisplayNearestWindow(
147      const aura::Window* window) const;
148
149  // Returns the display that most closely intersects |match_rect|.
150  const gfx::Display& GetDisplayMatching(
151      const gfx::Rect& match_rect)const;
152
153  // Retuns the display info associated with |display_id|.
154  const DisplayInfo& GetDisplayInfo(int64 display_id) const;
155
156  // Returns the human-readable name for the display |id|.
157  std::string GetDisplayNameForId(int64 id);
158
159  // Returns the display id that is capable of UI scaling. On device,
160  // this returns internal display's ID if its device scale factor is 2,
161  // or invalid ID if such internal display doesn't exist. On linux
162  // desktop, this returns the first display ID.
163  int64 GetDisplayIdForUIScaling() const;
164
165  // RootWindowObserver overrides:
166  virtual void OnRootWindowResized(const aura::RootWindow* root,
167                                   const gfx::Size& new_size) OVERRIDE;
168
169 private:
170  FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, ConvertPoint);
171  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, TestNativeDisplaysChanged);
172  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest,
173                           NativeDisplaysChangedAfterPrimaryChange);
174  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, AutomaticOverscanInsets);
175  friend class ash::AcceleratorControllerTest;
176  friend class test::DisplayManagerTestApi;
177  friend class DisplayManagerTest;
178  friend class test::SystemGestureEventFilterTest;
179
180  typedef std::vector<gfx::Display> DisplayList;
181
182  void set_change_display_upon_host_resize(bool value) {
183    change_display_upon_host_resize_ = value;
184  }
185
186  void Init();
187  void CycleDisplayImpl();
188  void ScaleDisplayImpl();
189
190  gfx::Display& FindDisplayForRootWindow(const aura::RootWindow* root);
191  gfx::Display& FindDisplayForId(int64 id);
192
193  // Refer to |CreateDisplayFromSpec| API for the format of |spec|.
194  void AddDisplayFromSpec(const std::string& spec);
195
196  // Inserts and update the DisplayInfo according to the overscan
197  // state. Note that The DisplayInfo stored in the |internal_display_info_|
198  // can be different from |new_info| (due to overscan state), so
199  // you must use |GetDisplayInfo| to get the correct DisplayInfo for
200  // a display.
201  void InsertAndUpdateDisplayInfo(const DisplayInfo& new_info);
202
203  // Creates a display object from the DisplayInfo for |display_id|.
204  gfx::Display CreateDisplayFromDisplayInfoById(int64 display_id);
205
206  int64 first_display_id_;
207
208  int64 mirrored_display_id_;
209
210  // List of current active dispays.
211  DisplayList displays_;
212
213  int num_connected_displays_;
214
215  bool force_bounds_changed_;
216
217  // The mapping from the display ID to its internal data.
218  std::map<int64, DisplayInfo> display_info_;
219
220  // When set to true, the host window's resize event updates
221  // the display's size. This is set to true when running on
222  // desktop environment (for debugging) so that resizing the host
223  // window wil update the display properly. This is set to false
224  // on device as well as during the unit tests.
225  bool change_display_upon_host_resize_;
226
227  DISALLOW_COPY_AND_ASSIGN(DisplayManager);
228};
229
230extern const aura::WindowProperty<int64>* const kDisplayIdKey;
231
232}  // namespace internal
233}  // namespace ash
234
235#endif  // ASH_DISPLAY_DISPLAY_MANAGER_H_
236