Bugfix and SceneProviderExample added
[profile/ivi/layer-management.git] / LayerManagerService / src / Layermanager.cpp
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 #include "Layermanager.h"
21 #include "Log.h"
22 #include <iomanip>
23 #include "LayerList.h"
24 #include "ICommand.h"
25 #include "ICommunicator.h"
26 #include "IRenderer.h"
27 #include "ISceneProvider.h"
28
29 Layermanager::Layermanager()
30 {
31     m_pScene = new Scene();
32     m_pRendererList = new RendererList();
33     m_pCommunicatorList = new CommunicatorList();
34     m_pSceneProviderList = new SceneProviderList();
35 }
36
37 Layermanager::~Layermanager()
38 {
39     if (m_pScene)
40     {
41         delete m_pScene;
42     }
43
44     if (m_pRendererList)
45     {
46         delete m_pRendererList;
47     }
48
49     if (m_pCommunicatorList)
50     {
51         delete m_pCommunicatorList;
52     }
53 }
54
55 uint Layermanager::getLayerTypeCapabilities(const LayerType layertype) const
56 {
57     uint capabilities = 0;
58
59     // get the first renderer for now TODO
60     // change to possibly load multiple renderers
61
62     IRenderer* renderer = *m_pRendererList->begin();
63     if (renderer)
64     {
65         capabilities = renderer->getLayerTypeCapabilities(layertype);
66     }
67
68     return capabilities;
69 }
70
71 uint Layermanager::getNumberOfHardwareLayers(const uint screenID) const
72 {
73     uint numberOfHardwareLayers = 0;
74
75     // get the first renderer for now TODO
76     // change to possibly load multiple renderers
77     IRenderer* renderer = *m_pRendererList->begin();
78
79     if (renderer)
80     {
81         numberOfHardwareLayers = renderer->getNumberOfHardwareLayers(screenID);
82     }
83
84     return numberOfHardwareLayers;
85 }
86
87 uint* Layermanager::getScreenResolution(const uint screenID) const
88 {
89     uint* returnValue = 0;
90
91     // get the first renderer for now TODO
92     // change to possibly load multiple renderers
93     IRenderer* renderer = *m_pRendererList->begin();
94
95     if (renderer)
96     {
97         returnValue = renderer->getScreenResolution(screenID);
98     }
99
100     return returnValue;
101 }
102
103 uint* Layermanager::getScreenIDs(uint* length) const
104 {
105     uint* returnValue = 0;
106
107     // get the first renderer for now TODO
108     // change to possibly load multiple renderers
109     IRenderer* renderer = *m_pRendererList->begin();
110     if (renderer)
111     {
112         returnValue = renderer->getScreenIDs(length);
113     }
114
115     return returnValue;
116 }
117
118 // This function is used to propagate a signal to the window system
119 // via the renderer for a redraw because of a rendering property change
120 void Layermanager::signalRendererRedraw()
121 {
122         IRenderer* renderer = *m_pRendererList->begin();
123         if (NULL!=renderer)
124                 renderer->signalWindowSystemRedraw();
125 }
126 void Layermanager::addRenderer(IRenderer* renderer)
127 {
128     if (renderer)
129     {
130         m_pRendererList->push_back(renderer);
131     }
132
133 }
134
135 void Layermanager::removeRenderer(IRenderer* renderer)
136 {
137     if (renderer)
138     {
139         m_pRendererList->remove(renderer);
140     }
141 }
142
143 void Layermanager::addSceneProvider(ISceneProvider* sceneProvider)
144 {
145     if (sceneProvider)
146     {
147         m_pSceneProviderList->push_back(sceneProvider);
148     }
149
150 }
151
152 void Layermanager::removeSceneProvider(ISceneProvider* sceneProvider)
153 {
154     if (sceneProvider)
155     {
156         m_pSceneProviderList->remove(sceneProvider);
157     }
158 }
159
160 void Layermanager::addCommunicator(ICommunicator* communicator)
161 {
162     if (communicator)
163     {
164         m_pCommunicatorList->push_back(communicator);
165     }
166 }
167
168 void Layermanager::removeCommunicator(ICommunicator* communicator)
169 {
170     if (communicator)
171     {
172         m_pCommunicatorList->remove(communicator);
173     }
174 }
175
176 void Layermanager::printDebugInformation() const
177 {
178     // print stuff about layerlist
179     LOG_INFO("LayerManager", "Layer: ID |  X |  Y |  W |  H |Al.| Z \n");
180
181     unsigned int length;
182     unsigned int* LayerIDs;
183
184     // loop the layers
185     m_pScene->getLayerIDs(&length, &LayerIDs);
186
187     for (unsigned int i = 0; i < length; i++)
188     {
189         Layer* currentLayer = m_pScene->getLayer(LayerIDs[i]);
190         LOG_INFO("LayerManager", "      " << std::setw(4) << currentLayer->getID() << "\n");
191
192         LOG_INFO("LayerManager", "    Surface:  ID |Al.| Z |  SVP: X |  Y |  W |  H     DVP:  X |  Y |  W |  H \n");
193
194         // loop the surfaces of within each layer
195         SurfaceListConstIterator iter = currentLayer->getAllSurfaces().begin();
196         SurfaceListConstIterator iterEnd = currentLayer->getAllSurfaces().end();
197
198         for (; iter != iterEnd; ++iter)
199         {
200             LOG_INFO("LayerManager", "            " << std::setw(4) << (*iter)->getID() << std::setw(4) << std::setprecision(3) << (*iter)->opacity << "\n");
201         }
202     }
203 }
204
205 bool Layermanager::executeCommand(ICommand* commandToBeExecuted)
206 {
207     ExecutionResult status = ExecutionFailed;
208     LOG_INFO("Layermanager", "executing command: " << commandToBeExecuted->getString());
209     m_pScene->lockScene();
210     status = commandToBeExecuted->execute(this);
211     m_pScene->unlockScene();
212
213     if (status == ExecutionFailedRedraw || status == ExecutionSuccessRedraw)
214     {
215         // either a command that has changed the output was executed, or
216         // commit command was executed which contained commands that requested redraw was executed.
217         signalRendererRedraw();
218     }
219
220     return (status == ExecutionSuccess || status == ExecutionSuccessRedraw);
221 }
222
223 bool Layermanager::enqueueCommand(ICommand* commandToBeExecuted)
224 {
225     LOG_INFO("Layermanager", "add command to queue: " << commandToBeExecuted->getString());
226     m_pScene->lockScene();
227     m_pScene->m_toBeCommittedList.push_back(commandToBeExecuted);
228     m_pScene->unlockScene();
229     return true; // TODO
230 }
231
232 bool Layermanager::execute(ICommand* commandToBeExecuted)
233 {
234     bool status = false;
235
236     ExecutionType type = commandToBeExecuted->getExecutionType();
237
238     switch (type)
239     {
240     case ExecuteSynchronous:
241         status = executeCommand(commandToBeExecuted);
242         delete commandToBeExecuted;
243         break;
244
245     case ExecuteAsynchronous:
246         status = enqueueCommand(commandToBeExecuted);
247         break;
248
249     default:
250         // TODO: error
251         LOG_ERROR("Layermanager", "Command " << commandToBeExecuted->getString() << " was not processed");
252         break;
253     }
254
255     return status;
256 }
257
258 bool Layermanager::startAllRenderers(const int width, const int height,
259         const char *displayName)
260 {
261     bool allStarted = false;
262
263     RendererListIterator iter = m_pRendererList->begin();
264     RendererListIterator iterEnd = m_pRendererList->end();
265     for (; iter != iterEnd; ++iter)
266     {
267         IRenderer* renderer = *iter;
268         if (renderer)
269         {
270             allStarted = renderer->start(width, height, displayName);
271         }
272         if (renderer == NULL || allStarted == false)
273         {
274           break;   
275         }
276     }
277     if (!allStarted)
278     {
279         LOG_ERROR("LayerManager","Could not start Compositing Controllers");
280     }
281     return allStarted;
282 }
283
284 bool Layermanager::delegateScene()
285 {
286     bool allStarted = true;
287
288     SceneProviderListIterator iter = m_pSceneProviderList->begin();
289     SceneProviderListIterator iterEnd = m_pSceneProviderList->end();
290
291     for (; iter != iterEnd; ++iter)
292     {
293         ISceneProvider* sceneProvider = *iter;
294         if (sceneProvider)
295         {
296             allStarted &= sceneProvider->delegateScene();
297         }
298         else
299         {
300             allStarted = false;
301         }
302     }
303     if (!allStarted)
304     {
305         LOG_WARNING("LayerManager","Could not start delegate Scenes, inital Scene is lost");
306     }
307     return allStarted;
308 }
309
310 bool Layermanager::startAllCommunicators()
311 {
312     bool allStarted = true;
313
314     CommunicatorListIterator communicatorIter = m_pCommunicatorList->begin();
315     CommunicatorListIterator communicatorIterEnd = m_pCommunicatorList->end();
316
317     for (; communicatorIter != communicatorIterEnd; ++communicatorIter)
318     {
319         ICommunicator *communicator = *communicatorIter;
320         if (communicator)
321         {
322             allStarted &= communicator->start();
323         }
324         else
325         {
326             allStarted = false;
327         }
328     }
329     if (!allStarted)
330     {
331         LOG_ERROR("LayerManager","Could not start Communication Controllers");
332     }
333     return allStarted;
334 }
335
336 bool Layermanager::startManagement(const int width, const int height,
337         const char* displayName)
338 {
339     bool renderersStarted = startAllRenderers(width, height, displayName);
340     bool communicatorsStarted = startAllCommunicators();
341     /* only call the initial layerscenery if all needed plugins are 
342      * successfully started */
343      
344     if (renderersStarted && communicatorsStarted)
345     {
346         delegateScene();
347     }
348     return renderersStarted && communicatorsStarted;
349 }
350
351 void Layermanager::stopAllRenderers()
352 {
353     RendererListIterator iter = m_pRendererList->begin();
354     RendererListIterator iterEnd = m_pRendererList->end();
355     for (; iter != iterEnd; ++iter)
356     {
357         IRenderer *renderer = *iter;
358         if (renderer)
359         {
360             renderer->stop();
361         }
362     }
363 }
364
365 void Layermanager::stopAllCommunicators()
366 {
367     CommunicatorListIterator iter = m_pCommunicatorList->begin();
368     CommunicatorListIterator iterEnd = m_pCommunicatorList->end();
369     for (; iter != iterEnd; ++iter)
370     {
371         ICommunicator *communicator = *iter;
372         if (communicator)
373         {
374             communicator->stop();
375         }
376     }
377 }
378
379 bool Layermanager::stopManagement()
380 {
381     stopAllRenderers();
382     stopAllCommunicators();
383     return true; // TODO
384 }
385