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.
5 #ifndef UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_
6 #define UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_
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/chromeos/native_display_delegate.h"
19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/size.h"
22 // Forward declarations for Xlib and Xrandr.
23 // This is so unused X definitions don't pollute the namespace.
24 typedef unsigned long XID;
31 typedef struct _XDisplay Display;
32 struct _XRROutputInfo;
33 typedef _XRROutputInfo XRROutputInfo;
34 struct _XRRScreenResources;
35 typedef _XRRScreenResources XRRScreenResources;
37 typedef _XRRCrtcGamma XRRCrtcGamma;
42 class DisplaySnapshotX11;
43 class NativeDisplayEventDispatcherX11;
45 class DISPLAY_EXPORT NativeDisplayDelegateX11 : public NativeDisplayDelegate {
47 // Helper class that allows NativeDisplayEventDispatcherX11 and
48 // NativeDisplayDelegateX11::MessagePumpObserverX11 to interact with this
49 // class or with mocks in tests.
50 class HelperDelegate {
52 virtual ~HelperDelegate() {}
54 // Tells XRandR to update its configuration in response to |event|, an
55 // RRScreenChangeNotify event.
56 virtual void UpdateXRandRConfiguration(const base::NativeEvent& event) = 0;
58 // Returns the list of current outputs. This is used to discard duplicate
60 virtual const std::vector<DisplaySnapshot*>& GetCachedOutputs() const = 0;
62 // Notify |observers_| that a change in configuration has occurred.
63 virtual void NotifyDisplayObservers() = 0;
66 NativeDisplayDelegateX11();
67 virtual ~NativeDisplayDelegateX11();
69 // NativeDisplayDelegate overrides:
70 virtual void Initialize() OVERRIDE;
71 virtual void GrabServer() OVERRIDE;
72 virtual void UngrabServer() OVERRIDE;
73 virtual void SyncWithServer() OVERRIDE;
74 virtual void SetBackgroundColor(uint32_t color_argb) OVERRIDE;
75 virtual void ForceDPMSOn() OVERRIDE;
76 virtual std::vector<DisplaySnapshot*> GetOutputs() OVERRIDE;
77 virtual void AddMode(const DisplaySnapshot& output,
78 const DisplayMode* mode) OVERRIDE;
79 virtual bool Configure(const DisplaySnapshot& output,
80 const DisplayMode* mode,
81 const gfx::Point& origin) OVERRIDE;
82 virtual void CreateFrameBuffer(const gfx::Size& size) OVERRIDE;
83 virtual bool GetHDCPState(const DisplaySnapshot& output,
84 HDCPState* state) OVERRIDE;
85 virtual bool SetHDCPState(const DisplaySnapshot& output,
86 HDCPState state) OVERRIDE;
87 virtual std::vector<ColorCalibrationProfile>
88 GetAvailableColorCalibrationProfiles(
89 const DisplaySnapshot& output) OVERRIDE;
90 virtual bool SetColorCalibrationProfile(
91 const DisplaySnapshot& output,
92 ColorCalibrationProfile new_profile) OVERRIDE;
93 virtual void AddObserver(NativeDisplayObserver* observer) OVERRIDE;
94 virtual void RemoveObserver(NativeDisplayObserver* observer) OVERRIDE;
97 class HelperDelegateX11;
98 class MessagePumpObserverX11;
100 // Parses all the modes made available by |screen_|.
103 // Helper method for GetOutputs() that returns an OutputSnapshot struct based
104 // on the passed-in information.
105 DisplaySnapshotX11* InitDisplaySnapshot(RROutput id,
107 RRCrtc* last_used_crtc,
110 // Destroys unused CRTCs and parks used CRTCs in a way which allows a
111 // framebuffer resize. This is faster than turning them off, resizing,
112 // then turning them back on.
113 void DestroyUnusedCrtcs();
115 bool ConfigureCrtc(RRCrtc crtc, RRMode mode, RROutput output, int x, int y);
117 // Returns whether |id| is configured to preserve aspect when scaling.
118 bool IsOutputAspectPreservingScaling(RROutput id);
120 // Creates the gamma ramp for |new_profile|, or NULL if it doesn't exist.
121 // The caller should take the ownership.
122 XRRCrtcGamma* CreateGammaRampForProfile(
123 const DisplaySnapshotX11& x11_output,
124 ColorCalibrationProfile new_profile);
129 // Initialized when the server is grabbed and freed when it's ungrabbed.
130 XRRScreenResources* screen_;
132 std::map<RRMode, DisplayModeX11*> modes_;
134 // Every time GetOutputs() is called we cache the updated list of outputs in
135 // |cached_outputs_| so that we can check for duplicate events rather than
137 ScopedVector<DisplaySnapshot> cached_outputs_;
139 scoped_ptr<HelperDelegate> helper_delegate_;
141 // Processes X11 display events associated with the root window and notifies
142 // |observers_| when a display change has occurred.
143 scoped_ptr<NativeDisplayEventDispatcherX11> message_pump_dispatcher_;
145 // Processes X11 display events that have no X11 window associated with it.
146 scoped_ptr<MessagePumpObserverX11> message_pump_observer_;
148 // List of observers waiting for display configuration change events.
149 ObserverList<NativeDisplayObserver> observers_;
151 DISALLOW_COPY_AND_ASSIGN(NativeDisplayDelegateX11);
156 #endif // UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_