1/*
2 * Copyright 2013 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "perfMonitor.h"
18
19namespace ndk_helper
20{
21
22PerfMonitor::PerfMonitor() :
23                last_tick_( 0.f ),
24                tv_last_sec_( 0 ),
25                tickindex_( 0 ),
26                ticksum_( 0 )
27{
28    for( int32_t i = 0; i < NUM_SAMPLES; ++i )
29        ticklist_[i] = 0;
30}
31
32PerfMonitor::~PerfMonitor()
33{
34}
35
36double PerfMonitor::UpdateTick( double currentTick )
37{
38    ticksum_ -= ticklist_[tickindex_];
39    ticksum_ += currentTick;
40    ticklist_[tickindex_] = currentTick;
41    tickindex_ = (tickindex_ + 1) % NUM_SAMPLES;
42
43    return ((double) ticksum_ / NUM_SAMPLES);
44}
45
46bool PerfMonitor::Update( float &fFPS )
47{
48    struct timeval Time;
49    gettimeofday( &Time, NULL );
50
51    double time = Time.tv_sec + Time.tv_usec * 1.0 / 1000000.0;
52    double tick = time - last_tick_;
53    double d = UpdateTick( tick );
54    last_tick_ = time;
55
56    if( Time.tv_sec - tv_last_sec_ >= 1 )
57    {
58        double time = Time.tv_sec + Time.tv_usec * 1.0 / 1000000.0;
59        current_FPS_ = 1.f / d;
60        tv_last_sec_ = Time.tv_sec;
61        fFPS = current_FPS_;
62        return true;
63    }
64    else
65    {
66        fFPS = current_FPS_;
67        return false;
68    }
69}
70
71}   //namespace ndkHelper
72
73