Don't Update twice after a property setter
[platform/core/uifw/dali-adaptor.git] / adaptors / base / update-thread.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "update-thread.h"
20
21 // EXTERNAL INCLUDES
22 #include <boost/thread.hpp>
23 #include <cstdio>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/debug.h>
27 #include <base/interfaces/adaptor-internal-services.h>
28 #include <base/update-render-synchronization.h>
29 #include <base/environment-options.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42 const char* DALI_TEMP_UPDATE_FPS_FILE( "/tmp/dalifps.txt" );
43 const unsigned int MICROSECONDS_PER_MILLISECOND( 1000 );
44
45 #if defined(DEBUG_ENABLED)
46 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
47 #endif
48 } // unnamed namespace
49
50 UpdateThread::UpdateThread( UpdateRenderSynchronization& sync,
51                             AdaptorInternalServices& adaptorInterfaces,
52                             const EnvironmentOptions& environmentOptions )
53 : mUpdateRenderSync( sync ),
54   mCore( adaptorInterfaces.GetCore()),
55   mFpsTrackingSeconds( environmentOptions.GetFrameRateLoggingFrequency() ),
56   mElapsedTime( 0.0f ),
57   mElapsedSeconds( 0u ),
58   mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
59   mStatusLogCount( 0u ),
60   mThread( NULL ),
61   mEnvironmentOptions( environmentOptions )
62 {
63   if( mFpsTrackingSeconds > 0 )
64   {
65     mFpsRecord.resize( mFpsTrackingSeconds, 0.0f );
66   }
67 }
68
69 UpdateThread::~UpdateThread()
70 {
71   if(mFpsTrackingSeconds > 0)
72   {
73     OutputFPSRecord();
74   }
75   Stop();
76 }
77
78 void UpdateThread::Start()
79 {
80   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
81   if ( !mThread )
82   {
83     // Create and run the update-thread
84     mThread = new boost::thread( boost::bind( &UpdateThread::Run, this ) );
85   }
86 }
87
88 void UpdateThread::Stop()
89 {
90   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
91   if( mThread )
92   {
93     // wait for the thread to finish
94     mThread->join();
95
96     delete mThread;
97     mThread = NULL;
98   }
99 }
100
101 bool UpdateThread::Run()
102 {
103   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
104   Integration::UpdateStatus status;
105
106   // install a function for logging
107   mEnvironmentOptions.InstallLogFunction();
108
109   bool running( true );
110
111   // Update loop, we stay inside here while the update-thread is running
112   while ( running )
113   {
114     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - Sync()\n");
115
116     // Inform synchronization object update is ready to run, this will pause update thread if required.
117     mUpdateRenderSync.UpdateReadyToRun();
118     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Ready()\n");
119
120     // get the last delta and the predict when this update will be rendered
121     float lastFrameDelta( 0.0f );
122     unsigned int lastSyncTime( 0 );
123     unsigned int nextSyncTime( 0 );
124     mUpdateRenderSync.PredictNextSyncTime( lastFrameDelta, lastSyncTime, nextSyncTime );
125
126     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - Update(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
127
128     mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
129
130     if( mFpsTrackingSeconds > 0 )
131     {
132       FPSTracking(status.SecondsFromLastFrame());
133     }
134
135     bool renderNeedsUpdate;
136
137     // tell the synchronisation class that a buffer has been written to,
138     // and to wait until there is a free buffer to write to
139     running = mUpdateRenderSync.UpdateSyncWithRender( status.NeedsNotification(), renderNeedsUpdate );
140     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 4 - UpdateSyncWithRender complete\n");
141
142     if( running )
143     {
144       unsigned int keepUpdatingStatus = status.KeepUpdating();
145
146       // Optional logging of update/render status
147       if ( mStatusLogInterval )
148       {
149         UpdateStatusLogging( keepUpdatingStatus, renderNeedsUpdate );
150       }
151
152       //  2 things can keep update running.
153       // - The status of the last update
154       // - The status of the last render
155       bool runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus) || renderNeedsUpdate;
156
157       if( !runUpdate )
158       {
159         DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 5 - Nothing to update, trying to sleep\n");
160
161         running = mUpdateRenderSync.UpdateTryToSleep();
162       }
163     }
164   }
165
166   // uninstall a function for logging
167   mEnvironmentOptions.UnInstallLogFunction();
168
169   return true;
170 }
171
172 void UpdateThread::FPSTracking(float secondsFromLastFrame)
173 {
174   if (mElapsedSeconds < mFpsTrackingSeconds)
175   {
176     mElapsedTime += secondsFromLastFrame;
177     if( secondsFromLastFrame  > 1.0 )
178     {
179       int seconds = floor(mElapsedTime);
180       mElapsedSeconds += seconds;
181       mElapsedTime -= static_cast<float>(seconds);
182     }
183     else
184     {
185       if( mElapsedTime>=1.0f )
186       {
187         mElapsedTime -= 1.0f;
188         mFpsRecord[mElapsedSeconds] += 1.0f - mElapsedTime/secondsFromLastFrame;
189         mElapsedSeconds++;
190         mFpsRecord[mElapsedSeconds] += mElapsedTime/secondsFromLastFrame;
191       }
192       else
193       {
194         mFpsRecord[mElapsedSeconds] += 1.0f;
195       }
196     }
197   }
198   else
199   {
200     OutputFPSRecord();
201     mFpsRecord.clear();
202     mFpsTrackingSeconds = 0;
203   }
204 }
205
206 void UpdateThread::OutputFPSRecord()
207 {
208   for(unsigned int i = 0; i < mElapsedSeconds; i++)
209   {
210     DALI_LOG_FPS("fps( %d ):%f\n",i ,mFpsRecord[i]);
211   }
212
213   // Dumps out the DALI_FPS_TRACKING worth of frame rates.
214   // E.g. if we run:   DALI_FPS_TRACKING=30  dali-demo
215   // it will dump out the first 30 seconds of FPS information to a temp file
216   FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w");
217   if( outfile )
218   {
219     for(unsigned int i = 0; i < mElapsedSeconds; i++)
220     {
221       char fpsString[10];
222       snprintf(fpsString,sizeof(fpsString),"%.2f \n",mFpsRecord[i]);
223       int ret = fputs( fpsString, outfile );
224       if( ret < 0)
225       {
226         break;
227       }
228     }
229
230   }
231   fclose( outfile );
232
233 }
234
235 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus, bool renderNeedsUpdate )
236 {
237   DALI_ASSERT_ALWAYS( mStatusLogInterval );
238
239   std::string oss;
240
241   if ( !(++mStatusLogCount % mStatusLogInterval) )
242   {
243     oss = "UpdateStatusLogging keepUpdating: " + keepUpdatingStatus ? "true":"false";
244
245     if ( keepUpdatingStatus )
246     {
247       oss += " because: ";
248     }
249
250     if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
251     {
252       oss += "<Stage::KeepRendering() used> ";
253     }
254
255     if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
256     {
257       oss  +=  "<Animations running> ";
258     }
259
260     if ( keepUpdatingStatus & Integration::KeepUpdating::DYNAMICS_CHANGED )
261     {
262       oss  +=  "<Dynamics running> ";
263     }
264
265     if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
266     {
267       oss  +=  "<Resources loading> ";
268     }
269
270     if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
271     {
272       oss += "<Monitoring performance> ";
273     }
274
275     if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
276     {
277       oss += "<Render task waiting for completion> ";
278     }
279
280     if ( renderNeedsUpdate )
281     {
282       oss  +=  "<Render needs Update> ";
283     }
284
285     DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
286   }
287 }
288
289 } // namespace Adaptor
290
291 } // namespace Internal
292
293 } // namespace Dali