Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ozone / wayland / display.h
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Copyright 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef OZONE_WAYLAND_DISPLAY_H_
7 #define OZONE_WAYLAND_DISPLAY_H_
8
9 #if !defined(__STDC_FORMAT_MACROS)
10 #define __STDC_FORMAT_MACROS
11 #endif
12
13 #include <wayland-client.h>
14 #include <list>
15 #include <map>
16
17 #include "base/basictypes.h"
18 #include "ozone/ui/events/window_state_change_handler.h"
19 #include "ozone/ui/gfx/ozone_display.h"
20 #include "ozone/wayland/input/text-client-protocol.h"
21
22 namespace ozonewayland {
23
24 class WaylandDisplayPollThread;
25 class WaylandInputDevice;
26 class WaylandScreen;
27 class WaylandShell;
28 class WaylandWindow;
29 struct wl_egl_window;
30
31 typedef std::map<unsigned, WaylandWindow*> WindowMap;
32
33 // WaylandDisplay is a wrapper around wl_display. Once we get a valid
34 // wl_display, the Wayland server will send different events to register
35 // the Wayland compositor, shell, screens, input devices, ...
36 class WaylandDisplay : public ui::WindowStateChangeHandler,
37                        public gfx::OzoneDisplay {
38  public:
39   enum RegistrationType {
40     RegisterAsNeeded,  // Handles all the required registrations.
41     RegisterOutputOnly  // Only screen registration.
42   };
43
44   explicit WaylandDisplay(RegistrationType type = RegisterAsNeeded);
45   virtual ~WaylandDisplay();
46
47   // Ownership is not passed to the caller.
48   static WaylandDisplay* GetInstance() { return instance_; }
49
50   // Returns a pointer to wl_display.
51   wl_display* display() const { return display_; }
52
53   wl_registry* registry() const { return registry_; }
54
55   WaylandInputDevice* PrimaryInput() const { return primary_input_; }
56
57   // Returns a list of the registered screens.
58   const std::list<WaylandScreen*>& GetScreenList() const;
59   WaylandScreen* PrimaryScreen() const { return primary_screen_ ; }
60
61   WaylandShell* GetShell() const { return shell_; }
62
63   wl_shm* shm() const { return shm_; }
64   wl_compositor* GetCompositor() const { return compositor_; }
65   struct wl_text_input_manager* GetTextInputManager() const;
66
67   int GetDisplayFd() const { return wl_display_get_fd(display_); }
68   unsigned GetSerial() const { return serial_; }
69   void SetSerial(unsigned serial) { serial_ = serial; }
70   // Returns WaylandWindow associated with w. The ownership is not transferred
71   // to the caller.
72   WaylandWindow* GetWindow(unsigned window_handle) const;
73   gfx::AcceleratedWidget GetNativeWindow(unsigned window_handle);
74
75   // Destroys WaylandWindow whose handle is w.
76   void DestroyWindow(unsigned w);
77
78   // Does a round trip to Wayland server. This call blocks the current thread
79   // until all pending request are processed by the server.
80   void SyncDisplay();
81   void FlushDisplay();
82
83   // Ozone Display implementation:
84   virtual gfx::SurfaceFactoryOzone::HardwareState InitializeHardware() OVERRIDE;
85   virtual void ShutdownHardware() OVERRIDE;
86   virtual intptr_t GetNativeDisplay() OVERRIDE;
87
88   virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
89   virtual void LookAheadOutputGeometry() OVERRIDE;
90
91   // Ownership is passed to the caller.
92   virtual scoped_ptr<gfx::SurfaceOzoneEGL> CreateEGLSurfaceForWidget(
93         gfx::AcceleratedWidget widget) OVERRIDE;
94
95   virtual bool LoadEGLGLES2Bindings(
96     gfx::SurfaceFactoryOzone::AddGLLibraryCallback add_gl_library,
97     gfx::SurfaceFactoryOzone::SetGLGetProcAddressProcCallback
98         proc_address) OVERRIDE;
99   virtual const int32* GetEGLSurfaceProperties(
100       const int32* desired_list) OVERRIDE;
101
102   // WindowStateChangeHandler implementation:
103   virtual void SetWidgetState(unsigned widget,
104                               ui::WidgetState state,
105                               unsigned width = 0,
106                               unsigned height = 0) OVERRIDE;
107   virtual void SetWidgetTitle(unsigned w,
108                               const base::string16& title) OVERRIDE;
109   virtual void SetWidgetCursor(int cursor_type) OVERRIDE;
110   virtual void SetWidgetAttributes(unsigned widget,
111                                    unsigned parent,
112                                    unsigned x,
113                                    unsigned y,
114                                    ui::WidgetType type) OVERRIDE;
115
116  private:
117   void InitializeDisplay(RegistrationType type);
118   // Creates a WaylandWindow backed by EGL Window and maps it to w. This can be
119   // useful for callers to track a particular surface. By default the type of
120   // surface(i.e. toplevel, menu) is none. One needs to explicitly call
121   // WaylandWindow::SetShellAttributes to set this. The ownership of
122   // WaylandWindow is not passed to the caller.
123   WaylandWindow* CreateAcceleratedSurface(unsigned w);
124
125   // Starts polling on display fd. This should be used when one needs to
126   // continuously read pending events coming from Wayland compositor and
127   // dispatch them. The polling is done completely on a separate thread and
128   // doesn't block the thread from which this is called.
129   void StartProcessingEvents();
130   // Stops polling on display fd.
131   void StopProcessingEvents();
132
133   void Terminate();
134   WaylandWindow* GetWidget(unsigned w) const;
135   // This handler resolves all server events used in initialization. It also
136   // handles input device registration, screen registration.
137   static void DisplayHandleGlobal(
138       void *data,
139       struct wl_registry *registry,
140       uint32_t name,
141       const char *interface,
142       uint32_t version);
143   // This handler resolves only screen registration. In general you don't want
144   // to use this but the one below.
145   static void DisplayHandleOutputOnly(
146       void *data,
147       struct wl_registry *registry,
148       uint32_t name,
149       const char *interface,
150       uint32_t version);
151
152   // WaylandDisplay manages the memory of all these pointers.
153   wl_display* display_;
154   wl_registry* registry_;
155   wl_compositor* compositor_;
156   WaylandShell* shell_;
157   wl_shm* shm_;
158   struct wl_text_input_manager* text_input_manager_;
159   WaylandScreen* primary_screen_;
160   WaylandInputDevice* primary_input_;
161   WaylandDisplayPollThread* display_poll_thread_;
162
163   std::list<WaylandScreen*> screen_list_;
164   std::list<WaylandInputDevice*> input_list_;
165   WindowMap widget_map_;
166   unsigned serial_;
167   bool processing_events_;
168   static WaylandDisplay* instance_;
169   DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
170 };
171
172 }  // namespace ozonewayland
173
174 #endif  // OZONE_WAYLAND_DISPLAY_H_