bootmanager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include <QApplication>
  2. #include <QHBoxLayout>
  3. #include <QKeyEvent>
  4. #include <QScreen>
  5. #include <QWindow>
  6. #include <fmt/format.h>
  7. #include "common/microprofile.h"
  8. #include "common/scm_rev.h"
  9. #include "common/string_util.h"
  10. #include "core/core.h"
  11. #include "core/frontend/framebuffer_layout.h"
  12. #include "core/settings.h"
  13. #include "input_common/keyboard.h"
  14. #include "input_common/main.h"
  15. #include "input_common/motion_emu.h"
  16. #include "yuzu/bootmanager.h"
  17. EmuThread::EmuThread(GRenderWindow* render_window) : render_window(render_window) {}
  18. void EmuThread::run() {
  19. render_window->MakeCurrent();
  20. MicroProfileOnThreadCreate("EmuThread");
  21. stop_run = false;
  22. // holds whether the cpu was running during the last iteration,
  23. // so that the DebugModeLeft signal can be emitted before the
  24. // next execution step
  25. bool was_active = false;
  26. while (!stop_run) {
  27. if (running) {
  28. if (!was_active)
  29. emit DebugModeLeft();
  30. Core::System::ResultStatus result = Core::System::GetInstance().RunLoop();
  31. if (result != Core::System::ResultStatus::Success) {
  32. this->SetRunning(false);
  33. emit ErrorThrown(result, Core::System::GetInstance().GetStatusDetails());
  34. }
  35. was_active = running || exec_step;
  36. if (!was_active && !stop_run)
  37. emit DebugModeEntered();
  38. } else if (exec_step) {
  39. if (!was_active)
  40. emit DebugModeLeft();
  41. exec_step = false;
  42. Core::System::GetInstance().SingleStep();
  43. emit DebugModeEntered();
  44. yieldCurrentThread();
  45. was_active = false;
  46. } else {
  47. std::unique_lock<std::mutex> lock(running_mutex);
  48. running_cv.wait(lock, [this] { return IsRunning() || exec_step || stop_run; });
  49. }
  50. }
  51. // Shutdown the core emulation
  52. Core::System::GetInstance().Shutdown();
  53. #if MICROPROFILE_ENABLED
  54. MicroProfileOnThreadExit();
  55. #endif
  56. render_window->moveContext();
  57. }
  58. // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL
  59. // context.
  60. // The corresponding functionality is handled in EmuThread instead
  61. class GGLWidgetInternal : public QGLWidget {
  62. public:
  63. GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent)
  64. : QGLWidget(fmt, parent), parent(parent) {}
  65. void paintEvent(QPaintEvent* ev) override {
  66. if (do_painting) {
  67. QPainter painter(this);
  68. }
  69. }
  70. void resizeEvent(QResizeEvent* ev) override {
  71. parent->OnClientAreaResized(ev->size().width(), ev->size().height());
  72. parent->OnFramebufferSizeChanged();
  73. }
  74. void DisablePainting() {
  75. do_painting = false;
  76. }
  77. void EnablePainting() {
  78. do_painting = true;
  79. }
  80. private:
  81. GRenderWindow* parent;
  82. bool do_painting;
  83. };
  84. GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread)
  85. : QWidget(parent), child(nullptr), emu_thread(emu_thread) {
  86. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_name,
  87. Common::g_scm_branch, Common::g_scm_desc);
  88. setWindowTitle(QString::fromStdString(window_title));
  89. InputCommon::Init();
  90. }
  91. GRenderWindow::~GRenderWindow() {
  92. InputCommon::Shutdown();
  93. }
  94. void GRenderWindow::moveContext() {
  95. DoneCurrent();
  96. // If the thread started running, move the GL Context to the new thread. Otherwise, move it
  97. // back.
  98. auto thread = (QThread::currentThread() == qApp->thread() && emu_thread != nullptr)
  99. ? emu_thread
  100. : qApp->thread();
  101. child->context()->moveToThread(thread);
  102. }
  103. void GRenderWindow::SwapBuffers() {
  104. #if !defined(QT_NO_DEBUG)
  105. // Qt debug runtime prints a bogus warning on the console if you haven't called makeCurrent
  106. // since the last time you called swapBuffers. This presumably means something if you're using
  107. // QGLWidget the "regular" way, but in our multi-threaded use case is harmless since we never
  108. // call doneCurrent in this thread.
  109. child->makeCurrent();
  110. #endif
  111. child->swapBuffers();
  112. }
  113. void GRenderWindow::MakeCurrent() {
  114. child->makeCurrent();
  115. }
  116. void GRenderWindow::DoneCurrent() {
  117. child->doneCurrent();
  118. }
  119. void GRenderWindow::PollEvents() {}
  120. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  121. //
  122. // Older versions get the window size (density independent pixels),
  123. // and hence, do not support DPI scaling ("retina" displays).
  124. // The result will be a viewport that is smaller than the extent of the window.
  125. void GRenderWindow::OnFramebufferSizeChanged() {
  126. // Screen changes potentially incur a change in screen DPI, hence we should update the
  127. // framebuffer size
  128. qreal pixelRatio = windowPixelRatio();
  129. unsigned width = child->QPaintDevice::width() * pixelRatio;
  130. unsigned height = child->QPaintDevice::height() * pixelRatio;
  131. UpdateCurrentFramebufferLayout(width, height);
  132. }
  133. void GRenderWindow::BackupGeometry() {
  134. geometry = ((QGLWidget*)this)->saveGeometry();
  135. }
  136. void GRenderWindow::RestoreGeometry() {
  137. // We don't want to back up the geometry here (obviously)
  138. QWidget::restoreGeometry(geometry);
  139. }
  140. void GRenderWindow::restoreGeometry(const QByteArray& geometry) {
  141. // Make sure users of this class don't need to deal with backing up the geometry themselves
  142. QWidget::restoreGeometry(geometry);
  143. BackupGeometry();
  144. }
  145. QByteArray GRenderWindow::saveGeometry() {
  146. // If we are a top-level widget, store the current geometry
  147. // otherwise, store the last backup
  148. if (parent() == nullptr)
  149. return ((QGLWidget*)this)->saveGeometry();
  150. else
  151. return geometry;
  152. }
  153. qreal GRenderWindow::windowPixelRatio() {
  154. // windowHandle() might not be accessible until the window is displayed to screen.
  155. return windowHandle() ? windowHandle()->screen()->devicePixelRatio() : 1.0f;
  156. }
  157. void GRenderWindow::closeEvent(QCloseEvent* event) {
  158. emit Closed();
  159. QWidget::closeEvent(event);
  160. }
  161. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  162. InputCommon::GetKeyboard()->PressKey(event->key());
  163. }
  164. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  165. InputCommon::GetKeyboard()->ReleaseKey(event->key());
  166. }
  167. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  168. auto pos = event->pos();
  169. if (event->button() == Qt::LeftButton) {
  170. qreal pixelRatio = windowPixelRatio();
  171. this->TouchPressed(static_cast<unsigned>(pos.x() * pixelRatio),
  172. static_cast<unsigned>(pos.y() * pixelRatio));
  173. } else if (event->button() == Qt::RightButton) {
  174. InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y());
  175. }
  176. }
  177. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  178. auto pos = event->pos();
  179. qreal pixelRatio = windowPixelRatio();
  180. this->TouchMoved(std::max(static_cast<unsigned>(pos.x() * pixelRatio), 0u),
  181. std::max(static_cast<unsigned>(pos.y() * pixelRatio), 0u));
  182. InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y());
  183. }
  184. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  185. if (event->button() == Qt::LeftButton)
  186. this->TouchReleased();
  187. else if (event->button() == Qt::RightButton)
  188. InputCommon::GetMotionEmu()->EndTilt();
  189. }
  190. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  191. QWidget::focusOutEvent(event);
  192. InputCommon::GetKeyboard()->ReleaseAllKeys();
  193. }
  194. void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height) {
  195. NotifyClientAreaSizeChanged(std::make_pair(width, height));
  196. }
  197. void GRenderWindow::InitRenderTarget() {
  198. if (child) {
  199. delete child;
  200. }
  201. if (layout()) {
  202. delete layout();
  203. }
  204. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  205. // WA_DontShowOnScreen, WA_DeleteOnClose
  206. QGLFormat fmt;
  207. fmt.setVersion(3, 3);
  208. fmt.setProfile(QGLFormat::CoreProfile);
  209. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  210. fmt.setOption(QGL::NoDeprecatedFunctions);
  211. child = new GGLWidgetInternal(fmt, this);
  212. QBoxLayout* layout = new QHBoxLayout(this);
  213. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  214. layout->addWidget(child);
  215. layout->setMargin(0);
  216. setLayout(layout);
  217. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  218. OnFramebufferSizeChanged();
  219. NotifyClientAreaSizeChanged(std::pair<unsigned, unsigned>(child->width(), child->height()));
  220. BackupGeometry();
  221. }
  222. void GRenderWindow::OnMinimalClientAreaChangeRequest(
  223. const std::pair<unsigned, unsigned>& minimal_size) {
  224. setMinimumSize(minimal_size.first, minimal_size.second);
  225. }
  226. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  227. this->emu_thread = emu_thread;
  228. child->DisablePainting();
  229. }
  230. void GRenderWindow::OnEmulationStopping() {
  231. emu_thread = nullptr;
  232. child->EnablePainting();
  233. }
  234. void GRenderWindow::showEvent(QShowEvent* event) {
  235. QWidget::showEvent(event);
  236. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  237. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  238. Qt::UniqueConnection);
  239. }