gfx/layers/composite/FPSCounter.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial