michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_layers_opengl_FPSCounter_h_ michael@0: #define mozilla_layers_opengl_FPSCounter_h_ michael@0: michael@0: #include // for size_t michael@0: #include // for min michael@0: #include "GLDefs.h" // for GLuint michael@0: #include "mozilla/TimeStamp.h" // for TimeStamp, TimeDuration michael@0: #include "nsTArray.h" // for nsAutoTArray, nsTArray_Impl, etc michael@0: #include "VBOArena.h" // for gl::VBOArena michael@0: michael@0: namespace mozilla { michael@0: namespace gl { michael@0: class GLContext; michael@0: } michael@0: namespace layers { michael@0: michael@0: class DataTextureSource; michael@0: class ShaderProgramOGL; michael@0: michael@0: const double kFpsWindowMs = 250.0; michael@0: const size_t kNumFrameTimeStamps = 16; michael@0: struct FPSCounter { michael@0: FPSCounter() : mCurrentFrameIndex(0) { michael@0: mFrames.SetLength(kNumFrameTimeStamps); michael@0: } michael@0: michael@0: // We keep a circular buffer of the time points at which the last K michael@0: // frames were drawn. To estimate FPS, we count the number of michael@0: // frames we've drawn within the last kFPSWindowMs milliseconds and michael@0: // divide by the amount time since the first of those frames. michael@0: nsAutoTArray mFrames; michael@0: size_t mCurrentFrameIndex; michael@0: michael@0: void AddFrame(TimeStamp aNewFrame) { michael@0: mFrames[mCurrentFrameIndex] = aNewFrame; michael@0: mCurrentFrameIndex = (mCurrentFrameIndex + 1) % kNumFrameTimeStamps; michael@0: } michael@0: michael@0: double AddFrameAndGetFps(TimeStamp aCurrentFrame) { michael@0: AddFrame(aCurrentFrame); michael@0: return EstimateFps(aCurrentFrame); michael@0: } michael@0: michael@0: double GetFpsAt(TimeStamp aNow) { michael@0: return EstimateFps(aNow); michael@0: } michael@0: michael@0: private: michael@0: double EstimateFps(TimeStamp aNow) { michael@0: TimeStamp beginningOfWindow = michael@0: (aNow - TimeDuration::FromMilliseconds(kFpsWindowMs)); michael@0: TimeStamp earliestFrameInWindow = aNow; michael@0: size_t numFramesDrawnInWindow = 0; michael@0: for (size_t i = 0; i < kNumFrameTimeStamps; ++i) { michael@0: const TimeStamp& frame = mFrames[i]; michael@0: if (!frame.IsNull() && frame > beginningOfWindow) { michael@0: ++numFramesDrawnInWindow; michael@0: earliestFrameInWindow = std::min(earliestFrameInWindow, frame); michael@0: } michael@0: } michael@0: double realWindowSecs = (aNow - earliestFrameInWindow).ToSeconds(); michael@0: if (realWindowSecs == 0.0 || numFramesDrawnInWindow == 1) { michael@0: return 0.0; michael@0: } michael@0: return double(numFramesDrawnInWindow - 1) / realWindowSecs; michael@0: } michael@0: }; michael@0: michael@0: struct FPSState { michael@0: FPSCounter mCompositionFps; michael@0: FPSCounter mTransactionFps; michael@0: michael@0: FPSState() {} michael@0: michael@0: void DrawFPS(TimeStamp, unsigned, Compositor* aCompositor); michael@0: michael@0: void NotifyShadowTreeTransaction() { michael@0: mTransactionFps.AddFrame(TimeStamp::Now()); michael@0: } michael@0: michael@0: private: michael@0: RefPtr mFPSTextureSource; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif // mozilla_layers_opengl_FPSCounter_h_