1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
6#include <limits>
7
8#include "nacl_app/flock.h"
9
10void FrameCounter::BeginFrame() {
11  struct timeval start_time;
12  gettimeofday(&start_time, NULL);
13  frame_start_ = start_time.tv_sec * kMicroSecondsPerSecond +
14                 start_time.tv_usec;
15}
16
17void FrameCounter::EndFrame() {
18  struct timeval end_time;
19  gettimeofday(&end_time, NULL);
20  double frame_end = end_time.tv_sec * kMicroSecondsPerSecond +
21                     end_time.tv_usec;
22  double dt = frame_end - frame_start_;
23  if (dt < 0)
24    return;
25  frame_duration_accumulator_ += dt;
26  frame_count_++;
27  if (frame_count_ > kFrameRateRefreshCount ||
28      frame_duration_accumulator_ >= kMicroSecondsPerSecond) {
29    double elapsed_time = frame_duration_accumulator_ /
30                          kMicroSecondsPerSecond;
31    if (fabs(elapsed_time) > std::numeric_limits<double>::epsilon()) {
32      frames_per_second_ = frame_count_ / elapsed_time;
33    }
34    frame_duration_accumulator_ = 0;
35    frame_count_ = 0;
36  }
37}
38
39void FrameCounter::Reset() {
40  frames_per_second_ = 0;
41  frame_duration_accumulator_ = 0;
42  frame_count_ = 0;
43}
44