bootmanager.h 7.5 KB

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