bootmanager.h 7.6 KB

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