Extract out FPS Tracker & Update Status Logger from UpdateThread class
[platform/core/uifw/dali-adaptor.git] / adaptors / base / update-thread.cpp
index a1b6a00..d37528c 100644 (file)
@@ -38,8 +38,6 @@ namespace Adaptor
 
 namespace
 {
-const char* DALI_TEMP_UPDATE_FPS_FILE( "/tmp/dalifps.txt" );
-
 #if defined(DEBUG_ENABLED)
 Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_UPDATE_THREAD");
 #endif
@@ -48,13 +46,10 @@ Integration::Log::Filter* gUpdateLogFilter = Integration::Log::Filter::New(Debug
 UpdateThread::UpdateThread( ThreadSynchronization& sync,
                             AdaptorInternalServices& adaptorInterfaces,
                             const EnvironmentOptions& environmentOptions )
-: mThreadSync( sync ),
+: mThreadSynchronization( sync ),
   mCore( adaptorInterfaces.GetCore()),
-  mFpsTrackingSeconds( fabsf( environmentOptions.GetFrameRateLoggingFrequency() ) ),
-  mFrameCount( 0.0f ),
-  mElapsedTime( 0.0f ),
-  mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
-  mStatusLogCount( 0u ),
+  mFpsTracker( environmentOptions ),
+  mUpdateStatusLogger( environmentOptions ),
   mThread( NULL ),
   mEnvironmentOptions( environmentOptions )
 {
@@ -62,10 +57,6 @@ UpdateThread::UpdateThread( ThreadSynchronization& sync,
 
 UpdateThread::~UpdateThread()
 {
-  if( mFpsTrackingSeconds > 0.f )
-  {
-    OutputFPSRecord();
-  }
   Stop();
 }
 
@@ -97,154 +88,54 @@ void UpdateThread::Stop()
 bool UpdateThread::Run()
 {
   DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run()\n");
-  Integration::UpdateStatus status;
 
-  // install a function for logging
+  // Install a function for logging
   mEnvironmentOptions.InstallLogFunction();
 
-  bool running( true );
+  Integration::UpdateStatus status;
+  bool runUpdate = true;
+  float lastFrameDelta( 0.0f );
+  unsigned int lastSyncTime( 0 );
+  unsigned int nextSyncTime( 0 );
 
   // Update loop, we stay inside here while the update-thread is running
-  while ( running )
+  // We also get the last delta and the predict when this update will be rendered
+  while ( mThreadSynchronization.UpdateReady( status.NeedsNotification(), runUpdate, lastFrameDelta, lastSyncTime, nextSyncTime ) )
   {
-    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - Sync()\n");
-
-    // Inform synchronization object update is ready to run, this will pause update thread if required.
-    mThreadSync.UpdateReadyToRun();
-    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Ready()\n");
+    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 1 - UpdateReady(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
 
-    // get the last delta and the predict when this update will be rendered
-    float lastFrameDelta( 0.0f );
-    unsigned int lastSyncTime( 0 );
-    unsigned int nextSyncTime( 0 );
-    mThreadSync.PredictNextSyncTime( lastFrameDelta, lastSyncTime, nextSyncTime );
-
-    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - Update(delta:%f, lastSync:%u, nextSync:%u)\n", lastFrameDelta, lastSyncTime, nextSyncTime);
+    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 2 - Core.Update()\n");
 
+    mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_START );
     mCore.Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
+    mThreadSynchronization.AddPerformanceMarker( PerformanceInterface::UPDATE_END );
 
-    if( mFpsTrackingSeconds > 0.f )
-    {
-      FPSTracking(status.SecondsFromLastFrame());
-    }
-
-    bool renderNeedsUpdate;
+    mFpsTracker.Track( status.SecondsFromLastFrame() );
 
-    // tell the synchronisation class that a buffer has been written to,
-    // and to wait until there is a free buffer to write to
-    running = mThreadSync.UpdateSyncWithRender( status.NeedsNotification(), renderNeedsUpdate );
-    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 4 - UpdateSyncWithRender complete\n");
+    unsigned int keepUpdatingStatus = status.KeepUpdating();
 
-    if( running )
-    {
-      unsigned int keepUpdatingStatus = status.KeepUpdating();
+    // Optional logging of update/render status
+    mUpdateStatusLogger.Log( keepUpdatingStatus );
 
-      // Optional logging of update/render status
-      if ( mStatusLogInterval )
-      {
-        UpdateStatusLogging( keepUpdatingStatus, renderNeedsUpdate );
-      }
+    //  2 things can keep update running.
+    // - The status of the last update
+    // - The status of the last render
+    runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus);
 
-      //  2 things can keep update running.
-      // - The status of the last update
-      // - The status of the last render
-      bool runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus) || renderNeedsUpdate;
+    DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 3 - runUpdate(%d)\n", runUpdate );
 
-      if( !runUpdate )
-      {
-        DALI_LOG_INFO( gUpdateLogFilter, Debug::Verbose, "UpdateThread::Run. 5 - Nothing to update, trying to sleep\n");
-
-        running = mThreadSync.UpdateTryToSleep();
-      }
-    }
+    // Reset time variables
+    lastFrameDelta = 0.0f;
+    lastSyncTime = 0;
+    nextSyncTime = 0;
   }
 
-  // uninstall a function for logging
+  // Uninstall the logging function
   mEnvironmentOptions.UnInstallLogFunction();
 
   return true;
 }
 
-void UpdateThread::FPSTracking( float secondsFromLastFrame )
-{
-  if ( mElapsedTime < mFpsTrackingSeconds )
-  {
-    mElapsedTime += secondsFromLastFrame;
-    mFrameCount += 1.f;
-  }
-  else
-  {
-    OutputFPSRecord();
-    mFrameCount = 0.f;
-    mElapsedTime = 0.f;
-  }
-}
-
-void UpdateThread::OutputFPSRecord()
-{
-  float fps = mFrameCount / mElapsedTime;
-  DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps );
-
-  // Dumps out the frame rate.
-  FILE* outfile = fopen( DALI_TEMP_UPDATE_FPS_FILE, "w" );
-  if( outfile )
-  {
-    char fpsString[10];
-    snprintf(fpsString,sizeof(fpsString),"%.2f \n", fps );
-    fputs( fpsString, outfile ); // ignore the error on purpose
-    fclose( outfile );
-  }
-}
-
-void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus, bool renderNeedsUpdate )
-{
-  DALI_ASSERT_ALWAYS( mStatusLogInterval );
-
-  std::string oss;
-
-  if ( !(++mStatusLogCount % mStatusLogInterval) )
-  {
-    oss = "UpdateStatusLogging keepUpdating: " + keepUpdatingStatus ? "true":"false";
-
-    if ( keepUpdatingStatus )
-    {
-      oss += " because: ";
-    }
-
-    if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
-    {
-      oss += "<Stage::KeepRendering() used> ";
-    }
-
-    if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
-    {
-      oss  +=  "<Animations running> ";
-    }
-
-    if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
-    {
-      oss  +=  "<Resources loading> ";
-    }
-
-    if ( keepUpdatingStatus & Integration::KeepUpdating::MONITORING_PERFORMANCE )
-    {
-      oss += "<Monitoring performance> ";
-    }
-
-    if ( keepUpdatingStatus & Integration::KeepUpdating::RENDER_TASK_SYNC )
-    {
-      oss += "<Render task waiting for completion> ";
-    }
-
-    if ( renderNeedsUpdate )
-    {
-      oss  +=  "<Render needs Update> ";
-    }
-
-    DALI_LOG_UPDATE_STATUS( "%s\n", oss.c_str());
-  }
-}
-
 } // namespace Adaptor
 
 } // namespace Internal