1e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot/*
2e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * Copyright 2013 The Android Open Source Project
3e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot *
4e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * Licensed under the Apache License, Version 2.0 (the "License");
5e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * you may not use this file except in compliance with the License.
6e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * You may obtain a copy of the License at
7e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot *
8e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot *      http://www.apache.org/licenses/LICENSE-2.0
9e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot *
10e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * Unless required by applicable law or agreed to in writing, software
11e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * distributed under the License is distributed on an "AS IS" BASIS,
12e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * See the License for the specific language governing permissions and
14e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot * limitations under the License.
15e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot */
16e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
17e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot#include "perfMonitor.h"
18e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
19e779e31468999c7ef69f424a2b8271969833df2android-build-team Robotnamespace ndk_helper
20e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot{
21e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
22e779e31468999c7ef69f424a2b8271969833df2android-build-team RobotPerfMonitor::PerfMonitor() :
23e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot                last_tick_( 0.f ),
24e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot                tv_last_sec_( 0 ),
25e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot                tickindex_( 0 ),
26e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot                ticksum_( 0 )
27e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot{
28e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    for( int32_t i = 0; i < NUM_SAMPLES; ++i )
29e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        ticklist_[i] = 0;
30e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot}
31e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
32e779e31468999c7ef69f424a2b8271969833df2android-build-team RobotPerfMonitor::~PerfMonitor()
33e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot{
34e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot}
35e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
36e779e31468999c7ef69f424a2b8271969833df2android-build-team Robotdouble PerfMonitor::UpdateTick( double currentTick )
37e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot{
38e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    ticksum_ -= ticklist_[tickindex_];
39e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    ticksum_ += currentTick;
40e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    ticklist_[tickindex_] = currentTick;
41e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    tickindex_ = (tickindex_ + 1) % NUM_SAMPLES;
42e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
43e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    return ((double) ticksum_ / NUM_SAMPLES);
44e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot}
45e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
46e779e31468999c7ef69f424a2b8271969833df2android-build-team Robotbool PerfMonitor::Update( float &fFPS )
47e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot{
48e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    struct timeval Time;
49e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    gettimeofday( &Time, NULL );
50e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
51e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    double time = Time.tv_sec + Time.tv_usec * 1.0 / 1000000.0;
52e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    double tick = time - last_tick_;
53e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    double d = UpdateTick( tick );
54e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    last_tick_ = time;
55e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
56e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    if( Time.tv_sec - tv_last_sec_ >= 1 )
57e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    {
58e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        double time = Time.tv_sec + Time.tv_usec * 1.0 / 1000000.0;
59e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        current_FPS_ = 1.f / d;
60e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        tv_last_sec_ = Time.tv_sec;
61e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        fFPS = current_FPS_;
62e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        return true;
63e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    }
64e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    else
65e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    {
66e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        fFPS = current_FPS_;
67e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot        return false;
68e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot    }
69e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot}
70e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
71e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot}   //namespace ndkHelper
72e779e31468999c7ef69f424a2b8271969833df2android-build-team Robot
73