gfx/layers/composite/FPSCounter.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef mozilla_layers_opengl_FPSCounter_h_
     7 #define mozilla_layers_opengl_FPSCounter_h_
     9 #include <stddef.h>                     // for size_t
    10 #include <algorithm>                    // for min
    11 #include "GLDefs.h"                     // for GLuint
    12 #include "mozilla/TimeStamp.h"          // for TimeStamp, TimeDuration
    13 #include "nsTArray.h"                   // for nsAutoTArray, nsTArray_Impl, etc
    14 #include "VBOArena.h"                   // for gl::VBOArena
    16 namespace mozilla {
    17 namespace gl {
    18 class GLContext;
    19 }
    20 namespace layers {
    22 class DataTextureSource;
    23 class ShaderProgramOGL;
    25 const double kFpsWindowMs = 250.0;
    26 const size_t kNumFrameTimeStamps = 16;
    27 struct FPSCounter {
    28   FPSCounter() : mCurrentFrameIndex(0) {
    29       mFrames.SetLength(kNumFrameTimeStamps);
    30   }
    32   // We keep a circular buffer of the time points at which the last K
    33   // frames were drawn.  To estimate FPS, we count the number of
    34   // frames we've drawn within the last kFPSWindowMs milliseconds and
    35   // divide by the amount time since the first of those frames.
    36   nsAutoTArray<TimeStamp, kNumFrameTimeStamps> mFrames;
    37   size_t mCurrentFrameIndex;
    39   void AddFrame(TimeStamp aNewFrame) {
    40     mFrames[mCurrentFrameIndex] = aNewFrame;
    41     mCurrentFrameIndex = (mCurrentFrameIndex + 1) % kNumFrameTimeStamps;
    42   }
    44   double AddFrameAndGetFps(TimeStamp aCurrentFrame) {
    45     AddFrame(aCurrentFrame);
    46     return EstimateFps(aCurrentFrame);
    47   }
    49   double GetFpsAt(TimeStamp aNow) {
    50     return EstimateFps(aNow);
    51   }
    53 private:
    54   double EstimateFps(TimeStamp aNow) {
    55     TimeStamp beginningOfWindow =
    56       (aNow - TimeDuration::FromMilliseconds(kFpsWindowMs));
    57     TimeStamp earliestFrameInWindow = aNow;
    58     size_t numFramesDrawnInWindow = 0;
    59     for (size_t i = 0; i < kNumFrameTimeStamps; ++i) {
    60       const TimeStamp& frame = mFrames[i];
    61       if (!frame.IsNull() && frame > beginningOfWindow) {
    62         ++numFramesDrawnInWindow;
    63         earliestFrameInWindow = std::min(earliestFrameInWindow, frame);
    64       }
    65     }
    66     double realWindowSecs = (aNow - earliestFrameInWindow).ToSeconds();
    67     if (realWindowSecs == 0.0 || numFramesDrawnInWindow == 1) {
    68       return 0.0;
    69     }
    70     return double(numFramesDrawnInWindow - 1) / realWindowSecs;
    71   }
    72 };
    74 struct FPSState {
    75   FPSCounter mCompositionFps;
    76   FPSCounter mTransactionFps;
    78   FPSState() {}
    80   void DrawFPS(TimeStamp, unsigned, Compositor* aCompositor);
    82   void NotifyShadowTreeTransaction() {
    83     mTransactionFps.AddFrame(TimeStamp::Now());
    84   }
    86 private:
    87   RefPtr<DataTextureSource> mFPSTextureSource;
    88 };
    90 }
    91 }
    93 #endif // mozilla_layers_opengl_FPSCounter_h_

mercurial