bootmanager.cpp 9.3 KB

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