profiler.cpp 7.6 KB

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