gfx/layers/composite/FPSCounter.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/composite/FPSCounter.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_layers_opengl_FPSCounter_h_
    1.10 +#define mozilla_layers_opengl_FPSCounter_h_
    1.11 +
    1.12 +#include <stddef.h>                     // for size_t
    1.13 +#include <algorithm>                    // for min
    1.14 +#include "GLDefs.h"                     // for GLuint
    1.15 +#include "mozilla/TimeStamp.h"          // for TimeStamp, TimeDuration
    1.16 +#include "nsTArray.h"                   // for nsAutoTArray, nsTArray_Impl, etc
    1.17 +#include "VBOArena.h"                   // for gl::VBOArena
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace gl {
    1.21 +class GLContext;
    1.22 +}
    1.23 +namespace layers {
    1.24 +
    1.25 +class DataTextureSource;
    1.26 +class ShaderProgramOGL;
    1.27 +
    1.28 +const double kFpsWindowMs = 250.0;
    1.29 +const size_t kNumFrameTimeStamps = 16;
    1.30 +struct FPSCounter {
    1.31 +  FPSCounter() : mCurrentFrameIndex(0) {
    1.32 +      mFrames.SetLength(kNumFrameTimeStamps);
    1.33 +  }
    1.34 +
    1.35 +  // We keep a circular buffer of the time points at which the last K
    1.36 +  // frames were drawn.  To estimate FPS, we count the number of
    1.37 +  // frames we've drawn within the last kFPSWindowMs milliseconds and
    1.38 +  // divide by the amount time since the first of those frames.
    1.39 +  nsAutoTArray<TimeStamp, kNumFrameTimeStamps> mFrames;
    1.40 +  size_t mCurrentFrameIndex;
    1.41 +
    1.42 +  void AddFrame(TimeStamp aNewFrame) {
    1.43 +    mFrames[mCurrentFrameIndex] = aNewFrame;
    1.44 +    mCurrentFrameIndex = (mCurrentFrameIndex + 1) % kNumFrameTimeStamps;
    1.45 +  }
    1.46 +
    1.47 +  double AddFrameAndGetFps(TimeStamp aCurrentFrame) {
    1.48 +    AddFrame(aCurrentFrame);
    1.49 +    return EstimateFps(aCurrentFrame);
    1.50 +  }
    1.51 +
    1.52 +  double GetFpsAt(TimeStamp aNow) {
    1.53 +    return EstimateFps(aNow);
    1.54 +  }
    1.55 +
    1.56 +private:
    1.57 +  double EstimateFps(TimeStamp aNow) {
    1.58 +    TimeStamp beginningOfWindow =
    1.59 +      (aNow - TimeDuration::FromMilliseconds(kFpsWindowMs));
    1.60 +    TimeStamp earliestFrameInWindow = aNow;
    1.61 +    size_t numFramesDrawnInWindow = 0;
    1.62 +    for (size_t i = 0; i < kNumFrameTimeStamps; ++i) {
    1.63 +      const TimeStamp& frame = mFrames[i];
    1.64 +      if (!frame.IsNull() && frame > beginningOfWindow) {
    1.65 +        ++numFramesDrawnInWindow;
    1.66 +        earliestFrameInWindow = std::min(earliestFrameInWindow, frame);
    1.67 +      }
    1.68 +    }
    1.69 +    double realWindowSecs = (aNow - earliestFrameInWindow).ToSeconds();
    1.70 +    if (realWindowSecs == 0.0 || numFramesDrawnInWindow == 1) {
    1.71 +      return 0.0;
    1.72 +    }
    1.73 +    return double(numFramesDrawnInWindow - 1) / realWindowSecs;
    1.74 +  }
    1.75 +};
    1.76 +
    1.77 +struct FPSState {
    1.78 +  FPSCounter mCompositionFps;
    1.79 +  FPSCounter mTransactionFps;
    1.80 +
    1.81 +  FPSState() {}
    1.82 +
    1.83 +  void DrawFPS(TimeStamp, unsigned, Compositor* aCompositor);
    1.84 +
    1.85 +  void NotifyShadowTreeTransaction() {
    1.86 +    mTransactionFps.AddFrame(TimeStamp::Now());
    1.87 +  }
    1.88 +
    1.89 +private:
    1.90 +  RefPtr<DataTextureSource> mFPSTextureSource;
    1.91 +};
    1.92 +
    1.93 +}
    1.94 +}
    1.95 +
    1.96 +#endif // mozilla_layers_opengl_FPSCounter_h_

mercurial