bootmanager.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 is running or not
  48. * @param running_ Boolean value, set the emulation thread to running if true
  49. * @note This function is thread-safe
  50. */
  51. void SetRunning(bool running_) {
  52. std::unique_lock lock{running_mutex};
  53. running = running_;
  54. lock.unlock();
  55. running_cv.notify_all();
  56. if (!running) {
  57. running_wait.Set();
  58. /// Wait until effectively paused
  59. while (running_guard)
  60. ;
  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. * @note This function is thread-safe
  67. */
  68. bool IsRunning() const {
  69. return running;
  70. }
  71. /**
  72. * Requests for the emulation thread to immediately stop running
  73. */
  74. void ForceStop() {
  75. LOG_WARNING(Frontend, "Force stopping EmuThread");
  76. stop_source.request_stop();
  77. SetRunning(false);
  78. }
  79. private:
  80. bool running = false;
  81. std::stop_source stop_source;
  82. std::mutex running_mutex;
  83. std::condition_variable_any running_cv;
  84. Common::Event running_wait{};
  85. std::atomic_bool running_guard{false};
  86. Core::System& system;
  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 ErrorThrown(Core::SystemResultStatus, std::string);
  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. void closeEvent(QCloseEvent* event) override;
  124. void resizeEvent(QResizeEvent* event) override;
  125. /// Converts a Qt keybard key into NativeKeyboard key
  126. static int QtKeyToSwitchKey(Qt::Key qt_keys);
  127. /// Converts a Qt modifier keys into NativeKeyboard modifier keys
  128. static int QtModifierToSwitchModifier(Qt::KeyboardModifiers qt_modifiers);
  129. void keyPressEvent(QKeyEvent* event) override;
  130. void keyReleaseEvent(QKeyEvent* event) override;
  131. /// Converts a Qt mouse button into MouseInput mouse button
  132. static InputCommon::MouseButton QtButtonToMouseButton(Qt::MouseButton button);
  133. void mousePressEvent(QMouseEvent* event) override;
  134. void mouseMoveEvent(QMouseEvent* event) override;
  135. void mouseReleaseEvent(QMouseEvent* event) override;
  136. void wheelEvent(QWheelEvent* event) override;
  137. void InitializeCamera();
  138. void FinalizeCamera();
  139. bool event(QEvent* event) override;
  140. void focusOutEvent(QFocusEvent* event) override;
  141. bool InitRenderTarget();
  142. /// Destroy the previous run's child_widget which should also destroy the child_window
  143. void ReleaseRenderTarget();
  144. bool IsLoadingComplete() const;
  145. void CaptureScreenshot(const QString& screenshot_path);
  146. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  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. };