Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "vgui_budgetpanelshared.h"
  7. #include "vgui/IVGui.h"
  8. #include "vgui/ILocalize.h"
  9. #include "vgui/ISurface.h"
  10. #include "vgui_controls/Label.h"
  11. #include "ivprofexport.h"
  12. #include "convar.h"
  13. #include "mathlib/mathlib.h"
  14. // NOTE: This has to be the last file included!
  15. #include "tier0/memdbgon.h"
  16. #ifdef VPROF_ENABLED
  17. #ifdef BUDGET_ADMIN_SERVER
  18. #define BUDGET_CVAR_CALLBACK NULL
  19. #else
  20. #define BUDGET_CVAR_CALLBACK PanelGeometryChangedCallBack
  21. #endif
  22. // Global ConVars.
  23. ConVar budget_history_range_ms( "budget_history_range_ms", "66.666666667", FCVAR_ARCHIVE, "budget history range in milliseconds", BUDGET_CVAR_CALLBACK );
  24. ConVar budget_panel_bottom_of_history_fraction( "budget_panel_bottom_of_history_fraction", ".25", FCVAR_ARCHIVE, "number between 0 and 1", BUDGET_CVAR_CALLBACK );
  25. ConVar budget_bargraph_range_ms( "budget_bargraph_range_ms", "16.6666666667", FCVAR_ARCHIVE, "budget bargraph range in milliseconds", BUDGET_CVAR_CALLBACK );
  26. ConVar budget_background_alpha( "budget_background_alpha", "128", FCVAR_ARCHIVE, "how translucent the budget panel is" );
  27. ConVar budget_panel_x( "budget_panel_x", "0", FCVAR_ARCHIVE, "number of pixels from the left side of the game screen to draw the budget panel", BUDGET_CVAR_CALLBACK );
  28. ConVar budget_panel_y( "budget_panel_y", "50", FCVAR_ARCHIVE, "number of pixels from the top side of the game screen to draw the budget panel", BUDGET_CVAR_CALLBACK );
  29. ConVar budget_panel_width( "budget_panel_width", "512", FCVAR_ARCHIVE, "width in pixels of the budget panel", BUDGET_CVAR_CALLBACK );
  30. ConVar budget_panel_height( "budget_panel_height", "384", FCVAR_ARCHIVE, "height in pixels of the budget panel", BUDGET_CVAR_CALLBACK );
  31. static CUtlVector<IVProfExport::CExportedBudgetGroupInfo> g_TempBudgetGroupSpace;
  32. double CBudgetPanelShared::g_fFrameTimeLessBudget = 0;
  33. double CBudgetPanelShared::g_fFrameRate = 0;
  34. static CFastTimer g_TimerLessBudget;
  35. static CBudgetPanelShared *g_pBudgetPanelShared = NULL;
  36. extern IVProfExport *g_pVProfExport;
  37. void PanelGeometryChangedCallBack( IConVar *var, const char *pOldString, float flOldValue )
  38. {
  39. // screw it . . rebuild the whole damn thing.
  40. // GetBudgetPanel()->InvalidateLayout();
  41. if ( g_pBudgetPanelShared )
  42. {
  43. g_pBudgetPanelShared->SendConfigDataToBase();
  44. }
  45. }
  46. // -------------------------------------------------------------------------------------------------------------------- //
  47. // CBudgetPanelShared implementation.
  48. // -------------------------------------------------------------------------------------------------------------------- //
  49. CBudgetPanelShared::CBudgetPanelShared( vgui::Panel *pParent, const char *pElementName, int budgetFlagsFilter )
  50. : BaseClass( pParent, pElementName )
  51. {
  52. Assert( !g_pBudgetPanelShared );
  53. g_pBudgetPanelShared = this;
  54. if ( g_pVProfExport )
  55. g_pVProfExport->SetBudgetFlagsFilter( budgetFlagsFilter );
  56. SendConfigDataToBase();
  57. SetZPos( 1001 );
  58. SetVisible( false );
  59. vgui::ivgui()->AddTickSignal( GetVPanel() );
  60. SetPostChildPaintEnabled( true ); // so we can turn vprof back on
  61. }
  62. CBudgetPanelShared::~CBudgetPanelShared()
  63. {
  64. Assert( g_pBudgetPanelShared == this );
  65. g_pBudgetPanelShared = NULL;
  66. }
  67. void CBudgetPanelShared::OnNumBudgetGroupsChanged()
  68. {
  69. SendConfigDataToBase();
  70. }
  71. void CBudgetPanelShared::SetupCustomConfigData( CBudgetPanelConfigData &data )
  72. {
  73. data.m_xCoord = budget_panel_x.GetInt();
  74. data.m_yCoord = budget_panel_y.GetInt();
  75. data.m_Width = budget_panel_width.GetInt();
  76. data.m_Height = budget_panel_height.GetInt();
  77. }
  78. void CBudgetPanelShared::SendConfigDataToBase()
  79. {
  80. // Setup all the data.
  81. CBudgetPanelConfigData data;
  82. // Copy the budget group names in.
  83. int nGroups = 0;
  84. if ( g_pVProfExport )
  85. {
  86. nGroups = g_pVProfExport->GetNumBudgetGroups();
  87. // Make sure we have space to store the results.
  88. if ( g_TempBudgetGroupSpace.Count() < nGroups )
  89. g_TempBudgetGroupSpace.SetSize( nGroups );
  90. g_pVProfExport->GetBudgetGroupInfos( g_TempBudgetGroupSpace.Base() );
  91. }
  92. data.m_BudgetGroupInfo.SetSize( nGroups );
  93. for ( int i=0; i < nGroups; i++ )
  94. {
  95. data.m_BudgetGroupInfo[i].m_Name = g_TempBudgetGroupSpace[i].m_pName;
  96. data.m_BudgetGroupInfo[i].m_Color = g_TempBudgetGroupSpace[i].m_Color;
  97. }
  98. data.m_HistoryLabelValues.AddToTail( 1000.0 / 20 );
  99. data.m_HistoryLabelValues.AddToTail( 1000.0 / 30 );
  100. data.m_HistoryLabelValues.AddToTail( 1000.0 / 60 );
  101. // Copy all the cvars in.
  102. data.m_flHistoryRange = budget_history_range_ms.GetFloat();
  103. data.m_flBottomOfHistoryFraction = budget_panel_bottom_of_history_fraction.GetFloat();
  104. data.m_flBarGraphRange = budget_bargraph_range_ms.GetFloat();
  105. data.m_flTimeLabelInterval = 5;
  106. data.m_nLinesPerTimeLabel = 5;
  107. data.m_flBackgroundAlpha = budget_background_alpha.GetFloat();
  108. SetupCustomConfigData( data );
  109. // Send the config data to the base class.
  110. OnConfigDataChanged( data );
  111. }
  112. void CBudgetPanelShared::DrawColoredText(
  113. vgui::HFont font,
  114. int x, int y,
  115. int r, int g, int b, int a,
  116. const char *pText,
  117. ... )
  118. {
  119. char msg[4096];
  120. va_list marker;
  121. va_start( marker, pText );
  122. _vsnprintf( msg, sizeof( msg ), pText, marker );
  123. va_end( marker );
  124. wchar_t unicodeStr[4096];
  125. int nChars = g_pVGuiLocalize->ConvertANSIToUnicode( msg, unicodeStr, sizeof( unicodeStr ) );
  126. vgui::surface()->DrawSetTextFont( font );
  127. vgui::surface()->DrawSetTextColor( r, g, b, a );
  128. vgui::surface()->DrawSetTextPos( x, y );
  129. vgui::surface()->DrawPrintText( unicodeStr, nChars );
  130. }
  131. void CBudgetPanelShared::PaintBackground()
  132. {
  133. if ( g_pVProfExport )
  134. g_pVProfExport->PauseProfile();
  135. BaseClass::PaintBackground();
  136. }
  137. void CBudgetPanelShared::Paint()
  138. {
  139. if( m_BudgetGroupTimes.Count() == 0 )
  140. {
  141. return;
  142. }
  143. static bool TimerInitialized = false;
  144. if( !TimerInitialized )
  145. {
  146. g_TimerLessBudget.Start();
  147. TimerInitialized=true;
  148. }
  149. g_TimerLessBudget.End();
  150. BaseClass::Paint();
  151. g_fFrameTimeLessBudget = ( g_TimerLessBudget.GetDuration().GetSeconds() );
  152. g_fFrameRate = 1.0 / g_fFrameTimeLessBudget;
  153. }
  154. void CBudgetPanelShared::PostChildPaint()
  155. {
  156. g_TimerLessBudget.Start();
  157. if ( g_pVProfExport )
  158. g_pVProfExport->ResumeProfile();
  159. }
  160. void CBudgetPanelShared::SnapshotVProfHistory( float filteredtime )
  161. {
  162. m_BudgetHistoryOffset = ( m_BudgetHistoryOffset + 1 ) % BUDGET_HISTORY_COUNT;
  163. ClearTimesForAllGroupsForThisFrame();
  164. if ( g_pVProfExport )
  165. {
  166. if ( GetNumCachedBudgetGroups() != g_pVProfExport->GetNumBudgetGroups() )
  167. {
  168. SendConfigDataToBase();
  169. }
  170. float times[IVProfExport::MAX_BUDGETGROUP_TIMES];
  171. g_pVProfExport->GetBudgetGroupTimes( times );
  172. for ( int groupID=0; groupID < GetNumCachedBudgetGroups(); groupID++ )
  173. {
  174. float dt = times[groupID];
  175. // Hack: add filtered time into unnaccounted group...
  176. if ( groupID == VPROF_BUDGET_GROUP_ID_UNACCOUNTED )
  177. {
  178. dt += 1000.0f * filteredtime;
  179. }
  180. m_BudgetGroupTimes[groupID].m_Time[m_BudgetHistoryOffset] = dt;
  181. }
  182. }
  183. }
  184. void CBudgetPanelShared::SetTimeLabelText()
  185. {
  186. for ( int i=0; i < m_TimeLabels.Count(); i++ )
  187. {
  188. char text[512];
  189. Q_snprintf( text, sizeof( text ), "%dms", (int)( i * GetConfigData().m_flTimeLabelInterval ) );
  190. m_TimeLabels[i]->SetText( text );
  191. }
  192. }
  193. void CBudgetPanelShared::SetHistoryLabelText()
  194. {
  195. Assert( m_HistoryLabels.Count() == 3 );
  196. m_HistoryLabels[0]->SetText( "20 fps (50 ms)" );
  197. m_HistoryLabels[1]->SetText( "30 fps (33 1/3 ms)" );
  198. m_HistoryLabels[2]->SetText( "60 fps (16 2/3 ms)" );
  199. }
  200. #endif // VPROF_ENABLED