Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ui / display / chromeos / x11 / native_display_delegate_x11.h
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/chromeos/native_display_delegate.h"
19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/size.h"
21
22 // Forward declarations for Xlib and Xrandr.
23 // This is so unused X definitions don't pollute the namespace.
24 typedef unsigned long XID;
25 typedef XID RROutput;
26 typedef XID RRCrtc;
27 typedef XID RRMode;
28 typedef XID Window;
29
30 struct _XDisplay;
31 typedef struct _XDisplay Display;
32 struct _XRROutputInfo;
33 typedef _XRROutputInfo XRROutputInfo;
34 struct _XRRScreenResources;
35 typedef _XRRScreenResources XRRScreenResources;
36 struct _XRRCrtcGamma;
37 typedef _XRRCrtcGamma XRRCrtcGamma;
38
39 namespace ui {
40
41 class DisplayModeX11;
42 class DisplaySnapshotX11;
43 class NativeDisplayEventDispatcherX11;
44
45 class DISPLAY_EXPORT NativeDisplayDelegateX11 : public NativeDisplayDelegate {
46  public:
47   // Helper class that allows NativeDisplayEventDispatcherX11 and
48   // NativeDisplayDelegateX11::MessagePumpObserverX11 to interact with this
49   // class or with mocks in tests.
50   class HelperDelegate {
51    public:
52     virtual ~HelperDelegate() {}
53
54     // Tells XRandR to update its configuration in response to |event|, an
55     // RRScreenChangeNotify event.
56     virtual void UpdateXRandRConfiguration(const base::NativeEvent& event) = 0;
57
58     // Returns the list of current outputs. This is used to discard duplicate
59     // events.
60     virtual const std::vector<DisplaySnapshot*>& GetCachedOutputs() const = 0;
61
62     // Notify |observers_| that a change in configuration has occurred.
63     virtual void NotifyDisplayObservers() = 0;
64   };
65
66   NativeDisplayDelegateX11();
67   virtual ~NativeDisplayDelegateX11();
68
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;
95
96  private:
97   class HelperDelegateX11;
98   class MessagePumpObserverX11;
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 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();
114
115   bool ConfigureCrtc(RRCrtc crtc, RRMode mode, RROutput output, int x, int y);
116
117   // Returns whether |id| is configured to preserve aspect when scaling.
118   bool IsOutputAspectPreservingScaling(RROutput id);
119
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);
125
126   Display* display_;
127   Window window_;
128
129   // Initialized when the server is grabbed and freed when it's ungrabbed.
130   XRRScreenResources* screen_;
131
132   std::map<RRMode, DisplayModeX11*> modes_;
133
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
136   // propagate them.
137   ScopedVector<DisplaySnapshot> cached_outputs_;
138
139   scoped_ptr<HelperDelegate> helper_delegate_;
140
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_;
144
145   // Processes X11 display events that have no X11 window associated with it.
146   scoped_ptr<MessagePumpObserverX11> message_pump_observer_;
147
148   // List of observers waiting for display configuration change events.
149   ObserverList<NativeDisplayObserver> observers_;
150
151   DISALLOW_COPY_AND_ASSIGN(NativeDisplayDelegateX11);
152 };
153
154 }  // namespace ui
155
156 #endif  // UI_DISPLAY_CHROMEOS_X11_NATIVE_DISPLAY_DELEGATE_X11_H_