bootmanager.cpp 9.1 KB

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