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"
25 #include <boost/thread.hpp>
28 #include <dali/integration-api/debug.h>
29 #include <base/interfaces/adaptor-internal-services.h>
30 #include <base/update-render-synchronization.h>
31 #include <base/environment-options.h>
45 const unsigned int MICROSECONDS_PER_MILLISECOND( 1000 );
47 } // unnamed namespace
49 UpdateThread::UpdateThread( UpdateRenderSynchronization& sync,
50 AdaptorInternalServices& adaptorInterfaces,
51 const EnvironmentOptions& environmentOptions )
52 : mUpdateRenderSync( sync ),
53 mCore( adaptorInterfaces.GetCore()),
54 mFpsTrackingSeconds( environmentOptions.GetFrameRateLoggingFrequency() ),
56 mElapsedSeconds( 0u ),
57 mStatusLogInterval( environmentOptions.GetUpdateStatusLoggingFrequency() ),
58 mStatusLogCount( 0u ),
59 mNotificationTrigger( adaptorInterfaces.GetTriggerEventInterface() ),
61 mEnvironmentOptions( environmentOptions )
63 if( mFpsTrackingSeconds > 0 )
65 mFpsRecord.resize( mFpsTrackingSeconds, 0.0f );
69 UpdateThread::~UpdateThread()
71 if(mFpsTrackingSeconds > 0)
78 void UpdateThread::Start()
82 // Create and run the update-thread
83 mThread = new boost::thread( boost::bind( &UpdateThread::Run, this ) );
87 void UpdateThread::Stop()
91 // wait for the thread to finish
99 bool UpdateThread::Run()
101 Integration::UpdateStatus status;
103 // install a function for logging
104 mEnvironmentOptions.InstallLogFunction();
106 bool running( true );
108 // Update loop, we stay inside here while the update-thread is running
111 // Inform synchronization object update is ready to run, this will pause update thread if required.
112 mUpdateRenderSync.UpdateReadyToRun();
115 mCore.Update( status );
117 if( mFpsTrackingSeconds > 0 )
119 FPSTracking(status.SecondsFromLastFrame());
122 // Do the notifications first so the actor-thread can start processing them
123 if( status.NeedsNotification() )
125 // Tell the event-thread to wake up (if asleep) and send a notification event to Core
126 mNotificationTrigger.Trigger();
129 bool renderNeedsUpdate;
131 // tell the synchronisation class that a buffer has been written to,
132 // and to wait until there is a free buffer to write to
133 running = mUpdateRenderSync.UpdateSyncWithRender( renderNeedsUpdate );
137 unsigned int keepUpdatingStatus = status.KeepUpdating();
139 // Optional logging of update/render status
140 if ( mStatusLogInterval )
142 UpdateStatusLogging( keepUpdatingStatus, renderNeedsUpdate );
145 // 2 things can keep update running.
146 // - The status of the last update
147 // - The status of the last render
148 bool runUpdate = (Integration::KeepUpdating::NOT_REQUESTED != keepUpdatingStatus) || renderNeedsUpdate;
152 running = mUpdateRenderSync.UpdateTryToSleep();
157 // uninstall a function for logging
158 mEnvironmentOptions.UnInstallLogFunction();
163 void UpdateThread::FPSTracking(float secondsFromLastFrame)
165 if (mElapsedSeconds < mFpsTrackingSeconds)
167 mElapsedTime += secondsFromLastFrame;
168 if( secondsFromLastFrame > 1.0 )
170 int seconds = floor(mElapsedTime);
171 mElapsedSeconds += seconds;
172 mElapsedTime -= static_cast<float>(seconds);
176 if( mElapsedTime>=1.0f )
178 mElapsedTime -= 1.0f;
179 mFpsRecord[mElapsedSeconds] += 1.0f - mElapsedTime/secondsFromLastFrame;
181 mFpsRecord[mElapsedSeconds] += mElapsedTime/secondsFromLastFrame;
185 mFpsRecord[mElapsedSeconds] += 1.0f;
193 mFpsTrackingSeconds = 0;
197 void UpdateThread::OutputFPSRecord()
199 for(unsigned int i = 0; i < mElapsedSeconds; i++)
201 DALI_LOG_FPS("fps( %d ):%f\n",i ,mFpsRecord[i]);
203 std::ofstream outFile("/tmp/dalifps.txt");
204 if(outFile.is_open())
206 for(unsigned int i = 0; i < mElapsedSeconds; i++)
208 outFile << mFpsRecord[i]<<std::endl;
214 void UpdateThread::UpdateStatusLogging( unsigned int keepUpdatingStatus, bool renderNeedsUpdate )
216 DALI_ASSERT_ALWAYS( mStatusLogInterval );
218 std::ostringstream oss;
220 if ( !(++mStatusLogCount % mStatusLogInterval) )
222 oss << "UpdateStatusLogging keepUpdating: " << (bool)keepUpdatingStatus << " ";
224 if ( keepUpdatingStatus )
229 if ( keepUpdatingStatus & Integration::KeepUpdating::STAGE_KEEP_RENDERING )
231 oss << "<Stage::KeepRendering() used> ";
234 if ( keepUpdatingStatus & Integration::KeepUpdating::INCOMING_MESSAGES )
236 oss << "<Messages sent to Update> ";
239 if ( keepUpdatingStatus & Integration::KeepUpdating::ANIMATIONS_RUNNING )
241 oss << "<Animations running> ";
244 if ( keepUpdatingStatus & Integration::KeepUpdating::DYNAMICS_CHANGED )
246 oss << "<Dynamics running> ";
249 if ( keepUpdatingStatus & Integration::KeepUpdating::LOADING_RESOURCES )
251 oss << "<Resources loading> ";
254 if ( keepUpdatingStatus & Integration::KeepUpdating::NOTIFICATIONS_PENDING )
256 oss << "<Notifications pending> ";
259 if ( renderNeedsUpdate )
261 oss << "<Render needs Update> ";
264 DALI_LOG_UPDATE_STATUS( "%s\n", oss.str().c_str() );
268 } // namespace Adaptor
270 } // namespace Internal