ScreenDump: Renderers: add screenId in doScreenShot function
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Platform / X11GLESRenderer / src / X11GLESRenderer.cpp
1 /***************************************************************************
2 *
3 * Copyright 2010 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 #include "X11GLESRenderer.h"
21 #include "Configuration.h"
22 #include "Shader.h"
23 #include "ShaderProgramGLES.h"
24 #include "X11/Xlib.h"
25 #include "TextureBinders/X11CopyGLES.h"
26 #include "TextureBinders/X11EglImage.h"
27 #include <pthread.h>
28 #include <signal.h>
29
30 X11GLESRenderer::X11GLESRenderer(ICommandExecutor& executor, Configuration& config)
31 : BaseRenderer(executor, config)
32 , m_pWindowSystem(0)
33 , m_pGraphicSystem(0)
34 , m_width(0)
35 , m_height(0)
36 , m_binder(NULL)
37 {
38     LOG_DEBUG("X11GLESRenderer", "Creating Renderer");
39 }
40
41 bool X11GLESRenderer::start(int width, int height, const char* displayname, int maxIterationDurationInMS)
42 {
43     Display* nativeDisplayHandle = NULL;
44     EGLDisplay eglDisplayhandle = NULL;
45     m_binder = NULL;
46     m_width = width;
47     m_height = height;
48
49     // add default screen
50     LmScreenList& screenList = m_pScene->getScreenList();
51     LmScreen* lmScreen = new LmScreen();
52     screenList.push_back(lmScreen);
53
54     // create X11 windows, register as composite manager etc
55     m_pWindowSystem = new X11WindowSystem(displayname, width, height, m_pScene, m_pInputManager);
56     m_pGraphicSystem = new GLESGraphicsystem(width, height, ShaderProgramGLES::createProgram);
57
58     if (!m_pWindowSystem->init(m_pGraphicSystem))
59     {
60         goto fail; // TODO bad style
61     }
62
63     m_pGraphicSystem->setBaseWindowSystem(m_pWindowSystem);
64
65     // create graphic context from window, init egl etc
66     nativeDisplayHandle = m_pWindowSystem->getNativeDisplayHandle();
67
68     LOG_DEBUG("X11GLESRenderer", "Got nativedisplay handle: " << nativeDisplayHandle << " from windowsystem");
69
70     eglDisplayhandle = m_pGraphicSystem->getEGLDisplay();
71
72 #ifdef WITH_FORCE_COPY
73     m_binder = new X11CopyGLES(eglDisplayhandle, nativeDisplayHandle);
74 #else // WITH_FORCE_COPY
75 #ifdef EGL_NATIVE_PIXMAP_KHR
76     m_binder = new X11EglImage(eglDisplayhandle, nativeDisplayHandle);
77 #else // EGL_NATIVE_PIXMAP_KHR
78     m_binder = new X11CopyGLES(eglDisplayhandle, nativeDisplayHandle);
79 #endif // EGL_NATIVE_PIXMAP_KHR
80 #endif // WITH_FORCE_COPY
81     if (m_binder && nativeDisplayHandle && eglDisplayhandle)
82     {
83         m_pGraphicSystem->setTextureBinder(m_binder);
84
85         if (!m_pWindowSystem->start(maxIterationDurationInMS))
86         {
87             goto fail; // TODO bad style
88         }
89     }
90     else
91     {
92         goto fail; // TODO bad style
93     }
94     return true;
95
96 fail: // TODO bad style
97
98     LOG_ERROR("X11GLESRenderer", "Initialization failed !");
99     return false;
100 }
101
102 void X11GLESRenderer::stop()
103 {
104     m_pWindowSystem->stop();
105     delete m_pGraphicSystem;
106     delete m_pWindowSystem;
107     if (m_binder)
108     {
109         delete m_binder;
110     }
111 }
112
113 void X11GLESRenderer::doScreenShot(std::string fileToSave, uint screen_id)
114 {
115     m_pWindowSystem->doScreenShot(fileToSave, screen_id);
116 }
117
118 void X11GLESRenderer::doScreenShotOfLayer(std::string fileToSave, uint id)
119 {
120     m_pWindowSystem->doScreenShotOfLayer(fileToSave, id);
121 }
122
123 void X11GLESRenderer::doScreenShotOfSurface(std::string fileToSave, uint id, uint layer_id)
124 {
125     m_pWindowSystem->doScreenShotOfSurface(fileToSave, id, layer_id);
126 }
127
128 uint X11GLESRenderer::getNumberOfHardwareLayers(uint screenID)
129 {
130     (void)screenID;
131
132     return 0; // TODO provide real value here
133 }
134
135 uint* X11GLESRenderer::getScreenResolution(uint screenID)
136 {
137     (void)screenID;
138
139     // TODO provide value of real screen here
140     uint * resolution = new uint[2];
141     resolution[0] = m_width;
142     resolution[1] = m_height;
143     return resolution;
144 }
145
146 uint* X11GLESRenderer::getScreenIDs(uint* length)
147 {
148     Display* x11Display = m_pWindowSystem->getNativeDisplayHandle();
149     if (!x11Display)
150     {
151         return NULL;
152     }
153     // Screens in X11 can be addresses/accessed by just the number - we must only know how many there are
154     uint numberOfScreens = ScreenCount(x11Display);
155     uint* screenIDS = new uint[numberOfScreens];
156     for (uint i = 0; i < numberOfScreens; ++i)
157     {
158         screenIDS[i] = i;
159     }
160     *length = numberOfScreens;
161     return screenIDS;
162 }
163
164 void X11GLESRenderer::signalWindowSystemRedraw()
165 {
166     m_pWindowSystem->signalRedrawEvent();
167 }
168
169 void X11GLESRenderer::forceCompositionWindowSystem()
170 {
171     m_pWindowSystem->m_forceComposition = true;
172 }
173
174 bool X11GLESRenderer::setOptimizationMode(OptimizationType id, OptimizationModeType mode)
175 {
176     return m_pGraphicSystem->setOptimizationMode(id, mode);
177 }
178
179 bool X11GLESRenderer::getOptimizationMode(OptimizationType id, OptimizationModeType *mode)
180 {
181     return m_pGraphicSystem->getOptimizationMode(id, mode);
182 }
183
184 t_ilm_const_string X11GLESRenderer::pluginGetName() const
185 {
186     return "X11GLESRenderer";
187 }
188
189 Shader* X11GLESRenderer::createShader(const string* vertexName, const string* fragmentName)
190 {
191     Shader *result = NULL;
192     m_pWindowSystem->setSystemState(WAKEUP_STATE);
193     m_pWindowSystem->wakeUpRendererThread();
194     m_pScene->unlockScene();
195     while (m_pWindowSystem->getSystemState() != IDLE_STATE);
196     m_pGraphicSystem->activateGraphicContext();
197     result = Shader::createShader(*vertexName, *fragmentName);
198     m_pGraphicSystem->releaseGraphicContext();
199     m_pWindowSystem->setSystemState(WAKEUP_STATE);
200     m_pScene->lockScene();
201     return result;
202 }
203
204 int X11GLESRenderer::getIterationCounter()
205 {
206     return m_pWindowSystem->getIterationCounter();
207 }
208
209 DECLARE_LAYERMANAGEMENT_PLUGIN(X11GLESRenderer)