OLD | NEW |
| (Empty) |
1 // Copyright 2011 (c) The Native Client 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 /** | |
6 * @file | |
7 * A small helper class that keeps track of tick deltas and a tick | |
8 * count. It accumulates the duration of each frame. Updates the frame rate | |
9 * every 100 frames. | |
10 */ | |
11 | |
12 goog.provide('FrameCounter'); | |
13 | |
14 goog.require('goog.Disposable'); | |
15 | |
16 /** | |
17 * Constructor for the FrameCounter class. | |
18 * @constructor | |
19 * @extends {goog.Disposable} | |
20 */ | |
21 FrameCounter = function() { | |
22 goog.Disposable.call(this); | |
23 | |
24 /** | |
25 * The frame duration accumulator. Holds the accumulated simulation time. | |
26 * Measured in milliseconds. | |
27 * @type {number} | |
28 * @private | |
29 */ | |
30 this.frameDurationAccumulator_ = 0; | |
31 | |
32 /** | |
33 * The number of frames accumulated so far. This is reset to 0 about once | |
34 * a second. | |
35 * @type {number} | |
36 * @private | |
37 */ | |
38 this.frameCount_ = 0; | |
39 | |
40 /** | |
41 * The time stamp at the the start of a simulation tick. | |
42 * @type {number} | |
43 * @private | |
44 */ | |
45 this.frameStart_ = 0.0; | |
46 | |
47 /** | |
48 * The current frame rate. This is updated about once per second, and is | |
49 * not a valid value for the first second of the simulation.. | |
50 * @type {number} | |
51 * @private | |
52 */ | |
53 this.framesPerSecond_ = 0; | |
54 | |
55 this.reset(); | |
56 } | |
57 goog.inherits(FrameCounter, goog.Disposable); | |
58 | |
59 /** | |
60 * Number of clock ticks per second. | |
61 * @type {number} | |
62 * @private | |
63 */ | |
64 FrameCounter.prototype.CLOCK_TICKS_PER_SECOND_ = 1000.0; | |
65 | |
66 /** | |
67 * Frame count that triggers an update the frame rate. | |
68 * @type {number} | |
69 * @private | |
70 */ | |
71 FrameCounter.prototype.FRAME_RATE_REFRESH_COUNT_ = 100; | |
72 | |
73 /** | |
74 * Override of disposeInternal() to dispose of retained objects. | |
75 * @override | |
76 */ | |
77 FrameCounter.prototype.disposeInternal = function() { | |
78 FrameCounter.superClass_.disposeInternal.call(this); | |
79 } | |
80 | |
81 /** | |
82 * Record the current time, which is used to compute the frame duration | |
83 * when endFrame() is called. | |
84 */ | |
85 FrameCounter.prototype.beginFrame = function() { | |
86 this.frameStart_ = new Date().getTime(); | |
87 } | |
88 | |
89 /** | |
90 * Compute the delta since the last call to beginFrame() and increment the | |
91 * frame count. Update the frame rate whenever the prescribed number of | |
92 * frames have been counted, or at least one second of simulator time has | |
93 * passed, whichever is less. | |
94 */ | |
95 FrameCounter.prototype.endFrame = function() { | |
96 var frameEnd = new Date().getTime(); | |
97 var dt = frameEnd - this.frameStart_; | |
98 if (dt < 0) { | |
99 return; // Just in case. | |
100 } | |
101 this.frameDurationAccumulator_ += dt; | |
102 this.frameCount_++; | |
103 if (this.frameCount_ > this.FRAME_RATE_REFRESH_COUNT_ || | |
104 this.frameDurationAccumulator_ >= this.CLOCK_TICKS_PER_SECOND_) { | |
105 var elapsedTime = this.frameDurationAccumulator_ / | |
106 this.CLOCK_TICKS_PER_SECOND_; | |
107 this.framesPerSecond_ = this.frameCount_ / elapsedTime; | |
108 this.frameDurationAccumulator_ = 0; | |
109 this.frameCount_ = 0; | |
110 } | |
111 } | |
112 | |
113 /** | |
114 * The current frame rate. Note that this is 0 for the first second in | |
115 * the accumulator, and is updated about once per second. | |
116 * @return {number} The current frame rate in frames per second. | |
117 */ | |
118 FrameCounter.prototype.framesPerSecond = function() { | |
119 return this.framesPerSecond_; | |
120 } | |
121 | |
122 /** | |
123 * Reset all the counters back to 0. | |
124 */ | |
125 FrameCounter.prototype.reset = function() { | |
126 this.frameDurationAccumulator_ = 0; | |
127 this.framesPerSecond_ = 0; | |
128 this.frameCount_ = 0; | |
129 } | |
130 | |
OLD | NEW |