Initial Version of LayerManagementService added.
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / src / WindowSystems / NullWindowSystem.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 "WindowSystems/NullWindowSystem.h"
21 #include "Log.h"
22 #include "Layer.h"
23 #include <time.h>
24 #include <sys/time.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <iomanip>
30
31
32 int NullWindowSystem::windowWidth= 1280;
33
34
35 int NullWindowSystem::windowHeight= 480;
36
37
38 int NullWindowSystem::resolutionWidth= 1280;
39
40
41 int NullWindowSystem::resolutionHeight= 480;
42
43
44 bool NullWindowSystem::m_initialized= false;
45
46
47 bool NullWindowSystem::m_success= false;
48
49
50 bool NullWindowSystem::debugMode= false;
51
52
53 void NullWindowSystem::printDebug(int posx,int posy){
54         // print stuff about layerlist
55         std::stringstream debugmessage;
56         debugmessage << "Layer:  ID |   X  |   Y  |   W  |   H  | Al. \n";
57         // loop the layers
58         std::list<Layer*> list = layerlist->getCurrentRenderOrder();
59         for(std::list<Layer*>::const_iterator currentLayer = list.begin();
60         currentLayer != list.end(); currentLayer++)
61         {
62                 Rectangle dest = (*currentLayer)->getDestinationRegion();
63                 debugmessage << "            " << std::setw(4) << (*currentLayer)->getID() << " " << std::setw(3) << dest.x << " " << std::setw(3) << dest.y << " " << std::setw(3) << dest.width << " " << std::setw(3) << dest.height << " " << std::setw(3) << (*currentLayer)->opacity << "\n";
64
65                 debugmessage << "    Surface:  ID |Al.|  SVP: X |  Y |  W |  H     DVP:  X |  Y |  W |  H \n";
66                 // loop the surfaces of within each layer
67                 for(std::list<Surface*>::iterator current = (*currentLayer)->surfaces.begin();
68                 current != (*currentLayer)->surfaces.end(); current++)
69                 {
70                         Rectangle src = (*current)->getSourceRegion();
71                         Rectangle dest = (*current)->getDestinationRegion();
72                         debugmessage << "                        " << std::setw(4) << (*current)->getID() << " " << std::setprecision(3) << (*current)->opacity<< " " << std::setw(3) << src.x << " " << std::setw(3) << src.y << " " << std::setw(3) << src.width << " " << std::setw(3) << src.height << " " << std::setw(3) << dest.x << " " << std::setw(3) << dest.y << " " << std::setw(3) << dest.width << " " << std::setw(3) << dest.height  << "\n";
73                 }
74         }
75         LOG_INFO("NullWindowSystem",debugmessage.str());
76 }
77
78 float                   TimeCounter, LastFrameTimeCounter, DT, prevTime = 0.0, FPS;
79 struct timeval          tv, tv0;
80 int                     Frame = 1, FramesPerFPS;
81
82 void UpdateTimeCounter() {
83         LastFrameTimeCounter = TimeCounter;
84         gettimeofday(&tv, NULL);
85         TimeCounter = (float)(tv.tv_sec-tv0.tv_sec) + 0.000001*((float)(tv.tv_usec-tv0.tv_usec));
86         DT = TimeCounter - LastFrameTimeCounter; }
87
88 void CalculateFPS() {
89         Frame ++;
90
91         if((Frame%FramesPerFPS) == 0) {
92                 FPS = ((float)(FramesPerFPS)) / (TimeCounter-prevTime);
93                 prevTime = TimeCounter; } }
94
95
96 void NullWindowSystem::Redraw()
97 {
98 //      LOG_INFO("NullWindowSystem","redraw");
99         // draw all the layers
100         graphicSystem->clearBackground();
101         std::list<Layer*> layers = layerlist->getCurrentRenderOrder();
102         for(std::list<Layer*>::const_iterator current = layers.begin(); current != layers.end(); current++){
103                 Layer* currentLayer = (Layer*)*current;
104                 if ((*currentLayer).visibility && (*currentLayer).opacity>0.0f){
105                         // glPushMatrix();
106                         //   glRotated(-90*currentLayer->getOrientation(),0,0,1.0);
107                         // draw all the surfaces of the layer
108                         std::list<Surface*> surfaces = currentLayer->surfaces;
109                         for(std::list<Surface*>::const_iterator currentS = surfaces.begin(); currentS != surfaces.end(); currentS++){
110                                 if ((*currentS)->visibility && (*currentS)->opacity>0.0f){
111                                         Surface* currentSurface = (Surface*)*currentS;
112                                         PlatformSurface* nativeSurface = (PlatformSurface*)currentSurface->platform;
113                                         if (NULL==nativeSurface)
114                                         {
115                                                 LOG_INFO("NullWindowSystem","creating native surface for new window");
116                                                 // this surface does not have a native platform surface attached yet!
117                                                 currentSurface->platform = graphicSystem->m_binder->createPlatformSurface(currentSurface);
118                                                 currentSurface->OriginalSourceWidth=256;
119                                                 currentSurface->OriginalSourceHeight=256;
120                                                 graphicSystem->m_binder->createClientBuffer(currentSurface);
121                                                 LOG_INFO("NullWindowSystem","created native surface for new window");
122                                         }
123                                         graphicSystem->drawSurface(currentLayer,currentSurface);
124
125                                 }
126                         }
127                 }
128         }
129
130         if (debugMode)
131         {
132                 printDebug(200,windowHeight-40);
133         }
134         UpdateTimeCounter();
135         CalculateFPS();
136         char floatStringBuffer[256];
137         sprintf(floatStringBuffer, "FPS: %f", FPS);
138         if ((Frame % 1000 ) == 0)
139         {
140                 LOG_INFO("NullWindowSystem",floatStringBuffer);
141         }
142
143         graphicSystem->swapBuffers();
144
145 }
146
147
148
149 void* NullWindowSystem::EventLoop(void * ptr)
150 {
151         LOG_DEBUG("NullWindowSystem", "Enter thread");
152         NullWindowSystem *windowsys = (NullWindowSystem *) ptr;
153
154         // init own stuff
155         m_success = false;
156
157         // then init graphiclib
158         int display = 0;
159         void* window = NULL;
160         m_success = windowsys->graphicSystem->init(&display,&window,windowHeight,windowWidth);
161
162         m_initialized = true;
163         // start
164 //      // get instance of Renderer for this thread
165         Layer* defaultLayer;
166 //      X11WindowSystem *renderer = (X11WindowSystem *) ptr;
167 //      if (renderer != NULL)
168 //      {
169 //      }
170 //      // initialize the renderer (OpenGL etc)
171 //      renderer->init();
172 //      // run the main event loop while rendering
173         bool running = windowsys->m_success;
174         gettimeofday(&tv0, NULL);
175         FramesPerFPS = 30;
176         if (debugMode)
177         {
178                 defaultLayer = windowsys->layerlist->createLayer();
179                 defaultLayer->setOpacity(1.0);
180                 defaultLayer->setDestinationRegion(Rectangle(0,0,windowsys->resolutionWidth,windowsys->resolutionHeight));
181                 defaultLayer->setSourceRegion(Rectangle(0,0,windowsys->resolutionWidth,windowsys->resolutionHeight));
182                 windowsys->layerlist->getCurrentRenderOrder().push_back(defaultLayer);
183         }
184         LOG_DEBUG("NullWindowSystem", "Enter render loop");
185         while (running)
186         {
187                 windowsys->Redraw();
188         }
189         LOG_INFO("NullWindowSystem", "Renderer thread finished");
190         return NULL;
191 }
192
193
194 bool NullWindowSystem::start(int displayWidth, int displayHeight, const char* DisplayName){
195         LOG_INFO("NullWindowSystem", "Starting / Creating thread");
196         pthread_t renderThread;
197         NullWindowSystem *renderer = this;
198         resolutionWidth = displayWidth;
199         resolutionHeight = displayHeight;
200         int status = pthread_create( &renderThread, NULL, NullWindowSystem::EventLoop, (void*) renderer);
201         if (0 != status )
202         {
203                 return false;
204         }
205         while ( m_initialized == false )
206         {
207                 sleep(1);
208                 LOG_INFO("NullWindowSystem","Waiting start complete " << m_initialized);
209         }
210         LOG_INFO("NullWindowSystem","Start complete " << m_initialized << " success " << m_success);
211         return m_success;
212 }