bootmanager.cpp 9.5 KB

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