bootmanager.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-FileCopyrightText: 2014 Citra 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 <QWidget>
  18. #include <qglobal.h>
  19. #include <qnamespace.h>
  20. #include <qobjectdefs.h>
  21. #include "common/common_types.h"
  22. #include "common/logging/log.h"
  23. #include "common/polyfill_thread.h"
  24. #include "common/thread.h"
  25. #include "core/frontend/emu_window.h"
  26. class GMainWindow;
  27. class QCamera;
  28. class QCameraImageCapture;
  29. class QCloseEvent;
  30. class QFocusEvent;
  31. class QKeyEvent;
  32. class QMouseEvent;
  33. class QObject;
  34. class QResizeEvent;
  35. class QShowEvent;
  36. class QTimer;
  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 resizeEvent(QResizeEvent* event) override;
  141. /// Converts a Qt keybard key into NativeKeyboard key
  142. static int QtKeyToSwitchKey(Qt::Key qt_keys);
  143. /// Converts a Qt modifier keys into NativeKeyboard modifier keys
  144. static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
  145. void keyPressEvent(QKeyEvent* event) override;
  146. void keyReleaseEvent(QKeyEvent* event) override;
  147. /// Converts a Qt mouse button into MouseInput mouse button
  148. static InputCommon::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  149. void mousePressEvent(QMouseEvent* event) override;
  150. void mouseMoveEvent(QMouseEvent* event) override;
  151. void mouseReleaseEvent(QMouseEvent* event) override;
  152. void wheelEvent(QWheelEvent* event) override;
  153. void InitializeCamera();
  154. void FinalizeCamera();
  155. bool event(QEvent* event) override;
  156. void focusOutEvent(QFocusEvent* event) override;
  157. bool InitRenderTarget();
  158. /// Destroy the previous run's child_widget which should also destroy the child_window
  159. void ReleaseRenderTarget();
  160. bool IsLoadingComplete() const;
  161. void CaptureScreenshot(const QString& screenshot_path);
  162. /**
  163. * Instructs the window to re-launch the application using the specified program_index.
  164. * @param program_index Specifies the index within the application of the program to launch.
  165. */
  166. void ExecuteProgram(std::size_t program_index);
  167. /// Instructs the window to exit the application.
  168. void Exit();
  169. public slots:
  170. void OnEmulationStarting(EmuThread* emu_thread_);
  171. void OnEmulationStopping();
  172. void OnFramebufferSizeChanged();
  173. signals:
  174. /// Emitted when the window is closed
  175. void Closed();
  176. void FirstFrameDisplayed();
  177. void ExecuteProgramSignal(std::size_t program_index);
  178. void ExitSignal();
  179. void MouseActivity();
  180. void TasPlaybackStateChanged();
  181. private:
  182. void TouchBeginEvent(const QTouchEvent* event);
  183. void TouchUpdateEvent(const QTouchEvent* event);
  184. void TouchEndEvent();
  185. void RequestCameraCapture();
  186. void OnCameraCapture(int requestId, const QImage& img);
  187. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  188. bool InitializeOpenGL();
  189. bool InitializeVulkan();
  190. void InitializeNull();
  191. bool LoadOpenGL();
  192. QStringList GetUnsupportedGLExtensions() const;
  193. EmuThread* emu_thread;
  194. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem;
  195. // Main context that will be shared with all other contexts that are requested.
  196. // If this is used in a shared context setting, then this should not be used directly, but
  197. // should instead be shared from
  198. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  199. /// Temporary storage of the screenshot taken
  200. QImage screenshot_image;
  201. QByteArray geometry;
  202. QWidget* child_widget = nullptr;
  203. bool first_frame = false;
  204. InputCommon::TasInput::TasState last_tas_state;
  205. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && YUZU_USE_QT_MULTIMEDIA
  206. bool is_virtual_camera;
  207. int pending_camera_snapshots;
  208. std::vector<u32> camera_data;
  209. std::unique_ptr<QCamera> camera;
  210. std::unique_ptr<QCameraImageCapture> camera_capture;
  211. std::unique_ptr<QTimer> camera_timer;
  212. #endif
  213. Core::System& system;
  214. protected:
  215. void showEvent(QShowEvent* event) override;
  216. bool eventFilter(QObject* object, QEvent* event) override;
  217. };