1 // Copyright (c) 2017 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #ifndef _Graphic3d_FrameStats_HeaderFile
15 #define _Graphic3d_FrameStats_HeaderFile
16 
17 #include <Graphic3d_FrameStatsData.hxx>
18 #include <Graphic3d_RenderingParams.hxx>
19 #include <Standard_Type.hxx>
20 #include <Standard_Transient.hxx>
21 #include <TColStd_IndexedDataMapOfStringString.hxx>
22 
23 class Graphic3d_CView;
24 
25 //! Class storing the frame statistics.
26 class Graphic3d_FrameStats : public Standard_Transient
27 {
28   DEFINE_STANDARD_RTTIEXT(Graphic3d_FrameStats, Standard_Transient)
29 public:
30 
31   //! Default constructor.
32   Standard_EXPORT Graphic3d_FrameStats();
33 
34   //! Destructor.
35   Standard_EXPORT virtual ~Graphic3d_FrameStats();
36 
37   //! Returns interval in seconds for updating meters across several frames; 1 second by default.
UpdateInterval() const38   Standard_Real UpdateInterval() const { return myUpdateInterval; }
39 
40   //! Sets interval in seconds for updating values.
SetUpdateInterval(Standard_Real theInterval)41   void SetUpdateInterval (Standard_Real theInterval) { myUpdateInterval = theInterval; }
42 
43   //! Prefer longer lines over more greater of lines.
IsLongLineFormat() const44   Standard_Boolean IsLongLineFormat() const { return myIsLongLineFormat; }
45 
46   //! Set if format should prefer longer lines over greater number of lines.
SetLongLineFormat(Standard_Boolean theValue)47   void SetLongLineFormat (Standard_Boolean theValue) { myIsLongLineFormat = theValue; }
48 
49   //! Frame redraw started.
50   Standard_EXPORT virtual void FrameStart (const Handle(Graphic3d_CView)& theView,
51                                            bool theIsImmediateOnly);
52 
53   //! Frame redraw finished.
54   Standard_EXPORT virtual void FrameEnd (const Handle(Graphic3d_CView)& theView,
55                                          bool theIsImmediateOnly);
56 
57 public:
58 
59   //! Returns formatted string.
60   Standard_EXPORT virtual TCollection_AsciiString FormatStats (Graphic3d_RenderingParams::PerfCounters theFlags) const;
61 
62   //! Fill in the dictionary with formatted statistic info.
63   Standard_EXPORT virtual void FormatStats (TColStd_IndexedDataMapOfStringString&   theDict,
64                                             Graphic3d_RenderingParams::PerfCounters theFlags) const;
65 
66   //! Returns duration of the last frame in seconds.
FrameDuration() const67   Standard_Real FrameDuration() const { return myFrameDuration; }
68 
69   //! Returns FPS (frames per seconds, elapsed time).
70   //! This number indicates an actual frame rate averaged for several frames within UpdateInterval() duration,
71   //! basing on a real elapsed time between updates.
FrameRate() const72   Standard_Real FrameRate() const { return LastDataFrame().FrameRate(); }
73 
74   //! Returns CPU FPS (frames per seconds, CPU time).
75   //! This number indicates a PREDICTED frame rate,
76   //! basing on CPU elapsed time between updates and NOT real elapsed time (which might include periods of CPU inactivity).
77   //! Number is expected to be greater then actual frame rate returned by FrameRate().
78   //! Values significantly greater actual frame rate indicate that rendering is limited by GPU performance (CPU is stalled in-between),
79   //! while values around actual frame rate indicate rendering being limited by CPU performance (GPU is stalled in-between).
FrameRateCpu() const80   Standard_Real FrameRateCpu() const { return LastDataFrame().FrameRateCpu(); }
81 
82   //! Returns value of specified counter, cached between stats updates.
83   //! Should NOT be called between ::FrameStart() and ::FrameEnd() calls.
CounterValue(Graphic3d_FrameStatsCounter theCounter) const84   Standard_Size CounterValue (Graphic3d_FrameStatsCounter theCounter) const { return LastDataFrame()[theCounter]; }
85 
86   //! Returns value of specified timer for modification, should be called between ::FrameStart() and ::FrameEnd() calls.
87   //! Should NOT be called between ::FrameStart() and ::FrameEnd() calls.
TimerValue(Graphic3d_FrameStatsTimer theTimer) const88   Standard_Real TimerValue (Graphic3d_FrameStatsTimer theTimer) const { return LastDataFrame()[theTimer]; }
89 
90   //! Returns TRUE if some Layers have been culled.
HasCulledLayers() const91   Standard_Boolean HasCulledLayers() const { return LastDataFrame()[Graphic3d_FrameStatsCounter_NbLayersNotCulled] != LastDataFrame()[Graphic3d_FrameStatsCounter_NbLayers]; }
92 
93   //! Returns TRUE if some structures have been culled.
HasCulledStructs() const94   Standard_Boolean HasCulledStructs() const { return LastDataFrame()[Graphic3d_FrameStatsCounter_NbStructsNotCulled] != LastDataFrame()[Graphic3d_FrameStatsCounter_NbStructs]; }
95 
96   //! Returns last data frame, cached between stats updates.
97   //! Should NOT be called between ::FrameStart() and ::FrameEnd() calls.
LastDataFrame() const98   const Graphic3d_FrameStatsData& LastDataFrame() const { return myCounters.Value (myLastFrameIndex); }
99 
100   //! Returns last data frame index.
LastDataFrameIndex() const101   Standard_Integer LastDataFrameIndex() const { return myLastFrameIndex; }
102 
103   //! Returns data frames.
DataFrames() const104   const NCollection_Array1<Graphic3d_FrameStatsData>& DataFrames() const { return myCounters; }
105 
106   //! Returns data frames.
ChangeDataFrames()107   NCollection_Array1<Graphic3d_FrameStatsData>& ChangeDataFrames() { return myCounters; }
108 
109 public:
110 
111   //! Returns value of specified counter for modification, should be called between ::FrameStart() and ::FrameEnd() calls.
ChangeCounter(Graphic3d_FrameStatsCounter theCounter)112   Standard_Size& ChangeCounter (Graphic3d_FrameStatsCounter theCounter) { return ActiveDataFrame()[theCounter]; }
113 
114   //! Returns value of specified timer for modification, should be called between ::FrameStart() and ::FrameEnd() calls.
ChangeTimer(Graphic3d_FrameStatsTimer theTimer)115   Standard_Real& ChangeTimer (Graphic3d_FrameStatsTimer theTimer) { return ActiveDataFrame()[theTimer]; }
116 
117   //! Returns currently filling data frame for modification, should be called between ::FrameStart() and ::FrameEnd() calls.
ActiveDataFrame()118   Graphic3d_FrameStatsDataTmp& ActiveDataFrame() { return myCountersTmp; }
119 
120 protected:
121 
122   //! Method to collect statistics from the View; called by FrameEnd().
123   virtual void updateStatistics (const Handle(Graphic3d_CView)& theView,
124                                  bool theIsImmediateOnly) = 0;
125 
126 protected:
127 
128   OSD_Timer        myFpsTimer;                //!< timer for FPS measurements
129   Standard_Real    myFrameStartTime;          //!< time at the beginning of frame redraw
130   Standard_Real    myFrameDuration;           //!< frame duration
131   Standard_Real    myUpdateInterval;          //!< interval to update meters
132   Standard_Size    myFpsFrameCount;           //!< FPS counter (within short measurement time slice)
133   NCollection_Array1<Graphic3d_FrameStatsData> myCounters; //!< data frames history
134   Graphic3d_FrameStatsDataTmp myCountersTmp;  //!< data frame values filled to be filled between FrameStart() and FrameEnd() calls
135   Graphic3d_FrameStatsData    myCountersMax;  //!< data frame values with absolute maximum values in the history
136   Standard_Integer myLastFrameIndex;          //!< last data frame index
137   Standard_Boolean myIsLongLineFormat;        //!< prefer longer lines over greater number of lines
138 
139 };
140 
141 #endif // _Graphic3d_FrameStats_HeaderFile
142