Health: added thread dead-lock detection
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / include / WindowSystems / X11WindowSystem.h
1 /***************************************************************************
2  *
3  * Copyright 2010,2011 BMW Car IT GmbH
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ****************************************************************************/
19
20 #ifndef _X11WINDOWSYSTEM_H_
21 #define _X11WINDOWSYSTEM_H_
22 #include "WindowSystems/BaseWindowSystem.h"
23 #include "GraphicSystems/BaseGraphicSystem.h"
24 #include "X11/Xlib.h"
25 #include "Surface.h"
26 #include "PlatformSurfaces/XPlatformSurface.h"
27 #include <X11/Xutil.h>
28 #include <X11/extensions/Xfixes.h>
29 #include "Log.h"
30 #include "ScreenShotType.h"
31
32
33 typedef XVisualInfo* (*GetVisualInfoFunction)(Display *dpy);
34
35 typedef enum x11WindowSystemStates
36 {
37     REDRAW_STATE = 0,
38     WAKEUP_STATE = 1,
39     IDLE_STATE = 2,
40     UNKOWN_STATE
41 } X11WindowSystemStates;
42
43 class X11WindowSystem : public BaseWindowSystem
44 {
45 public:
46     X11WindowSystem(const char* displayname,
47                     int width,
48                     int height,
49                     Scene* pScene,
50                     InputManager* pInputManager,
51                     GetVisualInfoFunction func = X11WindowSystem::getDefaultVisual);
52     virtual ~X11WindowSystem();
53     bool init(BaseGraphicSystem<Display*, Window>* sys);
54     bool start(int maxIterationDurationInMS);
55     void stop();
56     static XVisualInfo * getDefaultVisual(Display *dpy);
57     void signalRedrawEvent();
58     void wakeUpRendererThread();
59     void setSystemState(X11WindowSystemStates state);
60     X11WindowSystemStates getSystemState();
61     Display* getNativeDisplayHandle();
62     Window getCompositorNativeWindowHandle();
63     virtual void allocatePlatformSurface(Surface *surface);
64     virtual void deallocatePlatformSurface(Surface *surface);
65     void doScreenShot(std::string fileName);
66     void doScreenShotOfLayer(std::string fileName, const uint id);
67     void doScreenShotOfSurface(std::string fileName, const uint id, const uint layer_id);
68     int getIterationCounter();
69 private:
70     ScreenShotType takeScreenshot;
71     std::string screenShotFile;
72     uint screenShotSurfaceID;
73     uint screenShotLayerID;
74     const char* displayname;
75     GetVisualInfoFunction getVisualFunc;
76     bool debugMode;
77     int resolutionWidth;
78     int resolutionHeight;
79     static int composite_opcode;
80     int composite_event;
81     int composite_error;
82     int composite_major;
83     int composite_minor;
84     static int damage_opcode;
85     int damage_event;
86     int damage_error;
87     int damage_major;
88     int damage_minor;
89     static const char CompositorWindowTitle[];
90     bool m_running;
91 /*    bool m_initialized; */
92     bool m_success;
93     X11WindowSystemStates m_systemState;
94     const char* m_displayEnvironment;
95     int m_iterationCounter;
96     int m_maxIterationDurationInMS;
97
98 protected:
99     Display* x11Display;
100
101     XserverRegion x11DamageRegion;
102     pthread_t renderThread;
103     //void setDisplayMode();
104     int windowWidth;
105     int windowHeight;
106     //    Window background;
107     Window CompositorWindow;
108     XVisualInfo* windowVis;
109     float m_fpsinterval;
110     pthread_mutex_t run_lock;
111     pthread_mutex_t init_lock;
112     pthread_cond_t init_condition;
113     pthread_cond_t run_condition;
114     BaseGraphicSystem<Display*, Window>* graphicSystem;
115     virtual bool initXServer();
116     virtual bool RedrawAllLayers(bool clear, bool swap);
117     virtual void renderHWLayer(Layer* layer);
118     virtual void calculateFps();
119     virtual void calculateSurfaceFps(Surface *currentSurface, float time);
120 private:
121     void cleanup();
122     void Screenshot();
123     void Redraw();
124     bool OpenDisplayConnection();
125     bool checkForCompositeExtension();
126     bool checkForDamageExtension();
127     void createSurfaceForWindow(Window w);
128     void configureSurfaceWindow(Window w);
129     Surface* getSurfaceForWindow(Window w);
130     void checkForNewSurfaceNativeContent();
131     void destroy_surface(Window w);
132     void updateSurface(Surface* s, Window w, XPlatformSurface* x11surf);
133     void MapWindow(Window w);
134     void UnMapWindow(Window w);
135     void NewWindow(Surface* s, Window w);
136     void DestroyWindow(Window w);
137     bool isWindowValid(Window w);
138     bool CreatePixmapsForAllWindows();
139     bool CreateCompositorWindow();
140     void UnredirectSpecialWIndows(Window w);
141
142     void printDebug();
143     void* EventLoop();
144     static int error(Display *dpy, XErrorEvent *ev);
145     void ManageXInputEvent(InputDevice type, InputEventState state, XEvent *pevent);
146     static bool m_xerror;
147
148     friend void * X11eventLoopCallback(void *);
149 };
150
151 inline void X11WindowSystem::setSystemState(X11WindowSystemStates state)
152 {
153     m_systemState = state;
154 }
155
156 inline X11WindowSystemStates X11WindowSystem::getSystemState()
157 {
158     return m_systemState;
159 }
160
161 inline Display* X11WindowSystem::getNativeDisplayHandle()
162 {
163     return x11Display;
164 }
165
166 inline Window X11WindowSystem::getCompositorNativeWindowHandle()
167 {
168     return CompositorWindow;
169 }
170
171 #endif /* _X11WINDOWSYSTEM_H_ */