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 "chromeos/display/output_configurator.h"
     21 #endif
     22 
     23 namespace gfx {
     24 class Display;
     25 class Insets;
     26 class Rect;
     27 }
     28 
     29 namespace ash {
     30 class AcceleratorControllerTest;
     31 class DisplayController;
     32 
     33 namespace test {
     34 class DisplayManagerTestApi;
     35 class SystemGestureEventFilterTest;
     36 }
     37 namespace internal {
     38 class DisplayLayoutStore;
     39 
     40 // DisplayManager maintains the current display configurations,
     41 // and notifies observers when configuration changes.
     42 //
     43 // TODO(oshima): Make this non internal.
     44 class ASH_EXPORT DisplayManager
     45 #if defined(OS_CHROMEOS)
     46     : public chromeos::OutputConfigurator::SoftwareMirroringController
     47 #endif
     48       {
     49  public:
     50   class ASH_EXPORT Delegate {
     51    public:
     52     virtual ~Delegate() {}
     53 
     54     // Create or updates the mirror window with |display_info|.
     55     virtual void CreateOrUpdateMirrorWindow(
     56         const DisplayInfo& display_info) = 0;
     57 
     58     // Closes the mirror window if exists.
     59     virtual void CloseMirrorWindow() = 0;
     60 
     61     // Called before and after the display configuration changes.
     62     virtual void PreDisplayConfigurationChange() = 0;
     63     virtual void PostDisplayConfigurationChange() = 0;
     64   };
     65 
     66   // Returns the list of possible UI scales for the display.
     67   static std::vector<float> GetScalesForDisplay(const DisplayInfo& info);
     68 
     69   // Returns next valid UI scale.
     70   static float GetNextUIScale(const DisplayInfo& info, bool up);
     71 
     72   // Updates the bounds of the display given by |secondary_display_id|
     73   // according to |layout|.
     74   static void UpdateDisplayBoundsForLayoutById(
     75       const DisplayLayout& layout,
     76       const gfx::Display& primary_display,
     77       int64 secondary_display_id);
     78 
     79   DisplayManager();
     80   virtual ~DisplayManager();
     81 
     82   DisplayLayoutStore* layout_store() {
     83     return layout_store_.get();
     84   }
     85 
     86   void set_delegate(Delegate* delegate) { delegate_ = delegate; }
     87 
     88   // When set to true, the MonitorManager calls OnDisplayBoundsChanged
     89   // even if the display's bounds didn't change. Used to swap primary
     90   // display.
     91   void set_force_bounds_changed(bool force_bounds_changed) {
     92     force_bounds_changed_ = force_bounds_changed;
     93   }
     94 
     95   // Returns the display id of the first display in the outupt list.
     96   int64 first_display_id() const { return first_display_id_; }
     97 
     98   // Initializes displays using command line flag, or uses
     99   // defualt if no options are specified.
    100   void InitFromCommandLine();
    101 
    102   // True if the given |display| is currently connected.
    103   bool IsActiveDisplay(const gfx::Display& display) const;
    104 
    105   // True if there is an internal display.
    106   bool HasInternalDisplay() const;
    107 
    108   bool IsInternalDisplayId(int64 id) const;
    109 
    110   // Returns display for given |id|;
    111   const gfx::Display& GetDisplayForId(int64 id) const;
    112 
    113   // Finds the display that contains |point| in screeen coordinates.
    114   // Returns invalid display if there is no display that can satisfy
    115   // the condition.
    116   const gfx::Display& FindDisplayContainingPoint(
    117       const gfx::Point& point_in_screen) const;
    118 
    119   // Sets the work area's |insets| to the display given by |display_id|.
    120   bool UpdateWorkAreaOfDisplay(int64 display_id, const gfx::Insets& insets);
    121 
    122   // Registers the overscan insets for the display of the specified ID. Note
    123   // that the insets size should be specified in DIP size. It also triggers the
    124   // display's bounds change.
    125   void SetOverscanInsets(int64 display_id, const gfx::Insets& insets_in_dip);
    126 
    127   // Sets the display's rotation.
    128   void SetDisplayRotation(int64 display_id, gfx::Display::Rotation rotation);
    129 
    130   // Sets the display's ui scale.
    131   void SetDisplayUIScale(int64 display_id, float ui_scale);
    132 
    133   // Sets the display's resolution.
    134   void SetDisplayResolution(int64 display_id, const gfx::Size& resolution);
    135 
    136   // Register per display properties. |overscan_insets| is NULL if
    137   // the display has no custom overscan insets.
    138   void RegisterDisplayProperty(int64 display_id,
    139                                gfx::Display::Rotation rotation,
    140                                float ui_scale,
    141                                const gfx::Insets* overscan_insets,
    142                                const gfx::Size& resolution_in_pixels);
    143 
    144   // Returns the display's selected resolution.
    145   bool GetSelectedResolutionForDisplayId(int64 display_id,
    146                                          gfx::Size* resolution_out) const;
    147 
    148   // Tells if display rotation/ui scaling features are enabled.
    149   bool IsDisplayRotationEnabled() const;
    150   bool IsDisplayUIScalingEnabled() const;
    151 
    152   // Returns the current overscan insets for the specified |display_id|.
    153   // Returns an empty insets (0, 0, 0, 0) if no insets are specified for
    154   // the display.
    155   gfx::Insets GetOverscanInsets(int64 display_id) const;
    156 
    157   // Called when display configuration has changed. The new display
    158   // configurations is passed as a vector of Display object, which
    159   // contains each display's new infomration.
    160   void OnNativeDisplaysChanged(
    161       const std::vector<DisplayInfo>& display_info_list);
    162 
    163   // Updates the internal display data and notifies observers about the changes.
    164   void UpdateDisplays(const std::vector<DisplayInfo>& display_info_list);
    165 
    166   // Updates current displays using current |display_info_|.
    167   void UpdateDisplays();
    168 
    169   // Returns the display at |index|. The display at 0 is
    170   // no longer considered "primary".
    171   const gfx::Display& GetDisplayAt(size_t index) const;
    172 
    173   const gfx::Display* GetPrimaryDisplayCandidate() const;
    174 
    175   // Returns the logical number of displays. This returns 1
    176   // when displays are mirrored.
    177   size_t GetNumDisplays() const;
    178 
    179   // Returns the number of connected displays. This returns 2
    180   // when displays are mirrored.
    181   size_t num_connected_displays() const { return num_connected_displays_; }
    182 
    183   // Returns the mirroring status.
    184   bool IsMirrored() const;
    185   const gfx::Display& mirrored_display() const { return mirrored_display_; }
    186 
    187   // Retuns the display info associated with |display_id|.
    188   const DisplayInfo& GetDisplayInfo(int64 display_id) const;
    189 
    190   // Returns the human-readable name for the display |id|.
    191   std::string GetDisplayNameForId(int64 id);
    192 
    193   // Returns the display id that is capable of UI scaling. On device,
    194   // this returns internal display's ID if its device scale factor is 2,
    195   // or invalid ID if such internal display doesn't exist. On linux
    196   // desktop, this returns the first display ID.
    197   int64 GetDisplayIdForUIScaling() const;
    198 
    199   // Change the mirror mode.
    200   void SetMirrorMode(bool mirrored);
    201 
    202   // Used to emulate display change when run in a desktop environment instead
    203   // of on a device.
    204   void AddRemoveDisplay();
    205   void ToggleDisplayScaleFactor();
    206 
    207   // SoftwareMirroringController override:
    208 #if defined(OS_CHROMEOS)
    209   virtual void SetSoftwareMirroring(bool enabled) OVERRIDE;
    210 #else
    211   void SetSoftwareMirroring(bool enabled);
    212 #endif
    213 
    214   // Update the bounds of the display given by |display_id|.
    215   bool UpdateDisplayBounds(int64 display_id,
    216                            const gfx::Rect& new_bounds);
    217 private:
    218   FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, ConvertPoint);
    219   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, TestNativeDisplaysChanged);
    220   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest,
    221                            NativeDisplaysChangedAfterPrimaryChange);
    222   FRIEND_TEST_ALL_PREFIXES(DisplayManagerTest, AutomaticOverscanInsets);
    223   friend class ash::AcceleratorControllerTest;
    224   friend class test::DisplayManagerTestApi;
    225   friend class test::SystemGestureEventFilterTest;
    226   friend class DisplayManagerTest;
    227 
    228   typedef std::vector<gfx::Display> DisplayList;
    229 
    230   void set_change_display_upon_host_resize(bool value) {
    231     change_display_upon_host_resize_ = value;
    232   }
    233 
    234   gfx::Display* FindDisplayForId(int64 id);
    235 
    236   // Add the mirror display's display info if the software based
    237   // mirroring is in use.
    238   void AddMirrorDisplayInfoIfAny(std::vector<DisplayInfo>* display_info_list);
    239 
    240   // Inserts and update the DisplayInfo according to the overscan
    241   // state. Note that The DisplayInfo stored in the |internal_display_info_|
    242   // can be different from |new_info| (due to overscan state), so
    243   // you must use |GetDisplayInfo| to get the correct DisplayInfo for
    244   // a display.
    245   void InsertAndUpdateDisplayInfo(const DisplayInfo& new_info);
    246 
    247   // Creates a display object from the DisplayInfo for |display_id|.
    248   gfx::Display CreateDisplayFromDisplayInfoById(int64 display_id);
    249 
    250   // Updates the bounds of the secondary display in |display_list|
    251   // using the layout registered for the display pair and set the
    252   // index of display updated to |updated_index|. Returns true
    253   // if the secondary display's bounds has been changed from current
    254   // value, or false otherwise.
    255   bool UpdateSecondaryDisplayBoundsForLayout(DisplayList* display_list,
    256                                              size_t* updated_index) const;
    257 
    258   static void UpdateDisplayBoundsForLayout(
    259       const DisplayLayout& layout,
    260       const gfx::Display& primary_display,
    261       gfx::Display* secondary_display);
    262 
    263   Delegate* delegate_;  // not owned.
    264 
    265   scoped_ptr<DisplayLayoutStore> layout_store_;
    266 
    267   int64 first_display_id_;
    268 
    269   gfx::Display mirrored_display_;
    270 
    271   // List of current active dispays.
    272   DisplayList displays_;
    273 
    274   int num_connected_displays_;
    275 
    276   bool force_bounds_changed_;
    277 
    278   // The mapping from the display ID to its internal data.
    279   std::map<int64, DisplayInfo> display_info_;
    280 
    281   // Selected resolutions for displays. Key is the displays' ID.
    282   std::map<int64, gfx::Size> resolutions_;
    283 
    284   // When set to true, the host window's resize event updates
    285   // the display's size. This is set to true when running on
    286   // desktop environment (for debugging) so that resizing the host
    287   // window wil update the display properly. This is set to false
    288   // on device as well as during the unit tests.
    289   bool change_display_upon_host_resize_;
    290 
    291   bool software_mirroring_enabled_;
    292 
    293   DISALLOW_COPY_AND_ASSIGN(DisplayManager);
    294 };
    295 
    296 }  // namespace internal
    297 }  // namespace ash
    298 
    299 #endif  // ASH_DISPLAY_DISPLAY_MANAGER_H_
    300