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