[xswiftbus] Added FPS monitoring

This commit is contained in:
Mat Sutcliffe
2019-12-31 21:22:00 +00:00
parent 836eb107a3
commit 516c8a2fce
5 changed files with 84 additions and 2 deletions

View File

@@ -319,6 +319,15 @@ namespace BlackSimPlugin
m_dbusInterface->callDBusAsync(QLatin1String("isUsingRealTime"), setterCallback(o_isRealTime)); m_dbusInterface->callDBusAsync(QLatin1String("isUsingRealTime"), setterCallback(o_isRealTime));
} }
double CXSwiftBusServiceProxy::getAverageFPS() const
{
return m_dbusInterface->callDBusRet<double>(QLatin1String("getAverageFPS"));
}
void CXSwiftBusServiceProxy::getAverageFPSAsync(double *o_frameRate)
{
m_dbusInterface->callDBusAsync(QLatin1String("getAverageFPS"), setterCallback(o_frameRate));
}
double CXSwiftBusServiceProxy::getLatitudeDeg() const double CXSwiftBusServiceProxy::getLatitudeDeg() const
{ {
return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg")); return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg"));

View File

@@ -208,6 +208,12 @@ namespace BlackSimPlugin
void isUsingRealTimeAsync(bool *o_isRealTime); void isUsingRealTimeAsync(bool *o_isRealTime);
//! @} //! @}
//! \copydoc XSwiftBus::CService::getAverageFPS
//! @{
double getAverageFPS() const;
void getAverageFPSAsync(double *o_frameRate);
//! @}
//! \copydoc XSwiftBus::CService::getLatitudeDeg //! \copydoc XSwiftBus::CService::getLatitudeDeg
//! @{ //! @{
double getLatitudeDeg() const; double getLatitudeDeg() const;

View File

@@ -36,6 +36,8 @@ namespace XSwiftBus
} }
} }
bool isValid() const { return m_ref; }
template <typename T> template <typename T>
void implSet(T); void implSet(T);
@@ -60,6 +62,8 @@ namespace XSwiftBus
} }
} }
bool isValid() const { return m_ref; }
template <typename T> template <typename T>
void implSetAll(std::vector<T> const &); void implSetAll(std::vector<T> const &);
@@ -108,6 +112,8 @@ namespace XSwiftBus
//! Get the value of the dataref //! Get the value of the dataref
DataRefType get() const { return DataRefImpl::implGet<DataRefType>(); } DataRefType get() const { return DataRefImpl::implGet<DataRefType>(); }
using DataRefImpl::isValid;
}; };
/*! /*!
@@ -141,6 +147,8 @@ namespace XSwiftBus
//! Get the value of a single element //! Get the value of a single element
DataRefType getAt(int index) const { return ArrayDataRefImpl::implGetAt<DataRefType>(index); } DataRefType getAt(int index) const { return ArrayDataRefImpl::implGetAt<DataRefType>(index); }
using ArrayDataRefImpl::isValid;
}; };
/*! /*!
@@ -165,6 +173,9 @@ namespace XSwiftBus
} }
} }
//! True if the dataref exists
bool isValid() const { return m_ref; }
//! Set the value of the whole string (if it is writable) //! Set the value of the whole string (if it is writable)
void set(std::string const &s) { setSubstr(0, s); } void set(std::string const &s) { setSubstr(0, s); }

View File

@@ -23,12 +23,54 @@ using namespace BlackMisc::Simulation::XPlane::QtFreeUtils;
namespace XSwiftBus namespace XSwiftBus
{ {
CService::CService(CSettingsProvider *settingsProvider) : CDBusObject(settingsProvider) //! \private
struct CService::FramePeriodSampler : public CDrawable
{
DataRef<xplane::data::sim::operation::misc::frame_rate_period> m_thisFramePeriod;
DataRef<xplane::data::sim::time::framerate_period> m_thisFramePeriodXP11;
std::vector<float> m_samples;
float m_total = 0;
size_t m_lastSampleIndex = 0;
static constexpr size_t c_maxSampleCount = 500;
FramePeriodSampler() : CDrawable(xplm_Phase_LastCockpit, false) {}
float getAverageFPS()
{
if (m_total == 0) { return 0; }
const float fps = m_samples.size() / m_total;
m_total = 0;
m_samples.clear();
m_lastSampleIndex = 0;
return fps;
}
protected:
virtual void draw() override // called once per frame
{
float current = m_thisFramePeriodXP11.isValid() ? m_thisFramePeriodXP11.get() : m_thisFramePeriod.get();
++m_lastSampleIndex %= c_maxSampleCount;
if (m_samples.size() == c_maxSampleCount)
{
m_total -= m_samples[m_lastSampleIndex];
m_samples[m_lastSampleIndex] = current;
}
else { m_samples.push_back(current); }
m_total += m_samples[m_lastSampleIndex];
}
};
CService::CService(CSettingsProvider *settingsProvider) :
CDBusObject(settingsProvider),
m_framePeriodSampler(std::make_unique<FramePeriodSampler>())
{ {
this->updateAirportsInRange(); this->updateAirportsInRange();
this->updateMessageBoxFromSettings(); this->updateMessageBoxFromSettings();
m_framePeriodSampler->show();
} }
CService::~CService() = default;
void CService::onAircraftModelChanged() void CService::onAircraftModelChanged()
{ {
char filename[256]; char filename[256];
@@ -63,6 +105,12 @@ namespace XSwiftBus
return XSWIFTBUS_COMMIT; return XSWIFTBUS_COMMIT;
} }
double CService::getAverageFPS()
{
if (!m_framePeriodSampler) { return 0; }
return static_cast<double>(m_framePeriodSampler->getAverageFPS());
}
void CService::addTextMessage(const std::string &text, double red, double green, double blue) void CService::addTextMessage(const std::string &text, double red, double green, double blue)
{ {
if (text.empty()) { return; } if (text.empty()) { return; }

View File

@@ -40,7 +40,7 @@ namespace XSwiftBus
CService(CSettingsProvider *settingsProvider); CService(CSettingsProvider *settingsProvider);
//! Destructor //! Destructor
virtual ~CService() override = default; virtual ~CService() override;
//! DBus interface name //! DBus interface name
static const std::string &InterfaceName() static const std::string &InterfaceName()
@@ -110,6 +110,11 @@ namespace XSwiftBus
//! True if sim time is tracking operating system time //! True if sim time is tracking operating system time
bool isUsingRealTime() const { return m_useSystemTime.get(); } bool isUsingRealTime() const { return m_useSystemTime.get(); }
//! Frames-per-second, averaged over the last 500 frames,
//! or since this function was last called, whichever is later.
//! \return Zero if no samples were collected since this function was last called.
double getAverageFPS();
//! Get aircraft latitude in degrees //! Get aircraft latitude in degrees
double getLatitudeDeg() const { return m_latitude.get(); } double getLatitudeDeg() const { return m_latitude.get(); }
@@ -309,6 +314,9 @@ namespace XSwiftBus
//! Redraw message box after reading from the settings //! Redraw message box after reading from the settings
void updateMessageBoxFromSettings(); void updateMessageBoxFromSettings();
struct FramePeriodSampler;
std::unique_ptr<FramePeriodSampler> m_framePeriodSampler;
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath; StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao; StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip; StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;