Merge "Build and package Layer Management service binaries." into tizen
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / include / WindowSystems / WaylandBaseWindowSystem.h
1 /***************************************************************************
2  *
3  * Copyright 2010, 2011 BMW Car IT GmbH
4  * Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *
20  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
21  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
24  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  *
28  ****************************************************************************/
29
30 #ifndef _WAYLANDBASEWINDOWSYSTEM_H_
31 #define _WAYLANDBASEWINDOWSYSTEM_H_
32 #include "WindowSystems/BaseWindowSystem.h"
33 #include "GraphicSystems/BaseGraphicSystem.h"
34 #include "Surface.h"
35 #include <wayland-server.h>
36 #include <wayland-client.h>
37 #include "PlatformSurfaces/WaylandPlatformSurface.h"
38 #include "Log.h"
39 #include "ScreenShotType.h"
40 #include "config.h"
41 #include "InputManager.h"
42 #include "WindowSystems/WaylandInputEvent.h"
43 #include "Rectangle.h"
44
45 extern "C" {
46 // TODO:to abstract
47 typedef enum waylandWindowSystemStates
48 {
49     REDRAW_STATE = 0,
50     WAKEUP_STATE = 1,
51     IDLE_STATE = 2,
52     UNKOWN_STATE
53 } WaylandWindowSystemStates;
54
55 typedef struct _wlevent
56 {
57     int        x;
58     int y;
59     int32_t    button;
60     enum wl_pointer_button_state buttonState;
61     long       keyCode;
62     enum wl_keyboard_key_state   keyState;
63     uint32_t   serial;
64     uint32_t   axis;
65     wl_fixed_t axisValue;
66     int        touchId;
67     int        touchType;
68 } WLEvent;
69 } // extern "C"
70
71 struct native_surface;
72
73 class WaylandBaseWindowSystem : public BaseWindowSystem
74 {
75 public:
76     WaylandBaseWindowSystem(const char* displayname, int width, int height, Scene* pScene, InputManager* pInputManager);
77     virtual ~WaylandBaseWindowSystem();
78     bool init(BaseGraphicSystem<void*, void*>* sys);
79     bool start(int maxIterationDurationInMS);
80     void stop();
81     void signalRedrawEvent();
82     void wakeUpRendererThread();
83     void setSystemState(WaylandWindowSystemStates state);   // TODO:don't check state
84     WaylandWindowSystemStates getSystemState();
85     struct wl_display* getNativeDisplayHandle();
86     virtual void allocatePlatformSurface(Surface *surface);
87     virtual void deallocatePlatformSurface(Surface *surface);
88     void doScreenShot(std::string fileName, const uint screen_id);
89     void doScreenShotOfLayer(std::string fileName, const uint id);
90     void doScreenShotOfSurface(std::string fileName, const uint id, const uint layer_id);
91     int getWindowWidth() const;
92     int getWindowHeight() const;
93     virtual void finishFrame();
94     virtual void scheduleRepaint(void *data);
95
96 protected:
97     struct wl_display* m_wlDisplay;
98     struct wl_display* m_wlDisplayClient;
99     struct wl_registry* m_wlRegistryClient;
100     struct wl_compositor* m_wlCompositorClient;
101     struct wl_surface* m_wlSurfaceClient;
102     pthread_t renderThread;
103     pthread_mutex_t run_lock;
104     BaseGraphicSystem<void*, void*>* graphicSystem;
105     virtual void RedrawAllLayers(bool clear, bool swap);
106     virtual void renderHWLayer(Layer* layer);
107     virtual bool initCompositor();
108     struct wl_shm* m_wlShm;
109     struct wl_global* m_serverInfoGlobal;
110     void* m_serverInfo;
111     struct wl_global* m_wlCompositorGlobal;
112     virtual bool createNativeContext() = 0;
113     virtual bool initGraphicSystem() = 0;
114
115     bool m_initialized;
116     ScreenShotType m_takeScreenshot;
117     const char* m_displayname;
118     bool m_success;
119     WaylandWindowSystemStates m_systemState;
120     uint m_manageConnectionId;
121     std::string m_screenShotFile;
122     uint m_screenShotScreenID;
123     uint m_screenShotSurfaceID;
124     uint m_screenShotLayerID;
125     bool m_debugMode;
126     bool m_error;
127     int m_width;
128     int m_height;
129     bool m_bRepaintNeeded;
130     bool m_bRepaintScheduled;
131     bool m_bUseFrameTimer;
132     struct wl_event_source* m_finishFrameTimer;
133
134     struct wl_list m_listFrameCallback;
135     struct wl_list m_nativeSurfaceList;
136
137     int m_maxIterationDurationInMS;
138
139     void createServerinfo(WaylandBaseWindowSystem* windowSystem);
140     struct native_surface* createNativeSurface();
141     void postReleaseBuffer(struct wl_buffer *buffer);
142     virtual void attachBufferToNativeSurface(struct wl_buffer* buffer, struct wl_surface* surface);
143     void repaint(int msecs);
144     void cleanup();
145     void Screenshot();
146     void Redraw();
147     void shutdownCompositor();
148     Surface* getSurfaceFromNativeSurface(struct native_surface* nativeSurface);
149     struct native_surface* getNativeSurfaceFromSurface(Surface* surface);
150     virtual void checkForNewSurfaceNativeContent();
151     void calculateFps();
152     void calculateSurfaceFps(Surface *currentSurface, float time);
153     void printDebug();
154     void* eventLoop();
155
156     static void bindCompositor(struct wl_client* client, void* data, uint32_t version, uint32_t id);
157     static int signalEventOnTerm(int signal_number, void *data);
158     static void destroyListenerSurfaceBuffer(struct wl_listener* listener, void *data);
159     static void destroyListenerSurfacePendingBuffer(struct wl_listener* listener, void *data);
160     static void idleEventRepaint(void *data);
161     bool createWaylandClient();
162     void releaseWaylandClient();
163
164     WaylandInputEvent* m_inputEvent;
165     virtual bool createInputEvent();
166
167 public:
168     static void serverinfoIFCreateConnection(struct wl_client *client, struct wl_resource *resource);
169     static void bindServerinfo(struct wl_client *client, void *data, uint32_t version, uint32_t id);
170     static uint32_t getTime(void);
171     static void destroySurfaceCallback(struct wl_resource* resource);
172     static void destroyFrameCallback(struct wl_resource *resource);
173     static void* eventLoopCallback(void* ptr);
174     static void registryHandleGlobalClient(void* data,
175                                             struct wl_registry* registry,
176                                             uint32_t name,
177                                             const char* interface,
178                                             uint32_t version);
179     static void surfaceListenerFrame(void* data, struct wl_callback* callback, uint32_t time);
180     static int finishFrameHandler(void *data);
181
182     // wl_surface interface
183     static void surfaceIFDestroy(struct wl_client *client,
184                                 struct wl_resource *resource);
185     static void surfaceIFAttach(struct wl_client *client,
186                                 struct wl_resource *resource,
187                                 struct wl_resource *buffer_resource,
188                                 int32_t x,
189                                 int32_t y);
190     static void surfaceIFDamage(struct wl_client *client,
191                                 struct wl_resource *resource,
192                                 int32_t x,
193                                 int32_t y,
194                                 int32_t width,
195                                 int32_t height);
196     static void surfaceIFFrame(struct wl_client *client,
197                                 struct wl_resource *resource,
198                                 uint32_t callback);
199     static void surfaceIFCommit(struct wl_client *client,
200                                 struct wl_resource *resource);
201
202     // wl_compositor interface
203     static void compositorIFCreateSurface(struct wl_client *client, struct wl_resource* resource, uint32_t id);
204
205     struct wl_list m_connectionList;
206
207     // Input event
208     void manageWLInputEvent(const InputDevice type, const InputEventState state, const WLEvent *wlEvent);
209 };
210
211 inline void WaylandBaseWindowSystem::setSystemState(WaylandWindowSystemStates state)
212 {
213     m_systemState = state;
214 }
215
216 inline WaylandWindowSystemStates WaylandBaseWindowSystem::getSystemState()
217 {
218     return m_systemState;
219 }
220
221 inline struct wl_display* WaylandBaseWindowSystem::getNativeDisplayHandle()
222 {
223     return m_wlDisplay;
224 }
225
226 inline bool WaylandBaseWindowSystem::createInputEvent()
227 {
228     return false;
229 }
230
231 inline int WaylandBaseWindowSystem::getWindowWidth() const
232 {
233     return m_width;
234 }
235
236 inline int WaylandBaseWindowSystem::getWindowHeight() const
237 {
238     return m_height;
239 }
240
241 extern "C" {
242 struct native_surface
243 {
244     struct wl_surface* surface;
245     WaylandBaseWindowSystem* windowSystem;
246     struct wl_buffer* buffer;
247     int connectionId;
248
249     uint32_t visual; // TODO confirm:it's necessary or not
250     struct wl_listener buffer_destroy_listener;
251     struct wl_list link;
252
253     /* All the pending state, that wl_surface.commit will apply. */
254     struct
255     {
256         /* wl_surface.attach */
257         int remove_contents;
258         struct wl_buffer *buffer;
259         struct wl_listener buffer_destroy_listener;
260         int32_t sx;
261         int32_t sy;
262
263         /* wl_surface.damage */
264         Rectangle damage;
265         bool damaged;
266
267         /* wl_surface.frame */
268         struct wl_list frame_callback_list;
269     } pending;
270 };
271
272 struct native_frame_callback
273 {
274     struct wl_resource* resource;
275     struct wl_list link;
276 };
277
278 struct native_process;
279 typedef void (*cleanupFuncForNativeProcess)(struct native_process* process, int status);
280
281 struct native_process
282 {
283     pid_t pid;
284     cleanupFuncForNativeProcess cleanup;
285     struct wl_list link;
286 };
287 }
288
289 #endif /* _WAYLANDBASEWINDOWSYSTEM_H_ */