From adc6ea017b3bd61d53817d40906754824922a06a Mon Sep 17 00:00:00 2001
From: XenHat <commits@xenh.at>
Date: Wed, 14 Jul 2021 21:44:01 -0400
Subject: [PATCH] Add FPS counter to top bar

Doesn't resize/move elements on hide though.
---
 .../newview/app_settings/settings_alchemy.xml | 11 +++++++
 indra/newview/llstatusbar.cpp                 | 32 +++++++++++++++++++
 indra/newview/llstatusbar.h                   |  2 ++
 .../skins/default/xui/en/menu_hide_navbar.xml | 11 +++++++
 .../skins/default/xui/en/panel_status_bar.xml | 23 ++++++++++++-
 5 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/indra/newview/app_settings/settings_alchemy.xml b/indra/newview/app_settings/settings_alchemy.xml
index ab08a6d4650..9913d87c8a5 100644
--- a/indra/newview/app_settings/settings_alchemy.xml
+++ b/indra/newview/app_settings/settings_alchemy.xml
@@ -695,6 +695,17 @@
       <key>Value</key>
       <integer>0</integer>
     </map>
+    <key>ShowStatusBarFPS</key>
+    <map>
+      <key>Comment</key>
+      <string>Show FPS in Status Bar</string>
+      <key>Persist</key>
+      <integer>1</integer>
+      <key>Type</key>
+      <string>Boolean</string>
+      <key>Value</key>
+      <integer>1</integer>
+    </map>
     <key>UIImgTransparentUUID</key>
     <map>
       <key>Comment</key>
diff --git a/indra/newview/llstatusbar.cpp b/indra/newview/llstatusbar.cpp
index f074755c7a2..477bf5d8693 100644
--- a/indra/newview/llstatusbar.cpp
+++ b/indra/newview/llstatusbar.cpp
@@ -108,6 +108,7 @@ const F32 ICON_TIMER_EXPIRY		= 3.f; // How long the balance and health icons sho
 LLStatusBar::LLStatusBar(const LLRect& rect)
 :	LLPanel(),
 	mTextTime(NULL),
+	mTextFPS(nullptr),
 	mSGBandwidth(NULL),
 	mSGPacketLoss(NULL),
 	mPanelPopupHolder(nullptr),
@@ -129,6 +130,7 @@ LLStatusBar::LLStatusBar(const LLRect& rect)
 
 	mBalanceTimer = new LLFrameTimer();
 	mHealthTimer = new LLFrameTimer();
+	mFPSUpdateTimer = new LLFrameTimer();
 
 	buildFromFile("panel_status_bar.xml");
 }
@@ -148,6 +150,25 @@ LLStatusBar::~LLStatusBar()
 // Overrides
 //-----------------------------------------------------------------------
 
+static int32_t fastFloor(const float* in, const ptrdiff_t length = 1)
+{
+  int32_t* out;
+  #define ALIGNMENT alignof(max_align_t)
+  static_assert(sizeof(float) == sizeof(int32_t), "");
+  assert((uintptr_t)(void*)in % ALIGNMENT == 0);
+  assert((uintptr_t)(void*)out % ALIGNMENT == 0);
+  assert((size_t)length % (ALIGNMENT/sizeof(int32_t)) == 0);
+
+  alignas(ALIGNMENT) const float* const input = in;
+  alignas(ALIGNMENT) int32_t* const output = out;
+
+  // Do the conversion
+  for (int i = 0; i < length; ++i) {
+    output[i] = static_cast<int32_t>(std::floor(input[i]));
+  }
+  return *out;
+}
+
 // virtual
 void LLStatusBar::draw()
 {
@@ -204,6 +225,8 @@ BOOL LLStatusBar::postBuild()
 	gSavedSettings.getControl("MuteAudio")->getSignal()->connect(boost::bind(&LLStatusBar::onVolumeChanged, this, _2));
 	gSavedPerAccountSettings.getControl("AlchemyAOEnable")->getCommitSignal()->connect(boost::bind(&LLStatusBar::onAOStateChanged, this));
 
+	mTextFPS = getChild<LLTextBox>("FPSText");
+
 	// Adding Net Stat Graph
 	S32 x = getRect().getWidth() - 2;
 	S32 y = 0;
@@ -300,6 +323,7 @@ BOOL LLStatusBar::postBuild()
 void LLStatusBar::refresh()
 {
 	static LLCachedControl<bool> show_net_stats(gSavedSettings, "ShowNetStats", false);
+	static LLCachedControl<bool> show_fps(gSavedSettings, "ShowStatusBarFPS", false);
 	bool net_stats_visible = show_net_stats;
 
 	if (net_stats_visible)
@@ -365,6 +389,13 @@ void LLStatusBar::refresh()
 							  media_inst->isParcelMediaPlaying() ||
 							  media_inst->isParcelAudioPlaying());
 	mMediaToggle->setValue(!any_media_playing);
+
+	if (show_fps && mFPSUpdateTimer->getElapsedTimeF32() > 0.125f)
+	{
+		mFPSUpdateTimer->reset();
+		auto fps = (float)LLTrace::get_frame_recording().getPeriodMean(LLStatViewer::FPS);
+		mTextFPS->setText(fmt::format(FMT_STRING("{:d}"), fastFloor(&fps)));
+	}
 }
 
 void LLStatusBar::setVisibleForMouselook(bool visible)
@@ -382,6 +413,7 @@ void LLStatusBar::setVisibleForMouselook(bool visible)
 	setBackgroundVisible(visible);
 	mIconPresetsCamera->setVisible(visible);
 	mIconPresetsGraphic->setVisible(visible);
+	mTextFPS->setVisible(visible);
 }
 
 void LLStatusBar::debitBalance(S32 debit)
diff --git a/indra/newview/llstatusbar.h b/indra/newview/llstatusbar.h
index ac807ec82f6..ab6e9007d54 100644
--- a/indra/newview/llstatusbar.h
+++ b/indra/newview/llstatusbar.h
@@ -128,6 +128,7 @@ class LLStatusBar final
 	void onAOStateChanged();
 
 	LLTextBox	*mTextTime;
+	LLTextBox	*mTextFPS;
 
 	LLStatGraph *mSGBandwidth;
 	LLStatGraph *mSGPacketLoss;
@@ -141,6 +142,7 @@ class LLStatusBar final
 	LLTextBox	*mBoxBalance;
 	LLButton	*mMediaToggle;
 	LLFrameTimer	mClockUpdateTimer;
+	LLFrameTimer*	mFPSUpdateTimer;
 
 	S32				mBalance;
 	S32				mHealth;
diff --git a/indra/newview/skins/default/xui/en/menu_hide_navbar.xml b/indra/newview/skins/default/xui/en/menu_hide_navbar.xml
index b517fd7957d..1dd2c295c69 100644
--- a/indra/newview/skins/default/xui/en/menu_hide_navbar.xml
+++ b/indra/newview/skins/default/xui/en/menu_hide_navbar.xml
@@ -30,4 +30,15 @@
              function="CheckControl"
              parameter="ShowMiniLocationPanel" />
     </menu_item_check>
+    <menu_item_check
+         label="Show FPS"
+         layout="topleft"
+         name="ShowStatusBarFPS">
+           <on_click
+             function="ToggleControl"
+             parameter="ShowStatusBarFPS" /> 
+             <on_check
+             function="CheckControl"
+             parameter="ShowStatusBarFPS" />
+    </menu_item_check>
 </menu>
diff --git a/indra/newview/skins/default/xui/en/panel_status_bar.xml b/indra/newview/skins/default/xui/en/panel_status_bar.xml
index 7995d43f08e..59004539171 100644
--- a/indra/newview/skins/default/xui/en/panel_status_bar.xml
+++ b/indra/newview/skins/default/xui/en/panel_status_bar.xml
@@ -75,7 +75,7 @@
   </panel>
   <panel
     height="18"
-    left="-474"
+    left="-510"
     width="185"
     top="1"
     follows="right|top" 
@@ -210,4 +210,25 @@
       function="Floater.ToggleOrBringToFront"
       parameter="quick_settings" />
     </button>
+    <panel
+    height="18"
+    left_pad="0"
+    width="40"
+    top="2"
+    follows="right|top"
+    visibility_control="ShowStatusBarFPS"
+    name="fps_bg">
+     <text
+      type="string"
+      follows="right|top"
+      halign="center"
+      height="16"
+      top="3"
+      left="0"
+      name="FPSText"
+      tool_tip="Current FPS"
+      width="35">
+         000.0
+     </text>
+    </panel>
 </panel>
-- 
GitLab