fpsdisplay.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Displays frames per second (FPS) for the current window.
  16. * Only supported in browsers that support requestAnimationFrame.
  17. * See: https://developer.mozilla.org/en/DOM/window.requestAnimationFrame.
  18. *
  19. * @see ../demos/fpsdisplay.html
  20. */
  21. goog.provide('goog.debug.FpsDisplay');
  22. goog.require('goog.asserts');
  23. goog.require('goog.async.AnimationDelay');
  24. goog.require('goog.dom');
  25. goog.require('goog.dom.TagName');
  26. goog.require('goog.ui.Component');
  27. /**
  28. * Displays frames per seconds that the window this component is
  29. * rendered in is animating at.
  30. *
  31. * @param {goog.dom.DomHelper=} opt_domHelper An optional dom helper.
  32. * @constructor
  33. * @extends {goog.ui.Component}
  34. * @final
  35. */
  36. goog.debug.FpsDisplay = function(opt_domHelper) {
  37. goog.debug.FpsDisplay.base(this, 'constructor', opt_domHelper);
  38. };
  39. goog.inherits(goog.debug.FpsDisplay, goog.ui.Component);
  40. /**
  41. * CSS class for the FPS display.
  42. */
  43. goog.debug.FpsDisplay.CSS = goog.getCssName('goog-fps-display');
  44. /**
  45. * The number of samples per FPS report.
  46. */
  47. goog.debug.FpsDisplay.SAMPLES = 10;
  48. /**
  49. * The current animation.
  50. * @type {goog.debug.FpsDisplay.FpsAnimation_}
  51. * @private
  52. */
  53. goog.debug.FpsDisplay.prototype.animation_ = null;
  54. /** @override */
  55. goog.debug.FpsDisplay.prototype.createDom = function() {
  56. this.setElementInternal(
  57. this.getDomHelper().createDom(
  58. goog.dom.TagName.DIV, goog.debug.FpsDisplay.CSS));
  59. };
  60. /** @override */
  61. goog.debug.FpsDisplay.prototype.enterDocument = function() {
  62. goog.debug.FpsDisplay.base(this, 'enterDocument');
  63. this.animation_ = new goog.debug.FpsDisplay.FpsAnimation_(this.getElement());
  64. this.delay_ = new goog.async.AnimationDelay(
  65. this.handleDelay_, this.getDomHelper().getWindow(), this);
  66. this.delay_.start();
  67. };
  68. /**
  69. * @param {number} now The current time.
  70. * @private
  71. */
  72. goog.debug.FpsDisplay.prototype.handleDelay_ = function(now) {
  73. if (this.isInDocument()) {
  74. this.animation_.onAnimationFrame(now);
  75. this.delay_.start();
  76. }
  77. };
  78. /** @override */
  79. goog.debug.FpsDisplay.prototype.exitDocument = function() {
  80. goog.debug.FpsDisplay.base(this, 'exitDocument');
  81. this.animation_ = null;
  82. goog.dispose(this.delay_);
  83. };
  84. /**
  85. * @return {number} The average frames per second.
  86. */
  87. goog.debug.FpsDisplay.prototype.getFps = function() {
  88. goog.asserts.assert(
  89. this.isInDocument(), 'Render the FPS display before querying FPS');
  90. return this.animation_.lastFps_;
  91. };
  92. /**
  93. * @param {Element} elem An element to hold the FPS count.
  94. * @constructor
  95. * @private
  96. */
  97. goog.debug.FpsDisplay.FpsAnimation_ = function(elem) {
  98. /**
  99. * An element to hold the current FPS rate.
  100. * @type {Element}
  101. * @private
  102. */
  103. this.element_ = elem;
  104. /**
  105. * The number of frames observed so far.
  106. * @type {number}
  107. * @private
  108. */
  109. this.frameNumber_ = 0;
  110. };
  111. /**
  112. * The last time which we reported FPS at.
  113. * @type {number}
  114. * @private
  115. */
  116. goog.debug.FpsDisplay.FpsAnimation_.prototype.lastTime_ = 0;
  117. /**
  118. * The last average FPS.
  119. * @type {number}
  120. * @private
  121. */
  122. goog.debug.FpsDisplay.FpsAnimation_.prototype.lastFps_ = -1;
  123. /**
  124. * @param {number} now The current time.
  125. */
  126. goog.debug.FpsDisplay.FpsAnimation_.prototype.onAnimationFrame = function(now) {
  127. var SAMPLES = goog.debug.FpsDisplay.SAMPLES;
  128. if (this.frameNumber_ % SAMPLES == 0) {
  129. this.lastFps_ = Math.round((1000 * SAMPLES) / (now - this.lastTime_));
  130. goog.dom.setTextContent(this.element_, this.lastFps_);
  131. this.lastTime_ = now;
  132. }
  133. this.frameNumber_++;
  134. };