Home | History | Annotate | Download | only in x11
      1 // Copyright 2014 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 UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_
      6 #define UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include <map>
     11 #include <vector>
     12 
     13 #include "base/compiler_specific.h"
     14 #include "base/event_types.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/observer_list.h"
     18 #include "ui/display/display_export.h"
     19 #include "ui/display/types/chromeos/native_display_delegate.h"
     20 #include "ui/gfx/geometry/point.h"
     21 #include "ui/gfx/geometry/size.h"
     22 
     23 // Forward declarations for Xlib and Xrandr.
     24 // This is so unused X definitions don't pollute the namespace.
     25 typedef unsigned long XID;
     26 typedef XID RROutput;
     27 typedef XID RRCrtc;
     28 typedef XID RRMode;
     29 typedef XID Window;
     30 
     31 struct _XDisplay;
     32 typedef struct _XDisplay Display;
     33 struct _XRROutputInfo;
     34 typedef _XRROutputInfo XRROutputInfo;
     35 struct _XRRScreenResources;
     36 typedef _XRRScreenResources XRRScreenResources;
     37 struct _XRRCrtcGamma;
     38 typedef _XRRCrtcGamma XRRCrtcGamma;
     39 
     40 namespace ui {
     41 
     42 class DisplayModeX11;
     43 class DisplaySnapshotX11;
     44 class NativeDisplayEventDispatcherX11;
     45 
     46 class DISPLAY_EXPORT NativeDisplayDelegateX11 : public NativeDisplayDelegate {
     47  public:
     48   // Helper class that allows NativeDisplayEventDispatcherX11 and
     49   // NativeDisplayDelegateX11::PlatformEventObserverX11 to interact with this
     50   // class or with mocks in tests.
     51   class HelperDelegate {
     52    public:
     53     virtual ~HelperDelegate() {}
     54 
     55     // Tells XRandR to update its configuration in response to |event|, an
     56     // RRScreenChangeNotify event.
     57     virtual void UpdateXRandRConfiguration(const base::NativeEvent& event) = 0;
     58 
     59     // Returns the list of current outputs. This is used to discard duplicate
     60     // events.
     61     virtual const std::vector<DisplaySnapshot*>& GetCachedDisplays() const = 0;
     62 
     63     // Notify |observers_| that a change in configuration has occurred.
     64     virtual void NotifyDisplayObservers() = 0;
     65   };
     66 
     67   NativeDisplayDelegateX11();
     68   virtual ~NativeDisplayDelegateX11();
     69 
     70   // NativeDisplayDelegate overrides:
     71   virtual void Initialize() OVERRIDE;
     72   virtual void GrabServer() OVERRIDE;
     73   virtual void UngrabServer() OVERRIDE;
     74   virtual void SyncWithServer() OVERRIDE;
     75   virtual void SetBackgroundColor(uint32_t color_argb) OVERRIDE;
     76   virtual void ForceDPMSOn() OVERRIDE;
     77   virtual std::vector<DisplaySnapshot*> GetDisplays() OVERRIDE;
     78   virtual void AddMode(const DisplaySnapshot& output,
     79                        const DisplayMode* mode) OVERRIDE;
     80   virtual bool Configure(const DisplaySnapshot& output,
     81                          const DisplayMode* mode,
     82                          const gfx::Point& origin) OVERRIDE;
     83   virtual void CreateFrameBuffer(const gfx::Size& size) OVERRIDE;
     84   virtual bool GetHDCPState(const DisplaySnapshot& output,
     85                             HDCPState* state) OVERRIDE;
     86   virtual bool SetHDCPState(const DisplaySnapshot& output,
     87                             HDCPState state) OVERRIDE;
     88   virtual std::vector<ColorCalibrationProfile>
     89       GetAvailableColorCalibrationProfiles(
     90           const DisplaySnapshot& output) OVERRIDE;
     91   virtual bool SetColorCalibrationProfile(
     92       const DisplaySnapshot& output,
     93       ColorCalibrationProfile new_profile) OVERRIDE;
     94   virtual void AddObserver(NativeDisplayObserver* observer) OVERRIDE;
     95   virtual void RemoveObserver(NativeDisplayObserver* observer) OVERRIDE;
     96 
     97  private:
     98   class HelperDelegateX11;
     99   class PlatformEventObserverX11;
    100 
    101   // Parses all the modes made available by |screen_|.
    102   void InitModes();
    103 
    104   // Helper method for GetOutputs() that returns an OutputSnapshot struct based
    105   // on the passed-in information.
    106   DisplaySnapshotX11* InitDisplaySnapshot(RROutput id,
    107                                           XRROutputInfo* info,
    108                                           RRCrtc* last_used_crtc,
    109                                           int index);
    110 
    111   // Destroys unused CRTCs.
    112   void DestroyUnusedCrtcs();
    113 
    114   // Parks used CRTCs in a way which allows a framebuffer resize. This is faster
    115   // than turning them off, resizing, then turning them back on.
    116   // |min_screen_size| represent the smallest size between the current
    117   // framebuffer size and the requested framebuffer size.
    118   void UpdateCrtcsForNewFramebuffer(const gfx::Size& min_screen_size);
    119 
    120   bool ConfigureCrtc(RRCrtc crtc, RRMode mode, RROutput output, int x, int y);
    121 
    122   // Returns whether |id| is configured to preserve aspect when scaling.
    123   bool IsOutputAspectPreservingScaling(RROutput id);
    124 
    125   // Creates the gamma ramp for |new_profile|, or NULL if it doesn't exist.
    126   // The caller should take the ownership.
    127   XRRCrtcGamma* CreateGammaRampForProfile(
    128       const DisplaySnapshotX11& x11_output,
    129       ColorCalibrationProfile new_profile);
    130 
    131   void DrawBackground();
    132 
    133   Display* display_;
    134   Window window_;
    135 
    136   // Initialized when the server is grabbed and freed when it's ungrabbed.
    137   XRRScreenResources* screen_;
    138 
    139   std::map<RRMode, DisplayModeX11*> modes_;
    140 
    141   // Every time GetOutputs() is called we cache the updated list of outputs in
    142   // |cached_outputs_| so that we can check for duplicate events rather than
    143   // propagate them.
    144   ScopedVector<DisplaySnapshot> cached_outputs_;
    145 
    146   scoped_ptr<HelperDelegate> helper_delegate_;
    147 
    148   // Processes X11 display events associated with the root window and notifies
    149   // |observers_| when a display change has occurred.
    150   scoped_ptr<NativeDisplayEventDispatcherX11> platform_event_dispatcher_;
    151 
    152   // Processes X11 display events that have no X11 window associated with it.
    153   scoped_ptr<PlatformEventObserverX11> platform_event_observer_;
    154 
    155   // List of observers waiting for display configuration change events.
    156   ObserverList<NativeDisplayObserver> observers_;
    157 
    158   // A background color used during boot time + multi displays.
    159   uint32_t background_color_argb_;
    160 
    161   DISALLOW_COPY_AND_ASSIGN(NativeDisplayDelegateX11);
    162 };
    163 
    164 }  // namespace ui
    165 
    166 #endif  // UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_
    167