profiler.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QAction>
  5. #include <QLayout>
  6. #include <QMouseEvent>
  7. #include <QPainter>
  8. #include <QString>
  9. #include <QTimer>
  10. #include "common/common_types.h"
  11. #include "common/microprofile.h"
  12. #include "yuzu/debugger/profiler.h"
  13. #include "yuzu/util/util.h"
  14. // Include the implementation of the UI in this file. This isn't in microprofile.cpp because the
  15. // non-Qt frontends don't need it (and don't implement the UI drawing hooks either).
  16. #if MICROPROFILE_ENABLED
  17. #define MICROPROFILEUI_IMPL 1
  18. #include "common/microprofileui.h"
  19. class MicroProfileWidget : public QWidget {
  20. public:
  21. MicroProfileWidget(QWidget* parent = nullptr);
  22. protected:
  23. void paintEvent(QPaintEvent* ev) override;
  24. void showEvent(QShowEvent* ev) override;
  25. void hideEvent(QHideEvent* ev) override;
  26. void mouseMoveEvent(QMouseEvent* ev) override;
  27. void mousePressEvent(QMouseEvent* ev) override;
  28. void mouseReleaseEvent(QMouseEvent* ev) override;
  29. void wheelEvent(QWheelEvent* ev) override;
  30. void keyPressEvent(QKeyEvent* ev) override;
  31. void keyReleaseEvent(QKeyEvent* ev) override;
  32. private:
  33. /// This timer is used to redraw the widget's contents continuously. To save resources, it only
  34. /// runs while the widget is visible.
  35. QTimer update_timer;
  36. /// Scale the coordinate system appropriately when dpi != 96.
  37. qreal x_scale = 1.0, y_scale = 1.0;
  38. };
  39. #endif
  40. MicroProfileDialog::MicroProfileDialog(QWidget* parent) : QWidget(parent, Qt::Dialog) {
  41. setObjectName(QStringLiteral("MicroProfile"));
  42. setWindowTitle(tr("MicroProfile"));
  43. resize(1000, 600);
  44. // Remove the "?" button from the titlebar and enable the maximize button
  45. setWindowFlags((windowFlags() & ~Qt::WindowContextHelpButtonHint) |
  46. Qt::WindowMaximizeButtonHint);
  47. #if MICROPROFILE_ENABLED
  48. MicroProfileWidget* widget = new MicroProfileWidget(this);
  49. QLayout* layout = new QVBoxLayout(this);
  50. layout->setContentsMargins(0, 0, 0, 0);
  51. layout->addWidget(widget);
  52. setLayout(layout);
  53. // Configure focus so that widget is focusable and the dialog automatically forwards focus to
  54. // it.
  55. setFocusProxy(widget);
  56. widget->setFocusPolicy(Qt::StrongFocus);
  57. widget->setFocus();
  58. #endif
  59. }
  60. QAction* MicroProfileDialog::toggleViewAction() {
  61. if (toggle_view_action == nullptr) {
  62. toggle_view_action = new QAction(windowTitle(), this);
  63. toggle_view_action->setCheckable(true);
  64. toggle_view_action->setChecked(isVisible());
  65. connect(toggle_view_action, &QAction::toggled, this, &MicroProfileDialog::setVisible);
  66. }
  67. return toggle_view_action;
  68. }
  69. void MicroProfileDialog::showEvent(QShowEvent* ev) {
  70. if (toggle_view_action) {
  71. toggle_view_action->setChecked(isVisible());
  72. }
  73. QWidget::showEvent(ev);
  74. }
  75. void MicroProfileDialog::hideEvent(QHideEvent* ev) {
  76. if (toggle_view_action) {
  77. toggle_view_action->setChecked(isVisible());
  78. }
  79. QWidget::hideEvent(ev);
  80. }
  81. #if MICROPROFILE_ENABLED
  82. /// There's no way to pass a user pointer to MicroProfile, so this variable is used to make the
  83. /// QPainter available inside the drawing callbacks.
  84. static QPainter* mp_painter = nullptr;
  85. MicroProfileWidget::MicroProfileWidget(QWidget* parent) : QWidget(parent) {
  86. // Send mouse motion events even when not dragging.
  87. setMouseTracking(true);
  88. MicroProfileSetDisplayMode(1); // Timers screen
  89. MicroProfileInitUI();
  90. connect(&update_timer, &QTimer::timeout, this,
  91. static_cast<void (MicroProfileWidget::*)()>(&MicroProfileWidget::update));
  92. }
  93. void MicroProfileWidget::paintEvent(QPaintEvent* ev) {
  94. QPainter painter(this);
  95. // The units used by Microprofile for drawing are based in pixels on a 96 dpi display.
  96. x_scale = qreal(painter.device()->logicalDpiX()) / 96.0;
  97. y_scale = qreal(painter.device()->logicalDpiY()) / 96.0;
  98. painter.scale(x_scale, y_scale);
  99. painter.setBackground(Qt::black);
  100. painter.eraseRect(rect());
  101. QFont font = GetMonospaceFont();
  102. font.setPixelSize(MICROPROFILE_TEXT_HEIGHT);
  103. painter.setFont(font);
  104. mp_painter = &painter;
  105. MicroProfileDraw(rect().width() / x_scale, rect().height() / y_scale);
  106. mp_painter = nullptr;
  107. }
  108. void MicroProfileWidget::showEvent(QShowEvent* ev) {
  109. update_timer.start(15); // ~60 Hz
  110. QWidget::showEvent(ev);
  111. }
  112. void MicroProfileWidget::hideEvent(QHideEvent* ev) {
  113. update_timer.stop();
  114. QWidget::hideEvent(ev);
  115. }
  116. void MicroProfileWidget::mouseMoveEvent(QMouseEvent* ev) {
  117. MicroProfileMousePosition(ev->x() / x_scale, ev->y() / y_scale, 0);
  118. ev->accept();
  119. }
  120. void MicroProfileWidget::mousePressEvent(QMouseEvent* ev) {
  121. MicroProfileMousePosition(ev->x() / x_scale, ev->y() / y_scale, 0);
  122. MicroProfileMouseButton(ev->buttons() & Qt::LeftButton, ev->buttons() & Qt::RightButton);
  123. ev->accept();
  124. }
  125. void MicroProfileWidget::mouseReleaseEvent(QMouseEvent* ev) {
  126. MicroProfileMousePosition(ev->x() / x_scale, ev->y() / y_scale, 0);
  127. MicroProfileMouseButton(ev->buttons() & Qt::LeftButton, ev->buttons() & Qt::RightButton);
  128. ev->accept();
  129. }
  130. void MicroProfileWidget::wheelEvent(QWheelEvent* ev) {
  131. MicroProfileMousePosition(ev->x() / x_scale, ev->y() / y_scale, ev->delta() / 120);
  132. ev->accept();
  133. }
  134. void MicroProfileWidget::keyPressEvent(QKeyEvent* ev) {
  135. if (ev->key() == Qt::Key_Control) {
  136. // Inform MicroProfile that the user is holding Ctrl.
  137. MicroProfileModKey(1);
  138. }
  139. QWidget::keyPressEvent(ev);
  140. }
  141. void MicroProfileWidget::keyReleaseEvent(QKeyEvent* ev) {
  142. if (ev->key() == Qt::Key_Control) {
  143. MicroProfileModKey(0);
  144. }
  145. QWidget::keyReleaseEvent(ev);
  146. }
  147. // These functions are called by MicroProfileDraw to draw the interface elements on the screen.
  148. void MicroProfileDrawText(int x, int y, u32 hex_color, const char* text, u32 text_length) {
  149. // hex_color does not include an alpha, so it must be assumed to be 255
  150. mp_painter->setPen(QColor::fromRgb(hex_color));
  151. // It's impossible to draw a string using a monospaced font with a fixed width per cell in a
  152. // way that's reliable across different platforms and fonts as far as I (yuriks) can tell, so
  153. // draw each character individually in order to precisely control the text advance.
  154. for (u32 i = 0; i < text_length; ++i) {
  155. // Position the text baseline 1 pixel above the bottom of the text cell, this gives nice
  156. // vertical alignment of text for a wide range of tested fonts.
  157. mp_painter->drawText(x, y + MICROPROFILE_TEXT_HEIGHT - 2, QString{QLatin1Char{text[i]}});
  158. x += MICROPROFILE_TEXT_WIDTH + 1;
  159. }
  160. }
  161. void MicroProfileDrawBox(int left, int top, int right, int bottom, u32 hex_color,
  162. MicroProfileBoxType type) {
  163. QColor color = QColor::fromRgba(hex_color);
  164. QBrush brush = color;
  165. if (type == MicroProfileBoxTypeBar) {
  166. QLinearGradient gradient(left, top, left, bottom);
  167. gradient.setColorAt(0.f, color.lighter(125));
  168. gradient.setColorAt(1.f, color.darker(125));
  169. brush = gradient;
  170. }
  171. mp_painter->fillRect(left, top, right - left, bottom - top, brush);
  172. }
  173. void MicroProfileDrawLine2D(u32 vertices_length, float* vertices, u32 hex_color) {
  174. // Temporary vector used to convert between the float array and QPointF. Marked static to reuse
  175. // the allocation across calls.
  176. static std::vector<QPointF> point_buf;
  177. for (u32 i = 0; i < vertices_length; ++i) {
  178. point_buf.emplace_back(vertices[i * 2 + 0], vertices[i * 2 + 1]);
  179. }
  180. // hex_color does not include an alpha, so it must be assumed to be 255
  181. mp_painter->setPen(QColor::fromRgb(hex_color));
  182. mp_painter->drawPolyline(point_buf.data(), vertices_length);
  183. point_buf.clear();
  184. }
  185. #endif