display_manager.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 "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"
18
19#if defined(OS_CHROMEOS)
20#include "ui/display/chromeos/display_configurator.h"
21#endif
22
23namespace gfx {
24class Display;
25class Insets;
26class Rect;
27class Screen;
28}
29
30namespace ash {
31class AcceleratorControllerTest;
32class DisplayController;
33class DisplayLayoutStore;
34class ScreenAsh;
35
36namespace test {
37class DisplayManagerTestApi;
38class SystemGestureEventFilterTest;
39}
40
41// DisplayManager maintains the current display configurations,
42// and notifies observers when configuration changes.
43//
44// TODO(oshima): Make this non internal.
45class ASH_EXPORT DisplayManager
46#if defined(OS_CHROMEOS)
47    : public ui::DisplayConfigurator::SoftwareMirroringController
48#endif
49      {
50 public:
51  class ASH_EXPORT Delegate {
52   public:
53    virtual ~Delegate() {}
54
55    // Create or updates the non desktop window with |display_info|.
56    virtual void CreateOrUpdateNonDesktopDisplay(
57        const DisplayInfo& display_info) = 0;
58
59    // Closes the mirror window if exists.
60    virtual void CloseNonDesktopDisplay() = 0;
61
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;
67  };
68
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 {
77    EXTENDED,
78    MIRRORING,
79    VIRTUAL_KEYBOARD
80  };
81
82  // Returns the list of possible UI scales for the display.
83  static std::vector<float> GetScalesForDisplay(const DisplayInfo& info);
84
85  // Returns next valid UI scale.
86  static float GetNextUIScale(const DisplayInfo& info, bool up);
87
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);
94
95  DisplayManager();
96  virtual ~DisplayManager();
97
98  DisplayLayoutStore* layout_store() {
99    return layout_store_.get();
100  }
101
102  gfx::Screen* screen() {
103    return screen_;
104  }
105
106  void set_delegate(Delegate* delegate) { delegate_ = delegate; }
107
108  // When set to true, the MonitorManager calls OnDisplayBoundsChanged
109  // even if the display's bounds didn't change. Used to swap primary
110  // display.
111  void set_force_bounds_changed(bool force_bounds_changed) {
112    force_bounds_changed_ = force_bounds_changed;
113  }
114
115  // Returns the display id of the first display in the outupt list.
116  int64 first_display_id() const { return first_display_id_; }
117
118  // Initializes displays using command line flag. Returns false
119  // if no command line flag was provided.
120  bool InitFromCommandLine();
121
122  // Initialize default display.
123  void InitDefaultDisplay();
124
125  // True if the given |display| is currently connected.
126  bool IsActiveDisplay(const gfx::Display& display) const;
127
128  // True if there is an internal display.
129  bool HasInternalDisplay() const;
130
131  bool IsInternalDisplayId(int64 id) const;
132
133  // Returns the display layout used for current displays.
134  DisplayLayout GetCurrentDisplayLayout();
135
136  // Returns the current display pair.
137  DisplayIdPair GetCurrentDisplayIdPair() const;
138
139  // Sets the layout for the current display pair. The |layout| specifies
140  // the locaion of the secondary display relative to the primary.
141  void SetLayoutForCurrentDisplays(
142      const DisplayLayout& layout_relative_to_primary);
143
144  // Returns display for given |id|;
145  const gfx::Display& GetDisplayForId(int64 id) const;
146
147  // Finds the display that contains |point| in screeen coordinates.
148  // Returns invalid display if there is no display that can satisfy
149  // the condition.
150  const gfx::Display& FindDisplayContainingPoint(
151      const gfx::Point& point_in_screen) const;
152
153  // Sets the work area's |insets| to the display given by |display_id|.
154  bool UpdateWorkAreaOfDisplay(int64 display_id, const gfx::Insets& insets);
155
156  // Registers the overscan insets for the display of the specified ID. Note
157  // that the insets size should be specified in DIP size. It also triggers the
158  // display's bounds change.
159  void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
160
161  // Sets the display's rotation.
162  void SetDisplayRotation(int64 display_id, gfx::Display::Rotation rotation);
163
164  // Sets the display's ui scale.
165  void SetDisplayUIScale(int64 display_id, float ui_scale);
166
167  // Sets the display's resolution.
168  void SetDisplayResolution(int64 display_id, const gfx::Size& resolution);
169
170  // Register per display properties. |overscan_insets| is NULL if
171  // the display has no custom overscan insets.
172  void RegisterDisplayProperty(int64 display_id,
173                               gfx::Display::Rotation rotation,
174                               float ui_scale,
175                               const gfx::Insets* overscan_insets,
176                               const gfx::Size& resolution_in_pixels,
177                               ui::ColorCalibrationProfile color_profile);
178
179  // Returns the display's selected mode.
180  bool GetSelectedModeForDisplayId(int64 display_id,
181                                   DisplayMode* mode_out) const;
182
183  // Tells if the virtual resolution feature is enabled.
184  bool IsDisplayUIScalingEnabled() const;
185
186  // Returns the current overscan insets for the specified |display_id|.
187  // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
188  // the display.
189  gfx::Insets GetOverscanInsets(int64 display_id) const;
190
191  // Sets the color calibration of the display to |profile|.
192  void SetColorCalibrationProfile(int64 display_id,
193                                  ui::ColorCalibrationProfile profile);
194
195  // Called when display configuration has changed. The new display
196  // configurations is passed as a vector of Display object, which
197  // contains each display's new infomration.
198  void OnNativeDisplaysChanged(
199      const std::vector<DisplayInfo>& display_info_list);
200
201  // Updates the internal display data and notifies observers about the changes.
202  void UpdateDisplays(const std::vector<DisplayInfo>& display_info_list);
203
204  // Updates current displays using current |display_info_|.
205  void UpdateDisplays();
206
207  // Returns the display at |index|. The display at 0 is
208  // no longer considered "primary".
209  const gfx::Display& GetDisplayAt(size_t index) const;
210
211  const gfx::Display& GetPrimaryDisplayCandidate() const;
212
213  // Returns the logical number of displays. This returns 1
214  // when displays are mirrored.
215  size_t GetNumDisplays() const;
216
217  const std::vector<gfx::Display>& displays() const { return displays_; }
218
219  // Returns the number of connected displays. This returns 2
220  // when displays are mirrored.
221  size_t num_connected_displays() const { return num_connected_displays_; }
222
223  // Returns the mirroring status.
224  bool IsMirrored() const;
225  int64 mirrored_display_id() const { return mirrored_display_id_; }
226
227  // Returns the display object that is not a part of desktop.
228  const gfx::Display& non_desktop_display() const {
229    return non_desktop_display_;
230  }
231
232  // Retuns the display info associated with |display_id|.
233  const DisplayInfo& GetDisplayInfo(int64 display_id) const;
234
235  // Returns the human-readable name for the display |id|.
236  std::string GetDisplayNameForId(int64 id);
237
238  // Returns the display id that is capable of UI scaling. On device,
239  // this returns internal display's ID if its device scale factor is 2,
240  // or invalid ID if such internal display doesn't exist. On linux
241  // desktop, this returns the first display ID.
242  int64 GetDisplayIdForUIScaling() const;
243
244  // Change the mirror mode.
245  void SetMirrorMode(bool mirrored);
246
247  // Used to emulate display change when run in a desktop environment instead
248  // of on a device.
249  void AddRemoveDisplay();
250  void ToggleDisplayScaleFactor();
251
252  // SoftwareMirroringController override:
253#if defined(OS_CHROMEOS)
254  virtual void SetSoftwareMirroring(bool enabled) OVERRIDE;
255#endif
256  bool software_mirroring_enabled() const {
257    return second_display_mode_ == MIRRORING;
258  };
259
260  bool virtual_keyboard_root_window_enabled() const {
261    return second_display_mode_ == VIRTUAL_KEYBOARD;
262  };
263
264  // Sets/gets second display mode.
265  void SetSecondDisplayMode(SecondDisplayMode mode);
266  SecondDisplayMode second_display_mode() const {
267    return second_display_mode_;
268  }
269
270  // Update the bounds of the display given by |display_id|.
271  bool UpdateDisplayBounds(int64 display_id,
272                           const gfx::Rect& new_bounds);
273
274  // Creates mirror window if the software mirror mode is enabled.
275  // This is used only for bootstrap.
276  void CreateMirrorWindowIfAny();
277
278  // Create a screen instance to be used during shutdown.
279  void CreateScreenForShutdown() const;
280
281private:
282  FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, ConvertPoint);
283  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, TestNativeDisplaysChanged);
284  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest,
285                           NativeDisplaysChangedAfterPrimaryChange);
286  FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, AutomaticOverscanInsets);
287  friend class ash::AcceleratorControllerTest;
288  friend class test::DisplayManagerTestApi;
289  friend class test::SystemGestureEventFilterTest;
290  friend class DisplayManagerTest;
291
292  typedef std::vector<gfx::Display> DisplayList;
293
294  void set_change_display_upon_host_resize(bool value) {
295    change_display_upon_host_resize_ = value;
296  }
297
298  gfx::Display* FindDisplayForId(int64 id);
299
300  // Add the mirror display's display info if the software based
301  // mirroring is in use.
302  void AddMirrorDisplayInfoIfAny(std::vector<DisplayInfo>* display_info_list);
303
304  // Inserts and update the DisplayInfo according to the overscan
305  // state. Note that The DisplayInfo stored in the |internal_display_info_|
306  // can be different from |new_info| (due to overscan state), so
307  // you must use |GetDisplayInfo| to get the correct DisplayInfo for
308  // a display.
309  void InsertAndUpdateDisplayInfo(const DisplayInfo& new_info);
310
311  // Called when the display info is updated through InsertAndUpdateDisplayInfo.
312  void OnDisplayInfoUpdated(const DisplayInfo& display_info);
313
314  // Creates a display object from the DisplayInfo for |display_id|.
315  gfx::Display CreateDisplayFromDisplayInfoById(int64 display_id);
316
317  // Updates the bounds of the secondary display in |display_list|
318  // using the layout registered for the display pair and set the
319  // index of display updated to |updated_index|. Returns true
320  // if the secondary display's bounds has been changed from current
321  // value, or false otherwise.
322  bool UpdateSecondaryDisplayBoundsForLayout(DisplayList* display_list,
323                                             size_t* updated_index) const;
324
325  static void UpdateDisplayBoundsForLayout(
326      const DisplayLayout& layout,
327      const gfx::Display& primary_display,
328      gfx::Display* secondary_display);
329
330  Delegate* delegate_;  // not owned.
331
332  scoped_ptr<ScreenAsh> screen_ash_;
333  // This is to have an accessor without ScreenAsh definition.
334  gfx::Screen* screen_;
335
336  scoped_ptr<DisplayLayoutStore> layout_store_;
337
338  int64 first_display_id_;
339
340  // List of current active displays.
341  DisplayList displays_;
342
343  int num_connected_displays_;
344
345  bool force_bounds_changed_;
346
347  // The mapping from the display ID to its internal data.
348  std::map<int64, DisplayInfo> display_info_;
349
350  // Selected display modes for displays. Key is the displays' ID.
351  std::map<int64, DisplayMode> display_modes_;
352
353  // When set to true, the host window's resize event updates
354  // the display's size. This is set to true when running on
355  // desktop environment (for debugging) so that resizing the host
356  // window will update the display properly. This is set to false
357  // on device as well as during the unit tests.
358  bool change_display_upon_host_resize_;
359
360  SecondDisplayMode second_display_mode_;
361  int64 mirrored_display_id_;
362  gfx::Display non_desktop_display_;
363
364  DISALLOW_COPY_AND_ASSIGN(DisplayManager);
365};
366
367}  // namespace ash
368
369#endif  // ASH_DISPLAY_DISPLAY_MANAGER_H_
370