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/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 100 // Parses all the modes made available by |screen_|. 101 void InitModes(); 102 103 // Helper method for GetOutputs() that returns an OutputSnapshot struct based 104 // on the passed-in information. 105 DisplaySnapshotX11* InitDisplaySnapshot(RROutput id, 106 XRROutputInfo* info, 107 RRCrtc* last_used_crtc, 108 int index); 109 110 // Destroys unused CRTCs. 111 void DestroyUnusedCrtcs(); 112 113 // Parks used CRTCs in a way which allows a framebuffer resize. This is faster 114 // than turning them off, resizing, then turning them back on. 115 // |min_screen_size| represent the smallest size between the current 116 // framebuffer size and the requested framebuffer size. 117 void UpdateCrtcsForNewFramebuffer(const gfx::Size& min_screen_size); 118 119 bool ConfigureCrtc(RRCrtc crtc, RRMode mode, RROutput output, int x, int y); 120 121 // Returns whether |id| is configured to preserve aspect when scaling. 122 bool IsOutputAspectPreservingScaling(RROutput id); 123 124 // Creates the gamma ramp for |new_profile|, or NULL if it doesn't exist. 125 // The caller should take the ownership. 126 XRRCrtcGamma* CreateGammaRampForProfile( 127 const DisplaySnapshotX11& x11_output, 128 ColorCalibrationProfile new_profile); 129 130 void DrawBackground(); 131 132 Display* display_; 133 Window window_; 134 135 // Initialized when the server is grabbed and freed when it's ungrabbed. 136 XRRScreenResources* screen_; 137 138 std::map<RRMode, DisplayModeX11*> modes_; 139 140 // Every time GetOutputs() is called we cache the updated list of outputs in 141 // |cached_outputs_| so that we can check for duplicate events rather than 142 // propagate them. 143 ScopedVector<DisplaySnapshot> cached_outputs_; 144 145 scoped_ptr<HelperDelegate> helper_delegate_; 146 147 // Processes X11 display events associated with the root window and notifies 148 // |observers_| when a display change has occurred. 149 scoped_ptr<NativeDisplayEventDispatcherX11> platform_event_dispatcher_; 150 151 // List of observers waiting for display configuration change events. 152 ObserverList<NativeDisplayObserver> observers_; 153 154 // A background color used during boot time + multi displays. 155 uint32_t background_color_argb_; 156 157 DISALLOW_COPY_AND_ASSIGN(NativeDisplayDelegateX11); 158 }; 159 160 } // namespace ui 161 162 #endif // UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_ 163