Idle callback boost removal and tidy up
[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
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
46 #endif
47 } // unnamed namespace
48
49 UpdateThread::UpdateThread( UpdateRenderSynchronization& sync,
50                             AdaptorInternalServices& adaptorInterfaces,
51                             const EnvironmentOptions& environmentOptions )
52 : mUpdateRenderSync( sync ),
53   mCore( adaptorInterfaces.GetCore()),
54   mFpsTrackingSeconds( fabsf( environmentOptions.GetFrameRateLoggingFrequency() ) ),
55   mFrameCount( 0.0f ),
56   mElapsedTime( 0.0f ),
57   mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
58   mStatusLogCount( 0u ),
59   mThread( NULL ),
60   mEnvironmentOptions( environmentOptions )
61 {
62 }
63
64 UpdateThread::~UpdateThread()
65 {
66   if( mFpsTrackingSeconds > 0.f )
67   {
68     OutputFPSRecord();
69   }
70   Stop();
71 }
72
73 void UpdateThread::Start()
74 {
75   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
76   if ( !mThread )
77   {
78     // Create and run the update-thread
79     mThread = new boost::thread( boost::bind( &UpdateThread::Run, this ) );
80   }
81 }
82
83 void UpdateThread::Stop()
84 {
85   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
86   if( mThread )
87   {
88     // wait for the thread to finish
89     mThread->join();
90
91     delete mThread;
92     mThread = NULL;
93   }
94 }
95
96 bool UpdateThread::Run()
97 {
98   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
99   Integration::UpdateStatus status;
100
101   // install a function for logging
102   mEnvironmentOptions.InstallLogFunction();
103
104   bool running( true );
105
106   // Update loop, we stay inside here while the update-thread is running
107   while ( running )
108   {
109     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - Sync()\n");
110
111     // Inform synchronization object update is ready to run, this will pause update thread if required.
112     mUpdateRenderSync.UpdateReadyToRun();
113     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Ready()\n");
114
115     // get the last delta and the predict when this update will be rendered
116     float lastFrameDelta( 0.0f );
117     unsigned int lastSyncTime( 0 );
118     unsigned int nextSyncTime( 0 );
119     mUpdateRenderSync.PredictNextSyncTime( lastFrameDelta, lastSyncTime, nextSyncTime );
120
121     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - Update(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
122
123     mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
124
125     if( mFpsTrackingSeconds > 0.f )
126     {
127       FPSTracking(status.SecondsFromLastFrame());
128     }
129
130     bool renderNeedsUpdate;
131
132     // tell the synchronisation class that a buffer has been written to,
133     // and to wait until there is a free buffer to write to
134     running = mUpdateRenderSync.UpdateSyncWithRender( status.NeedsNotification(), renderNeedsUpdate );
135     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 4 - UpdateSyncWithRender complete\n");
136
137     if( running )
138     {
139       unsigned int keepUpdatingStatus = status.KeepUpdating();
140
141       // Optional logging of update/render status
142       if ( mStatusLogInterval )
143       {
144         UpdateStatusLogging( keepUpdatingStatus, renderNeedsUpdate );
145       }
146
147       //  2 things can keep update running.
148       // - The status of the last update
149       // - The status of the last render
150       bool runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus) || renderNeedsUpdate;
151
152       if( !runUpdate )
153       {
154         DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 5 - Nothing to update, trying to sleep\n");
155
156         running = mUpdateRenderSync.UpdateTryToSleep();
157       }
158     }
159   }
160
161   // uninstall a function for logging
162   mEnvironmentOptions.UnInstallLogFunction();
163
164   return true;
165 }
166
167 void UpdateThread::FPSTracking( float secondsFromLastFrame )
168 {
169   if ( mElapsedTime < mFpsTrackingSeconds )
170   {
171     mElapsedTime += secondsFromLastFrame;
172     mFrameCount += 1.f;
173   }
174   else
175   {
176     OutputFPSRecord();
177     mFrameCount = 0.f;
178     mElapsedTime = 0.f;
179   }
180 }
181
182 void UpdateThread::OutputFPSRecord()
183 {
184   float fps = mFrameCount / mElapsedTime;
185   DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps );
186
187   // Dumps out the frame rate.
188   FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w" );
189   if( outfile )
190   {
191     char fpsString[10];
192     snprintf(fpsString,sizeof(fpsString),"%.2f \n", fps );
193     fputs( fpsString, outfile ); // ignore the error on purpose
194     fclose( outfile );
195   }
196 }
197
198 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus, bool renderNeedsUpdate )
199 {
200   DALI_ASSERT_ALWAYS( mStatusLogInterval );
201
202   std::string oss;
203
204   if ( !(++mStatusLogCount % mStatusLogInterval) )
205   {
206     oss = "UpdateStatusLogging keepUpdating: " + keepUpdatingStatus ? "true":"false";
207
208     if ( keepUpdatingStatus )
209     {
210       oss += " because: ";
211     }
212
213     if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
214     {
215       oss += "<Stage::KeepRendering() used> ";
216     }
217
218     if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
219     {
220       oss  +=  "<Animations running> ";
221     }
222
223     if ( keepUpdatingStatus & Integration::KeepUpdating::DYNAMICS_CHANGED )
224     {
225       oss  +=  "<Dynamics running> ";
226     }
227
228     if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
229     {
230       oss  +=  "<Resources loading> ";
231     }
232
233     if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
234     {
235       oss += "<Monitoring performance> ";
236     }
237
238     if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
239     {
240       oss += "<Render task waiting for completion> ";
241     }
242
243     if ( renderNeedsUpdate )
244     {
245       oss  +=  "<Render needs Update> ";
246     }
247
248     DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
249   }
250 }
251
252 } // namespace Adaptor
253
254 } // namespace Internal
255
256 } // namespace Dali