LayerManagerPlugins: drm backend for Wayland
[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 _WAYLANDWINDOWSYSTEM_H_
31 #define _WAYLANDWINDOWSYSTEM_H_
32 #include "WindowSystems/BaseWindowSystem.h"
33 #include "GraphicSystems/BaseGraphicSystem.h"
34 #include "Surface.h"
35 #include <EGL/egl.h>
36 #include <EGL/eglext.h>
37 #include <wayland-server.h>
38 #include "PlatformSurfaces/WaylandPlatformSurface.h"
39 #include "Log.h"
40 #include "ScreenShotType.h"
41 #include "config.h"
42
43 extern "C" {
44 // TODO:to abstract
45 typedef enum waylandWindowSystemStates
46 {
47     REDRAW_STATE = 0,
48     WAKEUP_STATE = 1,
49     IDLE_STATE = 2,
50     UNKOWN_STATE
51 } WaylandWindowSystemStates;
52
53 } // extern "C"
54
55 struct native_surface;
56
57 class WaylandBaseWindowSystem: public BaseWindowSystem
58 {
59 public:
60     WaylandBaseWindowSystem(const char* displayname, int width, int height, Scene* pScene);
61     virtual ~WaylandBaseWindowSystem();
62     bool init(BaseGraphicSystem<EGLNativeDisplayType, EGLNativeWindowType>* sys);
63     bool start();
64     void stop();
65     void signalRedrawEvent();
66     void wakeUpRendererThread();
67     void setSystemState(WaylandWindowSystemStates state);       // TODO:don't check state
68     WaylandWindowSystemStates getSystemState();
69     struct wl_display* getNativeDisplayHandle();
70     virtual void allocatePlatformSurface(Surface *surface);
71     void doScreenShot(std::string fileName);
72     void doScreenShotOfLayer(std::string fileName, const uint id);
73     void doScreenShotOfSurface(std::string fileName, const uint id, const uint layer_id);
74
75 protected:
76     struct wl_display* m_wlDisplay;
77     pthread_t renderThread;
78     pthread_mutex_t run_lock;
79     BaseGraphicSystem<EGLNativeDisplayType, EGLNativeWindowType>* graphicSystem;
80     virtual void CheckRedrawAllLayers();
81     virtual void RedrawAllLayers();
82     virtual bool initCompositor();
83     struct wl_shm* m_wlShm;
84     virtual bool createNativeContext() = 0;
85     virtual bool initGraphicSystem() = 0;
86
87     bool m_initialized;
88     ScreenShotType m_takeScreenshot;
89     const char* m_displayname;
90     bool m_success;
91     WaylandWindowSystemStates m_systemState;
92     uint m_manageConnectionId;
93     std::string m_screenShotFile;
94     uint m_screenShotSurfaceID;
95     uint m_screenShotLayerID;
96     bool m_debugMode;
97     bool m_error;
98     int m_width;
99     int m_height;
100
101     struct wl_list m_listFrameCallback; // TODO:confirm:what's this
102     struct wl_event_source* m_idleSource; // TODO:confirm:what's this
103     int m_idleTime; // TODO:confirm:what's this
104
105     void createServerinfo(WaylandBaseWindowSystem* windowSystem);
106     void addIdleEventRepaint(); // TODO:confirm:what's this
107     struct native_surface* createNativeSurface();
108     void postReleaseBuffer(struct wl_buffer *buffer);
109     void attachBufferToNativeSurface(struct wl_buffer* buffer, struct wl_surface* surface); // ADIT TODO:reconfirm!!
110     void repaint(int msecs);
111     void cleanup();
112     void Screenshot();
113     void Redraw();
114     void shutdownCompositor();
115     Surface* getSurfaceFromNativeSurface(struct native_surface* nativeSurface);
116     void checkForNewSurface();
117     void printDebug();
118     void* eventLoop();
119
120     static void bindCompositor(struct wl_client* client, void* data, uint32_t version, uint32_t id);
121     static int signalEventOnTerm(int signal_number, void *data);
122     static void destroyListenerSurfaceBuffer(struct wl_listener* listener, struct wl_resource* resource, uint32_t time);
123     static int timerEventIdle(void *data);
124     static void idleEventRepaint(void *data);
125
126 public:
127     static void serverinfoIFCreateConnection(struct wl_client *client, struct wl_resource *resource);
128     static void bindServerinfo(struct wl_client *client, void *data, uint32_t version, uint32_t id);
129     static uint32_t getTime(void);
130     static void destroySurfaceCallback(struct wl_resource* resource);
131     static void destroyFrameCallback(struct wl_resource *resource);
132     static void* eventLoopCallback(void* ptr);
133
134     // wl_surface interface
135     static void surfaceIFDestroy(struct wl_client *client, struct wl_resource *resource);
136     static void surfaceIFAttach(struct wl_client *client, struct wl_resource *resource, struct wl_resource *buffer_resource, int32_t x, int32_t y);
137     static void surfaceIFDamage(struct wl_client *client, struct wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height);
138     static void surfaceIFFrame(struct wl_client *client, struct wl_resource *resource, uint32_t callback);
139
140     // wl_compositor interface
141     static void compositorIFCreateSurface(struct wl_client *client, struct wl_resource* resource, uint32_t id);
142
143     // wl_shm interface
144     static void shmIFBufferCreated(struct wl_buffer *buffer);
145     static void shmIFBufferDamaged(struct wl_buffer *buffer, int32_t x, int32_t y, int32_t width, int32_t height);
146     static void shmIFBufferDestroyed(struct wl_buffer *buffer);
147
148     struct wl_list m_connectionList;
149 };
150
151 inline void WaylandBaseWindowSystem::setSystemState (WaylandWindowSystemStates state)
152 {
153     m_systemState = state;
154 };
155 inline WaylandWindowSystemStates WaylandBaseWindowSystem::getSystemState ()
156 {
157     return m_systemState;
158 };
159
160 inline struct wl_display* WaylandBaseWindowSystem::getNativeDisplayHandle()
161 {
162     return m_wlDisplay;
163 }
164
165 extern "C" {
166     struct native_surface {
167         struct wl_surface surface;
168         WaylandBaseWindowSystem* windowSystem;
169         struct wl_buffer* buffer;
170         int connectionId;
171
172         struct wl_list buffer_link; // TODO confirm:it's necessary or not
173         uint32_t visual; // TODO confirm:it's necessary or not
174         struct wl_listener buffer_destroy_listener; // TODO it's necessary or not
175         struct wl_list link;
176     };
177
178     struct native_frame_callback {
179         struct wl_resource resource;
180         struct wl_list link;
181     };
182
183     struct native_process;
184     typedef void (*cleanupFuncForNativeProcess)(struct native_process* process, int status);
185
186     struct native_process {
187         pid_t pid;
188         cleanupFuncForNativeProcess cleanup;
189         struct wl_list link;
190     };
191 }
192
193 #endif /* _WAYLANDWINDOWSYSTEM_H_ */