bootmanager.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. void EmulationPaused(std::unique_lock<std::mutex>& lk);
  79. void EmulationResumed(std::unique_lock<std::mutex>& lk);
  80. private:
  81. Core::System& m_system;
  82. std::stop_source m_stop_source;
  83. std::mutex m_should_run_mutex;
  84. std::condition_variable_any m_should_run_cv;
  85. std::atomic<bool> m_is_running{false};
  86. bool m_should_run{true};
  87. signals:
  88. /**
  89. * Emitted when the CPU has halted execution
  90. *
  91. * @warning When connecting to this signal from other threads, make sure to specify either
  92. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  93. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  94. */
  95. void DebugModeEntered();
  96. /**
  97. * Emitted right before the CPU continues execution
  98. *
  99. * @warning When connecting to this signal from other threads, make sure to specify either
  100. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  101. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  102. */
  103. void DebugModeLeft();
  104. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  105. };
  106. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  107. Q_OBJECT
  108. public:
  109. explicit GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  110. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_,
  111. Core::System& system_);
  112. ~GRenderWindow() override;
  113. // EmuWindow implementation.
  114. void OnFrameDisplayed() override;
  115. bool IsShown() const override;
  116. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  117. void BackupGeometry();
  118. void RestoreGeometry();
  119. void restoreGeometry(const QByteArray& geometry_); // overridden
  120. QByteArray saveGeometry(); // overridden
  121. qreal windowPixelRatio() const;
  122. void closeEvent(QCloseEvent* event) override;
  123. void resizeEvent(QResizeEvent* event) override;
  124. /// Converts a Qt keybard key into NativeKeyboard key
  125. static int QtKeyToSwitchKey(Qt::Key qt_keys);
  126. /// Converts a Qt modifier keys into NativeKeyboard modifier keys
  127. static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
  128. void keyPressEvent(QKeyEvent* event) override;
  129. void keyReleaseEvent(QKeyEvent* event) override;
  130. /// Converts a Qt mouse button into MouseInput mouse button
  131. static InputCommon::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  132. void mousePressEvent(QMouseEvent* event) override;
  133. void mouseMoveEvent(QMouseEvent* event) override;
  134. void mouseReleaseEvent(QMouseEvent* event) override;
  135. void wheelEvent(QWheelEvent* event) override;
  136. void InitializeCamera();
  137. void FinalizeCamera();
  138. bool event(QEvent* event) override;
  139. void focusOutEvent(QFocusEvent* event) override;
  140. bool InitRenderTarget();
  141. /// Destroy the previous run's child_widget which should also destroy the child_window
  142. void ReleaseRenderTarget();
  143. bool IsLoadingComplete() const;
  144. void CaptureScreenshot(const QString& screenshot_path);
  145. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  146. /**
  147. * Instructs the window to re-launch the application using the specified program_index.
  148. * @param program_index Specifies the index within the application of the program to launch.
  149. */
  150. void ExecuteProgram(std::size_t program_index);
  151. /// Instructs the window to exit the application.
  152. void Exit();
  153. public slots:
  154. void OnEmulationStarting(EmuThread* emu_thread_);
  155. void OnEmulationStopping();
  156. void OnFramebufferSizeChanged();
  157. signals:
  158. /// Emitted when the window is closed
  159. void Closed();
  160. void FirstFrameDisplayed();
  161. void ExecuteProgramSignal(std::size_t program_index);
  162. void ExitSignal();
  163. void MouseActivity();
  164. void TasPlaybackStateChanged();
  165. private:
  166. void TouchBeginEvent(const QTouchEvent* event);
  167. void TouchUpdateEvent(const QTouchEvent* event);
  168. void TouchEndEvent();
  169. void RequestCameraCapture();
  170. void OnCameraCapture(int requestId, const QImage& img);
  171. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  172. bool InitializeOpenGL();
  173. bool InitializeVulkan();
  174. void InitializeNull();
  175. bool LoadOpenGL();
  176. QStringList GetUnsupportedGLExtensions() const;
  177. EmuThread* emu_thread;
  178. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  179. // Main context that will be shared with all other contexts that are requested.
  180. // If this is used in a shared context setting, then this should not be used directly, but
  181. // should instead be shared from
  182. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  183. /// Temporary storage of the screenshot taken
  184. QImage screenshot_image;
  185. QByteArray geometry;
  186. QWidget* child_widget = nullptr;
  187. bool first_frame = false;
  188. InputCommon::TasInput::TasState last_tas_state;
  189. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  190. bool is_virtual_camera;
  191. int pending_camera_snapshots;
  192. std::vector<u32> camera_data;
  193. std::unique_ptr<QCamera> camera;
  194. std::unique_ptr<QCameraImageCapture> camera_capture;
  195. std::unique_ptr<QTimer> camera_timer;
  196. #endif
  197. Core::System& system;
  198. protected:
  199. void showEvent(QShowEvent* event) override;
  200. bool eventFilter(QObject* object, QEvent* event) override;
  201. };