bootmanager.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <condition_variable>
  7. #include <memory>
  8. #include <mutex>
  9. #include <QImage>
  10. #include <QThread>
  11. #include <QTouchEvent>
  12. #include <QWidget>
  13. #include <QWindow>
  14. #include "common/thread.h"
  15. #include "core/core.h"
  16. #include "core/frontend/emu_window.h"
  17. class GRenderWindow;
  18. class GMainWindow;
  19. class QKeyEvent;
  20. class QStringList;
  21. namespace InputCommon {
  22. class InputSubsystem;
  23. }
  24. namespace MouseInput {
  25. enum class MouseButton;
  26. }
  27. namespace VideoCore {
  28. enum class LoadCallbackStage;
  29. }
  30. class EmuThread final : public QThread {
  31. Q_OBJECT
  32. public:
  33. explicit EmuThread();
  34. ~EmuThread() override;
  35. /**
  36. * Start emulation (on new thread)
  37. * @warning Only call when not running!
  38. */
  39. void run() override;
  40. /**
  41. * Steps the emulation thread by a single CPU instruction (if the CPU is not already running)
  42. * @note This function is thread-safe
  43. */
  44. void ExecStep() {
  45. exec_step = true;
  46. running_cv.notify_all();
  47. }
  48. /**
  49. * Sets whether the emulation thread is running or not
  50. * @param running Boolean value, set the emulation thread to running if true
  51. * @note This function is thread-safe
  52. */
  53. void SetRunning(bool running) {
  54. std::unique_lock lock{running_mutex};
  55. this->running = running;
  56. lock.unlock();
  57. running_cv.notify_all();
  58. if (!running) {
  59. running_wait.Set();
  60. /// Wait until effectively paused
  61. while (running_guard)
  62. ;
  63. }
  64. }
  65. /**
  66. * Check if the emulation thread is running or not
  67. * @return True if the emulation thread is running, otherwise false
  68. * @note This function is thread-safe
  69. */
  70. bool IsRunning() const {
  71. return running;
  72. }
  73. /**
  74. * Requests for the emulation thread to stop running
  75. */
  76. void RequestStop() {
  77. stop_run = true;
  78. SetRunning(false);
  79. }
  80. private:
  81. bool exec_step = false;
  82. bool running = false;
  83. std::atomic_bool stop_run{false};
  84. std::mutex running_mutex;
  85. std::condition_variable running_cv;
  86. Common::Event running_wait{};
  87. std::atomic_bool running_guard{false};
  88. signals:
  89. /**
  90. * Emitted when the CPU has halted execution
  91. *
  92. * @warning When connecting to this signal from other threads, make sure to specify either
  93. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  94. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  95. */
  96. void DebugModeEntered();
  97. /**
  98. * Emitted right before the CPU continues execution
  99. *
  100. * @warning When connecting to this signal from other threads, make sure to specify either
  101. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  102. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  103. */
  104. void DebugModeLeft();
  105. void ErrorThrown(Core::System::ResultStatus, std::string);
  106. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  107. };
  108. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  109. Q_OBJECT
  110. public:
  111. explicit GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  112. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_);
  113. ~GRenderWindow() override;
  114. // EmuWindow implementation.
  115. void OnFrameDisplayed() override;
  116. bool IsShown() const override;
  117. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  118. void BackupGeometry();
  119. void RestoreGeometry();
  120. void restoreGeometry(const QByteArray& geometry); // overridden
  121. QByteArray saveGeometry(); // overridden
  122. qreal windowPixelRatio() const;
  123. void closeEvent(QCloseEvent* event) override;
  124. void resizeEvent(QResizeEvent* event) override;
  125. void keyPressEvent(QKeyEvent* event) override;
  126. void keyReleaseEvent(QKeyEvent* event) override;
  127. /// Converts a Qt mouse button into MouseInput mouse button
  128. static MouseInput::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  129. void mousePressEvent(QMouseEvent* event) override;
  130. void mouseMoveEvent(QMouseEvent* event) override;
  131. void mouseReleaseEvent(QMouseEvent* event) override;
  132. bool event(QEvent* event) override;
  133. void focusOutEvent(QFocusEvent* event) override;
  134. bool InitRenderTarget();
  135. /// Destroy the previous run's child_widget which should also destroy the child_window
  136. void ReleaseRenderTarget();
  137. bool IsLoadingComplete() const;
  138. void CaptureScreenshot(u32 res_scale, const QString& screenshot_path);
  139. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  140. /**
  141. * Instructs the window to re-launch the application using the specified program_index.
  142. * @param program_index Specifies the index within the application of the program to launch.
  143. */
  144. void ExecuteProgram(std::size_t program_index);
  145. public slots:
  146. void OnEmulationStarting(EmuThread* emu_thread);
  147. void OnEmulationStopping();
  148. void OnFramebufferSizeChanged();
  149. signals:
  150. /// Emitted when the window is closed
  151. void Closed();
  152. void FirstFrameDisplayed();
  153. void ExecuteProgramSignal(std::size_t program_index);
  154. void MouseActivity();
  155. private:
  156. void TouchBeginEvent(const QTouchEvent* event);
  157. void TouchUpdateEvent(const QTouchEvent* event);
  158. void TouchEndEvent();
  159. bool TouchStart(const QTouchEvent::TouchPoint& touch_point);
  160. bool TouchUpdate(const QTouchEvent::TouchPoint& touch_point);
  161. bool TouchExist(std::size_t id, const QList<QTouchEvent::TouchPoint>& touch_points) const;
  162. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  163. bool InitializeOpenGL();
  164. bool InitializeVulkan();
  165. bool LoadOpenGL();
  166. QStringList GetUnsupportedGLExtensions() const;
  167. EmuThread* emu_thread;
  168. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  169. // Main context that will be shared with all other contexts that are requested.
  170. // If this is used in a shared context setting, then this should not be used directly, but
  171. // should instead be shared from
  172. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  173. /// Temporary storage of the screenshot taken
  174. QImage screenshot_image;
  175. QByteArray geometry;
  176. QWidget* child_widget = nullptr;
  177. bool first_frame = false;
  178. std::array<std::size_t, 16> touch_ids{};
  179. protected:
  180. void showEvent(QShowEvent* event) override;
  181. bool eventFilter(QObject* object, QEvent* event) override;
  182. };