bootmanager.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "citra_qt/bootmanager.h"
  10. #include "common/microprofile.h"
  11. #include "common/scm_rev.h"
  12. #include "common/string_util.h"
  13. #include "core/3ds.h"
  14. #include "core/core.h"
  15. #include "core/settings.h"
  16. #include "input_common/keyboard.h"
  17. #include "input_common/main.h"
  18. EmuThread::EmuThread(GRenderWindow* render_window)
  19. : exec_step(false), running(false), stop_run(false), render_window(render_window) {}
  20. void EmuThread::run() {
  21. render_window->MakeCurrent();
  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. emit ErrorThrown(result, Core::System::GetInstance().GetStatusDetails());
  35. }
  36. was_active = running || exec_step;
  37. if (!was_active && !stop_run)
  38. emit DebugModeEntered();
  39. } else if (exec_step) {
  40. if (!was_active)
  41. emit DebugModeLeft();
  42. exec_step = false;
  43. Core::System::GetInstance().SingleStep();
  44. emit DebugModeEntered();
  45. yieldCurrentThread();
  46. was_active = false;
  47. } else {
  48. std::unique_lock<std::mutex> lock(running_mutex);
  49. running_cv.wait(lock, [this] { return IsRunning() || exec_step || stop_run; });
  50. }
  51. }
  52. // Shutdown the core emulation
  53. Core::System::GetInstance().Shutdown();
  54. #if MICROPROFILE_ENABLED
  55. MicroProfileOnThreadExit();
  56. #endif
  57. render_window->moveContext();
  58. }
  59. // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL
  60. // context.
  61. // The corresponding functionality is handled in EmuThread instead
  62. class GGLWidgetInternal : public QGLWidget {
  63. public:
  64. GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent)
  65. : QGLWidget(fmt, parent), parent(parent) {}
  66. void paintEvent(QPaintEvent* ev) override {
  67. if (do_painting) {
  68. QPainter painter(this);
  69. }
  70. }
  71. void resizeEvent(QResizeEvent* ev) override {
  72. parent->OnClientAreaResized(ev->size().width(), ev->size().height());
  73. parent->OnFramebufferSizeChanged();
  74. }
  75. void DisablePainting() {
  76. do_painting = false;
  77. }
  78. void EnablePainting() {
  79. do_painting = true;
  80. }
  81. private:
  82. GRenderWindow* parent;
  83. bool do_painting;
  84. };
  85. GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread)
  86. : QWidget(parent), child(nullptr), emu_thread(emu_thread) {
  87. std::string window_title = Common::StringFromFormat("Citra %s| %s-%s", Common::g_build_name,
  88. Common::g_scm_branch, Common::g_scm_desc);
  89. setWindowTitle(QString::fromStdString(window_title));
  90. InputCommon::Init();
  91. }
  92. GRenderWindow::~GRenderWindow() {
  93. InputCommon::Shutdown();
  94. }
  95. void GRenderWindow::moveContext() {
  96. DoneCurrent();
  97. // We need to move GL context to the swapping thread in Qt5
  98. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  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. #endif
  106. }
  107. void GRenderWindow::SwapBuffers() {
  108. #if !defined(QT_NO_DEBUG)
  109. // Qt debug runtime prints a bogus warning on the console if you haven't called makeCurrent
  110. // since the last time you called swapBuffers. This presumably means something if you're using
  111. // QGLWidget the "regular" way, but in our multi-threaded use case is harmless since we never
  112. // call doneCurrent in this thread.
  113. child->makeCurrent();
  114. #endif
  115. child->swapBuffers();
  116. }
  117. void GRenderWindow::MakeCurrent() {
  118. child->makeCurrent();
  119. }
  120. void GRenderWindow::DoneCurrent() {
  121. child->doneCurrent();
  122. }
  123. void GRenderWindow::PollEvents() {}
  124. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  125. //
  126. // Older versions get the window size (density independent pixels),
  127. // and hence, do not support DPI scaling ("retina" displays).
  128. // The result will be a viewport that is smaller than the extent of the window.
  129. void GRenderWindow::OnFramebufferSizeChanged() {
  130. // Screen changes potentially incur a change in screen DPI, hence we should update the
  131. // framebuffer size
  132. qreal pixelRatio = windowPixelRatio();
  133. unsigned width = child->QPaintDevice::width() * pixelRatio;
  134. unsigned height = child->QPaintDevice::height() * pixelRatio;
  135. UpdateCurrentFramebufferLayout(width, height);
  136. }
  137. void GRenderWindow::BackupGeometry() {
  138. geometry = ((QGLWidget*)this)->saveGeometry();
  139. }
  140. void GRenderWindow::RestoreGeometry() {
  141. // We don't want to back up the geometry here (obviously)
  142. QWidget::restoreGeometry(geometry);
  143. }
  144. void GRenderWindow::restoreGeometry(const QByteArray& geometry) {
  145. // Make sure users of this class don't need to deal with backing up the geometry themselves
  146. QWidget::restoreGeometry(geometry);
  147. BackupGeometry();
  148. }
  149. QByteArray GRenderWindow::saveGeometry() {
  150. // If we are a top-level widget, store the current geometry
  151. // otherwise, store the last backup
  152. if (parent() == nullptr)
  153. return ((QGLWidget*)this)->saveGeometry();
  154. else
  155. return geometry;
  156. }
  157. qreal GRenderWindow::windowPixelRatio() {
  158. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  159. // windowHandle() might not be accessible until the window is displayed to screen.
  160. return windowHandle() ? windowHandle()->screen()->devicePixelRatio() : 1.0f;
  161. #else
  162. return 1.0f;
  163. #endif
  164. }
  165. void GRenderWindow::closeEvent(QCloseEvent* event) {
  166. motion_emu = nullptr;
  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. motion_emu->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. motion_emu->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. motion_emu->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. fmt.setSwapInterval(Settings::values.use_vsync);
  219. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  220. fmt.setOption(QGL::NoDeprecatedFunctions);
  221. child = new GGLWidgetInternal(fmt, this);
  222. QBoxLayout* layout = new QHBoxLayout(this);
  223. resize(Core::kScreenTopWidth, Core::kScreenTopHeight + Core::kScreenBottomHeight);
  224. layout->addWidget(child);
  225. layout->setMargin(0);
  226. setLayout(layout);
  227. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  228. OnFramebufferSizeChanged();
  229. NotifyClientAreaSizeChanged(std::pair<unsigned, unsigned>(child->width(), child->height()));
  230. BackupGeometry();
  231. }
  232. void GRenderWindow::OnMinimalClientAreaChangeRequest(
  233. const std::pair<unsigned, unsigned>& minimal_size) {
  234. setMinimumSize(minimal_size.first, minimal_size.second);
  235. }
  236. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  237. motion_emu = std::make_unique<Motion::MotionEmu>(*this);
  238. this->emu_thread = emu_thread;
  239. child->DisablePainting();
  240. }
  241. void GRenderWindow::OnEmulationStopping() {
  242. motion_emu = nullptr;
  243. emu_thread = nullptr;
  244. child->EnablePainting();
  245. }
  246. void GRenderWindow::showEvent(QShowEvent* event) {
  247. QWidget::showEvent(event);
  248. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  249. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  250. connect(this->windowHandle(), SIGNAL(screenChanged(QScreen*)), this,
  251. SLOT(OnFramebufferSizeChanged()), Qt::UniqueConnection);
  252. #endif
  253. }