2 * Copyright (c) 2014 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"
22 #include <boost/thread.hpp>
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>
42 const char* DALI_TEMP_UPDATE_FPS_FILE( "/tmp/dalifps.txt" );
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
47 } // unnamed namespace
49 UpdateThread::UpdateThread( UpdateRenderSynchronization& sync,
50 AdaptorInternalServices& adaptorInterfaces,
51 const EnvironmentOptions& environmentOptions )
52 : mUpdateRenderSync( sync ),
53 mCore( adaptorInterfaces.GetCore()),
54 mFpsTrackingSeconds( fabsf( environmentOptions.GetFrameRateLoggingFrequency() ) ),
57 mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
58 mStatusLogCount( 0u ),
60 mEnvironmentOptions( environmentOptions )
64 UpdateThread::~UpdateThread()
66 if( mFpsTrackingSeconds > 0.f )
73 void UpdateThread::Start()
75 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Start()\n");
78 // Create and run the update-thread
79 mThread = new boost::thread( boost::bind( &UpdateThread::Run, this ) );
83 void UpdateThread::Stop()
85 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Stop()\n");
88 // wait for the thread to finish
96 bool UpdateThread::Run()
98 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
99 Integration::UpdateStatus status;
101 // install a function for logging
102 mEnvironmentOptions.InstallLogFunction();
104 bool running( true );
106 // Update loop, we stay inside here while the update-thread is running
109 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - Sync()\n");
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");
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 );
121 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - Update(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
123 mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
125 if( mFpsTrackingSeconds > 0.f )
127 FPSTracking(status.SecondsFromLastFrame());
130 bool renderNeedsUpdate;
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");
139 unsigned int keepUpdatingStatus = status.KeepUpdating();
141 // Optional logging of update/render status
142 if ( mStatusLogInterval )
144 UpdateStatusLogging( keepUpdatingStatus, renderNeedsUpdate );
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;
154 DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 5 - Nothing to update, trying to sleep\n");
156 running = mUpdateRenderSync.UpdateTryToSleep();
161 // uninstall a function for logging
162 mEnvironmentOptions.UnInstallLogFunction();
167 void UpdateThread::FPSTracking( float secondsFromLastFrame )
169 if ( mElapsedTime < mFpsTrackingSeconds )
171 mElapsedTime += secondsFromLastFrame;
182 void UpdateThread::OutputFPSRecord()
184 float fps = mFrameCount / mElapsedTime;
185 DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps );
187 // Dumps out the frame rate.
188 FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w" );
192 snprintf(fpsString,sizeof(fpsString),"%.2f \n", fps );
193 fputs( fpsString, outfile ); // ignore the error on purpose
198 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus, bool renderNeedsUpdate )
200 DALI_ASSERT_ALWAYS( mStatusLogInterval );
204 if ( !(++mStatusLogCount % mStatusLogInterval) )
206 oss = "UpdateStatusLogging keepUpdating: " + keepUpdatingStatus ? "true":"false";
208 if ( keepUpdatingStatus )
213 if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
215 oss += "<Stage::KeepRendering() used> ";
218 if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
220 oss += "<Animations running> ";
223 if ( keepUpdatingStatus & Integration::KeepUpdating::DYNAMICS_CHANGED )
225 oss += "<Dynamics running> ";
228 if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
230 oss += "<Resources loading> ";
233 if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
235 oss += "<Monitoring performance> ";
238 if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
240 oss += "<Render task waiting for completion> ";
243 if ( renderNeedsUpdate )
245 oss += "<Render needs Update> ";
248 DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
252 } // namespace Adaptor
254 } // namespace Internal