bootmanager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <condition_variable>
  7. #include <mutex>
  8. #include <QImage>
  9. #include <QThread>
  10. #include <QWidget>
  11. #include <QWindow>
  12. #include "common/thread.h"
  13. #include "core/core.h"
  14. #include "core/frontend/emu_window.h"
  15. class GRenderWindow;
  16. class GMainWindow;
  17. class QKeyEvent;
  18. class QTouchEvent;
  19. class QStringList;
  20. namespace VideoCore {
  21. enum class LoadCallbackStage;
  22. }
  23. class EmuThread final : public QThread {
  24. Q_OBJECT
  25. public:
  26. explicit EmuThread();
  27. ~EmuThread() override;
  28. /**
  29. * Start emulation (on new thread)
  30. * @warning Only call when not running!
  31. */
  32. void run() override;
  33. /**
  34. * Steps the emulation thread by a single CPU instruction (if the CPU is not already running)
  35. * @note This function is thread-safe
  36. */
  37. void ExecStep() {
  38. exec_step = true;
  39. running_cv.notify_all();
  40. }
  41. /**
  42. * Sets whether the emulation thread is running or not
  43. * @param running Boolean value, set the emulation thread to running if true
  44. * @note This function is thread-safe
  45. */
  46. void SetRunning(bool running) {
  47. std::unique_lock lock{running_mutex};
  48. this->running = running;
  49. lock.unlock();
  50. running_cv.notify_all();
  51. }
  52. /**
  53. * Check if the emulation thread is running or not
  54. * @return True if the emulation thread is running, otherwise false
  55. * @note This function is thread-safe
  56. */
  57. bool IsRunning() const {
  58. return running;
  59. }
  60. /**
  61. * Requests for the emulation thread to stop running
  62. */
  63. void RequestStop() {
  64. stop_run = true;
  65. SetRunning(false);
  66. }
  67. private:
  68. bool exec_step = false;
  69. bool running = false;
  70. std::atomic_bool stop_run{false};
  71. std::mutex running_mutex;
  72. std::condition_variable running_cv;
  73. signals:
  74. /**
  75. * Emitted when the CPU has halted execution
  76. *
  77. * @warning When connecting to this signal from other threads, make sure to specify either
  78. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  79. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  80. */
  81. void DebugModeEntered();
  82. /**
  83. * Emitted right before the CPU continues execution
  84. *
  85. * @warning When connecting to this signal from other threads, make sure to specify either
  86. * Qt::QueuedConnection (invoke slot within the destination object's message thread) or even
  87. * Qt::BlockingQueuedConnection (additionally block source thread until slot returns)
  88. */
  89. void DebugModeLeft();
  90. void ErrorThrown(Core::System::ResultStatus, std::string);
  91. void LoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total);
  92. };
  93. class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow {
  94. Q_OBJECT
  95. public:
  96. GRenderWindow(GMainWindow* parent, EmuThread* emu_thread);
  97. ~GRenderWindow() override;
  98. // EmuWindow implementation.
  99. void PollEvents() override;
  100. bool IsShown() const override;
  101. std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
  102. void BackupGeometry();
  103. void RestoreGeometry();
  104. void restoreGeometry(const QByteArray& geometry); // overridden
  105. QByteArray saveGeometry(); // overridden
  106. qreal windowPixelRatio() const;
  107. void closeEvent(QCloseEvent* event) override;
  108. void resizeEvent(QResizeEvent* event) override;
  109. void keyPressEvent(QKeyEvent* event) override;
  110. void keyReleaseEvent(QKeyEvent* event) override;
  111. void mousePressEvent(QMouseEvent* event) override;
  112. void mouseMoveEvent(QMouseEvent* event) override;
  113. void mouseReleaseEvent(QMouseEvent* event) override;
  114. bool event(QEvent* event) override;
  115. void focusOutEvent(QFocusEvent* event) override;
  116. bool InitRenderTarget();
  117. /// Destroy the previous run's child_widget which should also destroy the child_window
  118. void ReleaseRenderTarget();
  119. void CaptureScreenshot(u32 res_scale, const QString& screenshot_path);
  120. std::pair<u32, u32> ScaleTouch(const QPointF& pos) const;
  121. public slots:
  122. void OnEmulationStarting(EmuThread* emu_thread);
  123. void OnEmulationStopping();
  124. void OnFramebufferSizeChanged();
  125. signals:
  126. /// Emitted when the window is closed
  127. void Closed();
  128. void FirstFrameDisplayed();
  129. private:
  130. void TouchBeginEvent(const QTouchEvent* event);
  131. void TouchUpdateEvent(const QTouchEvent* event);
  132. void TouchEndEvent();
  133. void OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) override;
  134. bool InitializeOpenGL();
  135. bool InitializeVulkan();
  136. bool LoadOpenGL();
  137. QStringList GetUnsupportedGLExtensions() const;
  138. EmuThread* emu_thread;
  139. // Main context that will be shared with all other contexts that are requested.
  140. // If this is used in a shared context setting, then this should not be used directly, but
  141. // should instead be shared from
  142. std::shared_ptr<Core::Frontend::GraphicsContext> main_context;
  143. /// Temporary storage of the screenshot taken
  144. QImage screenshot_image;
  145. QByteArray geometry;
  146. QWidget* child_widget = nullptr;
  147. bool first_frame = false;
  148. protected:
  149. void showEvent(QShowEvent* event) override;
  150. };