Merge "Doxygen grouping" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / base / update-thread.cpp
1 /*
2  * Copyright (c) 2015 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/thread-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( ThreadSynchronization& sync,
49                             AdaptorInternalServices& adaptorInterfaces,
50                             const EnvironmentOptions& environmentOptions )
51 : mThreadSynchronization( 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
101   // Install a function for logging
102   mEnvironmentOptions.InstallLogFunction();
103
104   Integration::UpdateStatus status;
105   bool runUpdate = true;
106   float lastFrameDelta( 0.0f );
107   unsigned int lastSyncTime( 0 );
108   unsigned int nextSyncTime( 0 );
109
110   // Update loop, we stay inside here while the update-thread is running
111   // We also get the last delta and the predict when this update will be rendered
112   while ( mThreadSynchronization.UpdateReady( status.NeedsNotification(), runUpdate, lastFrameDelta, lastSyncTime, nextSyncTime ) )
113   {
114     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - UpdateReady(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
115
116     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Core.Update()\n");
117
118     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_START );
119     mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
120     mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_END );
121
122     if( mFpsTrackingSeconds > 0.f )
123     {
124       FPSTracking(status.SecondsFromLastFrame());
125     }
126
127     unsigned int keepUpdatingStatus = status.KeepUpdating();
128
129     // Optional logging of update/render status
130     if ( mStatusLogInterval )
131     {
132       UpdateStatusLogging( keepUpdatingStatus );
133     }
134
135     //  2 things can keep update running.
136     // - The status of the last update
137     // - The status of the last render
138     runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus);
139
140     DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - runUpdate(%d)\n", runUpdate );
141
142     // Reset time variables
143     lastFrameDelta = 0.0f;
144     lastSyncTime = 0;
145     nextSyncTime = 0;
146   }
147
148   // Uninstall the logging function
149   mEnvironmentOptions.UnInstallLogFunction();
150
151   return true;
152 }
153
154 void UpdateThread::FPSTracking( float secondsFromLastFrame )
155 {
156   if ( mElapsedTime < mFpsTrackingSeconds )
157   {
158     mElapsedTime += secondsFromLastFrame;
159     mFrameCount += 1.f;
160   }
161   else
162   {
163     OutputFPSRecord();
164     mFrameCount = 0.f;
165     mElapsedTime = 0.f;
166   }
167 }
168
169 void UpdateThread::OutputFPSRecord()
170 {
171   float fps = mFrameCount / mElapsedTime;
172   DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps );
173
174   // Dumps out the frame rate.
175   FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w" );
176   if( outfile )
177   {
178     char fpsString[10];
179     snprintf(fpsString,sizeof(fpsString),"%.2f \n", fps );
180     fputs( fpsString, outfile ); // ignore the error on purpose
181     fclose( outfile );
182   }
183 }
184
185 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus )
186 {
187   DALI_ASSERT_ALWAYS( mStatusLogInterval );
188
189   std::string oss;
190
191   if ( !(++mStatusLogCount % mStatusLogInterval) )
192   {
193     oss = "UpdateStatusLogging keepUpdating: ";
194     oss += (keepUpdatingStatus ? "true":"false");
195
196     if ( keepUpdatingStatus )
197     {
198       oss += " because: ";
199     }
200
201     if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
202     {
203       oss += "<Stage::KeepRendering() used> ";
204     }
205
206     if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
207     {
208       oss  +=  "<Animations running> ";
209     }
210
211     if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
212     {
213       oss  +=  "<Resources loading> ";
214     }
215
216     if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
217     {
218       oss += "<Monitoring performance> ";
219     }
220
221     if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
222     {
223       oss += "<Render task waiting for completion> ";
224     }
225
226     DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
227   }
228 }
229
230 } // namespace Adaptor
231
232 } // namespace Internal
233
234 } // namespace Dali