bootmanager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "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. if (!Settings::values.use_multi_core) {
  19. // Single core mode must acquire OpenGL context for entire emulation session
  20. render_window->MakeCurrent();
  21. }
  22. MicroProfileOnThreadCreate("EmuThread");
  23. stop_run = false;
  24. // holds whether the cpu was running during the last iteration,
  25. // so that the DebugModeLeft signal can be emitted before the
  26. // next execution step
  27. bool was_active = false;
  28. while (!stop_run) {
  29. if (running) {
  30. if (!was_active)
  31. emit DebugModeLeft();
  32. Core::System::ResultStatus result = Core::System::GetInstance().RunLoop();
  33. if (result != Core::System::ResultStatus::Success) {
  34. this->SetRunning(false);
  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. setWindowTitle(QStringLiteral("yuzu %1 | %2-%3")
  89. .arg(Common::g_build_name, Common::g_scm_branch, Common::g_scm_desc));
  90. setAttribute(Qt::WA_AcceptTouchEvents);
  91. InputCommon::Init();
  92. InputCommon::StartJoystickEventHandler();
  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() const {
  157. // windowHandle() might not be accessible until the window is displayed to screen.
  158. return windowHandle() ? windowHandle()->screen()->devicePixelRatio() : 1.0f;
  159. }
  160. std::pair<unsigned, unsigned> GRenderWindow::ScaleTouch(const QPointF pos) const {
  161. const qreal pixel_ratio = windowPixelRatio();
  162. return {static_cast<unsigned>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
  163. static_cast<unsigned>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
  164. }
  165. void GRenderWindow::closeEvent(QCloseEvent* event) {
  166. emit Closed();
  167. QWidget::closeEvent(event);
  168. }
  169. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  170. InputCommon::GetKeyboard()->PressKey(event->key());
  171. }
  172. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  173. InputCommon::GetKeyboard()->ReleaseKey(event->key());
  174. }
  175. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  176. if (event->source() == Qt::MouseEventSynthesizedBySystem)
  177. return; // touch input is handled in TouchBeginEvent
  178. auto pos = event->pos();
  179. if (event->button() == Qt::LeftButton) {
  180. const auto [x, y] = ScaleTouch(pos);
  181. this->TouchPressed(x, y);
  182. } else if (event->button() == Qt::RightButton) {
  183. InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y());
  184. }
  185. }
  186. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  187. if (event->source() == Qt::MouseEventSynthesizedBySystem)
  188. return; // touch input is handled in TouchUpdateEvent
  189. auto pos = event->pos();
  190. const auto [x, y] = ScaleTouch(pos);
  191. this->TouchMoved(x, y);
  192. InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y());
  193. }
  194. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  195. if (event->source() == Qt::MouseEventSynthesizedBySystem)
  196. return; // touch input is handled in TouchEndEvent
  197. if (event->button() == Qt::LeftButton)
  198. this->TouchReleased();
  199. else if (event->button() == Qt::RightButton)
  200. InputCommon::GetMotionEmu()->EndTilt();
  201. }
  202. void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
  203. // TouchBegin always has exactly one touch point, so take the .first()
  204. const auto [x, y] = ScaleTouch(event->touchPoints().first().pos());
  205. this->TouchPressed(x, y);
  206. }
  207. void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
  208. QPointF pos;
  209. int active_points = 0;
  210. // average all active touch points
  211. for (const auto tp : event->touchPoints()) {
  212. if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) {
  213. active_points++;
  214. pos += tp.pos();
  215. }
  216. }
  217. pos /= active_points;
  218. const auto [x, y] = ScaleTouch(pos);
  219. this->TouchMoved(x, y);
  220. }
  221. void GRenderWindow::TouchEndEvent() {
  222. this->TouchReleased();
  223. }
  224. bool GRenderWindow::event(QEvent* event) {
  225. if (event->type() == QEvent::TouchBegin) {
  226. TouchBeginEvent(static_cast<QTouchEvent*>(event));
  227. return true;
  228. } else if (event->type() == QEvent::TouchUpdate) {
  229. TouchUpdateEvent(static_cast<QTouchEvent*>(event));
  230. return true;
  231. } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
  232. TouchEndEvent();
  233. return true;
  234. }
  235. return QWidget::event(event);
  236. }
  237. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  238. QWidget::focusOutEvent(event);
  239. InputCommon::GetKeyboard()->ReleaseAllKeys();
  240. }
  241. void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height) {
  242. NotifyClientAreaSizeChanged(std::make_pair(width, height));
  243. }
  244. void GRenderWindow::InitRenderTarget() {
  245. if (child) {
  246. delete child;
  247. }
  248. if (layout()) {
  249. delete layout();
  250. }
  251. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  252. // WA_DontShowOnScreen, WA_DeleteOnClose
  253. QGLFormat fmt;
  254. fmt.setVersion(4, 3);
  255. fmt.setProfile(QGLFormat::CoreProfile);
  256. fmt.setSwapInterval(false);
  257. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  258. fmt.setOption(QGL::NoDeprecatedFunctions);
  259. child = new GGLWidgetInternal(fmt, this);
  260. QBoxLayout* layout = new QHBoxLayout(this);
  261. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  262. layout->addWidget(child);
  263. layout->setMargin(0);
  264. setLayout(layout);
  265. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  266. OnFramebufferSizeChanged();
  267. NotifyClientAreaSizeChanged(std::pair<unsigned, unsigned>(child->width(), child->height()));
  268. BackupGeometry();
  269. }
  270. void GRenderWindow::OnMinimalClientAreaChangeRequest(
  271. const std::pair<unsigned, unsigned>& minimal_size) {
  272. setMinimumSize(minimal_size.first, minimal_size.second);
  273. }
  274. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  275. this->emu_thread = emu_thread;
  276. child->DisablePainting();
  277. }
  278. void GRenderWindow::OnEmulationStopping() {
  279. emu_thread = nullptr;
  280. child->EnablePainting();
  281. }
  282. void GRenderWindow::showEvent(QShowEvent* event) {
  283. QWidget::showEvent(event);
  284. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  285. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  286. Qt::UniqueConnection);
  287. }