bootmanager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/key_map.h"
  11. #include "common/microprofile.h"
  12. #include "common/scm_rev.h"
  13. #include "common/string_util.h"
  14. #include "core/core.h"
  15. #include "core/settings.h"
  16. #include "core/system.h"
  17. #include "video_core/debug_utils/debug_utils.h"
  18. #include "video_core/video_core.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::RunLoop();
  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::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. System::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), keyboard_id(0), emu_thread(emu_thread), child(nullptr) {
  85. std::string window_title =
  86. Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  87. setWindowTitle(QString::fromStdString(window_title));
  88. keyboard_id = KeyMap::NewDeviceId();
  89. ReloadSetKeymaps();
  90. }
  91. void GRenderWindow::moveContext() {
  92. DoneCurrent();
  93. // We need to move GL context to the swapping thread in Qt5
  94. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  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. #endif
  102. }
  103. void GRenderWindow::SwapBuffers() {
  104. #if !defined(QT_NO_DEBUG)
  105. // Qt debug runtime prints a bogus warning on the console if you haven't called makeCurrent
  106. // since the last time you called swapBuffers. This presumably means something if you're using
  107. // QGLWidget the "regular" way, but in our multi-threaded use case is harmless since we never
  108. // call doneCurrent in this thread.
  109. child->makeCurrent();
  110. #endif
  111. child->swapBuffers();
  112. }
  113. void GRenderWindow::MakeCurrent() {
  114. child->makeCurrent();
  115. }
  116. void GRenderWindow::DoneCurrent() {
  117. child->doneCurrent();
  118. }
  119. void GRenderWindow::PollEvents() {}
  120. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  121. //
  122. // Older versions get the window size (density independent pixels),
  123. // and hence, do not support DPI scaling ("retina" displays).
  124. // The result will be a viewport that is smaller than the extent of the window.
  125. void GRenderWindow::OnFramebufferSizeChanged() {
  126. // Screen changes potentially incur a change in screen DPI, hence we should update the
  127. // framebuffer size
  128. qreal pixelRatio = windowPixelRatio();
  129. unsigned width = child->QPaintDevice::width() * pixelRatio;
  130. unsigned height = child->QPaintDevice::height() * pixelRatio;
  131. UpdateCurrentFramebufferLayout(width, height);
  132. }
  133. void GRenderWindow::BackupGeometry() {
  134. geometry = ((QGLWidget*)this)->saveGeometry();
  135. }
  136. void GRenderWindow::RestoreGeometry() {
  137. // We don't want to back up the geometry here (obviously)
  138. QWidget::restoreGeometry(geometry);
  139. }
  140. void GRenderWindow::restoreGeometry(const QByteArray& geometry) {
  141. // Make sure users of this class don't need to deal with backing up the geometry themselves
  142. QWidget::restoreGeometry(geometry);
  143. BackupGeometry();
  144. }
  145. QByteArray GRenderWindow::saveGeometry() {
  146. // If we are a top-level widget, store the current geometry
  147. // otherwise, store the last backup
  148. if (parent() == nullptr)
  149. return ((QGLWidget*)this)->saveGeometry();
  150. else
  151. return geometry;
  152. }
  153. qreal GRenderWindow::windowPixelRatio() {
  154. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  155. // windowHandle() might not be accessible until the window is displayed to screen.
  156. return windowHandle() ? windowHandle()->screen()->devicePixelRatio() : 1.0f;
  157. #else
  158. return 1.0f;
  159. #endif
  160. }
  161. void GRenderWindow::closeEvent(QCloseEvent* event) {
  162. emit Closed();
  163. QWidget::closeEvent(event);
  164. }
  165. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  166. KeyMap::PressKey(*this, {event->key(), keyboard_id});
  167. }
  168. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  169. KeyMap::ReleaseKey(*this, {event->key(), keyboard_id});
  170. }
  171. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  172. if (event->button() == Qt::LeftButton) {
  173. auto pos = event->pos();
  174. qreal pixelRatio = windowPixelRatio();
  175. this->TouchPressed(static_cast<unsigned>(pos.x() * pixelRatio),
  176. static_cast<unsigned>(pos.y() * pixelRatio));
  177. }
  178. }
  179. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  180. auto pos = event->pos();
  181. qreal pixelRatio = windowPixelRatio();
  182. this->TouchMoved(std::max(static_cast<unsigned>(pos.x() * pixelRatio), 0u),
  183. std::max(static_cast<unsigned>(pos.y() * pixelRatio), 0u));
  184. }
  185. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  186. if (event->button() == Qt::LeftButton)
  187. this->TouchReleased();
  188. }
  189. void GRenderWindow::ReloadSetKeymaps() {
  190. KeyMap::ClearKeyMapping(keyboard_id);
  191. for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
  192. KeyMap::SetKeyMapping(
  193. {Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id},
  194. KeyMap::mapping_targets[i]);
  195. }
  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. fmt.setSwapInterval(Settings::values.use_vsync);
  213. // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X
  214. fmt.setOption(QGL::NoDeprecatedFunctions);
  215. child = new GGLWidgetInternal(fmt, this);
  216. QBoxLayout* layout = new QHBoxLayout(this);
  217. resize(VideoCore::kScreenTopWidth,
  218. VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
  219. layout->addWidget(child);
  220. layout->setMargin(0);
  221. setLayout(layout);
  222. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  223. OnFramebufferSizeChanged();
  224. NotifyClientAreaSizeChanged(std::pair<unsigned, unsigned>(child->width(), child->height()));
  225. BackupGeometry();
  226. }
  227. void GRenderWindow::OnMinimalClientAreaChangeRequest(
  228. const std::pair<unsigned, unsigned>& minimal_size) {
  229. setMinimumSize(minimal_size.first, minimal_size.second);
  230. }
  231. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  232. this->emu_thread = emu_thread;
  233. child->DisablePainting();
  234. }
  235. void GRenderWindow::OnEmulationStopping() {
  236. emu_thread = nullptr;
  237. child->EnablePainting();
  238. }
  239. void GRenderWindow::showEvent(QShowEvent* event) {
  240. QWidget::showEvent(event);
  241. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  242. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  243. connect(this->windowHandle(), SIGNAL(screenChanged(QScreen*)), this,
  244. SLOT(OnFramebufferSizeChanged()), Qt::UniqueConnection);
  245. #endif
  246. }