|
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/. */ |
|
5 |
|
6 #ifndef mozilla_layers_opengl_FPSCounter_h_ |
|
7 #define mozilla_layers_opengl_FPSCounter_h_ |
|
8 |
|
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 |
|
15 |
|
16 namespace mozilla { |
|
17 namespace gl { |
|
18 class GLContext; |
|
19 } |
|
20 namespace layers { |
|
21 |
|
22 class DataTextureSource; |
|
23 class ShaderProgramOGL; |
|
24 |
|
25 const double kFpsWindowMs = 250.0; |
|
26 const size_t kNumFrameTimeStamps = 16; |
|
27 struct FPSCounter { |
|
28 FPSCounter() : mCurrentFrameIndex(0) { |
|
29 mFrames.SetLength(kNumFrameTimeStamps); |
|
30 } |
|
31 |
|
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; |
|
38 |
|
39 void AddFrame(TimeStamp aNewFrame) { |
|
40 mFrames[mCurrentFrameIndex] = aNewFrame; |
|
41 mCurrentFrameIndex = (mCurrentFrameIndex + 1) % kNumFrameTimeStamps; |
|
42 } |
|
43 |
|
44 double AddFrameAndGetFps(TimeStamp aCurrentFrame) { |
|
45 AddFrame(aCurrentFrame); |
|
46 return EstimateFps(aCurrentFrame); |
|
47 } |
|
48 |
|
49 double GetFpsAt(TimeStamp aNow) { |
|
50 return EstimateFps(aNow); |
|
51 } |
|
52 |
|
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 }; |
|
73 |
|
74 struct FPSState { |
|
75 FPSCounter mCompositionFps; |
|
76 FPSCounter mTransactionFps; |
|
77 |
|
78 FPSState() {} |
|
79 |
|
80 void DrawFPS(TimeStamp, unsigned, Compositor* aCompositor); |
|
81 |
|
82 void NotifyShadowTreeTransaction() { |
|
83 mTransactionFps.AddFrame(TimeStamp::Now()); |
|
84 } |
|
85 |
|
86 private: |
|
87 RefPtr<DataTextureSource> mFPSTextureSource; |
|
88 }; |
|
89 |
|
90 } |
|
91 } |
|
92 |
|
93 #endif // mozilla_layers_opengl_FPSCounter_h_ |