1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_gfx_layers_composite_Diagnostics_h
8 #define mozilla_gfx_layers_composite_Diagnostics_h
9 
10 #include "FPSCounter.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/StaticPrefs_layers.h"
13 #include "mozilla/TimeStamp.h"
14 #include <deque>
15 #include <string>
16 #include <utility>
17 
18 namespace mozilla {
19 namespace layers {
20 
21 class PaintTiming;
22 
23 class TimedMetric {
24   typedef std::pair<float, TimeStamp> Entry;
25 
26  public:
Add(float aValue)27   void Add(float aValue) {
28     if (mHistory.size() > kMaxHistory) {
29       mHistory.pop_front();
30     }
31     mHistory.push_back(Entry(aValue, TimeStamp::Now()));
32   }
33 
34   float Average() const;
Empty()35   bool Empty() const { return mHistory.empty(); }
36 
37  private:
38   static const size_t kMaxHistory = 60;
39 
40   std::deque<Entry> mHistory;
41 };
42 
43 // These statistics are collected by layers backends, preferably by the GPU
44 struct GPUStats {
GPUStatsGPUStats45   GPUStats() : mInvalidPixels(0), mScreenPixels(0), mPixelsFilled(0) {}
46 
47   uint32_t mInvalidPixels;
48   uint32_t mScreenPixels;
49   uint32_t mPixelsFilled;
50   Maybe<float> mDrawTime;
51 };
52 
53 // Collects various diagnostics about layers performance.
54 class Diagnostics {
55  public:
56   Diagnostics();
57 
58   void RecordPaintTimes(const PaintTiming& aPaintTimes);
RecordUpdateTime(float aValue)59   void RecordUpdateTime(float aValue) { mUpdateMs.Add(aValue); }
RecordPrepareTime(float aValue)60   void RecordPrepareTime(float aValue) { mPrepareMs.Add(aValue); }
RecordCompositeTime(float aValue)61   void RecordCompositeTime(float aValue) { mCompositeMs.Add(aValue); }
AddTxnFrame()62   void AddTxnFrame() { mTransactionFps.AddFrame(TimeStamp::Now()); }
63 
64   std::string GetFrameOverlayString(const GPUStats& aStats);
65 
66   class Record {
67    public:
68     explicit Record(TimeStamp aStart = TimeStamp()) {
69       if (StaticPrefs::layers_acceleration_draw_fps()) {
70         mStart = aStart.IsNull() ? TimeStamp::Now() : aStart;
71       }
72     }
Recording()73     bool Recording() const { return !mStart.IsNull(); }
Duration()74     float Duration() const {
75       return (TimeStamp::Now() - mStart).ToMilliseconds();
76     }
77 
78    private:
79     TimeStamp mStart;
80   };
81 
82  private:
83   FPSCounter mCompositeFps;
84   FPSCounter mTransactionFps;
85   TimedMetric mDlbMs;
86   TimedMetric mDlb2Ms;
87   TimedMetric mFlbMs;
88   TimedMetric mRasterMs;
89   TimedMetric mSerializeMs;
90   TimedMetric mSendMs;
91   TimedMetric mUpdateMs;
92   TimedMetric mPrepareMs;
93   TimedMetric mCompositeMs;
94   TimedMetric mGPUDrawMs;
95 };
96 
97 }  // namespace layers
98 }  // namespace mozilla
99 
100 #endif  // mozilla_gfx_layers_composite_Diagnostics_h
101