bootmanager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project & 2024 suyu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <condition_variable>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <mutex>
  8. #include <utility>
  9. #include <vector>
  10. #include <QByteArray>
  11. #include <QImage>
  12. #include <QObject>
  13. #include <QPoint>
  14. #include <QString>
  15. #include <QStringList>
  16. #include <QThread>
  17. #include <QTimer>
  18. #include <QWidget>
  19. #include <qglobal.h>
  20. #include <qnamespace.h>
  21. #include <qobjectdefs.h>
  22. #include "common/common_types.h"
  23. #include "common/logging/log.h"
  24. #include "common/polyfill_thread.h"
  25. #include "common/thread.h"
  26. #include "core/frontend/emu_window.h"
  27. class GMainWindow;
  28. class QCamera;
  29. class QCameraImageCapture;
  30. class QCloseEvent;
  31. class QFocusEvent;
  32. class QKeyEvent;
  33. class QMouseEvent;
  34. class QObject;
  35. class QResizeEvent;
  36. class QShowEvent;
  37. class QTouchEvent;
  38. class QWheelEvent;
  39. namespace Core {
  40. class System;
  41. } // namespace Core
  42. namespace InputCommon {
  43. class InputSubsystem;
  44. enum class MouseButton;
  45. } // namespace InputCommon
  46. namespace InputCommon::TasInput {
  47. enum class TasState;
  48. } // namespace InputCommon::TasInput
  49. namespace VideoCore {
  50. enum class LoadCallbackStage;
  51. } // namespace VideoCore
  52. class EmuThread final : public QThread {
  53. Q_OBJECT
  54. public:
  55. explicit EmuThread(Core::System& system);
  56. ~EmuThread() override;
  57. /**
  58. * Start emulation (on new thread)
  59. * @warning Only call when not running!
  60. */
  61. void run() override;
  62. /**
  63. * Sets whether the emulation thread should run or not
  64. * @param should_run Boolean value, set the emulation thread to running if true
  65. */
  66. void SetRunning(bool should_run) {
  67. // TODO: Prevent other threads from modifying the state until we finish.
  68. {
  69. // Notify the running thread to change state.
  70. std::unique_lock run_lk{m_should_run_mutex};
  71. m_should_run = should_run;
  72. m_should_run_cv.notify_one();
  73. }
  74. // Wait until paused, if pausing.
  75. if (!should_run) {
  76. m_stopped.Wait();
  77. }
  78. }
  79. /**
  80. * Check if the emulation thread is running or not
  81. * @return True if the emulation thread is running, otherwise false
  82. */
  83. bool IsRunning() const {
  84. return m_should_run;
  85. }
  86. /**
  87. * Requests for the emulation thread to immediately stop running
  88. */
  89. void ForceStop() {
  90. LOG_WARNING(Frontend, "Force stopping EmuThread");
  91. m_stop_source.request_stop();
  92. }
  93. private:
  94. void EmulationPaused(std::unique_lock<std::mutex>& lk);
  95. void EmulationResumed(std::unique_lock<std::mutex>& lk);
  96. private:
  97. Core::System& m_system;
  98. std::stop_source m_stop_source;
  99. std::mutex m_should_run_mutex;
  100. std::condition_variable_any m_should_run_cv;
  101. Common::Event m_stopped;
  102. bool m_should_run{true};
  103. signals:
  104. /**
  105. * Emitted when the CPU has halted execution
  106. *
  107. * @warning When connecting to this signal from other threads, make sure to specify either
  108. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  109. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  110. */
  111. void DebugModeEntered();
  112. /**
  113. * Emitted right before the CPU continues execution
  114. *
  115. * @warning When connecting to this signal from other threads, make sure to specify either
  116. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  117. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  118. */
  119. void DebugModeLeft();
  120. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  121. };
  122. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  123. Q_OBJECT
  124. public:
  125. explicit GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  126. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_,
  127. Core::System& system_);
  128. ~GRenderWindow() override;
  129. // EmuWindow implementation.
  130. void OnFrameDisplayed() override;
  131. bool IsShown() const override;
  132. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  133. void BackupGeometry();
  134. void RestoreGeometry();
  135. void restoreGeometry(const QByteArray& geometry_); // overridden
  136. QByteArray saveGeometry(); // overridden
  137. qreal windowPixelRatio() const;
  138. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  139. void closeEvent(QCloseEvent* event) override;
  140. void leaveEvent(QEvent* event) override;
  141. void resizeEvent(QResizeEvent* event) override;
  142. /// Converts a Qt keyboard key into NativeKeyboard key
  143. static int QtKeyToSwitchKey(Qt::Key qt_keys);
  144. /// Converts a Qt modifier keys into NativeKeyboard modifier keys
  145. static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
  146. void keyPressEvent(QKeyEvent* event) override;
  147. void keyReleaseEvent(QKeyEvent* event) override;
  148. /// Converts a Qt mouse button into MouseInput mouse button
  149. static InputCommon::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  150. void mousePressEvent(QMouseEvent* event) override;
  151. void mouseMoveEvent(QMouseEvent* event) override;
  152. void mouseReleaseEvent(QMouseEvent* event) override;
  153. void wheelEvent(QWheelEvent* event) override;
  154. void InitializeCamera();
  155. void FinalizeCamera();
  156. bool event(QEvent* event) override;
  157. void focusOutEvent(QFocusEvent* event) override;
  158. bool InitRenderTarget();
  159. /// Destroy the previous run's child_widget which should also destroy the child_window
  160. void ReleaseRenderTarget();
  161. bool IsLoadingComplete() const;
  162. void CaptureScreenshot(const QString& screenshot_path);
  163. /**
  164. * Instructs the window to re-launch the application using the specified program_index.
  165. * @param program_index Specifies the index within the application of the program to launch.
  166. */
  167. void ExecuteProgram(std::size_t program_index);
  168. /// Instructs the window to exit the application.
  169. void Exit();
  170. public slots:
  171. void OnEmulationStarting(EmuThread* emu_thread_);
  172. void OnEmulationStopping();
  173. void OnFramebufferSizeChanged();
  174. signals:
  175. /// Emitted when the window is closed
  176. void Closed();
  177. void FirstFrameDisplayed();
  178. void ExecuteProgramSignal(std::size_t program_index);
  179. void ExitSignal();
  180. void MouseActivity();
  181. void TasPlaybackStateChanged();
  182. private:
  183. void TouchBeginEvent(const QTouchEvent* event);
  184. void TouchUpdateEvent(const QTouchEvent* event);
  185. void TouchEndEvent();
  186. void ConstrainMouse();
  187. void RequestCameraCapture();
  188. void OnCameraCapture(int requestId, const QImage& img);
  189. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  190. bool InitializeOpenGL();
  191. bool InitializeVulkan();
  192. void InitializeNull();
  193. bool LoadOpenGL();
  194. QStringList GetUnsupportedGLExtensions() const;
  195. EmuThread* emu_thread;
  196. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  197. // Main context that will be shared with all other contexts that are requested.
  198. // If this is used in a shared context setting, then this should not be used directly, but
  199. // should instead be shared from
  200. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  201. /// Temporary storage of the screenshot taken
  202. QImage screenshot_image;
  203. QByteArray geometry;
  204. QWidget* child_widget = nullptr;
  205. bool first_frame = false;
  206. InputCommon::TasInput::TasState last_tas_state;
  207. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && SUYU_USE_QT_MULTIMEDIA
  208. bool is_virtual_camera;
  209. int pending_camera_snapshots;
  210. std::vector<u32> camera_data;
  211. std::unique_ptr<QCamera> camera;
  212. std::unique_ptr<QCameraImageCapture> camera_capture;
  213. std::unique_ptr<QTimer> camera_timer;
  214. #endif
  215. QTimer mouse_constrain_timer;
  216. Core::System& system;
  217. protected:
  218. void showEvent(QShowEvent* event) override;
  219. bool eventFilter(QObject* object, QEvent* event) override;
  220. };