easy_frame_rate_viewer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /************************************************************************
  2. * file name : easy_frame_rate_viewer.cpp
  3. * ----------------- :
  4. * creation time : 2017/04/02
  5. * author : Victor Zarubkin
  6. * email : [email protected]
  7. * ----------------- :
  8. * description : This file contains implementation of EasyFrameRateViewer widget.
  9. * ----------------- :
  10. * change log : * 2017/04/02 Victor Zarubkin: Initial commit.
  11. * :
  12. * : *
  13. * ----------------- :
  14. * license : Lightweight profiler library for c++
  15. * : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin
  16. * :
  17. * : Licensed under either of
  18. * : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
  19. * : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  20. * : at your option.
  21. * :
  22. * : The MIT License
  23. * :
  24. * : Permission is hereby granted, free of charge, to any person obtaining a copy
  25. * : of this software and associated documentation files (the "Software"), to deal
  26. * : in the Software without restriction, including without limitation the rights
  27. * : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  28. * : of the Software, and to permit persons to whom the Software is furnished
  29. * : to do so, subject to the following conditions:
  30. * :
  31. * : The above copyright notice and this permission notice shall be included in all
  32. * : copies or substantial portions of the Software.
  33. * :
  34. * : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  35. * : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  36. * : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  37. * : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  38. * : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  39. * : USE OR OTHER DEALINGS IN THE SOFTWARE.
  40. * :
  41. * : The Apache License, Version 2.0 (the "License")
  42. * :
  43. * : You may not use this file except in compliance with the License.
  44. * : You may obtain a copy of the License at
  45. * :
  46. * : http://www.apache.org/licenses/LICENSE-2.0
  47. * :
  48. * : Unless required by applicable law or agreed to in writing, software
  49. * : distributed under the License is distributed on an "AS IS" BASIS,
  50. * : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  51. * : See the License for the specific language governing permissions and
  52. * : limitations under the License.
  53. ************************************************************************/
  54. #include <QGraphicsScene>
  55. #include <QResizeEvent>
  56. #include <QContextMenuEvent>
  57. #include <QMenu>
  58. #include <QAction>
  59. #include "easy_frame_rate_viewer.h"
  60. #include "globals.h"
  61. const int INTERVAL_WIDTH = 20;
  62. //////////////////////////////////////////////////////////////////////////
  63. EasyFPSGraphicsItem::EasyFPSGraphicsItem() : Parent(nullptr)
  64. {
  65. }
  66. EasyFPSGraphicsItem::~EasyFPSGraphicsItem()
  67. {
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. QRectF EasyFPSGraphicsItem::boundingRect() const
  71. {
  72. return m_boundingRect;
  73. }
  74. void EasyFPSGraphicsItem::setBoundingRect(const QRectF& _boundingRect)
  75. {
  76. m_boundingRect = _boundingRect;
  77. }
  78. void EasyFPSGraphicsItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h)
  79. {
  80. m_boundingRect.setRect(x, y, w, h);
  81. }
  82. //////////////////////////////////////////////////////////////////////////
  83. void EasyFPSGraphicsItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem*, QWidget*)
  84. {
  85. if (m_frames.empty())
  86. return;
  87. const auto fontMetrics = QFontMetrics(scene()->font());
  88. const int fontHeight = fontMetrics.height() + 2;
  89. const qreal h = m_boundingRect.height() - (fontHeight << 1) - 4;
  90. if (h < 0)
  91. return;
  92. const qreal halfWidth = m_boundingRect.width() * 0.5 - INTERVAL_WIDTH;
  93. const int halfMax = static_cast<int>(0.5 + halfWidth / INTERVAL_WIDTH);
  94. const int half = static_cast<int>(m_frames.size() / 2);
  95. const qreal top = fontHeight, bottom = h + 4 + fontHeight;
  96. qreal y;
  97. _painter->save();
  98. _painter->drawLine(QPointF(0, top), QPointF(m_boundingRect.width(), top));
  99. _painter->drawLine(QPointF(0, bottom), QPointF(m_boundingRect.width(), bottom));
  100. _painter->setPen(Qt::lightGray);
  101. y = m_boundingRect.height() * 0.5;
  102. _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y));
  103. y -= h * 0.25;
  104. _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y));
  105. y += h * 0.5;
  106. _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y));
  107. m_points1.reserve(m_frames.size());
  108. m_points2.reserve(m_frames.size());
  109. int n = 0;
  110. qreal x = m_boundingRect.width() * 0.5 + std::min(halfMax, half) * INTERVAL_WIDTH, localMax = 0, localMin = 1e30;
  111. const qreal xCurrent = x;
  112. for (int i = static_cast<int>(m_frames.size()) - 1; i > -1 && x >= 0; --i, x -= INTERVAL_WIDTH, ++n)
  113. {
  114. const auto& val = m_frames[i];
  115. if (val.first > localMax)
  116. localMax = val.first;
  117. if (val.first < localMin)
  118. localMin = val.first;
  119. m_points1.emplace_back(x, static_cast<qreal>(val.first) + 1e-3);
  120. if (val.second > localMax)
  121. localMax = val.second;
  122. if (val.second < localMin)
  123. localMin = val.second;
  124. m_points2.emplace_back(x, static_cast<qreal>(val.second) + 1e-3);
  125. _painter->drawLine(QPointF(x, top + 1), QPointF(x, bottom - 1));
  126. }
  127. const auto delta = std::max(localMax - localMin, 1e-3);
  128. _painter->setPen(Qt::black);
  129. qreal frameTime = std::max(localMax, 1.);
  130. _painter->drawText(5, 0, m_boundingRect.width() - 10, fontHeight, Qt::AlignVCenter | Qt::AlignLeft, QString("Slowest %1 FPS (%2)")
  131. .arg(static_cast<quint64>(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMax, 1)));
  132. frameTime = std::max(m_frames.back().first, 1U);
  133. _painter->drawText(5, 0, xCurrent - 5, fontHeight, Qt::AlignVCenter | Qt::AlignRight, QString("Max current %1 FPS (%2)")
  134. .arg(static_cast<quint64>(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, m_frames.back().first, 1)));
  135. frameTime = std::max(m_frames.back().second, 1U);
  136. _painter->drawText(5, bottom, xCurrent - 5, fontHeight, Qt::AlignVCenter | Qt::AlignRight, QString("Avg current %1 FPS (%2)")
  137. .arg(static_cast<quint64>(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, m_frames.back().second, 1)));
  138. frameTime = std::max(localMin, 1.);
  139. _painter->drawText(5, bottom, m_boundingRect.width() - 10, fontHeight, Qt::AlignVCenter | Qt::AlignLeft, QString("Fastest %1 FPS (%2)")
  140. .arg(static_cast<quint64>(1e6 / frameTime)).arg(::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin, 1)));
  141. if (localMin < EASY_GLOBALS.frame_time && EASY_GLOBALS.frame_time < localMax)
  142. {
  143. y = fontHeight + 2 + h * (1. - (EASY_GLOBALS.frame_time - localMin) / delta);
  144. _painter->setPen(Qt::DashLine);
  145. _painter->drawLine(QPointF(0, y), QPointF(m_boundingRect.width(), y));
  146. }
  147. for (int i = 0; i < n; ++i)
  148. {
  149. auto& point1 = m_points1[i];
  150. point1.setY(fontHeight + 2 + h * (1. - (point1.y() - localMin) / delta));
  151. auto& point2 = m_points2[i];
  152. point2.setY(fontHeight + 2 + h * (1. - (point2.y() - localMin) / delta));
  153. }
  154. _painter->setRenderHint(QPainter::Antialiasing, true);
  155. QPen pen(QColor::fromRgba(0x80ff0000));
  156. pen.setWidth(EASY_GLOBALS.fps_widget_line_width);
  157. _painter->setPen(pen);
  158. if (n > 1)
  159. {
  160. _painter->drawPolyline(m_points1.data(), n);
  161. pen.setColor(QColor::fromRgba(0x800000ff));
  162. _painter->setPen(pen);
  163. _painter->drawPolyline(m_points2.data(), n);
  164. }
  165. else
  166. {
  167. _painter->drawPoint(m_points1.back());
  168. pen.setColor(QColor::fromRgba(0x800000ff));
  169. _painter->setPen(pen);
  170. _painter->drawPoint(m_points2.back());
  171. }
  172. const auto txtTop = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.75, 1);
  173. const auto txtMid = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.5, 1);
  174. const auto txtBtm = ::profiler_gui::timeStringReal(EASY_GLOBALS.time_units, localMin + delta * 0.25, 1);
  175. _painter->setPen(Qt::NoPen);
  176. _painter->setBrush(Qt::white);
  177. _painter->drawRect(0, top + 1, std::max({fontMetrics.width(txtTop), fontMetrics.width(txtMid), fontMetrics.width(txtBtm)}) + 8, bottom - top - 1);
  178. _painter->setPen(Qt::black);
  179. _painter->setBrush(Qt::NoBrush);
  180. y = m_boundingRect.height() * 0.5;
  181. _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtMid);
  182. y -= h * 0.25;
  183. _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtTop);
  184. y += h * 0.5;
  185. _painter->drawText(5, y - (fontHeight >> 1), m_boundingRect.width(), fontHeight, Qt::AlignVCenter | Qt::AlignLeft, txtBtm);
  186. _painter->restore();
  187. m_points1.clear();
  188. m_points2.clear();
  189. }
  190. //////////////////////////////////////////////////////////////////////////
  191. void EasyFPSGraphicsItem::clear()
  192. {
  193. m_frames.clear();
  194. }
  195. void EasyFPSGraphicsItem::addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime)
  196. {
  197. m_frames.emplace_back(_maxFrameTime, _avgFrameTime);
  198. if (static_cast<int>(m_frames.size()) > EASY_GLOBALS.max_fps_history)
  199. m_frames.pop_front();
  200. }
  201. //////////////////////////////////////////////////////////////////////////
  202. EasyFrameRateViewer::EasyFrameRateViewer(QWidget* _parent) : Parent(_parent), m_fpsItem(nullptr)
  203. {
  204. setCacheMode(QGraphicsView::CacheNone);
  205. //setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
  206. setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  207. setOptimizationFlag(QGraphicsView::DontSavePainterState, true);
  208. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  209. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  210. setContentsMargins(0, 0, 0, 0);
  211. setScene(new QGraphicsScene(this));
  212. scene()->setSceneRect(0, 0, 50, 50);
  213. m_fpsItem = new EasyFPSGraphicsItem();
  214. m_fpsItem->setPos(0, 0);
  215. m_fpsItem->setBoundingRect(0, 0, 50, 50);
  216. scene()->addItem(m_fpsItem);
  217. centerOn(0, 0);
  218. // Dirty hack for QDockWidget stupid initial size policy :(
  219. setFixedHeight(10); // Set very small height to enable appropriate minimum height on the application startup
  220. QTimer::singleShot(100, [this]()
  221. {
  222. // Now set appropriate minimum height
  223. setMinimumHeight((QFontMetrics(scene()->font()).height() + 3) * 6);
  224. setMaximumHeight(minimumHeight() * 20);
  225. });
  226. }
  227. EasyFrameRateViewer::~EasyFrameRateViewer()
  228. {
  229. }
  230. void EasyFrameRateViewer::clear()
  231. {
  232. m_fpsItem->clear();
  233. scene()->update();
  234. }
  235. void EasyFrameRateViewer::addPoint(uint32_t _maxFrameTime, uint32_t _avgFrameTime)
  236. {
  237. m_fpsItem->addPoint(_maxFrameTime, _avgFrameTime);
  238. scene()->update();
  239. }
  240. void EasyFrameRateViewer::resizeEvent(QResizeEvent* _event)
  241. {
  242. Parent::resizeEvent(_event);
  243. auto size = _event->size();
  244. m_fpsItem->setBoundingRect(0, 0, size.width(), size.height());
  245. scene()->setSceneRect(m_fpsItem->boundingRect());
  246. scene()->update();
  247. }
  248. void EasyFrameRateViewer::hideEvent(QHideEvent* _event)
  249. {
  250. Parent::hideEvent(_event);
  251. EASY_GLOBALS.fps_enabled = isVisible();
  252. clear();
  253. }
  254. void EasyFrameRateViewer::showEvent(QShowEvent* _event)
  255. {
  256. Parent::showEvent(_event);
  257. EASY_GLOBALS.fps_enabled = isVisible();
  258. clear();
  259. }
  260. void EasyFrameRateViewer::contextMenuEvent(QContextMenuEvent* _event)
  261. {
  262. QMenu menu;
  263. QAction* action = nullptr;
  264. action = menu.addAction(QIcon(":/Delete"), "Clear");
  265. connect(action, &QAction::triggered, [this](bool){ clear(); });
  266. action = menu.addAction("Close");
  267. connect(action, &QAction::triggered, [this](bool){ parentWidget()->hide(); });
  268. menu.exec(QCursor::pos());
  269. _event->accept();
  270. }
  271. //////////////////////////////////////////////////////////////////////////