2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "update-thread.h"
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>
41 const char* DALI_TEMP_UPDATE_FPS_FILE( "/tmp/dalifps.txt" );
43 #if defined(DEBUG_ENABLED)
44 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
46 } // unnamed namespace
48 UpdateThread::UpdateThread( ThreadSynchronization& sync,
49 AdaptorInternalServices& adaptorInterfaces,
50 const EnvironmentOptions& environmentOptions )
51 : mThreadSynchronization( sync ),
52 mCore( adaptorInterfaces.GetCore()),
53 mFpsTrackingSeconds( fabsf( environmentOptions.GetFrameRateLoggingFrequency() ) ),
56 mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
57 mStatusLogCount( 0u ),
59 mEnvironmentOptions( environmentOptions )
63 UpdateThread::~UpdateThread()
65 if( mFpsTrackingSeconds > 0.f )
72 void UpdateThread::Start()
74 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
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" );
84 void UpdateThread::Stop()
86 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
89 // wait for the thread to finish
90 pthread_join(*mThread, NULL);
97 bool UpdateThread::Run()
99 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
101 // Install a function for logging
102 mEnvironmentOptions.InstallLogFunction();
104 Integration::UpdateStatus status;
105 bool runUpdate = true;
106 float lastFrameDelta( 0.0f );
107 unsigned int lastSyncTime( 0 );
108 unsigned int nextSyncTime( 0 );
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 ) )
114 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - UpdateReady(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
116 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Core.Update()\n");
118 mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_START );
119 mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
120 mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_END );
122 if( mFpsTrackingSeconds > 0.f )
124 FPSTracking(status.SecondsFromLastFrame());
127 unsigned int keepUpdatingStatus = status.KeepUpdating();
129 // Optional logging of update/render status
130 if ( mStatusLogInterval )
132 UpdateStatusLogging( keepUpdatingStatus );
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);
140 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - runUpdate(%d)\n", runUpdate );
142 // Reset time variables
143 lastFrameDelta = 0.0f;
148 // Uninstall the logging function
149 mEnvironmentOptions.UnInstallLogFunction();
154 void UpdateThread::FPSTracking( float secondsFromLastFrame )
156 if ( mElapsedTime < mFpsTrackingSeconds )
158 mElapsedTime += secondsFromLastFrame;
169 void UpdateThread::OutputFPSRecord()
171 float fps = mFrameCount / mElapsedTime;
172 DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps );
174 // Dumps out the frame rate.
175 FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w" );
179 snprintf(fpsString,sizeof(fpsString),"%.2f \n", fps );
180 fputs( fpsString, outfile ); // ignore the error on purpose
185 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus )
187 DALI_ASSERT_ALWAYS( mStatusLogInterval );
191 if ( !(++mStatusLogCount % mStatusLogInterval) )
193 oss = "UpdateStatusLogging keepUpdating: ";
194 oss += (keepUpdatingStatus ? "true":"false");
196 if ( keepUpdatingStatus )
201 if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
203 oss += "<Stage::KeepRendering() used> ";
206 if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
208 oss += "<Animations running> ";
211 if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
213 oss += "<Resources loading> ";
216 if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
218 oss += "<Monitoring performance> ";
221 if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
223 oss += "<Render task waiting for completion> ";
226 DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
230 } // namespace Adaptor
232 } // namespace Internal