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 <dali/internal/system/common/frame-time-stats.h>
35 const float EPSILON = 0.9f; // rolling average = (average * epsilon) + (current * epsilon)
36 const float ONE_OVER_MICROSECONDS_TO_SECONDS = 1.f / 1000000.f; ///< microseconds per second
39 FrameTimeStats::FrameTimeStats()
42 mSamples.Reserve( 16 ); // Fill out a little to avoid early reallocations
47 FrameTimeStats::~FrameTimeStats()
51 void FrameTimeStats::StartTime( const FrameTimeStamp& timeStamp )
53 // check to make sure we don't get 2 start times in a row
54 if( mTimeState != WAITING_FOR_START_TIME )
60 mTimeState = WAITING_FOR_END_TIME;
63 void FrameTimeStats::EndTime( const FrameTimeStamp& timeStamp )
65 if( mTimeState != WAITING_FOR_END_TIME )
71 mTimeState = WAITING_FOR_START_TIME;
74 // frame time in seconds
75 unsigned int elapsedTime = FrameTimeStamp::MicrosecondDiff( mStart, timeStamp);
77 mSamples.PushBack( elapsedTime );
79 // if the min and max times haven't been set, do that now.
84 mMinMaxTimeSet = true;
88 if (elapsedTime < mMin)
92 else if (elapsedTime > mMax)
98 mTotal += elapsedTime;
101 void FrameTimeStats::Reset()
103 mTimeState = WAITING_FOR_START_TIME;
104 mMinMaxTimeSet = false;
108 mStart = FrameTimeStamp();
112 float FrameTimeStats::GetMaxTime() const
114 return mMax * ONE_OVER_MICROSECONDS_TO_SECONDS;
117 float FrameTimeStats::GetMinTime() const
119 return mMin * ONE_OVER_MICROSECONDS_TO_SECONDS;
122 float FrameTimeStats::GetTotalTime() const
124 return mTotal * ONE_OVER_MICROSECONDS_TO_SECONDS;
127 unsigned int FrameTimeStats::GetRunCount() const
132 void FrameTimeStats::CalculateMean( float& meanOut, float& standardDeviationOut ) const
134 if( mSamples.Size() > 0 )
137 unsigned int sum = 0;
138 for( Samples::ConstIterator it = mSamples.Begin(), itEnd = mSamples.End(); it != itEnd; ++it )
140 unsigned int value = *it;
145 meanOut = static_cast<float>(sum) / mSamples.Size();
148 float variance = 0.0f;
149 for( Samples::ConstIterator it = mSamples.Begin(), itEnd = mSamples.End(); it != itEnd; ++it )
151 unsigned int value = *it;
153 float difference = static_cast<float>(value) - meanOut;
155 variance += difference * difference;
158 variance /= mSamples.Size();
160 // Standard deviation
161 standardDeviationOut = sqrtf( variance );
163 meanOut *= ONE_OVER_MICROSECONDS_TO_SECONDS;
164 standardDeviationOut *= ONE_OVER_MICROSECONDS_TO_SECONDS;
169 standardDeviationOut = 0.0f;
174 } // namespace Adaptor
176 } // namespace Internal