Home | History | Annotate | Download | only in display
      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 
     23 namespace gfx {
     24 class Display;
     25 class Insets;
     26 class Rect;
     27 class Screen;
     28 }
     29 
     30 namespace ash {
     31 class AcceleratorControllerTest;
     32 class DisplayController;
     33 class DisplayLayoutStore;
     34 class ScreenAsh;
     35 
     36 namespace test {
     37 class DisplayManagerTestApi;
     38 class 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.
     45 class 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   enum SecondDisplayMode {
     74     EXTENDED,
     75     MIRRORING
     76   };
     77 
     78   // Returns the list of possible UI scales for the display.
     79   static std::vector<float> GetScalesForDisplay(const DisplayInfo& info);
     80 
     81   // Returns next valid UI scale.
     82   static float GetNextUIScale(const DisplayInfo& info, bool up);
     83 
     84   // Updates the bounds of the display given by |secondary_display_id|
     85   // according to |layout|.
     86   static void UpdateDisplayBoundsForLayoutById(
     87       const DisplayLayout& layout,
     88       const gfx::Display& primary_display,
     89       int64 secondary_display_id);
     90 
     91   DisplayManager();
     92   virtual ~DisplayManager();
     93 
     94   DisplayLayoutStore* layout_store() {
     95     return layout_store_.get();
     96   }
     97 
     98   gfx::Screen* screen() {
     99     return screen_;
    100   }
    101 
    102   void set_delegate(Delegate* delegate) { delegate_ = delegate; }
    103 
    104   // When set to true, the MonitorManager calls OnDisplayMetricsChanged
    105   // even if the display's bounds didn't change. Used to swap primary
    106   // display.
    107   void set_force_bounds_changed(bool force_bounds_changed) {
    108     force_bounds_changed_ = force_bounds_changed;
    109   }
    110 
    111   // Returns the display id of the first display in the outupt list.
    112   int64 first_display_id() const { return first_display_id_; }
    113 
    114   // Initializes displays using command line flag. Returns false
    115   // if no command line flag was provided.
    116   bool InitFromCommandLine();
    117 
    118   // Initialize default display.
    119   void InitDefaultDisplay();
    120 
    121   // Initializes font related params that depends on display
    122   // configuration.
    123   void InitFontParams();
    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   // TODO(mukai): remove this and merge into SetDisplayMode.
    166   void SetDisplayUIScale(int64 display_id, float ui_scale);
    167 
    168   // Sets the display's resolution.
    169   // TODO(mukai): remove this and merge into SetDisplayMode.
    170   void SetDisplayResolution(int64 display_id, const gfx::Size& resolution);
    171 
    172   // Sets the external display's configuration, including resolution change,
    173   // ui-scale change, and device scale factor change. Returns true if it changes
    174   // the display resolution so that the caller needs to show a notification in
    175   // case the new resolution actually doesn't work.
    176   bool SetDisplayMode(int64 display_id, const DisplayMode& display_mode);
    177 
    178   // Register per display properties. |overscan_insets| is NULL if
    179   // the display has no custom overscan insets.
    180   void RegisterDisplayProperty(int64 display_id,
    181                                gfx::Display::Rotation rotation,
    182                                float ui_scale,
    183                                const gfx::Insets* overscan_insets,
    184                                const gfx::Size& resolution_in_pixels,
    185                                float device_scale_factor,
    186                                ui::ColorCalibrationProfile color_profile);
    187 
    188   // Register stored rotation properties for the internal display.
    189   void RegisterDisplayRotationProperties(bool rotation_lock,
    190                                          gfx::Display::Rotation rotation);
    191 
    192   // Returns the stored rotation lock preference if it has been loaded,
    193   // otherwise false.
    194   bool registered_internal_display_rotation_lock() const {
    195     return registered_internal_display_rotation_lock_;
    196   }
    197 
    198   // Returns the stored rotation preference for the internal display if it has
    199   // been loaded, otherwise |gfx::Display::Rotate_0|.
    200   gfx::Display::Rotation registered_internal_display_rotation() const {
    201     return registered_internal_display_rotation_;
    202   }
    203 
    204   // Returns the display mode of |display_id| which is currently used.
    205   DisplayMode GetActiveModeForDisplayId(int64 display_id) const;
    206 
    207   // Returns the display's selected mode. This returns false and doesn't
    208   // set |mode_out| if the display mode is in default.
    209   bool GetSelectedModeForDisplayId(int64 display_id,
    210                                    DisplayMode* mode_out) const;
    211 
    212   // Tells if the virtual resolution feature is enabled.
    213   bool IsDisplayUIScalingEnabled() const;
    214 
    215   // Returns the current overscan insets for the specified |display_id|.
    216   // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
    217   // the display.
    218   gfx::Insets GetOverscanInsets(int64 display_id) const;
    219 
    220   // Sets the color calibration of the display to |profile|.
    221   void SetColorCalibrationProfile(int64 display_id,
    222                                   ui::ColorCalibrationProfile profile);
    223 
    224   // Called when display configuration has changed. The new display
    225   // configurations is passed as a vector of Display object, which
    226   // contains each display's new infomration.
    227   void OnNativeDisplaysChanged(
    228       const std::vector<DisplayInfo>& display_info_list);
    229 
    230   // Updates the internal display data and notifies observers about the changes.
    231   void UpdateDisplays(const std::vector<DisplayInfo>& display_info_list);
    232 
    233   // Updates current displays using current |display_info_|.
    234   void UpdateDisplays();
    235 
    236   // Returns the display at |index|. The display at 0 is
    237   // no longer considered "primary".
    238   const gfx::Display& GetDisplayAt(size_t index) const;
    239 
    240   const gfx::Display& GetPrimaryDisplayCandidate() const;
    241 
    242   // Returns the logical number of displays. This returns 1
    243   // when displays are mirrored.
    244   size_t GetNumDisplays() const;
    245 
    246   const std::vector<gfx::Display>& displays() const { return displays_; }
    247 
    248   // Returns the number of connected displays. This returns 2
    249   // when displays are mirrored.
    250   size_t num_connected_displays() const { return num_connected_displays_; }
    251 
    252   // Returns the mirroring status.
    253   bool IsMirrored() const;
    254   int64 mirrored_display_id() const { return mirrored_display_id_; }
    255 
    256   // Returns the display object that is not a part of desktop.
    257   const gfx::Display& non_desktop_display() const {
    258     return non_desktop_display_;
    259   }
    260 
    261   // Retuns the display info associated with |display_id|.
    262   const DisplayInfo& GetDisplayInfo(int64 display_id) const;
    263 
    264   // Returns the human-readable name for the display |id|.
    265   std::string GetDisplayNameForId(int64 id);
    266 
    267   // Returns the display id that is capable of UI scaling. On device,
    268   // this returns internal display's ID if its device scale factor is 2,
    269   // or invalid ID if such internal display doesn't exist. On linux
    270   // desktop, this returns the first display ID.
    271   int64 GetDisplayIdForUIScaling() const;
    272 
    273   // Change the mirror mode.
    274   void SetMirrorMode(bool mirrored);
    275 
    276   // Used to emulate display change when run in a desktop environment instead
    277   // of on a device.
    278   void AddRemoveDisplay();
    279   void ToggleDisplayScaleFactor();
    280 
    281   // SoftwareMirroringController override:
    282 #if defined(OS_CHROMEOS)
    283   virtual void SetSoftwareMirroring(bool enabled) OVERRIDE;
    284   virtual bool SoftwareMirroringEnabled() const OVERRIDE;
    285 #endif
    286   bool software_mirroring_enabled() const {
    287     return second_display_mode_ == MIRRORING;
    288   };
    289 
    290   // Sets/gets second display mode.
    291   void SetSecondDisplayMode(SecondDisplayMode mode);
    292   SecondDisplayMode second_display_mode() const {
    293     return second_display_mode_;
    294   }
    295 
    296   // Update the bounds of the display given by |display_id|.
    297   bool UpdateDisplayBounds(int64 display_id,
    298                            const gfx::Rect& new_bounds);
    299 
    300   // Creates mirror window if the software mirror mode is enabled.
    301   // This is used only for bootstrap.
    302   void CreateMirrorWindowIfAny();
    303 
    304   // Create a screen instance to be used during shutdown.
    305   void CreateScreenForShutdown() const;
    306 
    307 private:
    308   FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, ConvertPoint);
    309   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, TestNativeDisplaysChanged);
    310   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest,
    311                            NativeDisplaysChangedAfterPrimaryChange);
    312   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, AutomaticOverscanInsets);
    313   friend class ash::AcceleratorControllerTest;
    314   friend class test::DisplayManagerTestApi;
    315   friend class test::SystemGestureEventFilterTest;
    316   friend class DisplayManagerTest;
    317 
    318   typedef std::vector<gfx::Display> DisplayList;
    319 
    320   void set_change_display_upon_host_resize(bool value) {
    321     change_display_upon_host_resize_ = value;
    322   }
    323 
    324   gfx::Display* FindDisplayForId(int64 id);
    325 
    326   // Add the mirror display's display info if the software based
    327   // mirroring is in use.
    328   void AddMirrorDisplayInfoIfAny(std::vector<DisplayInfo>* display_info_list);
    329 
    330   // Inserts and update the DisplayInfo according to the overscan
    331   // state. Note that The DisplayInfo stored in the |internal_display_info_|
    332   // can be different from |new_info| (due to overscan state), so
    333   // you must use |GetDisplayInfo| to get the correct DisplayInfo for
    334   // a display.
    335   void InsertAndUpdateDisplayInfo(const DisplayInfo& new_info);
    336 
    337   // Called when the display info is updated through InsertAndUpdateDisplayInfo.
    338   void OnDisplayInfoUpdated(const DisplayInfo& display_info);
    339 
    340   // Creates a display object from the DisplayInfo for |display_id|.
    341   gfx::Display CreateDisplayFromDisplayInfoById(int64 display_id);
    342 
    343   // Updates the bounds of the secondary display in |display_list|
    344   // using the layout registered for the display pair and set the
    345   // index of display updated to |updated_index|. Returns true
    346   // if the secondary display's bounds has been changed from current
    347   // value, or false otherwise.
    348   bool UpdateSecondaryDisplayBoundsForLayout(DisplayList* display_list,
    349                                              size_t* updated_index) const;
    350 
    351   static void UpdateDisplayBoundsForLayout(
    352       const DisplayLayout& layout,
    353       const gfx::Display& primary_display,
    354       gfx::Display* secondary_display);
    355 
    356   Delegate* delegate_;  // not owned.
    357 
    358   scoped_ptr<ScreenAsh> screen_ash_;
    359   // This is to have an accessor without ScreenAsh definition.
    360   gfx::Screen* screen_;
    361 
    362   scoped_ptr<DisplayLayoutStore> layout_store_;
    363 
    364   int64 first_display_id_;
    365 
    366   // List of current active displays.
    367   DisplayList displays_;
    368 
    369   int num_connected_displays_;
    370 
    371   bool force_bounds_changed_;
    372 
    373   // The mapping from the display ID to its internal data.
    374   std::map<int64, DisplayInfo> display_info_;
    375 
    376   // Selected display modes for displays. Key is the displays' ID.
    377   std::map<int64, DisplayMode> display_modes_;
    378 
    379   // When set to true, the host window's resize event updates
    380   // the display's size. This is set to true when running on
    381   // desktop environment (for debugging) so that resizing the host
    382   // window will update the display properly. This is set to false
    383   // on device as well as during the unit tests.
    384   bool change_display_upon_host_resize_;
    385 
    386   SecondDisplayMode second_display_mode_;
    387   int64 mirrored_display_id_;
    388   gfx::Display non_desktop_display_;
    389 
    390   // User preference for rotation lock of the internal display.
    391   bool registered_internal_display_rotation_lock_;
    392 
    393   // User preference for the rotation of the internal display.
    394   gfx::Display::Rotation registered_internal_display_rotation_;
    395 
    396   DISALLOW_COPY_AND_ASSIGN(DisplayManager);
    397 };
    398 
    399 }  // namespace ash
    400 
    401 #endif  // ASH_DISPLAY_DISPLAY_MANAGER_H_
    402